Skip to content

JAVA-7401 Rank library overloads to pick the most specific method - #6578

Open
rombirli wants to merge 2 commits into
rombirli/semsitter-lambda-inferencefrom
rombirli/semsitter-overload-ranking
Open

rombirli wants to merge 2 commits into
rombirli/semsitter-lambda-inferencefrom
rombirli/semsitter-overload-ranking

Conversation

@rombirli

@rombirli rombirli commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Part 11 of 17 of JAVA-7392. Ticket: JAVA-7401.

Intent

Replace the OverloadRanking stub. When the server leaves a library call open between overloads, the translator now applies the JLS phases (strict, loose with boxing, variable arity) and keeps the most specific candidate.

flowchart LR
  C[candidates] --> P1{phase 1: subtyping}
  P1 -->|none| P2{phase 2: boxing}
  P2 -->|none| P3{phase 3: varargs}
  P1 & P2 & P3 --> M[most specific]
Loading
String.valueOf(1)          // valueOf(int), applicable without boxing, beats valueOf(Object)
String.valueOf(chars)      // valueOf(char[]) is more specific than valueOf(Object)
log(msg, () -> value)      // the overload taking the function the argument is

Source calls are already resolved by the server, so the replay goldens barely change. The unit tests carry this PR.

Test plan

  • OverloadRankingTest: candidate competition, phases, function arguments, variable arity

@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

JAVA-7401

@rombirli
rombirli added this pull request to stack #6570 November 25, 2026 11:48
@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch from 145c4a2 to 89310b7 Compare November 25, 2026 12:00
@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch 2 times, most recently from c37c80a to b873a40 Compare November 25, 2026 13:08
@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch from b873a40 to 01f8a91 Compare November 25, 2026 13:41
@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch from 01f8a91 to 43fd47a Compare November 25, 2026 14:14
@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch 2 times, most recently from 834313e to d5e33f2 Compare November 25, 2026 15:32
@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch from d5e33f2 to 96071b2 Compare November 25, 2026 16:31
@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch from 96071b2 to 968c938 Compare November 25, 2026 17:16
@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch 2 times, most recently from 7eaebf7 to 7641997 Compare November 25, 2026 20:45
@rombirli
rombirli removed this pull request from stack #6570 November 25, 2026 20:47
@rombirli
rombirli added this pull request to stack #6589 November 25, 2026 20:48
@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch from 7641997 to c318ac5 Compare November 25, 2026 20:57
@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch 2 times, most recently from 8229122 to f404fff Compare November 25, 2026 21:28
@datadog-sonarsource

This comment has been minimized.

@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch from f404fff to b332ab1 Compare November 27, 2026 07:55
@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch from b332ab1 to 1d2d2d4 Compare November 27, 2026 08:29
@rombirli
rombirli force-pushed the rombirli/semsitter-overload-ranking branch from 1d2d2d4 to 477b5ac Compare November 27, 2026 09:24
@gitar-bot

gitar-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown
CI failed: The Ruling Update and Notify job failed because it could not download actual_* artifacts, since the prerequisite Ruling QA job was cancelled. The log does not show why Ruling QA was cancelled, and the finding is not attributed to this PR's overload-ranking change.

Overview

The Ruling Update and Notify job failed because it could not download actual_* artifacts, since the prerequisite Ruling QA job was cancelled. The log does not show why Ruling QA was cancelled, and the finding is not attributed to this PR's overload-ranking change.

Failures

Ruling Update and Notify failed to download actual_* artifacts after Ruling QA was cancelled (job 140420034075, not related to PR)

  • Type: infrastructure
  • Affected jobs: 140420034075
  • Related to change: no
  • Confidence: high
  • Root cause: The log shows needs.ruling-qa.result was cancelled, but the workflow set RULING_FAILED=true for any non-success result and proceeded to sync ruling artifacts. The download step then failed because no artifacts were present. The log does not explain why Ruling QA was cancelled; the supplied changes concern Java overload ranking and do not explain this downstream artifact-sync failure.
  • Suggested fix: Rerun the cancelled Ruling QA job so it can produce the expected artifacts. Update the workflow to handle cancelled separately from failure and skip artifact syncing when no artifacts could have been produced.

Summary

  • Change-related failures: 0
  • Infrastructure/flaky failures: 1
  • Recommended action: Rerun the cancelled Ruling QA job so it can produce the expected artifacts. Update the workflow to handle cancelled separately from failure and skip artifact syncing when no artifacts could have been produced.
