From 17dadf791a5b42eb406227901b1c16940e1537cb Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 29 Oct 2024 17:52:13 +0000 Subject: [PATCH] Fix extending protected nested java classes PR 21362 added an accessibility fix to Erasure, but that revealed a mistake in determining the accessibility of inner java classes, which I'm now fixing. --- .../dotty/tools/dotc/core/classfile/ClassfileParser.scala | 3 ++- compiler/src/dotty/tools/dotc/printing/Formatting.scala | 7 +++++++ tests/pos/i21631_joint/AbstractChannel.java | 7 +++++++ tests/pos/i21631_joint/i21631.scala | 5 +++++ tests/pos/i21631_separ/AbstractChannel_1.java | 7 +++++++ tests/pos/i21631_separ/i21631_2.scala | 5 +++++ 6 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i21631_joint/AbstractChannel.java create mode 100644 tests/pos/i21631_joint/i21631.scala create mode 100644 tests/pos/i21631_separ/AbstractChannel_1.java create mode 100644 tests/pos/i21631_separ/i21631_2.scala diff --git a/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala b/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala index 9e7b59a0cfac..f6ad61148208 100644 --- a/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala +++ b/compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala @@ -401,7 +401,8 @@ class ClassfileParser( classRoot.setFlag(sflags) moduleRoot.setFlag(Flags.JavaDefined | Flags.ModuleClassCreationFlags) - val privateWithin = getPrivateWithin(jflags) + val jflags1 = innerClasses.get(currentClassName.toString).fold(jflags: Int)(_.jflags) + val privateWithin = getPrivateWithin(jflags1) classRoot.setPrivateWithin(privateWithin) moduleRoot.setPrivateWithin(privateWithin) diff --git a/compiler/src/dotty/tools/dotc/printing/Formatting.scala b/compiler/src/dotty/tools/dotc/printing/Formatting.scala index a36e6f48533a..14b822b11333 100644 --- a/compiler/src/dotty/tools/dotc/printing/Formatting.scala +++ b/compiler/src/dotty/tools/dotc/printing/Formatting.scala @@ -109,6 +109,13 @@ object Formatting { case Atoms.Range(lo, hi) => CtxShow(s"Range(${toStr(lo.toList)}, ${toStr(hi.toList)})") end given + given Show[ast.untpd.Modifiers] with + def show(x: ast.untpd.Modifiers) = + CtxShow(s"Modifiers(${toStr(x.flags)}, ${toStr(x.privateWithin)}, ${toStr(x.annotations)}, ${toStr(x.mods)})") + + given Show[ast.untpd.Mod] with + def show(x: ast.untpd.Mod) = CtxShow(s"Mod(${toStr(x.flags)})") + given Show[Showable] = ShowAny given Show[Shown] = ShowAny given Show[Int] = ShowAny diff --git a/tests/pos/i21631_joint/AbstractChannel.java b/tests/pos/i21631_joint/AbstractChannel.java new file mode 100644 index 000000000000..fbcd04549def --- /dev/null +++ b/tests/pos/i21631_joint/AbstractChannel.java @@ -0,0 +1,7 @@ +public abstract class AbstractChannel { + protected AbstractChannel() {} + protected abstract AbstractUnsafe newUnsafe(); + protected abstract class AbstractUnsafe { + public abstract void connect(); + } +} diff --git a/tests/pos/i21631_joint/i21631.scala b/tests/pos/i21631_joint/i21631.scala new file mode 100644 index 000000000000..c567d75b1375 --- /dev/null +++ b/tests/pos/i21631_joint/i21631.scala @@ -0,0 +1,5 @@ +class Channel extends AbstractChannel() { + override def newUnsafe(): AbstractChannel#AbstractUnsafe = new AbstractUnsafe { + override def connect(): Unit = ??? + } +} diff --git a/tests/pos/i21631_separ/AbstractChannel_1.java b/tests/pos/i21631_separ/AbstractChannel_1.java new file mode 100644 index 000000000000..e89767d6d73e --- /dev/null +++ b/tests/pos/i21631_separ/AbstractChannel_1.java @@ -0,0 +1,7 @@ +public abstract class AbstractChannel_1 { + protected AbstractChannel_1() {} + protected abstract AbstractUnsafe newUnsafe(); + protected abstract class AbstractUnsafe { + public abstract void connect(); + } +} diff --git a/tests/pos/i21631_separ/i21631_2.scala b/tests/pos/i21631_separ/i21631_2.scala new file mode 100644 index 000000000000..89524e333920 --- /dev/null +++ b/tests/pos/i21631_separ/i21631_2.scala @@ -0,0 +1,5 @@ +class Channel extends AbstractChannel_1() { + override def newUnsafe(): AbstractChannel_1#AbstractUnsafe = new AbstractUnsafe { + override def connect(): Unit = ??? + } +}