GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Loose invocation conversion accepts int to Long and null to int · Issue #7149 · pmd/pmd

[java] Loose invocation conversion accepts int to Long and null to int #7149

New issue
Copy link
New issue
Copy link
Closed
#7163
Closed
[java] Loose invocation conversion accepts int to Long and null to int#7149
#7163
Copy link
Labels
in:type-resolutionAffects the type resolution code
Milestone
7.29.0

Description

@saryn17
saryn17
opened on Oct 6, 2026
Issue body actions

Affects PMD Version: 7.28.0 (also 7.29.0-SNAPSHOT)

Description:

With f(int, Long) and f(long, long), javac compiles f(value, 2) for an Integer value to f(long, long). PMD resolves it to f(int, Long). For PMD both methods are applicable and f(int, Long) wins as more specific. g(1) for a single g(Long) resolves to g(Long) and h(null) for h(int) to h(int), with getOverloadSelectionInfo().isFailed() false, where javac reports incompatible types: int cannot be converted to Long and incompatible types: <null> cannot be converted to int.

When one side is primitive, Infer.isConvertible boxes both sides first and, when that fails, unboxes both sides (line 1010):

pmd/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/Infer.java

Lines 998 to 1015 in 5c9271c

static Convertibility isConvertible(JTypeMirror exprType, JTypeMirror formalType, boolean canBox) {
if (exprType == formalType) {
// fast path
return Convertibility.SUBTYPING;
}
if (canBox && exprType.isPrimitive() ^ formalType.isPrimitive()) {
// then boxing conversions may be useful
Convertibility result = TypeOps.isConvertible(exprType.box(), formalType.box());
if (!result.never()) {
return result;
} else {
return TypeOps.isConvertible(exprType.unbox(), formalType.unbox());
}
}
return TypeOps.isConvertible(exprType, formalType);
}

For a primitive argument the second step accepts int for Long through the widening int to long, and int for Double through int to double. JLS 5.3 allows a boxing conversion followed by a widening reference conversion, not a widening primitive conversion followed by boxing. The first step accepts the null type for a primitive parameter, because null converts to Integer.

The test "Two overloads with boxed types, widening required, ambiguous" in OverloadResolutionTest.kt (lines 343-367) expects an ambiguity for assertThat(1) with assertThat(Double) and assertThat(Long). javac reports no suitable method found for assertThat(int), because neither method is applicable. Above it, the test for #4557 (lines 304-341) describes int -> Long as widening and then boxing.

Found while working on #7136.

Code Sample demonstrating the issue:

class Loose {
    static void f(int x, Long y) { }
    static void f(long x, long y) { }

    void t(Integer value) {
        f(value, 2);
    }
}

Steps to reproduce:

  1. javac Loose.java, also with --release 8, then javap -c -p Loose: the call is f:(JJ)V.
  2. Run PMD on Loose.java with the XPath rule //MethodCall[pmd-java:matchesSig("Loose#f(int,java.lang.Long)")]. It reports line 6.

Running PMD through: Other (PmdAnalysis API)

Activity

  1. saryn17
    added a commit that references this issue on Oct 6, 2026

    [java] UnnecessaryBoxing: rerun the overload resolution without the u…

    15327e0
  2. adangel
    added
    in:type-resolutionAffects the type resolution code
    on Oct 27, 2026
  3. adangel
    added this to the 7.29.0 milestone on Oct 27, 2026
  4. adangel
    closed this as completedin #7163on Oct 27, 2026
  5. adangel
    added 2 commits that reference this issue on Oct 27, 2026

    [java] Fix #7146, #7149: Loose invocation conversion and the most spe…

    d26fe78

    Enable test cases (pmd#7146, pmd#7149)

    6727f3a
  6. saryn17
    added a commit that references this issue on Nov 9, 2026

    [java] UnnecessaryBoxing: update the expected reports after the fix of …

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    in:type-resolutionAffects the type resolution code

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.29.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.