diff --git a/Makefile b/Makefile index 4a1d1a28..01d68dea 100644 --- a/Makefile +++ b/Makefile @@ -274,6 +274,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_chained_index \ $(BIN)/test_chained_write \ $(BIN)/test_dotbase_chained \ + $(BIN)/test_parse_error \ $(BIN)/test_fnparams_bare_leaf_shadow \ $(BIN)/test_fnret_bare_leaf_shadow \ $(BIN)/test_param_shadow_mod \ @@ -659,6 +660,10 @@ $(BIN)/test_dotbase_chained: test/wcc/741_dotbase_chained.c \ $(BIN)/w6c $(BIN)/w6c_ww | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_parse_error: test/wcc/742_parse_error.c \ + $(BIN)/w6c $(BIN)/w6c_ww | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_fnparams_bare_leaf_shadow: test/wcc/732_fnparams_bare_leaf_shadow.c \ $(BIN)/w6c $(BIN)/w6c_ww | $(BIN) $(CC) $(CFLAGS) -o $@ $< diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 3b9229f5..e5cf6590 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -20949,12 +20949,14 @@ export fn main(argc: i32, argv: **u8) i32 = { let ps: parser; parserinit(&ps, ar, &l); let f: *node = parsefile(&ps); + // Gate cgen on parse-stage errors. Mirrors cmd/w6c/main.c's + // `if (l.errs || p.errs) return 1;` — broken AST otherwise reaches + // cgen and emits junk asm with a zero exit (silent miscompile). + if (l.errs > 0 || ps.errs > 0) { return 1; }; let cg: cgen; cgeninit(&cg, ar); cgfile(&cg, f); - - if (l.errs > 0) { return 1; }; return 0; }; diff --git a/selfhost/cmd/w6c/main.ww b/selfhost/cmd/w6c/main.ww index 7ee04c5f..cae8312f 100644 --- a/selfhost/cmd/w6c/main.ww +++ b/selfhost/cmd/w6c/main.ww @@ -142,11 +142,13 @@ export fn main(argc: i32, argv: **u8) i32 = { let ps: parser; parserinit(&ps, ar, &l); let f: *node = parsefile(&ps); + // Gate cgen on parse-stage errors. Mirrors cmd/w6c/main.c's + // `if (l.errs || p.errs) return 1;` — broken AST otherwise reaches + // cgen and emits junk asm with a zero exit (silent miscompile). + if (l.errs > 0 || ps.errs > 0) { return 1; }; let cg: cgen; cgeninit(&cg, ar); cgfile(&cg, f); - - if (l.errs > 0) { return 1; }; return 0; }; diff --git a/test/wcc/742_parse_error.c b/test/wcc/742_parse_error.c new file mode 100644 index 00000000..2427ee38 --- /dev/null +++ b/test/wcc/742_parse_error.c @@ -0,0 +1,96 @@ +/* + * 742_parse_error — both stages of w6c must exit non-zero on a parse + * error. Pins rule 10 symmetry on the failure path. + * + * Predecessor symptom: wwstage `w6c_ww` ran cgen on the broken AST and + * exited 0 (stderr noise only). Drivers downstream of w6c (`ww build`, + * make rules) saw no signal and proceeded. + * + * cstage `w6c` checks `l.errs || p.errs` before cgen and returns 1. + * This test pins the same exit-code contract on both binaries against a + * known-bad fixture. + */ +#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 +write_file(const char *path, const char *content) +{ + FILE *f = fopen(path, "wb"); + if (!f) return -1; + fputs(content, f); + fclose(f); + return 0; +} + +static int +check_fail(const char *tool, const char *src) +{ + char cmd[4096]; + snprintf(cmd, sizeof cmd, "%s %s >/dev/null 2>/dev/null", tool, src); + int rc = runwait(cmd); + if (rc == 0) { + fprintf(stderr, "742 FAIL: %s exited 0 on parse-error fixture\n", + tool); + return -1; + } + return 0; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[2048]; + 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 src[64], outs[64]; + snprintf(src, sizeof src, "/tmp/parse_err_%d.ww", getpid()); + snprintf(outs, sizeof outs, "/tmp/parse_err_%d.s", getpid()); + if (write_file(src, "fn fn fn ;\n") != 0) { + fprintf(stderr, "742: cannot write fixture\n"); + return 1; + } + + char w6c[2240], w6c_ww[2240], w6c_ww_bin[2240]; + snprintf(w6c, sizeof w6c, "%s/w6c -o %s", bin, outs); + snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww -o %s", bin, outs); + snprintf(w6c_ww_bin, sizeof w6c_ww_bin, "%s/w6c_ww", bin); + + int total = 0, fail = 0; + + total++; + if (check_fail(w6c, src) != 0) fail++; + + if (access(w6c_ww_bin, X_OK) == 0) { + total++; + if (check_fail(w6c_ww, src) != 0) fail++; + } + + unlink(src); + unlink(outs); + + if (fail) { + fprintf(stderr, "742_parse_error: %d/%d failed\n", fail, total); + return 1; + } + printf("742_parse_error: %d/%d ok\n", total, total); + return 0; +}