cmd+selfhost+lib+test: directory-as-module enumeration in driver (#22)

Replace the cmd/ww + selfhost driver's file-walk import resolver
with true directory enumeration. `import encoding.utf8;` now finds
the lib/encoding/utf8/ directory and concatenates every *.ww file
in it (excluding *test.ww and the driver's *.combined.ww artifacts)
in byte-wise sorted order, instead of just finding the single
lib/encoding/utf8/utf8.ww file. Mirrors Hare's
hare/module/srcs.ha:183 _findsrcs minus tag handling.

Lookup order in both stages: (1) <dir>/<dot-as-slash>/ as directory
→ enumerate. (2) <dir>/<dot-as-slash>.ww as file. The legacy
<dir>/<name>/<name>.ww shape from #18's retained divergence is
dropped per rule-9 Hare-fidelity — Hare has no foo/foo.ha fallback;
a module IS the directory.

Symmetric across cstage (cmd/ww/main.c via opendir+qsort+stat) and
wwstage (selfhost/cmd/ww/main.ww via existing lib/os.getdents64 +
os.stat — no new lib/os surface needed; the rundirtests() walker
in main.ww from #18 was the model). Bootstrap ww2.s==ww3.s==ww4.s
byte-identical post-change.

Bundling justification (rule 11): strict-same-package validation is
bundled because the failure mode is dir-enum's own (a non-dir-enum
compilation unit cannot trigger mismatch across enumerated files).
The natural enforcement site is the driver — the parser can't
distinguish dir-enum concat from file-walk concat. Both stages
peek each file's first `package <name>;` line in expand_dir /
expanddir and exit(1) on mismatch with a precise error pointing
at the offending file. Hare's hare/module/srcs.ha:131 has the
same constraint via its README gate. Other half of #23 (strict
missing-package error tightening — 63 inline-source test wrappers
blocker) stays deferred per its filing.

Parser side (cmd/wcc/parse.c parseuse + lib/ww/parse/decl.ww
parseuse): n->str now carries only the LEAF identifier from a
dotted import. With the driver translating the full dotted path
to a directory walk, the checker only needs the package bareword
(last component) for the N_USE → decl disambiguation walk in
check.c's src_imports / decl_mod. Mirrors Hare's
`use encoding::utf8;` → `utf8::name` semantics
(ref/hare/hare/ast/import.ha:7).

Migration: lib/ww/sym.ww drops `import typ; import ast;`;
lib/ww/parse/parse.ww drops `import expr; import stmt; import
decl;`; lib/ww/lex/lex.ww drops `import tok;` — all sibling
imports auto-resolve via the new dir-enum when callers import the
package directory. lib/strings/, lib/encoding/utf8/utf8test.ww
migrate `import utf8;` → `import encoding.utf8;`. Makefile drops
-I lib/encoding/utf8 stopgap from wwdump_ww + w6c_ww. Seven test
wrappers (700_e2e, 966_strings_run, 970_fmt_run, 971_log_run,
972_fnmatch_run, 982_getopt_run, 990_selfhost) and 995_self_rebuild
drop the -I lib/encoding/utf8 runtime stopgap.

Tests: new 737_direnum C wrapper + test/wcc/data/direnum/ fixtures
pin (a) cross-pkg multi-file dir-enum build at runtime (both stages
must succeed) and (b) strict-same-package mismatch error (both
stages must surface "differs from" + exit non-zero). 738_module_decl
gains row 6 pinning the n_use->str leaf-only storage post-parser
change.

Retained workaround at selfhost/cmd/ww/main.ww expanddir loop:
`names[i][k]` nested-deref-then-index split into
`let nm: *u8 = names[i]; nm[k]` because wwstage cgen miscompiles
the chained form (treats inner u8 element as 8B sizeof *u8 instead
of 1B sizeof u8: extra MOVQ $8 + IMULQ on the inner index, MOVQ
instead of MOVZBQ load). Inline rule-8 WHY comment cites task #24
(wwstage cgen chained-index inner element size on **T). Two-step
form routes through the bare-pointer index path which both stages
handle byte-identically.

Class A wwstage cgen UNDER (chained-index inner element size on
**T) surfaced first time the codebase exercises the **T[i][k]
shape via enumeratedir() — corpus-coverage-blind landmine pattern,
same family as the trio (#27/#28/#31) from STATUS-5.

112/112 ok. ww2 == ww3 == ww4 byte-id holds.
This commit is contained in:
2026-05-18 19:22:27 +09:00
parent 79d9528a00
commit 9e0816e199
31 changed files with 2521 additions and 1564 deletions

View File

@@ -1748,14 +1748,8 @@ main(void)
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwe2e_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
/* -I lib/encoding/utf8 for any fixture that pulls in fmt /
* strconv / strings via `use` (transitive utf8.encoderune;
* task #17). Unused -I is benign for fixtures that don't. */
char cwd700[1024];
if (getcwd(cwd700, sizeof cwd700) == NULL) { fail++; continue; }
snprintf(cmd, sizeof cmd,
"cd %s && %s/ww build -I %s/lib/encoding/utf8 %s",
tmpdir, bin, cwd700, src);
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s",
tmpdir, bin, src);
if (runwait(cmd) != 0) { fail++; continue; }
char outbin[128];

142
test/wcc/737_direnum.c Normal file
View File

@@ -0,0 +1,142 @@
/*
* 737_direnum — driver-level sentinel for task #22 directory-as-
* module enumeration. Two row families pinning the contract:
*
* ok — multi-file dir is concatenated by both stages; the entry
* reads cross-pkg bare-leaf fns from sibling files. Build
* must succeed for both C-built `ww` and ww-built `ww_ww`.
* bad — multi-file dir with mismatched `package <name>;` decls
* triggers the strict-same-package error (task #25 subset
* bundled with #22 because the failure mode is dir-enum's
* own; cross-stage symmetric).
*
* Asm-presence isn't checked separately — 968_utf8_run, 966_strings_
* run, 995_self_rebuild already exercise dir-enum end-to-end at
* binary level. This file pins the cstage/wwstage symmetric error
* path so a regression on either driver fails loud.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
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
runbuild(const char *driver, const char *src, const char *outbin)
{
char cmd[2048];
snprintf(cmd, sizeof cmd, "%s build %s -o /dev/null >/dev/null 2>%s",
driver, src, outbin);
return runwait(cmd);
}
static int
stderr_contains(const char *path, const char *needle)
{
FILE *f = fopen(path, "rb");
if (!f) return 0;
char buf[4096];
size_t n = fread(buf, 1, sizeof buf - 1, f);
fclose(f);
buf[n] = '\0';
return strstr(buf, needle) != NULL;
}
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 ww[1280], ww_ww[1280];
snprintf(ww, sizeof ww, "%s/ww", bin);
snprintf(ww_ww, sizeof ww_ww, "%s/ww_ww", bin);
int total = 0, fail = 0;
char errp[64];
/* ok: cross-pkg bare-leaf via dir-enum, both stages. */
{
const char *src = "test/wcc/data/direnum/entry.ww";
char tmp[64];
snprintf(tmp, sizeof tmp, "/tmp/direnum_%d_ok", getpid());
char cmd[2048];
snprintf(cmd, sizeof cmd, "%s run %s >/dev/null 2>&1", ww, src);
total++;
if (runwait(cmd) != 0) {
fprintf(stderr, "737[ok-cstage]: ww run %s failed\n", src);
fail++;
}
if (access(ww_ww, X_OK) == 0) {
snprintf(cmd, sizeof cmd, "%s run %s >/dev/null 2>&1",
ww_ww, src);
total++;
if (runwait(cmd) != 0) {
fprintf(stderr, "737[ok-wwstage]: ww_ww run %s failed\n", src);
fail++;
}
}
(void)tmp;
}
/* bad: mismatched package decls in same dir → strict-same-package
* error. Both stages must surface "differs from" in stderr. */
{
const char *src = "test/wcc/data/direnum/bad_entry.ww";
snprintf(errp, sizeof errp, "/tmp/direnum_%d_bad.err", getpid());
char cmd[2048];
snprintf(cmd, sizeof cmd, "%s build %s 2>%s >/dev/null",
ww, src, errp);
int rc = runwait(cmd);
total++;
if (rc == 0) {
fprintf(stderr, "737[bad-cstage]: expected build failure, succeeded\n");
fail++;
} else if (!stderr_contains(errp, "differs from")) {
fprintf(stderr, "737[bad-cstage]: stderr missing 'differs from'\n");
fail++;
}
unlink(errp);
if (access(ww_ww, X_OK) == 0) {
snprintf(cmd, sizeof cmd, "%s build %s 2>%s >/dev/null",
ww_ww, src, errp);
rc = runwait(cmd);
total++;
if (rc == 0) {
fprintf(stderr, "737[bad-wwstage]: expected build failure, succeeded\n");
fail++;
} else if (!stderr_contains(errp, "differs from")) {
fprintf(stderr, "737[bad-wwstage]: stderr missing 'differs from'\n");
fail++;
}
unlink(errp);
}
}
if (fail) {
fprintf(stderr, "737_direnum: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("737_direnum: %d/%d ok\n", total, total);
return 0;
}

View File

@@ -98,6 +98,30 @@ main(void)
"package foo;\nimport encoding.utf8;\nfn x() void = {};\n")) pass++;
else { fprintf(stderr, "738[5] dotted import accept FAILED\n"); fail++; }
/* Row 6: dotted import stores only the leaf identifier on
* N_USE.str (post-task-#22 — the driver translates the full
* dotted path to a directory walk; the checker only needs the
* package bareword for n_use → decl disambiguation). */
{
Arena *a = newarena();
Lex l;
Parser p;
const char *src = "package foo;\nimport encoding.utf8;\n";
lexinit(&l, a, "<test>", src, strlen(src));
parserinit(&p, a, &l);
Node *n = parsefile(&p);
int ok = 0;
if (n != NULL && p.errs == 0 && l.errs == 0 && n->list != NULL) {
Node *u = n->list;
ok = (u->kind == N_USE
&& u->str != NULL
&& strcmp(u->str, "utf8") == 0);
}
freearena(a);
if (ok) pass++;
else { fprintf(stderr, "738[6] dotted import leaf-store FAILED\n"); fail++; }
}
printf("738_module_decl: %d pass, %d fail\n", pass, fail);
return fail == 0 ? 0 : 1;
}

