Commit 4f107247

4f107247e636Merged

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

Description

codegen: box instance_exec/instance_eval block-param args into widened slots

The lifted instance_exec/instance_eval block binds each block parameter with a direct lv_<param> = <rvalue>;, emitting the call-site arg via emit_expr. When the parameter slot is poly the int/float/str rvalue no longer matches the slot type and the C compile fails ("assigning to sp_RbVal from mrb_int"). Box the rvalue with emit_boxed when the slot is poly, across all four binding paths: numbered params, requireds (direct args, auto-splat array elements, and trampoline args), and keyword params. Under --int-overflow=promote every int param is widened to poly, so this unblocks the whole instance_exec family (+16 promote tests); default mode is unchanged for non-poly params and now also correct for genuinely heterogeneous ones.

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

Details

Commit
4f107247e636
Parent
3a22755772ce

src/codegen_call.cmodifiedView file

@@ -5267,11 +5267,13 @@
52675267 int maxn = (int)nt_int(nt, bp_node, "maximum", 0);
52685268 for (int p = 0; p < maxn; p++) {
52695269 char pn[16]; snprintf(pn, sizeof pn, "_%d", p + 1);
5270+ LocalVar *plv = scope_local(comp_scope_of(c, id), pn);
5271+ int ppoly = plv && plv->type == TY_POLY;
52705272 emit_indent(g_pre, g_indent);
52715273 buf_printf(g_pre, "lv_%s = ", rename_local(pn));
52725274 if (is_exec) {
5273- if (p < iac) emit_expr(c, iav[p], g_pre);
5274- else { LocalVar *plv = scope_local(comp_scope_of(c, id), pn); emit_ie_param_default(c, plv ? plv->type : TY_POLY, g_pre); }
5275+ if (p < iac) { if (ppoly) emit_boxed(c, iav[p], g_pre); else emit_expr(c, iav[p], g_pre); }
5276+ else emit_ie_param_default(c, plv ? plv->type : TY_POLY, g_pre);
52755277 }
52765278 else buf_printf(g_pre, "_t%d", tr);
52775279 buf_puts(g_pre, ";\n");
@@ -5310,18 +5312,31 @@
53105312 for (int p = 0; p < npar; p++) {
53115313 const char *pn = nt_str(nt, reqs[p], "name");
53125314 if (!pn) continue;
5315+ LocalVar *plv = scope_local(comp_scope_of(c, id), pn);
5316+ int ppoly = plv && plv->type == TY_POLY; /* widened slot needs a boxed rvalue */
53135317 emit_indent(g_pre, g_indent);
53145318 buf_printf(g_pre, "lv_%s = ", rename_local(pn));
5315- if (as_kind) buf_printf(g_pre, "sp_%sArray_get(_t%d, %d)", as_kind, as_arr, p);
5319+ if (as_kind) {
5320+ /* element of the auto-splat array; box the scalar kinds into the
5321+ poly slot (PolyArray_get already yields an sp_RbVal). */
5322+ const char *bx = !ppoly || !strcmp(as_kind, "Poly") ? NULL
5323+ : !strcmp(as_kind, "Int") ? "sp_box_int"
5324+ : !strcmp(as_kind, "Float") ? "sp_box_float"
5325+ : !strcmp(as_kind, "Str") ? "sp_box_str" : NULL;
5326+ if (bx) buf_printf(g_pre, "%s(", bx);
5327+ buf_printf(g_pre, "sp_%sArray_get(_t%d, %d)", as_kind, as_arr, p);
5328+ if (bx) buf_puts(g_pre, ")");
5329+ }
53165330 else if (tramp_argc >= 0) {
53175331 int an = ie_tramp_effective_arg(c, id, p);
53185332 Buf eb; memset(&eb, 0, sizeof eb);
5319- if (an >= 0) emit_expr(c, an, &eb); else buf_puts(&eb, "0");
5333+ if (an >= 0) { if (ppoly) emit_boxed(c, an, &eb); else emit_expr(c, an, &eb); }
5334+ else buf_puts(&eb, "0");
53205335 buf_puts(g_pre, eb.p ? eb.p : "0"); free(eb.p);
53215336 }
53225337 else if (is_exec) {
5323- if (p < iac) emit_expr(c, iav[p], g_pre);
5324- else { LocalVar *plv = scope_local(comp_scope_of(c, id), pn); emit_ie_param_default(c, plv ? plv->type : TY_POLY, g_pre); }
5338+ if (p < iac) { if (ppoly) emit_boxed(c, iav[p], g_pre); else emit_expr(c, iav[p], g_pre); }
5339+ else emit_ie_param_default(c, plv ? plv->type : TY_POLY, g_pre);
53255340 }
53265341 else buf_printf(g_pre, "_t%d", tr); /* instance_eval yields self */
53275342 buf_puts(g_pre, ";\n");
@@ -5334,8 +5349,11 @@
53345349 if (!kpn) continue;
53355350 int vn = ie_kwhash_value(c, ie_kwhash, kpn);
53365351 if (vn < 0) vn = nt_ref(nt, kws[k], "value"); /* omitted optional -> default */
5352+ LocalVar *kplv = scope_local(comp_scope_of(c, id), kpn);
5353+ int kppoly = kplv && kplv->type == TY_POLY;
53375354 Buf vb; memset(&vb, 0, sizeof vb);
5338- if (vn >= 0) emit_expr(c, vn, &vb); else buf_puts(&vb, "0");
5355+ if (vn >= 0) { if (kppoly) emit_boxed(c, vn, &vb); else emit_expr(c, vn, &vb); }
5356+ else buf_puts(&vb, "0");
53395357 emit_indent(g_pre, g_indent);
53405358 buf_printf(g_pre, "lv_%s = %s;\n", rename_local(kpn), vb.p ? vb.p : "0");
53415359 free(vb.p);