Skip to content

Commit 19e47b7

Browse files
Merge pull request #10097 from dotty-staging/rename-Type-underlying-type
Rename Type.T to Type.Underlying
2 parents 5649570 + 42405e0 commit 19e47b7

File tree

9 files changed

+28
-26
lines changed

9 files changed

+28
-26
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ class Definitions {
830830
@tu lazy val InternalQuotedType_unapply: Symbol = InternalQuotedTypeModule.requiredMethod(nme.unapply)
831831

832832
@tu lazy val QuotedTypeClass: ClassSymbol = requiredClass("scala.quoted.Type")
833-
@tu lazy val QuotedType_splice: Symbol = QuotedTypeClass.requiredType(tpnme.spliceType)
833+
@tu lazy val QuotedType_splice: Symbol = QuotedTypeClass.requiredType(tpnme.Underlying)
834834

835835
@tu lazy val QuotedTypeModule: Symbol = QuotedTypeClass.companionModule
836836
@tu lazy val QuotedTypeModule_apply: Symbol = QuotedTypeModule.requiredMethod("apply")

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ object StdNames {
242242
final val Tree: N = "Tree"
243243
final val Type : N = "Type"
244244
final val TypeTree: N = "TypeTree"
245+
final val Underlying: N = "Underlying"
245246

246247
// Annotation simple names, used in Namer
247248
final val BeanPropertyAnnot: N = "BeanProperty"
@@ -583,7 +584,6 @@ object StdNames {
583584
val setSymbol: N = "setSymbol"
584585
val setType: N = "setType"
585586
val setTypeSignature: N = "setTypeSignature"
586-
val spliceType: N = "T"
587587
val standardInterpolator: N = "standardInterpolator"
588588
val staticClass : N = "staticClass"
589589
val staticModule : N = "staticModule"

compiler/src/dotty/tools/dotc/transform/PCPCheckAndHeal.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,13 @@ object PCPCheckAndHeal {
271271

272272
private def mkTagSymbolAndAssignType(spliced: TermRef): TypeDef = {
273273
val splicedTree = tpd.ref(spliced).withSpan(span)
274-
val rhs = splicedTree.select(tpnme.spliceType).withSpan(span)
274+
val rhs = splicedTree.select(tpnme.Underlying).withSpan(span)
275275
val alias = ctx.typeAssigner.assignType(untpd.TypeBoundsTree(rhs, rhs), rhs, rhs, EmptyTree)
276276
val local = newSymbol(
277277
owner = ctx.owner,
278278
name = UniqueName.fresh((splicedTree.symbol.name.toString + "$_").toTermName).toTypeName,
279279
flags = Synthetic,
280-
info = TypeAlias(splicedTree.tpe.select(tpnme.spliceType)),
280+
info = TypeAlias(splicedTree.tpe.select(tpnme.Underlying)),
281281
coord = span).asType
282282
local.addAnnotation(Annotation(defn.InternalQuoted_QuoteTypeTagAnnot))
283283
ctx.typeAssigner.assignType(untpd.TypeDef(local.name, alias), local)

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class ReifyQuotes extends MacroTransform {
370370
case tree: RefTree if isCaptured(tree.symbol, level) =>
371371
val body = capturers(tree.symbol).apply(tree)
372372
if (tree.isType)
373-
transformSpliceType(body, body.select(tpnme.spliceType))
373+
transformSpliceType(body, body.select(tpnme.Underlying))
374374
else
375375
val splice = ref(defn.InternalQuoted_exprSplice).appliedToType(tree.tpe).appliedTo(body)
376376
transformSplice(body, splice)

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ trait QuotesAndSplices {
176176
typeSym.addAnnotation(Annotation(New(ref(defn.InternalQuotedPatterns_patternTypeAnnot.typeRef)).withSpan(tree.expr.span)))
177177
val pat = typedPattern(expr, defn.QuotedTypeClass.typeRef.appliedTo(typeSym.typeRef))(
178178
using spliceContext.retractMode(Mode.QuotedPattern).withOwner(spliceOwner(ctx)))
179-
pat.select(tpnme.spliceType)
179+
pat.select(tpnme.Underlying)
180180
else
181-
val tree1 = typedSelect(untpd.Select(tree.expr, tpnme.spliceType), pt)(using spliceContext).withSpan(tree.span)
181+
val tree1 = typedSelect(untpd.Select(tree.expr, tpnme.Underlying), pt)(using spliceContext).withSpan(tree.span)
182182
val msg = em"Consider using canonical type reference ${tree1.tpe} instead"
183183
if sourceVersion.isAtLeast(`3.1-migration`) then report.error(msg, tree.srcPos)
184184
else report.warning(msg, tree.srcPos)

docs/docs/reference/metaprogramming/macros.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,25 +187,25 @@ Indeed, the definition of `to` above uses `T` in the next stage, there is a
187187
quote but no splice between the parameter binding of `T` and its
188188
usage. But the code can be rewritten by adding a binding of a `Type[T]` tag:
189189
```scala
190-
def to[T, R: Type](f: Expr[T] => Expr[R])(using t: Type[T])(using QuoteContext): Expr[T => R] =
191-
'{ (x: $t) => ${ f('x) } }
190+
def to[T, R](f: Expr[T] => Expr[R])(using Type[T], Type[R], QuoteContext): Expr[T => R] =
191+
'{ (x: T) => ${ f('x) } }
192192
```
193193
In this version of `to`, the type of `x` is now the result of
194194
splicing the `Type` value `t`. This operation _is_ splice correct -- there
195195
is one quote and one splice between the use of `t` and its definition.
196196

197197
To avoid clutter, the Scala implementation tries to convert any type
198-
reference to a type `T` in subsequent phases to a type-splice, by rewriting `T` to `${ summon[Type[T]] }`.
198+
reference to a type `T` in subsequent phases to a type-splice, by rewriting `T` to `summon[Type[T]].Underlying`.
199199
For instance, the user-level definition of `to`:
200200

201201
```scala
202-
def to[T: Type, R: Type](f: Expr[T] => Expr[R])(using QuoteContext): Expr[T => R] =
202+
def to[T, R](f: Expr[T] => Expr[R])(using t: Type[T], r: Type[R], QuoteContext): Expr[T => R] =
203203
'{ (x: T) => ${ f('x) } }
204204
```
205205
would be rewritten to
206206
```scala
207-
def to[T: Type, R: Type](f: Expr[T] => Expr[R])(using QuoteContext): Expr[T => R] =
208-
'{ (x: ${ summon[Type[T]] }) => ${ f('x) } }
207+
def to[T, R](f: Expr[T] => Expr[R])(using t: Type[T], r: Type[R], QuoteContext): Expr[T => R] =
208+
'{ (x: t.Underlying }) => ${ f('x) } }
209209
```
210210
The `summon` query succeeds because there is a given instance of
211211
type `Type[T]` available (namely the given parameter corresponding
@@ -499,10 +499,10 @@ function `f` and one `sum` that performs a sum by delegating to `map`.
499499

500500
```scala
501501
object Macros {
502-
def map[T](arr: Expr[Array[T]], f: Expr[T] => Expr[Unit])(using t: Type[T], qctx: QuoteContext): Expr[Unit] = '{
502+
def map[T](arr: Expr[Array[T]], f: Expr[T] => Expr[Unit])(using Type[T], QuoteContext): Expr[Unit] = '{
503503
var i: Int = 0
504504
while (i < ($arr).length) {
505-
val element: $t = ($arr)(i)
505+
val element: T = ($arr)(i)
506506
${f('element)}
507507
i += 1
508508
}
@@ -703,11 +703,11 @@ Sometimes it is necessary to get a more precise type for an expression. This can
703703
```scala
704704
def f(exp: Expr[Any])(using QuoteContext) =
705705
expr match
706-
case '{ $x: $t } =>
706+
case '{ $x: $T } =>
707707
// If the pattern match succeeds, then there is some type `T` such that
708708
// - `x` is bound to a variable of type `Expr[T]`
709-
// - `t` is bound to a given instance of type `Type[T]`
710-
// That is, we have `x: Expr[T]` and `given t: Type[T]`, for some (unknown) type `T`.
709+
// - `T` is bound to a new type T and a given instance `Type[T]` is provided for it
710+
// That is, we have `x: Expr[T]` and `given Type[T]`, for some (unknown) type `T`.
711711
```
712712

713713
This might be used to then perform an implicit search as in:
@@ -720,9 +720,8 @@ private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using
720720
argsExpr match {
721721
case Varargs(argExprs) =>
722722
val argShowedExprs = argExprs.map {
723-
case '{ $arg: $tp } =>
724-
val showTp = '[Show[$tp]]
725-
Expr.summon(using showTp) match {
723+
case '{ $arg: $T } =>
724+
Expr.summon[Show[T]] match {
726725
case Some(showExpr) => '{ $showExpr.show($arg) }
727726
case None => Reporting.error(s"could not find implicit for ${showTp.show}", arg); '{???}
728727
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import scala.annotation.compileTimeOnly
44
import scala.quoted.show.SyntaxHighlight
55

66
/** Quoted type (or kind) `T` */
7-
abstract class Type[X <: AnyKind] private[scala] {
8-
type T = X
7+
abstract class Type[T <: AnyKind] private[scala] {
8+
9+
/** The type represented `Type` */
10+
type Underlying = T
911

1012
/** Show a source code like representation of this type without syntax highlight */
1113
def show(using qctx: QuoteContext): String =

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package scala.quoted
22

33
import scala.annotation.compileTimeOnly
44

5-
abstract class Type[X <: AnyKind] private[scala]:
6-
type T = X
5+
abstract class Type[T <: AnyKind] private[scala]:
6+
type Underlying = T
7+
78
def unseal(using qctx: QuoteContext): qctx.reflect.TypeTree
89

910
object Type:

tests/pos-macros/i9518/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def shiftTerm(using QuoteContext): Expr[Unit] = {
1111
val tp1 = '[CB[Int]].unseal.tpe
1212
val tp2 = '[([X] =>> CB[X])[Int]].unseal.tpe
1313
val ta = '[[X] =>> CB[X]]
14-
val tp3 = '[ta.T[Int]].unseal.tpe
14+
val tp3 = '[ta.Underlying[Int]].unseal.tpe
1515
val tp4 = '[CB].unseal.tpe.appliedTo(TypeRepr.of[Int])
1616
assert(nTree.tpe <:< tp1)
1717
assert(nTree.tpe <:< tp2)

0 commit comments

Comments
 (0)