Commit 9bdc3cc2

9bdc3cc2cdcdMerged

Sam Ruby <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(rbs_extract): resolve unqualified types via enclosing scope

Thread an enclosing-scope arg through map_type / map_class_instance so that an unqualified nominal type reference inside a module/class resolves to the parent scope. Example: def record: () -> Base inside module ActiveRecord; class RecordInvalid now emits obj_ActiveRecord_Base instead of the bare obj_Base, which matched no class in the analyzer's class table.

Heuristic — single-level parent fallback rather than a full lexical walk; no symbol table, and a wrong guess (e.g. StandardError inside ActiveRecord) is a no-op because the analyzer silently drops seeds for types it doesn't recognize. Covers the common sibling-in-module pattern that dominates real RBS in practice.

Probe: framework_tests_spinel inflector_test (roundhouse) drops from 6 → 3 C-compile errors with --rbs runtime; together with a roundhouse-side runtime/spinel/broadcasts.rbs sidecar, all 6 close and the binary runs.

Details

Commit
9bdc3cc2cdcd
Parent
8e1166f0fdb0

spinel_rbs_extract.cmodifiedView file

@@ -165,12 +165,31 @@
165165 }
166166
167167 /* Forward decl. Write a spinel type tag into out; return true on
168- * success, false to signal "out of subset, caller should skip". */
169-static bool map_type(rbs_parser_t *p, rbs_node_t *node, sbuf_t *out);
168+ * success, false to signal "out of subset, caller should skip".
169+ *
170+ * `enclosing_scope` (may be NULL/"") is the qualified name of the
171+ * declaration we're currently inside, e.g. `ActiveRecord` while
172+ * traversing members of `module ActiveRecord; class RecordInvalid`.
173+ * Used to resolve unqualified nominal type references: `Base` inside
174+ * `ActiveRecord` becomes `obj_ActiveRecord_Base`. Without this, the
175+ * extractor emits `obj_Base` and the seed misses any class actually
176+ * stored at the qualified name in spinel's tables. */
177+static bool map_type(rbs_parser_t *p, rbs_node_t *node,
178+ const char *enclosing_scope, sbuf_t *out);
170179
180+/* True if the type_name was written unqualified in source (no `::`
181+ * separators, no leading `::`). RBS exposes this as an empty
182+ * namespace path. */
183+static bool type_name_is_unqualified(rbs_type_name_t *tn) {
184+ if (tn == NULL || tn->rbs_namespace == NULL) return true;
185+ if (tn->rbs_namespace->path == NULL) return true;
186+ return tn->rbs_namespace->path->length == 0;
187+}
188+
171189 /* Map a ClassInstance node. Handles Array[T], Hash[K,V], primitives,
172190 * and nominal classes. */
173-static bool map_class_instance(rbs_parser_t *p, rbs_types_class_instance_t *ci, sbuf_t *out) {
191+static bool map_class_instance(rbs_parser_t *p, rbs_types_class_instance_t *ci,
192+ const char *enclosing_scope, sbuf_t *out) {
174193 sbuf_t name;
175194 sbuf_init(&name);
176195 name_of_type_name(p, ci->name, &name);
@@ -206,7 +225,7 @@
206225 if (name.len >= 5 && strcmp(name.buf + name.len - 5, "Array") == 0 && argc == 1) {
207226 sbuf_t elem;
208227 sbuf_init(&elem);
209- if (!map_type(p, ci->args->head->node, &elem)) {
228+ if (!map_type(p, ci->args->head->node, enclosing_scope, &elem)) {
210229 sbuf_free(&elem);
211230 sbuf_free(&name);
212231 return false;
@@ -232,8 +251,8 @@
232251 if (name.len >= 4 && strcmp(name.buf + name.len - 4, "Hash") == 0 && argc == 2) {
233252 sbuf_t k, v;
234253 sbuf_init(&k); sbuf_init(&v);
235- if (!map_type(p, ci->args->head->node, &k)
236- || !map_type(p, ci->args->head->next->node, &v)) {
254+ if (!map_type(p, ci->args->head->node, enclosing_scope, &k)
255+ || !map_type(p, ci->args->head->next->node, enclosing_scope, &v)) {
237256 sbuf_free(&k); sbuf_free(&v); sbuf_free(&name); return false;
238257 }
239258 const char *tag = hash_tag_for_kv(k.buf, v.buf);
@@ -250,14 +269,28 @@
250269 return false;
251270 }
252271
253- /* Nominal class instance: emit obj_<QualifiedName>. */
272+ /* Nominal class instance: emit obj_<QualifiedName>. If the source
273+ * wrote the name unqualified AND we're inside a class/module scope,
274+ * resolve relative to that scope so e.g. `Base` inside
275+ * `module ActiveRecord; class RecordInvalid` becomes
276+ * `obj_ActiveRecord_Base`. Heuristic: spinel will silently drop
277+ * seeds for types it doesn't recognize, so a wrong guess (e.g.
278+ * `StandardError` inside `ActiveRecord`) is a no-op rather than a
279+ * miscompile. Full lexical-scope walk would require a symbol
280+ * table; this single-level prefix covers the common case. */
254281 sbuf_set(out, "obj_", 4);
282+ if (enclosing_scope != NULL && enclosing_scope[0] != '\0'
283+ && type_name_is_unqualified(ci->name)) {
284+ sbuf_append_cstr(out, enclosing_scope);
285+ sbuf_append_cstr(out, "_");
286+ }
255287 sbuf_append(out, name.buf, name.len);
256288 sbuf_free(&name);
257289 return true;
258290 }
259291
260-static bool map_type(rbs_parser_t *p, rbs_node_t *node, sbuf_t *out) {
292+static bool map_type(rbs_parser_t *p, rbs_node_t *node,
293+ const char *enclosing_scope, sbuf_t *out) {
261294 if (node == NULL) return false;
262295 switch (node->type) {
263296 case RBS_TYPES_BASES_BOOL:
@@ -272,12 +305,12 @@
272305 sbuf_set(out, "nil", 3);
273306 return true;
274307 case RBS_TYPES_CLASS_INSTANCE:
275- return map_class_instance(p, (rbs_types_class_instance_t *) node, out);
308+ return map_class_instance(p, (rbs_types_class_instance_t *) node, enclosing_scope, out);
276309 case RBS_TYPES_OPTIONAL: {
277310 rbs_types_optional_t *opt = (rbs_types_optional_t *) node;
278311 sbuf_t inner;
279312 sbuf_init(&inner);
280- if (!map_type(p, opt->type, &inner)) { sbuf_free(&inner); return false; }
313+ if (!map_type(p, opt->type, enclosing_scope, &inner)) { sbuf_free(&inner); return false; }
281314 /* Avoid double-?: nil? → just nil; obj_Foo?? → obj_Foo? */
282315 if (inner.len > 0 && inner.buf[inner.len - 1] == '?') {
283316 sbuf_set(out, inner.buf, inner.len);
@@ -303,7 +336,7 @@
303336 if (t == NULL) return false;
304337 sbuf_t inner;
305338 sbuf_init(&inner);
306- if (!map_type(p, t, &inner)) { sbuf_free(&inner); return false; }
339+ if (!map_type(p, t, enclosing_scope, &inner)) { sbuf_free(&inner); return false; }
307340 if (inner.len > 0 && inner.buf[inner.len - 1] == '?') {
308341 sbuf_set(out, inner.buf, inner.len);
309342 } else {
@@ -329,7 +362,8 @@
329362 * class table (@cls_cmeth_*). SINGLETON_INSTANCE (def self?.foo)
330363 * emits both so seed_class_method on the analyzer side hits whichever
331364 * the source actually defined. */
332-static void emit_method(rbs_parser_t *p, rbs_ast_members_method_definition_t *m, FILE *out) {
365+static void emit_method(rbs_parser_t *p, rbs_ast_members_method_definition_t *m,
366+ const char *enclosing_scope, FILE *out) {
333367 if (m->overloads == NULL || m->overloads->head == NULL) return;
334368 /* Only the first overload; multiple overloads are out of subset
335369 * (spinel can't pick between them deterministically). */
@@ -374,7 +408,7 @@
374408 rbs_types_function_param_t *pp = (rbs_types_function_param_t *) cur->node;
375409 sbuf_t t;
376410 sbuf_init(&t);
377- if (!map_type(p, pp->type, &t)) { sbuf_free(&t); sbuf_free(&ptypes); return; }
411+ if (!map_type(p, pp->type, enclosing_scope, &t)) { sbuf_free(&t); sbuf_free(&ptypes); return; }
378412 if (!first_p) sbuf_append_cstr(&ptypes, ",");
379413 sbuf_append(&ptypes, t.buf, t.len);
380414 first_p = false;
@@ -386,7 +420,7 @@
386420
387421 sbuf_t ret;
388422 sbuf_init(&ret);
389- if (!map_type(p, fn->return_type, &ret)) { sbuf_free(&ret); sbuf_free(&ptypes); return; }
423+ if (!map_type(p, fn->return_type, enclosing_scope, &ret)) { sbuf_free(&ret); sbuf_free(&ptypes); return; }
390424
391425 sbuf_t mname;
392426 sbuf_init(&mname);
@@ -407,10 +441,11 @@
407441
408442 /* ---- emit attr_accessor / reader / writer ------------------ */
409443
410-static void emit_attr(rbs_parser_t *p, rbs_ast_symbol_t *name, rbs_node_t *type, FILE *out) {
444+static void emit_attr(rbs_parser_t *p, rbs_ast_symbol_t *name, rbs_node_t *type,
445+ const char *enclosing_scope, FILE *out) {
411446 sbuf_t t;
412447 sbuf_init(&t);
413- if (!map_type(p, type, &t)) { sbuf_free(&t); return; }
448+ if (!map_type(p, type, enclosing_scope, &t)) { sbuf_free(&t); return; }
414449 sbuf_t n;
415450 sbuf_init(&n);
416451 name_of_symbol(p, name, &n);
@@ -421,8 +456,20 @@
421456
422457 /* ---- traverse class/module body ---------------------------- */
423458
459+/* `qualified_scope` is this declaration's full path (used as the
460+ * `class X` line). `lookup_scope` is the *parent* path (used to
461+ * resolve unqualified type references inside the members). For
462+ * `module ActiveRecord; class RecordInvalid; def record: () -> Base`:
463+ * qualified_scope = "ActiveRecord_RecordInvalid",
464+ * lookup_scope = "ActiveRecord".
465+ * That makes `Base` resolve to `obj_ActiveRecord_Base` — the sibling-
466+ * in-module pattern, which dominates real RBS in practice. Ruby's
467+ * actual constant lookup walks every enclosing scope outward; this
468+ * single-level fallback covers the common case without a symbol
469+ * table. */
424470 static void traverse_members(rbs_parser_t *p, rbs_node_list_t *members,
425- const char *qualified_scope, FILE *out) {
471+ const char *qualified_scope,
472+ const char *lookup_scope, FILE *out) {
426473 if (members == NULL || members->length == 0) return;
427474 fprintf(out, "class %s\n", qualified_scope);
428475 rbs_node_list_node_t *cur = members->head;
@@ -430,21 +477,21 @@
430477 rbs_node_t *n = cur->node;
431478 switch (n->type) {
432479 case RBS_AST_MEMBERS_METHOD_DEFINITION:
433- emit_method(p, (rbs_ast_members_method_definition_t *) n, out);
480+ emit_method(p, (rbs_ast_members_method_definition_t *) n, lookup_scope, out);
434481 break;
435482 case RBS_AST_MEMBERS_ATTR_ACCESSOR: {
436483 rbs_ast_members_attr_accessor_t *a = (rbs_ast_members_attr_accessor_t *) n;
437- emit_attr(p, a->name, a->type, out);
484+ emit_attr(p, a->name, a->type, lookup_scope, out);
438485 break;
439486 }
440487 case RBS_AST_MEMBERS_ATTR_READER: {
441488 rbs_ast_members_attr_reader_t *a = (rbs_ast_members_attr_reader_t *) n;
442- emit_attr(p, a->name, a->type, out);
489+ emit_attr(p, a->name, a->type, lookup_scope, out);
443490 break;
444491 }
445492 case RBS_AST_MEMBERS_ATTR_WRITER: {
446493 rbs_ast_members_attr_writer_t *a = (rbs_ast_members_attr_writer_t *) n;
447- emit_attr(p, a->name, a->type, out);
494+ emit_attr(p, a->name, a->type, lookup_scope, out);
448495 break;
449496 }
450497 default:
@@ -491,7 +538,7 @@
491538 }
492539 sbuf_append(&qualified, leaf.buf, leaf.len);
493540
494- traverse_members(p, members, qualified.buf, out);
541+ traverse_members(p, members, qualified.buf, parent_scope, out);
495542
496543 /* Recurse into nested declarations. */
497544 if (members != NULL) {