From 8f0577c603ac86e9bb1cc8fc01bb74f01501b5dc Mon Sep 17 00:00:00 2001 From: Lukas Rytz Date: Thu, 9 Dec 2021 19:20:16 +0100 Subject: [PATCH] Don't emit `releaseFence` for class params of specialized classes Non-specialized val fields in a specialized class are made non-final because of the way specialization is encoded. A `releaseFence` call is added to the constructor to ensure safe publication. The releaseFence call is not necessary for class parameters as those remain final. Follow-up for #9704, fixes scala/bug#12500 --- src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala | 2 +- test/junit/scala/tools/nsc/backend/jvm/BytecodeTest.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala b/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala index 20f3b8c59486..14077bb69e49 100644 --- a/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala +++ b/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala @@ -750,7 +750,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers { for (m <- normMembers) { if (!needsSpecialization(fullEnv, m)) { - if (m.isValue && !m.isMutable && !m.isMethod && !m.isDeferred && !m.isLazy) { + if (m.isValue && !m.isMutable && !m.isMethod && !m.isDeferred && !m.isLazy && !m.isParamAccessor) { // non-specialized `val` fields are made mutable (in Constructors) and assigned from the // constructors of specialized subclasses. See PR scala/scala#9704. clazz.primaryConstructor.updateAttachment(ConstructorNeedsFence) diff --git a/test/junit/scala/tools/nsc/backend/jvm/BytecodeTest.scala b/test/junit/scala/tools/nsc/backend/jvm/BytecodeTest.scala index f7a0de1c5372..44983abe6524 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/BytecodeTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/BytecodeTest.scala @@ -393,7 +393,7 @@ class BytecodeTest extends BytecodeTesting { @Test def nonSpecializedValFence(): Unit = { def code(u1: String) = - s"""abstract class Speck[@specialized(Int) T](t: T) { + s"""abstract class Speck[@specialized(Int) T](t: T, sm: String, val sn: String) { | val a = t | $u1 | lazy val u2 = "?"