w6c+wwstage: narrow int/rune array-literal elements to the declared type (#251)

`let a:[4]u8=[65,66,67,68]`, `def D:[4]u8=['A',..]`, and `enc{m=[65,..]}`
rejected with "init [4]i32 not assignable to declared [4]u8": an array
literal's element type came from the elements via type_default (int-lit
-> i32, rune-lit -> rune) with no declared-element-type propagation. The
scalar path already narrows (`let c:u8='A'`); only array aggregation at
the let/def/struct-field sites #130 (test 920) left unwired did not.

Fix = the int/rune analogue of coerce_floatlit, realised as the EXISTING
#130 accept-if-fits range-check — NOT a node-type restamp. cgen drives
the array element WIDTH from the declared type at every site (cgen.c
local-let lu->sub, emit_array_data d->type), so a restamp would be dead
code (the array literal keeps its [N]i32/[N]rune node type; the cs==ww
byte-id gate confirms the bytes emit u8-wide regardless). Per element:
foldable int/rune literal -> defcastfits range-check vs declared T
(in-range accept, out-of-range REJECT loud, rule-7); non-foldable ->
type_assignable / isassignable.

cstage (check.c): wire arrlit_init_fits into clet (local let),
struct-field-init, and def-init — the three sites the #130 module-let
path already covered.

