Skip to content

Use asRefelctTree #10287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/src-bootstrapped/scala/quoted/Const.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object Const {
case Inlined(_, Nil, e) => rec(e)
case _ => None
}
rec(expr.unseal)
rec(expr.asReflectTree)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have trees other than reflect trees? What about call it reflect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have other things in reflect such as TypeRepr, Position, Symbol, ... . All things that could be taken out from this expression.

There is also a better use of reflect in #10289.

We also want the method to be a bit longer to make it pop out a bit bore in the source as this is where we go into the other API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the changes, for meta-programmers, asReflectTree seems a little verbose. What about expr.asTree?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, that was the intention. We want to stop anyone that does not need to use it for calling it in the middle of some code just because it has a short name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asTree tempted as a first option but I noticed that it was too dangerous and the changed to asReflectTree to have something that is immediately associated with reflect.Tree.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only realistic alternative we have is to remove this method and create one in the module of Tree or Term.

- val tree: Term = expr.asReflectTree
+ val tree: Term = Term.of(expr)
+ val tree: Term = Tree.of(expr)

This keeps the short name and has the added advantage that we need to import qctx.reflect._ or use the path explicitly (making it clear where we change from one API to the other).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #10332 with this alternative. This alternative looks better and is more inline with the rest of the API

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a better alternative 👍

}

}
4 changes: 2 additions & 2 deletions library/src-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object Expr {
* Some bindings may be elided as an early optimization.
*/
def betaReduce[T](expr: Expr[T])(using qctx: QuoteContext): Expr[T] =
qctx.reflect.Term.betaReduce(expr.unseal) match
qctx.reflect.Term.betaReduce(expr.asReflectTree) match
case Some(expr1) => expr1.asExpr.asInstanceOf[Expr[T]]
case _ => expr

Expand All @@ -24,7 +24,7 @@ object Expr {
*/
def block[T](statements: List[Expr[Any]], expr: Expr[T])(using qctx: QuoteContext): Expr[T] = {
import qctx.reflect._
Block(statements.map(_.unseal), expr.unseal).asExpr.asInstanceOf[Expr[T]]
Block(statements.map(_.asReflectTree), expr.asReflectTree).asExpr.asInstanceOf[Expr[T]]
}

/** Lift a value into an expression containing the construction of that value */
Expand Down
4 changes: 2 additions & 2 deletions library/src-bootstrapped/scala/quoted/ExprMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ trait ExprMap:
type X
val expr = tree.asExpr.asInstanceOf[Expr[X]]
val t = tpe.asType.asInstanceOf[Type[X]]
transform(expr)(using qctx, t).unseal
transform(expr)(using qctx, t).asReflectTree
case _ =>
transformTermChildren(tree, tpe)

Expand Down Expand Up @@ -144,7 +144,7 @@ trait ExprMap:
trees mapConserve (transformTypeCaseDef(_))

}
new MapChildren().transformTermChildren(e.unseal, TypeRepr.of[T]).asExprOf[T]
new MapChildren().transformTermChildren(e.asReflectTree, TypeRepr.of[T]).asExprOf[T]
}

