Skip to content

Commit c041e2b

Browse files
committed
Fix scala#6997: Adapt quote type direct aliase to type splice
1 parent de43ee1 commit c041e2b

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
112112
if (tp.isTerm)
113113
ctx.error(i"splice outside quotes", pos)
114114
tp
115+
case tp: TypeRef if tp.prefix.derivesFrom(defn.QuotedTypeClass) && tp.name.toString == "T" =>
116+
// Adapt direct references to the type of the type parameter T of a quoted.Type[T].
117+
// Replace it with a properly encoded type splice. This is the normal for expected for type splices.
118+
tp.prefix.select(tpnme.splice)
115119
case tp: NamedType =>
116120
checkSymLevel(tp.symbol, tp, pos) match {
117121
case Some(tpRef) => tpRef.tpe

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class Staging extends MacroTransform {
6363
}
6464

6565
tree.tpe match {
66+
case tp: TypeRef if tp.prefix.derivesFrom(defn.QuotedTypeClass) =>
67+
assert(tp.name.toString != "T", "unexpected reference t.T from t: quoted.Type[T]")
6668
case tpe @ TypeRef(prefix, _) if tpe.typeSymbol eq defn.QuotedType_splice =>
6769
// Type splices must have a know term ref, usually to an implicit argument
6870
// This is mostly intended to catch `quoted.Type[T]#splice` types which should just be `T`

tests/pos/i6997.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
import scala.quoted._
3+
class Foo {
4+
def mcrImpl(body: Expr[Any]) given (t: Type[_ <: Any]) given (ctx: QuoteContext): Expr[Any] = '{
5+
val tmp = ???.asInstanceOf[$t]
6+
tmp
7+
}
8+
}

tests/pos/i6997b.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package playground
2+
3+
import scala.quoted._, scala.quoted.matching._
4+
import delegate scala.quoted._
5+
6+
inline def mcr(x: => Any): Any = ${mcrImpl('x)}
7+
8+
def mcrImpl(body: Expr[Any]) given (ctx: QuoteContext): Expr[Any] = {
9+
val '{$x: $t} = body
10+
'{
11+
val tmp: $t = $x.asInstanceOf[$t]
12+
println(tmp)
13+
tmp
14+
}
15+
}

0 commit comments

Comments
 (0)