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

@@ -12883,7 +12883,17 @@
1288312883 elsif nrt == "sym_array"
1288412884 types.push("symbol")
1288512885 elsif is_ptr_array_type(nrt) == 1
12886- types.push(ptr_array_elem_type(nrt))
12886+ # When the iterated element is itself an array and the
12887+ # block uses _1, _2, ... (max >= 2), Ruby destructures
12888+ # the yielded sub-array into the numbered slots. Each
12889+ # _i then takes the *inner* element type, not the
12890+ # outer ptr_array element type.
12891+ ptr_elem = ptr_array_elem_type(nrt)
12892+ if nmax >= 2 && is_array_type(ptr_elem) == 1
12893+ types.push(elem_type_of_array(ptr_elem))
12894+ else
12895+ types.push(ptr_elem)
12896+ end
1288712897 else
1288812898 types.push("int")
1288912899 end
@@ -23894,15 +23904,52 @@
2389423904 elem_type = ptr_array_elem_type(rt)
2389523905 tmp = new_temp
2389623906 bp_tmp = new_temp
23907+ # Detect numbered-params auto-destructure: `[[1,10]].each { _1; _2 }`
23908+ # binds _1=1, _2=10 in Ruby. Trigger when the block uses
23909+ # NumberedParametersNode with maximum >= 2 over an element that is
23910+ # itself an array we know how to index.
23911+ blk = @nd_block[nid]
23912+ bp = blk >= 0 ? @nd_parameters[blk] : -1
23913+ destruct_n = 0
23914+ if bp >= 0 && @nd_type[bp] == "NumberedParametersNode"
23915+ if @nd_value[bp] >= 2 && is_array_type(elem_type) == 1
23916+ destruct_n = @nd_value[bp]
23917+ end
23918+ end
2389723919 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_PtrArray_length(" + rc + "); " + tmp + "++) {")
23898- if has_bp == 1
23920+ if destruct_n > 0
23921+ elem_pfx = array_c_prefix(elem_type)
2389923922 emit(" " + c_type(elem_type) + " " + bp_tmp + " = (" + c_type(elem_type) + ")sp_PtrArray_get(" + rc + ", " + tmp + ");")
23900- emit(" lv_" + bp1 + " = " + bp_tmp + ";")
23923+ # Bounds-check missing slots: when the yielded sub-array is shorter
23924+ # than `destruct_n`, CRuby fills with nil; the typed-array codegen
23925+ # has no nil so we default to 0 (the typed-zero analogue). Without
23926+ # this guard the slot read is OOB into the sub-array's data buffer.
23927+ dlen_tmp = new_temp
23928+ emit(" mrb_int " + dlen_tmp + " = sp_" + elem_pfx + "_length(" + bp_tmp + ");")
23929+ di = 0
23930+ while di < destruct_n
23931+ slot = "lv__" + (di + 1).to_s
23932+ emit(" " + slot + " = (" + dlen_tmp + " > " + di.to_s + ") ? sp_" + elem_pfx + "_get(" + bp_tmp + ", " + di.to_s + ") : 0;")
23933+ di = di + 1
23934+ end
23935+ else
23936+ if has_bp == 1
23937+ emit(" " + c_type(elem_type) + " " + bp_tmp + " = (" + c_type(elem_type) + ")sp_PtrArray_get(" + rc + ", " + tmp + ");")
23938+ emit(" lv_" + bp1 + " = " + bp_tmp + ";")
23939+ end
2390123940 end
2390223941 @indent = @indent + 1
2390323942 push_scope
23904- if has_bp == 1
23905- declare_var(bp1, elem_type)
23943+ if destruct_n > 0
23944+ di = 0
23945+ while di < destruct_n
23946+ declare_var("_" + (di + 1).to_s, elem_type_of_array(elem_type))
23947+ di = di + 1
23948+ end
23949+ else
23950+ if has_bp == 1
23951+ declare_var(bp1, elem_type)
23952+ end
2390623953 end
2390723954 compile_stmts_body(@nd_body[@nd_block[nid]])
2390823955 pop_scope