wcc/check: #141 def-dim array as struct field — fold def in dim, shared arrayelen across 3 ww readers (both stages)

A def-dimensioned array [MAX]u8 used as a struct field was BOTH-WRONG: cstage
loud-rejected ("array length must be an integer literal"); wwstage silently
sized the dim to 0, so the next field overlapped it (frame-smash). The
reference is neither stage — it is Hare: accept + fold the def.

cstage: fold the def into the dim via eval_def_const. The fold needs def NAMES
visible when resolve_typedecl walks struct bodies, so a stub loop binds
def-name stubs (type=NULL, filled in place by the existing def loop) before
resolve_typedecl — this extends check_file's existing names-first USE+TYPEDECL
pass to DEFs; def-TYPE resolution stays in its original order, and the
kind-filtered type lookup (#225) keeps the SK_DEF stub out of type position.

wwstage: one shared arrayelen(c, rhs) (INTLIT -> uval; else evaldefconst;
else 0) routed through astsize / tinfofornode / checkarrlitfits.

Closes #13's def-dim cstage-reject half (the slice-repeat clause stays open).
Pin test/wcc/951 (5 rows incl a cross-module os.PATH_MAX dim + a ~4KB shape;
teeth = cstage loud-reject + ww frame-smash). cgen-first blocker for the
path::buffer arc (type buffer = struct{[MAX]u8, ...}).
This commit is contained in:
2026-06-08 01:00:29 +09:00
parent 620e733444
commit f1dcd4ecae
6 changed files with 382 additions and 44 deletions

View File

@@ -467,6 +467,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_dotbase_arr_run \ $(BIN)/test_dotbase_arr_run \
$(BIN)/test_dotbase_addr_slice_run \ $(BIN)/test_dotbase_addr_slice_run \
$(BIN)/test_structlit_arrfield_run \ $(BIN)/test_structlit_arrfield_run \
$(BIN)/test_defdim_struct_run \
$(BIN)/test_arraytoslice_run \ $(BIN)/test_arraytoslice_run \
$(BIN)/test_arrlit_slice_run \ $(BIN)/test_arrlit_slice_run \
$(BIN)/test_valstruct_subsize_run \ $(BIN)/test_valstruct_subsize_run \
@@ -2133,6 +2134,11 @@ $(BIN)/test_structlit_arrfield_run: test/wcc/949_structlit_arrfield_run.c \
$(LIB)/libwwrt.a | $(BIN) $(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $< $(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_defdim_struct_run: test/wcc/951_defdim_struct_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_arraytoslice_run: test/wcc/953_arraytoslice_run.c \ $(BIN)/test_arraytoslice_run: test/wcc/953_arraytoslice_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
$(LIB)/libwwrt.a | $(BIN) $(LIB)/libwwrt.a | $(BIN)

View File

@@ -653,13 +653,19 @@ resolve_type(Checker *c, Node *n)
case N_TSLICE: case N_TSLICE:
return type_slice(c->a, resolve_type(c, n->lhs)); return type_slice(c->a, resolve_type(c, n->lhs));
case N_TARRAY: { case N_TARRAY: {
u64 len = 0; u64 len = 0, v;
if (n->rhs == NULL) { if (n->rhs == NULL) {
/* `[_]T` — length inferred at the use site (currently /* `[_]T` — length inferred at the use site (currently
* only `let x: [_]T = arrlit;`). Leave alen=0 as a * only `let x: [_]T = arrlit;`). Leave alen=0 as a
* sentinel; clet patches it from the initialiser. */ * sentinel; clet patches it from the initialiser. */
} else if (n->rhs->kind == N_INTLIT) { } else if (n->rhs->kind == N_INTLIT) {
len = n->rhs->uval; len = n->rhs->uval;
} else if (eval_def_const(c, n->rhs, &v, 0)) {
/* #141: a def-dimensioned `[MAX]u8`; fold the
* const-expr dimension (the same machinery #133's
* let-init fold uses). The err below stays for a
* genuinely non-const rhs. */
len = v;
} else { } else {
err(c, n->pos, "array length must be an integer literal"); err(c, n->pos, "array length must be an integer literal");
} }
@@ -2716,6 +2722,29 @@ check_file(Checker *c, Node *file)
} }
d->type = named; d->type = named;
} }
/* #141: bind def NAMES before resolving type bodies, so a struct
* field `[MAX]u8` whose dimension is a def-ref folds via
* eval_def_const (which reads decl->rhs) when resolve_typedecl
* walks the body below. The def's type is resolved in the
* decl loop further down; only the name->decl binding is needed
* here. A foldable stub carries type NULL until then. A duplicate
* (prev already bound non-USE) is left for that loop to diagnose. */
c->cur_mod = NULL;
for (Node *d = file->list; d; d = d->next) {
if (d->kind != N_DEF) continue;
c->cur_mod = decl_mod(file, d);
const char *mod = decl_mod(file, d);
Sym *prev = scope_lookup_local(c->cur, d->str);
if (prev && prev->kind == SK_USE) {
prev->kind = SK_DEF; prev->decl = d;
prev->use_alias = 1;
if (mod && prev->mod == NULL) prev->mod = mod;
} else if (prev == NULL) {
scope_define_in_module(c->cur, d->str, mod,
SK_DEF, NULL, d);
}
}
c->cur_mod = NULL;
for (Node *d = file->list; d; d = d->next) { for (Node *d = file->list; d; d = d->next) {
if (d->kind != N_TYPEDECL) continue; if (d->kind != N_TYPEDECL) continue;
resolve_typedecl(c, d); resolve_typedecl(c, d);
@@ -2733,7 +2762,11 @@ check_file(Checker *c, Node *file)
d->type = t; d->type = t;
Sym *prev = scope_lookup_local(c->cur, d->str); Sym *prev = scope_lookup_local(c->cur, d->str);
const char *mod = decl_mod(file, d); const char *mod = decl_mod(file, d);
if (prev && prev->kind == SK_USE) { if (prev && prev->kind == SK_DEF && prev->decl == d) {
/* #141: the foldable stub bound before type-body
* resolution; fill in its now-resolved type. */
prev->type = t;
} else if (prev && prev->kind == SK_USE) {
/* `use mod; ... def mod = ...;` — promote the /* `use mod; ... def mod = ...;` — promote the
* SK_USE to the def symbol but remember it was * SK_USE to the def symbol but remember it was
* also a module name so dotted qualifiers * also a module name so dotted qualifiers

View File

@@ -11387,10 +11387,10 @@ fn astsize(c: *checker, t: *node) i64 = {
if (k == nkind.N_TCHAN) { return 8i64; }; if (k == nkind.N_TCHAN) { return 8i64; };
if (k == nkind.N_TFN) { return 8i64; }; if (k == nkind.N_TFN) { return 8i64; };
if (k == nkind.N_TARRAY) { if (k == nkind.N_TARRAY) {
let elen: i64 = 0i64; // #141: a def-dimensioned field array sized to 0 here, so the
if (t.rhs != nil) { // struct loop (off += astsize(field)) overlapped the next
if (t.rhs.kind == nkind.N_INTLIT) { elen = t.rhs.uval: i64; }; // field; arrayelen folds the def.
}; let elen: i64 = arrayelen(c, t.rhs): i64;
return astsize(c, t.lhs) * elen; return astsize(c, t.lhs) * elen;
}; };
if (k == nkind.N_TTUPLE) { if (k == nkind.N_TTUPLE) {
@@ -11823,6 +11823,23 @@ fn stampintlit(n: *node, v: u64) void = {
// n.type_ left intact (the type exprtype inferred for the rhs). // n.type_ left intact (the type exprtype inferred for the rhs).
}; };
// arrayelen — an array type's dimension as an element count. An
// N_INTLIT yields its uval; a def-ref or const-expr dim (`[MAX]u8`,
// MAX a def) folds through evaldefconst (#141, ken oracle); a nil
// child (the `[_]T` inferred-length sentinel) or non-const rhs gives
// 0. cstage carries the folded length in the resolved Type, but
// wwstage computes size lazily on independent paths (no AST-stamp
// SSoT), so every N_TARRAY length reader routes here to fold the dim
// identically — astsize (struct layout), tinfofornode (canonical
// tinfo), checkarrlitfits (count gate).
fn arrayelen(c: *checker, rhs: *node) u64 = {
if (rhs == nil) { return 0u64; };
if (rhs.kind == nkind.N_INTLIT) { return rhs.uval; };
let v: u64 = 0u64;
if (evaldefconst(c, rhs, &v, 0)) { return v; };
return 0u64;
};
// enumvalfold — fold an enum member's value expression to a u64 // enumvalfold — fold an enum member's value expression to a u64
// constant. The Hare-fidelity set: literal leaves, unary +/-/~, // constant. The Hare-fidelity set: literal leaves, unary +/-/~,
// binary arithmetic (+ - * / %), bitwise (& | ^), shifts (<< >>), // binary arithmetic (+ - * / %), bitwise (& | ^), shifts (<< >>),
@@ -12159,10 +12176,7 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = {
// Reverts A.4's r.size override (which conflated stride with // Reverts A.4's r.size override (which conflated stride with
// natural size); the slot-padded stride now lives in slotsize // natural size); the slot-padded stride now lives in slotsize
// where cgenutil's fast-path reads it. // where cgenutil's fast-path reads it.
let elen: u64 = 0u64; let elen: u64 = arrayelen(c, n.rhs); // #141: fold def-dim
if (n.rhs != nil) {
if (n.rhs.kind == nkind.N_INTLIT) { elen = n.rhs.uval; };
};
let sub: *tinfo = tinfofornode(c, n.lhs); let sub: *tinfo = tinfofornode(c, n.lhs);
// #62/#69: `type a = [2]a` value cycle — loud, cstage twin. // #62/#69: `type a = [2]a` value cycle — loud, cstage twin.
if (circularnamed(c, sub, n)) { sub = c.tc.tyerr; }; if (circularnamed(c, sub, n)) { sub = c.tc.tyerr; };
@@ -14466,12 +14480,13 @@ fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
// A nil or zero length child stays exempt: nil is the un-inferred // A nil or zero length child stays exempt: nil is the un-inferred
// [_] sentinel in def/struct-field contexts and 0 doubles as both // [_] sentinel in def/struct-field contexts and 0 doubles as both
// [0] and the cstage [_] sentinel (conflation: task #11); the let // [0] and the cstage [_] sentinel (conflation: task #11); the let
// paths stamp the real length before reaching here. A non-INTLIT // paths stamp the real length before reaching here. #141: a def-dim
// length child (def-named [N]) is also exempt — task #13. // child (`[MAX]u8`) now folds via arrayelen and is count-checked
// like a literal (closes #13's def-dim exemption).
// Under-long (count < N, no `...`) stays accepted as before; Hare // Under-long (count < N, no `...`) stays accepted as before; Hare
// rejects it — task #10. // rejects it — task #10.
if (arrtn.rhs != nil && arrtn.rhs.kind == nkind.N_INTLIT let declen: u64 = arrayelen(c, arrtn.rhs);
&& arrtn.rhs.uval > 0u64) { if (declen > 0u64) {
let cnt: u64 = 0u64; let cnt: u64 = 0u64;
let ce: *node = rhs.list; let ce: *node = rhs.list;
for (ce != nil) { for (ce != nil) {
@@ -14482,11 +14497,11 @@ fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
if (!cskip) { cnt += 1u64; }; if (!cskip) { cnt += 1u64; };
ce = ce.next; ce = ce.next;
}; };
if (cnt > arrtn.rhs.uval) { if (cnt > declen) {
cerr("array literal has "); cerr("array literal has ");
cerr(strconv.u64tos(cnt, strconv.base.DEC)); cerr(strconv.u64tos(cnt, strconv.base.DEC));
cerr(" elements but declared array holds "); cerr(" elements but declared array holds ");
cerr(strconv.u64tos(arrtn.rhs.uval, strconv.base.DEC)); cerr(strconv.u64tos(declen, strconv.base.DEC));
cerr("\n"); cerr("\n");
c.errs += 1; c.errs += 1;
return; return;

View File

@@ -1106,10 +1106,10 @@ fn astsize(c: *checker, t: *node) i64 = {
if (k == nkind.N_TCHAN) { return 8i64; }; if (k == nkind.N_TCHAN) { return 8i64; };
if (k == nkind.N_TFN) { return 8i64; }; if (k == nkind.N_TFN) { return 8i64; };
if (k == nkind.N_TARRAY) { if (k == nkind.N_TARRAY) {
let elen: i64 = 0i64; // #141: a def-dimensioned field array sized to 0 here, so the
if (t.rhs != nil) { // struct loop (off += astsize(field)) overlapped the next
if (t.rhs.kind == nkind.N_INTLIT) { elen = t.rhs.uval: i64; }; // field; arrayelen folds the def.
}; let elen: i64 = arrayelen(c, t.rhs): i64;
return astsize(c, t.lhs) * elen; return astsize(c, t.lhs) * elen;
}; };
if (k == nkind.N_TTUPLE) { if (k == nkind.N_TTUPLE) {
@@ -1542,6 +1542,23 @@ fn stampintlit(n: *node, v: u64) void = {
// n.type_ left intact (the type exprtype inferred for the rhs). // n.type_ left intact (the type exprtype inferred for the rhs).
}; };
// arrayelen — an array type's dimension as an element count. An
// N_INTLIT yields its uval; a def-ref or const-expr dim (`[MAX]u8`,
// MAX a def) folds through evaldefconst (#141, ken oracle); a nil
// child (the `[_]T` inferred-length sentinel) or non-const rhs gives
// 0. cstage carries the folded length in the resolved Type, but
// wwstage computes size lazily on independent paths (no AST-stamp
// SSoT), so every N_TARRAY length reader routes here to fold the dim
// identically — astsize (struct layout), tinfofornode (canonical
// tinfo), checkarrlitfits (count gate).
fn arrayelen(c: *checker, rhs: *node) u64 = {
if (rhs == nil) { return 0u64; };
if (rhs.kind == nkind.N_INTLIT) { return rhs.uval; };
let v: u64 = 0u64;
if (evaldefconst(c, rhs, &v, 0)) { return v; };
return 0u64;
};
// enumvalfold — fold an enum member's value expression to a u64 // enumvalfold — fold an enum member's value expression to a u64
// constant. The Hare-fidelity set: literal leaves, unary +/-/~, // constant. The Hare-fidelity set: literal leaves, unary +/-/~,
// binary arithmetic (+ - * / %), bitwise (& | ^), shifts (<< >>), // binary arithmetic (+ - * / %), bitwise (& | ^), shifts (<< >>),
@@ -1878,10 +1895,7 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = {
// Reverts A.4's r.size override (which conflated stride with // Reverts A.4's r.size override (which conflated stride with
// natural size); the slot-padded stride now lives in slotsize // natural size); the slot-padded stride now lives in slotsize
// where cgenutil's fast-path reads it. // where cgenutil's fast-path reads it.
let elen: u64 = 0u64; let elen: u64 = arrayelen(c, n.rhs); // #141: fold def-dim
if (n.rhs != nil) {
if (n.rhs.kind == nkind.N_INTLIT) { elen = n.rhs.uval; };
};
let sub: *tinfo = tinfofornode(c, n.lhs); let sub: *tinfo = tinfofornode(c, n.lhs);
// #62/#69: `type a = [2]a` value cycle — loud, cstage twin. // #62/#69: `type a = [2]a` value cycle — loud, cstage twin.
if (circularnamed(c, sub, n)) { sub = c.tc.tyerr; }; if (circularnamed(c, sub, n)) { sub = c.tc.tyerr; };
@@ -4185,12 +4199,13 @@ fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
// A nil or zero length child stays exempt: nil is the un-inferred // A nil or zero length child stays exempt: nil is the un-inferred
// [_] sentinel in def/struct-field contexts and 0 doubles as both // [_] sentinel in def/struct-field contexts and 0 doubles as both
// [0] and the cstage [_] sentinel (conflation: task #11); the let // [0] and the cstage [_] sentinel (conflation: task #11); the let
// paths stamp the real length before reaching here. A non-INTLIT // paths stamp the real length before reaching here. #141: a def-dim
// length child (def-named [N]) is also exempt — task #13. // child (`[MAX]u8`) now folds via arrayelen and is count-checked
// like a literal (closes #13's def-dim exemption).
// Under-long (count < N, no `...`) stays accepted as before; Hare // Under-long (count < N, no `...`) stays accepted as before; Hare
// rejects it — task #10. // rejects it — task #10.
if (arrtn.rhs != nil && arrtn.rhs.kind == nkind.N_INTLIT let declen: u64 = arrayelen(c, arrtn.rhs);
&& arrtn.rhs.uval > 0u64) { if (declen > 0u64) {
let cnt: u64 = 0u64; let cnt: u64 = 0u64;
let ce: *node = rhs.list; let ce: *node = rhs.list;
for (ce != nil) { for (ce != nil) {
@@ -4201,11 +4216,11 @@ fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
if (!cskip) { cnt += 1u64; }; if (!cskip) { cnt += 1u64; };
ce = ce.next; ce = ce.next;
}; };
if (cnt > arrtn.rhs.uval) { if (cnt > declen) {
cerr("array literal has "); cerr("array literal has ");
cerr(strconv.u64tos(cnt, strconv.base.DEC)); cerr(strconv.u64tos(cnt, strconv.base.DEC));
cerr(" elements but declared array holds "); cerr(" elements but declared array holds ");
cerr(strconv.u64tos(arrtn.rhs.uval, strconv.base.DEC)); cerr(strconv.u64tos(declen, strconv.base.DEC));
cerr("\n"); cerr("\n");
c.errs += 1; c.errs += 1;
return; return;

View File

@@ -11387,10 +11387,10 @@ fn astsize(c: *checker, t: *node) i64 = {
if (k == nkind.N_TCHAN) { return 8i64; }; if (k == nkind.N_TCHAN) { return 8i64; };
if (k == nkind.N_TFN) { return 8i64; }; if (k == nkind.N_TFN) { return 8i64; };
if (k == nkind.N_TARRAY) { if (k == nkind.N_TARRAY) {
let elen: i64 = 0i64; // #141: a def-dimensioned field array sized to 0 here, so the
if (t.rhs != nil) { // struct loop (off += astsize(field)) overlapped the next
if (t.rhs.kind == nkind.N_INTLIT) { elen = t.rhs.uval: i64; }; // field; arrayelen folds the def.
}; let elen: i64 = arrayelen(c, t.rhs): i64;
return astsize(c, t.lhs) * elen; return astsize(c, t.lhs) * elen;
}; };
if (k == nkind.N_TTUPLE) { if (k == nkind.N_TTUPLE) {
@@ -11823,6 +11823,23 @@ fn stampintlit(n: *node, v: u64) void = {
// n.type_ left intact (the type exprtype inferred for the rhs). // n.type_ left intact (the type exprtype inferred for the rhs).
}; };
// arrayelen — an array type's dimension as an element count. An
// N_INTLIT yields its uval; a def-ref or const-expr dim (`[MAX]u8`,
// MAX a def) folds through evaldefconst (#141, ken oracle); a nil
// child (the `[_]T` inferred-length sentinel) or non-const rhs gives
// 0. cstage carries the folded length in the resolved Type, but
// wwstage computes size lazily on independent paths (no AST-stamp
// SSoT), so every N_TARRAY length reader routes here to fold the dim
// identically — astsize (struct layout), tinfofornode (canonical
// tinfo), checkarrlitfits (count gate).
fn arrayelen(c: *checker, rhs: *node) u64 = {
if (rhs == nil) { return 0u64; };
if (rhs.kind == nkind.N_INTLIT) { return rhs.uval; };
let v: u64 = 0u64;
if (evaldefconst(c, rhs, &v, 0)) { return v; };
return 0u64;
};
// enumvalfold — fold an enum member's value expression to a u64 // enumvalfold — fold an enum member's value expression to a u64
// constant. The Hare-fidelity set: literal leaves, unary +/-/~, // constant. The Hare-fidelity set: literal leaves, unary +/-/~,
// binary arithmetic (+ - * / %), bitwise (& | ^), shifts (<< >>), // binary arithmetic (+ - * / %), bitwise (& | ^), shifts (<< >>),
@@ -12159,10 +12176,7 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = {
// Reverts A.4's r.size override (which conflated stride with // Reverts A.4's r.size override (which conflated stride with
// natural size); the slot-padded stride now lives in slotsize // natural size); the slot-padded stride now lives in slotsize
// where cgenutil's fast-path reads it. // where cgenutil's fast-path reads it.
let elen: u64 = 0u64; let elen: u64 = arrayelen(c, n.rhs); // #141: fold def-dim
if (n.rhs != nil) {
if (n.rhs.kind == nkind.N_INTLIT) { elen = n.rhs.uval; };
};
let sub: *tinfo = tinfofornode(c, n.lhs); let sub: *tinfo = tinfofornode(c, n.lhs);
// #62/#69: `type a = [2]a` value cycle — loud, cstage twin. // #62/#69: `type a = [2]a` value cycle — loud, cstage twin.
if (circularnamed(c, sub, n)) { sub = c.tc.tyerr; }; if (circularnamed(c, sub, n)) { sub = c.tc.tyerr; };
@@ -14466,12 +14480,13 @@ fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
// A nil or zero length child stays exempt: nil is the un-inferred // A nil or zero length child stays exempt: nil is the un-inferred
// [_] sentinel in def/struct-field contexts and 0 doubles as both // [_] sentinel in def/struct-field contexts and 0 doubles as both
// [0] and the cstage [_] sentinel (conflation: task #11); the let // [0] and the cstage [_] sentinel (conflation: task #11); the let
// paths stamp the real length before reaching here. A non-INTLIT // paths stamp the real length before reaching here. #141: a def-dim
// length child (def-named [N]) is also exempt — task #13. // child (`[MAX]u8`) now folds via arrayelen and is count-checked
// like a literal (closes #13's def-dim exemption).
// Under-long (count < N, no `...`) stays accepted as before; Hare // Under-long (count < N, no `...`) stays accepted as before; Hare
// rejects it — task #10. // rejects it — task #10.
if (arrtn.rhs != nil && arrtn.rhs.kind == nkind.N_INTLIT let declen: u64 = arrayelen(c, arrtn.rhs);
&& arrtn.rhs.uval > 0u64) { if (declen > 0u64) {
let cnt: u64 = 0u64; let cnt: u64 = 0u64;
let ce: *node = rhs.list; let ce: *node = rhs.list;
for (ce != nil) { for (ce != nil) {
@@ -14482,11 +14497,11 @@ fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
if (!cskip) { cnt += 1u64; }; if (!cskip) { cnt += 1u64; };
ce = ce.next; ce = ce.next;
}; };
if (cnt > arrtn.rhs.uval) { if (cnt > declen) {
cerr("array literal has "); cerr("array literal has ");
cerr(strconv.u64tos(cnt, strconv.base.DEC)); cerr(strconv.u64tos(cnt, strconv.base.DEC));
cerr(" elements but declared array holds "); cerr(" elements but declared array holds ");
cerr(strconv.u64tos(arrtn.rhs.uval, strconv.base.DEC)); cerr(strconv.u64tos(declen, strconv.base.DEC));
cerr("\n"); cerr("\n");
c.errs += 1; c.errs += 1;
return; return;

View File

@@ -0,0 +1,254 @@
/*
* 951_defdim_struct_run — runtime + byte-id net for #141: a struct
* field whose array dimension is a `def` (`[MAX]u8`, MAX a same-module
* def), not an integer literal. BOTH stages were wrong, oppositely:
*
* cstage LOUD-rejected ("array length must be an integer literal").
* resolve_type's N_TARRAY arm folded only N_INTLIT dims; a def-ref
* fell through to the err. Root was a phase-ordering trap: def names
* weren't bound in scope when resolve_typedecl walked the struct
* body, so eval_def_const couldn't see MAX. Fix binds def-name stubs
* before type-body resolution, then folds the dim via eval_def_const.
*
* wwstage SILENTLY mis-laid-out the struct. astsize sized the def-dim
* field to 0 (only N_INTLIT dims were read), so the N_TSTRUCT
* `off += astsize(field)` loop gave the NEXT field the array's offset
* — the trailing scalar overlapped the array, reading/writing the
* wrong bytes (ken pdefM4: end != written). Fix routes every N_TARRAY
* length reader through a shared arrayelen() that folds the def.
*
* The TEETH row is the trailing scalar after the def-dim array: pre-fix
* ww read a corrupted `end`; post-fix it round-trips and cs==ww byte-id.
* Closes #13's def-dim half (the slice-repeat clause stays open).
*
* cstage `ww build` + run for exit code; w6c vs w6c_ww `.s` cmp for the
* rule-10 byte-id gate. The byte-id leg compiles the `ww build`-produced
* .combined.ww, not the raw src: w6c does no import resolution, so the
* cross-module row (C1, `import os`) only folds once concatenated, and a
* same-module row combines to itself. The last row is the ACTUAL
* path::buffer shape — a cross-module `os.PATH_MAX` def dimension (the
* N_DOT fold arm). u8 array elements + i32 trailing scalars only (the
* i32 return is MOVL/MOVSXD byte-id; see 949_dotbase_arr_run).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
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_exit; };
static const struct row rows[] = {
/* TEETH: def-dim array FOLLOWED by a scalar; write both, read the
* scalar back. Pre-fix ww laid `end` at the array's offset (array
* sized 0) → end read garbage. Post-fix end at offset 4 → 42. */
{ "m4_end",
"package main;\n"
"def M: i32 = 4;\n"
"type t = struct { b: [M]u8, end: i32 };\n"
"export fn main() i32 = {\n"
" let s: t;\n"
" s.b[3] = 9u8;\n"
" s.end = 42;\n"
" return s.end;\n"
"};\n", 42 },
/* the array element itself must survive too — read the last byte
* (pre-fix the overlapping `end` write would clobber it). */
{ "m4_belem",
"package main;\n"
"def M: i32 = 4;\n"
"type t = struct { b: [M]u8, end: i32 };\n"
"export fn main() i32 = {\n"
" let s: t;\n"
" s.b[3] = 200u8;\n"
" s.end = 7;\n"
" return s.b[3]: i32;\n"
"};\n", 200 },
/* the ~4KB path::buffer shape — def MAX=4095, read the trailing
* scalar after a 4095-byte array. */
{ "m4095_end",
"package main;\n"
"def MAX: i32 = 4095;\n"
"type t = struct { b: [MAX]u8, end: i32 };\n"
"export fn main() i32 = {\n"
" let s: t;\n"
" s.b[4094] = 5u8;\n"
" s.end = 99;\n"
" return s.end;\n"
"};\n", 99 },
/* two scalars after the def-dim array — pins the cumulative offset
* (a+c read their written values: 11+22=33). */
{ "twoafter",
"package main;\n"
"def M: i32 = 4;\n"
"type t = struct { b: [M]u8, a: i32, c: i32 };\n"
"export fn main() i32 = {\n"
" let s: t;\n"
" s.a = 11;\n"
" s.c = 22;\n"
" return s.a + s.c;\n"
"};\n", 33 },
/* C1 (rob-mandatory): the ACTUAL path::buffer shape — a CROSS-
* MODULE def dimension. `def MAX = os.PATH_MAX - 1` folds an N_DOT
* (os.PATH_MAX) const-ref through eval_def_const's cross-module arm,
* which the same-module rows don't exercise. os.PATH_MAX is 4096, so
* MAX is 4095. Byte-id runs on the ww-build combined.ww (w6c alone
* does no import resolution, so it can't compile the raw `import os`
* source). */
{ "xmod_pathmax",
"package main;\n"
"import os;\n"
"def MAX: i32 = os.PATH_MAX - 1;\n"
"type t = struct { b: [MAX]u8, end: i32 };\n"
"export fn main() i32 = {\n"
" let s: t;\n"
" s.b[4094] = 7u8;\n"
" s.end = 77;\n"
" return s.end;\n"
"};\n", 77 },
{ NULL, NULL, 0 }
};
static int
slurp_eq(const char *a, const char *b)
{
FILE *fa = fopen(a, "rb");
FILE *fb = fopen(b, "rb");
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
int rc = 0;
for (;;) {
int ca = fgetc(fa);
int cb = fgetc(fb);
if (ca != cb) { rc = -1; break; }
if (ca == EOF) break;
}
fclose(fa); fclose(fb);
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 w6c[1100], w6c_ww[1100];
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
if (access(w6c_ww, X_OK) != 0) {
fprintf(stderr, "defdim_struct: w6c_ww missing — cannot run "
"the cs==ww byte-id gate (the whole point of this test)\n");
return 1;
}
int n = 0, fail = 0;
for (int i = 0; rows[i].src; i++, n++) {
char src[64];
snprintf(src, sizeof src, "/tmp/wwdds_%d_%d.ww", getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; continue; }
fputs(rows[i].src, f);
fclose(f);
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwdds_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
char cmd[2048];
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s",
tmpdir, bin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: cstage build failed\n",
rows[i].label);
fail++;
unlink(src); rmdir(tmpdir);
continue;
}
char base[64];
const char *b = strrchr(src, '/');
b = b ? b + 1 : src;
snprintf(base, sizeof base, "%s", b);
char *dot = strrchr(base, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
char outbin[160], combined[96], arto[96], arts[96];
/* `ww build` writes the runnable binary into the build cwd
* (tmpdir) but its intermediates — the concatenated module
* source .combined.ww, plus .o/.s — next to the SOURCE. The
* byte-id leg compiles that combined form, not the raw src:
* w6c does no import resolution, so the cross-module row's
* `import os` only folds once concatenated. A same-module row
* combines to itself, so the one path serves every row. */
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
snprintf(combined, sizeof combined,
"/tmp/wwdds_%d_%d.combined.ww", getpid(), i);
snprintf(arto, sizeof arto, "/tmp/wwdds_%d_%d.o", getpid(), i);
snprintf(arts, sizeof arts, "/tmp/wwdds_%d_%d.s", getpid(), i);
int got = runwait(outbin);
if (got != rows[i].want_exit) {
fprintf(stderr, "row[%s]: cstage exit %d, want %d\n",
rows[i].label, got, rows[i].want_exit);
fail++;
}
char cs_s[64], ws_s[64];
snprintf(cs_s, sizeof cs_s, "/tmp/wwdds_%d_%d_cs.s",
getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwdds_%d_%d_ww.s",
getpid(), i);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
w6c, cs_s, combined);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
fail++;
} else {
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
w6c_ww, ws_s, combined);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww failed\n",
rows[i].label);
fail++;
} else if (slurp_eq(cs_s, ws_s) != 0) {
fprintf(stderr,
"row[%s]: cstage/wwstage .s DIFFER "
"(rule-10 byte-id violation)\n",
rows[i].label);
fail++;
}
}
unlink(outbin); rmdir(tmpdir);
unlink(combined); unlink(arto); unlink(arts);
unlink(src); unlink(cs_s); unlink(ws_s);
}
if (fail) {
fprintf(stderr, "%d/%d defdim-struct tests failed\n",
fail, n);
return 1;
}
printf("defdim_struct: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}