From dec026d615b7bf9fb85e739e1c2a0d3ec0f6d93c Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Mon, 14 Oct 2024 15:20:12 +0200 Subject: [PATCH] backend computes line number from source of position [Cherry-picked c6b23cc2944c183a36bc607e1eed9361cc9e3b83] --- .../src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala | 8 +++++++- compiler/src/dotty/tools/dotc/util/SourceFile.scala | 3 ++- compiler/src/dotty/tools/dotc/util/SourcePosition.scala | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala b/compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala index 0ab9ed85b6cf..3ed21a6de3e8 100644 --- a/compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala +++ b/compiler/src/dotty/tools/backend/jvm/BCodeSkelBuilder.scala @@ -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 { diff --git a/compiler/src/dotty/tools/dotc/util/SourceFile.scala b/compiler/src/dotty/tools/dotc/util/SourceFile.scala index 48675b6b8eae..246255ae3802 100644 --- a/compiler/src/dotty/tools/dotc/util/SourceFile.scala +++ b/compiler/src/dotty/tools/dotc/util/SourceFile.scala @@ -118,7 +118,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() diff --git a/compiler/src/dotty/tools/dotc/util/SourcePosition.scala b/compiler/src/dotty/tools/dotc/util/SourcePosition.scala index 904704b2349c..384d2f1fb2f3 100644 --- a/compiler/src/dotty/tools/dotc/util/SourcePosition.scala +++ b/compiler/src/dotty/tools/dotc/util/SourcePosition.scala @@ -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"