@@ -9,6 +9,7 @@ package typechecker
99import scala .collection .{ mutable , immutable }
1010import scala .util .control .ControlThrowable
1111import symtab .Flags ._
12+ import scala .reflect .internal .Depth
1213
1314/** This trait contains methods related to type parameter inference.
1415 *
@@ -21,6 +22,7 @@ trait Infer extends Checkable {
2122 import global ._
2223 import definitions ._
2324 import typeDebug .ptBlock
25+ import typeDebug .str .parentheses
2426 import typingStack .{ printTyping }
2527
2628 /** The formal parameter types corresponding to `formals`.
@@ -132,34 +134,17 @@ trait Infer extends Checkable {
132134 * @param upper When `true` search for max solution else min.
133135 * @throws NoInstance
134136 */
135- def solvedTypes (tvars : List [TypeVar ], tparams : List [Symbol ],
136- variances : List [Variance ], upper : Boolean , depth : Int ): List [Type ] = {
137-
138- if (tvars.nonEmpty) {
139- def tp_s = (tparams, tvars).zipped map { case (tp, tv) => s " ${tp.name}/ $tv" } mkString " ,"
140- printTyping(s " solving for $tp_s" )
141- }
142-
143- if (! solve(tvars, tparams, variances, upper, depth)) {
144- // no panic, it's good enough to just guess a solution, we'll find out
145- // later whether it works. *ZAP* @M danger, Will Robinson! this means
146- // that you should never trust inferred type arguments!
147- //
148- // Need to call checkBounds on the args/typars or type1 on the tree
149- // for the expression that results from type inference see e.g., #2421:
150- // implicit search had been ignoring this caveat
151- // throw new DeferredNoInstance(() =>
152- // "no solution exists for constraints"+(tvars map boundsString))
137+ def solvedTypes (tvars : List [TypeVar ], tparams : List [Symbol ], variances : List [Variance ], upper : Boolean , depth : Depth ): List [Type ] = {
138+ if (tvars.isEmpty) Nil else {
139+ printTyping(" solving for " + parentheses((tparams, tvars).zipped map ((p, tv) => s " ${p.name}: $tv" )))
140+ // !!! What should be done with the return value of "solve", which is at present ignored?
141+ // The historical commentary says "no panic, it's good enough to just guess a solution,
142+ // we'll find out later whether it works", meaning don't issue an error here when types
143+ // don't conform to bounds. That means you can never trust the results of implicit search.
144+ // For an example where this was not being heeded, SI-2421.
145+ solve(tvars, tparams, variances, upper, depth)
146+ tvars map instantiate
153147 }
154- for (tvar <- tvars ; if tvar.constr.inst == tvar) {
155- if (tvar.origin.typeSymbol.info eq ErrorType )
156- // this can happen if during solving a cyclic type parameter
157- // such as T <: T gets completed. See #360
158- tvar.constr.inst = ErrorType
159- else
160- abort(tvar.origin+ " at " + tvar.origin.typeSymbol.owner)
161- }
162- tvars map instantiate
163148 }
164149
165150 def skipImplicit (tp : Type ) = tp match {
@@ -554,10 +539,7 @@ trait Infer extends Checkable {
554539 " argument expression's type is not compatible with formal parameter type" + foundReqMsg(tp1, pt1))
555540 }
556541 }
557- val targs = solvedTypes(
558- tvars, tparams, tparams map varianceInTypes(formals),
559- upper = false , lubDepth(formals) max lubDepth(argtpes)
560- )
542+ val targs = solvedTypes(tvars, tparams, tparams map varianceInTypes(formals), upper = false , lubDepth(formals) max lubDepth(argtpes))
561543 // Can warn about inferring Any/AnyVal as long as they don't appear
562544 // explicitly anywhere amongst the formal, argument, result, or expected type.
563545 def canWarnAboutAny = ! (pt :: restpe :: formals ::: argtpes exists (t => (t contains AnyClass ) || (t contains AnyValClass )))
@@ -1030,7 +1012,10 @@ trait Infer extends Checkable {
10301012 val variances =
10311013 if (ctorTp.paramTypes.isEmpty) undetparams map varianceInType(ctorTp)
10321014 else undetparams map varianceInTypes(ctorTp.paramTypes)
1033- val targs = solvedTypes(tvars, undetparams, variances, upper = true , lubDepth(List (resTp, pt)))
1015+
1016+ // Note: this is the only place where solvedTypes (or, indirectly, solve) is called
1017+ // with upper = true.
1018+ val targs = solvedTypes(tvars, undetparams, variances, upper = true , lubDepth(resTp :: pt :: Nil ))
10341019 // checkBounds(tree, NoPrefix, NoSymbol, undetparams, targs, "inferred ")
10351020 // no checkBounds here. If we enable it, test bug602 fails.
10361021 // TODO: reinstate checkBounds, return params that fail to meet their bounds to undetparams
@@ -1099,7 +1084,7 @@ trait Infer extends Checkable {
10991084 val tvars1 = tvars map (_.cloneInternal)
11001085 // Note: right now it's not clear that solving is complete, or how it can be made complete!
11011086 // So we should come back to this and investigate.
1102- solve(tvars1, tvars1 map (_.origin.typeSymbol), tvars1 map (_ => Variance .Covariant ), upper = false )
1087+ solve(tvars1, tvars1 map (_.origin.typeSymbol), tvars1 map (_ => Variance .Covariant ), upper = false , Depth . AnyDepth )
11031088 }
11041089
11051090 // this is quite nasty: it destructively changes the info of the syms of e.g., method type params
0 commit comments