Skip to content

Fix ValDef span assignment in PatternMatcher #15783

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

Merged
merged 1 commit into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,8 @@ object PatternMatcher {
}
emitWithMashedConditions(plan :: Nil)
case LetPlan(sym, body) =>
seq(ValDef(sym, initializer(sym).ensureConforms(sym.info)) :: Nil, emit(body))
val valDef = ValDef(sym, initializer(sym).ensureConforms(sym.info)).withSpan(sym.span)
seq(valDef :: Nil, emit(body))
case LabeledPlan(label, expr) =>
Labeled(label, emit(expr))
case ReturnPlan(label) =>
Expand Down
84 changes: 84 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,90 @@ class DottyBytecodeTests extends DottyBytecodeTest {
testSig("bar", "()I")
}
}

@Test def i15535 = {
// primary goal of this test is to check that `LineNumber` have correct numbers
val source =
"""object Main {
| def m(x: Int): Unit = {
| x match {
| case y =>
| println(y)
| println(y)
| }
| }
|}
""".stripMargin

checkBCode(source) { dir =>
val clsIn = dir.lookupName("Main$.class", directory = false).input
val clsNode = loadClassNode(clsIn, skipDebugInfo = false)
val method = getMethod(clsNode, "m")
val instructions = instructionsFromMethod(method).filter(_.isInstanceOf[LineNumber])

val expected = List(
LineNumber(2, Label(0)),
LineNumber(3, Label(0)),
LineNumber(4, Label(5)), // case y =>
LineNumber(5, Label(9)),
LineNumber(6, Label(15)),
)

assertSameCode(instructions, expected)
}
}

@Test def i15535_2 = {
// primary goal of this test is to check that `LineNumber` have correct numbers
val source =
"""object Main {
| def m(x: Matchable): Unit = {
| x match {
| case a if a == 3 =>
| println(a)
| println(a)
| case b: Int =>
| println(b)
| println(b)
| case c @ Left(l) =>
| println(l)
| println(c)
| case d =>
| println(d)
| println(d)
| println(d)
| }
| }
|}
""".stripMargin

checkBCode(source) { dir =>
val clsIn = dir.lookupName("Main$.class", directory = false).input
val clsNode = loadClassNode(clsIn, skipDebugInfo = false)
val method = getMethod(clsNode, "m")
val instructions = instructionsFromMethod(method).filter(_.isInstanceOf[LineNumber])

val expected = List(
LineNumber(2, Label(0)),
LineNumber(3, Label(0)),
LineNumber(4, Label(5)), // case a if a == 3 =>
LineNumber(5, Label(15)),
LineNumber(6, Label(20)),
LineNumber(7, Label(26)), // case b: Int =>
LineNumber(8, Label(35)),
LineNumber(9, Label(41)),
LineNumber(10, Label(48)), // case c @ Left(l) =>
LineNumber(11, Label(63)),
LineNumber(12, Label(68)),
LineNumber(13, Label(74)), // case d =>
LineNumber(14, Label(79)),
LineNumber(15, Label(84)),
LineNumber(16, Label(89)),
)

assertSameCode(instructions, expected)
}
}
}

object invocationReceiversTestCode {
Expand Down