Commit ebee821b

ebee821b3fb0Merged

Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): preserve cls_id through poly_array literal w/ typed-ptr-array CallNode elem

[A, B] outer literal where A and B are non-ArrayNode expressions returning a typed <X>_ptr_array (e.g. (0..N).map { ... }, Array.new(N) { ... }) used to hit compile_array_literal's poly_array branch's box_value_to_poly(et, val) fallback, which lowers to sp_box_ptr_array(val) — cls_id PTR_ARRAY only, the inner element type is erased. Subsequent arr[b][i][j] reads dispatch through the PTR_ARRAY arm at level 1 and box the next-level element as sp_box_obj(p, 0) (cls_id 0, unknown obj); the level-2 dispatch matches no cls_id arm and the leaf returns 0.

compile_array_literal_as_poly already handles the ArrayNode- inner case via recursion, but only walks ArrayNodes — non-ArrayNode elements fell through. Add a runtime conversion loop that iterates the typed PtrArray and pushes each inner with its real cls_id (sp_box_int_array etc.). Same dispatch shape as the compile_map_expr deep-array branch already uses for .map-of-typed-array results.

Apply to all four sites that emit a poly_array container with ArrayNode-inner runtime conversion already wired:

  • compile_array_literal_as_poly (recursive helper)
  • compile_array_literal poly_array branch
  • compile_assign InstanceVariableWriteNode poly_array branch
  • compile_constructor InstanceVariableWriteNode poly_array branch

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
ebee821b3fb0
Parent
2e1c2c409741

spinel_codegen.rbmodifiedView file

