Skip to content

Commit ec4f0ea

Browse files
committed
fix(rust-lsp): a type-qualified call never takes the free-function fallback
`Opts::write(true)` is an associated function. When the Rust LSP cannot place it, the registry can (import_map or qualified_suffix onto fsx.Opts.write). The two free-function short-name fallbacks (the cross-crate member lookup and the project-wide lsp_short_name_unique) ran first, though. They bound the call to the project's only free `write`, and that LSP row pre-empted the right answer. While the Rust registry also held other languages' definitions, a same-named JS or Perl function often made the free name ambiguous and hid this. With Rust-only registries the hijack replaced 516 qualified_suffix and 84 import_map edges on 7 Rust repos. A call whose qualifying segment names a type now skips both fallbacks. That covers an ASCII-uppercase initial, `Self`, and a qualified `<T as Tr>::f` path. Signed-off-by: Martin Vogel <[email protected]>
1 parent 42769c2 commit ec4f0ea

2 files changed

Lines changed: 137 additions & 31 deletions

File tree

‎internal/cbm/lsp/rust_lsp.c‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4128,6 +4128,32 @@ static bool rust_qn_has_suffix(const char *qualified_name, const char *suffix) {
41284128
(qn_len == suffix_len || qualified_name[qn_len - suffix_len - 1U] == '.');
41294129
}
41304130

4131+
/* True when the segment that qualifies a call path's tail names a TYPE:
4132+
* `Store::new`, `Self::helper`, `T::default`, `<T as Tr>::f` (turbofish is
4133+
* already stripped, so a `>` before the last `::` closes a qualified path).
4134+
* Such a call is an associated function, never a free function, so the
4135+
* free-function short-name fallbacks must not bind it: with a project-wide
4136+
* unique free `new`/`write`, `Store::new()` landed on that function and the
4137+
* LSP row pre-empted the registry's correct `Store.new`. Module segments are
4138+
* snake_case, so an ASCII-uppercase initial is a type. `tail` must point just
4139+
* past the path's last `::`, or equal `path` when there is none. */
4140+
static bool rust_path_qualifier_is_type(const char *path, const char *tail) {
4141+
if (!path || !tail || tail - path < 3) {
4142+
return false;
4143+
}
4144+
const char *seg_end = tail - 2; /* the last "::" */
4145+
if (seg_end[-1] == '>') {
4146+
return true;
4147+
}
4148+
const char *seg = path;
4149+
for (const char *p = path; p + 1 < seg_end; p++) {
4150+
if (p[0] == ':' && p[1] == ':') {
4151+
seg = p + 2;
4152+
}
4153+
}
4154+
return seg < seg_end && seg[0] >= 'A' && seg[0] <= 'Z';
4155+
}
4156+
41314157
/* Resolve only an exact identifier/scoped-path value. No branch, closure, or
41324158
* other composite expression enters this helper. A suffix fallback is admitted
41334159
* only for a qualified import path and only when it identifies one free func. */
@@ -4514,6 +4540,9 @@ static void rust_resolve_call_expression_inner(RustLSPContext *ctx, TSNode node)
45144540
} else {
45154541
tail = path;
45164542
}
4543+
/* `Type::f` is an associated function: the two free-function
4544+
* fallbacks below never bind it (see rust_path_qualifier_is_type). */
4545+
const bool assoc_call = rust_path_qualifier_is_type(path, tail);
45174546

