Skip to content

Commit 828d2a0

Browse files
committed
Stop emitting fields for inlined fields.
Also move final val inlining logic to Inline as mentioned in #1879.
1 parent add9a03 commit 828d2a0

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,11 @@ import Decorators._
9797

9898
if (sym.is(Accessor, butNot = NoFieldNeeded))
9999
if (sym.isGetter) {
100-
def skipBlocks(t: Tree): Tree = t match {
101-
case Block(_, t1) => skipBlocks(t1)
102-
case _ => t
103-
}
104-
skipBlocks(tree.rhs) match {
105-
case lit: Literal if sym.is(Final, butNot = Mutable) && isIdempotentExpr(tree.rhs) =>
106-
// duplicating scalac behavior: for final vals that have rhs as constant, we do not create a field
107-
// and instead return the value. This seemingly minor optimization has huge effect on initialization
108-
// order and the values that can be observed during superconstructor call
109-
110-
// see remark about idempotency in PostTyper#normalizeTree
111-
cpy.DefDef(tree)(rhs = lit)
112-
case _ =>
113-
var rhs = tree.rhs.changeOwnerAfter(sym, field, thisTransform)
114-
if (isWildcardArg(rhs)) rhs = EmptyTree
115-
116-
val fieldDef = transformFollowing(ValDef(field, adaptToField(rhs)))
117-
val getterDef = cpy.DefDef(tree)(rhs = transformFollowingDeep(ref(field))(ctx.withOwner(sym), info))
118-
Thicket(fieldDef, getterDef)
119-
}
100+
var rhs = tree.rhs.changeOwnerAfter(sym, field, thisTransform)
101+
if (isWildcardArg(rhs)) rhs = EmptyTree
102+
val fieldDef = transformFollowing(ValDef(field, adaptToField(rhs)))
103+
val getterDef = cpy.DefDef(tree)(rhs = transformFollowingDeep(ref(field))(ctx.withOwner(sym), info))
104+
Thicket(fieldDef, getterDef)
120105
} else if (sym.isSetter) {
121106
if (!sym.is(ParamAccessor)) { val Literal(Constant(())) = tree.rhs } // this is intended as an assertion
122107
field.setFlag(Mutable) // necessary for vals mixed in from Scala2 traits
@@ -127,5 +112,5 @@ import Decorators._
127112
// neither getters nor setters
128113
else tree
129114
}
130-
private val NoFieldNeeded = Lazy | Deferred | JavaDefined
115+
private val NoFieldNeeded = Lazy | Deferred | JavaDefined | Inline
131116
}

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
11731173
if (sym.is(Inline, butNot = DeferredOrParamAccessor))
11741174
checkInlineConformant(rhs1, em"right-hand side of inline $sym")
11751175
patchIfLazy(vdef1)
1176+
patchFinalVals(vdef1)
11761177
vdef1
11771178
}
11781179

@@ -1185,6 +1186,27 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
11851186
patch(Position(toUntyped(vdef).pos.start), "@volatile ")
11861187
}
11871188

1189+
/** Adds inline to final vals with idempotent rhs
1190+
*
1191+
* duplicating scalac behavior: for final vals that have rhs as constant, we do not create a field
1192+
* and instead return the value. This seemingly minor optimization has huge effect on initialization
1193+
* order and the values that can be observed during superconstructor call
1194+
*
1195+
* see remark about idempotency in PostTyper#normalizeTree
1196+
*/
1197+
private def patchFinalVals(vdef: ValDef)(implicit ctx: Context): Unit = {
1198+
def skipBlocks(t: Tree): Tree = t match {
1199+
case Block(_, t1) => skipBlocks(t1)
1200+
case _ => t
1201+
}
1202+
val sym = vdef.symbol
1203+
skipBlocks(vdef.rhs) match {
1204+
case lit: Literal if sym.is(Final, butNot = Mutable) && isIdempotentExpr(vdef.rhs) /* && ctx.scala2Mode (stay compatible with Scala2 for now) */ =>
1205+
sym.flags = sym.flags | Inline
1206+
case _ =>
1207+
}
1208+
}
1209+
11881210
def typedDefDef(ddef: untpd.DefDef, sym: Symbol)(implicit ctx: Context) = track("typedDefDef") {
11891211
val DefDef(name, tparams, vparamss, tpt, _) = ddef
11901212
completeAnnotations(ddef, sym)

0 commit comments

Comments
 (0)