Skip to content

Commit 2ac9c33

Browse files
committed
fix(rust-lsp): a Rust call never binds another language's definition
Both Rust registries were built from the pipeline's mixed-language def set. The shared Tier-2 build (cbm_rust_build_cross_registry) and the per-file conversion (pxc_lspdefs_to_rust) copied every def, so a Perl `sub new` or any C function became a Rust free function. The lsp_short_name_unique fallback then bound `Store::new()` to the Perl constructor at confidence 0.90 on both venues: its "crate scope" check compares only the first module-QN segment, which is the project name. (Found by the generic-engine lane on a 12-language fixture.) Register Rust defs only, at both conversion points, exactly as the Go builder filters to Go. Rust impl-relation records already carry CBM_LANG_RUST. As a side effect, the shared Rust build no longer registers the C defs of a mixed C+Rust tree. Tests: pipeline_rust_call_never_binds_another_language (sequential and parallel): no CALLS from the Rust caller into the Perl file, and the Rust associated function still binds. Signed-off-by: Martin Vogel <[email protected]>
1 parent 9f85f10 commit 2ac9c33

3 files changed

Lines changed: 159 additions & 38 deletions

File tree

‎internal/cbm/lsp/rust_lsp.c‎

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6323,37 +6323,45 @@ static void rust_resolve_against_registry(CBMArena *arena, const char *source, i
63236323
* read-only, and shared across every Rust file's resolve (mirrors C/py/cs/ts).
63246324
* Converts the pipeline's CBMLSPDef into CBMRustLSPDef inline (same field copy as
63256325
* 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. */
6326+
* module_qn=NULL is byte-identical because all_defs always carry def_module_qn.
6327+
* all_defs is mixed-language: only Rust defs are registered, exactly like the
6328+
* per-file conversion, so a Perl `new` or a C function can never become the
6329+
* target of a Rust call (the Go builder filters the same way). */
63276330
CBMTypeRegistry *cbm_rust_build_cross_registry(CBMArena *arena, CBMLSPDef *defs, int def_count) {
63286331
if (!arena)
63296332
return NULL;
63306333
CBMTypeRegistry *reg = (CBMTypeRegistry *)cbm_arena_alloc(arena, sizeof(*reg));
63316334
if (!reg)
63326335
return NULL;
63336336
CBMRustLSPDef *rdefs = NULL;
6337+
int n = 0;
63346338
if (def_count > 0) {
63356339
rdefs = (CBMRustLSPDef *)cbm_arena_alloc(arena, (size_t)def_count * sizeof(CBMRustLSPDef));
63366340
if (!rdefs)
63376341
return NULL;
63386342
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);
6343+
if (defs[i].lang != CBM_LANG_RUST) {
6344+
continue;
6345+
}
6346+
rdefs[n].qualified_name = defs[i].qualified_name;
6347+
rdefs[n].short_name = defs[i].short_name;
6348+
rdefs[n].label = defs[i].label;
6349+
rdefs[n].receiver_type = defs[i].receiver_type;
6350+
rdefs[n].def_module_qn = defs[i].def_module_qn;
6351+
rdefs[n].return_types = defs[i].return_types;
6352+
rdefs[n].embedded_types = defs[i].embedded_types;
6353+
rdefs[n].field_defs = defs[i].field_defs;
6354+
rdefs[n].method_names_str = defs[i].method_names_str;
6355+
rdefs[n].signature_param_types = defs[i].signature_param_types;
6356+
rdefs[n].signature_param_count = defs[i].signature_param_count;
6357+
rdefs[n].trait_qn = defs[i].trait_qn;
6358+
rdefs[n].is_interface = defs[i].is_interface;
6359+
rdefs[n].is_rust_impl_relation = defs[i].is_rust_impl_relation;
6360+
rdefs[n].is_abstract = defs[i].is_abstract;
6361+
n++;
6362+
}
6363+
}
6364+
rust_populate_cross_registry(reg, arena, n > 0 ? rdefs : NULL, n, /*module_qn=*/NULL);
63576365
reg->read_only = true; /* seal: shared Tier-2 registry is read-only during resolve */
63586366
return reg;
63596367
}

