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
"final var", public fields (!) and the inliner #3569
Comments
Imported From: https://issues.scala-lang.org/browse/SI-3569?orig=1 |
@dragos said: Mutation on final fields is allowed (at the JVM level) only from the defining class of the field. This is definitely a bug on the part of the inliner, anyway. If you run the code it throws an Paul, since you are 'cozy' with the inliner, would you like to tweak the safety check to disallow this? |
@paulp said: final class D(val i: Int) {
def f = i*2
}
// public final int i; On the other hand the example in the ticket doesn't generate a runtime error anymore, because apparently nothing is inlined. Which is either another bug (I am having a lot of trouble matching up what I imagine the inliner to do with what I see it doing) or some penetrating insight on its part. |
@adriaanm said (edited on Mar 7, 2012 2:45:35 PM UTC): this produces bytecode that assigns repeatedly to vars marked final, final class Box {
private[this] final var TMP:Object = null
final def put(o:Object) {
TMP = o
}
final def get() = {
val tmp = TMP
TMP = null
tmp
}
}
class Client(box:Box) {
var count = 0
final def run(cycles:Int) {
if (cycles > 0) {
box.put(new Object)
if (box.get() != null) count += 1
run(cycles - 1)
}
}
}
object Main {
def main(args:Array[String]) {
val c = new Client(new Box)
val N = 1000000000
c.run(N)
assert(c.count == N, c.count)
}
}
$ sbt run
[error] (run-main) java.lang.AssertionError: assertion failed: 999982178
java.lang.AssertionError: assertion failed: 999982178
at scala.Predef$.assert(Predef.scala:103)
at Main$.main(Bug.scala:33)
at Main.main(Bug.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
$ java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Server VM (build 20.1-b02, mixed mode)
$ scala -version
Scala code runner version 2.9.1.final -- Copyright 2002-2011, LAMP/EPFL
|
@odersky said: |
@paulp said: |
Discovered looking at inliner tickets.
Now if you turn on -Yinliner:
public final int x;
and the result:
The text was updated successfully, but these errors were encountered: