w6c+w6c_ww: cast-wrapped tuple literal widens its whole payload into a tagged slot (#66)

The #242 tuple arm of the widen choke-point (cg_widen_tagged_store /
cgwidentaggedstorebp) gated on a BARE N_TUPLE source. The cast-to-
CONCRETE-VARIANT wrapper ((a, b): range_alias) — the only spelling
real code uses (ref/hare/regex/regex.ha:213) — is not a widen-cast
(its destination is the variant, not the union), so the peel left it
intact and it fell to the SCALAR arm: cursor word 0 stored, payload
slot 1+ silently zero-filled. Both stages, byte-id, gate-blind.

Fix at the choke-point: peel N_CAST(lhs=N_TUPLE) where the NAMED-
peeled cast type is TY_TUPLE and iterate the inner element list; the
variant tag keeps resolving from the CAST's type (exact named match),
so the #241 untyped-element loud-stop stays scoped to the bare form
on both stages.

Closure by construction needed two more arms (reviewer proof-grep):
cg_widen_tagged_push's direct-push fast path classified a tuple-typed
ARG source as scalar — pushed word 0 only AND coerced an unresolved
tag to 0 — so f(((a,b): rng)) bypassed the fixed arm entirely (and
the bare typed (a,b) arg dropped slot 1 the same way). Tuple-typed
sources now route through the scratch store. The remaining non-
literal tuple sources (ident / call result / match binding) have no
word-copy arm in the store and fell to its scalar arm — loud-stop
(rule 7) until #72 wires them. Every tagged-payload materialisation
now funnels through cg_widen_tagged_store, which handles or rejects
every tuple shape: let/assign/return/append (cgen.c:7511) directly,
arg push via the scratch route.

test/936: cast-tuple matrix (let / append local+index-place+deref-
place+ptr-field-place / ident+float+str elements / 3-member layout-neutrality /
direct-arg) + bare-form no-regress (return + arg) + bare-literal and
tuple-ident reject pins, per-row cs==ww byte-id; verified failing
24/40 at parent 8578ad0.

Unblocks regex fold-4 (charset_range_item construction).
This commit is contained in:
2026-06-04 20:06:21 +09:00
parent 8578ad0533
commit bb8a44a564
6 changed files with 762 additions and 29 deletions

View File

