GitNexus/gitnexus/tools/compiler-conformance/java/test/provider/unresolved-receivers.ts
Gergő Magyar 50228e11ab
Some checks are pending
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Gitleaks / gitleaks (push) Waiting to run
Publish / Classify release event (push) Waiting to run
Publish / RC guard (marker + release-PR skip) (push) Blocked by required conditions
Publish / ci (push) Blocked by required conditions
Publish / Publish to npm (push) Blocked by required conditions
Publish / Build & Push RC Docker images (push) Blocked by required conditions
Scorecard / Scorecard analysis (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-cli) (push) Waiting to run
Trivy Image Scan / Trivy (gitnexus-web) (push) Waiting to run
fix(java): match javac resolution and move fixtures into conformance (#4056)
* test(java): compare overload resolution with javac in CI

* test(conformance): share call resolution reports across tools

* test(java): cover the overload conversion matrix with javac (#4057)

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>

* test(java): run Java-backed fixtures in dedicated CI shards

* test(java): replace handwritten resolution cases with javac comparisons

* test(java): compare lambda and receiver fixtures against javac

* fix(ci): restore Java test evidence and use locked toolchain commands

* test(skills): fail incomplete CLI generation checks (#4056)

* test(java): compare method references with javac

* fix(java): resolve qualified bases and static wildcard overloads

* test(java): preserve declared dollar names in compiler comparisons

* fix(java): preserve distinct call targets for colliding type names

* fix(conformance): resolve private nested bases and retain overload selections

* fix(java): distinguish variadic overloads in the published graph

* fix(java): match javac for nested types, imports and initializer calls

* Move Java fixture testing out of Vitest and into compiler conformance

* Verify Java graph endpoints and leave call selection to javac

* Match javac for annotated bases and legal static imports

* Resolve nested Java member types and clean up failed conformance fixtures

* Preserve qualified Java bases across unrelated static wildcard imports

* test(java): run inline source fixtures in compiler conformance

* test(conformance): check fetch callers, lexical bindings and cleanup

* test(java): move virtual source and text fixtures into conformance

* fix(java): resolve imported member types and qualified base collisions

* Match javac constructor overloads and preserve Java conformance coverage

* Compare recovered Java inheritance with javac in the conformance runner

* Run the Java query compilation smoke test in native conformance

* Clarify Java conformance expectations and check populated suites

* Write javac conformance records as UTF-8 on Windows

* Compile the Java conformance tools for SonarCloud analysis

* Compare Java method implementation edges with javac

* Resolve fixture writers and inherited Java methods in their lexical context

* Resolve qualified Java bases through their accessible type members

* Match local qualified Java bases and bounded varargs identities to javac

* Match Java call resolution to javac across the conformance corpus

Preserve generic declaration identity, follow nested call results, and type lambda aliases and enhanced-loop variables in their declaration scopes. Report proven JDK call targets without creating workspace edges. Compare every selected source and binary call against javac and remove the twelve mismatch lists.

* Compare Java recovery targets as sets and check empty destination addresses

Keep repeated uses of the same declaration equivalent across graph producers while detecting missing and extra targets. Check raw destination addresses and document the source and classpath inputs to the compiler comparison.

* Avoid extra native parent walks while capturing Java call facts

Index lambda origins by spelling before walking lexical scopes, and only check value shadowing for qualifiers rooted in an identifier. This restores the capture traversal budget for anonymous classes while preserving javac call targets.

* Refresh the Java capture fingerprint for corrected fixture comments

The module captures include the revised comments and the following declarations move to earlier lines. Both emitters produce the same 4,288 capture groups when given the same fixture revision.

* Preserve Java conformance assertions and use trusted fixture paths

Use explicit canonical sort orders, require missing evidence to fail assertions, and keep pending query results in the replay cache. Resolve the Git executable independently of the test repository and isolate the Spring workspace per test.

* Resolve inherited Java members through the scoped superclass

* Reuse validated analyzer build hashes for parser cache identity

* Resolve SonarCloud findings in Java resolution and compiler conformance

* Align Java compiler tool packages and pin benchmark baselines to the tested merge

* Honor wildcard method families and fixture ownership in Java conformance

* fix(java): resolve inherited and annotation member types against javac

Keep workspace links tied to import declarations and compare Java varargs overloads through comments. Make generated-project selection, method metadata checks and Java fixture ownership explicit in conformance.

* fix(java): resolve bounded and imported bases during dispatch

Keep the type selected from a qualified heritage clause available during overload resolution. Compare annotated bounds, inherited static imports, and String.valueOf arguments directly with javac. Detect Java fixture paths on either side of a filesystem rename.

* Conform Java method visibility and wildcard lambda bounds to javac

Compare method access at source positions, recognize CommonJS fixture reads, and preserve diagnostics when native conformance setup fails.

* Match javac for qualified member overloads and selective call diagnostics

* Track reassigned Java fixture paths at their point of use

* Respect Java visibility and Node path semantics in conformance checks

* Align Java graph benchmarks with compiler-verified visibility

* Record the public Java record accessor in the mixed-provider benchmark

* Address remaining Java conformance analysis findings

Keep repository import scans sequential, simplify Java header and overload resolution, and reuse Spring conformance measurements. Preserve compiler comparison assertions and emitted fixture contents.

* Match javac on nested binary names, null factories and recovered calls

Preserve the qualified annotation target in workspace links. Compare valid and invalid arguments separately so an attributed recovery symbol cannot mask a failed invocation.

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
2026-11-25 18:03:32 +00:00

244 lines
9.7 KiB
TypeScript

/**
* #2744 — the summary `impact()`/`context()` read to decide exact vs
* lower-bound. Keyed by member name because a dropped site's callee is
* unknown; see the module doc for why per-target attribution is impossible.
*/
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import type { ReceiverOrigin } from '../../../../../src/core/ingestion/scope-resolution/resolution-outcome.js';
import {
buildReceiverOriginEvidence,
classifyReceiverOrigin,
} from '../../../../../src/core/ingestion/scope-resolution/passes/receiver-bound-calls.js';
import { decodeReceiverChain } from '../../../../../src/core/ingestion/utils/receiver-chain-codec.js';
import { javaScopeResolver } from '../../../../../src/core/ingestion/languages/java/scope-resolver.js';
import {
buildScopeModel,
type ScopeModelFixture,
} from '../../../../../test/helpers/scope-model.js';
// ---------------------------------------------------------------------------
// `classifyReceiverOrigin` — the classifier the routing above consumes.
//
// Built from real scope extraction rather than hand-assembled indexes: the
// defect being pinned is that source-level intuition about what a binding
// CONTAINS is wrong (Go normalizes a free parameter's `*Host` to `Host` at
// capture but leaves a method receiver's spelled `*Host` intact), so a fixture
// that asserts the binding shape by hand would pin the intuition, not the code.
// ---------------------------------------------------------------------------
/** The provider-hook bag `classifyReceiverOrigin` reads. Derived from the
* function so the two cannot drift. */
type OriginHooks = Parameters<typeof classifyReceiverOrigin>[5];
/** Classify the receiver of the (unique) reference site invoking `memberName`
* under `hooks`, through exactly the arguments the pass threads at its drop
* recorder. */
function classifyOriginOf(
fixture: ScopeModelFixture,
memberName: string,
hooks: OriginHooks,
): ReceiverOrigin {
const site =
fixture.sites.find(
(candidate) => candidate.name === memberName && candidate.explicitReceiver !== undefined,
) ?? assert.fail(`no receiver site invokes \`${memberName}\``);
const receiver =
site.explicitReceiver ?? assert.fail(`\`${memberName}\` has no explicit receiver`);
return classifyReceiverOrigin(
decodeReceiverChain(site.receiverChain),
site.inScope,
receiver.name,
site.name,
fixture.scopes,
hooks,
);
}
/** The normal path: the language's own contract hooks, built by the same
* factory the pass calls. The fold flags mirror the ones the pass hands
* `resolveCompoundReceiverClass`, read off the resolver. */
function originOf(fixture: ScopeModelFixture, memberName: string): ReceiverOrigin {
const { resolver } = fixture;
return classifyOriginOf(
fixture,
memberName,
buildReceiverOriginEvidence({
parsedFiles: [fixture.parsed],
scopes: fixture.scopes,
index: fixture.index,
foldOptions: {
fieldFallback: resolver.fieldFallbackOnMethodLookup ?? true,
elementTypeOf: resolver.elementTypeOf,
hoistTypeBindingsToModule: resolver.hoistTypeBindingsToModule === true,
stripTypePreservingDecoration: resolver.stripTypePreservingDecoration,
resolveThisViaEnclosingClass: resolver.resolveThisViaEnclosingClass,
},
stripTypePreservingDecoration: resolver.stripTypePreservingDecoration,
isBuiltInName: resolver.languageProvider.isBuiltInName,
externalReceiverNames: resolver.languageProvider.externalReceiverNames,
isExternalModuleSpecifier: resolver.languageProvider.isExternalModuleSpecifier,
}),
);
}
/** The degradation path: the same site with the language's provider hooks
* WITHHELD, as for a language that declares neither.
*
* Named rather than spelled `originOf(fixture, name, {})` — an empty options
* object reads as "defaults", so a reader "simplifying" it away silently
* flips the assertion from the degradation path to the normal path, and some
* of these would still pass while no longer testing anything. */
function originOfWithoutHooks(fixture: ScopeModelFixture, memberName: string): ReceiverOrigin {
return classifyOriginOf(fixture, memberName, {});
}
// #2744. Java is the language the boundary signal matters most for (the
// Spring/DI analysis is built on it) and the one that had no built-in set at
// all, so every drop in it hedged. Same construction as the fixtures above —
// real scope extraction, real provider hook — because the cases that matter are
// exactly the ones source-level intuition gets wrong: a `java.util` import does
// NOT produce an in-program binding (so the base still reaches the built-in
// set), and a `List<String>` declaration does not bind its base to `List`.
const javaFixture = buildScopeModel(
javaScopeResolver,
`import java.util.List;
import java.util.Map;
public class Probe {
private UserService svc;
private List<String> names;
public void go(OrderRepository repo, String raw) {
System.out.println("x");
String.format("%s", raw);
raw.trim();
names.iterator();
List.of("a");
Map.entry("a", "b");
Set.copyOf(names);
Helper.assist();
svc.loadUser();
repo.findAll();
}
}
class Helper {
static void assist() {}
}
class UserService {
User loadUser() { return null; }
}
class User {}
`,
'src/Probe.java',
);
describe('classifyReceiverOrigin', () => {
// ── Java (#2744) ────────────────────────────────────────────────────────
// Before Java had a built-in set, every assertion in this block read
// `lower-bound`-inducing `unknown`.
it('reports a Java static platform receiver as external', () => {
// `System.out.println(...)` — the chain base is `System`, not `out`.
assert.equal(originOf(javaFixture, 'println'), 'external');
assert.equal(originOf(javaFixture, 'format'), 'external');
});
it('reports a Java base whose declared type is a platform type as external', () => {
// `String raw` — the parameter is in-program, the member it dispatches is not.
assert.equal(originOf(javaFixture, 'trim'), 'external');
// A FIELD declared `List<String>`. Java binds a known container to its
// ELEMENT type, so what reaches the built-in check is `String`, not `List` —
// asserting this from the source spelling would pin the wrong thing. Either
// way the verdict is the honest one, and without the set it read `in-program`
// off the value channel and hedged every JDK collection call in the repo.
assert.equal(originOf(javaFixture, 'iterator'), 'external');
});
it('reports a java.util static receiver as external despite its import', () => {
// `import java.util.List` resolves to no workspace file, so it leaves no
// in-program binding and `List` falls through to the built-in set.
assert.equal(originOf(javaFixture, 'of'), 'external');
});
it('still reports Java receivers the program declares as in-program', () => {
assert.equal(originOf(javaFixture, 'assist'), 'in-program');
assert.equal(originOf(javaFixture, 'loadUser'), 'in-program');
});
it('still hedges a Java receiver whose declared type is simply unknown here', () => {
// `OrderRepository` is declared nowhere in this program and is not a platform
// name. Absence of evidence stays `unknown` — the safe direction.
assert.notEqual(originOf(javaFixture, 'findAll'), 'external');
assert.equal(originOf(javaFixture, 'findAll'), 'unknown');
});
it('hedges an unimported platform name deliberately kept OUT of the set', () => {
// `Set.copyOf(...)` is the exact syntactic twin of the `List.of(...)` above,
// and nothing in the file imports `Set`. `Set` is a name applications really
// do declare, and in Java an unimported one can only be a type of this
// package, so it stays out and its drops keep hedging. This pins the
// under-inclusion choice as a choice, not an oversight.
assert.equal(originOf(javaFixture, 'copyOf'), 'unknown');
});
it('reads an explicit platform import as evidence whatever the set says', () => {
// `import java.util.Map` shadows any same-package `Map` (JLS §6.4.1) and can
// never resolve in-workspace, so the import itself places `Map.entry(...)`
// in the JDK. Before the import counted, this drop hedged on set membership
// alone.
assert.equal(originOf(javaFixture, 'entry'), 'external');
});
it('degrades every Java verdict to unknown when the hook is withheld', () => {
assert.equal(originOfWithoutHooks(javaFixture, 'format'), 'unknown');
assert.equal(originOfWithoutHooks(javaFixture, 'trim'), 'unknown');
});
});
const javaLibraryFixture = buildScopeModel(
javaScopeResolver,
`public class Probe {
public void qualified() {
java.util.Optional.of("x");
}
public void shadowed(Holder java) {
java.util.run();
}
}
class Holder {
Util util;
}
class Util {
void run() {}
}
`,
'src/Probe.java',
);
describe('classifyReceiverOrigin — library and runtime receivers', () => {
for (const example of [['Java platform package root', javaLibraryFixture, 'of']] as const) {
it(`reports ${example[0]} as external`, () => {
const [_label, fixture, memberName] = example;
assert.equal(originOf(fixture, memberName), 'external');
});
}
for (const example of [
['a Java parameter named java', 'in-program', javaLibraryFixture, 'run'],
] as const) {
it(`keeps ${example[0]} ${example[1]}`, () => {
const [_label, expected, fixture, memberName] = example;
assert.equal(originOf(fixture, memberName), expected);
});
}
});