Commit d3dba14a

d3dba14a2d70Merged

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

Description

Integer Range#bsearch in find-minimum mode

Emit an inline binary search for (lo..hi).bsearch { |x| cond }: it returns the smallest member where the block is truthy, or nil (the SP_INT_NIL nullable-int sentinel) when none qualifies. analyze types the block parameter as int and reports a nullable int result.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
d3dba14a2d70
Parent
d2d4f09ccf31

src/analyze.cmodifiedView file

@@ -653,6 +653,7 @@
653653 !strcmp(name, "first") || !strcmp(name, "last") ||
654654 !strcmp(name, "size") || !strcmp(name, "count") ||
655655 !strcmp(name, "begin") || !strcmp(name, "end")) return TY_INT;
656+ if (!strcmp(name, "bsearch")) return TY_INT; /* a member, or nil (nullable int) */
656657 int block = nt_ref(nt, id, "block");
657658 if (block >= 0 && (!strcmp(name, "map") || !strcmp(name, "collect"))) {
658659 int body = nt_ref(nt, block, "body");
@@ -1984,7 +1985,8 @@
19841985 else if ((!strcmp(name, "each") || !strcmp(name, "map") || !strcmp(name, "collect") ||
19851986 !strcmp(name, "select") || !strcmp(name, "reject") || !strcmp(name, "filter") ||
19861987 !strcmp(name, "find") || !strcmp(name, "detect") || !strcmp(name, "each_with_index") ||
1987- !strcmp(name, "sort_by") || !strcmp(name, "find_all") || !strcmp(name, "count")) && rt == TY_RANGE)
1988+ !strcmp(name, "sort_by") || !strcmp(name, "find_all") || !strcmp(name, "count") ||
1989+ !strcmp(name, "bsearch")) && rt == TY_RANGE)
19881990 pt = TY_INT;
19891991 else if ((!strcmp(name, "each") || !strcmp(name, "map") || !strcmp(name, "collect") ||
19901992 !strcmp(name, "select") || !strcmp(name, "reject") || !strcmp(name, "filter") ||