Skip to content

Commit e449b68

Browse files
Backport "Refactor hasOnlyLocalInstantiation to use isLocalToCompilationUnit" to LTS (#20993)
Backports #19886 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 4126175 + 3266eb7 commit e449b68

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,11 @@ object SymDenotations {
12001200
final def isEffectivelySealed(using Context): Boolean =
12011201
isOneOf(FinalOrSealed) || isClass && !isOneOf(EffectivelyOpenFlags)
12021202

1203+
final def isLocalToCompilationUnit(using Context): Boolean =
1204+
is(Private)
1205+
|| owner.ownersIterator.takeWhile(!_.isStaticOwner).exists(_.isTerm)
1206+
|| accessBoundary(defn.RootClass).isProperlyContainedIn(symbol.topLevelClass)
1207+
12031208
final def isTransparentClass(using Context): Boolean =
12041209
is(TransparentType)
12051210
|| defn.isAssumedTransparent(symbol)

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

Lines changed: 4 additions & 5 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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)