Commit e711be5c

e711be5c57f0Merged

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

Description

analyze: route map/collect and select/filter checks through ty_iter_shape

The iterator-shape groups were hardcoded as inline !strcmp(name, "map") || !strcmp(name, "collect") (and select/filter) lists scattered across the inference passes, duplicating the membership that types.c's ty_iter_shape() already owns. A new map-like iterator added to ty_iter_shape would silently miss these sites -- the same mirror-incompleteness class as #1469.

Replace the inline pairs with ty_iter_shape(name) == TY_ITER_MAP / == TY_ITER_SELECT (12 + 1 sites in analyze_infer.c / analyze_pass.c). The predicate's MAP membership is exactly {map, collect} and SELECT is exactly {select, filter}, so the booleans are unchanged.

Pure refactor: emitted C is byte-for-byte identical across optcarrot and all 1007 benchmark/test inputs.

Gates: 950 tests pass, optcarrot checksum 59662.

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

Details

Commit
e711be5c57f0
Parent
2dec192e5cc6

src/analyze_infer.cmodifiedView file

@@ -1397,7 +1397,7 @@
13971397 }
13981398
13991399 /* each_slice(n).map/collect { |...| } chain: return array of block result type */
1400- if (recv >= 0 && rt == TY_UNKNOWN && (!strcmp(name, "map") || !strcmp(name, "collect")) &&
1400+ if (recv >= 0 && rt == TY_UNKNOWN && (ty_iter_shape(name) == TY_ITER_MAP) &&
14011401 nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&
14021402 nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "each_slice") &&
14031403 nt_ref(nt, recv, "block") < 0) {
@@ -1410,7 +1410,7 @@
14101410 }
14111411
14121412 /* each_cons(n).map/collect { |...| } chain: return array of block result type */
1413- if (recv >= 0 && rt == TY_UNKNOWN && (!strcmp(name, "map") || !strcmp(name, "collect")) &&
1413+ if (recv >= 0 && rt == TY_UNKNOWN && (ty_iter_shape(name) == TY_ITER_MAP) &&
14141414 nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&
14151415 nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "each_cons") &&
14161416 nt_ref(nt, recv, "block") < 0) {
@@ -1423,7 +1423,7 @@
14231423 }
14241424
14251425 /* each_cons(n).with_index(off).map/collect { |...| } chain */
1426- if (recv >= 0 && rt == TY_UNKNOWN && (!strcmp(name, "map") || !strcmp(name, "collect")) &&
1426+ if (recv >= 0 && rt == TY_UNKNOWN && (ty_iter_shape(name) == TY_ITER_MAP) &&
14271427 nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&
14281428 nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "with_index") &&
14291429 nt_ref(nt, recv, "block") < 0) {
@@ -1468,7 +1468,7 @@
14681468 if (recv >= 0 && ty_is_array(rt)) {
14691469 int block = nt_ref(nt, id, "block");
14701470 if (block >= 0) {
1471- if (!strcmp(name, "map") || !strcmp(name, "collect")) {
1471+ if (ty_iter_shape(name) == TY_ITER_MAP) {
14721472 int body = nt_ref(nt, block, "body");
14731473 int bn = 0;
14741474 const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
@@ -1725,7 +1725,7 @@
17251725 if (!strcmp(name, "dig") && argc >= 1) return TY_POLY;
17261726 {
17271727 int blk = nt_ref(nt, id, "block");
1728- if (blk >= 0 && (!strcmp(name, "map") || !strcmp(name, "collect"))) {
1728+ if (blk >= 0 && (ty_iter_shape(name) == TY_ITER_MAP)) {
17291729 int body = nt_ref(nt, blk, "body");
17301730 int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
17311731 TyKind et = bn > 0 ? infer_type(c, bb[bn - 1]) : TY_UNKNOWN;
@@ -1818,7 +1818,7 @@
18181818 !strcmp(name, "begin") || !strcmp(name, "end")) return TY_INT;
18191819 if (!strcmp(name, "bsearch")) return TY_INT; /* a member, or nil (nullable int) */
18201820 int block = nt_ref(nt, id, "block");
1821- if (block >= 0 && (!strcmp(name, "map") || !strcmp(name, "collect"))) {
1821+ if (block >= 0 && (ty_iter_shape(name) == TY_ITER_MAP)) {
18221822 int body = nt_ref(nt, block, "body");
18231823 int bn = 0;
18241824 const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
@@ -1897,12 +1897,12 @@
18971897 if (!strcmp(name, "values_at") || !strcmp(name, "fetch_values")) return TY_POLY_ARRAY;
18981898 {
18991899 int block = nt_ref(nt, id, "block");
1900- if (block >= 0 && (!strcmp(name, "map") || !strcmp(name, "collect"))) {
1900+ if (block >= 0 && (ty_iter_shape(name) == TY_ITER_MAP)) {
19011901 int body = nt_ref(nt, block, "body");
19021902 int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
19031903 return ty_array_of(bn > 0 ? infer_type(c, bb[bn - 1]) : TY_UNKNOWN);
19041904 }
1905- if (block >= 0 && (!strcmp(name, "select") || !strcmp(name, "filter") || !strcmp(name, "reject"))) return rt;
1905+ if (block >= 0 && (ty_iter_shape(name) == TY_ITER_SELECT || !strcmp(name, "reject"))) return rt;
19061906 if (block >= 0 && !strcmp(name, "transform_keys")) {
19071907 int body = nt_ref(nt, block, "body");
19081908 int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;