Skip to content

fix(rust): bind an in-file call to the type it names, not to any same-named function - #4238

Closed
rohit-jsfreaky wants to merge 1 commit into
Graphify-Labs:v8from
rohit-jsfreaky:fix/rust-in-file-calls-respect-qualifier
Closed

rohit-jsfreaky wants to merge 1 commit into
Graphify-Labs:v8from
rohit-jsfreaky:fix/rust-in-file-calls-respect-qualifier

Conversation

@rohit-jsfreaky

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #4237.

In extract_rust (graphify/extractors/rust.py), the in-file call match now follows the call's shape:

  • Type::f() and Self::f() bind only to f in an impl Type block of the same file. The type is the last path segment without generic arguments (crate::a::Matcher, Vec::<u8>, io::Error, i32), on both the call and the impl. With no such method there is no in-file edge, except Enum::Variant(x), which still gets its references (constructor) edge to the variant node.
  • self.f() binds only to f on the caller's own impl type. With no such method in the file it now reaches the existing cross-file self pass (fix(rust): receiver-method calls (self.method()) are dropped cross-file — Rust has no _resolve_*_member_calls pass #2234) instead of another type's f.
  • A bare f() binds only to a free function or a type (tuple struct / variant constructor), never to a method. x.f() binds only to a method, never to a free function.

Module paths (fs::read, self::helper) keep the old match.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

Invariant: an in-file Type::f() / Self::f() / self.f() edge goes to a method of that type; a free call never reaches a method and a method call never reaches a free function.
Persisted state it could invalidate: AST cache entries for .rs files. The cache directory is keyed by package version (cache/ast/v{version}-s{schema}), so the next release re-extracts; no schema bump needed (no new persisted field).

Against rust-analyzer 1.97.1 (go-to-definition at every call inside every fn), 0.9.80 vs this branch, cold graphify update <dir> --no-cluster --force, calls edges with both ends in .rs files:

repo wrong edges wrong share correct edges calls found
ripgrep 1232 -> 532 39.4% -> 18.9% 1852 -> 2236 40.5% -> 48.2%
bat 138 -> 36 17.8% -> 5.0% 602 -> 655 65.8% -> 71.5%
fd 29 -> 9 16.0% -> 5.4% 148 -> 154 34.4% -> 35.8%

"Calls found": (caller, callee) pairs rust-analyzer resolves to a fn in the repo that the graph also has. Correct edges go up because B::new() now reaches B::new instead of the last new in the file.

Correct (caller, callee) pairs lost: ripgrep 1, bat 0, fd 0. The one is Default::default() in a globset test: a trait-qualified call whose target depends on the variable's type (let set: GlobSet), so the old edge was the bare-name guess landing right.

New pairs rust-analyzer does not confirm: ripgrep 25, bat 0, fd 0. All are x.f() calls with an untyped receiver, which this PR leaves as a bare-name guess; with methods and free functions in separate indexes, the guess now lands on a different method in those 25 cases (for example searcher.search_path() in crates/core/search.rs, whose target is Searcher::search_path in crates/searcher, now reaching the local SearchWorker::search_path; before, the name matched the caller itself and the edge was dropped). Over all x.f() / expr.f() / self.field.f() edges in ripgrep: wrong 659 -> 502, correct 560 -> 646.

Changed-files rebuild vs full build (watch._rebuild_code, bat, changing src/printer.rs): 1118 / 1118 / 1118 calls edges, identical.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary (the in-file match in walk_calls, plus the method index it reads).
  • Added a regression test: tests/test_rust_in_file_call_binding.py, 7 tests; 6 fail on 0.9.80, test_enum_variant_constructor_is_still_a_reference passes on both.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

Limitations: x.f() with a receiver that is not self is still bound by name in-file (a typed-receiver pass is the follow-up). Trait-qualified calls (Default::default(), From::from(x)) and <T as Trait>::f() get no in-file edge. Type::f() where impl Type is in another file still gets no edge, as before.

How was this tested?

Project venv (Python 3.12.12, graphify from this branch via PYTHONPATH), Windows 11; rust-analyzer 1.97.1 for the ground truth:

