Skip to content

Commit b60d505

Browse files
committed
Set positions for constructed trees manually
1 parent 359064c commit b60d505

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ trait TreeUtils {
1616
implicit val bodyTpe: quoted.Type[U] = bodyType.seal.asInstanceOf[quoted.Type[U]]
1717
implicit val rhsTpe: quoted.Type[T] = rhs.tpe.seal.asInstanceOf[quoted.Type[T]]
1818
val rhsExpr = rhs.seal[T]
19-
let[T, U](rhsExpr)(x => body(x.unseal.asInstanceOf[Term.Ident]).seal[U]).unseal
19+
20+
let[T, U](rhsExpr) { x =>
21+
val id = ('(x)).unseal.asInstanceOf[Term.Ident]
22+
id.withPos(rhs.pos)
23+
body(id).seal[U]
24+
}.unseal
2025
}
2126

22-
/** */
2327
private def let[T: quoted.Type, U: quoted.Type](rhs: Expr[T])(in: Expr[T] => Expr[U]): Expr[U] = '{
2428
val x = ~rhs
2529
~in('(x))
2630
}
27-
2831
}

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)