GitNexus/gitnexus/test/integration/rust-qualified-factory-boundaries.test.ts
Abhigyan Patwari 65b101407e
fix(rust): type locals from module-qualified factory calls (#3657)
* fix(scope): follow namespace factory return types and record untyped drops

`import mod as rp; x = rp.make(); x.m()` lost the receiver type even when
`make` is declared `-> C`: `x` was bound to the raw dotted name `rp.make`,
and nothing followed a dotted name to a function's return type. The inline
`rp.make().m()` failed the same way in the compound resolver, which read
`ns.f()` only as a construction.

Resolve `ns.f` to `f`'s return annotation when `ns` is a namespace import,
the fact `propagateImportedReturnTypes` already mirrors for a named import.
The post-finalize chain follow uses it for bindings and the compound
resolver for inline calls. Only sites that emitted nothing change: the
full lang-resolution corpus emits the identical 2,838 CALLS/ACCESSES edges.

Record the drops that left `impact` at "exact":
- a plain receiver bound to a namespace function with no usable return type
  (`x = ns.untyped()`);
- an outer call whose `file:line:col` key a resolved inner site already
  claimed (`ns.untyped().m()`, `s.repo.Delete(id)`). Only a duplicate
  capture of the same member hides a drop now.

Receiver baseline: call drops 128 -> 151, reads 44 -> 48, ten shape-arm
cells INVISIBLE-GAP -> VISIBLE-GAP; every new drop has no CALLS edge to its
member (BASELINE.md lists them). The python.factory-qualified accuracy gap
is removed; the python-scope fingerprint and capture golden gain the new
fixture only.

Fixes #3498

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(dart): type locals from prefixed-import factory calls

`import 'mod.dart' as rp; final x = rp.make(); x.m()` gave `x` no type:
local call inference accepted only a bare callee (`Callee()`), and the
prefix-stripping type normalizer would have reduced `rp.make` to `make`.

Bind a local initialized by exactly `prefix.member(...)` (direct or
awaited) to the dotted callee and keep its prefix, so the shared chain
follow types it from the namespace function's return annotation. Only
call-derived bindings keep the prefix; annotated `prefix.Type` spellings
are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(zig): type locals from namespace-module factory calls

`const rp = @import("mod.zig"); const x = rp.make(); x.m()` bound `x` to
the namespace handle `rp` itself: the call-return rule reads a non-value
head as the type being constructed (`Counter.init()`), and import bindings
are not values. The member name was gone before the shared chain follow
could use the new namespace return-type lookup.

For a module-level handle on a lowercase file (a namespace module, by Zig
convention), bind the dotted callee `rp.make` so the shared chain follow
types it from `make`'s return annotation. File-struct handles
(`@import("Page.zig")`, any spelling of the handle) keep naming the
struct, so `Page.init()` is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(bench): rebaseline captures and receiver counts for Dart and Zig

- receiver-resolution: the Dart fixture's untyped factory adds one call
  drop (151 -> 152); every typed form in the Dart and Zig fixtures resolves.
- scope-capture (dart): two existing dart-method-enrichment locals
  initialized by `obj.method()` / `Type.method()` gain a call-derived
  binding, plus the new fixture's captures. A full lang-resolution A/B
  emits the identical edges outside the new fixtures.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(rust): type locals from module-qualified factory calls

`mod modx; use crate::modx as rp; let x = rp::make(); x.m()` left `x`
untyped although `make` is declared `-> C`. The call-return rule kept only
the text before the first `::` (the `Foo::new()` idiom), so `x` was bound
to the module `rp` and the function name was lost. `modx::make()` and
`crate::modx::make()` failed the same way, and the inline `rp::make().m()`
failed in the compound resolver, which cannot split a `::` path. With a
`let modx = Other;` in scope, `modx::make()` even typed `x` as `Other` and
emitted a wrong `Other.m` edge.

- Rust keeps the whole callee path when every segment is lower-case: a
  module function, by rustc's lint-enforced casing. `Foo::new()` and
  `Self::new()` still read as the type.
- New `ScopeResolver.resolveQualifiedCallee` hook. The shared chain follow,
  the compound call branch and the drop recorder hand a callee text no
  scope binds to the provider, which returns the callable it names; the
  shared side reads that callable's return annotation by definition
  identity and never splits the path. Rust answers with the module-tree
  walk behind `resolveQualifiedFreeCall`, so a call's type and its CALLS
  edge come from the same resolution.
- A receiver bound to an in-program module function with no return
  annotation (`x = rp::untyped()`) records a `receiver-unresolved` drop,
  as #3498 does for `ns.untyped()`.
- `rp::make()` did not resolve as a call either: the module-name filter
  rejected the alias `rp`, and under Cargo the import binds the module's
  `mod modx;` declaration rather than `modx.rs`. The `use` candidate now
  admits an alias that names a module and reads a module from its
  top-level declaration.

A full lang-resolution A/B against the parent emits identical CALLS and
ACCESSES edges outside the new fixture, except one new correct edge:
`rust-ambiguous` `main -> models::Handler.handle`, typed from
`let h = services::create_handler()`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(bench): rebaseline rust captures and receiver counts

- receiver-resolution: the new rust-qualified-factory-return fixture's two
  untyped sites (`x = rp::untyped(); x.m()` and inline
  `modx::untyped().m()`) are each a call and a callee-position read drop:
  call drops 152 -> 154, reads 48 -> 50, all kinds 220 -> 224. No other
  drop moves in a same-corpus A/B, and shape-arm cells are unchanged.
- scope-capture (rust): corpus growth only. emitRustScopeCaptures is
  untouched, and parking the new fixture reproduces the prior fingerprint
  byte-for-byte; it adds 131 capture groups and 3 fixture entries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* perf(scope): reuse qualified call file sets

* docs(scope): clarify finalized type refinement

* fix(rust): scope generic alias parameters

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 09:05:48 +00:00

98 lines
4.1 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { runPipelineFromRepo } from '../../src/core/ingestion/pipeline.js';
import { TransitionalScopeTree } from '../../src/storage/scope-index-store.js';
describe('Rust qualified factory boundaries', () => {
it('keeps an alias type parameter out of the enclosing module', async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'rust-alias-parameter-'));
try {
await fs.writeFile(
path.join(root, 'main.rs'),
'mod T { pub struct Product; impl Product { pub fn m(&self) {} } pub fn make() -> Product { Product } }\ntype Alias<T> = Option<T>;\nfn caller() { let x = T::make(); x.m(); }\n',
);
const result = await runPipelineFromRepo(root, () => {}, {});
const calls = [...result.graph.iterRelationships()].filter(
(edge) =>
edge.type === 'CALLS' &&
result.graph.getNode(edge.sourceId)?.properties.name === 'caller',
);
expect(
calls.map((edge) => result.graph.getNode(edge.targetId)?.properties.name).sort(),
).toEqual(['m', 'make']);
} finally {
await fs.rm(root, { recursive: true, force: true });
}
}, 120000);
it('retains the outer unresolved call when its member repeats the inner callee', async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'rust-qualified-repeat-'));
try {
await fs.writeFile(
path.join(root, 'main.rs'),
'mod factory { pub fn untyped() {} }\nfn caller() { factory::untyped().untyped(); }\n',
);
const result = await runPipelineFromRepo(root, () => {}, {});
const drops = result.resolutionOutcomes.filter(
(outcome) =>
outcome.kind === 'suppressed' &&
outcome.reason === 'receiver-unresolved' &&
outcome.siteKind === 'call' &&
outcome.name === 'untyped',
);
expect(drops).toHaveLength(1);
const calls = [...result.graph.iterRelationships()].filter(
(edge) =>
edge.type === 'CALLS' &&
result.graph.getNode(edge.sourceId)?.properties.name === 'caller' &&
result.graph.getNode(edge.targetId)?.properties.name === 'untyped',
);
expect(calls).toHaveLength(1);
} finally {
await fs.rm(root, { recursive: true, force: true });
}
}, 120000);
it('resolves a cold inline-only module after sealing disk scopes', async () => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'rust-qualified-disk-'));
const store = await fs.mkdtemp(path.join(os.tmpdir(), 'rust-qualified-store-'));
const previous = process.env.GITNEXUS_DISK_SCOPE_INDEX;
const original = TransitionalScopeTree.prototype.seal;
let seals = 0;
TransitionalScopeTree.prototype.seal = function (...args: Parameters<typeof original>) {
seals++;
return original.apply(this, args);
};
try {
process.env.GITNEXUS_DISK_SCOPE_INDEX = '1';
await fs.writeFile(
path.join(root, 'main.rs'),
'mod factory { pub fn untyped() {} }\nfn caller() { factory::untyped().m(); }\n',
);
const result = await runPipelineFromRepo(root, () => {}, { parsedFileStorePath: store });
expect(seals).toBe(1);
expect(
[...result.graph.iterRelationships()].filter(
(edge) =>
edge.type === 'CALLS' && result.graph.getNode(edge.targetId)?.properties.name === 'm',
),
).toEqual([]);
expect(
result.resolutionOutcomes.some(
(outcome) =>
outcome.kind === 'suppressed' &&
outcome.reason === 'receiver-unresolved' &&
outcome.siteKind === 'call' &&
outcome.name === 'm',
),
).toBe(true);
} finally {
TransitionalScopeTree.prototype.seal = original;
if (previous === undefined) delete process.env.GITNEXUS_DISK_SCOPE_INDEX;
else process.env.GITNEXUS_DISK_SCOPE_INDEX = previous;
await fs.rm(root, { recursive: true, force: true });
await fs.rm(store, { recursive: true, force: true });
}
}, 120000);
});