wcc/ww: try-unwrap str success reads the stamped operand type
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.
This commit is contained in:
12
Makefile
12
Makefile
@@ -255,6 +255,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
||||
$(BIN)/test_tagtupfieldsize_run \
|
||||
$(BIN)/test_arrlit_tail_zero_run \
|
||||
$(BIN)/test_defdim_slice_run \
|
||||
$(BIN)/test_trystr_run \
|
||||
$(BIN)/test_gunsigned_run \
|
||||
$(BIN)/test_taggedidx_run \
|
||||
$(BIN)/test_fnptrcollide_run \
|
||||
@@ -734,6 +735,17 @@ $(BIN)/test_defdim_slice_run: test/wcc/989_defdim_slice_run.c \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
# 989_trystr_run (#16): `?`/`!` unwrap of a str-success tagged union shuffles
|
||||
# the str header for ANY operand shape + variant order, keyed off the stamped
|
||||
# operand type (not a name-keyed call-only first-variant lookup). Builds+runs
|
||||
# on BOTH driver twins (rule-10).
|
||||
$(BIN)/test_trystr_run: test/wcc/989_trystr_run.c \
|
||||
$(BIN)/ww $(BIN)/ww_ww \
|
||||
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
||||
$(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
# 989_gunsigned_run (F7-c5, #25): a module-global unsigned ident on the
|
||||
# divide/shift/relational path must pick the unsigned opcode. Builds+runs on
|
||||
# BOTH driver twins (rule-10). CLASS-M — see the test header.
|
||||
|
||||
@@ -23738,42 +23738,13 @@ fn cgtryprop(c: *cgen, n: *node) void = {
|
||||
// the codegen expects the success value in AX (and BX for str).
|
||||
// AX=tag, DX=val0, CX=val1 from the call ABI. For str success,
|
||||
// shuffle (DX,CX) → (AX,BX); else move DX → AX.
|
||||
// #16: read the STAMPED operand's success-variant type (any source
|
||||
// shape, any variant order), not a name-keyed call-only lookup of
|
||||
// the first variant. Mirrors cstage cgen.c:10459-10466
|
||||
// (success_is_str = type_isstr(succ_t at cg_tagged_success_tag)).
|
||||
let succisstr: bool = false;
|
||||
if (n.lhs != nil) {
|
||||
if (n.lhs.kind == nkind.N_CALL) {
|
||||
let callee: *node = n.lhs.lhs;
|
||||
if (callee != nil) {
|
||||
let cname: str;
|
||||
cname.ptr = nil; cname.len = 0;
|
||||
let cmod: str;
|
||||
cmod.ptr = nil; cmod.len = 0;
|
||||
if (callee.kind == nkind.N_IDENT) {
|
||||
cname = callee.str;
|
||||
cmod = c.curmod;
|
||||
};
|
||||
if (callee.kind == nkind.N_DOT) {
|
||||
cname = callee.str;
|
||||
if (callee.lhs != nil) {
|
||||
if (callee.lhs.kind == nkind.N_IDENT) {
|
||||
cmod = callee.lhs.str;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (cname.len > 0) {
|
||||
let rtyp: *node = fnretlookupmod(c, cname, cmod);
|
||||
if (rtyp != nil) {
|
||||
if (rtyp.kind == nkind.N_TTAGGED) {
|
||||
let first: *node = rtyp.list;
|
||||
if (first != nil) {
|
||||
if (isstrtype(c, first)) {
|
||||
succisstr = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
succisstr = typeisstr(successvariant(n.lhs.type_: *tinfo));
|
||||
};
|
||||
if (succisstr) {
|
||||
// str IS []u8: success arrives DX=ptr, CX=len, R8=cap
|
||||
@@ -23836,42 +23807,11 @@ fn cgtryunw(c: *cgen, n: *node) void = {
|
||||
if (cgtrytupleshift(c, n)) { return; };
|
||||
if (cgtrytaggedshift(c, n)) { return; };
|
||||
// Unwrap success value. (Same shuffle pattern as cgtryprop.)
|
||||
// #16: stamped success-variant type, any source/order (twin of
|
||||
// cgtryprop; cstage cgen.c:10595-10602).
|
||||
let succisstr: bool = false;
|
||||
if (n.lhs != nil) {
|
||||
if (n.lhs.kind == nkind.N_CALL) {
|
||||
let callee: *node = n.lhs.lhs;
|
||||
if (callee != nil) {
|
||||
let cname: str;
|
||||
cname.ptr = nil; cname.len = 0;
|
||||
let cmod: str;
|
||||
cmod.ptr = nil; cmod.len = 0;
|
||||
if (callee.kind == nkind.N_IDENT) {
|
||||
cname = callee.str;
|
||||
cmod = c.curmod;
|
||||
};
|
||||
if (callee.kind == nkind.N_DOT) {
|
||||
cname = callee.str;
|
||||
if (callee.lhs != nil) {
|
||||
if (callee.lhs.kind == nkind.N_IDENT) {
|
||||
cmod = callee.lhs.str;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (cname.len > 0) {
|
||||
let rtyp: *node = fnretlookupmod(c, cname, cmod);
|
||||
if (rtyp != nil) {
|
||||
if (rtyp.kind == nkind.N_TTAGGED) {
|
||||
let first: *node = rtyp.list;
|
||||
if (first != nil) {
|
||||
if (isstrtype(c, first)) {
|
||||
succisstr = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
succisstr = typeisstr(successvariant(n.lhs.type_: *tinfo));
|
||||
};
|
||||
if (succisstr) {
|
||||
// str IS []u8: success arrives DX=ptr, CX=len, R8=cap
|
||||
|
||||
@@ -518,42 +518,13 @@ fn cgtryprop(c: *cgen, n: *node) void = {
|
||||
// the codegen expects the success value in AX (and BX for str).
|
||||
// AX=tag, DX=val0, CX=val1 from the call ABI. For str success,
|
||||
// shuffle (DX,CX) → (AX,BX); else move DX → AX.
|
||||
// #16: read the STAMPED operand's success-variant type (any source
|
||||
// shape, any variant order), not a name-keyed call-only lookup of
|
||||
// the first variant. Mirrors cstage cgen.c:10459-10466
|
||||
// (success_is_str = type_isstr(succ_t at cg_tagged_success_tag)).
|
||||
let succisstr: bool = false;
|
||||
if (n.lhs != nil) {
|
||||
if (n.lhs.kind == nkind.N_CALL) {
|
||||
let callee: *node = n.lhs.lhs;
|
||||
if (callee != nil) {
|
||||
let cname: str;
|
||||
cname.ptr = nil; cname.len = 0;
|
||||
let cmod: str;
|
||||
cmod.ptr = nil; cmod.len = 0;
|
||||
if (callee.kind == nkind.N_IDENT) {
|
||||
cname = callee.str;
|
||||
cmod = c.curmod;
|
||||
};
|
||||
if (callee.kind == nkind.N_DOT) {
|
||||
cname = callee.str;
|
||||
if (callee.lhs != nil) {
|
||||
if (callee.lhs.kind == nkind.N_IDENT) {
|
||||
cmod = callee.lhs.str;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (cname.len > 0) {
|
||||
let rtyp: *node = fnretlookupmod(c, cname, cmod);
|
||||
if (rtyp != nil) {
|
||||
if (rtyp.kind == nkind.N_TTAGGED) {
|
||||
let first: *node = rtyp.list;
|
||||
if (first != nil) {
|
||||
if (isstrtype(c, first)) {
|
||||
succisstr = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
succisstr = typeisstr(successvariant(n.lhs.type_: *tinfo));
|
||||
};
|
||||
if (succisstr) {
|
||||
// str IS []u8: success arrives DX=ptr, CX=len, R8=cap
|
||||
@@ -616,42 +587,11 @@ fn cgtryunw(c: *cgen, n: *node) void = {
|
||||
if (cgtrytupleshift(c, n)) { return; };
|
||||
if (cgtrytaggedshift(c, n)) { return; };
|
||||
// Unwrap success value. (Same shuffle pattern as cgtryprop.)
|
||||
// #16: stamped success-variant type, any source/order (twin of
|
||||
// cgtryprop; cstage cgen.c:10595-10602).
|
||||
let succisstr: bool = false;
|
||||
if (n.lhs != nil) {
|
||||
if (n.lhs.kind == nkind.N_CALL) {
|
||||
let callee: *node = n.lhs.lhs;
|
||||
if (callee != nil) {
|
||||
let cname: str;
|
||||
cname.ptr = nil; cname.len = 0;
|
||||
let cmod: str;
|
||||
cmod.ptr = nil; cmod.len = 0;
|
||||
if (callee.kind == nkind.N_IDENT) {
|
||||
cname = callee.str;
|
||||
cmod = c.curmod;
|
||||
};
|
||||
if (callee.kind == nkind.N_DOT) {
|
||||
cname = callee.str;
|
||||
if (callee.lhs != nil) {
|
||||
if (callee.lhs.kind == nkind.N_IDENT) {
|
||||
cmod = callee.lhs.str;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (cname.len > 0) {
|
||||
let rtyp: *node = fnretlookupmod(c, cname, cmod);
|
||||
if (rtyp != nil) {
|
||||
if (rtyp.kind == nkind.N_TTAGGED) {
|
||||
let first: *node = rtyp.list;
|
||||
if (first != nil) {
|
||||
if (isstrtype(c, first)) {
|
||||
succisstr = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
succisstr = typeisstr(successvariant(n.lhs.type_: *tinfo));
|
||||
};
|
||||
if (succisstr) {
|
||||
// str IS []u8: success arrives DX=ptr, CX=len, R8=cap
|
||||
|
||||
@@ -23738,42 +23738,13 @@ fn cgtryprop(c: *cgen, n: *node) void = {
|
||||
// the codegen expects the success value in AX (and BX for str).
|
||||
// AX=tag, DX=val0, CX=val1 from the call ABI. For str success,
|
||||
// shuffle (DX,CX) → (AX,BX); else move DX → AX.
|
||||
// #16: read the STAMPED operand's success-variant type (any source
|
||||
// shape, any variant order), not a name-keyed call-only lookup of
|
||||
// the first variant. Mirrors cstage cgen.c:10459-10466
|
||||
// (success_is_str = type_isstr(succ_t at cg_tagged_success_tag)).
|
||||
let succisstr: bool = false;
|
||||
if (n.lhs != nil) {
|
||||
if (n.lhs.kind == nkind.N_CALL) {
|
||||
let callee: *node = n.lhs.lhs;
|
||||
if (callee != nil) {
|
||||
let cname: str;
|
||||
cname.ptr = nil; cname.len = 0;
|
||||
let cmod: str;
|
||||
cmod.ptr = nil; cmod.len = 0;
|
||||
if (callee.kind == nkind.N_IDENT) {
|
||||
cname = callee.str;
|
||||
cmod = c.curmod;
|
||||
};
|
||||
if (callee.kind == nkind.N_DOT) {
|
||||
cname = callee.str;
|
||||
if (callee.lhs != nil) {
|
||||
if (callee.lhs.kind == nkind.N_IDENT) {
|
||||
cmod = callee.lhs.str;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (cname.len > 0) {
|
||||
let rtyp: *node = fnretlookupmod(c, cname, cmod);
|
||||
if (rtyp != nil) {
|
||||
if (rtyp.kind == nkind.N_TTAGGED) {
|
||||
let first: *node = rtyp.list;
|
||||
if (first != nil) {
|
||||
if (isstrtype(c, first)) {
|
||||
succisstr = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
succisstr = typeisstr(successvariant(n.lhs.type_: *tinfo));
|
||||
};
|
||||
if (succisstr) {
|
||||
// str IS []u8: success arrives DX=ptr, CX=len, R8=cap
|
||||
@@ -23836,42 +23807,11 @@ fn cgtryunw(c: *cgen, n: *node) void = {
|
||||
if (cgtrytupleshift(c, n)) { return; };
|
||||
if (cgtrytaggedshift(c, n)) { return; };
|
||||
// Unwrap success value. (Same shuffle pattern as cgtryprop.)
|
||||
// #16: stamped success-variant type, any source/order (twin of
|
||||
// cgtryprop; cstage cgen.c:10595-10602).
|
||||
let succisstr: bool = false;
|
||||
if (n.lhs != nil) {
|
||||
if (n.lhs.kind == nkind.N_CALL) {
|
||||
let callee: *node = n.lhs.lhs;
|
||||
if (callee != nil) {
|
||||
let cname: str;
|
||||
cname.ptr = nil; cname.len = 0;
|
||||
let cmod: str;
|
||||
cmod.ptr = nil; cmod.len = 0;
|
||||
if (callee.kind == nkind.N_IDENT) {
|
||||
cname = callee.str;
|
||||
cmod = c.curmod;
|
||||
};
|
||||
if (callee.kind == nkind.N_DOT) {
|
||||
cname = callee.str;
|
||||
if (callee.lhs != nil) {
|
||||
if (callee.lhs.kind == nkind.N_IDENT) {
|
||||
cmod = callee.lhs.str;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (cname.len > 0) {
|
||||
let rtyp: *node = fnretlookupmod(c, cname, cmod);
|
||||
if (rtyp != nil) {
|
||||
if (rtyp.kind == nkind.N_TTAGGED) {
|
||||
let first: *node = rtyp.list;
|
||||
if (first != nil) {
|
||||
if (isstrtype(c, first)) {
|
||||
succisstr = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
succisstr = typeisstr(successvariant(n.lhs.type_: *tinfo));
|
||||
};
|
||||
if (succisstr) {
|
||||
// str IS []u8: success arrives DX=ptr, CX=len, R8=cap
|
||||
|
||||
168
test/wcc/989_trystr_run.c
Normal file
168
test/wcc/989_trystr_run.c
Normal file
@@ -0,0 +1,168 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
Reference in New Issue
Block a user