Commit 4599b32c

4599b32cf20bMerged

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

Description

analyze: widen a typed-array method return to poly-array when its body does

A method declared to return an int/str/float array whose elements are computed from now-widened (poly) params builds a PolyArray in its body (e.g. def divmod(a,b)=[a/b, a%b] with poly a,b). Re-derive the return type from the body in the post-widen fixpoint so the C return type matches the value, then the node-cache refresh propagates it to callers. Unblocks multi_return.

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

Details

Commit
4599b32cf20b
Parent
2d7524373b5d

src/analyze.cmodifiedView file

@@ -2353,6 +2353,25 @@
23532353 TyKind pr = vnode >= 0 ? proc_ret_of(c, vnode) : TY_UNKNOWN;
23542354 if (pr != TY_UNKNOWN && (TyKind)lv->proc_ret != pr) { lv->proc_ret = (int)pr; changed = 1; }
23552355 }
2356+ /* (3) a typed-array-returning method whose elements widened now yields a
2357+ poly array in its body (e.g. `[a/b, a%b]` with poly a,b builds a
2358+ PolyArray), so its return type must follow to TY_POLY_ARRAY. */
2359+ for (int s = 0; s < c->nscopes; s++) {
2360+ Scope *sc = &c->scopes[s];
2361+ TyKind r = (TyKind)sc->ret;
2362+ if (r != TY_INT_ARRAY && r != TY_STR_ARRAY && r != TY_FLOAT_ARRAY) continue;
2363+ TyKind br = TY_UNKNOWN;
2364+ if (sc->body >= 0) { int bn = 0; const int *bb = nt_arr(nt, sc->body, "body", &bn); if (bn > 0) br = comp_ntype(c, bb[bn - 1]); }
2365+ for (int id = 0; id < nt->count && br != TY_POLY_ARRAY; id++) {
2366+ const char *ty = nt_type(nt, id);
2367+ if (ty && !strcmp(ty, "ReturnNode") && comp_scope_of(c, id) == sc) {
2368+ int a = nt_ref(nt, id, "arguments"); int an = 0;
2369+ const int *av = a >= 0 ? nt_arr(nt, id, "arguments", &an) : NULL;
2370+ if (an == 1 && comp_ntype(c, av[0]) == TY_POLY_ARRAY) br = TY_POLY_ARRAY;
2371+ }
2372+ }
2373+ if (br == TY_POLY_ARRAY) { sc->ret = TY_POLY_ARRAY; changed = 1; }
2374+ }
23562375 }
23572376 /* refresh the node-type cache so a `proc.call` node picks up the updated
23582377 proc_ret (codegen reads comp_ntype, not lv->proc_ret directly). Re-infer