Commit 335838be
335838be2dacMerged
Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): emit sized PolyArray for [nil] * N against poly slot
@arr = [nil] * N against an ivar slot inferred as poly
(sp_RbVal — wider than poly_array) used to compile to
sp_box_int_array(sp_IntArray_new()) followed by N bit-shift
left ops via sp_poly_shl. The runtime storage was IntArray,
so subsequent heterogeneous writes (@fetch[i] = method(:foo))
stored the Method pointer as an int into the IntArray and reads
returned ints — the Method dispatch never fired.
Add a parallel branch alongside the existing poly_array / ptr_array sized-empty-array codegen in three sites:
compile_expr_for_expected_type(default-arg / call-arg path)emit_constructor's ivar-init scan (defaultnewconstructor)compile_stmt's InstanceVariableWriteNode emit (explicit init)
Each emits a sized sp_PolyArray of sp_box_nil()s and boxes it
via sp_box_poly_array(...). The poly slot then holds a poly
value with cls_id == SP_BUILTIN_POLY_ARRAY, and the poly-recv
[]= / << dispatch correctly stores boxed values in the
underlying PolyArray instead of casting them through mrb_int.
Effect on optcarrot: the natural @fetch[a][a] path now reaches
real Method dispatch at runtime (previously the IntArray storage
collapsed Method pointers to ints). Spinel-compiled binary still
renders incorrectly (checksum 16384 vs target 59662) — likely a
related but separate inference issue elsewhere — but Method
dispatch is now actually invoked, which is the prerequisite.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 335838be2dac
- Parent
- a8d909e5a878
spinel_codegen.rbmodifiedView file
end return tmp end # `[nil] * N` against a poly slot (e.g. optcarrots @fetch widened # to sp_RbVal because of cyclic inference): build a sp_PolyArray # of N nils and box it. Subsequent heterogeneous writes via the # poly-recv `[]=` branch dispatch into the PolyArray correctly. # Without this branch, `[nil] * N` produces an IntArray and the # subsequent `@x[i] = method_obj` writes the pointer as an int — # reads then return ints and the method dispatch never fires. if expected_base == "poly" && is_sized_empty_array_default(nid) == 1 @needs_gc = 1 @needs_rb_value = 1 cnt_e = compile_arg0(nid) tmp = new_temp emit(" sp_PolyArray *" + tmp + " = sp_PolyArray_new();") emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp + ", sp_box_nil()); }") return "sp_box_poly_array(" + tmp + ")" end val = compile_expr(nid) at = infer_type(nid) if expected_base == "poly" && at != "poly" emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_iv + ", NULL); }") end emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";") elsif ivt == "poly" && is_sized_empty_array_default(expr_id_iv) == 1 # `@arr = [nil] * N` against a poly slot. Build a # sized PolyArray and box it so the runtime storage # supports heterogeneous writes via the poly-recv # `[]=` dispatch. @needs_gc = 1 @needs_rb_value = 1 cnt_e_iv = compile_arg0(expr_id_iv) tmp_iv = new_temp emit_raw(" sp_PolyArray *" + tmp_iv + " = sp_PolyArray_new();") emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp_iv + ", sp_box_nil()); }") emit_raw(" " + self_arrow + ivar + " = sp_box_poly_array(" + tmp_iv + ");") elsif ivt == "poly_array" && @nd_type[expr_id_iv] == "ArrayNode" # Non-empty array literal `[a, b, ...]` going into a # poly_array slot. compile_array_literal infers a typed emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";") return end # `@arr = [nil] * N` where @arr is a poly slot (sp_RbVal). The # cyclic inference can leave a slot at poly when its definition # site `[nil] * N` would naturally infer as int_array but a # subsequent heterogeneous write demands more. Emit as a sized # PolyArray boxed to poly so the runtime storage supports # heterogeneous writes via the poly-recv `[]=` dispatch. if ivt == "poly" && is_sized_empty_array_default(expr_id) == 1 @needs_gc = 1 @needs_rb_value = 1 cnt_e = compile_arg0(expr_id) tmp_arr = new_temp emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();") emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp_arr + ", sp_box_nil()); }") emit(" " + self_arrow + sanitize_ivar(iname) + " = sp_box_poly_array(" + tmp_arr + ");") return end # Non-empty array literal `[a, b, ...]` going into a poly_array # slot. compile_array_literal infers a typed array (ptr_array of # 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…