Skip to content

Commit 5164b87

Browse files
committed
Add quoted.Type.asTypeTree and tasty.Type.asQuotedType
1 parent ffef834 commit 5164b87

File tree

35 files changed

+98
-89
lines changed

35 files changed

+98
-89
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ abstract class Expr[+T] private[scala] {
4747
/** Convert to an `quoted.Expr[X]` if this expression is a valid expression of type `X` or throws */
4848
def asExprOf[X](using tp: scala.quoted.Type[X])(using qctx: QuoteContext): scala.quoted.Expr[X] = {
4949
val tree = this.asTerm
50-
val expectedType = tp.unseal.tpe
50+
val expectedType = tp.asTypeTree.tpe
5151
if (tree.tpe <:< expectedType)
5252
this.asInstanceOf[scala.quoted.Expr[X]]
5353
else
@@ -199,7 +199,7 @@ object Expr {
199199
*/
200200
def summon[T](using tpe: Type[T])(using qctx: QuoteContext): Option[Expr[T]] = {
201201
import qctx.tasty._
202-
searchImplicit(tpe.unseal.tpe) match {
202+
searchImplicit(tpe.asTypeTree.tpe) match {
203203
case iss: ImplicitSearchSuccess => Some(iss.tree.asExpr.asInstanceOf[Expr[T]])
204204
case isf: ImplicitSearchFailure => None
205205
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object Lambda {
1717
*/
1818
def unapply[F, Args <: Tuple, Res, G](expr: Expr[F])(using qctx: QuoteContext, tf: TupledFunction[F, Args => Res], tg: TupledFunction[G, Tuple.Map[Args, Expr] => Expr[Res]], functionType: Type[F]): Option[/*QuoteContext ?=>*/ G] = {
1919
import qctx.tasty._
20-
val argTypes = functionType.unseal.tpe match
20+
val argTypes = functionType.asTypeTree.tpe match
2121
case AppliedType(_, functionArguments) => functionArguments.init.asInstanceOf[List[Type]]
2222
qctx.tasty.internal.lambdaExtractor(expr.asTerm, argTypes).map { fn =>
2323
def f(args: Tuple.Map[Args, Expr]): Expr[Res] =

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ abstract class Type[X <: AnyKind] private[scala] {
99

1010
/** Show a source code like representation of this type without syntax highlight */
1111
def show(using qctx: QuoteContext): String =
12-
this.unseal.showWith(SyntaxHighlight.plain)
12+
this.asTypeTree.showWith(SyntaxHighlight.plain)
1313

1414
/** Show a source code like representation of this type */
1515
def showWith(syntaxHighlight: SyntaxHighlight)(using qctx: QuoteContext): String =
16-
this.unseal.showWith(syntaxHighlight)
16+
this.asTypeTree.showWith(syntaxHighlight)
1717

1818
/** View this expression `quoted.Type[T]` as a `TypeTree` */
19-
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree
19+
@deprecated("Replaced with `asExprOf`", "0.27.0")
20+
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree = asTypeTree
21+
22+
/** View this expression `quoted.Type[T]` as a `qctx.tasty.TypeTree` */
23+
def asTypeTree(using qctx: QuoteContext): qctx.tasty.TypeTree
2024

2125
}
2226

@@ -28,30 +32,30 @@ object Type {
2832
given apply[T <: AnyKind] as (QuoteContext ?=> Type[T]) = ???
2933

3034
def UnitTag: QuoteContext ?=> Type[Unit] =
31-
qctx.tasty.defn.UnitType.seal.asInstanceOf[quoted.Type[Unit]]
35+
qctx.tasty.defn.UnitType.asQuotedType.asInstanceOf[quoted.Type[Unit]]
3236

3337
def BooleanTag: QuoteContext ?=> Type[Boolean] =
34-
qctx.tasty.defn.BooleanType.seal.asInstanceOf[quoted.Type[Boolean]]
38+
qctx.tasty.defn.BooleanType.asQuotedType.asInstanceOf[quoted.Type[Boolean]]
3539

3640
def ByteTag: QuoteContext ?=> Type[Byte] =
37-
qctx.tasty.defn.ByteType.seal.asInstanceOf[quoted.Type[Byte]]
41+
qctx.tasty.defn.ByteType.asQuotedType.asInstanceOf[quoted.Type[Byte]]
3842

3943
def CharTag: QuoteContext ?=> Type[Char] =
40-
qctx.tasty.defn.CharType.seal.asInstanceOf[quoted.Type[Char]]
44+
qctx.tasty.defn.CharType.asQuotedType.asInstanceOf[quoted.Type[Char]]
4145

4246
def ShortTag: QuoteContext ?=> Type[Short] =
43-
qctx.tasty.defn.ShortType.seal.asInstanceOf[quoted.Type[Short]]
47+
qctx.tasty.defn.ShortType.asQuotedType.asInstanceOf[quoted.Type[Short]]
4448

4549
def IntTag: QuoteContext ?=> Type[Int] =
46-
qctx.tasty.defn.IntType.seal.asInstanceOf[quoted.Type[Int]]
50+
qctx.tasty.defn.IntType.asQuotedType.asInstanceOf[quoted.Type[Int]]
4751

4852
def LongTag: QuoteContext ?=> Type[Long] =
49-
qctx.tasty.defn.LongType.seal.asInstanceOf[quoted.Type[Long]]
53+
qctx.tasty.defn.LongType.asQuotedType.asInstanceOf[quoted.Type[Long]]
5054

5155
def FloatTag: QuoteContext ?=> Type[Float] =
52-
qctx.tasty.defn.FloatType.seal.asInstanceOf[quoted.Type[Float]]
56+
qctx.tasty.defn.FloatType.asQuotedType.asInstanceOf[quoted.Type[Float]]
5357

5458
def DoubleTag: QuoteContext ?=> Type[Double] =
55-
qctx.tasty.defn.DoubleType.seal.asInstanceOf[quoted.Type[Double]]
59+
qctx.tasty.defn.DoubleType.asQuotedType.asInstanceOf[quoted.Type[Double]]
5660

5761
}

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

Lines changed: 1 addition & 1 deletion
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.tasty._
20-
Repeated(xs.map[Term](_.asTerm).toList, tp.unseal).asExpr.asInstanceOf[Expr[Seq[T]]]
20+
Repeated(xs.map[Term](_.asTerm).toList, tp.asTypeTree).asExpr.asInstanceOf[Expr[Seq[T]]]
2121
}
2222

2323
/** Matches a literal sequence of expressions and return a sequence of expressions.

library/src-bootstrapped/scala/quoted/util/ExprMap.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ trait ExprMap {
6666
case AppliedType(TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "<repeated>"), List(tp0: Type)) =>
6767
// TODO rewrite without using quotes
6868
type T
69-
val qtp: quoted.Type[T] = tp0.seal.asInstanceOf[quoted.Type[T]]
69+
val qtp: quoted.Type[T] = tp0.asQuotedType.asInstanceOf[quoted.Type[T]]
7070
given qtp.type = qtp
71-
'[Seq[T]].unseal.tpe
71+
'[Seq[T]].asTypeTree.tpe
7272
case tp => tp
7373
Typed.copy(tree)(transformTerm(expr, tp), transformTypeTree(tpt))
7474
case tree: NamedArg =>
@@ -110,7 +110,7 @@ trait ExprMap {
110110
case _ if tree.isExpr =>
111111
type X
112112
val expr = tree.asExpr.asInstanceOf[Expr[X]]
113-
val t = tpe.seal.asInstanceOf[quoted.Type[X]]
113+
val t = tpe.asQuotedType.asInstanceOf[quoted.Type[X]]
114114
transform(expr)(using qctx, t).asTerm
115115
case _ =>
116116
transformTermChildren(tree, tpe)
@@ -152,7 +152,7 @@ trait ExprMap {
152152
trees mapConserve (transformTypeCaseDef(_))
153153

154154
}
155-
new MapChildren().transformTermChildren(e.asTerm, tpe.unseal.tpe).asExprOf[T]
155+
new MapChildren().transformTermChildren(e.asTerm, tpe.asTypeTree.tpe).asExprOf[T]
156156
}
157157

158158
}

library/src-non-bootstrapped/scala/quoted/Type.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package scala.quoted
22

33
abstract class Type[T <: AnyKind] private[scala]:
44
type `$splice` = T
5-
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree
5+
def asTypeTree(using qctx: QuoteContext): qctx.tasty.TypeTree

library/src/scala/internal/quoted/Matcher.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ object Matcher {
154154
// that we have found and seal them in a quoted.Type
155155
matchings.asOptionOfTuple.map { tup =>
156156
Tuple.fromArray(tup.toArray.map { // TODO improve performance
157-
case x: SymBinding => internal.Context_GADT_approximation(summon[Context])(x.sym, !x.fromAbove).seal
157+
case x: SymBinding => internal.Context_GADT_approximation(summon[Context])(x.sym, !x.fromAbove).asQuotedType
158158
case x => x
159159
})
160160
}
@@ -175,7 +175,7 @@ object Matcher {
175175
// that we have found and seal them in a quoted.Type
176176
matchings.asOptionOfTuple.map { tup =>
177177
Tuple.fromArray(tup.toArray.map { // TODO improve performance
178-
case x: SymBinding => internal.Context_GADT_approximation(summon[Context])(x.sym, !x.fromAbove).seal
178+
case x: SymBinding => internal.Context_GADT_approximation(summon[Context])(x.sym, !x.fromAbove).asQuotedType
179179
case x => x
180180
})
181181
}

library/src/scala/internal/quoted/Type.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quote
1313
}
1414

1515
/** View this expression `quoted.Type[T]` as a `TypeTree` */
16-
def unseal(using qctx: QuoteContext): qctx.tasty.TypeTree =
16+
def asTypeTree(using qctx: QuoteContext): qctx.tasty.TypeTree =
1717
if (qctx.tasty.internal.compilerId != scopeId)
1818
throw new scala.quoted.ScopeException("Cannot call `scala.quoted.staging.run(...)` within a macro or another `run(...)`")
1919
typeTree.asInstanceOf[qctx.tasty.TypeTree]
@@ -39,7 +39,7 @@ object Type {
3939
*/
4040
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeType: scala.quoted.Type[_])(using patternType: scala.quoted.Type[_],
4141
hasTypeSplices: Boolean, qctx: QuoteContext): Option[Tup] = {
42-
new Matcher.QuoteMatcher[qctx.type].typeTreeMatch(scrutineeType.unseal, patternType.unseal, hasTypeSplices).asInstanceOf[Option[Tup]]
42+
new Matcher.QuoteMatcher[qctx.type].typeTreeMatch(scrutineeType.asTypeTree, patternType.asTypeTree, hasTypeSplices).asInstanceOf[Option[Tup]]
4343
}
4444

4545
}

library/src/scala/tasty/Reflection.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
500500

501501
/** Convert to an `quoted.Expr[Any]` if the tree is a valid expression or throws */
502502
def asExpr(using QuoteContext): scala.quoted.Expr[Any] =
503-
assert(tree.isExpr, tree)
503+
assert(tree.isExpr, tree.show)
504504
new scala.internal.quoted.Expr(tree, internal.compilerId)
505505

506506
end extension
@@ -1788,9 +1788,14 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
17881788
extension (self: Type):
17891789

17901790
/** Convert `Type` to an `quoted.Type[_]` */
1791+
@deprecated("Replaced with `asQuotedType`", "0.27.0")
17911792
def seal(using ctx: Context): scala.quoted.Type[_] =
17921793
new scala.internal.quoted.Type(Inferred(self), internal.compilerId)
17931794

1795+
/** Convert to a `quoted.Type[_]` */
1796+
def asQuotedType(using ctx: Context): scala.quoted.Type[_] =
1797+
new scala.internal.quoted.Type(Inferred(self), internal.compilerId)
1798+
17941799
/** Is `self` type the same as `that` type?
17951800
* This is the case iff `self <:< that` and `that <:< self`.
17961801
*/

tests/neg-macros/delegate-match-1/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inline def f: Any = ${ fImpl }
55

66
private def fImpl(using qctx: QuoteContext): Expr[Unit] = {
77
import qctx.tasty._
8-
searchImplicit(('[A]).unseal.tpe) match {
8+
searchImplicit(('[A]).asTypeTree.tpe) match {
99
case x: ImplicitSearchSuccess =>
1010
'{}
1111
case x: DivergingImplicit => '{}

tests/neg-macros/delegate-match-2/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inline def f: Any = ${ fImpl }
55

66
private def fImpl (using qctx: QuoteContext) : Expr[Unit] = {
77
import qctx.tasty._
8-
searchImplicit(('[A]).unseal.tpe) match {
8+
searchImplicit(('[A]).asTypeTree.tpe) match {
99
case x: ImplicitSearchSuccess =>
1010
'{}
1111
case x: DivergingImplicit => '{}

tests/neg-macros/delegate-match-3/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ inline def f: Any = ${ fImpl }
55

66
private def fImpl(using qctx: QuoteContext) : Expr[Unit] = {
77
import qctx.tasty._
8-
searchImplicit(('[A]).unseal.tpe) match {
8+
searchImplicit(('[A]).asTypeTree.tpe) match {
99
case x: ImplicitSearchSuccess =>
1010
'{}
1111
case x: DivergingImplicit => '{}

tests/neg-macros/i7919.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ object Test {
44
def staged[T](using qctx: QuoteContext) = {
55
import qctx.tasty._
66
given typeT as quoted.Type[T] // error
7-
val tTypeTree = typeT.unseal
7+
val tTypeTree = typeT.asTypeTree
88
val tt = typeOf[T]
99
'{ "in staged" }
1010
}

tests/neg-macros/i8871.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scala.quoted._
22
object Macro {
33
def impl[A : Type](using qctx: QuoteContext): Unit = {
44
import qctx.tasty._
5-
val tpe = typeOf[A].seal.asInstanceOf[quoted.Type[_ <: AnyRef]]
5+
val tpe = typeOf[A].asQuotedType.asInstanceOf[quoted.Type[_ <: AnyRef]]
66
'{ (a: ${tpe}) => ???} // error
77
}
88
}

tests/neg-macros/i8871b.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import scala.quoted._
22
object Macro {
33
def impl[A : Type](using qctx: QuoteContext): Unit = {
44
import qctx.tasty._
5-
val tpe/*: quoted.Type[? <: AnyKind]*/ = typeOf[A].seal
5+
val tpe/*: quoted.Type[? <: AnyKind]*/ = typeOf[A].asQuotedType
66
'{ f[$tpe] } // error
77
}
88
def f[T <: AnyKind]: Unit = ()

tests/neg-staging/i5941/macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object Lens {
1717
import util._
1818
// obj.copy(field = value)
1919
def setterBody(obj: Expr[S], value: Expr[T], field: String): Expr[S] =
20-
Select.overloaded(obj.asTerm, "copy", Nil, NamedArg(field, value.asTerm) :: Nil).seal.cast[S]
20+
Select.overloaded(obj.asTerm, "copy", Nil, NamedArg(field, value.asTerm) :: Nil).asExprOf[S]
2121

2222
// exception: getter.asTerm.underlyingArgument
2323
getter.asTerm match {

tests/pos-macros/i9251/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ object Async {
2121
def checkPrintTypeImpl[F[_]:Type,T:Type](f: Expr[T])(using qctx: QuoteContext): Expr[Unit] =
2222
import qctx.tasty._
2323

24-
val fu = f.unseal
24+
val fu = f.asTerm
2525
fu match
2626
case Inlined(_,_,Block(_,Apply(TypeApply(Select(q,n),tparams),List(param)))) =>
2727
param.tpe match
2828
case AppliedType(tp,tparams1) =>
2929
val fType = summon[quoted.Type[F]]
3030
val ptp = tparams1.tail.head
31-
val ptpTree = Inferred(AppliedType(fType.unseal.tpe,List(ptp)))
31+
val ptpTree = Inferred(AppliedType(fType.asTypeTree.tpe,List(ptp)))
3232
'{ println(${Expr(ptpTree.show)}) }
3333

3434
}

tests/pos-macros/tasty-constant-type/Macro_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ object Macro {
99
def impl[A <: Int : Type, B <: Int : Type](a: Type[A], b: Type[B])(using qctx: QuoteContext) : Expr[AddInt[A, B]] = {
1010
import qctx.tasty._
1111

12-
val ConstantType(Constant(v1: Int)) = a.unseal.tpe
13-
val ConstantType(Constant(v2: Int)) = b.unseal.tpe
12+
val ConstantType(Constant(v1: Int)) = a.asTypeTree.tpe
13+
val ConstantType(Constant(v2: Int)) = b.asTypeTree.tpe
1414

15-
Literal(Constant((v1 + v2): Int)).tpe.seal match
15+
Literal(Constant((v1 + v2): Int)).tpe.asQuotedType match
1616
case '[$t] => '{ null: AddInt[$a, $b] { type Out = $t } }
1717
}
1818
}

tests/run-custom-args/run-macros-erased/reflect-isFunctionType/macro_1.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,29 @@ inline def isFunctionType[T]: Boolean = ${ isFunctionTypeImpl('[T]) }
55

66
def isFunctionTypeImpl[T](tp: Type[T])(using qctx: QuoteContext) : Expr[Boolean] = {
77
import qctx.tasty._
8-
Expr(tp.unseal.tpe.isFunctionType)
8+
Expr(tp.asTypeTree.tpe.isFunctionType)
99
}
1010

1111

1212
inline def isContextFunctionType[T]: Boolean = ${ isContextFunctionTypeImpl('[T]) }
1313

1414
def isContextFunctionTypeImpl[T](tp: Type[T])(using qctx: QuoteContext) : Expr[Boolean] = {
1515
import qctx.tasty._
16-
Expr(tp.unseal.tpe.isContextFunctionType)
16+
Expr(tp.asTypeTree.tpe.isContextFunctionType)
1717
}
1818

1919

2020
inline def isErasedFunctionType[T]: Boolean = ${ isErasedFunctionTypeImpl('[T]) }
2121

2222
def isErasedFunctionTypeImpl[T](tp: Type[T])(using qctx: QuoteContext) : Expr[Boolean] = {
2323
import qctx.tasty._
24-
Expr(tp.unseal.tpe.isErasedFunctionType)
24+
Expr(tp.asTypeTree.tpe.isErasedFunctionType)
2525
}
2626

2727
inline def isDependentFunctionType[T]: Boolean = ${ isDependentFunctionTypeImpl('[T]) }
2828

2929
def isDependentFunctionTypeImpl[T](tp: Type[T])(using qctx: QuoteContext) : Expr[Boolean] = {
3030
import qctx.tasty._
31-
Expr(tp.unseal.tpe.isDependentFunctionType)
31+
Expr(tp.asTypeTree.tpe.isDependentFunctionType)
3232
}
3333

0 commit comments

Comments
 (0)