GitNexus/gitnexus/test/unit/scope-resolution/java/java-overload-argument-facts.test.ts
Gergő Magyar b5b4f13f2c
fix(java): resolve overloads by JLS phases (#3791)
* docs(plans): add Java overload repair plan

* fix(java): resolve overloads by JLS phases

Select complete overload families before shared pruning so strict,
loose and variable-arity applicability preserve Java call targets.
Keep unsupported or ambiguous families terminal and persist their
omission boundaries so impact counts cannot imply complete coverage.

Preserve declared return types, synthetic members and bounded runtime
dispatch. Memoize inheritance walks to avoid exponential diamond work.

Fixes #3789

* refactor(java): simplify overload capture paths

Reuse record accessor captures and require suppression identity context.
Remove redundant cache-layout assertions and test-history comments.
Populate real overload receipts in the accuracy fixture so its exact
control matches the production metadata contract.

* docs: remove implementation plan from PR

Exclude the planning document from the published change.

* fix(java): honor enclosing protected access

* docs(mcp): qualify overload coverage (#3791)

* perf(java): bound overload ingestion costs

Index method facts and lexical bindings, and avoid repeated native child and callable-definition scans. Add measured cold, warm, and edited scaling checks at 128, 512, and 2048 units.

Refs #3789

* refactor(java): simplify overload lookup state

Index diagnostic candidates once to bound lookup work linearly,
preserving scope and first-match precedence. Keep local bindings
out of the field index and remove redundant metadata.

Refs #3789

* fix(java): correct replay diagnostics and CI

Repeated impact queries exhausted the persistence test's 128 MiB buffer
pool. Give the reader 512 MiB and reject partial results before checking
callers.

Coalesce receiver replay diagnostics by full source-site identity while
compacting resolved drops in one linear pass. This removes 51 duplicate
receipts without merging distinct calls at the same start position.

Refresh the Java capture fingerprints after attributing every difference
to return types, var initializers, and seven new fixture files. Keep the
1.5 scaling budget. Receiver calls move 140 -> 141: two new fixture sites
and one existing chain newly resolved. Shape states are unchanged; origin
counts reflect selected return types instead of earlier guesses.

Refs #3789

* ci(java): enforce overload scaling benchmark

Run the 54-analysis correctness and scaling check in the existing
scope-resolution benchmark job. Upload its report with that job's
measurements so regressions fail CI instead of relying on manual runs.

Refs #3789

* test(java): clarify overload coverage (#3791)

Separate unmodeled call-argument coverage from Java accessibility and
confirm the accessible Object overload with an executed javac oracle.

Give incomplete-attribution cases distinct titles: JSON formatting drops
undefined properties and made three test identities collide in CI.

* perf(scope): skip unmatched value names

Reject impossible callable names before parsing definition positions or
allocating positional keys. Keep the exact file/line/name admission and
ambiguity rules unchanged.

The callable-value-flow benchmark overhead drops from 1.747-1.883 locally
to 1.210-1.271, below the unchanged 1.9 budget. Target fingerprints and
cardinalities are identical.

Refs #3791

* fix(java): resolve types from static imports

Map package-qualified model names to scope-index keys before resolving
explicit static member-type imports. Preserve uncertainty for missing
or conflicting imports.

Verify Number selection after boxing long and preserve the imported
source type identity, with an executed javac oracle for both targets.

Refs #3791

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
2026-11-01 11:08:00 +00:00

380 lines
15 KiB
TypeScript

import { afterEach, describe, expect, it } from 'vitest';
import { getJavaParser } from '../../../../src/core/ingestion/languages/java/query.js';
import {
captureJavaOverloadFacts,
clearJavaOverloadFacts,
getJavaOverloadCall,
getJavaOverloadFacts,
isJavaOverloadFileFacts,
setJavaOverloadFacts,
} from '../../../../src/core/ingestion/languages/java/overload-argument-facts.js';
function capture(source: string) {
const tree = getJavaParser().parse(source);
return captureJavaOverloadFacts(tree.rootNode)!;
}
afterEach(clearJavaOverloadFacts);
describe('Java lossless overload argument facts', () => {
it('does not compare bindings from unrelated methods with the same parameter name', () => {
const size = 128;
const tree = getJavaParser().parse(
`class Wide { ${Array.from(
{ length: size },
(_, i) => `void method${i}(String value) { use(value); }`,
).join('\n')} }`,
);
const owner = tree.rootNode.namedChildren[0]!;
const id = owner.id;
let reads = 0;
Object.defineProperty(owner, 'id', {
get() {
reads++;
return id;
},
});
const facts = captureJavaOverloadFacts(tree.rootNode)!;
expect(facts.calls.map((call) => call.arguments[0])).toEqual(
Array.from({ length: size }, () =>
expect.objectContaining({ kind: 'typed', spelling: 'String' }),
),
);
expect(reads).toBeLessThanOrEqual(size * 16);
});
it('materializes a wide class body once during traversal', () => {
const source = `class Wide { ${Array.from(
{ length: 128 },
(_, i) => `void method${i}() { use("text"); }`,
).join('\n')} }`;
const tree = getJavaParser().parse(source);
const body = tree.rootNode.namedChildren[0]!.childForFieldName('body')!;
const children = body.namedChildren;
let reads = 0;
Object.defineProperty(body, 'namedChildren', {
get() {
reads++;
return children;
},
});
const facts = captureJavaOverloadFacts(tree.rootNode)!;
expect(facts.methods).toHaveLength(128);
expect(facts.calls).toHaveLength(128);
expect(reads).toBe(1);
});
it('keeps formal parameters in their own methods and respects field/block shadowing', () => {
const facts = capture(`class A {
Long value;
void first(String value) { use(value); }
void second(Long value) { use(value); }
void blocks() { { String value = "x"; use(value); } use(value); }
}`);
expect(
facts.calls.filter((call) => call.name === 'use').map((call) => call.arguments[0]),
).toMatchObject([
{ kind: 'typed', spelling: 'String' },
{ kind: 'typed', spelling: 'Long' },
{ kind: 'typed', spelling: 'String' },
{ kind: 'typed', spelling: 'Long' },
]);
});
it('does not let a later local declaration hide the field at an earlier call', () => {
const facts = capture(
'class A { Long value; void run() { use(value); String value = "x"; use(value); } }',
);
expect(facts.calls.map((call) => call.arguments[0])).toMatchObject([
{ kind: 'typed', spelling: 'Long' },
{ kind: 'typed', spelling: 'String' },
]);
});
it('uses an explicit this field declaration despite a local with the same name', () => {
const facts = capture(
'class A { Long value; void run(String value) { use(value); use(this.value); } }',
);
expect(facts.calls.map((call) => call.arguments[0])).toMatchObject([
{ kind: 'typed', spelling: 'String' },
{ kind: 'typed', spelling: 'Long' },
]);
});
it('looks up inherited fields before enclosing fields and retains declaration context', () => {
const facts = capture(`class Base { String value; }
class Outer { Long value; class Inner<String> extends Base {
void run(Long value) { use(this.value); use(value); }
void implicit() { use(value); }
} }`);
const arguments_ = facts.calls
.filter((call) => call.name === 'use')
.map((call) => call.arguments[0]);
expect(arguments_).toMatchObject([
{ kind: 'typed', spelling: 'String', at: { startLine: 1 } },
{ kind: 'typed', spelling: 'Long', at: { startLine: 3 } },
{ kind: 'typed', spelling: 'String', at: { startLine: 1 } },
]);
});
it('keeps enhanced-for bindings out of the iterable expression', () => {
const facts = capture(
'class A { Long item; void run() { for (String item : source(item)) use(item); } }',
);
expect(facts.calls.map((call) => call.arguments[0])).toMatchObject([
{ kind: 'typed', spelling: 'Long' },
{ kind: 'typed', spelling: 'String' },
]);
});
it.each([
['unknown superclass', '', 'extends External'],
['generic field substitution', 'class Base<T> { T value; }', 'extends Base<String>'],
[
'ambiguous interface fields',
'interface Left { String value = "x"; } interface Right { Long value = 1L; }',
'implements Left, Right',
],
])('does not infer an outer field across %s', (_description, declarations, heritage) => {
const facts = capture(`${declarations} class Outer { Long value; class Inner ${heritage} {
void run() { use(value); use(this.value); }
} }`);
expect(
facts.calls.filter((call) => call.name === 'use').map((call) => call.arguments[0]),
).toEqual([{ kind: 'unknown' }, { kind: 'unknown' }]);
});
it('does not inherit private fields or use an outer field for explicit this', () => {
const facts = capture(
'class Base { private String value; } class Outer { Long value; class Inner extends Base { void run() { use(value); use(this.value); } } }',
);
expect(facts.calls.map((call) => call.arguments[0])).toMatchObject([
{ kind: 'typed', spelling: 'Long' },
{ kind: 'unknown' },
]);
});
it('resolves an enclosing field through a deep empty inheritance diamond', () => {
const declarations = ['interface A0 {}', 'interface B0 {}'];
// Without memoization this small hierarchy expands into millions of paths.
for (let depth = 1; depth <= 24; depth++) {
declarations.push(
`interface A${depth} extends A${depth - 1}, B${depth - 1} {}`,
`interface B${depth} extends A${depth - 1}, B${depth - 1} {}`,
);
}
const facts = capture(`${declarations.join('\n')}
class Outer { Long value; class Inner implements A24, B24 {
void run() { use(value); use(value); }
} }`);
expect(facts.calls.map((call) => call.arguments[0])).toMatchObject([
{ kind: 'typed', spelling: 'Long' },
{ kind: 'typed', spelling: 'Long' },
]);
});
it('does not reuse a shallow field proof beyond the inheritance depth bound', () => {
const declarations = ['interface A0 {}'];
for (let depth = 1; depth <= 63; depth++)
declarations.push(`interface A${depth} extends A${depth - 1} {}`);
const facts = capture(`${declarations.join('\n')}
class Outer { Long value; class Inner implements A0, A63 {
void run() { use(value); use(value); }
} }`);
expect(facts.calls.map((call) => call.arguments[0])).toEqual([
{ kind: 'unknown' },
{ kind: 'unknown' },
]);
});
it('keeps cyclic field inheritance unknown across repeated lookups', () => {
const facts = capture(`interface Left extends Right {} interface Right extends Left {}
class Outer { Long value; class Inner implements Left, Right {
void run() { use(value); use(value); }
} }`);
expect(facts.calls.map((call) => call.arguments[0])).toEqual([
{ kind: 'unknown' },
{ kind: 'unknown' },
]);
});
it('retains numeric suffixes, declared containers, dimensions, casts, and null separately', () => {
const facts = capture(
'class A { void run(java.util.List<Long> ids, int[][] grid) { use(1L, 1f, ids, grid, (String)null, null); } }',
);
expect(facts.calls[0]!.arguments).toMatchObject([
{ kind: 'typed', spelling: 'long' },
{ kind: 'typed', spelling: 'float' },
{ kind: 'typed', spelling: 'java.util.List<Long>' },
{ kind: 'typed', spelling: 'int[][]' },
{ kind: 'typed', spelling: 'String' },
{ kind: 'null' },
]);
});
it('retains qualified generic formal types and array dimensions in candidate declarations', () => {
const facts = capture(
'class A { void use(java.util.Collection<Long> ids, String[] names, int value[], Long... rest) {} }',
);
expect(facts.methods[0]!.parameterTypes).toEqual([
'java.util.Collection<Long>',
'String[]',
'int[]',
'Long',
'varargs',
]);
});
it('preserves trailing dimensions on method and explicitly typed lambda formals', () => {
const facts = capture(
'class A { void run(String value[]) { use(value); java.util.function.Consumer<String[]> lambda = (String item[]) -> use(item); } }',
);
expect(
facts.calls.filter((call) => call.name === 'use').map((call) => call.arguments[0]),
).toMatchObject([
{ kind: 'typed', spelling: 'String[]' },
{ kind: 'typed', spelling: 'String[]' },
]);
});
it('retains poly factories and explicit type arguments rather than freezing List<Long>', () => {
const facts = capture(
'import java.util.Arrays; class A { void run(Long id) { use(Arrays.<Number>asList(id, 1L)); } }',
);
expect(facts.calls.find((call) => call.name === 'use')!.arguments[0]).toMatchObject({
kind: 'factory',
owner: 'Arrays',
name: 'asList',
typeArguments: ['Number'],
shadowed: false,
arguments: [
{ kind: 'typed', spelling: 'Long' },
{ kind: 'typed', spelling: 'long' },
],
});
});
it('records value shadowing of a factory qualifier and type-parameter declarations', () => {
const facts = capture(
'class A<Long> { <String> void run(Object Arrays) { use(Arrays.asList(1L)); } }',
);
expect(facts.types[0]!.typeParameters).toEqual(['Long']);
expect(facts.methods[0]!.typeParameters).toEqual(['String']);
expect(facts.calls.find((call) => call.name === 'use')!.arguments[0]).toMatchObject({
kind: 'factory',
shadowed: true,
});
});
it('keeps same-start chained call sites distinct through a plain-data round trip', () => {
const facts = capture('class A { void run(Long id) { lookup.find(id).get(id); } }');
const restored: unknown = JSON.parse(JSON.stringify(facts));
expect(isJavaOverloadFileFacts(restored)).toBe(true);
setJavaOverloadFacts('A.java', restored as typeof facts);
for (const call of facts.calls)
expect(getJavaOverloadCall('A.java', call.range, call.name)).toEqual(call);
setJavaOverloadFacts('A.java', undefined);
expect(getJavaOverloadFacts('A.java')).toBeUndefined();
expect(
getJavaOverloadCall('A.java', facts.calls[0]!.range, facts.calls[0]!.name),
).toBeUndefined();
});
it('assigns enum constant methods to their synthetic owners and retains the host base', () => {
const facts = capture(`class Host { enum Mode {
FIRST { void hook() { helper(); } }, SECOND { void hook() {} }, PLAIN;
void helper() {}
} }`);
expect(facts.types.map(({ name, simpleName, bases }) => ({ name, simpleName, bases }))).toEqual(
[
{ name: 'Host', simpleName: 'Host', bases: [] },
{ name: 'Mode', simpleName: 'Mode', bases: [] },
{ name: 'Host$Mode$1', simpleName: 'Host$Mode$1', bases: ['Mode'] },
{ name: 'Host$Mode$2', simpleName: 'Host$Mode$2', bases: ['Mode'] },
],
);
expect(facts.methods.map(({ owner, name }) => `${owner}.${name}`)).toEqual([
'Host$Mode$1.hook',
'Host$Mode$2.hook',
'Mode.helper',
]);
});
it('captures implicit record accessors without duplicating explicit zero-argument accessors', () => {
const facts = capture(`class Host { void run() {
record Data(String name, int count, String... tags) {
public String name() { return name; }
int count(int factor) { return count * factor; }
}
} }`);
const accessors = facts.methods.filter((method) => method.owner === 'Host$1Data');
expect(
accessors.map(({ name, parameterTypes, modifiers }) => ({
name,
parameterTypes,
modifiers,
})),
).toEqual([
{ name: 'count', parameterTypes: [], modifiers: ['public'] },
{ name: 'tags', parameterTypes: [], modifiers: ['public'] },
{ name: 'name', parameterTypes: [], modifiers: ['public'] },
{ name: 'count', parameterTypes: ['int'], modifiers: [] },
]);
expect(isJavaOverloadFileFacts(JSON.parse(JSON.stringify(facts)))).toBe(true);
});
it('does not synthesize accessor facts for malformed record component names', () => {
const facts = capture('record Missing(int x, y) {} record Unnamed(int _) {}');
expect(facts.methods.map(({ owner, name }) => `${owner}.${name}`)).toEqual(['Missing.x']);
});
it('retains type visibility and gives only direct interface members implicit public access', () => {
const facts = capture(`public class Outer {
private static class Hidden {}
protected class Protected {}
class PackageMember {}
}
interface API {
class Member {}
enum Mode { VALUE }
default void run() { class Local {} }
}
@interface Annotation { class Member {} }`);
expect(facts.types.map(({ name, modifiers }) => ({ name, modifiers }))).toEqual([
{ name: 'Outer', modifiers: ['public'] },
{ name: 'Hidden', modifiers: ['private', 'static'] },
{ name: 'Protected', modifiers: ['protected'] },
{ name: 'PackageMember', modifiers: [] },
{ name: 'API', modifiers: [] },
{ name: 'Member', modifiers: ['public'] },
{ name: 'Mode', modifiers: ['public'] },
{ name: 'API$1Local', modifiers: [] },
{ name: 'Annotation', modifiers: [] },
{ name: 'Member', modifiers: ['public'] },
]);
});
it('validates optional persisted type modifiers without rejecting older facts', () => {
const facts = capture('public class A {}');
const { modifiers: _modifiers, ...olderType } = facts.types[0]!;
expect(isJavaOverloadFileFacts({ ...facts, types: [olderType] })).toBe(true);
expect(isJavaOverloadFileFacts(JSON.parse(JSON.stringify(facts)))).toBe(true);
expect(isJavaOverloadFileFacts({ ...facts, types: [{ ...olderType, modifiers: null }] })).toBe(
false,
);
expect(isJavaOverloadFileFacts({ ...facts, types: [{ ...olderType, modifiers: [1] }] })).toBe(
false,
);
});
it('rejects malformed persisted facts and retains empty types for inherited inventories', () => {
expect(capture('class Empty {}').types).toMatchObject([{ name: 'Empty', bases: [] }]);
const facts = capture('class A { void run() { use(null); } }');
expect(
isJavaOverloadFileFacts({
...facts,
calls: [{ ...facts.calls[0], arguments: [{ kind: 'typed', spelling: 1 }] }],
}),
).toBe(false);
expect(isJavaOverloadFileFacts({ ...facts, methods: [null] })).toBe(false);
});
});