diff --git a/Makefile b/Makefile index 515ee5f9..da6168b7 100644 --- a/Makefile +++ b/Makefile @@ -241,6 +241,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_arr_elem_field_write \ $(BIN)/test_arr_enum_elem \ $(BIN)/test_arr_strslice_elem \ + $(BIN)/test_arr_infer_len \ $(BIN)/test_dot_str_chained_arg \ $(BIN)/test_dot_slice_arg \ $(BIN)/test_dot_tagged_source \ @@ -561,6 +562,12 @@ $(BIN)/test_arr_strslice_elem: test/wcc/683_arr_strslice_elem.c $(BIN)/ww \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_arr_infer_len: test/wcc/684_arr_infer_len.c $(BIN)/ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_dot_str_chained_arg: test/wcc/692_dot_str_chained_arg.c $(BIN)/ww \ $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/cmd/wcc/check.c b/cmd/wcc/check.c index 416933c9..ae27e895 100644 --- a/cmd/wcc/check.c +++ b/cmd/wcc/check.c @@ -1901,10 +1901,14 @@ clet(Checker *c, Node *n) if (n->rhs) initt = cexpr(c, n->rhs); c->alloc_octx = saved_octx; /* `let xs: [_]T = arrlit;` — fill in the inferred length from the - * initialiser. `resolve_type` left alen=0 as a sentinel. */ - if (declared && declared->kind == TY_ARRAY && declared->alen == 0 && - initt) { - Type *iu = (initt->kind == TY_NAMED) ? initt->under : initt; + * initialiser. `resolve_type` left alen=0 as a sentinel. A `[_]T` + * with no array-literal initialiser (no init at all, or a non-array + * init) can't infer its length — that is a loud error, never a + * silent zero-length array (rule 7, #7). */ + if (declared && declared->kind == TY_ARRAY && declared->alen == 0) { + Type *iu = initt + ? ((initt->kind == TY_NAMED) ? initt->under : initt) + : NULL; if (iu && iu->kind == TY_ARRAY) declared = type_array(c->a, declared->sub, iu->alen); else @@ -2545,12 +2549,46 @@ check_file(Checker *c, Node *file) case N_LET: { if (d->rhs) { Type *rt = cexpr(c, d->rhs); + /* `let xs: [_]T = arrlit;` at module level — infer the + * length from the initialiser, the same patch clet + * applies for a local let (#7). pass-1.5 resolve_type + * left alen=0 as the sentinel; patching d->type feeds + * cgen's letvars registration (lv->type = d->type), + * which both lays the full-length DATA row and reads + * the right `.len`. */ + if (d->type && d->type->kind == TY_ARRAY + && d->type->alen == 0) { + Type *iu = rt + ? ((rt->kind == TY_NAMED) ? rt->under : rt) + : NULL; + if (iu && iu->kind == TY_ARRAY) { + d->type = type_array(c->a, + d->type->sub, iu->alen); + /* The Sym installed in pass-1.5 still + * carries the alen=0 sentinel; a later + * `x.len` resolves `x` through the Sym + * (its type stamps n->lhs->type, which + * cgen reads as u->alen). Re-point it at + * the inferred-length type too. */ + Sym *s = scope_lookup_local(c->cur, + d->str); + if (s) s->type = d->type; + } else + err(c, d->pos, "[_]T needs an " + "array-literal initialiser"); + } if (d->type == NULL) d->type = type_default(rt); if (d->type && rt != ty_err && d->type != ty_err && !type_assignable(d->type, rt) && !arrlit_init_fits(c, d->type, d->rhs)) err(c, d->pos, "let %s init not assignable", d->str); + } else if (d->type && d->type->kind == TY_ARRAY + && d->type->alen == 0) { + /* `let x: [_]T;` — no initialiser, length can't be + * inferred (rule 7, #7). */ + err(c, d->pos, "[_]T needs an array-literal " + "initialiser"); } break; } diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 941c0a35..afc8f46c 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -14313,8 +14313,48 @@ fn checkassign(c: *checker, n: *node) void = { n.rhs = desugararrayslice(c, ltn, rtn, n.rhs); }; +// inferarraylen — `let xs: [_]T = arrlit;` length inference (#7). The +// parser leaves a `[_]` array's length child nil as the infer sentinel +// (parse.ww, mirror cstage parse.c:186). Count the array-literal's +// elements (skipping the `...` repeat marker, same walk as the N_ARRLIT +// exprtype at L2905) and stamp a synthesized N_INTLIT length node so +// tinfofornode / cgen / `.len` all read the real count — the wwstage +// analogue of cstage clet's `declared = type_array(.., iu->alen)` patch. +// A `[_]T` with no array-literal initialiser can't infer: loud error, +// never a silent zero-length array (rule 7). Idempotent (skips once the +// length child is set), so the module-level double-call (checkfile's +// pre-resolvewalk call + checkletassign here) raises at most one error. +fn inferarraylen(c: *checker, n: *node) void = { + if (n == nil) { return; }; + if (n.lhs == nil) { return; }; + if (n.lhs.kind != nkind.N_TARRAY) { return; }; + if (n.lhs.rhs != nil) { return; }; // explicit [N] or already inferred + if (n.rhs == nil || n.rhs.kind != nkind.N_ARRLIT) { + cerr("error: [_]T needs an array-literal initialiser\n"); + c.errs += 1; + let z: *node = newnode(nkind.N_INTLIT, "", 0, 0); + z.uval = 0u64; + n.lhs.rhs = z; // sentinel: idempotent, error already raised + return; + }; + let cnt: u64 = 0u64; + let it: *node = n.rhs.list; + for (it != nil) { + let skip: bool = false; + if (it.kind == nkind.N_FIELD) { + if (streq(it.str, "...")) { skip = true; }; + }; + if (!skip) { cnt += 1u64; }; + it = it.next; + }; + let cn: *node = newnode(nkind.N_INTLIT, "", 0, 0); + cn.uval = cnt; + n.lhs.rhs = cn; +}; + fn checkletassign(c: *checker, n: *node) void = { if (n == nil) { return; }; + inferarraylen(c, n); // #7: must run before the n.rhs==nil bail if (n.rhs == nil) { return; }; // no init // hint = nil for A.6.0; A.6.1 will pass n.lhs once STRUCTLIT/ARRLIT // arms consume it. Plumbing-only at this point. @@ -14870,6 +14910,12 @@ export fn checkfile(c: *checker, file: *node) void = { case nkind.N_TYPEDECL: if (d.lhs != nil) { resolvewalk(c, d.lhs); }; case nkind.N_LET: + // #7: a module-level `let xs: [_]T = arrlit;` must infer + // its length BEFORE resolvewalk stamps d.lhs's tinfo — + // otherwise the array tinfo caches the alen=0 sentinel and + // the patched length child never reaches the size/data + // reads. Idempotent with the checkletassign call below. + inferarraylen(c, d); if (d.lhs != nil) { resolvewalk(c, d.lhs); }; if (d.rhs != nil) { resolvewalk(c, d.rhs); }; // #130: top-level let assignability — the subtree @@ -17396,50 +17442,10 @@ fn inferletcalltype(c: *cgen, rhs: *node) *node = { // tagged-init branch writes past the local and tramples the next // slot. export fn letslotsize(c: *cgen, n: *node) i32 = { - // `[_]T = arrlit;` — inferred-length array. slotsize would - // return elem_size * 1 (treating missing length as 1); intercept - // and compute the real count first. - if (n.lhs != nil) { - if (n.lhs.kind == nkind.N_TARRAY) { - if (n.lhs.rhs == nil) { - if (n.rhs != nil) { - if (n.rhs.kind == nkind.N_ARRLIT) { - let elemn: *node = n.lhs.lhs; - let esz: i32 = 8; - if (elemn != nil) { - if (elemn.kind == nkind.N_TNAME) { - // Composite primitive: `str` is 16B - // (ptr+len) — primsize returns 0 for - // it, so it'd slot 8B without this. - if (streq(elemn.str, "str")) { - esz = primtypesize("str"): i32; - } else { - let ps: i32 = primsize(elemn.str); - if (ps > 0) { esz = ps; }; - }; - }; - }; - let cnt: i32 = 0; - let e: *node = n.rhs.list; - for (e != nil) { - let adv: bool = true; - if (e.kind == nkind.N_FIELD) { - if (streq(e.str, "...")) { - e = nil; - adv = false; - }; - }; - if (adv) { - cnt += 1; - e = e.next; - }; - }; - return esz * cnt; - }; - }; - }; - }; - }; + // `[_]T = arrlit;` inferred-length arrays no longer need a slot-size + // intercept here: the checker (inferarraylen, check.ww) stamps the + // real element count onto the array type's length child before cgen + // runs, so slotsize reads it like any explicit `[N]T` (#7). if (n.lhs != nil) { return slotsize(c, n.lhs); }; // Annotation-less init: defer to the call's return type if we // can infer it. Tagged-union returns need 24B; everything else @@ -22165,6 +22171,41 @@ fn cgdot(c: *cgen, n: *node) void = { }; }; }; + // Top-level [N]T global pseudo-fields (#7): `.len` is the static + // element count (immediate from the array type node's length child); + // `.ptr` is the array's base address (LEAQ name(SB)). Without this a + // module-level array's `x.len` falls to the module-qualified SB + // fallback below and mis-emits `MOVQ len(SB), AX` (linker: undefined + // reference to len). Mirror of the local-array arm above and cstage + // cg_base_cap's `aimm(bu->alen)` immediate (cgen.c:1692). + if (lhs != nil) { + if (lhs.kind == nkind.N_IDENT) { + let gtn: *node = letvartnode(c, lhs.str); + if (gtn != nil) { + if (gtn.kind == nkind.N_TARRAY) { + if (streq(fld, "ptr")) { + emitline("\tLEAQ\t"); + emitsymname(c, lhs.str); + emitline("(SB), AX\n"); + return; + }; + if (streq(fld, "len")) { + let lenn: *node = gtn.rhs; + let alen: i64 = 0i64; + if (lenn != nil) { + if (lenn.kind == nkind.N_INTLIT) { + alen = lenn.uval: i64; + }; + }; + emitline("\tMOVQ\t$"); + emitint(alen); + emitline(", AX\n"); + return; + }; + }; + }; + }; + }; // Top-level struct global field read — LEAQ name(SB), CX then // load at fi.foff(CX). Mirrors the local "Direct struct local" // branch above, swapping the BP frame slot for the global VA. diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 8e752d66..1aaf8246 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -2441,6 +2441,41 @@ fn cgdot(c: *cgen, n: *node) void = { }; }; }; + // Top-level [N]T global pseudo-fields (#7): `.len` is the static + // element count (immediate from the array type node's length child); + // `.ptr` is the array's base address (LEAQ name(SB)). Without this a + // module-level array's `x.len` falls to the module-qualified SB + // fallback below and mis-emits `MOVQ len(SB), AX` (linker: undefined + // reference to len). Mirror of the local-array arm above and cstage + // cg_base_cap's `aimm(bu->alen)` immediate (cgen.c:1692). + if (lhs != nil) { + if (lhs.kind == nkind.N_IDENT) { + let gtn: *node = letvartnode(c, lhs.str); + if (gtn != nil) { + if (gtn.kind == nkind.N_TARRAY) { + if (streq(fld, "ptr")) { + emitline("\tLEAQ\t"); + emitsymname(c, lhs.str); + emitline("(SB), AX\n"); + return; + }; + if (streq(fld, "len")) { + let lenn: *node = gtn.rhs; + let alen: i64 = 0i64; + if (lenn != nil) { + if (lenn.kind == nkind.N_INTLIT) { + alen = lenn.uval: i64; + }; + }; + emitline("\tMOVQ\t$"); + emitint(alen); + emitline(", AX\n"); + return; + }; + }; + }; + }; + }; // Top-level struct global field read — LEAQ name(SB), CX then // load at fi.foff(CX). Mirrors the local "Direct struct local" // branch above, swapping the BP frame slot for the global VA. diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index f30cf590..4a9a881e 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -1808,50 +1808,10 @@ fn inferletcalltype(c: *cgen, rhs: *node) *node = { // tagged-init branch writes past the local and tramples the next // slot. export fn letslotsize(c: *cgen, n: *node) i32 = { - // `[_]T = arrlit;` — inferred-length array. slotsize would - // return elem_size * 1 (treating missing length as 1); intercept - // and compute the real count first. - if (n.lhs != nil) { - if (n.lhs.kind == nkind.N_TARRAY) { - if (n.lhs.rhs == nil) { - if (n.rhs != nil) { - if (n.rhs.kind == nkind.N_ARRLIT) { - let elemn: *node = n.lhs.lhs; - let esz: i32 = 8; - if (elemn != nil) { - if (elemn.kind == nkind.N_TNAME) { - // Composite primitive: `str` is 16B - // (ptr+len) — primsize returns 0 for - // it, so it'd slot 8B without this. - if (streq(elemn.str, "str")) { - esz = primtypesize("str"): i32; - } else { - let ps: i32 = primsize(elemn.str); - if (ps > 0) { esz = ps; }; - }; - }; - }; - let cnt: i32 = 0; - let e: *node = n.rhs.list; - for (e != nil) { - let adv: bool = true; - if (e.kind == nkind.N_FIELD) { - if (streq(e.str, "...")) { - e = nil; - adv = false; - }; - }; - if (adv) { - cnt += 1; - e = e.next; - }; - }; - return esz * cnt; - }; - }; - }; - }; - }; + // `[_]T = arrlit;` inferred-length arrays no longer need a slot-size + // intercept here: the checker (inferarraylen, check.ww) stamps the + // real element count onto the array type's length child before cgen + // runs, so slotsize reads it like any explicit `[N]T` (#7). if (n.lhs != nil) { return slotsize(c, n.lhs); }; // Annotation-less init: defer to the call's return type if we // can infer it. Tagged-union returns need 24B; everything else diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index 43fd6b9d..93393efe 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -3946,8 +3946,48 @@ fn checkassign(c: *checker, n: *node) void = { n.rhs = desugararrayslice(c, ltn, rtn, n.rhs); }; +// inferarraylen — `let xs: [_]T = arrlit;` length inference (#7). The +// parser leaves a `[_]` array's length child nil as the infer sentinel +// (parse.ww, mirror cstage parse.c:186). Count the array-literal's +// elements (skipping the `...` repeat marker, same walk as the N_ARRLIT +// exprtype at L2905) and stamp a synthesized N_INTLIT length node so +// tinfofornode / cgen / `.len` all read the real count — the wwstage +// analogue of cstage clet's `declared = type_array(.., iu->alen)` patch. +// A `[_]T` with no array-literal initialiser can't infer: loud error, +// never a silent zero-length array (rule 7). Idempotent (skips once the +// length child is set), so the module-level double-call (checkfile's +// pre-resolvewalk call + checkletassign here) raises at most one error. +fn inferarraylen(c: *checker, n: *node) void = { + if (n == nil) { return; }; + if (n.lhs == nil) { return; }; + if (n.lhs.kind != nkind.N_TARRAY) { return; }; + if (n.lhs.rhs != nil) { return; }; // explicit [N] or already inferred + if (n.rhs == nil || n.rhs.kind != nkind.N_ARRLIT) { + cerr("error: [_]T needs an array-literal initialiser\n"); + c.errs += 1; + let z: *node = newnode(nkind.N_INTLIT, "", 0, 0); + z.uval = 0u64; + n.lhs.rhs = z; // sentinel: idempotent, error already raised + return; + }; + let cnt: u64 = 0u64; + let it: *node = n.rhs.list; + for (it != nil) { + let skip: bool = false; + if (it.kind == nkind.N_FIELD) { + if (streq(it.str, "...")) { skip = true; }; + }; + if (!skip) { cnt += 1u64; }; + it = it.next; + }; + let cn: *node = newnode(nkind.N_INTLIT, "", 0, 0); + cn.uval = cnt; + n.lhs.rhs = cn; +}; + fn checkletassign(c: *checker, n: *node) void = { if (n == nil) { return; }; + inferarraylen(c, n); // #7: must run before the n.rhs==nil bail if (n.rhs == nil) { return; }; // no init // hint = nil for A.6.0; A.6.1 will pass n.lhs once STRUCTLIT/ARRLIT // arms consume it. Plumbing-only at this point. @@ -4503,6 +4543,12 @@ export fn checkfile(c: *checker, file: *node) void = { case nkind.N_TYPEDECL: if (d.lhs != nil) { resolvewalk(c, d.lhs); }; case nkind.N_LET: + // #7: a module-level `let xs: [_]T = arrlit;` must infer + // its length BEFORE resolvewalk stamps d.lhs's tinfo — + // otherwise the array tinfo caches the alen=0 sentinel and + // the patched length child never reaches the size/data + // reads. Idempotent with the checkletassign call below. + inferarraylen(c, d); if (d.lhs != nil) { resolvewalk(c, d.lhs); }; if (d.rhs != nil) { resolvewalk(c, d.rhs); }; // #130: top-level let assignability — the subtree diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index e548d904..7905e6c0 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -14313,8 +14313,48 @@ fn checkassign(c: *checker, n: *node) void = { n.rhs = desugararrayslice(c, ltn, rtn, n.rhs); }; +// inferarraylen — `let xs: [_]T = arrlit;` length inference (#7). The +// parser leaves a `[_]` array's length child nil as the infer sentinel +// (parse.ww, mirror cstage parse.c:186). Count the array-literal's +// elements (skipping the `...` repeat marker, same walk as the N_ARRLIT +// exprtype at L2905) and stamp a synthesized N_INTLIT length node so +// tinfofornode / cgen / `.len` all read the real count — the wwstage +// analogue of cstage clet's `declared = type_array(.., iu->alen)` patch. +// A `[_]T` with no array-literal initialiser can't infer: loud error, +// never a silent zero-length array (rule 7). Idempotent (skips once the +// length child is set), so the module-level double-call (checkfile's +// pre-resolvewalk call + checkletassign here) raises at most one error. +fn inferarraylen(c: *checker, n: *node) void = { + if (n == nil) { return; }; + if (n.lhs == nil) { return; }; + if (n.lhs.kind != nkind.N_TARRAY) { return; }; + if (n.lhs.rhs != nil) { return; }; // explicit [N] or already inferred + if (n.rhs == nil || n.rhs.kind != nkind.N_ARRLIT) { + cerr("error: [_]T needs an array-literal initialiser\n"); + c.errs += 1; + let z: *node = newnode(nkind.N_INTLIT, "", 0, 0); + z.uval = 0u64; + n.lhs.rhs = z; // sentinel: idempotent, error already raised + return; + }; + let cnt: u64 = 0u64; + let it: *node = n.rhs.list; + for (it != nil) { + let skip: bool = false; + if (it.kind == nkind.N_FIELD) { + if (streq(it.str, "...")) { skip = true; }; + }; + if (!skip) { cnt += 1u64; }; + it = it.next; + }; + let cn: *node = newnode(nkind.N_INTLIT, "", 0, 0); + cn.uval = cnt; + n.lhs.rhs = cn; +}; + fn checkletassign(c: *checker, n: *node) void = { if (n == nil) { return; }; + inferarraylen(c, n); // #7: must run before the n.rhs==nil bail if (n.rhs == nil) { return; }; // no init // hint = nil for A.6.0; A.6.1 will pass n.lhs once STRUCTLIT/ARRLIT // arms consume it. Plumbing-only at this point. @@ -14870,6 +14910,12 @@ export fn checkfile(c: *checker, file: *node) void = { case nkind.N_TYPEDECL: if (d.lhs != nil) { resolvewalk(c, d.lhs); }; case nkind.N_LET: + // #7: a module-level `let xs: [_]T = arrlit;` must infer + // its length BEFORE resolvewalk stamps d.lhs's tinfo — + // otherwise the array tinfo caches the alen=0 sentinel and + // the patched length child never reaches the size/data + // reads. Idempotent with the checkletassign call below. + inferarraylen(c, d); if (d.lhs != nil) { resolvewalk(c, d.lhs); }; if (d.rhs != nil) { resolvewalk(c, d.rhs); }; // #130: top-level let assignability — the subtree @@ -17396,50 +17442,10 @@ fn inferletcalltype(c: *cgen, rhs: *node) *node = { // tagged-init branch writes past the local and tramples the next // slot. export fn letslotsize(c: *cgen, n: *node) i32 = { - // `[_]T = arrlit;` — inferred-length array. slotsize would - // return elem_size * 1 (treating missing length as 1); intercept - // and compute the real count first. - if (n.lhs != nil) { - if (n.lhs.kind == nkind.N_TARRAY) { - if (n.lhs.rhs == nil) { - if (n.rhs != nil) { - if (n.rhs.kind == nkind.N_ARRLIT) { - let elemn: *node = n.lhs.lhs; - let esz: i32 = 8; - if (elemn != nil) { - if (elemn.kind == nkind.N_TNAME) { - // Composite primitive: `str` is 16B - // (ptr+len) — primsize returns 0 for - // it, so it'd slot 8B without this. - if (streq(elemn.str, "str")) { - esz = primtypesize("str"): i32; - } else { - let ps: i32 = primsize(elemn.str); - if (ps > 0) { esz = ps; }; - }; - }; - }; - let cnt: i32 = 0; - let e: *node = n.rhs.list; - for (e != nil) { - let adv: bool = true; - if (e.kind == nkind.N_FIELD) { - if (streq(e.str, "...")) { - e = nil; - adv = false; - }; - }; - if (adv) { - cnt += 1; - e = e.next; - }; - }; - return esz * cnt; - }; - }; - }; - }; - }; + // `[_]T = arrlit;` inferred-length arrays no longer need a slot-size + // intercept here: the checker (inferarraylen, check.ww) stamps the + // real element count onto the array type's length child before cgen + // runs, so slotsize reads it like any explicit `[N]T` (#7). if (n.lhs != nil) { return slotsize(c, n.lhs); }; // Annotation-less init: defer to the call's return type if we // can infer it. Tagged-union returns need 24B; everything else @@ -22165,6 +22171,41 @@ fn cgdot(c: *cgen, n: *node) void = { }; }; }; + // Top-level [N]T global pseudo-fields (#7): `.len` is the static + // element count (immediate from the array type node's length child); + // `.ptr` is the array's base address (LEAQ name(SB)). Without this a + // module-level array's `x.len` falls to the module-qualified SB + // fallback below and mis-emits `MOVQ len(SB), AX` (linker: undefined + // reference to len). Mirror of the local-array arm above and cstage + // cg_base_cap's `aimm(bu->alen)` immediate (cgen.c:1692). + if (lhs != nil) { + if (lhs.kind == nkind.N_IDENT) { + let gtn: *node = letvartnode(c, lhs.str); + if (gtn != nil) { + if (gtn.kind == nkind.N_TARRAY) { + if (streq(fld, "ptr")) { + emitline("\tLEAQ\t"); + emitsymname(c, lhs.str); + emitline("(SB), AX\n"); + return; + }; + if (streq(fld, "len")) { + let lenn: *node = gtn.rhs; + let alen: i64 = 0i64; + if (lenn != nil) { + if (lenn.kind == nkind.N_INTLIT) { + alen = lenn.uval: i64; + }; + }; + emitline("\tMOVQ\t$"); + emitint(alen); + emitline(", AX\n"); + return; + }; + }; + }; + }; + }; // Top-level struct global field read — LEAQ name(SB), CX then // load at fi.foff(CX). Mirrors the local "Direct struct local" // branch above, swapping the BP frame slot for the global VA. diff --git a/test/wcc/684_arr_infer_len.c b/test/wcc/684_arr_infer_len.c new file mode 100644 index 00000000..56af61fa --- /dev/null +++ b/test/wcc/684_arr_infer_len.c @@ -0,0 +1,349 @@ +/* + * 684_arr_infer_len — cstage and wwstage agree, byte-for-byte and at + * runtime, that a `[_]T = [...]` array infers its length from the + * initialiser's element count (task #7, a canonical Hare form). + * + * The bug: `[_]T` silently miscompiled to a zero-length array. The + * parser already left the array type's length child nil as the infer + * sentinel (distinct from an explicit `[N]`), but neither checker + * stamped the real count — so `len(x)` / `x.len` returned 0 with no + * diagnostic (rule-7 silent miscompile). Module-level was worse on + * wwstage: `x.len` on ANY global array (even explicit `[N]`) fell to + * the SB fallback and mis-emitted `MOVQ len(SB), AX` (linker: + * undefined reference to len). + * + * The fix (BOTH stages, converged byte-identical): + * - checker (cmd/wcc/check.c clet + module-level N_LET; + * selfhost/cmd/wcc/check.ww inferarraylen): count the array-literal + * elements and stamp the length onto the array TYPE. cgen already + * keys stride/length/data off the stamped length, so it "just + * works" (rob's #7 ruling). A `[_]T` with no array-literal init + * can't infer → LOUD error, never a silent zero-length array. + * - cgen (selfhost/cmd/wcc/cgenexpr.ww cgdot): a top-level `[N]T` + * global's `.len` / `.ptr` pseudo-fields, the wwstage arm that was + * missing (cstage cgen.c:8011 already handled it). + * + * Coverage: `[_]int` / `[_]str` / `[_]u8`, both local and module-level, + * `len` read-back and element read-back, dual-stage runtime + asm + * byte-id. Mutation-sanity: the old collapse-to-0 fails every `*_len` + * row (len would be 0, not the count). Plus three negative rows where + * `[_]T` can't infer (no init / non-array init) — both stages must + * FAIL the build. + * + * row | shape | want + * ----------------+--------------------------------------+------ + * local_int_len | local [_]int=[10,20,30,40], x.len | 4 + * local_int_elem | local, x[2] | 30 + * mod_int_len | global [_]int=[..], x.len | 4 + * mod_int_elem | global, x[2] | 30 + * local_str_len | local [_]str=["a","b","c"], x.len | 3 + * local_str_elem | local [_]str=["ab","cde"], x[0].len | 2 + * mod_str_len | global [_]str=["a","b","c"], x.len | 3 + * local_u8_len | local [_]u8=[1..5], x.len | 5 + * local_u8_elem | local, x[4] | 5 + */ +#include +#include +#include +#include +#include +#include + +static int +runwait(const char *cmd) +{ + int rc = system(cmd); + if (rc == -1) return -1; + if (WIFEXITED(rc)) return WEXITSTATUS(rc); + return -1; +} + +struct row { const char *label; const char *src; int want; }; + +static const struct row rows[] = { + { "local_int_len", + "package main;\n" + "export fn main() i32 = {\n" + "\tlet x: [_]int = [10, 20, 30, 40];\n" + "\treturn x.len: i32;\n" + "};\n", + 4 }, + + { "local_int_elem", + "package main;\n" + "export fn main() i32 = {\n" + "\tlet x: [_]int = [10, 20, 30, 40];\n" + "\treturn x[2]: i32;\n" + "};\n", + 30 }, + + { "mod_int_len", + "package main;\n" + "let x: [_]int = [10, 20, 30, 40];\n" + "export fn main() i32 = {\n" + "\treturn x.len: i32;\n" + "};\n", + 4 }, + + { "mod_int_elem", + "package main;\n" + "let x: [_]int = [10, 20, 30, 40];\n" + "export fn main() i32 = {\n" + "\treturn x[2]: i32;\n" + "};\n", + 30 }, + + { "local_str_len", + "package main;\n" + "export fn main() i32 = {\n" + "\tlet x: [_]str = [\"a\", \"b\", \"c\"];\n" + "\treturn x.len: i32;\n" + "};\n", + 3 }, + + { "local_str_elem", + "package main;\n" + "export fn main() i32 = {\n" + "\tlet x: [_]str = [\"ab\", \"cde\"];\n" + "\treturn x[0].len: i32;\n" + "};\n", + 2 }, + + { "mod_str_len", + "package main;\n" + "let x: [_]str = [\"a\", \"b\", \"c\"];\n" + "export fn main() i32 = {\n" + "\treturn x.len: i32;\n" + "};\n", + 3 }, + + { "local_u8_len", + "package main;\n" + "export fn main() i32 = {\n" + "\tlet x: [_]u8 = [1u8, 2u8, 3u8, 4u8, 5u8];\n" + "\treturn x.len: i32;\n" + "};\n", + 5 }, + + { "local_u8_elem", + "package main;\n" + "export fn main() i32 = {\n" + "\tlet x: [_]u8 = [1u8, 2u8, 3u8, 4u8, 5u8];\n" + "\treturn x[4]: i32;\n" + "};\n", + 5 }, +}; + +/* `[_]T` that can't infer its length — both stages must FAIL the build + * (loud diagnostic, not a silent zero-length array). */ +static const char *neg[] = { + /* no init, local */ + "package main;\n" + "export fn main() i32 = {\n" + "\tlet x: [_]str;\n" + "\treturn 0;\n" + "};\n", + /* no init, module-level */ + "package main;\n" + "let x: [_]int;\n" + "export fn main() i32 = { return 0; };\n", + /* non-array initialiser */ + "package main;\n" + "export fn main() i32 = {\n" + "\tlet x: [_]int = 5;\n" + "\treturn 0;\n" + "};\n", +}; + +static int +run_driver(const char *driver, const struct row *r, int i) +{ + char src[64], tmpdir[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/ail_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/ail_%d_d_%d", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + mkdir(tmpdir, 0755); + snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", + tmpdir, driver, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: build via %s failed\n", + r->label, driver); + unlink(src); rmdir(tmpdir); + return -1; + } + + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + char outbin[128]; + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + char *dot = strrchr(outbin, '.'); + if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; + int got = runwait(outbin); + + unlink(src); unlink(outbin); rmdir(tmpdir); + return got; +} + +/* build_should_fail — a `[_]T` that can't infer must error on `driver`; + * returns 0 when the build correctly FAILS, non-zero when it wrongly + * succeeded. */ +static int +build_should_fail(const char *driver, const char *src, int i) +{ + char s[64], tmpdir[64], cmd[1024]; + snprintf(s, sizeof s, "/tmp/ailn_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/ailn_%d_d_%d", getpid(), i); + + FILE *f = fopen(s, "wb"); + if (!f) return -1; + fputs(src, f); + fclose(f); + + mkdir(tmpdir, 0755); + snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", + tmpdir, driver, s); + int rc = runwait(cmd); + unlink(s); + /* clean any emitted binary */ + const char *base = strrchr(s, '/'); + base = base ? base + 1 : s; + char outbin[128]; + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + char *dot = strrchr(outbin, '.'); + if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; + unlink(outbin); + rmdir(tmpdir); + return rc == 0 ? -1 : 0; /* build must NOT succeed */ +} + +/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */ +static int +asm_byte_identical(const char *bin, const struct row *r, int i) +{ + char src[64], cs[64], ws[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/ail_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/ail_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/ail_asm_%d_%d_w.s", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c errored\n", r->label); + unlink(src); + return -1; + } + snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null", + bin, ws, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label); + unlink(src); unlink(cs); + return -1; + } + + FILE *fc = fopen(cs, "rb"); + FILE *fw = fopen(ws, "rb"); + int rc = 0; + if (!fc || !fw) { + rc = -1; + } else { + for (;;) { + int a = fgetc(fc); + int b = fgetc(fw); + if (a != b) { rc = -1; break; } + if (a == EOF) break; + } + } + if (fc) fclose(fc); + if (fw) fclose(fw); + if (rc != 0) + fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n", + r->label); + unlink(src); unlink(cs); unlink(ws); + return rc; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[1024]; + if (bin[0] != '/') { + char cwd[1024]; + if (getcwd(cwd, sizeof cwd) == NULL) return 1; + snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); + bin = absbin; + } + + char cdrv[1024]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + char wdrv[1024]; + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + struct { const char *name; const char *path; int gated_on_existence; } + drivers[] = { + { "cstage", cdrv, 0 }, + { "wwstage", wdrv, 1 }, + { NULL, NULL, 0 }, + }; + + int n = (int)(sizeof rows / sizeof rows[0]); + int nn = (int)(sizeof neg / sizeof neg[0]); + int total = 0, fail = 0; + + for (int d = 0; drivers[d].name; d++) { + if (drivers[d].gated_on_existence + && access(drivers[d].path, X_OK) != 0) { + fprintf(stderr, "arr_infer_len: skip %s (no %s)\n", + drivers[d].name, drivers[d].path); + continue; + } + for (int i = 0; i < n; i++) { + int got = run_driver(drivers[d].path, &rows[i], i); + total++; + if (got != rows[i].want) { + fprintf(stderr, + "arr_infer_len[%s][%s]: exit=%d want=%d\n", + drivers[d].name, rows[i].label, + got, rows[i].want); + fail++; + } + } + for (int i = 0; i < nn; i++) { + total++; + if (build_should_fail(drivers[d].path, neg[i], + 100 + i) != 0) { + fprintf(stderr, + "arr_infer_len[%s][neg%d]: built ok, " + "expected a loud error\n", + drivers[d].name, i); + fail++; + } + } + } + + if (access(wdrv, X_OK) == 0) { + for (int i = 0; i < n; i++) { + total++; + if (asm_byte_identical(bin, &rows[i], i) != 0) + fail++; + } + } + + if (fail) { + fprintf(stderr, + "arr_infer_len: %d/%d fixtures failed\n", fail, total); + return 1; + } + printf("arr_infer_len: %d/%d ok\n", total, total); + return 0; +}