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.

feat(rust): capture bodiless fn declarations as symbols (#2382)

Raghav Chamadiyaca5bafa664ce
diff --git a/packages/core/src/repowise/core/ingestion/extractors/signatures.py b/packages/core/src/repowise/core/ingestion/extractors/signatures.py
index 858239c10..66a470070 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/signatures.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/signatures.py
@@ -33,8 +33,10 @@ def build_signature(node_type: str, name: str, params_text: str, def_node: Node,
# Detect async via child "async" keyword (tree-sitter-python >= 0.23)
prefix = "async " if any(c.type == "async" for c in def_node.children) else ""
return f"{prefix}def {name}{params_text}{_ret(('return_type',))}"
- if node_type == "function_item":
- # Rust: return_type field
+ if node_type in ("function_item", "function_signature_item"):
+ # Rust: return_type field. A bodiless ``function_signature_item`` carries
+ # the same ``name`` / ``parameters`` / ``return_type`` fields, so it
+ # takes this branch rather than the bare-name fallback at the end.
return f"fn {name}{params_text}{_ret(('return_type',))}"
if node_type in ("function_declaration", "generator_function_declaration"):
# TS/JS use return_type; Go uses result
diff --git a/packages/core/src/repowise/core/ingestion/language_configs.py b/packages/core/src/repowise/core/ingestion/language_configs.py
index 89003ce30..a3733b0f0 100644
--- a/packages/core/src/repowise/core/ingestion/language_configs.py
+++ b/packages/core/src/repowise/core/ingestion/language_configs.py
@@ -161,6 +161,7 @@ LANGUAGE_CONFIGS: dict[str, LanguageConfig] = {
"rust": LanguageConfig(
symbol_node_types={
"function_item": "function",
+ "function_signature_item": "function",
"struct_item": "struct",
"enum_item": "enum",
"trait_item": "trait",
@@ -184,6 +185,11 @@ LANGUAGE_CONFIGS: dict[str, LanguageConfig] = {
# keeps its kind at ``function``, since that upgrade is gated on having
# a parent.
parent_class_types=frozenset({"impl_item", "mod_item", "trait_item"}),
+ # A bodiless ``fn foo();`` is a declaration in the same sense a C
+ # prototype is: it promises a body it does not carry. Unmarked it reads
+ # as a definition, which puts a trait's method names into the global
+ # bare-name index that call resolution consults for unqualified calls.
+ declaration_node_types=frozenset({"function_signature_item"}),
reference_call_node_types=frozenset({"macro_invocation"}),
),
"java": LanguageConfig(
diff --git a/packages/core/src/repowise/core/ingestion/queries/rust.scm b/packages/core/src/repowise/core/ingestion/queries/rust.scm
index ae679fd17..ebf4be42f 100644
--- a/packages/core/src/repowise/core/ingestion/queries/rust.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/rust.scm
@@ -20,6 +20,21 @@
parameters: (parameters) @symbol.params
) @symbol.def
+; function_signature_item WITH visibility -- a bodiless ``fn foo();``. Shared by
+; a trait's undefaulted methods, which may not write a modifier, and an
+; ``extern "C"`` block's declarations, which may and often do.
+(function_signature_item
+ (visibility_modifier) @symbol.modifiers
+ name: (identifier) @symbol.name
+ parameters: (parameters) @symbol.params
+) @symbol.def
+
+; function_signature_item WITHOUT visibility
+(function_signature_item
+ name: (identifier) @symbol.name
+ parameters: (parameters) @symbol.params
+) @symbol.def
+
; struct_item WITH visibility
(struct_item
(visibility_modifier) @symbol.modifiers
diff --git a/tests/unit/ingestion/test_rust_bodiless_signatures.py b/tests/unit/ingestion/test_rust_bodiless_signatures.py
new file mode 100644
index 000000000..5d41d22c6
--- /dev/null
+++ b/tests/unit/ingestion/test_rust_bodiless_signatures.py
@@ -0,0 +1,111 @@
+"""Rust bodiless ``fn foo();`` declarations are captured as symbols.
+
+``function_signature_item`` had no pattern in ``rust.scm``, so a trait's
+undefaulted methods and every ``extern "C"`` declaration were invisible to the
+symbol graph. The node type is shared by both shapes, which is why the query
+needs a with-visibility and a without-visibility pattern: a trait item may not
+write a modifier, an ``extern`` declaration may and often does.
+
+The ``extern`` half has no coverage in the measurement corpus -- serde, ripgrep
+and goose contain no ``foreign_mod_item`` at all -- so these tests are its only
+gate.
+"""
+
+from __future__ import annotations
+
+from datetime import datetime
+
+from repowise.core.ingestion.models import FileInfo
+from repowise.core.ingestion.parser import ASTParser
+
+_PARSER = ASTParser()
+
+
+def _symbols(src: str):
+ info = FileInfo(
+ path="src/lib.rs",
+ abs_path="/repo/src/lib.rs",
+ language="rust",
+ size_bytes=100,
+ git_hash="",
+ last_modified=datetime.now(),
+ is_test=False,
+ is_config=False,
+ is_api_contract=False,
+ is_entry_point=False,
+ )
+ parsed = _PARSER.parse_file(info, src.encode("utf-8"))
+ return {(s.parent_name, s.name): s for s in parsed.symbols}
+
+
+def test_undefaulted_trait_method_is_captured() -> None:
+ src = """
+pub trait Matcher {
+ fn is_match(&self, hay: &str) -> bool;
+ fn defaulted(&self) -> u8 { 1 }
+}
+"""
+ syms = _symbols(src)
+ decl = syms[("Matcher", "is_match")]
+ assert decl.kind == "method"
+ assert decl.visibility == "public"
+ assert decl.is_declaration is True
+ # The defaulted sibling is unchanged and is not a declaration.
+ assert syms[("Matcher", "defaulted")].is_declaration is False
+
+
+def test_bodiless_signature_keeps_its_return_type() -> None:
+ """``build_signature`` dispatches on node type, and the bodiless node is a
+ different one. Left out it falls through to the bare-name fallback and the
+ signature loses both the ``fn`` prefix and the return type."""
+ src = """
+pub trait Matcher {
+ fn is_match(&self, hay: &str) -> bool;
+}
+"""
+ assert _symbols(src)[("Matcher", "is_match")].signature == (
+ "fn is_match(&self, hay: &str) -> bool"
+ )
+
+
+def test_private_trait_keeps_its_bodiless_methods_private() -> None:
+ src = """
+trait Hidden {
+ fn secret(&self) -> u8;
+}
+"""
+ assert _symbols(src)[("Hidden", "secret")].visibility == "private"
+
+
+def test_extern_block_declarations_read_their_own_visibility() -> None:
+ """The reason the query needs two patterns: an ``extern`` declaration uses
+ the same node type as a trait's method but does carry a real ``pub``."""
+ src = """
+extern "C" {
+ pub fn ffi_pub(a: i32) -> i32;
+ fn ffi_priv();
+}
+"""
+ syms = _symbols(src)
+ pub = syms[(None, "ffi_pub")]
+ priv = syms[(None, "ffi_priv")]
+ assert pub.visibility == "public"
+ assert priv.visibility == "private"
+ # An extern block is not a parent scope, so these stay plain functions.
+ assert pub.kind == "function"
+ assert pub.signature == "fn ffi_pub(a: i32) -> i32"
+ assert pub.is_declaration is True
+ assert priv.is_declaration is True
+
+
+def test_doc_comment_attaches_to_a_bodiless_method() -> None:
+ """Docstring extraction walks preceding siblings rather than a body, so a
+ node with no ``block`` child must still pick its ``///`` line up."""
+ src = """
+pub trait Matcher {
+ /// Whether the haystack matches.
+ fn is_match(&self, hay: &str) -> bool;
+}
+"""
+ doc = _symbols(src)[("Matcher", "is_match")].docstring
+ assert doc == "Whether the haystack matches."