Skip to content

[java] Fix #7148: Allow boxing of lambda and method reference results in the strict phase - #7164

Merged
adangel merged 4 commits into
pmd:mainfrom
saryn17:fix-7148
Oct 27, 2026
Merged

adangel merged 4 commits into
pmd:mainfrom
saryn17:fix-7148

Conversation

@saryn17

@saryn17 saryn17 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Describe the PR

In the strict phase, the result of a lambda body and the return type of a method reference were checked without boxing. () -> 1 did not fit Supplier<Integer>, and LambdaResult::len with an int return did not fit Function<String, Integer>. The calls ended in an ambiguity, or picked the other overload when an implicitly typed lambda hid the ambiguity. JLS 15.27.3 and 15.13.2 check these results in an assignment context, where boxing is allowed. ExprCheckHelper now checks them with a checker that allows boxing and unboxing in every phase.

This builds on #7163, which is the first commit here. Without that fix, the checker would also apply the wrong conversions of #7149 to lambda results in the strict phase. int would convert to Long and null to int, and h(() -> 1, i) would pick h(Supplier<Long>, int) over h(Supplier<Integer>, Integer). Two cases cover that.

Added tests for the three calls from the issue and the two interaction cases above. Two more cover the unboxing direction: m(() -> value, i) with an Integer value selects m(IntSupplier, int), and f(i, LambdaResult::len) with an Integer return type selects f(int, ToIntFunction<String>).

Ran ./mvnw -B -ntp -pl pmd-java verify with Java 21. The tests for the three calls fail on main.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

…r boxing

Infer.isConvertible boxed both sides and, when that failed, unboxed
both sides, so int was convertible to Long and null to int. JLS 5.3
allows boxing followed by widening reference conversion and unboxing
followed by widening primitive conversion. The most specific check in
PhaseOverloadSet compared parameter types with the same loose
convertibility and had a special case for boxed parameters. JLS
15.12.2.5 does not use boxing or unboxing for this comparison, so the
check now uses the strict conversion path and the special case is
removed.

A conditional with a null literal and a numeric or boolean branch is
typed as the box of that branch, as javac does, so h(b ? 1 : null)
still resolves to h(int). The branches of a standalone conditional
are checked against their own standalone types.
…ict phase

In the strict phase, the result expressions of a lambda and the return
type of a method reference were checked with the checker of the phase,
which does not box. JLS 15.27.3 and 15.13.2 check them in an assignment
context, where boxing is allowed. ExprCheckHelper now uses a second
checker for these results that allows boxing and unboxing in every
phase.
@pmd-actions-helper

pmd-actions-helper Bot commented Oct 23, 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-27 09:10:30+00:00 for 58b97b5)

@adangel adangel changed the title [java] Allow boxing of lambda and method reference results in the strict phase [java] Fix #7148: Allow boxing of lambda and method reference results in the strict phase Oct 27, 2026
@adangel adangel added the in:type-resolution Affects the type resolution code label Oct 27, 2026
@adangel adangel added this to the 7.29.0 milestone Oct 27, 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!

@adangel
adangel merged commit 785a6a0 into pmd:main Oct 27, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in:type-resolution Affects the type resolution code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] Strict phase rejects lambda and method reference results that need boxing

2 participants