Skip to content

Commit 5f2d7ef

Browse files
Backport "Fix extending protected nested java classes" to LTS (#22141)
Backports #21857 to the 3.3.5. PR submitted by the release tooling.
2 parents 06b6c96 + 023d0a2 commit 5f2d7ef

File tree

7 files changed

+55
-14
lines changed

7 files changed

+55
-14
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,23 @@ object SymbolLoaders {
4848
*/
4949
def enterClass(
5050
owner: Symbol, name: PreName, completer: SymbolLoader,
51-
flags: FlagSet = EmptyFlags, scope: Scope = EmptyScope)(using Context): Symbol = {
52-
val cls = newClassSymbol(owner, name.toTypeName.unmangleClassName.decode, flags, completer, assocFile = completer.sourceFileOrNull)
51+
flags: FlagSet = EmptyFlags, scope: Scope = EmptyScope, privateWithin: Symbol = NoSymbol,
52+
)(using Context): Symbol = {
53+
val cls = newClassSymbol(owner, name.toTypeName.unmangleClassName.decode, flags, completer, privateWithin, assocFile = completer.sourceFileOrNull)
5354
enterNew(owner, cls, completer, scope)
5455
}
5556

5657
/** Enter module with given `name` into scope of `owner`.
5758
*/
5859
def enterModule(
5960
owner: Symbol, name: PreName, completer: SymbolLoader,
60-
modFlags: FlagSet = EmptyFlags, clsFlags: FlagSet = EmptyFlags, scope: Scope = EmptyScope)(using Context): Symbol = {
61+
modFlags: FlagSet = EmptyFlags, clsFlags: FlagSet = EmptyFlags,
62+
scope: Scope = EmptyScope, privateWithin: Symbol = NoSymbol,
63+
)(using Context): Symbol = {
6164
val module = newModuleSymbol(
6265
owner, name.toTermName.decode, modFlags, clsFlags,
6366
(module, _) => completer.proxy.withDecls(newScope).withSourceModule(module),
67+
privateWithin,
6468
assocFile = completer.sourceFileOrNull)
6569
enterNew(owner, module, completer, scope)
6670
enterNew(owner, module.moduleClass, completer, scope)
@@ -100,13 +104,16 @@ object SymbolLoaders {
100104
*/
101105
def enterClassAndModule(
102106
owner: Symbol, name: PreName, completer: SymbolLoader,
103-
flags: FlagSet = EmptyFlags, scope: Scope = EmptyScope)(using Context): Unit = {
104-
val clazz = enterClass(owner, name, completer, flags, scope)
107+
flags: FlagSet = EmptyFlags, scope: Scope = EmptyScope, privateWithin: Symbol = NoSymbol,
108+
)(using Context): Unit = {
109+
val clazz = enterClass(owner, name, completer, flags, scope, privateWithin)
105110
val module = enterModule(
106111
owner, name, completer,
107112
modFlags = flags.toTermFlags & RetainedModuleValFlags,
108113
clsFlags = flags.toTypeFlags & RetainedModuleClassFlags,
109-
scope = scope)
114+
scope = scope,
115+
privateWithin = privateWithin,
116+
)
110117
}
111118

112119
/** Enter all toplevel classes and objects in file `src` into package `owner`, provided

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,10 @@ class ClassfileParser(
228228

229229
val privateWithin = getPrivateWithin(jflags)
230230

231-
classRoot.setPrivateWithin(privateWithin)
232-
moduleRoot.setPrivateWithin(privateWithin)
233-
moduleRoot.sourceModule.setPrivateWithin(privateWithin)
231+
if privateWithin.exists then
232+
classRoot.setPrivateWithin(privateWithin)
233+
moduleRoot.setPrivateWithin(privateWithin)
234+
moduleRoot.sourceModule.setPrivateWithin(privateWithin)
234235

235236
for (i <- 0 until in.nextChar) parseMember(method = false)
236237
for (i <- 0 until in.nextChar) parseMember(method = true)
@@ -884,11 +885,13 @@ class ClassfileParser(
884885
private def enterOwnInnerClasses()(using Context, DataReader): Unit = {
885886
def enterClassAndModule(entry: InnerClassEntry, file: AbstractFile, jflags: Int) =
886887
SymbolLoaders.enterClassAndModule(
887-
getOwner(jflags),
888-
entry.originalName,
889-
new ClassfileLoader(file),
890-
classTranslation.flags(jflags),
891-
getScope(jflags))
888+
getOwner(jflags),
889+
entry.originalName,
890+
new ClassfileLoader(file),
891+
classTranslation.flags(jflags),
892+
getScope(jflags),
893+
getPrivateWithin(jflags),
894+
)
892895

893896
for entry <- innerClasses.valuesIterator do
894897
// create a new class member for immediate inner classes

compiler/src/dotty/tools/dotc/printing/Formatting.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ object Formatting {
112112
case Atoms.Range(lo, hi) => CtxShow(s"Range(${toStr(lo.toList)}, ${toStr(hi.toList)})")
113113
end given
114114

115+
given Show[ast.untpd.Modifiers] with
116+
def show(x: ast.untpd.Modifiers) =
117+
CtxShow(s"Modifiers(${toStr(x.flags)}, ${toStr(x.privateWithin)}, ${toStr(x.annotations)}, ${toStr(x.mods)})")
118+
119+
given Show[ast.untpd.Mod] with
120+
def show(x: ast.untpd.Mod) = CtxShow(s"Mod(${toStr(x.flags)})")
121+
115122
given Show[Showable] = ShowAny
116123
given Show[Shown] = ShowAny
117124
given Show[Int] = ShowAny
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public abstract class AbstractChannel {
2+
protected AbstractChannel() {}
3+
protected abstract AbstractUnsafe newUnsafe();
4+
protected abstract class AbstractUnsafe {
5+
public abstract void connect();
6+
}
7+
}

tests/pos/i21631_joint/i21631.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Channel extends AbstractChannel() {
2+
override def newUnsafe(): AbstractChannel#AbstractUnsafe = new AbstractUnsafe {
3+
override def connect(): Unit = ???
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public abstract class AbstractChannel_1 {
2+
protected AbstractChannel_1() {}
3+
protected abstract AbstractUnsafe newUnsafe();
4+
protected abstract class AbstractUnsafe {
5+
public abstract void connect();
6+
}
7+
}

tests/pos/i21631_separ/i21631_2.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Channel extends AbstractChannel_1() {
2+
override def newUnsafe(): AbstractChannel_1#AbstractUnsafe = new AbstractUnsafe {
3+
override def connect(): Unit = ???
4+
}
5+
}

0 commit comments

Comments
 (0)