@@ -15250,12 +15250,9 @@ TEST(clsp_tier2_shared_registry_readonly_c) {
1525015250 PASS();
1525115251}
1525215252
15253- /* Direct guard for the finalize-time short-name / embedded-type indexes and their
15254- * iterators (type_registry.c), which the Rust trait/free-func fast paths rely on.
15255- * Verifies: embed index yields exactly the types whose embedded_types carry a
15256- * matching BARE name, in ascending registry order, deduped when a type lists the
15257- * same bare twice; free-func index yields only free funcs (receiver_type==NULL) with
15258- * the given short_name, not methods. */
15253+ /* Direct guard for the type-name / embedded-type / free-function registry
15254+ * indexes and their iterators (type_registry.c). Verifies that every iterator
15255+ * preserves ascending registry order, which is part of resolver tie-breaking. */
1525915256TEST(registry_short_name_indexes) {
1526015257 CBMArena arena;
1526115258 cbm_arena_init(&arena);
@@ -15288,6 +15285,25 @@ TEST(registry_short_name_indexes) {
1528815285 t.short_name = "C";
1528915286 t.embedded_types = c_emb;
1529015287 cbm_registry_add_type(®, t);
15288+ memset(&t, 0, sizeof(t));
15289+ t.qualified_name = "other.Trait";
15290+ t.short_name = "Trait";
15291+ cbm_registry_add_type(®, t);
15292+ memset(&t, 0, sizeof(t));
15293+ t.qualified_name = "Trait";
15294+ t.short_name = "Trait";
15295+ cbm_registry_add_type(®, t);
15296+
15297+ /* Populate enough distinct names to force many hash-bucket collisions, with
15298+ * repeated short names spread across the registry. The differential check
15299+ * below compares the index against the exact former linear-scan order. */
15300+ for (int i = 0; i < 512; i++) {
15301+ memset(&t, 0, sizeof(t));
15302+ t.qualified_name = cbm_arena_sprintf(&arena, "bulk.mod%d.Name%d", i, i % 23);
15303+ ASSERT_NOT_NULL(t.qualified_name);
15304+ t.short_name = strrchr(t.qualified_name, '.') + 1;
15305+ cbm_registry_add_type(®, t);
15306+ }
1529115307
1529215308 /* free func "helper" (x2 — different QNs), method "M.helper", free func "other". */
1529315309 CBMRegisteredFunc f;
@@ -15310,6 +15326,54 @@ TEST(registry_short_name_indexes) {
1531015326 cbm_registry_add_func(®, f);
1531115327
1531215328 cbm_registry_finalize(®);
15329+ cbm_registry_build_type_short_index(®);
15330+
15331+ /* 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. */
15333+ CBMTypeNameIter nit;
15334+ cbm_registry_types_by_short_name(®, "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);
15339+ cbm_registry_types_by_short_name(®, "Missing", &nit);
15340+ ASSERT_EQ(cbm_type_name_iter_next(&nit), -1);
15341+
15342+ for (int name_i = 0; name_i < 23; name_i++) {
15343+ char short_name[32];
15344+ snprintf(short_name, sizeof(short_name), "Name%d", name_i);
15345+ cbm_registry_types_by_short_name(®, short_name, &nit);
15346+ int linear_i = 0;
15347+ for (;;) {
15348+ int expected = -1;
15349+ while (linear_i < reg.type_count) {
15350+ int candidate = linear_i++;
15351+ const char *qn = reg.types[candidate].qualified_name;
15352+ const char *last_dot = qn ? strrchr(qn, '.') : NULL;
15353+ const char *candidate_short = last_dot ? last_dot + 1 : qn;
15354+ if (candidate_short && strcmp(candidate_short, short_name) == 0) {
15355+ expected = candidate;
15356+ break;
15357+ }
15358+ }
15359+
15360+ int actual;
15361+ for (;;) {
15362+ actual = cbm_type_name_iter_next(&nit);
15363+ if (actual < 0)
15364+ break;
15365+ const char *qn = reg.types[actual].qualified_name;
15366+ const char *last_dot = qn ? strrchr(qn, '.') : NULL;
15367+ const char *candidate_short = last_dot ? last_dot + 1 : qn;
15368+ if (candidate_short && strcmp(candidate_short, short_name) == 0)
15369+ break;
15370+ }
15371+
15372+ ASSERT_EQ(actual, expected);
15373+ if (expected < 0)
15374+ break;
15375+ }
15376+ }
1531315377
1531415378 /* Embed index for bare "Trait": types 1 (A) then 2 (B), ascending, B once. */
1531515379 CBMTypeEmbedIter it;
@@ -15339,6 +15403,41 @@ TEST(registry_short_name_indexes) {
1533915403 ASSERT_EQ(cbm_free_func_iter_next(&fit), 3);
1534015404 ASSERT_EQ(cbm_free_func_iter_next(&fit), -1);
1534115405
15406+ /* A type appended after finalize is covered by the iterator tail. If the
15407+ * auxiliary allocation is unavailable, the iterator falls back to the same
15408+ * complete linear scan rather than silently dropping candidates. Keep this
15409+ * after the other iterator assertions because their tails intentionally
15410+ * expose post-finalize candidates for caller-side exact filtering too. */
15411+ int tail_type_i = reg.type_count;
15412+ memset(&t, 0, sizeof(t));
15413+ t.qualified_name = "tail.Trait";
15414+ t.short_name = "Trait";
15415+ cbm_registry_add_type(®, t);
15416+ cbm_registry_types_by_short_name(®, "Trait", &nit);
15417+ ASSERT_EQ(cbm_type_name_iter_next(&nit), 0);
15418+ ASSERT_EQ(cbm_type_name_iter_next(&nit), 4);
15419+ ASSERT_EQ(cbm_type_name_iter_next(&nit), 5);
15420+ ASSERT_EQ(cbm_type_name_iter_next(&nit), tail_type_i);
15421+ ASSERT_EQ(cbm_type_name_iter_next(&nit), -1);
15422+
15423+ int *saved_type_short_buckets = reg.type_short_buckets;
15424+ reg.type_short_buckets = NULL;
15425+ cbm_registry_types_by_short_name(®, "Trait", &nit);
15426+ int fallback_expected[] = {0, 4, 5, tail_type_i};
15427+ int fallback_count = 0;
15428+ int candidate;
15429+ while ((candidate = cbm_type_name_iter_next(&nit)) >= 0) {
15430+ const char *qn = reg.types[candidate].qualified_name;
15431+ const char *last_dot = qn ? strrchr(qn, '.') : NULL;
15432+ const char *candidate_short = last_dot ? last_dot + 1 : qn;
15433+ if (!candidate_short || strcmp(candidate_short, "Trait") != 0)
15434+ continue;
15435+ ASSERT_TRUE(fallback_count < 4);
15436+ ASSERT_EQ(candidate, fallback_expected[fallback_count++]);
15437+ }
15438+ ASSERT_EQ(fallback_count, 4);
15439+ reg.type_short_buckets = saved_type_short_buckets;
15440+
1534215441 cbm_arena_destroy(&arena);
1534315442 PASS();
1534415443}
0 commit comments