@@ -0,0 +1,525 @@
/*
* 936_tagged_tuple_widen_run — #66 (fold-4 prereq): the cast-wrapped
* tuple literal `((a, b): variant_alias)` widened into a tagged-union
* payload. Pre-#66 the #242 tuple arm of the widen choke-point
* (cg_widen_tagged_store / cgwidentaggedstorebp) gated on a BARE
* N_TUPLE source; the cast-to-CONCRETE-VARIANT wrapper — the only
* spelling real code uses (ref/hare/regex/regex.ha:213
* `(start_b, end_b): charset_range_item`) — was not peeled (it is not
* a widen-cast: its destination is the variant, not the union), fell
* to the SCALAR arm and silently stored cursor word 0 only, zero-
* filling payload slot 1+ (both stages, byte-id — gate-blind). The
* read side (match-extract slot walk) was always correct.
*
* row | shape | want
* -----------------------+--------------------------------------+-----
* cast_let_packed | let v: item = ((65,90): rng); .0/.1 | 0
* cast_let_3member | + (str,*fn) member present, never |
* | constructed (layout-neutral pin) | 0
* cast_append_local | append(xs, cast) → index → extract | 0
* cast_append_indexplace | append(cs[len-1], cast), [][]item | 0
* cast_append_derefplace | through *[][]item param (via_outer) | 0
* cast_append_ptrfield | append(p.cs[len-1], cast) — ptr- |
* | FIELD-rooted place | 0
* cast_ident_elems | (start_b, end_b) u32 LOCALS — the |
* | handle_bracket shape | 0
* cast_float_elem | (u32, f64) — MOVSD element store | 0
* cast_str_elem | (i64, str) — 3-word wide element | 0
* cast_arg_direct | cast tuple as a DIRECT tagged ARG — |
* | cg_widen_tagged_push routes tuple |
* | srcs to the scratch store (the fast |
* | scalar push dropped slot 1+) | 0
* bare_ident_arg | bare (a, b) typed idents as a tagged |
* | ARG — same push routing, tag from |
* | the typed tuple | 0
* bare_ident_no_regress | bare (a, b) typed-ident return into |
* | tagged — the strconv parseint shape | 0
* bare_literal_reject | bare (5, 6) literal-element tuple |
* | into tagged stays the #242/#241 loud |
* | stop (the cast form lifts it; the |
* | bare form must NOT silently change) | err
* tuple_ident_reject | tuple-typed IDENT into tagged stays |
* | LOUD (#72) — was a silent slot-1+ |
* | drop through the scalar arm | err
*
* Every K_RUN row also asserts cstage/wwstage asm byte-id. NNN<950,
* self-contained (/tmp, no imports) — rule-14's selfhost-sibling race
* does not apply (941 precedent).
*/
#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;
}
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), cb = fgetc(fb);
if (ca != cb) { rc = -1; break; }
if (ca == EOF) break;
}
fclose(fa); fclose(fb);
return rc;
}
#define K_RUN 0 /* build+run both drivers, exit==want, + cs==ww byte-id */
#define K_BUILDERR 1 /* build must FAIL with experr on BOTH drivers (rule 7) */
struct row { const char *label; const char *src; int want;
int kind; const char *experr; };
/* errlog_has — a BUILDERR row must fail WITH its diagnostic; any other
* failure (parse error, crash) is a vacuous reject (940 precedent). */
static int
errlog_has(const char *path, const char *needle)
{
FILE *f = fopen(path, "rb");
if (!f) return 0;
char buf[8192];
size_t got = fread(buf, 1, sizeof buf - 1, f);
fclose(f);
buf[got] = '\0';
return strstr(buf, needle) != NULL;
}
static const struct row rows[] = {
{ "cast_let_packed",
"package main;\n"
"type lit = rune;\n"
"type rng = (u32, u32);\n"
"type item = (lit | rng);\n"
"export fn main() i32 = {\n"
" let v: item = ((65, 90): rng);\n"
" match (v) {\n"
" case let r: rng => {\n"
" if (r.0 != 65) { return 1; };\n"
" if (r.1 != 90) { return 2; };\n"
" };\n"
" case => return 3;\n"
" };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* the real charset element: the (str,*fn) class member is PRESENT
* IN THE TYPE but never constructed — its presence must not alter
* the lit/range slot layout or walks (fold-4 PF1 caveat). */
{ "cast_let_3member",
"package main;\n"
"type lit = rune;\n"
"type rng = (u32, u32);\n"
"type cls = (str, *fn(c: rune) bool);\n"
"type item = (lit | rng | cls);\n"
"export fn main() i32 = {\n"
" let v: item = ((65, 90): rng);\n"
" match (v) {\n"
" case let r: rng => {\n"
" if (r.0 != 65) { return 1; };\n"
" if (r.1 != 90) { return 2; };\n"
" };\n"
" case => return 3;\n"
" };\n"
" let w: item = ('a': lit);\n"
" match (w) {\n"
" case let l: lit => { if ((l: rune) != 'a') { return 4; }; };\n"
" case => return 5;\n"
" };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
{ "cast_append_local",
"package main;\n"
"type lit = rune;\n"
"type rng = (u32, u32);\n"
"type item = (lit | rng);\n"
"export fn main() i32 = {\n"
" let xs: []item;\n"
" append(xs, ('a': lit));\n"
" append(xs, ((65, 90): rng));\n"
" if (len(xs) != 2) { return 1; };\n"
" let e: item = xs[1];\n"
" match (e) {\n"
" case let r: rng => {\n"
" if (r.0 != 65) { return 2; };\n"
" if (r.1 != 90) { return 3; };\n"
" };\n"
" case => return 4;\n"
" };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* append place = indexed inner slice — the handle_bracket
* `append(charsets[len(charsets)-1], ...)` route. */
{ "cast_append_indexplace",
"package main;\n"
"type lit = rune;\n"
"type rng = (u32, u32);\n"
"type item = (lit | rng);\n"
"export fn main() i32 = {\n"
" let charsets: [][]item;\n"
" let empty: []item;\n"
" append(charsets, empty);\n"
" append(charsets[len(charsets) - 1], ((7, 9): rng));\n"
" if (len(charsets[0]) != 1) { return 1; };\n"
" let e: item = charsets[0][0];\n"
" match (e) {\n"
" case let r: rng => {\n"
" if (r.0 != 7) { return 2; };\n"
" if (r.1 != 9) { return 3; };\n"
" };\n"
" case => return 4;\n"
" };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* append place behind a *[][]item param — the via_outer
* (pointer-rooted) widen route. */
{ "cast_append_derefplace",
"package main;\n"
"type lit = rune;\n"
"type rng = (u32, u32);\n"
"type item = (lit | rng);\n"
"fn addrange(cs: *[][]item, a: u32, b: u32) void = {\n"
" if (len(*cs) == 0) {\n"
" let empty: []item;\n"
" append(*cs, empty);\n"
" };\n"
" append((*cs)[len(*cs) - 1], ((a, b): rng));\n"
"};\n"
"export fn main() i32 = {\n"
" let charsets: [][]item;\n"
" addrange(&charsets, 5, 8);\n"
" if (len(charsets) != 1) { return 1; };\n"
" if (len(charsets[0]) != 1) { return 2; };\n"
" let e: item = charsets[0][0];\n"
" match (e) {\n"
" case let r: rng => {\n"
" if (r.0 != 5) { return 3; };\n"
" if (r.1 != 8) { return 4; };\n"
" };\n"
" case => return 5;\n"
" };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* append place rooted at a ptr-FIELD ([][]item field through
* *struct) — the re.charsets-style place spelling. */
{ "cast_append_ptrfield",
"package main;\n"
"type lit = rune;\n"
"type rng = (u32, u32);\n"
"type item = (lit | rng);\n"
"type holder = struct { cs: [][]item, n: i64 };\n"
"export fn main() i32 = {\n"
" let h: holder = holder { n = 0, ... };\n"
" let p: *holder = &h;\n"
" let empty: []item;\n"
" append(p.cs, empty);\n"
" append(p.cs[len(p.cs) - 1], ((7, 9): rng));\n"
" if (len(h.cs[0]) != 1) { return 1; };\n"
" match (h.cs[0][0]) {\n"
" case let r: rng => {\n"
" if (r.0 != 7) { return 2; };\n"
" if (r.1 != 9) { return 3; };\n"
" };\n"
" case => return 4;\n"
" };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* typed u32 LOCALS as the tuple elements — handle_bracket's
* exact `(start_b, end_b): charset_range_item` shape. */
{ "cast_ident_elems",
"package main;\n"
"type lit = rune;\n"
"type rng = (u32, u32);\n"
"type item = (lit | rng);\n"
"export fn main() i32 = {\n"
" let start_b: u32 = 97;\n"
" let end_b: u32 = 122;\n"
" let v: item = ((start_b, end_b): rng);\n"
" match (v) {\n"
" case let r: rng => {\n"
" if (r.0 != 97) { return 1; };\n"
" if (r.1 != 122) { return 2; };\n"
" };\n"
" case => return 3;\n"
" };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* float element rides the MOVSD store branch under the cast form */
{ "cast_float_elem",
"package main;\n"
"type mf = (u32, f64);\n"
"type item = (rune | mf);\n"
"export fn main() i32 = {\n"
" let a: u32 = 9;\n"
" let x: f64 = 2.5;\n"
" let v: item = ((a, x): mf);\n"
" match (v) {\n"
" case let m: mf => {\n"
" if (m.0 != 9) { return 1; };\n"
" if (m.1 != 2.5) { return 2; };\n"
" };\n"
" case => return 3;\n"
" };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* str element rides the 3-word wide branch under the cast form */
{ "cast_str_elem",
"package main;\n"
"type ms = (i64, str);\n"
"type item = (rune | ms);\n"
"export fn main() i32 = {\n"
" let n: i64 = 12;\n"
" let s: str = \"wxyz\";\n"
" let v: item = ((n, s): ms);\n"
" match (v) {\n"
" case let m: ms => {\n"
" if (m.0 != 12) { return 1; };\n"
" if (len(m.1) != 4) { return 2; };\n"
" };\n"
" case => return 3;\n"
" };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* cast tuple passed DIRECTLY as a tagged call arg — pre-fix
* cg_widen_tagged_push's scalar fast path pushed word 0 only
* (and coerced an unresolved tag to 0); tuple-typed sources now
* route through the scratch store choke-point. */
{ "cast_arg_direct",
"package main;\n"
"type lit = rune;\n"
"type rng = (u32, u32);\n"
"type item = (lit | rng);\n"
"fn check(v: item) i32 = {\n"
" match (v) {\n"
" case let r: rng => {\n"
" if (r.0 != 65) { return 1; };\n"
" if (r.1 != 90) { return 2; };\n"
" return 0;\n"
" };\n"
" case => return 3;\n"
" };\n"
"};\n"
"export fn main() i32 = {\n"
" return check(((65, 90): rng));\n"
"};\n", 0, K_RUN, NULL },
/* bare typed-ident tuple as a tagged call arg — the strconv
* `(neg, n)` shape at a CALL boundary; rides the same push
* routing, tag resolved from the typed tuple. */
{ "bare_ident_arg",
"package main;\n"
"type item = (rune | (i64, i64));\n"
"fn check(v: item) i32 = {\n"
" match (v) {\n"
" case let r: (i64, i64) => {\n"
" if (r.0 != 5) { return 1; };\n"
" if (r.1 != 6) { return 2; };\n"
" return 0;\n"
" };\n"
" case => return 3;\n"
" };\n"
"};\n"
"export fn main() i32 = {\n"
" let a: i64 = 5;\n"
" let b: i64 = 6;\n"
" return check((a, b));\n"
"};\n", 0, K_RUN, NULL },
/* bare N_TUPLE of TYPED idents (the strconv parseint `(neg, n)`
* return shape) must keep working — no-regress on the bare arm. */
{ "bare_ident_no_regress",
"package main;\n"
"fn mk() ((i64, i64) | nomem) = {\n"
" let a: i64 = 5;\n"
" let b: i64 = 6;\n"
" return (a, b);\n"
"};\n"
"export fn main() i32 = {\n"
" match (mk()) {\n"
" case let t: (i64, i64) => {\n"
" if (t.0 != 5) { return 1; };\n"
" if (t.1 != 6) { return 2; };\n"
" };\n"
" case => return 3;\n"
" };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* bare literal-element tuple into tagged stays the #242/#241
* loud-stop — the cast form lifts it (the cast supplies the
* variant type), the bare form must not silently change. */
{ "bare_literal_reject",
"package main;\n"
"fn mk() ((i64, i64) | nomem) = {\n"
" return (5, 6);\n"
"};\n"
"export fn main() i32 = {\n"
" match (mk()) {\n"
" case let t: (i64, i64) => { if (t.0 != 5) { return 1; }; };\n"
" case => return 2;\n"
" };\n"
" return 0;\n"
"};\n", 0,
K_BUILDERR, "tuple-in-union variant tag unresolved" },
/* tuple-typed IDENT source into a tagged slot: the widen
* choke-point has no word-copy arm for it — pre-#72 it fell to
* the scalar arm and SILENTLY dropped payload slot 1+; now loud
* (rule 7) until #72 wires ident/call/match-binding sources. */
{ "tuple_ident_reject",
"package main;\n"
"type lit = rune;\n"
"type rng = (u32, u32);\n"
"type item = (lit | rng);\n"
"export fn main() i32 = {\n"
" let t: rng = ((65, 90): rng);\n"
" let v: item = t;\n"
" match (v) {\n"
" case let r: rng => { if (r.0 != 65) { return 1; }; };\n"
" case => return 2;\n"
" };\n"
" return 0;\n"
"};\n", 0,
K_BUILDERR, "tuple-typed source shape unwired" },
};
/* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */
static int
run_driver(const char *driver, const struct row *r, int i)
{
char src[96], tmpdir[96], errf[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/ttw_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/ttw_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/ttw_%d_e_%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 >/dev/null 2>%s",
tmpdir, driver, src, errf);
int brc = runwait(cmd);
if (r->kind == K_BUILDERR) {
int ok = (brc != 0)
&& (r->experr == NULL || errlog_has(errf, r->experr));
if (!ok)
fprintf(stderr, "row[%s]: %s expected loud builderr "
"\"%s\" (brc=%d)\n", r->label, driver,
r->experr ? r->experr : "", brc);
unlink(src); unlink(errf); rmdir(tmpdir);
return ok ? 0 : 1;
}
if (brc != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); unlink(errf); rmdir(tmpdir);
return -1;
}
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[256];
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); unlink(errf); rmdir(tmpdir);
if (got != r->want) {
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
r->label, driver, got, r->want);
return 1;
}
return 0;
}
/* cs==ww .s byte-id (rule 10). */
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[96], cs[96], ws[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/ttw_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/ttw_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/ttw_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;
}
int rc = slurp_eq(cs, ws);
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[2080];
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 cdrv[2120], wdrv[2120];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
total++;
if (run_driver(cdrv, &rows[i], i) != 0) fail++;
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (run_driver(wdrv, &rows[i], i) != 0) fail++;
}
for (int i = 0; i < n; i++) {
if (rows[i].kind == K_BUILDERR)
continue;
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "tagged_tuple_widen: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("tagged_tuple_widen: %d/%d ok\n", total, total);
return 0;
}