Commit d7aa7e53

d7aa7e53388dMerged

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

Description

Array#replace return type + heterogeneous array literal push lift

Array#replace now returns the receiver type in analyze.c (parallel to the existing hash-receiver replace arm).

For [typed_literal] << non_matching_elem, the analyze pass now returns TY_POLY_ARRAY instead of the receiver's typed-array kind when the pushed element type is incompatible and the receiver is an ArrayNode literal. The codegen counterpart rebuilds the literal as a sp_PolyArray (boxing each existing element) and pushes the new element boxed, avoiding the sp_IntArray_push(char*) type error.

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

Details

Commit
d7aa7e53388d
Parent
8a93d3dda44a

src/analyze.cmodifiedView file

@@ -655,6 +655,19 @@
655655 !strcmp(name, "keep_if") || !strcmp(name, "delete_if")) && block >= 0) return rt;
656656 if (!strcmp(name, "find_index") || !strcmp(name, "index")) return TY_INT; /* int or nil */
657657 if (!strcmp(name, "each_index")) return rt;
658+ if ((!strcmp(name, "push") || !strcmp(name, "<<") || !strcmp(name, "append")) &&
659+ argc >= 1 && argv && rt != TY_POLY_ARRAY && ty_array_elem(rt) != TY_UNKNOWN) {
660+ /* Heterogeneous push on a typed-array literal: lift to poly. */
661+ TyKind elem_t = ty_array_elem(rt);
662+ const char *rty = nt_type(nt, recv);
663+ if (rty && !strcmp(rty, "ArrayNode")) {
664+ for (int ai = 0; ai < argc; ai++) {
665+ TyKind at = infer_type(c, argv[ai]);
666+ if (at != TY_UNKNOWN && at != elem_t) return TY_POLY_ARRAY;
667+ }
668+ }
669+ return rt;
670+ }
658671 if (!strcmp(name, "push") || !strcmp(name, "<<") || !strcmp(name, "append") ||
659672 !strcmp(name, "reverse") || !strcmp(name, "sort") || !strcmp(name, "uniq") ||
660673 !strcmp(name, "to_a") || !strcmp(name, "dup") || !strcmp(name, "clone") ||
@@ -664,6 +677,7 @@
664677 !strcmp(name, "reverse!") || !strcmp(name, "sort!") || !strcmp(name, "shuffle!") ||
665678 !strcmp(name, "rotate!") || !strcmp(name, "insert") || !strcmp(name, "freeze") ||
666679 (!strcmp(name, "fill") && argc == 1) ||
680+ !strcmp(name, "replace") ||
667681 !strcmp(name, "values_at")) return rt;
668682 if (!strcmp(name, "frozen?")) return TY_BOOL;
669683 if ((!strcmp(name, "delete_at") || !strcmp(name, "delete")) && argc == 1)