From ffb858bba685d6898d1fbc3e283058fbff1ced3f Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sat, 13 Jun 2026 00:17:57 +0900 Subject: [PATCH] wcc/ww: under-length array-literal tail zero-fills MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The unspecified tail of a short array literal repeated the last value instead of zeroing — wwstage only (cstage already zeroes; the review's both-stages reading didn't survive ground truth). Zero-fill the tail per the zero-value semantics ruling. Review item #13. --- Makefile | 11 ++ selfhost/cmd/w6c/main.combined.ww | 17 ++- selfhost/cmd/wcc/cgen.ww | 17 ++- selfhost/cmd/wwdump/main.combined.ww | 17 ++- test/wcc/989_arrlit_tail_zero_run.c | 161 +++++++++++++++++++++++++++ 5 files changed, 214 insertions(+), 9 deletions(-) create mode 100644 test/wcc/989_arrlit_tail_zero_run.c diff --git a/Makefile b/Makefile index 0331dc44..8dbbf0ea 100644 --- a/Makefile +++ b/Makefile @@ -253,6 +253,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_chainidx_run \ $(BIN)/test_tupfieldsize_run \ $(BIN)/test_tagtupfieldsize_run \ + $(BIN)/test_arrlit_tail_zero_run \ $(BIN)/test_gunsigned_run \ $(BIN)/test_taggedidx_run \ $(BIN)/test_fnptrcollide_run \ @@ -712,6 +713,16 @@ $(BIN)/test_tagtupfieldsize_run: test/wcc/989_tagtupfieldsize_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 989_arrlit_tail_zero_run (#13): an under-length array literal zero-fills the +# unspecified tail, not the last value. Builds+runs on BOTH driver twins +# (rule-10), pinning the absolute value (pre-fix ww tail = last value). +$(BIN)/test_arrlit_tail_zero_run: test/wcc/989_arrlit_tail_zero_run.c \ + $(BIN)/ww $(BIN)/ww_ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + # 989_gunsigned_run (F7-c5, #25): a module-global unsigned ident on the # divide/shift/relational path must pick the unsigned opcode. Builds+runs on # BOTH driver twins (rule-10). CLASS-M — see the test header. diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 0c8a1d98..006e939e 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -42632,13 +42632,24 @@ fn emitarraylitbytes(c: *cgen, arrt: *tinfo, rhs: *node, e = rhs.list; let inrepeat: bool = false; for (idx < alen) { - let v: u64 = last; - if (!inrepeat && e != nil) { + // #13: explicit elements fold normally; a `...` repeat replays the + // LAST value; the tail PAST the explicit elements (no `...`) is + // ZERO-filled. Pre-fix the default was `last`, so an under-length + // literal (`[4]u64 = [1, 2]`) repeated the last value into the tail + // instead of zero — cstage already zeroes (Hare: unspecified array + // elements are zeroed; the #16-task zero-value ruling); this aligns + // wwstage, a gate-blind cs!=ww divergence at the array-global path. + let v: u64 = 0u64; + if (inrepeat) { + v = last; + } else { if (e != nil) { if (e.kind == nkind.N_FIELD) { if (streq(e.str, "...")) { inrepeat = true; + v = last; } else { e = e.next; + v = last; }; } else { let ev: *node = e; @@ -42647,7 +42658,7 @@ fn emitarraylitbytes(c: *cgen, arrt: *tinfo, rhs: *node, last = v; e = e.next; }; - }; + };}; let nb: u64 = v; let bb: i32 = 0; for (bb < esz) { diff --git a/selfhost/cmd/wcc/cgen.ww b/selfhost/cmd/wcc/cgen.ww index 486361ac..c758f77a 100644 --- a/selfhost/cmd/wcc/cgen.ww +++ b/selfhost/cmd/wcc/cgen.ww @@ -2101,13 +2101,24 @@ fn emitarraylitbytes(c: *cgen, arrt: *tinfo, rhs: *node, e = rhs.list; let inrepeat: bool = false; for (idx < alen) { - let v: u64 = last; - if (!inrepeat && e != nil) { + // #13: explicit elements fold normally; a `...` repeat replays the + // LAST value; the tail PAST the explicit elements (no `...`) is + // ZERO-filled. Pre-fix the default was `last`, so an under-length + // literal (`[4]u64 = [1, 2]`) repeated the last value into the tail + // instead of zero — cstage already zeroes (Hare: unspecified array + // elements are zeroed; the #16-task zero-value ruling); this aligns + // wwstage, a gate-blind cs!=ww divergence at the array-global path. + let v: u64 = 0u64; + if (inrepeat) { + v = last; + } else { if (e != nil) { if (e.kind == nkind.N_FIELD) { if (streq(e.str, "...")) { inrepeat = true; + v = last; } else { e = e.next; + v = last; }; } else { let ev: *node = e; @@ -2116,7 +2127,7 @@ fn emitarraylitbytes(c: *cgen, arrt: *tinfo, rhs: *node, last = v; e = e.next; }; - }; + };}; let nb: u64 = v; let bb: i32 = 0; for (bb < esz) { diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 16ac9fe8..b9ef040e 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -42632,13 +42632,24 @@ fn emitarraylitbytes(c: *cgen, arrt: *tinfo, rhs: *node, e = rhs.list; let inrepeat: bool = false; for (idx < alen) { - let v: u64 = last; - if (!inrepeat && e != nil) { + // #13: explicit elements fold normally; a `...` repeat replays the + // LAST value; the tail PAST the explicit elements (no `...`) is + // ZERO-filled. Pre-fix the default was `last`, so an under-length + // literal (`[4]u64 = [1, 2]`) repeated the last value into the tail + // instead of zero — cstage already zeroes (Hare: unspecified array + // elements are zeroed; the #16-task zero-value ruling); this aligns + // wwstage, a gate-blind cs!=ww divergence at the array-global path. + let v: u64 = 0u64; + if (inrepeat) { + v = last; + } else { if (e != nil) { if (e.kind == nkind.N_FIELD) { if (streq(e.str, "...")) { inrepeat = true; + v = last; } else { e = e.next; + v = last; }; } else { let ev: *node = e; @@ -42647,7 +42658,7 @@ fn emitarraylitbytes(c: *cgen, arrt: *tinfo, rhs: *node, last = v; e = e.next; }; - }; + };}; let nb: u64 = v; let bb: i32 = 0; for (bb < esz) { diff --git a/test/wcc/989_arrlit_tail_zero_run.c b/test/wcc/989_arrlit_tail_zero_run.c new file mode 100644 index 00000000..4cd0997c --- /dev/null +++ b/test/wcc/989_arrlit_tail_zero_run.c @@ -0,0 +1,161 @@ +/* + * 989_arrlit_tail_zero_run (#13) — an UNDER-LENGTH array literal zero-fills + * the unspecified tail (Hare: unspecified array elements are zeroed; the + * #16-task zero-value ruling), not the last value. + * + * THE BUG (wwstage only, gate-blind cs!=ww): emitarraylitbytes' int-element + * tail loop defaulted each unfilled slot to `last` (the previously emitted + * value), so `let g: [4]u64 = [1, 2]` emitted [1,2,2,2] — the DATAW tail + * repeated \x02. cstage already zeroes the tail (cg_array_data). THE FIX: + * default the tail to 0; replay `last` only inside a `...` repeat. + * + * The float / struct / nested-array element paths already zero-filled their + * tails; only the int path defaulted to `last`. The str/slice element paths + * ride a different helper (emitstrarraydata / emitslicedata) and were also + * already correct — the str_tail row documents the family is uniformly + * zeroed, it is coverage, not a teeth on this fix. + * + * row | shape | exit (cs==ww) + * --------------+--------------------------------+-------------- + * scalar_tail | [4]u64=[1,2]; sum | 3 (1+2+0+0) + * repeat_ctl | [4]u64=[7,9...]; sum (control) | 34 (7+9+9+9) + * str_tail | [4]str=["ab","cde"]; tail.len | 5 (2+3, tail len 0) + * Pre-fix (int tail = last): scalar_tail ww=7 (1+2+2+2) != cs=3. + */ +#include +#include +#include +#include +#include +#include + +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_exit; }; + +static const struct row rows[] = { + { "scalar_tail", + "package main;\n" + "let g: [4]u64 = [1, 2];\n" + "export fn main() i32 = {\n" + " return (g[0] + g[1] + g[2] + g[3]): i32;\n" + "};\n", + 3 }, + + { "repeat_ctl", + "package main;\n" + "let g: [4]u64 = [7, 9...];\n" + "export fn main() i32 = {\n" + " return (g[0] + g[1] + g[2] + g[3]): i32;\n" + "};\n", + 34 }, + + { "str_tail", + "package main;\n" + "let g: [4]str = [\"ab\", \"cde\"];\n" + "export fn main() i32 = {\n" + " if (g[2].len != 0) { return 21; };\n" + " if (g[3].len != 0) { return 22; };\n" + " return (g[0].len + g[1].len): i32;\n" + "};\n", + 5 }, +}; + +static int +run_build(const char *driver, const struct row *r, int i) +{ + char src[64], tmpdir[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/atz_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/atz_%d_d_%d", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -2; + fputs(r->src, f); + fclose(f); + + mkdir(tmpdir, 0755); + snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", + tmpdir, driver, src); + int brc = runwait(cmd); + + 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 = -1; + if (brc == 0) got = runwait(outbin); + + unlink(src); unlink(outbin); rmdir(tmpdir); + return brc == 0 ? got : -1; +} + +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], wdrv[1024]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + int have_ww = (access(wdrv, X_OK) == 0); + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + + for (int i = 0; i < n; i++) { + total++; + int gc = run_build(cdrv, &rows[i], i); + if (gc < 0) { + fprintf(stderr, "arrlit_tail_zero[cstage][%s]: build/run " + "failed (got %d)\n", rows[i].label, gc); + fail++; + continue; + } + if (gc != rows[i].want_exit) { + fprintf(stderr, "arrlit_tail_zero[cstage][%s]: exit=%d " + "want=%d\n", rows[i].label, gc, rows[i].want_exit); + fail++; + } + if (!have_ww) { + fprintf(stderr, "arrlit_tail_zero: skip wwstage (no %s)\n", wdrv); + continue; + } + int gw = run_build(wdrv, &rows[i], i); + if (gw != gc) { + fprintf(stderr, "arrlit_tail_zero[%s]: cs=%d != ww=%d " + "(tail fill divergence — #13)\n", rows[i].label, gc, gw); + fail++; + } + if (gw != rows[i].want_exit) { + fprintf(stderr, "arrlit_tail_zero[wwstage][%s]: exit=%d " + "want=%d\n", rows[i].label, gw, rows[i].want_exit); + fail++; + } + } + + if (fail) { + fprintf(stderr, "arrlit_tail_zero_run: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("arrlit_tail_zero_run: %d/%d ok\n", total, total); + return 0; +}