Skip to content

Commit 9ef24bf

Browse files
authored
Refactor hasOnlyLocalInstantiation to use isLocalToCompilationUnit (#19886)
Refactor hasOnlyLocalInstantiation to use isLocalToCompilationUnit
2 parents 3d5cf9c + f8a5583 commit 9ef24bf

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1208,8 +1208,8 @@ object SymDenotations {
12081208

12091209
final def isLocalToCompilationUnit(using Context): Boolean =
12101210
is(Private)
1211-
|| owner.ownersIterator.exists(_.isTerm)
1212-
|| accessBoundary(defn.RootClass).isContainedIn(symbol.topLevelClass)
1211+
|| owner.ownersIterator.takeWhile(!_.isStaticOwner).exists(_.isTerm)
1212+
|| accessBoundary(defn.RootClass).isProperlyContainedIn(symbol.topLevelClass)
12131213

12141214
final def isTransparentClass(using Context): Boolean =
12151215
is(TransparentType)

compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala

+4-5
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,17 @@ object ExplicitOuter {
218218
*/
219219
private def needsOuterAlways(cls: ClassSymbol)(using Context): Boolean =
220220
needsOuterIfReferenced(cls) &&
221-
(!hasLocalInstantiation(cls) || // needs outer because we might not know whether outer is referenced or not
221+
(!hasOnlyLocalInstantiation(cls) || // needs outer because we might not know whether outer is referenced or not
222222
cls.mixins.exists(needsOuterIfReferenced) || // needs outer for parent traits
223223
cls.info.parents.exists(parent => // needs outer to potentially pass along to parent
224224
needsOuterIfReferenced(parent.classSymbol.asClass)))
225225

226226
/** Class is only instantiated in the compilation unit where it is defined */
227-
private def hasLocalInstantiation(cls: ClassSymbol)(using Context): Boolean =
227+
private def hasOnlyLocalInstantiation(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(!_.isStaticOwner).exists(_.isTerm)
231-
|| cls.is(Private, butNot = Module)
232-
|| cls.is(Module) && !cls.owner.is(Trait)
230+
if cls.is(Module) then !cls.owner.is(Trait)
231+
else cls.isLocalToCompilationUnit
233232

234233
/** The outer parameter accessor of cass `cls` */
235234
private def outerParamAccessor(cls: ClassSymbol)(using Context): TermSymbol =

tests/generic-java-signatures/outer-ref-elimination.check

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ List(public T6$$anon$3$C6())
77
List(public T7$C7$1())
88
List(public T8$$anon$4$C8())
99
List(public T9$C9$1(T9))
10+
List(public T10$C10(T10))
11+
List(public T11$D11$C11())

tests/generic-java-signatures/outer-ref-elimination.scala

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class T8 { def t8(): Unit = new AnyRef { class C8; test(classOf[C8]) } }
2626
// the field x.
2727
class T9 { var x = 451; def t9(): Unit = { class C9 {def getX = x}; test(classOf[C9])} }
2828

29+
class T10 { private[T10] class C10; test(classOf[C10]) }
30+
31+
class T11 { class D11 { private[D11] class C11; test(classOf[C11]) } }
32+
2933
object Test {
3034
def main(args: Array[String]): Unit = {
3135
T1
@@ -37,5 +41,7 @@ object Test {
3741
new T7().t7()
3842
new T8().t8()
3943
new T9().t9()
44+
new T10()
45+
val t11 = new T11(); new t11.D11()
4046
}
4147
}

0 commit comments

Comments
 (0)