Skip to content

Commit e9058e1

Browse files
committed
fix(registry): reach a cfg-fenced function from its bare name
cbm_registry_add takes the symbol's `name`, discards it, and derives the bare-name lookup key from the QN's last dot segment. That makes the QN's tail load-bearing for the by-name index every language shares, and for one grammar the tail is not a name anybody writes. rust_cfg_qualified_name mints a `#[cfg(test)]` twin as "proj.lib.add#cfg(test)"; simple_name() has no '#' handling, so that function is filed under the whole string "add#cfg(test)", which no bare `add` callee can reach, leaving a test-only twin of a resolved function absent from the by-name index entirely. The caller always knows the name, because it is the argument. So the index takes a second key from it, only where the derived key carries a fence: const char *derived = simple_name(qualified_name); index_under_name(r, derived, owned_qn); if (name && name[0] && strchr(derived, '#') && strcmp(name, derived) != 0) { index_under_name(r, name, owned_qn); } The gate keys on the fence rather than on the file's language, and that is the part worth reviewing. It makes "unchanged for every other grammar" checkable: the derived key is written unconditionally exactly as before, and every by-name lookup keys through simple_name(), which splits on '.' and "::" only. A passed name that differs from the derived key by anything other than a fence is therefore a key no lookup can reach, so adding it would only widen the bucket the scorer walks. A grammar that mints no '#' sees no new key at all. Two of the rows a language gate would have had to cover turn out not to reach this function. Registry membership is decided by cbm_label_is_registry_symbol at all three production callers (pass_definitions.c, pass_parallel.c, pipeline_incremental.c), and it admits neither "Module" nor "Section". So a file's Module node never registers, and neither does a Markdown heading. What does register, under label "Class", is the dotted-name direction: grammar def->name QN tail HCL resource.aws_instance.web web TOML tool.poetry.dependencies dependencies INI tool.isort isort These are why the derived key stays unconditional. `instance = aws_instance.web.id` reaches the block through the tail `web`, so re-keying the index on the passed name would drop that edge outright. The fence gate leaves all three byte-identical. `name` is NULL or empty only for callers that have no symbol name to give; those have the derived key and nothing else. tests/test_registry.c: registry_indexes_by_passed_name_not_qn_tail resolves a bare `add` against a cfg-fenced QN and is the reproducer, failing without this commit. registry_indexes_a_dotted_name_under_its_tail_too pins the unconditional derived key for the HCL and Module shapes. tests/test_pipeline.c: pipeline_hcl_block_reference_resolves_to_its_block indexes a two-resource Terraform file end to end and asserts the surviving USAGE edge. The latter two pass without this commit and are guards: what would break them is re-keying the by-name index, and an HCL block name is dotted where its QN tail is not, so they pin the shape that regression would take. A throwaway probe walked tests/grammar_cases.h (one fixture per grammar, all 162) comparing def->name with simple_name(def->qualified_name), then targeted fixtures for the dotted shapes above. HCL is the only fixture-level mismatch among registry-eligible labels; TOML and INI mismatch on targeted fixtures. XML namespaced elements (`ns:root`) do not mismatch, because ':' is not a QN separator. Signed-off-by: Chad <4307099+BobbieBarker@users.noreply.github.com>
1 parent 5df8b04 commit e9058e1

3 files changed

Lines changed: 168 additions & 25 deletions

File tree

