Skip to content

Commit 8d6d8ac

Browse files
author
Aggelos Biboudis
authored
Merge pull request #4363 from dotty-staging/fix-#4350
Fix #4350: Do not check prefix of class type parameters
2 parents e515bd0 + 78c41fe commit 8d6d8ac

File tree

7 files changed

+43
-6
lines changed

7 files changed

+43
-6
lines changed

compiler/src/dotty/tools/dotc/quoted/TreeCleaner.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ class TreeCleaner extends tpd.TreeMap {
1717
/** List of symbols and their types for type aliases `type T = U` */
1818
private[this] var aliasesSyms: List[Symbol] = Nil
1919
private[this] var aliasesTypes: List[Type] = Nil
20+
private[this] val aliases = newMutableSymbolMap[Tree]
2021

2122
override def transform(tree: Tree)(implicit ctx: Context): Tree = {
2223
val tree0 = tree match {
2324
case TypeDef(_, TypeBoundsTree(lo, hi)) if lo == hi =>
2425
aliasesSyms = tree.symbol :: aliasesSyms
2526
aliasesTypes = lo.tpe :: aliasesTypes
27+
aliases(tree.symbol) = ref(lo.tpe.typeSymbol)
2628
Literal(Constant(()))
2729
case _ => tree
2830
}
@@ -40,6 +42,7 @@ class TreeCleaner extends tpd.TreeMap {
4042
case expr3 => Block(flatStats, expr3)
4143
}
4244
case tree1: TypeTree => TypeTree(tree1.tpe.subst(aliasesSyms, aliasesTypes))
45+
case tree1: Ident => aliases.get(tree1.symbol).getOrElse(tree1)
4346
case tree1 => tree1
4447
}
4548
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
207207
explicitTags.clear()
208208

209209
// Maps type splices to type references of tags e.g., ~t -> some type T$1
210-
val map: Map[Type, Type] = tagsExplicitTypeDefsPairs.map(x => (x._1, x._2.symbol.typeRef)).toMap
210+
val map: Map[Type, Type] = {
211+
tagsExplicitTypeDefsPairs.map(x => (x._1, x._2.symbol.typeRef)) ++
212+
(itags.map(_._1) zip typeDefs.map(_.symbol.typeRef))
213+
}.toMap
211214
val tMap = new TypeMap() {
212215
override def apply(tp: Type): Type = map.getOrElse(tp, mapOver(tp))
213216
}
@@ -242,7 +245,7 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
242245
l == level ||
243246
sym.is(Inline) && sym.owner.is(Macro) && sym.info.isValueType && l - 1 == level
244247
case None =>
245-
true
248+
level == 0
246249
}
247250

248251
/** Issue a "splice outside quote" error unless we ar in the body of an inline method */
@@ -286,12 +289,12 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
286289
if (!isThis) sym.show
287290
else if (sym.is(ModuleClass)) sym.sourceModule.show
288291
else i"${sym.name}.this"
289-
if (!isThis && sym.maybeOwner.isType)
292+
if (!isThis && sym.maybeOwner.isType && !sym.is(Param))
290293
check(sym.owner, sym.owner.thisType, pos)
291294
else if (sym.exists && !sym.isStaticOwner && !levelOK(sym))
292295
for (errMsg <- tryHeal(tp, pos))
293296
ctx.error(em"""access to $symStr from wrong staging level:
294-
| - the definition is at level ${levelOf(sym)},
297+
| - the definition is at level ${levelOf.getOrElse(sym, 0)},
295298
| - but the access is at level $level.$errMsg""", pos)
296299
}
297300

@@ -310,7 +313,8 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
310313
}
311314
case tp: NamedType =>
312315
check(tp.symbol, tp, pos)
313-
foldOver(acc, tp)
316+
if (!tp.symbol.is(Param))
317+
foldOver(acc, tp)
314318
case tp: ThisType =>
315319
check(tp.cls, tp, pos)
316320
foldOver(acc, tp)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ trait Implicits { self: Typer =>
571571
}
572572

573573
def synthesizedTypeTag(formal: Type): Tree = formal.argInfos match {
574-
case arg :: Nil =>
574+
case arg :: Nil if !arg.typeSymbol.is(Param) =>
575575
object bindFreeVars extends TypeMap {
576576
var ok = true
577577
def apply(t: Type) = t match {

tests/neg/i4350.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import scala.quoted.Type
2+
3+
class Foo[T] {
4+
'(null.asInstanceOf[T]) // error
5+
}

tests/pos/i4350.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import scala.quoted.Type
2+
3+
class Foo[T: Type] {
4+
'(null.asInstanceOf[T])
5+
}

tests/run-with-compiler/i4350.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
null.asInstanceOf[Object]
3+
}
4+
{
5+
null.asInstanceOf[String]
6+
}

tests/run-with-compiler/i4350.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import dotty.tools.dotc.quoted.Toolbox._
2+
3+
import scala.quoted.Type
4+
5+
class Foo[T: Type] {
6+
def q = '(null.asInstanceOf[T])
7+
}
8+
9+
object Test {
10+
def main(args: Array[String]): Unit = {
11+
println((new Foo[Object]).q.show)
12+
println((new Foo[String]).q.show)
13+
}
14+
}

0 commit comments

Comments
 (0)