Skip to content

JAVA-7400 Infer lambda and method reference types from their target - #6577

Open
rombirli wants to merge 2 commits into
rombirli/semsitter-return-inferencefrom
rombirli/semsitter-lambda-inference
Open

rombirli wants to merge 2 commits into
rombirli/semsitter-return-inferencefrom
rombirli/semsitter-lambda-inference

Conversation

@rombirli

@rombirli rombirli commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Part 10 of 17 of JAVA-7392. Ticket: JAVA-7400.

Intent

Replace the remaining FunctionalInference stubs: lambdas and method references take their parameter and result types from the functional interface they are passed as, and a generic call takes its result from that.

flowchart LR
  T[target: Function<T,R>] --> P[lambda parameter types]
  B[lambda body type] --> R[R]
  R --> C[type of the generic call]
Loading

Before and after, from Lambdas.expected.txt (list.stream().map(x -> x + 1).filter(...).collect(toList())):

-call Stream.map ... returns java.util.stream.Stream<R erasure java.lang.Object>
+call Stream.map ... returns java.util.stream.Stream<java.lang.Integer>
-call Stream.collect ... returns java.lang.Object
+call Stream.collect ... returns java.util.List<java.lang.Integer>

Test plan

  • FunctionalInferenceTest for function results, unbound references and targeted parameters
  • Replay goldens updated

@hashicorp-vault-sonar-prod

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

Copy link
Copy Markdown
Contributor

JAVA-7400

@rombirli
rombirli added this pull request to stack #6570 November 25, 2026 11:48
@rombirli
rombirli force-pushed the rombirli/semsitter-lambda-inference branch 2 times, most recently from fa3513c to bde0124 Compare November 25, 2026 12:03
@rombirli
rombirli force-pushed the rombirli/semsitter-lambda-inference branch from bde0124 to 219e845 Compare November 25, 2026 13:08
@rombirli
rombirli force-pushed the rombirli/semsitter-lambda-inference branch from 219e845 to 36dbaa5 Compare November 25, 2026 13:41
@datadog-sonarsource

This comment has been minimized.

@rombirli
rombirli force-pushed the rombirli/semsitter-lambda-inference branch 2 times, most recently from 45365f0 to fc12994 Compare November 25, 2026 14:55
@rombirli
rombirli force-pushed the rombirli/semsitter-lambda-inference branch from fc12994 to b812cb6 Compare November 25, 2026 15:32
@rombirli
rombirli force-pushed the rombirli/semsitter-lambda-inference branch 2 times, most recently from 9324e86 to cf059b4 Compare November 25, 2026 17:16
@rombirli
rombirli force-pushed the rombirli/semsitter-lambda-inference branch from cf059b4 to da91488 Compare November 25, 2026 18:03
@rombirli
rombirli force-pushed the rombirli/semsitter-lambda-inference branch from da91488 to 2395568 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-lambda-inference branch 2 times, most recently from 302210a to e469184 Compare November 25, 2026 21:26
@rombirli
rombirli force-pushed the rombirli/semsitter-lambda-inference branch from e469184 to e001a0a Compare November 25, 2026 21:28
@rombirli

rombirli commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

gitar collect at GenericEdges:84 is still typed Map<String,Integer>, see resolved thread

@gitar-bot

gitar-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

gitar collect at GenericEdges:84 is still typed Map<String,Integer>, see resolved thread

No push was confirmed in this run. Check the branch before relying on the requested changes.

@rombirli
rombirli force-pushed the rombirli/semsitter-lambda-inference branch 2 times, most recently from f47e2b7 to dc375a7 Compare November 27, 2026 08:29
@rombirli
rombirli force-pushed the rombirli/semsitter-lambda-inference branch from dc375a7 to 731a299 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 no actual_* ruling artifacts could be downloaded, most likely as a downstream effect of an upstream ruling QA failure. The log does not identify the root cause of that QA failure, so its relationship to this PR's lambda and method-reference inference change remains unclear.

Overview

The Ruling Update and Notify job failed because no actual_* ruling artifacts could be downloaded, most likely as a downstream effect of an upstream ruling QA failure. The log does not identify the root cause of that QA failure, so its relationship to this PR's lambda and method-reference inference change remains unclear.