‎src/pipeline/pass_lsp_cross.c‎

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,31 +1470,42 @@ const struct CBMCargoManifest *cbm_pxc_get_rust_manifest(void) {
14701470
/* Convert a CBMLSPDef array (the pipeline's lingua franca, go_lsp.h:73)
14711471
* into a CBMRustLSPDef array (rust_lsp.h) inside `arena`. The two structs
14721472
* have similar fields but different layouts; CBMLSPDef adds
1473-
* language/namespace metadata, so a memcpy is unsafe — copy field-by-field. */
1474-
static CBMRustLSPDef *pxc_lspdefs_to_rust(CBMArena *arena, const CBMLSPDef *defs, int def_count) {
1473+
* language/namespace metadata, so a memcpy is unsafe — copy field-by-field.
1474+
* Only Rust defs are kept: the def set is mixed-language, and a Perl or C
1475+
* function must never become a Rust call target (the Go registrar filters
1476+
* the same way). *out_count gets the number kept. */
1477+
static CBMRustLSPDef *pxc_lspdefs_to_rust(CBMArena *arena, const CBMLSPDef *defs, int def_count,
1478+
int *out_count) {
1479+
*out_count = 0;
14751480
if (!defs || def_count <= 0)
14761481
return NULL;
14771482
CBMRustLSPDef *out =
14781483
(CBMRustLSPDef *)cbm_arena_alloc(arena, (size_t)def_count * sizeof(CBMRustLSPDef));
14791484
if (!out)
14801485
return NULL;
1486+
int n = 0;
14811487
for (int i = 0; i < def_count; i++) {
1482-
out[i].qualified_name = defs[i].qualified_name;
1483-
out[i].short_name = defs[i].short_name;
1484-
out[i].label = defs[i].label;
1485-
out[i].receiver_type = defs[i].receiver_type;
1486-
out[i].def_module_qn = defs[i].def_module_qn;
1487-
out[i].return_types = defs[i].return_types;
1488-
out[i].embedded_types = defs[i].embedded_types;
1489-
out[i].field_defs = defs[i].field_defs;
1490-
out[i].method_names_str = defs[i].method_names_str;
1491-
out[i].signature_param_types = defs[i].signature_param_types;
1492-
out[i].signature_param_count = defs[i].signature_param_count;
1493-
out[i].trait_qn = defs[i].trait_qn;
1494-
out[i].is_interface = defs[i].is_interface;
1495-
out[i].is_rust_impl_relation = defs[i].is_rust_impl_relation;
1496-
out[i].is_abstract = defs[i].is_abstract;
1488+
if (defs[i].lang != CBM_LANG_RUST) {
1489+
continue;
1490+
}
1491+
out[n].qualified_name = defs[i].qualified_name;
1492+
out[n].short_name = defs[i].short_name;
1493+
out[n].label = defs[i].label;
1494+
out[n].receiver_type = defs[i].receiver_type;
1495+
out[n].def_module_qn = defs[i].def_module_qn;
1496+
out[n].return_types = defs[i].return_types;
1497+
out[n].embedded_types = defs[i].embedded_types;
1498+
out[n].field_defs = defs[i].field_defs;
1499+
out[n].method_names_str = defs[i].method_names_str;
1500+
out[n].signature_param_types = defs[i].signature_param_types;
1501+
out[n].signature_param_count = defs[i].signature_param_count;
1502+
out[n].trait_qn = defs[i].trait_qn;
1503+
out[n].is_interface = defs[i].is_interface;
1504+
out[n].is_rust_impl_relation = defs[i].is_rust_impl_relation;
1505+
out[n].is_abstract = defs[i].is_abstract;
1506+
n++;
14971507
}
1508+
*out_count = n;
14981509
return out;
14991510
}
15001511

@@ -1581,9 +1592,10 @@ void cbm_pxc_run_one_with_php_bindings(CBMLanguage lang, CBMFileResult *r, const
15811592
* but diverge after, so convert into the scratch arena. The
15821593
* workspace manifest (set once by the sequential driver) lets
15831594
* `crate_a::foo` route across the crate boundary (#56). */
1584-
CBMRustLSPDef *rdefs = pxc_lspdefs_to_rust(&scratch, defs, def_count);
1595+
int rust_def_count = 0;
1596+
CBMRustLSPDef *rdefs = pxc_lspdefs_to_rust(&scratch, defs, def_count, &rust_def_count);
15851597
cbm_run_rust_lsp_cross_with_manifest(&scratch, source, source_len, module_qn, rdefs,
1586-
def_count, imp_names, imp_qns, imp_count, tree,
1598+
rust_def_count, imp_names, imp_qns, imp_count, tree,
15871599
g_pxc_rust_manifest, &out, &synthetic_calls);
15881600
break;
15891601
}

‎tests/test_pipeline.c‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8757,6 +8757,105 @@ TEST(pipeline_go_rw_usage_never_cross_into_c) {
87578757
PASS();
87588758
}
87598759

8760+
/* The Rust registries were built from the mixed-language def set: a Perl
8761+
* `sub new` became a Rust free function, and the project-wide
8762+
* lsp_short_name_unique fallback bound `Store::new()` to it at 0.90 (found by
8763+
* the generic-engine lane on its 12-language fixture). A Rust call never
8764+
* targets another language's definition. pad_files > 0 forces the parallel
8765+
* resolver, whose Rust registry is the shared all-defs build. */
8766+
static int rust_registry_rust_only_case(int pad_files) {
8767+
char tmp[256];
8768+
snprintf(tmp, sizeof(tmp), "/tmp/cbm_rust_only_XXXXXX");
8769+
if (!cbm_mkdtemp(tmp)) {
8770+
FAIL("tmpdir");
8771+
}
8772+
write_temp_file(tmp, "Cargo.toml", "[package]\nname = \"app\"\nversion = \"0.1.0\"\n");
8773+
write_temp_file(tmp, "src/store.rs",
8774+
"pub struct Store {\n"
8775+
" n: u32,\n"
8776+
"}\n"
8777+
"\n"
8778+
"impl Store {\n"
8779+
" pub fn new() -> Store {\n"
8780+
" Store { n: 0 }\n"
8781+
" }\n"
8782+
"\n"
8783+
" pub fn get(&self, k: u32) -> u32 {\n"
8784+
" self.n + k\n"
8785+
" }\n"
8786+
"}\n");
8787+
write_temp_file(tmp, "src/main.rs",
8788+
"mod store;\n"
8789+
"\n"
8790+
"use crate::store::Store;\n"
8791+
"\n"
8792+
"fn run() -> u32 {\n"
8793+
" let s = Store::new();\n"
8794+
" s.get(1)\n"
8795+
"}\n"
8796+
"\n"
8797+
"fn main() {\n"
8798+
" run();\n"
8799+
"}\n");
8800+
write_temp_file(tmp, "perlsrc/Lib.pm",
8801+
"package Lib;\n"
8802+
"\n"
8803+
"sub new {\n"
8804+
" my ($class) = @_;\n"
8805+
" return bless {}, $class;\n"
8806+
"}\n"
8807+
"\n"
8808+
"1;\n");
8809+
for (int i = 0; i < pad_files; i++) {
8810+
char name[64];
8811+
char body[128];
8812+
snprintf(name, sizeof(name), "src/filler%d.rs", i);
8813+
snprintf(body, sizeof(body), "pub fn filler%d() -> u32 {\n %d\n}\n", i, i);
8814+
write_temp_file(tmp, name, body);
8815+
}
8816+
8817+
char *saved = NULL;
8818+
if (pad_files > 0) {
8819+
char *old_workers = getenv("CBM_WORKERS");
8820+
saved = old_workers ? strdup(old_workers) : NULL;
8821+
cbm_setenv("CBM_WORKERS", "4", 1);
8822+
}
8823+
char db_path[512];
8824+
snprintf(db_path, sizeof(db_path), "%s/rust_only.db", tmp);
8825+
cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
8826+
ASSERT_NOT_NULL(p);
8827+
ASSERT_EQ(cbm_pipeline_run(p), 0);
8828+
const char *project = cbm_pipeline_project_name(p);
8829+
cbm_store_t *s = cbm_store_open_path(db_path);
8830+
ASSERT_NOT_NULL(s);
8831+
8832+
/* Never onto the Perl constructor ... */
8833+
ASSERT_EQ(named_edge_to_file_count(s, project, "CALLS", "run", "new", "perlsrc/Lib.pm"), 0);
8834+
/* ... and the Rust call still binds the Rust associated function. */
8835+
ASSERT_GTE(named_edge_to_file_count(s, project, "CALLS", "run", "new", "src/store.rs"), 1);
8836+
8837+
cbm_store_close(s);
8838+
cbm_pipeline_free(p);
8839+
if (pad_files > 0) {
8840+
if (saved) {
8841+
cbm_setenv("CBM_WORKERS", saved, 1);
8842+
free(saved);
8843+
} else {
8844+
cbm_unsetenv("CBM_WORKERS");
8845+
}
8846+
}
8847+
th_rmtree(tmp);
8848+
PASS();
8849+
}
8850+
8851+
TEST(pipeline_rust_call_never_binds_another_language) {
8852+
return rust_registry_rust_only_case(0);
8853+
}
8854+
8855+
TEST(pipeline_rust_call_never_binds_another_language_parallel) {
8856+
return rust_registry_rust_only_case(52);
8857+
}
8858+
87608859
TEST(pipeline_go_rw_usage_never_cross_into_c_parallel) {
87618860
/* Parallel twin of the test above: USAGE and WRITES/READS each have a
87628861
* second, independent resolver in pass_parallel.c (resolve_file_usages /
@@ -23615,6 +23714,8 @@ SUITE(pipeline) {
2361523714
RUN_TEST(pipeline_html_embedded_member_call_stays_unbound);
2361623715
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c);
2361723716
RUN_TEST(pipeline_go_rw_usage_never_cross_into_c_parallel);
23717+
RUN_TEST(pipeline_rust_call_never_binds_another_language);
23718+
RUN_TEST(pipeline_rust_call_never_binds_another_language_parallel);
2361823719
RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method);
2361923720
RUN_TEST(pipeline_rust_std_receiver_never_binds_project_method_parallel);
2362023721
RUN_TEST(pipeline_go_bare_ref_never_binds_field);

0 commit comments

Comments
 (0)