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

@@ -18487,14 +18487,27 @@
1848718487 end
1848818488 end
1848918489 bp1 = get_block_param(nid, 0)
18490+ synth = 0
1849018491 if bp1 == ""
1849118492 bp1 = "_i"
18493+ synth = 1
1849218494 end
18495+ # When the block omits its parameter, the synthesized `_i` isn't
18496+ # declared anywhere — wrap the loop in a block scope and declare
18497+ # it locally. This also avoids redefinition errors when multiple
18498+ # paramless `step` blocks appear in the same function.
18499+ if synth == 1
18500+ emit(" {")
18501+ emit(" mrb_int lv_" + bp1 + " = 0;")
18502+ end
1849318503 emit(" for (lv_" + bp1 + " = " + rc + "; lv_" + bp1 + " <= " + limit_val + "; lv_" + bp1 + " += " + step_val + ") {")
1849418504 @indent = @indent + 1
1849518505 compile_stmts_body(@nd_body[@nd_block[nid]])
1849618506 @indent = @indent - 1
1849718507 emit(" }")
18508+ if synth == 1
18509+ emit(" }")
18510+ end
1849818511 @in_loop = old
1849918512 return 1
1850018513 end