Skip to content

Use canonical underlying type reference #10110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class ReifyQuotes extends MacroTransform {
case tree: RefTree if !Inliner.inInlineMethod =>
assert(!tree.symbol.isQuote)
assert(!tree.symbol.isExprSplice)
assert(!tree.symbol.isTypeSplice)
case _ : TypeDef =>
assert(!tree.symbol.hasAnnotation(defn.InternalQuoted_QuoteTypeTagAnnot),
s"${tree.symbol} should have been removed by PickledQuotes because it has a @quoteTypeTag")
Expand Down Expand Up @@ -210,16 +209,15 @@ class ReifyQuotes extends MacroTransform {
* are in the body of an inline method.
*/
protected def transformSpliceType(body: Tree, splice: Select)(using Context): Tree =
if (level > 1) {
if level > 1 then
val body1 = nested(isQuote = false).transform(body)(using spliceContext)
cpy.Select(splice)(body1, splice.name)
}
else {
assert(level == 1, "unexpected top splice outside quote")
else if level == 1 then
val (body1, quotes) = nested(isQuote = false).splitSplice(body)(using spliceContext)
val tpe = outer.embedded.getHoleType(body, splice)
makeHole(splice.isTerm, body1, quotes, tpe).withSpan(splice.span)
}
else
splice

/** Transforms the contents of a nested splice
* Assuming
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i6997.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import scala.quoted._
class Foo {
def mcrImpl(body: Expr[Any])(using t: Type[_ <: Any])(using ctx: QuoteContext): Expr[Any] = '{
val tmp = ???.asInstanceOf[$t] // error // error
val tmp = ???.asInstanceOf[t.Underlying] // error // error
tmp
}
}
12 changes: 6 additions & 6 deletions tests/neg-macros/i7048e.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ abstract class Test {
import t.given
println(summon[Type[t.T]].show)
// val r = '{Option.empty[t.T]} // access to value t from wrong staging level
val r2 = '{Option.empty[${t.T}]} // works
val r2 = '{Option.empty[t.T.Underlying]} // works
}

{
val r1 = '{Option.empty[${T}]} // works
val r2 = '{Option.empty[List[${T}]]} // works
val r3 = '{summon[Type[${T}]]} // error: is not stable
val r4 = '{summon[${T} <:< Any]} // error: is not stable
val r1 = '{Option.empty[T.Underlying]} // works
val r2 = '{Option.empty[List[T.Underlying]]} // works
val r3 = '{summon[Type[T.Underlying]]} // error: is not stable
val r4 = '{summon[T.Underlying <:< Any]} // error: is not stable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little surprising that List[T.Underlying] works but summon[Type[T.Underlying]]] is an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one was subtle.

[error] (Test.this.given_Type_T : => quoted.Type[Test.this.T]) is not a valid type witness, since it is not an immutable path

The issue is that given Type[T] = getT is not stable. This is where I usually write given getT.type = getT to not lose the path in the given.

The other cases happen to work because we only referred to T.Underlying and never needed to head some type. This is the expected behavior.

}

{
val s = '{Option.empty[${T}]} // works
val s = '{Option.empty[T.Underlying]} // works
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val s = '{Option.empty[T.Underlying]} // works
val s = '{Option.empty[T]} // works

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is equivalent but should not be changed to keep testing the same thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, the second commit found the bug because I kept it as T.Underlying.

val r = '{identity($s)} // works
val r2 = '{identity(${s: Expr[Option[T]]})} // error // error : is not stable
}
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i4023c/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import scala.quoted._
object Macro {
inline def ff[T](x: T): T = ${ impl('x) }
def impl[T](x: Expr[T])(implicit t: Type[T], qctx: QuoteContext): Expr[T] = '{ $x: $t }
def impl[T](x: Expr[T])(implicit t: Type[T], qctx: QuoteContext): Expr[T] = '{ $x: T }
}
2 changes: 1 addition & 1 deletion tests/pos-macros/i4774a.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import scala.quoted._

object Test {
def loop[T](x: Expr[T])(implicit t: Type[T], qctx: QuoteContext): Expr[T] = '{
val y: $t = $x
val y: T = $x
${loop('y)}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import scala.quoted._

object Test {
def loop[T](x: Expr[T])(implicit t: Type[T], qctx: QuoteContext): Expr[T] = '{
val y: $t = $x;
${loop[$t]( // error
val y: t.Underlying = $x;
${loop[t.Underlying](
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was actually crashing with an assertion failure at
8afec66#diff-b3069d0753da3a2c12847b53d12eaf5d5003af1d6e6438c84f1dd5f6ef6a668eL218.

This also happens on master.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not open an issue as the fix was straightforward.

'y
)}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/pos-macros/i6210/Macros_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ object Macro {
def impl[A : Type, B : Type](using QuoteContext): Expr[Any] = {
val t = Type[Map[A, B]]
'{
new Object().asInstanceOf[$t]
???.asInstanceOf[$t]
new Object().asInstanceOf[t.Underlying]
???.asInstanceOf[t.Underlying]
}
}
}
12 changes: 6 additions & 6 deletions tests/pos-macros/i7048e.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ abstract class Test {
import t.given
println(summon[Type[t.T]].show)
// val r = '{Option.empty[t.T]} // access to value t from wrong staging level
val r2 = '{Option.empty[${t.T}]}
val r2 = '{Option.empty[t.T.Underlying]}
}

{
val r1 = '{Option.empty[${T}]} // works
val r2 = '{Option.empty[List[${T}]]} // works
// val r3 = '{summon[Type[${T}]]} // access to Test.this from wrong staging level
val r4 = '{summon[${T} <:< Any]}
val r1 = '{Option.empty[T.Underlying]} // works
val r2 = '{Option.empty[List[T.Underlying]]} // works
// val r3 = '{summon[Type[T.Underlying]]} // access to Test.this from wrong staging level
val r4 = '{summon[T.Underlying <:< Any]}
}

{
val s = '{Option.empty[${T}]}
val s = '{Option.empty[T.Underlying]}
val r = '{identity($s)} // works
val r2 = '{identity(${s: Expr[Option[T]]})}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i7405.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Foo {
val x: X = ???
${
val t: Type[X] = Type[X] // Level 0
'{ val y: $t = x }
'{ val y: t.Underlying = x }
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/pos-macros/i7405b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Foo {
type Z = x.Y
${
val t: Type[Z] = Type[Z]
'{ val y: $t = x.y }
'{ val y: Z = x.y }
'{ val y: t.Underlying = x.y }
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/pos-macros/i7887.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def typed[A](using t: quoted.Type[A], qctx: quoted.QuoteContext): Unit = {
import qctx.reflect._
'{
type T = $t
type T = A
${'{???}.cast[T]}
}
}
4 changes: 2 additions & 2 deletions tests/pos-macros/i9020-b/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ object Show {
import quoted._
def impl[T](using ctx: QuoteContext, tpe: Type[T]): Expr[Show[T]] =
'{
new Show[$tpe] {
def show(t: $tpe): String = "TODO"
new Show[tpe.Underlying] {
def show(t: tpe.Underlying): String = "TODO"
}
}
}
2 changes: 1 addition & 1 deletion tests/pos-macros/quote-1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import scala.quoted._
class Test(using QuoteContext) {

def f[T](x: Expr[T])(implicit t: Type[T]) = '{
val y: $t = $x
val y: T = $x
val z = $x
}

Expand Down
2 changes: 1 addition & 1 deletion tests/run-macros/i7887/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def myMacroImpl(a: quoted.Expr[_])(using qctx: quoted.QuoteContext) = {
def typed[A] = {
implicit val t: quoted.Type[A] = a.unseal.tpe.widen.seal.asInstanceOf[quoted.Type[A]]
'{
type T = $t
type T = A
${a.unseal.seal.cast[T]}
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/run-macros/refined-selectable-macro/Macro_2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ object Macro2 {
object Record extends SelectableRecordCompanion[Record] {
import scala.quoted._

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

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

def fromUntypedTuple(elems: (String, Any)*): Record = Record(elems: _*)
Expand Down
2 changes: 1 addition & 1 deletion tests/run-staging/i3823-b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object Test {
given Toolbox = Toolbox.make(getClass.getClassLoader)
def main(args: Array[String]): Unit = withQuoteContext {
def f[T](x: Expr[T])(implicit t: Type[T]) = '{
val z: $t = $x
val z: t.Underlying = $x
}
println(f('{2})(Type[Int]).show)
}
Expand Down
6 changes: 3 additions & 3 deletions tests/run-staging/i3823.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import scala.quoted.staging._
object Test {
given Toolbox = Toolbox.make(getClass.getClassLoader)
def main(args: Array[String]): Unit = withQuoteContext {
def f[T: Type](x: Expr[T])(t: Type[T]) = '{
val z: $t = $x
def f[T](x: Expr[T])(using t: Type[T]) = '{
val z: t.Underlying = $x
}
println(f('{2})(Type[Int]).show)
println(f('{2})(using Type[Int]).show)
}
}
2 changes: 1 addition & 1 deletion tests/run-staging/i3847-b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Arrays {
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], qctx: QuoteContext): Liftable[Array[List[T]]] = {
new Liftable[Array[List[T]]] {
def toExpr(arr: Array[List[T]]) = '{
new Array[List[$t]](${Expr(arr.length)})
new Array[List[T]](${Expr(arr.length)})
// TODO add elements
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/run-staging/i3847.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Arrays {
implicit def ArrayIsLiftable[T: Liftable](implicit t: Type[T], ct: Expr[ClassTag[T]]): Liftable[Array[T]] = {
new Liftable[Array[T]] {
def toExpr(arr: Array[T]) = '{
new Array[$t](${Expr(arr.length)})($ct)
new Array[t.Underlying](${Expr(arr.length)})($ct)
// TODO add elements
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/run-staging/i4044e.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Foo {
val e: Expr[Int] = '{3}
val f: Expr[Int] = '{5}
val t: Type[Int] = Type[Int]
val q = '{ ${ '{ ($e + $f).asInstanceOf[$t] } } }
val q = '{ ${ '{ ($e + $f).asInstanceOf[t.Underlying] } } }
println(q.show)
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/run-staging/i5247.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ object Test {
}
def foo[H : Type](using QuoteContext): Expr[H] = {
val t = Type[H]
'{ null.asInstanceOf[$t] }
'{ null.asInstanceOf[t.Underlying] }
}
def bar[H : Type](using QuoteContext): Expr[List[H]] = {
val t = Type[List[H]]
'{ null.asInstanceOf[$t] }
'{ null.asInstanceOf[t.Underlying] }
}
}
14 changes: 7 additions & 7 deletions tests/run-staging/quote-lib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ package liftable {
}

object Lets {
def letVal[T, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(implicit t: Type[T], qctx: QuoteContext): Expr[U] =
'{ val letVal: $t = $expr; ${ body('letVal) } }
def letLazyVal[T, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(implicit t: Type[T], qctx: QuoteContext): Expr[U] =
'{ lazy val letLazyVal: $t = $expr; ${ body('letLazyVal) } }
def letDef[T, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(implicit t: Type[T], qctx: QuoteContext): Expr[U] =
'{ def letDef: $t = $expr; ${ body('letDef) } }
def letVal[T: Type, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(implicit qctx: QuoteContext): Expr[U] =
'{ val letVal: T = $expr; ${ body('letVal) } }
def letLazyVal[T: Type, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(implicit qctx: QuoteContext): Expr[U] =
'{ lazy val letLazyVal: T = $expr; ${ body('letLazyVal) } }
def letDef[T: Type, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(implicit qctx: QuoteContext): Expr[U] =
'{ def letDef: T = $expr; ${ body('letDef) } }
}

object Loops {
Expand All @@ -145,7 +145,7 @@ package liftable {

implicit class LiftedOps[T: Liftable](list: Expr[List[T]])(implicit t: Type[T]) {
def foldLeft[U](acc: Expr[U])(f: Expr[(U, T) => U])(implicit u: Type[U], qctx: QuoteContext): Expr[U] =
'{ ($list).foldLeft[$u]($acc)($f) }
'{ ($list).foldLeft[U]($acc)($f) }
def foreach(f: Expr[T => Unit])(using QuoteContext): Expr[Unit] =
'{ ($list).foreach($f) }
}
Expand Down
4 changes: 2 additions & 2 deletions tests/run-staging/quote-owners-2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ object Test {

def f(t: Type[List[Int]])(using QuoteContext): Expr[Int] = '{
def ff: Int = {
val a: $t = {
type T = $t
val a: t.Underlying = {
type T = t.Underlying
val b: T = 3 :: Nil
b
}
Expand Down
2 changes: 1 addition & 1 deletion tests/run-staging/quote-type-tags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Test {
given Toolbox = Toolbox.make(getClass.getClassLoader)
def main(args: Array[String]): Unit = run {
def asof[T: Type, U](x: Expr[T], t: Type[U]): Expr[U] =
'{$x.asInstanceOf[$t]}
'{$x.asInstanceOf[t.Underlying]}

println(asof('{}, Type[Unit]).show)
println(asof('{true}, Type[Boolean]).show)
Expand Down
2 changes: 1 addition & 1 deletion tests/run-staging/quote-unrolled-foreach.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object Test {
val size = ($arrRef).length
var i = 0
while (i < size) {
val element: $t = ($arrRef)(i)
val element: T = ($arrRef)(i)
($f)(element)
i += 1
}
Expand Down