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
end95109510
return tmp95119511
end9512+
# `[nil] * N` against a poly slot (e.g. optcarrots @fetch widened9513+
# to sp_RbVal because of cyclic inference): build a sp_PolyArray9514+
# of N nils and box it. Subsequent heterogeneous writes via the9515+
# poly-recv `[]=` branch dispatch into the PolyArray correctly.9516+
# Without this branch, `[nil] * N` produces an IntArray and the9517+
# 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) == 19520+
@needs_gc = 19521+
@needs_rb_value = 19522+
cnt_e = compile_arg0(nid)9523+
tmp = new_temp9524+
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+
end95129528
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
end1728317299
emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";")17300+
elsif ivt == "poly" && is_sized_empty_array_default(expr_id_iv) == 117301+
# `@arr = [nil] * N` against a poly slot. Build a17302+
# sized PolyArray and box it so the runtime storage17303+
# supports heterogeneous writes via the poly-recv17304+
# `[]=` dispatch.17305+
@needs_gc = 117306+
@needs_rb_value = 117307+
cnt_e_iv = compile_arg0(expr_id_iv)17308+
tmp_iv = new_temp17309+
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 a1728617314
# poly_array slot. compile_array_literal infers a typed@@ -28216,6 +28244,22 @@
2821628244
emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";")2821728245
return2821828246
end28247+
# `@arr = [nil] * N` where @arr is a poly slot (sp_RbVal). The28248+
# cyclic inference can leave a slot at poly when its definition28249+
# site `[nil] * N` would naturally infer as int_array but a28250+
# subsequent heterogeneous write demands more. Emit as a sized28251+
# PolyArray boxed to poly so the runtime storage supports28252+
# heterogeneous writes via the poly-recv `[]=` dispatch.28253+
if ivt == "poly" && is_sized_empty_array_default(expr_id) == 128254+
@needs_gc = 128255+
@needs_rb_value = 128256+
cnt_e = compile_arg0(expr_id)28257+
tmp_arr = new_temp28258+
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+
return28262+
end2821928263
# Non-empty array literal `[a, b, ...]` going into a poly_array2822028264
# slot. compile_array_literal infers a typed array (ptr_array of2822128265
# one class for homogeneous obj literals, etc.) and emits thetest/sized_poly_array_nil_n.rbaddedView file
Loading diff…
test/sized_poly_array_nil_n.rb.expectedaddedView file
Loading diff…