Skip to content

Commit 86e83af

Browse files
committed
Constructors: Do not store private fields used only to initialise other fields
1 parent 030ab74 commit 86e83af

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ class Constructors extends MiniPhaseTransform with SymTransformer { thisTransfor
6060

6161
tree match {
6262
case Ident(_) | Select(This(_), _) =>
63-
def inConstructor = ctx.owner.enclosingMethod.isPrimaryConstructor && ctx.owner.enclosingClass == owner
63+
def inConstructor = {
64+
val method = ctx.owner.enclosingMethod
65+
method.isPrimaryConstructor && ctx.owner.enclosingClass == owner
66+
}
6467
if (inConstructor && (sym.is(ParamAccessor) || seenPrivateVals.contains(sym))) {
6568
// used inside constructor, accessed on this,
6669
// could use constructor argument instead, no need to retain field
@@ -131,6 +134,8 @@ class Constructors extends MiniPhaseTransform with SymTransformer { thisTransfor
131134

132135
override def transformTemplate(tree: Template)(implicit ctx: Context, info: TransformerInfo): Tree = {
133136
val cls = ctx.owner.asClass
137+
if (cls.toString.contains("VarianceChecker"))
138+
println("hoho")
134139
val constr @ DefDef(nme.CONSTRUCTOR, Nil, vparams :: Nil, _, EmptyTree) = tree.constr
135140

136141
// Produce aligned accessors and constructor parameters. We have to adjust
@@ -298,6 +303,8 @@ class Constructors extends MiniPhaseTransform with SymTransformer { thisTransfor
298303
cls.copy(
299304
info = clsInfo.derivedClassInfo(
300305
decls = clsInfo.decls.filteredScope(!dropped.contains(_))))
306+
307+
// TODO: this happens to work only because Constructors is the last phase in group
301308
}
302309

303310
val (superCalls, followConstrStats) = constrStats.toList match {

0 commit comments

Comments
 (0)