Commit 5faff878

5faff8781125Merged

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

Description

codegen: coerce for-range endpoints and counter at the poly boundary under promote

Under --int-overflow=promote (full int->poly widen, this branch) a for i in lo..hi loop can have its counter and/or endpoints widened to poly, making the emitted for (lv_i = lo; lv_i <= hi; lv_i++) invalid C (assign/compare/increment on an sp_RbVal). Coerce each endpoint independently with sp_poly_to_i when its static type is poly/bigint, and when the counter slot is poly drive the loop with a fresh mrb_int temp, re-boxing the counter local each iteration so the body sees a poly value.

Correct (for i in 1..10 and a poly-endpoint variant both yield 55), but it does not raise the promote pass-count on its own: each failing promote test layers several poly<->int boundaries, so closing one does not turn any test green. Preserved as one piece of the full int->poly widen big-bang.

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

Details

Commit
5faff8781125
Parent
76e18f573934

src/codegen_stmt.cmodifiedView file

@@ -1704,13 +1704,47 @@
17041704 TyKind ct = comp_ntype(c, coll);
17051705
17061706 if (ct == TY_RANGE && nt_type(nt, coll) && !strcmp(nt_type(nt, coll), "RangeNode")) {
1707- /* for v in lo..hi -- a plain counted loop */
1707+ /* for v in lo..hi -- a plain counted loop. Under --int-overflow=promote the
1708+ counter and/or endpoints may be widened to poly, so coerce each endpoint
1709+ with sp_poly_to_i when its static type is poly/bigint, and when the
1710+ counter slot is poly drive the loop with a fresh mrb_int temp and re-box
1711+ the counter local each iteration so the body sees a poly value. */
17081712 int excl = (int)(nt_int(nt, coll, "flags", 0) & 4) ? 1 : 0;
1713+ int lref = nt_ref(nt, coll, "left");
1714+ int rref = nt_ref(nt, coll, "right");
1715+ TyKind lty = comp_ntype(c, lref);
1716+ TyKind rty = comp_ntype(c, rref);
1717+ int lpoly = (lty == TY_POLY || lty == TY_BIGINT);
1718+ int rpoly = (rty == TY_POLY || rty == TY_BIGINT);
1719+ LocalVar *clv = scope_local(comp_scope_of(c, idx), vn);
1720+ int cpoly = clv && clv->type == TY_POLY;
17091721 int thi = ++g_tmp;
17101722 emit_indent(b, indent); buf_puts(b, "{ mrb_int ");
1711- buf_printf(b, "_t%d = ", thi); emit_expr(c, nt_ref(nt, coll, "right"), b); buf_puts(b, ";\n");
1723+ buf_printf(b, "_t%d = ", thi);
1724+ if (rpoly) buf_puts(b, "sp_poly_to_i(");
1725+ emit_expr(c, rref, b);
1726+ if (rpoly) buf_puts(b, ")");
1727+ buf_puts(b, ";\n");
1728+ if (cpoly) {
1729+ int tc = ++g_tmp;
1730+ emit_indent(b, indent + 1);
1731+ buf_printf(b, "for (mrb_int _t%d = ", tc);
1732+ if (lpoly) buf_puts(b, "sp_poly_to_i(");
1733+ emit_expr(c, lref, b);
1734+ if (lpoly) buf_puts(b, ")");
1735+ buf_printf(b, "; _t%d %s _t%d; _t%d++) {\n", tc, excl ? "<" : "<=", thi, tc);
1736+ emit_indent(b, indent + 2);
1737+ buf_printf(b, "lv_%s = sp_box_int(_t%d);\n", vn, tc);
1738+ emit_loop_body(c, body, b, indent + 2);
1739+ emit_indent(b, indent + 1); buf_puts(b, "}\n");
1740+ emit_indent(b, indent); buf_puts(b, "}\n");
1741+ return;
1742+ }
17121743 emit_indent(b, indent + 1);
1713- buf_printf(b, "for (lv_%s = ", vn); emit_expr(c, nt_ref(nt, coll, "left"), b);
1744+ buf_printf(b, "for (lv_%s = ", vn);
1745+ if (lpoly) buf_puts(b, "sp_poly_to_i(");
1746+ emit_expr(c, lref, b);
1747+ if (lpoly) buf_puts(b, ")");
17141748 buf_printf(b, "; lv_%s %s _t%d; lv_%s++) {\n", vn, excl ? "<" : "<=", thi, vn);
17151749 emit_loop_body(c, body, b, indent + 2);
17161750 emit_indent(b, indent + 1); buf_puts(b, "}\n");