Skip to content

fix(rust): land a use on the declaration it imports - #4443

Closed
bercedev wants to merge 2 commits into
Graphify-Labs:v8from
bercedev:fix/rust-use-resolve
Closed

bercedev wants to merge 2 commits into
Graphify-Labs:v8from
bercedev:fix/rust-use-resolve

Conversation

@bercedev

@bercedev bercedev commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #4439.

A Rust use adds an imports_from edge whose target is the bare, case-folded name it imports (invoice for use crate::model::Invoice;). That only reached the declaration when a type annotation somewhere had left a sourceless node under the same id for _rewire_unique_stub_nodes to remap. When the type is only constructed or called, the edge dangled, and since 0.9.63 build mints the target as an external stub that ties with the real declaration, so graphify affected Invoice and graphify explain Invoice stop resolving.

What changes:

  • The use edge carries the imported name as written (_rust_use_name, stripped before output like the other _rust_use_* markers), because the id has already folded its case and the rewire matches case-sensitively.
  • Right before the rewire, each imported name that has no node gets a sourceless one lent to it. The lent nodes are never kept: an import with no unique declaration dangles exactly as it does today, and build mints it as before.
  • Only a path rooted at crate, self or super is resolved. use serde::Serialize; or use std::fs::File; names another crate's item, never the corpus's own Serialize or File (second commit, after the review bot's finding).
  • An incremental build re-extracts only the changed files, so the rewire now also takes resolution_context_nodes as candidates. Without that, watch resolved less than a full build and test_incremental_builds_match_full_builds caught it.
  • AST cache schema 15 -> 16, since cached edges don't have the name.

I first tried emitting the stub from the extractor. That changed other passes (in-file call resolution bound to it, and the colliding-id pass renamed existing stubs), so the node only exists for the duration of the rewire.

Type of change

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

Verification & Invariants

Invariant: the only thing that moves is the target of a Rust imports_from edge whose name has exactly one declaration.

I checked that on a real corpus, the RustDesk source tree (289 .rs files), comparing extract() output before and after:

imports_from edges retargeted to a declaration: 489
other edges added or removed:                   0
nodes added or removed:                         0
confidence changes on existing edges:           0
  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

Limitations:

  • Resolution is by name, the same rule type annotations already get. A name declared twice stays unresolved even when the use path says which one is meant (test_a_use_of_a_name_declared_twice_stays_unresolved).
  • The edge target is a shared id, so once one file imports the crate's own Config through crate::, a use other_crate::Config; elsewhere follows it. On RustDesk about 38 of the 489 edges come from a path that is not rooted at crate/self/super, mostly hbb_common:: and scrap::, which are workspace crates there. An annotation naming the type does the same today.
  • Workspace crates are not resolved on their own: use hbb_common::Foo; only lands when something else already resolved that name.
  • Three existing expectations in tests/test_rust_use_trees.py pinned the bare target (parse, render) and now expect the declaration id. That is the behaviour change this PR is about, but it does edit tests that were written on purpose, so please look at those.
  • The resolution_context_nodes change is on a path every language shares. The full suite passes; I have no incremental corpus for other languages to compare.

How was this tested?

# the repro from the issue, before
graphify affected Invoice   -> No unique node match for Invoice
graphify explain Invoice    -> Ambiguous: 'Invoice' matches 2 nodes in different files.

# after
graphify affected Invoice   -> make() [references], billing.rs [imports_from]
graphify explain Invoice    -> Node: Invoice, ID: src_model_invoice

uv run --frozen pytest tests/test_rust_use_trees.py -q
33 passed

uv run --frozen pytest -q
7773 passed, 184 skipped

Linux, Python from the repo's uv.lock.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments.
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

Note: I used Claude Code while working on this fix. I reviewed and ran all of it myself.

…4439)

A Rust `use` targets the bare, case-folded name it imports. That reached the
declaration only when a type annotation in the corpus had left a sourceless
node under the same id for the stub rewire to remap. Otherwise the edge
dangled, and since 0.9.63 build mints the target as an external stub that
ties with the real declaration in `affected` and `explain`.

