diff options
-rw-r--r-- | compiler/src/dotty/tools/dotc/transform/Memoize.scala | 2 | ||||
-rw-r--r-- | tests/run/final-var.check | 4 | ||||
-rw-r--r-- | tests/run/final-var.scala | 20 |
3 files changed, 25 insertions, 1 deletions
diff --git a/compiler/src/dotty/tools/dotc/transform/Memoize.scala b/compiler/src/dotty/tools/dotc/transform/Memoize.scala index 0314d4ec4..63edc0256 100644 --- a/compiler/src/dotty/tools/dotc/transform/Memoize.scala +++ b/compiler/src/dotty/tools/dotc/transform/Memoize.scala @@ -102,7 +102,7 @@ import Decorators._ case _ => t } skipBlocks(tree.rhs) match { - case lit: Literal if sym.is(Final) && isIdempotentExpr(tree.rhs) => + case lit: Literal if sym.is(Final, butNot = Mutable) && isIdempotentExpr(tree.rhs) => // duplicating scalac behavior: for final vals that have rhs as constant, we do not create a field // and instead return the value. This seemingly minor optimization has huge effect on initialization // order and the values that can be observed during superconstructor call diff --git a/tests/run/final-var.check b/tests/run/final-var.check new file mode 100644 index 000000000..7565230c0 --- /dev/null +++ b/tests/run/final-var.check @@ -0,0 +1,4 @@ +false +true +false +true diff --git a/tests/run/final-var.scala b/tests/run/final-var.scala new file mode 100644 index 000000000..94a6c453c --- /dev/null +++ b/tests/run/final-var.scala @@ -0,0 +1,20 @@ +object Test { + def main(args: Array[String]): Unit = { + println(Obj.myFinalVar) + Obj.myFinalVar = true + println(Obj.myFinalVar) + + val o = new Cls + println(o.myFinalVar) + o.myFinalVar = true + println(o.myFinalVar) + } +} + +object Obj { + final var myFinalVar: Boolean = false +} + +class Cls { + final var myFinalVar: Boolean = false +} |