New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stricter override checking for protected Scala members which override Java members #9525
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 apublic
or package-protected (default access) Java member. (Overriding aprotected
Java member is already checked correctly).Since scalac emits all
protected
,protected[x]
andprivate[x]
methods aspublic
in bytecode, the generated classfiles are valid.This PR tightens the checks, so that
public
Java member can only be overridden by apublic
Scala memberpublic
,protected[p]
orprivate[p]
Scala member wherep
is the same packageFixes part of scala/bug#12349.