Commit d0dc173f
d0dc173f8f38Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): while predicate side-effects run each iteration
Closes #500. while (n = gets.to_i) > 0 looped forever:
const char * _t1 = sp_gets(); SP_GC_ROOT(_t1); while (((lv_n = ((mrb_int)atoll(_t1))) > 0LL)) { ... }
compile_expr_gc_rooted for the CallNode receiver emitted
_t = call(); SP_GC_ROOT(_t); at the current insertion point,
which during while-predicate compile sits BEFORE the eventual
while (cond) { line. Every iteration then re-read the same
captured first line of input. EOF never advances _t1, so
atoll keeps returning the same value and the loop never exits.
Pre-test while now compiles the predicate into a scratch
@out_lines buffer, captures any emitted hoist lines, and
replays them inside while (1) { ...hoists...; if (!cond) break; ...body... } so the receiver call (and its transient root)
runs per-iter. The deliberate length-loop optimization
(try_hoist_strlen) still hoists OUTSIDE the loop — it's
evaluated before the scratch-buffer swap, so its mrb_int _strlen = sp_str_length(...) stays at the outer indent.
The post-test do ... while (cond) form was already correct:
the predicate compiles AFTER the body, and its hoists land
inside the do-while block where they run each iteration.
Test fixture uses an instance-method-driven next_line iterator
returning String rather than stdin gets (which the harness can't
feed). Same code path: assignment-in-while-predicate with a
heap-allocated receiver value that needs a per-iter transient
root.
Local: 471 tests pass, optcarrot checksum 59662, the original
repro echo -e '1\\n2\\n0' | ./test1 now exits immediately.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- d0dc173f8f38
- Parent
- 2d24a16ed3c8
spinel_codegen.rbmodifiedView file
cond = compile_cond_expr(@nd_predicate[nid]) emit(" } while (" + cond + ");") else # Compile the predicate into a scratch buffer so any side-effecting # emits (compile_expr_gc_rooted's `temp = call(); SP_GC_ROOT(temp);` # for a CallNode receiver, etc.) land INSIDE the loop body instead # of once before it. Issue #500: `while (n = gets.to_i) > 0` hoisted # `_t1 = sp_gets(); SP_GC_ROOT(_t1);` ahead of the while, so every # iteration re-read the same captured first line and the loop never # terminated. Capturing the cond's emits and replaying them inside # the loop body — wrapped in `while (1) { ...; if (!cond) break; ... }` # — keeps the receiver call (and its transient root) per-iteration, # so EOF on stdin actually advances `_t1` and the condition flips. saved_out = @out_lines @out_lines = "".split(",") cond = compile_cond_expr(@nd_predicate[nid]) emit(" while (" + cond + ") {") @indent = @indent + 1 redo_label = push_redo_label emit_redo_label(redo_label) compile_stmts_body(@nd_body[nid]) pop_redo_label @indent = @indent - 1 emit(" }") cond_hoists = @out_lines @out_lines = saved_out if cond_hoists.length > 0 emit(" while (1) {") @indent = @indent + 1 ch_i = 0 while ch_i < cond_hoists.length @out_lines.push(cond_hoists[ch_i]) ch_i = ch_i + 1 end emit("if (!(" + cond + ")) break;") redo_label = push_redo_label emit_redo_label(redo_label) compile_stmts_body(@nd_body[nid]) pop_redo_label @indent = @indent - 1 emit(" }") else emit(" while (" + cond + ") {") @indent = @indent + 1 redo_label = push_redo_label emit_redo_label(redo_label) compile_stmts_body(@nd_body[nid]) pop_redo_label @indent = @indent - 1 emit(" }") end end @hoisted_strlen_var = saved_var @hoisted_strlen_recv = saved_recvtest/while_cond_side_effect.rbaddedView file
Loading diff…
test/while_cond_side_effect.rb.expectedaddedView file
Loading diff…