-
Notifications
You must be signed in to change notification settings - Fork 21
scalac compiles without error access to static protected (not visible) field #6934
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
Comments
Imported From: https://issues.scala-lang.org/browse/SI-6934?orig=1 |
@retronym said: |
hrhino
added a commit
to hrhino/scala
that referenced
this issue
Sep 5, 2017
…ses. This more-or-less reverts the fix added in 1ebbe02. Instead of wantonly treating `protected static` Java members like they're public, it suffices to modify `isSubClassOrCompanion` to consider a class a "subclass or companion" of the companions of its parent classes, provided the parent is a Java-defined class. It's a bit wacky, but comparing the Java and Scala definitions of `protected` access: SLS 5.2.8: Protected members of a class can be accessed from within - the template of the defining class, - all templates that have the defining class as a base class, - the companion module of any of those classes. JLS 6.6.1: A member (class, interface, field, or method) of a reference type ... is accessible only if the type is accessible and the member or constructor is declared to permit access: ... - if the member or constructor is declared protected, then access is permitted only when one of the following is true: - Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared. - Access is correct as described in §6.6.2. JLS 6.6.2.1: Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C. The important differences are that Java protected members are indeed accessible from the package that they're declared in (this behavior is implemented by `accessBoundary`, and that `static` methods are treated the same as instance methods for purposes of access control. Therefore, I feel comfortable adding the third case to `isSubClassOrCompanion`, thus taking the Java access control model when dealing with Java-defined types. Fixes scala/bug#6934, review by adriaanm and (of course) whoever feels like swinging by and saying hi.
hrhino
added a commit
to hrhino/scala
that referenced
this issue
Sep 5, 2017
…ses. This more-or-less reverts the fix added in 1ebbe02. Instead of wantonly treating `protected static` Java members like they're public, it suffices to modify `isSubClassOrCompanion` to consider a class a "subclass or companion" of the companions of its parent classes, provided the parent is a Java-defined class. It's a bit wacky, but comparing the Java and Scala definitions of `protected` access: SLS 5.2.8: Protected members of a class can be accessed from within - the template of the defining class, - all templates that have the defining class as a base class, - the companion module of any of those classes. JLS 6.6.1: A member (class, interface, field, or method) of a reference type ... is accessible only if the type is accessible and the member or constructor is declared to permit access: ... - if the member or constructor is declared protected, then access is permitted only when one of the following is true: - Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared. - Access is correct as described in §6.6.2. JLS 6.6.2.1: Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C. The important differences are that Java protected members are indeed accessible from the package that they're declared in (this behavior is implemented by `accessBoundary`, and that `static` methods are treated the same as instance methods for purposes of access control. Therefore, I feel comfortable adding the third case to `isSubClassOrCompanion`, thus taking the Java access control model when dealing with Java-defined types. I also scouted a grammar tweak, which makes this have a few more files in the diff than would be expected. Fixes scala/bug#6934, review by adriaanm and (of course) whoever feels like swinging by and saying hi.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Scala 2.10.0 compiles without reporting an error the following classes:
But it fails during execution:
Compilation correctly fails when using Scala 2.9.2
Compilation also correctly fails on 2.10.0 for non-static protected fields.
The text was updated successfully, but these errors were encountered: