Skip to content
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

Cross-referencing lazy vals lead to stack overflow #3253

Closed
scabug opened this issue Apr 4, 2010 · 4 comments
Closed

Cross-referencing lazy vals lead to stack overflow #3253

scabug opened this issue Apr 4, 2010 · 4 comments

Comments

@scabug
Copy link

scabug commented Apr 4, 2010

Cross-references in lazy vals initialization is not detected by compiler as a compilation error and leads to stack overflow while executing the code.

Here is a small script example:

class Test2 {
  lazy val lazy1: Int = 2 * lazy2
  lazy val lazy2: Int = 3 * lazy1
}

val test2 = new Test2()
println(test2.lazy1)

And here is the script execution output:

java.lang.StackOverflowError
        at Main$$$$anon$$1$$Test2.lazy2((virtual file):8)
        at Main$$$$anon$$1$$Test2.lazy1((virtual file):7)
        at Main$$$$anon$$1$$Test2.lazy2((virtual file):8)
        at Main$$$$anon$$1$$Test2.lazy1((virtual file):7)
        ...
@scabug
Copy link
Author

scabug commented Apr 4, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3253?orig=1
Reporter: Eduard Shustrov (neonice)

@scabug
Copy link
Author

scabug commented Apr 4, 2010

Eduard Shustrov (neonice) said:
The bug has been tested sucessfully against (the bug presents in) versions 2.7.7.final and 2.8.0.r21325-b20100404020122.

@scabug
Copy link
Author

scabug commented Apr 7, 2010

@dragos said:
I wouldn't call this a bug. There is no guarantee in the spec that scalac catches such cases, and static checking of initialization is still being researched. In the presence of overriding, this becomes much more difficult:

class Test2 {
  lazy val lazy1: Int = 2
  lazy val lazy2: Int = 3 * lazy1
}

class Test extends Test2 {
  override lazy val lazy1: Int = 2  * lazy2
}

object Test extends Application {
  val x: Test2 = new Test2
  val y: Test2 = new Test

  println(y.lazy2)
}

These classes may be compiled separately, so I don't see any way to guarantee correct errors. I agree that a warning is desirable, but it would work only in cases so obvious that it's not worth the effort.

@scabug
Copy link
Author

scabug commented Apr 7, 2010

Eduard Shustrov (neonice) said:
OK. Let's consider this case to be not a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant