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

@@ -23096,15 +23096,46 @@
2309623096 cond = compile_cond_expr(@nd_predicate[nid])
2309723097 emit(" } while (" + cond + ");")
2309823098 else
23099+ # Compile the predicate into a scratch buffer so any side-effecting
23100+ # emits (compile_expr_gc_rooted's `temp = call(); SP_GC_ROOT(temp);`
23101+ # for a CallNode receiver, etc.) land INSIDE the loop body instead
23102+ # of once before it. Issue #500: `while (n = gets.to_i) > 0` hoisted
23103+ # `_t1 = sp_gets(); SP_GC_ROOT(_t1);` ahead of the while, so every
23104+ # iteration re-read the same captured first line and the loop never
23105+ # terminated. Capturing the cond's emits and replaying them inside
23106+ # the loop body — wrapped in `while (1) { ...; if (!cond) break; ... }`
23107+ # — keeps the receiver call (and its transient root) per-iteration,
23108+ # so EOF on stdin actually advances `_t1` and the condition flips.
23109+ saved_out = @out_lines
23110+ @out_lines = "".split(",")
2309923111 cond = compile_cond_expr(@nd_predicate[nid])
23100- emit(" while (" + cond + ") {")
23101- @indent = @indent + 1
23102- redo_label = push_redo_label
23103- emit_redo_label(redo_label)
23104- compile_stmts_body(@nd_body[nid])
23105- pop_redo_label
23106- @indent = @indent - 1
23107- emit(" }")
23112+ cond_hoists = @out_lines
23113+ @out_lines = saved_out
23114+ if cond_hoists.length > 0
23115+ emit(" while (1) {")
23116+ @indent = @indent + 1
23117+ ch_i = 0
23118+ while ch_i < cond_hoists.length
23119+ @out_lines.push(cond_hoists[ch_i])
23120+ ch_i = ch_i + 1
23121+ end
23122+ emit("if (!(" + cond + ")) break;")
23123+ redo_label = push_redo_label
23124+ emit_redo_label(redo_label)
23125+ compile_stmts_body(@nd_body[nid])
23126+ pop_redo_label
23127+ @indent = @indent - 1
23128+ emit(" }")
23129+ else
23130+ emit(" while (" + cond + ") {")
23131+ @indent = @indent + 1
23132+ redo_label = push_redo_label
23133+ emit_redo_label(redo_label)
23134+ compile_stmts_body(@nd_body[nid])
23135+ pop_redo_label
23136+ @indent = @indent - 1
23137+ emit(" }")
23138+ end
2310823139 end
2310923140 @hoisted_strlen_var = saved_var
2311023141 @hoisted_strlen_recv = saved_recv