Commit 72fbbe24

72fbbe24a1cbMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

Don't bind the iteration slot for a no-parameter predicate block

[].any? { true } (and the all?/none?/one? peers) emitted an undeclared lv__x = sp_IntArray_get(...) write: the predicate emitter defaulted the absent block parameter to a synthetic _x and bound it, but that slot is never declared (only real params, including it/_1, are). A block with no formal parameter can't reference the yielded value, so skip the element read entirely via a new bind_elem flag on emit_iter_open. Existing callers pass the default and are byte-for-byte unchanged.

Fixes #1312

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
72fbbe24a1cb
Parent
62242d383aed

spinel_codegen.rbmodifiedView file

@@ -45226,13 +45226,17 @@
4522645226 # the caller emits the block body and a closing '}'. idx_var holds
4522745227 # the loop counter (position or value for range); elem_var gets the
4522845228 # current element.
45229- def emit_iter_open(rc, recv_type, elem_var, idx_var)
45229+ def emit_iter_open(rc, recv_type, elem_var, idx_var, bind_elem = 1)
4523045230 # `--int-overflow=promote` may have widened the block param's
4523145231 # declared slot to sp_Bigint *, but the underlying iterator
4523245232 # (sp_IntArray_get, range counter, ...) yields a raw mrb_int.
4523345233 # Detect the widening via find_var_type on the stripped-`lv_`
4523445234 # name and wrap the get with sp_bigint_new_int so the assign
4523545235 # is typed-clean.
45236+ # bind_elem == 0 skips the element read entirely: a block with no
45237+ # formal parameter can't reference the yielded value (`it`/`_1` are
45238+ # surfaced as real params), and the synthetic `_x` slot is never
45239+ # declared, so emitting `lv__x = ...` would be an undeclared write.
4523645240 iter_elem_t = iter_elem_type(recv_type)
4523745241 needs_big_wrap = 0
4523845242 if elem_var.start_with?("lv_") && iter_elem_t == "int"
@@ -45246,19 +45250,23 @@
4524645250 rtmp = new_temp
4524745251 emit(" sp_Range " + rtmp + " = " + rc + ";")
4524845252 emit(" for (mrb_int " + idx_var + " = " + rtmp + ".first; " + idx_var + " <= " + rtmp + ".last - " + rtmp + ".excl; " + idx_var + "++) {")
45249- if needs_big_wrap == 1
45250- emit(" " + elem_var + " = sp_bigint_new_int(" + idx_var + ");")
45251- else
45252- emit(" " + elem_var + " = " + idx_var + ";")
45253+ if bind_elem == 1
45254+ if needs_big_wrap == 1
45255+ emit(" " + elem_var + " = sp_bigint_new_int(" + idx_var + ");")
45256+ else
45257+ emit(" " + elem_var + " = " + idx_var + ";")
45258+ end
4525345259 end
4525445260 return
4525545261 end
4525645262 pfx = array_c_prefix(recv_type)
4525745263 emit(" for (mrb_int " + idx_var + " = 0; " + idx_var + " < sp_" + pfx + "_length(" + rc + "); " + idx_var + "++) {")
45258- if needs_big_wrap == 1
45259- emit(" " + elem_var + " = sp_bigint_new_int(sp_" + pfx + "_get(" + rc + ", " + idx_var + "));")
45260- else
45261- emit(" " + elem_var + " = sp_" + pfx + "_get(" + rc + ", " + idx_var + ");")
45264+ if bind_elem == 1
45265+ if needs_big_wrap == 1
45266+ emit(" " + elem_var + " = sp_bigint_new_int(sp_" + pfx + "_get(" + rc + ", " + idx_var + "));")
45267+ else
45268+ emit(" " + elem_var + " = sp_" + pfx + "_get(" + rc + ", " + idx_var + ");")
45269+ end
4526245270 end
4526345271 end
4526445272
@@ -45689,8 +45697,10 @@
4568945697 def compile_array_predicate_block(nid, rc, recv_type, mname)
4569045698 # Implements any?/all?/none? with block by short-circuit loop
4569145699 bp1 = get_block_param(nid, 0)
45700+ bind1 = 1
4569245701 if bp1 == ""
4569345702 bp1 = "_x"
45703+ bind1 = 0
4569445704 end
4569545705 tmp_res = new_temp
4569645706 tmp_i = new_temp
@@ -45699,7 +45709,7 @@
4569945709 init_val = "TRUE"
4570045710 end
4570145711 emit(" mrb_bool " + tmp_res + " = " + init_val + ";")
45702- emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i)
45712+ emit_iter_open(rc, recv_type, "lv_" + bp1, tmp_i, bind1)
4570345713 push_scope
4570445714 bp1_t_pred = find_var_type(bp1)
4570545715 if bp1_t_pred != "bigint"