Skip to content

GROOVY-12484: STC: method and constructor calls on an inaccessible ty… - #3019

Merged
paulk-asert merged 1 commit into
apache:masterfrom
paulk-asert:groovy12484
Oct 31, 2026
Merged

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

Conversation

@paulk-asert

Copy link
Copy Markdown
Contributor

…pe compile and fail at runtime with IllegalAccessError

@codecov-commenter

codecov-commenter commented Oct 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.6100%. Comparing base (cfd9b2f) to head (ceb466e).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3019        +/-   ##
==================================================
+ Coverage     73.6047%   73.6100%   +0.0052%     
- Complexity      42348      42356         +8     
==================================================
  Files            1657       1657                
  Lines          145708     145718        +10     
  Branches        27498      27501         +3     
==================================================
+ Hits           107248     107263        +15     
+ Misses          28479      28477         -2     
+ Partials         9981       9978         -3     
Files with missing lines Coverage Δ
...roovy/transform/stc/StaticTypeCheckingVisitor.java 87.5000% <100.0000%> (+0.0289%) ⬆️

... and 12 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 aligning STC with the existing inaccessible-type checks for fields, and for documenting the JLS 6.6.1 angle in the comments.

Observations:

  1. Guarding constructor selection with !call.isSpecialCall() and hasAccessToClass(...) looks correct so that this / super construction is not wrongly rejected while new on a package-private type from outside is.

  2. For method calls, keying the check off ClassExpression and the chosen receiver (rather than only the method's modifiers) matches the "qualifying type must be accessible" rule. The Pub.inherited() case in the tests is a good regression guard for that distinction.

  3. Leaving dynamic dispatch and class-literal / Class method usage alone is helpful; the tests make that boundary clear.

  4. Optional follow-up (not blocking): are instance calls whose static type is inaccessible but not written as a ClassExpression already covered elsewhere, or intentionally out of scope for this ticket? Either way is fine; I mainly want to confirm the intended surface.

Groovy12484 is a clear, minimal suite for the behavior change.

…pe compile and fail at runtime with IllegalAccessError
@paulk-asert

paulk-asert commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks Jochen. You're right that STC doesn't support an extension or DGM method acting as a constructor, so nothing stops the constructor check from coming earlier. I've checked the code and the runtime:

  • findMethod adds extension methods only when the name isn't <init>.
  • Dynamic Groovy never treats a DGM or category method as a constructor. MetaClassImpl.invokeConstructor chooses only among the class's own constructors.
  • The one runtime way to add a constructor is ExpandoMetaClass (Foo.metaClass.constructor = { Integer i -> … }). It works for dynamic calls, but STC doesn't see it either and reports "Cannot find matching constructor".

So the current revised version moves the constructor check ahead of lookup. For an inaccessible type, STC now reports "Cannot access constructor of class: T" and skips lookup altogether. It still checks the arguments the same way it does when no constructor matches. A new test covers new p.Outer.Impl('x'). Before the move, that reported "Cannot find matching constructor", which pointed at the wrong problem.

For method calls, I don't think the check can go inside findMethod. What has to be accessible is the type written at the call site, not the class that declares the method. findMethod only gets (receiver, name, args), so it can't tell T.m() from a call on an expression whose type happens to be T. Filtering by declaring class would reject p.Pub.s() (row 4 below), which is legal in Java and in Groovy. Filtering by receiver type would drop every candidate at once, so you'd get the same rejection with a "Cannot find matching method" message, which is worse when the method plainly exists. That's why the method check stays on the ClassExpression branch.

Fixtures (all Java, precompiled): in package p, Base and Impl are package-private, Pub is public, Pub extends Base, Impl extends Pub, and Outer.makeImpl() returns an Impl. Every call is made from another package.

# Call from another package Java (javac) Groovy dynamic @CompileStatic before PR @CompileStatic with PR
1 new p.Impl() error: Impl is not public OK compiles, then IllegalAccessError error: Cannot access constructor
2 new p.Impl("x") (no matching constructor) error: Impl is not public GroovyRuntimeException: Could not find matching constructor error: Cannot find matching constructor error: Cannot access constructor
3 p.Impl.hello() error: Impl is not public OK compiles, then IllegalAccessError error: Cannot access method
4 p.Pub.s() (s declared in package-private Base) OK OK OK OK
5 p.Pub.make().i() (i declared in Base) OK OK OK OK
6 p.Impl.class.getSimpleName() / p.Impl.getSimpleName() error: Impl is not public OK OK OK (Class methods exempt)
7 p.Outer.makeImpl().own() (own declared in Impl) error: defined in an inaccessible class OK compiles, then IllegalAccessError compiles, then IllegalAccessError (unchanged)
8 p.Outer.makeImpl().i() (i declared in Base) error: defined in an inaccessible class OK compiles, then IllegalAccessError compiles, then IllegalAccessError (unchanged)
9 p.Outer.makeImpl().toString() / .hashCode() error: defined in an inaccessible class OK OK OK
10 constructor added with metaClass.constructor n/a OK error: Cannot find matching constructor unchanged

Rows 4 and 5 are why the check has to use the type written at the call site rather than the declaring class. Rows 1–3 are the cases the ticket is about. Row 6 is a deliberate difference from Java, because Groovy lets you use an inaccessible class as a Class value.

Rows 7–9 are the case Daniel asked about, a call on an expression whose type is inaccessible. Java rejects all of them, even hashCode(). @CompileStatic names the method's declaring class in the bytecode when that class is accessible, and otherwise the receiver's static type. Row 9 works because the bytecode names Object (hashCode) or calls the DGM toString(Object). Rows 7 and 8 fail because neither the declaring class nor the receiver type is accessible. Row 8 could be made to work by naming the nearest accessible type in between (Pub, as row 5 already does). Row 7 can't, because only Impl declares own, so it should be a compile error. That's where your "select only from accessible candidates" idea fits, since there it really is a property of each candidate method. I'd rather handle both in a follow-up ticket than widen this PR.

@testlens-app

testlens-app Bot commented Oct 28, 2026

Copy link
Copy Markdown

🚨 All tests passed but jobs failed 🚨

Failed Jobs without Test Failures

❌ Build and test / lts (17, macos-latest)

🏷️ Commit: ceb466e
▶️ Tests: 112445 executed
⚪️ Checks: 32/32 completed

Rerun Controls

Click the checkbox to trigger a rerun:

  • Rerun jobs

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 for the follow-up on tip ceb466e.

Since my earlier comment: moving the constructor accessibility check ahead of lookup addresses Jochen’s point cleanly, and testConstructorCallRejectedBeforeLookup makes the diagnostic improvement concrete (Cannot access constructor instead of a misleading “no matching constructor”). I agree STC has no DGM-as-constructor path, so the earlier check is safe.

On the optional question from last time — instance calls whose static type is inaccessible but not written as a ClassExpression — your table (rows 7–9) answers it well. Leaving those for a follow-up, while keeping the ClassExpression / constructor cases in this ticket, is a sensible boundary; I will not re-litigate that here.

The Pub.inherited() and class-literal / Class method exemptions remain the right compatibility posture for this change. No further blocking comments from me on this tip.

@paulk-asert
paulk-asert merged commit 4cbdbe0 into apache:master Oct 31, 2026
32 of 33 checks passed
@paulk-asert
paulk-asert deleted the groovy12484 branch October 31, 2026 19:09
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.

4 participants