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
if is_obj_type(bret) == 1 return bret + "_ptr_array" end # Block returns a non-trivial type (an array literal, # poly value, ...). The map's overall result is still an # Array — fall through to the recv-based default below # only when recv is already array-shaped, otherwise # return int_array as a generic placeholder. end end end return infer_type(recv) rt_recv = infer_type(recv) # Range#map / Integer#step.map / non-array recv → result is # an Array, not a Range/IntArray. Without this, an # `@x = (0...n).map {...}` recorded the ivar as `range` and # later `@x = something_else` writes failed to type-check. if rt_recv == "range" || rt_recv == "int" return "int_array" end return rt_recv end return "int_array" endtest/map_range_recv_array_block.rbaddedView file
Loading diff…