scala3
scala3 copied to clipboard
Compiler should not allow to access Java protected static method defined in different package
Compiler version
All Scala 3 versions
Minimized code
The snippet below can be compiled with Scala 2 leading to runtime failure.
// Task.java
package foo;
public abstract class Task{
protected static Task poll(){ return null; }
}
@main def Test = {
val task = foo.Task.poll()
println(task)
}
Output
Exception in thread "main" java.lang.IllegalAccessError: class main$package$ tried to access protected method 'foo.Task foo.Task.poll()' (main$package$ and foo.Task are in unnamed module of loader 'app')
at main$package$.Test(main.scala:1)
at Test.main(main.scala:1)
Expectation
Typer should prevent access to protected static method which cannot be accessed. Scala 2 handles this case correctly
Obvious after morning coffee, but it should say Scala 2 refuses to compile, but Scala 3 fails at runtime.
Scala 2 messaging:
i18446.scala:5: error: method poll in class Task cannot be accessed as a member of object foo.Task from class Subtask
Access to protected method poll not permitted because
enclosing class Subtask is not a subclass of
class Task in package foo where target is defined
@bracevac would that seem like an appropriate spree issue to you? For example for tomorrow? 🙂
This issue was picked for the Scala Issue Spree of tomorrow, July 2nd. @bracevac and @hamzaremmal will be working on it! If you have any insight into the issue or guidance on how to fix it, please leave it here.
Preliminary conclusions after the spree:
- The culprit appears to be this line in
core/SymDenotations.scala
which makes theprotected static
method public. - An ad hoc fix makes the example fail at compile time, as intended, but then we lose the ability to access
poll
from classes inheriting fromTask
that reside outside of thefoo
package. Java allows us to do that. - We're investigating if there is a relatively straightforward way to support Java's semantics for
protected static
. - We should also check that the positive Java interop test cases do not exhibit similar runtime crashes.