Repository navigation
[java] Fix #7148: Allow boxing of lambda and method reference results in the strict phase - #7164
Merged
Merged
Conversation
…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.
3 of 4 tasks
Contributor
|
Compared to main: (comment created at 2026-10-27 09:10:30+00:00 for 58b97b5) |
3 of 4 tasks
adangel
reviewed
Oct 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
() -> 1did not fitSupplier<Integer>, andLambdaResult::lenwith anintreturn did not fitFunction<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.ExprCheckHelpernow 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.
intwould convert toLongandnulltoint, andh(() -> 1, i)would pickh(Supplier<Long>, int)overh(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 anInteger valueselectsm(IntSupplier, int), andf(i, LambdaResult::len)with anIntegerreturn type selectsf(int, ToIntFunction<String>).Ran
./mvnw -B -ntp -pl pmd-java verifywith Java 21. The tests for the three calls fail onmain.Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)