From 1f2bc7fc26a45fe36baf36480cb8bf0576907e01 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sat, 13 Jun 2026 03:04:52 +0900 Subject: [PATCH] wcc/ww: exprtypeoftry prefers the current module's symbol MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The try-operand resolution used bare lookups (ident + bare-leaf call); a cross-module same-leaf collision mistyped the operand. Prefer the current module. The historic 995 byte-id break attributed to this swap was contamination from the guard-bug-carrying bundle — re-probed clean in isolation and at the full stack. The N_DOT module-keyed leaf stays task #51. Report item [11], lookup half. --- Makefile | 12 ++ selfhost/cmd/w6c/main.combined.ww | 27 ++-- selfhost/cmd/wcc/check.ww | 27 ++-- selfhost/cmd/wwdump/main.combined.ww | 27 ++-- test/wcc/989_trycallcollide_run.c | 180 +++++++++++++++++++++++++++ 5 files changed, 237 insertions(+), 36 deletions(-) create mode 100644 test/wcc/989_trycallcollide_run.c diff --git a/Makefile b/Makefile index 00ca67d1..be18fc30 100644 --- a/Makefile +++ b/Makefile @@ -261,6 +261,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_defdim_argslice_run \ $(BIN)/test_slttypepref_run \ $(BIN)/test_ampfncollide_run \ + $(BIN)/test_trycallcollide_run \ $(BIN)/test_gunsigned_run \ $(BIN)/test_taggedidx_run \ $(BIN)/test_fnptrcollide_run \ @@ -804,6 +805,17 @@ $(BIN)/test_ampfncollide_run: test/wcc/989_ampfncollide_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 989_trycallcollide_run (#11a, c3): exprtypeoftry prefers the current +# module's symbol when typing a `?`/`!` operand whose bare leaf collides with +# a same-leaf decl in a later module. Builds+runs on BOTH driver twins +# (rule-10). See the test header. +$(BIN)/test_trycallcollide_run: test/wcc/989_trycallcollide_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 cc44cac5..afaa394b 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -16307,14 +16307,13 @@ fn checkisas(c: *checker, n: *node) void = { fn exprtypeoftry(c: *checker, e: *node) *node = { if (e == nil) { return nil; }; if (e.kind == nkind.N_IDENT) { - // #11a/task #50: this bare-leaf lookup wants curmod preference - // (scopelookupprefer, the #53/#55 family) but the swap BROKE the - // 995 self-rebuild byte-id — in the bundled self-compile ww's - // scopelookupprefer diverges from cstage's resolution (the same - // curmod-in-bundle layer that blocked #5), flipping a try-operand - // stamp in the selfhost source. Held at plain scopelookup until #50 - // lands the bundle curmod fix; aligning here in isolation is unsound. - let s: *sym = scopelookup(c.cur, e.str); + // #11a: curmod preference (the #53/#55 family). A bare ident + // whose leaf also names a global in a later module otherwise + // binds the foreign decl's type, mis-typing the try operand and + // either spuriously rejecting valid `?` code or skipping the F8 + // reject. Mirrors exprtype's own N_IDENT arm (scopelookupprefer + // at :2897); cstage types the operand via cexpr with cur_mod. + let s: *sym = scopelookupprefer(c.cur, c.curmod, e.str); if (s == nil) { return nil; }; if (s.decl == nil) { return nil; }; return s.decl.lhs; @@ -16335,10 +16334,14 @@ fn exprtypeoftry(c: *checker, e: *node) *node = { // returns nil below) rides task #51 with the fn-ptr-callee desugar. if (callee.kind == nkind.N_DOT) { nm = callee.str; }; if (nm.len == 0) { return nil; }; - // #11a/task #50: curmod preference wanted here too, held at plain - // scopelookup — the prefer swap broke 995 byte-id (bundle curmod - // divergence, same as the N_IDENT arm above + #5/#50). - let s: *sym = scopelookup(c.cur, nm); + // #11a: curmod preference for the bare-leaf callee. A same-leaf + // `op()?` declared in a later module otherwise binds the foreign + // op's return type — its error subset then spuriously fails (or + // wrongly passes) the enclosing-return check. Mirrors exprtype's + // N_CALL arm (scopelookupprefer at :3336). The N_DOT-callee leaf + // (callee.str above) still resolves bare-leaf, not via the module + // qualifier callee.lhs.str — that module-keyed fix rides task #51. + let s: *sym = scopelookupprefer(c.cur, c.curmod, nm); if (s == nil) { return nil; }; if (s.skind != skind.SK_FN) { return nil; }; if (s.decl == nil) { return nil; }; diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index c2ee3f9c..9abcb0d9 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -5867,14 +5867,13 @@ fn checkisas(c: *checker, n: *node) void = { fn exprtypeoftry(c: *checker, e: *node) *node = { if (e == nil) { return nil; }; if (e.kind == nkind.N_IDENT) { - // #11a/task #50: this bare-leaf lookup wants curmod preference - // (scopelookupprefer, the #53/#55 family) but the swap BROKE the - // 995 self-rebuild byte-id — in the bundled self-compile ww's - // scopelookupprefer diverges from cstage's resolution (the same - // curmod-in-bundle layer that blocked #5), flipping a try-operand - // stamp in the selfhost source. Held at plain scopelookup until #50 - // lands the bundle curmod fix; aligning here in isolation is unsound. - let s: *sym = scopelookup(c.cur, e.str); + // #11a: curmod preference (the #53/#55 family). A bare ident + // whose leaf also names a global in a later module otherwise + // binds the foreign decl's type, mis-typing the try operand and + // either spuriously rejecting valid `?` code or skipping the F8 + // reject. Mirrors exprtype's own N_IDENT arm (scopelookupprefer + // at :2897); cstage types the operand via cexpr with cur_mod. + let s: *sym = scopelookupprefer(c.cur, c.curmod, e.str); if (s == nil) { return nil; }; if (s.decl == nil) { return nil; }; return s.decl.lhs; @@ -5895,10 +5894,14 @@ fn exprtypeoftry(c: *checker, e: *node) *node = { // returns nil below) rides task #51 with the fn-ptr-callee desugar. if (callee.kind == nkind.N_DOT) { nm = callee.str; }; if (nm.len == 0) { return nil; }; - // #11a/task #50: curmod preference wanted here too, held at plain - // scopelookup — the prefer swap broke 995 byte-id (bundle curmod - // divergence, same as the N_IDENT arm above + #5/#50). - let s: *sym = scopelookup(c.cur, nm); + // #11a: curmod preference for the bare-leaf callee. A same-leaf + // `op()?` declared in a later module otherwise binds the foreign + // op's return type — its error subset then spuriously fails (or + // wrongly passes) the enclosing-return check. Mirrors exprtype's + // N_CALL arm (scopelookupprefer at :3336). The N_DOT-callee leaf + // (callee.str above) still resolves bare-leaf, not via the module + // qualifier callee.lhs.str — that module-keyed fix rides task #51. + let s: *sym = scopelookupprefer(c.cur, c.curmod, nm); if (s == nil) { return nil; }; if (s.skind != skind.SK_FN) { return nil; }; if (s.decl == nil) { return nil; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index f20fcc75..204ffe1f 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -16307,14 +16307,13 @@ fn checkisas(c: *checker, n: *node) void = { fn exprtypeoftry(c: *checker, e: *node) *node = { if (e == nil) { return nil; }; if (e.kind == nkind.N_IDENT) { - // #11a/task #50: this bare-leaf lookup wants curmod preference - // (scopelookupprefer, the #53/#55 family) but the swap BROKE the - // 995 self-rebuild byte-id — in the bundled self-compile ww's - // scopelookupprefer diverges from cstage's resolution (the same - // curmod-in-bundle layer that blocked #5), flipping a try-operand - // stamp in the selfhost source. Held at plain scopelookup until #50 - // lands the bundle curmod fix; aligning here in isolation is unsound. - let s: *sym = scopelookup(c.cur, e.str); + // #11a: curmod preference (the #53/#55 family). A bare ident + // whose leaf also names a global in a later module otherwise + // binds the foreign decl's type, mis-typing the try operand and + // either spuriously rejecting valid `?` code or skipping the F8 + // reject. Mirrors exprtype's own N_IDENT arm (scopelookupprefer + // at :2897); cstage types the operand via cexpr with cur_mod. + let s: *sym = scopelookupprefer(c.cur, c.curmod, e.str); if (s == nil) { return nil; }; if (s.decl == nil) { return nil; }; return s.decl.lhs; @@ -16335,10 +16334,14 @@ fn exprtypeoftry(c: *checker, e: *node) *node = { // returns nil below) rides task #51 with the fn-ptr-callee desugar. if (callee.kind == nkind.N_DOT) { nm = callee.str; }; if (nm.len == 0) { return nil; }; - // #11a/task #50: curmod preference wanted here too, held at plain - // scopelookup — the prefer swap broke 995 byte-id (bundle curmod - // divergence, same as the N_IDENT arm above + #5/#50). - let s: *sym = scopelookup(c.cur, nm); + // #11a: curmod preference for the bare-leaf callee. A same-leaf + // `op()?` declared in a later module otherwise binds the foreign + // op's return type — its error subset then spuriously fails (or + // wrongly passes) the enclosing-return check. Mirrors exprtype's + // N_CALL arm (scopelookupprefer at :3336). The N_DOT-callee leaf + // (callee.str above) still resolves bare-leaf, not via the module + // qualifier callee.lhs.str — that module-keyed fix rides task #51. + let s: *sym = scopelookupprefer(c.cur, c.curmod, nm); if (s == nil) { return nil; }; if (s.skind != skind.SK_FN) { return nil; }; if (s.decl == nil) { return nil; }; diff --git a/test/wcc/989_trycallcollide_run.c b/test/wcc/989_trycallcollide_run.c new file mode 100644 index 00000000..dcb581b5 --- /dev/null +++ b/test/wcc/989_trycallcollide_run.c @@ -0,0 +1,180 @@ +/* + * 989_trycallcollide_run (#11a, c3) — exprtypeoftry must prefer the current + * module's symbol when typing a `?`/`!` operand whose bare leaf collides with + * a same-leaf decl in a later module. + * + * THE BUG (review item #11, 2026-06-11): exprtypeoftry (check.ww ~5850 N_IDENT, + * ~5874 N_CALL bare-leaf callee) typed the try operand with bare scopelookup — + * no (name,module) preference. scopedefineinmodule PREPENDS, so a later + * module's same-leaf decl heads the chain and the operand is typed against ITS + * signature. For `op()?` the wrong op's error subset is then checked against + * the enclosing fn's return: a spurious "?: error variant not in enclosing + * return" rejects valid code (or, with a narrower foreign subset, silently + * skips the F8 multi-success reject). cstage types the operand via cexpr with + * cur_mod. THE FIX: both arms use scopelookupprefer(c.cur, c.curmod, ...), + * mirroring exprtype's own N_IDENT/N_CALL arms (:2897/:3336). + * + * GATING NOTE: this swap broke 995_self_rebuild once in a CONTAMINATED earlier + * attempt (bundled with a u64-guard-buggy scopelookuptype). Re-probed clean + * atop c1 (i32-correct scopelookuptype): repro closes AND 990-997/995 green. + * + * row | shape | run (cs==ww) + * -----------+---------------------------------------------+------------- + * callcollide| beta.user() does `op()?`; gamma also has | 42 (was ww + * | `op` with a wider error subset | build-fail) + * noclash | same, gamma.op renamed (no collision) | 42 (control) + * + * The N_DOT-callee module-keyed resolution stays task #51 (out of scope). + * Single-file multi-package source is the sanctioned shape (cmd/ww/main.c). + */ +#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; }; + +static const struct row rows[] = { + { "callcollide", + "package beta;\n" + "export type berr = !void;\n" + "export fn op() (i32 | berr) = { return 5; };\n" + "export fn user() (i32 | berr) = {\n" + " let v: i32 = op()?;\n" + " return v + 37;\n" + "};\n" + "package gamma;\n" + "export type gerr1 = !void;\n" + "export type gerr2 = !u8;\n" + "export fn op() (i32 | gerr1 | gerr2) = { return 0; };\n" + "package main;\n" + "import beta;\n" + "import gamma;\n" + "fn main() int = {\n" + " match (beta.user()) {\n" + " case let n: i32 => return n: int;\n" + " case berr => return 99;\n" + " };\n" + "};\n", + 42 }, + + { "noclash", + "package beta;\n" + "export type berr = !void;\n" + "export fn op() (i32 | berr) = { return 5; };\n" + "export fn user() (i32 | berr) = {\n" + " let v: i32 = op()?;\n" + " return v + 37;\n" + "};\n" + "package gamma;\n" + "export type gerr1 = !void;\n" + "export fn other() (i32 | gerr1) = { return 0; };\n" + "package main;\n" + "import beta;\n" + "import gamma;\n" + "fn main() int = {\n" + " match (beta.user()) {\n" + " case let n: i32 => return n: int;\n" + " case berr => return 99;\n" + " };\n" + "};\n", + 42 }, +}; + +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/tcc_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/tcc_%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; /* -1 == build failed */ +} + +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 != rows[i].want) { + fprintf(stderr, "trycallcollide[cstage][%s]: got=%d want=%d\n", + rows[i].label, gc, rows[i].want); + fail++; + } + if (!have_ww) { + fprintf(stderr, "trycallcollide: skip wwstage (no %s)\n", wdrv); + continue; + } + int gw = run_build(wdrv, &rows[i], i); + if (gw != gc) { + fprintf(stderr, "trycallcollide[%s]: cs=%d != ww=%d " + "(exprtypeoftry cross-module try-operand misbind — #11a c3)\n", + rows[i].label, gc, gw); + fail++; + } + if (gw != rows[i].want) { + fprintf(stderr, "trycallcollide[wwstage][%s]: got=%d want=%d\n", + rows[i].label, gw, rows[i].want); + fail++; + } + } + + if (fail) { + fprintf(stderr, "trycallcollide_run: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("trycallcollide_run: %d/%d ok\n", total, total); + return 0; +}