The `use` edge now carries the name as written, and the rewire is lent a
sourceless node for each imported name that has none. The lent nodes are not
kept, so an import with no unique declaration dangles exactly as before.
Standard-library paths are left out.

An incremental build re-extracts only the changed files, so the rewire also
takes the unchanged ones as candidates; without that it resolved less than a
full build.

Bumps the AST cache schema: cached edges lack the name.
@bercedev
bercedev requested a review from safishamsi as a code owner October 13, 2026 22:42
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @bercedev. 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.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 13, 2026

@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.

Code graph out of date: Health delta baseline: last indexed commit 6996bcd, 3 commit(s) behind this PR's base. Impact and hotspot numbers below may be wrong.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 1 advisory finding(s) below merit a look before merge.

Not checked: tests were not run; formal verification proved 0 of 2 changed function(s) (2 sampled, not proven).

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


Graphify review — findings

Fixes Rust use imports so they resolve to their corpus declaration whenever the imported name is declared exactly once, instead of only when a type annotation left a sourceless node to rewire. Each import edge is tagged with _rust_use_name (skipped for std/core/alloc), and extract lends _rewire_unique_stub_nodes a temporary stub per name, plus unchanged nodes on incremental builds; the stubs are discarded afterwards, so ambiguous or undeclared imports still dangle. Bumps _AST_CACHE_SCHEMA to 16, so cached ASTs are re-extracted.

Worth a look

  • Imports from external (non-std) crates are rewired onto same-named local declarations — graphify/extractors/rust.py:724 · Escalate · medium
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 3456 functions depend on the 458 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: walk() — 1 callers, 13 callees

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

Health delta baseline: last indexed commit 6996bcd, 3 commit(s) behind this PR's base.

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: 3213 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

214 of 418 test file(s) selected (51%) via static blast radius.

  • tests/test_affected_overrides.py — impact
  • tests/test_astro_extraction.py — impact
  • tests/test_astro_import_ids.py — impact
  • tests/test_blade_extractor.py — impact
  • tests/test_build.py — impact
  • tests/test_builtin_global_type_refs.py — impact
  • tests/test_c_include_resolution.py — impact
  • tests/test_cache.py — impact
  • tests/test_cache_stale_import_target.py — impact
  • tests/test_case_sensitive_resolution.py — impact
  • tests/test_charmap_encoding.py — impact
  • tests/test_chunking.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cobol_extractor.py — impact
  • tests/test_cpp_method_declarations.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cpp_template_type_params.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_cross_language_call_resolution.py — impact
  • tests/test_cross_repo_external_call_guards.py — impact
  • tests/test_cross_repo_member_calls.py — impact
  • tests/test_csharp_call_site_generic_args.py — impact
  • tests/test_csharp_enum_members.py — impact
  • tests/test_csharp_field_generic_args.py — impact
  • tests/test_csharp_foreach_loop_variable.py — impact
  • tests/test_csharp_generic_callsites.py — impact
  • tests/test_csharp_interface_dispatch.py — impact
  • tests/test_csharp_member_calls.py — impact
  • tests/test_csharp_member_chain_calls.py — impact
  • tests/test_csharp_member_nodes.py — impact
  • tests/test_csharp_object_creation.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_csharp_tuple_type_refs.py — impact
  • tests/test_csharp_type_resolution.py — impact
  • tests/test_csharp_underscore_collisions.py — impact
  • tests/test_csharp_var_call_return_type.py — impact
  • tests/test_csharp_verbatim_interpolation.py — impact
  • tests/test_definition_file_portability.py — impact
  • tests/test_detect.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_duplicate_annotation_edges.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_elixir_defdelegate.py — impact
  • tests/test_elixir_import_resolution.py — impact
  • tests/test_elixir_keyword_def_calls.py — impact
  • tests/test_elixir_qualified_calls.py — impact
  • tests/test_elixir_unqualified_call_scope.py — impact
  • tests/test_erlang_extractor.py — impact
  • tests/test_extract.py — impact
  • … and 164 more

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 (not a proof).

The verifier ran both versions of extract 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.

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 more finding(s) on lines outside this diff (see the check run).

