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 (default new constructor)
  • 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

@@ -9416,6 +9416,22 @@
94169416 end
94179417 return tmp
94189418 end
9419+ # `[nil] * N` against a poly slot (e.g. optcarrots @fetch widened
9420+ # to sp_RbVal because of cyclic inference): build a sp_PolyArray
9421+ # of N nils and box it. Subsequent heterogeneous writes via the
9422+ # poly-recv `[]=` branch dispatch into the PolyArray correctly.
9423+ # Without this branch, `[nil] * N` produces an IntArray and the
9424+ # subsequent `@x[i] = method_obj` writes the pointer as an int —
9425+ # reads then return ints and the method dispatch never fires.
9426+ if expected_base == "poly" && is_sized_empty_array_default(nid) == 1
9427+ @needs_gc = 1
9428+ @needs_rb_value = 1
9429+ cnt_e = compile_arg0(nid)
9430+ tmp = new_temp
9431+ emit(" sp_PolyArray *" + tmp + " = sp_PolyArray_new();")
9432+ emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp + ", sp_box_nil()); }")
9433+ return "sp_box_poly_array(" + tmp + ")"
9434+ end
94199435 val = compile_expr(nid)
94209436 at = infer_type(nid)
94219437 if expected_base == "poly" && at != "poly"
@@ -17132,6 +17148,18 @@
1713217148 emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_iv + ", NULL); }")
1713317149 end
1713417150 emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";")
17151+ elsif ivt == "poly" && is_sized_empty_array_default(expr_id_iv) == 1
17152+ # `@arr = [nil] * N` against a poly slot. Build a
17153+ # sized PolyArray and box it so the runtime storage
17154+ # supports heterogeneous writes via the poly-recv
17155+ # `[]=` dispatch.
17156+ @needs_gc = 1
17157+ @needs_rb_value = 1
17158+ cnt_e_iv = compile_arg0(expr_id_iv)
17159+ tmp_iv = new_temp
17160+ emit_raw(" sp_PolyArray *" + tmp_iv + " = sp_PolyArray_new();")
17161+ emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp_iv + ", sp_box_nil()); }")
17162+ emit_raw(" " + self_arrow + ivar + " = sp_box_poly_array(" + tmp_iv + ");")
1713517163 elsif ivt == "poly_array" && @nd_type[expr_id_iv] == "ArrayNode"
1713617164 # Non-empty array literal `[a, b, ...]` going into a
1713717165 # poly_array slot. compile_array_literal infers a typed
@@ -27930,6 +27958,22 @@
2793027958 emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";")
2793127959 return
2793227960 end
27961+ # `@arr = [nil] * N` where @arr is a poly slot (sp_RbVal). The
27962+ # cyclic inference can leave a slot at poly when its definition
27963+ # site `[nil] * N` would naturally infer as int_array but a
27964+ # subsequent heterogeneous write demands more. Emit as a sized
27965+ # PolyArray boxed to poly so the runtime storage supports
27966+ # heterogeneous writes via the poly-recv `[]=` dispatch.
27967+ if ivt == "poly" && is_sized_empty_array_default(expr_id) == 1
27968+ @needs_gc = 1
27969+ @needs_rb_value = 1
27970+ cnt_e = compile_arg0(expr_id)
27971+ tmp_arr = new_temp
27972+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
27973+ emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp_arr + ", sp_box_nil()); }")
27974+ emit(" " + self_arrow + sanitize_ivar(iname) + " = sp_box_poly_array(" + tmp_arr + ");")
27975+ return
27976+ end
2793327977 # Non-empty array literal `[a, b, ...]` going into a poly_array
2793427978 # slot. compile_array_literal infers a typed array (ptr_array of
2793527979 # one class for homogeneous obj literals, etc.) and emits the