Skip to content

Java: when two overloads take the call's argument count, the call links to the first overload of any arity #3040

Open

Description

@inth3shadows

What happens

A Java call to an overloaded method normally links to the overload whose parameter count fits. But when two or more overloads share the call's argument count, the call falls back to the first overload declared, even one with a different number of parameters.

// src/p/Util.java
package p;

import java.io.File;
import java.nio.file.Path;

public class Util {
    public static String load(File f, String c, String b) { return b; }                // line 7
    public static String load(File f, String c, String b, Parser p) { return b; }      // line 8
    public static String load(Path f, String c, String b, Parser p) { return b; }      // line 9
}

// src/p/Parser.java
package p;
public class Parser {
}

// src/p/App.java
package p;

import java.io.File;

public class App {
    String run(File file, String c, String b, Parser p) {
        return Util.load(file, c, b, p);                                              // 4 arguments
    }
}
overloads present run → load on main expected
lines 7 and 8 only line 8 ✓ line 8
lines 7, 8 and 9 (above) line 7, which takes 3 parameters ✗ line 8 (or 8 and 9, if parameter types aren't compared)

The argument count alone rules out line 7. The fallback should at least stay among the overloads that take the call's argument count.

Where it shows up

jsoup's Jsoup.parse(File, String, String, Parser) calls DataUtil.load(file, charsetName, baseUri, parser). DataUtil has six load overloads, three parameters and four parameters each for File, Path and InputStream. The call links to load(File, String, String) at line 68, not to load(File, String, String, Parser) at line 86. Calls of this shape came up repeatedly when I diffed jsoup's call edges against scip-java (the Java compiler's references).

Environment: Linux (WSL2), Node 22.23.2. Reproduced on main at 60b0d7a both with CODEGRAPH_KERNEL=0 and with the native kernel.

Related: argument-count overload selection landed for C++ (#2541), Swift (#2238) and Solidity (#2241). #2820 covers which overrides an overload links to, not which overload a call picks.


Edited 2026-10-29: replaced "accounts for most of the disagreements" with "came up repeatedly". I checked samples of the disagreements, not all of them. Nothing else changed.

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions