Skip to content

Commit b02d898

Browse files
authored
Change isStatic to isStaticOwner in hasLocalInstantiation (#19803)
2 parents de6a090 + a6dd92f commit b02d898

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

Diff for: compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ object ExplicitOuter {
227227
private def hasLocalInstantiation(cls: ClassSymbol)(using Context): Boolean =
228228
// Modules are normally locally instantiated, except if they are declared in a trait,
229229
// in which case they will be instantiated in the classes that mix in the trait.
230-
cls.owner.ownersIterator.takeWhile(!_.isStatic).exists(_.isTerm)
230+
cls.owner.ownersIterator.takeWhile(!_.isStaticOwner).exists(_.isTerm)
231231
|| cls.is(Private, butNot = Module)
232232
|| cls.is(Module) && !cls.owner.is(Trait)
233233

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
List(public T1$C1())
2+
List(public T2$$anon$1$C2())
3+
List(public T3$C3$1())
4+
List(public T4$$anon$2$C4())
5+
List(public T5$C5(T5))
6+
List(public T6$$anon$3$C6())
7+
List(public T7$C7$1())
8+
List(public T8$$anon$4$C8())
9+
List(public T9$C9$1(T9))
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// This test checks that references to outer classes in inner classes are
2+
// eliminated in some cases when they are not used. This is done in the
3+
// ExplicitOuter phase. See issue #19569 for discussions.
4+
5+
object helper {
6+
def test(cls: Class[?]) = println(cls.getDeclaredConstructors.toList)
7+
}
8+
9+
import helper.test
10+
11+
object T1 { class C1; test(classOf[C1]) }
12+
object T2 { new AnyRef { class C2; test(classOf[C2]) } }
13+
object T3 { def t3(): Unit = { class C3; test(classOf[C3]) } }
14+
object T4 { def t4(): Unit = new AnyRef { class C4; test(classOf[C4]) } }
15+
16+
// The outer reference in C5 is not eliminated because C5 is publicly
17+
// accessible as a member of T5. Therefore, its constructor needs to conform
18+
// to the expected signature, with the outer reference parameter.
19+
class T5 { class C5; test(classOf[C5]) }
20+
21+
class T6 { new AnyRef { class C6; test(classOf[C6]) } }
22+
class T7 { def t7(): Unit = { class C7; test(classOf[C7]) } }
23+
class T8 { def t8(): Unit = new AnyRef { class C8; test(classOf[C8]) } }
24+
25+
// Here, the outer reference in C9 is not eliminated because C9 needs to access
26+
// the field x.
27+
class T9 { var x = 451; def t9(): Unit = { class C9 {def getX = x}; test(classOf[C9])} }
28+
29+
object Test {
30+
def main(args: Array[String]): Unit = {
31+
T1
32+
T2
33+
T3.t3()
34+
T4.t4()
35+
new T5()
36+
new T6()
37+
new T7().t7()
38+
new T8().t8()
39+
new T9().t9()
40+
}
41+
}

0 commit comments

Comments
 (0)