wwstage (check.ww): factor checkletassign's inline #130 block into
checkarrlitfits and call it from the let path, the def path, and a
TARGETED array-field walk in the N_STRUCTLIT arm. This also closes a
pre-existing rule-7 wwstage over-accept: the def path ran NO init
assignability check and the N_STRUCTLIT head-stamp parks field
assignability (#23), so out-of-range / str array elements silently
over-accepted (a truncating miscompile) at those two sites. The
struct-field walk is the array-field accept-if-fits ONLY — it reuses the
stable N_TSTRUCT field-list walk (astoffset precedent), isolated from
the broader parked #23 field-assignability walk.

Regenerated w6c + wwdump combined.ww (embed check.ww). New test 951
covers let/def/struct-field x int/rune accept (run + cs==ww byte-id) and
out-of-range/str reject (both stages). test-unit 237 + smoke green.
This commit is contained in:
2026-06-02 01:53:57 +09:00
parent 0fb4bae337
commit d56b7ca946
6 changed files with 601 additions and 157 deletions

View File

@@ -13181,6 +13181,38 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = {
let nti: *tinfo = tinfofornode(c, tnm);
if (nti != nil) { e.type_ = nti: *void; }
else { e.type_ = tinfofornode(c, tn): *void; };
// #251: range-check array-typed field inits
// (`enc{m=[65,..]}`). The head-stamp above is
// field-walk-free — general per-field assignability
// is parked behind #23. This is the ISOLATED
// array-field accept-if-fits ONLY: it reuses the
// stable N_TSTRUCT field-list walk (astoffset
// precedent), zero coupling to the parked #23 walk,
// and closes the rule-7 silent out-of-range truncate
// at this site (cstage check.c:1546 range-checks via
// arrlit_init_fits).
let stn: *node = resolvealias(c, tn);
if (stn != nil && stn.kind == nkind.N_TSTRUCT) {
let fi: *node = e.list;
for (fi != nil) {
if (fi.kind == nkind.N_FIELD
&& fi.lhs != nil
&& fi.lhs.kind == nkind.N_ARRLIT) {
let ftn: *node = nil;
let tf: *node = stn.list;
for (tf != nil) {
if (tf.kind == nkind.N_TFIELD) {
if (streq(tf.str, fi.str)) { ftn = tf.lhs; };
};
tf = tf.next;
};
if (ftn != nil) {
checkarrlitfits(c, ftn, fi.lhs);
};
};
fi = fi.next;
};
};
return tn;
};
}; }; };
@@ -14017,6 +14049,71 @@ fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = {
c.errs += 1;
};
// checkarrlitfits — #130/#251 array-init accept-if-fits, shared by the
// let / def / struct-field init sites. arrtn is the declared [N]T type
// node, rhs the N_ARRLIT. Per element: foldable int/rune literal →
// defcastfits range-check against T (in-range accept, out-of-range LOUD
// reject — rule-7/Drew, Hare range-checks at the literal-value level);
// non-foldable → isassignable to the element type. Mirrors cstage
// cmd/wcc/check.c arrlit_init_fits. The element WIDTH is driven by the
// declared type at cgen, so no element-type restamp is needed — #251
// proved coerce_floatlit's restamp shape does NOT transfer (cgen reads
// the declared type's element size, not the literal node's stamp).
fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
if (arrtn == nil) { return; };
if (rhs == nil) { return; };
if (arrtn.kind != nkind.N_TARRAY) { return; };
if (rhs.kind != nkind.N_ARRLIT) { return; };
let elemtn: *node = arrtn.lhs;
let at: *tinfo = tinfofornode(c, arrtn);
let et: *tinfo = nil;
if (at != nil) { et = at.sub; };
for (et != nil && et.kind == tykind.TY_NAMED) { et = et.under; };
let e: *node = rhs.list;
for (e != nil) {
let skip: bool = false;
if (e.kind == nkind.N_FIELD) {
if (streq(e.str, "...")) { skip = true; };
};
if (!skip) {
let ev: *node = e;
for (ev != nil && ev.kind == nkind.N_CAST) { ev = ev.lhs; };
let v: u64 = 0u64;
let folded: bool = false;
if (et != nil) {
if (typeisint(et)) {
if (ev != nil) {
folded = foldintliteral(ev, &v);
};
};
};
if (folded) {
if (!defcastfits(et, v)) {
let m: str = "array element out of range\n";
os.write(2, m.ptr, m.len: u64);
c.errs += 1;
return;
};
} else {
let est: *node = exprtype(c, ev, elemtn);
let conf2: bool = false;
if (est != nil) {
if (!isassignable(c, elemtn, est, &conf2)) {
if (conf2) {
// #206: direct `&fn` array element.
if (!assignableaddrfn(c, elemtn, ev)) {
errnotassign(c, elemtn, est, "array element");
return;
};
};
};
};
};
};
e = e.next;
};
};
fn checkletassign(c: *checker, n: *node) void = {
if (n == nil) { return; };
if (n.rhs == nil) { return; }; // no init
@@ -14098,58 +14195,9 @@ fn checkletassign(c: *checker, n: *node) void = {
// (#146 merged). Mirrors cstage check.c arrlit_init_fits. Scoped
// to the array path; scalar-init range-check is a separate
// language-wide gap (#148).
if (n.lhs.kind == nkind.N_TARRAY) {
if (n.rhs.kind == nkind.N_ARRLIT) {
let elemtn: *node = n.lhs.lhs;
let at: *tinfo = tinfofornode(c, n.lhs);
let et: *tinfo = nil;
if (at != nil) { et = at.sub; };
for (et != nil && et.kind == tykind.TY_NAMED) { et = et.under; };
let e: *node = n.rhs.list;
for (e != nil) {
let skip: bool = false;
if (e.kind == nkind.N_FIELD) {
if (streq(e.str, "...")) { skip = true; };
};
if (!skip) {
let ev: *node = e;
for (ev != nil && ev.kind == nkind.N_CAST) { ev = ev.lhs; };
let v: u64 = 0u64;
let folded: bool = false;
if (et != nil) {
if (typeisint(et)) {
if (ev != nil) {
folded = foldintliteral(ev, &v);
};
};
};
if (folded) {
if (!defcastfits(et, v)) {
let m: str = "let: array element out of range\n";
os.write(2, m.ptr, m.len: u64);
c.errs += 1;
return;
};
} else {
let est: *node = exprtype(c, ev, elemtn);
let conf2: bool = false;
if (est != nil) {
if (!isassignable(c, elemtn, est, &conf2)) {
if (conf2) {
// #206: direct `&fn` array element.
if (!assignableaddrfn(c, elemtn, ev)) {
errnotassign(c, elemtn, est, "let");
return;
};
};
};
};
};
};
e = e.next;
};
return;
};
if (n.lhs.kind == nkind.N_TARRAY && n.rhs.kind == nkind.N_ARRLIT) {
checkarrlitfits(c, n.lhs, n.rhs);
return;
};
let conf: bool = false;
let ok: bool = isassignable(c, n.lhs, src, &conf);
@@ -14559,6 +14607,15 @@ export fn checkfile(c: *checker, file: *node) void = {
} else { if (k == nkind.N_DEF) {
if (d.lhs != nil) { resolvewalk(c, d.lhs); };
if (d.rhs != nil) { resolvewalk(c, d.rhs); };
// #251: `def D: [N]T = [int/rune lits]` array-init
// accept-if-fits. The wwstage def path otherwise runs NO
// init-assignability check (cstage check.c:2424 does), so
// an out-of-range / str element silently over-accepted
// (rule-7). Same predicate as the let path; scoped to the
// array-init shape (a no-op for every other def).
if (d.lhs != nil && d.rhs != nil) {
checkarrlitfits(c, d.lhs, d.rhs);
};
// #88: const-fold sibling/imported def refs, casts, and
// arithmetic so cgen's literal-only emitdefconstants can
// lay down the DATA row. GATED on the plain literal fold

View File

@@ -2776,6 +2776,38 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = {
let nti: *tinfo = tinfofornode(c, tnm);
if (nti != nil) { e.type_ = nti: *void; }
else { e.type_ = tinfofornode(c, tn): *void; };
// #251: range-check array-typed field inits
// (`enc{m=[65,..]}`). The head-stamp above is
// field-walk-free — general per-field assignability
// is parked behind #23. This is the ISOLATED
// array-field accept-if-fits ONLY: it reuses the
// stable N_TSTRUCT field-list walk (astoffset
// precedent), zero coupling to the parked #23 walk,
// and closes the rule-7 silent out-of-range truncate
// at this site (cstage check.c:1546 range-checks via
// arrlit_init_fits).
let stn: *node = resolvealias(c, tn);
if (stn != nil && stn.kind == nkind.N_TSTRUCT) {
let fi: *node = e.list;
for (fi != nil) {
if (fi.kind == nkind.N_FIELD
&& fi.lhs != nil
&& fi.lhs.kind == nkind.N_ARRLIT) {
let ftn: *node = nil;
let tf: *node = stn.list;
for (tf != nil) {
if (tf.kind == nkind.N_TFIELD) {
if (streq(tf.str, fi.str)) { ftn = tf.lhs; };
};
tf = tf.next;
};
if (ftn != nil) {
checkarrlitfits(c, ftn, fi.lhs);
};
};
fi = fi.next;
};
};
return tn;
};
}; }; };
@@ -3612,6 +3644,71 @@ fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = {
c.errs += 1;
};
// checkarrlitfits — #130/#251 array-init accept-if-fits, shared by the
// let / def / struct-field init sites. arrtn is the declared [N]T type
// node, rhs the N_ARRLIT. Per element: foldable int/rune literal →
// defcastfits range-check against T (in-range accept, out-of-range LOUD
// reject — rule-7/Drew, Hare range-checks at the literal-value level);
// non-foldable → isassignable to the element type. Mirrors cstage
// cmd/wcc/check.c arrlit_init_fits. The element WIDTH is driven by the
// declared type at cgen, so no element-type restamp is needed — #251
// proved coerce_floatlit's restamp shape does NOT transfer (cgen reads
// the declared type's element size, not the literal node's stamp).
fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
if (arrtn == nil) { return; };
if (rhs == nil) { return; };
if (arrtn.kind != nkind.N_TARRAY) { return; };
if (rhs.kind != nkind.N_ARRLIT) { return; };
let elemtn: *node = arrtn.lhs;
let at: *tinfo = tinfofornode(c, arrtn);
let et: *tinfo = nil;
if (at != nil) { et = at.sub; };
for (et != nil && et.kind == tykind.TY_NAMED) { et = et.under; };
let e: *node = rhs.list;
for (e != nil) {
let skip: bool = false;
if (e.kind == nkind.N_FIELD) {
if (streq(e.str, "...")) { skip = true; };
};
if (!skip) {
let ev: *node = e;
for (ev != nil && ev.kind == nkind.N_CAST) { ev = ev.lhs; };
let v: u64 = 0u64;
let folded: bool = false;
if (et != nil) {
if (typeisint(et)) {
if (ev != nil) {
folded = foldintliteral(ev, &v);
};
};
};
if (folded) {
if (!defcastfits(et, v)) {
let m: str = "array element out of range\n";
os.write(2, m.ptr, m.len: u64);
c.errs += 1;
return;
};
} else {
let est: *node = exprtype(c, ev, elemtn);
let conf2: bool = false;
if (est != nil) {
if (!isassignable(c, elemtn, est, &conf2)) {
if (conf2) {
// #206: direct `&fn` array element.
if (!assignableaddrfn(c, elemtn, ev)) {
errnotassign(c, elemtn, est, "array element");
return;
};
};
};
};
};
};
e = e.next;
};
};
fn checkletassign(c: *checker, n: *node) void = {
if (n == nil) { return; };
if (n.rhs == nil) { return; }; // no init
@@ -3693,58 +3790,9 @@ fn checkletassign(c: *checker, n: *node) void = {
// (#146 merged). Mirrors cstage check.c arrlit_init_fits. Scoped
// to the array path; scalar-init range-check is a separate
// language-wide gap (#148).
if (n.lhs.kind == nkind.N_TARRAY) {
if (n.rhs.kind == nkind.N_ARRLIT) {
let elemtn: *node = n.lhs.lhs;
let at: *tinfo = tinfofornode(c, n.lhs);
let et: *tinfo = nil;
if (at != nil) { et = at.sub; };
for (et != nil && et.kind == tykind.TY_NAMED) { et = et.under; };
let e: *node = n.rhs.list;
for (e != nil) {
let skip: bool = false;
if (e.kind == nkind.N_FIELD) {
if (streq(e.str, "...")) { skip = true; };
};
if (!skip) {
let ev: *node = e;
for (ev != nil && ev.kind == nkind.N_CAST) { ev = ev.lhs; };
let v: u64 = 0u64;
let folded: bool = false;
if (et != nil) {
if (typeisint(et)) {
if (ev != nil) {
folded = foldintliteral(ev, &v);
};
};
};
if (folded) {
if (!defcastfits(et, v)) {
let m: str = "let: array element out of range\n";
os.write(2, m.ptr, m.len: u64);
c.errs += 1;
return;
};
} else {
let est: *node = exprtype(c, ev, elemtn);
let conf2: bool = false;
if (est != nil) {
if (!isassignable(c, elemtn, est, &conf2)) {
if (conf2) {
// #206: direct `&fn` array element.
if (!assignableaddrfn(c, elemtn, ev)) {
errnotassign(c, elemtn, est, "let");
return;
};
};
};
};
};
};
e = e.next;
};
return;
};
if (n.lhs.kind == nkind.N_TARRAY && n.rhs.kind == nkind.N_ARRLIT) {
checkarrlitfits(c, n.lhs, n.rhs);
return;
};
let conf: bool = false;
let ok: bool = isassignable(c, n.lhs, src, &conf);
@@ -4154,6 +4202,15 @@ export fn checkfile(c: *checker, file: *node) void = {
} else { if (k == nkind.N_DEF) {
if (d.lhs != nil) { resolvewalk(c, d.lhs); };
if (d.rhs != nil) { resolvewalk(c, d.rhs); };
// #251: `def D: [N]T = [int/rune lits]` array-init
// accept-if-fits. The wwstage def path otherwise runs NO
// init-assignability check (cstage check.c:2424 does), so
// an out-of-range / str element silently over-accepted
// (rule-7). Same predicate as the let path; scoped to the
// array-init shape (a no-op for every other def).
if (d.lhs != nil && d.rhs != nil) {
checkarrlitfits(c, d.lhs, d.rhs);
};
// #88: const-fold sibling/imported def refs, casts, and
// arithmetic so cgen's literal-only emitdefconstants can
// lay down the DATA row. GATED on the plain literal fold

View File

@@ -13181,6 +13181,38 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = {
let nti: *tinfo = tinfofornode(c, tnm);
if (nti != nil) { e.type_ = nti: *void; }
else { e.type_ = tinfofornode(c, tn): *void; };
// #251: range-check array-typed field inits
// (`enc{m=[65,..]}`). The head-stamp above is
// field-walk-free — general per-field assignability
// is parked behind #23. This is the ISOLATED
// array-field accept-if-fits ONLY: it reuses the
// stable N_TSTRUCT field-list walk (astoffset
// precedent), zero coupling to the parked #23 walk,
// and closes the rule-7 silent out-of-range truncate
// at this site (cstage check.c:1546 range-checks via
// arrlit_init_fits).
let stn: *node = resolvealias(c, tn);
if (stn != nil && stn.kind == nkind.N_TSTRUCT) {
let fi: *node = e.list;
for (fi != nil) {
if (fi.kind == nkind.N_FIELD
&& fi.lhs != nil
&& fi.lhs.kind == nkind.N_ARRLIT) {
let ftn: *node = nil;
let tf: *node = stn.list;
for (tf != nil) {
if (tf.kind == nkind.N_TFIELD) {
if (streq(tf.str, fi.str)) { ftn = tf.lhs; };
};
tf = tf.next;
};
if (ftn != nil) {
checkarrlitfits(c, ftn, fi.lhs);
};
};
fi = fi.next;
};
};
return tn;
};
}; }; };
@@ -14017,6 +14049,71 @@ fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = {
c.errs += 1;
};
// checkarrlitfits — #130/#251 array-init accept-if-fits, shared by the
// let / def / struct-field init sites. arrtn is the declared [N]T type
// node, rhs the N_ARRLIT. Per element: foldable int/rune literal →
// defcastfits range-check against T (in-range accept, out-of-range LOUD
// reject — rule-7/Drew, Hare range-checks at the literal-value level);
// non-foldable → isassignable to the element type. Mirrors cstage
// cmd/wcc/check.c arrlit_init_fits. The element WIDTH is driven by the
// declared type at cgen, so no element-type restamp is needed — #251
// proved coerce_floatlit's restamp shape does NOT transfer (cgen reads
// the declared type's element size, not the literal node's stamp).
fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
if (arrtn == nil) { return; };
if (rhs == nil) { return; };
if (arrtn.kind != nkind.N_TARRAY) { return; };
if (rhs.kind != nkind.N_ARRLIT) { return; };
let elemtn: *node = arrtn.lhs;
let at: *tinfo = tinfofornode(c, arrtn);
let et: *tinfo = nil;
if (at != nil) { et = at.sub; };
for (et != nil && et.kind == tykind.TY_NAMED) { et = et.under; };
let e: *node = rhs.list;
for (e != nil) {
let skip: bool = false;
if (e.kind == nkind.N_FIELD) {
if (streq(e.str, "...")) { skip = true; };
};
if (!skip) {
let ev: *node = e;
for (ev != nil && ev.kind == nkind.N_CAST) { ev = ev.lhs; };
let v: u64 = 0u64;
let folded: bool = false;
if (et != nil) {
if (typeisint(et)) {
if (ev != nil) {
folded = foldintliteral(ev, &v);
};
};
};
if (folded) {
if (!defcastfits(et, v)) {
let m: str = "array element out of range\n";
os.write(2, m.ptr, m.len: u64);
c.errs += 1;
return;
};
} else {
let est: *node = exprtype(c, ev, elemtn);
let conf2: bool = false;
if (est != nil) {
if (!isassignable(c, elemtn, est, &conf2)) {
if (conf2) {
// #206: direct `&fn` array element.
if (!assignableaddrfn(c, elemtn, ev)) {
errnotassign(c, elemtn, est, "array element");
return;
};
};
};
};
};
};
e = e.next;
};
};
fn checkletassign(c: *checker, n: *node) void = {
if (n == nil) { return; };
if (n.rhs == nil) { return; }; // no init
@@ -14098,58 +14195,9 @@ fn checkletassign(c: *checker, n: *node) void = {
// (#146 merged). Mirrors cstage check.c arrlit_init_fits. Scoped
// to the array path; scalar-init range-check is a separate
// language-wide gap (#148).
if (n.lhs.kind == nkind.N_TARRAY) {
if (n.rhs.kind == nkind.N_ARRLIT) {
let elemtn: *node = n.lhs.lhs;
let at: *tinfo = tinfofornode(c, n.lhs);
let et: *tinfo = nil;
if (at != nil) { et = at.sub; };
for (et != nil && et.kind == tykind.TY_NAMED) { et = et.under; };
let e: *node = n.rhs.list;
for (e != nil) {
let skip: bool = false;
if (e.kind == nkind.N_FIELD) {
if (streq(e.str, "...")) { skip = true; };
};
if (!skip) {
let ev: *node = e;
for (ev != nil && ev.kind == nkind.N_CAST) { ev = ev.lhs; };
let v: u64 = 0u64;
let folded: bool = false;
if (et != nil) {
if (typeisint(et)) {
if (ev != nil) {
folded = foldintliteral(ev, &v);
};
};
};
if (folded) {
if (!defcastfits(et, v)) {
let m: str = "let: array element out of range\n";
os.write(2, m.ptr, m.len: u64);
c.errs += 1;
return;
};
} else {
let est: *node = exprtype(c, ev, elemtn);
let conf2: bool = false;
if (est != nil) {
if (!isassignable(c, elemtn, est, &conf2)) {
if (conf2) {
// #206: direct `&fn` array element.
if (!assignableaddrfn(c, elemtn, ev)) {
errnotassign(c, elemtn, est, "let");
return;
};
};
};
};
};
};
e = e.next;
};
return;
};
if (n.lhs.kind == nkind.N_TARRAY && n.rhs.kind == nkind.N_ARRLIT) {
checkarrlitfits(c, n.lhs, n.rhs);
return;
};
let conf: bool = false;
let ok: bool = isassignable(c, n.lhs, src, &conf);
@@ -14559,6 +14607,15 @@ export fn checkfile(c: *checker, file: *node) void = {
} else { if (k == nkind.N_DEF) {
if (d.lhs != nil) { resolvewalk(c, d.lhs); };
if (d.rhs != nil) { resolvewalk(c, d.rhs); };
// #251: `def D: [N]T = [int/rune lits]` array-init
// accept-if-fits. The wwstage def path otherwise runs NO
// init-assignability check (cstage check.c:2424 does), so
// an out-of-range / str element silently over-accepted
// (rule-7). Same predicate as the let path; scoped to the
// array-init shape (a no-op for every other def).
if (d.lhs != nil && d.rhs != nil) {
checkarrlitfits(c, d.lhs, d.rhs);
};
// #88: const-fold sibling/imported def refs, casts, and
// arithmetic so cgen's literal-only emitdefconstants can
// lay down the DATA row. GATED on the plain literal fold