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

Ouverte
#2,926 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
45/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
rust

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

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.

Langage dominant
C
Étoiles
45.8k
Forks
3.8k
Merge moyen
2 j 18 h
PR mergées (30 j)
108

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de DeusData/codebase-memory-mcp

Toutes les issues de DeusData/codebase-memory-mcp

Issues similaires

Plus d'issues C

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.