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

@@ -414,6 +414,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_struct_composite_init_run \
$(BIN)/test_array_static_init_run \
$(BIN)/test_array_init_acceptiffits_run \
$(BIN)/test_arrlit_elem_narrow_run \
$(BIN)/test_amp_def_global_run \
$(BIN)/test_f64cgen_run \
$(BIN)/test_f64crossmod_run \
@@ -1654,6 +1655,11 @@ $(BIN)/test_array_init_acceptiffits_run: test/wcc/920_array_init_acceptiffits_ru
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_arrlit_elem_narrow_run: test/wcc/951_arrlit_elem_narrow_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_amp_def_global_run: test/wcc/921_amp_def_global_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
$(LIB)/libwwrt.a | $(BIN)

View File

@@ -1542,6 +1542,7 @@ cexpr(Checker *c, Node *n)
f->str, type_name(c->a, t));
else if (vt != ty_err &&
!type_assignable(match->type, vt) &&
!arrlit_init_fits(c, match->type, f->lhs) &&
!assignable_addrfn(c, match->type, f->lhs))
err(c, f->pos, "field %s: %s not assignable to %s",
f->str, type_name(c->a, vt),
@@ -1897,6 +1898,7 @@ clet(Checker *c, Node *n)
}
if (declared && initt && initt != ty_err && !has_arr_repeat &&
!type_assignable(declared, initt) &&
!arrlit_init_fits(c, declared, n->rhs) &&
!assignable_addrfn(c, declared, n->rhs))
err(c, n->pos, "init %s not assignable to declared %s",
type_name(c->a, initt), type_name(c->a, declared));
@@ -2422,7 +2424,8 @@ check_file(Checker *c, Node *file)
&& def_cast_fits(d->type, dv))
art = ty_untyped_int;
if (d->type && rt != ty_err && d->type != ty_err
&& !type_assignable(d->type, art))
&& !type_assignable(d->type, art)
&& !arrlit_init_fits(c, d->type, d->rhs))
err(c, d->pos, "def %s init %s not assignable to %s",
d->str, type_name(c->a, rt),
type_name(c->a, d->type));

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

View File

