Commit 2112f518

2112f518e68bMerged

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

Description

Array#flatten and empty-array-literal receivers

Array#flatten now lowers for poly arrays (recursive walk via sp_PolyArray_flatten / _flatten_n) and is identity for scalar-element arrays. A bare empty array literal used as a receiver ([].flatten, [].inspect, [].length) has no usage to fold an element type from and fell through to the unresolved-call error; it is now treated as an empty poly array for the common non-element-returning methods.

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

Details

Commit
2112f518e68b
Parent
ce5755346930

src/analyze.cmodifiedView file

@@ -209,6 +209,17 @@
209209 TyKind rt = recv >= 0 ? infer_type(c, recv) : TY_UNKNOWN;
210210 TyKind a0 = argc >= 1 ? infer_type(c, argv[0]) : TY_UNKNOWN;
211211
212+ /* an empty array literal used directly as a receiver (`[].flatten`) has no
213+ usage to fold an element type from; treat it as an empty poly array so
214+ array methods dispatch instead of falling through to unresolved. */
215+ if (rt == TY_UNKNOWN && recv >= 0) {
216+ const char *rty = nt_type(nt, recv);
217+ if (rty && !strcmp(rty, "ArrayNode")) {
218+ int en = 0; nt_arr(nt, recv, "elements", &en);
219+ if (en == 0) rt = TY_POLY_ARRAY;
220+ }
221+ }
222+
212223 /* `<&block-param>.call(...)` inside a yielding method: the explicit-call form
213224 of yield. Its value is the call-site block's value (resolved like yield). */
214225 {
@@ -458,7 +469,7 @@
458469 if (!strcmp(name, "push") || !strcmp(name, "<<") || !strcmp(name, "append") ||
459470 !strcmp(name, "reverse") || !strcmp(name, "sort") || !strcmp(name, "uniq") ||
460471 !strcmp(name, "to_a") || !strcmp(name, "dup") || !strcmp(name, "clone") ||
461- !strcmp(name, "compact")) return rt;
472+ !strcmp(name, "compact") || !strcmp(name, "flatten")) return rt;
462473 if (!strcmp(name, "[]=")) return ty_array_elem(rt);
463474 }
464475