python -m pytest tests/test_rust_in_file_call_binding.py -q   # 7 passed (on 0.9.80: 6 failed, 1 passed)
python -m pytest tests/test_rust_*.py -q                      # 42 passed
python -m pytest tests -q                                     # 23 failed, 6765 passed, 27 skipped; all 23 also fail on clean 0.9.80 on this machine (the usual 18, plus 4 no-cluster CLI tests that fail the same way there when run alone, plus the timing-sensitive mtime test, which passes alone on both), 0 new
python -m ruff check graphify tests/test_rust_in_file_call_binding.py   # All checks passed
pyright graphify/extractors/rust.py tests/test_rust_in_file_call_binding.py   # 9 errors, same 9 as 0.9.80, 0 new
bandit -ll graphify/extractors/rust.py                        # 0 findings, same as 0.9.80

Graphify-specific checklist

  • I updated generated skill artifacts — not needed, no skill fragments touched.
  • I confirmed that AST/structural extraction remains deterministic.
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed that no API keys or local-only graph data are included.
  • I disclosed AI authorship in my commit messages.

🤖 Generated with Claude Code

https://claude.ai/code/session_01B8gwEpjajiWHVW3c9smuKf

…-named function

extract_rust bound every in-file call through one bare-name index (last one wins),
so `Matcher::new()` reached `Printer::new()` when the file defined both, `Arc::new(x)`
reached the file's own `fn new`, `self.go()` another type's `go`, `drop(x)` the
`impl Drop` method and `x.total()` a free `fn total`, all EXTRACTED.

`Type::f()` / `Self::f()` / `self.f()` now bind only to that type's method in the file
(type = last path segment without generics, on the call and on the impl; `i32` and
other primitives count as types). With none, `Enum::Variant(x)` still gets its
constructor reference and `self.f()` falls through to the cross-file self pass. A bare
`f()` binds only to a free function or a type, `x.f()` only to a method.

Against rust-analyzer: wrong edges ripgrep 1232 -> 532, bat 138 -> 36, fd 29 -> 9;
correct edges 1852 -> 2236, 602 -> 655, 148 -> 154.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B8gwEpjajiWHVW3c9smuKf
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

Thanks for the pull request, @rohit-jsfreaky. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Looks safe to merge — no coupling regressions and no blocking issues, checked against the code graph (not a self-assessment).

Formal verification. PR-changed functions: 1/1 verified (0 proven, 1 may-equivalent, 0 distinguished) · 0 not verified.


Graphify review — findings

Fixes Rust in-file call binding to follow the call's shape. Type::f(), Self::f() and self.f() now bind only to that type's method from this file's impl blocks, and draw no in-file edge when the file doesn't define one, so Arc::new(x) no longer lands on a local fn new and unresolved self calls fall to the cross-file pass. Enum::Variant(x) falls back to the variant node, a bare f() reaches only free functions or constructors, and x.f() only methods. _rust_qualifier_type treats lowercase paths as modules, accepts primitives like i32, and strips generics and &/dyn from impl types.

No blocking issues surfaced. 3 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 65 functions depend on the 32 functions this change touches.

Health — this change adds coupling hotspots:

  • new: extract_rust() — 35 callers, 9 callees
  • new: walk() — 1 callers, 10 callees

Verification — 65 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 65 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

4 of 345 test file(s) selected (1%) via static blast radius.

  • tests/test_multilang.py — impact
  • tests/test_rust_in_file_call_binding.py — impact, changed-test
  • tests/test_rust_macro_rules.py — impact
  • tests/test_rust_self_member_calls.py — impact

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

No difference found (not proven): No behavior difference found in extract\_rust (not a proof).

The verifier ran both versions of extract\_rust on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

· 1 grounded finding(s) anchored inline below; 1 more finding(s) on lines outside this diff (see the check run).

return segments[-1] if segments else ""


def extract_rust(path: Path) -> dict:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Health regression — extract_rust()

fans out to 9 callees (efferent coupling); 35 callers depend on it (afferent coupling).

Grounded coupling-delta finding (deterministic), not an LLM guess.

@safishamsi

Copy link
Copy Markdown
Member

Landed in v0.9.82 via an authorship-preserving cherry-pick, so your commit is on v8 with you credited as the author. Closing as shipped — thanks @rohit-jsfreaky!

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.

[Bug]: Rust: an in-file call binds to any same-named function, so Matcher::new() reaches Printer::new() and Arc::new(x) reaches the file's own new

2 participants