‎src/pipeline/registry.c‎

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -858,9 +858,38 @@ void cbm_registry_free(cbm_registry_t *r) {
858858

859859
/* ── Registration ────────────────────────────────────────────────── */
860860

861+
/* Record `owned_qn` in the by-name bucket for `key`, keeping the bucket's
862+
* is_test flags in step with its entries.
863+
*
864+
* No array dedup needed: cbm_registry_add's exact-map check guarantees the QN
865+
* is new, and it calls this at most once per distinct key. */
866+
static void index_under_name(cbm_registry_t *r, const char *key, const char *owned_qn) {
867+
qn_array_t *arr = cbm_ht_get(r->by_name, key);
868+
if (!arr) {
869+
arr = calloc(CBM_ALLOC_ONE, sizeof(qn_array_t));
870+
cbm_ht_set(r->by_name, strdup(key), arr);
871+
}
872+
int before = arr->count;
873+
cbm_da_push(arr, (char *)owned_qn);
874+
if (arr->count == before) {
875+
return; /* the name could not be recorded: no verdict to cache */
876+
}
877+
if (arr->count > arr->is_test_cap) {
878+
int want = arr->cap > 0 ? arr->cap : arr->count;
879+
uint8_t *grown =
880+
cbm_realloc(CBM_MEM_CLASS_DYN_ARRAY, arr->is_test, (size_t)want * sizeof(uint8_t));
881+
if (grown) {
882+
arr->is_test = grown;
883+
arr->is_test_cap = want;
884+
}
885+
}
886+
if (arr->count <= arr->is_test_cap) {
887+
arr->is_test[arr->count - SKIP_ONE] = is_test_qn(owned_qn) ? 1 : 0;
888+
}
889+
}
890+
861891
void cbm_registry_add(cbm_registry_t *r, const char *name, const char *qualified_name,
862892
const char *label) {
863-
(void)name;
864893
if (!r || !qualified_name || !label) {
865894
return;
866895
}
@@ -893,30 +922,37 @@ void cbm_registry_add(cbm_registry_t *r, const char *name, const char *qualified
893922
cbm_ht_set(r->exact, strdup(qualified_name), (void *)interned);
894923
const char *owned_qn = cbm_ht_get_key(r->exact, qualified_name);
895924

896-
/* Index by simple name.
897-
* No array dedup needed: exact-map check above guarantees uniqueness. */
898-
const char *simple = simple_name(qualified_name);
899-
qn_array_t *arr = cbm_ht_get(r->by_name, simple);
900-
if (!arr) {
901-
arr = calloc(CBM_ALLOC_ONE, sizeof(qn_array_t));
902-
cbm_ht_set(r->by_name, strdup(simple), arr);
903-
}
904-
int before = arr->count;
905-
cbm_da_push(arr, (char *)owned_qn);
906-
if (arr->count == before) {
907-
return; /* the name could not be recorded: no verdict to cache */
908-
}
909-
if (arr->count > arr->is_test_cap) {
910-
int want = arr->cap > 0 ? arr->cap : arr->count;
911-
uint8_t *grown =
912-
cbm_realloc(CBM_MEM_CLASS_DYN_ARRAY, arr->is_test, (size_t)want * sizeof(uint8_t));
913-
if (grown) {
914-
arr->is_test = grown;
915-
arr->is_test_cap = want;
916-
}
917-
}
918-
if (arr->count <= arr->is_test_cap) {
919-
arr->is_test[arr->count - SKIP_ONE] = is_test_qn(owned_qn) ? 1 : 0;
925+
/* Index the symbol under the QN's last dot segment, and additionally under
926+
* the name its caller passed when that segment carries a '#' fence.
927+
*
928+
* The derived key is the one every language has always used and it stays
929+
* unconditional, so a grammar whose QNs carry no '#' is byte-identical to
930+
* before this commit. The second key exists for one shape: a fenced tail is
931+
* a literal string no bare callee is ever written as. rust_cfg_qualified_name
932+
* mints a `#[cfg(test)]` twin as "proj.lib.add#cfg(test)", simple_name() has
933+
* no '#' handling, so the derived key filed that function under the whole
934+
* string "add#cfg(test)" where no bare `add` callee could reach it.
935+
*
936+
* Gating on the fence rather than on the file's language is what makes
937+
* "unchanged for every other grammar" checkable instead of asserted. Every
938+
* by-name lookup keys through simple_name(), which splits on '.' and "::"
939+
* only, so a passed name that differs from the derived key by anything
940+
* OTHER than a fence is a key no lookup can reach: an HCL block's
941+
* "resource.aws_instance.web" and a TOML table's "tool.poetry.dependencies"
942+
* are both already indexed under the tail a reference spells, and adding
943+
* their dotted form would only widen the bucket the scorer walks.
944+
*
945+
* `name` is NULL or empty only for callers that have no symbol name to
946+
* give; those have the derived key and nothing else. */
947+
const char *derived = simple_name(qualified_name);
948+
index_under_name(r, derived, owned_qn);
949+
/* '#' is a QN fence, and extract_defs.c's rust_cfg_qualified_name is the
950+
* only thing in the tree that mints one today. A grammar that starts
951+
* minting a '#' opts into this second key by doing so, whatever it means by
952+
* the fence: its symbols become reachable under the passed name as well,
953+
* and they share that name's bucket with everything else filed under it. */
954+
if (name && name[0] && strchr(derived, '#') && strcmp(name, derived) != 0) {
955+
index_under_name(r, name, owned_qn);
920956
}
921957
}
922958

‎tests/test_pipeline.c‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,60 @@ TEST(pipeline_nix_scoped_binding_calls_resolve) {
11471147
PASS();
11481148
}
11491149

1150+
/* Terraform reference resolution, end to end.
1151+
*
1152+
* An HCL block names itself with its labels appended -- find_hcl_block_name
1153+
* mints "resource.aws_instance.web" -- while its QN's last dot segment is
1154+
* bare "web", and a reference written `aws_instance.web.id` reaches the
1155+
* by-name index through that tail. HCL is therefore a language where the
1156+
* definition name and the QN tail are different strings, and an index keyed on
1157+
* either one alone drops every cross-resource reference in the file.
1158+
*
1159+
* This has to be a pipeline test: the registry is shared by every language
1160+
* and nothing in the HCL extractor mentions the index key, so the two halves
1161+
* can disagree with every extraction-level assertion still green.
1162+
*/
1163+
TEST(pipeline_hcl_block_reference_resolves_to_its_block) {
1164+
if (setup_test_repo() != 0) {
1165+
FAIL("failed to create temp dir");
1166+
}
1167+
1168+
char tf_path[512];
1169+
snprintf(tf_path, sizeof(tf_path), "%s/main.tf", g_tmpdir);
1170+
FILE *tf = fopen(tf_path, "w");
1171+
if (!tf) {
1172+
teardown_test_repo();
1173+
FAIL("failed to write terraform fixture");
1174+
}
1175+
fprintf(tf, "resource \"aws_instance\" \"web\" {\n"
1176+
" ami = \"ami-0c55b159cbfafe1f0\"\n"
1177+
" instance_type = \"t2.micro\"\n"
1178+
"}\n"
1179+
"\n"
1180+
"resource \"aws_eip\" \"ip\" {\n"
1181+
" instance = aws_instance.web.id\n"
1182+
"}\n");
1183+
fclose(tf);
1184+
1185+
char tf_db[512];
1186+
snprintf(tf_db, sizeof(tf_db), "%s/test_hcl_refs.db", g_tmpdir);
1187+
1188+
cbm_pipeline_t *tp = cbm_pipeline_new(g_tmpdir, tf_db, CBM_MODE_FULL);
1189+
ASSERT_NOT_NULL(tp);
1190+
ASSERT_EQ(cbm_pipeline_run(tp), 0);
1191+
1192+
cbm_store_t *ts = cbm_store_open_path(tf_db);
1193+
ASSERT_NOT_NULL(ts);
1194+
const char *tf_project = cbm_pipeline_project_name(tp);
1195+
1196+
ASSERT(cross_file_edge_exists(ts, tf_project, "main", "resource.aws_instance.web", "USAGE"));
1197+
1198+
cbm_store_close(ts);
1199+
cbm_pipeline_free(tp);
1200+
teardown_test_repo();
1201+
PASS();
1202+
}
1203+
11501204
/* Regression: incremental re-index of an edited file must NOT drop inbound
11511205
* cross-file CALLS edges whose source lives in an UNCHANGED file.
11521206
*
@@ -15166,6 +15220,7 @@ SUITE(pipeline) {
1516615220
/* Calls pass */
1516715221
RUN_TEST(pipeline_calls_resolution);
1516815222
RUN_TEST(pipeline_nix_scoped_binding_calls_resolve);
15223+
RUN_TEST(pipeline_hcl_block_reference_resolves_to_its_block);
1516915224
RUN_TEST(pipeline_incremental_preserves_cross_file_calls);
1517015225
RUN_TEST(pipeline_objectscript_export_preserves_calls_sequential_parallel);
1517115226
RUN_TEST(pipeline_objectscript_export_incremental_matches_full_relationships);

‎tests/test_registry.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,56 @@ TEST(resolve_qualified_ambiguous_tail_falls_through) {
325325
PASS();
326326
}
327327

328+
/* cbm_registry_add used to discard its `name` argument and re-derive the
329+
* lookup key from the QN's last dot segment. That made the QN's tail load
330+
* bearing for the bare-name index every language shares: a Rust cfg twin,
331+
* minted as "add#cfg(test)", was indexed under that literal string and no bare
332+
* `add` callee could ever reach it. */
333+
TEST(registry_indexes_by_passed_name_not_qn_tail) {
334+
cbm_registry_t *r = cbm_registry_new();
335+
cbm_registry_add(r, "add", "proj.lib.add#cfg(test)", "Function");
336+
337+
cbm_resolution_t res = cbm_registry_resolve(r, "add", "proj.lib.caller", NULL, NULL, 0);
338+
ASSERT_STR_EQ(res.qualified_name, "proj.lib.add#cfg(test)");
339+
340+
cbm_registry_free(r);
341+
PASS();
342+
}
343+
344+
/* The derived key stays unconditional, and this pins that.
345+
*
346+
* A name may carry segments the QN's tail drops, and then the tail is the key
347+
* callers actually spell. An HCL block is named "resource.aws_instance.web" by
348+
* find_hcl_block_name while its QN tail is bare "web", which is how an
349+
* `aws_instance.web.id` reference reaches it. Re-keying the index on the passed
350+
* name would lose that lookup outright, so the fence gate above leaves these
351+
* shapes exactly as they were.
352+
*
353+
* The Module row registers a shape production cannot produce: every caller of
354+
* cbm_registry_add gates on cbm_label_is_registry_symbol, which does not admit
355+
* "Module". It is here because the registry's own contract is per-label-string,
356+
* not per-caller, and a future label change should not silently drop the stem
357+
* lookup a bare module reference needs. */
358+
TEST(registry_indexes_a_dotted_name_under_its_tail_too) {
359+
cbm_registry_t *r = cbm_registry_new();
360+
cbm_registry_add(r, "resource.aws_instance.web", "proj.main.resource.aws_instance.web",
361+
"Class");
362+
cbm_registry_add(r, "helper.py", "proj.pkg.helper", "Module");
363+
364+
cbm_resolution_t tail = cbm_registry_resolve(r, "web", "proj.main", NULL, NULL, 0);
365+
ASSERT_STR_EQ(tail.qualified_name, "proj.main.resource.aws_instance.web");
366+
367+
cbm_resolution_t whole =
368+
cbm_registry_resolve(r, "resource.aws_instance.web", "proj.main", NULL, NULL, 0);
369+
ASSERT_STR_EQ(whole.qualified_name, "proj.main.resource.aws_instance.web");
370+
371+
cbm_resolution_t stem = cbm_registry_resolve(r, "helper", "proj.pkg.caller", NULL, NULL, 0);
372+
ASSERT_STR_EQ(stem.qualified_name, "proj.pkg.helper");
373+
374+
cbm_registry_free(r);
375+
PASS();
376+
}
377+
328378
TEST(resolve_import_map) {
329379
cbm_registry_t *r = cbm_registry_new();
330380
cbm_registry_add(r, "Process", "proj.pkg.worker.Process", "Function");
@@ -1201,6 +1251,8 @@ SUITE(registry) {
12011251
RUN_TEST(resolve_same_module);
12021252
RUN_TEST(resolve_qualified_disambiguates_same_name);
12031253
RUN_TEST(resolve_qualified_ambiguous_tail_falls_through);
1254+
RUN_TEST(registry_indexes_by_passed_name_not_qn_tail);
1255+
RUN_TEST(registry_indexes_a_dotted_name_under_its_tail_too);
12041256
RUN_TEST(resolve_import_map);
12051257
RUN_TEST(resolve_import_map_bare_function);
12061258
RUN_TEST(resolve_import_map_bare_alias);

0 commit comments

Comments
 (0)