Skip to content

Commit bf2daa3

Browse files
committed
fix(lsp): detach field arrays before overlay refinement
Copy inherited field-type pointer arrays into the writable registry arena before Python refines an existing field. Keep shared type objects intact and avoid retaining per-file scratch types in the sealed fallback. Add deterministic shared-state and later-overlay coverage, retaining the sequential Python field regression and adding a forced-parallel variant. Refs DeusData#1277 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 519c765 commit bf2daa3

3 files changed

Lines changed: 203 additions & 20 deletions

File tree

‎internal/cbm/lsp/type_registry.c‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,8 +677,28 @@ CBMRegisteredType *cbm_registry_type_for_update(CBMTypeRegistry *head, const cha
677677
for (const CBMTypeRegistry *r = head->fallback; r; r = r->fallback) {
678678
const CBMRegisteredType *base = lookup_type_self(r, qualified_name);
679679
if (base) {
680+
CBMRegisteredType copy = *base;
681+
/* Field refinements overwrite entries in this array. The type
682+
* objects remain immutable, but the writable overlay must own
683+
* the pointer array rather than mutate its shared fallback. */
684+
if (base->field_types) {
685+
size_t count = 0;
686+
/* Types are parallel to names; a named field may have no
687+
* type, so an interior NULL does not terminate that array. */
688+
while (base->field_names ? base->field_names[count] != NULL
689+
: base->field_types[count] != NULL) {
690+
count++;
691+
}
692+
const CBMType **field_types = (const CBMType **)cbm_arena_alloc(
693+
head->arena, (count + 1) * sizeof(*field_types));
694+
if (!field_types) {
695+
return NULL;
696+
}
697+
memcpy(field_types, base->field_types, (count + 1) * sizeof(*field_types));
698+
copy.field_types = field_types;
699+
}
680700
int before = head->type_count;
681-
cbm_registry_add_type(head, *base);
701+
cbm_registry_add_type(head, copy);
682702
if (head->type_count == before + 1) {
683703
return &head->types[before];
684704
}

‎tests/test_c_lsp.c‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15350,6 +15350,10 @@ TEST(registry_overlay_chain_iterates_and_copies_on_write) {
1535015350
memset(&t, 0, sizeof(t));
1535115351
t.qualified_name = "pkg.T";
1535215352
t.short_name = "T";
15353+
const char *field_names[] = {"untyped", "typed", NULL};
15354+
const CBMType *field_types[] = {NULL, cbm_type_named(&arena, "pkg.Value"), NULL};
15355+
t.field_names = field_names;
15356+
t.field_types = field_types;
1535315357
cbm_registry_add_type(&base, t);
1535415358
cbm_registry_finalize(&base);
1535515359
base.read_only = true;
@@ -15414,6 +15418,10 @@ TEST(registry_overlay_chain_iterates_and_copies_on_write) {
1541415418
CBMRegisteredType *wt = cbm_registry_type_for_update(&overlay, "pkg.T");
1541515419
ASSERT_NOT_NULL(wt);
1541615420
ASSERT_EQ(overlay.type_count, 1);
15421+
/* A missing type for an earlier named field must not truncate the copy. */
15422+
bool field_slots_preserved = wt->field_types && wt->field_types != field_types &&
15423+
wt->field_types[0] == NULL &&
15424+
wt->field_types[1] == field_types[1] && wt->field_types[2] == NULL;
1541715425
cbm_registry_all_types_chain(&overlay, &ti);
1541815426
seen = 0;
1541915427
while (cbm_type_short_iter_next(&ti) >= 0) {
@@ -15430,6 +15438,7 @@ TEST(registry_overlay_chain_iterates_and_copies_on_write) {
1543015438

1543115439
cbm_arena_destroy(&scratch);
1543215440
cbm_arena_destroy(&arena);
15441+
ASSERT_TRUE(field_slots_preserved);
1543315442
PASS();
1543415443
}
1543515444

@@ -15804,6 +15813,111 @@ TEST(seal_py_shared_registry_readonly_fields) {
1580415813
PASS();
1580515814
}
1580615815

15816+
/* #1277: the production resolver receives a writable overlay, so refining an
15817+
* existing field must detach its pointer array from the sealed fallback. */
15818+
TEST(seal_py_overlay_existing_field_keeps_shared_type) {
15819+
CBMArena arena;
15820+
cbm_arena_init(&arena);
15821+
CBMLSPDef defs[4] = {0};
15822+
defs[0].qualified_name = "test.mod.Foo";
15823+
defs[0].short_name = "Foo";
15824+
defs[0].label = "Class";
15825+
defs[0].def_module_qn = "test.mod";
15826+
defs[0].field_defs = "x:test.contracts.Contract";
15827+
defs[1].qualified_name = "test.mod.Foo.m";
15828+
defs[1].short_name = "m";
15829+
defs[1].label = "Method";
15830+
defs[1].receiver_type = "test.mod.Foo";
15831+
defs[1].def_module_qn = "test.mod";
15832+
defs[2].qualified_name = "test.contracts.Contract";
15833+
defs[2].short_name = "Contract";
15834+
defs[2].label = "Class";
15835+
defs[2].def_module_qn = "test.contracts";
15836+
defs[3].qualified_name = "test.contracts.Contract.process_batch";
15837+
defs[3].short_name = "process_batch";
15838+
defs[3].label = "Method";
15839+
defs[3].receiver_type = "test.contracts.Contract";
15840+
defs[3].def_module_qn = "test.contracts";
15841+
for (int i = 0; i < 4; i++) {
15842+
defs[i].lang = CBM_LANG_PYTHON;
15843+
}
15844+
CBMTypeRegistry *reg = cbm_py_build_cross_registry(&arena, defs, 4);
15845+
ASSERT_NOT_NULL(reg);
15846+
ASSERT_TRUE(reg->read_only);
15847+
const CBMRegisteredType *base = cbm_registry_lookup_type(reg, "test.mod.Foo");
15848+
ASSERT_NOT_NULL(base);
15849+
ASSERT_NOT_NULL(base->field_names);
15850+
ASSERT_NOT_NULL(base->field_types);
15851+
const char **names_before = base->field_names;
15852+
const CBMType **types_before = base->field_types;
15853+
const CBMType *type_before = base->field_types[0];
15854+
ASSERT_NOT_NULL(type_before);
15855+
ASSERT_NULL(base->field_types[1]);
15856+
15857+
CBMArena scratch;
15858+
cbm_arena_init(&scratch);
15859+
CBMTypeRegistry overlay;
15860+
cbm_registry_init(&overlay, &scratch);
15861+
overlay.fallback = reg;
15862+
const char *writer = "class Foo:\n"
15863+
" def m(self):\n"
15864+
" self.x = 1\n";
15865+
CBMResolvedCallArray out = {0};
15866+
cbm_run_py_lsp_cross_with_registry(&scratch, writer, (int)strlen(writer), "test.mod", &overlay,
15867+
NULL, NULL, 0, NULL, &out, NULL);
15868+
15869+
/* Observe while scratch is live, then release both arenas before any
15870+
* failure assertion so the regression also fails cleanly under LSan. */
15871+
bool shared_names_unchanged = base->field_names == names_before;
15872+
bool shared_types_unchanged = base->field_types == types_before;
15873+
bool shared_element_unchanged = base->field_types[0] == type_before;
15874+
bool shared_terminator_unchanged = base->field_types[1] == NULL;
15875+
const CBMRegisteredType *refined = cbm_registry_lookup_type(&overlay, "test.mod.Foo");
15876+
bool local_refined = refined && refined != base && refined->field_types &&
15877+
refined->field_types != types_before && refined->field_types[0] &&
15878+
refined->field_types[0] != type_before &&
15879+
refined->field_types[0]->kind == CBM_TYPE_BUILTIN &&
15880+
refined->field_types[1] == NULL;
15881+
cbm_arena_destroy(&scratch);
15882+
15883+
/* Never read a corrupted shared pointer after scratch destruction. When
15884+
* intact, a later file must still see the original Contract field. */
15885+
int hits = 0;
15886+
if (shared_names_unchanged && shared_types_unchanged && shared_element_unchanged &&
15887+
shared_terminator_unchanged && local_refined) {
15888+
cbm_arena_init(&scratch);
15889+
cbm_registry_init(&overlay, &scratch);
15890+
overlay.fallback = reg;
15891+
const char *reader = "from mod import Foo\n\n"
15892+
"def run(foo: Foo):\n"
15893+
" foo.x.process_batch()\n";
15894+
const char *import_names[] = {"Foo"};
15895+
const char *import_qns[] = {"test.mod.Foo"};
15896+
memset(&out, 0, sizeof(out));
15897+
cbm_run_py_lsp_cross_with_registry(&scratch, reader, (int)strlen(reader), "test.reader",
15898+
&overlay, import_names, import_qns, 1, NULL, &out, NULL);
15899+
for (int i = 0; i < out.count; i++) {
15900+
const CBMResolvedCall *call = &out.items[i];
15901+
if (call->caller_qn && call->callee_qn &&
15902+
strcmp(call->caller_qn, "test.reader.run") == 0 &&
15903+
strcmp(call->callee_qn, "test.contracts.Contract.process_batch") == 0 &&
15904+
call->kind == CBM_RESOLVED_INVOCATION &&
15905+
call->confidence >= CBM_LSP_CONFIDENCE_FLOOR) {
15906+
hits++;
15907+
}
15908+
}
15909+
cbm_arena_destroy(&scratch);
15910+
}
15911+
cbm_arena_destroy(&arena);
15912+
ASSERT_TRUE(shared_names_unchanged);
15913+
ASSERT_TRUE(shared_types_unchanged);
15914+
ASSERT_TRUE(shared_element_unchanged);
15915+
ASSERT_TRUE(shared_terminator_unchanged);
15916+
ASSERT_TRUE(local_refined);
15917+
ASSERT_EQ(hits, 1);
15918+
PASS();
15919+
}
15920+
1580715921
TEST(seal_cs_shared_registry_readonly) {
1580815922
CBMArena arena;
1580915923
cbm_arena_init(&arena);
@@ -16402,6 +16516,7 @@ SUITE(c_lsp) {
1640216516
RUN_TEST(registry_overlay_chain_iterates_and_copies_on_write);
1640316517
RUN_TEST(clsp_method_return_refinement_is_copy_on_write);
1640416518
RUN_TEST(seal_py_shared_registry_readonly_fields);
16519+
RUN_TEST(seal_py_overlay_existing_field_keeps_shared_type);
1640516520
RUN_TEST(seal_cs_shared_registry_readonly);
1640616521
RUN_TEST(seal_ts_shared_registry_readonly);
1640716522
RUN_TEST(seal_go_shared_registry_readonly);

‎tests/test_pipeline.c‎

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8198,7 +8198,7 @@ static int count_lsp_calls(cbm_store_t *s, const char *project, const char *call
81988198
return hits;
81998199
}
82008200

8201-
TEST(python_crossfile_typed_field_calls_issue1277) {
8201+
static int check_python_crossfile_typed_field_calls_issue1277(bool parallel) {
82028202
const char *contracts = "class Contract:\n"
82038203
" def process_batch(self) -> None:\n"
82048204
" ...\n";
@@ -8226,38 +8226,85 @@ TEST(python_crossfile_typed_field_calls_issue1277) {
82268226
}
82278227
char path[512];
82288228
snprintf(path, sizeof(path), "%s/loop.py", g_usages_tmpdir);
8229-
FILE *f = fopen(path, "w");
8230-
ASSERT_NOT_NULL(f);
8231-
fputs(loop, f);
8232-
fclose(f);
8229+
if (th_write_file(path, loop) != 0) {
8230+
teardown_usages_repo();
8231+
FAIL("failed to write loop.py");
8232+
}
8233+
if (parallel) {
8234+
/* Three semantic files plus 50 inert files exceed the production
8235+
* threshold for the fused parallel pipeline. */
8236+
for (int i = 0; i < 50; i++) {
8237+
snprintf(path, sizeof(path), "%s/field_pad_%02d.py", g_usages_tmpdir, i);
8238+
if (th_write_file(path, "field_padding = 0\n") != 0) {
8239+
teardown_usages_repo();
8240+
FAIL("failed to write parallel-selection fixture");
8241+
}
8242+
}
8243+
}
8244+
8245+
const char *old_workers = getenv("CBM_WORKERS");
8246+
char *saved_workers = old_workers ? strdup(old_workers) : NULL;
8247+
const char *old_single = getenv("CBM_INDEX_SINGLE_THREAD");
8248+
char *saved_single = old_single ? strdup(old_single) : NULL;
8249+
if ((old_workers && !saved_workers) || (old_single && !saved_single)) {
8250+
free(saved_workers);
8251+
free(saved_single);
8252+
teardown_usages_repo();
8253+
FAIL("failed to save pipeline environment");
8254+
}
8255+
int pin_workers_rc = parallel ? cbm_setenv("CBM_WORKERS", "4", 1) : 0;
8256+
int pin_single_rc = parallel ? cbm_unsetenv("CBM_INDEX_SINGLE_THREAD") : 0;
82338257

82348258
char db_path[512];
82358259
snprintf(db_path, sizeof(db_path), "%s/test_1277.db", g_usages_tmpdir);
8236-
cbm_pipeline_t *p = cbm_pipeline_new(g_usages_tmpdir, db_path, CBM_MODE_FULL);
8237-
ASSERT_NOT_NULL(p);
8238-
ASSERT_EQ(cbm_pipeline_run(p), 0);
8239-
cbm_store_t *s = cbm_store_open_path(db_path);
8240-
ASSERT_NOT_NULL(s);
8241-
const char *project = cbm_pipeline_project_name(p);
8242-
8243-
int via_alias = count_lsp_calls(s, project, "loop.run", "contracts.Contract.process_batch");
8244-
int via_typed =
8245-
count_lsp_calls(s, project, "loop.run_typed", "contracts.Contract.process_batch");
8246-
int via_class =
8247-
count_lsp_calls(s, project, "loop.run_classlevel", "contracts.Contract.process_batch");
8248-
int via_untyped = count_lsp_calls(s, project, "loop.run_untyped", "process_batch");
8260+
cbm_pipeline_t *p = pin_workers_rc == 0 && pin_single_rc == 0
8261+
? cbm_pipeline_new(g_usages_tmpdir, db_path, CBM_MODE_FULL)
8262+
: NULL;
8263+
int run_rc = p ? cbm_pipeline_run(p) : -1;
8264+
cbm_store_t *s = run_rc == 0 ? cbm_store_open_path(db_path) : NULL;
8265+
bool store_opened = s != NULL;
8266+
int via_alias = -1, via_typed = -1, via_class = -1, via_untyped = -1;
8267+
if (s) {
8268+
const char *project = cbm_pipeline_project_name(p);
8269+
via_alias = count_lsp_calls(s, project, "loop.run", "contracts.Contract.process_batch");
8270+
via_typed =
8271+
count_lsp_calls(s, project, "loop.run_typed", "contracts.Contract.process_batch");
8272+
via_class =
8273+
count_lsp_calls(s, project, "loop.run_classlevel", "contracts.Contract.process_batch");
8274+
via_untyped = count_lsp_calls(s, project, "loop.run_untyped", "process_batch");
8275+
cbm_store_close(s);
8276+
}
82498277

8250-
cbm_store_close(s);
82518278
cbm_pipeline_free(p);
8279+
int restore_workers_rc =
8280+
saved_workers ? cbm_setenv("CBM_WORKERS", saved_workers, 1) : cbm_unsetenv("CBM_WORKERS");
8281+
int restore_single_rc = saved_single ? cbm_setenv("CBM_INDEX_SINGLE_THREAD", saved_single, 1)
8282+
: cbm_unsetenv("CBM_INDEX_SINGLE_THREAD");
8283+
free(saved_workers);
8284+
free(saved_single);
82528285
teardown_usages_repo();
82538286

8287+
ASSERT_EQ(pin_workers_rc, 0);
8288+
ASSERT_EQ(pin_single_rc, 0);
8289+
ASSERT_EQ(restore_workers_rc, 0);
8290+
ASSERT_EQ(restore_single_rc, 0);
8291+
ASSERT_EQ(run_rc, 0);
8292+
ASSERT_TRUE(store_opened);
82548293
ASSERT_EQ(via_alias, 1); /* self.x = <annotated __init__ parameter> */
82558294
ASSERT_EQ(via_typed, 1); /* self.x: T = ... */
82568295
ASSERT_EQ(via_class, 1); /* class-level x: T */
82578296
ASSERT_EQ(via_untyped, 0); /* control: no annotation, no guess */
82588297
PASS();
82598298
}
82608299

8300+
TEST(python_crossfile_typed_field_calls_issue1277) {
8301+
return check_python_crossfile_typed_field_calls_issue1277(false);
8302+
}
8303+
8304+
TEST(python_crossfile_typed_field_calls_issue1277_parallel) {
8305+
return check_python_crossfile_typed_field_calls_issue1277(true);
8306+
}
8307+
82618308
TEST(usages_creates_edges) {
82628309
/* Port of TestPassUsagesCreatesEdges.
82638310
* Go source with callback reference → USAGE edge. */
@@ -16471,6 +16518,7 @@ SUITE(pipeline) {
1647116518
RUN_TEST(implements_no_match);
1647216519
/* Usages pass (full pipeline integration) */
1647316520
RUN_TEST(python_crossfile_typed_field_calls_issue1277);
16521+
RUN_TEST(python_crossfile_typed_field_calls_issue1277_parallel);
1647416522
RUN_TEST(usages_creates_edges);
1647516523
RUN_TEST(usages_no_duplicate_calls);
1647616524
RUN_TEST(calls_edge_carries_call_site_line);

0 commit comments

Comments
 (0)