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

@@ -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) {