wcc/check: #12+#106 reject overlong array-literal in return/call-arg/alias positions (wwstage)
An overlong array literal (more initializers than the declared length) is invalid -- cstage loud-rejects it everywhere -- but wwstage silently accepted (and truncated) it in several positions; #9 wired only the decl position. This folds the remaining three (one class: checkarrlitfits over-fill coverage), all wwstage-only reject-align: - #12a return fn f() [2]int = [1,2,3] -- silently accepted. - #12b call-arg g([1,2,3]) -- louded only late via cgen #271. - #106 alias type A=[2]int; let g: A = [1,2,3] -- silently truncated; checkarrlitfits bailed on the N_TNAME alias without chasing. Four inserts in check.ww: an alias-chase (resolvealias) at the top of checkarrlitfits (makes all callers alias-aware), the over-fill check wired into checkretassign (hoisted above the isassignable short-circuit) and desugarcallargs, and the alias-let-global guard made alias-aware. cstage unchanged (w6c md5 unchanged); reject-only, so no asm moves -- 990-997 8/8, no lib byte-id pin flips. A 5th position (tuple-element overlong) is a separate pre-existing hole, filed (#20). test/wcc/828 table-driven.
This commit is contained in:
7
Makefile
7
Makefile
@@ -256,6 +256,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
|||||||
$(BIN)/test_errfirst_union_try \
|
$(BIN)/test_errfirst_union_try \
|
||||||
$(BIN)/test_alias_tuple_coerce \
|
$(BIN)/test_alias_tuple_coerce \
|
||||||
$(BIN)/test_str_eq \
|
$(BIN)/test_str_eq \
|
||||||
|
$(BIN)/test_overlong_arrlit \
|
||||||
$(BIN)/test_slice_str_global_zero \
|
$(BIN)/test_slice_str_global_zero \
|
||||||
$(BIN)/test_slice_literal_global \
|
$(BIN)/test_slice_literal_global \
|
||||||
$(BIN)/test_global_arr_elem_field \
|
$(BIN)/test_global_arr_elem_field \
|
||||||
@@ -700,6 +701,12 @@ $(BIN)/test_str_eq: test/wcc/827_str_eq.c $(BIN)/ww \
|
|||||||
$(LIB)/libwwrt.a | $(BIN)
|
$(LIB)/libwwrt.a | $(BIN)
|
||||||
$(CC) $(CFLAGS) -o $@ $<
|
$(CC) $(CFLAGS) -o $@ $<
|
||||||
|
|
||||||
|
$(BIN)/test_overlong_arrlit: test/wcc/828_overlong_arrlit.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_def_arr_infer_len: test/wcc/814_def_arr_infer_len.c $(BIN)/ww \
|
$(BIN)/test_def_arr_infer_len: test/wcc/814_def_arr_infer_len.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 \
|
||||||
|
|||||||
@@ -14507,6 +14507,14 @@ fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = {
|
|||||||
fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
|
fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
|
||||||
if (arrtn == nil) { return; };
|
if (arrtn == nil) { return; };
|
||||||
if (rhs == nil) { return; };
|
if (rhs == nil) { return; };
|
||||||
|
// #106: chase a TY_NAMED alias (`type A=[2]int`) to the underlying
|
||||||
|
// [N]T before the kind gate — else an alias declared type bails
|
||||||
|
// here and the over-fill (#9) never fires (silent DATA-truncate).
|
||||||
|
// Idempotent on a non-alias (resolvealias returns the node), so a
|
||||||
|
// direct N_TARRAY is untouched. Makes EVERY caller (decl/let/def/
|
||||||
|
// struct/return/call-arg) alias-aware by construction.
|
||||||
|
arrtn = resolvealias(c, unwrapbang(arrtn));
|
||||||
|
if (arrtn == nil) { return; };
|
||||||
if (arrtn.kind != nkind.N_TARRAY) { return; };
|
if (arrtn.kind != nkind.N_TARRAY) { return; };
|
||||||
if (rhs.kind != nkind.N_ARRLIT) { return; };
|
if (rhs.kind != nkind.N_ARRLIT) { return; };
|
||||||
// #71: more elements than the declared [N] passed every per-element
|
// #71: more elements than the declared [N] passed every per-element
|
||||||
@@ -14745,6 +14753,14 @@ fn desugarcallargs(c: *checker, n: *node) void = {
|
|||||||
};
|
};
|
||||||
}; };
|
}; };
|
||||||
}; };
|
}; };
|
||||||
|
// #12: overlong array-lit CALL-ARG — `g([1,2,3])`.
|
||||||
|
// Reject at CHECK time (clean over-fill msg) instead
|
||||||
|
// of falling to cgen #271's late aggregate-arg loud.
|
||||||
|
// param.lhs is the declared param type (alias-aware
|
||||||
|
// via the resolvealias chase in checkarrlitfits).
|
||||||
|
if (a.kind == nkind.N_ARRLIT) {
|
||||||
|
checkarrlitfits(c, param.lhs, a);
|
||||||
|
};
|
||||||
// #31/#33: bare array-literal arg has no backing
|
// #31/#33: bare array-literal arg has no backing
|
||||||
// — loud-reject (supported only at a `let`).
|
// — loud-reject (supported only at a `let`).
|
||||||
if (!rejectarrlitborrow(c, param.lhs, a)) {
|
if (!rejectarrlitborrow(c, param.lhs, a)) {
|
||||||
@@ -14953,7 +14969,16 @@ fn checkletassign(c: *checker, n: *node) void = {
|
|||||||
// (#146 merged). Mirrors cstage check.c arrlit_init_fits. Scoped
|
// (#146 merged). Mirrors cstage check.c arrlit_init_fits. Scoped
|
||||||
// to the array path; scalar-init range-check is a separate
|
// to the array path; scalar-init range-check is a separate
|
||||||
// language-wide gap (#148).
|
// language-wide gap (#148).
|
||||||
if (n.lhs.kind == nkind.N_TARRAY && n.rhs.kind == nkind.N_ARRLIT) {
|
// #106: an alias-of-array lhs (`type A=[2]int; let g: A = […]`) arrives
|
||||||
|
// as N_TNAME, so the bare N_TARRAY gate skipped it and the over-fill
|
||||||
|
// never fired (silent DATA-truncate). The let path is the one caller
|
||||||
|
// that pre-gates on the declared node's kind (def/return/call-arg pass
|
||||||
|
// it straight to the alias-aware checkarrlitfits); resolve the alias
|
||||||
|
// here too so an alias-of-array routes through the same over-fill. A
|
||||||
|
// direct N_TARRAY is unchanged (resolvealias is idempotent), and the
|
||||||
|
// early return matches the direct-array branch's pre-existing return.
|
||||||
|
let llhs: *node = resolvealias(c, unwrapbang(n.lhs));
|
||||||
|
if (llhs != nil && llhs.kind == nkind.N_TARRAY && n.rhs.kind == nkind.N_ARRLIT) {
|
||||||
checkarrlitfits(c, n.lhs, n.rhs);
|
checkarrlitfits(c, n.lhs, n.rhs);
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@@ -15025,6 +15050,16 @@ fn checkretassign(c: *checker, n: *node) void = {
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if (c.fnret == nil) { return; };
|
if (c.fnret == nil) { return; };
|
||||||
|
// #12: overlong array-lit RETURN — `fn f() [2]int = { return [1,2,3]; }`.
|
||||||
|
// #9 wired the over-fill at DECL only; the return position was lenient.
|
||||||
|
// Fire BEFORE the isassignable/conf guards below — a direct [N]T return
|
||||||
|
// type drives isassignable to conf=false (array-length leniency), which
|
||||||
|
// would short-circuit the check (the alias path set conf=true, so neg3
|
||||||
|
// caught it but the direct neg0 slipped). Alias-aware via the
|
||||||
|
// resolvealias chase at the top of checkarrlitfits.
|
||||||
|
if (n.lhs.kind == nkind.N_ARRLIT) {
|
||||||
|
checkarrlitfits(c, c.fnret, n.lhs);
|
||||||
|
};
|
||||||
let src: *node = exprtype(c, n.lhs, nil);
|
let src: *node = exprtype(c, n.lhs, nil);
|
||||||
if (src == nil) { return; };
|
if (src == nil) { return; };
|
||||||
let conf: bool = false;
|
let conf: bool = false;
|
||||||
|
|||||||
@@ -4226,6 +4226,14 @@ fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = {
|
|||||||
fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
|
fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
|
||||||
if (arrtn == nil) { return; };
|
if (arrtn == nil) { return; };
|
||||||
if (rhs == nil) { return; };
|
if (rhs == nil) { return; };
|
||||||
|
// #106: chase a TY_NAMED alias (`type A=[2]int`) to the underlying
|
||||||
|
// [N]T before the kind gate — else an alias declared type bails
|
||||||
|
// here and the over-fill (#9) never fires (silent DATA-truncate).
|
||||||
|
// Idempotent on a non-alias (resolvealias returns the node), so a
|
||||||
|
// direct N_TARRAY is untouched. Makes EVERY caller (decl/let/def/
|
||||||
|
// struct/return/call-arg) alias-aware by construction.
|
||||||
|
arrtn = resolvealias(c, unwrapbang(arrtn));
|
||||||
|
if (arrtn == nil) { return; };
|
||||||
if (arrtn.kind != nkind.N_TARRAY) { return; };
|
if (arrtn.kind != nkind.N_TARRAY) { return; };
|
||||||
if (rhs.kind != nkind.N_ARRLIT) { return; };
|
if (rhs.kind != nkind.N_ARRLIT) { return; };
|
||||||
// #71: more elements than the declared [N] passed every per-element
|
// #71: more elements than the declared [N] passed every per-element
|
||||||
@@ -4464,6 +4472,14 @@ fn desugarcallargs(c: *checker, n: *node) void = {
|
|||||||
};
|
};
|
||||||
}; };
|
}; };
|
||||||
}; };
|
}; };
|
||||||
|
// #12: overlong array-lit CALL-ARG — `g([1,2,3])`.
|
||||||
|
// Reject at CHECK time (clean over-fill msg) instead
|
||||||
|
// of falling to cgen #271's late aggregate-arg loud.
|
||||||
|
// param.lhs is the declared param type (alias-aware
|
||||||
|
// via the resolvealias chase in checkarrlitfits).
|
||||||
|
if (a.kind == nkind.N_ARRLIT) {
|
||||||
|
checkarrlitfits(c, param.lhs, a);
|
||||||
|
};
|
||||||
// #31/#33: bare array-literal arg has no backing
|
// #31/#33: bare array-literal arg has no backing
|
||||||
// — loud-reject (supported only at a `let`).
|
// — loud-reject (supported only at a `let`).
|
||||||
if (!rejectarrlitborrow(c, param.lhs, a)) {
|
if (!rejectarrlitborrow(c, param.lhs, a)) {
|
||||||
@@ -4672,7 +4688,16 @@ fn checkletassign(c: *checker, n: *node) void = {
|
|||||||
// (#146 merged). Mirrors cstage check.c arrlit_init_fits. Scoped
|
// (#146 merged). Mirrors cstage check.c arrlit_init_fits. Scoped
|
||||||
// to the array path; scalar-init range-check is a separate
|
// to the array path; scalar-init range-check is a separate
|
||||||
// language-wide gap (#148).
|
// language-wide gap (#148).
|
||||||
if (n.lhs.kind == nkind.N_TARRAY && n.rhs.kind == nkind.N_ARRLIT) {
|
// #106: an alias-of-array lhs (`type A=[2]int; let g: A = […]`) arrives
|
||||||
|
// as N_TNAME, so the bare N_TARRAY gate skipped it and the over-fill
|
||||||
|
// never fired (silent DATA-truncate). The let path is the one caller
|
||||||
|
// that pre-gates on the declared node's kind (def/return/call-arg pass
|
||||||
|
// it straight to the alias-aware checkarrlitfits); resolve the alias
|
||||||
|
// here too so an alias-of-array routes through the same over-fill. A
|
||||||
|
// direct N_TARRAY is unchanged (resolvealias is idempotent), and the
|
||||||
|
// early return matches the direct-array branch's pre-existing return.
|
||||||
|
let llhs: *node = resolvealias(c, unwrapbang(n.lhs));
|
||||||
|
if (llhs != nil && llhs.kind == nkind.N_TARRAY && n.rhs.kind == nkind.N_ARRLIT) {
|
||||||
checkarrlitfits(c, n.lhs, n.rhs);
|
checkarrlitfits(c, n.lhs, n.rhs);
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@@ -4744,6 +4769,16 @@ fn checkretassign(c: *checker, n: *node) void = {
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if (c.fnret == nil) { return; };
|
if (c.fnret == nil) { return; };
|
||||||
|
// #12: overlong array-lit RETURN — `fn f() [2]int = { return [1,2,3]; }`.
|
||||||
|
// #9 wired the over-fill at DECL only; the return position was lenient.
|
||||||
|
// Fire BEFORE the isassignable/conf guards below — a direct [N]T return
|
||||||
|
// type drives isassignable to conf=false (array-length leniency), which
|
||||||
|
// would short-circuit the check (the alias path set conf=true, so neg3
|
||||||
|
// caught it but the direct neg0 slipped). Alias-aware via the
|
||||||
|
// resolvealias chase at the top of checkarrlitfits.
|
||||||
|
if (n.lhs.kind == nkind.N_ARRLIT) {
|
||||||
|
checkarrlitfits(c, c.fnret, n.lhs);
|
||||||
|
};
|
||||||
let src: *node = exprtype(c, n.lhs, nil);
|
let src: *node = exprtype(c, n.lhs, nil);
|
||||||
if (src == nil) { return; };
|
if (src == nil) { return; };
|
||||||
let conf: bool = false;
|
let conf: bool = false;
|
||||||
|
|||||||
@@ -14507,6 +14507,14 @@ fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = {
|
|||||||
fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
|
fn checkarrlitfits(c: *checker, arrtn: *node, rhs: *node) void = {
|
||||||
if (arrtn == nil) { return; };
|
if (arrtn == nil) { return; };
|
||||||
if (rhs == nil) { return; };
|
if (rhs == nil) { return; };
|
||||||
|
// #106: chase a TY_NAMED alias (`type A=[2]int`) to the underlying
|
||||||
|
// [N]T before the kind gate — else an alias declared type bails
|
||||||
|
// here and the over-fill (#9) never fires (silent DATA-truncate).
|
||||||
|
// Idempotent on a non-alias (resolvealias returns the node), so a
|
||||||
|
// direct N_TARRAY is untouched. Makes EVERY caller (decl/let/def/
|
||||||
|
// struct/return/call-arg) alias-aware by construction.
|
||||||
|
arrtn = resolvealias(c, unwrapbang(arrtn));
|
||||||
|
if (arrtn == nil) { return; };
|
||||||
if (arrtn.kind != nkind.N_TARRAY) { return; };
|
if (arrtn.kind != nkind.N_TARRAY) { return; };
|
||||||
if (rhs.kind != nkind.N_ARRLIT) { return; };
|
if (rhs.kind != nkind.N_ARRLIT) { return; };
|
||||||
// #71: more elements than the declared [N] passed every per-element
|
// #71: more elements than the declared [N] passed every per-element
|
||||||
@@ -14745,6 +14753,14 @@ fn desugarcallargs(c: *checker, n: *node) void = {
|
|||||||
};
|
};
|
||||||
}; };
|
}; };
|
||||||
}; };
|
}; };
|
||||||
|
// #12: overlong array-lit CALL-ARG — `g([1,2,3])`.
|
||||||
|
// Reject at CHECK time (clean over-fill msg) instead
|
||||||
|
// of falling to cgen #271's late aggregate-arg loud.
|
||||||
|
// param.lhs is the declared param type (alias-aware
|
||||||
|
// via the resolvealias chase in checkarrlitfits).
|
||||||
|
if (a.kind == nkind.N_ARRLIT) {
|
||||||
|
checkarrlitfits(c, param.lhs, a);
|
||||||
|
};
|
||||||
// #31/#33: bare array-literal arg has no backing
|
// #31/#33: bare array-literal arg has no backing
|
||||||
// — loud-reject (supported only at a `let`).
|
// — loud-reject (supported only at a `let`).
|
||||||
if (!rejectarrlitborrow(c, param.lhs, a)) {
|
if (!rejectarrlitborrow(c, param.lhs, a)) {
|
||||||
@@ -14953,7 +14969,16 @@ fn checkletassign(c: *checker, n: *node) void = {
|
|||||||
// (#146 merged). Mirrors cstage check.c arrlit_init_fits. Scoped
|
// (#146 merged). Mirrors cstage check.c arrlit_init_fits. Scoped
|
||||||
// to the array path; scalar-init range-check is a separate
|
// to the array path; scalar-init range-check is a separate
|
||||||
// language-wide gap (#148).
|
// language-wide gap (#148).
|
||||||
if (n.lhs.kind == nkind.N_TARRAY && n.rhs.kind == nkind.N_ARRLIT) {
|
// #106: an alias-of-array lhs (`type A=[2]int; let g: A = […]`) arrives
|
||||||
|
// as N_TNAME, so the bare N_TARRAY gate skipped it and the over-fill
|
||||||
|
// never fired (silent DATA-truncate). The let path is the one caller
|
||||||
|
// that pre-gates on the declared node's kind (def/return/call-arg pass
|
||||||
|
// it straight to the alias-aware checkarrlitfits); resolve the alias
|
||||||
|
// here too so an alias-of-array routes through the same over-fill. A
|
||||||
|
// direct N_TARRAY is unchanged (resolvealias is idempotent), and the
|
||||||
|
// early return matches the direct-array branch's pre-existing return.
|
||||||
|
let llhs: *node = resolvealias(c, unwrapbang(n.lhs));
|
||||||
|
if (llhs != nil && llhs.kind == nkind.N_TARRAY && n.rhs.kind == nkind.N_ARRLIT) {
|
||||||
checkarrlitfits(c, n.lhs, n.rhs);
|
checkarrlitfits(c, n.lhs, n.rhs);
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@@ -15025,6 +15050,16 @@ fn checkretassign(c: *checker, n: *node) void = {
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if (c.fnret == nil) { return; };
|
if (c.fnret == nil) { return; };
|
||||||
|
// #12: overlong array-lit RETURN — `fn f() [2]int = { return [1,2,3]; }`.
|
||||||
|
// #9 wired the over-fill at DECL only; the return position was lenient.
|
||||||
|
// Fire BEFORE the isassignable/conf guards below — a direct [N]T return
|
||||||
|
// type drives isassignable to conf=false (array-length leniency), which
|
||||||
|
// would short-circuit the check (the alias path set conf=true, so neg3
|
||||||
|
// caught it but the direct neg0 slipped). Alias-aware via the
|
||||||
|
// resolvealias chase at the top of checkarrlitfits.
|
||||||
|
if (n.lhs.kind == nkind.N_ARRLIT) {
|
||||||
|
checkarrlitfits(c, c.fnret, n.lhs);
|
||||||
|
};
|
||||||
let src: *node = exprtype(c, n.lhs, nil);
|
let src: *node = exprtype(c, n.lhs, nil);
|
||||||
if (src == nil) { return; };
|
if (src == nil) { return; };
|
||||||
let conf: bool = false;
|
let conf: bool = false;
|
||||||
|
|||||||
320
test/wcc/828_overlong_arrlit.c
Normal file
320
test/wcc/828_overlong_arrlit.c
Normal file
@@ -0,0 +1,320 @@
|
|||||||
|
/*
|
||||||
|
* 828_overlong_arrlit — an OVERLONG array literal (more initialisers than the
|
||||||
|
* declared [N]) is INVALID ww; BOTH stages must LOUDLY REJECT at check time
|
||||||
|
* (#12 + #106; rob spec .ai/rob-12-106-spec.md). #9 wired the over-fill at the
|
||||||
|
* DECL position only; this extends checkarrlitfits to the RETURN + CALL-ARG
|
||||||
|
* positions and chases a TY_NAMED alias to its underlying [N]T at the top of
|
||||||
|
* checkarrlitfits, so EVERY caller is alias-aware by construction.
|
||||||
|
*
|
||||||
|
* WWSTAGE-ONLY fix — cstage already rejects all four shapes (type_assignable
|
||||||
|
* counts elements). Pre-fix wwstage divergences (the mutation-sanity targets):
|
||||||
|
* - #12a RETURN `fn f() [2]int = [1,2,3]` : ww silently ACCEPTED (exit 0)
|
||||||
|
* - #12b CALL-ARG `g([1,2,3])`, g(a:[2]int) : ww loud-but-LATE via cgen #271
|
||||||
|
* - #106 alias-global `type A=[2]int; let g:A=[1,2,3]` : ww silently ACCEPTED
|
||||||
|
* (+ truncated DATA)
|
||||||
|
* - alias-RETURN `type A=[2]int; fn f() A=[1,2,3]` : proves the alias
|
||||||
|
* chase covers the return too
|
||||||
|
*
|
||||||
|
* The diagnostic TEXT may differ between stages ("over-fill" vs "not
|
||||||
|
* assignable") — that is byte-id-blind (stderr is not asm); both REJECT and
|
||||||
|
* emit no asm, so 990-997 byte-id is untouched. Do NOT chase message parity.
|
||||||
|
*
|
||||||
|
* neg row | shape | gate
|
||||||
|
* -----------------+---------------------------------------------+----------
|
||||||
|
* ret_overlong | fn f() [2]int = [1,2,3] | build FAIL
|
||||||
|
* arg_overlong | fn g(a:[2]int)..; g([1,2,3]) | build FAIL
|
||||||
|
* alias_g_over | type A=[2]int; let g:A=[1,2,3] | build FAIL
|
||||||
|
* alias_ret_over | type A=[2]int; fn f() A = [1,2,3] | build FAIL
|
||||||
|
*
|
||||||
|
* pos row | shape | want
|
||||||
|
* -----------------+---------------------------------------------+------
|
||||||
|
* ret_exact | fn f() [2]int = [1,2]; f()[1] | 2
|
||||||
|
* arg_exact | fn g(a:[2]int) int = a[1]; g([10,20]) | 20
|
||||||
|
* alias_exact | type A=[2]int; let g:A=[1,2]; g[1] | 2
|
||||||
|
*/
|
||||||
|
#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[] = {
|
||||||
|
{ "ret_exact",
|
||||||
|
"package main;\n"
|
||||||
|
"fn f() [2]int = {\n"
|
||||||
|
"\treturn [1, 2];\n"
|
||||||
|
"};\n"
|
||||||
|
"export fn main() i32 = {\n"
|
||||||
|
"\tlet r: [2]int = f();\n"
|
||||||
|
"\treturn r[1]: i32;\n"
|
||||||
|
"};\n",
|
||||||
|
2 },
|
||||||
|
|
||||||
|
/* A bare array-LITERAL fixed-array arg is blocked on BOTH stages by
|
||||||
|
* cgen #271 (aggregate arg from unsupported source); the supported
|
||||||
|
* valid form passes via a variable. This control confirms the #12b
|
||||||
|
* desugarcallargs over-fill wiring (gated to `a.kind == N_ARRLIT`)
|
||||||
|
* leaves a normal variable call-arg untouched. */
|
||||||
|
{ "arg_exact",
|
||||||
|
"package main;\n"
|
||||||
|
"fn g(a: [2]int) int = {\n"
|
||||||
|
"\treturn a[1];\n"
|
||||||
|
"};\n"
|
||||||
|
"export fn main() i32 = {\n"
|
||||||
|
"\tlet v: [2]int = [10, 20];\n"
|
||||||
|
"\treturn g(v): i32;\n"
|
||||||
|
"};\n",
|
||||||
|
20 },
|
||||||
|
|
||||||
|
{ "alias_exact",
|
||||||
|
"package main;\n"
|
||||||
|
"type A = [2]int;\n"
|
||||||
|
"let g: A = [1, 2];\n"
|
||||||
|
"export fn main() i32 = {\n"
|
||||||
|
"\treturn g[1]: i32;\n"
|
||||||
|
"};\n",
|
||||||
|
2 },
|
||||||
|
};
|
||||||
|
|
||||||
|
/* An overlong array literal — both stages must FAIL the build (loud checker
|
||||||
|
* diagnostic, not silent accept / DATA-truncate / late cgen loud). */
|
||||||
|
static const char *neg[] = {
|
||||||
|
/* ret_overlong (#12a) */
|
||||||
|
"package main;\n"
|
||||||
|
"fn f() [2]int = {\n"
|
||||||
|
"\treturn [1, 2, 3];\n"
|
||||||
|
"};\n"
|
||||||
|
"export fn main() i32 = {\n"
|
||||||
|
"\tlet r: [2]int = f();\n"
|
||||||
|
"\treturn r[1]: i32;\n"
|
||||||
|
"};\n",
|
||||||
|
/* arg_overlong (#12b) */
|
||||||
|
"package main;\n"
|
||||||
|
"fn g(a: [2]int) int = {\n"
|
||||||
|
"\treturn a[1];\n"
|
||||||
|
"};\n"
|
||||||
|
"export fn main() i32 = {\n"
|
||||||
|
"\treturn g([1, 2, 3]): i32;\n"
|
||||||
|
"};\n",
|
||||||
|
/* alias_g_over (#106) */
|
||||||
|
"package main;\n"
|
||||||
|
"type A = [2]int;\n"
|
||||||
|
"let g: A = [1, 2, 3];\n"
|
||||||
|
"export fn main() i32 = {\n"
|
||||||
|
"\treturn g[1]: i32;\n"
|
||||||
|
"};\n",
|
||||||
|
/* alias_ret_over (alias + return) */
|
||||||
|
"package main;\n"
|
||||||
|
"type A = [2]int;\n"
|
||||||
|
"fn f() A = {\n"
|
||||||
|
"\treturn [1, 2, 3];\n"
|
||||||
|
"};\n"
|
||||||
|
"export fn main() i32 = {\n"
|
||||||
|
"\tlet r: A = f();\n"
|
||||||
|
"\treturn r[1]: i32;\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/oal_%d_%d.ww", getpid(), i);
|
||||||
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/oal_%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 — an overlong array literal 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/oaln_%d_%d.ww", getpid(), i);
|
||||||
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/oaln_%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);
|
||||||
|
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/oal_asm_%d_%d.ww", getpid(), i);
|
||||||
|
snprintf(cs, sizeof cs, "/tmp/oal_asm_%d_%d_c.s", getpid(), i);
|
||||||
|
snprintf(ws, sizeof ws, "/tmp/oal_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, "overlong_arrlit: 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,
|
||||||
|
"overlong_arrlit[%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,
|
||||||
|
"overlong_arrlit[%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,
|
||||||
|
"overlong_arrlit: %d/%d fixtures failed\n", fail, total);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("overlong_arrlit: %d/%d ok\n", total, total);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user