Skip to content

[java] Fix #7146, #7149: Loose invocation conversion and the most specific check for boxing - #7163

Merged
adangel merged 2 commits into
pmd:mainfrom
saryn17:fix-7149
Oct 27, 2026
Merged

adangel merged 2 commits into
pmd:mainfrom
saryn17:fix-7149

Conversation

@saryn17

@saryn17 saryn17 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Describe the PR

In a loose invocation context PMD boxed both sides and, when that failed, unboxed both sides. So int was convertible to Long through long, and null to int through Integer (#7149). JLS 5.3 allows boxing followed by a widening reference conversion, and unboxing followed by a widening primitive conversion. Infer.isConvertible now 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) beat d(long, Integer) for an Integer and 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, because assertThat(Long) does not accept an int.

Without the first change, h(b ? 1 : null) with h(int) resolved only because null was accepted for int. javac types that conditional as Integer and 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 int and with a boolean branch. Another test keeps f(1) applicable to f(Number), which is boxing followed by a widening reference conversion. One existing test expected an ambiguity for assertThat(1) with Double and Long overloads. javac reports no applicable method there, and the test now expects that.

Ran ./mvnw -B -ntp -pl pmd-java verify with Java 21. The new tests fail on main, except the one for f(Number).

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.
@pmd-actions-helper

pmd-actions-helper Bot commented Oct 7, 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 08:22:15+00:00 for a9859de)

@adangel adangel changed the title [java] Fix loose invocation conversion and the most specific check for boxing [java] Fix #7146, #7149: Loose invocation conversion and the most specific check for boxing Oct 26, 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 d26fe78 into pmd:main Oct 27, 2026
13 checks passed
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.
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

2 participants