Commit 102e2bb5
102e2bb52557Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
codegen: run upto/downto block body when the block declares no parameter
The integer upto/downto block loop used the block's first parameter name
directly as the C loop counter, so it only matched when the block declared
a parameter. A no-parameter block (1.upto(5) { h += 1 }) fell through and
its body was dropped entirely (the counter printed 0 instead of 5).
Drive the loop with a fresh temp, like n.times does, and rebind the block
parameter from it each iteration only when one is declared. The rebind also
makes mutating the parameter inside the block not perturb the loop, matching
CRuby (1.upto(3) { |n| n += 100; ... }).
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 102e2bb52557
- Parent
- 38918e9605c0
src/codegen_call.cmodifiedView file
return 1; } /* n.upto(m) / n.downto(m) { |i| ... } */ if ((!strcmp(name, "upto") || !strcmp(name, "downto")) && rt == TY_INT && p0) { /* n.upto(m) / n.downto(m) { [|i|] ... } -- a fresh temp drives the loop and the block param (if any) is rebound from it each iteration, like n.times. A blockless-param form (`1.upto(5) { body }`) must still run the body. */ if ((!strcmp(name, "upto") || !strcmp(name, "downto")) && rt == TY_INT) { int up = !strcmp(name, "upto"); int args = nt_ref(nt, id, "arguments"); int argc = 0; if (argc != 1) return 0; Buf lo; memset(&lo, 0, sizeof lo); emit_expr(c, recv, &lo); Buf hi; memset(&hi, 0, sizeof hi); emit_expr(c, argv[0], &hi); int ti = ++g_tmp; emit_indent(b, indent); buf_printf(b, "for (lv_%s = ", p0); buf_puts(b, lo.p); buf_printf(b, "; lv_%s %s ", p0, up ? "<=" : ">="); buf_puts(b, hi.p); buf_printf(b, "; lv_%s%s) {\n", p0, up ? "++" : "--"); buf_printf(b, "for (mrb_int _t%d = ", ti); buf_puts(b, lo.p); buf_printf(b, "; _t%d %s ", ti, up ? "<=" : ">="); buf_puts(b, hi.p); buf_printf(b, "; _t%d%s) {\n", ti, up ? "++" : "--"); if (p0) { char ts[32]; snprintf(ts, sizeof ts, "_t%d", ti); emit_iter_param_assign(c, block, p0_orig, p0, TY_INT, ts, b, indent + 1); } emit_loop_body(c, body, b, indent + 1); emit_indent(b, indent); buf_puts(b, "}\n"); free(lo.p); free(hi.p);test/upto_downto_block.rbaddedView file
Loading diff…
test/upto_downto_block.rb.expectedaddedView file
Loading diff…