Skip to content

Commit 5df222f

Browse files
committed
Fix #15107: Avoid re-emitting a LineNumber after only LabelNodes.
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.
1 parent af95ceb commit 5df222f

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,11 +556,17 @@ trait BCodeSkelBuilder extends BCodeHelpers {
556556
case _ => false } )
557557
}
558558
def lineNumber(tree: Tree): Unit = {
559+
@tailrec
560+
def getNonLabelNode(a: asm.tree.AbstractInsnNode): asm.tree.AbstractInsnNode = a match {
561+
case a: asm.tree.LabelNode => getNonLabelNode(a.getPrevious)
562+
case _ => a
563+
}
564+
559565
if (!emitLines || !tree.span.exists) return;
560566
val nr = ctx.source.offsetToLine(tree.span.point) + 1
561567
if (nr != lastEmittedLineNr) {
562568
lastEmittedLineNr = nr
563-
lastInsn match {
569+
getNonLabelNode(lastInsn) match {
564570
case lnn: asm.tree.LineNumberNode =>
565571
// overwrite previous landmark as no instructions have been emitted for it
566572
lnn.line = nr

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,6 @@ class DottyBytecodeTests extends DottyBytecodeTest {
16221622
val instructions = instructionsFromMethod(method).filter(_.isInstanceOf[LineNumber])
16231623

16241624
val expected = List(
1625-
LineNumber(2, Label(0)),
16261625
LineNumber(3, Label(0)),
16271626
LineNumber(4, Label(5)), // case y =>
16281627
LineNumber(5, Label(9)),
@@ -1664,7 +1663,6 @@ class DottyBytecodeTests extends DottyBytecodeTest {
16641663
val instructions = instructionsFromMethod(method).filter(_.isInstanceOf[LineNumber])
16651664

16661665
val expected = List(
1667-
LineNumber(2, Label(0)),
16681666
LineNumber(3, Label(0)),
16691667
LineNumber(4, Label(5)), // case a if a == 3 =>
16701668
LineNumber(5, Label(15)),

0 commit comments

Comments
 (0)