45184547
/* Cross-crate workspace-member resolution (#56): when the call
45194548
* path's head is a declared Cargo workspace member (e.g.
@@ -4524,7 +4553,7 @@ static void rust_resolve_call_expression_inner(RustLSPContext *ctx, TSNode node)
45244553
* MEMBER crate by matching the registered QN's `.<member>.`
45254554
* path segment plus the call tail. Requires a parsed manifest
45264555
* (threaded through pass_lsp_cross.c); NULL manifest skips this. */
4527-
if (ctx->cargo_manifest && tail && *tail) {
4556+
if (!assoc_call && ctx->cargo_manifest && tail && *tail) {
45284557
const char *head_sep = strstr(path, "::");
45294558
if (head_sep && head_sep > path) {
45304559
char *head = cbm_arena_strndup(ctx->arena, path, (size_t)(head_sep - path));
@@ -4568,7 +4597,7 @@ static void rust_resolve_call_expression_inner(RustLSPContext *ctx, TSNode node)
45684597
* foo; use foo::bar; bar()` a chance to resolve when the
45694598
* intermediate module wasn't tracked through an explicit
45704599
* use-map entry. */
4571-
if (tail && *tail && ctx->module_qn) {
4600+
if (!assoc_call && tail && *tail && ctx->module_qn) {
45724601
/* Crate prefix is the first dotted segment of module_qn after
45734602
* the project name, but for simplicity we just match on
45744603
* "starts with first dot-segment". */

‎tests/test_pipeline.c‎

Lines changed: 106 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8874,6 +8874,40 @@ TEST(pipeline_go_rw_usage_never_cross_into_c) {
88748874
* the generic-engine lane on its 12-language fixture). A Rust call never
88758875
* targets another language's definition. pad_files > 0 forces the parallel
88768876
* resolver, whose Rust registry is the shared all-defs build. */
8877+
/* Pads a Rust fixture with pad_files filler modules (pad_files > 0 forces the
8878+
* parallel resolver: CBM_WORKERS=4 for the run), runs the full pipeline into
8879+
* <tmp>/<db_name> and opens the store. NULL store = the run failed. */
8880+
static cbm_store_t *rust_fixture_run(const char *tmp, int pad_files, const char *db_name,
8881+
cbm_pipeline_t **out_p) {
8882+
for (int i = 0; i < pad_files; i++) {
8883+
char name[64];
8884+
char body[128];
8885+
snprintf(name, sizeof(name), "src/filler%d.rs", i);
8886+
snprintf(body, sizeof(body), "pub fn filler%d() -> u32 {\n %d\n}\n", i, i);
8887+
write_temp_file(tmp, name, body);
8888+
}
8889+
char *saved = NULL;
8890+
if (pad_files > 0) {
8891+
char *old_workers = getenv("CBM_WORKERS");
8892+
saved = old_workers ? strdup(old_workers) : NULL;
8893+
cbm_setenv("CBM_WORKERS", "4", 1);
8894+
}
8895+
char db_path[512];
8896+
snprintf(db_path, sizeof(db_path), "%s/%s", tmp, db_name);
8897+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
8898+
int rc = p ? cbm_pipeline_run(p) : -1;
8899+
if (pad_files > 0) {
8900+
if (saved) {
8901+
cbm_setenv("CBM_WORKERS", saved, 1);
8902+
free(saved);
8903+
} else {
8904+
cbm_unsetenv("CBM_WORKERS");
8905+
}
8906+
}
8907+
*out_p = p;
8908+
return rc == 0 ? cbm_store_open_path(db_path) : NULL;
8909+
}
8910+
88778911
static int rust_registry_rust_only_case(int pad_files) {
88788912
char tmp[256];
88798913
snprintf(tmp, sizeof(tmp), "/tmp/cbm_rust_only_XXXXXX");
@@ -8917,28 +8951,10 @@ static int rust_registry_rust_only_case(int pad_files) {
89178951
"}\n"
89188952
"\n"
89198953
"1;\n");
8920-
for (int i = 0; i < pad_files; i++) {
8921-
char name[64];
8922-
char body[128];
8923-
snprintf(name, sizeof(name), "src/filler%d.rs", i);
8924-
snprintf(body, sizeof(body), "pub fn filler%d() -> u32 {\n %d\n}\n", i, i);
8925-
write_temp_file(tmp, name, body);
8926-
}
8927-
8928-
char *saved = NULL;
8929-
if (pad_files > 0) {
8930-
char *old_workers = getenv("CBM_WORKERS");
8931-
saved = old_workers ? strdup(old_workers) : NULL;
8932-
cbm_setenv("CBM_WORKERS", "4", 1);
8933-
}
8934-
char db_path[512];
8935-
snprintf(db_path, sizeof(db_path), "%s/rust_only.db", tmp);
8936-
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
8937-
ASSERT_NOT_NULL(p);
8938-
ASSERT_EQ(cbm_pipeline_run(p), 0);
8939-
const char *project = cbm_pipeline_project_name(p);
8940-
cbm_store_t *s = cbm_store_open_path(db_path);
8954+
cbm_pipeline_t *p = NULL;
8955+
cbm_store_t *s = rust_fixture_run(tmp, pad_files, "rust_only.db", &p);
89418956
ASSERT_NOT_NULL(s);
8957+
const char *project = cbm_pipeline_project_name(p);
89428958

89438959
/* Never onto the Perl constructor ... */
89448960
ASSERT_EQ(named_edge_to_file_count(s, project, "CALLS", "run", "new", "perlsrc/Lib.pm"), 0);
@@ -8947,14 +8963,6 @@ static int rust_registry_rust_only_case(int pad_files) {
89478963

89488964
cbm_store_close(s);
89498965
cbm_pipeline_free(p);
8950-
if (pad_files > 0) {
8951-
if (saved) {
8952-
cbm_setenv("CBM_WORKERS", saved, 1);
8953-
free(saved);
8954-
} else {
8955-
cbm_unsetenv("CBM_WORKERS");
8956-
}
8957-
}
89588966
th_rmtree(tmp);
89598967
PASS();
89608968
}
@@ -8967,6 +8975,73 @@ TEST(pipeline_rust_call_never_binds_another_language_parallel) {
89678975
return rust_registry_rust_only_case(52);
89688976
}
89698977

8978+
/* `Opts::write(true)` is an associated function. The LSP leaves it to the
8979+
* registry (import_map -> fsx.Opts.write), but its free-function short-name
8980+
* fallback bound it to the project's only free `write` (cache.write) first, and
8981+
* that LSP row pre-empted the right answer. With the cross-language namesakes
8982+
* gone (above), this hijack replaced 600+ correct qualified_suffix/import_map
8983+
* edges on the Rust corpus. A type-qualified call never takes that fallback. */
8984+
static int rust_assoc_call_free_namesake_case(int pad_files) {
8985+
char tmp[256];
8986+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_rust_assoc_XXXXXX");
8987+
if (!cbm_mkdtemp(tmp)) {
8988+
FAIL("tmpdir");
8989+
}
8990+
write_temp_file(tmp, "Cargo.toml", "[package]\nname = \"app\"\nversion = \"0.1.0\"\n");
8991+
write_temp_file(tmp, "src/fsx.rs",
8992+
"pub struct Opts {\n"
8993+
" pub w: bool,\n"
8994+
"}\n"
8995+
"\n"
8996+
"impl Opts {\n"
8997+
" pub fn write(w: bool) -> Opts {\n"
8998+
" Opts { w }\n"
8999+
" }\n"
9000+
"}\n");
9001+
write_temp_file(tmp, "src/cache.rs",
9002+
"pub fn write(p: &str) -> usize {\n"
9003+
" p.len()\n"
9004+
"}\n");
9005+
write_temp_file(tmp, "src/main.rs",
9006+
"mod cache;\n"
9007+
"mod fsx;\n"
9008+
"\n"
9009+
"use crate::fsx::Opts;\n"
9010+
"\n"
9011+
"fn run() -> bool {\n"
9012+
" let o = Opts::write(true);\n"
9013+
" o.w\n"
9014+
"}\n"
9015+
"\n"
9016+
"fn main() {\n"
9017+
" run();\n"
9018+
" cache::write(\"x\");\n"
9019+
"}\n");
9020+
cbm_pipeline_t *p = NULL;
9021+
cbm_store_t *s = rust_fixture_run(tmp, pad_files, "rust_assoc.db", &p);
9022+
ASSERT_NOT_NULL(s);
9023+
const char *project = cbm_pipeline_project_name(p);
9024+
9025+
/* The associated function, never the free namesake ... */
9026+
ASSERT_GTE(named_edge_to_file_count(s, project, "CALLS", "run", "write", "src/fsx.rs"), 1);
9027+
ASSERT_EQ(named_edge_to_file_count(s, project, "CALLS", "run", "write", "src/cache.rs"), 0);
9028+
/* ... while the module-qualified free call keeps its target. */
9029+
ASSERT_GTE(named_edge_to_file_count(s, project, "CALLS", "main", "write", "src/cache.rs"), 1);
9030+
9031+
cbm_store_close(s);
9032+
cbm_pipeline_free(p);
9033+
th_rmtree(tmp);
9034+
PASS();
9035+
}
9036+
9037+
TEST(pipeline_rust_assoc_call_never_binds_free_namesake) {
9038+
return rust_assoc_call_free_namesake_case(0);
9039+
}
9040+
9041+
TEST(pipeline_rust_assoc_call_never_binds_free_namesake_parallel) {
9042+
return rust_assoc_call_free_namesake_case(52);
9043+
}
9044+
89709045
TEST(pipeline_go_rw_usage_never_cross_into_c_parallel) {
89719046
/* Parallel twin of the test above: USAGE and WRITES/READS each have a
89729047
* second, independent resolver in pass_parallel.c (resolve_file_usages /
@@ -23829,6 +23904,8 @@ SUITE(pipeline) {
2382923904
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c_parallel);
2383023905
RUN_TEST(pipeline_rust_call_never_binds_another_language);
2383123906
RUN_TEST(pipeline_rust_call_never_binds_another_language_parallel);
23907+
RUN_TEST(pipeline_rust_assoc_call_never_binds_free_namesake);
23908+
RUN_TEST(pipeline_rust_assoc_call_never_binds_free_namesake_parallel);
2383223909
RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method);
2383323910
RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method_parallel);
2383423911
RUN_TEST(pipeline_go_bare_ref_never_binds_field);

0 commit comments

Comments
 (0)