Commit bcc7d4b7
bcc7d4b7700eMerged
Chris Hasiński <[email protected]> · 7 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): respect start/length in Array#fill(val, start, len)
Two- and three-argument forms silently fell through and filled the whole array (the dispatch only used compile_arg0 for the value and ignored the rest of the args). arr.fill(0, 1, 3) on [1,2,3,4,5] filled all five elements with 0 instead of leaving 1 and 5.
Adds 2-arg (val, start) and 3-arg (val, start, length) handling. Negative start now indexes from the end as in CRuby.
Note: the related "fill in rescue" failure with an empty array
literal (arr = []; arr.fill(0,0,3) rescue ...) involves a separate
inference bug where empty [] infers IntArray but rescue tries to
unify the result with the rescue clause. Tracked separately.
Details
- Commit
- bcc7d4b7700e
- Parent
- e99ca66646a4
spinel_codegen.rbmodifiedView file
end if mname == "fill" pfx = array_c_prefix(recv_type) args_id_fill = @nd_arguments[nid] val = compile_arg0(nid) start_expr = "0" # Default end: current array length (matches CRuby's no-args # form which fills the entire existing array). end_expr = "sp_" + pfx + "_length(" + rc + ")" if args_id_fill >= 0 aargs_fill = get_args(args_id_fill) if aargs_fill.length >= 3 # arr.fill(value, start, length): negative start counts from # the end; if still negative after that, clamp to 0 # (matches CRuby: `[1,2,3].fill(9, -5, 2) #=> [9, 9, 3]`). # end_expr = start + length lets the array grow past its # current length when start+length > length; sp_*_set # auto-grows and zero-fills gaps (matches CRuby: # `[1,2,3].fill(9, 5, 2) #=> [1, 2, 3, 0, 0, 9, 9]`). start_tmp = new_temp len_tmp = new_temp emit(" mrb_int " + start_tmp + " = " + compile_expr(aargs_fill[1]) + ";") emit(" mrb_int " + len_tmp + " = " + compile_expr(aargs_fill[2]) + ";") emit(" if (" + start_tmp + " < 0) " + start_tmp + " += sp_" + pfx + "_length(" + rc + ");") emit(" if (" + start_tmp + " < 0) " + start_tmp + " = 0;") start_expr = start_tmp end_expr = "(" + start_tmp + " + " + len_tmp + ")" elsif aargs_fill.length == 2 # arr.fill(value, start): fills from start to end of EXISTING # array. If start >= length, fills nothing (does NOT grow, # matching CRuby: `[1,2,3].fill(9, 5) #=> [1, 2, 3]`). start_tmp = new_temp emit(" mrb_int " + start_tmp + " = " + compile_expr(aargs_fill[1]) + ";") emit(" if (" + start_tmp + " < 0) " + start_tmp + " += sp_" + pfx + "_length(" + rc + ");") emit(" if (" + start_tmp + " < 0) " + start_tmp + " = 0;") start_expr = start_tmp end end itmp = new_temp emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_" + pfx + "_length(" + rc + "); " + itmp + "++)") emit(" for (mrb_int " + itmp + " = " + start_expr + "; " + itmp + " < " + end_expr + "; " + itmp + "++)") emit(" sp_" + pfx + "_set(" + rc + ", " + itmp + ", " + val + ");") return rc endtest/bm_array_fill.rbaddedView file
Loading diff…