@@ -4111,14 +4111,16 @@ object Types {
4111
4111
* @param origin The parameter that's tracked by the type variable.
4112
4112
* @param creatorState The typer state in which the variable was created.
4113
4113
*/
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 {
4115
4115
4116
- def origin : TypeParamRef = _origin
4116
+ private var currentOrigin = initOrigin
4117
+
4118
+ def origin : TypeParamRef = currentOrigin
4117
4119
4118
4120
/** Set origin to new parameter. Called if we merge two conflicting constraints.
4119
4121
* See OrderingConstraint#merge, OrderingConstraint#rename
4120
4122
*/
4121
- def setOrigin (p : TypeParamRef ) = _origin = p
4123
+ def setOrigin (p : TypeParamRef ) = currentOrigin = p
4122
4124
4123
4125
/** The permanent instance type of the variable, or NoType is none is given yet */
4124
4126
private var myInst : Type = NoType
@@ -4147,34 +4149,34 @@ object Types {
4147
4149
/** Is the variable already instantiated? */
4148
4150
def isInstantiated (implicit ctx : Context ): Boolean = instanceOpt.exists
4149
4151
4150
- def hygienic (tp : Type )(using Context ): Type =
4152
+ def avoidCaptures (tp : Type )(using Context ): Type =
4151
4153
val problemSyms = new TypeAccumulator [Set [Symbol ]]:
4152
4154
def apply (syms : Set [Symbol ], t : Type ): Set [Symbol ] = t match
4153
4155
case ref @ TermRef (NoPrefix , _)
4154
- if ref.symbol.maybeOwner.ownersIterator.length > level =>
4156
+ if ref.symbol.maybeOwner.nestingLevel > nestingLevel =>
4155
4157
syms + ref.symbol
4156
4158
case _ =>
4157
4159
foldOver(syms, t)
4158
4160
val problems = problemSyms(Set .empty, tp)
4159
4161
if problems.isEmpty then tp
4160
4162
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 "
4163
4165
typr.println(msg)
4164
4166
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
4168
4170
4169
4171
/** Instantiate variable with given type */
4170
4172
def instantiateWith (tp : Type )(implicit ctx : Context ): Type = {
4171
4173
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}" )
4174
4176
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
4178
4180
}
4179
4181
4180
4182
/** Instantiate variable from the constraints over its `origin`.
@@ -4217,6 +4219,9 @@ object Types {
4217
4219
s " TypeVar( $origin$instStr) "
4218
4220
}
4219
4221
}
4222
+ object TypeVar :
4223
+ def apply (initOrigin : TypeParamRef , creatorState : TyperState )(using Context ) =
4224
+ new TypeVar (initOrigin, creatorState, ctx.owner.nestingLevel)
4220
4225
4221
4226
type TypeVars = SimpleIdentitySet [TypeVar ]
4222
4227
0 commit comments