Skip to content

Commit

Permalink
backend computes line number from source of position
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Oct 14, 2024
1 parent dbce4e8 commit c6b23cc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 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 @@ -623,7 +623,13 @@ trait BCodeSkelBuilder extends BCodeHelpers {
}

if (emitLines && tree.span.exists && !tree.hasAttachment(SyntheticUnit)) {
val nr = ctx.source.offsetToLine(tree.span.point) + 1
val nr =
val sourcePos = tree.sourcePos
(
if sourcePos.exists then sourcePos.finalPosition.line
else ctx.source.offsetToLine(tree.span.point) // fallback
) + 1

if (nr != lastEmittedLineNr) {
lastEmittedLineNr = nr
getNonLabelNode(lastInsn) match {
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/util/SourceFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
* For regular source files, simply return the argument.
*/
def positionInUltimateSource(position: SourcePosition): SourcePosition =
SourcePosition(underlying, position.span shift start)
if isSelfContained then position // return the argument
else SourcePosition(underlying, position.span shift start)

private def calculateLineIndicesFromContents() = {
val cs = content()
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/util/SourcePosition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ extends SrcPos, interfaces.SourcePosition, Showable {
rec(this)
}

def finalPosition: SourcePosition = {
source.positionInUltimateSource(this)
}


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

0 comments on commit c6b23cc

Please sign in to comment.