Skip to content

Commit c325428

Browse files
committed
Simplify Synthetics.transform
No backward mapping is necessary anymore. # Conflicts: # compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala
1 parent 38cd627 commit c325428

File tree

2 files changed

+21
-66
lines changed

2 files changed

+21
-66
lines changed

compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object CheckCaptures:
4242
if sym.isAllOf(PrivateParamAccessor) && !sym.hasAnnotation(defn.ConstructorOnlyAnnot) then
4343
sym.copySymDenotation(initFlags = sym.flags &~ Private | Recheck.ResetPrivate)
4444
else if Synthetics.needsTransform(sym) then
45-
Synthetics.transform(sym, toCC = true)
45+
Synthetics.transform(sym)
4646
else sym
4747
else sym
4848
end Pre
@@ -203,11 +203,7 @@ class CheckCaptures extends Recheck, SymTransformer:
203203
override def run(using Context): Unit =
204204
if Feature.ccEnabled then
205205
super.run
206-
207-
override def transformSym(sym: SymDenotation)(using Context): SymDenotation =
208-
if Synthetics.needsTransform(sym) then Synthetics.transform(sym, toCC = false)
209-
else super.transformSym(sym)
210-
206+
211207
override def printingContext(ctx: Context) = ctx.withProperty(ccStateKey, Some(new CCState))
212208

213209
class CaptureChecker(ictx: Context) extends Rechecker(ictx):

compiler/src/dotty/tools/dotc/cc/Synthetics.scala

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ object Synthetics:
6161
* @param sym The method to transform @pre needsTransform(sym) must hold.
6262
* @param toCC Whether to transform the type to capture checking or back.
6363
*/
64-
def transform(sym: SymDenotation, toCC: Boolean)(using Context): SymDenotation =
64+
def transform(sym: SymDenotation)(using Context): SymDenotation =
6565

