From 3546673756d18db98282b5f90cdcd7c7d704a324 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sat, 6 Jun 2026 10:36:16 +0900 Subject: [PATCH] wcc_ww/cgen: #63 alias-named struct-lit fill via structlookupchain (let-init + sret-return) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cglet's N_STRUCTLIT init arm resolved the struct by a bare structlookup(c, sname). For an alias-NAMED literal (`type rep2 = rep; let r = rep2{id=6}`) the type ref carries the alias name "rep2" but only the base `rep` is registered, so the lookup returned nil and the field-fill never fired. The nil then split by slot size into two symptoms of one root: - <=8B: the small-let scalar default zeroed the slot and DROPPED the literal (SILENT wrong — the field read 0), and - >8B: no fill arm matched, falling to the cglet "unhandled rhs shape" LOUD (task #7/rule-7). Route the arm through structlookupchain (the #92/W2 SSoT already adopted at cgenstmt:1974/:2687), which chases the alias chain to the base struct. trefn (rhs.lhs) is already the N_IDENT/N_TNAME type ref structlookupchain accepts, so the bare sname extraction is dropped. cstage operates on the resolved Type* via type_chase_named and was always correct: ww-only align-UP, cs UNTOUCHED. ROUTED (the two reachable silent sites, one class): :2421 local N_STRUCTLIT let-init — the #63 repro hits it for both the <=8B silent-zero and the >24B loud symptoms. :1250 >24B sret RETURN twin (reviewer-63). sretretsize chases the alias for the size GATE so this sret arm fires, but the fill used the same bare structlookup(sname) — for an alias-named >24B literal it returned nil and the fill was SKIPPED, so the callee returned an uninitialised sret buffer (SILENT wrong, runtime-0; cs correct). Same root, same symptom, sibling site → folded by construction. DECLINED (traced, not blind-routed; rule-11 + the #101 precedent): :2625 N_IDENT struct-copy — also bare-structlookup but the copy falls through to a generic path byte-identical with cstage; both stages run correct. The post-copy field-READ diverges (cs direct-offset vs ww LEAQ-indirect) = the #81/#65 alias field-read class, out of #63 scope. :2511 N_CALL struct-recv — blocked UPSTREAM by the aggregate- return shape (#272/#277); ww louds at the sender. :1363 <=24B register RETURN — alias case louds via the same scalar-default catch (#277), not silently wrong. The 2 already-chasing sites (1974/2687) untouched. CONVERGENCE: m3_letinit_typed + m3_letinit_untyped (ww silent-zero -> 6/6 byte-id) + m6_letlit_alias (ww loud -> 7 byte-id) + sret_return_- alias32 (ww silent-0 -> 10 byte-id), plus a non-alias control (no-regress). Bootstrap byte-id NEUTRAL (selfhost has no alias-struct-litinit/return; all 4 selfhost tools cs==ww confirmed). Test: 944_alias_structlit_init_run (5 rows x cs-run + ww-run + cs==ww byte-id = 15 checks), Makefile-wired. --- Makefile | 7 + selfhost/cmd/w6c/main.combined.ww | 37 ++-- selfhost/cmd/wcc/cgenstmt.ww | 37 ++-- selfhost/cmd/wwdump/main.combined.ww | 37 ++-- test/wcc/944_alias_structlit_init_run.c | 253 ++++++++++++++++++++++++ 5 files changed, 329 insertions(+), 42 deletions(-) create mode 100644 test/wcc/944_alias_structlit_init_run.c diff --git a/Makefile b/Makefile index 60fd9b0a..753c0df9 100644 --- a/Makefile +++ b/Makefile @@ -303,6 +303,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_subslice_ptresz_run \ $(BIN)/test_deref_slice_store_run \ $(BIN)/test_alias_decl_order_size_run \ + $(BIN)/test_alias_structlit_init_run \ $(BIN)/test_alias_accept_run \ $(BIN)/test_alias_idx_family_run \ $(BIN)/test_alias_amp_idx_run \ @@ -1473,6 +1474,12 @@ $(BIN)/test_alias_decl_order_size_run: test/wcc/944_alias_decl_order_size_run.c $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_alias_structlit_init_run: test/wcc/944_alias_structlit_init_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_accept_run: test/wcc/944_alias_accept_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 13682e70..976da51e 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -33730,14 +33730,20 @@ fn cgreturn(c: *cgen, n: *node) void = { }; if (okrhs) { if (rhs.kind == nkind.N_STRUCTLIT) { + // #63: the >24B sret RETURN twin of the :2421 + // let-init fix. sretretsize chases the alias for + // the size GATE (so this sret arm fires for a >24B + // alias struct), but the field-fill resolved the + // struct by a bare structlookup(c, sname): for an + // alias-NAMED literal (`type biga = big; return + // biga{...}`) sname is "biga", unregistered, so + // sret_si was nil and the fill was SKIPPED — the + // callee returned an uninitialised sret buffer + // (SILENT wrong, runtime-0). structlookupchain chases + // to the base struct; cs fills via the resolved + // Type*, runtime-correct. let trefn: *node = rhs.lhs; - let sname: str; - sname.ptr = nil; sname.len = 0; - if (trefn != nil) { - if (trefn.kind == nkind.N_IDENT) { sname = trefn.str; } - else { if (trefn.kind == nkind.N_TNAME) { sname = trefn.str; }; }; - }; - let sret_si: *structinfo = structlookup(c, sname); + let sret_si: *structinfo = structlookupchain(c, trefn); if (sret_si != nil) { let emptys: str; emptys.ptr = nil; emptys.len = 0; @@ -34898,14 +34904,17 @@ fn cglet(c: *cgen, n: *node) void = { // (the #17 silent-zero fix). Mirror of cstage cgen.c N_LET // structlit branch. if (rhs.kind == nkind.N_STRUCTLIT) { + // #63: an alias-NAMED struct literal (`type rep2 = rep; + // let r = rep2{id=6}`) parses its type ref as N_IDENT/N_TNAME + // "rep2", but only the base `rep` is registered — bare + // structlookup(c, "rep2") returns nil, so the fill never + // fired: the slot zeroed + the lit DROPPED (≤8B silent) or + // fell to the :2920 LOUD (>8B). structlookupchain chases the + // alias chain to the base struct, the #92/W2 SSoT already + // adopted at cgenstmt:1974/:2687. cs chases via + // type_chase_named, runtime-correct. let trefn: *node = rhs.lhs; - let sname: str; - sname.ptr = nil; sname.len = 0; - if (trefn != nil) { - if (trefn.kind == nkind.N_IDENT) { sname = trefn.str; } - else { if (trefn.kind == nkind.N_TNAME) { sname = trefn.str; }; }; - }; - let si: *structinfo = structlookup(c, sname); + let si: *structinfo = structlookupchain(c, trefn); if (si != nil) { cgstructlitfillbp(c, si, rhs, off); c.lastwasreturn = 0; diff --git a/selfhost/cmd/wcc/cgenstmt.ww b/selfhost/cmd/wcc/cgenstmt.ww index 6b216bcd..514b5953 100644 --- a/selfhost/cmd/wcc/cgenstmt.ww +++ b/selfhost/cmd/wcc/cgenstmt.ww @@ -1240,14 +1240,20 @@ fn cgreturn(c: *cgen, n: *node) void = { }; if (okrhs) { if (rhs.kind == nkind.N_STRUCTLIT) { + // #63: the >24B sret RETURN twin of the :2421 + // let-init fix. sretretsize chases the alias for + // the size GATE (so this sret arm fires for a >24B + // alias struct), but the field-fill resolved the + // struct by a bare structlookup(c, sname): for an + // alias-NAMED literal (`type biga = big; return + // biga{...}`) sname is "biga", unregistered, so + // sret_si was nil and the fill was SKIPPED — the + // callee returned an uninitialised sret buffer + // (SILENT wrong, runtime-0). structlookupchain chases + // to the base struct; cs fills via the resolved + // Type*, runtime-correct. let trefn: *node = rhs.lhs; - let sname: str; - sname.ptr = nil; sname.len = 0; - if (trefn != nil) { - if (trefn.kind == nkind.N_IDENT) { sname = trefn.str; } - else { if (trefn.kind == nkind.N_TNAME) { sname = trefn.str; }; }; - }; - let sret_si: *structinfo = structlookup(c, sname); + let sret_si: *structinfo = structlookupchain(c, trefn); if (sret_si != nil) { let emptys: str; emptys.ptr = nil; emptys.len = 0; @@ -2408,14 +2414,17 @@ fn cglet(c: *cgen, n: *node) void = { // (the #17 silent-zero fix). Mirror of cstage cgen.c N_LET // structlit branch. if (rhs.kind == nkind.N_STRUCTLIT) { + // #63: an alias-NAMED struct literal (`type rep2 = rep; + // let r = rep2{id=6}`) parses its type ref as N_IDENT/N_TNAME + // "rep2", but only the base `rep` is registered — bare + // structlookup(c, "rep2") returns nil, so the fill never + // fired: the slot zeroed + the lit DROPPED (≤8B silent) or + // fell to the :2920 LOUD (>8B). structlookupchain chases the + // alias chain to the base struct, the #92/W2 SSoT already + // adopted at cgenstmt:1974/:2687. cs chases via + // type_chase_named, runtime-correct. let trefn: *node = rhs.lhs; - let sname: str; - sname.ptr = nil; sname.len = 0; - if (trefn != nil) { - if (trefn.kind == nkind.N_IDENT) { sname = trefn.str; } - else { if (trefn.kind == nkind.N_TNAME) { sname = trefn.str; }; }; - }; - let si: *structinfo = structlookup(c, sname); + let si: *structinfo = structlookupchain(c, trefn); if (si != nil) { cgstructlitfillbp(c, si, rhs, off); c.lastwasreturn = 0; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 0ed4bc28..41d8a514 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -33730,14 +33730,20 @@ fn cgreturn(c: *cgen, n: *node) void = { }; if (okrhs) { if (rhs.kind == nkind.N_STRUCTLIT) { + // #63: the >24B sret RETURN twin of the :2421 + // let-init fix. sretretsize chases the alias for + // the size GATE (so this sret arm fires for a >24B + // alias struct), but the field-fill resolved the + // struct by a bare structlookup(c, sname): for an + // alias-NAMED literal (`type biga = big; return + // biga{...}`) sname is "biga", unregistered, so + // sret_si was nil and the fill was SKIPPED — the + // callee returned an uninitialised sret buffer + // (SILENT wrong, runtime-0). structlookupchain chases + // to the base struct; cs fills via the resolved + // Type*, runtime-correct. let trefn: *node = rhs.lhs; - let sname: str; - sname.ptr = nil; sname.len = 0; - if (trefn != nil) { - if (trefn.kind == nkind.N_IDENT) { sname = trefn.str; } - else { if (trefn.kind == nkind.N_TNAME) { sname = trefn.str; }; }; - }; - let sret_si: *structinfo = structlookup(c, sname); + let sret_si: *structinfo = structlookupchain(c, trefn); if (sret_si != nil) { let emptys: str; emptys.ptr = nil; emptys.len = 0; @@ -34898,14 +34904,17 @@ fn cglet(c: *cgen, n: *node) void = { // (the #17 silent-zero fix). Mirror of cstage cgen.c N_LET // structlit branch. if (rhs.kind == nkind.N_STRUCTLIT) { + // #63: an alias-NAMED struct literal (`type rep2 = rep; + // let r = rep2{id=6}`) parses its type ref as N_IDENT/N_TNAME + // "rep2", but only the base `rep` is registered — bare + // structlookup(c, "rep2") returns nil, so the fill never + // fired: the slot zeroed + the lit DROPPED (≤8B silent) or + // fell to the :2920 LOUD (>8B). structlookupchain chases the + // alias chain to the base struct, the #92/W2 SSoT already + // adopted at cgenstmt:1974/:2687. cs chases via + // type_chase_named, runtime-correct. let trefn: *node = rhs.lhs; - let sname: str; - sname.ptr = nil; sname.len = 0; - if (trefn != nil) { - if (trefn.kind == nkind.N_IDENT) { sname = trefn.str; } - else { if (trefn.kind == nkind.N_TNAME) { sname = trefn.str; }; }; - }; - let si: *structinfo = structlookup(c, sname); + let si: *structinfo = structlookupchain(c, trefn); if (si != nil) { cgstructlitfillbp(c, si, rhs, off); c.lastwasreturn = 0; diff --git a/test/wcc/944_alias_structlit_init_run.c b/test/wcc/944_alias_structlit_init_run.c new file mode 100644 index 00000000..b5862c2c --- /dev/null +++ b/test/wcc/944_alias_structlit_init_run.c @@ -0,0 +1,253 @@ +/* + * 944_alias_structlit_init_run — #63: let-init from an alias-NAMED + * struct LITERAL. wwstage's cglet N_STRUCTLIT arm resolved the struct + * by a BARE structlookup(c, sname): for an alias `type rep2 = rep` + * only the base `rep` is registered, so the lookup returned nil and + * the field-fill never fired. The nil then split by slot size — + * • <=8B: the small-let scalar default zeroed the slot and DROPPED + * the literal (SILENT wrong: `let r = rep2{id=6}` read 0), and + * • >8B: no fill arm matched -> the cglet "unhandled rhs shape" + * LOUD (task #7/rule-7). + * Same root, two symptoms. The fix routes the arm through + * structlookupchain (the #92/W2 SSoT already used at cgenstmt:1974 / + * :2687), which chases the alias chain to the base struct. cstage + * operates on the resolved Type* via type_chase_named and was always + * correct, so this is ww-only align-UP; cs UNTOUCHED. Oracle: ken + * .ai/ken-63-oracle.md. + * + * The N_IDENT-copy (cgenstmt:2625) and N_CALL-recv (:2511) sibling + * arms are also bare-structlookup but are NOT in scope: the copy + * falls through to a generic path byte-identical with cstage (both + * stages run correct), and the recv is blocked upstream by the + * aggregate-return shape (#272/#277), and the ≤24B return (:1363) + * likewise louds via the scalar-default catch. The >24B sret RETURN + * (:1250) had NO such catch — it was silently wrong (returned an + * uninitialised sret buffer); reviewer-63 routed it through + * structlookupchain in the same commit (see sret_return_alias32). + * The N_IDENT struct-copy (:2625) falls through to a generic path + * both stages run correct (asm divergence is the #81/#65 alias + * field-READ class). See the #63 ledger. + * + * row | shape | want + * -------------------+---------------------------------------+----- + * letinit_typed | type rep2=rep(8B); let r: rep2 = | + * | rep2{id=6}; read r.id (was silent 0) | 6 + * letinit_untyped | inferred: let r = rep2{id=6}; r.id | 6 + * letlit_alias16 | type row=st(16B); let a: row = | + * | row{a=3,b=4}; read a.a+a.b (was LOUD) | 7 + * control_direct | non-alias pt2{...} 16B let-init — the | + * | no-regress control (alias hop is the | + * | trigger), byte-id both stages | 7 + * sret_return_alias32| type biga=big(32B); mk() returns | + * | biga{...}; the >24B sret RETURN twin | + * | (:1250, was silent 0) | 10 + * + * Every row asserts cstage/wwstage asm byte-id (rule 10). NNN<950, + * self-contained (/tmp, no imports) — rule-14's selfhost-sibling race + * does not apply (941/944 precedent). + */ +#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; +} + +struct row { const char *label; const char *src; int want; }; + +static const struct row rows[] = { + { "letinit_typed", + "package main;\n" + "type rep = struct { id: size };\n" + "type rep2 = rep;\n" + "export fn main() i32 = {\n" + " let r: rep2 = rep2{id=6};\n" + " return r.id: i32;\n" + "};\n", 6 }, + { "letinit_untyped", + "package main;\n" + "type rep = struct { id: size };\n" + "type rep2 = rep;\n" + "export fn main() i32 = {\n" + " let r = rep2{id=6};\n" + " return r.id: i32;\n" + "};\n", 6 }, + { "letlit_alias16", + "package main;\n" + "type st = struct { a: size, b: size };\n" + "type row = st;\n" + "export fn main() i32 = {\n" + " let a: row = row{a=3, b=4};\n" + " return (a.a + a.b): i32;\n" + "};\n", 7 }, + { "control_direct", + "package main;\n" + "type pt2 = struct { a: size, b: size };\n" + "export fn main() i32 = {\n" + " let x: pt2 = pt2{a=3, b=4};\n" + " return (x.a + x.b): i32;\n" + "};\n", 7 }, + /* reviewer-63 survivor leg: the >24B sret-RETURN twin of the + * let-init site (cgenstmt:1250). sretretsize chases the alias for + * the size gate so the sret arm fires, but the field-fill used a + * bare structlookup(sname) that missed the alias name -> the fill + * was SKIPPED and the callee returned an uninitialised sret buffer + * (ww silent 0; cs correct 10). The impl ledger wrongly declined + * :1250 as "already chases"; routed through structlookupchain. */ + { "sret_return_alias32", + "package main;\n" + "type big = struct { a: size, b: size, c: size, d: size };\n" + "type biga = big;\n" + "fn mk() biga = { return biga{a=1, b=2, c=3, d=4}; };\n" + "export fn main() i32 = {\n" + " let r: biga = mk();\n" + " return (r.a + r.b + r.c + r.d): i32;\n" + "};\n", 10 }, +}; + +/* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */ +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/asli_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/asli_%d_d_%d", getpid(), i); + snprintf(errf, sizeof errf, "/tmp/asli_%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 (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; +} + +/* cs==ww .s byte-id (rule 10). */ +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/asli_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/asli_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/asli_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; +} + +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 (asm_byte_identical(bin, &rows[i], i) != 0) fail++; + } + } + + if (fail) { + fprintf(stderr, "alias_structlit_init: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("alias_structlit_init: %d/%d ok\n", total, total); + return 0; +}