You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Should tree be moved in front of super constructor call?
604
-
// (I.e., is it derived from an early definition?)
605
-
valconstrBufPart=
606
-
if (stat.mods hasFlag PRESUPER|PARAMACCESSOR) constrPrefixBuf
607
-
else constrStatBuf
614
+
// Should tree be moved in front of super constructor call?
615
+
// (I.e., is it derived from an early definition?)
616
+
valconstrBufPart=
617
+
if (stat.mods hasFlag PRESUPER|PARAMACCESSOR) constrPrefixBuf
618
+
else constrStatBuf
608
619
609
-
valassignSym=
610
-
if (stat.symbol.isGetter) stat.symbol.asInstanceOf[MethodSymbol].referenced //setterIn(clazz, hasExpandedName = false) // not yet expanded -- see Mixin
611
-
else stat.symbol
620
+
valeffect=
621
+
if (fieldNeeded) {
622
+
valassignSym=
623
+
if (stat.symbol.isGetter) stat.symbol.asInstanceOf[MethodSymbol].referenced //setterIn(clazz, hasExpandedName = false) // not yet expanded -- see Mixin
624
+
else stat.symbol
612
625
613
-
assert(assignSym ne NoSymbol, stat)
626
+
assert(assignSym ne NoSymbol, stat)
627
+
mkAssign(assignSym, rhs1)
628
+
} else rhs1
629
+
630
+
constrBufPart += effect
631
+
}
614
632
615
-
constrBufPart += mkAssign(assignSym, rhs1)
633
+
fieldNeeded
616
634
}
617
635
636
+
618
637
// TODO: we probably can't emit an Assign tree after typers, need to Apply the setter
619
638
// Create an assignment to class field `to` with rhs `from`
620
639
defmkAssign(to: Symbol, from: Tree):Tree=
@@ -692,24 +711,20 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
692
711
elseif (stat.symbol.isConstructor) auxConstructorBuf += stat
693
712
// a concrete getter's RHS is treated like a ValDef (the actual field isn't emitted until Mixin augments the class that inherits the trait)
694
713
elseif (inTrait && stat.symbol.isGetter && (rhs ne EmptyTree)) {
695
-
assignmentIntoCtor(dd)
714
+
moveEffectToCtor(dd)// ignore whether we need a field -- it will be added in the inheriting class by Mixin
696
715
defBuf += deriveDefDef(dd)(_ =>EmptyTree)
697
716
}
698
717
else defBuf += stat
699
718
}
700
719
case vd@ValDef(mods, _, _, rhs) if!mods.hasStaticFlag =>
701
-
// val defs with constant right-hand sides are eliminated.
702
-
// for all other val defs, an empty valdef goes into the template and
703
-
// the initializer goes as an assignment into the constructor
704
-
// if the val def is an early initialized or a parameter accessor, it goes
705
-
// before the superclass constructor call, otherwise it goes after.
706
-
// Lazy vals don't get the assignment in the constructor.
707
-
if (!stat.symbol.tpe.isInstanceOf[ConstantType]) { // TODO: MethodSynthesis should no longer emit ValDefs with ConstantTypes
708
-
if (rhs !=EmptyTree&&!stat.symbol.isLazy) {
709
-
assignmentIntoCtor(vd)
710
-
}
711
-
defBuf += deriveValDef(stat)(_ =>EmptyTree)
712
-
}
720
+
// Move field's effect into the constructor (usually an assignment of rhs to field,
721
+
// but it could also be the side-effect of the rhs when no storage is needed).
722
+
valneedsField= moveEffectToCtor(vd)
723
+
println(s"$vd needsField: $needsField")
724
+
725
+
// Only emit fields that actually need to be stored
726
+
if (needsField) defBuf += deriveValDef(stat)(_ =>EmptyTree)
727
+
713
728
caseValDef(_, _, _, rhs) =>
714
729
// Add static initializer statements to classInitStatBuf and remove the rhs from the val def.
0 commit comments