selfhost/cmd/wcc/cgenutil+test: slotsize zero for void, recurse N_TBANG

Wwstage's slotsize had a catch-all `return 8` for any N_TNAME where
primsize's `> 0` guard failed. `primsize("void") == 0` (correct —
void is zero-sized per cmd/wcc/type.c:46), so void landed on the
catch-all. (void | !void) then sized as `8 (tag) + max(8, 8) = 16`
instead of `8 + 0 = 8`, and the phantom payload word made
cgwidentaggedstore spill DX for the let-init — diverging from
cstage's `8`-byte slot.

Two narrow additions per rule 10 (align wwstage DOWN to cstage):
1. N_TBANG case at the top of slotsize, recurse on .lhs. Mirrors
   cstage resolve_type N_TBANG which copies the underlying type's
   size unchanged.
2. `void => 0` in N_TNAME BEFORE the primsize guard, so the SSoT
   matches cmd/wcc/type.c:46.

757_letbind_void_bang_void exercises three shapes — void-arm,
invalid-arm, full natural-form fromutf8 — and pins cstage/wwstage
asm byte-identity per row.

lib/strings/strings.ww fromutf8 WHY-comment drops the Bug-B
SIGSEGV caveat (measurement artifact: original test linked without
rt/start.s; RET popped argc). Keeps #19 dependency for the
eventual collapse to `utf8.validate(in)?`.

Hare matches ww's design (void zero-sized, !T inherits T's
layout); this is a pure wwstage implementation gap, not a
divergence to argue about.
This commit is contained in:
2026-05-20 02:27:17 +09:00
parent 6d006da26c
commit f8770d1502
7 changed files with 334 additions and 40 deletions

View File

@@ -291,6 +291,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_slice_of_slice_index \
$(BIN)/test_amp_dot_idx \
$(BIN)/test_alias_chain_unwrap \
$(BIN)/test_letbind_void_bang_void \
$(BIN)/test_param_shadow_mod \
$(BIN)/test_localoff_scope \
$(BIN)/test_cast_enum_movl \
@@ -762,6 +763,12 @@ $(BIN)/test_alias_chain_unwrap: test/wcc/756_alias_chain_unwrap.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_letbind_void_bang_void: test/wcc/757_letbind_void_bang_void.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_match_4arm_cross_module_run: test/wcc/929_match_4arm_cross_module_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \

View File

