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

0 commit comments

Comments
 (0)