From 3e9a6955e70f0cedde53fbda0d32eacd952cbfc7 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Fri, 5 Jun 2026 23:12:00 +0900 Subject: [PATCH] wcc_ww/cgen: #88 defisaddressable array leg chases the stamped def type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The `&D` addressability gate (defisaddressable, cgen.ww) keyed its array leg on the UNCHASED syntactic dtnode (N_TARRAY) — a def whose declared type is an ALIAS of an array missed the gate and fell to the rule-7 loud error, but the gate was lying: the ww def-array DATA emitter (emitdefconstants' array arm) already peels TY_NAMED off d.lhs.type_ transitively, so the alias def HAS a DATA symbol (probe-OBSERVED: `DATA main.D(SB)` emitted byte-id by both stages for the &-less program). Gate-only fix — tichase(dtn.type_) == TY_ARRAY — restores gate == emission set exactly; no emitter twin, no half-state. The struct leg (defvarstructinfo) already chased; plain [N]T defs agree under tnode and chased reads, so existing rows are byte-id-neutral by construction. cstage gates TK_AMP on the def_isarraydef registry fed by the g-fold-G1 chased let_isarray (cgen.c:3975-3977, 1446) and runs every row 0 — align ww UP. Pin: 944_alias_def_addr_run, 6 rows (plain + struct-def controls hold 0/0; 1/2-level alias + fwd-ref decl order graduate ww-LOUD -> 0/0 byte-id; str-def &S error-path STAYS LOUD both stages with a byte-identical diagnostic — the rule-7 tail text is compared w6c vs w6c_ww, so a silent reject or a divergent message both fail the row). def_l2's readback casts to the base array ptr: the natural (*p)[2] spelling over a 2-LEVEL-alias pointee trips a SEPARATE pre-existing CSTAGE double-deref (spurious MOVQ (AX),AX, SEGV; def-independent, ww correct) — filed as task #93 (#85 type_unwrap kin, F2b OUT), not fixed here (site-set form). Light gates: test-unit 290 green; sizelint 0; 989 ratchet zero flips; five-mains NEUTRAL vs master-74195ac scratch on identical inputs + cs==ww on all five. combined.ww regens ride along (#110). --- Makefile | 7 + selfhost/cmd/w6c/main.combined.ww | 10 +- selfhost/cmd/wcc/cgen.ww | 10 +- selfhost/cmd/wwdump/main.combined.ww | 10 +- test/wcc/944_alias_def_addr_run.c | 321 +++++++++++++++++++++++++++ 5 files changed, 355 insertions(+), 3 deletions(-) create mode 100644 test/wcc/944_alias_def_addr_run.c diff --git a/Makefile b/Makefile index 5c87ab9f..c8e7a15a 100644 --- a/Makefile +++ b/Makefile @@ -306,6 +306,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_alias_accept_run \ $(BIN)/test_alias_idx_family_run \ $(BIN)/test_alias_amp_idx_run \ + $(BIN)/test_alias_def_addr_run \ $(BIN)/test_alias_global_decl_run \ $(BIN)/test_tuple_nary_destructure_run \ $(BIN)/test_rvalue_tuple_destructure_run \ @@ -1475,6 +1476,12 @@ $(BIN)/test_alias_amp_idx_run: test/wcc/944_alias_amp_idx_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_alias_def_addr_run: test/wcc/944_alias_def_addr_run.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_alias_global_decl_run: test/wcc/944_alias_global_decl_run.c \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 6bbfdb02..27aca54c 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -40077,8 +40077,16 @@ fn floatlitleaf(rhs: *node) bool = { fn defisaddressable(c: *cgen, opnd: *node) bool = { let nm: str = opnd.str; if (defvarstructinfo(c, nm) != nil) { return true; }; + // #88: the emission side (emitdefconstants' array arm) peels + // TY_NAMED off d.lhs.type_ transitively, so an alias-typed def + // array HAS a DATA symbol — keying this gate on the unchased + // dtnode kind (N_TARRAY) lied it back to the loud error. Chase + // the same stamped tinfo so gate == emission set stays exact. let dtn: *node = defvartnode(c, nm); - if (dtn != nil) { if (dtn.kind == nkind.N_TARRAY) { return true; }; }; + if (dtn != nil) { + let du88: *tinfo = tichase(dtn.type_: *tinfo); + if (du88 != nil) { if (du88.kind == tykind.TY_ARRAY) { return true; }; }; + }; let drhs: *node = deflookuprhs(c, nm); if (drhs == nil) { return false; }; let v: u64 = 0u64; diff --git a/selfhost/cmd/wcc/cgen.ww b/selfhost/cmd/wcc/cgen.ww index 64d8dc5e..dac157cd 100644 --- a/selfhost/cmd/wcc/cgen.ww +++ b/selfhost/cmd/wcc/cgen.ww @@ -3085,8 +3085,16 @@ fn floatlitleaf(rhs: *node) bool = { fn defisaddressable(c: *cgen, opnd: *node) bool = { let nm: str = opnd.str; if (defvarstructinfo(c, nm) != nil) { return true; }; + // #88: the emission side (emitdefconstants' array arm) peels + // TY_NAMED off d.lhs.type_ transitively, so an alias-typed def + // array HAS a DATA symbol — keying this gate on the unchased + // dtnode kind (N_TARRAY) lied it back to the loud error. Chase + // the same stamped tinfo so gate == emission set stays exact. let dtn: *node = defvartnode(c, nm); - if (dtn != nil) { if (dtn.kind == nkind.N_TARRAY) { return true; }; }; + if (dtn != nil) { + let du88: *tinfo = tichase(dtn.type_: *tinfo); + if (du88 != nil) { if (du88.kind == tykind.TY_ARRAY) { return true; }; }; + }; let drhs: *node = deflookuprhs(c, nm); if (drhs == nil) { return false; }; let v: u64 = 0u64; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 570da5b3..61514535 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -40077,8 +40077,16 @@ fn floatlitleaf(rhs: *node) bool = { fn defisaddressable(c: *cgen, opnd: *node) bool = { let nm: str = opnd.str; if (defvarstructinfo(c, nm) != nil) { return true; }; + // #88: the emission side (emitdefconstants' array arm) peels + // TY_NAMED off d.lhs.type_ transitively, so an alias-typed def + // array HAS a DATA symbol — keying this gate on the unchased + // dtnode kind (N_TARRAY) lied it back to the loud error. Chase + // the same stamped tinfo so gate == emission set stays exact. let dtn: *node = defvartnode(c, nm); - if (dtn != nil) { if (dtn.kind == nkind.N_TARRAY) { return true; }; }; + if (dtn != nil) { + let du88: *tinfo = tichase(dtn.type_: *tinfo); + if (du88 != nil) { if (du88.kind == tykind.TY_ARRAY) { return true; }; }; + }; let drhs: *node = deflookuprhs(c, nm); if (drhs == nil) { return false; }; let v: u64 = 0u64; diff --git a/test/wcc/944_alias_def_addr_run.c b/test/wcc/944_alias_def_addr_run.c new file mode 100644 index 00000000..d8c92989 --- /dev/null +++ b/test/wcc/944_alias_def_addr_run.c @@ -0,0 +1,321 @@ +/* + * 944_alias_def_addr_run — #5 alias arc F2a batch 3 c2 (task #88): + * defisaddressable (cgen.ww:3089) keyed the array leg on the UNCHASED + * syntactic dtnode (N_TARRAY) — `&D` where D is a def whose declared + * type is an ALIAS of an array missed the gate and fell to the rule-7 + * loud error "cannot take address of non-addressable def". LOUD class, + * but the gate was lying: the ww def-array DATA emitter already peels + * TY_NAMED transitively (emitdefconstants' array arm), so the alias + * def HAS a DATA symbol (probe-observed: `DATA main.D(SB)` emitted + * byte-id by both stages for the &-less program). Gate-only fix — + * tichase(dtn.type_) == TY_ARRAY — restores gate == emission set; no + * emitter twin. cstage registers def arrays via the g-fold-G1 chased + * let_isarray and gates TK_AMP on def_isarraydef (cgen.c:3975-3977), + * so cs is the runtime-correct reference (observed 0 on every row). + * + * row | shape | want + * ------------+----------------------------------------------+----- + * def_ctrl | plain [3]int def; &D; read via ptr | 0 + * def_l1 | 1-level alias def array, &D | 0 + * def_l2 | 2-level, readback via cast-to-base ptr | 0 + * def_order | type decl AFTER the def | 0 + * def_struct | alias-typed STRUCT def, &D (defvarstructinfo | + * | leg already chased — held throughout) | 0 + * def_nonaddr | str def, &D -> rule-7 loud error | LOUD + * + * Pre-fix: def_l1/def_l2/def_order ww LOUD / cs 0; controls 0/0. + * Post-fix: every value row 0/0 byte-id; def_nonaddr STAYS LOUD both + * stages with a BYTE-IDENTICAL diagnostic (the error-path pin — + * genuinely symbol-less defs must keep firing the rule-7 tail, and + * the tail's text is itself a convergence surface). + * + * def_l2's readback casts to the BASE array ptr (`p: *[3]int`) before + * the deref-index: the natural `(*p)[2]` spelling over a 2-LEVEL-alias + * pointee trips a SEPARATE pre-existing CSTAGE double-deref (spurious + * MOVQ (AX),AX, SEGV; def-INDEPENDENT — a local twin reproduces it; + * ww runs it correctly) — filed as task #93 (#85 type_unwrap kin, F2b + * OUT territory), deliberately not pinned here. + */ +#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; +} + +static int +slurp_eq(const char *a, const char *b) +{ + FILE *fa = fopen(a, "rb"); + FILE *fb = fopen(b, "rb"); + if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; } + int rc = 0; + for (;;) { + int ca = fgetc(fa), cb = fgetc(fb); + if (ca != cb) { rc = -1; break; } + if (ca == EOF) break; + } + fclose(fa); fclose(fb); + return rc; +} + +/* mustfail: build must FAIL on BOTH drivers (the error-path pin); + * such rows trade the asm byte-id check for a DIAGNOSTIC byte-id + * check (w6c vs w6c_ww stderr compared byte-for-byte). */ +struct row { const char *label; const char *src; int want; int mustfail; }; + +static const struct row rows[] = { + { "def_ctrl", + "package main;\n" + "def D: [3]int = [1000: int, 2000: int, 3000: int];\n" + "export fn main() i32 = {\n" + " let p: *[3]int = &D;\n" + " if ((*p)[2] != 3000) { return 1; };\n" + " return 0;\n" + "};\n", 0, 0 }, + { "def_l1", + "package main;\n" + "type arr = [3]int;\n" + "def D: arr = [1000: int, 2000: int, 3000: int];\n" + "export fn main() i32 = {\n" + " let p: *arr = &D;\n" + " if ((*p)[2] != 3000) { return 1; };\n" + " return 0;\n" + "};\n", 0, 0 }, + /* cast-to-base readback — see the header note on task #93. */ + { "def_l2", + "package main;\n" + "type arr = [3]int;\n" + "type arr2 = arr;\n" + "def D: arr2 = [1000: int, 2000: int, 3000: int];\n" + "export fn main() i32 = {\n" + " let p: *arr2 = &D;\n" + " let q: *[3]int = p: *[3]int;\n" + " if ((*q)[2] != 3000) { return 1; };\n" + " return 0;\n" + "};\n", 0, 0 }, + { "def_order", + "package main;\n" + "def D: arr = [1000: int, 2000: int, 3000: int];\n" + "export fn main() i32 = {\n" + " let p: *arr = &D;\n" + " if ((*p)[2] != 3000) { return 1; };\n" + " return 0;\n" + "};\n" + "type arr = [3]int;\n", 0, 0 }, + /* PREMISE-5 leg: defvarstructinfo already walks aliaslookup + * transitively — held before and after. */ + { "def_struct", + "package main;\n" + "type pt = struct { x: int, y: int };\n" + "type pt2 = pt;\n" + "def D: pt2 = pt2{x=1000, y=2000};\n" + "export fn main() i32 = {\n" + " let p: *pt2 = &D;\n" + " if (p.y != 2000) { return 1; };\n" + " return 0;\n" + "};\n", 0, 0 }, + /* str def has no DATA symbol — the rule-7 loud tail must keep + * firing (error-path pin). */ + { "def_nonaddr", + "package main;\n" + "def S: str = \"hello\";\n" + "export fn main() i32 = {\n" + " let p = &S;\n" + " return 0;\n" + "};\n", 0, 1 }, +}; + +static int +run_driver(const char *driver, const struct row *r, int i) +{ + char src[96], tmpdir[96], errf[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/ada_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/ada_%d_d_%d", getpid(), i); + snprintf(errf, sizeof errf, "/tmp/ada_%d_e_%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 && timeout 20 %s build %s >/dev/null 2>%s", + tmpdir, driver, src, errf); + int brc = runwait(cmd); + if (r->mustfail) { + unlink(src); unlink(errf); rmdir(tmpdir); + if (brc == 0) { + fprintf(stderr, "row[%s]: %s built but must fail " + "loud\n", r->label, driver); + return 1; + } + return 0; + } + if (brc != 0) { + fprintf(stderr, "row[%s]: build via %s failed\n", + r->label, driver); + unlink(src); unlink(errf); rmdir(tmpdir); + return -1; + } + + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + char outbin[256]; + 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); unlink(errf); rmdir(tmpdir); + if (got != r->want) { + fprintf(stderr, "row[%s]: %s exit %d, want %d\n", + r->label, driver, got, r->want); + return 1; + } + return 0; +} + +static int +asm_byte_identical(const char *bin, const struct row *r, int i) +{ + char src[96], cs[96], ws[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/ada_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/ada_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/ada_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; + } + int rc = slurp_eq(cs, ws); + if (rc != 0) + fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n", + r->label); + unlink(src); unlink(cs); unlink(ws); + return rc; +} + +/* Error-path twin of asm_byte_identical: both compilers must REJECT + * the row and emit the same diagnostic bytes (an empty stderr is a + * fail — a silent reject is not the rule-7 loud tail). */ +static int +diag_byte_identical(const char *bin, const struct row *r, int i) +{ + char src[96], cs[96], ws[96], ce[96], we[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/ada_diag_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/ada_diag_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/ada_diag_%d_%d_w.s", getpid(), i); + snprintf(ce, sizeof ce, "/tmp/ada_diag_%d_%d_c.e", getpid(), i); + snprintf(we, sizeof we, "/tmp/ada_diag_%d_%d_w.e", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + int rc = 0; + snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>%s", bin, cs, src, ce); + if (runwait(cmd) == 0) { + fprintf(stderr, "row[%s]: w6c accepted but must fail loud\n", + r->label); + rc = -1; + } + snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>%s", + bin, ws, src, we); + if (rc == 0 && runwait(cmd) == 0) { + fprintf(stderr, "row[%s]: w6c_ww accepted but must fail " + "loud\n", r->label); + rc = -1; + } + if (rc == 0) { + FILE *fe = fopen(ce, "rb"); + if (!fe || fgetc(fe) == EOF) { + fprintf(stderr, "row[%s]: empty diagnostic\n", + r->label); + rc = -1; + } + if (fe) fclose(fe); + } + if (rc == 0) { + rc = slurp_eq(ce, we); + if (rc != 0) + fprintf(stderr, "row[%s]: cstage vs wwstage " + "diagnostic differs\n", r->label); + } + unlink(src); unlink(cs); unlink(ws); unlink(ce); unlink(we); + return rc; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[2080]; + 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[2120], wdrv[2120]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + + for (int i = 0; i < n; i++) { + total++; + if (run_driver(cdrv, &rows[i], i) != 0) fail++; + } + if (access(wdrv, X_OK) == 0) { + for (int i = 0; i < n; i++) { + total++; + if (run_driver(wdrv, &rows[i], i) != 0) fail++; + } + for (int i = 0; i < n; i++) { + total++; + if (rows[i].mustfail) { + if (diag_byte_identical(bin, &rows[i], i) + != 0) fail++; + continue; + } + if (asm_byte_identical(bin, &rows[i], i) != 0) fail++; + } + } + + if (fail) { + fprintf(stderr, "alias_def_addr: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("alias_def_addr: %d/%d ok\n", total, total); + return 0; +}