Skip to content

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

Closed
@vasilmkd

Description

@vasilmkd

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

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions