Repository navigation
GROOVY-12537: STC: field access through a protected or doubly-nested … - #3052
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3052 +/- ##
==================================================
+ Coverage 73.6210% 73.6719% +0.0509%
- Complexity 42528 42592 +64
==================================================
Files 1677 1678 +1
Lines 146901 146919 +18
Branches 27792 27798 +6
==================================================
+ Hits 108150 108238 +88
+ Misses 28609 28526 -83
- Partials 10142 10155 +13
🚀 New features to boost your workflow:
|
daniellansun
left a comment
There was a problem hiding this comment.
Thanks for the clear write-up on GROOVY-12537 — the old hasAccessToClass path really was the wrong shape for member types.
The new breakdown (outer accessibility first, then public / private+inSameNest / same package as outer / protected via enclosing classes of the accessor) matches JLS 6.6.1–6.6.2 for the cases that still have an outer ClassNode. The outer == null branch is a conscious loosening for precompiled member types; it would help to keep that rationale in the method javadoc so nobody "tightens" it later without reading the ticket.
A few requests before I +1:
-
Please coordinate with #3044 — both touch hasAccessToClass. #3044 is the narrow nestmate-field regression fix with a stronger test matrix; this PR should either rebase on it or absorb those tests (write, attribute, closure, enum, and the cross-nest negative).
-
testProtectedMemberTypeFromSubclassBodyInSameUnit only compiles. Could it assert a run result the way the other tests do?
-
Negatives would help a lot: protected member type from a non-subclass in another package, and package-private nested still rejected across packages (including when outer is missing). Codecov is already hinting at uncovered branches here.
I am fine with the direction; I just want the overlap and the rejection paths nailed down.
daniellansun
left a comment
There was a problem hiding this comment.
Thanks Paul — first pass on tip d1bfbc0 (GROOVY-12537).
What is right
Splitting hasAccessToClass so that:
- private member types use
inSameNest, - protected member types admit the same package or any enclosing class of the accessor that derives from the enclosing type,
- and the enclosing type is still checked recursively,
fixes the two bugs in the description: protected Prot from a subclass body (nested class / anon / closure), and doubly nested private A.B. The multi-unit GroovyClassLoader / CompilationUnit tests are the right shape for the precompiled and same-unit paths.
Blocking: outer == null over-admits
DecompiledClassNode (and plain ClassNode built from a Class) does not override getOuterClass(), so a precompiled nested type typically arrives here with outer == null. The new branch:
…private member type is wrongly rejected as inaccessible hasAccessToClass applied the member-access rule with the enclosing type as the receiver. For a precompiled member type the enclosing type is not known, so a protected member type was checked against itself, and no subclass derives from it. Protected access was also checked against the accessing class alone rather than the whole body of the subclass, and private access stopped at one level of nesting. A member type is now accessible from anywhere in the same nest when private, and from the same package or anywhere in the body of a subclass of its enclosing type when protected. For a precompiled member type only package-private access is enforced.
d1bfbc0 to
f16a2f1
Compare
|
Thanks @daniellansun. Pushed f16a2f1, rebased on master now that #3044 is merged. Blocking: outer == null admits too much. Agreed, and it was worse than lenient: the branch admitted a precompiled private member type, which doesn't work at runtime. @CompileStatic code in another package reading lib.Lib.Hidden.x compiled and then threw IllegalAccessError, where 6.0.0 rejected it at compile time. That branch now follows javac:
The two private tests fail without the change. Groovy12537 passes 9/9. groovy.transform.stc., org.codehaus.groovy.classgen.asm.sc., bugs.* and gls.innerClass.* pass (6,400 tests, 0 failures). |
✅ All tests passed ✅🏷️ Commit: f16a2f1 Learn more about TestLens at testlens.app/docs. |
daniellansun
left a comment
There was a problem hiding this comment.
Thanks Paul — f16a2f1 addresses the blocking point from my earlier review.
What changed vs d1bfbc0
The old outer == null branch really was over-admitting: a precompiled private member type compiling and then throwing IllegalAccessError is worse than a false STC reject. The new breakdown matches what you described:
- private — never accessible when there is no outer (nest is one source unit) → rejects same-package and cross-package
Hidden, Class-loaded and class-file-loaded. - package-private — same package only.
- protected — still admitted without the subclass check when outer is unknown; class file marks it public, so runtime cannot fail. Javadoc now records the rationale so nobody "tightens" it blindly.
Rebase onto #3044, testProtectedMemberTypeFromSubclassBodyInSameUnit asserting run() == 'ff', and the new negatives (non-subclass protected, package-private cross-package, precompiled private) close the gaps I asked for. Groovy12537 at 9/9 plus the stc / asm.sc / bugs / gls.innerClass sweep is reassuring.
Follow-up (non-blocking)
Once #3077's outer-class / InnerClasses data is on master, we can revisit the protected-without-outer case and align closer to javac. Not a merge blocker for GROOVY-12537.
Approving.
…private member type is wrongly rejected as inaccessible
hasAccessToClass applied the member-access rule with the enclosing type as the receiver. For a precompiled member type the enclosing type is not known, so a protected member type was checked against itself, and no subclass derives from it. Protected access was also checked against the accessing class alone rather than the whole body of the subclass, and private access stopped at one level of nesting.
A member type is now accessible from anywhere in the same nest when private, and from the same package or anywhere in the body of a subclass of its enclosing type when protected. For a precompiled member type only package-private access is enforced.