@@ -2160,7 +2160,7 @@ object Types {
21602160 trait ProtoType extends Type {
21612161 def isMatchedBy (tp : Type , keepConstraint : Boolean = false )(using Context ): Boolean
21622162 def fold [T ](x : T , ta : TypeAccumulator [T ] @ retains(caps.* ))(using Context ): T
2163- def map (tm : TypeMap )(using Context ): ProtoType
2163+ def map (tm : TypeMap @ retains(caps. * ) )(using Context ): ProtoType
21642164
21652165 /** If this prototype captures a context, the same prototype except that the result
21662166 * captures the given context `ctx`.
@@ -5572,8 +5572,10 @@ object Types {
55725572 case result : CaptureRef if result.canBeTracked => result
55735573 end BiTypeMap
55745574
5575- abstract class TypeMap (implicit protected var mapCtx : Context )
5576- extends VariantTraversal with (Type -> Type ) { thisMap : TypeMap =>
5575+ abstract class TypeMap (using protected val mapCtx : Context @ retains(caps.* ))
5576+ extends VariantTraversal with (Type -> Type ) { thisMap : TypeMap @ retains(mapCtx) =>
5577+
5578+ def detach : TypeMap = this .asInstanceOf [TypeMap ] // !cc! change once we track contexts
55775579
55785580 def apply (tp : Type ): Type
55795581
@@ -5639,7 +5641,7 @@ object Types {
56395641 protected def mapCapturingType (tp : Type , parent : Type , refs : CaptureSet , v : Int ): Type =
56405642 val saved = variance
56415643 variance = v
5642- try derivedCapturingType(tp, this (parent), refs.map(this ))
5644+ try derivedCapturingType(tp, this (parent), refs.map(detach ))
56435645 finally variance = saved
56445646
56455647 /** Map this function over given type */
@@ -5705,16 +5707,16 @@ object Types {
57055707 derivedSuperType(tp, this (thistp), this (supertp))
57065708
57075709 case tp : LazyRef =>
5710+ val mapCtx1 = mapCtx.asInstanceOf [FreshContext ]
57085711 LazyRef { refCtx =>
5709- given Context = refCtx
5710- val ref1 = tp.ref
5711- if refCtx.runId == mapCtx.runId then this (ref1)
5712+ val ref1 = tp.ref(using refCtx)
5713+ if refCtx.runId == mapCtx1.runId then this (ref1)
57125714 else // splice in new run into map context
5713- val saved = mapCtx
5714- mapCtx = mapCtx.fresh
5715- .setPeriod(Period (refCtx.runId, mapCtx .phaseId))
5716- .setRun(refCtx.run )
5717- try this (ref1) finally mapCtx = saved
5715+ val savedPeriod = mapCtx1.period
5716+ val savedRun = mapCtx1.run
5717+ mapCtx1 .setPeriod(Period (refCtx.runId, mapCtx1 .phaseId)).setRun(refCtx.run )
5718+ try this (ref1 )
5719+ finally mapCtx1.setPeriod(savedPeriod).setRun(savedRun)
57185720 }
57195721
57205722 case tp : ClassInfo =>
@@ -5769,7 +5771,8 @@ object Types {
57695771 }
57705772
57715773 /** A type map that maps also parents and self type of a ClassInfo */
5772- abstract class DeepTypeMap (using Context ) extends TypeMap {
5774+ abstract class DeepTypeMap (using mapCtx : Context @ retains(caps.* ))
5775+ extends TypeMap { thisMap : TypeMap @ retains(mapCtx) =>
57735776 override def mapClassInfo (tp : ClassInfo ): ClassInfo = {
57745777 val prefix1 = this (tp.prefix)
57755778 val parents1 = tp.declaredParents mapConserve this
@@ -5781,7 +5784,7 @@ object Types {
57815784 }
57825785 }
57835786
5784- @ sharable object IdentityTypeMap extends TypeMap ()( NoContext ) {
5787+ @ sharable object IdentityTypeMap extends TypeMap (using NoContext ) {
57855788 def apply (tp : Type ): Type = tp
57865789 }
57875790
@@ -5792,7 +5795,8 @@ object Types {
57925795 * variance < 0 : approximate by lower bound
57935796 * variance = 0 : propagate bounds to next outer level
57945797 */
5795- abstract class ApproximatingTypeMap (using Context ) extends TypeMap { thisMap =>
5798+ abstract class ApproximatingTypeMap (using mapCtx : Context @ retains(caps.* ))
5799+ extends TypeMap { thisMap : TypeMap @ retains(mapCtx) =>
57965800
57975801 protected def range (lo : Type , hi : Type ): Type =
57985802 if (variance > 0 ) hi
0 commit comments