Commit 58a4b5bf

58a4b5bfd49eMerged

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

Description

Merge pull request #262 from mame/pr/poly-array-slot-array-literal

fix(codegen): poly_array slot consumes ArrayNode literal via PolyArray

Details

Commit
58a4b5bfd49e
Parent
b5eafce33982
Merge parent
94c29ab5b72f

spinel_codegen.rbmodifiedView file

@@ -15060,6 +15060,27 @@
1506015060 emit_raw(" sp_PtrArray *" + tmp_iv + " = sp_PtrArray_new();")
1506115061 emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_iv + ", NULL); }")
1506215062 emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";")
15063+ elsif ivt == "poly_array" && @nd_type[expr_id_iv] == "ArrayNode"
15064+ # Non-empty array literal `[a, b, ...]` going into a
15065+ # poly_array slot. compile_array_literal infers a typed
15066+ # storage (ptr_array of one class for homogeneous obj
15067+ # literals, etc.) — emit a fresh PolyArray with each
15068+ # element boxed instead.
15069+ @needs_gc = 1
15070+ @needs_rb_value = 1
15071+ tmp_iv = new_temp
15072+ emit_raw(" sp_PolyArray *" + tmp_iv + " = sp_PolyArray_new();")
15073+ elems_iv = parse_id_list(@nd_elements[expr_id_iv])
15074+ ek = 0
15075+ while ek < elems_iv.length
15076+ eid = elems_iv[ek]
15077+ et = infer_type(eid)
15078+ ev = compile_expr(eid)
15079+ ebox = et == "poly" ? ev : box_value_to_poly(et, ev)
15080+ emit_raw(" sp_PolyArray_push(" + tmp_iv + ", " + ebox + ");")
15081+ ek = ek + 1
15082+ end
15083+ emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";")
1506315084 else
1506415085 # Issue #130: same poly-slot boxing as the general
1506515086 # InstanceVariableWriteNode emit path (compile_expr branch).
@@ -24749,6 +24770,30 @@
2474924770 emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";")
2475024771 return
2475124772 end
24773+ # Non-empty array literal `[a, b, ...]` going into a poly_array
24774+ # slot. compile_array_literal infers a typed array (ptr_array of
24775+ # one class for homogeneous obj literals, etc.) and emits the
24776+ # corresponding storage — but the slot was widened to poly_array,
24777+ # so a typed-array-pointer assignment would fail the C compile.
24778+ # Emit a fresh PolyArray with each element boxed to sp_RbVal.
24779+ if ivt == "poly_array" && @nd_type[expr_id] == "ArrayNode"
24780+ @needs_gc = 1
24781+ @needs_rb_value = 1
24782+ tmp_arr = new_temp
24783+ emit(" sp_PolyArray *" + tmp_arr + " = sp_PolyArray_new();")
24784+ elems_pa = parse_id_list(@nd_elements[expr_id])
24785+ ek = 0
24786+ while ek < elems_pa.length
24787+ eid = elems_pa[ek]
24788+ et = infer_type(eid)
24789+ ev = compile_expr(eid)
24790+ ebox = et == "poly" ? ev : box_value_to_poly(et, ev)
24791+ emit(" sp_PolyArray_push(" + tmp_arr + ", " + ebox + ");")
24792+ ek = ek + 1
24793+ end
24794+ emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";")
24795+ return
24796+ end
2475224797 # Issue #130: poly slot — every concrete-typed RHS must be boxed
2475324798 # to sp_RbVal. Without the box, C compiler sees `sp_RbVal = mrb_int`
2475424799 # (or const char *, etc.) and either rejects the assignment or