end ExprMap
4 changes: 2 additions & 2 deletions library/src-bootstrapped/scala/quoted/Varargs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object Varargs {
*/
def apply[T](xs: Seq[Expr[T]])(using tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] = {
import qctx.reflect._
Repeated(xs.map[Term](_.unseal).toList, TypeTree.of[T]).asExpr.asInstanceOf[Expr[Seq[T]]]
Repeated(xs.map[Term](_.asReflectTree).toList, TypeTree.of[T]).asExpr.asInstanceOf[Expr[Seq[T]]]
}

/** Matches a literal sequence of expressions and return a sequence of expressions.
Expand All @@ -40,7 +40,7 @@ object Varargs {
case Inlined(_, Nil, e) => rec(e)
case _ => None
}
rec(expr.unseal)
rec(expr.asReflectTree)
}

}
3 changes: 0 additions & 3 deletions library/src/scala/quoted/QuoteContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ trait QuoteContext { self: internal.QuoteUnpickler & internal.QuoteMatching =>
report.throwError(msg, self)(using QuoteContext.this)
unlift.fromExpr(self)(using QuoteContext.this).getOrElse(reportError)

/** View this expression `quoted.Expr[T]` as a `Term` */
def unseal: reflect.Term = self.asReflectTree // TODO remove

/** View this expression `quoted.Expr[T]` as a `Term` */
def asReflectTree: reflect.Term
end extension
Expand Down
4 changes: 2 additions & 2 deletions library/src/scala/quoted/report.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object report:

/** Report an error at the on the position of `expr` */
def error(msg: => String, expr: Expr[Any])(using qctx: QuoteContext): Unit =
qctx.reflect.Reporting.error(msg, expr.unseal.pos)
qctx.reflect.Reporting.error(msg, expr.asReflectTree.pos)

/** Report an error at the position of the macro expansion and throws a StopQuotedContext */
def throwError(msg: => String)(using qctx: QuoteContext): Nothing = {
Expand All @@ -27,7 +27,7 @@ object report:

/** Report a warning at the on the position of `expr` */
def warning(msg: => String, expr: Expr[_])(using qctx: QuoteContext): Unit =
qctx.reflect.Reporting.warning(msg, expr.unseal.pos)
qctx.reflect.Reporting.warning(msg, expr.asReflectTree.pos)

/** Throwable used to stop the expansion of a macro after an error was reported */
class StopQuotedContext extends Throwable
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i6432/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Macro {
sc match {
case '{ StringContext(${Varargs(parts)}: _*) } =>
for (part @ Const(s) <- parts)
Reporting.error(s, part.unseal.pos)
Reporting.error(s, part.asReflectTree.pos)
}
'{}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i6432b/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Macro {
sc match {
case '{ StringContext(${Varargs(parts)}: _*) } =>
for (part @ Const(s) <- parts)
Reporting.error(s, part.unseal.pos)
Reporting.error(s, part.asReflectTree.pos)
}
'{}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i6976/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ object macros {

def mcrImpl(body: Expr[Any])(using ctx: QuoteContext) : Expr[Any] = {
import ctx.reflect._
body.unseal match { case Block(_, _) => '{2} }
body.asReflectTree match { case Block(_, _) => '{2} }
}
}
2 changes: 1 addition & 1 deletion tests/neg-macros/i7698.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ trait Show[T] {
}

def showInterpolatorImpl(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext): Expr[String] =
argsExpr.unseal match
argsExpr.asReflectTree match
case '{ $arg: $t } => // error
case '[ Int ] => // error
???
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i9801/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ def impl(prog: Expr[Double])(using QuoteContext) : Expr[Double] =
triggerStackOverflow(0)
} catch {
case e =>
qctx.reflect.Reporting.error(e.getMessage, prog.unseal.pos)
qctx.reflect.Reporting.error(e.getMessage, prog.asReflectTree.pos)
'{ 42.0 }
}
2 changes: 1 addition & 1 deletion tests/neg-macros/tasty-macro-assert-1/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Asserts {
def impl(cond: Expr[Boolean])(using qctx: QuoteContext) : Expr[Unit] = {
import qctx.reflect._

val tree = cond.unseal
val tree = cond.asReflectTree

def isOps(tpe: TypeRepr): Boolean = tpe match {
case tpe: TermRef => tpe.termSymbol.isDefDef && tpe.name == "Ops"// TODO check that the parent is Asserts
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/tasty-macro-assert-2/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Asserts {
def impl(cond: Expr[Boolean])(using qctx: QuoteContext) : Expr[Unit] = {
import qctx.reflect._

val tree = cond.unseal
val tree = cond.asReflectTree

def isOps(tpe: TypeRepr): Boolean = tpe match {
case tpe: TermRef => tpe.termSymbol.isDefDef && tpe.name == "Ops"// TODO check that the parent is Asserts
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/tasty-macro-error/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Macros {

def impl(x: Expr[Any])(using qctx: QuoteContext) : Expr[Unit] = {
import qctx.reflect._
Reporting.error("here is the the argument is " + x.unseal.underlyingArgument.show, x.unseal.underlyingArgument.pos)
Reporting.error("here is the the argument is " + x.asReflectTree.underlyingArgument.show, x.asReflectTree.underlyingArgument.pos)
'{}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/neg-macros/tasty-macro-positions/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ object Macros {

def impl(x: Expr[Any])(using qctx: QuoteContext) : Expr[Unit] = {
import qctx.reflect._
val pos = x.unseal.underlyingArgument.pos
Reporting.error("here is the the argument is " + x.unseal.underlyingArgument.show, pos)
Reporting.error("here (+5) is the the argument is " + x.unseal.underlyingArgument.show, pos.sourceFile, pos.start + 5, pos.end + 5)
val pos = x.asReflectTree.underlyingArgument.pos
Reporting.error("here is the the argument is " + x.asReflectTree.underlyingArgument.show, pos)
Reporting.error("here (+5) is the the argument is " + x.asReflectTree.underlyingArgument.show, pos.sourceFile, pos.start + 5, pos.end + 5)
'{}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object FIntepolator {

def apply(strCtxExpr: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext) : Expr[String] = {
import qctx.reflect._
Reporting.error("there are no parts", strCtxExpr.unseal.underlyingArgument.pos)
Reporting.error("there are no parts", strCtxExpr.asReflectTree.underlyingArgument.pos)
'{ ($strCtxExpr).s($argsExpr: _*) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Macro {
object FIntepolator {
def apply(strCtxExpr: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext) : Expr[String] = {
import qctx.reflect._
Reporting.error("there are no args", argsExpr.unseal.underlyingArgument.pos)
Reporting.error("there are no args", argsExpr.asReflectTree.underlyingArgument.pos)
'{ ($strCtxExpr).s($argsExpr: _*) }
}

Expand Down
6 changes: 3 additions & 3 deletions tests/neg-staging/i5941/macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ object Lens {
import util._
// obj.copy(field = value)
def setterBody(obj: Expr[S], value: Expr[T], field: String): Expr[S] =
Select.overloaded(obj.unseal, "copy", Nil, NamedArg(field, value.unseal) :: Nil, TypeBounds.empty).asExprOf[S]
Select.overloaded(obj.asReflectTree, "copy", Nil, NamedArg(field, value.asReflectTree) :: Nil, TypeBounds.empty).asExprOf[S]

// exception: getter.unseal.underlyingArgument
getter.unseal match {
// exception: getter.asReflectTree.underlyingArgument
getter.asReflectTree match {
case Inlined(
None, Nil,
Block(
Expand Down
2 changes: 1 addition & 1 deletion tests/pending/run/tasty-comments/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Macros {
def impl[T](x: Expr[T])(using qctx: QuoteContext) : Expr[Unit] = {
import qctx.reflect._

val tree = x.unseal
val tree = x.asReflectTree
tree.symbol.comment.map(_.raw) match {
case Some(str) => '{ println(${str}) }
case None => '{ println() }
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i6171/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object scalatest {

def assertImpl(x: Expr[Any])(using qctx: QuoteContext) : Expr[Unit] = {
import qctx.reflect._
x.unseal.underlyingArgument
x.asReflectTree.underlyingArgument
'{ () }
}
}
4 changes: 2 additions & 2 deletions tests/pos-macros/i6535/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object scalatest {
import util._
import ValDef.let

cond.unseal.underlyingArgument match {
cond.asReflectTree.underlyingArgument match {
case t @ Apply(Select(lhs, op), rhs :: Nil) =>
let(lhs) { left =>
let(rhs) { right =>
Expand All @@ -19,7 +19,7 @@ object scalatest {
val r = right.asExpr
val b = result.asExprOf[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
code.asReflectTree
}
}
}.asExprOf[Unit]
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i7011/Macros_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ inline def mcr(body: => Any): Unit = ${mcrImpl('body)}
def mcrImpl[T](body: Expr[Any])(using QuoteContext) : Expr[Any] = {
import qctx.reflect._

val bTree = body.unseal
val bTree = body.asReflectTree
val under = bTree.underlyingArgument

val res = '{Box(${under.asInstanceOf[Term].asExpr})}
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i7030/Macros_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ def innerImpl(exprs: Expr[Any])(using QuoteContext): Expr[Any] =
inline def outer(expr: => Any): Any = ${outerImpl('expr)}
def outerImpl(body: Expr[Any])(using QuoteContext): Expr[Any] = {
import qctx.reflect._
body.unseal.underlyingArgument.asExpr
body.asReflectTree.underlyingArgument.asExpr
}
2 changes: 1 addition & 1 deletion tests/pos-macros/i8325/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object A:

def transformImplExpr[A:Type](using qctx: QuoteContext)(expr: Expr[A]): Expr[A] = {
import qctx.reflect._
expr.unseal match {
expr.asReflectTree match {
case Inlined(x,y,z) => transformImplExpr(z.asExpr.asInstanceOf[Expr[A]])
case Apply(fun,args) => '{ A.pure(${Apply(fun,args).asExpr.asInstanceOf[Expr[A]]}) }
case other => expr
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i8325b/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object A:

def transformImplExpr[A:Type](using qctx: QuoteContext)(expr: Expr[A]): Expr[A] = {
import qctx.reflect._
expr.unseal match {
expr.asReflectTree match {
case Inlined(x,y,z) => transformImplExpr(z.asExpr.asInstanceOf[Expr[A]])
case r@Apply(fun,args) => '{
A.pure(${r.asExpr.asInstanceOf[Expr[A]]}) }
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i8866/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object Macro {

ValDef.let(
Select.unique(
'{ OtherMacro }.unseal,
'{ OtherMacro }.asReflectTree,
"apply"
)
)(identity).asExprOf[Int]
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i8866b/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Macro {

ValDef.let(
Select.unique(
'{ Other }.unseal,
'{ Other }.asReflectTree,
"apply"
)
)(identity).asExprOf[Int]
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i9251/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object Async {
def checkPrintTypeImpl[F[_]:Type,T:Type](f: Expr[T])(using qctx: QuoteContext): Expr[Unit] =
import qctx.reflect._

val fu = f.unseal
val fu = f.asReflectTree
fu match
case Inlined(_,_,Block(_,Apply(TypeApply(Select(q,n),tparams),List(param)))) =>
param.tpe match
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i9518/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ inline def shift : Unit = ${ shiftTerm }

def shiftTerm(using QuoteContext): Expr[Unit] = {
import qctx.reflect._
val nTree = '{ ??? : CB[Int] }.unseal
val nTree = '{ ??? : CB[Int] }.asReflectTree
val tp1 = TypeRepr.of[CB[Int]]
val tp2 = TypeRepr.of[([X] =>> CB[X])[Int]]
val ta = Type.of[[X] =>> CB[X]]
Expand Down
6 changes: 3 additions & 3 deletions tests/pos-macros/i9687/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ object X {

def transformImpl[A:Type](x:Expr[A])(using qctx: QuoteContext):Expr[A] = {
import qctx.reflect._
val slowPath = '{ SlowPath }.unseal
val fastPath = '{ FastPath }.unseal
val slowPath = '{ SlowPath }.asReflectTree
val fastPath = '{ FastPath }.asReflectTree
val transformer = new TreeMap() {
override def transformTerm(term:Term)(using ctx:Context):Term = {
term match
Expand All @@ -37,7 +37,7 @@ object X {
case _ => super.transformTerm(term)
}
}
val r = transformer.transformTerm(x.unseal).asExprOf[A]
val r = transformer.transformTerm(x.asReflectTree).asExprOf[A]
s"result: ${r.show}"
r
}
Expand Down
4 changes: 2 additions & 2 deletions tests/pos-macros/i9894/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object X:
case Block(stats, last) => Block(stats, transform(last))
case Inlined(x,List(),body) => transform(body)
case l@Literal(x) =>
'{ CBM.pure(${term.asExpr}) }.unseal
'{ CBM.pure(${term.asExpr}) }.asReflectTree
case other =>
throw RuntimeException(s"Not supported $other")

Expand Down Expand Up @@ -64,4 +64,4 @@ object X:
}
changes.transformTerm(body)

transform(f.unseal).asExprOf[CB[T]]
transform(f.asReflectTree).asExprOf[CB[T]]
2 changes: 1 addition & 1 deletion tests/pos-macros/treemap-unapply/Macro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import scala.quoted._
inline def mcr(x: => Unit): Unit = ${mcrImpl('x)}
def mcrImpl(x: Expr[Unit])(using QuoteContext) : Expr[Unit] =
import qctx.reflect._
val tr: Term = x.unseal
val tr: Term = x.asReflectTree
object m extends TreeMap
m.transformTerm(tr).asExprOf[Unit]
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object Foo {

def inspectBodyImpl(x: Expr[Int])(using qctx: QuoteContext) : Expr[String] = {
import qctx.reflect._
x.unseal match {
x.asReflectTree match {
case Inlined(None, Nil, arg) => Expr(arg.symbol.tree.showExtractors)
case arg => Expr(arg.symbol.tree.showExtractors) // TODO should all by name parameters be in an inline node?
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object Foo {

def inspectBodyImpl(x: Expr[Int])(using qctx: QuoteContext) : Expr[String] = {
import qctx.reflect._
x.unseal match {
x.asReflectTree match {
case Inlined(None, Nil, arg) => Expr(arg.symbol.tree.showExtractors)
case arg => Expr(arg.symbol.tree.showExtractors) // TODO should all by name parameters be in an inline node?
}
Expand Down
Loading