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

@@ -14064,9 +14064,43 @@
1406414064 end
1406514065 if mname == "fill"
1406614066 pfx = array_c_prefix(recv_type)
14067+ args_id_fill = @nd_arguments[nid]
1406714068 val = compile_arg0(nid)
14069+ start_expr = "0"
14070+ # Default end: current array length (matches CRuby's no-args
14071+ # form which fills the entire existing array).
14072+ end_expr = "sp_" + pfx + "_length(" + rc + ")"
14073+ if args_id_fill >= 0
14074+ aargs_fill = get_args(args_id_fill)
14075+ if aargs_fill.length >= 3
14076+ # arr.fill(value, start, length): negative start counts from
14077+ # the end; if still negative after that, clamp to 0
14078+ # (matches CRuby: `[1,2,3].fill(9, -5, 2) #=> [9, 9, 3]`).
14079+ # end_expr = start + length lets the array grow past its
14080+ # current length when start+length > length; sp_*_set
14081+ # auto-grows and zero-fills gaps (matches CRuby:
14082+ # `[1,2,3].fill(9, 5, 2) #=> [1, 2, 3, 0, 0, 9, 9]`).
14083+ start_tmp = new_temp
14084+ len_tmp = new_temp
14085+ emit(" mrb_int " + start_tmp + " = " + compile_expr(aargs_fill[1]) + ";")
14086+ emit(" mrb_int " + len_tmp + " = " + compile_expr(aargs_fill[2]) + ";")
14087+ emit(" if (" + start_tmp + " < 0) " + start_tmp + " += sp_" + pfx + "_length(" + rc + ");")
14088+ emit(" if (" + start_tmp + " < 0) " + start_tmp + " = 0;")
14089+ start_expr = start_tmp
14090+ end_expr = "(" + start_tmp + " + " + len_tmp + ")"
14091+ elsif aargs_fill.length == 2
14092+ # arr.fill(value, start): fills from start to end of EXISTING
14093+ # array. If start >= length, fills nothing (does NOT grow,
14094+ # matching CRuby: `[1,2,3].fill(9, 5) #=> [1, 2, 3]`).
14095+ start_tmp = new_temp
14096+ emit(" mrb_int " + start_tmp + " = " + compile_expr(aargs_fill[1]) + ";")
14097+ emit(" if (" + start_tmp + " < 0) " + start_tmp + " += sp_" + pfx + "_length(" + rc + ");")
14098+ emit(" if (" + start_tmp + " < 0) " + start_tmp + " = 0;")
14099+ start_expr = start_tmp
14100+ end
14101+ end
1406814102 itmp = new_temp
14069- emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_" + pfx + "_length(" + rc + "); " + itmp + "++)")
14103+ emit(" for (mrb_int " + itmp + " = " + start_expr + "; " + itmp + " < " + end_expr + "; " + itmp + "++)")
1407014104 emit(" sp_" + pfx + "_set(" + rc + ", " + itmp + ", " + val + ");")
1407114105 return rc
1407214106 end