6666
/** Add capture dependencies to the type of the `apply` or `copy` method of a case class.
6767
* An apply method in a case class like this:
@@ -92,31 +92,18 @@ object Synthetics:
9292
case _ =>
9393
info
9494

95-
/** Drop capture dependencies from the type of `apply` or `copy` method of a case class */
96-
def dropCaptureDeps(tp: Type): Type = tp match
97-
case tp: MethodOrPoly =>
98-
tp.derivedLambdaType(resType = dropCaptureDeps(tp.resType))
99-
case CapturingType(parent, _) =>
100-
dropCaptureDeps(parent)
101-
case RefinedType(parent, _, _) =>
102-
dropCaptureDeps(parent)
103-
case _ =>
104-
tp
105-
10695
/** Add capture information to the type of the default getter of a case class copy method
107-
* if toCC = true, or remove the added info again if toCC = false.
10896
*/
10997
def transformDefaultGetterCaptures(info: Type, owner: Symbol, idx: Int)(using Context): Type = info match
11098
case info: MethodOrPoly =>
11199
info.derivedLambdaType(resType = transformDefaultGetterCaptures(info.resType, owner, idx))
112100
case info: ExprType =>
113101
info.derivedExprType(transformDefaultGetterCaptures(info.resType, owner, idx))
114102
case EventuallyCapturingType(parent, _) =>
115-
if toCC then transformDefaultGetterCaptures(parent, owner, idx)
116-
else parent
103+
transformDefaultGetterCaptures(parent, owner, idx)
117104
case info @ AnnotatedType(parent, annot) =>
118105
info.derivedAnnotatedType(transformDefaultGetterCaptures(parent, owner, idx), annot)
119-
case _ if toCC && idx < owner.asClass.paramGetters.length =>
106+
case _ if idx < owner.asClass.paramGetters.length =>
120107
val param = owner.asClass.paramGetters(idx)
121108
val pinfo = param.info
122109
atPhase(ctx.phase.next) {
@@ -126,49 +113,29 @@ object Synthetics:
126113
case _ =>
127114
info
128115

129-
/** Augment an unapply of type `(x: C): D` to `(x: C^{cap}): D^{x}` if toCC is true,
130-
* or remove the added capture sets again if toCC = false.
131-
*/
116+
/** Augment an unapply of type `(x: C): D` to `(x: C^{cap}): D^{x}` */
132117
def transformUnapplyCaptures(info: Type)(using Context): Type = info match
133118
case info: MethodType =>
134-
if toCC then
135-
val paramInfo :: Nil = info.paramInfos: @unchecked
136-
val newParamInfo = CapturingType(paramInfo, CaptureSet.universal)
137-
val trackedParam = info.paramRefs.head
138-
def newResult(tp: Type): Type = tp match
139-
case tp: MethodOrPoly =>
140-
tp.derivedLambdaType(resType = newResult(tp.resType))
141-
case _ =>
142-
CapturingType(tp, CaptureSet(trackedParam))
143-
info.derivedLambdaType(paramInfos = newParamInfo :: Nil, resType = newResult(info.resType))
144-
.showing(i"augment unapply type $info to $result", capt)
145-
else info.paramInfos match
146-
case CapturingType(oldParamInfo, _) :: Nil =>
147-
def oldResult(tp: Type): Type = tp match
148-
case tp: MethodOrPoly =>
149-
tp.derivedLambdaType(resType = oldResult(tp.resType))
150-
case CapturingType(tp, _) =>
151-
tp
152-
info.derivedLambdaType(paramInfos = oldParamInfo :: Nil, resType = oldResult(info.resType))
119+
val paramInfo :: Nil = info.paramInfos: @unchecked
120+
val newParamInfo = CapturingType(paramInfo, CaptureSet.universal)
121+
val trackedParam = info.paramRefs.head
122+
def newResult(tp: Type): Type = tp match
123+
case tp: MethodOrPoly =>
124+
tp.derivedLambdaType(resType = newResult(tp.resType))
153125
case _ =>
154-
info
126+
CapturingType(tp, CaptureSet(trackedParam))
127+
info.derivedLambdaType(paramInfos = newParamInfo :: Nil, resType = newResult(info.resType))
128+
.showing(i"augment unapply type $info to $result", capt)
155129
case info: PolyType =>
156130
info.derivedLambdaType(resType = transformUnapplyCaptures(info.resType))
157131

158132
def transformComposeCaptures(symd: SymDenotation) =
159133
val (pt: PolyType) = symd.info: @unchecked
160134
val (mt: MethodType) = pt.resType: @unchecked
161135
val (enclThis: ThisType) = symd.owner.thisType: @unchecked
162-
val mt1 =
163-
if toCC then
164-
MethodType(mt.paramNames)(
165-
mt1 => mt.paramInfos.map(_.capturing(CaptureSet.universal)),
166-
mt1 => CapturingType(mt.resType, CaptureSet(enclThis, mt1.paramRefs.head)))
167-
else
168-
MethodType(mt.paramNames)(
169-
mt1 => mt.paramInfos.map(_.stripCapturing),
170-
mt1 => mt.resType.stripCapturing)
171-
pt.derivedLambdaType(resType = mt1)
136+
pt.derivedLambdaType(resType = MethodType(mt.paramNames)(
137+
mt1 => mt.paramInfos.map(_.capturing(CaptureSet.universal)),
138+
mt1 => CapturingType(mt.resType, CaptureSet(enclThis, mt1.paramRefs.head))))
172139

173140
def transformCurriedTupledCaptures(symd: SymDenotation) =
174141
val (et: ExprType) = symd.info: @unchecked
@@ -179,26 +146,18 @@ object Synthetics:
179146
defn.FunctionOf(args, mapFinalResult(res, f), isContextual)
180147
else
181148
f(tp)
182-
val resType1 =
183-
if toCC then
184-
mapFinalResult(et.resType, CapturingType(_, CaptureSet(enclThis)))
185-
else
186-
et.resType.stripCapturing
187-
ExprType(resType1)
149+
ExprType(mapFinalResult(et.resType, CapturingType(_, CaptureSet(enclThis))))
188150

189151
def transformCompareCaptures =
190-
if toCC then
191-
MethodType(defn.ObjectType.capturing(CaptureSet.universal) :: Nil, defn.BooleanType)
192-
else
193-
defn.methOfAnyRef(defn.BooleanType)
152+
MethodType(defn.ObjectType.capturing(CaptureSet.universal) :: Nil, defn.BooleanType)
194153

195154
sym.copySymDenotation(info = sym.name match
196155
case DefaultGetterName(nme.copy, n) =>
197156
transformDefaultGetterCaptures(sym.info, sym.owner, n)
198157
case nme.unapply =>
199158
transformUnapplyCaptures(sym.info)
200159
case nme.apply | nme.copy =>
201-
if toCC then addCaptureDeps(sym.info) else dropCaptureDeps(sym.info)
160+
addCaptureDeps(sym.info)
202161
case nme.andThen | nme.compose =>
203162
transformComposeCaptures(sym)
204163
case nme.curried | nme.tupled =>

0 commit comments

Comments
 (0)