Commit 858f8fac

858f8facc9a5Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Merge pull request #165 from mame/pr/map-non-array-recv-returns-array

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

Details

Commit
858f8facc9a5
Parent
cc56bd446ad2
Merge parent
6cef5f84c373

spinel_codegen.rbmodifiedView file

@@ -2887,10 +2887,23 @@
28872887 if is_obj_type(bret) == 1
28882888 return bret + "_ptr_array"
28892889 end
2890+ # Block returns a non-trivial type (an array literal,
2891+ # poly value, ...). The map's overall result is still an
2892+ # Array — fall through to the recv-based default below
2893+ # only when recv is already array-shaped, otherwise
2894+ # return int_array as a generic placeholder.
28902895 end
28912896 end
28922897 end
2893- return infer_type(recv)
2898+ rt_recv = infer_type(recv)
2899+ # Range#map / Integer#step.map / non-array recv → result is
2900+ # an Array, not a Range/IntArray. Without this, an
2901+ # `@x = (0...n).map {...}` recorded the ivar as `range` and
2902+ # later `@x = something_else` writes failed to type-check.
2903+ if rt_recv == "range" || rt_recv == "int"
2904+ return "int_array"
2905+ end
2906+ return rt_recv
28942907 end
28952908 return "int_array"
28962909 end