Skip to content

Commit 32ae20d

Browse files
committed
fix(lsp): preserve short-name index fallback
Signed-off-by: astandrik <astandrik@yandex-team.ru>
1 parent 793716d commit 32ae20d

2 files changed

Lines changed: 64 additions & 7 deletions

File tree

‎internal/cbm/lsp/type_registry.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,12 @@ static void build_type_embed_index(CBMTypeRegistry *reg, CBMArena *idx_arena) {
171171
* Descending iteration plus prepend preserves ascending registry scan order.
172172
* Consumers opt in after the QN index captures the finalized tail boundary. */
173173
void cbm_registry_build_type_short_index(CBMTypeRegistry *reg) {
174-
if (!reg || !reg->arena || !reg->type_qn_buckets || reg->type_qn_bucket_count <= 0)
174+
if (!reg)
175+
return;
176+
reg->type_short_buckets = NULL;
177+
reg->type_short_entries = NULL;
178+
reg->type_short_bucket_count = 0;
179+
if (!reg->arena || !reg->type_qn_buckets || reg->type_qn_bucket_count <= 0)
175180
return;
176181
CBMArena *idx_arena = reg->arena;
177182
int count = 0;

‎tests/test_c_lsp.c‎

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15329,15 +15329,38 @@ TEST(registry_short_name_indexes) {
1532915329
cbm_registry_build_type_short_index(&reg);
1533015330

1533115331
/* Qualified-name bare segment "Trait": types 0, 4, then the bare-QN type
15332-
* 5. C-LSP applies its stricter dotted-QN predicate on top of this index. */
15332+
* 5. The iterator is a hash prefilter, so apply the exact predicate before
15333+
* asserting its order just like production consumers do. */
1533315334
CBMTypeNameIter nit;
1533415335
cbm_registry_types_by_short_name(&reg, "Trait", &nit);
15335-
ASSERT_EQ(cbm_type_name_iter_next(&nit), 0);
15336-
ASSERT_EQ(cbm_type_name_iter_next(&nit), 4);
15337-
ASSERT_EQ(cbm_type_name_iter_next(&nit), 5);
15338-
ASSERT_EQ(cbm_type_name_iter_next(&nit), -1);
15336+
{
15337+
int expected[] = {0, 4, 5};
15338+
int exact_count = 0;
15339+
int candidate;
15340+
while ((candidate = cbm_type_name_iter_next(&nit)) >= 0) {
15341+
const char *qn = reg.types[candidate].qualified_name;
15342+
const char *last_dot = qn ? strrchr(qn, '.') : NULL;
15343+
const char *candidate_short = last_dot ? last_dot + 1 : qn;
15344+
if (!candidate_short || strcmp(candidate_short, "Trait") != 0)
15345+
continue;
15346+
ASSERT_TRUE(exact_count < 3);
15347+
ASSERT_EQ(candidate, expected[exact_count++]);
15348+
}
15349+
ASSERT_EQ(exact_count, 3);
15350+
}
1533915351
cbm_registry_types_by_short_name(&reg, "Missing", &nit);
15340-
ASSERT_EQ(cbm_type_name_iter_next(&nit), -1);
15352+
{
15353+
int exact_count = 0;
15354+
int candidate;
15355+
while ((candidate = cbm_type_name_iter_next(&nit)) >= 0) {
15356+
const char *qn = reg.types[candidate].qualified_name;
15357+
const char *last_dot = qn ? strrchr(qn, '.') : NULL;
15358+
const char *candidate_short = last_dot ? last_dot + 1 : qn;
15359+
if (candidate_short && strcmp(candidate_short, "Missing") == 0)
15360+
exact_count++;
15361+
}
15362+
ASSERT_EQ(exact_count, 0);
15363+
}
1534115364

1534215365
for (int name_i = 0; name_i < 23; name_i++) {
1534315366
char short_name[32];
@@ -15438,6 +15461,35 @@ TEST(registry_short_name_indexes) {
1543815461
ASSERT_EQ(fallback_count, 4);
1543915462
reg.type_short_buckets = saved_type_short_buckets;
1544015463

15464+
/* A failed rebuild must discard the previous auxiliary index. Re-finalize
15465+
* after adding the tail type so the QN boundary advances, then force arena
15466+
* exhaustion for the optional rebuild. The iterator must fall back to the
15467+
* complete linear scan instead of retaining the stale pre-tail index. */
15468+
cbm_registry_finalize(&reg);
15469+
int saved_nblocks = arena.nblocks;
15470+
size_t saved_used = arena.used;
15471+
arena.nblocks = CBM_ARENA_MAX_BLOCKS;
15472+
arena.used = arena.block_size;
15473+
cbm_registry_build_type_short_index(&reg);
15474+
arena.nblocks = saved_nblocks;
15475+
arena.used = saved_used;
15476+
ASSERT_NULL(reg.type_short_buckets);
15477+
ASSERT_NULL(reg.type_short_entries);
15478+
ASSERT_EQ(reg.type_short_bucket_count, 0);
15479+
15480+
cbm_registry_types_by_short_name(&reg, "Trait", &nit);
15481+
fallback_count = 0;
15482+
while ((candidate = cbm_type_name_iter_next(&nit)) >= 0) {
15483+
const char *qn = reg.types[candidate].qualified_name;
15484+
const char *last_dot = qn ? strrchr(qn, '.') : NULL;
15485+
const char *candidate_short = last_dot ? last_dot + 1 : qn;
15486+
if (!candidate_short || strcmp(candidate_short, "Trait") != 0)
15487+
continue;
15488+
ASSERT_TRUE(fallback_count < 4);
15489+
ASSERT_EQ(candidate, fallback_expected[fallback_count++]);
15490+
}
15491+
ASSERT_EQ(fallback_count, 4);
15492+
1544115493
cbm_arena_destroy(&arena);
1544215494
PASS();
1544315495
}

0 commit comments

Comments
 (0)