Stricter override checking for protected Scala members which override Java members#9525
Stricter override checking for protected Scala members which override Java members#9525lrytz merged 2 commits intoscala:2.13.xfrom kynthus:2.13.x
Conversation
There was a problem hiding this comment.
Thank you @kynthus, this looks very good! I split up the change in two commits, so that the it's easy to see what's changed in the test output.
Scala 3 seems to have the same issue. Are you interested in submitting a ticket? https://github.com/lampepfl/dotty/issues.
My only reservation is: does this break codebases that currently compile? The additional restrictions are correct, but the fact that our checks are too lax doesn't manifest at runtime, because the Scala compiler emits protected, protected[X] and private[X] as public in bytecode.
@SethTisue wdyt?
|
@lrytz Thank you for your review and various reinforcements.
Scala 3 doesn't seem to need this fix. However, the following cases are still alive in Scala 3.
About the bottom one, |
Right, I missed something in my previous test. Scala 3 does the same as this PR. @SethTisue @dwijnand I think the new restrictions can be added. They are all about overriding public or package-private Java members with |
|
over at scala/community-build#1384 , this caused two community build failures: this seems acceptable (there are 246 projects in the 2.13 community build!) I will submit the necessary changes to the upstream projects (and see if they object, though I don't see why they would). |
scala/scala#9525, which has been merged for inclusion in Scala 2.13.6, makes it illegal to override a public method and specify a more restrictive access level. (It should have been illegal all along.) This came up in the Scala community build.
|
👍 thanks. The required change is binary compatible, |
|
(The changes to the upstream projects were only in one place each, and don't seem to contravene any design intent; I strongly suspect the |
Currently, a
protected,protected[x]orprivate[x]Scala member is always allowed when overriding apublicor package-protected (default access) Java member. (Overriding aprotectedJava member is already checked correctly).Since scalac emits all
protected,protected[x]andprivate[x]methods aspublicin bytecode, the generated classfiles are valid.This PR tightens the checks, so that
publicJava member can only be overridden by apublicScala memberpublic,protected[p]orprivate[p]Scala member wherepis the same packageFixes part of scala/bug#12349.