Skip to content

Commit c6b23cc

Browse files
committed
backend computes line number from source of position
1 parent dbce4e8 commit c6b23cc

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,13 @@ trait BCodeSkelBuilder extends BCodeHelpers {
623623
}
624624

625625
if (emitLines && tree.span.exists && !tree.hasAttachment(SyntheticUnit)) {
626-
val nr = ctx.source.offsetToLine(tree.span.point) + 1
626+
val nr =
627+
val sourcePos = tree.sourcePos
628+
(
629+
if sourcePos.exists then sourcePos.finalPosition.line
630+
else ctx.source.offsetToLine(tree.span.point) // fallback
631+
) + 1
632+
627633
if (nr != lastEmittedLineNr) {
628634
lastEmittedLineNr = nr
629635
getNonLabelNode(lastInsn) match {

compiler/src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
119119
* For regular source files, simply return the argument.
120120
*/
121121
def positionInUltimateSource(position: SourcePosition): SourcePosition =
122-
SourcePosition(underlying, position.span shift start)
122+
if isSelfContained then position // return the argument
123+
else SourcePosition(underlying, position.span shift start)
123124

124125
private def calculateLineIndicesFromContents() = {
125126
val cs = content()

compiler/src/dotty/tools/dotc/util/SourcePosition.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ extends SrcPos, interfaces.SourcePosition, Showable {
7979
rec(this)
8080
}
8181

82+
def finalPosition: SourcePosition = {
83+
source.positionInUltimateSource(this)
84+
}
85+
8286

8387
override def toString: String =
8488
s"${if (source.exists) source.file.toString else "(no source)"}:$span"

0 commit comments

Comments
 (0)