Skip to content

Commit 4d33dbf

Browse files
committed
Make quoted.Type fully contextual
Type is designed to be a contextual value. As such we should be able to use it without needing to name it and use it explicitly. Now that quoted pattterns provide the name of the type we can do this. * Replaced methods `show`/`showAnsiColored` by equivalent methods in `object Type` * Remove `unseal`, it can be replaced directly with `TypeRepr.of` in most cases __Migration__ * `Type[T].show` -> `Type.show[T]` * `Type[T].unseal.tpe` -> `TypeRepr.of[T]` * `Type[T].unseal` -> `TypeTree.of[T]`
1 parent f2d546c commit 4d33dbf

File tree

47 files changed

+165
-170
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+165
-170
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,10 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
10011001
case _ => None
10021002
end TypeTreeTypeTest
10031003

1004-
object TypeTree extends TypeTreeModule
1004+
object TypeTree extends TypeTreeModule:
1005+
def of[T <: AnyKind](using tp: scala.quoted.Type[T]): TypeTree =
1006+
tp.asInstanceOf[scala.internal.quoted.Type[TypeTree]].typeTree
1007+
end TypeTree
10051008

10061009
object TypeTreeMethodsImpl extends TypeTreeMethods:
10071010
extension (self: TypeTree):
@@ -1568,8 +1571,8 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
15681571
type TypeRepr = dotc.core.Types.Type
15691572

15701573
object TypeRepr extends TypeReprModule:
1571-
def of[T <: AnyKind](using qtype: scala.quoted.Type[T]): TypeRepr =
1572-
qtype.asInstanceOf[scala.internal.quoted.Type[TypeTree]].typeTree.tpe
1574+
def of[T <: AnyKind](using tp: scala.quoted.Type[T]): TypeRepr =
1575+
tp.asInstanceOf[scala.internal.quoted.Type[TypeTree]].typeTree.tpe
15731576
def typeConstructorOf(clazz: Class[?]): TypeRepr =
15741577
if (clazz.isPrimitive)
15751578
if (clazz == classOf[Boolean]) dotc.core.Symbols.defn.BooleanType
@@ -2640,7 +2643,7 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.intern
26402643
treeMatch(scrutinee.unseal(using this), pattern.unseal(using this))
26412644

26422645
def typeMatch(scrutinee: scala.quoted.Type[?], pattern: scala.quoted.Type[?]): Option[Tuple] =
2643-
treeMatch(scrutinee.unseal(using this), pattern.unseal(using this))
2646+
treeMatch(reflect.TypeTree.of(using scrutinee), reflect.TypeTree.of(using pattern))
26442647

26452648
private def treeMatch(scrutinee: reflect.Tree, pattern: reflect.Tree): Option[Tuple] = {
26462649
import reflect._

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract class Expr[+T] private[scala] {
2525

2626
/** Checks is the `quoted.Expr[?]` is valid expression of type `X` */
2727
def isExprOf[X](using tp: scala.quoted.Type[X])(using qctx: QuoteContext): Boolean =
28-
this.unseal.tpe <:< tp.unseal.tpe
28+
this.unseal.tpe <:< qctx.reflect.TypeRepr.of[X]
2929

3030
/** Convert this to an `quoted.Expr[X]` if this expression is a valid expression of type `X` or throws */
3131
def asExprOf[X](using tp: scala.quoted.Type[X])(using qctx: QuoteContext): scala.quoted.Expr[X] = {
@@ -35,7 +35,7 @@ abstract class Expr[+T] private[scala] {
3535
throw Exception(
3636
s"""Expr cast exception: ${this.show}
3737
|of type: ${this.unseal.tpe.show}
38-
|did not conform to type: ${tp.unseal.tpe.show}
38+
|did not conform to type: ${qctx.reflect.TypeRepr.of[X].show}
3939
|""".stripMargin
4040
)
4141
}
@@ -189,7 +189,7 @@ object Expr {
189189
*/
190190
def summon[T](using tpe: Type[T])(using qctx: QuoteContext): Option[Expr[T]] = {
191191
import qctx.reflect._
192-
Implicits.search(tpe.unseal.tpe) match {
192+
Implicits.search(TypeRepr.of[T]) match {
193193
case iss: ImplicitSearchSuccess => Some(iss.tree.seal.asInstanceOf[Expr[T]])
194194
case isf: ImplicitSearchFailure => None
195195
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ trait ExprMap:
144144
trees mapConserve (transformTypeCaseDef(_))
145145

146146
}
147-
new MapChildren().transformTermChildren(e.unseal, tpe.unseal.tpe).asExprOf[T]
147+
new MapChildren().transformTermChildren(e.unseal, TypeRepr.of[T]).asExprOf[T]
148148
}
149149

150150
end ExprMap

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,24 @@ package scala.quoted
33
import scala.annotation.compileTimeOnly
44

55
/** Quoted type (or kind) `T` */
6-
abstract class Type[T <: AnyKind] private[scala] {
7-
6+
abstract class Type[T <: AnyKind] private[scala]:
87
/** The type represented `Type` */
98
type Underlying = T
9+
end Type
10+
11+
/** Some basic type tags, currently incomplete */
12+
object Type:
1013

1114
/** Show a source code like representation of this type without syntax highlight */
12-
def show(using qctx: QuoteContext): String = this.unseal.show
15+
def show[T](using tp: Type[T])(using qctx: QuoteContext): String =
16+
qctx.reflect.TypeTree.of[T].show
1317

1418
/** Shows the tree as fully typed source code colored with ANSI */
15-
def showAnsiColored(using qctx: QuoteContext): String = this.unseal.showAnsiColored
16-
17-
/** View this expression `quoted.Type[T]` as a `TypeTree` */
18-
def unseal(using qctx: QuoteContext): qctx.reflect.TypeTree
19-
20-
}
21-
22-
/** Some basic type tags, currently incomplete */
23-
object Type {
19+
def showAnsiColored[T](using tp: Type[T])(using qctx: QuoteContext): String =
20+
qctx.reflect.TypeTree.of[T].showAnsiColored
2421

2522
/** Return a quoted.Type with the given type */
2623
@compileTimeOnly("Reference to `scala.quoted.Type.apply` was not handled by ReifyQuotes")
2724
given apply[T <: AnyKind] as (QuoteContext ?=> Type[T]) = ???
2825

29-
}
26+
end Type

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.reflect._
20-
Repeated(xs.map[Term](_.unseal).toList, tp.unseal).seal.asInstanceOf[Expr[Seq[T]]]
20+
Repeated(xs.map[Term](_.unseal).toList, TypeTree.of[T]).seal.asInstanceOf[Expr[Seq[T]]]
2121
}
2222

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

0 commit comments

Comments
 (0)