@@ -4111,14 +4111,16 @@ object Types {
41114111 * @param origin The parameter that's tracked by the type variable.
41124112 * @param creatorState The typer state in which the variable was created.
41134113 */
4114- final class TypeVar ( private var _origin : TypeParamRef , creatorState : TyperState , level : Int ) extends CachedProxyType with ValueType {
4114+ final class TypeVar private ( initOrigin : TypeParamRef , creatorState : TyperState , nestingLevel : Int ) extends CachedProxyType with ValueType {
41154115
4116- def origin : TypeParamRef = _origin
4116+ private var currentOrigin = initOrigin
4117+
4118+ def origin : TypeParamRef = currentOrigin
41174119
41184120 /** Set origin to new parameter. Called if we merge two conflicting constraints.
41194121 * See OrderingConstraint#merge, OrderingConstraint#rename
41204122 */
4121- def setOrigin (p : TypeParamRef ) = _origin = p
4123+ def setOrigin (p : TypeParamRef ) = currentOrigin = p
41224124
41234125 /** The permanent instance type of the variable, or NoType is none is given yet */
41244126 private var myInst : Type = NoType
@@ -4147,34 +4149,34 @@ object Types {
41474149 /** Is the variable already instantiated? */
41484150 def isInstantiated (implicit ctx : Context ): Boolean = instanceOpt.exists
41494151
4150- def hygienic (tp : Type )(using Context ): Type =
4152+ def avoidCaptures (tp : Type )(using Context ): Type =
41514153 val problemSyms = new TypeAccumulator [Set [Symbol ]]:
41524154 def apply (syms : Set [Symbol ], t : Type ): Set [Symbol ] = t match
41534155 case ref @ TermRef (NoPrefix , _)
4154- if ref.symbol.maybeOwner.ownersIterator.length > level =>
4156+ if ref.symbol.maybeOwner.nestingLevel > nestingLevel =>
41554157 syms + ref.symbol
41564158 case _ =>
41574159 foldOver(syms, t)
41584160 val problems = problemSyms(Set .empty, tp)
41594161 if problems.isEmpty then tp
41604162 else
4161- val htp = ctx.typer.avoid(tp, problems.toList)
4162- val msg = i " Inaccessible variables captured by instance for $this. \n $tp was fixed to $htp "
4163+ val atp = ctx.typer.avoid(tp, problems.toList)
4164+ val msg = i " Inaccessible variables captured in instantation of type variable $this. \n $tp was fixed to $atp "
41634165 typr.println(msg)
41644166 val bound = ctx.typeComparer.fullUpperBound(origin)
4165- if ! (htp <:< bound) then
4166- throw new TypeError (s " $msg, \n but this does not conform to upper bound $bound" )
4167- htp
4167+ if ! (atp <:< bound) then
4168+ throw new TypeError (s " $msg, \n but the latter type does not conform to the upper bound $bound" )
4169+ atp
41684170
41694171 /** Instantiate variable with given type */
41704172 def instantiateWith (tp : Type )(implicit ctx : Context ): Type = {
41714173 assert(tp ne this , s " self instantiation of ${tp.show}, constraint = ${ctx.typerState.constraint.show}" )
4172- val htp = hygienic (tp)
4173- typr.println(s " instantiating ${this .show} with ${htp .show}" )
4174+ val atp = avoidCaptures (tp)
4175+ typr.println(s " instantiating ${this .show} with ${atp .show}" )
41744176 if ((ctx.typerState eq owningState.get) && ! ctx.typeComparer.subtypeCheckInProgress)
4175- inst = htp
4176- ctx.typerState.constraint = ctx.typerState.constraint.replace(origin, htp )
4177- htp
4177+ inst = atp
4178+ ctx.typerState.constraint = ctx.typerState.constraint.replace(origin, atp )
4179+ atp
41784180 }
41794181
41804182 /** Instantiate variable from the constraints over its `origin`.
@@ -4217,6 +4219,9 @@ object Types {
42174219 s " TypeVar( $origin$instStr) "
42184220 }
42194221 }
4222+ object TypeVar :
4223+ def apply (initOrigin : TypeParamRef , creatorState : TyperState )(using Context ) =
4224+ new TypeVar (initOrigin, creatorState, ctx.owner.nestingLevel)
42204225
42214226 type TypeVars = SimpleIdentitySet [TypeVar ]
42224227
0 commit comments