Commit 6cef5f84

6cef5f84c373Merged

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

Description

fix(codegen): infer array-returning result for non-array recv on #map

`infer_method_name_type` for `map` consulted the block's return type for the standard cases (string → str_array, int → int_array, etc.) and otherwise fell through to `infer_type(recv)`. That's right when recv is itself an Array (e.g. `[1,2,3].map { foo }` → the recv's array type carries through), but wrong when the recv is a Range or other non-array enumerable: `(0...264).map { [...] }` came back as `range`, the ivar holding it was typed sp_Range, and the subsequent assignment site emitted `self->iv_x = 0` against a sp_Range field.

Treat range / int recv as array-returning. The exact element type is unknown when the block returns an array literal or other non-trivial shape, so use int_array as a generic placeholder — better than poisoning the ivar with the recv's type.

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

Details

Commit
6cef5f84c373
Parent
bee7e89d5b64

spinel_codegen.rbmodifiedView file

@@ -2855,10 +2855,23 @@
28552855 if is_obj_type(bret) == 1
28562856 return bret + "_ptr_array"
28572857 end
2858+ # Block returns a non-trivial type (an array literal,
2859+ # poly value, ...). The map's overall result is still an
2860+ # Array — fall through to the recv-based default below
2861+ # only when recv is already array-shaped, otherwise
2862+ # return int_array as a generic placeholder.
28582863 end
28592864 end
28602865 end
2861- return infer_type(recv)
2866+ rt_recv = infer_type(recv)
2867+ # Range#map / Integer#step.map / non-array recv → result is
2868+ # an Array, not a Range/IntArray. Without this, an
2869+ # `@x = (0...n).map {...}` recorded the ivar as `range` and
2870+ # later `@x = something_else` writes failed to type-check.
2871+ if rt_recv == "range" || rt_recv == "int"
2872+ return "int_array"
2873+ end
2874+ return rt_recv
28622875 end
28632876 return "int_array"
28642877 end