Commit a3c9b7fa

a3c9b7fabdb8Merged

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

Description

codegen: box a define_method subst literal at a poly-return tail

A define_method("m_#{n}") { n } synthetic method whose return widened to poly returned the captured literal unboxed (return 1LL into an sp_RbVal slot): the tail box-decision read comp_ntype of the loop-var read (poly), not the substituted literal (int), so it skipped the box. Size the tail decision by the subst literal's type and box the literal node. Fixes compile_time_define_method_int.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
a3c9b7fabdb8
Parent
74647f7ce815

src/codegen_stmt.cmodifiedView file

@@ -4050,13 +4050,20 @@
40504050 /* a value expression: return it (or assign to the begin/rescue result) */
40514051 emit_indent(b, indent);
40524052 emit_tail_lead(b);
4053+ /* A define_method subst read emits the captured literal, not the (poly)
4054+ loop-var slot it nominally reads; size the box decision by the literal's
4055+ type and box the literal node so a poly return slot typechecks. */
4056+ int is_subst = g_dm_subst_name && g_dm_subst_node >= 0 &&
4057+ !strcmp(ty, "LocalVariableReadNode") &&
4058+ nt_str(nt, id, "name") && !strcmp(nt_str(nt, id, "name"), g_dm_subst_name);
4059+ TyKind vty = is_subst ? comp_ntype(c, g_dm_subst_node) : comp_ntype(c, id);
40534060 int want_poly = g_result_var ? g_result_poly : (g_ret_type == TY_POLY);
4054- if (want_poly && comp_ntype(c, id) != TY_POLY) emit_boxed(c, id, b);
4061+ if (want_poly && vty != TY_POLY) emit_boxed(c, is_subst ? g_dm_subst_node : id, b);
40554062 /* a poly tail value feeding a narrower (non-poly) return slot -- a scalar
40564063 method(:sym) target, or an RBS-typed String/object method whose body yields
40574064 poly -- needs coercing. (Only for a real return slot, not a begin/rescue
40584065 result var, which stays poly.) */
4059- else if (!g_result_var && tail_needs_unbox(comp_ntype(c, id), g_ret_type)) emit_unbox_node(c, g_ret_type, id, b);
4066+ else if (!g_result_var && tail_needs_unbox(vty, g_ret_type)) emit_unbox_node(c, g_ret_type, id, b);
40604067 else emit_tail_value(c, id, b);
40614068 buf_puts(b, ";\n");
40624069 }