Commit 28e43f99

28e43f9924e9Merged

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

Description

analyze+codegen: Array.new, count{block}, Symbol Comparable, push widening

  • analyze: Array.new with 0 args now infers TY_POLY_ARRAY instead of TY_UNKNOWN; Array.new(n, v) already worked via the argc==2 path
  • analyze: push/append into a TY_POLY_ARRAY slot leaves it as TY_POLY_ARRAY (ty_unify between POLY_ARRAY and a typed array kind returned TY_POLY scalar; use array-aware widening instead)
  • analyze: add count/any?/all?/none?/one? to the array block-param typing list so element type flows into block params
  • codegen: Array.new() emits sp_PolyArray_new()
  • codegen: count without block on typed/poly arrays uses length
  • codegen: count{block} on typed/poly arrays emits a counter loop
  • codegen: Symbol includes Comparable, so is_a?(Comparable) on a Symbol receiver now returns true (matches Ruby semantics)

438 pass (+1 vs 437).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
28e43f9924e9
Parent
59e86e74ebb5

src/analyze.cmodifiedView file

@@ -306,6 +306,7 @@
306306 int ci = comp_class_index(c, cn);
307307 if (ci >= 0) return ty_object(ci);
308308 if (cn && !strcmp(cn, "Array") && argc == 2) return ty_array_of(infer_type(c, argv[1]));
309+ if (cn && !strcmp(cn, "Array")) return TY_POLY_ARRAY; /* Array.new / Array.new(n) */
309310 if (cn && !strcmp(cn, "String")) return TY_STRING;
310311 if (cn && !strcmp(cn, "StringIO")) return TY_STRINGIO;
311312 if (cn && !strcmp(cn, "StringScanner")) return TY_STRINGSCANNER;
@@ -1754,10 +1755,16 @@
17541755
17551756 TyKind before = *slot;
17561757 if (is_push) {
1757- /* explicit push/append: definitely array */
1758+ /* explicit push/append: definitely array. A PolyArray stays PolyArray
1759+ regardless of the pushed value type; mixing typed arrays widens to
1760+ PolyArray (ty_unify would return TY_POLY scalar, so use array-aware
1761+ widening instead). */
17581762 if (vt == TY_UNKNOWN) continue;
17591763 if (*slot != TY_UNKNOWN && !ty_is_array(*slot)) continue;
1760- *slot = ty_unify(*slot, ty_array_of(vt));
1764+ if (*slot == TY_POLY_ARRAY) continue; /* already widest array type */
1765+ TyKind want = ty_array_of(vt);
1766+ if (*slot != TY_UNKNOWN && want != *slot) want = TY_POLY_ARRAY;
1767+ *slot = want;
17611768 }
17621769 else if (kt == TY_INT) {
17631770 /* int key []=: if slot already array, leave it; otherwise infer int-keyed hash */
@@ -2200,8 +2207,9 @@
22002207 !strcmp(name, "max_by") || !strcmp(name, "min_by") || !strcmp(name, "sort_by") ||
22012208 !strcmp(name, "take_while") || !strcmp(name, "drop_while") ||
22022209 !strcmp(name, "reverse_each") || !strcmp(name, "each_entry") ||
2203- !strcmp(name, "sum") ||
2204- !strcmp(name, "each_with_index")) &&
2210+ !strcmp(name, "sum") || !strcmp(name, "count") ||
2211+ !strcmp(name, "any?") || !strcmp(name, "all?") || !strcmp(name, "none?") ||
2212+ !strcmp(name, "one?") || !strcmp(name, "each_with_index")) &&
22052213 ty_is_array(rt))
22062214 pt = ty_array_elem(rt);
22072215