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:

  1. @arr[i] = val on a ptr_array ivar emits sp_PtrArray_set; previously the path was unhandled and the write was silently dropped.

  2. @arr = [nil] * N (or [0] * N) when the LHS slot has already been promoted to ptr_array now emits a fresh sp_PtrArray pre-filled with N NULLs, instead of the default sp_IntArray that would type-mismatch the slot. The same [nil] * N reseed is recognised in three emit sites: compile_typed_call_args, compile_stmt's IVW path, and emit_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

@@ -4267,6 +4267,40 @@
42674267 0
42684268 end
42694269
4270+ # `[nil] * N` / `[0] * N` is a sized empty default — the elements
4271+ # come from `nil`/`0`, so the array's effective element type is the
4272+ # same as `[]`'s default (int_array). Used by writer-scan and the
4273+ # expected-type-aware compile path so a later typed `arr[i] = obj`
4274+ # write can promote / direct-allocate the right kind of container.
4275+ def is_sized_empty_array_default(nid)
4276+ if nid < 0
4277+ return 0
4278+ end
4279+ if @nd_type[nid] != "CallNode" || @nd_name[nid] != "*"
4280+ return 0
4281+ end
4282+ arr_recv = @nd_receiver[nid]
4283+ if arr_recv < 0 || @nd_type[arr_recv] != "ArrayNode"
4284+ return 0
4285+ end
4286+ recv_elems = parse_id_list(@nd_elements[arr_recv])
4287+ if recv_elems.length == 0
4288+ return 0
4289+ end
4290+ ei = 0
4291+ while ei < recv_elems.length
4292+ ti = recv_elems[ei]
4293+ if ti < 0
4294+ return 0
4295+ end
4296+ if @nd_type[ti] != "NilNode" && @nd_type[ti] != "IntegerNode"
4297+ return 0
4298+ end
4299+ ei = ei + 1
4300+ end
4301+ 1
4302+ end
4303+
42704304 def base_type(t)
42714305 if t.length > 1 && t[t.length - 1] == "?"
42724306 return t[0, t.length - 1]
@@ -7404,6 +7438,25 @@
74047438 return val
74057439 end
74067440 end
7441+ # `[nil] * N` / `[0] * N` against a pointer-array LHS: build a
7442+ # fresh ptr_array pre-filled with N NULLs so subsequent
7443+ # `arr[i] = obj` writes via sp_PtrArray_set land in valid slots.
7444+ # Without this, the default `*` codegen produces an int_array
7445+ # which doesn't match the LHS storage.
7446+ if (is_ptr_array_type(expected_base) == 1 || expected_base == "poly_array") && is_sized_empty_array_default(nid) == 1
7447+ @needs_gc = 1
7448+ cnt_e = compile_arg0(nid)
7449+ tmp = new_temp
7450+ if expected_base == "poly_array"
7451+ @needs_rb_value = 1
7452+ emit(" sp_PolyArray *" + tmp + " = sp_PolyArray_new();")
7453+ emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PolyArray_push(" + tmp + ", sp_box_nil()); }")
7454+ else
7455+ emit(" sp_PtrArray *" + tmp + " = sp_PtrArray_new();")
7456+ emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp + ", NULL); }")
7457+ end
7458+ return tmp
7459+ end
74077460 val = compile_expr(nid)
74087461 at = infer_type(nid)
74097462 if expected_base == "poly" && at != "poly"
@@ -14238,6 +14291,17 @@
1423814291 if iv_ctor != ""
1423914292 @needs_gc = 1
1424014293 emit_raw(" " + self_arrow + ivar + " = " + iv_ctor + ";")
14294+ elsif is_ptr_array_type(ivt) == 1 && is_sized_empty_array_default(expr_id_iv) == 1
14295+ # `@arr = [nil] * N` with @arr already widened to
14296+ # ptr_array via writer-scan. The default `*` codegen
14297+ # produces an sp_IntArray that mismatches the slot
14298+ # type; emit a sized PtrArray of NULLs inline.
14299+ @needs_gc = 1
14300+ cnt_e_iv = compile_arg0(expr_id_iv)
14301+ tmp_iv = new_temp
14302+ emit_raw(" sp_PtrArray *" + tmp_iv + " = sp_PtrArray_new();")
14303+ emit_raw(" { mrb_int _n = " + cnt_e_iv + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_iv + ", NULL); }")
14304+ emit_raw(" " + self_arrow + ivar + " = " + tmp_iv + ";")
1424114305 else
1424214306 # Issue #130: same poly-slot boxing as the general
1424314307 # InstanceVariableWriteNode emit path (compile_expr branch).
@@ -23735,6 +23799,20 @@
2373523799 return
2373623800 end
2373723801 end
23802+ # `@arr = [nil] * N` where @arr is a ptr_array slot. The
23803+ # default `*` codegen produces an sp_IntArray, which mismatches
23804+ # the slot type and corrupts later sp_PtrArray_set calls. Emit
23805+ # a sized PtrArray pre-filled with NULLs inline so the slot
23806+ # holds the matching storage.
23807+ if is_ptr_array_type(ivt) == 1 && is_sized_empty_array_default(expr_id) == 1
23808+ @needs_gc = 1
23809+ cnt_e = compile_arg0(expr_id)
23810+ tmp_arr = new_temp
23811+ emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")
23812+ emit(" { mrb_int _n = " + cnt_e + "; for (mrb_int _i = 0; _i < _n; _i++) sp_PtrArray_push(" + tmp_arr + ", NULL); }")
23813+ emit(" " + self_arrow + sanitize_ivar(iname) + " = " + tmp_arr + ";")
23814+ return
23815+ end
2373823816 # Issue #130: poly slot — every concrete-typed RHS must be boxed
2373923817 # to sp_RbVal. Without the box, C compiler sees `sp_RbVal = mrb_int`
2374023818 # (or const char *, etc.) and either rejects the assignment or
@@ -27219,6 +27297,10 @@
2721927297 emit(" sp_StrArray_set(" + rc + ", " + idx + ", " + val + ");")
2722027298 return
2722127299 end
27300+ if is_ptr_array_type(rt) == 1
27301+ emit(" sp_PtrArray_set(" + rc + ", " + idx + ", (void *)" + val + ");")
27302+ return
27303+ end
2722227304 if rt == "str_int_hash"
2722327305 emit(" sp_StrIntHash_set(" + rc + ", " + idx + ", " + val + ");")
2722427306 return