Skip to content

Commit 02395c4

Browse files
authored
Merge pull request #13644 from dotty-staging/mb/splice-position-backport
[Backport] Fix Positioned#cloneIn to also update the source
2 parents 71acf8d + abbf09f commit 02395c4

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

compiler/src/dotty/tools/dotc/ast/Positioned.scala

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src
2323

2424
private var mySpan: Span = _
2525

26+
private var mySource: SourceFile = src
27+
2628
/** A unique identifier in case -Yshow-tree-ids, or -Ydebug-tree-with-id
2729
* is set, -1 otherwise.
2830
*/
@@ -48,7 +50,8 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Src
4850

4951
span = envelope(src)
5052

51-
val source: SourceFile = src
53+
def source: SourceFile = mySource
54+
5255
def sourcePos(using Context): SourcePosition = source.atSpan(span)
5356

5457
/** 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
127130
def cloneIn(src: SourceFile): this.type = {
128131
val newpd: this.type = clone.asInstanceOf[this.type]
129132
newpd.allocateId()
130-
// assert(newpd.uniqueId != 2208, s"source = $this, ${this.uniqueId}, ${this.span}")
133+
newpd.mySource = src
131134
newpd
132135
}
133136

tests/run/splice-position.check

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Test$.main(Test.scala:3)
2+
Test$.main(Test.scala:4)

tests/run/splice-position/Test.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Test:
2+
def main(args: Array[String]) =
3+
try assertTrue(1 == 2) catch e => println(e.getStackTrace()(0))
4+
try assertTrue(1 == 3) catch e => println(e.getStackTrace()(0))
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.quoted.{Quotes, Expr, quotes}
2+
3+
inline def assertTrue(cond: Boolean) =
4+
${ assertTrueImpl('cond) }
5+
6+
def assertTrueImpl(cond: Expr[Boolean])(using Quotes) =
7+
'{ if (!$cond) throw new Error(${'{""}}) }

0 commit comments

Comments
 (0)