Skip to content

Commit e1c482a

Browse files
committed
Make Memoize not depend on prepareForDefDef.
[@DarkDimius] I see no good reason for this, as it creates tight coupling between `ifs` in two methods. [@odersky] The reason is probably in the deleted comment: // allocate field early so that initializer has the right owner for subsequent phases in // the group. We now transform the rhs in subsequent phases with the getter as owner, where before it was the field. It is not clear to me whether this matters or not. [Update] We figured out the problem: It was a missing changeOwnerAfter in Constructor. Added to next commit.
1 parent b1ce9c2 commit e1c482a

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

src/dotty/tools/dotc/transform/Memoize.scala

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,38 +48,26 @@ import Decorators._
4848
case _ =>
4949
}
5050

51-
override def prepareForDefDef(tree: DefDef)(implicit ctx: Context) = {
52-
val sym = tree.symbol
53-
if (sym.isGetter && !sym.is(NoFieldNeeded)) {
54-
// allocate field early so that initializer has the right owner for subsequeny phases in
55-
// the group.
56-
val maybeMutable = if (sym is Stable) EmptyFlags else Mutable
57-
val field = ctx.newSymbol(
58-
owner = ctx.owner,
59-
name = sym.name.asTermName.fieldName,
60-
flags = Private | maybeMutable,
61-
info = sym.info.resultType,
62-
coord = tree.pos).enteredAfter(thisTransform)
63-
tree.rhs.changeOwnerAfter(sym, field, thisTransform)
64-
}
65-
this
66-
}
67-
6851
override def transformDefDef(tree: DefDef)(implicit ctx: Context, info: TransformerInfo): Tree = {
6952
val sym = tree.symbol
70-
def field = {
71-
val field = sym.field.asTerm
72-
assert(field.exists, i"no field for ${sym.showLocated} in ${sym.owner.info.decls.toList.map{_.showDcl}}%; %")
73-
field
74-
}
53+
54+
def newField = ctx.newSymbol(
55+
owner = ctx.owner,
56+
name = sym.name.asTermName.fieldName,
57+
flags = Private | (if (sym is Stable) EmptyFlags else Mutable),
58+
info = sym.info.resultType,
59+
coord = tree.pos).enteredAfter(thisTransform)
60+
61+
lazy val field = sym.field.orElse(newField).asTerm
7562
if (sym.is(Accessor, butNot = NoFieldNeeded))
7663
if (sym.isGetter) {
64+
tree.rhs.changeOwnerAfter(sym, field, thisTransform)
7765
val fieldDef = transformFollowing(ValDef(field, tree.rhs))
7866
val getterDef = cpy.DefDef(tree)(rhs = transformFollowingDeep(ref(field)))
7967
Thicket(fieldDef, getterDef)
8068
}
8169
else if (sym.isSetter) {
82-
if (!sym.is(ParamAccessor)) { val Literal(Constant(())) = tree.rhs }
70+
if (!sym.is(ParamAccessor)) { val Literal(Constant(())) = tree.rhs } // this is intended as an assertion
8371
val initializer = Assign(ref(field), ref(tree.vparamss.head.head.symbol))
8472
cpy.DefDef(tree)(rhs = transformFollowingDeep(initializer))
8573
}

0 commit comments

Comments
 (0)