Commit a03517c7
a03517c7529dMerged
Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): declare synthesized index for paramless Integer#step blocks
Integer#step with a do-block that omits its parameter referenced an
undeclared _i index:
sum = 0 1.step(10, 1) do sum += 1 end puts sum
/tmp/_step.c: In function 'main': /tmp/_step.c:14:10: error: 'lv__i' undeclared (first use in this function) 14 | for (lv__i = 1; lv__i <= 10; lv__i += 1) { | ^~~~~
Wrap the loop in a { ... } block and emit mrb_int lv__i = 0;
inside it when the block param is synthesized. The block scope also
prevents a second paramless step in the same function from clashing
with the first.
Adds test/range_step_no_block_param.rb.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- a03517c7529d
- Parent
- d6d1ec889ccf
spinel_codegen.rbmodifiedView file
end end bp1 = get_block_param(nid, 0) synth = 0 if bp1 == "" bp1 = "_i" synth = 1 end # When the block omits its parameter, the synthesized `_i` isn't # declared anywhere — wrap the loop in a block scope and declare # it locally. This also avoids redefinition errors when multiple # paramless `step` blocks appear in the same function. if synth == 1 emit(" {") emit(" mrb_int lv_" + bp1 + " = 0;") end emit(" for (lv_" + bp1 + " = " + rc + "; lv_" + bp1 + " <= " + limit_val + "; lv_" + bp1 + " += " + step_val + ") {") @indent = @indent + 1 compile_stmts_body(@nd_body[@nd_block[nid]]) @indent = @indent - 1 emit(" }") if synth == 1 emit(" }") end @in_loop = old return 1 endtest/range_step_no_block_param.rbaddedView file
Loading diff…