Skip to content

Commit 34f56c2

Browse files
authored
Merge pull request #15783 from Florian3k/fix-15535
Fix ValDef span assignment in PatternMatcher
2 parents b3df0ef + d3d694f commit 34f56c2

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,8 @@ object PatternMatcher {
932932
}
933933
emitWithMashedConditions(plan :: Nil)
934934
case LetPlan(sym, body) =>
935-
seq(ValDef(sym, initializer(sym).ensureConforms(sym.info)) :: Nil, emit(body))
935+
val valDef = ValDef(sym, initializer(sym).ensureConforms(sym.info)).withSpan(sym.span)
936+
seq(valDef :: Nil, emit(body))
936937
case LabeledPlan(label, expr) =>
937938
Labeled(label, emit(expr))
938939
case ReturnPlan(label) =>

compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,6 +1577,90 @@ class DottyBytecodeTests extends DottyBytecodeTest {
15771577
testSig("bar", "()I")
15781578
}
15791579
}
1580+
1581+
@Test def i15535 = {
1582+
// primary goal of this test is to check that `LineNumber` have correct numbers
1583+
val source =
1584+
"""object Main {
1585+
| def m(x: Int): Unit = {
1586+
| x match {
1587+
| case y =>
1588+
| println(y)
1589+
| println(y)
1590+
| }
1591+
| }
1592+
|}
1593+
""".stripMargin
1594+
1595+
checkBCode(source) { dir =>
1596+
val clsIn = dir.lookupName("Main$.class", directory = false).input
1597+
val clsNode = loadClassNode(clsIn, skipDebugInfo = false)
1598+
val method = getMethod(clsNode, "m")
1599+
val instructions = instructionsFromMethod(method).filter(_.isInstanceOf[LineNumber])
1600+
1601+
val expected = List(
1602+
LineNumber(2, Label(0)),
1603+
LineNumber(3, Label(0)),
1604+
LineNumber(4, Label(5)), // case y =>
1605+
LineNumber(5, Label(9)),
1606+
LineNumber(6, Label(15)),
1607+
)
1608+
1609+
assertSameCode(instructions, expected)
1610+
}
1611+
}
1612+
1613+
@Test def i15535_2 = {
1614+
// primary goal of this test is to check that `LineNumber` have correct numbers
1615+
val source =
1616+
"""object Main {
1617+
| def m(x: Matchable): Unit = {
1618+
| x match {
1619+
| case a if a == 3 =>
1620+
| println(a)
1621+
| println(a)
1622+
| case b: Int =>
1623+
| println(b)
1624+
| println(b)
1625+
| case c @ Left(l) =>
1626+
| println(l)
1627+
| println(c)
1628+
| case d =>
1629+
| println(d)
1630+
| println(d)
1631+
| println(d)
1632+
| }
1633+
| }
1634+
|}
1635+
""".stripMargin
1636+
1637+
checkBCode(source) { dir =>
1638+
val clsIn = dir.lookupName("Main$.class", directory = false).input
1639+
val clsNode = loadClassNode(clsIn, skipDebugInfo = false)
1640+
val method = getMethod(clsNode, "m")
1641+
val instructions = instructionsFromMethod(method).filter(_.isInstanceOf[LineNumber])
1642+
1643+
val expected = List(
1644+
LineNumber(2, Label(0)),
1645+
LineNumber(3, Label(0)),
1646+
LineNumber(4, Label(5)), // case a if a == 3 =>
1647+
LineNumber(5, Label(15)),
1648+
LineNumber(6, Label(20)),
1649+
LineNumber(7, Label(26)), // case b: Int =>
1650+
LineNumber(8, Label(35)),
1651+
LineNumber(9, Label(41)),
1652+
LineNumber(10, Label(48)), // case c @ Left(l) =>
1653+
LineNumber(11, Label(63)),
1654+
LineNumber(12, Label(68)),
1655+
LineNumber(13, Label(74)), // case d =>
1656+
LineNumber(14, Label(79)),
1657+
LineNumber(15, Label(84)),
1658+
LineNumber(16, Label(89)),
1659+
)
1660+
1661+
assertSameCode(instructions, expected)
1662+
}
1663+
}
15801664
}
15811665

15821666
object invocationReceiversTestCode {

0 commit comments

Comments
 (0)