Commit 82375f69

82375f69dca3Merged

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

Description

codegen: box int rvalues into widened slots across multiple-assignment targets

Multiple-assignment binds each target with a direct C assignment from an int temp or an IntArray element. Under --int-overflow=promote the local / class-var slot is widened to poly, so the int rvalue no longer typechecks. Box the rvalue with emit_boxed/emit_boxed_text when the destination slot is poly, across the masgn target paths: scalar-RHS locals (and their typed-zero rest defaults), tuple-element locals and class vars, the typed-array destructure get, and the trailing rights get. The slot's widened type is read from the scope (the target node's own type cache still reports the pre-widen scalar type). Unblocks the common masgn promote cases (global_multi_assign, numeric_coerce, masgn_classvar_target, masgn_constpath_target, multi_write_scalar_rhs, multi_write_call_rhs_as_expr); leading-splat temp binding and the rightward pattern form remain.

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

Details

Commit
82375f69dca3
Parent
97b5c1205b18

src/codegen_stmt.cmodifiedView file

@@ -3215,8 +3215,20 @@
32153215 const char *lty = nt_type(nt, lefts[i]);
32163216 if (!lty || strcmp(lty, "LocalVariableTargetNode")) continue;
32173217 emit_indent(b, indent);
3218- buf_printf(b, "lv_%s = ", nt_str(nt, lefts[i], "name"));
3219- if (i == 0) emit_expr(c, value, b);
3218+ const char *lvn = nt_str(nt, lefts[i], "name");
3219+ buf_printf(b, "lv_%s = ", lvn);
3220+ LocalVar *llv = lvn ? scope_local(comp_scope_of(c, id), lvn) : NULL;
3221+ int lpoly = llv && llv->type == TY_POLY;
3222+ if (i == 0) { if (lpoly && st != TY_POLY) emit_boxed(c, value, b); else emit_expr(c, value, b); }
3223+ else if (lpoly) {
3224+ /* rest target's typed-zero default, boxed into the widened slot.
3225+ comp_ntype on the target node still reports its pre-widen scalar
3226+ type, so default_value lands the same 0 default mode emits. */
3227+ TyKind tt = comp_ntype(c, lefts[i]);
3228+ Buf bx; memset(&bx, 0, sizeof bx);
3229+ emit_boxed_text(c, tt, default_value(tt), &bx);
3230+ buf_puts(b, bx.p ? bx.p : "sp_box_nil()"); free(bx.p);
3231+ }
32203232 else buf_puts(b, default_value(comp_ntype(c, lefts[i])));
32213233 buf_puts(b, ";\n");
32223234 }
@@ -3239,8 +3251,18 @@
32393251 if (!lty) continue;
32403252 if (!strcmp(lty, "LocalVariableTargetNode")) {
32413253 emit_indent(b, indent);
3242- buf_printf(b, "lv_%s = sp_%sArray_get(_t%d, %dLL);\n",
3243- nt_str(nt, lefts[i], "name"), k, tarr, i);
3254+ const char *lvn = nt_str(nt, lefts[i], "name");
3255+ buf_printf(b, "lv_%s = ", lvn);
3256+ LocalVar *llv = lvn ? scope_local(rt_scope, lvn) : NULL;
3257+ TyKind ltt = llv ? llv->type : comp_ntype(c, lefts[i]);
3258+ char gx[64]; snprintf(gx, sizeof gx, "sp_%sArray_get(_t%d, %dLL)", k, tarr, i);
3259+ if (ltt == TY_POLY && strcmp(k, "Poly")) {
3260+ Buf bx; memset(&bx, 0, sizeof bx);
3261+ emit_boxed_text(c, elem, gx, &bx);
3262+ buf_puts(b, bx.p ? bx.p : "sp_box_nil()"); free(bx.p);
3263+ }
3264+ else buf_puts(b, gx);
3265+ buf_puts(b, ";\n");
32443266 }
32453267 else if (!strcmp(lty, "InstanceVariableTargetNode") &&
32463268 rt_scope && rt_scope->class_id >= 0) {
@@ -3323,8 +3345,17 @@
33233345 if (!lty) continue;
33243346 if (!strcmp(lty, "LocalVariableTargetNode")) {
33253347 emit_indent(b, indent);
3326- buf_printf(b, "lv_%s = sp_%sArray_get(_t%d, _t%d->len - %dLL + %dLL);\n",
3327- nt_str(nt, rights[j], "name"), k, tarr, tarr, rn, j);
3348+ const char *rlvn = nt_str(nt, rights[j], "name");
3349+ buf_printf(b, "lv_%s = ", rlvn);
3350+ LocalVar *rllv = rlvn ? scope_local(rt_scope, rlvn) : NULL;
3351+ char rgx[80]; snprintf(rgx, sizeof rgx, "sp_%sArray_get(_t%d, _t%d->len - %dLL + %dLL)", k, tarr, tarr, rn, j);
3352+ if (rllv && rllv->type == TY_POLY && strcmp(k, "Poly")) {
3353+ Buf bx; memset(&bx, 0, sizeof bx);
3354+ emit_boxed_text(c, elem, rgx, &bx);
3355+ buf_puts(b, bx.p ? bx.p : "sp_box_nil()"); free(bx.p);
3356+ }
3357+ else buf_puts(b, rgx);
3358+ buf_puts(b, ";\n");
33283359 }
33293360 else if (!strcmp(lty, "InstanceVariableTargetNode") &&
33303361 rt_scope && rt_scope->class_id >= 0) {
@@ -3419,7 +3450,19 @@
34193450 }
34203451 if (lty && !strcmp(lty, "LocalVariableTargetNode")) {
34213452 emit_indent(b, indent);
3422- buf_printf(b, "lv_%s = _t%d;\n", nt_str(nt, lefts[i], "name"), tmps[i]);
3453+ const char *lvn = nt_str(nt, lefts[i], "name");
3454+ buf_printf(b, "lv_%s = ", lvn);
3455+ LocalVar *llv = lvn ? scope_local(comp_scope_of(c, id), lvn) : NULL;
3456+ TyKind ltt = llv ? llv->type : comp_ntype(c, lefts[i]);
3457+ TyKind valt = comp_ntype(c, els[i]);
3458+ if (ltt == TY_POLY && valt != TY_POLY) {
3459+ char expr[32]; snprintf(expr, sizeof expr, "_t%d", tmps[i]);
3460+ Buf bx; memset(&bx, 0, sizeof bx);
3461+ emit_boxed_text(c, valt, expr, &bx);
3462+ buf_puts(b, bx.p ? bx.p : "sp_box_nil()"); free(bx.p);
3463+ }
3464+ else buf_printf(b, "_t%d", tmps[i]);
3465+ buf_puts(b, ";\n");
34233466 }
34243467 else if (lty && (!strcmp(lty, "ConstantPathTargetNode") || !strcmp(lty, "ConstantTargetNode")) &&
34253468 nt_str(nt, lefts[i], "name") && comp_const(c, nt_str(nt, lefts[i], "name"))) {
@@ -3552,9 +3595,20 @@
35523595 Scope *cv_sc = comp_scope_of(c, id);
35533596 int cv_cid = (cv_sc && cv_sc->class_id >= 0) ? cv_sc->class_id : g_class_body_id;
35543597 if (cv_cid < 0) { unsupported(c, id, "multiple assignment class variable target no class"); continue; }
3555- if (comp_cvar_index(&c->classes[cv_cid], cnm) < 0) { unsupported(c, id, "multiple assignment class variable target unregistered"); continue; }
3598+ int cv_idx = comp_cvar_index(&c->classes[cv_cid], cnm);
3599+ if (cv_idx < 0) { unsupported(c, id, "multiple assignment class variable target unregistered"); continue; }
35563600 emit_indent(b, indent);
3557- buf_printf(b, "cvar_%s_%s = _t%d;\n", c->classes[cv_cid].name, cnm + 2, tmps[i]);
3601+ buf_printf(b, "cvar_%s_%s = ", c->classes[cv_cid].name, cnm + 2);
3602+ TyKind cvt = c->classes[cv_cid].cvar_types[cv_idx];
3603+ TyKind valt = comp_ntype(c, els[i]);
3604+ if (cvt == TY_POLY && valt != TY_POLY) {
3605+ char expr[32]; snprintf(expr, sizeof expr, "_t%d", tmps[i]);
3606+ Buf bx; memset(&bx, 0, sizeof bx);
3607+ emit_boxed_text(c, valt, expr, &bx);
3608+ buf_puts(b, bx.p ? bx.p : "sp_box_nil()"); free(bx.p);
3609+ }
3610+ else buf_printf(b, "_t%d", tmps[i]);
3611+ buf_puts(b, ";\n");
35583612 }
35593613 else unsupported(c, id, "multiple assignment target");
35603614 }