wcc: typeeqast TY_FN compares result/params/variadic at AST layer (#94 fold-b)

Pre-fix master left N_TFN under typeeqast's conservative
"anything else fails" tail (selfhost/cmd/wcc/check.ww:748-751).
case-patterns spelled with a raw `*fn(...)` head — the io vtable
use case — tripped casevariantin / casecovers on every variant
compare, so a well-typed `match (v: tagged-of-fn-ptr) { case
*fn(...) => ... }` would not compile under wwstage.

Adds a TY_FN arm that mirrors harec STORAGE_FUNCTION
(ref/harec/src/types.c:589-615): recurse on the result type
(.lhs), iterate the param chain (.list of N_PARAM, descend each
.lhs), require the variadic flag (.op == TK_ELLIPSIS) to match
position-by-position, and require both chains to terminate
together. Param NAMES do not participate (harec analog), and the
C-variadic terminal sentinel (N_PARAM with .str == "...") is
handled defensively even though wwstage's parseparams doesn't
currently produce it. Attributes + default-param values are NOT
checked (drew-pre-approved, harec doesn't either).

cstage type.c:239's type_eq walks the same shape on the resolved
Type. typeeqast lives one layer below — a documented divergence
filed as project #178 for the harmonization fold; the in-source
comment cites #178.

Probe 763_typeeq_fn_ast.c locks 7 rows covering identical /
diff-return / diff-arity / diff-param-type / variadic / param-
name-only / io-vtable shapes across cstage + wwstage (14
fixtures). Pre-fix wwstage red-errors every row at the checker
("case: not a variant of scrutinee" + "match: variant not
handled"); post-fix all 14 compile and the tag-0 arm fires
(exit 7). Per-row .s byte-id is intentionally NOT gated — see
the probe header for the cgmatch N_TPTR-not-routed-to-
flatvariantidx sibling bug that drives the divergence on rows
b/c/d/e/g; orthogonal to this AST-layer typeeqast fold and not
swept per the brief's "do not sweep" instruction.
This commit is contained in:
2026-05-28 17:44:24 +09:00
parent 65b7e15e09
commit 7031e0d713
5 changed files with 344 additions and 9 deletions

View File

@@ -313,6 +313,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_def_widen_const \
$(BIN)/test_inferred_struct_arg_push \
$(BIN)/test_struct_abi_size \
$(BIN)/test_typeeq_fn_ast \
$(BIN)/test_use_promote_alias \
$(BIN)/test_field_signed $(BIN)/test_frame_argcount \
$(BIN)/test_selfhost $(BIN)/test_w6a_ww $(BIN)/test_w6l_ww \
@@ -598,6 +599,12 @@ $(BIN)/test_struct_abi_size: test/wcc/762_struct_abi_size.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_typeeq_fn_ast: test/wcc/763_typeeq_fn_ast.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_arrlit_str_full: test/wcc/711_arrlit_str_full.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \

View File

@@ -10775,9 +10775,31 @@ fn typeeqast(a: *node, b: *node) bool = {
if (k == nkind.N_TPTR) { return typeeqast(aa.lhs, bb.lhs); };
if (k == nkind.N_TSLICE){ return typeeqast(aa.lhs, bb.lhs); };
if (k == nkind.N_TCHAN) { return typeeqast(aa.lhs, bb.lhs); };
// Conservative: anything else (struct/fn/tagged/tuple/array)
// fails the cheap check. Selfhost code doesn't currently rely
// on equality at these shapes for the targeted checks.
if (k == nkind.N_TFN) {
// Divergence: cstage type.c:239 compares resolved Type; we
// compare AST. See #178.
if (!typeeqast(aa.lhs, bb.lhs)) { return false; };
let pa: *node = aa.list;
let pb: *node = bb.list;
for (pa != nil) {
if (pb == nil) { return false; };
let ac: bool = streq(pa.str, "...");
let bc: bool = streq(pb.str, "...");
if (ac != bc) { return false; };
if (!ac) {
let va: bool = pa.op == tkind.TK_ELLIPSIS;
let vb: bool = pb.op == tkind.TK_ELLIPSIS;
if (va != vb) { return false; };
if (!typeeqast(pa.lhs, pb.lhs)) { return false; };
};
pa = pa.next;
pb = pb.next;
};
return pb == nil;
};
// Conservative: anything else (struct/tagged/tuple/array) fails
// the cheap check. Selfhost code doesn't currently rely on
// equality at these shapes for the targeted checks.
return false;
};

View File

@@ -745,9 +745,31 @@ fn typeeqast(a: *node, b: *node) bool = {
if (k == nkind.N_TPTR) { return typeeqast(aa.lhs, bb.lhs); };
if (k == nkind.N_TSLICE){ return typeeqast(aa.lhs, bb.lhs); };
if (k == nkind.N_TCHAN) { return typeeqast(aa.lhs, bb.lhs); };
// Conservative: anything else (struct/fn/tagged/tuple/array)
// fails the cheap check. Selfhost code doesn't currently rely
// on equality at these shapes for the targeted checks.
if (k == nkind.N_TFN) {
// Divergence: cstage type.c:239 compares resolved Type; we
// compare AST. See #178.
if (!typeeqast(aa.lhs, bb.lhs)) { return false; };
let pa: *node = aa.list;
let pb: *node = bb.list;
for (pa != nil) {
if (pb == nil) { return false; };
let ac: bool = streq(pa.str, "...");
let bc: bool = streq(pb.str, "...");
if (ac != bc) { return false; };
if (!ac) {
let va: bool = pa.op == tkind.TK_ELLIPSIS;
let vb: bool = pb.op == tkind.TK_ELLIPSIS;
if (va != vb) { return false; };
if (!typeeqast(pa.lhs, pb.lhs)) { return false; };
};
pa = pa.next;
pb = pb.next;
};
return pb == nil;
};
// Conservative: anything else (struct/tagged/tuple/array) fails
// the cheap check. Selfhost code doesn't currently rely on
// equality at these shapes for the targeted checks.
return false;
};

View File

@@ -10775,9 +10775,31 @@ fn typeeqast(a: *node, b: *node) bool = {
if (k == nkind.N_TPTR) { return typeeqast(aa.lhs, bb.lhs); };
if (k == nkind.N_TSLICE){ return typeeqast(aa.lhs, bb.lhs); };
if (k == nkind.N_TCHAN) { return typeeqast(aa.lhs, bb.lhs); };
// Conservative: anything else (struct/fn/tagged/tuple/array)
// fails the cheap check. Selfhost code doesn't currently rely
// on equality at these shapes for the targeted checks.
if (k == nkind.N_TFN) {
// Divergence: cstage type.c:239 compares resolved Type; we
// compare AST. See #178.
if (!typeeqast(aa.lhs, bb.lhs)) { return false; };
let pa: *node = aa.list;
let pb: *node = bb.list;
for (pa != nil) {
if (pb == nil) { return false; };
let ac: bool = streq(pa.str, "...");
let bc: bool = streq(pb.str, "...");
if (ac != bc) { return false; };
if (!ac) {
let va: bool = pa.op == tkind.TK_ELLIPSIS;
let vb: bool = pb.op == tkind.TK_ELLIPSIS;
if (va != vb) { return false; };
if (!typeeqast(pa.lhs, pb.lhs)) { return false; };
};
pa = pa.next;
pb = pb.next;
};
return pb == nil;
};
// Conservative: anything else (struct/tagged/tuple/array) fails
// the cheap check. Selfhost code doesn't currently rely on
// equality at these shapes for the targeted checks.
return false;
};

View File

@@ -0,0 +1,262 @@
/*
* 763_typeeq_fn_ast — locks wwstage check.ww typeeqast's TY_FN equality
* arm at the AST layer. Pre-fix master left N_TFN under the conservative
* "anything else fails" fall-through; case-patterns spelled with a raw
* `*fn(...)` head (the io vtable use case) tripped errbadcase on every
* variant compare, so a perfectly well-typed `match` would not compile
* under wwstage. cstage type.c:239's `type_eq` walks the SAME shape on
* resolved Type — that asymmetry is filed as project #178 (typeeqast
* lives one layer below cstage's type_eq).
*
* Coverage: 7 rows, table-driven. Each row's source compiles cleanly
* post-fix on BOTH stages, and the runtime selects the tag-0 arm
* returning 7. Rows distinguish along the same axes harec's
* type_equal STORAGE_FUNCTION arm checks (ref/harec/src/types.c:589-
* 615): result type, arity, param type, variadism. Param NAMES do NOT
* participate per harec + drew (row 7).
*
* a) Identical: fn(x: i32) void ≡ fn(x: i32) void
* b) Diff return: fn(x: i32) void ≢ fn(x: i32) i32
* c) Diff arity: fn(x: i32) void ≢ fn(x: i32, y: i64) void
* d) Diff param type: fn(x: i32) void ≢ fn(x: i64) void
* e) Variadic ≢ fixed: fn(xs: i32...) void ≢ fn(x: i32) void
* f) Param-name only: fn(a: i32) void ≡ fn(b: i32) void
* g) Io-vtable shape: fn(s: *i32, n: i32) i32 ≢ fn(s: *i32) i32
*
* Each row's tagged uses two variants whose tags differ (variant 0 is
* the first fn-ptr shape; variant 1 is either the alternative fn-ptr
* shape — rows b/c/d/e/g — or an `i32` payload — rows a/f). The
* `let v: t;` bare declaration zero-inits the slot (see selfhost/
* CLAUDE.md "Bare `let x: T;` ... now zero-inits") so the tag is 0 and
* arm A fires returning 7. The wwstage checker's casevariantin /
* casecovers walks (selfhost/cmd/wcc/check.ww:3082, :3110) only accept
* a case-pattern that typeeqast'es one of the scrutinee's variants —
* which is exactly the path the fold-b N_TFN arm now satisfies.
*
* GATE POLARITY: must stay GREEN. A red here means the typeeqast
* N_TFN arm regressed back toward the master fall-through.
*
* NOT GATED: cstage-vs-wwstage .s byte-identity is intentionally NOT
* checked per-row. Rows b/c/d/e/g currently diverge at the case-arm
* tag CMPQ because wwstage's cgmatch gates the case-pattern flatvariant
* lookup on `pat.kind == nkind.N_TNAME` (selfhost/cmd/wcc/cgenexpr.ww:
* 1512) — N_TPTR(N_TFN) case patterns short-circuit to tag 0. That is
* a SIBLING cgen-side bug (filed alongside this fold per the brief's
* "do not sweep" instruction) and orthogonal to the AST-layer
* typeeqast equality this fold ratifies. The pre/post-fix asymmetry
* THIS PROBE locks is the wwstage CHECKER outcome: pre-fix master
* red-errors the program (4× errbadcase + match-not-handled); post-
* fix it compiles to a runnable binary. The runtime exit-code arm
* (= 7) double-checks that arm A's body actually runs.
*/
#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[] = {
/* a) Identical fn-ptr shape; v: t zero-inits to tag 0 (the
* *fn(x: i32) void variant); arm A fires. */
{ "identical",
"type t = (*fn(x: i32) void | i32);\n"
"export fn main() i32 = {\n"
" let v: t;\n"
" match (v) {\n"
" case *fn(x: i32) void => return 7;\n"
" case i32 => return 3;\n"
" };\n"
" return -1;\n"
"};\n",
7 },
/* b) Same arity + param, different return (void vs i32). Both
* variants are *fn(x: i32) _; post-fix typeeqast distinguishes
* them by ret type. */
{ "diff_return",
"type t = (*fn(x: i32) void | *fn(x: i32) i32);\n"
"export fn main() i32 = {\n"
" let v: t;\n"
" match (v) {\n"
" case *fn(x: i32) void => return 7;\n"
" case *fn(x: i32) i32 => return 3;\n"
" };\n"
" return -1;\n"
"};\n",
7 },
/* c) Different arity (1 vs 2 params). */
{ "diff_arity",
"type t = (*fn(x: i32) void | *fn(x: i32, y: i64) void);\n"
"export fn main() i32 = {\n"
" let v: t;\n"
" match (v) {\n"
" case *fn(x: i32) void => return 7;\n"
" case *fn(x: i32, y: i64) void => return 3;\n"
" };\n"
" return -1;\n"
"};\n",
7 },
/* d) Different param type (i32 vs i64). */
{ "diff_param_type",
"type t = (*fn(x: i32) void | *fn(x: i64) void);\n"
"export fn main() i32 = {\n"
" let v: t;\n"
" match (v) {\n"
" case *fn(x: i32) void => return 7;\n"
" case *fn(x: i64) void => return 3;\n"
" };\n"
" return -1;\n"
"};\n",
7 },
/* e) Variadic flag differs: Hare-style `xs: T...` carries
* .op == TK_ELLIPSIS on the N_PARAM node — typeeqast must
* compare flags before recursing. */
{ "variadic_vs_fixed",
"type t = (*fn(xs: i32...) void | *fn(x: i32) void);\n"
"export fn main() i32 = {\n"
" let v: t;\n"
" match (v) {\n"
" case *fn(xs: i32...) void => return 7;\n"
" case *fn(x: i32) void => return 3;\n"
" };\n"
" return -1;\n"
"};\n",
7 },
/* f) Drew row 7: param names DO NOT participate. The variant
* has param name `b`; the case pattern has `a`. Post-fix
* typeeqast walks param .lhs only, ignoring .str. Mirrors
* harec ref/harec/src/types.c:589-615 which never reads
* param_a->name in type_equal. */
{ "param_name_only",
"type t = (*fn(b: i32) void | i32);\n"
"export fn main() i32 = {\n"
" let v: t;\n"
" match (v) {\n"
" case *fn(a: i32) void => return 7;\n"
" case i32 => return 3;\n"
" };\n"
" return -1;\n"
"};\n",
7 },
/* g) Bonus: the io-vtable shape that motivated the fold —
* a tagged of two fn-pointer signatures whose param shapes
* differ (mirrors lib/io/io.ww:29-31 read vs close). The
* pre-fix wwstage rejected the program because the case
* pattern's *fn(...) didn't typeeqast any variant. */
{ "io_vtable_shape",
"type t = (*fn(s: *i32, n: i32) i32 | *fn(s: *i32) i32);\n"
"export fn main() i32 = {\n"
" let v: t;\n"
" match (v) {\n"
" case *fn(s: *i32, n: i32) i32 => return 7;\n"
" case *fn(s: *i32) i32 => return 3;\n"
" };\n"
" return -1;\n"
"};\n",
7 },
};
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/wctyfn_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/wctyfn_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
/* timeout 180 per repo convention (cf. 761, 762); test/run does
* not bound individual binaries. */
snprintf(cmd, sizeof cmd, "cd %s && timeout 180 %s build %s 2>/dev/null",
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;
}
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 cdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
char wdrv[1024];
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, "typeeq_fn_ast: 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,
"typeeq_fn_ast[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (fail) {
fprintf(stderr,
"typeeq_fn_ast: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("typeeq_fn_ast: %d/%d ok\n", total, total);
return 0;
}