@@ -0,0 +1,264 @@
/*
* 951_arrlit_elem_narrow_run — #251: narrow int/rune array-literal
* elements to a declared [N]T element type, at the let / def /
* struct-field init sites that #130 (test 920) left unwired.
*
* Pre-#251 state (verified by building both stages):
* - cstage REJECTED in-range bare-int/rune array lits at the LOCAL
* let, def, and struct-field sites ("init [4]i32 not assignable to
* declared [4]u8"): the array-literal element type came from the
* elements via type_default (int-lit→i32, rune-lit→rune) with no
* declared-element-type propagation. Only the MODULE-level let site
* (#130) accepted.
* - wwstage was UNEVEN: local/module let correct, but the DEF path ran
* NO init-assignability check and the N_STRUCTLIT head-stamp left
* field assignability parked (#23) — so def + struct-field SILENTLY
* over-accepted out-of-range and str→u8 elements (a rule-7
* miscompile: out-of-range truncates).
*
* #251 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 element WIDTH from the DECLARED type, so a restamp
* would be dead code). Both stages now, at let/def/struct-field:
* - foldable int/rune literal → defcastfits range-check vs declared T
* (in-range accept; out-of-range REJECT loud, rule-7/Drew).
* - non-foldable → type_assignable / isassignable to the element type.
* - cstage: arrlit_init_fits wired at check.c clet (local let),
* struct-field-init, and def-init.
* - wwstage: checkarrlitfits (factored from checkletassign's inline
* #130 block) called from the let path, the def path, and a TARGETED
* array-field walk in the N_STRUCTLIT arm (the array-field
* accept-if-fits ONLY — isolated from the parked #23 field walk).
*
* Element width is declared-type-driven at cgen, so the array literal
* node keeps its [N]i32 / [N]rune type and no restamp is needed; the
* cs==ww byte-id gate confirms the emitted bytes are u8-wide regardless.
*
* Accept rows (run + cs==ww byte-id), int and rune, across the 3 sites:
* - let_int `let a:[4]u8=[65,66,67,68]; a[0]` → 65
* - let_rune `let a:[4]u8=['A','B','C','D']; a[0]` → 65
* - def_int `def D:[4]u8=[65,..]; D[0]` → 65
* - def_rune `def D:[4]u8=['A',..]; D[0]` → 65
* - struct_int `enc{m=[65,..]}; E.m[0]` → 65
* - struct_rune `enc{m=['A',..]}; E.m[1]` → 66
*
* Reject rows (must FAIL build on BOTH stages — rule-7 loud reject):
* - let_over `let a:[2]u8=[300,1]` (out of range)
* - def_over `def D:[2]u8=[300,1]` (was a wwstage over-accept)
* - struct_over `enc{m=[300,1]}` (was a wwstage over-accept)
* - def_str `def D:[2]u8=["x","y"]` (was a wwstage over-accept)
*/
#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 arow { const char *label; const char *src; int want_exit; };
struct rrow { const char *label; const char *src; };
static const struct arow accept_rows[] = {
{ "let_int",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [4]u8 = [65, 66, 67, 68];\n"
" return a[0]: i32;\n"
"};\n", 65 },
{ "let_rune",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [4]u8 = ['A', 'B', 'C', 'D'];\n"
" return a[0]: i32;\n"
"};\n", 65 },
{ "def_int",
"package main;\n"
"def D: [4]u8 = [65, 66, 67, 68];\n"
"export fn main() i32 = { return D[0]: i32; };\n", 65 },
{ "def_rune",
"package main;\n"
"def D: [4]u8 = ['A', 'B', 'C', 'D'];\n"
"export fn main() i32 = { return D[0]: i32; };\n", 65 },
{ "struct_int",
"package main;\n"
"type enc = struct { m: [4]u8 };\n"
"let E: enc = enc { m = [65, 66, 67, 68] };\n"
"export fn main() i32 = { return E.m[0]: i32; };\n", 65 },
{ "struct_rune",
"package main;\n"
"type enc = struct { m: [4]u8 };\n"
"export fn main() i32 = {\n"
" let e: enc = enc { m = ['A', 'B', 'C', 'D'] };\n"
" return e.m[1]: i32;\n"
"};\n", 66 },
{ NULL, NULL, 0 }
};
static const struct rrow reject_rows[] = {
{ "let_over",
"package main;\n"
"export fn main() i32 = { let a: [2]u8 = [300, 1]; return a[0]: i32; };\n" },
{ "def_over",
"package main;\n"
"def D: [2]u8 = [300, 1];\n"
"export fn main() i32 = { return D[0]: i32; };\n" },
{ "struct_over",
"package main;\n"
"type enc = struct { m: [2]u8 };\n"
"let E: enc = enc { m = [300, 1] };\n"
"export fn main() i32 = { return E.m[0]: i32; };\n" },
{ "def_str",
"package main;\n"
"def D: [2]u8 = [\"x\", \"y\"];\n"
"export fn main() i32 = { return 0; };\n" },
{ NULL, NULL }
};
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, "arrnarrow: w6c_ww missing — cannot run the "
"cs==ww gate (the whole point of this test)\n");
return 1;
}
int n = 0, fail = 0;
/* Accept rows: cstage build + run + cs==ww byte-id. */
for (int i = 0; accept_rows[i].src; i++, n++) {
char src[64];
snprintf(src, sizeof src, "/tmp/wwan_%d_%d.ww", getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; continue; }
fputs(accept_rows[i].src, f);
fclose(f);
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwan_%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, "accept[%s]: cstage build failed\n",
accept_rows[i].label);
fail++; unlink(src); rmdir(tmpdir); continue;
}
char outbin[128];
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
if (got != accept_rows[i].want_exit) {
fprintf(stderr, "accept[%s]: exit %d, want %d\n",
accept_rows[i].label, got, accept_rows[i].want_exit);
fail++;
}
unlink(outbin); rmdir(tmpdir);
char cs_s[64], ws_s[64];
snprintf(cs_s, sizeof cs_s, "/tmp/wwan_%d_%d_cs.s",
getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwan_%d_%d_ww.s",
getpid(), i);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, cs_s, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "accept[%s]: w6c failed\n", accept_rows[i].label);
fail++; unlink(src); continue;
}
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c_ww, ws_s, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "accept[%s]: w6c_ww failed\n", accept_rows[i].label);
fail++; unlink(src); unlink(cs_s); continue;
}
if (slurp_eq(cs_s, ws_s) != 0) {
fprintf(stderr, "accept[%s]: cs/ww .s DIFFER (rule-10)\n",
accept_rows[i].label);
fail++;
}
unlink(src); unlink(cs_s); unlink(ws_s);
}
/* Reject rows: BOTH stages must fail to build (loud reject). */
for (int i = 0; reject_rows[i].src; i++, n++) {
char src[64];
snprintf(src, sizeof src, "/tmp/wwrn_%d_%d.ww", getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; continue; }
fputs(reject_rows[i].src, f);
fclose(f);
char cmd[2048], dst[80];
snprintf(dst, sizeof dst, "/tmp/wwrn_%d_%d.s", getpid(), i);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, dst, src);
int cs_rc = runwait(cmd);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c_ww, dst, src);
int ww_rc = runwait(cmd);
if (cs_rc == 0) {
fprintf(stderr, "reject[%s]: cstage ACCEPTED (want reject)\n",
reject_rows[i].label);
fail++;
}
if (ww_rc == 0) {
fprintf(stderr, "reject[%s]: wwstage ACCEPTED (want reject)\n",
reject_rows[i].label);
fail++;
}
unlink(src); unlink(dst);
}
if (fail) {
fprintf(stderr, "%d/%d arrlit-elem-narrow tests failed\n",
fail, n);
return 1;
}
printf("arrnarrow: %d/%d ok (accept: run + cs==ww; reject: both-stage fail)\n",
n, n);
return 0;
}