Code Review ✅ Approved 🟡 Medium risk 3 closed / 3 findings

Ranking library overloads changes which methods library calls bind to, potentially altering downstream semantic analysis and reported findings.

Replaces the OverloadRanking stub so library calls left open between overloads are resolved by JLS phases (strict, loose with boxing, variable arity) and the most specific candidate is kept. Resolved the varargs ranking issue that dropped the implicit <init> from class members. Two other findings were closed without a fix in this PR, and no open issues remain.

✅ 3 closed
✅ Bug: Varargs ranking drops the implicit <init> from class members

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/OverloadRanking.java:66 📄 java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/Overloads.expected.txt:16 📄 java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/CastContexts.expected.txt:115
The new mostSpecificVarArgs calls owner.lookupSymbols(chosen.name()) while the call is still being translated. When the owner is declared in the current file, membersNamed falls back to memberSymbols(), which builds the members list once and caches it. addImplicitConstructor adds the default constructor only if sema.resolve(id + "#<init>()") is already known, and early in translation it is not.

So a varargs call into a class of the same file, such as call("a", 1, 2) in Overloads or varargs(...) in CastContexts, caches a member list without the default constructor. The updated goldens show the regression: <init> disappears from members [...] for both classes. The addImplicitConstructor doc comment says such a class "still has the default one as a member". The goldens were updated to accept the regression instead of catching it.

Fix: avoid filling the memberSymbols() cache during translation. For example, collect candidates without caching for in-document types, or skip in-document owners, since the server already resolves source calls. Then restore the <init> entries in both goldens.

Closed: Bug: Overload swap in bindLibraryOrOverload leaves the use on the old method

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/InvocationTranslator.java:132-135 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/InvocationTranslator.java:143-148 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/OverloadRanking.java:36-44
Before this PR preferOverload always returned current, so the bind(...) at InvocationTranslator.java:134 never changed anything. Now it can return candidate. bindUse has already recorded this identifier under the server-bound method's id (sema.use(usageId(symbol, target), identifier)), and only the symbol gets rebound. The result is that bound.usages() still lists an identifier that no longer refers to it, and candidate.usages() doesn't list it. The varargs refinement a few lines below (line 144-147) handles the same situation by calling sema.moveUse(chosen.id, specific.id, identifier) before it rebinds. The fix is to do the same here whenever the preferred overload differs from bound.

Closed: Bug: Ranking can pick an overload that cannot accept the lambda argument

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/OverloadRanking.java:40-43 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/OverloadRanking.java:113-116 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/OverloadRanking.java:125-137
preferOverload applies the function-argument check in one direction only. It switches to candidate when current takes no function and candidate does. In the reverse case, where current accepts the lambda or method reference and candidate takes a plain class there (so takesNoFunction(candidate) is true), it falls through to byPhase.

Lambda and method-reference arguments are typed Type.UNKNOWN at this point (the test at line 82 relies on this), and convertible treats unknown arguments as applicable. So both overloads pass phase 0, and moreSpecific decides. Take foo(Runnable) versus foo(Thread) called with () -> {}: Thread is a subtype of Runnable, so the call is rebound to foo(Thread). That overload cannot take a lambda at all.

MemberLookup.declaredMethod avoids this by filtering out every candidate where takesNoFunction is true. preferOverload should do the same and keep current when candidate takes no function at a function position.

Verification Rules 3/3 met Repos 1 inspected · PRs 3 inspected

Functional validation No results

Rules 3 passed 2 not applicable

✅ Internal artifact registries
✅ Release secrets usage
✅ Repository license compliance

Broader impact Repos 1 inspected PRs 3 inspected
Cross-repo 1 repository inspected 0 open impacts

SonarSource/sonarlint-eclipse Inspected

Cross-PR 3 PRs inspected 0 open impacts

PR 6577: JAVA-7400 Infer lambda and method reference types from their target Inspected
PR 6580: JAVA-7403 Derive unused import and no-effect assignment warnings Inspected
PR 6581: JAVA-7404 Derive redundant cast and unreachable catch warnings Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Tip

Comment Gitar fix CI or enable auto-apply: gitar auto-apply:on

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@sonarqube-next

Copy link
Copy Markdown
Contributor

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

Development

Successfully merging this pull request may close these issues.

1 participant