@@ -17855,6 +17855,22 @@
1785517855 if @nd_type[eid] == "ArrayNode" && (is_ptr_array_type(et) == 1 || et == "poly_array")
1785617856 inner_iv = compile_array_literal_as_poly(eid)
1785717857 emit_raw(" sp_PolyArray_push(" + tmp_iv + ", sp_box_poly_array(" + inner_iv + "));")
17858+ elsif is_ptr_array_type(et) == 1
17859+ # Same 3D+ erasure but the element is a non-ArrayNode
17860+ # expression (e.g. `Array.new(N) { ... }` returning a
17861+ # typed `<X>_ptr_array`). Convert at runtime so each
17862+ # inner element keeps its real cls_id —
17863+ # `arr[b][i][j]` chains correctly.
17864+ ev = compile_expr(eid)
17865+ inner_t_iv = ptr_array_elem_type(et)
17866+ conv_iv = new_temp
17867+ ii_iv = new_temp
17868+ emit_raw(" sp_PolyArray *" + conv_iv + " = sp_PolyArray_new();")
17869+ emit_raw(" for (mrb_int " + ii_iv + " = 0; " + ii_iv + " < sp_PtrArray_length(" + ev + "); " + ii_iv + "++) {")
17870+ inner_get_iv = "(" + c_type(inner_t_iv) + ")sp_PtrArray_get(" + ev + ", " + ii_iv + ")"
17871+ emit_raw(" sp_PolyArray_push(" + conv_iv + ", " + box_value_to_poly(inner_t_iv, inner_get_iv) + ");")
17872+ emit_raw(" }")
17873+ emit_raw(" sp_PolyArray_push(" + tmp_iv + ", sp_box_poly_array(" + conv_iv + "));")
1785817874 else
1785917875 ev = compile_expr(eid)
1786017876 ebox = et == "poly" ? ev : box_value_to_poly(et, ev)
@@ -28211,6 +28227,22 @@
2821128227 if @nd_type[eid] == "ArrayNode" && (is_ptr_array_type(et) == 1 || et == "poly_array")
2821228228 inner = compile_array_literal_as_poly(eid)
2821328229 emit(" sp_PolyArray_push(" + tmp + ", sp_box_poly_array(" + inner + "));")
28230+ elsif is_ptr_array_type(et) == 1
28231+ # Non-ArrayNode whose static type is a typed `<X>_ptr_array`
28232+ # (e.g. `Array.new(N) { ... }` returning int_array_ptr_array).
28233+ # Convert at runtime so each inner element keeps its real
28234+ # cls_id (sp_box_int_array etc.) — `arr[i][j][k]` chains
28235+ # correctly through the next-level dispatch.
28236+ val = compile_expr(eid)
28237+ inner_t = ptr_array_elem_type(et)
28238+ conv = new_temp
28239+ ii = new_temp
28240+ emit(" sp_PolyArray *" + conv + " = sp_PolyArray_new();")
28241+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < sp_PtrArray_length(" + val + "); " + ii + "++) {")
28242+ inner_get = "(" + c_type(inner_t) + ")sp_PtrArray_get(" + val + ", " + ii + ")"
28243+ emit(" sp_PolyArray_push(" + conv + ", " + box_value_to_poly(inner_t, inner_get) + ");")
28244+ emit(" }")
28245+ emit(" sp_PolyArray_push(" + tmp + ", sp_box_poly_array(" + conv + "));")
2821428246 else
2821528247 val = compile_expr(eid)
2821628248 emit(" sp_PolyArray_push(" + tmp + ", " + box_value_to_poly(et, val) + ");")
@@ -28269,6 +28301,23 @@
2826928301 if @nd_type[elems[k]] == "ArrayNode" && (is_ptr_array_type(et) == 1 || et == "poly_array")
2827028302 val = compile_array_literal_as_poly(elems[k])
2827128303 emit(" sp_PolyArray_push(" + tmp + ", sp_box_poly_array(" + val + "));")
28304+ elsif is_ptr_array_type(et) == 1
28305+ # Same 3D+ erasure but the element is a non-ArrayNode
28306+ # expression (e.g. `Array.new(N) { ... }` returning a
28307+ # typed `<X>_ptr_array`). compile_array_literal_as_poly
28308+ # only walks ArrayNodes — emit a runtime conversion that
28309+ # iterates the typed PtrArray and tags each inner with
28310+ # its real cls_id (sp_box_int_array etc.).
28311+ val = compile_expr(elems[k])
28312+ inner_t = ptr_array_elem_type(et)
28313+ conv = new_temp
28314+ ii = new_temp
28315+ emit(" sp_PolyArray *" + conv + " = sp_PolyArray_new();")
28316+ emit(" for (mrb_int " + ii + " = 0; " + ii + " < sp_PtrArray_length(" + val + "); " + ii + "++) {")
28317+ inner_get = "(" + c_type(inner_t) + ")sp_PtrArray_get(" + val + ", " + ii + ")"
28318+ emit(" sp_PolyArray_push(" + conv + ", " + box_value_to_poly(inner_t, inner_get) + ");")
28319+ emit(" }")
28320+ emit(" sp_PolyArray_push(" + tmp + ", sp_box_poly_array(" + conv + "));")
2827228321 else
2827328322 val = compile_expr(elems[k])
2827428323 emit(" sp_PolyArray_push(" + tmp + ", " + box_value_to_poly(et, val) + ");")
@@ -28978,6 +29027,22 @@
2897829027 if @nd_type[eid] == "ArrayNode" && (is_ptr_array_type(et) == 1 || et == "poly_array")
2897929028 inner = compile_array_literal_as_poly(eid)
2898029029 emit(" sp_PolyArray_push(" + tmp_arr + ", sp_box_poly_array(" + inner + "));")
29030+ elsif is_ptr_array_type(et) == 1
29031+ # Same 3D+ erasure but the element is a non-ArrayNode
29032+ # expression (e.g. `Array.new(N) { ... }` returning a
29033+ # typed `<X>_ptr_array`). Convert at runtime so each
29034+ # inner element keeps its real cls_id —
29035+ # `arr[b][i][j]` chains correctly.
29036+ ev = compile_expr(eid)
29037+ inner_t_pa = ptr_array_elem_type(et)
29038+ conv_pa = new_temp
29039+ ii_pa = new_temp
29040+ emit(" sp_PolyArray *" + conv_pa + " = sp_PolyArray_new();")
29041+ emit(" for (mrb_int " + ii_pa + " = 0; " + ii_pa + " < sp_PtrArray_length(" + ev + "); " + ii_pa + "++) {")
29042+ inner_get_pa = "(" + c_type(inner_t_pa) + ")sp_PtrArray_get(" + ev + ", " + ii_pa + ")"
29043+ emit(" sp_PolyArray_push(" + conv_pa + ", " + box_value_to_poly(inner_t_pa, inner_get_pa) + ");")
29044+ emit(" }")
29045+ emit(" sp_PolyArray_push(" + tmp_arr + ", sp_box_poly_array(" + conv_pa + "));")
2898129046 else
2898229047 ev = compile_expr(eid)
2898329048 ebox = et == "poly" ? ev : box_value_to_poly(et, ev)