Skip to content

No "Reference to uninitialized value" warning #15883

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

Closed
Bersier opened this issue Aug 19, 2022 · 4 comments · Fixed by #15984
Closed

No "Reference to uninitialized value" warning #15883

Bersier opened this issue Aug 19, 2022 · 4 comments · Fixed by #15984

Comments

@Bersier
Copy link
Contributor

Bersier commented Aug 19, 2022

Compiler version

3.1.3

Minimized code

val a = b
val b = 1

@main def main(): Unit =
  println(a)

Output

0

Expectation

Expected output: 1. The natural expected behavior is that initialization would follow the dependency graph, and it would be a compiler error if it contains a cycle.

At the very least, the compiler should issue an uninitialized value warning, as in Scala 2, a fortiori when -Ysafe-init is enabled.

As it stands, referential transparency and val immutability are violated without so much as a warning.

@Bersier Bersier added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 19, 2022
@SethTisue
Copy link
Member

SethTisue commented Aug 19, 2022

@szymon-rd szymon-rd added area:transform discussion and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 19, 2022
@keynmol
Copy link
Contributor

keynmol commented Aug 19, 2022

-Ysafe-init doesn't catch it in the presented by the author, but in a modified scenario it does:

object test:
  val a = b
  val b = 1

@main def hello =
  println(test.a)
-- Warning: test.scala:3:6 -----------------------------------------------------
3 |  val b = 1
  |      ^
  |      Access non-initialized value b. Calling trace:
  |      -> val a = b	[ test.scala:2 ]
  |                 ^
1 warning found

@odersky
Copy link
Contributor

odersky commented Aug 20, 2022

Maybe -Ysafe-init can be changed so that it also works for toplevel code?

@liufengyun
Copy link
Contributor

Maybe -Ysafe-init can be changed so that it also works for toplevel code?

I'll have a look, it should behave the same as top-level objects.

liufengyun added a commit to dotty-staging/dotty that referenced this issue Sep 6, 2022
liufengyun added a commit that referenced this issue Sep 10, 2022
Fix #15883: Interpret inner static object access as this access
@Kordyjan Kordyjan added this to the 3.2.2 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment