Rust: no CALLS edge for any call inside a user `macro_rules!` macro (macro_invocation has no `arguments` field)

Open
#2,926 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust

Research direction

Start with internal/cbm/lsp/rust_lsp.c, especially rust_expand_user_macro, rust_walk_macro_tokens, and rust_inject_syn_call, then trace how call sites reach src/pipeline/pass_parallel.c:3170. Reproduce with the macro-probe example in the issue and inspect the indexed CALLS edges. Done means calls in macro arguments produce edges with source lines, with a regression test covering the behavior; the issue shows that fixing the token-tree lookup alone does not achieve this.

Written by the indexing model from the issue text.

Description

parsing/quality

Version: codebase-memory-mcp v0.12.1 (pkg/go/v0.12.1, a7158ac2). The same two lookups are on
main at 612eee25 (internal/cbm/lsp/rust_lsp.c:3562 and :4861).

What I did

I indexed jdx/mise v2026.11.2 and looked at install_missing_runtimes in src/cli/install.rs.
Its body wraps a call in mise's own timing macro:

measure!("install_all_versions", { ts.install_all_versions_with_progress(…) })

measure! is #[macro_export] macro_rules! measure { ($fmt:expr, $block:block) => {{ … $block … }} }
in src/timings.rs:93.

What happened

The function has CALLS edges for line 717 and then line 730, and none for lines 718-729, the whole
measure! body.

Minimal repro

Cargo.toml:

[package]
name = "macro-probe"
version = "0.1.0"
edition = "2021"

src/timings.rs:

#[macro_export]
macro_rules! measure_x {
    ($fmt:expr, $block:block) => {{ $block }};
}

src/main.rs:

#[macro_use]
mod timings;

macro_rules! measure_l {
    ($fmt:expr, $block:block) => {{ $block }};
}

macro_rules! body_l {
    () => {{ Ts.via_body() }};
}

fn free_body() -> u32 { 5 }
fn free_print() -> u32 { 7 }
fn free_arg() -> u32 { 6 }

macro_rules! body_f {
    () => {{ free_body() }};
}

struct Ts;
impl Ts {
    fn via_local(&self) -> u32 { 1 }
    fn via_cross(&self) -> u32 { 2 }
    fn via_plain(&self) -> u32 { 3 }
    fn via_body(&self) -> u32 { 4 }
}

fn run() -> u32 {
    let ts = Ts;
    let a = measure_l!("l", { ts.via_local() });
    let b = measure_x!("x", { ts.via_cross() });
    let c = ts.via_plain();
    let d = body_l!();
    let e = body_f!();
    let f = measure_l!("f", { free_arg() });
    println!("{}", free_print());
    a + b + c + d + e + f
}

fn main() { run(); }

It builds and prints 7. Make the folder a git repo with one commit, then:

codebase-memory-mcp cli index_repository '{"repo_path":"<abs>/macro-probe","name":"macro-probe","mode":"full"}'
codebase-memory-mcp cli query_graph '{"project":"macro-probe","query":"MATCH (a:Function {name:\"run\"})-[r:CALLS]->(b) RETURN b.name, r.line, r.strategy"}'
via_plain "32" lsp_method_dispatch
free_print - lsp_direct

run makes seven calls; only the first two get an edge:

call edge?
ts.via_plain() yes
free_print() inside println! yes (the edge has no line number)
ts.via_local() passed as $block to the same-file measure_l! no
free_arg() passed as $block to the same-file measure_l! no
ts.via_cross() passed to the #[macro_export] measure_x! from timings.rs no
Ts.via_body() written in the same-file body_l!'s own transcriber no
free_body() written in the same-file body_f!'s own transcriber no

Why

The pipeline only makes an edge for a syntactic call site (src/pipeline/pass_parallel.c:3170,
src/pipeline/pass_calls.c:1374). The LSP's resolved_calls are looked up for those sites. Inside
a macro, the tree-sitter-rust grammar gives the arguments as a token_tree with no
call_expression in it, so the generic extractor finds no site. The LSP adds synthetic sites
(rust_inject_syn_call, internal/cbm/lsp/rust_lsp.c:4045-4060) only for the built-in expression
macros (:3755-3772). Nothing covers user macros:

  1. rust_expand_user_macro (rust_lsp.c:3526-3652) reads a field that doesn't exist. It takes the
    invocation's arguments with ts_node_child_by_field_name(invocation, "arguments", 9) (:3546).
    In the vendored grammar, macro_invocation has only a macro field; its token_tree child has
    no field name. (I checked this by loading internal/cbm/vendored/grammars/rust into py-tree-sitter:
    the children are identifier (field macro), ! and token_tree (no field), and
    child_by_field_name("arguments") returns nothing.) So inv_args is always NULL, no pattern
    matches, and the first rule is used without substitution (:3571-3572): $block is never
    replaced by the caller's code.
  2. rust_walk_macro_tokens never runs. The macro_invocation handler's fallback for every
    other macro does the same lookup (:4816-4818), so it always gets a null node.
  3. Expanded source gets no site anyway. The expansion is walked with emit_site_start_byte =
    emit_site_end_byte = 0 (:3635), and the comment at :2918-2920 says "Generated macro_rules!
    source has no faithful mapped site and stays semantic only." That matches the probe: even calls
    written in the transcriber itself, which need no substitution, get no edge.
  4. Rules are per file. rust_collect_macro_rules (:3015-3050, called once at :5304) collects
    definitions from the current file only, so a #[macro_export] macro used from another file, as
    measure! is, is never expanded.

Proposal

  • Find the argument token_tree as the invocation's first named token_tree child instead of by
    field name, in both places (:3546 and :4816).
  • Map calls in a substituted $block/$expr fragment back to their bytes in the invocation, the
    way the built-in expression macros are mapped (rust_map_source_range), and inject a site for
    each resolved call.
  • Optionally, collect #[macro_export] rules across the crate.
  • Add a test that indexes a call passed to a macro_rules! macro and expects a CALLS edge with its
    line.

Fixing the field lookup alone brings back no edge

I rebuilt v0.12.1 with both lookups replaced by "first named token_tree child" and indexed the
probe above again (a direct cbm_pipeline_run driver, like tests/test_integration.c). The CALLS edges
are the same as before: only ts.via_plain() and the call inside println!. With stderr prints
added, the expansion does now run with bound arguments, for example measure_l!("l", { ts.via_local() })
matches and expands to fn __cbm_macro_expand() { { { ts.via_local() } }; }. Each of the four
same-file expansions pushes one resolved call with the right caller (...main.run). They are
dropped later:

  • The call-edge pass (src/pipeline/pass_parallel.c:3170) loops over the extractor's CBMCall
    sites and only attaches a resolved call to one of them. The extractor sees a token_tree, not a
    call, so there is no carrier.
  • A resolved call can create its own carrier only through rust_inject_syn_call, which pushes a
    CBMCall (rust_lsp.c:4060). rust_emit_resolved_call_reason calls it only when
    emit_site_end_byte > emit_site_start_byte (rust_lsp.c:4111-4113), and the expansion sets both
    to 0. The comment at pass_parallel.c:4026-4032 says this is on purpose: "Zero-span generated
    semantics are excluded: without source provenance they cannot safely join a graph carrier."
  • rust_walk_macro_tokens now gets the token_tree, but it only looks for call_expression,
    macro_invocation and field_expression nodes (:2831-2846), which a token tree never has. It
    pushed 0 resolved calls on every invocation.
  • The cross-file measure_x! was never expanded (point 4).

So the field fix is needed but not enough: the second proposal item, a real site for each call in
a substituted fragment, is what would bring the edges back.

Dominant language
C
Stars
45.8k
Forks
3.8k
Avg merge
2d 18h
Merged PRs (30d)
108

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DeusData/codebase-memory-mcp

All issues in DeusData/codebase-memory-mcp

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.