Skip to content

Commit 08ee95a

Browse files
authored
Merge pull request DeusData#2681 from DeusData/fix/rust-registry-rust-defs-only
fix(rust-lsp): Rust calls never bind definitions of other languages
2 parents bddacfe + 5ae2a34 commit 08ee95a

4 files changed

Lines changed: 271 additions & 40 deletions

File tree

‎internal/cbm/lsp/rust_lsp.c‎

Lines changed: 58 additions & 21 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". */
@@ -6323,37 +6352,45 @@ static void rust_resolve_against_registry(CBMArena *arena, const char *source, i
63236352
* read-only, and shared across every Rust file's resolve (mirrors C/py/cs/ts).
63246353
* Converts the pipeline's CBMLSPDef into CBMRustLSPDef inline (same field copy as
63256354
* pass_lsp_cross.c's pxc_lspdefs_to_rust, including proven trait provenance).
6326-
* module_qn=NULL is byte-identical because all_defs always carry def_module_qn. */
6355+
* module_qn=NULL is byte-identical because all_defs always carry def_module_qn.
6356+
* all_defs is mixed-language: only Rust defs are registered, exactly like the
6357+
* per-file conversion, so a Perl `new` or a C function can never become the
6358+
* target of a Rust call (the Go builder filters the same way). */
63276359
CBMTypeRegistry *cbm_rust_build_cross_registry(CBMArena *arena, CBMLSPDef *defs, int def_count) {
63286360
if (!arena)
63296361
return NULL;
63306362
CBMTypeRegistry *reg = (CBMTypeRegistry *)cbm_arena_alloc(arena, sizeof(*reg));
63316363
if (!reg)
63326364
return NULL;
63336365
CBMRustLSPDef *rdefs = NULL;
6366+
int n = 0;
63346367
if (def_count > 0) {
63356368
rdefs = (CBMRustLSPDef *)cbm_arena_alloc(arena, (size_t)def_count * sizeof(CBMRustLSPDef));
63366369
if (!rdefs)
63376370
return NULL;
63386371
for (int i = 0; i < def_count; i++) {
6339-
rdefs[i].qualified_name = defs[i].qualified_name;
6340-
rdefs[i].short_name = defs[i].short_name;
6341-
rdefs[i].label = defs[i].label;
6342-
rdefs[i].receiver_type = defs[i].receiver_type;
6343-
rdefs[i].def_module_qn = defs[i].def_module_qn;
6344-
rdefs[i].return_types = defs[i].return_types;
6345-
rdefs[i].embedded_types = defs[i].embedded_types;
6346-
rdefs[i].field_defs = defs[i].field_defs;
6347-
rdefs[i].method_names_str = defs[i].method_names_str;
6348-
rdefs[i].signature_param_types = defs[i].signature_param_types;
6349-
rdefs[i].signature_param_count = defs[i].signature_param_count;
6350-
rdefs[i].trait_qn = defs[i].trait_qn;
6351-
rdefs[i].is_interface = defs[i].is_interface;
6352-
rdefs[i].is_rust_impl_relation = defs[i].is_rust_impl_relation;
6353-
rdefs[i].is_abstract = defs[i].is_abstract;
6354-
}
6355-
}
6356-
rust_populate_cross_registry(reg, arena, rdefs, def_count, /*module_qn=*/NULL);
6372+
if (defs[i].lang != CBM_LANG_RUST) {
6373+
continue;
6374+
}
6375+
rdefs[n].qualified_name = defs[i].qualified_name;
6376+
rdefs[n].short_name = defs[i].short_name;
6377+
rdefs[n].label = defs[i].label;
6378+
rdefs[n].receiver_type = defs[i].receiver_type;
6379+
rdefs[n].def_module_qn = defs[i].def_module_qn;
6380+
rdefs[n].return_types = defs[i].return_types;
6381+
rdefs[n].embedded_types = defs[i].embedded_types;
6382+
rdefs[n].field_defs = defs[i].field_defs;
6383+
rdefs[n].method_names_str = defs[i].method_names_str;
6384+
rdefs[n].signature_param_types = defs[i].signature_param_types;
6385+
rdefs[n].signature_param_count = defs[i].signature_param_count;
6386+
rdefs[n].trait_qn = defs[i].trait_qn;
6387+
rdefs[n].is_interface = defs[i].is_interface;
6388+
rdefs[n].is_rust_impl_relation = defs[i].is_rust_impl_relation;
6389+
rdefs[n].is_abstract = defs[i].is_abstract;
6390+
n++;
6391+
}
6392+
}
6393+
rust_populate_cross_registry(reg, arena, n > 0 ? rdefs : NULL, n, /*module_qn=*/NULL);
63576394
reg->read_only = true; /* seal: shared Tier-2 registry is read-only during resolve */
63586395
return reg;
63596396
}

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,31 +1598,42 @@ const struct CBMCargoManifest *cbm_pxc_get_rust_manifest(void) {
15981598
/* Convert a CBMLSPDef array (the pipeline's lingua franca, go_lsp.h:73)
15991599
* into a CBMRustLSPDef array (rust_lsp.h) inside `arena`. The two structs
16001600
* have similar fields but different layouts; CBMLSPDef adds
1601-
* language/namespace metadata, so a memcpy is unsafe — copy field-by-field. */
1602-
static CBMRustLSPDef *pxc_lspdefs_to_rust(CBMArena *arena, const CBMLSPDef *defs, int def_count) {
1601+
* language/namespace metadata, so a memcpy is unsafe — copy field-by-field.
1602+
* Only Rust defs are kept: the def set is mixed-language, and a Perl or C
1603+
* function must never become a Rust call target (the Go registrar filters
1604+
* the same way). *out_count gets the number kept. */
1605+
static CBMRustLSPDef *pxc_lspdefs_to_rust(CBMArena *arena, const CBMLSPDef *defs, int def_count,
1606+
int *out_count) {
1607+
*out_count = 0;
16031608
if (!defs || def_count <= 0)
16041609
return NULL;
16051610
CBMRustLSPDef *out =
16061611
(CBMRustLSPDef *)cbm_arena_alloc(arena, (size_t)def_count * sizeof(CBMRustLSPDef));
16071612
if (!out)
16081613
return NULL;
1614+
int n = 0;
16091615
for (int i = 0; i < def_count; i++) {
1610-
out[i].qualified_name = defs[i].qualified_name;
1611-
out[i].short_name = defs[i].short_name;
1612-
out[i].label = defs[i].label;
1613-
out[i].receiver_type = defs[i].receiver_type;
1614-
out[i].def_module_qn = defs[i].def_module_qn;
1615-
out[i].return_types = defs[i].return_types;
1616-
out[i].embedded_types = defs[i].embedded_types;
1617-
out[i].field_defs = defs[i].field_defs;
1618-
out[i].method_names_str = defs[i].method_names_str;
1619-
out[i].signature_param_types = defs[i].signature_param_types;
1620-
out[i].signature_param_count = defs[i].signature_param_count;
1621-
out[i].trait_qn = defs[i].trait_qn;
1622-
out[i].is_interface = defs[i].is_interface;
1623-
out[i].is_rust_impl_relation = defs[i].is_rust_impl_relation;
1624-
out[i].is_abstract = defs[i].is_abstract;
1616+
if (defs[i].lang != CBM_LANG_RUST) {
1617+
continue;
1618+
}
1619+
out[n].qualified_name = defs[i].qualified_name;
1620+
out[n].short_name = defs[i].short_name;
1621+
out[n].label = defs[i].label;
1622+
out[n].receiver_type = defs[i].receiver_type;
1623+
out[n].def_module_qn = defs[i].def_module_qn;
1624+
out[n].return_types = defs[i].return_types;
1625+
out[n].embedded_types = defs[i].embedded_types;
1626+
out[n].field_defs = defs[i].field_defs;
1627+
out[n].method_names_str = defs[i].method_names_str;
1628+
out[n].signature_param_types = defs[i].signature_param_types;
1629+
out[n].signature_param_count = defs[i].signature_param_count;
1630+
out[n].trait_qn = defs[i].trait_qn;
1631+
out[n].is_interface = defs[i].is_interface;
1632+
out[n].is_rust_impl_relation = defs[i].is_rust_impl_relation;
1633+
out[n].is_abstract = defs[i].is_abstract;
1634+
n++;
16251635
}
1636+
*out_count = n;
16261637
return out;
16271638
}
16281639

@@ -1709,9 +1720,10 @@ void cbm_pxc_run_one_with_php_bindings(CBMLanguage lang, CBMFileResult *r, const
17091720
* but diverge after, so convert into the scratch arena. The
17101721
* workspace manifest (set once by the sequential driver) lets
17111722
* `crate_a::foo` route across the crate boundary (#56). */
1712-
CBMRustLSPDef *rdefs = pxc_lspdefs_to_rust(&scratch, defs, def_count);
1723+
int rust_def_count = 0;
1724+
CBMRustLSPDef *rdefs = pxc_lspdefs_to_rust(&scratch, defs, def_count, &rust_def_count);
17131725
cbm_run_rust_lsp_cross_with_manifest(&scratch, source, source_len, module_qn, rdefs,
1714-
def_count, imp_names, imp_qns, imp_count, tree,
1726+
rust_def_count, imp_names, imp_qns, imp_count, tree,
17151727
g_pxc_rust_manifest, &out, &synthetic_calls);
17161728
break;
17171729
}

‎tests/test_pipeline.c‎

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8868,6 +8868,180 @@ TEST(pipeline_go_rw_usage_never_cross_into_c) {
88688868
PASS();
88698869
}
88708870

8871+
/* The Rust registries were built from the mixed-language def set: a Perl
8872+
* `sub new` became a Rust free function, and the project-wide
8873+
* lsp_short_name_unique fallback bound `Store::new()` to it at 0.90 (found by
8874+
* the generic-engine lane on its 12-language fixture). A Rust call never
8875+
* targets another language's definition. pad_files > 0 forces the parallel
8876+
* 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+
8911+
static int rust_registry_rust_only_case(int pad_files) {
8912+
char tmp[256];
8913+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_rust_only_XXXXXX");
8914+
if (!cbm_mkdtemp(tmp)) {
8915+
FAIL("tmpdir");
8916+
}
8917+
write_temp_file(tmp, "Cargo.toml", "[package]\nname = \"app\"\nversion = \"0.1.0\"\n");
8918+
write_temp_file(tmp, "src/store.rs",
8919+
"pub struct Store {\n"
8920+
" n: u32,\n"
8921+
"}\n"
8922+
"\n"
8923+
"impl Store {\n"
8924+
" pub fn new() -> Store {\n"
8925+
" Store { n: 0 }\n"
8926+
" }\n"
8927+
"\n"
8928+
" pub fn get(&self, k: u32) -> u32 {\n"
8929+
" self.n + k\n"
8930+
" }\n"
8931+
"}\n");
8932+
write_temp_file(tmp, "src/main.rs",
8933+
"mod store;\n"
8934+
"\n"
8935+
"use crate::store::Store;\n"
8936+
"\n"
8937+
"fn run() -> u32 {\n"
8938+
" let s = Store::new();\n"
8939+
" s.get(1)\n"
8940+
"}\n"
8941+
"\n"
8942+
"fn main() {\n"
8943+
" run();\n"
8944+
"}\n");
8945+
write_temp_file(tmp, "perlsrc/Lib.pm",
8946+
"package Lib;\n"
8947+
"\n"
8948+
"sub new {\n"
8949+
" my ($class) = @_;\n"
8950+
" return bless {}, $class;\n"
8951+
"}\n"
8952+
"\n"
8953+
"1;\n");
8954+
cbm_pipeline_t *p = NULL;
8955+
cbm_store_t *s = rust_fixture_run(tmp, pad_files, "rust_only.db", &p);
8956+
ASSERT_NOT_NULL(s);
8957+
const char *project = cbm_pipeline_project_name(p);
8958+
8959+
/* Never onto the Perl constructor ... */
8960+
ASSERT_EQ(named_edge_to_file_count(s, project, "CALLS", "run", "new", "perlsrc/Lib.pm"), 0);
8961+
/* ... and the Rust call still binds the Rust associated function. */
8962+
ASSERT_GTE(named_edge_to_file_count(s, project, "CALLS", "run", "new", "src/store.rs"), 1);
8963+
8964+
cbm_store_close(s);
8965+
cbm_pipeline_free(p);
8966+
th_rmtree(tmp);
8967+
PASS();
8968+
}
8969+
8970+
TEST(pipeline_rust_call_never_binds_another_language) {
8971+
return rust_registry_rust_only_case(0);
8972+
}
8973+
8974+
TEST(pipeline_rust_call_never_binds_another_language_parallel) {
8975+
return rust_registry_rust_only_case(52);
8976+
}
8977+
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+
88719045
TEST(pipeline_go_rw_usage_never_cross_into_c_parallel) {
88729046
/* Parallel twin of the test above: USAGE and WRITES/READS each have a
88739047
* second, independent resolver in pass_parallel.c (resolve_file_usages /
@@ -23845,6 +24019,10 @@ SUITE(pipeline) {
2384524019
RUN_TEST(pipeline_html_embedded_member_call_stays_unbound);
2384624020
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c);
2384724021
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c_parallel);
24022+
RUN_TEST(pipeline_rust_call_never_binds_another_language);
24023+
RUN_TEST(pipeline_rust_call_never_binds_another_language_parallel);
24024+
RUN_TEST(pipeline_rust_assoc_call_never_binds_free_namesake);
24025+
RUN_TEST(pipeline_rust_assoc_call_never_binds_free_namesake_parallel);
2384824026
RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method);
2384924027
RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method_parallel);
2385024028
RUN_TEST(pipeline_go_bare_ref_never_binds_field);

0 commit comments

Comments
 (0)