Skip to content

Commit 3dd4c6d

Browse files
committed
Fix #15883: Interpret inner static object access as this access
1 parent 2eb7b72 commit 3dd4c6d

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

compiler/src/dotty/tools/dotc/transform/init/Semantic.scala

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ object Semantic:
156156
def hasField(f: Symbol) = fields.contains(f)
157157

158158
object Promoted:
159-
class PromotionInfo:
159+
class PromotionInfo(val entryClass: ClassSymbol):
160160
var isCurrentObjectPromoted: Boolean = false
161161
val values = mutable.Set.empty[Value]
162162
override def toString(): String = values.toString()
@@ -165,14 +165,15 @@ object Semantic:
165165
opaque type Promoted = PromotionInfo
166166

167167
/** Note: don't use `val` to avoid incorrect sharing */
168-
def empty: Promoted = new PromotionInfo
168+
def empty(entryClass: ClassSymbol): Promoted = new PromotionInfo(entryClass)
169169

170170
extension (promoted: Promoted)
171171
def isCurrentObjectPromoted: Boolean = promoted.isCurrentObjectPromoted
172172
def promoteCurrent(thisRef: ThisRef): Unit = promoted.isCurrentObjectPromoted = true
173173
def contains(value: Value): Boolean = promoted.values.contains(value)
174174
def add(value: Value): Unit = promoted.values += value
175175
def remove(value: Value): Unit = promoted.values -= value
176+
def entryClass: ClassSymbol = promoted.entryClass
176177
end extension
177178
end Promoted
178179
type Promoted = Promoted.Promoted
@@ -658,7 +659,7 @@ object Semantic:
658659

659660
def select(field: Symbol, receiver: Type, needResolve: Boolean = true): Contextual[Value] = log("select " + field.show + ", this = " + value, printer, (_: Value).show) {
660661
if promoted.isCurrentObjectPromoted then Hot
661-
else value match {
662+
else value match
662663
case Hot =>
663664
Hot
664665

@@ -710,7 +711,6 @@ object Semantic:
710711

711712
case RefSet(refs) =>
712713
refs.map(_.select(field, receiver)).join
713-
}
714714
}
715715

716716
def call(meth: Symbol, args: List[ArgInfo], receiver: Type, superType: Type, needResolve: Boolean = true): Contextual[Value] = log("call " + meth.show + ", args = " + args.map(_.value.show), printer, (_: Value).show) {
@@ -1230,7 +1230,7 @@ object Semantic:
12301230

12311231
@tailrec
12321232
def iterate(): Unit = {
1233-
given Promoted = Promoted.empty
1233+
given Promoted = Promoted.empty(thisRef.klass)
12341234
given Trace = Trace.empty.add(thisRef.klass.defTree)
12351235
given reporter: Reporter.BufferedReporter = new Reporter.BufferedReporter
12361236

@@ -1513,16 +1513,19 @@ object Semantic:
15131513
thisV.accessLocal(tmref, klass)
15141514

15151515
case tmref: TermRef =>
1516-
cases(tmref.prefix, thisV, klass).select(tmref.symbol, receiver = tmref.prefix)
1516+
val cls = tmref.widenSingleton.classSymbol.asClass
1517+
if cls.isStaticOwner && !cls.isContainedIn(promoted.entryClass) then
1518+
Hot
1519+
else
1520+
cases(tmref.prefix, thisV, klass).select(tmref.symbol, receiver = tmref.prefix)
15171521

15181522
case tp @ ThisType(tref) =>
15191523
val cls = tref.classSymbol.asClass
15201524
if cls.isStaticOwner && !klass.isContainedIn(cls) then
15211525
// O.this outside the body of the object O
15221526
Hot
15231527
else
1524-
val value = resolveThis(cls, thisV, klass)
1525-
value
1528+
resolveThis(cls, thisV, klass)
15261529

15271530
case _: TermParamRef | _: RecThis =>
15281531
// possible from checking effects of types

tests/init/neg/i15883.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val a = b
2+
val b = 1 // error

0 commit comments

Comments
 (0)