Skip to content

Commit

Permalink
Fix Positioned#cloneIn to also update the source
Browse files Browse the repository at this point in the history
Fixes scala#13626 (regression introduced in scala#9900).
  • Loading branch information
mbovel committed Sep 30, 2021
1 parent 71acf8d commit abbf09f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/Positioned.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions tests/run/splice-position.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Test$.main(Test.scala:3)
Test$.main(Test.scala:4)
4 changes: 4 additions & 0 deletions tests/run/splice-position/Test.scala
Original file line number Diff line number Diff line change
@@ -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))
7 changes: 7 additions & 0 deletions tests/run/splice-position/macros.scala
Original file line number Diff line number Diff line change
@@ -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(${'{""}}) }

0 comments on commit abbf09f

Please sign in to comment.