diff --git a/Makefile b/Makefile index 027ed368..15fed889 100644 --- a/Makefile +++ b/Makefile @@ -219,6 +219,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_int_cast_signed $(BIN)/test_dot_chain \ $(BIN)/test_amp_dot $(BIN)/test_arr_elem_field \ $(BIN)/test_arr_elem_field_write \ + $(BIN)/test_dot_str_chained_arg \ $(BIN)/test_field_signed $(BIN)/test_frame_argcount \ $(BIN)/test_selfhost $(BIN)/test_w6a_ww $(BIN)/test_w6l_ww \ $(BIN)/test_w6c_ww $(BIN)/test_ww_ww $(BIN)/test_self_rebuild \ @@ -311,6 +312,12 @@ $(BIN)/test_arr_elem_field_write: test/wcc/681_arr_elem_field_write.c $(BIN)/ww $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_dot_str_chained_arg: test/wcc/692_dot_str_chained_arg.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_field_signed: test/wcc/660_field_signed.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 6ccf4c43..2d177d77 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -6063,6 +6063,26 @@ fn nodeisstr(c: *cgen, n: *node) bool = { }; }; }; + // Chained dot through value-struct hops (`p.inner.s`): + // dotinnerstructptr above only walks *struct fields, so + // a value-struct chain falls through and the call-arg + // path then pushes only 1 word for the str instead of + // 2 (ptr+len), silently dropping the len half. + // dotchainresolve handles arbitrary depth through value + // struct AND `*T` root, returning the leaf fieldinfo. + let rootnm: str = ""; + let rootoff: i32 = 0; + let totaloff: i32 = 0; + let lfi: *fieldinfo = nil; + let sdelta: i32 = -1; + let isglobal: bool = false; + let ptrroot: bool = false; + let ok: bool = dotchainresolve(c, n, + &rootnm, &rootoff, &totaloff, + &lfi, &sdelta, &isglobal, &ptrroot); + if (ok && sdelta < 0 && lfi != nil) { + return isstrtype(c, lfi.tnode); + }; }; return false; }; diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index 0d76b4dd..e485be7d 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -493,6 +493,26 @@ fn nodeisstr(c: *cgen, n: *node) bool = { }; }; }; + // Chained dot through value-struct hops (`p.inner.s`): + // dotinnerstructptr above only walks *struct fields, so + // a value-struct chain falls through and the call-arg + // path then pushes only 1 word for the str instead of + // 2 (ptr+len), silently dropping the len half. + // dotchainresolve handles arbitrary depth through value + // struct AND `*T` root, returning the leaf fieldinfo. + let rootnm: str = ""; + let rootoff: i32 = 0; + let totaloff: i32 = 0; + let lfi: *fieldinfo = nil; + let sdelta: i32 = -1; + let isglobal: bool = false; + let ptrroot: bool = false; + let ok: bool = dotchainresolve(c, n, + &rootnm, &rootoff, &totaloff, + &lfi, &sdelta, &isglobal, &ptrroot); + if (ok && sdelta < 0 && lfi != nil) { + return isstrtype(c, lfi.tnode); + }; }; return false; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index e736c250..6fa76813 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -6063,6 +6063,26 @@ fn nodeisstr(c: *cgen, n: *node) bool = { }; }; }; + // Chained dot through value-struct hops (`p.inner.s`): + // dotinnerstructptr above only walks *struct fields, so + // a value-struct chain falls through and the call-arg + // path then pushes only 1 word for the str instead of + // 2 (ptr+len), silently dropping the len half. + // dotchainresolve handles arbitrary depth through value + // struct AND `*T` root, returning the leaf fieldinfo. + let rootnm: str = ""; + let rootoff: i32 = 0; + let totaloff: i32 = 0; + let lfi: *fieldinfo = nil; + let sdelta: i32 = -1; + let isglobal: bool = false; + let ptrroot: bool = false; + let ok: bool = dotchainresolve(c, n, + &rootnm, &rootoff, &totaloff, + &lfi, &sdelta, &isglobal, &ptrroot); + if (ok && sdelta < 0 && lfi != nil) { + return isstrtype(c, lfi.tnode); + }; }; return false; }; diff --git a/test/wcc/692_dot_str_chained_arg.c b/test/wcc/692_dot_str_chained_arg.c new file mode 100644 index 00000000..ad882990 --- /dev/null +++ b/test/wcc/692_dot_str_chained_arg.c @@ -0,0 +1,180 @@ +/* + * 692_dot_str_chained_arg — pass `.strfield` as a call argument. + * + * Task #30: selfhost's `nodeisstr` surface check used `dotinnerstructptr` + * to resolve the base of a chained N_DOT, which only walked *struct + * fields — value-struct hops fell through. The call-arg path then + * pushed only 1 word (`.ptr`) instead of 2 (`.ptr`+`.len`), silently + * dropping the str length at the call site. + * + * Cstage was unaffected because its `node_isstr` reads `n->type` from + * the checker rather than re-walking the AST. The bug only bit when + * the wwstage was the driver — surfaced via task #29's parallel + * slice-field fix when a chained-str test row failed only on wwstage. + * + * Each fixture passes a str header through a call and reads `.len` + * on the callee side to prove both header words round-trip. Rows + * cover: + * - chained `*T`-rooted through value-struct hop (`p.inner.s`) + * — the failing shape filed as #30 + * - single dot through `*T` root (`p.s`) + * — pre-existing path; baseline / negative control + * - chained local-rooted value-struct (`o.inner.s`) + * — same surface gap as #30 but rooted at a local rather + * than a `*T` param + * + * Exercises both stages via `ww` (cstage) and `ww_ww` (wwstage). + */ +#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[] = { + /* Baseline: single dot through `*T` root. Pre-existing path — + * `nodeisstr` resolves the base local's `*T` tnode directly, + * no dot-chain walker involved. Pinned so a future refactor + * of either branch can't regress it silently. */ + { "ptr_root_str_arg", + "type outer = struct { s: str, x: i32 };\n" + "fn slen(s: str) i32 = { return s.len: i32; };\n" + "fn main() i32 = {\n" + " let o: outer; o.s = \"hello\";\n" + " let p: *outer = &o;\n" + " if (slen(p.s) != 5) { return 1; };\n" + " return 42;\n" + "};\n", + 42 }, + /* #30 repro: chained through `*T` root with a value-struct + * hop. Without the fix, the wwstage pushed only `.ptr` — + * the called function then read whatever was in BX from the + * prior expression as the length. */ + { "ptr_root_chained_str_arg", + "type inner = struct { s: str, pad: i32 };\n" + "type outer = struct { i: inner, tag: i32 };\n" + "fn slen(s: str) i32 = { return s.len: i32; };\n" + "fn main() i32 = {\n" + " let o: outer; o.i.s = \"abcdef\";\n" + " let p: *outer = &o;\n" + " if (slen(p.i.s) != 6) { return 1; };\n" + " return 42;\n" + "};\n", + 42 }, + /* Local value-struct rooted, chained value-struct hop — + * same surface gap as the #30 repro above, but rooted on a + * local rather than a `*T` param. dotchainresolve walks + * both shapes; both go through the same fallback in + * `nodeisstr`. */ + { "local_root_chained_str_arg", + "type inner = struct { s: str, pad: i32 };\n" + "type outer = struct { i: inner, tag: i32 };\n" + "fn slen(s: str) i32 = { return s.len: i32; };\n" + "fn main() i32 = {\n" + " let o: outer; o.i.s = \"qrstuvw\";\n" + " if (slen(o.i.s) != 7) { return 1; };\n" + " return 42;\n" + "};\n", + 42 }, +}; + +static int +run_driver(const char *driver, const struct row *r, int i) +{ + char src[64], tmpdir[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/wdsc_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/wdsc_%d_d_%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 && %s build %s", + tmpdir, driver, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: build via %s failed\n", + r->label, driver); + unlink(src); rmdir(tmpdir); + return -1; + } + + 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 = runwait(outbin); + + unlink(src); unlink(outbin); rmdir(tmpdir); + return got; +} + +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]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + char wdrv[1024]; + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + struct { const char *name; const char *path; int gated_on_existence; } + drivers[] = { + { "cstage", cdrv, 0 }, + { "wwstage", wdrv, 1 }, + { NULL, NULL, 0 }, + }; + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + for (int d = 0; drivers[d].name; d++) { + if (drivers[d].gated_on_existence + && access(drivers[d].path, X_OK) != 0) { + fprintf(stderr, "dot_str_chained_arg: skip %s (no %s)\n", + drivers[d].name, drivers[d].path); + continue; + } + for (int i = 0; i < n; i++) { + int got = run_driver(drivers[d].path, &rows[i], i); + total++; + if (got != rows[i].want) { + fprintf(stderr, + "dot_str_chained_arg[%s][%s]: exit=%d want=%d\n", + drivers[d].name, rows[i].label, + got, rows[i].want); + fail++; + } + } + } + if (fail) { + fprintf(stderr, + "dot_str_chained_arg: %d/%d fixtures failed\n", fail, total); + return 1; + } + printf("dot_str_chained_arg: %d/%d ok\n", total, total); + return 0; +}