Failures

Ruling Update and Notify failed: no actual_* ruling artifacts downloaded (likely downstream of ruling QA failure)

  • Type: infrastructure
  • Affected jobs: 140420079775
  • Related to change: unclear
  • Confidence: high
  • Root cause: Artifact synchronization reports that no actual_* artifacts were downloaded; the log lists missing uploads, expiration or availability, and insufficient permissions as possible causes. Since the job also records RULING_FAILED=true, the upstream ruling QA failure may have prevented artifact creation or upload, but this log does not identify why ruling QA failed. The PR changes lambda and method-reference type inference, which could affect ruling results, but no upstream failure details connect a concrete change to this missing-artifact error, so the relationship is unclear.
  • Suggested fix: Inspect the failed ruling QA matrix jobs and address their underlying failure first, then rerun the workflow. Confirm those jobs upload artifacts matching actual_* and that the notify job has access to them; if ruling QA succeeds but artifacts are still missing, check the artifact name pattern, availability, and permissions.

Summary

  • Change-related failures: 0
  • Infrastructure/flaky failures: 1
  • Recommended action: Inspect the failed ruling QA matrix jobs and address their underlying failure first, then rerun the workflow. Confirm those jobs upload artifacts matching actual_* and that the notify job has access to them; if ruling QA succeeds but artifacts are still missing, check the artifact name pattern, availability, and permissions.
Code Review ✅ Approved 🟡 Medium risk 1 closed / 1 findings

Target-based lambda and method-reference inference changes generic expression types, which could alter downstream Java analysis results.

Lambda and method reference types are now inferred from their functional interface target, and generic calls take their result type from that target. One open finding needs attention before merge: toMap/collect in GenericEdges.java:84 now infers Map<!capture! erasure java.lang.Object, java.lang.Integer> instead of Map<java.lang.String,java.lang.Integer>, because K binds to the capture from Function.identity() and target typing no longer applies. The updated GenericEdges.expected.txt golden locks in this regression and must be restored once FunctionalInference stops binding variables from Object-typed captures or lets the target type win.

✅ 1 closed
Closed: Bug: toMap/collect now typed Map<capture,Integer> instead of Map<String,Integer>

📄 java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericEdges.expected.txt:1312 📄 java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericEdges.expected.txt:1327 📄 java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericEdges.expected.txt:1331 📄 java-frontend-semsitter/src/test/resources/org/sonar/java/semsitter/replay/GenericEdges.expected.txt:1337 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/FunctionalInference.java:47-60
In GenericEdges.java:84 (Map<String, Integer> counts = strings.stream().collect(Collectors.toMap(Function.identity(), String::length, Integer::sum))), the collect call used to get the correct type java.util.Map<java.lang.String,java.lang.Integer> from its target. With this PR, functionProducing gives String::length the type Function<?, Integer>. That is enough for toMap to infer its type variables, but K gets bound to the !capture! erasure Object that Function.identity() is already typed with. The wrong Collector<capture, capture, Map<capture, Integer>> then becomes the result of collect, so target typing no longer applies. The expression now has type Map<!capture! erasure java.lang.Object, java.lang.Integer> when javac gives Map<String,Integer>, and the updated golden locks this regression in. Two ways to fix it: refuse to bind a variable from a capture whose ground type is Object, or let the target type win when the inferred call result still contains an unresolved capture. Then restore the golden line.

Verification Rules 2/3 met Repos 1 inspected · PRs 3 inspected
Rules 1 failed 2 passed 2 not applicable

❌ Repository license compliance - LICENSE.txt (and pom.xml) contain SSALv1, but the central exception for sonar-java requires LICENSE.txt to be LGPL-3.0; the maintainers must confirm the intended license (not changed by this PR).
✅ Internal artifact registries
✅ Release secrets usage

Functional validation No results

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

SonarSource/sonar-java-symbolic-execution Inspected

Cross-PR 3 PRs inspected 0 open impacts

PR 6578: JAVA-7401 Rank library overloads to pick the most specific method Inspected
PR 6579: JAVA-7402 Expose annotations and nullability through symbol metadata 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