Skip to content

trivial lazy self-reference deadlocks on reference ... can we detect this case? #12943

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
tpolecat opened this issue Jun 25, 2021 · 3 comments
Closed

Comments

@tpolecat
Copy link

Compiler version

3.0.0

Minimized code

scala> lazy val a: Int = a                                                                          
lazy val a: Int

scala> a         
<hangs>

Suggestion

In Scala 2 this situation results in a stack overflow, but in Scala 3 it hangs awaiting an object monitor. It also happens with given values that reference themselves directly, which seem to desugar to a lazy val. I ran into this when someone new to doobie was trying to bring an instance into scope by saying

given Meta[Json] = Meta[Json]

and his program deadlocked trying to summon the instance. This ends up being a common beginner mistake.

In any case my suggestion is that we detect at least this specific case at compile time because it can be very hard to track down.

@som-snytt
Copy link
Contributor

less trivial #10947

@nicolasstucki
Copy link
Contributor

#13011 propose a way to mitigate simple instances of this issue.

@odersky
Copy link
Contributor

odersky commented Apr 7, 2022

We now get:

-- Warning: --------------------------------------------------------------------
1 |lazy val a: Int = a
  |                  ^
  |                  Infinite loop in function body
  |                  a

@odersky odersky closed this as completed Apr 7, 2022
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

6 participants