Skip to content

Use Tree.asExpr #10278

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

Merged
merged 1 commit into from
Nov 12, 2020
Merged
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 community-build/community-projects/utest
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/quoted/Matcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ object Matcher {
if patternHole.symbol == patternHoleSymbol &&
s.tpe <:< tpt.tpe &&
tpt2.tpe.derivesFrom(defn.RepeatedParamClass) =>
matched(scrutinee.seal)
matched(scrutinee.asExpr)

/* Term hole */
// Match a scala.internal.Quoted.patternHole and return the scrutinee tree
case (ClosedPatternTerm(scrutinee), TypeApply(patternHole, tpt :: Nil))
if patternHole.symbol == patternHoleSymbol &&
scrutinee.tpe <:< tpt.tpe =>
matched(scrutinee.seal)
matched(scrutinee.asExpr)

/* Higher order term hole */
// Matches an open term and wraps it into a lambda that provides the free variables
Expand All @@ -213,7 +213,7 @@ object Matcher {
val argTypes = args.map(x => x.tpe.widenTermRefExpr)
val resType = pattern.tpe
val res = Lambda(MethodType(names)(_ => argTypes, _ => resType), bodyFn)
matched(res.seal)
matched(res.asExpr)

//
// Match two equivalent trees
Expand Down
9 changes: 4 additions & 5 deletions docs/docs/reference/metaprogramming/tasty-reflect.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
Reporting.error("Parameter must be natural number")
'{0}
} else {
xTree.seal.cast[Int]
xTree.asExprOf[Int]
}
case _ =>
Reporting.error("Parameter must be a known constant")
Expand All @@ -61,10 +61,9 @@ def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
To easily know which extractors are needed, the `showExtractors` method on a
`qctx.reflect.Term` returns the string representation of the extractors.

The method `qctx.reflect.Term.seal` provides a way to go back to a
`quoted.Expr[Any]`. Note that the type is `Expr[Any]`. Consequently, the type
must be set explicitly with a checked `cast` call. If the type does not conform
to it an exception will be thrown at runtime.
The methods `qctx.reflect.Term.{asExpr, asExprOf}` provide a way to go back to a `quoted.Expr`.
Note that `asExpr` returns a `Expr[Any]`.
On the other hand `asExprOf[T]` returns a `Expr[T]`, if the type does not conform to it an exception will be thrown at runtime.


### Positions
Expand Down
6 changes: 3 additions & 3 deletions library/src-bootstrapped/scala/quoted/Expr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object Expr {
*/
def betaReduce[T](expr: Expr[T])(using qctx: QuoteContext): Expr[T] =
qctx.reflect.Term.betaReduce(expr.unseal) match
case Some(expr1) => expr1.seal.asInstanceOf[Expr[T]]
case Some(expr1) => expr1.asExpr.asInstanceOf[Expr[T]]
case _ => expr

/** Returns an expression containing a block with the given statements and ending with the expresion
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).seal.asInstanceOf[Expr[T]]
Block(statements.map(_.unseal), expr.unseal).asExpr.asInstanceOf[Expr[T]]
}

/** Lift a value into an expression containing the construction of that value */
Expand Down Expand Up @@ -211,7 +211,7 @@ object Expr {
def summon[T](using tpe: Type[T])(using qctx: QuoteContext): Option[Expr[T]] = {
import qctx.reflect._
Implicits.search(TypeRepr.of[T]) match {
case iss: ImplicitSearchSuccess => Some(iss.tree.seal.asInstanceOf[Expr[T]])
case iss: ImplicitSearchSuccess => Some(iss.tree.asExpr.asInstanceOf[Expr[T]])
case isf: ImplicitSearchFailure => None
}
}
Expand Down
20 changes: 10 additions & 10 deletions library/src-bootstrapped/scala/quoted/Liftable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,70 +25,70 @@ object Liftable {
given BooleanLiftable[T <: Boolean] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
Literal(Constant.Boolean(x)).seal.asInstanceOf[Expr[T]]
Literal(Constant.Boolean(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Byte */
given ByteLiftable[T <: Byte] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
Literal(Constant.Byte(x)).seal.asInstanceOf[Expr[T]]
Literal(Constant.Byte(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Short */
given ShortLiftable[T <: Short] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
Literal(Constant.Short(x)).seal.asInstanceOf[Expr[T]]
Literal(Constant.Short(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Int */
given IntLiftable[T <: Int] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
Literal(Constant.Int(x)).seal.asInstanceOf[Expr[T]]
Literal(Constant.Int(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Long */
given LongLiftable[T <: Long] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
Literal(Constant.Long(x)).seal.asInstanceOf[Expr[T]]
Literal(Constant.Long(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Float */
given FloatLiftable[T <: Float] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
Literal(Constant.Float(x)).seal.asInstanceOf[Expr[T]]
Literal(Constant.Float(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Double */
given DoubleLiftable[T <: Double] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
Literal(Constant.Double(x)).seal.asInstanceOf[Expr[T]]
Literal(Constant.Double(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Char */
given CharLiftable[T <: Char] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
Literal(Constant.Char(x)).seal.asInstanceOf[Expr[T]]
Literal(Constant.Char(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for String */
given StringLiftable[T <: String] as Liftable[T] {
def toExpr(x: T) =
import qctx.reflect._
Literal(Constant.String(x)).seal.asInstanceOf[Expr[T]]
Literal(Constant.String(x)).asExpr.asInstanceOf[Expr[T]]
}

/** Default liftable for Class[T] */
given ClassLiftable[T] as Liftable[Class[T]] = new Liftable[Class[T]] {
def toExpr(x: Class[T]) = {
import qctx.reflect._
Ref(defn.Predef_classOf).appliedToType(TypeRepr.typeConstructorOf(x)).seal.asInstanceOf[Expr[Class[T]]]
Ref(defn.Predef_classOf).appliedToType(TypeRepr.typeConstructorOf(x)).asExpr.asInstanceOf[Expr[Class[T]]]
}
}

Expand Down
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]).seal.asInstanceOf[Expr[Seq[T]]]
Repeated(xs.map[Term](_.unseal).toList, TypeTree.of[T]).asExpr.asInstanceOf[Expr[Seq[T]]]
}

/** Matches a literal sequence of expressions and return a sequence of expressions.
Expand All @@ -35,7 +35,7 @@ object Varargs {
def unapply[T](expr: Expr[Seq[T]])(using qctx: QuoteContext): Option[Seq[Expr[T]]] = {
import qctx.reflect._
def rec(tree: Term): Option[Seq[Expr[T]]] = tree match {
case Typed(Repeated(elems, _), _) => Some(elems.map(x => x.seal.asInstanceOf[Expr[T]]))
case Typed(Repeated(elems, _), _) => Some(elems.map(x => x.asExpr.asInstanceOf[Expr[T]]))
case Block(Nil, e) => rec(e)
case Inlined(_, Nil, e) => rec(e)
case _ => None
Expand Down
6 changes: 0 additions & 6 deletions library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,6 @@ trait Reflection { reflection =>
trait TermMethods {
extension (self: Term):

/** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression or throws */
def seal: scala.quoted.Expr[Any]

/** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression */
def sealOpt: Option[scala.quoted.Expr[Any]]

/** TypeRepr of this term */
def tpe: TypeRepr

Expand Down
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 @@ -15,8 +15,8 @@ object scalatest {
let(rhs) { right =>
val app = Select.overloaded(left, op, Nil, right :: Nil)
let(app) { result =>
val l = left.seal
val r = right.seal
val l = left.asExpr
val r = right.asExpr
val b = result.asExprOf[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
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 @@ -8,7 +8,7 @@ def mcrImpl[T](body: Expr[Any])(using QuoteContext) : Expr[Any] = {
val bTree = body.unseal
val under = bTree.underlyingArgument

val res = '{Box(${under.asInstanceOf[Term].seal})}
val res = '{Box(${under.asInstanceOf[Term].asExpr})}
res
}

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.seal
body.unseal.underlyingArgument.asExpr
}
4 changes: 2 additions & 2 deletions tests/pos-macros/i8325/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ object A:
def transformImplExpr[A:Type](using qctx: QuoteContext)(expr: Expr[A]): Expr[A] = {
import qctx.reflect._
expr.unseal match {
case Inlined(x,y,z) => transformImplExpr(z.seal.asInstanceOf[Expr[A]])
case Apply(fun,args) => '{ A.pure(${Apply(fun,args).seal.asInstanceOf[Expr[A]]}) }
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
}
}
4 changes: 2 additions & 2 deletions tests/pos-macros/i8325b/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ object A:
def transformImplExpr[A:Type](using qctx: QuoteContext)(expr: Expr[A]): Expr[A] = {
import qctx.reflect._
expr.unseal match {
case Inlined(x,y,z) => transformImplExpr(z.seal.asInstanceOf[Expr[A]])
case Inlined(x,y,z) => transformImplExpr(z.asExpr.asInstanceOf[Expr[A]])
case r@Apply(fun,args) => '{
A.pure(${r.seal.asInstanceOf[Expr[A]]}) }
A.pure(${r.asExpr.asInstanceOf[Expr[A]]}) }
case other => expr
}
}
2 changes: 1 addition & 1 deletion 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.seal}) }.unseal
'{ CBM.pure(${term.asExpr}) }.unseal
case other =>
throw RuntimeException(s"Not supported $other")

Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/f-interpolation-1/FQuote_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object FQuote {

def liftListOfAny(lst: List[Term]): Expr[List[Any]] = lst match {
case x :: xs =>
val head = x.seal
val head = x.asExpr
val tail = liftListOfAny(xs)
'{ $head :: $tail }
case Nil => '{Nil}
Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/i5533b/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ object scalatest {

tree.underlyingArgument match {
case Apply(Select(lhs, op), rhs :: Nil) =>
val left = lhs.seal
val right = rhs.seal
val left = lhs.asExpr
val right = rhs.asExpr
op match {
case "==" =>
'{
Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/i5536/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ object scalatest {

tree.underlyingArgument match {
case Apply(Select(lhs, op), rhs :: Nil) =>
val left = lhs.seal
val right = rhs.seal
val left = lhs.asExpr
val right = rhs.asExpr
op match {
case "===" =>
'{
Expand Down
8 changes: 4 additions & 4 deletions tests/run-macros/i6171/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ object scalatest {
ValDef.let(rhs) { right =>
val app = Select.overloaded(left, op, Nil, right :: Nil)
ValDef.let(app) { result =>
val l = left.seal
val r = right.seal
val l = left.asExpr
val r = right.asExpr
val b = result.asExprOf[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
Expand All @@ -32,8 +32,8 @@ object scalatest {
ValDef.let(rhs) { right =>
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
ValDef.let(Apply(app, implicits)) { result =>
val l = left.seal
val r = right.seal
val l = left.asExpr
val r = right.asExpr
val b = result.asExprOf[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/i6988/FirstArg_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ object Macros {
else enclosingParamList(owner.owner)

def literal(value: String): Expr[String] =
Literal(Constant.String(value)).seal.asInstanceOf[Expr[String]]
Literal(Constant.String(value)).asExpr.asInstanceOf[Expr[String]]
val paramss = enclosingParamList(Symbol.currentOwner)
val firstArg = paramss.flatten.head
val ref = Select.unique(This(enclosingClass()), firstArg.name)
'{ FirstArg(${ref.seal}, ${Expr(firstArg.name)}) }
'{ FirstArg(${ref.asExpr}, ${Expr(firstArg.name)}) }
}
}
4 changes: 2 additions & 2 deletions tests/run-macros/i7898/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object Main {
val showed = bodyTerm.show
'{
println(${Expr(showed)})
${bodyTerm.seal}
${bodyTerm.asExpr}
}
}

Expand All @@ -17,5 +17,5 @@ object Main {
}

def underlyingArgument[T](expr: Expr[T])(using qctx: QuoteContext): Expr[T] =
expr.unseal.underlyingArgument.seal.asInstanceOf[Expr[T]]
expr.unseal.underlyingArgument.asExpr.asInstanceOf[Expr[T]]
}
2 changes: 1 addition & 1 deletion tests/run-macros/i8007/Macro_2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object Macro2 {

val body: Expr[T] => Expr[String] = elem =>
fields.reverse.foldLeft(Expr("")){ (acc, field) =>
val res = Select.unique(elem.unseal, field).seal
val res = Select.unique(elem.unseal, field).asExpr
'{ $res.toString + " " + $acc }
}

Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/i9812b/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ case object NIL extends Lst[Nothing]
given IntLiftable[T <: Int] as Liftable[T]:
def toExpr(x: T): QuoteContext ?=> Expr[T] = (using qctx) => {
import qctx.reflect._
Literal(Constant.Int(x)).seal.asInstanceOf[Expr[T]]
Literal(Constant.Int(x)).asExpr.asInstanceOf[Expr[T]]
}

given LiftLst[T: Type: Liftable](using ev1: => Liftable[CONS[T]], ev2: => Liftable[NIL.type]) as Liftable[Lst[T]]:
Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/quote-matcher-symantics-2/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ object Macros {
object UnsafeExpr {
def open[T1, R, X](f: Expr[T1 => R])(content: (Expr[R], [t] => Expr[t] => Expr[T1] => Expr[t]) => X)(using qctx: QuoteContext): X = {
val (params, bodyExpr) = paramsAndBody[R](f)
content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).seal.asInstanceOf[Expr[t]])
content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).asExpr.asInstanceOf[Expr[t]])
}
private def paramsAndBody[R](using qctx: QuoteContext)(f: Expr[Any]): (List[qctx.reflect.ValDef], Expr[R]) = {
import qctx.reflect._
val Block(List(DefDef("$anonfun", Nil, List(params), _, Some(body))), Closure(Ident("$anonfun"), None)) = f.unseal.etaExpand
(params, body.seal.asInstanceOf[Expr[R]])
(params, body.asExpr.asInstanceOf[Expr[R]])
}

private def bodyFn[t](using qctx: QuoteContext)(e: qctx.reflect.Term, params: List[qctx.reflect.ValDef], args: List[qctx.reflect.Term]): qctx.reflect.Term = {
Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/quote-matcher-symantics-3/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ object Macros {
object UnsafeExpr {
def open[T1, R, X](f: Expr[T1 => R])(content: (Expr[R], [t] => Expr[t] => Expr[T1] => Expr[t]) => X)(using qctx: QuoteContext): X = {
val (params, bodyExpr) = paramsAndBody[R](f)
content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).seal.asInstanceOf[Expr[t]])
content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).asExpr.asInstanceOf[Expr[t]])
}
private def paramsAndBody[R](using qctx: QuoteContext)(f: Expr[Any]): (List[qctx.reflect.ValDef], Expr[R]) = {
import qctx.reflect._
val Block(List(DefDef("$anonfun", Nil, List(params), _, Some(body))), Closure(Ident("$anonfun"), None)) = f.unseal.etaExpand
(params, body.seal.asInstanceOf[Expr[R]])
(params, body.asExpr.asInstanceOf[Expr[R]])
}

private def bodyFn[t](using qctx: QuoteContext)(e: qctx.reflect.Term, params: List[qctx.reflect.ValDef], args: List[qctx.reflect.Term]): qctx.reflect.Term = {
Expand Down
Loading