The ?/! success-is-str decision was name-keyed off the FIRST variant and only handled call operands — an ident operand with junk registers unwrapped garbage, and error-first unions picked the wrong variant. Key on the stamped success variant (successvariant + typeisstr, mirror cgen.c:10459-10466/10595-10602). Review item #16.
169 lines
4.9 KiB
C
169 lines
4.9 KiB
C
/*
|
|
* 989_trystr_run (#16) — the `?`/`!` unwrap of a str-success tagged union
|
|
* must shuffle the str header (DX,CX,R8) → (AX,BX,CX) for ANY operand shape
|
|
* and ANY variant order, keyed off the STAMPED operand type.
|
|
*
|
|
* THE BUG (wwstage only): cgtryprop/cgtryunw computed succisstr ONLY when the
|
|
* operand was an N_CALL, via a name-keyed fnretlookupmod on the callee leaf,
|
|
* and tested the FIRST variant (not the success variant). So an ident-source
|
|
* unwrap (`let r = mk(); r!`) and an error-first union (`(e|str)`) both
|
|
* dropped the `MOVQ CX,BX / MOVQ R8,CX` shuffle — the unwrapped str kept a
|
|
* stale len/cap. cstage reads success_is_str = type_isstr(succ_t) at
|
|
* cg_tagged_success_tag from the stamped operand type. THE FIX: read the
|
|
* stamped operand's success-variant type via successvariant()+typeisstr().
|
|
*
|
|
* row | shape | exit (cs==ww)
|
|
* ----------------+----------------------------------------+--------------
|
|
* ident_source | let r=mk(); junk; r! → s.len | 2 (was ww 40)
|
|
* errfirst_call | (e|str) mk2()! → s.len | 3
|
|
* succfirst_call | (str|e) mk()! (control) | 4
|
|
* A 40-byte `junk` str precedes the unwrap so a dropped shuffle leaks junk's
|
|
* len into s.len: pre-fix ident_source ran ww=40 vs cs=2 (silent overrun).
|
|
*/
|
|
#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[] = {
|
|
{ "ident_source",
|
|
"package main;\n"
|
|
"type e = !i32;\n"
|
|
"fn mk() (str | e) = { return \"hi\"; };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let r: (str | e) = mk();\n"
|
|
" let junk: str = \"0123456789012345678901234567890123456789\";\n"
|
|
" let s: str = r!;\n"
|
|
" return s.len: i32;\n"
|
|
"};\n",
|
|
2 },
|
|
|
|
{ "errfirst_call",
|
|
"package main;\n"
|
|
"type e = !i32;\n"
|
|
"fn mk2() (e | str) = { return \"abc\"; };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let junk: str = \"0123456789012345678901234567890123456789\";\n"
|
|
" let s3: str = mk2()!;\n"
|
|
" return s3.len: i32;\n"
|
|
"};\n",
|
|
3 },
|
|
|
|
{ "succfirst_call",
|
|
"package main;\n"
|
|
"type e = !i32;\n"
|
|
"fn mk() (str | e) = { return \"wxyz\"; };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let junk: str = \"0123456789012345678901234567890123456789\";\n"
|
|
" let s: str = mk()!;\n"
|
|
" return s.len: i32;\n"
|
|
"};\n",
|
|
4 },
|
|
};
|
|
|
|
static int
|
|
run_build(const char *driver, const struct row *r, int i)
|
|
{
|
|
char src[64], tmpdir[64], cmd[1024];
|
|
snprintf(src, sizeof src, "/tmp/trystr_%d_%d.ww", getpid(), i);
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/trystr_%d_d_%d", getpid(), i);
|
|
|
|
FILE *f = fopen(src, "wb");
|
|
if (!f) return -2;
|
|
fputs(r->src, f);
|
|
fclose(f);
|
|
|
|
mkdir(tmpdir, 0755);
|
|
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
|
|
tmpdir, driver, src);
|
|
int brc = runwait(cmd);
|
|
|
|
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 = -1;
|
|
if (brc == 0) got = runwait(outbin);
|
|
|
|
unlink(src); unlink(outbin); rmdir(tmpdir);
|
|
return brc == 0 ? got : -1;
|
|
}
|
|
|
|
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], wdrv[1024];
|
|
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
|
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
|
int have_ww = (access(wdrv, X_OK) == 0);
|
|
|
|
int n = (int)(sizeof rows / sizeof rows[0]);
|
|
int total = 0, fail = 0;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
total++;
|
|
int gc = run_build(cdrv, &rows[i], i);
|
|
if (gc < 0) {
|
|
fprintf(stderr, "trystr[cstage][%s]: build/run failed "
|
|
"(got %d)\n", rows[i].label, gc);
|
|
fail++;
|
|
continue;
|
|
}
|
|
if (gc != rows[i].want_exit) {
|
|
fprintf(stderr, "trystr[cstage][%s]: exit=%d want=%d\n",
|
|
rows[i].label, gc, rows[i].want_exit);
|
|
fail++;
|
|
}
|
|
if (!have_ww) {
|
|
fprintf(stderr, "trystr: skip wwstage (no %s)\n", wdrv);
|
|
continue;
|
|
}
|
|
int gw = run_build(wdrv, &rows[i], i);
|
|
if (gw != gc) {
|
|
fprintf(stderr, "trystr[%s]: cs=%d != ww=%d "
|
|
"(str-success unwrap shuffle dropped — #16)\n",
|
|
rows[i].label, gc, gw);
|
|
fail++;
|
|
}
|
|
if (gw != rows[i].want_exit) {
|
|
fprintf(stderr, "trystr[wwstage][%s]: exit=%d want=%d\n",
|
|
rows[i].label, gw, rows[i].want_exit);
|
|
fail++;
|
|
}
|
|
}
|
|
|
|
if (fail) {
|
|
fprintf(stderr, "trystr_run: %d/%d checks failed\n",
|
|
fail, total);
|
|
return 1;
|
|
}
|
|
printf("trystr_run: %d/%d ok\n", total, total);
|
|
return 0;
|
|
}
|