Skip to content

Commit 658639e

Browse files
authored
Merge pull request #2685 from DeusData/fix/rust-generic-impl-call-scope
fix(rust): generic impl methods source their own calls (#763 Rust share)
2 parents ea5c1f7 + 1661673 commit 658639e

4 files changed

Lines changed: 180 additions & 0 deletions

File tree

‎internal/cbm/extract_unified.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,20 @@ static const char *compute_class_qn(CBMExtractCtx *ctx, TSNode node, const WalkS
11761176
if (!name || !name[0]) {
11771177
return NULL;
11781178
}
1179+
/* Rust: the impl's type text carries its generic arguments (`impl<T> Gen<T>`,
1180+
* `impl<'a> Parser<'a>`), which the def side strips before naming the type
1181+
* (extract_defs.c extract_rust_impl: `Buffer<T>` -> `Buffer`). Strip them the
1182+
* same way, or every method of a generic impl gets a scope QN no node carries
1183+
* and all its calls fall back to the File node. */
1184+
if (ctx->language == CBM_LANG_RUST && strcmp(ts_node_type(node), "impl_item") == 0) {
1185+
char *lt = strchr(name, '<');
1186+
if (lt == name) {
1187+
return NULL; /* `<T as Tr>`-style self type: no name to scope by */
1188+
}
1189+
if (lt) {
1190+
*lt = '\0';
1191+
}
1192+
}
11791193

11801194
/* Nested class: prefix with the enclosing class QN (Outer.Inner) so this
11811195
* scope QN matches the def-side class QN (extract_defs.c compute_class_qn /

‎internal/cbm/lsp/rust_lsp.c‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,22 @@ static void rust_strip_turbofish(char *path) {
306306
*write = '\0';
307307
}
308308

309+
/* In place: `Gen<T>` -> `Gen`, `foo::Bar<'a, T>` -> `foo::Bar` (#763). An
310+
* impl's self type keeps its generic arguments in the source text, but every
311+
* def-side QN (a method's receiver, the extractor's call scope) strips them.
312+
* Unstripped, the LSP's caller QN, `self`/`Self` dispatch and the return-type
313+
* harvest never matched a generic impl's methods. Text starting with `<` (a
314+
* qualified path) is left alone. Safe on NULL. */
315+
static void rust_strip_impl_type_args(char *type_text) {
316+
if (!type_text) {
317+
return;
318+
}
319+
char *lt = strchr(type_text, '<');
320+
if (lt && lt > type_text) {
321+
*lt = '\0';
322+
}
323+
}
324+
309325
/* Look up a `use` alias and return its fully-qualified module path,
310326
* or NULL if absent. The returned pointer aliases into the use map. */
311327
static const char *rust_resolve_use(RustLSPContext *ctx, const char *local_name) {
@@ -5274,6 +5290,7 @@ static void rust_process_impl(RustLSPContext *ctx, TSNode impl_node) {
52745290
char *type_text = rust_node_text(ctx, type_node);
52755291
if (!type_text)
52765292
return;
5293+
rust_strip_impl_type_args(type_text);
52775294

52785295
/* Detect blanket impl: `impl<T: Trait> ForeignTrait for T { ... }`
52795296
* where type_text is a name that appears in the impl's type
@@ -6028,6 +6045,7 @@ void cbm_rust_build_local_registry(CBMArena *arena, CBMTypeRegistry *reg, CBMFil
60286045
if (ts_node_is_null(type_node) || ts_node_is_null(body))
60296046
continue;
60306047
char *type_name = cbm_node_text(arena, type_node, source);
6048+
rust_strip_impl_type_args(type_name);
60316049
if (!type_name || !type_name[0])
60326050
continue;
60336051
const char *type_qn = cbm_arena_sprintf(arena, "%s.%s", module_qn, type_name);

‎tests/test_extraction.c‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7178,6 +7178,63 @@ TEST(extract_perl_method_call_flags_is_method) {
71787178
PASS();
71797179
}
71807180

7181+
/* The def QN of `name` and the enclosing-function QN of the call `callee` inside
7182+
* it, from one extraction result. */
7183+
static void rust_def_and_scope(const CBMFileResult *r, const char *name, const char *callee,
7184+
const char **def_qn, const char **scope_qn) {
7185+
*def_qn = NULL;
7186+
*scope_qn = NULL;
7187+
for (int i = 0; i < r->defs.count; i++) {
7188+
const CBMDefinition *d = &r->defs.items[i];
7189+
if (d->name && d->qualified_name && strcmp(d->name, name) == 0) {
7190+
*def_qn = d->qualified_name;
7191+
}
7192+
}
7193+
for (int i = 0; i < r->calls.count; i++) {
7194+
const CBMCall *c = &r->calls.items[i];
7195+
if (c->callee_name && strcmp(c->callee_name, callee) == 0 && c->enclosing_func_qn &&
7196+
strstr(c->enclosing_func_qn, name)) {
7197+
*scope_qn = c->enclosing_func_qn;
7198+
}
7199+
}
7200+
}
7201+
7202+
/* F-R4: a method of a generic impl (`impl<T> Gen<T>`, `impl<'a> Life<'a>`) got a
7203+
* call scope that kept the generic arguments ("...Gen<T>.in_generic") while the
7204+
* def side strips them ("...Gen.in_generic"), so no node carried the scope and
7205+
* every call in the body fell back to the File node (base census: 84,331 Rust
7206+
* CALLS sourced from a File although inside a function, 96% of all File-sourced
7207+
* Rust CALLS). The def QN and the call scope must agree. */
7208+
TEST(extract_rust_generic_impl_call_scope_matches_def) {
7209+
CBMFileResult *r = extract("pub struct Plain;\n"
7210+
"impl Plain {\n"
7211+
" pub fn in_plain(&self) -> u32 { helper() }\n"
7212+
"}\n"
7213+
"pub struct Gen<T> { v: T }\n"
7214+
"impl<T> Gen<T> {\n"
7215+
" pub fn in_generic(&self) -> u32 { helper() }\n"
7216+
"}\n"
7217+
"pub struct Life<'a> { s: &'a str }\n"
7218+
"impl<'a> Life<'a> {\n"
7219+
" pub fn in_lifetime(&self) -> u32 { helper() }\n"
7220+
"}\n"
7221+
"pub fn helper() -> u32 { 1 }\n",
7222+
CBM_LANG_RUST, "t", "src/lib.rs");
7223+
ASSERT_NOT_NULL(r);
7224+
ASSERT_FALSE(r->has_error);
7225+
static const char *const methods[] = {"in_plain", "in_generic", "in_lifetime", NULL};
7226+
for (int m = 0; methods[m]; m++) {
7227+
const char *def_qn = NULL;
7228+
const char *scope_qn = NULL;
7229+
rust_def_and_scope(r, methods[m], "helper", &def_qn, &scope_qn);
7230+
ASSERT_NOT_NULL(def_qn);
7231+
ASSERT_NOT_NULL(scope_qn);
7232+
ASSERT_STR_EQ(scope_qn, def_qn);
7233+
}
7234+
cbm_free_result(r);
7235+
PASS();
7236+
}
7237+
71817238
/* Languages OUTSIDE the is_method flag set (only Perl and TS/JS/TSX set it) must
71827239
* be unaffected: a Go method call never sets is_method. */
71837240
TEST(extract_flag_exempt_method_call_not_flagged_is_method) {
@@ -12512,6 +12569,7 @@ SUITE(extraction) {
1251212569
RUN_TEST(extract_perl_config_string_not_a_callee);
1251312570
RUN_TEST(extract_perl_builtin_call_is_function_not_method);
1251412571
RUN_TEST(extract_perl_method_call_flags_is_method);
12572+
RUN_TEST(extract_rust_generic_impl_call_scope_matches_def);
1251512573
RUN_TEST(extract_flag_exempt_method_call_not_flagged_is_method);
1251612574
RUN_TEST(extract_python_member_call_flags_is_method);
1251712575
RUN_TEST(extract_python_bare_call_flags_locally_bound_callee);

‎tests/test_pipeline.c‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9042,6 +9042,94 @@ TEST(pipeline_rust_assoc_call_never_binds_free_namesake_parallel) {
90429042
return rust_assoc_call_free_namesake_case(52);
90439043
}
90449044

9045+
/* F-R4 (#763, Rust): calls inside a method of a generic impl (`impl<T> Gen<T>`,
9046+
* `impl<'a> Life<'a>`) were sourced from the File node: the call scope kept the
9047+
* generic arguments the def side strips. Each method must be the source of its
9048+
* own call. pad_files > 0 forces the parallel resolver. */
9049+
static int rust_generic_impl_attribution_case(int pad_files) {
9050+
char tmp[256];
9051+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_rust_gimpl_XXXXXX");
9052+
if (!cbm_mkdtemp(tmp)) {
9053+
FAIL("tmpdir");
9054+
}
9055+
write_temp_file(tmp, "Cargo.toml", "[package]\nname = \"fx\"\nversion = \"0.1.0\"\n");
9056+
write_temp_file(
9057+
tmp, "src/lib.rs",
9058+
"pub struct Gen<T> { v: T }\n"
9059+
"impl<T> Gen<T> {\n"
9060+
" pub fn in_generic(&self) -> u32 { helper_a() + self.other_g() + Self::assoc_g() }\n"
9061+
" pub fn other_g(&self) -> u32 { 3 }\n"
9062+
" pub fn assoc_g() -> u32 { 4 }\n"
9063+
"}\n"
9064+
"pub struct Life<'a> { s: &'a str }\n"
9065+
"impl<'a> Life<'a> {\n"
9066+
" pub fn in_lifetime(&self) -> u32 { helper_b() + self.other_l() }\n"
9067+
" pub fn other_l(&self) -> u32 { 5 }\n"
9068+
"}\n"
9069+
"pub fn helper_a() -> u32 { 1 }\n"
9070+
"pub fn helper_b() -> u32 { 2 }\n");
9071+
for (int i = 0; i < pad_files; i++) {
9072+
char name[64];
9073+
char body[128];
9074+
snprintf(name, sizeof(name), "src/filler%d.rs", i);
9075+
snprintf(body, sizeof(body), "pub fn filler%d() -> u32 {\n %d\n}\n", i, i);
9076+
write_temp_file(tmp, name, body);
9077+
}
9078+
9079+
char *saved = NULL;
9080+
if (pad_files > 0) {
9081+
char *old_workers = getenv("CBM_WORKERS");
9082+
saved = old_workers ? strdup(old_workers) : NULL;
9083+
cbm_setenv("CBM_WORKERS", "4", 1);
9084+
}
9085+
char db_path[512];
9086+
snprintf(db_path, sizeof(db_path), "%s/rust_gimpl.db", tmp);
9087+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
9088+
ASSERT_NOT_NULL(p);
9089+
ASSERT_EQ(cbm_pipeline_run(p), 0);
9090+
const char *project = cbm_pipeline_project_name(p);
9091+
cbm_store_t *s = cbm_store_open_path(db_path);
9092+
ASSERT_NOT_NULL(s);
9093+
9094+
ASSERT_TRUE(cross_file_call_exists(s, project, "in_generic", "helper_a"));
9095+
ASSERT_TRUE(cross_file_call_exists(s, project, "in_lifetime", "helper_b"));
9096+
/* ... and not from the file. */
9097+
ASSERT_FALSE(cross_file_call_exists(s, project, "lib.rs", "helper_a"));
9098+
ASSERT_FALSE(cross_file_call_exists(s, project, "lib.rs", "helper_b"));
9099+
/* The LSP side names the impl's self type without its arguments too: its
9100+
* rows join these callers (lsp_*, not a registry fallback), and `self.` /
9101+
* `Self::` dispatch inside a generic impl resolves (Self::assoc_g had no
9102+
* edge at all). */
9103+
ASSERT_TRUE(
9104+
cross_file_call_has_strategy(s, project, "in_generic", "helper_a", "\"strategy\":\"lsp_"));
9105+
ASSERT_TRUE(
9106+
cross_file_call_has_strategy(s, project, "in_generic", "other_g", "\"strategy\":\"lsp_"));
9107+
ASSERT_TRUE(cross_file_call_exists(s, project, "in_generic", "assoc_g"));
9108+
ASSERT_TRUE(
9109+
cross_file_call_has_strategy(s, project, "in_lifetime", "other_l", "\"strategy\":\"lsp_"));
9110+
9111+
cbm_store_close(s);
9112+
cbm_pipeline_free(p);
9113+
if (pad_files > 0) {
9114+
if (saved) {
9115+
cbm_setenv("CBM_WORKERS", saved, 1);
9116+
free(saved);
9117+
} else {
9118+
cbm_unsetenv("CBM_WORKERS");
9119+
}
9120+
}
9121+
th_rmtree(tmp);
9122+
PASS();
9123+
}
9124+
9125+
TEST(pipeline_rust_generic_impl_method_sources_its_calls) {
9126+
return rust_generic_impl_attribution_case(0);
9127+
}
9128+
9129+
TEST(pipeline_rust_generic_impl_method_sources_its_calls_parallel) {
9130+
return rust_generic_impl_attribution_case(52);
9131+
}
9132+
90459133
TEST(pipeline_go_rw_usage_never_cross_into_c_parallel) {
90469134
/* Parallel twin of the test above: USAGE and WRITES/READS each have a
90479135
* second, independent resolver in pass_parallel.c (resolve_file_usages /
@@ -24748,6 +24836,8 @@ SUITE(pipeline) {
2474824836
RUN_TEST(pipeline_rust_call_never_binds_another_language_parallel);
2474924837
RUN_TEST(pipeline_rust_assoc_call_never_binds_free_namesake);
2475024838
RUN_TEST(pipeline_rust_assoc_call_never_binds_free_namesake_parallel);
24839+
RUN_TEST(pipeline_rust_generic_impl_method_sources_its_calls);
24840+
RUN_TEST(pipeline_rust_generic_impl_method_sources_its_calls_parallel);
2475124841
RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method);
2475224842
RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method_parallel);
2475324843
RUN_TEST(pipeline_go_bare_ref_never_binds_field);

0 commit comments

Comments
 (0)