Skip to content

JAVA-7420 Bind names SemSitter leaves unresolved to statically imported fields - #6588

Open
rombirli wants to merge 1 commit into
rombirli/semsitter-translatorfrom
rombirli/semsitter-static-import-fields
Open

rombirli wants to merge 1 commit into
rombirli/semsitter-translatorfrom
rombirli/semsitter-static-import-fields

Conversation

@rombirli

@rombirli rombirli commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Part 8 of 17 of JAVA-7392. Ticket: JAVA-7420.

Intent

When a file's batch only knows a supertype as a compiled class, the server can leave a statically imported field unresolved. The translator then made it an unknown symbol, which broke every check that follows the field.

import static org.jooq.SQLDialect.DERBY;   // flagged as unused (S1128)

if (dialect == DERBY) { ... }              // DERBY unknown, so its deprecation (S1874) was lost

The translator now binds such a name as ECJ does: to a field of the enclosing types first, since a declared or inherited field shadows a static import, then to the static field of the single static import, then of each on demand import.

flowchart LR
  N["unresolved name"] --> T{"field of an enclosing type?"}
  T -->|found| B["bind and ascribe the field"]
  T -->|no| S{"single static import?"}
  S -->|field found| B["bind and ascribe the field"]
  S -->|no| D{"on demand imports?"}
  D -->|static field found| B
  D -->|no| U["stays unknown"]
Loading
jOOQ probe before after
DERBY unknown SQLDialect.DERBY, deprecated
unused import on line 40 reported not reported

This explains the other part of the issues lost on the last PVF compared to the earlier stack.

State on master

NameTranslator does not exist on master: it comes with part 7.

Test plan

  • NameTranslatorTest.an_unresolved_name_is_the_field_a_static_import_brings_in: PI, E and SECONDS bind to their fields, an unimported name stays unknown
  • NameTranslatorTest.an_unresolved_name_is_the_field_of_its_enclosing_type_first: an unresolved count binds to the field of its class

@rombirli rombirli self-assigned this Nov 25, 2026
@hashicorp-vault-sonar-prod

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

Copy link
Copy Markdown
Contributor

JAVA-7420

@rombirli
rombirli added this pull request to stack #6589 November 25, 2026 20:48
@rombirli
rombirli force-pushed the rombirli/semsitter-static-import-fields branch 2 times, most recently from 431d444 to 8f917d7 Compare November 27, 2026 07:55
@rombirli
rombirli force-pushed the rombirli/semsitter-static-import-fields branch 2 times, most recently from 5aa9bfb to 874f07d Compare November 27, 2026 09:24
…ed fields

When the enclosing class has a supertype the server cannot read, it leaves the
names a static import brings in unresolved. Look the name up as a field of the
enclosing types first, since a declared or inherited field shadows a static
import, then as a static field of the single static import, then of each static
import on demand, as ECJ does.
@rombirli
rombirli force-pushed the rombirli/semsitter-static-import-fields branch from 874f07d to 53294f6 Compare November 27, 2026 10:46
@gitar-bot

gitar-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
CI failed: The ruling update job failed because the upstream ruling-qa job was cancelled, so no actual_* artifacts were available to download. The log does not establish why QA was cancelled, and the analyzer attributes this to infrastructure unrelated to the PR's SemSitter name-binding change.

Overview

The ruling update job failed because the upstream ruling-qa job was cancelled, so no actual_* artifacts were available to download. The log does not establish why QA was cancelled, and the analyzer attributes this to infrastructure unrelated to the PR's SemSitter name-binding change.

Failures

Ruling update failed: no actual_* artifacts due to cancelled upstream ruling QA job

  • Type: infrastructure
  • Affected jobs: 140458577817
  • Related to change: no
  • Confidence: high
  • Root cause: The log shows needs.ruling-qa.result was cancelled; the job consequently set RULING_FAILED=true and attempted to sync ruling artifacts, but none were found. This is a downstream failure from the cancelled QA job; the log does not establish why QA was cancelled. The PR changes SemSitter name binding in NameTranslator.java, which is unrelated to artifact production or the ruling-update workflow.
  • Suggested fix: Investigate why the ruling QA job was cancelled and rerun it so it can upload the expected artifacts. Update the ruling-update workflow to skip artifact syncing when the upstream job is cancelled or no artifacts were produced, rather than failing on the missing artifacts.

Summary

  • Change-related failures: 0
  • Infrastructure/flaky failures: 1
  • Recommended action: Investigate why the ruling QA job was cancelled and rerun it so it can upload the expected artifacts. Update the ruling-update workflow to skip artifact syncing when the upstream job is cancelled or no artifacts were produced, rather than failing on the missing artifacts.
Code Review ✅ Approved 🟡 Medium risk 1 closed / 1 findings

Resolving previously unknown names to enclosing or statically imported fields changes semantic analysis and could alter findings for affected references.

Binds unresolved names to statically imported fields, following ECJ's order: enclosing-type fields first, then the single static import, then on-demand imports. The previously open finding that the static-import fallback skipped fields of enclosing types has been resolved.

✅ 1 closed
✅ Bug: Static-import field fallback skips fields of enclosing types

📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/NameTranslator.java:83-85 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/NameTranslator.java:174-188 📄 java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/NameTranslator.java:242-247
importedField binds an unresolved simple name to the first static field it finds in the single or on-demand static imports, without first checking the enclosing types (state.types). The trigger is the scenario this PR describes: the batch only knows a supertype as a compiled class, so a field inherited from it is left unresolved. If any static-on-demand import also declares a static field with that name, the name is now bound to the imported field. Under JLS 6.4.1 (and in ECJ), a field the class declares or inherits shadows a statically imported one. Before this PR the name stayed unknown. Now it gets the wrong symbol and type, so checks such as deprecation or unused import can report on the wrong field. The method fallback in this same file does this correctly: libraryMethod lists state.types before the static imports, and bindStaticImport skips the import when declaredHere. To fix, look the name up with declaredField on each known enclosing type first, and only use the static imports when none of them has the field.

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

Functional validation No results

Rules 3 passed 2 not applicable

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

Broader impact Repos 3 inspected PRs 4 inspected
Cross-repo 3 repositories inspected 0 open impacts

SonarSource/sonarlint-eclipse Inspected
SonarSource/sonarlint-intellij Inspected
SonarSource/sonar-iac Inspected

Cross-PR 4 PRs inspected 0 open impacts

PR 6580: JAVA-7403 Derive unused import and no-effect assignment warnings Inspected
PR 6579: JAVA-7402 Expose annotations and nullability through symbol metadata Inspected
PR 6576: JAVA-7399 Infer invocation return types, thrown types and diamonds Inspected
PR 6578: JAVA-7401 Rank library overloads to pick the most specific method 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