Skip to content

Commit cda0338

Browse files
Merge pull request #10207 from dotty-staging/make-quoted-type-api-fully-contextual
Make quoted.Type fully contextual
2 parents 2df5dbb + 4d33dbf commit cda0338

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.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import scala.annotation.compileTimeOnly
55
abstract class Type[T <: AnyKind] private[scala]:
66
type Underlying = T
77

8-
def unseal(using qctx: QuoteContext): qctx.reflect.TypeTree
9-
108
object Type:
119
@compileTimeOnly("Reference to `scala.quoted.Type.apply` was not handled by ReifyQuotes")
1210
given apply[T <: AnyKind] as (QuoteContext ?=> Type[T]) = ???

library/src/scala/tasty/Reflection.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,10 @@ trait Reflection { reflection =>
11671167

11681168
val TypeTree: TypeTreeModule
11691169

1170-
trait TypeTreeModule { this: TypeTree.type => }
1170+
trait TypeTreeModule { this: TypeTree.type =>
1171+
/** Returns the tree of type or kind (TypeTree) of T */
1172+
def of[T <: AnyKind](using tp: scala.quoted.Type[T]): TypeTree
1173+
}
11711174

11721175
given TypeTreeMethods as TypeTreeMethods = TypeTreeMethodsImpl
11731176
protected val TypeTreeMethodsImpl: TypeTreeMethods
@@ -1750,7 +1753,7 @@ trait Reflection { reflection =>
17501753

17511754
trait TypeReprModule { this: TypeRepr.type =>
17521755
/** Returns the type or kind (TypeRepr) of T */
1753-
def of[T <: AnyKind](using qtype: scala.quoted.Type[T]): TypeRepr
1756+
def of[T <: AnyKind](using tp: scala.quoted.Type[T]): TypeRepr
17541757

17551758
/** Returns the type constructor of the runtime (erased) class */
17561759
def typeConstructorOf(clazz: Class[?]): TypeRepr

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.reflect._
8-
Implicits.search((Type[A]).unseal.tpe) match {
8+
Implicits.search(TypeRepr.of[A]) 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.reflect._
8-
Implicits.search((Type[A]).unseal.tpe) match {
8+
Implicits.search(TypeRepr.of[A]) 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.reflect._
8-
Implicits.search((Type[A]).unseal.tpe) match {
8+
Implicits.search(TypeRepr.of[A]) match {
99
case x: ImplicitSearchSuccess =>
1010
'{}
1111
case x: DivergingImplicit => '{}

tests/neg-macros/i7048e.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract class Test {
1414
{
1515
val t: Test = this
1616
import t.given
17-
println(summon[Type[t.T]].show)
17+
println(Type.show[t.T])
1818
// val r = '{Option.empty[t.T]} // access to value t from wrong staging level
1919
val r2 = '{Option.empty[t.T.Underlying]} // works
2020
}

tests/neg-macros/i7919.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ object Test {
44
def staged[T](using qctx: QuoteContext) = {
55
import qctx.reflect._
66
given typeT as Type[T] // error
7-
val tTypeTree = typeT.unseal
87
val tt = TypeRepr.of[T]
98
'{ "in staged" }
109
}

tests/pos-macros/i7048e.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract class Test {
1414
{
1515
val t: Test = this
1616
import t.given
17-
println(summon[Type[t.T]].show)
17+
println(Type.show[t.T])
1818
// val r = '{Option.empty[t.T]} // access to value t from wrong staging level
1919
val r2 = '{Option.empty[t.T.Underlying]}
2020
}

tests/pos-macros/i8521/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object Foo {
1111
packageToName(sym.owner)
1212
}
1313

14-
val sym = implicitly[Type[T]].unseal.symbol
14+
val sym = TypeRepr.of[T].typeSymbol
1515
if (!sym.isNoSymbol) {
1616
sym.tree match {
1717
case c: ClassDef =>

tests/pos-macros/i9251/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ object Async {
2626
case Inlined(_,_,Block(_,Apply(TypeApply(Select(q,n),tparams),List(param)))) =>
2727
param.tpe match
2828
case AppliedType(tp,tparams1) =>
29-
val fType = Type[F]
29+
val fType = TypeRepr.of[F]
3030
val ptp = tparams1.tail.head
31-
val ptpTree = Inferred(fType.unseal.tpe.appliedTo(ptp))
31+
val ptpTree = Inferred(fType.appliedTo(ptp))
3232
'{ println(${Expr(ptpTree.show)}) }
3333

3434
}

tests/pos-macros/i9518/Macro_1.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ inline def shift : Unit = ${ shiftTerm }
88
def shiftTerm(using QuoteContext): Expr[Unit] = {
99
import qctx.reflect._
1010
val nTree = '{ ??? : CB[Int] }.unseal
11-
val tp1 = Type[CB[Int]].unseal.tpe
12-
val tp2 = Type[([X] =>> CB[X])[Int]].unseal.tpe
11+
val tp1 = TypeRepr.of[CB[Int]]
12+
val tp2 = TypeRepr.of[([X] =>> CB[X])[Int]]
1313
val ta = Type[[X] =>> CB[X]]
14-
val tp3 = Type[ta.Underlying[Int]].unseal.tpe
15-
val tp4 = Type[CB].unseal.tpe.appliedTo(TypeRepr.of[Int])
14+
val tp3 = TypeRepr.of(using Type[ta.Underlying[Int]])
15+
val tp4 = TypeRepr.of[CB].appliedTo(TypeRepr.of[Int])
1616
assert(nTree.tpe <:< tp1)
1717
assert(nTree.tpe <:< tp2)
1818
assert(nTree.tpe <:< tp3)

tests/pos-macros/i9894/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ object X:
4545
case lt@Lambda(params, body) =>
4646
val paramTypes = params.map(_.tpt.tpe)
4747
val paramNames = params.map(_.name)
48-
val mt = MethodType(paramNames)(_ => paramTypes, _ => Type[CB].unseal.tpe.appliedTo(body.tpe.widen) )
48+
val mt = MethodType(paramNames)(_ => paramTypes, _ => TypeRepr.of[CB].appliedTo(body.tpe.widen) )
4949
val r = Lambda(mt, args => changeArgs(params,args,transform(body)) )
5050
r
5151
case _ =>
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import scala.quoted._
22

33

4-
inline def isFunctionType[T]: Boolean = ${ isFunctionTypeImpl(Type[T]) }
4+
inline def isFunctionType[T]: Boolean = ${ isFunctionTypeImpl[T] }
55

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

1111

12-
inline def isContextFunctionType[T]: Boolean = ${ isContextFunctionTypeImpl(Type[T]) }
12+
inline def isContextFunctionType[T]: Boolean = ${ isContextFunctionTypeImpl[T] }
1313

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

1919

20-
inline def isErasedFunctionType[T]: Boolean = ${ isErasedFunctionTypeImpl(Type[T]) }
20+
inline def isErasedFunctionType[T]: Boolean = ${ isErasedFunctionTypeImpl[T] }
2121

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

27-
inline def isDependentFunctionType[T]: Boolean = ${ isDependentFunctionTypeImpl(Type[T]) }
27+
inline def isDependentFunctionType[T]: Boolean = ${ isDependentFunctionTypeImpl[T] }
2828

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

tests/run-macros/flops-rewrite-3/Macro_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ class CheckedTransformation(transform: PartialFunction[Expr[Any], Expr[Any]]) ex
5959
def apply[T: Type](e: Expr[T])(using QuoteContext): Expr[T] = {
6060
transform.applyOrElse(e, identity) match {
6161
case '{ $e2: T } => e2
62-
case '{ $e2: $T } =>
62+
case '{ $e2: $T2 } =>
6363
throw new Exception(
6464
s"""Transformed
6565
|${e.show}
6666
|into
6767
|${e2.show}
6868
|
6969
|Expected type to be
70-
|${summon[Type[T]].show}
70+
|${Type.show[T]}
7171
|but was
72-
|${Type[T].show}
72+
|${Type.show[T2]}
7373
""".stripMargin)
7474
}
7575
}

tests/run-macros/flops-rewrite/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ private class Rewriter(preTransform: Expr[Any] => Expr[Any], postTransform: Expr
4747
|${x.show}
4848
|
4949
|Expected type to be
50-
|${summon[Type[T]].show}
50+
|${Type.show[T]}
5151
|but was
52-
|${Type[t].show}
52+
|${Type.show[t]}
5353
""".stripMargin)
5454
}
5555
}

0 commit comments

Comments
 (0)