Commit 63fb62ec

63fb62ec4328Merged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): support Array#flat_map block returning ptr_array

When a flat_map block returned a ptr_array, the inference fell back to the receiver type and the codegen mismatched:

/tmp/_t.c:53:24: error: initialization of 'sp_PtrArray *' from incompatible pointer type 'sp_IntArray *'

Follow-up to the introduction of ptr_array (mirrors the earlier poly_array fix): flat_map's block-return recognition (in infer_call_type) and accumulator typing (in compile_flat_map_expr) were missed when the new type was added. The block-param type case also needed extending to ptr_array_elem_type(rt) so that |f|-style params land on the right obj type.

Adds test/array_flat_map_ptr.rb.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
63fb62ec4328
Parent
b75070d3abf9

spinel_codegen.rbmodifiedView file

@@ -2380,7 +2380,7 @@
23802380 if bbs.length > 0
23812381 bret = infer_type(bbs.last)
23822382 # If block returns an array type, use it as result type
2383- if is_array_type(bret) == 1
2383+ if is_array_type(bret) == 1 || is_ptr_array_type(bret) == 1
23842384 return bret
23852385 end
23862386 end
@@ -21790,7 +21790,7 @@
2179021790 end
2179121791 end
2179221792 # Fall back to receiver type if block doesn't return an array
21793- if is_array_type(block_ret) == 0
21793+ if is_array_type(block_ret) == 0 && is_ptr_array_type(block_ret) == 0
2179421794 block_ret = rt
2179521795 end
2179621796 @needs_gc = 1
@@ -21809,6 +21809,8 @@
2180921809 elem_type = "float"
2181021810 elsif rt == "poly_array"
2181121811 elem_type = "poly"
21812+ elsif is_ptr_array_type(rt) == 1
21813+ elem_type = ptr_array_elem_type(rt)
2181221814 end
2181321815 declare_var(bp1, elem_type)
2181421816 emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx_src + "_length(" + rc + "); " + tmp_i + "++) {")