Skip to content

[java] Fix #7054: Capture wildcard components of intersection bounds - #7055

Merged
adangel merged 6 commits into
pmd:mainfrom
benstpierre:7054-capture-intersection-bound
Oct 23, 2026
Merged

adangel merged 6 commits into
pmd:mainfrom
benstpierre:7054-capture-intersection-bound

Conversation

@benstpierre

Copy link
Copy Markdown
Contributor

Describe the PR

I think this fixes #7054. Calling forEach through C extends Iterable<?> & Marker currently throws IllegalArgumentException: <?> cannot be a wildcard bound, even though the small reproducer compiles without warnings.

TypeOps.getMemberSource captures a single parameterized upper bound, but an intersection reaches member lookup with its component wildcards uncaptured. This change follows type-variable upper bounds and captures each intersection component before resolving members.

The commits preserve the test-first sequence:

  1. Add the exact seven-line reproducer to CaptureInferenceTest. On unmodified PMD, this test fails with the reported exception; the existing tests in that suite pass.
  2. Apply the fix and add five related cases: upper/lower wildcard bounds, a class bound, a chained type parameter, and an intersection cast. Assertions check the inferred method signature and lambda parameter type as well as successful analysis.

I'd appreciate a review of whether this is the right place to handle intersection member capture, especially any type-system cases the tests miss.

Related issues

Validation

On Corretto JDK 25.0.2 / macOS arm64:

./mvnw -B -ntp -pl pmd-java -am -PfastSkip -Dtest=CaptureInferenceTest -Dsurefire.failIfNoSpecifiedTests=false test
./mvnw -B -ntp -pl pmd-java -am -PfastSkip test
./mvnw -B -ntp -pl pmd-java -am checkstyle:check

The first command fails on the test-only commit and passes with the fix. The full Java reactor test run reports 16,701 tests, 31 skipped, zero failures/errors (16,670 passed), including the six new regression cases. Checkstyle reports zero violations. All six Java snippets also compile with javac --release 17 -Xlint:all -Werror.

The full distribution build across all languages has not been run locally.

Ready?

  • Added unit tests for the bug
  • Java reactor unit tests pass (including required core/test modules)
  • Complete build ./mvnw clean verify passes (checked automatically by GitHub Actions)
  • Added in-code documentation and a release-note entry

@pmd-actions-helper

pmd-actions-helper Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-10-22 16:12:50+00:00 for c0cc7b6)

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR!
I've updated it to have the regression tester run again (because last time it reported many removed violations - but that's probably unrelated). I hope, I find more time later this week.

adangel and others added 2 commits October 22, 2026 17:04
Method references never went through TypeOps#getMemberSource, so the
IllegalArgumentException fixed by the previous commit was still
reachable through `values::forEach`. There an intersection bound
isn't even needed: capture() is the identity on a bare type variable
too, so a receiver typed `C extends Iterable<?>` streamed the
uncaptured upper bound, substituted `T := ?` into
`forEach(Consumer<? super T>)` and tripped the check in
TypeSystem#wildcard.

Route both candidate-search sites in ExprOps through getMemberSource.
In getAccessibleCandidates the capture happens only after the
constructor-ref and raw second-search branches have been selected,
so those paths, which need the declared type, are unchanged.

Co-Authored-By: Claude Opus 5 <[email protected]>
@adangel adangel added this to the 7.29.0 milestone Oct 22, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

For method refs, we had a similar problem. This is now also fixed.

@adangel
adangel merged commit 4563b6a into pmd:main Oct 23, 2026
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.

[java] IllegalArgumentException with wildcard in the upper bound of a type variable, in a lambda or method reference

2 participants