Skip to content

Commit f75af00

Browse files
committed
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<T>`. That broke three things: - its caller QN (`Gen<T>.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 <martin.vogel.tech@gmail.com>
1 parent 2fbbecc commit f75af00

2 files changed

Lines changed: 44 additions & 11 deletions

File tree

‎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) {
@@ -5245,6 +5261,7 @@ static void rust_process_impl(RustLSPContext *ctx, TSNode impl_node) {
52455261
char *type_text = rust_node_text(ctx, type_node);
52465262
if (!type_text)
52475263
return;
5264+
rust_strip_impl_type_args(type_text);
52485265

52495266
/* Detect blanket impl: `impl<T: Trait> ForeignTrait for T { ... }`
52505267
* 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
59996016
if (ts_node_is_null(type_node) || ts_node_is_null(body))
60006017
continue;
60016018
char *type_name = cbm_node_text(arena, type_node, source);
6019+
rust_strip_impl_type_args(type_name);
60026020
if (!type_name || !type_name[0])
60036021
continue;
60046022
const char *type_qn = cbm_arena_sprintf(arena, "%s.%s", module_qn, type_name);

‎tests/test_pipeline.c‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8879,17 +8879,21 @@ static int rust_generic_impl_attribution_case(int pad_files) {
88798879
FAIL("tmpdir");
88808880
}
88818881
write_temp_file(tmp, "Cargo.toml", "[package]\nname = \"fx\"\nversion = \"0.1.0\"\n");
8882-
write_temp_file(tmp, "src/lib.rs",
8883-
"pub struct Gen<T> { v: T }\n"
8884-
"impl<T> Gen<T> {\n"
8885-
" pub fn in_generic(&self) -> u32 { helper_a() }\n"
8886-
"}\n"
8887-
"pub struct Life<'a> { s: &'a str }\n"
8888-
"impl<'a> Life<'a> {\n"
8889-
" pub fn in_lifetime(&self) -> u32 { helper_b() }\n"
8890-
"}\n"
8891-
"pub fn helper_a() -> u32 { 1 }\n"
8892-
"pub fn helper_b() -> u32 { 2 }\n");
8882+
write_temp_file(
8883+
tmp, "src/lib.rs",
8884+
"pub struct Gen<T> { v: T }\n"
8885+
"impl<T> Gen<T> {\n"
8886+
" pub fn in_generic(&self) -> u32 { helper_a() + self.other_g() + Self::assoc_g() }\n"
8887+
" pub fn other_g(&self) -> u32 { 3 }\n"
8888+
" pub fn assoc_g() -> u32 { 4 }\n"
8889+
"}\n"
8890+
"pub struct Life<'a> { s: &'a str }\n"
8891+
"impl<'a> Life<'a> {\n"
8892+
" pub fn in_lifetime(&self) -> u32 { helper_b() + self.other_l() }\n"
8893+
" pub fn other_l(&self) -> u32 { 5 }\n"
8894+
"}\n"
8895+
"pub fn helper_a() -> u32 { 1 }\n"
8896+
"pub fn helper_b() -> u32 { 2 }\n");
88938897
for (int i = 0; i < pad_files; i++) {
88948898
char name[64];
88958899
char body[128];
@@ -8918,6 +8922,17 @@ static int rust_generic_impl_attribution_case(int pad_files) {
89188922
/* ... and not from the file. */
89198923
ASSERT_FALSE(cross_file_call_exists(s, project, "lib.rs", "helper_a"));
89208924
ASSERT_FALSE(cross_file_call_exists(s, project, "lib.rs", "helper_b"));
8925+
/* The LSP side names the impl's self type without its arguments too: its
8926+
* rows join these callers (lsp_*, not a registry fallback), and `self.` /
8927+
* `Self::` dispatch inside a generic impl resolves (Self::assoc_g had no
8928+
* edge at all). */
8929+
ASSERT_TRUE(
8930+
cross_file_call_has_strategy(s, project, "in_generic", "helper_a", "\"strategy\":\"lsp_"));
8931+
ASSERT_TRUE(
8932+
cross_file_call_has_strategy(s, project, "in_generic", "other_g", "\"strategy\":\"lsp_"));
8933+
ASSERT_TRUE(cross_file_call_exists(s, project, "in_generic", "assoc_g"));
8934+
ASSERT_TRUE(
8935+
cross_file_call_has_strategy(s, project, "in_lifetime", "other_l", "\"strategy\":\"lsp_"));
89218936

89228937
cbm_store_close(s);
89238938
cbm_pipeline_free(p);

0 commit comments

Comments
 (0)