View File

@@ -5,10 +5,6 @@
* Same thin-wrapper shape as 967_bytes_run / 968_utf8_run / 979_hex_run:
* stringstest.ww carries its own `export fn main()` that drives the
* @test fns and signals which case failed via the exit code.
*
* -I lib/encoding/utf8 is required because lib/strings.byteindex
* encodes the rune-needle arm via utf8.encoderune; the import resolver
* doesn't yet walk encoding/ subdirs (task #17).
*/
#include <stdio.h>
#include <stdlib.h>
@@ -42,9 +38,8 @@ main(void)
const char *src = "lib/strings/stringstest.ww";
char path[1024], cmd[2048];
snprintf(path, sizeof path, "%s/%s", cwd, src);
snprintf(cmd, sizeof cmd,
"%s/ww run -I %s/lib/encoding/utf8 %s",
bin, cwd, path);
snprintf(cmd, sizeof cmd, "%s/ww run %s", bin, path);
(void)cwd;
int rc = runwait(cmd);
if (rc != 0) {
fprintf(stderr, "strings_run FAIL: %s exited %d\n", src, rc);

View File

@@ -41,9 +41,8 @@ main(void)
const char *src = "lib/fmt/fmttest.ww";
char path[1024], cmd[2048];
snprintf(path, sizeof path, "%s/%s", cwd, src);
/* -I lib/encoding/utf8 — task #17 */
snprintf(cmd, sizeof cmd,
"%s/ww run -I %s/lib/encoding/utf8 %s", bin, cwd, path);
snprintf(cmd, sizeof cmd, "%s/ww run %s", bin, path);
(void)cwd;
int rc = runwait(cmd);
if (rc != 0) {
fprintf(stderr, "fmt_run FAIL: %s exited %d\n", src, rc);

View File

@@ -41,9 +41,8 @@ main(void)
const char *src = "lib/log/logtest.ww";
char path[1024], cmd[2048];
snprintf(path, sizeof path, "%s/%s", cwd, src);
/* -I lib/encoding/utf8 — task #17 */
snprintf(cmd, sizeof cmd,
"%s/ww run -I %s/lib/encoding/utf8 %s", bin, cwd, path);
snprintf(cmd, sizeof cmd, "%s/ww run %s", bin, path);
(void)cwd;
int rc = runwait(cmd);
if (rc != 0) {
fprintf(stderr, "log_run FAIL: %s exited %d\n", src, rc);

View File

@@ -41,9 +41,8 @@ main(void)
const char *src = "lib/fnmatch/fnmatchtest.ww";
char path[1024], cmd[2048];
snprintf(path, sizeof path, "%s/%s", cwd, src);
/* -I lib/encoding/utf8 — task #17 */
snprintf(cmd, sizeof cmd,
"%s/ww run -I %s/lib/encoding/utf8 %s", bin, cwd, path);
snprintf(cmd, sizeof cmd, "%s/ww run %s", bin, path);
(void)cwd;
int rc = runwait(cmd);
if (rc != 0) {
fprintf(stderr, "fnmatch_run FAIL: %s exited %d\n", src, rc);

View File

@@ -40,10 +40,8 @@ main(void)
const char *src = "lib/getopt/getopttest.ww";
char path[1024], cmd[2048];
snprintf(path, sizeof path, "%s/%s", cwd, src);
/* -I lib/encoding/utf8: getopt -> strings -> utf8.encoderune
* (task #17 — resolver doesn't yet walk encoding/ subdirs). */
snprintf(cmd, sizeof cmd,
"%s/ww run -I %s/lib/encoding/utf8 %s", bin, cwd, path);
snprintf(cmd, sizeof cmd, "%s/ww run %s", bin, path);
(void)cwd;
int rc = runwait(cmd);
if (rc != 0) {
fprintf(stderr, "getopt_run FAIL: %s exited %d\n", src, rc);

View File

@@ -99,11 +99,9 @@ probe_smoke(const char *bin)
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwsh_%d", getpid());
mkdir(tmpdir, 0755);
char cmd[2048];
/* -I lib/encoding/utf8: smoke.ww uses strconv -> strings ->
* utf8.encoderune (task #17 — resolver doesn't walk encoding/). */
snprintf(cmd, sizeof cmd,
"cd %s && %s/ww build -I %s/lib/encoding/utf8 %s/selfhost/test/smoke.ww >/dev/null 2>&1",
tmpdir, bin, cwd, cwd);
"cd %s && %s/ww build %s/selfhost/test/smoke.ww >/dev/null 2>&1",
tmpdir, bin, cwd);
if (runwait(cmd) != 0) {
fprintf(stderr, "smoke FAIL: ww build did not succeed\n");
return -1;
@@ -745,8 +743,8 @@ probe_ww_links(const char *bin)
runwait(cmd);
/* ww build to get the .combined.ww as a side effect. */
snprintf(cmd, sizeof cmd,
"cd %s && %s/ww build -I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse -I %s/lib/encoding/utf8 -I %s/selfhost/cmd/wcc %s >/dev/null 2>&1",
tmpdir, bin, cwd, cwd, cwd, cwd, cwd, tmpsrc);
"cd %s && %s/ww build -I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse -I %s/selfhost/cmd/wcc %s >/dev/null 2>&1",
tmpdir, bin, cwd, cwd, cwd, cwd, tmpsrc);
if (runwait(cmd) != 0) {
fprintf(stderr, "ww-links FAIL: ww build %s\n", fix);
fail++;

View File

@@ -62,9 +62,8 @@ slurp_eq(const char *a, const char *b)
/* Each tool builds via `ww_ww build -I <local> -I lib/ww -I selfhost/cmd/wcc src`.
* lib/ww holds the language introspection (lex/tok/ast/parse/typ/sym);
* selfhost/cmd/wcc holds the compiler internals (mem/check/cgen*).
* lib/encoding/utf8 carries the rune codec strings.byteindex needs;
* the import resolver doesn't yet walk encoding/ subdirs (task #17),
* so the dep travels as an explicit -I until it does.
* Dotted `import encoding.utf8;` finds lib/encoding/utf8/ via the
* driver's default srclib path post-task-#22 dir-enum.
* Some tools have a local module dir (w6a, w6l with sibling .ww files).
* inc_local is "" for tools without one (w6c, ww, wwdump).
*/
@@ -80,14 +79,14 @@ rebuild_one(const char *bin, const char *cwd, const char *tool,
if (inc_local && inc_local[0]) {
snprintf(cmd, sizeof cmd,
"cd %s && %s/ww_ww build -I %s/%s -I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse -I %s/lib/encoding/utf8 -I %s/selfhost/cmd/wcc "
"cd %s && %s/ww_ww build -I %s/%s -I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse -I %s/selfhost/cmd/wcc "
"%s/%s >/dev/null 2>&1",
workdir, bin, cwd, inc_local, cwd, cwd, cwd, cwd, cwd, cwd, src_rel);
workdir, bin, cwd, inc_local, cwd, cwd, cwd, cwd, cwd, src_rel);
} else {
snprintf(cmd, sizeof cmd,
"cd %s && %s/ww_ww build -I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse -I %s/lib/encoding/utf8 -I %s/selfhost/cmd/wcc "
"cd %s && %s/ww_ww build -I %s/lib/ww -I %s/lib/ww/lex -I %s/lib/ww/parse -I %s/selfhost/cmd/wcc "
"%s/%s >/dev/null 2>&1",
workdir, bin, cwd, cwd, cwd, cwd, cwd, cwd, src_rel);
workdir, bin, cwd, cwd, cwd, cwd, cwd, src_rel);
}
if (runwait(cmd) != 0) {
fprintf(stderr, "self-rebuild FAIL: ww_ww build errored on %s\n", tool);

View File

@@ -0,0 +1,5 @@
package main;
import bad_pkg;
export fn main() i32 = { return 0; };

View File

@@ -0,0 +1,3 @@
package good;
fn aaa() i32 = { return 1; };

View File

@@ -0,0 +1,3 @@
package bad;
fn bbb() i32 = { return 2; };

View File

@@ -0,0 +1,10 @@
// Entry imports the dir; cross-pkg refs `ok.fromA` / `ok.fromB`
// resolve via dir-enum pulling a.ww + b.ww (both package ok).
package main;
import ok;
export fn main() i32 = {
if (ok.fromA() + ok.fromB() != 42) { return 1; };
return 0;
};

View File

@@ -0,0 +1,3 @@
package ok;
export fn fromA() i32 = { return 7; };

View File

@@ -0,0 +1,3 @@
package ok;
export fn fromB() i32 = { return 35; };