Skip to content

Commit 158543b

Browse files
committed
CC-UPDATE: Make TypeMaps impure
1 parent 7b9c1a8 commit 158543b

File tree

9 files changed

+38
-32
lines changed

9 files changed

+38
-32
lines changed

tests/pos-with-compiler-cc/dotc/ast/Trees.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ object Trees {
13941394
case _ =>
13951395
sourced
13961396

1397-
abstract class TreeMap(val cpy: TreeCopier = inst.cpy) { self =>
1397+
abstract class TreeMap(val cpy: TreeCopier = inst.cpy) { self: TreeMap @retains(caps.*) =>
13981398
def transform(tree: Tree)(using Context): Tree = {
13991399
inContext(transformCtx(tree)) {
14001400
Stats.record(s"TreeMap.transform/$getClass")

tests/pos-with-compiler-cc/dotc/core/Annotations.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import printing.{Showable, Printer}
99
import printing.Texts.Text
1010
import annotation.internal.sharable
1111
import language.experimental.pureFunctions
12+
import annotation.retains
1213

1314
object Annotations {
1415

@@ -54,7 +55,7 @@ object Annotations {
5455
* be overridden. Returns EmptyAnnotation if type type map produces a range
5556
* type, since ranges cannot be types of trees.
5657
*/
57-
def mapWith(tm: TypeMap)(using Context) =
58+
def mapWith(tm: TypeMap @retains(caps.*))(using Context) =
5859
val args = arguments
5960
if args.isEmpty then this
6061
else

tests/pos-with-compiler-cc/dotc/core/Types.scala

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/pos-with-compiler-cc/dotc/core/classfile/ClassfileParser.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import typer.Checking.checkNonCyclic
2424
import io.{AbstractFile, ZipArchive}
2525
import scala.util.control.NonFatal
2626
import language.experimental.pureFunctions
27+
import annotation.retains
2728

2829
object ClassfileParser {
2930
/** Marker trait for unpicklers that can be embedded in classfiles. */
@@ -34,7 +35,7 @@ object ClassfileParser {
3435
object NoEmbedded extends Embedded
3536

3637
/** Replace raw types with wildcard applications */
37-
def cook(using Context): TypeMap = new TypeMap {
38+
def cook(using ctx: Context): TypeMap = (new TypeMap {
3839
def apply(tp: Type): Type = tp match {
3940
case tp: TypeRef if tp.symbol.typeParams.nonEmpty =>
4041
AppliedType(tp, tp.symbol.typeParams.map(Function.const(TypeBounds.empty)))
@@ -50,7 +51,7 @@ object ClassfileParser {
5051
case _ =>
5152
mapOver(tp)
5253
}
53-
}
54+
}).detach // !cc! should thread context through instead
5455
}
5556

5657
class ClassfileParser(

tests/pos-with-compiler-cc/dotc/inlines/Inliner.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ class Inliner(val call: tpd.Tree)(using Context):
551551
// the owner from the inlined method to the current owner.
552552
val inliner = new InlinerMap(
553553
typeMap =
554-
new DeepTypeMap {
554+
(new DeepTypeMap {
555555
override def stopAt =
556556
if opaqueProxies.isEmpty then StopAt.Static else StopAt.Package
557557
def apply(t: Type) = t match {
@@ -562,7 +562,7 @@ class Inliner(val call: tpd.Tree)(using Context):
562562
else paramProxy.getOrElse(t, mapOver(t))
563563
case t => mapOver(t)
564564
}
565-
},
565+
}).detach,
566566
treeMap = {
567567
case tree: This =>
568568
tree.tpe match {
@@ -926,14 +926,14 @@ class Inliner(val call: tpd.Tree)(using Context):
926926
if (typeBindings.nonEmpty) {
927927
val typeBindingsSet = typeBindings.foldLeft[SimpleIdentitySet[Symbol]](SimpleIdentitySet.empty)(_ + _.symbol)
928928
val inlineTypeBindings = new TreeTypeMap(
929-
typeMap = new TypeMap() {
929+
typeMap = (new TypeMap() {
930930
override def apply(tp: Type): Type = tp match {
931931
case tr: TypeRef if tr.prefix.eq(NoPrefix) && typeBindingsSet.contains(tr.symbol) =>
932932
val TypeAlias(res) = tr.info: @unchecked
933933
res
934934
case tp => mapOver(tp)
935935
}
936-
},
936+
}).detach,
937937
treeMap = {
938938
case ident: Ident if ident.isType && typeBindingsSet.contains(ident.symbol) =>
939939
val TypeAlias(r) = ident.symbol.info: @unchecked

tests/pos-with-compiler-cc/dotc/quoted/PickledQuotes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ object PickledQuotes {
188188
mapOver(tp)
189189
}
190190
}
191-
val expansion2 = new TreeTypeMap(new ReplaceSplicedTyped).transform(expr1)
191+
val expansion2 = new TreeTypeMap((new ReplaceSplicedTyped).detach).transform(expr1)
192192
quotePickling.println(i"**** typed quote\n${expansion2.show}")
193193
expansion2
194194
case _ =>

tests/pos-with-compiler-cc/dotc/transform/HoistSuperArgs.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class HoistSuperArgs extends MiniPhase, IdentityDenotTransformer { thisPhase =>
138138
val paramSyms = prefss.flatten.map(pref =>
139139
if pref.isType then pref.tpe.typeSymbol else pref.symbol)
140140
val tmap = new TreeTypeMap(
141-
typeMap = new TypeMap {
141+
typeMap = (new TypeMap {
142142
lazy val origToParam = (origParams ::: lifted).zip(paramSyms).toMap
143143
def apply(tp: Type) = tp match {
144144
case tp: NamedType if needsRewire(tp) =>
@@ -149,7 +149,7 @@ class HoistSuperArgs extends MiniPhase, IdentityDenotTransformer { thisPhase =>
149149
case _ =>
150150
mapOver(tp)
151151
}
152-
},
152+
}).detach,
153153
treeMap = {
154154
case tree: RefTree if needsRewire(tree.tpe) =>
155155
cpy.Ident(tree)(tree.name).withType(tree.tpe)

tests/pos-with-compiler-cc/dotc/typer/ProtoTypes.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ object ProtoTypes {
125125
/** A trait for prototypes that match all types */
126126
trait MatchAlways extends ProtoType, caps.Pure {
127127
def isMatchedBy(tp1: Type, keepConstraint: Boolean)(using Context): Boolean = true
128-
def map(tm: TypeMap)(using Context): ProtoType = this
128+
def map(tm: TypeMap @retains(caps.*))(using Context): ProtoType = this
129129
def fold[T](x: T, ta: TypeAccumulator[T] @retains(caps.*))(using Context): T = x
130130
override def toString: String = getClass.toString
131131
}
@@ -239,7 +239,7 @@ object ProtoTypes {
239239
override def unusableForInference(using Context): Boolean =
240240
memberProto.unusableForInference
241241

242-
def map(tm: TypeMap)(using Context): SelectionProto = derivedSelectionProto(name, tm(memberProto), compat)
242+
def map(tm: TypeMap @retains(caps.*))(using Context): SelectionProto = derivedSelectionProto(name, tm(memberProto), compat)
243243
def fold[T](x: T, ta: TypeAccumulator[T] @retains(caps.*))(using Context): T = ta(x, memberProto)
244244

245245
override def deepenProto(using Context): SelectionProto =
@@ -542,7 +542,7 @@ object ProtoTypes {
542542

543543
override def toString: String = s"FunProto(${args mkString ","} => $resultType)"
544544

545-
def map(tm: TypeMap)(using Context): FunProto =
545+
def map(tm: TypeMap @retains(caps.*))(using Context): FunProto =
546546
derivedFunProto(args, tm(resultType), typer)
547547

548548
def fold[T](x: T, ta: TypeAccumulator[T] @retains(caps.*))(using Context): T =
@@ -600,7 +600,7 @@ object ProtoTypes {
600600
override def unusableForInference(using Context): Boolean =
601601
argType.unusableForInference || resType.unusableForInference
602602

603-
def map(tm: TypeMap)(using Context): ViewProto = derivedViewProto(tm(argType), tm(resultType))
603+
def map(tm: TypeMap @retains(caps.*))(using Context): ViewProto = derivedViewProto(tm(argType), tm(resultType))
604604

605605
def fold[T](x: T, ta: TypeAccumulator[T] @retains(caps.*))(using Context): T =
606606
ta(ta(x, argType), resultType)
@@ -653,7 +653,7 @@ object ProtoTypes {
653653
override def unusableForInference(using Context): Boolean =
654654
targs.exists(_.tpe.unusableForInference)
655655

656-
def map(tm: TypeMap)(using Context): PolyProto =
656+
def map(tm: TypeMap @retains(caps.*))(using Context): PolyProto =
657657
derivedPolyProto(targs, tm(resultType))
658658

659659
def fold[T](x: T, ta: TypeAccumulator[T] @retains(caps.*))(using Context): T =

tests/pos-with-compiler-cc/dotc/typer/QuotesAndSplices.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ trait QuotesAndSplices {
329329
case tp => mapOver(tp)
330330
}
331331
}
332-
new TreeTypeMap(typeMap = typeMap).transform(shape1)
332+
new TreeTypeMap(typeMap = typeMap.detach).transform(shape1)
333333
}
334334

335335
(typeBindings.toMap, shape2, patterns)

0 commit comments

Comments
 (0)