diff --git a/compiler/src/dotty/tools/dotc/transform/Constructors.scala b/compiler/src/dotty/tools/dotc/transform/Constructors.scala index 59582b4d69d4..f4c800e342e7 100644 --- a/compiler/src/dotty/tools/dotc/transform/Constructors.scala +++ b/compiler/src/dotty/tools/dotc/transform/Constructors.scala @@ -252,9 +252,17 @@ class Constructors extends MiniPhaseTransform with IdentityDenotTransformer { th case _ => superCalls } + // Lazy Vals may decide to create an eager val instead of a lazy val + // this val should be assigned before constructor body code starts running + + val (lazyAssignments, stats) = followConstrStats.partition { + case Assign(l, r) if l.symbol.name.is(NameKinds.LazyLocalName) => true + case _ => false + } + cpy.Template(tree)( constr = cpy.DefDef(constr)( - rhs = Block(copyParams ::: mappedSuperCalls ::: followConstrStats, unitLiteral)), + rhs = Block(copyParams ::: mappedSuperCalls ::: lazyAssignments ::: stats, unitLiteral)), body = clsStats.toList) } } diff --git a/tests/run/i2396b.scala b/tests/run/i2396b.scala new file mode 100644 index 000000000000..3b1db8f29537 --- /dev/null +++ b/tests/run/i2396b.scala @@ -0,0 +1,14 @@ +class Bees { + def f: PartialFunction[Bee, Unit] = { case Bee(_) => "" } + + f(new Bee("buzz")) + + case class Bee(value: String) + //object Bee // With this it works +} + +object Test { + def main(args: Array[String]): Unit = { + new Bees + } +}