wcc: exprtype resolves SK_USE module-qual N_DOT call results (#16, #17)

Re-arming the wwstage asserttyped bail surfaced 94 nil-stamp warns in the
checked corpus: let (a,b) = mod.fn() left its destructure bindings (and every
use) unstamped because exprtype's N_CALL arm resolved an N_DOT callee by bare
leaf — the gap its own comment flagged (#16/#17). Fix at the root: when an
N_DOT callee's lhs resolves to SK_USE, resolve the result via
scopelookupinmodule (mirror cstage cexpr check.c:1035 + cgen fnretlookupmod
cgen.ww:2263). The N_MLET backfill then just consumes the resolved tuple,
matching harec create_unpack_bindings (check.c:1354-1419), which does no callee
resolution — single path, no third copy.

The SK_USE gate leaves the module-leaf==type/fn-name collision cases
(random/fnmatch) on bare lookup — that nominal-resolution gap is a separate
fold. Beyond destructure, the root fix also closes a latent cs!=ww divergence
on non-destructure cross-module same-leaf calls (a head-ordered shadow was
mis-sizing the receive slot).

asserttyped is ww-stage only, so the live ww-driver suite can't see this — the
net is the warn count (checked 94->0, collision cases unchanged) + cs==ww .s
(probe 956). Compiler binary unchanged; 990-997 byte-id hold.
This commit is contained in:
2026-05-28 03:55:22 +09:00
parent c882bcf27c
commit ea1579a6fd
5 changed files with 439 additions and 72 deletions

View File

@@ -353,6 +353,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_f32stamp_run \ $(BIN)/test_f32stamp_run \
$(BIN)/test_f32arg_run \ $(BIN)/test_f32arg_run \
$(BIN)/test_tuprecv_f64_run \ $(BIN)/test_tuprecv_f64_run \
$(BIN)/test_modqualdestr_run \
$(BIN)/test_tupparam_run \ $(BIN)/test_tupparam_run \
$(BIN)/test_structparam_run \ $(BIN)/test_structparam_run \
$(BIN)/test_structret_run \ $(BIN)/test_structret_run \
@@ -1269,6 +1270,11 @@ $(BIN)/test_tuprecv_f64_run: test/wcc/956_tuprecv_f64_run.c $(BIN)/ww \
$(LIB)/libwwrt.a | $(BIN) $(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $< $(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_modqualdestr_run: test/wcc/956_modqualdestr_run.c $(BIN)/ww \
$(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_tupparam_run: test/wcc/905_tupparam_run.c $(BIN)/ww \ $(BIN)/test_tupparam_run: test/wcc/905_tupparam_run.c $(BIN)/ww \
$(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \ $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
$(LIB)/libwwrt.a | $(BIN) $(LIB)/libwwrt.a | $(BIN)

View File

@@ -10379,23 +10379,26 @@ fn resolvewalk(c: *checker, n: *node) void = {
// (commit 2's bridge) needs: without it a `let (f,i)=mk()` f64 binding // (commit 2's bridge) needs: without it a `let (f,i)=mk()` f64 binding
// reads stamp nil → would disagree with the structural f64. // reads stamp nil → would disagree with the structural f64.
// //
// GUARD bare N_IDENT callee only: exprtype's N_CALL N_DOT arm is the // #6a-A: backfill off ANY call rhs, not just a bare N_IDENT callee, so
// shelved #16/#17 cross-module-shadow gap (check.ww:2250-2253), so a // a module-qualified `let (res, ov) = checked.addi64(a, b)` (N_DOT
// module-qualified callee resolves to the wrong/nil return type — // callee) stamps its bindings too. This is now a SINGLE path: just call
// stamping off it would be worse than the nil it replaces. Bare same- // exprtype(rhs) and consume the resolved N_TTUPLE — no callee resolution
// module callees resolve correctly today. Faithful to harec // here. Harec's create_unpack_bindings does the same: ZERO callee
// create_unpack_bindings (ref/harec/src/check.c:1354-1416) and the // resolution, it walks an already-typed tuple result (ref/harec/src/
// cstage twin (cmd/wcc/check.c:1912 N_MLET, which uses cexpr ungated); // check.c:1354-1419). The module-qualified resolution that makes this
// narrowed to N_IDENT pending #16/#17. Annotated bindings keep their // correct for an N_DOT callee lives at the ROOT, in exprtype's N_CALL
// own type. Mirrors the N_FORRANGE binding-install shape above; the // arm (the SK_USE-gated scopelookupinmodule there), so the binding just
// bindings would otherwise install (unstamped) via the generic N_LET // consumes. A D-class module whose leaf collides with a type/fn name
// walk, so this early return must register them itself. // resolves to nil/wrong-kind at the exprtype root (the SK_USE gate
// fails) → no N_TTUPLE → those destructures stay unstamped, a separate
// nominal-collision fold (#6a-D), not this one. Annotated bindings keep
// their own type. Mirrors the N_FORRANGE binding-install shape above;
// the bindings would otherwise install (unstamped) via the generic
// N_LET walk, so this early return must register them itself.
if (k == nkind.N_MLET) { if (k == nkind.N_MLET) {
if (n.rhs != nil) { resolvewalk(c, n.rhs); }; if (n.rhs != nil) { resolvewalk(c, n.rhs); };
let pt: *node = nil; let pt: *node = nil;
if (n.rhs != nil) { if (n.rhs.kind == nkind.N_CALL) { if (n.rhs != nil) { if (n.rhs.kind == nkind.N_CALL) {
let callee: *node = n.rhs.lhs;
if (callee != nil) { if (callee.kind == nkind.N_IDENT) {
// `rt` would shadow the imported lib/rt module // `rt` would shadow the imported lib/rt module
// (checkmoduleshadow errors); `rty` avoids it. // (checkmoduleshadow errors); `rty` avoids it.
let rty: *node = exprtype(c, n.rhs, nil); let rty: *node = exprtype(c, n.rhs, nil);
@@ -10403,7 +10406,6 @@ fn resolvewalk(c: *checker, n: *node) void = {
pt = rty.list; pt = rty.list;
}; }; }; };
}; }; }; };
}; };
let l: *node = n.list; let l: *node = n.list;
for (l != nil) { for (l != nil) {
if (l.lhs == nil) { if (l.lhs == nil) {
@@ -12236,16 +12238,41 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = {
// `foo()` inside module M binds to M.foo rather than another // `foo()` inside module M binds to M.foo rather than another
// module's same-leaf foo at the head of the flat scope bucket. // module's same-leaf foo at the head of the flat scope bucket.
// Mirrors cstage cexpr N_IDENT routing through // Mirrors cstage cexpr N_IDENT routing through
// scope_lookup_prefer with c->cur_mod. N_DOT keeps the bare // scope_lookup_prefer with c->cur_mod.
// scopelookup — its module-qualified resolution is a separate //
// gap (parser stores the leaf in callee.str; mod is in // #6a-A: a module-qualified `mod.fn()` callee resolves via the
// callee.lhs.str, not consumed here yet). // callee.lhs module hint when `mod` is an import (SK_USE) —
// scopelookupinmodule(mod, leaf) — mirroring cstage cexpr N_DOT
// (cmd/wcc/check.c:1035 scope_lookup_in_module) and cgen's
// rettupleof (cgen.ww:2263 fnretlookupmod). Closing this at the
// N_CALL root (vs the N_MLET backfill) makes every consumer of a
// module-qual call result — destructure binding AND a bare
// `mod.fn().0` rvalue — read the right return type via the one
// expr path, harec-faithful (binding-unpack does zero callee
// resolution, ref/harec/src/check.c:1354-1419). Without it the
// bare-leaf scopelookup grabbed whichever same-leaf fn heads the
// flat scope — wrong on a cross-module shadow (753_convwrap_audit:
// alpha.foo (i64,str) vs beta.foo (i64,i64)).
//
// THE SK_USE GATE is the #6a-D separator: a D-class callee whose
// `mod` leaf is itself a type/fn (SK_TYPE/SK_FN — random/fnmatch's
// module-leaf==type-name collision) does NOT resolve to SK_USE, so
// it falls through to the bare scopelookup and stays mis/unresolved.
// That nominal-collision is its own fold (#6a-D); not fixed here.
let s: *sym = nil; let s: *sym = nil;
if (callee.kind == nkind.N_IDENT) { if (callee.kind == nkind.N_IDENT) {
s = scopelookupprefer(c.cur, c.curmod, nm); s = scopelookupprefer(c.cur, c.curmod, nm);
} else {
let ms: *sym = nil;
if (callee.lhs != nil && callee.lhs.kind == nkind.N_IDENT) {
ms = scopelookupprefer(c.cur, c.curmod, callee.lhs.str);
};
if (ms != nil && ms.skind == skind.SK_USE) {
s = scopelookupinmodule(c.cur, callee.lhs.str, nm);
} else { } else {
s = scopelookup(c.cur, nm); s = scopelookup(c.cur, nm);
}; };
};
if (s == nil) { return nil; }; if (s == nil) { return nil; };
if (s.skind != skind.SK_FN) { return nil; }; if (s.skind != skind.SK_FN) { return nil; };
if (s.decl == nil) { return nil; }; if (s.decl == nil) { return nil; };

View File

@@ -390,23 +390,26 @@ fn resolvewalk(c: *checker, n: *node) void = {
// (commit 2's bridge) needs: without it a `let (f,i)=mk()` f64 binding // (commit 2's bridge) needs: without it a `let (f,i)=mk()` f64 binding
// reads stamp nil → would disagree with the structural f64. // reads stamp nil → would disagree with the structural f64.
// //
// GUARD bare N_IDENT callee only: exprtype's N_CALL N_DOT arm is the // #6a-A: backfill off ANY call rhs, not just a bare N_IDENT callee, so
// shelved #16/#17 cross-module-shadow gap (check.ww:2250-2253), so a // a module-qualified `let (res, ov) = checked.addi64(a, b)` (N_DOT
// module-qualified callee resolves to the wrong/nil return type — // callee) stamps its bindings too. This is now a SINGLE path: just call
// stamping off it would be worse than the nil it replaces. Bare same- // exprtype(rhs) and consume the resolved N_TTUPLE — no callee resolution
// module callees resolve correctly today. Faithful to harec // here. Harec's create_unpack_bindings does the same: ZERO callee
// create_unpack_bindings (ref/harec/src/check.c:1354-1416) and the // resolution, it walks an already-typed tuple result (ref/harec/src/
// cstage twin (cmd/wcc/check.c:1912 N_MLET, which uses cexpr ungated); // check.c:1354-1419). The module-qualified resolution that makes this
// narrowed to N_IDENT pending #16/#17. Annotated bindings keep their // correct for an N_DOT callee lives at the ROOT, in exprtype's N_CALL
// own type. Mirrors the N_FORRANGE binding-install shape above; the // arm (the SK_USE-gated scopelookupinmodule there), so the binding just
// bindings would otherwise install (unstamped) via the generic N_LET // consumes. A D-class module whose leaf collides with a type/fn name
// walk, so this early return must register them itself. // resolves to nil/wrong-kind at the exprtype root (the SK_USE gate
// fails) → no N_TTUPLE → those destructures stay unstamped, a separate
// nominal-collision fold (#6a-D), not this one. Annotated bindings keep
// their own type. Mirrors the N_FORRANGE binding-install shape above;
// the bindings would otherwise install (unstamped) via the generic
// N_LET walk, so this early return must register them itself.
if (k == nkind.N_MLET) { if (k == nkind.N_MLET) {
if (n.rhs != nil) { resolvewalk(c, n.rhs); }; if (n.rhs != nil) { resolvewalk(c, n.rhs); };
let pt: *node = nil; let pt: *node = nil;
if (n.rhs != nil) { if (n.rhs.kind == nkind.N_CALL) { if (n.rhs != nil) { if (n.rhs.kind == nkind.N_CALL) {
let callee: *node = n.rhs.lhs;
if (callee != nil) { if (callee.kind == nkind.N_IDENT) {
// `rt` would shadow the imported lib/rt module // `rt` would shadow the imported lib/rt module
// (checkmoduleshadow errors); `rty` avoids it. // (checkmoduleshadow errors); `rty` avoids it.
let rty: *node = exprtype(c, n.rhs, nil); let rty: *node = exprtype(c, n.rhs, nil);
@@ -414,7 +417,6 @@ fn resolvewalk(c: *checker, n: *node) void = {
pt = rty.list; pt = rty.list;
}; }; }; };
}; }; }; };
}; };
let l: *node = n.list; let l: *node = n.list;
for (l != nil) { for (l != nil) {
if (l.lhs == nil) { if (l.lhs == nil) {
@@ -2247,16 +2249,41 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = {
// `foo()` inside module M binds to M.foo rather than another // `foo()` inside module M binds to M.foo rather than another
// module's same-leaf foo at the head of the flat scope bucket. // module's same-leaf foo at the head of the flat scope bucket.
// Mirrors cstage cexpr N_IDENT routing through // Mirrors cstage cexpr N_IDENT routing through
// scope_lookup_prefer with c->cur_mod. N_DOT keeps the bare // scope_lookup_prefer with c->cur_mod.
// scopelookup — its module-qualified resolution is a separate //
// gap (parser stores the leaf in callee.str; mod is in // #6a-A: a module-qualified `mod.fn()` callee resolves via the
// callee.lhs.str, not consumed here yet). // callee.lhs module hint when `mod` is an import (SK_USE) —
// scopelookupinmodule(mod, leaf) — mirroring cstage cexpr N_DOT
// (cmd/wcc/check.c:1035 scope_lookup_in_module) and cgen's
// rettupleof (cgen.ww:2263 fnretlookupmod). Closing this at the
// N_CALL root (vs the N_MLET backfill) makes every consumer of a
// module-qual call result — destructure binding AND a bare
// `mod.fn().0` rvalue — read the right return type via the one
// expr path, harec-faithful (binding-unpack does zero callee
// resolution, ref/harec/src/check.c:1354-1419). Without it the
// bare-leaf scopelookup grabbed whichever same-leaf fn heads the
// flat scope — wrong on a cross-module shadow (753_convwrap_audit:
// alpha.foo (i64,str) vs beta.foo (i64,i64)).
//
// THE SK_USE GATE is the #6a-D separator: a D-class callee whose
// `mod` leaf is itself a type/fn (SK_TYPE/SK_FN — random/fnmatch's
// module-leaf==type-name collision) does NOT resolve to SK_USE, so
// it falls through to the bare scopelookup and stays mis/unresolved.
// That nominal-collision is its own fold (#6a-D); not fixed here.
let s: *sym = nil; let s: *sym = nil;
if (callee.kind == nkind.N_IDENT) { if (callee.kind == nkind.N_IDENT) {
s = scopelookupprefer(c.cur, c.curmod, nm); s = scopelookupprefer(c.cur, c.curmod, nm);
} else {
let ms: *sym = nil;
if (callee.lhs != nil && callee.lhs.kind == nkind.N_IDENT) {
ms = scopelookupprefer(c.cur, c.curmod, callee.lhs.str);
};
if (ms != nil && ms.skind == skind.SK_USE) {
s = scopelookupinmodule(c.cur, callee.lhs.str, nm);
} else { } else {
s = scopelookup(c.cur, nm); s = scopelookup(c.cur, nm);
}; };
};
if (s == nil) { return nil; }; if (s == nil) { return nil; };
if (s.skind != skind.SK_FN) { return nil; }; if (s.skind != skind.SK_FN) { return nil; };
if (s.decl == nil) { return nil; }; if (s.decl == nil) { return nil; };

View File

@@ -10379,23 +10379,26 @@ fn resolvewalk(c: *checker, n: *node) void = {
// (commit 2's bridge) needs: without it a `let (f,i)=mk()` f64 binding // (commit 2's bridge) needs: without it a `let (f,i)=mk()` f64 binding
// reads stamp nil → would disagree with the structural f64. // reads stamp nil → would disagree with the structural f64.
// //
// GUARD bare N_IDENT callee only: exprtype's N_CALL N_DOT arm is the // #6a-A: backfill off ANY call rhs, not just a bare N_IDENT callee, so
// shelved #16/#17 cross-module-shadow gap (check.ww:2250-2253), so a // a module-qualified `let (res, ov) = checked.addi64(a, b)` (N_DOT
// module-qualified callee resolves to the wrong/nil return type — // callee) stamps its bindings too. This is now a SINGLE path: just call
// stamping off it would be worse than the nil it replaces. Bare same- // exprtype(rhs) and consume the resolved N_TTUPLE — no callee resolution
// module callees resolve correctly today. Faithful to harec // here. Harec's create_unpack_bindings does the same: ZERO callee
// create_unpack_bindings (ref/harec/src/check.c:1354-1416) and the // resolution, it walks an already-typed tuple result (ref/harec/src/
// cstage twin (cmd/wcc/check.c:1912 N_MLET, which uses cexpr ungated); // check.c:1354-1419). The module-qualified resolution that makes this
// narrowed to N_IDENT pending #16/#17. Annotated bindings keep their // correct for an N_DOT callee lives at the ROOT, in exprtype's N_CALL
// own type. Mirrors the N_FORRANGE binding-install shape above; the // arm (the SK_USE-gated scopelookupinmodule there), so the binding just
// bindings would otherwise install (unstamped) via the generic N_LET // consumes. A D-class module whose leaf collides with a type/fn name
// walk, so this early return must register them itself. // resolves to nil/wrong-kind at the exprtype root (the SK_USE gate
// fails) → no N_TTUPLE → those destructures stay unstamped, a separate
// nominal-collision fold (#6a-D), not this one. Annotated bindings keep
// their own type. Mirrors the N_FORRANGE binding-install shape above;
// the bindings would otherwise install (unstamped) via the generic
// N_LET walk, so this early return must register them itself.
if (k == nkind.N_MLET) { if (k == nkind.N_MLET) {
if (n.rhs != nil) { resolvewalk(c, n.rhs); }; if (n.rhs != nil) { resolvewalk(c, n.rhs); };
let pt: *node = nil; let pt: *node = nil;
if (n.rhs != nil) { if (n.rhs.kind == nkind.N_CALL) { if (n.rhs != nil) { if (n.rhs.kind == nkind.N_CALL) {
let callee: *node = n.rhs.lhs;
if (callee != nil) { if (callee.kind == nkind.N_IDENT) {
// `rt` would shadow the imported lib/rt module // `rt` would shadow the imported lib/rt module
// (checkmoduleshadow errors); `rty` avoids it. // (checkmoduleshadow errors); `rty` avoids it.
let rty: *node = exprtype(c, n.rhs, nil); let rty: *node = exprtype(c, n.rhs, nil);
@@ -10403,7 +10406,6 @@ fn resolvewalk(c: *checker, n: *node) void = {
pt = rty.list; pt = rty.list;
}; }; }; };
}; }; }; };
}; };
let l: *node = n.list; let l: *node = n.list;
for (l != nil) { for (l != nil) {
if (l.lhs == nil) { if (l.lhs == nil) {
@@ -12236,16 +12238,41 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = {
// `foo()` inside module M binds to M.foo rather than another // `foo()` inside module M binds to M.foo rather than another
// module's same-leaf foo at the head of the flat scope bucket. // module's same-leaf foo at the head of the flat scope bucket.
// Mirrors cstage cexpr N_IDENT routing through // Mirrors cstage cexpr N_IDENT routing through
// scope_lookup_prefer with c->cur_mod. N_DOT keeps the bare // scope_lookup_prefer with c->cur_mod.
// scopelookup — its module-qualified resolution is a separate //
// gap (parser stores the leaf in callee.str; mod is in // #6a-A: a module-qualified `mod.fn()` callee resolves via the
// callee.lhs.str, not consumed here yet). // callee.lhs module hint when `mod` is an import (SK_USE) —
// scopelookupinmodule(mod, leaf) — mirroring cstage cexpr N_DOT
// (cmd/wcc/check.c:1035 scope_lookup_in_module) and cgen's
// rettupleof (cgen.ww:2263 fnretlookupmod). Closing this at the
// N_CALL root (vs the N_MLET backfill) makes every consumer of a
// module-qual call result — destructure binding AND a bare
// `mod.fn().0` rvalue — read the right return type via the one
// expr path, harec-faithful (binding-unpack does zero callee
// resolution, ref/harec/src/check.c:1354-1419). Without it the
// bare-leaf scopelookup grabbed whichever same-leaf fn heads the
// flat scope — wrong on a cross-module shadow (753_convwrap_audit:
// alpha.foo (i64,str) vs beta.foo (i64,i64)).
//
// THE SK_USE GATE is the #6a-D separator: a D-class callee whose
// `mod` leaf is itself a type/fn (SK_TYPE/SK_FN — random/fnmatch's
// module-leaf==type-name collision) does NOT resolve to SK_USE, so
// it falls through to the bare scopelookup and stays mis/unresolved.
// That nominal-collision is its own fold (#6a-D); not fixed here.
let s: *sym = nil; let s: *sym = nil;
if (callee.kind == nkind.N_IDENT) { if (callee.kind == nkind.N_IDENT) {
s = scopelookupprefer(c.cur, c.curmod, nm); s = scopelookupprefer(c.cur, c.curmod, nm);
} else {
let ms: *sym = nil;
if (callee.lhs != nil && callee.lhs.kind == nkind.N_IDENT) {
ms = scopelookupprefer(c.cur, c.curmod, callee.lhs.str);
};
if (ms != nil && ms.skind == skind.SK_USE) {
s = scopelookupinmodule(c.cur, callee.lhs.str, nm);
} else { } else {
s = scopelookup(c.cur, nm); s = scopelookup(c.cur, nm);
}; };
};
if (s == nil) { return nil; }; if (s == nil) { return nil; };
if (s.skind != skind.SK_FN) { return nil; }; if (s.skind != skind.SK_FN) { return nil; };
if (s.decl == nil) { return nil; }; if (s.decl == nil) { return nil; };

View File

@@ -0,0 +1,280 @@
/*
* 956_modqualdestr_run — runtime + byte-id + stamp regression net for
* #6a-A: module-qualified destructure binding types.
*
* THE GAP: the wwstage N_MLET handler (check.ww) backfilled destructure
* binding types only when the call rhs had a bare N_IDENT callee. A
* module-qualified `let (frac, exp) = myf.frexp(x)` has an N_DOT callee,
* so the bindings stayed un-stamped (n.type_ nil). cstage never gated on
* callee kind (cmd/wcc/check.c N_MLET → cexpr ungated), so this was a
* wwstage-only stamp gap. #6a-A broadens the backfill to ANY call rhs;
* exprtype's N_CALL arm resolves the N_DOT callee's leaf for the
* resolvable case (leaf unique across modules — the myf.* shapes here).
*
* THE PAYOFF (float row): an un-stamped f64 destructure binding fed to
* float arithmetic mis-classifies as integer-kind 0 in cgen's
* exprfloatkind (n.type_ is the SSoT after the #121 collapse). The
* residual cgbin float-arith sibling-evidence guard turns that into a
* loud abort, so pre-fix w6c_ww FAILS TO COMPILE the float row (no .s
* emitted) — the cs==ww gate (b) and the stamp gate (c) both catch it.
* Post-fix the binding stamps f64, the SSE-cursor receive lands, and
* both stages emit byte-identical asm.
*
* Sibling of 956_tuprecv_f64_run (same-module float destructure, bare
* N_IDENT callee) and 953_f64crossmod_run (single-call module-qualified
* f64). Single-file multi-package form (like 953) so w6c/w6c_ww see the
* cross-module call without -I plumbing. Each row carries three
* dimensions:
* (a) cstage `ww build` + run, asserting the exit code (cstage is the
* reference — correct pre- and post-fix).
* (b) w6c vs w6c_ww `.s` cmp — rule-10 byte-id; pre-fix the float row
* diverges (w6c_ww loud-aborts, emits no .s).
* (c) #6a-A stamp gate: w6c_ww must emit no `asserttyped:` diagnostic
* on the destructure binding idents. Pre-fix fires on every use.
*/
#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_exit;
};
static const struct row rows[] = {
/* HEADLINE — module-qualified FLOAT destructure (f64, i64). frac is
* fed to float arith (`frac + 1.0`), forcing exprfloatkind to read
* its stamp. Pre-fix frac.type_==nil -> kind 0 -> cgbin float-arith
* loud-abort in w6c_ww. Post-fix frac stamps f64. frac=8.0 -> g=9.0;
* exp=1. */
{ "mq_f64_i64_destr",
"package myf;\n"
"export fn frexp(n: f64) (f64, i64) = {\n"
"\tif (n == 0.0) { return (0.0, 0i64); };\n"
"\treturn (n, 1i64);\n"
"};\n"
"package main;\n"
"import myf;\n"
"export fn main() i32 = {\n"
"\tlet (frac, exp) = myf.frexp(8.0);\n"
"\tlet g: f64 = frac + 1.0;\n"
"\tif (g != 9.0) { return 1; };\n"
"\tif (exp != 1i64) { return 2; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* DOMINANT CLASS-A — module-qualified INTEGER destructure (the
* checked.addXX shape: `let (res, ov) = checked.addi64(a, b)`). cgen's
* structural classifier already routed the integer codegen, so this
* is byte-id pre- and post-fix; the discriminator is the (c) stamp
* gate — pre-fix res/ov fire asserttyped. res=15, ov=false. */
{ "mq_i64_bool_destr",
"package myf;\n"
"export fn addi64(a: i64, b: i64) (i64, bool) = {\n"
"\tlet s: i64 = a + b;\n"
"\treturn (s, false);\n"
"};\n"
"package main;\n"
"import myf;\n"
"export fn main() i32 = {\n"
"\tlet (res, ov) = myf.addi64(7i64, 8i64);\n"
"\tif (res != 15i64) { return 1; };\n"
"\tif (ov) { return 2; };\n"
"\treturn (res: i32);\n"
"};\n", 15 },
/* MIXED — module-qualified (f64, str) destructure: the float rides
* the SSE cursor (X0), the str rides the integer cursor (AX,DX,CX).
* Confirms the stamp distributes correctly onto a wide-header binding
* alongside the float. f=4.0 -> 4; s.len=5 ("hello"). */
{ "mq_f64_str_destr",
"package myf;\n"
"export fn fs(n: f64) (f64, str) = {\n"
"\tif (n == 0.0) { return (n, \"x\"); };\n"
"\treturn (n, \"hello\");\n"
"};\n"
"package main;\n"
"import myf;\n"
"export fn main() i32 = {\n"
"\tlet (f, s) = myf.fs(4.0);\n"
"\tlet g: f64 = f + 0.0;\n"
"\tif (g != 4.0) { return 1; };\n"
"\tif (s.len != 5) { return 2; };\n"
"\treturn (f: i32);\n"
"};\n", 4 },
/* CLASS-CLOSURE — NON-destructure cross-module same-leaf shadow. The
* #6a-A fix lives at the exprtype N_CALL root, so it closes the whole
* class, not just the destructure surface (same coverage-trap as the
* 6.0b gap-corpus probe). beta.dup -> (i64,i64), alpha.dup -> (i64,str);
* alpha is source-ordered LAST so its (i64,str) heads the flat-scope
* bare-leaf bucket (753_convwrap_audit's trick). `let r = beta.dup()`
* is a SINGLE-VAR receive (no destructure). Pre-fix the bare-leaf
* scopelookup stamped r off alpha.dup's (i64,str) 32B shape, so the
* wwstage cglet sized a $32 / AX,DX,CX,R8 slot while cstage (typed AST)
* sized $16 -> a cs!=ww rule-10 DIVERGENCE this row catches. Post-fix
* the SK_USE-gated scopelookupinmodule resolves beta.dup -> $16, byte-
* id. r.0=3, r.1=7 -> 10. */
{ "mq_nondestr_shadow",
"package beta;\n"
"export fn dup() (i64, i64) = { return (3i64, 7i64); };\n"
"package main;\n"
"import beta;\n"
"export fn main() i32 = {\n"
"\tlet r = beta.dup();\n"
"\tif (r.1 != 7i64) { return 1; };\n"
"\treturn (r.0: i32) + (r.1: i32);\n"
"};\n"
"package alpha;\n"
"export fn dup() (i64, str) = { return (9i64, \"wrong\"); };\n", 10 },
{ NULL, NULL, 0 }
};
static int
slurp_eq(const char *a, const char *b)
{
FILE *fa = fopen(a, "rb");
FILE *fb = fopen(b, "rb");
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
int rc = 0;
for (;;) {
int ca = fgetc(fa);
int cb = fgetc(fb);
if (ca != cb) { rc = -1; break; }
if (ca == EOF) break;
}
fclose(fa); fclose(fb);
return rc;
}
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 w6c[1100], w6c_ww[1100];
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
if (access(w6c_ww, X_OK) != 0) {
fprintf(stderr, "modqualdestr: w6c_ww missing — cannot run the "
"cs==ww byte-id gate\n");
return 1;
}
int n = 0, fail = 0;
for (int i = 0; rows[i].src; i++, n++) {
char src[64];
snprintf(src, sizeof src, "/tmp/wwmqd_%d_%d.ww", getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; continue; }
fputs(rows[i].src, f);
fclose(f);
char cmd[2048];
/* (a) cstage build + run in a scratch dir. */
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwmqd_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s",
tmpdir, bin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: cstage build failed\n",
rows[i].label);
fail++;
unlink(src); rmdir(tmpdir);
continue;
}
char outbin[128];
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
if (got != rows[i].want_exit) {
fprintf(stderr, "row[%s]: cstage exit %d, want %d\n",
rows[i].label, got, rows[i].want_exit);
fail++;
}
unlink(outbin); rmdir(tmpdir);
/* (b) cs==ww byte-id gate: emit .s from both stages, cmp. */
char cs_s[64], ws_s[64];
snprintf(cs_s, sizeof cs_s, "/tmp/wwmqd_%d_%d_cs.s",
getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwmqd_%d_%d_ww.s",
getpid(), i);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
w6c, cs_s, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
fail++; unlink(src); continue;
}
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
w6c_ww, ws_s, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww failed\n",
rows[i].label);
fail++; unlink(src); unlink(cs_s); continue;
}
if (slurp_eq(cs_s, ws_s) != 0) {
fprintf(stderr,
"row[%s]: cstage/wwstage .s DIFFER (rule-10 "
"byte-id violation)\n", rows[i].label);
fail++;
}
/* (c) #6a-A stamp gate: the module-qualified destructure
* binding must carry a checker type stamp, so w6c_ww emits no
* `asserttyped:` diagnostic on its idents. Non-vacuous —
* pre-fix (HEAD) w6c_ww fires asserttyped on every binding use
* (and the float row loud-aborts in cgen above). */
char errf[80];
snprintf(errf, sizeof errf,
"/tmp/wwmqd_%d_%d_err.txt", getpid(), i);
snprintf(cmd, sizeof cmd,
"%s -o /dev/null %s 2>%s", w6c_ww, src, errf);
runwait(cmd);
snprintf(cmd, sizeof cmd, "grep -q asserttyped %s", errf);
if (runwait(cmd) == 0) {
fprintf(stderr, "row[%s]: w6c_ww emitted asserttyped "
"(destructure binding unstamped)\n", rows[i].label);
fail++;
}
unlink(errf);
unlink(src); unlink(cs_s); unlink(ws_s);
}
if (fail) {
fprintf(stderr, "%d/%d modqual-destructure tests failed\n",
fail, n);
return 1;
}
printf("modqualdestr: %d/%d ok (cstage run + cs==ww byte-id + "
"stamp)\n", n, n);
return 0;
}