Commit 3e82c71d

3e82c71d5d7aMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

runtime/codegen: widen IntArray to PolyArray on heterogeneous index write

sp_poly_arr_set was silently truncating non-int values when the backing storage was an IntArray. Add sp_poly_arr_widen_and_set which allocates a fresh PolyArray, copies existing elements, and sets the new slot when a non-numeric value is stored into an IntArray slot. The poly []= codegen with int key now emits an assignment back to the receiver so the widened array is retained.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
3e82c71d5d7a
Parent
bbfed34f4bc0

lib/sp_runtime.hmodifiedView file

@@ -3777,6 +3777,21 @@
37773777 }
37783778 return val;
37793779 }
3780+/* Like sp_poly_arr_set but widens IntArray to PolyArray when val is non-numeric.
3781+ Returns the (possibly new) array RbVal so the caller can update the slot. */
3782+static sp_RbVal sp_poly_arr_widen_and_set(sp_RbVal v, mrb_int idx, sp_RbVal val) {
3783+ if (v.tag == SP_TAG_OBJ && v.cls_id == SP_BUILTIN_INT_ARRAY &&
3784+ val.tag != SP_TAG_INT && val.tag != SP_TAG_FLT) {
3785+ sp_IntArray *ia = (sp_IntArray *)v.v.p;
3786+ sp_PolyArray *pa = sp_PolyArray_new();
3787+ for (mrb_int k = 0; k < ia->len; k++)
3788+ sp_PolyArray_push(pa, sp_box_int(ia->data[ia->start + k]));
3789+ sp_PolyArray_set(pa, idx, val);
3790+ return sp_box_poly_array(pa);
3791+ }
3792+ sp_poly_arr_set(v, idx, val);
3793+ return v;
3794+}
37803795 /* poly_val[poly_key] = val: fully dynamic dispatch for poly recv + poly key. */
37813796 static sp_RbVal sp_poly_set_poly(sp_RbVal v, sp_RbVal key, sp_RbVal val) {
37823797 if (v.tag != SP_TAG_OBJ) return val;