w6c+wcc/check: infer [_]T array length from initializer element count (fix #7)
`[_]T = [...]` (canonical Hare array-length inference) 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)` returned 0 with no diagnostic (rule-7 silent miscompile). Module-level was worse on wwstage, where `x.len` on ANY global array (even an explicit [N]) fell to the SB fallback and mis-emitted `MOVQ len(SB), AX` (linker: undefined reference to len). The length lives in the stamped TYPE and cgen already keys stride / length / data-emission off it, so stamping the inferred count at the one checker inference point closes it permanently (rob's #7 ruling): - check.c clet + module-level N_LET pass-2: count the initializer's elements and patch the array type's length (the Sym too, so a later x.len reads the inferred alen). No-init / non-array init can't infer -> loud error, never a silent zero-length array. - check.ww inferarraylen: the wwstage twin — stamp a synthesized N_INTLIT length child before resolvewalk caches the array tinfo; same loud-error rule. Idempotent for the module-level double-call. - cgenexpr.ww cgdot: the missing wwstage arm for a top-level [N]T global's .len / .ptr (cstage cgen.c:8011 already had it). - cgenutil.ww letslotsize: drop the now-redundant [_] slot-size intercept — a workaround for this very bug; the stamped length flows through the general slotsize path (rule 7). Both stages converge byte-identical; new table-driven test 684 covers [_]int/[_]str/[_]u8 local + module-level, len + element read-back, dual-stage runtime + asm byte-id, plus three negative no-infer rows.
This commit is contained in:
7
Makefile
7
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_elem_field_write \
|
||||||
$(BIN)/test_arr_enum_elem \
|
$(BIN)/test_arr_enum_elem \
|
||||||
$(BIN)/test_arr_strslice_elem \
|
$(BIN)/test_arr_strslice_elem \
|
||||||
|
$(BIN)/test_arr_infer_len \
|
||||||
$(BIN)/test_dot_str_chained_arg \
|
$(BIN)/test_dot_str_chained_arg \
|
||||||
$(BIN)/test_dot_slice_arg \
|
$(BIN)/test_dot_slice_arg \
|
||||||
$(BIN)/test_dot_tagged_source \
|
$(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)
|
$(LIB)/libwwrt.a | $(BIN)
|
||||||
$(CC) $(CFLAGS) -o $@ $<
|
$(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)/test_dot_str_chained_arg: test/wcc/692_dot_str_chained_arg.c $(BIN)/ww \
|
||||||
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
||||||
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
|
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
|
||||||
|
|||||||
@@ -1901,10 +1901,14 @@ clet(Checker *c, Node *n)
|
|||||||
if (n->rhs) initt = cexpr(c, n->rhs);
|
if (n->rhs) initt = cexpr(c, n->rhs);
|
||||||
c->alloc_octx = saved_octx;
|
c->alloc_octx = saved_octx;
|
||||||
/* `let xs: [_]T = arrlit;` — fill in the inferred length from the
|
/* `let xs: [_]T = arrlit;` — fill in the inferred length from the
|
||||||
* initialiser. `resolve_type` left alen=0 as a sentinel. */
|
* initialiser. `resolve_type` left alen=0 as a sentinel. A `[_]T`
|
||||||
if (declared && declared->kind == TY_ARRAY && declared->alen == 0 &&
|
* with no array-literal initialiser (no init at all, or a non-array
|
||||||
initt) {
|
* init) can't infer its length — that is a loud error, never a
|
||||||
Type *iu = (initt->kind == TY_NAMED) ? initt->under : initt;
|
* 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)
|
if (iu && iu->kind == TY_ARRAY)
|
||||||
declared = type_array(c->a, declared->sub, iu->alen);
|
declared = type_array(c->a, declared->sub, iu->alen);
|
||||||
else
|
else
|
||||||
@@ -2545,12 +2549,46 @@ check_file(Checker *c, Node *file)
|
|||||||
case N_LET: {
|
case N_LET: {
|
||||||
if (d->rhs) {
|
if (d->rhs) {
|
||||||
Type *rt = cexpr(c, 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 == NULL) d->type = type_default(rt);
|
||||||
if (d->type && rt != ty_err && d->type != ty_err
|
if (d->type && rt != ty_err && d->type != ty_err
|
||||||
&& !type_assignable(d->type, rt)
|
&& !type_assignable(d->type, rt)
|
||||||
&& !arrlit_init_fits(c, d->type, d->rhs))
|
&& !arrlit_init_fits(c, d->type, d->rhs))
|
||||||
err(c, d->pos, "let %s init not assignable",
|
err(c, d->pos, "let %s init not assignable",
|
||||||
d->str);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14313,8 +14313,48 @@ fn checkassign(c: *checker, n: *node) void = {
|
|||||||
n.rhs = desugararrayslice(c, ltn, rtn, n.rhs);
|
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 = {
|
fn checkletassign(c: *checker, n: *node) void = {
|
||||||
if (n == nil) { return; };
|
if (n == nil) { return; };
|
||||||
|
inferarraylen(c, n); // #7: must run before the n.rhs==nil bail
|
||||||
if (n.rhs == nil) { return; }; // no init
|
if (n.rhs == nil) { return; }; // no init
|
||||||
// hint = nil for A.6.0; A.6.1 will pass n.lhs once STRUCTLIT/ARRLIT
|
// hint = nil for A.6.0; A.6.1 will pass n.lhs once STRUCTLIT/ARRLIT
|
||||||
// arms consume it. Plumbing-only at this point.
|
// arms consume it. Plumbing-only at this point.
|
||||||
@@ -14870,6 +14910,12 @@ export fn checkfile(c: *checker, file: *node) void = {
|
|||||||
case nkind.N_TYPEDECL:
|
case nkind.N_TYPEDECL:
|
||||||
if (d.lhs != nil) { resolvewalk(c, d.lhs); };
|
if (d.lhs != nil) { resolvewalk(c, d.lhs); };
|
||||||
case nkind.N_LET:
|
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.lhs != nil) { resolvewalk(c, d.lhs); };
|
||||||
if (d.rhs != nil) { resolvewalk(c, d.rhs); };
|
if (d.rhs != nil) { resolvewalk(c, d.rhs); };
|
||||||
// #130: top-level let assignability — the subtree
|
// #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
|
// tagged-init branch writes past the local and tramples the next
|
||||||
// slot.
|
// slot.
|
||||||
export fn letslotsize(c: *cgen, n: *node) i32 = {
|
export fn letslotsize(c: *cgen, n: *node) i32 = {
|
||||||
// `[_]T = arrlit;` — inferred-length array. slotsize would
|
// `[_]T = arrlit;` inferred-length arrays no longer need a slot-size
|
||||||
// return elem_size * 1 (treating missing length as 1); intercept
|
// intercept here: the checker (inferarraylen, check.ww) stamps the
|
||||||
// and compute the real count first.
|
// real element count onto the array type's length child before cgen
|
||||||
if (n.lhs != nil) {
|
// runs, so slotsize reads it like any explicit `[N]T` (#7).
|
||||||
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;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
if (n.lhs != nil) { return slotsize(c, n.lhs); };
|
if (n.lhs != nil) { return slotsize(c, n.lhs); };
|
||||||
// Annotation-less init: defer to the call's return type if we
|
// Annotation-less init: defer to the call's return type if we
|
||||||
// can infer it. Tagged-union returns need 24B; everything else
|
// 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
|
// Top-level struct global field read — LEAQ name(SB), CX then
|
||||||
// load at fi.foff(CX). Mirrors the local "Direct struct local"
|
// load at fi.foff(CX). Mirrors the local "Direct struct local"
|
||||||
// branch above, swapping the BP frame slot for the global VA.
|
// branch above, swapping the BP frame slot for the global VA.
|
||||||
|
|||||||
@@ -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
|
// Top-level struct global field read — LEAQ name(SB), CX then
|
||||||
// load at fi.foff(CX). Mirrors the local "Direct struct local"
|
// load at fi.foff(CX). Mirrors the local "Direct struct local"
|
||||||
// branch above, swapping the BP frame slot for the global VA.
|
// branch above, swapping the BP frame slot for the global VA.
|
||||||
|
|||||||
@@ -1808,50 +1808,10 @@ fn inferletcalltype(c: *cgen, rhs: *node) *node = {
|
|||||||
// tagged-init branch writes past the local and tramples the next
|
// tagged-init branch writes past the local and tramples the next
|
||||||
// slot.
|
// slot.
|
||||||
export fn letslotsize(c: *cgen, n: *node) i32 = {
|
export fn letslotsize(c: *cgen, n: *node) i32 = {
|
||||||
// `[_]T = arrlit;` — inferred-length array. slotsize would
|
// `[_]T = arrlit;` inferred-length arrays no longer need a slot-size
|
||||||
// return elem_size * 1 (treating missing length as 1); intercept
|
// intercept here: the checker (inferarraylen, check.ww) stamps the
|
||||||
// and compute the real count first.
|
// real element count onto the array type's length child before cgen
|
||||||
if (n.lhs != nil) {
|
// runs, so slotsize reads it like any explicit `[N]T` (#7).
|
||||||
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;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
if (n.lhs != nil) { return slotsize(c, n.lhs); };
|
if (n.lhs != nil) { return slotsize(c, n.lhs); };
|
||||||
// Annotation-less init: defer to the call's return type if we
|
// Annotation-less init: defer to the call's return type if we
|
||||||
// can infer it. Tagged-union returns need 24B; everything else
|
// can infer it. Tagged-union returns need 24B; everything else
|
||||||
|
|||||||
@@ -3946,8 +3946,48 @@ fn checkassign(c: *checker, n: *node) void = {
|
|||||||
n.rhs = desugararrayslice(c, ltn, rtn, n.rhs);
|
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 = {
|
fn checkletassign(c: *checker, n: *node) void = {
|
||||||
if (n == nil) { return; };
|
if (n == nil) { return; };
|
||||||
|
inferarraylen(c, n); // #7: must run before the n.rhs==nil bail
|
||||||
if (n.rhs == nil) { return; }; // no init
|
if (n.rhs == nil) { return; }; // no init
|
||||||
// hint = nil for A.6.0; A.6.1 will pass n.lhs once STRUCTLIT/ARRLIT
|
// hint = nil for A.6.0; A.6.1 will pass n.lhs once STRUCTLIT/ARRLIT
|
||||||
// arms consume it. Plumbing-only at this point.
|
// arms consume it. Plumbing-only at this point.
|
||||||
@@ -4503,6 +4543,12 @@ export fn checkfile(c: *checker, file: *node) void = {
|
|||||||
case nkind.N_TYPEDECL:
|
case nkind.N_TYPEDECL:
|
||||||
if (d.lhs != nil) { resolvewalk(c, d.lhs); };
|
if (d.lhs != nil) { resolvewalk(c, d.lhs); };
|
||||||
case nkind.N_LET:
|
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.lhs != nil) { resolvewalk(c, d.lhs); };
|
||||||
if (d.rhs != nil) { resolvewalk(c, d.rhs); };
|
if (d.rhs != nil) { resolvewalk(c, d.rhs); };
|
||||||
// #130: top-level let assignability — the subtree
|
// #130: top-level let assignability — the subtree
|
||||||
|
|||||||
@@ -14313,8 +14313,48 @@ fn checkassign(c: *checker, n: *node) void = {
|
|||||||
n.rhs = desugararrayslice(c, ltn, rtn, n.rhs);
|
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 = {
|
fn checkletassign(c: *checker, n: *node) void = {
|
||||||
if (n == nil) { return; };
|
if (n == nil) { return; };
|
||||||
|
inferarraylen(c, n); // #7: must run before the n.rhs==nil bail
|
||||||
if (n.rhs == nil) { return; }; // no init
|
if (n.rhs == nil) { return; }; // no init
|
||||||
// hint = nil for A.6.0; A.6.1 will pass n.lhs once STRUCTLIT/ARRLIT
|
// hint = nil for A.6.0; A.6.1 will pass n.lhs once STRUCTLIT/ARRLIT
|
||||||
// arms consume it. Plumbing-only at this point.
|
// arms consume it. Plumbing-only at this point.
|
||||||
@@ -14870,6 +14910,12 @@ export fn checkfile(c: *checker, file: *node) void = {
|
|||||||
case nkind.N_TYPEDECL:
|
case nkind.N_TYPEDECL:
|
||||||
if (d.lhs != nil) { resolvewalk(c, d.lhs); };
|
if (d.lhs != nil) { resolvewalk(c, d.lhs); };
|
||||||
case nkind.N_LET:
|
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.lhs != nil) { resolvewalk(c, d.lhs); };
|
||||||
if (d.rhs != nil) { resolvewalk(c, d.rhs); };
|
if (d.rhs != nil) { resolvewalk(c, d.rhs); };
|
||||||
// #130: top-level let assignability — the subtree
|
// #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
|
// tagged-init branch writes past the local and tramples the next
|
||||||
// slot.
|
// slot.
|
||||||
export fn letslotsize(c: *cgen, n: *node) i32 = {
|
export fn letslotsize(c: *cgen, n: *node) i32 = {
|
||||||
// `[_]T = arrlit;` — inferred-length array. slotsize would
|
// `[_]T = arrlit;` inferred-length arrays no longer need a slot-size
|
||||||
// return elem_size * 1 (treating missing length as 1); intercept
|
// intercept here: the checker (inferarraylen, check.ww) stamps the
|
||||||
// and compute the real count first.
|
// real element count onto the array type's length child before cgen
|
||||||
if (n.lhs != nil) {
|
// runs, so slotsize reads it like any explicit `[N]T` (#7).
|
||||||
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;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
if (n.lhs != nil) { return slotsize(c, n.lhs); };
|
if (n.lhs != nil) { return slotsize(c, n.lhs); };
|
||||||
// Annotation-less init: defer to the call's return type if we
|
// Annotation-less init: defer to the call's return type if we
|
||||||
// can infer it. Tagged-union returns need 24B; everything else
|
// 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
|
// Top-level struct global field read — LEAQ name(SB), CX then
|
||||||
// load at fi.foff(CX). Mirrors the local "Direct struct local"
|
// load at fi.foff(CX). Mirrors the local "Direct struct local"
|
||||||
// branch above, swapping the BP frame slot for the global VA.
|
// branch above, swapping the BP frame slot for the global VA.
|
||||||
|
|||||||
349
test/wcc/684_arr_infer_len.c
Normal file
349
test/wcc/684_arr_infer_len.c
Normal file
@@ -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 <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; };
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user