Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(extract): a Rust generic impl's call scope drops its type arguments

Martin Vogel09c4885e11ba
diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c
index 96e6ac865..ce7be6d29 100644
--- a/internal/cbm/extract_unified.c
+++ b/internal/cbm/extract_unified.c
@@ -1159,6 +1159,20 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const WalkS
if (!name || !name[0]) {
return NULL;
}
+ /* Rust: the impl's type text carries its generic arguments (`impl<T> Gen<T>`,
+ * `impl<'a> Parser<'a>`), which the def side strips before naming the type
+ * (extract_defs.c extract_rust_impl: `Buffer<T>` -> `Buffer`). Strip them the
+ * same way, or every method of a generic impl gets a scope QN no node carries
+ * and all its calls fall back to the File node. */
+ if (ctx->language == CBM_LANG_RUST && strcmp(ts_node_type(node), "impl_item") == 0) {
+ char *lt = strchr(name, '<');
+ if (lt) {
+ *lt = '\0';
+ }
+ if (!name[0]) {
+ return NULL;
+ }
+ }
/* Nested class: prefix with the enclosing class QN (Outer.Inner) so this
* scope QN matches the def-side class QN (extract_defs.c compute_class_qn /
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index e39d40a8a..09a8dd208 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -6886,6 +6886,63 @@ TEST(extract_perl_method_call_flags_is_method) {
PASS();
}
+/* The def QN of `name` and the enclosing-function QN of the call `callee` inside
+ * it, from one extraction result. */
+static void rust_def_and_scope(const CBMFileResult *r, const char *name, const char *callee,
+ const char **def_qn, const char **scope_qn) {
+ *def_qn = NULL;
+ *scope_qn = NULL;
+ for (int i = 0; i < r->defs.count; i++) {
+ const CBMDefinition *d = &r->defs.items[i];
+ if (d->name && d->qualified_name && strcmp(d->name, name) == 0) {
+ *def_qn = d->qualified_name;
+ }
+ }
+ for (int i = 0; i < r->calls.count; i++) {
+ const CBMCall *c = &r->calls.items[i];
+ if (c->callee_name && strcmp(c->callee_name, callee) == 0 && c->enclosing_func_qn &&
+ strstr(c->enclosing_func_qn, name)) {
+ *scope_qn = c->enclosing_func_qn;
+ }
+ }
+}
+
+/* F-R4: a method of a generic impl (`impl<T> Gen<T>`, `impl<'a> Life<'a>`) got a
+ * call scope that kept the generic arguments ("...Gen<T>.in_generic") while the
+ * def side strips them ("...Gen.in_generic"), so no node carried the scope and
+ * every call in the body fell back to the File node (base census: 84,331 Rust
+ * CALLS sourced from a File although inside a function, 96% of all File-sourced
+ * Rust CALLS). The def QN and the call scope must agree. */
+TEST(extract_rust_generic_impl_call_scope_matches_def) {
+ CBMFileResult *r = extract("pub struct Plain;\n"
+ "impl Plain {\n"
+ " pub fn in_plain(&self) -> u32 { helper() }\n"
+ "}\n"
+ "pub struct Gen<T> { v: T }\n"
+ "impl<T> Gen<T> {\n"
+ " pub fn in_generic(&self) -> u32 { helper() }\n"
+ "}\n"
+ "pub struct Life<'a> { s: &'a str }\n"
+ "impl<'a> Life<'a> {\n"
+ " pub fn in_lifetime(&self) -> u32 { helper() }\n"
+ "}\n"
+ "pub fn helper() -> u32 { 1 }\n",
+ CBM_LANG_RUST, "t", "src/lib.rs");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ static const char *const methods[] = {"in_plain", "in_generic", "in_lifetime", NULL};
+ for (int m = 0; methods[m]; m++) {
+ const char *def_qn = NULL;
+ const char *scope_qn = NULL;
+ rust_def_and_scope(r, methods[m], "helper", &def_qn, &scope_qn);
+ ASSERT_NOT_NULL(def_qn);
+ ASSERT_NOT_NULL(scope_qn);
+ ASSERT_STR_EQ(scope_qn, def_qn);
+ }
+ cbm_free_result(r);
+ PASS();
+}
+
/* Languages OUTSIDE the is_method flag set (only Perl and TS/JS/TSX set it) must
* be unaffected: a Go method call never sets is_method. */
TEST(extract_flag_exempt_method_call_not_flagged_is_method) {
@@ -12220,6 +12277,7 @@ SUITE(extraction) {
RUN_TEST(extract_perl_config_string_not_a_callee);
RUN_TEST(extract_perl_builtin_call_is_function_not_method);
RUN_TEST(extract_perl_method_call_flags_is_method);
+ RUN_TEST(extract_rust_generic_impl_call_scope_matches_def);
RUN_TEST(extract_flag_exempt_method_call_not_flagged_is_method);
RUN_TEST(extract_python_member_call_flags_is_method);
RUN_TEST(extract_python_bare_call_flags_locally_bound_callee);