/* * 826_alias_tuple_coerce — an alias of a tuple type, initialised with an * UNTYPED tuple literal, must coerce element-wise just like a direct tuple * (task #99). `type pair = (int, int); let x: pair = (3, 4)` was rejected by * cstage — `init (untyped_int, untyped_int) not assignable to declared pair` * — while the direct `let x: (int,int) = (3,4)` and the typed-alias * `let x: pair = (3:int, 4:int)` both compiled. wwstage already accepted+ran * the untyped-alias form correctly. * * ROOT (cstage only): cmd/wcc/type.c type_assignable's tuple-to-tuple arm * gated on the UN-chased `dst->kind == TY_TUPLE`. For `pair` (a TY_NAMED * alias) dst->kind is TY_NAMED, so the arm was skipped and the per-element * untyped->int coercion never ran. The fix chases TY_NAMED on both sides * before the tuple check (mirroring the #258 slice-borrow arm just below), * so an alias-of-tuple gets the same element-wise coercion as a direct tuple. * type_assignable is the shared assignability predicate, so INIT, FN-ARG and * RETURN positions all flip reject->accept in one spot. cstage-only — * wwstage check.ww/cgen are unchanged. * * The coercion still type-checks per element, so a genuinely mismatched * element type or wrong arity STILL louds — those are the negative controls. * * row | shape | want * ------------+------------------------------------------------+------ * init | type pair=(int,int); let x:pair=(3,4); x.0+x.1 | 7 * fnarg | fn g(p:pair); g((3,4)); p.0+p.1 | 7 * ret | fn f() pair = { return (3,4); }; r.0+r.1 | 7 * mixed | type box=(int,u16); let x:box=(3,4); coerce ea | 7 * ctrl_dir | direct (int,int)=(3,4) (no-regress) | 7 * ctrl_typed | alias (3:int,4:int) (no-regress) | 7 * * init / fnarg / ret / mixed are the mutation-sane rows: pre-fix cstage * build-FAILS them (the untyped->alias coercion was rejected); pre-fix * wwstage built but a two-field alias-tuple fn-arg read garbage (the param * spill half). Post-fix BOTH stages build+run the want. * * Negative controls (cstage must STILL loud — don't over-accept): * neg_type | let x:pair=(3,"s") (element type mismatch) * neg_arity | let x:pair=(3,4,5) (wrong arity) * * byte-id: the init, fnarg and ret rows are asserted byte-identical * (cstage==wwstage .s). #99 shipped in two halves — the cstage type.c chase * (init/fnarg/ret reject->accept) AND the wwstage cgendecl.ww tuple-PARAM * spill chase (the param twin: an alias param gated on the SYNTACTIC * N_TTUPLE node fell through to the scalar path, spilling one arg reg and * dropping the second tuple word, so a two-field read of an alias-tuple * fn-arg returned garbage in wwstage). With the wwstage half landed the * fnarg/ret prologues converge to cstage, so both read both fields and * match byte-for-byte. mixed (an init row) stays runtime-only. */ #include #include #include #include #include #include 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; int byteid; }; static const struct row rows[] = { /* init — THE construct: untyped tuple literal -> alias, read both * fields. byte-id clean across stages. */ { "init", "package main;\n" "type pair = (int, int);\n" "export fn main() i32 = {\n" "\tlet x: pair = (3, 4);\n" "\treturn (x.0 + x.1): i32;\n" "};\n", 7, 1 }, /* fnarg — untyped tuple literal coerced into an alias-typed param at * the call site, reading BOTH fields. The wwstage tuple-PARAM spill * chase (cgendecl.ww, #99 second half) sizes the alias param slot 16B * and spills both arg regs, so p.0+p.1 is correct and byte-identical * to cstage. */ { "fnarg", "package main;\n" "type pair = (int, int);\n" "fn g(p: pair) i32 = {\n" "\treturn (p.0 + p.1): i32;\n" "};\n" "export fn main() i32 = {\n" "\treturn g((3, 4));\n" "};\n", 7, 1 }, /* ret — untyped tuple literal coerced into the declared alias return * type; the receiving alias-tuple let reads both fields, byte-id. */ { "ret", "package main;\n" "type pair = (int, int);\n" "fn f() pair = {\n" "\treturn (3, 4);\n" "};\n" "export fn main() i32 = {\n" "\tlet r: pair = f();\n" "\treturn (r.0 + r.1): i32;\n" "};\n", 7, 1 }, /* mixed — heterogeneous element types under the alias; each untyped * element coerces to its declared field type. runtime-only. */ { "mixed", "package main;\n" "type box = (int, u16);\n" "export fn main() i32 = {\n" "\tlet x: box = (3, 4);\n" "\treturn (x.0 + (x.1: int)): i32;\n" "};\n", 7, 0 }, /* ctrl_dir — direct (un-aliased) tuple init; already worked, guards * against a regression in the original tuple path. */ { "ctrl_dir", "package main;\n" "export fn main() i32 = {\n" "\tlet x: (int, int) = (3, 4);\n" "\treturn (x.0 + x.1): i32;\n" "};\n", 7, 0 }, /* ctrl_typed — alias init with already-typed elements (no untyped * coercion); already worked via the named-arm type_eq. */ { "ctrl_typed", "package main;\n" "type pair = (int, int);\n" "export fn main() i32 = {\n" "\tlet x: pair = (3: int, 4: int);\n" "\treturn (x.0 + x.1): i32;\n" "};\n", 7, 0 }, }; /* neg — cstage must STILL reject these (the coercion type-checks per * element; a type mismatch or wrong arity is not assignable). cstage-only: * wwstage pre-existingly over-accepts a mismatched tuple init (a separate * wwstage bug, out of #99 scope), so the negative is asserted against the * cstage driver only — the side #99 touches. */ static const char *neg[] = { /* neg_type — second element str vs declared int. */ "package main;\n" "type pair = (int, int);\n" "export fn main() i32 = {\n" "\tlet x: pair = (3, \"s\");\n" "\treturn x.0: i32;\n" "};\n", /* neg_arity — three elements vs the two-element alias. */ "package main;\n" "type pair = (int, int);\n" "export fn main() i32 = {\n" "\tlet x: pair = (3, 4, 5);\n" "\treturn x.0: i32;\n" "};\n", }; 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/atc_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/atc_%d_d_%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 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; } /* build_should_fail — the negative control must error on `driver`; returns * 0 when the build correctly FAILS, non-zero when it wrongly succeeded. */ static int build_should_fail(const char *driver, const char *src, int i) { char s[64], tmpdir[64], cmd[1024]; snprintf(s, sizeof s, "/tmp/atc_neg_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/atc_neg_%d_d_%d", getpid(), i); FILE *f = fopen(s, "wb"); if (!f) return -1; fputs(src, f); fclose(f); mkdir(tmpdir, 0755); snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", tmpdir, driver, s); int rc = runwait(cmd); unlink(s); const char *base = strrchr(s, '/'); base = base ? base + 1 : s; char outbin[128]; snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); char *dot = strrchr(outbin, '.'); if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; unlink(outbin); rmdir(tmpdir); return rc == 0 ? -1 : 0; /* build must NOT succeed */ } /* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */ static int asm_byte_identical(const char *bin, const struct row *r, int i) { char src[64], cs[64], ws[64], cmd[1024]; snprintf(src, sizeof src, "/tmp/atc_asm_%d_%d.ww", getpid(), i); snprintf(cs, sizeof cs, "/tmp/atc_asm_%d_%d_c.s", getpid(), i); snprintf(ws, sizeof ws, "/tmp/atc_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; } FILE *fc = fopen(cs, "rb"); FILE *fw = fopen(ws, "rb"); int rc = 0; if (!fc || !fw) { rc = -1; } else { for (;;) { int a = fgetc(fc); int b = fgetc(fw); if (a != b) { rc = -1; break; } if (a == EOF) break; } } if (fc) fclose(fc); if (fw) fclose(fw); 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[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 nneg = (int)(sizeof neg / sizeof neg[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, "alias_tuple_coerce: 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, "alias_tuple_coerce[%s][%s]: exit=%d want=%d\n", drivers[d].name, rows[i].label, got, rows[i].want); fail++; } } } /* Negative controls — cstage must STILL reject (don't over-accept). * cstage-only: the #99 fix is cstage-side, and wwstage pre-existingly * over-accepts a mismatched tuple init (out-of-scope wwstage bug). */ for (int i = 0; i < nneg; i++) { total++; if (build_should_fail(cdrv, neg[i], i) != 0) { fprintf(stderr, "alias_tuple_coerce[cstage][neg_%d]: built (should loud)\n", i); fail++; } } /* byte-id — only rows flagged byteid (the INIT form). */ if (access(wdrv, X_OK) == 0) { for (int i = 0; i < n; i++) { if (!rows[i].byteid) continue; total++; if (asm_byte_identical(bin, &rows[i], i) != 0) fail++; } } if (fail) { fprintf(stderr, "alias_tuple_coerce: %d/%d fixtures failed\n", fail, total); return 1; } printf("alias_tuple_coerce: %d/%d ok\n", total, total); return 0; }