Commit af893254

af8932544e54Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

Merge pull request #338 from mame/pr/sized-poly-array-nil-n

fix(codegen): emit sized PolyArray for [nil] * N against poly slot

Details

Commit
af8932544e54
Parent
a006b1148279
Merge parent
335838be2dac

spinel_codegen.rbmodifiedView file

@@ -9509,6 +9509,22 @@
95099509 end
95109510 return tmp
95119511 end
9512+ # `[nil] * N` against a poly slot (e.g. optcarrots @fetch widened
9513+ # to sp_RbVal because of cyclic inference): build a sp_PolyArray
9514+ # of N nils and box it. Subsequent heterogeneous writes via the
9515+ # poly-recv `[]=` branch dispatch into the PolyArray correctly.
9516+ # Without this branch, `[nil] * N` produces an IntArray and the
9517+ # subsequent `@x[i] = method_obj` writes the pointer as an int —
9518+ # reads then return ints and the method dispatch never fires.
9519+ if expected_base == "poly" && is_sized_empty_array_default(nid) == 1
9520+ @needs_gc = 1
9521+ @needs_rb_value = 1
9522+ cnt_e = compile_arg0(nid)
9523+ tmp = new_temp
9524+ emit(" sp_PolyArray *" + tmp + " = sp_PolyArray_new();")
9525+ emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp + ", sp_box_nil()); }")
9526+ return "sp_box_poly_array(" + tmp + ")"
9527+ end
95129528 val = compile_expr(nid)
95139529 at = infer_type(nid)
95149530 if expected_base == "poly" && at != "poly"
@@ -17281,6 +17297,18 @@
1728117297 emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_iv + ", NULL); }")
1728217298 end
1728317299 emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";")
17300+ elsif ivt == "poly" && is_sized_empty_array_default(expr_id_iv) == 1
17301+ # `@arr = [nil] * N` against a poly slot. Build a
17302+ # sized PolyArray and box it so the runtime storage
17303+ # supports heterogeneous writes via the poly-recv
17304+ # `[]=` dispatch.
17305+ @needs_gc = 1
17306+ @needs_rb_value = 1
17307+ cnt_e_iv = compile_arg0(expr_id_iv)
17308+ tmp_iv = new_temp
17309+ emit_raw(" sp_PolyArray *" + tmp_iv + " = sp_PolyArray_new();")
17310+ emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp_iv + ", sp_box_nil()); }")
17311+ emit_raw(" " + self_arrow + ivar + " = sp_box_poly_array(" + tmp_iv + ");")
1728417312 elsif ivt == "poly_array" && @nd_type[expr_id_iv] == "ArrayNode"
1728517313 # Non-empty array literal `[a, b, ...]` going into a
1728617314 # poly_array slot. compile_array_literal infers a typed
@@ -28216,6 +28244,22 @@
2821628244 emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";")
2821728245 return
2821828246 end
28247+ # `@arr = [nil] * N` where @arr is a poly slot (sp_RbVal). The
28248+ # cyclic inference can leave a slot at poly when its definition
28249+ # site `[nil] * N` would naturally infer as int_array but a
28250+ # subsequent heterogeneous write demands more. Emit as a sized
28251+ # PolyArray boxed to poly so the runtime storage supports
28252+ # heterogeneous writes via the poly-recv `[]=` dispatch.
28253+ if ivt == "poly" && is_sized_empty_array_default(expr_id) == 1
28254+ @needs_gc = 1
28255+ @needs_rb_value = 1
28256+ cnt_e = compile_arg0(expr_id)
28257+ tmp_arr = new_temp
28258+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
28259+ emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp_arr + ", sp_box_nil()); }")
28260+ emit(" " + self_arrow + sanitize_ivar(iname) + " = sp_box_poly_array(" + tmp_arr + ");")
28261+ return
28262+ end
2821928263 # Non-empty array literal `[a, b, ...]` going into a poly_array
2822028264 # slot. compile_array_literal infers a typed array (ptr_array of
2822128265 # one class for homogeneous obj literals, etc.) and emits the