Commit 9153a62f
9153a62f417cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #366 from mame/pr/empty-array-deferred-elem
fix(codegen): empty-array deferred element type + poly RHS multi-write destructure
Details
- Commit
- 9153a62f417c
- Parent
- 36f1c6d4f7e9
- Merge parent
- 44718b9fb969
spinel_codegen.rbmodifiedView file
@@ -4519,6 +4519,17 @@
45194519
if body_an >= 045204520
stmts_an = get_stmts(body_an)45214521
if stmts_an.length > 04522+
# `[]` / `[].dup` block tail: the inner element type4523+
# is statically ambiguous. Use poly_array (a4524+
# PolyArray-of-PolyArray) for the result so later4525+
# pushes of pointer-typed values (3-tuples,4526+
# IntArrays, etc.) survive — the sp_poly_shl runtime4527+
# dispatch handles any push kind via cls_id.4528+
if is_empty_array_or_dup(stmts_an.last) == 14529+
@needs_rb_value = 14530+
@needs_gc = 14531+
return "poly_array"4532+
end45224533
bret = infer_type(stmts_an.last)45234534
if bret == "string"45244535
return "str_array"@@ -5340,6 +5351,35 @@
53405351
053415352
end53425353
5354+
# `[]` / `[].dup` — an empty array whose static element type is5355+
# ambiguous. When this is the tail of a block whose result becomes5356+
# the inner storage of a nested array (`Array.new(N) { [].dup }`,5357+
# `(0..N).map { [].dup }`), defaulting to `int_array` is unsafe:5358+
# later pushes of pointer-typed values (3-tuples, IntArrays, etc.)5359+
# silently truncate the pointer to mrb_int. Use `poly_array` for5360+
# the inner container instead so push goes through sp_PolyArray5361+
# (with sp_RbVal slots) and the runtime cls_id dispatch in5362+
# sp_poly_shl handles any pushed kind correctly.5363+
def is_empty_array_or_dup(nid)5364+
if is_empty_array_literal(nid) == 15365+
return 15366+
end5367+
if nid < 05368+
return 05369+
end5370+
if @nd_type[nid] != "CallNode"5371+
return 05372+
end5373+
if @nd_name[nid] != "dup"5374+
return 05375+
end5376+
recv = @nd_receiver[nid]5377+
if recv < 05378+
return 05379+
end5380+
is_empty_array_literal(recv)5381+
end5382+
53435383
# `[nil] * N` / `[0] * N` is a sized empty default — the elements53445384
# come from `nil`/`0`, so the array's effective element type is the53455385
# same as `[]`'s default (int_array). Used by writer-scan and the@@ -8337,6 +8377,13 @@
83378377
if body_an2 >= 083388378
stmts_an2 = get_stmts(body_an2)83398379
if stmts_an2.length > 08380+
# `[]` / `[].dup` block tail → poly_array (see8381+
# infer_constructor_type for rationale).8382+
if is_empty_array_or_dup(stmts_an2.last) == 18383+
@needs_rb_value = 18384+
@needs_gc = 18385+
return "poly_array"8386+
end83408387
bret2 = infer_type(stmts_an2.last)83418388
if bret2 == "string"83428389
return "str_array"@@ -23844,10 +23891,21 @@
2384423891
arrnew_count = compile_expr(arrnew_aargs.first)2384523892
arrnew_bp = get_block_param(nid, 0)2384623893
block_ret_an = "int"23894+
arrnew_empty_inner = 02384723895
if arrnew_body >= 02384823896
arrnew_pre_stmts = get_stmts(arrnew_body)2384923897
if arrnew_pre_stmts.length > 023850-
block_ret_an = infer_type(arrnew_pre_stmts.last)23898+
# `[]` / `[].dup` block tail: emit each inner as a fresh23899+
# poly_array so future pushes (with sp_poly_shl runtime23900+
# cls_id dispatch) handle any pushed kind. This is the23901+
# "deferred element type" path — the static type is23902+
# `poly_array_ptr_array`.23903+
if is_empty_array_or_dup(arrnew_pre_stmts.last) == 123904+
arrnew_empty_inner = 123905+
block_ret_an = "poly_array"23906+
else23907+
block_ret_an = infer_type(arrnew_pre_stmts.last)23908+
end2385123909
end2385223910
end2385323911
an_ret_is_arr = (block_ret_an == "int_array" || block_ret_an == "float_array" || block_ret_an == "str_array" || block_ret_an == "sym_array")@@ -23890,19 +23948,27 @@
2389023948
compile_stmt(arrnew_stmts2[arrnew_k])2389123949
arrnew_k = arrnew_k + 12389223950
end23893-
arrnew_lastv = compile_expr(arrnew_stmts2.last)23894-
if block_ret_an == "string"23895-
emit(" sp_StrArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23896-
elsif block_ret_an == "float"23897-
emit(" sp_FloatArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23898-
elsif block_ret_an == "poly"23899-
emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23900-
elsif an_ret_is_deep_arr23901-
emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + box_value_to_poly(block_ret_an, arrnew_lastv) + ");")23902-
elsif an_ret_is_arr23903-
emit(" sp_PtrArray_push(" + arrnew_tmp + ", (void *)(" + arrnew_lastv + "));")23951+
if arrnew_empty_inner == 123952+
# Skip compiling the `[]` / `[].dup` tail entirely —23953+
# we know it's an empty array; emit a fresh23954+
# sp_PolyArray instead so each inner is allocated as23955+
# the deferred-type container.23956+
emit(" sp_PolyArray_push(" + arrnew_tmp + ", sp_box_poly_array(sp_PolyArray_new()));")2390423957
else23905-
emit(" sp_IntArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23958+
arrnew_lastv = compile_expr(arrnew_stmts2.last)23959+
if block_ret_an == "string"23960+
emit(" sp_StrArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23961+
elsif block_ret_an == "float"23962+
emit(" sp_FloatArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23963+
elsif block_ret_an == "poly"23964+
emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23965+
elsif an_ret_is_deep_arr23966+
emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + box_value_to_poly(block_ret_an, arrnew_lastv) + ");")23967+
elsif an_ret_is_arr23968+
emit(" sp_PtrArray_push(" + arrnew_tmp + ", (void *)(" + arrnew_lastv + "));")23969+
else23970+
emit(" sp_IntArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23971+
end2390623972
end2390723973
end2390823974
end@@ -27984,6 +28050,52 @@
2798428050
# Box an already-compiled value of static type `at` into an sp_RbVal.2798528051
# Mirrors box_expr_to_poly but operates on a raw (type, value) pair so2798628052
# callers that already have temps don't have to re-emit the expr.28053+
# Unbox an sp_RbVal expression `val` into the C representation28054+
# of `at`. Used at sites where the destructure / cls_id-aware28055+
# dispatch produced an sp_RbVal but the consumer slot is a28056+
# concrete C type (mrb_int, sp_IntArray *, const char *, ...).28057+
# No runtime cls_id check — caller has already narrowed via28058+
# static type / wrapping cls_id dispatch.28059+
def unbox_poly_to(at, val)28060+
base = is_nullable_type(at) == 1 ? base_type(at) : at28061+
if base == "int" || base == "bool" || base == "nil"28062+
return "(" + val + ").v.i"28063+
end28064+
if base == "float"28065+
return "(" + val + ").v.f"28066+
end28067+
if base == "string"28068+
return "(const char *)(" + val + ").v.p"28069+
end28070+
if base == "symbol"28071+
return "(sp_sym)(" + val + ").v.i"28072+
end28073+
if base == "int_array"28074+
return "(sp_IntArray *)(" + val + ").v.p"28075+
end28076+
if base == "float_array"28077+
return "(sp_FloatArray *)(" + val + ").v.p"28078+
end28079+
if base == "str_array"28080+
return "(sp_StrArray *)(" + val + ").v.p"28081+
end28082+
if base == "sym_array"28083+
return "(sp_IntArray *)(" + val + ").v.p"28084+
end28085+
if base == "poly_array"28086+
return "(sp_PolyArray *)(" + val + ").v.p"28087+
end28088+
if is_ptr_array_type(base) == 128089+
return "(sp_PtrArray *)(" + val + ").v.p"28090+
end28091+
if is_obj_type(base) == 128092+
cname = base[4, base.length - 4]28093+
return "(sp_" + cname + " *)(" + val + ").v.p"28094+
end28095+
# Fallback: cast generic pointer.28096+
"(void *)(" + val + ").v.p"28097+
end28098+
2798728099
def box_value_to_poly(at, val)2798828100
nullable = is_nullable_type(at)2798928101
raw_at = at@@ -30447,6 +30559,12 @@
3044730559
if vt == "poly" && value_type != "" && value_type != "poly"3044830560
v = box_value_to_poly(value_type, value_expr)3044930561
end30562+
# Reverse direction: poly RHS into a concrete-typed local30563+
# slot. Happens when destructuring a poly_array element30564+
# (each slot was sp_PolyArray_get, returning sp_RbVal).30565+
if value_type == "poly" && vt != "" && vt != "poly"30566+
v = unbox_poly_to(vt, value_expr)30567+
end3045030568
emit(" " + fiber_var_ref(lname) + " = " + v + ";")3045130569
return3045230570
end@@ -30475,6 +30593,9 @@
3047530593
if it == "poly" && value_type != "" && value_type != "poly"3047630594
v = box_value_to_poly(value_type, value_expr)3047730595
end30596+
if value_type == "poly" && it != "" && it != "poly"30597+
v = unbox_poly_to(it, value_expr)30598+
end3047830599
end3047930600
emit(" " + self_arrow + sanitize_ivar(iname) + " = " + v + ";")3048030601
end@@ -30827,6 +30948,16 @@
3082730948
if val_t_local == "poly_array"3082830949
@needs_rb_value = 13082930950
emit(" sp_PolyArray *" + tmp + " = " + compile_expr(val_id) + ";")30951+
elsif val_t_local == "poly"30952+
# RHS is a poly value (e.g. `@attr_lut[i]` where @attr_lut30953+
# is a poly_array of inner arrays — each element is a30954+
# boxed sp_PolyArray with cls_id POLY_ARRAY). Unbox to30955+
# sp_PolyArray * and destructure via sp_PolyArray_get.30956+
# Same shape as the static poly_array branch above.30957+
@needs_rb_value = 130958+
rb_tmp_mw = new_temp30959+
emit(" sp_RbVal " + rb_tmp_mw + " = " + compile_expr(val_id) + ";")30960+
emit(" sp_PolyArray *" + tmp + " = (" + rb_tmp_mw + ".cls_id == SP_BUILTIN_POLY_ARRAY) ? (sp_PolyArray *)" + rb_tmp_mw + ".v.p : NULL;")3083030961
else3083130962
@needs_int_array = 13083230963
emit(" sp_IntArray *" + tmp + " = " + compile_expr(val_id) + ";")@@ -30835,7 +30966,7 @@
3083530966
k = 03083630967
while k < targets.length3083730968
tid = targets[k]30838-
if val_t_local == "poly_array"30969+
if val_t_local == "poly_array" || val_t_local == "poly"3083930970
rhs = "sp_PolyArray_get(" + tmp + ", " + k.to_s + ")"3084030971
val_t_for_target = "poly"3084130972
elsetest/array_new_empty_inner_deferred.rbaddedView file
Loading diff…
test/array_new_empty_inner_deferred.rb.expectedaddedView file
Loading diff…