Skip to content

fix(rust): link calls written inside macro arguments - #4945

Closed
nothariharan wants to merge 2 commits into
Graphify-Labs:v8from
nothariharan:fix/rust-macro-arg-calls
Closed

nothariharan wants to merge 2 commits into
Graphify-Labs:v8from
nothariharan:fix/rust-macro-arg-calls

Conversation

@nothariharan

Copy link
Copy Markdown
Contributor

Problem

Tree-sitter keeps a Rust macro's arguments as raw tokens, so println!("{}", f(x)), assert_eq!(f(), 1) and vec![f()] produced no edge. A function used only inside formatting or assert macros showed no callers.

Change

Scan for name(...) in the token tree of the std and log macros whose arguments are plain expressions (println!, format!, assert*!, vec!, dbg!, panic!, write!, info!, and so on):

  • skip names after ., ::, # or $, names followed by !, UpperCamelCase constructors, and names on the existing builtin and trait-method blocklists;
  • an in-file match is INFERRED 0.85, since it's a token pattern rather than a parsed call. Anything else goes to the cross-file pass as a bare call;
  • macro edges are added after the bodies are walked, so a real parsed call to the same target keeps its EXTRACTED edge;
  • DSL macros (table!, quote!, html!) are not scanned, and macro_rules! bodies are untouched.

Evidence

  • 9 tests in tests/test_rust_macro_argument_calls.py. The positive cases, the dedupe case and the DSL case fail without the fix.
  • Repro crate: in_macro() -> via_macro_target() now appears across files. Two builds give identical graphs.
  • Full suite: 9000 passed. The 14 failures are the same ones plain v8 has on this machine. Ruff and pyright are clean.
  • I didn't bump the AST cache schema, so it can go into the batch bump.

Tree-sitter keeps a macro's arguments as a flat token tree, so
`println!("{}", f(x))`, `assert_eq!(f(), 1)` and `vec![f()]` had no
call_expression and the call produced no edge: `explain f` showed no
callers for a function used only inside formatting or assert macros.

Scan each macro invocation's token tree for an identifier followed by a
`(` group and treat it as a bare call. A name after `.` or `::`, a name
followed by `!`, uppercase names (constructors and variants) and the
builtin/trait-method blocklists are skipped. An in-file match is INFERRED,
since a token pattern is weaker evidence than a parsed call; anything else
goes to the cross-file pass like any other bare call. macro_rules! bodies
are macro_definition nodes and are not scanned.
… dedupe

Review follow-ups to the previous commit.

Any `name(...)` inside any macro was taken as a call, so DSL macros
(diesel `table!`, `quote!` with `#name(...)`, `html!`) produced calls the
code never makes. Scan only the std and log macros whose arguments are
plain expressions, and skip a name after `#`.

The in-file macro edge took the (caller, target) pair first, so a real
`compute(2)` later in the same function was deduped behind the INFERRED
token-pattern edge. Macro edges are now added after the function bodies
are walked, only for pairs no parsed call produced.
@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

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

View in Graphify

Graphify reviewed this change.

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 1 changed function(s) (1 sampled, not proven).

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


Graphify review — findings

Adds call edges for bare f(...) calls written inside the arguments of expression-style macros such as println!, assert_eq!, vec! and log::info!, which tree-sitter leaves as a flat token tree and previously produced no edge. In-file matches become INFERRED calls edges with a macro_argument_call context, and unresolved names are passed to the cross-file pass. These edges are appended after all parsed calls, so a real call_expression to the same target keeps its EXTRACTED edge, and the new edges now sit alongside a set of Rust calls edges that were all EXTRACTED with a plain call context. DSL macros (table!, quote!, html!), macro_rules! bodies, method and path calls, nested macro names and constructors are skipped, so a name that merely looks like a call isn't linked, though cross-file hits from the macro path risk being resolved without the INFERRED marking.

Worth a look

  • Macro-argument call edges break the invariant that every Rust calls edge is EXTRACTED with context 'call' — tests/test_multilang.py:351 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 91 functions depend on the 42 functions this change touches.

Health — grade A; 3 existing hotspot(s) in the area this change touches (pre-existing, not introduced here):

  • extract_rust() — 38 callers, 10 callees (high)
  • walk() — 1 callers, 14 callees (high)
  • test_a_cache_entry_from_before_child_mod_names_is_not_served() — 0 callers, 6 callees (medium)

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

Test selection

Test selection

7 of 490 test file(s) selected (1%) via static blast radius.

  • tests/test_multilang.py — impact
  • tests/test_rust_in_file_call_binding.py — impact
  • tests/test_rust_macro_argument_calls.py — impact, changed-test
  • tests/test_rust_macro_rules.py — impact
  • tests/test_rust_self_member_calls.py — impact
  • tests/test_rust_type_alias_union.py — impact
  • tests/test_rust_use_trees.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.

safishamsi added a commit that referenced this pull request Nov 2, 2026
…ase-preserving cache fixes

#4944 resolves Rust module-qualified calls, #4945 links calls inside
expression-macro arguments, and #4937 keeps cache keys case-preserving
(which also shifts the POSIX salt digest for uppercase-bearing paths).
All three change cached output, so warm caches must re-run.

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

Copy link
Copy Markdown
Member

Landed in 0.9.101 (published to PyPI), cherry-picked with your authorship preserved. Thanks @nothariharan.

@safishamsi safishamsi closed this Nov 2, 2026
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.

2 participants