Commit 5f29c311

5f29c311a19fMerged

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

Description

range.map/select, array[range]/[start,len] slicing, fix Range#last

  • range.map/select/reject materialize the range to an int array and reuse the array collect path; range iteration block params (map/select/ etc.) bind to int.
  • arr[a..b] / arr[start, len] return a subarray (sp_Array_slice_range/ slice); inference returns the array type for these forms.
  • Fix Range#last: it returns the end value (5 for 1...5), distinct from max (4). Previously last was conflated with max.

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

Details

Commit
5f29c311a19f
Parent
f7e208397dff

src/analyze.cmodifiedView file

@@ -257,7 +257,12 @@
257257 !strcmp(name, "take_while") || !strcmp(name, "drop_while"))
258258 return rt;
259259 }
260- if (!strcmp(name, "[]")) return ty_array_elem(rt);
260+ if (!strcmp(name, "[]")) {
261+ /* arr[range] / arr[start, len] -> a subarray; arr[i] -> an element */
262+ if (argc == 2) return rt;
263+ if (argc == 1 && nt_type(nt, argv[0]) && !strcmp(nt_type(nt, argv[0]), "RangeNode")) return rt;
264+ return ty_array_elem(rt);
265+ }
261266 /* index returns nil on a miss -> poly (int-or-nil) */
262267 if (!strcmp(name, "index") && (rt == TY_INT_ARRAY || rt == TY_STR_ARRAY)) return TY_POLY;
263268 if (!strcmp(name, "length") || !strcmp(name, "size") ||
@@ -1417,7 +1422,10 @@
14171422 if ((!strcmp(name, "times") || !strcmp(name, "upto") ||
14181423 !strcmp(name, "downto") || !strcmp(name, "step")) && rt == TY_INT)
14191424 pt = TY_INT;
1420- else if (!strcmp(name, "each") && rt == TY_RANGE)
1425+ else if ((!strcmp(name, "each") || !strcmp(name, "map") || !strcmp(name, "collect") ||
1426+ !strcmp(name, "select") || !strcmp(name, "reject") || !strcmp(name, "filter") ||
1427+ !strcmp(name, "find") || !strcmp(name, "detect") || !strcmp(name, "each_with_index") ||
1428+ !strcmp(name, "sort_by") || !strcmp(name, "find_all") || !strcmp(name, "count")) && rt == TY_RANGE)
14211429 pt = TY_INT;
14221430 else if ((!strcmp(name, "each") || !strcmp(name, "map") ||
14231431 !strcmp(name, "select") || !strcmp(name, "reject") ||