Commit 1cf6a187

1cf6a187efa1Merged

Yusuke Endoh <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): infer _ptr_array for arr.map { array_block }

When the block of <typed_array>.map { ... } itself returns a typed array (int_array/float_array/str_array/sym_array), the outer map's result is an array of arrays — type as <inner>_ptr_array so each element keeps its concrete shape and GC sees a real pointer per slot.

Only fires for array-shaped recv (where the elements have a definite shape). For range/int recv, fall through to the int_array placeholder so a follow-up int-typed assignment to the same slot still type-checks (the recv's elements aren't yet known to be array-shaped).

Optcarrot trigger: [1, 6].map { |i| (0..n).map { ... } } — each outer element is itself an int_array.

Details

Commit
1cf6a187efa1
Parent
60f1b75ea8d0

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