Commit ff464f13
ff464f13ad05Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #232 from mame/pr/map-array-block-result
fix(codegen): arr.map { array_block } returns array-of-arrays
Details
- Commit
- ff464f13ad05
- Parent
- 5ff8b80dbb02
- Merge parent
- b13c605b20bd
spinel_codegen.rbmodifiedView file
@@ -3280,11 +3280,34 @@
32803280
if is_obj_type(bret) == 132813281
return bret + "_ptr_array"32823282
end3283-
# Block returns a non-trivial type (an array literal,3284-
# poly value, ...). The map's overall result is still an3285-
# Array — fall through to the recv-based default below3286-
# only when recv is already array-shaped, otherwise3287-
# return int_array as a generic placeholder.3283+
# Block returns an array (e.g.3284+
# `[1, 6].map { (0..n).map { ... } }` — each outer3285+
# element is itself an int_array). The outer map's3286+
# result is then an array of arrays, encoded as3287+
# `<inner>_ptr_array`. Only fire this for array-shaped3288+
# recv (where the elements have a definite shape and3289+
# ivar typing reaches the ptr_array placeholder); for3290+
# range/int recv we fall through to the int_array3291+
# placeholder below so a follow-up int-typed assignment3292+
# to the same ivar still type-checks.3293+
if bret == "int_array" || bret == "float_array" || bret == "str_array" || bret == "sym_array"3294+
rt_recv_p = infer_type(recv)3295+
if rt_recv_p != "range" && rt_recv_p != "int"3296+
return bret + "_ptr_array"3297+
end3298+
end3299+
# poly_array bret intentionally falls through. Returning3300+
# poly_array_ptr_array would be more accurate, but ivars3301+
# holding the result (and the corresponding `[nil] *3302+
# n` companions) often haven't been widened to the3303+
# ptr_array shape by the type-inference pass yet.3304+
# Letting it fall through to int_array preserves the3305+
# pre-fix typing that those companion ivars match.3306+
# Block returns a non-trivial type (poly value, etc.).3307+
# The map's overall result is still an Array — fall3308+
# through to the recv-based default below only when3309+
# recv is already array-shaped, otherwise return3310+
# int_array as a generic placeholder.32883311
end32893312
end32903313
end@@ -28451,6 +28474,7 @@
2845128474
end2845228475
@needs_int_array = 12845328476
@needs_gc = 128477+
ret_is_arr = false2845428478
if block_ret_r == "string"2845528479
@needs_str_array = 12845628480
emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")@@ -28614,7 +28638,18 @@
2861428638
pop_scope2861528639
return tmp_arr2861628640
else28617-
emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")28641+
# Block returning a homogeneous typed array (e.g.28642+
# `[1, 6].map { (0..n).map { ... } }`) — accumulator must28643+
# be a PtrArray so GC keeps each element alive (an IntArray28644+
# accumulator with pointer-payload mrb_ints would let the28645+
# inner arrays be collected).28646+
ret_is_arr2 = (block_ret == "int_array" || block_ret == "float_array" || block_ret == "str_array" || block_ret == "sym_array")28647+
if ret_is_arr228648+
@needs_ptr_array = 128649+
emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")28650+
else28651+
emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")28652+
end2861828653
emit(" SP_GC_ROOT(" + tmp_arr + ");")2861928654
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")2862028655
if bp_is_lambda == 1@@ -28630,9 +28665,13 @@
2863028665
if stmts3.length > 02863128666
last = stmts3[stmts3.length - 1]2863228667
val = compile_expr(last)28633-
emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")28668+
if ret_is_arr228669+
emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)(" + val + "));")28670+
else28671+
emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")28672+
end2863428673
else28635-
emit_map_default_push(tmp_arr, "int_array")28674+
emit_map_default_push(tmp_arr, ret_is_arr2 ? "ptr_array" : "int_array")2863628675
end2863728676
end2863828677
@indent = @indent - 1test/map_array_block_result.rbaddedView file
Loading diff…
test/map_array_block_result.rb.expectedaddedView file
Loading diff…