Commit e7a064e3

e7a064e33677Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Array#index returns poly (nil on miss) for int/str arrays

sp_Array_index_poly returns boxed nil when the element is absent, so arr.index(x).nil? and narrowing (return 0 if i.nil?; i + 1 via poly arithmetic) work. include? and float arrays are unchanged.

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

Details

Commit
e7a064e33677
Parent
d9c51f33f282

src/analyze.cmodifiedView file

@@ -140,6 +140,8 @@
140140 return rt;
141141 }
142142 if (!strcmp(name, "[]")) return ty_array_elem(rt);
143+ /* index returns nil on a miss -> poly (int-or-nil) */
144+ if (!strcmp(name, "index") && (rt == TY_INT_ARRAY || rt == TY_STR_ARRAY)) return TY_POLY;
143145 if (!strcmp(name, "length") || !strcmp(name, "size") ||
144146 !strcmp(name, "count") || !strcmp(name, "index")) return TY_INT;
145147 if (!strcmp(name, "sum")) return ty_array_elem(rt);