Skip to content

Commit 5160387

Browse files
committed
Clean up a bit more in Constructors.
Comment about my poor naming choice in Types. NullaryMethodType sounds like the method has one empty argument list, whereas it really has no argument lists at all.
1 parent 9050aaf commit 5160387

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

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

Lines changed: 12 additions & 12 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

@@ -640,10 +639,11 @@ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
640639
// - the constructor, before the super call (early initialized or a parameter accessor),
641640
// - the constructor, after the super call (regular val).
642641
case ValDef(mods, _, _, rhs) =>
643-
val emitField = memoizeValue(statSym)
644-
moveEffectToCtor(mods, rhs, emitField)
645-
646-
if (emitField) defBuf += deriveValDef(stat)(_ => EmptyTree)
642+
if (rhs ne EmptyTree) {
643+
val emitField = memoizeValue(statSym)
644+
moveEffectToCtor(mods, rhs, if (emitField) statSym else NoSymbol)
645+
if (emitField) defBuf += deriveValDef(stat)(_ => EmptyTree)
646+
} else defBuf += stat
647647

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

src/reflect/scala/reflect/internal/Types.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,8 @@ trait Types
24962496
override def isJava = true
24972497
}
24982498

2499+
// TODO: rename so it's more appropriate for the type that is for a method without argument lists
2500+
// ("nullary" erroneously implies it has an argument list with zero arguments, it actually has zero argument lists)
24992501
case class NullaryMethodType(override val resultType: Type) extends Type with NullaryMethodTypeApi {
25002502
override def isTrivial = resultType.isTrivial && (resultType eq resultType.withoutAnnotations)
25012503
override def prefix: Type = resultType.prefix

0 commit comments

Comments
 (0)