Commit ee4d5394

ee4d5394f518Merged

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

Description

Infer splat-into-array-literal element type

[*0..10] and [*arr] widened to poly_array because infer_type had no SplatNode case: it returned TY_UNKNOWN, and ty_array_of(UNKNOWN) defaults to poly_array, boxing every int element. A splat inside an array literal contributes the element type of the splatted collection -- a range's endpoint type, or an array's element type -- so the literal stays a typed array. This is what feeds optcarrot's @palette = [*0..4096], which legacy types as int_array; spinelc was emitting a boxed poly_array.

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

Details

Commit
ee4d5394f518
Parent
20a6038f97f3

src/analyze_infer.cmodifiedView file

@@ -2161,6 +2161,22 @@
21612161 if (hi >= 0) infer_type(c, hi);
21622162 return TY_RANGE;
21632163 }
2164+ /* A splat inside an array literal (`[*0..10]`, `[*arr]`) contributes the
2165+ element type of the splatted collection, so the literal stays a typed
2166+ array instead of widening to poly_array. The value returned here is the
2167+ would-be element type, which the ArrayNode arm unifies in. */
2168+ if (!strcmp(ty, "SplatNode")) {
2169+ int inner = nt_ref(nt, id, "expression");
2170+ if (inner < 0) return TY_UNKNOWN;
2171+ const char *ity = nt_type(nt, inner);
2172+ if (ity && !strcmp(ity, "RangeNode")) {
2173+ int lo = nt_ref(nt, inner, "left");
2174+ return (lo >= 0 && infer_type(c, lo) == TY_STRING) ? TY_STRING : TY_INT;
2175+ }
2176+ TyKind it = infer_type(c, inner);
2177+ if (ty_is_array(it)) return ty_array_elem(it);
2178+ return TY_POLY;
2179+ }
21642180 if (!strcmp(ty, "LambdaNode")) return TY_PROC;
21652181 /* an assignment expression evaluates to the assigned value -- but codegen
21662182 lowers `x = expr` to `({ lv_x = ...; lv_x; })`, so the chain value IS the