Skip to content

Commit 121af88

Browse files
committed
Add a Java protected member case, which can only call super
1 parent 3cb01d6 commit 121af88

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ object ProtectedAccessors {
3838
sym.isTerm && sym.is(Protected) &&
3939
!sym.owner.is(Trait) && // trait methods need to be handled specially, are currently always public
4040
!insideBoundaryOf(sym) &&
41-
!ctx.owner.enclosingClass.derivesFrom(sym.owner)
41+
(sym.is(JavaDefined) || !ctx.owner.enclosingClass.derivesFrom(sym.owner))
4242
}
4343

4444
class ProtectedAccessors extends MiniPhase {

tests/run/i17021.ext-java/A.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Derives from run/i17021.defs, but with a Java protected member
2+
package p1;
3+
4+
public class A {
5+
protected int foo() { return 1; }
6+
}

tests/run/i17021.ext-java/Test.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Derives from run/i17021.defs
2+
// but with a Java protected member
3+
// which changes the behaviour
4+
package p2:
5+
trait B extends p1.A:
6+
def bar: Int = foo
7+
8+
class C extends B:
9+
override def foo: Int = 2
10+
11+
object Test:
12+
def main(args: Array[String]): Unit =
13+
val n = new p2.C().bar
14+
assert(n == 1, n) // B can only call super.foo

0 commit comments

Comments
 (0)