@@ -53,16 +53,12 @@ export fn fromutf8_unsafe(in: []u8) str = {
// malformed UTF-8 sequence. ref/hare/strings/utf8.ha:22 (#7).
//
// Hare's spelling is `utf8::validate(in)?; return fromutf8_unsafe(in)`.
// ww calls `utf8.validate` and matches its `(void | utf8.invalid)`
// result instead of `?` (cross-shape propagation is #19). Even with
// match, the call diverges cstage/wwstage when the result is bound to
// a local (#48: wwstage spills DX for the payload-less arm) and the
// success path then SIGSEGVs lifting `str` to `(str | utf8.invalid)`.
// So the validation is open-coded against the decoder — the same
// byte-by-byte DFA walk `utf8.validate` performs (ref/hare/encoding/
// utf8/decode.ha:207) — whose `(rune | done | more | invalid)` arm
// matches the failure axis of fromutf8 and lifts cleanly. Collapses
// to `utf8.validate(in)?` once #19 + #48 are fixed.
// ww open-codes the same byte-by-byte DFA walk (ref/hare/encoding/utf8/
// decode.ha:207) because the cross-shape `(void | invalid) →
// (str | invalid)` propagation `?` needs is #19. The decoder-walk
// form's `(rune | done | more | invalid)` arm matches the failure
// axis of fromutf8 and lifts cleanly. Collapses to `utf8.validate(in)?`
// once #19 lands.
export fn fromutf8(in: []u8) (str | utf8.invalid) = {
let d: utf8.decoder = utf8.decode(in);
for (true) {

View File

@@ -1890,16 +1890,12 @@ export fn fromutf8_unsafe(in: []u8) str = {
// malformed UTF-8 sequence. ref/hare/strings/utf8.ha:22 (#7).
//
// Hare's spelling is `utf8::validate(in)?; return fromutf8_unsafe(in)`.
// ww calls `utf8.validate` and matches its `(void | utf8.invalid)`
// result instead of `?` (cross-shape propagation is #19). Even with
// match, the call diverges cstage/wwstage when the result is bound to
// a local (#48: wwstage spills DX for the payload-less arm) and the
// success path then SIGSEGVs lifting `str` to `(str | utf8.invalid)`.
// So the validation is open-coded against the decoder — the same
// byte-by-byte DFA walk `utf8.validate` performs (ref/hare/encoding/
// utf8/decode.ha:207) — whose `(rune | done | more | invalid)` arm
// matches the failure axis of fromutf8 and lifts cleanly. Collapses
// to `utf8.validate(in)?` once #19 + #48 are fixed.
// ww open-codes the same byte-by-byte DFA walk (ref/hare/encoding/utf8/
// decode.ha:207) because the cross-shape `(void | invalid) →
// (str | invalid)` propagation `?` needs is #19. The decoder-walk
// form's `(rune | done | more | invalid)` arm matches the failure
// axis of fromutf8 and lifts cleanly. Collapses to `utf8.validate(in)?`
// once #19 lands.
export fn fromutf8(in: []u8) (str | utf8.invalid) = {
let d: utf8.decoder = utf8.decode(in);
for (true) {
@@ -10145,6 +10141,12 @@ export fn letslotsize(c: *cgen, n: *node) i32 = {
fn slotsize(c: *cgen, typn: *node) i32 = {
if (typn == nil) { return 8; };
let k: nkind = typn.kind;
// `!T` carries T's memory layout; the error-tag bit lives in the
// enclosing union's discriminant, not the variant payload. cstage
// resolve_type N_TBANG copies the inner size (cmd/wcc/check.c:303-
// 306); wwstage previously fell through to the catch-all 8, so a
// bare `!void` variant sized to 8 instead of 0. (#48 part-A.)
if (k == nkind.N_TBANG) { return slotsize(c, typn.lhs); };
if (k == nkind.N_TPTR) { return 8; };
if (k == nkind.N_TFN) { return 8; };
if (k == nkind.N_TCHAN) { return 8; };
@@ -10181,6 +10183,13 @@ fn slotsize(c: *cgen, typn: *node) i32 = {
};
if (k == nkind.N_TNAME) {
let nm: str = typn.str;
// `void` is zero-sized per Hare design; cstage's ty_void.size = 0
// at cmd/wcc/type.c:46. Pre-#48 wwstage fell through to the
// catch-all 8, so (void | !void) sized as tag + 8 = 16 and let-
// init through cgwidentaggedstore emitted a phantom DX spill at
// slot+8 picking up the callee's stale-DX. Bypassing primsize's
// `> 0` guard keeps the rest of the prim-pad-to-8 contract intact.
if (streq(nm, "void")) { return 0; };
if (streq(nm, "str")) { return 16; };
let ps: i32 = primsize(nm);
if (ps > 0) {

View File

@@ -1943,6 +1943,12 @@ export fn letslotsize(c: *cgen, n: *node) i32 = {
fn slotsize(c: *cgen, typn: *node) i32 = {
if (typn == nil) { return 8; };
let k: nkind = typn.kind;
// `!T` carries T's memory layout; the error-tag bit lives in the
// enclosing union's discriminant, not the variant payload. cstage
// resolve_type N_TBANG copies the inner size (cmd/wcc/check.c:303-
// 306); wwstage previously fell through to the catch-all 8, so a
// bare `!void` variant sized to 8 instead of 0. (#48 part-A.)
if (k == nkind.N_TBANG) { return slotsize(c, typn.lhs); };
if (k == nkind.N_TPTR) { return 8; };
if (k == nkind.N_TFN) { return 8; };
if (k == nkind.N_TCHAN) { return 8; };
@@ -1979,6 +1985,13 @@ fn slotsize(c: *cgen, typn: *node) i32 = {
};
if (k == nkind.N_TNAME) {
let nm: str = typn.str;
// `void` is zero-sized per Hare design; cstage's ty_void.size = 0
// at cmd/wcc/type.c:46. Pre-#48 wwstage fell through to the
// catch-all 8, so (void | !void) sized as tag + 8 = 16 and let-
// init through cgwidentaggedstore emitted a phantom DX spill at
// slot+8 picking up the callee's stale-DX. Bypassing primsize's
// `> 0` guard keeps the rest of the prim-pad-to-8 contract intact.
if (streq(nm, "void")) { return 0; };
if (streq(nm, "str")) { return 16; };
let ps: i32 = primsize(nm);
if (ps > 0) {

View File

@@ -1890,16 +1890,12 @@ export fn fromutf8_unsafe(in: []u8) str = {
// malformed UTF-8 sequence. ref/hare/strings/utf8.ha:22 (#7).
//
// Hare's spelling is `utf8::validate(in)?; return fromutf8_unsafe(in)`.
// ww calls `utf8.validate` and matches its `(void | utf8.invalid)`
// result instead of `?` (cross-shape propagation is #19). Even with
// match, the call diverges cstage/wwstage when the result is bound to
// a local (#48: wwstage spills DX for the payload-less arm) and the
// success path then SIGSEGVs lifting `str` to `(str | utf8.invalid)`.
// So the validation is open-coded against the decoder — the same
// byte-by-byte DFA walk `utf8.validate` performs (ref/hare/encoding/
// utf8/decode.ha:207) — whose `(rune | done | more | invalid)` arm
// matches the failure axis of fromutf8 and lifts cleanly. Collapses
// to `utf8.validate(in)?` once #19 + #48 are fixed.
// ww open-codes the same byte-by-byte DFA walk (ref/hare/encoding/utf8/
// decode.ha:207) because the cross-shape `(void | invalid) →
// (str | invalid)` propagation `?` needs is #19. The decoder-walk
// form's `(rune | done | more | invalid)` arm matches the failure
// axis of fromutf8 and lifts cleanly. Collapses to `utf8.validate(in)?`
// once #19 lands.
export fn fromutf8(in: []u8) (str | utf8.invalid) = {
let d: utf8.decoder = utf8.decode(in);
for (true) {
@@ -10145,6 +10141,12 @@ export fn letslotsize(c: *cgen, n: *node) i32 = {
fn slotsize(c: *cgen, typn: *node) i32 = {
if (typn == nil) { return 8; };
let k: nkind = typn.kind;
// `!T` carries T's memory layout; the error-tag bit lives in the
// enclosing union's discriminant, not the variant payload. cstage
// resolve_type N_TBANG copies the inner size (cmd/wcc/check.c:303-
// 306); wwstage previously fell through to the catch-all 8, so a
// bare `!void` variant sized to 8 instead of 0. (#48 part-A.)
if (k == nkind.N_TBANG) { return slotsize(c, typn.lhs); };
if (k == nkind.N_TPTR) { return 8; };
if (k == nkind.N_TFN) { return 8; };
if (k == nkind.N_TCHAN) { return 8; };
@@ -10181,6 +10183,13 @@ fn slotsize(c: *cgen, typn: *node) i32 = {
};
if (k == nkind.N_TNAME) {
let nm: str = typn.str;
// `void` is zero-sized per Hare design; cstage's ty_void.size = 0
// at cmd/wcc/type.c:46. Pre-#48 wwstage fell through to the
// catch-all 8, so (void | !void) sized as tag + 8 = 16 and let-
// init through cgwidentaggedstore emitted a phantom DX spill at
// slot+8 picking up the callee's stale-DX. Bypassing primsize's
// `> 0` guard keeps the rest of the prim-pad-to-8 contract intact.
if (streq(nm, "void")) { return 0; };
if (streq(nm, "str")) { return 16; };
let ps: i32 = primsize(nm);
if (ps > 0) {

View File

@@ -1781,16 +1781,12 @@ export fn fromutf8_unsafe(in: []u8) str = {
// malformed UTF-8 sequence. ref/hare/strings/utf8.ha:22 (#7).
//
// Hare's spelling is `utf8::validate(in)?; return fromutf8_unsafe(in)`.
// ww calls `utf8.validate` and matches its `(void | utf8.invalid)`
// result instead of `?` (cross-shape propagation is #19). Even with
// match, the call diverges cstage/wwstage when the result is bound to
// a local (#48: wwstage spills DX for the payload-less arm) and the
// success path then SIGSEGVs lifting `str` to `(str | utf8.invalid)`.
// So the validation is open-coded against the decoder — the same
// byte-by-byte DFA walk `utf8.validate` performs (ref/hare/encoding/
// utf8/decode.ha:207) — whose `(rune | done | more | invalid)` arm
// matches the failure axis of fromutf8 and lifts cleanly. Collapses
// to `utf8.validate(in)?` once #19 + #48 are fixed.
// ww open-codes the same byte-by-byte DFA walk (ref/hare/encoding/utf8/
// decode.ha:207) because the cross-shape `(void | invalid) →
// (str | invalid)` propagation `?` needs is #19. The decoder-walk
// form's `(rune | done | more | invalid)` arm matches the failure
// axis of fromutf8 and lifts cleanly. Collapses to `utf8.validate(in)?`
// once #19 lands.
export fn fromutf8(in: []u8) (str | utf8.invalid) = {
let d: utf8.decoder = utf8.decode(in);
for (true) {

View File

@@ -0,0 +1,264 @@
/*
* 757_letbind_void_bang_void — wwstage slotsize recognises `void` as
* zero-sized so `(void | !void)` (or any aliased equivalent) sizes its
* tagged slot at tag + 0 = 8B rather than tag + 8 = 16B. Surfaced by
* task #7's `fromutf8` (lib/strings/strings.ww), which let-binds the
* `(void | utf8.invalid)` result of `utf8.validate` before matching.
*
* Pre-fix (#48 part-A): wwstage's slotsize (selfhost/cmd/wcc/cgenutil.ww)
* lacked a `void`-as-0B case — primsize("void")==0 failed the `> 0`
* guard and fell through alias/struct lookup to the catch-all `return 8`.
* Aliased `!void` recursed into the same TNAME("void") branch with the
* same fallthrough. The N_TTAGGED branch then computed maxsz=8, slot =
* tag + pad(8) = 16. cglet routes the let-init through cgwidentaggedstore
* which sizes its AX→+0 / DX→+8 ABI receive by slot size — so wwstage
* emitted a phantom `MOVQ DX, off+8` spill, picking up the callee's
* stale DX (validate's bare `return;` doesn't zero it). Frame size
* diverged by 16B; every downstream offset shifted; bootstrap byte-
* identity broke as soon as any caller let-bound a `(void | !void)`.
*
* Cstage was already correct: cmd/wcc/type.c:46 sets `ty_void.size = 0`,
* cmd/wcc/check.c:432 reads it via `maxsz = 0; vsz = 0; size = 8 + 0`.
*
* Fix (#48, wwstage-only per rule 10): two narrow additions to
* `slotsize` in selfhost/cmd/wcc/cgenutil.ww —
* 1. N_TBANG case at the top: recurse on .lhs, mirroring cstage
* resolve_type N_TBANG which copies the inner type's size.
* 2. N_TNAME branch: `streq(nm, "void") => 0` before primsize, so
* the zero-size lands without the prim-pad-to-8 contract firing.
*
* What this test pins:
* - Asm byte-identity between cstage and wwstage for `(void | invalid)`
* let-bind through match, where invalid = !void (utf8.invalid shape).
* - Runtime: both arms return their expected exit code.
* - Frame size matches between stages (no $48 vs $32 drift in `foo`).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.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;
}
struct row { const char *label; const char *src; int want; };
static const struct row rows[] = {
/* 1. The canonical fromutf8 shape: let-bind `(void | invalid)`,
* match on it, return through both arms. validate always returns
* the void variant here. */
{ "letbind_void_arm",
"type invalid = !void;\n"
"fn validate() (void | invalid) = { return; };\n"
"fn main() i32 = {\n"
" let v: (void | invalid) = validate();\n"
" match (v) {\n"
" case void => return 7;\n"
" case let e: invalid => return 9;\n"
" };\n"
" return 0;\n"
"};\n",
7 },
/* 2. invalid arm — validate returns the !void variant. Pre-fix
* the phantom payload-spill picked up validate's stale DX; post-
* fix only the tag word lands in the slot. */
{ "letbind_invalid_arm",
"type invalid = !void;\n"
"fn validate() (void | invalid) = {\n"
" let e: invalid; return e;\n"
"};\n"
"fn main() i32 = {\n"
" let v: (void | invalid) = validate();\n"
" match (v) {\n"
" case void => return 7;\n"
" case let e: invalid => return 9;\n"
" };\n"
" return 0;\n"
"};\n",
9 },
/* 3. Natural fromutf8 form: success-arm lifts a plain str into
* `(str | invalid)`. Pre-fix the let-bind's bogus 16B slot
* pushed every downstream offset 8B further from BP; the lift
* itself (str → tagged-return ABI) was already correct, but
* frame-id drift broke byte-identity. */
{ "fromutf8_natural_match",
"type invalid = !void;\n"
"fn validate(s: str) (void | invalid) = { return; };\n"
"fn fromutf8(s: str) (str | invalid) = {\n"
" let v: (void | invalid) = validate(s);\n"
" match (v) {\n"
" case void => return s;\n"
" case let e: invalid => return e;\n"
" };\n"
" let e: invalid; return e;\n"
"};\n"
"fn main() i32 = {\n"
" match (fromutf8(\"hi\")) {\n"
" case let s: str => return s.len: i32;\n"
" case invalid => return -1;\n"
" };\n"
" return 0;\n"
"};\n",
2 },
};
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/wclbvv_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/wclbvv_%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;
}
/* asm_byte_identical — pin frame size + spill layout by diffing the
* w6c vs w6c_ww text output. #48 part-A's whole point is that
* wwstage's slot stops bloating for the (void | !void) shape, so the
* bytes must match. */
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[64], cs[64], ws[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/wclbvv_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/wclbvv_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/wclbvv_asm_%d_%d_w.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
unlink(src);
return -1;
}
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
bin, ws, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
unlink(src); unlink(cs);
return -1;
}
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
int rc = 0;
if (!fc || !fw) {
rc = -1;
} else {
for (;;) {
int a = fgetc(fc);
int b = fgetc(fw);
if (a != b) { rc = -1; break; }
if (a == EOF) break;
}
}
if (fc) fclose(fc);
if (fw) fclose(fw);
if (rc != 0)
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
r->label);
unlink(src); unlink(cs); unlink(ws);
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 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, "letbind_void_bang_void: 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,
"letbind_void_bang_void[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0)
fail++;
}
}
if (fail) {
fprintf(stderr,
"letbind_void_bang_void: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("letbind_void_bang_void: %d/%d ok\n", total, total);
return 0;
}