Skip to content

Commit a6d9b00

Browse files
Merge pull request #10278 from dotty-staging/use-asExpr
Use Tree.asExpr
2 parents 49e853c + 8da3608 commit a6d9b00

File tree

40 files changed

+107
-114
lines changed

40 files changed

+107
-114
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ object Matcher {
183183
if patternHole.symbol == patternHoleSymbol &&
184184
s.tpe <:< tpt.tpe &&
185185
tpt2.tpe.derivesFrom(defn.RepeatedParamClass) =>
186-
matched(scrutinee.seal)
186+
matched(scrutinee.asExpr)
187187

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

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

218218
//
219219
// Match two equivalent trees

docs/docs/reference/metaprogramming/tasty-reflect.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
4949
Reporting.error("Parameter must be natural number")
5050
'{0}
5151
} else {
52-
xTree.seal.cast[Int]
52+
xTree.asExprOf[Int]
5353
}
5454
case _ =>
5555
Reporting.error("Parameter must be a known constant")
@@ -61,10 +61,9 @@ def natConstImpl(x: Expr[Int])(using qctx: QuoteContext): Expr[Int] = {
6161
To easily know which extractors are needed, the `showExtractors` method on a
6262
`qctx.reflect.Term` returns the string representation of the extractors.
6363

64-
The method `qctx.reflect.Term.seal` provides a way to go back to a
65-
`quoted.Expr[Any]`. Note that the type is `Expr[Any]`. Consequently, the type
66-
must be set explicitly with a checked `cast` call. If the type does not conform
67-
to it an exception will be thrown at runtime.
64+
The methods `qctx.reflect.Term.{asExpr, asExprOf}` provide a way to go back to a `quoted.Expr`.
65+
Note that `asExpr` returns a `Expr[Any]`.
66+
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.
6867

6968

7069
### Positions

library/src-bootstrapped/scala/quoted/Expr.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ object Expr {
1515
*/
1616
def betaReduce[T](expr: Expr[T])(using qctx: QuoteContext): Expr[T] =
1717
qctx.reflect.Term.betaReduce(expr.unseal) match
18-
case Some(expr1) => expr1.seal.asInstanceOf[Expr[T]]
18+
case Some(expr1) => expr1.asExpr.asInstanceOf[Expr[T]]
1919
case _ => expr
2020

2121
/** Returns an expression containing a block with the given statements and ending with the expresion
@@ -24,7 +24,7 @@ object Expr {
2424
*/
2525
def block[T](statements: List[Expr[Any]], expr: Expr[T])(using qctx: QuoteContext): Expr[T] = {
2626
import qctx.reflect._
27-
Block(statements.map(_.unseal), expr.unseal).seal.asInstanceOf[Expr[T]]
27+
Block(statements.map(_.unseal), expr.unseal).asExpr.asInstanceOf[Expr[T]]
2828
}
2929

3030
/** Lift a value into an expression containing the construction of that value */
@@ -211,7 +211,7 @@ object Expr {
211211
def summon[T](using tpe: Type[T])(using qctx: QuoteContext): Option[Expr[T]] = {
212212
import qctx.reflect._
213213
Implicits.search(TypeRepr.of[T]) match {
214-
case iss: ImplicitSearchSuccess => Some(iss.tree.seal.asInstanceOf[Expr[T]])
214+
case iss: ImplicitSearchSuccess => Some(iss.tree.asExpr.asInstanceOf[Expr[T]])
215215
case isf: ImplicitSearchFailure => None
216216
}
217217
}

library/src-bootstrapped/scala/quoted/Liftable.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,70 +25,70 @@ object Liftable {
2525
given BooleanLiftable[T <: Boolean] as Liftable[T] {
2626
def toExpr(x: T) =
2727
import qctx.reflect._
28-
Literal(Constant.Boolean(x)).seal.asInstanceOf[Expr[T]]
28+
Literal(Constant.Boolean(x)).asExpr.asInstanceOf[Expr[T]]
2929
}
3030

3131
/** Default liftable for Byte */
3232
given ByteLiftable[T <: Byte] as Liftable[T] {
3333
def toExpr(x: T) =
3434
import qctx.reflect._
35-
Literal(Constant.Byte(x)).seal.asInstanceOf[Expr[T]]
35+
Literal(Constant.Byte(x)).asExpr.asInstanceOf[Expr[T]]
3636
}
3737

3838
/** Default liftable for Short */
3939
given ShortLiftable[T <: Short] as Liftable[T] {
4040
def toExpr(x: T) =
4141
import qctx.reflect._
42-
Literal(Constant.Short(x)).seal.asInstanceOf[Expr[T]]
42+
Literal(Constant.Short(x)).asExpr.asInstanceOf[Expr[T]]
4343
}
4444

4545
/** Default liftable for Int */
4646
given IntLiftable[T <: Int] as Liftable[T] {
4747
def toExpr(x: T) =
4848
import qctx.reflect._
49-
Literal(Constant.Int(x)).seal.asInstanceOf[Expr[T]]
49+
Literal(Constant.Int(x)).asExpr.asInstanceOf[Expr[T]]
5050
}
5151

5252
/** Default liftable for Long */
5353
given LongLiftable[T <: Long] as Liftable[T] {
5454
def toExpr(x: T) =
5555
import qctx.reflect._
56-
Literal(Constant.Long(x)).seal.asInstanceOf[Expr[T]]
56+
Literal(Constant.Long(x)).asExpr.asInstanceOf[Expr[T]]
5757
}
5858

5959
/** Default liftable for Float */
6060
given FloatLiftable[T <: Float] as Liftable[T] {
6161
def toExpr(x: T) =
6262
import qctx.reflect._
63-
Literal(Constant.Float(x)).seal.asInstanceOf[Expr[T]]
63+
Literal(Constant.Float(x)).asExpr.asInstanceOf[Expr[T]]
6464
}
6565

6666
/** Default liftable for Double */
6767
given DoubleLiftable[T <: Double] as Liftable[T] {
6868
def toExpr(x: T) =
6969
import qctx.reflect._
70-
Literal(Constant.Double(x)).seal.asInstanceOf[Expr[T]]
70+
Literal(Constant.Double(x)).asExpr.asInstanceOf[Expr[T]]
7171
}
7272

7373
/** Default liftable for Char */
7474
given CharLiftable[T <: Char] as Liftable[T] {
7575
def toExpr(x: T) =
7676
import qctx.reflect._
77-
Literal(Constant.Char(x)).seal.asInstanceOf[Expr[T]]
77+
Literal(Constant.Char(x)).asExpr.asInstanceOf[Expr[T]]
7878
}
7979

8080
/** Default liftable for String */
8181
given StringLiftable[T <: String] as Liftable[T] {
8282
def toExpr(x: T) =
8383
import qctx.reflect._
84-
Literal(Constant.String(x)).seal.asInstanceOf[Expr[T]]
84+
Literal(Constant.String(x)).asExpr.asInstanceOf[Expr[T]]
8585
}
8686

8787
/** Default liftable for Class[T] */
8888
given ClassLiftable[T] as Liftable[Class[T]] = new Liftable[Class[T]] {
8989
def toExpr(x: Class[T]) = {
9090
import qctx.reflect._
91-
Ref(defn.Predef_classOf).appliedToType(TypeRepr.typeConstructorOf(x)).seal.asInstanceOf[Expr[Class[T]]]
91+
Ref(defn.Predef_classOf).appliedToType(TypeRepr.typeConstructorOf(x)).asExpr.asInstanceOf[Expr[Class[T]]]
9292
}
9393
}
9494

library/src-bootstrapped/scala/quoted/Varargs.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object Varargs {
1717
*/
1818
def apply[T](xs: Seq[Expr[T]])(using tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] = {
1919
import qctx.reflect._
20-
Repeated(xs.map[Term](_.unseal).toList, TypeTree.of[T]).seal.asInstanceOf[Expr[Seq[T]]]
20+
Repeated(xs.map[Term](_.unseal).toList, TypeTree.of[T]).asExpr.asInstanceOf[Expr[Seq[T]]]
2121
}
2222

2323
/** Matches a literal sequence of expressions and return a sequence of expressions.
@@ -35,7 +35,7 @@ object Varargs {
3535
def unapply[T](expr: Expr[Seq[T]])(using qctx: QuoteContext): Option[Seq[Expr[T]]] = {
3636
import qctx.reflect._
3737
def rec(tree: Term): Option[Seq[Expr[T]]] = tree match {
38-
case Typed(Repeated(elems, _), _) => Some(elems.map(x => x.seal.asInstanceOf[Expr[T]]))
38+
case Typed(Repeated(elems, _), _) => Some(elems.map(x => x.asExpr.asInstanceOf[Expr[T]]))
3939
case Block(Nil, e) => rec(e)
4040
case Inlined(_, Nil, e) => rec(e)
4141
case _ => None

library/src/scala/tasty/Reflection.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,6 @@ trait Reflection { reflection =>
391391
trait TermMethods {
392392
extension (self: Term):
393393

394-
/** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression or throws */
395-
def seal: scala.quoted.Expr[Any]
396-
397-
/** Convert `Term` to an `quoted.Expr[Any]` if the term is a valid expression */
398-
def sealOpt: Option[scala.quoted.Expr[Any]]
399-
400394
/** TypeRepr of this term */
401395
def tpe: TypeRepr
402396

tests/pos-macros/i6535/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ object scalatest {
1515
let(rhs) { right =>
1616
val app = Select.overloaded(left, op, Nil, right :: Nil)
1717
let(app) { result =>
18-
val l = left.seal
19-
val r = right.seal
18+
val l = left.asExpr
19+
val r = right.asExpr
2020
val b = result.asExprOf[Boolean]
2121
val code = '{ scala.Predef.assert($b) }
2222
code.unseal

tests/pos-macros/i7011/Macros_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def mcrImpl[T](body: Expr[Any])(using QuoteContext) : Expr[Any] = {
88
val bTree = body.unseal
99
val under = bTree.underlyingArgument
1010

11-
val res = '{Box(${under.asInstanceOf[Term].seal})}
11+
val res = '{Box(${under.asInstanceOf[Term].asExpr})}
1212
res
1313
}
1414

tests/pos-macros/i7030/Macros_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ def innerImpl(exprs: Expr[Any])(using QuoteContext): Expr[Any] =
77
inline def outer(expr: => Any): Any = ${outerImpl('expr)}
88
def outerImpl(body: Expr[Any])(using QuoteContext): Expr[Any] = {
99
import qctx.reflect._
10-
body.unseal.underlyingArgument.seal
10+
body.unseal.underlyingArgument.asExpr
1111
}

tests/pos-macros/i8325/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ object A:
1414
def transformImplExpr[A:Type](using qctx: QuoteContext)(expr: Expr[A]): Expr[A] = {
1515
import qctx.reflect._
1616
expr.unseal match {
17-
case Inlined(x,y,z) => transformImplExpr(z.seal.asInstanceOf[Expr[A]])
18-
case Apply(fun,args) => '{ A.pure(${Apply(fun,args).seal.asInstanceOf[Expr[A]]}) }
17+
case Inlined(x,y,z) => transformImplExpr(z.asExpr.asInstanceOf[Expr[A]])
18+
case Apply(fun,args) => '{ A.pure(${Apply(fun,args).asExpr.asInstanceOf[Expr[A]]}) }
1919
case other => expr
2020
}
2121
}

tests/pos-macros/i8325b/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ object A:
1414
def transformImplExpr[A:Type](using qctx: QuoteContext)(expr: Expr[A]): Expr[A] = {
1515
import qctx.reflect._
1616
expr.unseal match {
17-
case Inlined(x,y,z) => transformImplExpr(z.seal.asInstanceOf[Expr[A]])
17+
case Inlined(x,y,z) => transformImplExpr(z.asExpr.asInstanceOf[Expr[A]])
1818
case r@Apply(fun,args) => '{
19-
A.pure(${r.seal.asInstanceOf[Expr[A]]}) }
19+
A.pure(${r.asExpr.asInstanceOf[Expr[A]]}) }
2020
case other => expr
2121
}
2222
}

tests/pos-macros/i9894/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ object X:
3636
case Block(stats, last) => Block(stats, transform(last))
3737
case Inlined(x,List(),body) => transform(body)
3838
case l@Literal(x) =>
39-
'{ CBM.pure(${term.seal}) }.unseal
39+
'{ CBM.pure(${term.asExpr}) }.unseal
4040
case other =>
4141
throw RuntimeException(s"Not supported $other")
4242

tests/run-macros/f-interpolation-1/FQuote_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object FQuote {
1313

1414
def liftListOfAny(lst: List[Term]): Expr[List[Any]] = lst match {
1515
case x :: xs =>
16-
val head = x.seal
16+
val head = x.asExpr
1717
val tail = liftListOfAny(xs)
1818
'{ $head :: $tail }
1919
case Nil => '{Nil}

tests/run-macros/i5533b/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ object scalatest {
1313

1414
tree.underlyingArgument match {
1515
case Apply(Select(lhs, op), rhs :: Nil) =>
16-
val left = lhs.seal
17-
val right = rhs.seal
16+
val left = lhs.asExpr
17+
val right = rhs.asExpr
1818
op match {
1919
case "==" =>
2020
'{

tests/run-macros/i5536/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ object scalatest {
1010

1111
tree.underlyingArgument match {
1212
case Apply(Select(lhs, op), rhs :: Nil) =>
13-
val left = lhs.seal
14-
val right = rhs.seal
13+
val left = lhs.asExpr
14+
val right = rhs.asExpr
1515
op match {
1616
case "===" =>
1717
'{

tests/run-macros/i6171/Macro_1.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ object scalatest {
1818
ValDef.let(rhs) { right =>
1919
val app = Select.overloaded(left, op, Nil, right :: Nil)
2020
ValDef.let(app) { result =>
21-
val l = left.seal
22-
val r = right.seal
21+
val l = left.asExpr
22+
val r = right.asExpr
2323
val b = result.asExprOf[Boolean]
2424
val code = '{ scala.Predef.assert($b) }
2525
code.unseal
@@ -32,8 +32,8 @@ object scalatest {
3232
ValDef.let(rhs) { right =>
3333
val app = Select.overloaded(Apply(qual, left :: Nil), op, Nil, right :: Nil)
3434
ValDef.let(Apply(app, implicits)) { result =>
35-
val l = left.seal
36-
val r = right.seal
35+
val l = left.asExpr
36+
val r = right.asExpr
3737
val b = result.asExprOf[Boolean]
3838
val code = '{ scala.Predef.assert($b) }
3939
code.unseal

tests/run-macros/i6988/FirstArg_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ object Macros {
2323
else enclosingParamList(owner.owner)
2424

2525
def literal(value: String): Expr[String] =
26-
Literal(Constant.String(value)).seal.asInstanceOf[Expr[String]]
26+
Literal(Constant.String(value)).asExpr.asInstanceOf[Expr[String]]
2727
val paramss = enclosingParamList(Symbol.currentOwner)
2828
val firstArg = paramss.flatten.head
2929
val ref = Select.unique(This(enclosingClass()), firstArg.name)
30-
'{ FirstArg(${ref.seal}, ${Expr(firstArg.name)}) }
30+
'{ FirstArg(${ref.asExpr}, ${Expr(firstArg.name)}) }
3131
}
3232
}

tests/run-macros/i7898/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ object Main {
88
val showed = bodyTerm.show
99
'{
1010
println(${Expr(showed)})
11-
${bodyTerm.seal}
11+
${bodyTerm.asExpr}
1212
}
1313
}
1414

@@ -17,5 +17,5 @@ object Main {
1717
}
1818

1919
def underlyingArgument[T](expr: Expr[T])(using qctx: QuoteContext): Expr[T] =
20-
expr.unseal.underlyingArgument.seal.asInstanceOf[Expr[T]]
20+
expr.unseal.underlyingArgument.asExpr.asInstanceOf[Expr[T]]
2121
}

tests/run-macros/i8007/Macro_2.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object Macro2 {
3030

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

tests/run-macros/i9812b/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ case object NIL extends Lst[Nothing]
2929
given IntLiftable[T <: Int] as Liftable[T]:
3030
def toExpr(x: T): QuoteContext ?=> Expr[T] = (using qctx) => {
3131
import qctx.reflect._
32-
Literal(Constant.Int(x)).seal.asInstanceOf[Expr[T]]
32+
Literal(Constant.Int(x)).asExpr.asInstanceOf[Expr[T]]
3333
}
3434

3535
given LiftLst[T: Type: Liftable](using ev1: => Liftable[CONS[T]], ev2: => Liftable[NIL.type]) as Liftable[Lst[T]]:

tests/run-macros/quote-matcher-symantics-2/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ object Macros {
6565
object UnsafeExpr {
6666
def open[T1, R, X](f: Expr[T1 => R])(content: (Expr[R], [t] => Expr[t] => Expr[T1] => Expr[t]) => X)(using qctx: QuoteContext): X = {
6767
val (params, bodyExpr) = paramsAndBody[R](f)
68-
content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).seal.asInstanceOf[Expr[t]])
68+
content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).asExpr.asInstanceOf[Expr[t]])
6969
}
7070
private def paramsAndBody[R](using qctx: QuoteContext)(f: Expr[Any]): (List[qctx.reflect.ValDef], Expr[R]) = {
7171
import qctx.reflect._
7272
val Block(List(DefDef("$anonfun", Nil, List(params), _, Some(body))), Closure(Ident("$anonfun"), None)) = f.unseal.etaExpand
73-
(params, body.seal.asInstanceOf[Expr[R]])
73+
(params, body.asExpr.asInstanceOf[Expr[R]])
7474
}
7575

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

tests/run-macros/quote-matcher-symantics-3/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ object Macros {
7777
object UnsafeExpr {
7878
def open[T1, R, X](f: Expr[T1 => R])(content: (Expr[R], [t] => Expr[t] => Expr[T1] => Expr[t]) => X)(using qctx: QuoteContext): X = {
7979
val (params, bodyExpr) = paramsAndBody[R](f)
80-
content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).seal.asInstanceOf[Expr[t]])
80+
content(bodyExpr, [t] => (e: Expr[t]) => (v: Expr[T1]) => bodyFn[t](e.unseal, params, List(v.unseal)).asExpr.asInstanceOf[Expr[t]])
8181
}
8282
private def paramsAndBody[R](using qctx: QuoteContext)(f: Expr[Any]): (List[qctx.reflect.ValDef], Expr[R]) = {
8383
import qctx.reflect._
8484
val Block(List(DefDef("$anonfun", Nil, List(params), _, Some(body))), Closure(Ident("$anonfun"), None)) = f.unseal.etaExpand
85-
(params, body.seal.asInstanceOf[Expr[R]])
85+
(params, body.asExpr.asInstanceOf[Expr[R]])
8686
}
8787

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

0 commit comments

Comments
 (0)