wcc/check: #9 reject explicit [N]=[init] over-fill incl [0] (both stages)

An explicit [N]T = [init] with more initializers than N silently
mis-compiled for N==0: the over-fill length-mismatch check was suppressed
when alen==0, because alen==0 doubles as the [_] infer-sentinel after
resolve_type collapses the two. So def/let [0]int=[1,2] silently resized
(cstage exit 2) or OOB-read/segfaulted (wwstage) instead of the loud
length-mismatch that [N]=[init>N] gets everywhere else.

The AST keeps the distinction the Type loses: [_] leaves the N_TARRAY
length-child NULL, an explicit [N] carries N_INTLIT. cstage adds an
is_infer_arr() helper, drops the alen>0 exemption at the over-fill check,
and gates the 4 infer-resize/no-init sites on is_infer_arr so an explicit
[0] flows to the over-fill -> loud. wwstage flips the one shared count
gate (checkarrlitfits) from declen>0 to arrtn.rhs!=nil, which also
dissolves a wwstage local-resize/module-OOB inconsistency.

[_] inference, [0]=[] empty, and [_]-no-init louding all preserved.
Under-long (count<N) stays out of scope (#10). byte-id 990-997 8/8.
test/wcc/820 table-driven; its one empty-[0] global row carves out
byte-id (pre-existing spurious-DATAW divergence, task #15).
This commit is contained in:
2026-06-08 19:54:29 +09:00
parent 1aaa0a3670
commit 29a2ab2a72
6 changed files with 386 additions and 14 deletions

View File

@@ -390,6 +390,16 @@ assignable_addrfn(Checker *c, Type *dst, Node *rhs)
return 0;
}
/* #9: an INFER `[_]T` leaves the N_TARRAY length-child NULL; an explicit
* `[N]T` (incl `[0]`) carries an N_INTLIT. resolve_type collapses BOTH to
* alen==0, so the Type can't tell them apart — key off the decl's type-AST
* node (d->lhs) instead. Mirrors wwstage's `arrtn.rhs != nil` test. */
static int
is_infer_arr(Node *tn)
{
return tn != NULL && tn->kind == N_TARRAY && tn->rhs == NULL;
}
/* arrlit_init_fits — #130: accept-if-fits for `let/def A: [N]T = [..]`
* where the whole-array type_assignable failed (bare-int elements
* synthesize [N]i32 via type_default, losing the literal flavor that
@@ -430,12 +440,14 @@ arrlit_init_fits(Checker *c, Type *dt, Node *rhs)
* check below and then smashed the frame at cgen (each element is
* stored at its natural offset — the overflow clobbered neighbours
* and even the saved BP). Reject loud before the element walk.
* alen==0 stays exempt: 0 doubles as the [_] infer sentinel ([0]
* vs [_] conflation, and [_] in def/struct-field never infers —
* task #11), and the let paths patch the real length in before
* reaching here. Under-long (count < N, no `...`) stays accepted
* as before; Hare rejects it — task #10. */
if (u->kind == TY_ARRAY && u->alen != SIZE_UNDEFINED && u->alen > 0
* #9: the `alen > 0` exemption is GONE. An infer `[_]` is resized to
* its real count at the decl sites (is_infer_arr-gated) BEFORE
* reaching here, so an array arriving with alen==0 is necessarily an
* EXPLICIT `[0]` — and `[0] = [1,2]` (count 2 > 0) must be loud, not
* silently resized. `[0] = []` (count 0) stays accepted. SIZE_UNDEFINED
* (opaque/unsized) still exempt. Under-long (count < N, no `...`) stays
* accepted as before; Hare rejects it — task #10. */
if (u->kind == TY_ARRAY && u->alen != SIZE_UNDEFINED
&& count > u->alen) {
err(c, rhs->pos, "array literal has %llu elements "
"but declared array holds %llu",
@@ -2199,7 +2211,8 @@ clet(Checker *c, Node *n)
* 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) {
if (declared && declared->kind == TY_ARRAY && declared->alen == 0
&& is_infer_arr(n->lhs)) {
Type *iu = type_chase_named(initt);
if (iu && iu->kind == TY_ARRAY)
declared = type_array(c->a, declared->sub, iu->alen);
@@ -2864,7 +2877,8 @@ check_file(Checker *c, Node *file)
* assignability check so arrlit_init_fits sees the
* inferred length. */
if (d->type && d->type->kind == TY_ARRAY
&& d->type->alen == 0) {
&& d->type->alen == 0
&& is_infer_arr(d->lhs)) {
Type *iu = type_chase_named(rt);
if (iu && iu->kind == TY_ARRAY) {
d->type = type_array(c->a,
@@ -2957,7 +2971,8 @@ check_file(Checker *c, Node *file)
* 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) {
&& d->type->alen == 0
&& is_infer_arr(d->lhs)) {
Type *iu = type_chase_named(rt);
if (iu && iu->kind == TY_ARRAY) {
d->type = type_array(c->a,
@@ -2997,9 +3012,10 @@ check_file(Checker *c, Node *file)
&& eval_def_const(c, d->rhs, &dv, 0))
stamp_intlit(c, d->rhs, dv);
} else if (d->type && d->type->kind == TY_ARRAY
&& d->type->alen == 0) {
&& d->type->alen == 0 && is_infer_arr(d->lhs)) {
/* `let x: [_]T;` — no initialiser, length can't be
* inferred (rule 7, #7). */
* inferred (rule 7, #7). An explicit `[0]T;` with no
* init is a valid empty array, not this error. */
err(c, d->pos, "[_]T needs an array-literal "
"initialiser");
}