@@ -359,7 +359,7 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
359
359
}
360
360
361
361
log(" merging: " + originalStats.mkString(" \n " ) + " \n with\n " + specializedStats.mkString(" \n " ))
362
- val res = for (s <- originalStats; stat = s.duplicate) yield {
362
+ for (s <- originalStats; stat = s.duplicate) yield {
363
363
log(" merge: looking at " + stat)
364
364
val stat1 = stat match {
365
365
case Assign (sel @ Select (This (_), field), _) =>
@@ -389,9 +389,8 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
389
389
} else
390
390
stat1
391
391
}
392
- if (specBuf.nonEmpty)
393
- println(" residual specialized constructor statements: " + specBuf)
394
- res
392
+ // if (specBuf.nonEmpty)
393
+ // println("residual specialized constructor statements: " + specBuf)
395
394
}
396
395
397
396
/* Add an 'if' around the statements coming after the super constructor. This
@@ -591,7 +590,7 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
591
590
case ValDef (mods, name, _, _) if mods hasFlag PRESUPER =>
592
591
// stat is the constructor-local definition of the field value
593
592
val fields = presupers filter (_.getterName == name)
594
- assert(fields.length == 1 )
593
+ assert(fields.length == 1 , s " expected exactly one field by name $name in $presupers of $clazz 's early initializers " )
595
594
val to = fields.head.symbol
596
595
597
596
if (memoizeValue(to)) constrStatBuf += mkAssign(to, Ident (stat.symbol))
@@ -607,9 +606,9 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
607
606
// it goes before the superclass constructor call, otherwise it goes after.
608
607
// A lazy val's effect is not moved to the constructor, as it is delayed.
609
608
// Returns `true` when a `ValDef` is needed.
610
- def moveEffectToCtor (mods : Modifiers , rhs : Tree , memoized : Boolean ): Unit = {
609
+ def moveEffectToCtor (mods : Modifiers , rhs : Tree , assignSym : Symbol ): Unit = {
611
610
val initializingRhs =
612
- if (! memoized || statSym.isLazy) EmptyTree // not memoized, or effect delayed (for lazy val)
611
+ if ((assignSym eq NoSymbol ) || statSym.isLazy) EmptyTree // not memoized, or effect delayed (for lazy val)
613
612
else if (! mods.hasStaticFlag) intoConstructor(statSym, primaryConstr.symbol)(rhs)
614
613
else rhs
615
614
@@ -619,7 +618,7 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
619
618
else if (mods hasFlag PRESUPER | PARAMACCESSOR ) constrPrefixBuf
620
619
else constrStatBuf
621
620
622
- initPhase += mkAssign(statSym , initializingRhs)
621
+ initPhase += mkAssign(assignSym , initializingRhs)
623
622
}
624
623
}
625
624
@@ -632,18 +631,21 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
632
631
// The primary constructor is dealt with separately (we're massaging it here).
633
632
case _ : DefDef if statSym.isPrimaryConstructor => ()
634
633
case _ : DefDef if statSym.isConstructor => auxConstructorBuf += stat
635
- case _ : DefDef => defBuf += stat
634
+ case stat : DefDef =>
635
+ defBuf += stat
636
+
636
637
637
638
// If a val needs a field, an empty valdef goes into the template.
638
639
// Except for lazy and ConstantTyped vals, the field is initialized by an assignment in:
639
640
// - the class initializer (static),
640
641
// - the constructor, before the super call (early initialized or a parameter accessor),
641
642
// - the constructor, after the super call (regular val).
642
643
case ValDef (mods, _, _, rhs) =>
643
- val emitField = memoizeValue(statSym)
644
- moveEffectToCtor(mods, rhs, emitField)
645
-
646
- if (emitField) defBuf += deriveValDef(stat)(_ => EmptyTree )
644
+ if (rhs ne EmptyTree ) {
645
+ val emitField = memoizeValue(statSym)
646
+ moveEffectToCtor(mods, rhs, statSym)
647
+ if (emitField) defBuf += deriveValDef(stat)(_ => EmptyTree )
648
+ } else defBuf += stat
647
649
648
650
// all other statements go into the constructor
649
651
case _ => constrStatBuf += intoConstructor(impl.symbol, primaryConstr.symbol)(stat)
0 commit comments