Files
ww/test/wcc/940_str_forrange_loopvar_run.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

165 lines
5.0 KiB
C

/*
* 940_str_forrange_loopvar_run — corpus coverage for the step-3 Fold 1
* fold: ranging a `str` value-form (`for (let b .. s)`) and reading the
* loop var `b` back must narrow to MOVZBQ (u8 zero-extend), matching
* cstage. Pre-fold the wwstage cgen registered the loop var with a nil
* tnode (str had no element node the way []u8 does), so the read-back
* fell through localloadop to a wide MOVQ; cstage's checker stamps the
* binding u8, so it narrowed. The fold synthesises the u8 element off
* str.sub (Phase 2 F1) so the loop var carries a u8 tnode and the
* existing narrow-load logic fires on its own.
*
* REAL GATE IS SHAPE BYTE-ID, NOT RUNTIME. The MOVQ-vs-MOVZBQ
* divergence is runtime-benign: the slot is always written by a
* MOVZBQ-into-AX then MOVQ-AX-into-slot, so the upper bytes are already
* zero — a MOVQ read-back yields the same value a MOVZBQ would. A
* runtime probe therefore passes on BOTH the buggy and the fixed code
* and CANNOT distinguish them. The load-bearing gate is diffing the
* cstage (w6c) and wwstage (w6c_ww) .s at the loop-var read:
* pre-fold: cstage MOVZBQ vs wwstage MOVQ (1-line diff)
* post-fold: byte-identical
* This fixture exists for corpus presence — it confirms the path lowers
* and runs correctly through both drivers; it does not by itself prove
* the narrow.
*
* Loop-var read-back is exercised two ways the fold touches:
* - Site A: pass `b` to a fn (`use1(b)` → cgident read into an arg).
* - Site B: use `b` in an arithmetic expression (`sum += b: i32`).
*/
#include <stdio.h>
#include <stdlib.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; int want; };
static const struct row rows[] = {
/* Site A — read-back into a fn arg. "AB" is 0x41,0x42; the callee
* checks each byte zero-extends to its exact value, so a botched
* wide read (had the slot held garbage) would mismatch. */
{ "sitea_arg_readback",
"fn check(x: u8) i32 = {\n"
" if (x: i32 < 65) { return 1; };\n"
" if (x: i32 > 66) { return 1; };\n"
" return 0;\n"
"};\n"
"export fn main() i32 = {\n"
" let s: str = \"AB\";\n"
" for (let b .. s) {\n"
" if (check(b) != 0) { return 1; };\n"
" };\n"
" return 0;\n"
"};\n",
0 },
/* Site B — read-back in an arithmetic expression. Sum the bytes of
* "AB" (0x41 + 0x42 = 131) and confirm. */
{ "siteb_expr_readback",
"export fn main() i32 = {\n"
" let s: str = \"AB\";\n"
" let sum: i32 = 0;\n"
" for (let b .. s) {\n"
" sum += b: i32;\n"
" };\n"
" if (sum != 131) { return 1; };\n"
" return 0;\n"
"};\n",
0 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[96], src[160], outbin[160], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/strforrange_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/strforrange_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/strforrange_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return -1; }
wwtest_fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s", driver, outbin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
runwait(rmcmd);
return -1;
}
int got = runwait(outbin);
runwait(rmcmd);
return got;
}
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 cdrv[640];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
char wdrv[640];
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,
"str_forrange_loopvar_run: 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,
"str_forrange_loopvar_run[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "str_forrange_loopvar_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("str_forrange_loopvar_run: %d/%d ok\n", total, total);
return 0;
}