Skip to content

Commit 1a8bf67

Browse files
Backport "Fix extending protected nested java classes" to 3.6.2 (#21904)
Backports #21857 to the 3.6.2 branch. PR submitted by the release tooling. [skip ci]
2 parents 5ae06ef + 6ca4c23 commit 1a8bf67

File tree

7 files changed

+55
-14
lines changed

7 files changed

+55
-14
lines changed

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,23 @@ object SymbolLoaders {
5151
*/
5252
def enterClass(
5353
owner: Symbol, name: PreName, completer: SymbolLoader,
54-
flags: FlagSet = EmptyFlags, scope: Scope = EmptyScope)(using Context): Symbol = {
55-
val cls = newClassSymbol(owner, name.toTypeName.unmangleClassName.decode, flags, completer, compUnitInfo = completer.compilationUnitInfo)
54+
flags: FlagSet = EmptyFlags, scope: Scope = EmptyScope, privateWithin: Symbol = NoSymbol,
55+
)(using Context): Symbol = {
56+
val cls = newClassSymbol(owner, name.toTypeName.unmangleClassName.decode, flags, completer, privateWithin, compUnitInfo = completer.compilationUnitInfo)
5657
enterNew(owner, cls, completer, scope)
5758
}
5859

5960
/** Enter module with given `name` into scope of `owner`.
6061
*/
6162
def enterModule(
6263
owner: Symbol, name: PreName, completer: SymbolLoader,
63-
modFlags: FlagSet = EmptyFlags, clsFlags: FlagSet = EmptyFlags, scope: Scope = EmptyScope)(using Context): Symbol = {
64+
modFlags: FlagSet = EmptyFlags, clsFlags: FlagSet = EmptyFlags,
65+
scope: Scope = EmptyScope, privateWithin: Symbol = NoSymbol,
66+
)(using Context): Symbol = {
6467
val module = newModuleSymbol(
6568
owner, name.toTermName.decode, modFlags, clsFlags,
6669
(module, _) => completer.proxy.withDecls(newScope).withSourceModule(module),
70+
privateWithin,
6771
compUnitInfo = completer.compilationUnitInfo)
6872
enterNew(owner, module, completer, scope)
6973
enterNew(owner, module.moduleClass, completer, scope)
@@ -103,13 +107,16 @@ object SymbolLoaders {
103107
*/
104108
def enterClassAndModule(
105109
owner: Symbol, name: PreName, completer: SymbolLoader,
106-
flags: FlagSet = EmptyFlags, scope: Scope = EmptyScope)(using Context): Unit = {
107-
val clazz = enterClass(owner, name, completer, flags, scope)
110+
flags: FlagSet = EmptyFlags, scope: Scope = EmptyScope, privateWithin: Symbol = NoSymbol,
111+
)(using Context): Unit = {
112+
val clazz = enterClass(owner, name, completer, flags, scope, privateWithin)
108113
val module = enterModule(
109114
owner, name, completer,
110115
modFlags = flags.toTermFlags & RetainedModuleValFlags,
111116
clsFlags = flags.toTypeFlags & RetainedModuleClassFlags,
112-
scope = scope)
117+
scope = scope,
118+
privateWithin = privateWithin,
119+
)
113120
}
114121

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

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

+11-8
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,10 @@ class ClassfileParser(
403403

404404
val privateWithin = getPrivateWithin(jflags)
405405

406-
classRoot.setPrivateWithin(privateWithin)
407-
moduleRoot.setPrivateWithin(privateWithin)
408-
moduleRoot.sourceModule.setPrivateWithin(privateWithin)
406+
if privateWithin.exists then
407+
classRoot.setPrivateWithin(privateWithin)
408+
moduleRoot.setPrivateWithin(privateWithin)
409+
moduleRoot.sourceModule.setPrivateWithin(privateWithin)
409410

410411
for (i <- 0 until in.nextChar) parseMember(method = false)
411412
for (i <- 0 until in.nextChar) parseMember(method = true)
@@ -1059,11 +1060,13 @@ class ClassfileParser(
10591060
private def enterOwnInnerClasses()(using Context, DataReader): Unit = {
10601061
def enterClassAndModule(entry: InnerClassEntry, file: AbstractFile, jflags: Int) =
10611062
SymbolLoaders.enterClassAndModule(
1062-
getOwner(jflags),
1063-
entry.originalName,
1064-
new ClassfileLoader(file),
1065-
classTranslation.flags(jflags),
1066-
getScope(jflags))
1063+
getOwner(jflags),
1064+
entry.originalName,
1065+
new ClassfileLoader(file),
1066+
classTranslation.flags(jflags),
1067+
getScope(jflags),
1068+
getPrivateWithin(jflags),
1069+
)
10671070

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

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

+7
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ object Formatting {
109109
case Atoms.Range(lo, hi) => CtxShow(s"Range(${toStr(lo.toList)}, ${toStr(hi.toList)})")
110110
end given
111111

112+
given Show[ast.untpd.Modifiers] with
113+
def show(x: ast.untpd.Modifiers) =
114+
CtxShow(s"Modifiers(${toStr(x.flags)}, ${toStr(x.privateWithin)}, ${toStr(x.annotations)}, ${toStr(x.mods)})")
115+
116+
given Show[ast.untpd.Mod] with
117+
def show(x: ast.untpd.Mod) = CtxShow(s"Mod(${toStr(x.flags)})")
118+
112119
given Show[Showable] = ShowAny
113120
given Show[Shown] = ShowAny
114121
given Show[Int] = ShowAny
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

+5
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+
}
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

+5
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)