Commit 631e29b0
631e29b0589eMerged
Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
feat(codegen): []= and sized init for ptr_array ivars
Two codegen pieces for ivars promoted to <obj>_ptr_array:
-
@arr[i] = valon a ptr_array ivar emitssp_PtrArray_set; previously the path was unhandled and the write was silently dropped. -
@arr = [nil] * N(or[0] * N) when the LHS slot has already been promoted to ptr_array now emits a freshsp_PtrArraypre-filled with N NULLs, instead of the defaultsp_IntArraythat would type-mismatch the slot. The same[nil] * Nreseed is recognised in three emit sites:compile_typed_call_args,compile_stmt's IVW path, andemit_constructor's in-initialize IVW path.
New helper: is_sized_empty_array_default recognises the
[nil] * N / [0] * N shape so both writer-scan widening
and the expected-type-aware compile path can treat it as an
empty default rather than a literal int_array assertion.
Test (test/ptr_array_set_widening.rb): @arr = [nil] * 4 +
heterogeneous obj writes via []= round-trip with mutation
through the slot to verify the read returns a live reference,
not a value copy.
Details
- Commit
- 631e29b0589e
- Parent
- 5dcdfe2e9687
spinel_codegen.rbmodifiedView file
0 end # `[nil] * N` / `[0] * N` is a sized empty default — the elements # come from `nil`/`0`, so the array's effective element type is the # same as `[]`'s default (int_array). Used by writer-scan and the # expected-type-aware compile path so a later typed `arr[i] = obj` # write can promote / direct-allocate the right kind of container. def is_sized_empty_array_default(nid) if nid < 0 return 0 end if @nd_type[nid] != "CallNode" || @nd_name[nid] != "*" return 0 end arr_recv = @nd_receiver[nid] if arr_recv < 0 || @nd_type[arr_recv] != "ArrayNode" return 0 end recv_elems = parse_id_list(@nd_elements[arr_recv]) if recv_elems.length == 0 return 0 end ei = 0 while ei < recv_elems.length ti = recv_elems[ei] if ti < 0 return 0 end if @nd_type[ti] != "NilNode" && @nd_type[ti] != "IntegerNode" return 0 end ei = ei + 1 end 1 end def base_type(t) if t.length > 1 && t[t.length - 1] == "?" return t[0, t.length - 1] return val end end # `[nil] * N` / `[0] * N` against a pointer-array LHS: build a # fresh ptr_array pre-filled with N NULLs so subsequent # `arr[i] = obj` writes via sp_PtrArray_set land in valid slots. # Without this, the default `*` codegen produces an int_array # which doesn't match the LHS storage. if (is_ptr_array_type(expected_base) == 1 || expected_base == "poly_array") && is_sized_empty_array_default(nid) == 1 @needs_gc = 1 cnt_e = compile_arg0(nid) tmp = new_temp if expected_base == "poly_array" @needs_rb_value = 1 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()); }") else emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();") emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp + ", NULL); }") end return tmp end val = compile_expr(nid) at = infer_type(nid) if expected_base == "poly" && at != "poly" if iv_ctor != "" @needs_gc = 1 emit_raw(" " + self_arrow + ivar + " = " + iv_ctor + ";") elsif is_ptr_array_type(ivt) == 1 && is_sized_empty_array_default(expr_id_iv) == 1 # `@arr = [nil] * N` with @arr already widened to # ptr_array via writer-scan. The default `*` codegen # produces an sp_IntArray that mismatches the slot # type; emit a sized PtrArray of NULLs inline. @needs_gc = 1 cnt_e_iv = compile_arg0(expr_id_iv) tmp_iv = new_temp emit_raw(" sp_PtrArray *" + tmp_iv + " = sp_PtrArray_new();") emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_iv + ", NULL); }") emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";") else # Issue #130: same poly-slot boxing as the general # InstanceVariableWriteNode emit path (compile_expr branch). return end end # `@arr = [nil] * N` where @arr is a ptr_array slot. The # default `*` codegen produces an sp_IntArray, which mismatches # the slot type and corrupts later sp_PtrArray_set calls. Emit # a sized PtrArray pre-filled with NULLs inline so the slot # holds the matching storage. if is_ptr_array_type(ivt) == 1 && is_sized_empty_array_default(expr_id) == 1 @needs_gc = 1 cnt_e = compile_arg0(expr_id) tmp_arr = new_temp emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();") emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_arr + ", NULL); }") emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";") return end # Issue #130: poly slot — every concrete-typed RHS must be boxed # to sp_RbVal. Without the box, C compiler sees `sp_RbVal = mrb_int` # (or const char *, etc.) and either rejects the assignment or emit(" sp_StrArray_set(" + rc + ", " + idx + ", " + val + ");") return end if is_ptr_array_type(rt) == 1 emit(" sp_PtrArray_set(" + rc + ", " + idx + ", (void *)" + val + ");") return end if rt == "str_int_hash" emit(" sp_StrIntHash_set(" + rc + ", " + idx + ", " + val + ");") returntest/ptr_array_set_widening.rbaddedView file
Loading diff…
test/ptr_array_set_widening.rb.expectedaddedView file
Loading diff…