|
| 1 | +package dotty.tools.backend.jvm |
| 2 | + |
| 3 | +import scala.language.unsafeNulls |
| 4 | + |
| 5 | +import org.junit.Assert._ |
| 6 | +import org.junit.Test |
| 7 | + |
| 8 | +class SourcePositionsTest extends DottyBytecodeTest: |
| 9 | + import ASMConverters._ |
| 10 | + |
| 11 | + @Test def issue18238_a(): Unit = { |
| 12 | + val code = |
| 13 | + """ |
| 14 | + |class Test { |
| 15 | + | def test(): Unit = { |
| 16 | + | var x = 3 |
| 17 | + | var y = 2 |
| 18 | + | while(true) { |
| 19 | + | if (x < y) |
| 20 | + | if (x >= y) |
| 21 | + | x += 1 |
| 22 | + | else |
| 23 | + | y -= 1 |
| 24 | + | } |
| 25 | + | } |
| 26 | + |}""".stripMargin |
| 27 | + |
| 28 | + checkBCode(code) { dir => |
| 29 | + val testClass = loadClassNode(dir.lookupName("Test.class", directory = false).input, skipDebugInfo = false) |
| 30 | + val testMethod = getMethod(testClass, "test") |
| 31 | + val lineNumbers = instructionsFromMethod(testMethod).collect{case ln: LineNumber => ln} |
| 32 | + val expected = List( |
| 33 | + LineNumber(4, Label(0)), // var x |
| 34 | + LineNumber(5, Label(4)), // var y |
| 35 | + LineNumber(6, Label(8)), // while(true) |
| 36 | + LineNumber(7, Label(13)), // if (x < y) |
| 37 | + LineNumber(8, Label(18)), // if (x >= y) |
| 38 | + LineNumber(9, Label(23)), // x += 1 |
| 39 | + LineNumber(11, Label(27)), // y -= 1 |
| 40 | + LineNumber(7, Label(32)) // <synthetic unit> point back to `if (x < y) |
| 41 | + ) |
| 42 | + assertEquals(expected, lineNumbers) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test def issue18238_b(): Unit = { |
| 47 | + val code = |
| 48 | + """ |
| 49 | + |class Test { |
| 50 | + | def test(): Unit = { |
| 51 | + | var x = 3 |
| 52 | + | var y = 2 |
| 53 | + | while(true) { |
| 54 | + | if (x < y) |
| 55 | + | if (x >= y) |
| 56 | + | x += 1 |
| 57 | + | else |
| 58 | + | y -= 1 |
| 59 | + | else () |
| 60 | + | } |
| 61 | + | } |
| 62 | + |}""".stripMargin |
| 63 | + |
| 64 | + checkBCode(code) { dir => |
| 65 | + val testClass = loadClassNode(dir.lookupName("Test.class", directory = false).input, skipDebugInfo = false) |
| 66 | + val testMethod = getMethod(testClass, "test") |
| 67 | + val lineNumbers = instructionsFromMethod(testMethod).collect{case ln: LineNumber => ln} |
| 68 | + val expected = List( |
| 69 | + LineNumber(4, Label(0)), // var x |
| 70 | + LineNumber(5, Label(4)), // var y |
| 71 | + LineNumber(6, Label(8)), // while(true) |
| 72 | + LineNumber(7, Label(13)), // if (x < y) |
| 73 | + LineNumber(8, Label(18)), // if (x >= y) |
| 74 | + LineNumber(9, Label(23)), // x += 1 |
| 75 | + LineNumber(11, Label(27)), // y -= 1 |
| 76 | + LineNumber(12, Label(32)) // else () |
| 77 | + ) |
| 78 | + assertEquals(expected, lineNumbers) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test def issue18238_c(): Unit = { |
| 83 | + val code = |
| 84 | + """ |
| 85 | + |class Test { |
| 86 | + | def test(): Unit = { |
| 87 | + | var x = 3 |
| 88 | + | var y = 2 |
| 89 | + | while(true) { |
| 90 | + | if (x < y) |
| 91 | + | if (x >= y) |
| 92 | + | x += 1 |
| 93 | + | else |
| 94 | + | y -= 1 |
| 95 | + | println() |
| 96 | + | } |
| 97 | + | } |
| 98 | + |}""".stripMargin |
| 99 | + |
| 100 | + checkBCode(code) { dir => |
| 101 | + val testClass = loadClassNode(dir.lookupName("Test.class", directory = false).input, skipDebugInfo = false) |
| 102 | + val testMethod = getMethod(testClass, "test") |
| 103 | + val lineNumbers = instructionsFromMethod(testMethod).collect{case ln: LineNumber => ln} |
| 104 | + val expected = List( |
| 105 | + LineNumber(4, Label(0)), // var x |
| 106 | + LineNumber(5, Label(4)), // var y |
| 107 | + LineNumber(6, Label(8)), // while(true) |
| 108 | + LineNumber(7, Label(13)), // if (x < y) |
| 109 | + LineNumber(8, Label(18)), // if (x >= y) |
| 110 | + LineNumber(9, Label(23)), // x += 1 |
| 111 | + LineNumber(11, Label(27)), // y -= 1 |
| 112 | + LineNumber(12, Label(31)) // println() |
| 113 | + ) |
| 114 | + assertEquals(expected, lineNumbers) |
| 115 | + } |
| 116 | + } |
0 commit comments