Skip to content

Commit 5d1e468

Browse files
oderskysmarter
authored andcommitted
Only instantiate toplevel typevars
1 parent 907950e commit 5d1e468

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

compiler/src/dotty/tools/dotc/typer/Inferencing.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ object Inferencing {
8181
if (depVars.nonEmpty) instantiateSelected(tp, depVars.toList)
8282
}
8383

84+
/** If `tp` is type variable with a lower bound in the current constraint,
85+
* instantiate it from below.
86+
*/
87+
def couldInstantiateTypeVar(tp: Type)(using Context): Boolean = tp.dealias match
88+
case tvar: TypeVar
89+
if !tvar.isInstantiated
90+
&& ctx.typerState.constraint.contains(tvar)
91+
&& tvar.hasLowerBound =>
92+
tvar.instantiate(fromBelow = true)
93+
true
94+
case _ =>
95+
false
96+
8497
/** The accumulator which forces type variables using the policy encoded in `force`
8598
* and returns whether the type is fully defined. The direction in which
8699
* a type variable is instantiated is determined as follows:

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import config.Printers.typr
1212
import ast.Trees._
1313
import NameOps._
1414
import ProtoTypes._
15-
import Inferencing.isFullyDefined
15+
import Inferencing.couldInstantiateTypeVar
1616
import collection.mutable
1717
import reporting._
1818
import Checking.{checkNoPrivateLeaks, checkNoWildcard}
@@ -160,7 +160,7 @@ trait TypeAssigner {
160160
TryDynamicCallType
161161
else if (qualType.isErroneous || name.toTermName == nme.ERROR)
162162
UnspecifiedErrorType
163-
else if !isFullyDefined(qualType, ForceDegree.none) && isFullyDefined(qualType, ForceDegree.failBottom) then
163+
else if couldInstantiateTypeVar(qualType) then
164164
// try again with more defined qualifier type
165165
selectionType(tree, qual1)
166166
else if (name == nme.CONSTRUCTOR)

0 commit comments

Comments
 (0)