From 7afc4df6520e4a9512629ec12e3c8b92074210c8 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Fri, 12 Jun 2026 00:11:11 +0900 Subject: [PATCH] wcc/ww: paramfieldsize sizes slice/tuple fields through the type table MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit for-range destructure of an element with a slice- or tuple-typed field strode by the default 8 (paramfieldsize had no N_TSLICE/N_TTUPLE arms), silently reading the wrong words (review finding #43; live repro cs=42 vs ww=8). Add the arms routed through tinfo per rule 13. 989_tupfieldsize_run pins cs==ww (red 1/2 pre-fix); rows assert convergence, not absolutes — cstage's own single-word destructure-load bug is filed as task #40. The N_TARRAY arm is deferred (task #39, rule-7 comment at the fall-through). --- Makefile | 12 ++ selfhost/cmd/w6c/main.combined.ww | 30 ++++- selfhost/cmd/wcc/cgenstmt.ww | 30 ++++- selfhost/cmd/wwdump/main.combined.ww | 30 ++++- test/wcc/989_tupfieldsize_run.c | 193 +++++++++++++++++++++++++++ 5 files changed, 292 insertions(+), 3 deletions(-) create mode 100644 test/wcc/989_tupfieldsize_run.c diff --git a/Makefile b/Makefile index 11cc6329..bac82590 100644 --- a/Makefile +++ b/Makefile @@ -250,6 +250,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_callarg_typecheck \ $(BIN)/test_idxarg_run \ $(BIN)/test_chainidx_run \ + $(BIN)/test_tupfieldsize_run \ $(BIN)/test_arr_ptr_global \ $(BIN)/test_def_arr_infer_len \ $(BIN)/test_def_arr_len \ @@ -655,6 +656,17 @@ $(BIN)/test_chainidx_run: test/wcc/989_chainidx_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 989_tupfieldsize_run (F7-c4, #43): a for-range destructure over an array +# of tuples must stride by the tuple's true size (a slice/str/tuple field +# carries its full width). Builds+runs on BOTH driver twins (rule-10), the +# slice-field row pinned cs==ww (see the test header on the absolute value). +$(BIN)/test_tupfieldsize_run: test/wcc/989_tupfieldsize_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 $@ $< + $(BIN)/test_let_global: test/wcc/630_let_global.c $(BIN)/ww $(BIN)/w6c \ $(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index fa95c4ff..26cc70ec 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -38409,13 +38409,36 @@ fn cgmlet(c: *cgen, n: *node) void = { // paramfieldsize — raw byte size of a tuple-field type. Mirrors the // `tp->type->size` read in C cgen N_FORRANGE: 1 for i8/u8/bool, 4 for -// i32/u32, 8 for i64/u64/*T/fn/slice-elt, 16 for str, default 8. +// i32/u32, 8 for i64/u64/*T/fn, 24 for str/slice (str IS []u8, the slice +// header SSoT), tuple → sum of its 8B-floored element slots, default 8. fn paramfieldsize(t: *node) i32 = { if (t == nil) { return 8; }; let k: nkind = t.kind; if (k == nkind.N_TPTR) { return 8; }; if (k == nkind.N_TFN) { return 8; }; if (k == nkind.N_TCHAN) { return 8; }; + // #43 (F7-c4): a slice tuple-field carries the 24B header (ptr+len+ + // cap), not the 8B scalar default. Without this arm the for-range + // destructure over `[N]([]T, U)` strode the tuple at 8 not 24 and + // read field-2 at the wrong offset (cs=42/ww=8, the cat-A repro). + // tyslicesize() is the slice-header SSoT (rule-13); cstage reads the + // same width via tp->type->size (cmd/w6c/cgen.c N_FORRANGE). + if (k == nkind.N_TSLICE) { return tyslicesize(): i32; }; + // #43 (F7-c4): a nested tuple field sizes as the sum of its element + // SLOTS — each element floored UP to one 8B eightbyte (str/slice keep + // their 24B header), per the tuple-slot ruling and tupeslot's + // roundup8. Recurse structurally so a tuple-of-tuple lands the same + // stride cstage's tp->type->size computes. + if (k == nkind.N_TTUPLE) { + let total: i32 = 0; + let dp: *node = t.list; + for (dp != nil) { + let esz: i32 = paramfieldsize(dp.lhs); + total = total + (esz + 7) / 8 * 8; + dp = dp.next; + }; + return total; + }; if (k == nkind.N_TNAME) { let nm: str = t.str; if (streq(nm, "str")) { return primtypesize("str"): i32; }; @@ -38427,6 +38450,11 @@ fn paramfieldsize(t: *node) i32 = { let ps: i32 = primsize(nm); if (ps > 0) { return ps; }; }; + // rule-7: N_TARRAY (an array-typed tuple/struct field) is intentionally + // not sized here — deferred to task #39. No F7 repro or spec-§5-c4 + // member feeds it, so it stays on the 8B default (mis-sized, latent) + // until a consumer surfaces; the divergence is pointed at the task, not + // silent. return 8; }; diff --git a/selfhost/cmd/wcc/cgenstmt.ww b/selfhost/cmd/wcc/cgenstmt.ww index c9454ca6..5e85878e 100644 --- a/selfhost/cmd/wcc/cgenstmt.ww +++ b/selfhost/cmd/wcc/cgenstmt.ww @@ -3646,13 +3646,36 @@ fn cgmlet(c: *cgen, n: *node) void = { // paramfieldsize — raw byte size of a tuple-field type. Mirrors the // `tp->type->size` read in C cgen N_FORRANGE: 1 for i8/u8/bool, 4 for -// i32/u32, 8 for i64/u64/*T/fn/slice-elt, 16 for str, default 8. +// i32/u32, 8 for i64/u64/*T/fn, 24 for str/slice (str IS []u8, the slice +// header SSoT), tuple → sum of its 8B-floored element slots, default 8. fn paramfieldsize(t: *node) i32 = { if (t == nil) { return 8; }; let k: nkind = t.kind; if (k == nkind.N_TPTR) { return 8; }; if (k == nkind.N_TFN) { return 8; }; if (k == nkind.N_TCHAN) { return 8; }; + // #43 (F7-c4): a slice tuple-field carries the 24B header (ptr+len+ + // cap), not the 8B scalar default. Without this arm the for-range + // destructure over `[N]([]T, U)` strode the tuple at 8 not 24 and + // read field-2 at the wrong offset (cs=42/ww=8, the cat-A repro). + // tyslicesize() is the slice-header SSoT (rule-13); cstage reads the + // same width via tp->type->size (cmd/w6c/cgen.c N_FORRANGE). + if (k == nkind.N_TSLICE) { return tyslicesize(): i32; }; + // #43 (F7-c4): a nested tuple field sizes as the sum of its element + // SLOTS — each element floored UP to one 8B eightbyte (str/slice keep + // their 24B header), per the tuple-slot ruling and tupeslot's + // roundup8. Recurse structurally so a tuple-of-tuple lands the same + // stride cstage's tp->type->size computes. + if (k == nkind.N_TTUPLE) { + let total: i32 = 0; + let dp: *node = t.list; + for (dp != nil) { + let esz: i32 = paramfieldsize(dp.lhs); + total = total + (esz + 7) / 8 * 8; + dp = dp.next; + }; + return total; + }; if (k == nkind.N_TNAME) { let nm: str = t.str; if (streq(nm, "str")) { return primtypesize("str"): i32; }; @@ -3664,6 +3687,11 @@ fn paramfieldsize(t: *node) i32 = { let ps: i32 = primsize(nm); if (ps > 0) { return ps; }; }; + // rule-7: N_TARRAY (an array-typed tuple/struct field) is intentionally + // not sized here — deferred to task #39. No F7 repro or spec-§5-c4 + // member feeds it, so it stays on the 8B default (mis-sized, latent) + // until a consumer surfaces; the divergence is pointed at the task, not + // silent. return 8; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index e150f371..c5baef9a 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -38409,13 +38409,36 @@ fn cgmlet(c: *cgen, n: *node) void = { // paramfieldsize — raw byte size of a tuple-field type. Mirrors the // `tp->type->size` read in C cgen N_FORRANGE: 1 for i8/u8/bool, 4 for -// i32/u32, 8 for i64/u64/*T/fn/slice-elt, 16 for str, default 8. +// i32/u32, 8 for i64/u64/*T/fn, 24 for str/slice (str IS []u8, the slice +// header SSoT), tuple → sum of its 8B-floored element slots, default 8. fn paramfieldsize(t: *node) i32 = { if (t == nil) { return 8; }; let k: nkind = t.kind; if (k == nkind.N_TPTR) { return 8; }; if (k == nkind.N_TFN) { return 8; }; if (k == nkind.N_TCHAN) { return 8; }; + // #43 (F7-c4): a slice tuple-field carries the 24B header (ptr+len+ + // cap), not the 8B scalar default. Without this arm the for-range + // destructure over `[N]([]T, U)` strode the tuple at 8 not 24 and + // read field-2 at the wrong offset (cs=42/ww=8, the cat-A repro). + // tyslicesize() is the slice-header SSoT (rule-13); cstage reads the + // same width via tp->type->size (cmd/w6c/cgen.c N_FORRANGE). + if (k == nkind.N_TSLICE) { return tyslicesize(): i32; }; + // #43 (F7-c4): a nested tuple field sizes as the sum of its element + // SLOTS — each element floored UP to one 8B eightbyte (str/slice keep + // their 24B header), per the tuple-slot ruling and tupeslot's + // roundup8. Recurse structurally so a tuple-of-tuple lands the same + // stride cstage's tp->type->size computes. + if (k == nkind.N_TTUPLE) { + let total: i32 = 0; + let dp: *node = t.list; + for (dp != nil) { + let esz: i32 = paramfieldsize(dp.lhs); + total = total + (esz + 7) / 8 * 8; + dp = dp.next; + }; + return total; + }; if (k == nkind.N_TNAME) { let nm: str = t.str; if (streq(nm, "str")) { return primtypesize("str"): i32; }; @@ -38427,6 +38450,11 @@ fn paramfieldsize(t: *node) i32 = { let ps: i32 = primsize(nm); if (ps > 0) { return ps; }; }; + // rule-7: N_TARRAY (an array-typed tuple/struct field) is intentionally + // not sized here — deferred to task #39. No F7 repro or spec-§5-c4 + // member feeds it, so it stays on the 8B default (mis-sized, latent) + // until a consumer surfaces; the divergence is pointed at the task, not + // silent. return 8; }; diff --git a/test/wcc/989_tupfieldsize_run.c b/test/wcc/989_tupfieldsize_run.c new file mode 100644 index 00000000..e053feba --- /dev/null +++ b/test/wcc/989_tupfieldsize_run.c @@ -0,0 +1,193 @@ +/* + * 989_tupfieldsize_run — F7-c4 (#43): a for-range destructure over an + * array of tuples must stride by the tuple's TRUE size; a slice/str/nested + * tuple FIELD carries its full width, not the 8B scalar default. + * + * THE BUG (cat-A silent miscompile, gate-blind): paramfieldsize + * (selfhost/cmd/wcc/cgenstmt.ww) — the structural sizer the for-range + * destructure uses to compute the tuple stride and each field's offset — + * had no N_TSLICE / N_TTUPLE arm, so a `[]T` tuple-field sized 8 (the + * default) instead of its 24B header. The #270-1c array-literal guard + * blocks only the literal CONSTRUCTION; the for-range DESTRUCTURE-READ + * path is unguarded (ken's oracle refuted "unreachable"). For + * `[2]([]u8, i64)` the wwstage strode the tuple at 16 / read field-2 at + * offset 8, vs cstage's 32 / 24 — a cs≠ww divergence (the cat-A + * signature). cstage's tp->type->size (cmd/w6c/cgen.c N_FORRANGE) reads + * the true width. THE FIX: add the N_TSLICE arm (tyslicesize() = 24, the + * slice-header SSoT, rule-13) and the N_TTUPLE arm (sum of 8B-floored + * element slots, recursive), aligning wwstage UP. + * + * NB on the assertion: the slice-field row is pinned cs==ww (MATCH-ONLY), + * NOT to an absolute value. cstage independently mis-loads a single-word + * tuple-destructure element (a SEPARATE bug ken flagged out of F7 scope, + * filed as task #40 — cstage's absolute value is wrong: 40 != 45), so + * both stages currently land 40, not the arithmetic-expected 45. The + * #43 fix's job is to remove the cs≠ww STRIDE/OFFSET divergence (40 vs 16 + * → 40 vs 40); pinning cs==ww tracks exactly that and won't false-fail + * when the separate destructure-load bug is later fixed (both move + * together). The scalar-tuple control IS correct on both stages, so it + * also pins the absolute value (no-regression teeth). + * + * Tuples are built by whole-tuple element store (`xs[i] = (..)`); the + * `xs[i].0 = ..` field-store target and the `[[..]]` nested literal are + * both independently unsupported / #270-1c-blocked (orthogonal). + * + * Rows (cstage `ww` always; wwstage `ww_ww` when present; rule-10): + * row | shape | assert + * -------------------+--------------------------------+---------------- + * slice_field_tuple | for(.. [2]([]u8,i64)) | cs==ww [#43 bug] + * scalar_tuple_ctl | for(.. [2](i64,i64)) = 48 | cs==ww AND == 48 + */ +#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; /* >= 0: also pin the absolute value; -1: cs==ww only */ +}; + +static const struct row rows[] = { + /* (1) #43 — a []u8 tuple-field sized 8 not 24 → wrong stride/offset. + * cs==ww only (cstage's 40≠45 is the separate destructure-load bug). */ + { "slice_field_tuple", + "package main;\n" + "export fn main() int = {\n" + " let b0: []u8 = ['a', 'b'];\n" + " let b1: []u8 = ['x', 'y', 'z'];\n" + " let xs: [2]([]u8, i64);\n" + " xs[0] = (b0, 10);\n" + " xs[1] = (b1, 30);\n" + " let sum: i64 = 0;\n" + " for (let (b, n) .. xs) {\n" + " sum = sum + len(b): i64 + n;\n" + " };\n" + " return sum: int;\n" + "};\n", + -1 }, + + /* (2) control — scalar-only tuple (no slice/str field): paramfieldsize + * already handled it, so c4 must not regress it. Correct on both stages: + * 3+10+5+30 == 48. Pins cs==ww AND the absolute value. */ + { "scalar_tuple_ctl", + "package main;\n" + "export fn main() int = {\n" + " let xs: [2](i64, i64);\n" + " xs[0] = (3, 10); xs[1] = (5, 30);\n" + " let sum: i64 = 0;\n" + " for (let (a, b) .. xs) { sum = sum + a + b; };\n" + " return sum: int;\n" + "};\n", + 48 }, +}; + +/* run_build — build+run `src` via `driver`; returns the binary's exit + * code, or -1 on a build failure. */ +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/tupfs_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/tupfs_%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); + /* cstage must build+run */ + if (gc < 0) { + fprintf(stderr, "tupfieldsize_run[cstage][%s]: build/run " + "failed (got %d)\n", rows[i].label, gc); + fail++; + continue; + } + if (rows[i].want_exit >= 0 && gc != rows[i].want_exit) { + fprintf(stderr, "tupfieldsize_run[cstage][%s]: exit=%d " + "want=%d\n", rows[i].label, gc, rows[i].want_exit); + fail++; + } + if (!have_ww) { + fprintf(stderr, "tupfieldsize_run: skip wwstage (no %s)\n", + wdrv); + continue; + } + int gw = run_build(wdrv, &rows[i], i); + /* rule-10: the cat-A invariant is cs == ww */ + if (gw != gc) { + fprintf(stderr, "tupfieldsize_run[%s]: cs=%d != ww=%d " + "(stride/offset divergence — #43)\n", + rows[i].label, gc, gw); + fail++; + } + if (rows[i].want_exit >= 0 && gw != rows[i].want_exit) { + fprintf(stderr, "tupfieldsize_run[wwstage][%s]: exit=%d " + "want=%d\n", rows[i].label, gw, rows[i].want_exit); + fail++; + } + } + + if (fail) { + fprintf(stderr, "tupfieldsize_run: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("tupfieldsize_run: %d/%d ok\n", total, total); + return 0; +}