Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(rust): read a mod declaration inside a macro as a module import (#2978)

Raghav Chamadiyae470dc287e08
diff --git a/packages/core/src/repowise/core/ingestion/extractors/bindings/rust.py b/packages/core/src/repowise/core/ingestion/extractors/bindings/rust.py
index 30c0ea84a..2a4959494 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/bindings/rust.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/bindings/rust.py
@@ -4,7 +4,7 @@ from __future__ import annotations
from tree_sitter import Node
-from ...models import NamedBinding
+from ...models import Import, NamedBinding
from ..helpers import node_text
@@ -120,3 +120,43 @@ def _walk_use_tree(
exported = path.rsplit("::", 1)[-1]
if exported and exported != "*":
leaves.append((path, alias or exported, exported))
+
+
+def macro_mod_imports(token_tree: Node, raw: str) -> list[Import]:
+ """One ``mod`` import per module a macro's tokens declare.
+
+ ``cfg_if! { if #[cfg(unix)] { mod unix; } }`` declares ``unix`` in the
+ module that makes the call, the same as a ``mod unix;`` item there.
+ """
+ return [
+ Import(
+ raw_statement=raw,
+ module_path=name,
+ imported_names=["*"],
+ is_relative=False,
+ resolved_file=None,
+ bindings=[NamedBinding(local_name="*", exported_name=None, source_file=None)],
+ )
+ for name in dict.fromkeys(macro_body_mod_names(token_tree))
+ ]
+
+
+def macro_body_mod_names(token_tree: Node) -> list[str]:
+ """Module names declared in a macro token tree, nested trees included.
+
+ Only the exact tokens ``mod``, a plain name and ``;`` count: a ``$name``
+ metavariable or a ``mod x { ... }`` with a body is no file declaration,
+ and a declaration inside such a body belongs to that inline module, so
+ its tokens are not read.
+ """
+ kids = token_tree.children
+ types = [kid.type for kid in kids]
+ names = [
+ node_text(kids[i + 1], "")
+ for i in range(len(kids) - 2)
+ if types[i : i + 3] == ["mod", "identifier", ";"]
+ ]
+ for i, kid in enumerate(kids):
+ if kid.type == "token_tree" and types[max(i - 2, 0) : i] != ["mod", "identifier"]:
+ names.extend(macro_body_mod_names(kid))
+ return names
diff --git a/packages/core/src/repowise/core/ingestion/graph_warmups.py b/packages/core/src/repowise/core/ingestion/graph_warmups.py
index cb5f7174c..5b357619f 100644
--- a/packages/core/src/repowise/core/ingestion/graph_warmups.py
+++ b/packages/core/src/repowise/core/ingestion/graph_warmups.py
@@ -24,6 +24,8 @@ import contextlib
from collections.abc import Callable
from typing import TYPE_CHECKING, Any
+import structlog
+
from .languages.specs.cpp import INCLUDE_FRAGMENT_EXTENSIONS
if TYPE_CHECKING:
@@ -315,9 +317,17 @@ def _warmup_rust(ctx: ResolverContext) -> None:
three are runner-loaded the same way a test file is, so they become
reachability roots instead — the same "roots, not entry points" split the
TypeScript warmup above makes for its own non-manifest entry paths.
+
+ First, ``mod`` items a ``macro_rules!`` body declares join the imports of
+ the files that call the macro, before any import is resolved.
"""
+ from .resolvers.rust import add_macro_rules_mod_imports
from .resolvers.rust_workspace import get_or_build_cargo_workspace_index
+ try:
+ add_macro_rules_mod_imports(ctx)
+ except Exception as exc: # the pass must not abort the build
+ structlog.get_logger(__name__).debug("rust_macro_mods_failed", error=str(exc))
index = get_or_build_cargo_workspace_index(ctx)
if index is None:
return
diff --git a/packages/core/src/repowise/core/ingestion/parser.py b/packages/core/src/repowise/core/ingestion/parser.py
index c340048df..69ba70e79 100644
--- a/packages/core/src/repowise/core/ingestion/parser.py
+++ b/packages/core/src/repowise/core/ingestion/parser.py
@@ -984,6 +984,11 @@ def _statement_imports(
return [_dart_import(stmt_node, module_node, module_text, raw, src)]
if language == "rust" and stmt_node.type == "use_declaration":
return _rust_use_imports(stmt_node, module_text, raw, src)
+ if language == "rust" and stmt_node.type == "macro_invocation":
+ from .extractors.bindings.rust import macro_mod_imports
+
+ # Most top-level calls (``s! { struct ... }``) declare no module.
+ return macro_mod_imports(module_node, raw) if "mod" in raw else []
if language in _TS_JS_LANGUAGES and _is_dynamic_esm_import(stmt_node):
# ``import('./mod')`` binds a module namespace at runtime, so it is a
# wildcard, which keeps the target's exports live.
diff --git a/packages/core/src/repowise/core/ingestion/queries/rust.scm b/packages/core/src/repowise/core/ingestion/queries/rust.scm
index 157cf67a4..b415cc19e 100644
--- a/packages/core/src/repowise/core/ingestion/queries/rust.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/rust.scm
@@ -176,6 +176,14 @@
!body
) @import.statement
+;; A macro call at the top of a file (`cfg_if! { if #[cfg(unix)] { mod unix; } }`)
+;; can declare child modules in its tokens; the parser reads them out.
+(source_file
+ (macro_invocation (token_tree) @import.module) @import.statement)
+(source_file
+ (expression_statement
+ (macro_invocation (token_tree) @import.module) @import.statement))
+
;; extern crate foo; / extern crate foo as bar;
(extern_crate_declaration
name: (identifier) @import.module
diff --git a/packages/core/src/repowise/core/ingestion/resolvers/rust.py b/packages/core/src/repowise/core/ingestion/resolvers/rust.py
index fb531ec1a..c79aad85f 100644
--- a/packages/core/src/repowise/core/ingestion/resolvers/rust.py
+++ b/packages/core/src/repowise/core/ingestion/resolvers/rust.py
@@ -4,9 +4,13 @@ from __future__ import annotations
from functools import lru_cache
from pathlib import Path
+from typing import TYPE_CHECKING, Any
from .context import ResolverContext
+if TYPE_CHECKING:
+ from tree_sitter import Node
+
def _get_frozen_path_set(ctx: ResolverContext) -> frozenset[str]:
"""Return a cached frozenset of ctx.path_set, built once per context."""
@@ -341,3 +345,111 @@ def _probe_rust_path(
) -> str | None:
"""Probe for a Rust module path, trying ``.rs`` and ``mod.rs`` variants."""
return _probe_rust_path_cached(base_dir, tuple(path_parts), path_set)
+
+
+def add_macro_rules_mod_imports(ctx: ResolverContext) -> int:
+ """Give each top-level ``name!()`` call the modules ``macro_rules! name`` declares.
+
+ A ``mod x;`` in a ``macro_rules!`` body is a template: it declares ``x`` in
+ the module that calls the macro, and nowhere if nothing calls it. The
+ definition and the call are paired by name within one crate (serde defines
+ ``crate_root!`` in ``crate_root.rs`` and calls it from ``lib.rs``). The
+ imports join the calling file's, so they resolve from there. Returns the
+ number added.
+ """
+ from tree_sitter import Parser
+
+ from ..parser import _get_language # local import: avoid a cycle at module load
+
+ language = _get_language("rust")
+ sources = _rust_sources(ctx)
+ if language is None or not any(b"macro_rules!" in src for src in sources.values()):
+ return 0
+ parser = Parser(language)
+ templates: dict[tuple[str, str], list[Node]] = {}
+ for path, src in sources.items():
+ if b"macro_rules!" in src:
+ crate_root = _find_rust_crate_root(path, ctx)
+ for name, bodies in _mod_declaring_macros(parser.parse(src).root_node):
+ templates.setdefault((crate_root, name), []).extend(bodies)
+ if not templates:
+ return 0
+ return sum(
+ _add_called_macro_mods(ctx, path, src, templates, parser) for path, src in sources.items()
+ )
+
+
+def _rust_sources(ctx: ResolverContext) -> dict[str, bytes]:
+ parsed_files = ctx.parsed_files or {}
+ sources = ctx.source_map or {}
+ return {
+ path: sources[path]
+ for path, parsed in parsed_files.items()
+ if parsed is not None and parsed.file_info.language == "rust" and path in sources
+ }
+
+
+def _mod_declaring_macros(root: Node) -> list[tuple[str, list[Node]]]:
+ """``(name, rule bodies)`` of each top-level ``macro_rules!`` that declares a module."""
+ from ..extractors.bindings.rust import macro_body_mod_names
+
+ found = []
+ for node in root.children:
+ if node.type != "macro_definition":
+ continue
+ rules = (
+ rule.child_by_field_name("right")
+ for rule in node.children
+ if rule.type == "macro_rule"
+ )
+ bodies = [body for body in rules if body is not None]
+ if any(macro_body_mod_names(body) for body in bodies):
+ found.append((_node_name(node.child_by_field_name("name")), bodies))
+ return found
+
+
+def _add_called_macro_mods(
+ ctx: ResolverContext,
+ path: str,
+ src: bytes,
+ templates: dict[tuple[str, str], list[Node]],
+ parser: Any,
+) -> int:
+ """Append to *path*'s imports the modules of each template it calls."""
+ from ..extractors.bindings.rust import macro_mod_imports
+
+ crate_root = _find_rust_crate_root(path, ctx)
+ names = {name for root, name in templates if root == crate_root and f"{name}!".encode() in src}
+ if not names:
+ return 0
+ imports = ctx.parsed_files[path].imports
+ have = {imp.module_path for imp in imports if imp.imported_names == ["*"]}
+ called = [
+ imp
+ for name in _top_level_macro_calls(parser.parse(src).root_node, names)
+ for body in templates[(crate_root, name)]
+ for imp in macro_mod_imports(body, f"{name}!()")
+ ]
+ added = 0
+ for imp in called:
+ if imp.module_path not in have:
+ have.add(imp.module_path)
+ imports.append(imp)
+ added += 1
+ return added
+
+
+def _top_level_macro_calls(root: Node, names: set[str]) -> list[str]:
+ """Which of *names* the file calls as a macro at its top level, in order."""
+ called: dict[str, None] = {}
+ for node in root.children:
+ call = node.children[0] if node.type == "expression_statement" and node.children else node
+ if call.type == "macro_invocation":
+ name = _node_name(call.child_by_field_name("macro"))
+ if name in names:
+ called[name] = None
+ return list(called)
+
+
+def _node_name(node: Node | None) -> str:
+ return (node.text or b"").decode() if node is not None else ""
diff --git a/tests/unit/ingestion/test_rust_module_paths.py b/tests/unit/ingestion/test_rust_module_paths.py
index 8824c2b8a..fd3728f71 100644
--- a/tests/unit/ingestion/test_rust_module_paths.py
+++ b/tests/unit/ingestion/test_rust_module_paths.py
@@ -219,3 +219,90 @@ class TestImportedNamesInScope:
def test_a_glob_import_brings_every_name_into_scope(self, tmp_path: Path) -> None:
caller = "use crate::a::*;\npub fn go() { other(); }\n"
assert "src/a.rs::other" in self._callees(tmp_path, caller)
+
+
+class TestModInMacroBody:
+ """``mod foo;`` written in macro tokens declares ``foo`` where the macro runs."""
+
+ @staticmethod
+ def _build(repo: Path, sources: dict[str, str]) -> nx.DiGraph:
+ builder = GraphBuilder(repo_path=repo)
+ source_map = {}
+ for rel, body in sources.items():
+ p = repo / rel
+ p.parent.mkdir(parents=True, exist_ok=True)
+ p.write_text(body, encoding="utf-8")
+ source_map[rel] = body.encode("utf-8")
+ info = _file_info(rel, str(p.resolve()))
+ builder.add_file(_PARSER.parse_file(info, source_map[rel]))
+ builder.set_source_map(source_map)
+ return builder.build()
+
+ def test_only_plain_declarations_in_a_top_level_call_are_imports(self) -> None:
+ body = (
+ "cfg_if::cfg_if! {\n"
+ " if #[cfg(unix)] { mod unix; } else { pub(crate) mod windows; }\n"
+ "}\n"
+ "cfg_if! { if #[cfg(x)] { mod $name; mod inline { mod nested; } } }\n"
+ "macro_rules! template { () => { mod templated; }; }\n"
+ "fn f() { cfg_if! { mod in_fn; } }\n"
+ "mod m { cfg_if! { mod in_inline_mod; } }\n"
+ )
+ assert _imports(body) == [("unix", ["*"]), ("windows", ["*"])]
+
+ def test_macro_rules_mod_resolves_from_the_calling_file(self, tmp_path: Path) -> None:
+ graph = self._build(
+ tmp_path,
+ {
+ "src/lib.rs": "#[macro_use]\nmod macros;\ncrate_root!();\n",
+ "src/macros.rs": "macro_rules! crate_root {\n () => { mod format; };\n}\n",
+ "src/format.rs": "pub struct Buf;\n",
+ "src/macros/format.rs": "pub struct Decoy;\n",
+ },
+ )
+ assert graph.has_edge("src/lib.rs", "src/format.rs")
+ assert not graph.has_edge("src/macros.rs", "src/macros/format.rs")
+ unused = TestIntraCrateDeadCode()._unused(graph)
+ assert "src/format.rs" not in unused
+ assert "src/format.rs::Buf" not in unused
+
+ def test_macro_rules_nobody_calls_declares_nothing(self, tmp_path: Path) -> None:
+ graph = self._build(
+ tmp_path,
+ {
+ "src/lib.rs": "macro_rules! unused {\n () => { mod ghost; };\n}\n",
+ "src/ghost.rs": "pub struct Ghost;\n",
+ },
+ )
+ assert not graph.has_edge("src/lib.rs", "src/ghost.rs")
+ assert "src/ghost.rs" in TestIntraCrateDeadCode()._unused(graph)
+
+ def test_macro_rules_mod_called_from_a_submodule_resolves_under_it(
+ self, tmp_path: Path
+ ) -> None:
+ graph = self._build(
+ tmp_path,
+ {
+ "src/lib.rs": "#[macro_use]\nmod macros;\nmod sub;\n",
+ "src/macros.rs": "macro_rules! declare {\n () => { mod x; };\n}\n",
+ "src/sub.rs": "declare!();\n",
+ "src/sub/x.rs": "pub struct X;\n",
+ "src/x.rs": "pub struct Decoy;\n",
+ },
+ )
+ assert graph.has_edge("src/sub.rs", "src/sub/x.rs")
+ assert not graph.has_edge("src/sub.rs", "src/x.rs")
+
+ def test_same_macro_name_in_another_crate_is_not_paired(self, tmp_path: Path) -> None:
+ template = "macro_rules! declare {\n () => { mod x; };\n}\n"
+ graph = self._build(
+ tmp_path,
+ {
+ "a/src/lib.rs": template + "declare!();\n",
+ "a/src/x.rs": "pub struct A;\n",
+ "b/src/lib.rs": template,
+ "b/src/x.rs": "pub struct B;\n",
+ },
+ )
+ assert graph.has_edge("a/src/lib.rs", "a/src/x.rs")
+ assert not graph.has_edge("b/src/lib.rs", "b/src/x.rs")