Commit 97b5c120

97b5c1205b18Merged

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

Description

codegen: drive (a..b).each fusion with an int temp when the loop var is poly

The range .each fusion runs the block param directly as the C loop counter (for (lv_i = _t.first; lv_i <= ...; lv_i++)). Under --int-overflow=promote the param is widened to poly, so the int loop ops no longer typecheck. Mirror emit_for's poly-counter arm: drive a fresh mrb_int temp and re-box lv_i each iteration so the body sees a poly value.

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

Details

Commit
97b5c1205b18
Parent
4f107247e636

src/codegen_call.cmodifiedView file

@@ -10752,6 +10752,21 @@
1075210752 emit_indent(b, indent);
1075310753 buf_printf(b, "sp_Range _t%d = ", t); buf_puts(b, rb.p ? rb.p : ""); buf_puts(b, ";\n");
1075410754 free(rb.p);
10755+ /* Under --int-overflow=promote the loop var is widened to poly; drive the
10756+ loop with a fresh mrb_int temp and re-box the counter each iteration
10757+ (mirrors emit_for's poly-counter arm). */
10758+ LocalVar *clv = p0_orig ? scope_local(comp_scope_of(c, block), p0_orig) : NULL;
10759+ if (clv && clv->type == TY_POLY) {
10760+ int tc = ++g_tmp;
10761+ emit_indent(b, indent);
10762+ buf_printf(b, "for (mrb_int _t%d = _t%d.first; _t%d <= _t%d.last - _t%d.excl; _t%d++) {\n",
10763+ tc, t, tc, t, t, tc);
10764+ emit_indent(b, indent + 1);
10765+ buf_printf(b, "lv_%s = sp_box_int(_t%d);\n", p0, tc);
10766+ emit_loop_body(c, body, b, indent + 1);
10767+ emit_indent(b, indent); buf_puts(b, "}\n");
10768+ return 1;
10769+ }
1075510770 emit_indent(b, indent);
1075610771 buf_printf(b, "for (lv_%s = _t%d.first; lv_%s <= _t%d.last - _t%d.excl; lv_%s++) {\n",
1075710772 p0, t, p0, t, t, p0);