Commit 10c6e42f
10c6e42f9c0dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): route .each_cons[.with_index].map fusion before emitting rc
compile_call_expr emits the receiver via compile_expr_gc_rooted(recv)
before consulting compile_enumerable_expr -> compile_map_expr. When
recv is the .with_index / .each_cons (no block) shape, that emit
produces a pair of placeholder temps (sp_PtrArray * _t = 0;) for
the enumerator-returning calls -- harmless on their own, but the
inner receiver of .each_cons (e.g., m.times.map { gets.to_i })
gets emitted into rc as a side effect first, then re-emitted by
compile_map_expr's fusion path at line ~32220. Two side-effecting
inner chains land in the output. With gets as the inner side
effect that consumed twice the input lines and produced wrong
output (or a segfault past EOF), as cielavenir reported in #621.
#540 covered the one-level case
(gets.split.map(&:to_i)) by threading the already-compiled rc
into compile_enumerable_expr; the fusion path here doesn't use rc
(it needs the inner receiver of each_cons, which is different from
the with_index/each_cons placeholder chain), so the threading
helps nothing.
Detect the fusion shape (mname == "map" with block + recv is
with_index(n) <- each_cons(m) or each_cons(m) directly, both
no-block) before line 13635 and route directly to
compile_map_expr's fusion. rc never gets emitted.
Fixes #621
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 10c6e42f9c0d
- Parent
- b14d5538273d
spinel_codegen.rbmodifiedView file
if recv_type != "poly" && expr_emits_poly_rb_val(recv) == 1 recv_type = "poly" end # Detect the each_cons[.with_index].map fusion shape before emitting # `rc`. compile_map_expr's fusion path re-compiles the each_cons # inner receiver itself; if we emit `rc` first, the # with_index/each_cons placeholders land as `sp_PtrArray * _t = 0;` # and (worse) the inner chain's side effects (`gets`) execute # twice. Route directly to compile_map_expr instead, which # produces the correct single fused loop. Issue #621. if mname == "map" && @nd_block[nid] >= 0 && recv >= 0 ec_check_recv = recv if @nd_type[ec_check_recv] == "CallNode" && @nd_name[ec_check_recv] == "with_index" && @nd_block[ec_check_recv] < 0 ec_check_recv = @nd_receiver[ec_check_recv] end if ec_check_recv >= 0 && @nd_type[ec_check_recv] == "CallNode" && @nd_name[ec_check_recv] == "each_cons" && @nd_block[ec_check_recv] < 0 return compile_map_expr(nid, "") end end rc = compile_expr_gc_rooted(recv) # Type-narrow unbox: when recv is a LocalVariableReadNode whose # narrowed type (post-`raise unless x.is_a?(C)` guard) is atest/each_cons_with_index_map_no_double_gets.rbaddedView file
Loading diff…
test/each_cons_with_index_map_no_double_gets.rb.expectedaddedView file
Loading diff…