Commit 9dd34a49

9dd34a49fe0aMerged

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

Description

rbs_extract: map heterogeneous unions to poly instead of dropping the seed

map_type's RBS_TYPES_UNION arm only accepted the T | nil shape (→ T?); every other union returned false. A false return type makes emit_method bail, so the entire method seed was dropped — not just the return slot. With no seed, the analyzer infers freely and unify_return_type collapses e.g. {int, string} to a single concrete C type, mis-compiling heterogeneous returns (the def [](field) case in matz/spinel#1255: incompatible integer to pointer conversion).

Map any non-T | nil union to poly (sp_RbVal, the tagged union) — the same target untyped already uses. This is the faithful choice: spinel's only representation for "one of several incompatible types" is the tagged union, so boxing is correct, and the seed now survives extraction instead of vanishing. T | nil still maps to T? (precision preserved); T | nil where T is itself out-of-subset falls through to poly.

Scoped to the RBS-seed layer only. This is distinct from the inference-level unify_return_type fix matz scoped in #1255 as "larger, separate work" (load-bearing for self-hosting): the self-host build ships no .rbs, so the extractor never runs on the compiler's own source and stage-2 is unaffected. This handles the case where an author explicitly annotates a heterogeneous union — exactly the -> untyped-equivalent the issue recommends as the workaround, now working for the precise union form too.

Verified against the issue's own repro: def []: (Symbol) -> (Integer | String) now compiles and runs (boxes), and the extractor emits meth [] poly symbol. (Integer | nil) still emits int?.

Details

Commit
9dd34a49fe0a
Parent
fce6225a1cf0

spinel_rbs_extract.cmodifiedView file

@@ -431,26 +431,46 @@
431431 return true;
432432 }
433433 case RBS_TYPES_UNION: {
434- /* Only support `T | nil` shape (equivalent to T?). Any
435- * other union → out-of-subset, skip. */
434+ /* `T | nil` (the nilable shape) maps to `T?`. Every other
435+ * union has no single concrete C representation, so it
436+ * maps to poly (sp_RbVal, the tagged union) -- the same
437+ * target `untyped` uses. This boxes the value correctly.
438+ *
439+ * Previously any non-`T | nil` union returned false, which
440+ * dropped the *entire* method seed (the caller bails on a
441+ * false return type), letting inference collapse e.g.
442+ * {int, string} to a single C type and mis-compile
443+ * heterogeneous returns (matz/spinel#1255). Mapping to poly
444+ * is the faithful choice: spinel's only representation for
445+ * "one of several incompatible types" is the tagged union.
446+ *
447+ * This touches only the RBS-seed layer. The self-host build
448+ * ships no `.rbs`, so the inference-level unify_return_type
449+ * heuristic (load-bearing for stage-2) is unaffected. */
436450 rbs_types_union_t *u = (rbs_types_union_t *) node;
437- if (u->types == NULL || u->types->length != 2) return false;
438- rbs_node_t *a = u->types->head->node;
439- rbs_node_t *b = u->types->head->next->node;
440- rbs_node_t *t = NULL;
441- if (a->type == RBS_TYPES_BASES_NIL) t = b;
442- else if (b->type == RBS_TYPES_BASES_NIL) t = a;
443- if (t == NULL) return false;
444- sbuf_t inner;
445- sbuf_init(&inner);
446- if (!map_type(p, t, enclosing_scope, &inner)) { sbuf_free(&inner); return false; }
447- if (inner.len > 0 && inner.buf[inner.len - 1] == '?') {
448- sbuf_set(out, inner.buf, inner.len);
449- } else {
450- sbuf_set(out, inner.buf, inner.len);
451- sbuf_append_cstr(out, "?");
451+ if (u->types != NULL && u->types->length == 2) {
452+ rbs_node_t *a = u->types->head->node;
453+ rbs_node_t *b = u->types->head->next->node;
454+ rbs_node_t *t = NULL;
455+ if (a->type == RBS_TYPES_BASES_NIL) t = b;
456+ else if (b->type == RBS_TYPES_BASES_NIL) t = a;
457+ if (t != NULL) {
458+ sbuf_t inner;
459+ sbuf_init(&inner);
460+ if (map_type(p, t, enclosing_scope, &inner)) {
461+ sbuf_set(out, inner.buf, inner.len);
462+ if (inner.len == 0 || inner.buf[inner.len - 1] != '?') {
463+ sbuf_append_cstr(out, "?");
464+ }
465+ sbuf_free(&inner);
466+ return true;
467+ }
468+ /* `T | nil` where T is itself out-of-subset →
469+ * fall through to poly below. */
470+ sbuf_free(&inner);
471+ }
452472 }
453- sbuf_free(&inner);
473+ sbuf_set(out, "poly", 4);
454474 return true;
455475 }
456476 default: