forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve source positions emited for synthetic unit in if-conditions (s…
…cala#20431) Fixes scala#18238 Source positions (lines) emitted for synthetic unit in if-conditions were incorrect, because they were missing explicit line position for the else condition (introduced by the compiler as synthetic unit) the debugger when stepping into the `else` branch it was using the last position of the `then` expression. Now, we explicitly use the line position of the condition for the synthetic `else` branch - it matches the behaviour of Scala 2. * Use `SyntheticUnit` and introduce `untpd.syntheticUnitLiteral` to detect if `else` branch is defined - allows to emit correct positions for the explicit unit `else` branch
- Loading branch information
Showing
7 changed files
with
134 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
compiler/test/dotty/tools/backend/jvm/SourcePositionsTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package dotty.tools.backend.jvm | ||
|
||
import scala.language.unsafeNulls | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
|
||
class SourcePositionsTest extends DottyBytecodeTest: | ||
import ASMConverters._ | ||
|
||
@Test def issue18238_a(): Unit = { | ||
val code = | ||
""" | ||
|class Test { | ||
| def test(): Unit = { | ||
| var x = 3 | ||
| var y = 2 | ||
| while(true) { | ||
| if (x < y) | ||
| if (x >= y) | ||
| x += 1 | ||
| else | ||
| y -= 1 | ||
| } | ||
| } | ||
|}""".stripMargin | ||
|
||
checkBCode(code) { dir => | ||
val testClass = loadClassNode(dir.lookupName("Test.class", directory = false).input, skipDebugInfo = false) | ||
val testMethod = getMethod(testClass, "test") | ||
val lineNumbers = instructionsFromMethod(testMethod).collect{case ln: LineNumber => ln} | ||
val expected = List( | ||
LineNumber(4, Label(0)), // var x | ||
LineNumber(5, Label(4)), // var y | ||
LineNumber(6, Label(8)), // while(true) | ||
LineNumber(7, Label(13)), // if (x < y) | ||
LineNumber(8, Label(18)), // if (x >= y) | ||
LineNumber(9, Label(23)), // x += 1 | ||
LineNumber(11, Label(27)), // y -= 1 | ||
LineNumber(7, Label(32)) // <synthetic unit> point back to `if (x < y) | ||
) | ||
assertEquals(expected, lineNumbers) | ||
} | ||
} | ||
|
||
@Test def issue18238_b(): Unit = { | ||
val code = | ||
""" | ||
|class Test { | ||
| def test(): Unit = { | ||
| var x = 3 | ||
| var y = 2 | ||
| while(true) { | ||
| if (x < y) | ||
| if (x >= y) | ||
| x += 1 | ||
| else | ||
| y -= 1 | ||
| else () | ||
| } | ||
| } | ||
|}""".stripMargin | ||
|
||
checkBCode(code) { dir => | ||
val testClass = loadClassNode(dir.lookupName("Test.class", directory = false).input, skipDebugInfo = false) | ||
val testMethod = getMethod(testClass, "test") | ||
val lineNumbers = instructionsFromMethod(testMethod).collect{case ln: LineNumber => ln} | ||
val expected = List( | ||
LineNumber(4, Label(0)), // var x | ||
LineNumber(5, Label(4)), // var y | ||
LineNumber(6, Label(8)), // while(true) | ||
LineNumber(7, Label(13)), // if (x < y) | ||
LineNumber(8, Label(18)), // if (x >= y) | ||
LineNumber(9, Label(23)), // x += 1 | ||
LineNumber(11, Label(27)), // y -= 1 | ||
LineNumber(12, Label(32)) // else () | ||
) | ||
assertEquals(expected, lineNumbers) | ||
} | ||
} | ||
|
||
@Test def issue18238_c(): Unit = { | ||
val code = | ||
""" | ||
|class Test { | ||
| def test(): Unit = { | ||
| var x = 3 | ||
| var y = 2 | ||
| while(true) { | ||
| if (x < y) | ||
| if (x >= y) | ||
| x += 1 | ||
| else | ||
| y -= 1 | ||
| println() | ||
| } | ||
| } | ||
|}""".stripMargin | ||
|
||
checkBCode(code) { dir => | ||
val testClass = loadClassNode(dir.lookupName("Test.class", directory = false).input, skipDebugInfo = false) | ||
val testMethod = getMethod(testClass, "test") | ||
val lineNumbers = instructionsFromMethod(testMethod).collect{case ln: LineNumber => ln} | ||
val expected = List( | ||
LineNumber(4, Label(0)), // var x | ||
LineNumber(5, Label(4)), // var y | ||
LineNumber(6, Label(8)), // while(true) | ||
LineNumber(7, Label(13)), // if (x < y) | ||
LineNumber(8, Label(18)), // if (x >= y) | ||
LineNumber(9, Label(23)), // x += 1 | ||
LineNumber(11, Label(27)), // y -= 1 | ||
LineNumber(12, Label(31)) // println() | ||
) | ||
assertEquals(expected, lineNumbers) | ||
} | ||
} |