From 09c4885e11ba8df308146a23c6582e11d88b12d7 Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sun, 18 Oct 2026 00:05:47 +0200 Subject: [PATCH 1/4] fix(extract): a Rust generic impl's call scope drops its type arguments The def side names a Rust impl method after the implementing type with its generic arguments stripped (`impl Gen` -> `Gen`, extract_rust_impl). The call-scope QN in extract_unified.c used the impl's type text verbatim, `Gen` / `Life<'a>`. So every method of a generic or lifetime-parameterised impl had a scope QN that no node carries, and every call in its body was sourced from the File node instead of the method. Base census, 9f85f104 par (lane L7's #763 measurement): 87,855 Rust CALLS start at a File/Module node, 84,331 of them (96%) on a line inside a function span. Go is 0%. Strip the generic arguments on the call side exactly as the def side does. Plain impls, trait impls, free functions and inline modules were already consistent (checked on a fixture with the base binary). Test: extract_rust_generic_impl_call_scope_matches_def (plain, type-generic and lifetime-generic impls; def QN == call scope). Refs #763 Signed-off-by: Martin Vogel --- internal/cbm/extract_unified.c | 14 ++++++++ tests/test_extraction.c | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c index 96e6ac865c..ce7be6d29b 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 Gen`, + * `impl<'a> Parser<'a>`), which the def side strips before naming the type + * (extract_defs.c extract_rust_impl: `Buffer` -> `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 e39d40a8ab..09a8dd2083 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 Gen`, `impl<'a> Life<'a>`) got a + * call scope that kept the generic arguments ("...Gen.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 { v: T }\n" + "impl Gen {\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); From d40f8e6be19c876167fc8d6cb794649403f98bdb Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sun, 18 Oct 2026 00:06:42 +0200 Subject: [PATCH 2/4] test(pipeline): generic Rust impl methods source their own calls (both resolvers) Sequential and parallel pipeline twin of extract_rust_generic_impl_call_scope_matches_def: the calls in a type-generic and a lifetime-generic impl method are sourced from the method, not from the File node. Refs #763 Signed-off-by: Martin Vogel --- tests/test_pipeline.c | 75 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c index b52513acee..b3df58e707 100644 --- a/tests/test_pipeline.c +++ b/tests/test_pipeline.c @@ -8757,6 +8757,79 @@ TEST(pipeline_go_rw_usage_never_cross_into_c) { PASS(); } +/* F-R4 (#763, Rust): calls inside a method of a generic impl (`impl Gen`, + * `impl<'a> Life<'a>`) were sourced from the File node: the call scope kept the + * generic arguments the def side strips. Each method must be the source of its + * own call. pad_files > 0 forces the parallel resolver. */ +static int rust_generic_impl_attribution_case(int pad_files) { + char tmp[256]; + snprintf(tmp, sizeof(tmp), "/tmp/cbm_rust_gimpl_XXXXXX"); + if (!cbm_mkdtemp(tmp)) { + FAIL("tmpdir"); + } + write_temp_file(tmp, "Cargo.toml", "[package]\nname = \"fx\"\nversion = \"0.1.0\"\n"); + write_temp_file(tmp, "src/lib.rs", + "pub struct Gen { v: T }\n" + "impl Gen {\n" + " pub fn in_generic(&self) -> u32 { helper_a() }\n" + "}\n" + "pub struct Life<'a> { s: &'a str }\n" + "impl<'a> Life<'a> {\n" + " pub fn in_lifetime(&self) -> u32 { helper_b() }\n" + "}\n" + "pub fn helper_a() -> u32 { 1 }\n" + "pub fn helper_b() -> u32 { 2 }\n"); + for (int i = 0; i < pad_files; i++) { + char name[64]; + char body[128]; + snprintf(name, sizeof(name), "src/filler%d.rs", i); + snprintf(body, sizeof(body), "pub fn filler%d() -> u32 {\n %d\n}\n", i, i); + write_temp_file(tmp, name, body); + } + + char *saved = NULL; + if (pad_files > 0) { + char *old_workers = getenv("CBM_WORKERS"); + saved = old_workers ? strdup(old_workers) : NULL; + cbm_setenv("CBM_WORKERS", "4", 1); + } + char db_path[512]; + snprintf(db_path, sizeof(db_path), "%s/rust_gimpl.db", tmp); + cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL); + ASSERT_NOT_NULL(p); + ASSERT_EQ(cbm_pipeline_run(p), 0); + const char *project = cbm_pipeline_project_name(p); + cbm_store_t *s = cbm_store_open_path(db_path); + ASSERT_NOT_NULL(s); + + ASSERT_TRUE(cross_file_call_exists(s, project, "in_generic", "helper_a")); + ASSERT_TRUE(cross_file_call_exists(s, project, "in_lifetime", "helper_b")); + /* ... and not from the file. */ + ASSERT_FALSE(cross_file_call_exists(s, project, "lib.rs", "helper_a")); + ASSERT_FALSE(cross_file_call_exists(s, project, "lib.rs", "helper_b")); + + cbm_store_close(s); + cbm_pipeline_free(p); + if (pad_files > 0) { + if (saved) { + cbm_setenv("CBM_WORKERS", saved, 1); + free(saved); + } else { + cbm_unsetenv("CBM_WORKERS"); + } + } + th_rmtree(tmp); + PASS(); +} + +TEST(pipeline_rust_generic_impl_method_sources_its_calls) { + return rust_generic_impl_attribution_case(0); +} + +TEST(pipeline_rust_generic_impl_method_sources_its_calls_parallel) { + return rust_generic_impl_attribution_case(52); +} + TEST(pipeline_go_rw_usage_never_cross_into_c_parallel) { /* Parallel twin of the test above: USAGE and WRITES/READS each have a * second, independent resolver in pass_parallel.c (resolve_file_usages / @@ -23615,6 +23688,8 @@ SUITE(pipeline) { RUN_TEST(pipeline_html_embedded_member_call_stays_unbound); RUN_TEST(pipeline_go_rw_usage_never_cross_into_c); RUN_TEST(pipeline_go_rw_usage_never_cross_into_c_parallel); + RUN_TEST(pipeline_rust_generic_impl_method_sources_its_calls); + RUN_TEST(pipeline_rust_generic_impl_method_sources_its_calls_parallel); RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method); RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method_parallel); RUN_TEST(pipeline_go_bare_ref_never_binds_field); From f75af00a8b01db44d84fdeebaf493f400662d74d Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sun, 18 Oct 2026 03:03:38 +0200 Subject: [PATCH 3/4] fix(rust-lsp): a generic impl's self type drops its type arguments The extractor fix above names a generic impl's methods `Gen.in_generic` on both the def and the call-scope side. The Rust LSP still built its self type from the impl's raw text, `Gen`. That broke three things: - its caller QN (`Gen.in_generic`) no longer joined the pipeline's call scope, so every LSP row inside a generic or lifetime impl was dropped for a registry guess. On 9 Rust repos that turned 1,927 lsp_* edges into suffix_match/unique_name; - `self.` and `Self::` dispatch never found the impl's own methods; `Self::assoc_g()` had no edge at all, on main as well; - the return-type harvest never matched those methods, so chained calls on them stayed untyped. Strip the generic arguments from the impl's self type in both places. Text that starts with `<` (a qualified path) is left alone. Signed-off-by: Martin Vogel --- internal/cbm/lsp/rust_lsp.c | 18 ++++++++++++++++++ tests/test_pipeline.c | 37 ++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/internal/cbm/lsp/rust_lsp.c b/internal/cbm/lsp/rust_lsp.c index 44680cb69f..fda3111dee 100644 --- a/internal/cbm/lsp/rust_lsp.c +++ b/internal/cbm/lsp/rust_lsp.c @@ -306,6 +306,22 @@ static void rust_strip_turbofish(char *path) { *write = '\0'; } +/* In place: `Gen` -> `Gen`, `foo::Bar<'a, T>` -> `foo::Bar` (#763). An + * impl's self type keeps its generic arguments in the source text, but every + * def-side QN (a method's receiver, the extractor's call scope) strips them. + * Unstripped, the LSP's caller QN, `self`/`Self` dispatch and the return-type + * harvest never matched a generic impl's methods. Text starting with `<` (a + * qualified path) is left alone. Safe on NULL. */ +static void rust_strip_impl_type_args(char *type_text) { + if (!type_text) { + return; + } + char *lt = strchr(type_text, '<'); + if (lt && lt > type_text) { + *lt = '\0'; + } +} + /* Look up a `use` alias and return its fully-qualified module path, * or NULL if absent. The returned pointer aliases into the use map. */ static const char *rust_resolve_use(RustLSPContext *ctx, const char *local_name) { @@ -5245,6 +5261,7 @@ static void rust_process_impl(RustLSPContext *ctx, TSNode impl_node) { char *type_text = rust_node_text(ctx, type_node); if (!type_text) return; + rust_strip_impl_type_args(type_text); /* Detect blanket impl: `impl ForeignTrait for T { ... }` * where type_text is a name that appears in the impl's type @@ -5999,6 +6016,7 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil if (ts_node_is_null(type_node) || ts_node_is_null(body)) continue; char *type_name = cbm_node_text(arena, type_node, source); + rust_strip_impl_type_args(type_name); if (!type_name || !type_name[0]) continue; const char *type_qn = cbm_arena_sprintf(arena, "%s.%s", module_qn, type_name); diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c index 0111318a26..085fa55dd2 100644 --- a/tests/test_pipeline.c +++ b/tests/test_pipeline.c @@ -8879,17 +8879,21 @@ static int rust_generic_impl_attribution_case(int pad_files) { FAIL("tmpdir"); } write_temp_file(tmp, "Cargo.toml", "[package]\nname = \"fx\"\nversion = \"0.1.0\"\n"); - write_temp_file(tmp, "src/lib.rs", - "pub struct Gen { v: T }\n" - "impl Gen {\n" - " pub fn in_generic(&self) -> u32 { helper_a() }\n" - "}\n" - "pub struct Life<'a> { s: &'a str }\n" - "impl<'a> Life<'a> {\n" - " pub fn in_lifetime(&self) -> u32 { helper_b() }\n" - "}\n" - "pub fn helper_a() -> u32 { 1 }\n" - "pub fn helper_b() -> u32 { 2 }\n"); + write_temp_file( + tmp, "src/lib.rs", + "pub struct Gen { v: T }\n" + "impl Gen {\n" + " pub fn in_generic(&self) -> u32 { helper_a() + self.other_g() + Self::assoc_g() }\n" + " pub fn other_g(&self) -> u32 { 3 }\n" + " pub fn assoc_g() -> u32 { 4 }\n" + "}\n" + "pub struct Life<'a> { s: &'a str }\n" + "impl<'a> Life<'a> {\n" + " pub fn in_lifetime(&self) -> u32 { helper_b() + self.other_l() }\n" + " pub fn other_l(&self) -> u32 { 5 }\n" + "}\n" + "pub fn helper_a() -> u32 { 1 }\n" + "pub fn helper_b() -> u32 { 2 }\n"); for (int i = 0; i < pad_files; i++) { char name[64]; char body[128]; @@ -8918,6 +8922,17 @@ static int rust_generic_impl_attribution_case(int pad_files) { /* ... and not from the file. */ ASSERT_FALSE(cross_file_call_exists(s, project, "lib.rs", "helper_a")); ASSERT_FALSE(cross_file_call_exists(s, project, "lib.rs", "helper_b")); + /* The LSP side names the impl's self type without its arguments too: its + * rows join these callers (lsp_*, not a registry fallback), and `self.` / + * `Self::` dispatch inside a generic impl resolves (Self::assoc_g had no + * edge at all). */ + ASSERT_TRUE( + cross_file_call_has_strategy(s, project, "in_generic", "helper_a", "\"strategy\":\"lsp_")); + ASSERT_TRUE( + cross_file_call_has_strategy(s, project, "in_generic", "other_g", "\"strategy\":\"lsp_")); + ASSERT_TRUE(cross_file_call_exists(s, project, "in_generic", "assoc_g")); + ASSERT_TRUE( + cross_file_call_has_strategy(s, project, "in_lifetime", "other_l", "\"strategy\":\"lsp_")); cbm_store_close(s); cbm_pipeline_free(p); From fed5cbf9cc0d11affde9030f640c697df3edd6dc Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sun, 18 Oct 2026 16:49:36 +0200 Subject: [PATCH 4/4] fix(extract): test the empty Rust impl name before the cut cppcheck 2.20 reports the post-cut `if (!name[0])` as always false (knownConditionTrueFalse): it cannot see that writing through `lt` may empty `name`. The name is non-empty here, so the cut empties it only when `<` is the first character. Test exactly that before the cut. The behaviour is unchanged. Signed-off-by: Martin Vogel --- internal/cbm/extract_unified.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/cbm/extract_unified.c b/internal/cbm/extract_unified.c index ce7be6d29b..b96e60a914 100644 --- a/internal/cbm/extract_unified.c +++ b/internal/cbm/extract_unified.c @@ -1166,12 +1166,12 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const WalkS * 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 == name) { + return NULL; /* ``-style self type: no name to scope by */ + } if (lt) { *lt = '\0'; } - if (!name[0]) { - return NULL; - } } /* Nested class: prefix with the enclosing class QN (Outer.Inner) so this