Commit 4fab9fcd

4fab9fcd5e0dMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Box a scalar begin/rescue tail value into a poly result slot

A begin/rescue used as an expression whose value type widened to poly assigned a scalar branch value (e.g. a rescue returning an Integer) to the poly result temp without boxing. A new g_result_poly flag tracks the result slot's poly-ness so the tail emitter boxes scalar values.

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

Details

Commit
4fab9fcd5e0d
Parent
e65e9a6d5ffb

src/codegen.cmodifiedView file

@@ -71,6 +71,9 @@
7171 /* When set, tail positions assign to this var instead of `return`ing
7272 (used to give a begin/rescue a value). */
7373 static const char *g_result_var = NULL;
74+/* When g_result_var is set, whether that result slot is poly (so a scalar
75+ tail value must be boxed into it). */
76+static int g_result_poly = 0;
7477 /* Return type of the method currently being emitted, so a tail/return value
7578 can be boxed when the method returns poly but the value is concrete. */
7679 static TyKind g_ret_type = TY_UNKNOWN;
@@ -4362,7 +4365,9 @@
43624365 char rv[32]; snprintf(rv, sizeof rv, "_t%d", t);
43634366 emit_indent(b, indent); emit_ctype(c, rt, b);
43644367 buf_printf(b, " _t%d = %s;\n", t, rt == TY_RANGE ? "(sp_Range){0}" : default_value(rt));
4368+ int sp = g_result_poly; g_result_poly = (rt == TY_POLY);
43654369 emit_begin(c, id, b, indent, rv);
4370+ g_result_poly = sp;
43664371 emit_indent(b, indent); emit_tail_lead(b); buf_printf(b, "_t%d;\n", t);
43674372 return;
43684373 }
@@ -4382,7 +4387,8 @@
43824387 /* a value expression: return it (or assign to the begin/rescue result) */
43834388 emit_indent(b, indent);
43844389 emit_tail_lead(b);
4385- if (!g_result_var && g_ret_type == TY_POLY && comp_ntype(c, id) != TY_POLY) emit_boxed(c, id, b);
4390+ int want_poly = g_result_var ? g_result_poly : (g_ret_type == TY_POLY);
4391+ if (want_poly && comp_ntype(c, id) != TY_POLY) emit_boxed(c, id, b);
43864392 else emit_expr(c, id, b);
43874393 buf_puts(b, ";\n");
43884394 }