Skip to content

Commit bc67630

Browse files
committed
Represent quoted macro trees with tasty.Term
1 parent 6290e1a commit bc67630

File tree

9 files changed

+40
-9
lines changed

9 files changed

+40
-9
lines changed

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ object PickledQuotes {
4141
case value: Class[_] => ref(defn.Predef_classOf).appliedToType(classToType(value))
4242
case value=> Literal(Constant(value))
4343
}
44-
case expr: TreeExpr[Tree] @unchecked => expr.tree
44+
case expr: TreeExpr @unchecked =>
45+
dotty.tools.dotc.tasty.internal.Term.tree(expr.term)
4546
case expr: FunctionAppliedTo[_, _] =>
4647
functionAppliedTo(quotedExprToTree(expr.f), quotedExprToTree(expr.x))
4748
}

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,10 @@ class TreeUnpickler(reader: TastyReader,
11401140
val idx = readNat()
11411141
val args = until(end)(readTerm())
11421142
val splice = splices(idx)
1143-
val reifiedArgs = args.map(arg => if (arg.isTerm) new TreeExpr(arg) else new TreeType(arg))
1143+
def wrap(arg: Tree) =
1144+
if (arg.isTerm) new TreeExpr(dotty.tools.dotc.tasty.internal.Term(arg))
1145+
else new TreeType(arg) // TODO use tasty type
1146+
val reifiedArgs = args.map(wrap)
11441147
if (isType) {
11451148
val quotedType =
11461149
if (reifiedArgs.isEmpty) splice.asInstanceOf[quoted.Type[_]]

compiler/src/dotty/tools/dotc/quoted/Toolbox.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import dotty.tools.dotc.tasty.internal.Term
99

1010
import scala.quoted.Expr
1111
import scala.runtime.BoxedUnit
12-
import scala.quoted.Exprs.LiftedExpr
12+
import scala.quoted.Exprs.{LiftedExpr, TreeExpr}
1313
import scala.runtime.quoted._
1414
import scala.tasty.trees.Term
1515

@@ -45,8 +45,10 @@ object Toolbox {
4545
case _ => new QuoteDriver().show(expr, showSettings)
4646
}
4747

48-
def toTasty(expr: Expr[T]): Term =
49-
new QuoteDriver().withTree(expr, (tree, ctx) => Term(tree)(ctx), Settings.run())
48+
def toTasty(expr: Expr[T]): Term = expr match {
49+
case expr: TreeExpr => expr.term
50+
case _ => new QuoteDriver().withTree(expr, (tree, ctx) => Term(tree)(ctx), Settings.run())
51+
}
5052

5153
}
5254

compiler/src/dotty/tools/dotc/tasty/internal/Term.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ object Term {
155155
case _ => None
156156
}
157157

158+
def tree(term: trees.Term): tpd.Tree = term.asInstanceOf[Impl].tree
159+
158160
private[tasty] class Impl(val tree: tpd.Tree)(implicit val ctx: Context) extends trees.Term with Positioned {
159161

160162
assert(tree.isTerm || tree.isInstanceOf[Trees.NamedArg[_]] || tree.isInstanceOf[Trees.SeqLiteral[_]])

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ object Splicer {
6060
case (arg, tp) =>
6161
assert(!tp.hasAnnotation(defn.InlineParamAnnot))
6262
// Replace argument by its binding
63-
new scala.quoted.Exprs.TreeExpr(bindMap.getOrElse(arg, arg))
63+
val term = dotty.tools.dotc.tasty.internal.Term(bindMap.getOrElse(arg, arg))
64+
new scala.quoted.Exprs.TreeExpr(term)
6465
}
6566
args1 ::: liftArgs(tp.resType, args.tail)
6667
case tp: PolyType =>

library/src/scala/quoted/Expr.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package scala.quoted
22

33
import scala.runtime.quoted.Toolbox
44
import scala.runtime.quoted.Unpickler.Pickled
5-
import scala.tasty.trees.Term
65

76
sealed abstract class Expr[T] {
87
final def unary_~ : T = throw new Error("~ should have been compiled away")
98
final def run(implicit toolbox: Toolbox[T]): T = toolbox.run(this)
109
final def show(implicit toolbox: Toolbox[T]): String = toolbox.show(this)
11-
final def toTasty(implicit toolbox: Toolbox[T]): Term = toolbox.toTasty(this)
10+
final def toTasty(implicit toolbox: Toolbox[T]): scala.tasty.trees.Term = toolbox.toTasty(this)
1211
}
1312

1413
object Expr {
@@ -36,7 +35,7 @@ object Exprs {
3635
}
3736

3837
/** An Expr backed by a tree. Only the current compiler trees are allowed. */
39-
final class TreeExpr[Tree](val tree: Tree) extends quoted.Expr[Any] {
38+
final class TreeExpr(val term: scala.tasty.trees.Term) extends quoted.Expr[Any] {
4039
override def toString: String = s"Expr(<raw>)"
4140
}
4241

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
6
2+
7
3+
8
4+
9
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.quoted._
2+
3+
import dotty.tools.dotc.quoted.Toolbox._
4+
5+
object Lines {
6+
inline def line(dummy: Int): Int = ~lineImpl('(dummy))
7+
def lineImpl(dummy: Expr[Int]): Expr[Int] = (dummy.toTasty.pos.startLine + 1).toExpr
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
import Lines._
3+
4+
object Test {
5+
def main(args: Array[String]): Unit = {
6+
println(line(0))
7+
println(line(0))
8+
println(line(0))
9+
println(line(0))
10+
}
11+
}

0 commit comments

Comments
 (0)