Commit fc5b0c4d
fc5b0c4da28cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(rbs_extract): walk lexical chain for unqualified Const, honor leading ::
Two related bugs in spinel_rbs_extract's nominal-type resolution:
- Bare
Articleinsidemodule Views; module Articlesresolved unconditionally toobj_Views_Article-- the single-level prepend heuristic never walked further outward to top-level. When Article is defined at top-level (the canonical case), the seed referenced a class that didn't exist and the downstream C compile failed withunknown type name 'sp_Views_Article'. - Leading
::was silently ignored. RBS exposes the absolute form viarbs_namespace->absolute; the extractor wasn't reading the field.
Fix:
- Pre-pass
collect_decl_nameswalks every Class/Module declaration before emit and registers each qualified name (with_separators) in a global set. map_class_instance's nominal arm: when the name is unqualified, walks the enclosing scope from innermost outward, picks the first candidate that's in the known-names set. Falls back to bare when no candidate matches (matches Ruby's::Consttop-level resolution).- When the source wrote
::Const, the absolute flag is now honored: emit the bare name with no scope prepend.
Closes #656.
Default 633/0/0. Optcarrot wrap checksum 59662 unchanged.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- fc5b0c4da28c
- Parent
- 2ab383c94a2d
spinel_rbs_extract.cmodifiedView file
@@ -83,6 +83,53 @@
8383
sbuf_append(s, src, n);8484
}8585
86+
/* ---- known-name set (issue #656) ---------------------------- */87+
88+
/* Set of qualified class/module names defined across all .rbs files89+
* traversed. Populated by collect_decl_names in a pre-pass before90+
* the emit pass; consulted by resolve_unqualified_name so a bare91+
* `Article` written inside `module Views; module Articles` walks92+
* the lexical chain (Views_Articles_Article, Views_Article,93+
* Article) and picks the first match. Without the set we either94+
* always prepend the enclosing scope (wrong when the referent is95+
* top-level) or never prepend (wrong for the sibling-in-module96+
* case). Issue #656. */97+
static char **known_names = NULL;98+
static size_t known_names_len = 0;99+
static size_t known_names_cap = 0;100+
101+
static void known_names_add(const char *name, size_t n) {102+
/* dedupe (linear; n is small in practice) */103+
for (size_t i = 0; i < known_names_len; i++) {104+
if (strlen(known_names[i]) == n && memcmp(known_names[i], name, n) == 0) return;105+
}106+
if (known_names_len >= known_names_cap) {107+
size_t ncap = known_names_cap == 0 ? 32 : known_names_cap * 2;108+
char **nbuf = (char **) realloc(known_names, ncap * sizeof(char *));109+
if (nbuf == NULL) {110+
fprintf(stderr, "spinel_rbs_extract: out of memory\n");111+
exit(1);112+
}113+
known_names = nbuf;114+
known_names_cap = ncap;115+
}116+
char *copy = (char *) malloc(n + 1);117+
if (copy == NULL) {118+
fprintf(stderr, "spinel_rbs_extract: out of memory\n");119+
exit(1);120+
}121+
memcpy(copy, name, n);122+
copy[n] = '\0';123+
known_names[known_names_len++] = copy;124+
}125+
126+
static bool known_names_has(const char *name) {127+
for (size_t i = 0; i < known_names_len; i++) {128+
if (strcmp(known_names[i], name) == 0) return true;129+
}130+
return false;131+
}132+
86133
/* ---- name lookup via rbs constant pool --------------------- */87134
88135
/* Read the bytes for a constant_id out of the parser's pool into a sbuf.@@ -290,22 +337,55 @@
290337
return false;291338
}292339
293-
/* Nominal class instance: emit obj_<QualifiedName>. If the source294-
* wrote the name unqualified AND we're inside a class/module scope,295-
* resolve relative to that scope so e.g. `Base` inside296-
* `module ActiveRecord; class RecordInvalid` becomes297-
* `obj_ActiveRecord_Base`. Heuristic: spinel will silently drop298-
* seeds for types it doesn't recognize, so a wrong guess (e.g.299-
* `StandardError` inside `ActiveRecord`) is a no-op rather than a300-
* miscompile. Full lexical-scope walk would require a symbol301-
* table; this single-level prefix covers the common case. */340+
/* Nominal class instance: emit obj_<QualifiedName>. Three cases:341+
*342+
* 1. Source wrote `::Const` (absolute): use the bare name as-is,343+
* never prepend the enclosing scope (issue #656).344+
* 2. Source wrote unqualified `Const` and we're inside a class/345+
* module scope: walk the lexical chain from innermost outward346+
* and pick the first qualified name present in known_names.347+
* Fall back to bare when no candidate matches (matches Ruby's348+
* `::Const` resolution at top-level).349+
* 3. Source wrote qualified `A::B::C`: use as-is (name already350+
* contains the full path via name_of_type_name).351+
*/302352
sbuf_set(out, "obj_", 4);303-
if (enclosing_scope != NULL && enclosing_scope[0] != '\0'353+
bool absolute = (ci->name != NULL && ci->name->rbs_namespace != NULL354+
&& ci->name->rbs_namespace->absolute);355+
if (!absolute && enclosing_scope != NULL && enclosing_scope[0] != '\0'304356
&& type_name_is_unqualified(ci->name)) {305-
sbuf_append_cstr(out, enclosing_scope);306-
sbuf_append_cstr(out, "_");357+
/* Walk the lexical chain: try `<chain>_<name>` for each suffix358+
* of enclosing_scope (innermost outward), then top-level. */359+
sbuf_t candidate;360+
sbuf_init(&candidate);361+
const char *scope_end = enclosing_scope + strlen(enclosing_scope);362+
const char *cur = enclosing_scope;363+
bool resolved = false;364+
while (cur != NULL && cur < scope_end) {365+
candidate.len = 0;366+
sbuf_append(&candidate, cur, scope_end - cur);367+
sbuf_append_cstr(&candidate, "_");368+
sbuf_append(&candidate, name.buf, name.len);369+
if (known_names_has(candidate.buf)) {370+
sbuf_append(out, candidate.buf, candidate.len);371+
resolved = true;372+
break;373+
}374+
/* Step to the next inner namespace by skipping past the375+
* next "_". `Views_Articles_Helpers` -> `Articles_Helpers`376+
* -> `Helpers` -> done. */377+
const char *next = (const char *) memchr(cur, '_', scope_end - cur);378+
if (next == NULL) break;379+
cur = next + 1;380+
}381+
if (!resolved) {382+
/* Top-level fallback: emit bare name. */383+
sbuf_append(out, name.buf, name.len);384+
}385+
sbuf_free(&candidate);386+
} else {387+
sbuf_append(out, name.buf, name.len);307388
}308-
sbuf_append(out, name.buf, name.len);309389
sbuf_free(&name);310390
return true;311391
}@@ -581,6 +661,54 @@
581661
}582662
}583663
664+
/* Pre-pass: collect every Class/Module declaration's qualified name665+
* (with "_" separators) into known_names. Recurses through members so666+
* `module A; module B; class C` registers all of A, A_B, A_B_C. The667+
* later emit pass uses the set to disambiguate unqualified type668+
* references (issue #656). */669+
static void collect_decl_names(rbs_parser_t *p, rbs_node_t *node,670+
const char *parent_scope) {671+
sbuf_t leaf;672+
sbuf_init(&leaf);673+
rbs_node_list_t *members = NULL;674+
675+
if (node->type == RBS_AST_DECLARATIONS_CLASS) {676+
rbs_ast_declarations_class_t *c = (rbs_ast_declarations_class_t *) node;677+
name_of_type_name(p, c->name, &leaf);678+
members = c->members;679+
} else if (node->type == RBS_AST_DECLARATIONS_MODULE) {680+
rbs_ast_declarations_module_t *m = (rbs_ast_declarations_module_t *) node;681+
name_of_type_name(p, m->name, &leaf);682+
members = m->members;683+
} else {684+
sbuf_free(&leaf);685+
return;686+
}687+
688+
sbuf_t qualified;689+
sbuf_init(&qualified);690+
if (parent_scope != NULL && parent_scope[0] != '\0') {691+
sbuf_append_cstr(&qualified, parent_scope);692+
sbuf_append_cstr(&qualified, "_");693+
}694+
sbuf_append(&qualified, leaf.buf, leaf.len);695+
known_names_add(qualified.buf, qualified.len);696+
697+
if (members != NULL) {698+
rbs_node_list_node_t *cur = members->head;699+
while (cur != NULL) {700+
if (cur->node->type == RBS_AST_DECLARATIONS_CLASS701+
|| cur->node->type == RBS_AST_DECLARATIONS_MODULE) {702+
collect_decl_names(p, cur->node, qualified.buf);703+
}704+
cur = cur->next;705+
}706+
}707+
708+
sbuf_free(&qualified);709+
sbuf_free(&leaf);710+
}711+
584712
/* Recurse into a declaration. For Class/Module: extend the scope path585713
* with the declaration's leaf name, emit members, then recurse into586714
* any nested Class/Module members so e.g. `module Foo; class Bar; end;@@ -675,6 +803,14 @@
675803
free(src);676804
return;677805
}806+
/* Pre-pass: register every Class/Module's qualified name so the807+
* emit pass can disambiguate unqualified type references via808+
* lexical-chain walk (issue #656). */809+
rbs_node_list_node_t *pre = sig->declarations->head;810+
while (pre != NULL) {811+
collect_decl_names(p, pre->node, "");812+
pre = pre->next;813+
}678814
rbs_node_list_node_t *cur = sig->declarations->head;679815
while (cur != NULL) {680816
traverse_decl(p, cur->node, "", out);