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) == 1
32813281 return bret + "_ptr_array"
32823282 end
3283- # Block returns a non-trivial type (an array literal,
3284- # poly value, ...). The map's overall result is still an
3285- # Array — fall through to the recv-based default below
3286- # only when recv is already array-shaped, otherwise
3287- # return int_array as a generic placeholder.
3283+ # Block returns an array (e.g.
3284+ # `[1, 6].map { (0..n).map { ... } }` — each outer
3285+ # element is itself an int_array). The outer map's
3286+ # result is then an array of arrays, encoded as
3287+ # `<inner>_ptr_array`. Only fire this for array-shaped
3288+ # recv (where the elements have a definite shape and
3289+ # ivar typing reaches the ptr_array placeholder); for
3290+ # range/int recv we fall through to the int_array
3291+ # placeholder below so a follow-up int-typed assignment
3292+ # 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+ end
3298+ end
3299+ # poly_array bret intentionally falls through. Returning
3300+ # poly_array_ptr_array would be more accurate, but ivars
3301+ # holding the result (and the corresponding `[nil] *
3302+ # n` companions) often haven't been widened to the
3303+ # ptr_array shape by the type-inference pass yet.
3304+ # Letting it fall through to int_array preserves the
3305+ # 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 — fall
3308+ # through to the recv-based default below only when
3309+ # recv is already array-shaped, otherwise return
3310+ # int_array as a generic placeholder.
32883311 end
32893312 end
32903313 end
@@ -28451,6 +28474,7 @@
2845128474 end
2845228475 @needs_int_array = 1
2845328476 @needs_gc = 1
28477+ ret_is_arr = false
2845428478 if block_ret_r == "string"
2845528479 @needs_str_array = 1
2845628480 emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
@@ -28614,7 +28638,18 @@
2861428638 pop_scope
2861528639 return tmp_arr
2861628640 else
28617- 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 must
28643+ # be a PtrArray so GC keeps each element alive (an IntArray
28644+ # accumulator with pointer-payload mrb_ints would let the
28645+ # 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_arr2
28648+ @needs_ptr_array = 1
28649+ emit(" sp_PtrArray *" + tmp_arr + " = sp_PtrArray_new();")
28650+ else
28651+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
28652+ end
2861828653 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 > 0
2863128666 last = stmts3[stmts3.length - 1]
2863228667 val = compile_expr(last)
28633- emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")
28668+ if ret_is_arr2
28669+ emit(" sp_PtrArray_push(" + tmp_arr + ", (void *)(" + val + "));")
28670+ else
28671+ emit(" sp_IntArray_push(" + tmp_arr + ", " + val + ");")
28672+ end
2863428673 else
28635- emit_map_default_push(tmp_arr, "int_array")
28674+ emit_map_default_push(tmp_arr, ret_is_arr2 ? "ptr_array" : "int_array")
2863628675 end
2863728676 end
2863828677 @indent = @indent - 1