From 08ba889be3469a15cd0b294f35c7617c29d56be9 Mon Sep 17 00:00:00 2001 From: leitoh Date: Tue, 29 Oct 2019 13:12:09 -0700 Subject: [PATCH] Mark single-use field MUTABLE for nullification (Fixes scala/bug#11781) --- src/compiler/scala/tools/nsc/transform/Mixin.scala | 4 +++- .../junit/scala/tools/nsc/backend/jvm/BytecodeTest.scala | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/compiler/scala/tools/nsc/transform/Mixin.scala b/src/compiler/scala/tools/nsc/transform/Mixin.scala index 24bdb65bbda5..0bfb77b0c04b 100644 --- a/src/compiler/scala/tools/nsc/transform/Mixin.scala +++ b/src/compiler/scala/tools/nsc/transform/Mixin.scala @@ -409,8 +409,10 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL with AccessorSynthes deriveDefDef(dd) { case blk@Block(stats, expr) => assert(dd.symbol.originalOwner.isClass, dd.symbol) - def nullify(sym: Symbol) = + def nullify(sym: Symbol) = { + sym.accessedOrSelf.setFlag(MUTABLE) Select(gen.mkAttributedThis(sym.enclClass), sym.accessedOrSelf) === NULL + } val stats1 = stats ::: fieldsToNull.map(nullify) treeCopy.Block(blk, stats1, expr) case tree => diff --git a/test/junit/scala/tools/nsc/backend/jvm/BytecodeTest.scala b/test/junit/scala/tools/nsc/backend/jvm/BytecodeTest.scala index c57b4a21f836..8f92253a885b 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/BytecodeTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/BytecodeTest.scala @@ -303,4 +303,13 @@ class BytecodeTest extends BytecodeTesting { } assertEquals(List("$outer", "x$1", "y$1"), assignedInConstr.sorted) } + + @Test + def t11718(): Unit = { + val code = "class A11718; class B11718 { private val a = new A; lazy val b = a }" + val cs = compileClasses(code) + val B = cs.find(_.name == "B11718").get + val a = B.fields.asScala.find(_.name == "a").get + assertEquals(0, a.access & Opcodes.ACC_FINAL) + } }