Skip to content

Fix #1878: Generate fields for final vars. #1879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/Memoize.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/run/final-var.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
false
true
false
true
20 changes: 20 additions & 0 deletions tests/run/final-var.scala
Original file line number Diff line number Diff line change
@@ -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
}