@@ -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+
1580715921TEST(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);
0 commit comments