Skip to content

GROOVY-12537: STC: field access through a protected or doubly-nested … - #3052

Merged
paulk-asert merged 1 commit into
apache:masterfrom
paulk-asert:groovy12537
Oct 30, 2026
Merged

paulk-asert merged 1 commit into
apache:masterfrom
paulk-asert:groovy12537

Conversation

@paulk-asert

Copy link
Copy Markdown
Contributor

…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.

@codecov-commenter

codecov-commenter commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 73.6719%. Comparing base (a39ba7a) to head (f16a2f1).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...roovy/transform/stc/StaticTypeCheckingVisitor.java 93.3333% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@                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     
Files with missing lines Coverage Δ
...roovy/transform/stc/StaticTypeCheckingVisitor.java 87.5000% <93.3333%> (+0.0591%) ⬆️

... and 28 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  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).

  2. testProtectedMemberTypeFromSubclassBodyInSameUnit only compiles. Could it assert a run result the way the other tests do?

  3. 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 daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@paulk-asert

Copy link
Copy Markdown
Contributor Author

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:

  • private is never accessible. A nest lies within one source unit, so a precompiled member type can't share the accessor's nest. This rejects the same-package case too, consistent with how master (after GROOVY-12509: STC: allow members of a private nested type from another class of the same nest #3044) treats a source member type of another top-level class.
  • package-private is accessible from the same package only.
  • protected is still admitted without the subclass check. Without the enclosing type, the method can't make it. Its class file marks it public, so the access can't fail at runtime; the only difference from javac is that we accept it. The Javadoc now gives the reasoning for each case, so nobody tightens or loosens it without context. If your review text (it cuts off at "The new branch:") was also about protected, I can follow up once GROOVY-12544: InnerClasses attribute omits nested classes that are no… #3077's outer-class data is available for classes read from bytecode.
  1. GROOVY-12509: STC: allow members of a private nested type from another class of the same nest #3044. Rebased on it. Its tests in FieldsAndPropertiesSTCTest, the write, attribute, closure, enum and other-top-level-class negative cases, are on master and pass unchanged. The early return it added is subsumed by the private branch here.

  2. Same-unit test. testProtectedMemberTypeFromSubclassBodyInSameUnit now compiles to a temp dir, loads the classes and asserts run() == 'ff'.

  3. Negatives. Added:

  • protected member type from a non-subclass in another package (same unit)
  • package-private member type from another package, both in the same unit and precompiled
  • precompiled private member type, from the same package and from another package, loaded both from a Class and from class files

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).

@testlens-app

testlens-app Bot commented Oct 29, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: f16a2f1
▶️ Tests: 124971 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@paulk-asert
paulk-asert merged commit 5c4842e into apache:master Oct 30, 2026
27 checks passed
@paulk-asert
paulk-asert deleted the groovy12537 branch October 30, 2026 05:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants