Commit 93e58da
authored
Fix isJvmAccessible to handle nested protected Java classes (#24625)
Fixes #24507
The previous implementation of `isJvmAccessible` failed to
recognize protected Java nested classes as accessible.
And it causes compilation errors when extending them from subclasses.
Previous implementation:
```scala
def isJvmAccessible(cls: Symbol): Boolean =
!cls.is(Flags.JavaDefined) || {
val boundary = cls.accessBoundary(cls.owner)(using preErasureCtx)
(boundary eq defn.RootClass) ||
(ctx.owner.enclosingPackageClass eq boundary)
}
```
For protected nested classes like B below, the access boundary is the
enclosing class (`A` in this case), not a `package a` or root.
However, `B` should be accessible through a public class `A`.
```java
package a;
public class A {
protected class B {}
}
```
This commit replace the manual boundary checks with
`cls.isAccessibleFrom`, which properly handles all Java access
modifiers.
This aligns with the Scala 2 implementation which uses the
equivalent `context.isAccessible(cls, cls.owner.thisType)`.
https://github.com/scala/scala/blob/efb71845113639bd1da231c917e18af33519fc07/src/compiler/scala/tools/nsc/transform/Erasure.scala#L1451-L1455
For example,
- `cls` will be `protected class B`
- `cls.owner.thisType` will be `public class A`
- And, `cls.isAccessibleFrom(cls.owner.thisType)` is true.
On the other hand, `isJvmAccessible` returns false for Java-defined
package protected class case like `java-package-protected` case.
https://github.com/tanishiking/scala3/tree/b2850be4c79996d8ebd9afaf36ed12f46febb0d1/tests/run/java-package-protected
In this case,
- `cls` = `class B` (package protected under `a`)
- `cls.owner.thisType` = `package a`
- And, `cls.isAccessibleFrom(cls.owner.thisType)` is false.
because `B` is package protected and, we cannot access B through `a.B`
from another package. In this case, cast to `A` happens.
---
Note that, this regression starts from
e7d479f
but the true commit is
d3ce551
but the reason why we stopped using `isAccessibleFrom` seems to be an
optimization and the `tests/pos/trait-access` still compiles with this
change.File tree
5 files changed
+26
-7
lines changed- compiler/src/dotty/tools/dotc/transform
- tests
- pos
- run
- i24507
5 files changed
+26
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
748 | 748 | | |
749 | 749 | | |
750 | 750 | | |
751 | | - | |
752 | | - | |
753 | | - | |
754 | | - | |
755 | | - | |
756 | | - | |
757 | | - | |
| 751 | + | |
| 752 | + | |
758 | 753 | | |
759 | 754 | | |
760 | 755 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
0 commit comments