Skip to content

Commit

Permalink
Disallow ill-staged references to local classes
Browse files Browse the repository at this point in the history
Fixes #19856
  • Loading branch information
nicolasstucki committed Mar 4, 2024
1 parent 02cc983 commit 3479c31
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/staging/HealType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ class HealType(pos: SrcPos)(using Context) extends TypeMap {
checkNotWildcardSplice(tp)
if level == 0 then tp else getTagRef(prefix)
case _: TermRef | _: ThisType | NoPrefix =>
if levelInconsistentRootOfPath(tp).exists then
val inconsistentRoot = levelInconsistentRootOfPath(tp)
if inconsistentRoot.isClass && inconsistentRoot.isLocal then
levelError(inconsistentRoot, tp, pos)
else if inconsistentRoot.exists then
tryHeal(tp)
else
tp
Expand Down
9 changes: 9 additions & 0 deletions tests/neg-macros/i19856.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.quoted.*

def test(using Quotes): Any =
class Foo {
class IdxWrapper
def foo(using Type[IdxWrapper]): Expr[Any] =
'{ new IdxWrapper } // error
}
()
34 changes: 34 additions & 0 deletions tests/neg-macros/i19856b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import scala.deriving.Mirror
import scala.quoted.{Expr, Quotes, Type}

trait macroTest[A] {
type ElemTop <: A
type Index[_]

case class IdxWrapper[X](idx: Index[X])

def indexOf[X <: ElemTop: Type](x: Expr[X]): Expr[Index[X]]

def indexOfA(a: Expr[A]): Expr[IdxWrapper[_ <: ElemTop]]
}
object macroTest {

def derivedImpl[A: Type, ElemTypes <: Tuple: Type, Label <: String: Type, Labels <: Tuple: Type](
using m: Expr[
Mirror.SumOf[A] {
type MirroredElemTypes = ElemTypes
type MirroredLabel = Label
type MirroredElemLabels = Labels
}
],
q: Quotes,
): macroTest[A] = new macroTest[A]:
override type Index[_] = Int

override def indexOf[X <: ElemTop: Type](x: Expr[X]): Expr[Index[X]] = '{ $m.ordinal($x) }

override def indexOfA(a: Expr[A]): Expr[IdxWrapper[_ <: ElemTop]] =
given Type[IdxWrapper] = Type.of[IdxWrapper] // error
given Type[ElemTop] = Type.of[ElemTop] // error
'{ new IdxWrapper(${ indexOf(a.asInstanceOf[Expr[ElemTop]]) }) } // error // error
}

0 comments on commit 3479c31

Please sign in to comment.