Skip to content
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

Disallow ill-staged references to local classes #19869

Merged
merged 1 commit into from
Mar 11, 2024
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
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
}
Loading