Skip to content

Commit

Permalink
Fix #15107: Avoid re-emitting a LineNumber after only LabelNodes.
Browse files Browse the repository at this point in the history
There was already some deduplication code to avoid consecutive
`LineNumber` nodes. However, it can happen that `LabelNode`s
appear in-between. In that case, we also want to deduplicate the
`LineNumber`s, since labels do not actually contribute to the
final bytecode.
  • Loading branch information
sjrd committed Feb 2, 2023
1 parent af95ceb commit 5df222f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -556,11 +556,17 @@ trait BCodeSkelBuilder extends BCodeHelpers {
case _ => false } )
}
def lineNumber(tree: Tree): Unit = {
@tailrec
def getNonLabelNode(a: asm.tree.AbstractInsnNode): asm.tree.AbstractInsnNode = a match {
case a: asm.tree.LabelNode => getNonLabelNode(a.getPrevious)
case _ => a
}

if (!emitLines || !tree.span.exists) return;
val nr = ctx.source.offsetToLine(tree.span.point) + 1
if (nr != lastEmittedLineNr) {
lastEmittedLineNr = nr
lastInsn match {
getNonLabelNode(lastInsn) match {
case lnn: asm.tree.LineNumberNode =>
// overwrite previous landmark as no instructions have been emitted for it
lnn.line = nr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,6 @@ class DottyBytecodeTests extends DottyBytecodeTest {
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)),
Expand Down Expand Up @@ -1664,7 +1663,6 @@ class DottyBytecodeTests extends DottyBytecodeTest {
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)),
Expand Down

0 comments on commit 5df222f

Please sign in to comment.