Skip to content

Commit 9ec34f8

Browse files
Merge pull request #10110 from dotty-staging/use-canonical-undelying-type
Use canonical underlying type reference
2 parents 1f433d5 + a779493 commit 9ec34f8

25 files changed

+54
-55
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class ReifyQuotes extends MacroTransform {
7777
case tree: RefTree if !Inliner.inInlineMethod =>
7878
assert(!tree.symbol.isQuote)
7979
assert(!tree.symbol.isExprSplice)
80-
assert(!tree.symbol.isTypeSplice)
8180
case _ : TypeDef =>
8281
assert(!tree.symbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot),
8382
s"${tree.symbol} should have been removed by PickledQuotes because it has a @quoteTypeTag")
@@ -210,16 +209,15 @@ class ReifyQuotes extends MacroTransform {
210209
* are in the body of an inline method.
211210
*/
212211
protected def transformSpliceType(body: Tree, splice: Select)(using Context): Tree =
213-
if (level > 1) {
212+
if level > 1 then
214213
val body1 = nested(isQuote = false).transform(body)(using spliceContext)
215214
cpy.Select(splice)(body1, splice.name)
216-
}
217-
else {
218-
assert(level == 1, "unexpected top splice outside quote")
215+
else if level == 1 then
219216
val (body1, quotes) = nested(isQuote = false).splitSplice(body)(using spliceContext)
220217
val tpe = outer.embedded.getHoleType(body, splice)
221218
makeHole(splice.isTerm, body1, quotes, tpe).withSpan(splice.span)
222-
}
219+
else
220+
splice
223221

224222
/** Transforms the contents of a nested splice
225223
* Assuming

tests/neg-macros/i6997.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import scala.quoted._
33
class Foo {
44
def mcrImpl(body: Expr[Any])(using t: Type[_ <: Any])(using ctx: QuoteContext): Expr[Any] = '{
5-
val tmp = ???.asInstanceOf[$t] // error // error
5+
val tmp = ???.asInstanceOf[t.Underlying] // error // error
66
tmp
77
}
88
}

tests/neg-macros/i7048e.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ abstract class Test {
1616
import t.given
1717
println(summon[Type[t.T]].show)
1818
// val r = '{Option.empty[t.T]} // access to value t from wrong staging level
19-
val r2 = '{Option.empty[${t.T}]} // works
19+
val r2 = '{Option.empty[t.T.Underlying]} // works
2020
}
2121

2222
{
23-
val r1 = '{Option.empty[${T}]} // works
24-
val r2 = '{Option.empty[List[${T}]]} // works
25-
val r3 = '{summon[Type[${T}]]} // error: is not stable
26-
val r4 = '{summon[${T} <:< Any]} // error: is not stable
23+
val r1 = '{Option.empty[T.Underlying]} // works
24+
val r2 = '{Option.empty[List[T.Underlying]]} // works
25+
val r3 = '{summon[Type[T.Underlying]]} // error: is not stable
26+
val r4 = '{summon[T.Underlying <:< Any]} // error: is not stable
2727
}
2828

2929
{
30-
val s = '{Option.empty[${T}]} // works
30+
val s = '{Option.empty[T.Underlying]} // works
3131
val r = '{identity($s)} // works
3232
val r2 = '{identity(${s: Expr[Option[T]]})} // error // error : is not stable
3333
}

tests/pos-macros/i4023c/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import scala.quoted._
22
object Macro {
33
inline def ff[T](x: T): T = ${ impl('x) }
4-
def impl[T](x: Expr[T])(implicit t: Type[T], qctx: QuoteContext): Expr[T] = '{ $x: $t }
4+
def impl[T](x: Expr[T])(implicit t: Type[T], qctx: QuoteContext): Expr[T] = '{ $x: T }
55
}

tests/pos-macros/i4774a.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import scala.quoted._
33

44
object Test {
55
def loop[T](x: Expr[T])(implicit t: Type[T], qctx: QuoteContext): Expr[T] = '{
6-
val y: $t = $x
6+
val y: T = $x
77
${loop('y)}
88
}
99
}

tests/neg-macros/i4774b.scala renamed to tests/pos-macros/i4774b.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import scala.quoted._
33

44
object Test {
55
def loop[T](x: Expr[T])(implicit t: Type[T], qctx: QuoteContext): Expr[T] = '{
6-
val y: $t = $x;
7-
${loop[$t]( // error
6+
val y: t.Underlying = $x;
7+
${loop[t.Underlying](
88
'y
99
)}
1010
}

tests/pos-macros/i6210/Macros_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ object Macro {
77
def impl[A : Type, B : Type](using QuoteContext): Expr[Any] = {
88
val t = Type[Map[A, B]]
99
'{
10-
new Object().asInstanceOf[$t]
11-
???.asInstanceOf[$t]
10+
new Object().asInstanceOf[t.Underlying]
11+
???.asInstanceOf[t.Underlying]
1212
}
1313
}
1414
}

tests/pos-macros/i7048e.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ abstract class Test {
1616
import t.given
1717
println(summon[Type[t.T]].show)
1818
// val r = '{Option.empty[t.T]} // access to value t from wrong staging level
19-
val r2 = '{Option.empty[${t.T}]}
19+
val r2 = '{Option.empty[t.T.Underlying]}
2020
}
2121

2222
{
23-
val r1 = '{Option.empty[${T}]} // works
24-
val r2 = '{Option.empty[List[${T}]]} // works
25-
// val r3 = '{summon[Type[${T}]]} // access to Test.this from wrong staging level
26-
val r4 = '{summon[${T} <:< Any]}
23+
val r1 = '{Option.empty[T.Underlying]} // works
24+
val r2 = '{Option.empty[List[T.Underlying]]} // works
25+
// val r3 = '{summon[Type[T.Underlying]]} // access to Test.this from wrong staging level
26+
val r4 = '{summon[T.Underlying <:< Any]}
2727
}
2828

2929
{
30-
val s = '{Option.empty[${T}]}
30+
val s = '{Option.empty[T.Underlying]}
3131
val r = '{identity($s)} // works
3232
val r2 = '{identity(${s: Expr[Option[T]]})}
3333
}

tests/pos-macros/i7405.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Foo {
66
val x: X = ???
77
${
88
val t: Type[X] = Type[X] // Level 0
9-
'{ val y: $t = x }
9+
'{ val y: t.Underlying = x }
1010
}
1111
}
1212
}

tests/pos-macros/i7405b.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class Foo {
1111
type Z = x.Y
1212
${
1313
val t: Type[Z] = Type[Z]
14-
'{ val y: $t = x.y }
14+
'{ val y: Z = x.y }
15+
'{ val y: t.Underlying = x.y }
1516
}
1617
}
1718
}

tests/pos-macros/i7887.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
def typed[A](using t: quoted.Type[A], qctx: quoted.QuoteContext): Unit = {
22
import qctx.reflect._
33
'{
4-
type T = $t
4+
type T = A
55
${'{???}.cast[T]}
66
}
77
}

tests/pos-macros/i9020-b/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ object Show {
88
import quoted._
99
def impl[T](using ctx: QuoteContext, tpe: Type[T]): Expr[Show[T]] =
1010
'{
11-
new Show[$tpe] {
12-
def show(t: $tpe): String = "TODO"
11+
new Show[tpe.Underlying] {
12+
def show(t: tpe.Underlying): String = "TODO"
1313
}
1414
}
1515
}

tests/pos-macros/quote-1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import scala.quoted._
33
class Test(using QuoteContext) {
44

55
def f[T](x: Expr[T])(implicit t: Type[T]) = '{
6-
val y: $t = $x
6+
val y: T = $x
77
val z = $x
88
}
99

tests/run-macros/i7887/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def myMacroImpl(a: quoted.Expr[_])(using qctx: quoted.QuoteContext) = {
33
def typed[A] = {
44
implicit val t: quoted.Type[A] = a.unseal.tpe.widen.seal.asInstanceOf[quoted.Type[A]]
55
'{
6-
type T = $t
6+
type T = A
77
${a.unseal.seal.cast[T]}
88
}
99
}

tests/run-macros/refined-selectable-macro/Macro_2.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object Macro2 {
1414
object Record extends SelectableRecordCompanion[Record] {
1515
import scala.quoted._
1616

17-
inline def apply[R <: Record](elems: (String, Any)*) : R = ${ applyImpl('elems, Type[R]) }
17+
inline def apply[R <: Record](elems: (String, Any)*) : R = ${ applyImpl[R]('elems) }
1818

19-
def applyImpl[R <: Record: Type](elems: Expr[Seq[(String, Any)]], ev: Type[R])(using qctx: QuoteContext) = {
20-
'{ new Record($elems:_*).asInstanceOf[$ev] }
19+
def applyImpl[R <: Record: Type](elems: Expr[Seq[(String, Any)]])(using qctx: QuoteContext) = {
20+
'{ new Record($elems:_*).asInstanceOf[R] }
2121
}
2222

2323
def fromUntypedTuple(elems: (String, Any)*): Record = Record(elems: _*)

tests/run-staging/i3823-b.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ object Test {
44
given Toolbox = Toolbox.make(getClass.getClassLoader)
55
def main(args: Array[String]): Unit = withQuoteContext {
66
def f[T](x: Expr[T])(implicit t: Type[T]) = '{
7-
val z: $t = $x
7+
val z: t.Underlying = $x
88
}
99
println(f('{2})(Type[Int]).show)
1010
}

tests/run-staging/i3823.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import scala.quoted.staging._
33
object Test {
44
given Toolbox = Toolbox.make(getClass.getClassLoader)
55
def main(args: Array[String]): Unit = withQuoteContext {
6-
def f[T: Type](x: Expr[T])(t: Type[T]) = '{
7-
val z: $t = $x
6+
def f[T](x: Expr[T])(using t: Type[T]) = '{
7+
val z: t.Underlying = $x
88
}
9-
println(f('{2})(Type[Int]).show)
9+
println(f('{2})(using Type[Int]).show)
1010
}
1111
}

tests/run-staging/i3847-b.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ object Arrays {
66
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], qctx: QuoteContext): Liftable[Array[List[T]]] = {
77
new Liftable[Array[List[T]]] {
88
def toExpr(arr: Array[List[T]]) = '{
9-
new Array[List[$t]](${Expr(arr.length)})
9+
new Array[List[T]](${Expr(arr.length)})
1010
// TODO add elements
1111
}
1212
}

tests/run-staging/i3847.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ object Arrays {
66
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], ct: Expr[ClassTag[T]]): Liftable[Array[T]] = {
77
new Liftable[Array[T]] {
88
def toExpr(arr: Array[T]) = '{
9-
new Array[$t](${Expr(arr.length)})($ct)
9+
new Array[t.Underlying](${Expr(arr.length)})($ct)
1010
// TODO add elements
1111
}
1212
}

tests/run-staging/i4044e.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Foo {
77
val e: Expr[Int] = '{3}
88
val f: Expr[Int] = '{5}
99
val t: Type[Int] = Type[Int]
10-
val q = '{ ${ '{ ($e + $f).asInstanceOf[$t] } } }
10+
val q = '{ ${ '{ ($e + $f).asInstanceOf[t.Underlying] } } }
1111
println(q.show)
1212
}
1313
}

tests/run-staging/i5247.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ object Test {
99
}
1010
def foo[H : Type](using QuoteContext): Expr[H] = {
1111
val t = Type[H]
12-
'{ null.asInstanceOf[$t] }
12+
'{ null.asInstanceOf[t.Underlying] }
1313
}
1414
def bar[H : Type](using QuoteContext): Expr[List[H]] = {
1515
val t = Type[List[H]]
16-
'{ null.asInstanceOf[$t] }
16+
'{ null.asInstanceOf[t.Underlying] }
1717
}
1818
}

tests/run-staging/quote-lib.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ package liftable {
127127
}
128128

129129
object Lets {
130-
def letVal[T, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(implicit t: Type[T], qctx: QuoteContext): Expr[U] =
131-
'{ val letVal: $t = $expr; ${ body('letVal) } }
132-
def letLazyVal[T, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(implicit t: Type[T], qctx: QuoteContext): Expr[U] =
133-
'{ lazy val letLazyVal: $t = $expr; ${ body('letLazyVal) } }
134-
def letDef[T, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(implicit t: Type[T], qctx: QuoteContext): Expr[U] =
135-
'{ def letDef: $t = $expr; ${ body('letDef) } }
130+
def letVal[T: Type, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(implicit qctx: QuoteContext): Expr[U] =
131+
'{ val letVal: T = $expr; ${ body('letVal) } }
132+
def letLazyVal[T: Type, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(implicit qctx: QuoteContext): Expr[U] =
133+
'{ lazy val letLazyVal: T = $expr; ${ body('letLazyVal) } }
134+
def letDef[T: Type, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(implicit qctx: QuoteContext): Expr[U] =
135+
'{ def letDef: T = $expr; ${ body('letDef) } }
136136
}
137137

138138
object Loops {
@@ -145,7 +145,7 @@ package liftable {
145145

146146
implicit class LiftedOps[T: Liftable](list: Expr[List[T]])(implicit t: Type[T]) {
147147
def foldLeft[U](acc: Expr[U])(f: Expr[(U, T) => U])(implicit u: Type[U], qctx: QuoteContext): Expr[U] =
148-
'{ ($list).foldLeft[$u]($acc)($f) }
148+
'{ ($list).foldLeft[U]($acc)($f) }
149149
def foreach(f: Expr[T => Unit])(using QuoteContext): Expr[Unit] =
150150
'{ ($list).foreach($f) }
151151
}

tests/run-staging/quote-owners-2.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ object Test {
1212

1313
def f(t: Type[List[Int]])(using QuoteContext): Expr[Int] = '{
1414
def ff: Int = {
15-
val a: $t = {
16-
type T = $t
15+
val a: t.Underlying = {
16+
type T = t.Underlying
1717
val b: T = 3 :: Nil
1818
b
1919
}

tests/run-staging/quote-type-tags.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object Test {
55
given Toolbox = Toolbox.make(getClass.getClassLoader)
66
def main(args: Array[String]): Unit = run {
77
def asof[T: Type, U](x: Expr[T], t: Type[U]): Expr[U] =
8-
'{$x.asInstanceOf[$t]}
8+
'{$x.asInstanceOf[t.Underlying]}
99

1010
println(asof('{}, Type[Unit]).show)
1111
println(asof('{true}, Type[Boolean]).show)

tests/run-staging/quote-unrolled-foreach.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object Test {
5757
val size = ($arrRef).length
5858
var i = 0
5959
while (i < size) {
60-
val element: $t = ($arrRef)(i)
60+
val element: T = ($arrRef)(i)
6161
($f)(element)
6262
i += 1
6363
}

0 commit comments

Comments
 (0)