Files
ww/test/wcc/944_variant_chain_b95_run.c
Hojun-Cho 56aac85f6f cgen: #95 c2 structural variant fallback — both-stage fused
A nominally-unrelated, structurally-equal NAMED source into a NAMED
variant (kb95_unrel: ta/tb same-layout structs, src ta -> (void|tb))
was LIVE both-wrong-identical byte-id silent: both checkers accept,
both cgens tagged 0. After c1's chain arm finds no shared chain
node, match the variant whose CHASED type type_eq's/typeeq's the
source's chased bottom — chased type EQUALITY only, no
type_is_assignable scalar import, no int widening (ken's binding
scalar warning). Same NAMED-source branch, both stages
(cg_tag_for_variant / flatvariantidxt), forced fuse.

Correctness reference, cite 1 — harec tagged_select_subtype P2+P3
(ref/harec/src/types.c:702-739), verbatim:

	if (t->id == subtype->id) {
		return t;
	}
	if (type_is_assignable(ctx, t, subtype)) {
		selected = t;
		++nassign;
	}
	...
	if (nassign == 1) {
		return selected;
	}
	return NULL;

with type_is_assignable's non-tagged path dealiasing both sides and
accepting composites only via interned pointer equality
(types.c:988-1002), verbatim:

	if (type_dealias(ctx, to)->storage != STORAGE_TAGGED) {
		to = type_dealias(ctx, to);
		from = type_dealias(ctx, from);
	}
	...
	if (to == from && to->storage != STORAGE_VOID) {
		return true;
	}

Cite 2 — type_hash interns bare composites STRUCTURALLY (banked as
types.c:72-81; verified in the vendored copy at types.c:444 +
struct/union arm :514-525), verbatim:

	case STORAGE_UNION:
		hash = fnv1a_size(hash, type->struct_union.packed);
		for (const struct struct_field *field = type->struct_union.fields;
				field; field = field->next) {
			if (field->name) {
				hash = fnv1a_s(hash, field->name);
			}
			hash = fnv1a_u32(hash, type_hash(field->type));
			hash = fnv1a_size(hash, field->offset);
		}

— no decl ident in the hash, so harec's two decls dealias to ONE
interned node and `to == from` holds: acceptance is DEFINITIONAL
under interning, not an arm whose text could be misread. Our store
does not intern; chased type equality is the non-interned rendering
of the same rule.

Honest divergence (the >=2-structural-match hard-error STAYS): under
harec's interning two structurally-identical variants are ONE type —
a union cannot contain it twice — so the ambiguity case is
unrepresentable there; our hard-error (twin texts, shared tail
"source structurally matches >=2 variants — ambiguous without
nominal layout (#95)") is the correct nominal-lossy-model rendering,
not a harec deviation.

Pin table: unrel_struct row added (kb95_unrel graduates ok/1-ok/1 ->
0/0, byte-id held) — suite now 48/48. All c1 rows unmoved.

Invariants: 163-row dissolution matrix at tip — same 3 family
graduations as c1, ZERO new movers; five mains cs-vs-ww byte-id OK;
make all 0; sizelint 0; peellint 0 (no new peel sites — the
structural leg reads only chased ends); all 944 suites + 808 green.
w6c_ww/wwdump_ww main.combined.ww regen'd.
2026-06-06 07:43:39 +09:00

515 lines
17 KiB
C

/*
* 944_variant_chain_b95_run — #95: variant-match chain-membership arm
* (pass 1b) + structural fallback (c2), BOTH stages fused (cstage
* cg_tag_for_variant + wwstage flatvariantidxt are the same route).
* Pre-#95 a NAMED struct source that was not pointer-identical to a
* NAMED variant fell through every pass and the widen stored tag 0 —
* both stages, byte-identical, gate-blind (the worst class on the
* board: silent wrong tag on VALID code).
*
* Row sources are ken's #95 first-position oracle
* (.ai/ken-95-oracle.md, probes /tmp/b95/src + kb4/kb5) and rob's
* fold spec (.ai/rob-95-spec.md). Reference: harec
* tagged_select_subtype P1/P2/P3 (ref/harec/src/types.c:702-739,
* :988-1140).
*
* row | shape (oracle row) | cs/ww
* --------------------+-------------------------------------+------
* chain_1lvl_i | kb5_v2s1i HEADLINE: depth-1 ident |
* | src base → (void|al0) — was both- |
* | wrong-IDENTICAL byte-id tag 0 | 0/0
* chain_2lvl_i | kb95_2lvl_i: ident src depth-2 — |
* | the class extends to any depth | 0/0
* chain_deep_src | kb95_deep_src: src ali2 → (void| |
* | ali) — chain-direction DOWN | 0/0
* chain_deep_var | kb95_deep_var: src base → (void| |
* | ali2) — chain-direction UP | 0/0
* chain_call_bound81 | kb5_v2s1: CALL src 1-lvl — runs |
* | 0/0; byte-id waived: pre-existing |
* | #81 uninit-local zero-fill asm |
* | noise (NO at the #95 base too) | 0/0
* chain_call2_bound81 | kb4_v2_struct2 (#95's original): |
* | CALL src 2-lvl — same #81 waiver | 0/0
* chain_amb_loud | kb95_amb: (ali|ali2) over one base, |
* | src base — TODAY silently tagged |
* | member 0; ≥2 chain hits cannot be |
* | disambiguated in the nominal-lossy |
* | model (harec nassign≥2 → NULL) — |
* | hard-error, twin texts share the |
* | experr tail | err/err
* chain_amb_srcA/B | src ali / src ali2 into (ali|ali2): |
* | pass-1 EXACT fires first (harec P1 |
* | short-circuit) — precedence pins, |
* | MUST NOT MOVE | 0/0
* nom_str / nom_err | (str|linerr) #218 nominal pins: str |
* | src tags str, linerr src tags |
* | linerr — MUST NOT MOVE through the |
* | new arm | 0/0
* exact_ctl | kb5_v2sE2: exact-match ident |
* | control — MUST NOT MOVE | 0/0
* bare_ctl/bare_2lvl | kb4_v2_ctrl / kb4_v2_alias2: pass-2 |
* | bare-source controls — MUST NOT |
* | MOVE | 0/0
* bare_ambig/2 | kb4_v2_ambig(2): pass-2 ambiguity |
* | guards hold their loud texts | err/err
* callret_bound277 | kb5_v2sE: CALL src typed-as-alias — |
* | ww #277 aggregate-return capability |
* | leg, OUT of #95; cells pinned as |
* | observed (cs runs, ww loud) | 0/err
* unrel_struct | kb95_unrel (c2): ta/tb nominally- |
* | unrelated same-layout structs, src |
* | ta → (void|tb) — was both-wrong- |
* | identical byte-id; harec interning |
* | makes dealias(ta)==dealias(tb) → |
* | accept, tag tb (types.c:72-81 + |
* | :1000-1002) | 0/0
*
* K_RUN rows build+run BOTH drivers (exit==want) and assert cstage/
* wwstage asm byte-id. K_RUN_NOID waives byte-id — cite the blocking
* task at the row. K_RUN_CS_WWERR: cstage build+run exit==cswant AND
* wwstage build FAILS with experr (944_alias_accept_run kind). NNN<950,
* self-contained /tmp sources, no imports (944 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, exits==wants, + byte-id */
#define K_BUILDERR 1 /* build FAILS with experr on BOTH drivers */
#define K_RUN_NOID 2 /* build+run BOTH, exits==wants, byte-id
* SKIPPED — cite the blocking task */
#define K_RUN_CS_WWERR 3 /* bound row: cstage build+run exit==cswant;
* wwstage build FAILS with experr — cite
* the blocking task */
struct row { const char *label; const char *src;
int cswant; int wwwant; int kind; const char *experr; };
/* errlog_has — a builderr cell must fail WITH its diagnostic; any other
* failure (parse error, crash, hang-kill) 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[] = {
/* ---- c1: chain-membership graduations (were tag-0 silent) */
{ "chain_1lvl_i",
"package main;\n"
"type base = struct { a: int, b: int };\n"
"type al0 = base;\n"
"export fn main() i32 = {\n"
" let s: base;\n"
" s.a = 4; s.b = 9;\n"
" let v: (void | al0) = s;\n"
" if (!(v is al0)) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL },
{ "chain_2lvl_i",
"package main;\n"
"type base = struct { a: int, b: int };\n"
"type al0 = base;\n"
"type ali = al0;\n"
"export fn main() i32 = {\n"
" let s: base;\n"
" s.a = 4; s.b = 9;\n"
" let v: (void | ali) = s;\n"
" if (!(v is ali)) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL },
{ "chain_deep_src",
"package main;\n"
"type base = struct { a: int, b: int };\n"
"type ali = base;\n"
"type ali2 = ali;\n"
"export fn main() i32 = {\n"
" let s: ali2;\n"
" s.a = 4; s.b = 9;\n"
" let v: (void | ali) = s;\n"
" if (!(v is ali)) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL },
{ "chain_deep_var",
"package main;\n"
"type base = struct { a: int, b: int };\n"
"type ali = base;\n"
"type ali2 = ali;\n"
"export fn main() i32 = {\n"
" let s: base;\n"
" s.a = 4; s.b = 9;\n"
" let v: (void | ali2) = s;\n"
" if (!(v is ali2)) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL },
/* CALL-src legs run 0/0; byte-id waived: the mk() frame carries
* the pre-existing #81 uninit-local zero-fill asm divergence
* (cells were byteid=NO at the #95 base too). */
{ "chain_call_bound81",
"package main;\n"
"type base = struct { a: int, b: int };\n"
"type al0 = base;\n"
"fn mk() base = {\n"
" let s: base;\n"
" s.a = 4; s.b = 9;\n"
" return s;\n"
"};\n"
"export fn main() i32 = {\n"
" let v: (void | al0) = mk();\n"
" if (!(v is al0)) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN_NOID, NULL }, /* byte-id: task #81 */
{ "chain_call2_bound81",
"package main;\n"
"type base = struct { a: int, b: int };\n"
"type al0 = base;\n"
"type ali = al0;\n"
"fn mk() base = {\n"
" let s: base;\n"
" s.a = 4; s.b = 9;\n"
" return s;\n"
"};\n"
"export fn main() i32 = {\n"
" let v: (void | ali) = mk();\n"
" if (!(v is ali)) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN_NOID, NULL }, /* byte-id: task #81 */
/* ≥2 chain hits — was a SILENT member-0 tag; twin texts share
* the experr tail (prefix convention). */
{ "chain_amb_loud",
"package main;\n"
"type base = struct { a: int, b: int };\n"
"type ali = base;\n"
"type ali2 = ali;\n"
"export fn main() i32 = {\n"
" let s: base;\n"
" s.a = 4; s.b = 9;\n"
" let v: (ali | ali2) = s;\n"
" if (v is ali) { return 1; };\n"
" if (v is ali2) { return 2; };\n"
" return 3;\n"
"};\n", 0, 0, K_BUILDERR,
"source alias chain reaches >=2 variants" },
/* pass-1 EXACT precedence pins: an exact source distinguishes
* (ali|ali2) — the chain arm must stay BEHIND pass 1. */
{ "chain_amb_srcA",
"package main;\n"
"type base = struct { a: int, b: int };\n"
"type ali = base;\n"
"type ali2 = ali;\n"
"export fn main() i32 = {\n"
" let s: ali;\n"
" s.a = 4; s.b = 9;\n"
" let v: (ali | ali2) = s;\n"
" if (!(v is ali)) { return 1; };\n"
" if (v is ali2) { return 2; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL },
{ "chain_amb_srcB",
"package main;\n"
"type base = struct { a: int, b: int };\n"
"type ali = base;\n"
"type ali2 = ali;\n"
"export fn main() i32 = {\n"
" let s: ali2;\n"
" s.a = 4; s.b = 9;\n"
" let v: (ali | ali2) = s;\n"
" if (!(v is ali2)) { return 1; };\n"
" if (v is ali) { return 2; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL },
/* #218 nominal regression pins (rob §3e): (str | linerr) stays
* distinguishable through the new arm. */
{ "nom_str",
"package main;\n"
"type linerr = str;\n"
"export fn main() i32 = {\n"
" let s: str = \"ok\";\n"
" let v: (str | linerr) = s;\n"
" if (!(v is str)) { return 1; };\n"
" if (v is linerr) { return 2; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL },
{ "nom_err",
"package main;\n"
"type linerr = str;\n"
"export fn main() i32 = {\n"
" let e: linerr = \"bad\";\n"
" let v: (str | linerr) = e;\n"
" if (!(v is linerr)) { return 1; };\n"
" if (v is str) { return 2; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL },
{ "exact_ctl",
"package main;\n"
"type base = struct { a: int, b: int };\n"
"type al0 = base;\n"
"type ali = al0;\n"
"export fn main() i32 = {\n"
" let s: ali;\n"
" s.a = 4; s.b = 9;\n"
" let v: (void | ali) = s;\n"
" if (!(v is ali)) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL },
/* ---- pass-2 bare-source controls (the #15 leg stays put) */
{ "bare_ctl",
"package main;\n"
"type vt = struct { a: int };\n"
"type stream = *vt;\n"
"export fn main() i32 = {\n"
" let x: vt;\n"
" x.a = 7;\n"
" let v: (void | stream) = &x;\n"
" if (!(v is stream)) { return 1; };\n"
" match (v) {\n"
" case let s: stream => { if (s.a != 7) { return 2; }; };\n"
" case void => { return 3; };\n"
" };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL },
{ "bare_2lvl",
"package main;\n"
"type vt = struct { a: int };\n"
"type st0 = *vt;\n"
"type stream = st0;\n"
"export fn main() i32 = {\n"
" let x: vt;\n"
" x.a = 7;\n"
" let v: (void | stream) = &x;\n"
" if (!(v is stream)) { return 1; };\n"
" match (v) {\n"
" case let s: stream => { if (s.a != 7) { return 2; }; };\n"
" case void => { return 3; };\n"
" };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL },
{ "bare_ambig",
"package main;\n"
"type p1 = *i32;\n"
"type p2 = *i32;\n"
"export fn main() i32 = {\n"
" let x: i32 = 7;\n"
" let v: (p1 | p2) = &x;\n"
" return 0;\n"
"};\n", 0, 0, K_BUILDERR,
"bare source structurally matches >=2 NAMED variants" },
{ "bare_ambig2",
"package main;\n"
"type b1 = *i32;\n"
"type b2 = *i32;\n"
"type q1 = b1;\n"
"type q2 = b2;\n"
"export fn main() i32 = {\n"
" let x: i32 = 7;\n"
" let v: (q1 | q2) = &x;\n"
" return 0;\n"
"};\n", 0, 0, K_BUILDERR,
"bare source structurally matches >=2 NAMED variants" },
/* ww #277 aggregate-return capability leg — OUT of #95; dual
* cells pinned as observed so neither side drifts silently. */
{ "callret_bound277",
"package main;\n"
"type base = struct { a: int, b: int };\n"
"type al0 = base;\n"
"type ali = al0;\n"
"fn mk() ali = {\n"
" let s: ali;\n"
" s.a = 4; s.b = 9;\n"
" return s;\n"
"};\n"
"export fn main() i32 = {\n"
" let v: (void | ali) = mk();\n"
" if (!(v is ali)) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN_CS_WWERR,
"deferred #277" }, /* ww: task #277 */
/* ---- c2: structural fallback — was both-wrong-identical
* byte-id silent (live metric-1: checkers accept, cgen tagged
* 0). harec accepts by interning (one node, tag tb). */
{ "unrel_struct",
"package main;\n"
"type ta = struct { a: int, b: int };\n"
"type tb = struct { a: int, b: int };\n"
"export fn main() i32 = {\n"
" let s: ta;\n"
" s.a = 4; s.b = 9;\n"
" let v: (void | tb) = s;\n"
" if (!(v is tb)) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL },
};
static int
run_driver(const char *driver, const struct row *r, int i, int expect_err,
int want)
{
char src[96], tmpdir[96], errf[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/vc95_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/vc95_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/vc95_%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 && timeout 20 %s build %s >/dev/null 2>%s",
tmpdir, driver, src, errf);
int brc = runwait(cmd);
if (expect_err) {
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 != want) {
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
r->label, driver, got, want);
return 1;
}
return 0;
}
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/vc95_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/vc95_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/vc95_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++;
int cs_err = rows[i].kind == K_BUILDERR;
if (run_driver(cdrv, &rows[i], i, cs_err,
rows[i].cswant) != 0) fail++;
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
int ww_err = rows[i].kind == K_BUILDERR
|| rows[i].kind == K_RUN_CS_WWERR;
if (run_driver(wdrv, &rows[i], i, ww_err,
rows[i].wwwant) != 0) fail++;
}
for (int i = 0; i < n; i++) {
if (rows[i].kind != K_RUN)
continue;
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "variant_chain_b95: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("variant_chain_b95: %d/%d ok\n", total, total);
return 0;
}