Skip to content

Commit 7dc8d5e

Browse files
Backport "Disallow ill-staged references to local classes" to LTS (#20985)
Backports #19869 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents f703838 + a60055b commit 7dc8d5e

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

compiler/src/dotty/tools/dotc/staging/HealType.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ class HealType(pos: SrcPos)(using Context) extends TypeMap {
4747
checkNotWildcardSplice(tp)
4848
if level == 0 then tp else getTagRef(prefix)
4949
case _: TermRef | _: ThisType | NoPrefix =>
50-
if levelInconsistentRootOfPath(tp).exists then
50+
val inconsistentRoot = levelInconsistentRootOfPath(tp)
51+
if inconsistentRoot.isClass && inconsistentRoot.isLocal then
52+
levelError(inconsistentRoot, tp, pos)
53+
else if inconsistentRoot.exists then
5154
tryHeal(tp)
5255
else
5356
tp

tests/neg-macros/i19856.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.quoted.*
2+
3+
def test(using Quotes): Any =
4+
class Foo {
5+
class IdxWrapper
6+
def foo(using Type[IdxWrapper]): Expr[Any] =
7+
'{ new IdxWrapper } // error
8+
}
9+
()

tests/neg-macros/i19856b.scala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import scala.deriving.Mirror
2+
import scala.quoted.{Expr, Quotes, Type}
3+
4+
trait macroTest[A] {
5+
type ElemTop <: A
6+
type Index[_]
7+
8+
case class IdxWrapper[X](idx: Index[X])
9+
10+
def indexOf[X <: ElemTop: Type](x: Expr[X]): Expr[Index[X]]
11+
12+
def indexOfA(a: Expr[A]): Expr[IdxWrapper[_ <: ElemTop]]
13+
}
14+
object macroTest {
15+
16+
def derivedImpl[A: Type, ElemTypes <: Tuple: Type, Label <: String: Type, Labels <: Tuple: Type](
17+
using m: Expr[
18+
Mirror.SumOf[A] {
19+
type MirroredElemTypes = ElemTypes
20+
type MirroredLabel = Label
21+
type MirroredElemLabels = Labels
22+
}
23+
],
24+
q: Quotes,
25+
): macroTest[A] = new macroTest[A]:
26+
override type Index[_] = Int
27+
28+
override def indexOf[X <: ElemTop: Type](x: Expr[X]): Expr[Index[X]] = '{ $m.ordinal($x) }
29+
30+
override def indexOfA(a: Expr[A]): Expr[IdxWrapper[_ <: ElemTop]] =
31+
given Type[IdxWrapper] = Type.of[IdxWrapper] // error
32+
given Type[ElemTop] = Type.of[ElemTop] // error
33+
'{ new IdxWrapper(${ indexOf(a.asInstanceOf[Expr[ElemTop]]) }) } // error // error
34+
}

0 commit comments

Comments
 (0)