diff --git a/compiler/src/dotty/tools/dotc/ast/Positioned.scala b/compiler/src/dotty/tools/dotc/ast/Positioned.scala index 75f210d58704..964b9855ae13 100644 --- a/compiler/src/dotty/tools/dotc/ast/Positioned.scala +++ b/compiler/src/dotty/tools/dotc/ast/Positioned.scala @@ -23,6 +23,8 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src private var mySpan: Span = _ + private var mySource: SourceFile = src + /** A unique identifier in case -Yshow-tree-ids, or -Ydebug-tree-with-id * is set, -1 otherwise. */ @@ -48,7 +50,8 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src span = envelope(src) - val source: SourceFile = src + def source: SourceFile = mySource + def sourcePos(using Context): SourcePosition = source.atSpan(span) /** This positioned item, widened to `SrcPos`. Used to make clear we only need the @@ -127,7 +130,7 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src def cloneIn(src: SourceFile): this.type = { val newpd: this.type = clone.asInstanceOf[this.type] newpd.allocateId() - // assert(newpd.uniqueId != 2208, s"source = $this, ${this.uniqueId}, ${this.span}") + newpd.mySource = src newpd } diff --git a/tests/run/splice-position.check b/tests/run/splice-position.check new file mode 100644 index 000000000000..904f7c9c2efd --- /dev/null +++ b/tests/run/splice-position.check @@ -0,0 +1,2 @@ +Test$.main(Test.scala:3) +Test$.main(Test.scala:4) diff --git a/tests/run/splice-position/Test.scala b/tests/run/splice-position/Test.scala new file mode 100644 index 000000000000..0bb354cf0115 --- /dev/null +++ b/tests/run/splice-position/Test.scala @@ -0,0 +1,4 @@ +object Test: + def main(args: Array[String]) = + try assertTrue(1 == 2) catch e => println(e.getStackTrace()(0)) + try assertTrue(1 == 3) catch e => println(e.getStackTrace()(0)) diff --git a/tests/run/splice-position/macros.scala b/tests/run/splice-position/macros.scala new file mode 100644 index 000000000000..b575a460b6e5 --- /dev/null +++ b/tests/run/splice-position/macros.scala @@ -0,0 +1,7 @@ +import scala.quoted.{Quotes, Expr, quotes} + +inline def assertTrue(cond: Boolean) = + ${ assertTrueImpl('cond) } + +def assertTrueImpl(cond: Expr[Boolean])(using Quotes) = + '{ if (!$cond) throw new Error(${'{""}}) }