Commit 75fe106c
75fe106cc7d7Merged
Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): destructure multi-arg numbered block params over sub-arrays
[[1,10],[2,20]].each { puts "#{_1}=#{_2}" } printed <ptr>=0 instead
of 1=10. The issue had two layers. scan_locals' NumberedParametersNode
branch typed every numbered slot as the outer receiver element type
(int_array for a ptr_array of int_arrays), so lv__1 / lv__2 ended up
declared as sp_IntArray * and nothing bound the inner integers to
them. compile_each_block's ptr_array arm then assigned only lv__1 to
the whole sub-array pointer; lv__2 stayed at its NULL/zero default.
Fix has two matching halves. In scan_locals, when the receiver is a
ptr_array AND the block uses NumberedParametersNode with maximum >= 2
AND the per-element type is itself an array, push the inner element
type into the per-slot types list (int_array_of_int_arrays => int,
int_array_of_str_arrays => string, etc.). In compile_each_block's
ptr_array arm, when the same condition holds, emit one
lv__<i> = sp_<Pfx>_get(<elem>, <i-1>); per slot instead of the
single lv_<bp1> = <elem>; line. The destructure mirrors Ruby's
implicit |a, b| over a single Array argument.
Trigger condition is intentionally narrow: only ptr_array receivers
whose elements are typed arrays (int_array / str_array / float_array /
sym_array / nested ptr_array). poly_array, hash, and mixed sub-array
shapes still flow through the original single-binding path. Single-
slot blocks ({ _1 }, max=1) keep binding _1 to the whole sub-array
to match Ruby's "no destructure when arity == 1" rule.
Test exercises four shapes:
- two-slot destructure over int_array sub-arrays
- three-slot destructure over int_array sub-arrays
- arithmetic on the destructured slots accumulating into a closure local (covers the case where the block body references both _1 and an outer variable across iterations)
- subtraction across the slots
Out of scope: poly_array sub-arrays (would need per-slot tag dispatch
at runtime); cross-block type unification (lv__1 is function-scoped,
so two each blocks at the same lexical level cannot disagree on _1's
inner type); destructure for .map, .select, etc. (separate compile
paths; this commit only touches .each).
Bootstrap: gen2.c == gen3.c. Tests: 203 pass.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 75fe106cc7d7
- Parent
- 02005f26deba
spinel_codegen.rbmodifiedView file
elsif nrt == "sym_array" types.push("symbol") elsif is_ptr_array_type(nrt) == 1 types.push(ptr_array_elem_type(nrt)) # When the iterated element is itself an array and the # block uses _1, _2, ... (max >= 2), Ruby destructures # the yielded sub-array into the numbered slots. Each # _i then takes the *inner* element type, not the # outer ptr_array element type. ptr_elem = ptr_array_elem_type(nrt) if nmax >= 2 && is_array_type(ptr_elem) == 1 types.push(elem_type_of_array(ptr_elem)) else types.push(ptr_elem) end else types.push("int") end elem_type = ptr_array_elem_type(rt) tmp = new_temp bp_tmp = new_temp # Detect numbered-params auto-destructure: `[[1,10]].each { _1; _2 }` # binds _1=1, _2=10 in Ruby. Trigger when the block uses # NumberedParametersNode with maximum >= 2 over an element that is # itself an array we know how to index. blk = @nd_block[nid] bp = blk >= 0 ? @nd_parameters[blk] : -1 destruct_n = 0 if bp >= 0 && @nd_type[bp] == "NumberedParametersNode" if @nd_value[bp] >= 2 && is_array_type(elem_type) == 1 destruct_n = @nd_value[bp] end end emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_PtrArray_length(" + rc + "); " + tmp + "++) {") if has_bp == 1 if destruct_n > 0 elem_pfx = array_c_prefix(elem_type) emit(" " + c_type(elem_type) + " " + bp_tmp + " = (" + c_type(elem_type) + ")sp_PtrArray_get(" + rc + ", " + tmp + ");") emit(" lv_" + bp1 + " = " + bp_tmp + ";") # Bounds-check missing slots: when the yielded sub-array is shorter # than `destruct_n`, CRuby fills with nil; the typed-array codegen # has no nil so we default to 0 (the typed-zero analogue). Without # this guard the slot read is OOB into the sub-array's data buffer. dlen_tmp = new_temp emit(" mrb_int " + dlen_tmp + " = sp_" + elem_pfx + "_length(" + bp_tmp + ");") di = 0 while di < destruct_n slot = "lv__" + (di + 1).to_s emit(" " + slot + " = (" + dlen_tmp + " > " + di.to_s + ") ? sp_" + elem_pfx + "_get(" + bp_tmp + ", " + di.to_s + ") : 0;") di = di + 1 end else if has_bp == 1 emit(" " + c_type(elem_type) + " " + bp_tmp + " = (" + c_type(elem_type) + ")sp_PtrArray_get(" + rc + ", " + tmp + ");") emit(" lv_" + bp1 + " = " + bp_tmp + ";") end end @indent = @indent + 1 push_scope if has_bp == 1 declare_var(bp1, elem_type) if destruct_n > 0 di = 0 while di < destruct_n declare_var("_" + (di + 1).to_s, elem_type_of_array(elem_type)) di = di + 1 end else if has_bp == 1 declare_var(bp1, elem_type) end end compile_stmts_body(@nd_body[@nd_block[nid]]) pop_scopetest/numbered_params_destructure.rbaddedView file
Loading diff…