cmd+selfhost+test: relax alloc-slice element-type pin via LHS retype
`alloc([], n)` synthesizes ([]u8 | nomem) at expression level — that's fine, since the slice form only legitimately appears in let-init position where the LHS carries the real element type. In clet, after type-checking the rhs, peel any N_TRYPROP/N_TRYUNW wrapper, match the alloc-slice AST shape with the same-module shadow gate (from #23), and retype the call's tagged return to ([]T | nomem) where T is the declared LHS element. Then assignability sees []T vs []T and accepts. Cgen N_LET shortcut gains a viatryprop arm next to the existing viatryunw — on rt_alloc returning null, emits the tagged-return nomem propagation (MOVQ $nidx, AX; epilogue) instead of exit(1). nidx comes from cg_tag_for_variant on the enclosing fn's return type, matching the existing TRYPROP propret path. Wwstage mirrors all four hunks (check.ww + cgenstmt.ww). Promotes the previously-silent conf=false skip into a confident accept. Unblocks #6 (dupall) and lays the path for #4/#7. Byte-identity holds modulo the pre-existing #44 alloc/rt_alloc symbol divergence.
This commit is contained in:
@@ -975,6 +975,58 @@ fn checkletassign(c: *checker, n: *node) void = {
|
||||
if (n.rhs == nil) { return; }; // no init
|
||||
let src: *node = exprtype(c, n.rhs);
|
||||
if (src == nil) { return; }; // can't infer
|
||||
// #45: alloc([], n) defers element type to the let-init context
|
||||
// (Hare-style). exprtype's alloc-slice branch synthesizes
|
||||
// ([]u8 | nomem) / []u8 (for the ?/! wrap) with no LHS context;
|
||||
// when the let declares []T, retype src to []T / ([]T | nomem)
|
||||
// so isassignable sees exact equality. cgenstmt cglet drives the
|
||||
// element size from n.lhs already (cmd/wcc/cgenstmt.ww), so this
|
||||
// stays symmetric with cstage check.c clet's parallel retype.
|
||||
if (n.lhs.kind == nkind.N_TSLICE) {
|
||||
let wrapped: bool = false;
|
||||
let inner: *node = n.rhs;
|
||||
if (inner.kind == nkind.N_TRYPROP) {
|
||||
wrapped = true;
|
||||
inner = inner.lhs;
|
||||
} else { if (inner.kind == nkind.N_TRYUNW) {
|
||||
wrapped = true;
|
||||
inner = inner.lhs;
|
||||
}; };
|
||||
if (inner != nil && inner.kind == nkind.N_CALL) {
|
||||
let callee: *node = inner.lhs;
|
||||
let a0: *node = inner.list;
|
||||
let a1: *node = nil;
|
||||
let a2: *node = nil;
|
||||
if (a0 != nil) { a1 = a0.next; };
|
||||
if (a1 != nil) { a2 = a1.next; };
|
||||
if (callee != nil
|
||||
&& callee.kind == nkind.N_IDENT
|
||||
&& streq(callee.str, "alloc")
|
||||
&& a0 != nil && a0.kind == nkind.N_ARRLIT
|
||||
&& a0.list == nil
|
||||
&& a1 != nil && a2 == nil) {
|
||||
let shadowed: bool = false;
|
||||
if (c.curmod.len > 0) {
|
||||
if (scopelookupinmodule(c.cur, c.curmod, "alloc") != nil) {
|
||||
shadowed = true;
|
||||
};
|
||||
};
|
||||
if (!shadowed) {
|
||||
let sl: *node = newnode(c.a, nkind.N_TSLICE, "", 0, 0);
|
||||
sl.lhs = n.lhs.lhs;
|
||||
if (wrapped) {
|
||||
src = sl;
|
||||
} else {
|
||||
let nome: *node = mktname(c, "nomem");
|
||||
sl.next = nome;
|
||||
let tt: *node = newnode(c.a, nkind.N_TTAGGED, "", 0, 0);
|
||||
tt.list = sl;
|
||||
src = tt;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
let conf: bool = false;
|
||||
let ok: bool = isassignable(c, n.lhs, src, &conf);
|
||||
if (!conf) { return; };
|
||||
|
||||
Reference in New Issue
Block a user