Skip to content

Commit 6da8f6f

Browse files
committed
Set positions for constructed trees manually
1 parent 605ee7a commit 6da8f6f

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/TreeOpsImpl.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
1212
def TreeDeco(tree: Tree): TreeAPI = new TreeAPI {
1313
def pos(implicit ctx: Context): Position = tree.sourcePos
1414
def symbol(implicit ctx: Context): Symbol = tree.symbol
15+
def withPos(pos: Position)(implicit ctx: Context): Unit = tree.withSpan(pos.span)
1516
}
1617

1718
def PackageClauseDeco(pack: PackageClause): PackageClauseAPI = new PackageClauseAPI {

library/src-bootstrapped/scala/tasty/reflect/utils/TreeUtils.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ trait TreeUtils {
1414
val rhsExpr = rhs.seal[T]
1515
val expr = '{
1616
val x = ~rhsExpr
17-
~in(('(x)).unseal.asInstanceOf[Term.Ident]).seal[Any]
17+
~{
18+
val id = ('(x)).unseal.asInstanceOf[Term.Ident]
19+
id.withPos(rhs.pos)
20+
in(id).seal[Any]
21+
}
1822
}
1923
expr.unseal
2024
}

library/src/scala/tasty/reflect/TreeOps.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ trait TreeOps extends Core {
4545
/** Position in the source code */
4646
def pos(implicit ctx: Context): Position
4747

48+
/** Set the position of the tree
49+
*
50+
* @note It only changes positions, not source file of the tree. Source
51+
* file of trees are derived from context and cannot be changed.
52+
*/
53+
def withPos(pos: Position)(implicit ctx: Context): Unit
54+
4855
def symbol(implicit ctx: Context): Symbol
4956
}
5057

tests/run/reflect-select-constructor/assert_1.scala renamed to tests/run-with-compiler/reflect-select-constructor/assert_1.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ object scalatest {
1414
Type.IsMethodType.unapply(tp).flatMap(tp => if tp.isImplicit then Some(true) else None).nonEmpty
1515

1616
cond.unseal.underlyingArgument match {
17-
case Term.Apply(Term.Select(lhs, op), rhs :: Nil) =>
17+
case t @ Term.Apply(Term.Select(lhs, op), rhs :: Nil) =>
1818
let(lhs) { left =>
1919
let(rhs) { right =>
20-
let(Term.Select.overloaded(left, op, Nil, right :: Nil)) { result =>
20+
val app = Term.Select.overloaded(left, op, Nil, right :: Nil)
21+
app.withPos(t.pos)
22+
let(app) { result =>
2123
val l = left.seal[Any]
2224
val r = right.seal[Any]
2325
val b = result.seal[Boolean]
@@ -30,7 +32,9 @@ object scalatest {
3032
if isImplicitMethodType(f.tpe) =>
3133
let(lhs) { left =>
3234
let(rhs) { right =>
33-
let(Term.Apply(Term.Select.overloaded(Term.Apply(qual, left :: Nil), op, Nil, right :: Nil), implicits)) { result =>
35+
val app = Term.Select.overloaded(Term.Apply(qual, left :: Nil), op, Nil, right :: Nil)
36+
app.withPos(f.pos)
37+
let(Term.Apply(app, implicits)) { result =>
3438
val l = left.seal[Any]
3539
val r = right.seal[Any]
3640
val b = result.seal[Boolean]

0 commit comments

Comments
 (0)