Commit 73dfad9f
73dfad9fd98dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
analyze(promote): follow a local to poly-array when its assigned value widened
In promote mode a map method whose element type widened (step 3) returns a
PolyArray, and an array literal of widened elements builds a PolyArray. A
local assigned such a value kept its narrower declared IntArray/StrArray/
FloatArray slot, so codegen emitted sp_IntArray * x = <sp_PolyArray *>.
Add a post-widen step (4): re-derive an array local's type from its assigned
value, following to TY_POLY_ARRAY. Fixes ptr_array; inert without the widen.
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 73dfad9fd98d
- Parent
- 828ca66a0f40
src/analyze.cmodifiedView file
@@ -2372,6 +2372,24 @@
23722372
}23732373
if (br == TY_POLY_ARRAY) { sc->ret = TY_POLY_ARRAY; changed = 1; }23742374
}2375+
/* (4) a local whose assigned value widened to a poly array must follow:2376+
its declared IntArray/StrArray/FloatArray slot would otherwise mismatch2377+
the PolyArray now produced -- whether by a map method whose return2378+
widened (step 3), or by an array literal whose elements widened2379+
(`arr = [x, y, x + y]` with poly x,y builds a PolyArray). */2380+
for (int id = 0; id < nt->count; id++) {2381+
const char *ty = nt_type(nt, id);2382+
if (!ty || strcmp(ty, "LocalVariableWriteNode")) continue;2383+
const char *nm = nt_str(nt, id, "name");2384+
if (!nm) continue;2385+
LocalVar *lv = scope_local(comp_scope_of(c, id), nm);2386+
if (!lv) continue;2387+
if (lv->type != TY_INT_ARRAY && lv->type != TY_STR_ARRAY &&2388+
lv->type != TY_FLOAT_ARRAY) continue;2389+
int vnode = nt_ref(nt, id, "value");2390+
if (vnode < 0) continue;2391+
if (infer_type(c, vnode) == TY_POLY_ARRAY) { lv->type = TY_POLY_ARRAY; changed = 1; }2392+
}23752393
}23762394
/* refresh the node-type cache so a `proc.call` node picks up the updated23772395
proc_ret (codegen reads comp_ntype, not lv->proc_ret directly). Re-infer