Commit 1073bb9e
1073bb9e7314Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #361 from mame/pr/array-new-block-typed-container
fix(codegen): preserve cls_id through poly_array literal w/ typed-ptr-array CallNode elem
Details
- Commit
- 1073bb9e7314
- Parent
- 0a69b225ac5d
- Merge parent
- e369fb8640e3
spinel_codegen.rbmodifiedView file
@@ -4461,6 +4461,42 @@
44614461
rn = constructor_class_name(recv)44624462
if rn != ""44634463
if rn == "Array"4464+
# Block form `Array.new(n) { ... }` — infer the container4465+
# from the block's tail expression, same shape as the4466+
# compile_constructor_expr emit. Without this branch4467+
# ivar widening saw `int_array` and the actual emit's4468+
# PtrArray / PolyArray clashed.4469+
blk_an = @nd_block[nid]4470+
if blk_an >= 04471+
body_an = @nd_body[blk_an]4472+
if body_an >= 04473+
stmts_an = get_stmts(body_an)4474+
if stmts_an.length > 04475+
bret = infer_type(stmts_an.last)4476+
if bret == "string"4477+
return "str_array"4478+
end4479+
if bret == "float"4480+
return "float_array"4481+
end4482+
if bret == "symbol"4483+
return "sym_array"4484+
end4485+
if bret == "poly"4486+
@needs_rb_value = 14487+
return "poly_array"4488+
end4489+
if is_ptr_array_type(bret) == 1 || bret == "poly_array"4490+
@needs_rb_value = 14491+
return "poly_array"4492+
end4493+
if bret == "int_array" || bret == "float_array" || bret == "str_array" || bret == "sym_array"4494+
@needs_gc = 14495+
return bret + "_ptr_array"4496+
end4497+
end4498+
end4499+
end44644500
# Check fill value type. Pointer-type fills must produce a typed44654501
# PtrArray; falling through to int_array would leave the44664502
# elements unscanned by GC.@@ -8227,6 +8263,51 @@
82278263
rname = constructor_class_name(r)82288264
if rname != ""82298265
if rname == "Array"8266+
# Block form `Array.new(n) { ... }` — infer container8267+
# from the block tail (matches compile_constructor_expr).8268+
# When the block tail's type can't be resolved at this8269+
# pre-compile pass (a LocalVariableReadNode whose write8270+
# is in the same block, e.g. `_a = [0]; _a.clear; _a`,8271+
# hasn't been declare_var'd yet — find_var_type returns8272+
# ""), fall through to the placeholder "int" path so8273+
# the later compile pass's update_ivar_type can widen8274+
# int → concrete-container without going through the8275+
# array+array → poly_array widening.8276+
blk_an2 = @nd_block[nid]8277+
if blk_an2 >= 08278+
body_an2 = @nd_body[blk_an2]8279+
if body_an2 >= 08280+
stmts_an2 = get_stmts(body_an2)8281+
if stmts_an2.length > 08282+
bret2 = infer_type(stmts_an2.last)8283+
if bret2 == "string"8284+
return "str_array"8285+
end8286+
if bret2 == "float"8287+
return "float_array"8288+
end8289+
if bret2 == "symbol"8290+
return "sym_array"8291+
end8292+
if bret2 == "poly"8293+
@needs_rb_value = 18294+
return "poly_array"8295+
end8296+
if is_ptr_array_type(bret2) == 1 || bret2 == "poly_array"8297+
@needs_rb_value = 18298+
return "poly_array"8299+
end8300+
if bret2 == "int_array" || bret2 == "float_array" || bret2 == "str_array" || bret2 == "sym_array"8301+
@needs_gc = 18302+
return bret2 + "_ptr_array"8303+
end8304+
# bret2 == "int" or "" — leave the slot untyped so8305+
# the later compile pass can refine without8306+
# triggering update_ivar_type's array-widening.8307+
return "int"8308+
end8309+
end8310+
end82308311
# Check fill value type for Array.new(n, val).82318312
# Pointer-type fills must produce a typed PtrArray; falling82328313
# through to int_array would leave the elements unscanned by GC.@@ -23442,11 +23523,16 @@
2344223523
if cname == "Array"2344323524
@needs_gc = 12344423525
args_id = @nd_arguments[nid]23445-
# Array.new(n) { |i| ... } -- IntArray-only fast path. We don't23446-
# try to introspect the block body to pick a typed container23447-
# (calling infer_type from this dispatch perturbs the bootstrap;23448-
# see bug-11 commit). Float/String collectors must be built via23449-
# explicit `[]` + `N.times { ... << }` instead.23526+
# Array.new(n) { |i| ... } — pick the accumulator container23527+
# from the block's last-statement type, mirroring23528+
# compile_map_expr's range/typed-container branch. Without the23529+
# introspection, every block return collapsed into IntArray23530+
# (`Array.new(N) { [].dup }` ended up as an int_array of stale23531+
# pointers, freed by GC). The infer_type call here is on the23532+
# block body's tail expression — that recursion is bounded by23533+
# the AST and doesn't perturb the bootstrap (the original23534+
# comment about bug-11 came from a now-removed call site that23535+
# re-entered constructor inference).2345023536
if args_id >= 0 && @nd_block[nid] >= 02345123537
arrnew_aargs = get_args(args_id)2345223538
if arrnew_aargs.length >= 1@@ -23454,10 +23540,36 @@
2345423540
arrnew_body = @nd_body[arrnew_blk]2345523541
arrnew_count = compile_expr(arrnew_aargs.first)2345623542
arrnew_bp = get_block_param(nid, 0)23543+
block_ret_an = "int"23544+
if arrnew_body >= 023545+
arrnew_pre_stmts = get_stmts(arrnew_body)23546+
if arrnew_pre_stmts.length > 023547+
block_ret_an = infer_type(arrnew_pre_stmts.last)23548+
end23549+
end23550+
an_ret_is_arr = (block_ret_an == "int_array" || block_ret_an == "float_array" || block_ret_an == "str_array" || block_ret_an == "sym_array")23551+
an_ret_is_deep_arr = (is_ptr_array_type(block_ret_an) == 1 || block_ret_an == "poly_array")2345723552
arrnew_tmp = new_temp2345823553
arrnew_iv = new_temp23459-
@needs_int_array = 123460-
emit(" sp_IntArray *" + arrnew_tmp + " = sp_IntArray_new();")23554+
if block_ret_an == "string"23555+
@needs_str_array = 123556+
emit(" sp_StrArray *" + arrnew_tmp + " = sp_StrArray_new();")23557+
elsif block_ret_an == "float"23558+
@needs_float_array = 123559+
emit(" sp_FloatArray *" + arrnew_tmp + " = sp_FloatArray_new();")23560+
elsif block_ret_an == "poly"23561+
@needs_rb_value = 123562+
emit(" sp_PolyArray *" + arrnew_tmp + " = sp_PolyArray_new();")23563+
elsif an_ret_is_deep_arr23564+
@needs_rb_value = 123565+
emit(" sp_PolyArray *" + arrnew_tmp + " = sp_PolyArray_new();")23566+
elsif an_ret_is_arr23567+
@needs_ptr_array = 123568+
emit(" sp_PtrArray *" + arrnew_tmp + " = sp_PtrArray_new();")23569+
else23570+
@needs_int_array = 123571+
emit(" sp_IntArray *" + arrnew_tmp + " = sp_IntArray_new();")23572+
end2346123573
# Root the new array before running the block body — pushing2346223574
# poly/string values inside the loop can trigger a GC cycle2346323575
# that would otherwise sweep the local pointer.@@ -23476,7 +23588,19 @@
2347623588
arrnew_k = arrnew_k + 12347723589
end2347823590
arrnew_lastv = compile_expr(arrnew_stmts2.last)23479-
emit(" sp_IntArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23591+
if block_ret_an == "string"23592+
emit(" sp_StrArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23593+
elsif block_ret_an == "float"23594+
emit(" sp_FloatArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23595+
elsif block_ret_an == "poly"23596+
emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23597+
elsif an_ret_is_deep_arr23598+
emit(" sp_PolyArray_push(" + arrnew_tmp + ", " + box_value_to_poly(block_ret_an, arrnew_lastv) + ");")23599+
elsif an_ret_is_arr23600+
emit(" sp_PtrArray_push(" + arrnew_tmp + ", (void *)(" + arrnew_lastv + "));")23601+
else23602+
emit(" sp_IntArray_push(" + arrnew_tmp + ", " + arrnew_lastv + ");")23603+
end2348023604
end2348123605
end2348223606
@indent = @indent - 1test/array_new_block_typed_container.rbaddedView file
Loading diff…
test/array_new_block_typed_container.rb.expectedaddedView file
Loading diff…