@@ -139,7 +139,7 @@ object Types extends TypeUtils {
139139 case t : AppliedType =>
140140 t.fold(false , (x, tp) => x || test(tp, theAcc))
141141 case t : TypeVar =>
142- ! t.inst.exists || test(t.inst , theAcc)
142+ ! t.isPermanentlyInstantiated || test(t.permanentInst , theAcc)
143143 case t : LazyRef =>
144144 ! t.completed || test(t.ref, theAcc)
145145 case _ =>
@@ -4934,20 +4934,24 @@ object Types extends TypeUtils {
49344934 def setOrigin (p : TypeParamRef ) = currentOrigin = p
49354935
49364936 /** The permanent instance type of the variable, or NoType is none is given yet */
4937- private var myInst : Type = NoType
4937+ private var inst : Type = NoType
49384938
4939- private [core] def inst : Type = myInst
4940- private [core] def setInst (tp : Type ): Unit =
4941- myInst = tp
4939+ /** The permanent instance type that's stored in the type variable, so it cannot be retracted
4940+ * anymore, or NoType if the variable can still be further constrained or a provisional
4941+ * instance type in the constraint can be retracted.
4942+ */
4943+ private [core] def permanentInst = inst
4944+ private [core] def setPermanentInst (tp : Type ): Unit =
4945+ inst = tp
49424946 if tp.exists && owningState != null then
49434947 val owningState1 = owningState.uncheckedNN.get
49444948 if owningState1 != null then
49454949 owningState1.ownedVars -= this
49464950 owningState = null // no longer needed; null out to avoid a memory leak
49474951
49484952 private [core] def resetInst (ts : TyperState ): Unit =
4949- assert(myInst .exists)
4950- myInst = NoType
4953+ assert(inst .exists)
4954+ inst = NoType
49514955 owningState = new WeakReference (ts)
49524956
49534957 /** The state owning the variable. This is at first `creatorState`, but it can
@@ -4985,18 +4989,23 @@ object Types extends TypeUtils {
49854989 /** Is the variable already instantiated? */
49864990 def isInstantiated (using Context ): Boolean = instanceOpt.exists
49874991
4992+ /** Is the variable already instantiated so that the instance cannot be
4993+ * retracted anymore?
4994+ */
4995+ def isPermanentlyInstantiated : Boolean = inst.exists
4996+
49884997 /** Instantiate variable with given type */
49894998 def instantiateWith (tp : Type )(using Context ): Type = {
49904999 assert(tp ne this , i " self instantiation of $origin, constraint = ${ctx.typerState.constraint}" )
4991- assert(! myInst .exists, i " $origin is already instantiated to $myInst but we attempted to instantiate it to $tp" )
5000+ assert(! inst .exists, i " $origin is already instantiated to $inst but we attempted to instantiate it to $tp" )
49925001 typr.println(i " instantiating $this with $tp" )
49935002
49945003 if Config .checkConstraintsSatisfiable then
49955004 assert(currentEntry.bounds.contains(tp),
49965005 i " $origin is constrained to be $currentEntry but attempted to instantiate it to $tp" )
49975006
49985007 if ((ctx.typerState eq owningState.nn.get.uncheckedNN) && ! TypeComparer .subtypeCheckInProgress)
4999- setInst (tp)
5008+ setPermanentInst (tp)
50005009 ctx.typerState.constraint = ctx.typerState.constraint.replace(origin, tp)
50015010 tp
50025011 }
@@ -5013,8 +5022,8 @@ object Types extends TypeUtils {
50135022 */
50145023 def instantiate (fromBelow : Boolean )(using Context ): Type =
50155024 val tp = typeToInstantiateWith(fromBelow)
5016- if myInst .exists then // The line above might have triggered instantiation of the current type variable
5017- myInst
5025+ if inst .exists then // The line above might have triggered instantiation of the current type variable
5026+ inst
50185027 else
50195028 instantiateWith(tp)
50205029
0 commit comments