The review bot is right that `use serde::Serialize;` should not land on a
local `Serialize`. Leaving out std/core/alloc was not enough: any other
crate has the same problem. A `use` is now resolved only when its path
starts at `crate`, `self` or `super`.
@liam-mcelhaney122

liam-mcelhaney122 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for taking this on, the by-name rewire is a clean, low-risk way to fix the case in #4439, and the second commit (only landing use paths rooted at crate/self/super) is the right call for name matching.

I ran this branch (ee27336a) against a 519-file multi-crate Cargo workspace next to 0.9.90 to see how far it goes, and wrote up the numbers on the issue: #4439 (comment). Short version: dangling imports_from edges drop from 4,591 to 2,619 and minted external stubs from 1,524 to 805, so it does what it says. Here's what it leaves behind on a workspace, in case it's useful for scoping a follow-up (or for deciding whether any of it belongs in this PR):

  1. Cross-crate imports. use core_lib::Stack; still dangles on a bare stack, because the crate-rooted restriction skips everything that isn't crate/self/super. In a workspace those paths do name corpus items, and the manifests say which crate they live in. On the workspace above, 616 of the edges my branch retargets are workspace-crate-prefixed paths of this kind.
  2. Re-export chains. Matching by name works until a name is declared twice, which your limitations note already calls out (test_a_use_of...). Following pub use item::Stack through the module tree resolves the real target and keeps the edge EXTRACTED instead of leaning on "exactly one declaration".
  3. Globs. use super::*; and use crate::a::b::*; still produce an edge to a bare super/b. That's 243 edges on this workspace, 206 of them use super::* inside #[cfg(test)] mod tests.
  4. Placeholder type references. An imported type that's then used in a field, signature or impl leaves a per-file <file>_rs_<name> node. Those are untouched here (1,619 placeholder nodes before and after; about 5,900 references edges still point at them), since only the imports_from edge is retargeted.

A four-file repro that shows 1 and 3 side by side is in the issue comment linked above (cross-crate use plus a use super::* test module): on 0.9.90 and on this branch both edges still dangle.

The approach I took for those cases is in https://github.com/liam-mcelhaney122/graphify/tree/fix/rust-use-path-resolution. The edge carries the path as written (metadata.use_path) and a separate pass resolves it through the module tree and the crate manifests, so exact resolution stays EXTRACTED and only name-in-crate fallbacks are marked INFERRED. It's a much bigger diff than yours (about 500 lines in a new module), so I'm not proposing to land it in place of this PR. My plan is to rebase it onto yours once this merges and open it as a follow-up, so that only the cross-crate, re-export, glob and placeholder parts remain. Both PRs bump the AST cache schema to 16, so whichever lands second just needs to move to 17.

If you'd rather fold any of this into this PR, or want me to split it differently, let me know.

@bercedev

Copy link
Copy Markdown
Contributor Author

Thanks for running it against a real workspace, those numbers are really useful. I'd like to keep this one narrow so it stays easy to review, so please go ahead with the follow-up as you planned. Your module-tree and manifest resolution is the right place for cross-crate paths, re-exports and globs. Happy to rebase onto 17 if yours lands first.

safishamsi added a commit that referenced this pull request Oct 14, 2026
Eight community PRs: Windows MCP-stdio hang (#4441), Rust use->declaration
(#4443), JS/TS file-scope callback param shadowing (#4457), sibling-file-id
symbol retention (#4452), TS class-field receiver scoping (#4297), Ollama
/api/chat num_ctx (#4414), cluster label retention on majority overlap (#4451),
and an ingest symlink test fix (#4442). AST cache schema bumped to 16.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Landed in 0.9.91 as ef1d256 + 5faa05c (cherry-picked with your authorship preserved, so it shows on your contributions). Thanks @bercedev! Released to PyPI: https://pypi.org/project/graphifyy/0.9.91/

@safishamsi safishamsi closed this Oct 14, 2026
@bercedev
bercedev deleted the fix/rust-use-resolve branch October 14, 2026 23:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Rust: use edges target a bare name, and since 0.9.63 the stub they leave makes affected/explain ambiguous

3 participants