Skip to content

Commit c8f875f

Browse files
committed
clean up constructors
1 parent 39b2b5d commit c8f875f

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/compiler/scala/tools/nsc/transform/Constructors.scala

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
359359
}
360360

361361
log("merging: " + originalStats.mkString("\n") + "\nwith\n" + specializedStats.mkString("\n"))
362-
val res = for (s <- originalStats; stat = s.duplicate) yield {
362+
for (s <- originalStats; stat = s.duplicate) yield {
363363
log("merge: looking at " + stat)
364364
val stat1 = stat match {
365365
case Assign(sel @ Select(This(_), field), _) =>
@@ -389,9 +389,8 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
389389
} else
390390
stat1
391391
}
392-
if (specBuf.nonEmpty)
393-
println("residual specialized constructor statements: " + specBuf)
394-
res
392+
// if (specBuf.nonEmpty)
393+
// println("residual specialized constructor statements: " + specBuf)
395394
}
396395

397396
/* 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 {
591590
case ValDef(mods, name, _, _) if mods hasFlag PRESUPER =>
592591
// stat is the constructor-local definition of the field value
593592
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")
595594
val to = fields.head.symbol
596595

597596
if (memoizeValue(to)) constrStatBuf += mkAssign(to, Ident(stat.symbol))
@@ -607,9 +606,9 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
607606
// it goes before the superclass constructor call, otherwise it goes after.
608607
// A lazy val's effect is not moved to the constructor, as it is delayed.
609608
// 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 = {
611610
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)
613612
else if (!mods.hasStaticFlag) intoConstructor(statSym, primaryConstr.symbol)(rhs)
614613
else rhs
615614

@@ -619,7 +618,7 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
619618
else if (mods hasFlag PRESUPER | PARAMACCESSOR) constrPrefixBuf
620619
else constrStatBuf
621620

622-
initPhase += mkAssign(statSym, initializingRhs)
621+
initPhase += mkAssign(assignSym, initializingRhs)
623622
}
624623
}
625624

@@ -632,18 +631,21 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
632631
// The primary constructor is dealt with separately (we're massaging it here).
633632
case _: DefDef if statSym.isPrimaryConstructor => ()
634633
case _: DefDef if statSym.isConstructor => auxConstructorBuf += stat
635-
case _: DefDef => defBuf += stat
634+
case stat: DefDef =>
635+
defBuf += stat
636+
636637

637638
// If a val needs a field, an empty valdef goes into the template.
638639
// Except for lazy and ConstantTyped vals, the field is initialized by an assignment in:
639640
// - the class initializer (static),
640641
// - the constructor, before the super call (early initialized or a parameter accessor),
641642
// - the constructor, after the super call (regular val).
642643
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
647649

648650
// all other statements go into the constructor
649651
case _ => constrStatBuf += intoConstructor(impl.symbol, primaryConstr.symbol)(stat)

0 commit comments

Comments
 (0)