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

Wrong debug information emitted in a while loop and if statement combination #18238

Closed
vasilmkd opened this issue Jul 18, 2023 · 0 comments · Fixed by #20431
Closed

Wrong debug information emitted in a while loop and if statement combination #18238

vasilmkd opened this issue Jul 18, 2023 · 0 comments · Fixed by #20431
Assignees
Milestone

Comments

@vasilmkd
Copy link
Contributor

vasilmkd commented Jul 18, 2023

Compiler version

3.3.0, 3.3.1-RC4 (but seems to be true for all Scala 3 versions, although I haven't checked them exhaustively)

Minimized code

object Reproduction:
  def method(): Int =
    var x = 3
    var y = 2

    while true do
      if x < y then      // always false because x and y are not modified below, PLACE BREAKPOINT ON THIS LINE
        if x >= y then
          x += 1
        else
          y -= 1         // stepping over with the debugger claims to stop on this line
    end while

    5
  end method

  def main(args: Array[String]): Unit =
    println(method())
end Reproduction

The combination of while loop, if statement in the body of the loop and no other code following the if statement in the while loop body triggers wrong debugging information to be emitted by the compiler. This is verified by using the step over functionality of both the IntelliJ debugger and the Metals debugger. Both debuggers stop on the y -= 1 line, but this line is never really executed in the code, which is confirmed when looking at the local variables which correctly don't change.

Modifying the code to:

object Reproduction:
  def method(): Int =
    var x = 3
    var y = 2

    while true do
      if x < y then      // always false because x and y are not modified below, PLACE BREAKPOINT ON THIS LINE
        if x >= y then
          x += 1
        else
          y -= 1

      println("Hello")
    end while

    5
  end method

  def main(args: Array[String]): Unit =
    println(method())
end Reproduction

results in correct stepping through in the debugger.

Output

Video of the IntelliJ debugger:

Screen.Recording.2023-07-18.at.12.59.47.mov

Video of the Metals debugger, same behavior:

Screen.Recording.2023-07-18.at.13.01.12.mov

Expected output

The debugger doesn't stop on the y -= 1 line, because the program never executes that statement.

Original sighting of the bug

https://www.youtube.com/watch?v=hs_NdVjnO9U

@vasilmkd vasilmkd added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 18, 2023
@bishabosha bishabosha added area:tooling and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 18, 2023
@Kordyjan Kordyjan self-assigned this Jul 19, 2023
@Gedochao Gedochao assigned WojciechMazur and unassigned Kordyjan Feb 19, 2024
sjrd added a commit to dotty-staging/dotty that referenced this issue Jun 26, 2024
If we don't, the code generated for the `else` branch remains
associated with the last instruction of the `then` branch, which
yields to confusing debugging sessions.
@sjrd sjrd closed this as completed in 796a7b9 Jun 26, 2024
@Kordyjan Kordyjan added this to the 3.5.1 milestone Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants