Skip to content

[java] Fix #7134, #7135: UnnecessaryBoxing: check whether another overload is applicable without the unboxing - #7136

Draft
saryn17 wants to merge 17 commits into
pmd:mainfrom
saryn17:unnecessaryboxing-overload-fn
Draft

saryn17 wants to merge 17 commits into
pmd:mainfrom
saryn17:unnecessaryboxing-overload-fn

Conversation

@saryn17

@saryn17 saryn17 commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

The overload check from #5971 looks only at the type that other overloads have at the converted argument. A reference type there always suppresses the warning, even if the boxed argument cannot be passed to it (#7134). A primitive type never does, even if that overload would be selected without the conversion (#7135).

This PR reruns the overload resolution of the call (Infer.getCompileTimeDecl) with the boxed expression in place of the unboxing call, with mirrors built as in UseDiamondOperatorRule. The rule reports the unboxing unless another method is selected or the call becomes ambiguous. It also reruns the call as written, because the result stored on the AST can lose the ambiguity flag of the overload selection. When that rerun fails, the rule has no selected method to compare with and stays silent.

In October, the rule reported 1,793 violations on the JDK sources and main reported 1,773. The earlier version of this PR reported the same 1,793 (my earlier comment counted 1,808 output lines, which include 15 lines of error output for 3 files that PMD could not process). I compiled the 24 calls that differ from main with and without the conversion. In 22 of them the conversion can be removed and main does not report it. The other 2 are in BCEL's PUSH(ConstantPoolGen, Boolean) and PUSH(ConstantPoolGen, Character), which main reports, where this(cp, value.booleanValue()) and this(cp, value.charValue()) would call their own constructor without the conversion.

Where PMD's overload resolution differs from javac, the rule follows PMD. Ten such differences are filed as #7144, #7145, #7146, #7147, #7148, #7149, #7150, #7151, #7152 and #7313.

Tests

Twenty cases are added to UnnecessaryBoxing.xml. Each expected violation was checked by compiling the test class with and without the conversion and comparing the invoked method descriptors shown by javap. The block that imports org.example.Lib and org.example.Task was compiled against stub classes. In the PMD test these types stay unresolved, and the lines that use them expect no report, except t(value.intValue(), Lib.count() + 1). PMD types Lib.count() + 1 as int, and since #7163 it selects t(int, long) with and without intValue(), as javac does when Lib.count() returns int. If Lib.count() returns long, the call without intValue() does not compile.

Two cases whose expected result depends on a difference between PMD's resolution and javac are disabled until #7147 and #7152 are fixed. Seven more were enabled after #7144, #7145, #7146, #7149, #7150, #7151 and #7313 were fixed. Two loop conditions of the #7144 case, (boolean) true and ON == true, moved to a new case for #7313. The protected constructor case uses a helper class in src/test/java-resources, as in #6993. Thirteen of the eighteen enabled cases fail with the rule as on main (b27f97a).

At 15327e0 I ran the commands of the CI jobs on Ubuntu 24.04 (WSL) with JDK 21 and the extra homes for 8, 11, 17 and 25.

  • UnnecessaryBoxingTest: 60 tests, 0 failures, 8 disabled
  • verify-unittests: 19,932 tests, 0 failures
  • verify: BUILD SUCCESS, 0 Checkstyle violations
  • dogfood: BUILD SUCCESS, 0 violations
  • regressiontester: 0 changed violations, 0 new violations

At 14cedf4 I ran verify for pmd-java on Windows 11 with JDK 21. It passed with 10,838 tests and 0 Checkstyle violations. UnnecessaryBoxingTest has 61 tests, 0 failures and 2 disabled.

docs/pages/release_notes.md lists #7134 and #7135 under Fixed Issues.

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)

…er overload is applicable without the unboxing
@pmd-actions-helper

pmd-actions-helper Bot commented Oct 2, 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, 16 errors and 9 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-11-16 12:24:45+00:00 for 14cedf4)

A protected method of a class in another package can be called from
classes nested in a subclass, but only on an expression of that
subclass type (JLS 6.6.2.1). A protected constructor of such a class
is not a candidate for a plain new (JLS 6.6.2.2).
@UncleOwen

Copy link
Copy Markdown
Member

That's a lot of code. I haven't looked at it in detail, just skimmed it to get a high-level overview.

There's a call to our existing overload resolution code in line 251. And the next almost-500-lines is another overload resolution algorithm. Is there any way to use the existing code for the what-if-we-remove-this-boxing-call-case?

Failing that: You mentioned that UnnecessaryVarargsArrayCreation does something similar. Is there some code that can be shared?

@saryn17

saryn17 commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@UncleOwen I tried both.

Rerunning the inference with value instead of value.intValue(), like UseDiamondOperatorRule does, is 23 lines instead of ~500. On the JDK sources it reports the same 1,808 lines as this PR. On the 3,477 javac-checked calls I collected while working on this, most of them constructed corner cases, it is wrong on 242 where this PR is wrong on 106. 126 of those are false positives that main doesn't have.

Most of them come from the resolver itself. For example, () -> { while (true) { } } isn't treated as value-compatible (LambdaMirrorImpl.java:141-148), and d(value, 5) with d(int, int) and d(long, Integer) resolves although javac reports it as ambiguous (PhaseOverloadSet.java:168-186). I can open issues for these.

Sharing with UnnecessaryVarargsArrayCreation works. With the overload code moved to a helper under rule.internal, UnnecessaryBoxing gives the same results. UnnecessaryVarargsArrayCreation on top of the helper goes from 24 false positives and 7 false negatives to 1 and 1 on 51 calls I checked with javac, and 2 of its 99 reports on the JDK change.

I haven't changed the PR yet. I'd move the current logic into the helper here and switch UnnecessaryVarargsArrayCreation in a separate PR. If you'd rather have the inference version, I can do that instead.

@UncleOwen

Copy link
Copy Markdown
Member

In that case, please use the existing overload resolution code. If there are bugs in there, please file additional issues/PRs.

@oowekyala

Copy link
Copy Markdown
Member

In that case, please use the existing overload resolution code. If there are bugs in there, please file additional issues/PRs.

+1 ---depending on the core type resolution allows us to expose and fix bugs with it and improve all rules that use it at once.

…nboxing

The rule now reruns PMD's overload resolution on the call with the
boxed expression in place of the unboxing call. The mirrors are built
as in UseDiamondOperatorRule. The unboxing is reported unless another
method is selected or the call becomes ambiguous. This replaces the
applicability check written for this rule.

Eight of the new test cases are disabled until pmd#7144, pmd#7145, pmd#7146,
pmd#7147, pmd#7149, pmd#7150, pmd#7151 and pmd#7152 are fixed. Their expected results
follow javac where PMD's overload resolution differs from it.
@saryn17

saryn17 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

@UncleOwen @oowekyala The rule now reruns the existing overload resolution with the boxed expression in place of the unboxing call and compares the selected methods. UnnecessaryBoxingRule.java is +24/-66 against main. I filed #7144, #7145, #7146, #7147, #7148, #7149, #7150, #7151 and #7152 for differences from javac that this exposed. Eight of them have a test case in UnnecessaryBoxing.xml, disabled until the issue is fixed.

@saryn17

saryn17 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@UncleOwen could you take another look at this? It now uses the existing overload resolution as you suggested.

The test for #7145 is enabled now that #7158 is merged. The fixes for #7146, #7149, #7150 and #7151 are in #7161, #7162 and #7163. Once those are merged, I can enable the corresponding tests here.

@adangel

adangel commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

So, if I understand this PR correctly, this PR depends on:

I'll put this PR then in draft mode.

@adangel
adangel marked this pull request as draft October 26, 2026 14:33
adangel and others added 3 commits October 27, 2026 10:19
…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.
@saryn17

saryn17 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

The unit tests failed on "Other arguments whose type is inferred or unknown" (12 problems instead of 11) after #7163 was merged. The extra report is t(value.intValue(), Lib.count() + 1) with t(int, long) and t(Integer, Long). Lib is unresolved, and PMD types Lib.count() + 1 as int. Before #7163, the overload resolution accepted int for the Long parameter (#7149), so the call without intValue() was ambiguous. Now it selects t(int, long) with and without intValue(), as javac does when Lib.count() returns int. If Lib.count() returns long, the call without intValue() does not compile, so this report depends on PMD typing Lib.count() + 1 as int.

6124cdd updates the expected lines. I also merged main to resolve the conflict in the release notes, and updated the description for the disabled cases and this line.

pmd#7144 was fixed by pmd#7218. The loop conditions (boolean) true and
ON == true move to a new disabled case, because ConstantFolder does
not fold them (pmd#7313).
@saryn17

saryn17 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Merged main (6365759) and enabled the #7144 case in 8647b35.

The two calls with (boolean) true and ON == true are now in a separate disabled test for #7313. #7314 fixes the constant folding issue.

#7299 fixes #7152, and #7315 builds on #7299 to fix #7147.

@saryn17

saryn17 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Merged main (1b84182) and enabled the #7313 case in 14cedf4.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants