Files
ww/test/wcc/725_nested_if_labels.c
Hojun-Cho 7b9488706b parse: enforce strict-package — reject package-less files (#24a)
Flip the soft-default to a hard "missing package clause" error symmetrically in
both stages (cmd/wcc/parse.c + lib/ww/syntax/parse.ww): the first real decl of a
primary section with empty pathmod/resetmod and no seen clause is now rejected.
Closes the documented soft-default divergence (the 63-wrapper carve-out).

The gate flip can't be split from the migration it breaks, so this is one atomic
commit: ~80 test/wcc wrappers gain `package main;` via a shared wwtestpkg.h
helper, 6 data fixtures plus 17 asm-grep assertions update for the bare->main.<leaf>
root-helper mangle shift, and rt/ declares `package rt;` with @symbol pinning the
bare rt_ensure/rt_malloc linker names.

Root mangling narrows: the executable entry `main` stays bare (existing
carve-out), but root helper symbols become main.X. The #84 cluster is rewritten
to assert main.run distinct from aa.run/test.run; its cgen fix and bare machinery
are retained — still load-bearing for package-less module-reset deps. New
table-driven test 782_strict_package.c (6 rows, both stages).

Retiring //ww:module-reset is deferred to #24b: it is load-bearing (clears the
.wwi pathmod so the body's package clause asserts), not a vestige; fusing its
removal here would be a silent mismatch.

All byte-id gates green; full make test reports "all 335 tests passed".
2026-06-29 03:55:26 +09:00

157 lines
4.6 KiB
C

/*
* 725_nested_if_labels — sentinel for STATUS-3 #15. Cstage's
* `cgen.c`-side per-function ≤24B-struct return path called
* `mklabel(c, "retscr")`, consuming one `labelseq` counter slot per
* function that returns a struct. Wwstage's equivalent emits a
* fixed-name `@retscr` SSoT (mirrors `c.retscroff` in
* selfhost/cmd/wcc/cgen.ww). Net effect pre-fix: every subsequent
* control-flow label (`ct_N`, `ce_N`, `else_N`, `end_N`) in any such
* function was 1 ahead in cstage.
*
* Filed STATUS-3 #15. Previously byte-id drift only; 995_self_rebuild
* masked because the bootstrap main.combined.ww's drift was within
* the threshold the test happens to allow. lib/strings (commit 1) +
* lib/time.add nested-if shapes compounded the skew past that
* threshold once worker-strings-v2 attempted the landing — promotes
* #15 from latent to bootstrap-blocking.
*
* Fix aligns cstage DOWN to wwstage's single-slot SSoT (rule 10):
* cgen.c's two `mklabel(c, "retscr")` sites now use the fixed string
* "@retscr" passed to local_alloc. Labels never appear in emitted
* asm — only the labelseq side-effect mattered, and dropping it
* lets cstage's label numbering match wwstage's.
*
* Row pins asm `cmp -s` byte-id between stages on the canonical
* time.add shape: ≤24B struct return + nested if/if/fallthrough.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "wwtestpkg.h"
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; };
static const struct row rows[] = {
/* Plain struct return + nested-if (time.add shape — struct
* `instant` return, multi-arm if/if/fallthrough). Each return
* site hits cgreturn's ≤24B-struct arm which allocates
* @retscr; pre-fix mklabel(c, "retscr") bumped labelseq, post-
* fix the fixed name leaves labelseq alone so cstage's
* ct/ce/end numbering matches wwstage. */
{ "struct_return_then_nested_if",
"type instant = struct { sec: i64, nsec: i64 };\n"
"export fn add(i: instant, x: i64) instant = {\n"
" let r: instant;\n"
" if (x == 0i64) {\n"
" r.sec = i.sec; r.nsec = i.nsec;\n"
" return r;\n"
" };\n"
" if (x > 0i64) {\n"
" r.sec = i.sec + x; r.nsec = i.nsec;\n"
" return r;\n"
" };\n"
" r.sec = i.sec - 1i64; r.nsec = i.nsec + x;\n"
" return r;\n"
"};\n" },
};
static int
emit_s(const char *w6c, const struct row *r, int i, char *out_s, size_t cap)
{
char src[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/nil_asm_%d_%d.ww", getpid(), i);
snprintf(out_s, cap, "/tmp/nil_asm_%d_%d_%s.s",
getpid(), i, w6c[strlen(w6c) - 1] == 'w' ? "ww" : "c");
FILE *f = fopen(src, "wb");
if (!f) return -1;
wwtest_fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, out_s, src);
int rc = runwait(cmd);
unlink(src);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[512];
if (bin[0] != '/') {
char cwd[256];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char w6c[640], w6c_ww[640];
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
int have_ww = (access(w6c_ww, X_OK) == 0);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
char cs_path[128], ws_path[128];
if (emit_s(w6c, &rows[i], i, cs_path, sizeof cs_path) != 0) {
fprintf(stderr,
"nested_if_labels[cstage][%s]: w6c failed\n",
rows[i].label);
fail++; total++; continue;
}
total++;
if (!have_ww) { unlink(cs_path); continue; }
if (emit_s(w6c_ww, &rows[i], i, ws_path, sizeof ws_path) != 0) {
fprintf(stderr,
"nested_if_labels[wwstage][%s]: w6c_ww failed\n",
rows[i].label);
fail++; total++;
unlink(cs_path);
continue;
}
total++;
/* Byte-id between stages — the fix-pin. Pre-fix cstage's
* `add_ct_N` was 1 ahead of wwstage's; post-fix they match. */
total++;
char cmd[512];
snprintf(cmd, sizeof cmd, "cmp -s %s %s", cs_path, ws_path);
if (runwait(cmd) != 0) {
fprintf(stderr,
"nested_if_labels[%s]: cstage vs wwstage asm differs\n",
rows[i].label);
fail++;
}
unlink(cs_path); unlink(ws_path);
}
if (fail) {
fprintf(stderr,
"nested_if_labels: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("nested_if_labels: %d/%d ok\n", total, total);
return 0;
}