Skip to content

Commit 38d859b

Browse files
authored
Merge pull request #254 from scala/backport-lts-3.3-22618
Backport "Fix issue with static `this` references erroring in quoted code" to 3.3 LTS
2 parents 85da96e + c20c16d commit 38d859b

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -189,19 +189,27 @@ class CrossStageSafety extends TreeMapWithStages {
189189

190190
/** Check level consistency of terms references */
191191
private def checkLevelConsistency(tree: Ident | This)(using Context): Unit =
192+
def isStatic(pre: Type)(using Context): Boolean = pre match
193+
case pre: NamedType =>
194+
val sym = pre.currentSymbol
195+
sym.is(Package) || sym.isStaticOwner && isStatic(pre.prefix)
196+
case pre: ThisType => isStatic(pre.tref)
197+
case _ => true
192198
new TypeTraverser {
193199
def traverse(tp: Type): Unit =
194200
tp match
195201
case tp @ TermRef(NoPrefix, _) if !tp.symbol.isStatic && level != levelOf(tp.symbol) =>
196202
levelError(tp.symbol, tp, tree.srcPos)
203+
case tp: ThisType if isStatic(tp) =>
204+
// static object (OK)
197205
case tp: ThisType if level != -1 && level != levelOf(tp.cls) =>
198206
levelError(tp.cls, tp, tree.srcPos)
199207
case tp: AnnotatedType =>
200208
traverse(tp.parent)
201209
case _ if tp.typeSymbol.is(Package) =>
202210
// OK
203211
case _ =>
204-
traverseChildren(tp)
212+
traverseChildren(tp)
205213
}.traverse(tree.tpe)
206214

207215
private def levelError(sym: Symbol, tp: Type, pos: SrcPos)(using Context): tp.type = {

tests/neg/i22592.scala

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.quoted.*
2+
3+
trait Foo:
4+
def inherited = ()
5+
6+
class Bar extends Foo:
7+
def local = ()
8+
def localArg(arg: Any) = ()
9+
10+
def macro1(using Quotes): Expr[Unit] = '{ local } // error
11+
def macro3(using Quotes): Expr[Unit] = '{ inherited } // error
12+
def macro4(using Quotes): Expr[Unit] = '{ this.local } // error
13+
def macro5(using Quotes): Expr[Unit] = '{ this.inherited } // error
14+
def macro6(using Quotes): Expr[Unit] = '{ localArg(this) } // error // error

tests/pos/i22592.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted.*
2+
3+
trait Foo:
4+
def inherited = ()
5+
6+
object Bar extends Foo:
7+
def local = ()
8+
def localArg(arg: Any) = ()
9+
10+
def macro1(using Quotes): Expr[Unit] = '{ local }
11+
def macro2(using Quotes): Expr[Unit] = '{ Bar.inherited }
12+
def macro3(using Quotes): Expr[Unit] = '{ inherited }
13+
def macro4(using Quotes): Expr[Unit] = '{ this.local }
14+
def macro5(using Quotes): Expr[Unit] = '{ this.inherited }
15+
def macro6(using Quotes): Expr[Unit] = '{ localArg(this) }

0 commit comments

Comments
 (0)