Commit 3b77e031

3b77e0316caeMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

feat(codegen): Array#[]=(start, length, src) slice assignment

arr[i, n] = src (3-arg []=) now replaces n elements of arr starting at index i with the elements of src. Same- length only (n must equal src.length); resize semantics not implemented.

Previously compile_bracket_assign only consumed arg_ids[0] and arg_ids[1] as idx and val, so a[2, 8] = [10, 20, ...] silently lowered to a[2] = 8 — the length argument was treated as the value, the source array discarded.

Adds compile_slice_assign that emits a per-element copy loop (sp_<X>Array_set(dst, start + i, sp_<X>Array_get(src, i))) for int_array, float_array, str_array, sym_array, ptr_array, and poly_array receivers. Cross-kind assignment (e.g. int_array ← float_array) is intentionally unhandled to avoid silent truncation.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
3b77e0316cae
Parent
fea5369dddb0

spinel_codegen.rbmodifiedView file

@@ -28189,6 +28189,20 @@
2818928189 if args_id >= 0
2819028190 arg_ids = get_args(args_id)
2819128191 end
28192+ # Slice assignment `arr[i, n] = src`: replace n elements
28193+ # starting at index i with the elements of src. Same-length
28194+ # only (n == src.length); resize semantics not yet supported.
28195+ # Only fires when both sides are the same C kind — falls
28196+ # through otherwise to avoid silent miscompiles on mistyped
28197+ # inputs (e.g. an `int_array`-typed src that's actually an
28198+ # array-of-IntArrays would copy the wrong shape).
28199+ if arg_ids.length == 3 && (rt == "int_array" || rt == "float_array" || rt == "str_array" || rt == "sym_array" || is_ptr_array_type(rt) == 1 || rt == "poly_array")
28200+ src_t_check = infer_type(arg_ids[2])
28201+ if array_c_prefix(rt) == array_c_prefix(src_t_check) && (src_t_check == "int_array" || src_t_check == "float_array" || src_t_check == "str_array" || src_t_check == "sym_array" || is_ptr_array_type(src_t_check) == 1 || src_t_check == "poly_array")
28202+ compile_slice_assign(nid, recv, rt, rc, arg_ids)
28203+ return
28204+ end
28205+ end
2819228206 idx = "0"
2819328207 val = "0"
2819428208 if arg_ids.length >= 1
@@ -28265,6 +28279,44 @@
2826528279 end
2826628280 end
2826728281
28282+ # Compile `arr[start, len] = src` slice assignment. Replaces
28283+ # `len` elements of `arr` starting at `start` with the elements
28284+ # of `src`. Same-length only — `src.length` must equal `len` at
28285+ # runtime; resize semantics not implemented. Each index `i in
28286+ # 0...len` is emitted as `arr[start + i] = src[i]`.
28287+ def compile_slice_assign(nid, recv, rt, rc, arg_ids)
28288+ start_id = arg_ids[0]
28289+ len_id = arg_ids[1]
28290+ src_id = arg_ids[2]
28291+ src_t = infer_type(src_id)
28292+ start_e = compile_expr(start_id)
28293+ len_e = compile_expr(len_id)
28294+ src_e = compile_expr_gc_rooted(src_id)
28295+ pfx = array_c_prefix(rt)
28296+ src_pfx = array_c_prefix(src_t)
28297+ if pfx == "" || src_pfx == ""
28298+ return
28299+ end
28300+ rc_t = new_temp
28301+ src_t_v = new_temp
28302+ start_t = new_temp
28303+ ii = new_temp
28304+ emit(" {")
28305+ emit(" sp_" + pfx + " *" + rc_t + " = " + rc + ";")
28306+ emit(" sp_" + src_pfx + " *" + src_t_v + " = " + src_e + ";")
28307+ emit(" mrb_int " + start_t + " = " + start_e + ";")
28308+ emit(" mrb_int _n = " + len_e + ";")
28309+ # Inner loop: copy n elements from src to dest. Casting between
28310+ # array kinds isn't supported here (e.g. int_array ← float_array
28311+ # would silently truncate); both must be the same C kind.
28312+ if pfx == src_pfx
28313+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < _n; " + ii + "++) {")
28314+ emit(" sp_" + pfx + "_set(" + rc_t + ", " + start_t + " + " + ii + ", sp_" + src_pfx + "_get(" + src_t_v + ", " + ii + "));")
28315+ emit(" }")
28316+ end
28317+ emit(" }")
28318+ end
28319+
2826828320 # Compile `recv[idx] OP= value` (IndexOperatorWriteNode).
2826928321 #
2827028322 # Emitted as a get-modify-set against the appropriate typed container,