Repository navigation
[java] Fix #7146, #7149: Loose invocation conversion and the most specific check for boxing - #7163
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.
Contributor
|
Compared to main: (comment created at 2026-10-27 08:22:15+00:00 for a9859de) |
Merged
3 of 4 tasks
3 of 4 tasks
saryn17
added a commit
to saryn17/pmd
that referenced
this pull request
Nov 9, 2026
…pmd#7149 Since pmd#7163, the test case "Other arguments whose type is inferred or unknown" reports t(value.intValue(), Lib.count() + 1) with t(int, long) and t(Integer, Long). Lib is not on the auxclasspath and Lib.count() + 1 is typed as int, which cannot be passed to t(Integer, Long). The overload resolution selects t(int, long) with and without intValue(). Before pmd#7163 it accepted int for the Long parameter, so the call without intValue() was ambiguous and the unboxing was not reported.
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 a loose invocation context PMD boxed both sides and, when that failed, unboxed both sides. So
intwas convertible toLongthroughlong, andnulltointthroughInteger(#7149). JLS 5.3 allows boxing followed by a widening reference conversion, and unboxing followed by a widening primitive conversion.Infer.isConvertiblenow handles these conversions without boxing or unboxing both sides.The most specific method check used the same loose convertibility and had a special case for boxed parameters from #4557, so
d(int, int)beatd(long, Integer)for anIntegerand an int literal, where javac reports an ambiguity (#7146). JLS 15.12.2.5 does not use boxing or unboxing for this comparison. The check now uses the strict conversion path, which leaves boxing and unboxing out. The special case is gone. Applicability decides the case of #4557 instead, becauseassertThat(Long)does not accept anint.Without the first change,
h(b ? 1 : null)withh(int)resolved only becausenullwas accepted forint. javac types that conditional asIntegerand unboxes it, so a conditional with a null literal and a numeric or boolean branch is now typed as the box of that branch. The branches of a standalone conditional are then checked against their own types instead of the parameter type.Added the samples from both issues and the conditional case, with an
intand with abooleanbranch. Another test keepsf(1)applicable tof(Number), which is boxing followed by a widening reference conversion. One existing test expected an ambiguity forassertThat(1)withDoubleandLongoverloads. javac reports no applicable method there, and the test now expects that.Ran
./mvnw -B -ntp -pl pmd-java verifywith Java 21. The new tests fail onmain, except the one forf(Number).Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)