Commit 0762c603

0762c603cbb2Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

codegen: coerce a nil-resolving call to the return slot's typed nil

A method whose return type is a concrete non-poly type (e.g. mrb_int from an RBS -> Integer) can have a tail/return expression that is a call resolving to nil through a nil or unresolved receiver -- e.g. AR.adapter.count(x) where adapter is nil. spinel types that call from the (non-nilable) RBS chain but resolves its value to nil at codegen, emitting the poly box sp_box_nil(). Returning that sp_RbVal raw from a non-poly C function is uncompilable ("returning sp_RbVal from a function with incompatible result type mrb_int"); it had been deferred to a gcc/ clang error on the generated C.

Detect the exact sp_box_nil() tail emission into a non-poly return slot and substitute the slot's typed nil instead (SP_INT_NIL / sp_float_nil() / NULL / the type's default). The test is on the emitted text, not the static type, because the static type is unreliable here (the call types UNKNOWN, not POLY, yet emits a box) -- and a comp_ntype-based widening wrongly coerces poly-dispatch tails that emit a plain scalar. A literal sp_box_nil() carries no side-effect prelude, so substituting it is safe; every other emission is passed through unchanged.

Fixes #1432

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
0762c603cbb2
Parent
884b6f664d8e

src/codegen_stmt.cmodifiedView file

@@ -1691,6 +1691,42 @@
16911691 emit_expr(c, node, b);
16921692 }
16931693
1694+/* A genuine poly body (TY_POLY) is unboxed into any narrower (non-poly) return
1695+ slot (#1417). */
1696+static int tail_needs_unbox(TyKind r0, TyKind ret) {
1697+ return r0 == TY_POLY && ret != TY_POLY;
1698+}
1699+
1700+/* The return slot's nil representation for a non-poly type. */
1701+static void emit_ret_nil(Compiler *c, TyKind t, Buf *b) {
1702+ if (t == TY_INT || t == TY_BOOL) buf_puts(b, "SP_INT_NIL");
1703+ else if (t == TY_FLOAT) buf_puts(b, "sp_float_nil()");
1704+ else if (t == TY_STRING || ty_is_object(t)) buf_puts(b, "NULL");
1705+ else {
1706+ const char *cn = c_type_name(t);
1707+ if (cn && cn[0] && cn[strlen(cn) - 1] == '*') buf_puts(b, "NULL");
1708+ else buf_puts(b, default_value(t));
1709+ }
1710+}
1711+
1712+/* Emit a tail/return value expression into a non-poly return slot. A call that
1713+ resolves to nil through a nil/unresolved receiver is typed `-> Integer` (etc.)
1714+ per RBS but emits the poly box `sp_box_nil()`; returning that raw from a
1715+ non-poly C function is uncompilable (#1432). Detect that exact emission and
1716+ substitute the slot's typed nil instead. The text test is precise: a literal
1717+ `sp_box_nil()` carries no side-effect prelude, so discarding it is safe, and
1718+ any other emission (e.g. a poly-dispatch `({...})`) is passed through
1719+ unchanged. */
1720+static void emit_tail_value(Compiler *c, int node, Buf *b) {
1721+ if (g_ret_type == TY_POLY) { emit_expr(c, node, b); return; }
1722+ Buf tmp; memset(&tmp, 0, sizeof tmp);
1723+ emit_expr(c, node, &tmp);
1724+ const char *txt = tmp.p ? tmp.p : "";
1725+ if (!strcmp(txt, "sp_box_nil()")) emit_ret_nil(c, g_ret_type, b);
1726+ else buf_puts(b, txt);
1727+ free(tmp.p);
1728+}
1729+
16941730 /* A literal whose evaluation has no side effects -- used to decide whether a
16951731 discarded `return <expr>` in a void function needs `(void)(<expr>)` to keep
16961732 the side effects or can collapse to a bare `return;`. Callers unwrap parens
@@ -1763,8 +1799,8 @@
17631799 /* a poly return value feeding a narrower (non-poly) return slot -- e.g. a
17641800 method(:sym) target pinned to mrb_int that returns a poly @ivar, or an
17651801 RBS-typed String/object method whose body yields poly -- needs coercing. */
1766- else if (r0 == TY_POLY && g_ret_type != TY_POLY) emit_unbox_node(c, g_ret_type, a[0], b);
1767- else emit_expr(c, a[0], b);
1802+ else if (tail_needs_unbox(r0, g_ret_type)) emit_unbox_node(c, g_ret_type, a[0], b);
1803+ else emit_tail_value(c, a[0], b);
17681804 buf_puts(b, ";\n");
17691805 }
17701806 else if (g_ret_type == TY_POLY) buf_puts(b, "return sp_box_nil();\n");
@@ -3656,8 +3692,8 @@
36563692 method(:sym) target, or an RBS-typed String/object method whose body yields
36573693 poly -- needs coercing. (Only for a real return slot, not a begin/rescue
36583694 result var, which stays poly.) */
3659- else if (!g_result_var && g_ret_type != TY_POLY && comp_ntype(c, id) == TY_POLY) emit_unbox_node(c, g_ret_type, id, b);
3660- else emit_expr(c, id, b);
3695+ else if (!g_result_var && tail_needs_unbox(comp_ntype(c, id), g_ret_type)) emit_unbox_node(c, g_ret_type, id, b);
3696+ else emit_tail_value(c, id, b);
36613697 buf_puts(b, ";\n");
36623698 }
36633699