diff --git a/Makefile b/Makefile index 54c5a186..6b062f57 100644 --- a/Makefile +++ b/Makefile @@ -308,6 +308,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_mixed_scalar_tuple_sret_run \ $(BIN)/test_tuple_in_union_run \ $(BIN)/test_tuple_slot_layout_run \ + $(BIN)/test_tagged_tuple_widen_run \ $(BIN)/test_errtype_compare \ $(BIN)/test_tuple_elem_slice_len_run \ $(BIN)/test_str_forrange_loopvar_run \ @@ -1421,6 +1422,12 @@ $(BIN)/test_tuple_slot_layout_run: test/wcc/941_tuple_slot_layout_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_tagged_tuple_widen_run: test/wcc/936_tagged_tuple_widen_run.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_errtype_compare: test/wcc/949_errtype_compare.c \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index c8f8ec38..36177eb8 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -2456,8 +2456,32 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src, * (AX=tag, DX=word0, CX=word1, R8=word2) and the cgmlet receive * cursor. NOT the packed-by-size t.N field layout (#238). Mirror of * the struct-literal field-flow below, but 8B-slotted, not field- - * offset. */ - if (su && su->kind == TY_TUPLE && src->kind == N_TUPLE) { + * offset. + * + * #66: the cast-wrapped tuple literal `((a, b): range_alias)` is + * the spelling real code uses (regex.ha:213) — the cast targets the + * CONCRETE variant, so the widen-cast peel above leaves it intact + * and pre-#66 it fell to the scalar arm, silently dropping payload + * slot 1+. Peel to the inner tuple here; st stays the CAST's type, + * which resolves the variant tag by exact named match, so the #241 + * untyped-element un-matchability does not arise for this form. */ + Node *tupsrc = NULL; + if (su && su->kind == TY_TUPLE) { + if (src->kind == N_TUPLE) + tupsrc = src; + else if (src->kind == N_CAST && src->lhs + && src->lhs->kind == N_TUPLE) + tupsrc = src->lhs; + /* #72: any OTHER tuple-typed source (ident, call result, + * match binding) would fall to the scalar arm below and + * silently drop payload slot 1+ — loud-stop (rule 7) until + * the word-copy / cursor-receive arms are wired. */ + if (tupsrc == NULL) + fatal("cg_widen_tagged_store: tuple-typed source " + "shape unwired (only the bare/cast tuple literal " + "carries a full payload; see #72)"); + } + if (tupsrc != NULL) { int tag = cg_tag_for_variant(du, st); /* #242: a tuple built from UNTYPED/literal elements (`(true,7)`) * leaves the src tuple type un-matchable by type_eq, so the @@ -2478,7 +2502,7 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src, * payload the slotted write assumes. Loud-stop (rule 7); the * SysV eightbyte tuple classification is a deferred follow-up. */ int total = 0; - for (Node *e = src->list; e; e = e->next) + for (Node *e = tupsrc->list; e; e = e->next) total += (node_isstr(e) || node_isslice(e)) ? 24 : 8; if (8 + total > sz) fatal("cg_widen_tagged_store: tuple-in-union payload needs " @@ -2489,7 +2513,7 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src, ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, write_off + k)); int foff = 0; - for (Node *e = src->list; e; e = e->next) { + for (Node *e = tupsrc->list; e; e = e->next) { int e_isf32 = 0; int isflt = fld_isfloat(e->type, &e_isf32); int wide = node_isstr(e) || node_isslice(e); @@ -2702,11 +2726,17 @@ cg_widen_tagged_push(Cg *c, Local **locals_p, Type *dst, Node *src, int sz) Type *su = (st && st->kind == TY_NAMED) ? st->under : st; int src_is_struct = su && su->kind == TY_STRUCT; int src_is_tagged = su && su->kind == TY_TAGGED; + /* #66: a tuple-typed source has no direct-push shape — the scalar + * fast arm below would push word 0 only (payload slot 1+ dropped) + * and coerce an unresolved tag to 0. Route through the scratch + * store, whose #242/#66 tuple arm handles the literal/cast forms + * and loud-stops the rest (#72). */ + int src_is_tuple = su && su->kind == TY_TUPLE; /* #38b: a MEMORY-class (>48B) dst slot always routes through the * scratch path — the str/slice fast arms push exactly 4 words, * short of the slot's msz/8 the mem pre-pass accounts for. */ int dst_is_mem = tagged_memarg_size(dst) > 0; - if (!src_is_struct && !src_is_tagged && !dst_is_mem) { + if (!src_is_struct && !src_is_tagged && !src_is_tuple && !dst_is_mem) { /* Direct-push fast path: str / slice / scalar / pointer. */ cgexpr(c, src, *locals_p); int tag = cg_tag_for_variant(du, st); diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index f54336ab..a21f269f 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -16228,8 +16228,24 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { // then push slot words high → low). Scalar / str go via // the direct push fast path below — keeps wwstage's asm // byte-identical to cstage for selfhost source. + // #66: a tuple-typed source has no direct-push shape — the + // scalar fast arm would push word 0 only (payload slot 1+ + // dropped) and coerce an unresolved tag to 0. Route through + // the scratch store, whose #242/#66 tuple arm handles the + // literal/cast forms and loud-stops the rest (#72). Mirrors + // cstage cg_widen_tagged_push src_is_tuple. + let argtup: *tinfo = arg.type_: *tinfo; + for (argtup != nil && argtup.kind == tykind.TY_NAMED) { + argtup = argtup.under; + }; + let argistuple: bool = false; + if (argtup != nil) { + if (argtup.kind == tykind.TY_TUPLE) { + argistuple = true; + }; + }; let pname: str = rhsstructpayload(c, arg); - if (pname.len > 0) { + if (pname.len > 0 || argistuple) { let ptype: *node = param.lhs; let scroff: i32 = tagscradd(c, widensz); emitline("\tXORQ\tAX, AX\n"); @@ -19221,7 +19237,46 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s // (AX=tag, DX=word0, CX=word1, R8=word2) and the cgmlet receive // cursor. NOT the packed-by-size t.N field layout (#238). Mirror of // cstage cg_widen_tagged_store's TY_TUPLE arm. - if (src != nil) { if (src.kind == nkind.N_TUPLE) { + // + // #66: the cast-wrapped tuple literal `((a, b): range_alias)` is + // the spelling real code uses (regex.ha:213) — the cast targets the + // CONCRETE variant, so the widen-cast peel above leaves it intact + // and pre-#66 it fell to the scalar arm, silently dropping payload + // slot 1+. Peel to the inner tuple here; src.type_ stays the CAST's + // type, which resolves the variant tag by exact named match, so the + // #241 untyped-element un-matchability does not arise for this form. + let tupsrc: *node = nil; + let tupcast: bool = false; + if (src != nil) { + if (src.kind == nkind.N_TUPLE) { tupsrc = src; }; + if (src.kind == nkind.N_CAST) { + if (src.lhs != nil) { + if (src.lhs.kind == nkind.N_TUPLE) { + tupsrc = src.lhs; + tupcast = true; + }; + }; + }; + // #72: any OTHER tuple-typed source (ident, call result, + // match binding) would fall to the scalar arm below and + // silently drop payload slot 1+ — loud-stop (rule 7) until + // the word-copy / cursor-receive arms are wired. Mirrors + // cstage cg_widen_tagged_store. + if (tupsrc == nil) { + let stu72: *tinfo = src.type_: *tinfo; + for (stu72 != nil && stu72.kind == tykind.TY_NAMED) { + stu72 = stu72.under; + }; + if (stu72 != nil) { + if (stu72.kind == tykind.TY_TUPLE) { + let m72: str = "cgwidentaggedstore: tuple-typed source shape unwired (only the bare/cast tuple literal carries a full payload; see #72)\n"; + os.write(2, m72.ptr, m72.len: u64); + os.exit(1); + }; + }; + }; + }; + if (tupsrc != nil) { let stu: *tinfo = src.type_: *tinfo; for (stu != nil && stu.kind == tykind.TY_NAMED) { stu = stu.under; }; if (stu != nil) { if (stu.kind == tykind.TY_TUPLE) { @@ -19235,9 +19290,11 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s // cstage can't type: a bool literal (N_TRUE/N_FALSE) or a // suffix-less numeric literal (untyped_int/untyped_float). // LIFT BOTH stage guards together when #241 fixes cstage - // literal typing -> symmetric accept. - let bl: *node = src.list; - for (bl != nil) { + // literal typing -> symmetric accept. BARE form only (#66): + // the cast form resolves its tag from the cast's type on + // BOTH stages, so bare elements are fine there. + let bl: *node = tupsrc.list; + for (bl != nil && !tupcast) { let bare: bool = false; if (bl.kind == nkind.N_TRUE) { bare = true; }; if (bl.kind == nkind.N_FALSE) { bare = true; }; @@ -19277,7 +19334,7 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s // SysV eightbyte tuple classification is a deferred // follow-up. Symmetric with cstage cg_widen_tagged_store. let ttotal: i32 = 0; - let ce: *node = src.list; + let ce: *node = tupsrc.list; for (ce != nil) { if (nodeisstr(c, ce) || nodeisslice(c, ce)) { ttotal += 24; @@ -19298,7 +19355,7 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s tzk += 8; }; let tfoff: i32 = 0; - let te: *node = src.list; + let te: *node = tupsrc.list; for (te != nil) { let isflt: bool = isfloattype(c, te); let wide: bool = nodeisstr(c, te) || nodeisslice(c, te); @@ -19342,7 +19399,7 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s emitline("(BP)\n"); return; }; }; - }; }; + }; // Struct payload (literal or ident). let sname: str = rhsstructpayload(c, src); if (sname.len > 0) { diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index cd8c96c9..7d6e68a7 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -312,8 +312,24 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { // then push slot words high → low). Scalar / str go via // the direct push fast path below — keeps wwstage's asm // byte-identical to cstage for selfhost source. + // #66: a tuple-typed source has no direct-push shape — the + // scalar fast arm would push word 0 only (payload slot 1+ + // dropped) and coerce an unresolved tag to 0. Route through + // the scratch store, whose #242/#66 tuple arm handles the + // literal/cast forms and loud-stops the rest (#72). Mirrors + // cstage cg_widen_tagged_push src_is_tuple. + let argtup: *tinfo = arg.type_: *tinfo; + for (argtup != nil && argtup.kind == tykind.TY_NAMED) { + argtup = argtup.under; + }; + let argistuple: bool = false; + if (argtup != nil) { + if (argtup.kind == tykind.TY_TUPLE) { + argistuple = true; + }; + }; let pname: str = rhsstructpayload(c, arg); - if (pname.len > 0) { + if (pname.len > 0 || argistuple) { let ptype: *node = param.lhs; let scroff: i32 = tagscradd(c, widensz); emitline("\tXORQ\tAX, AX\n"); @@ -3305,7 +3321,46 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s // (AX=tag, DX=word0, CX=word1, R8=word2) and the cgmlet receive // cursor. NOT the packed-by-size t.N field layout (#238). Mirror of // cstage cg_widen_tagged_store's TY_TUPLE arm. - if (src != nil) { if (src.kind == nkind.N_TUPLE) { + // + // #66: the cast-wrapped tuple literal `((a, b): range_alias)` is + // the spelling real code uses (regex.ha:213) — the cast targets the + // CONCRETE variant, so the widen-cast peel above leaves it intact + // and pre-#66 it fell to the scalar arm, silently dropping payload + // slot 1+. Peel to the inner tuple here; src.type_ stays the CAST's + // type, which resolves the variant tag by exact named match, so the + // #241 untyped-element un-matchability does not arise for this form. + let tupsrc: *node = nil; + let tupcast: bool = false; + if (src != nil) { + if (src.kind == nkind.N_TUPLE) { tupsrc = src; }; + if (src.kind == nkind.N_CAST) { + if (src.lhs != nil) { + if (src.lhs.kind == nkind.N_TUPLE) { + tupsrc = src.lhs; + tupcast = true; + }; + }; + }; + // #72: any OTHER tuple-typed source (ident, call result, + // match binding) would fall to the scalar arm below and + // silently drop payload slot 1+ — loud-stop (rule 7) until + // the word-copy / cursor-receive arms are wired. Mirrors + // cstage cg_widen_tagged_store. + if (tupsrc == nil) { + let stu72: *tinfo = src.type_: *tinfo; + for (stu72 != nil && stu72.kind == tykind.TY_NAMED) { + stu72 = stu72.under; + }; + if (stu72 != nil) { + if (stu72.kind == tykind.TY_TUPLE) { + let m72: str = "cgwidentaggedstore: tuple-typed source shape unwired (only the bare/cast tuple literal carries a full payload; see #72)\n"; + os.write(2, m72.ptr, m72.len: u64); + os.exit(1); + }; + }; + }; + }; + if (tupsrc != nil) { let stu: *tinfo = src.type_: *tinfo; for (stu != nil && stu.kind == tykind.TY_NAMED) { stu = stu.under; }; if (stu != nil) { if (stu.kind == tykind.TY_TUPLE) { @@ -3319,9 +3374,11 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s // cstage can't type: a bool literal (N_TRUE/N_FALSE) or a // suffix-less numeric literal (untyped_int/untyped_float). // LIFT BOTH stage guards together when #241 fixes cstage - // literal typing -> symmetric accept. - let bl: *node = src.list; - for (bl != nil) { + // literal typing -> symmetric accept. BARE form only (#66): + // the cast form resolves its tag from the cast's type on + // BOTH stages, so bare elements are fine there. + let bl: *node = tupsrc.list; + for (bl != nil && !tupcast) { let bare: bool = false; if (bl.kind == nkind.N_TRUE) { bare = true; }; if (bl.kind == nkind.N_FALSE) { bare = true; }; @@ -3361,7 +3418,7 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s // SysV eightbyte tuple classification is a deferred // follow-up. Symmetric with cstage cg_widen_tagged_store. let ttotal: i32 = 0; - let ce: *node = src.list; + let ce: *node = tupsrc.list; for (ce != nil) { if (nodeisstr(c, ce) || nodeisslice(c, ce)) { ttotal += 24; @@ -3382,7 +3439,7 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s tzk += 8; }; let tfoff: i32 = 0; - let te: *node = src.list; + let te: *node = tupsrc.list; for (te != nil) { let isflt: bool = isfloattype(c, te); let wide: bool = nodeisstr(c, te) || nodeisslice(c, te); @@ -3426,7 +3483,7 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s emitline("(BP)\n"); return; }; }; - }; }; + }; // Struct payload (literal or ident). let sname: str = rhsstructpayload(c, src); if (sname.len > 0) { diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 0ba2fed1..de3a0656 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -16228,8 +16228,24 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { // then push slot words high → low). Scalar / str go via // the direct push fast path below — keeps wwstage's asm // byte-identical to cstage for selfhost source. + // #66: a tuple-typed source has no direct-push shape — the + // scalar fast arm would push word 0 only (payload slot 1+ + // dropped) and coerce an unresolved tag to 0. Route through + // the scratch store, whose #242/#66 tuple arm handles the + // literal/cast forms and loud-stops the rest (#72). Mirrors + // cstage cg_widen_tagged_push src_is_tuple. + let argtup: *tinfo = arg.type_: *tinfo; + for (argtup != nil && argtup.kind == tykind.TY_NAMED) { + argtup = argtup.under; + }; + let argistuple: bool = false; + if (argtup != nil) { + if (argtup.kind == tykind.TY_TUPLE) { + argistuple = true; + }; + }; let pname: str = rhsstructpayload(c, arg); - if (pname.len > 0) { + if (pname.len > 0 || argistuple) { let ptype: *node = param.lhs; let scroff: i32 = tagscradd(c, widensz); emitline("\tXORQ\tAX, AX\n"); @@ -19221,7 +19237,46 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s // (AX=tag, DX=word0, CX=word1, R8=word2) and the cgmlet receive // cursor. NOT the packed-by-size t.N field layout (#238). Mirror of // cstage cg_widen_tagged_store's TY_TUPLE arm. - if (src != nil) { if (src.kind == nkind.N_TUPLE) { + // + // #66: the cast-wrapped tuple literal `((a, b): range_alias)` is + // the spelling real code uses (regex.ha:213) — the cast targets the + // CONCRETE variant, so the widen-cast peel above leaves it intact + // and pre-#66 it fell to the scalar arm, silently dropping payload + // slot 1+. Peel to the inner tuple here; src.type_ stays the CAST's + // type, which resolves the variant tag by exact named match, so the + // #241 untyped-element un-matchability does not arise for this form. + let tupsrc: *node = nil; + let tupcast: bool = false; + if (src != nil) { + if (src.kind == nkind.N_TUPLE) { tupsrc = src; }; + if (src.kind == nkind.N_CAST) { + if (src.lhs != nil) { + if (src.lhs.kind == nkind.N_TUPLE) { + tupsrc = src.lhs; + tupcast = true; + }; + }; + }; + // #72: any OTHER tuple-typed source (ident, call result, + // match binding) would fall to the scalar arm below and + // silently drop payload slot 1+ — loud-stop (rule 7) until + // the word-copy / cursor-receive arms are wired. Mirrors + // cstage cg_widen_tagged_store. + if (tupsrc == nil) { + let stu72: *tinfo = src.type_: *tinfo; + for (stu72 != nil && stu72.kind == tykind.TY_NAMED) { + stu72 = stu72.under; + }; + if (stu72 != nil) { + if (stu72.kind == tykind.TY_TUPLE) { + let m72: str = "cgwidentaggedstore: tuple-typed source shape unwired (only the bare/cast tuple literal carries a full payload; see #72)\n"; + os.write(2, m72.ptr, m72.len: u64); + os.exit(1); + }; + }; + }; + }; + if (tupsrc != nil) { let stu: *tinfo = src.type_: *tinfo; for (stu != nil && stu.kind == tykind.TY_NAMED) { stu = stu.under; }; if (stu != nil) { if (stu.kind == tykind.TY_TUPLE) { @@ -19235,9 +19290,11 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s // cstage can't type: a bool literal (N_TRUE/N_FALSE) or a // suffix-less numeric literal (untyped_int/untyped_float). // LIFT BOTH stage guards together when #241 fixes cstage - // literal typing -> symmetric accept. - let bl: *node = src.list; - for (bl != nil) { + // literal typing -> symmetric accept. BARE form only (#66): + // the cast form resolves its tag from the cast's type on + // BOTH stages, so bare elements are fine there. + let bl: *node = tupsrc.list; + for (bl != nil && !tupcast) { let bare: bool = false; if (bl.kind == nkind.N_TRUE) { bare = true; }; if (bl.kind == nkind.N_FALSE) { bare = true; }; @@ -19277,7 +19334,7 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s // SysV eightbyte tuple classification is a deferred // follow-up. Symmetric with cstage cg_widen_tagged_store. let ttotal: i32 = 0; - let ce: *node = src.list; + let ce: *node = tupsrc.list; for (ce != nil) { if (nodeisstr(c, ce) || nodeisslice(c, ce)) { ttotal += 24; @@ -19298,7 +19355,7 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s tzk += 8; }; let tfoff: i32 = 0; - let te: *node = src.list; + let te: *node = tupsrc.list; for (te != nil) { let isflt: bool = isfloattype(c, te); let wide: bool = nodeisstr(c, te) || nodeisslice(c, te); @@ -19342,7 +19399,7 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s emitline("(BP)\n"); return; }; }; - }; }; + }; // Struct payload (literal or ident). let sname: str = rhsstructpayload(c, src); if (sname.len > 0) { diff --git a/test/wcc/936_tagged_tuple_widen_run.c b/test/wcc/936_tagged_tuple_widen_run.c new file mode 100644 index 00000000..0210e73c --- /dev/null +++ b/test/wcc/936_tagged_tuple_widen_run.c @@ -0,0 +1,525 @@ +/* + * 936_tagged_tuple_widen_run — #66 (fold-4 prereq): the cast-wrapped + * tuple literal `((a, b): variant_alias)` widened into a tagged-union + * payload. Pre-#66 the #242 tuple arm of the widen choke-point + * (cg_widen_tagged_store / cgwidentaggedstorebp) gated on a BARE + * N_TUPLE source; the cast-to-CONCRETE-VARIANT wrapper — the only + * spelling real code uses (ref/hare/regex/regex.ha:213 + * `(start_b, end_b): charset_range_item`) — was not peeled (it is not + * a widen-cast: its destination is the variant, not the union), fell + * to the SCALAR arm and silently stored cursor word 0 only, zero- + * filling payload slot 1+ (both stages, byte-id — gate-blind). The + * read side (match-extract slot walk) was always correct. + * + * row | shape | want + * -----------------------+--------------------------------------+----- + * cast_let_packed | let v: item = ((65,90): rng); .0/.1 | 0 + * cast_let_3member | + (str,*fn) member present, never | + * | constructed (layout-neutral pin) | 0 + * cast_append_local | append(xs, cast) → index → extract | 0 + * cast_append_indexplace | append(cs[len-1], cast), [][]item | 0 + * cast_append_derefplace | through *[][]item param (via_outer) | 0 + * cast_append_ptrfield | append(p.cs[len-1], cast) — ptr- | + * | FIELD-rooted place | 0 + * cast_ident_elems | (start_b, end_b) u32 LOCALS — the | + * | handle_bracket shape | 0 + * cast_float_elem | (u32, f64) — MOVSD element store | 0 + * cast_str_elem | (i64, str) — 3-word wide element | 0 + * cast_arg_direct | cast tuple as a DIRECT tagged ARG — | + * | cg_widen_tagged_push routes tuple | + * | srcs to the scratch store (the fast | + * | scalar push dropped slot 1+) | 0 + * bare_ident_arg | bare (a, b) typed idents as a tagged | + * | ARG — same push routing, tag from | + * | the typed tuple | 0 + * bare_ident_no_regress | bare (a, b) typed-ident return into | + * | tagged — the strconv parseint shape | 0 + * bare_literal_reject | bare (5, 6) literal-element tuple | + * | into tagged stays the #242/#241 loud | + * | stop (the cast form lifts it; the | + * | bare form must NOT silently change) | err + * tuple_ident_reject | tuple-typed IDENT into tagged stays | + * | LOUD (#72) — was a silent slot-1+ | + * | drop through the scalar arm | err + * + * Every K_RUN row also asserts cstage/wwstage asm byte-id. NNN<950, + * self-contained (/tmp, no imports) — rule-14's selfhost-sibling race + * does not apply (941 precedent). + */ +#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; +} + +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 drivers, exit==want, + cs==ww byte-id */ +#define K_BUILDERR 1 /* build must FAIL with experr on BOTH drivers (rule 7) */ + +struct row { const char *label; const char *src; int want; + int kind; const char *experr; }; + +/* errlog_has — a BUILDERR row must fail WITH its diagnostic; any other + * failure (parse error, crash) 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[] = { + { "cast_let_packed", + "package main;\n" + "type lit = rune;\n" + "type rng = (u32, u32);\n" + "type item = (lit | rng);\n" + "export fn main() i32 = {\n" + " let v: item = ((65, 90): rng);\n" + " match (v) {\n" + " case let r: rng => {\n" + " if (r.0 != 65) { return 1; };\n" + " if (r.1 != 90) { return 2; };\n" + " };\n" + " case => return 3;\n" + " };\n" + " return 0;\n" + "};\n", 0, K_RUN, NULL }, + /* the real charset element: the (str,*fn) class member is PRESENT + * IN THE TYPE but never constructed — its presence must not alter + * the lit/range slot layout or walks (fold-4 PF1 caveat). */ + { "cast_let_3member", + "package main;\n" + "type lit = rune;\n" + "type rng = (u32, u32);\n" + "type cls = (str, *fn(c: rune) bool);\n" + "type item = (lit | rng | cls);\n" + "export fn main() i32 = {\n" + " let v: item = ((65, 90): rng);\n" + " match (v) {\n" + " case let r: rng => {\n" + " if (r.0 != 65) { return 1; };\n" + " if (r.1 != 90) { return 2; };\n" + " };\n" + " case => return 3;\n" + " };\n" + " let w: item = ('a': lit);\n" + " match (w) {\n" + " case let l: lit => { if ((l: rune) != 'a') { return 4; }; };\n" + " case => return 5;\n" + " };\n" + " return 0;\n" + "};\n", 0, K_RUN, NULL }, + { "cast_append_local", + "package main;\n" + "type lit = rune;\n" + "type rng = (u32, u32);\n" + "type item = (lit | rng);\n" + "export fn main() i32 = {\n" + " let xs: []item;\n" + " append(xs, ('a': lit));\n" + " append(xs, ((65, 90): rng));\n" + " if (len(xs) != 2) { return 1; };\n" + " let e: item = xs[1];\n" + " match (e) {\n" + " case let r: rng => {\n" + " if (r.0 != 65) { return 2; };\n" + " if (r.1 != 90) { return 3; };\n" + " };\n" + " case => return 4;\n" + " };\n" + " return 0;\n" + "};\n", 0, K_RUN, NULL }, + /* append place = indexed inner slice — the handle_bracket + * `append(charsets[len(charsets)-1], ...)` route. */ + { "cast_append_indexplace", + "package main;\n" + "type lit = rune;\n" + "type rng = (u32, u32);\n" + "type item = (lit | rng);\n" + "export fn main() i32 = {\n" + " let charsets: [][]item;\n" + " let empty: []item;\n" + " append(charsets, empty);\n" + " append(charsets[len(charsets) - 1], ((7, 9): rng));\n" + " if (len(charsets[0]) != 1) { return 1; };\n" + " let e: item = charsets[0][0];\n" + " match (e) {\n" + " case let r: rng => {\n" + " if (r.0 != 7) { return 2; };\n" + " if (r.1 != 9) { return 3; };\n" + " };\n" + " case => return 4;\n" + " };\n" + " return 0;\n" + "};\n", 0, K_RUN, NULL }, + /* append place behind a *[][]item param — the via_outer + * (pointer-rooted) widen route. */ + { "cast_append_derefplace", + "package main;\n" + "type lit = rune;\n" + "type rng = (u32, u32);\n" + "type item = (lit | rng);\n" + "fn addrange(cs: *[][]item, a: u32, b: u32) void = {\n" + " if (len(*cs) == 0) {\n" + " let empty: []item;\n" + " append(*cs, empty);\n" + " };\n" + " append((*cs)[len(*cs) - 1], ((a, b): rng));\n" + "};\n" + "export fn main() i32 = {\n" + " let charsets: [][]item;\n" + " addrange(&charsets, 5, 8);\n" + " if (len(charsets) != 1) { return 1; };\n" + " if (len(charsets[0]) != 1) { return 2; };\n" + " let e: item = charsets[0][0];\n" + " match (e) {\n" + " case let r: rng => {\n" + " if (r.0 != 5) { return 3; };\n" + " if (r.1 != 8) { return 4; };\n" + " };\n" + " case => return 5;\n" + " };\n" + " return 0;\n" + "};\n", 0, K_RUN, NULL }, + /* append place rooted at a ptr-FIELD ([][]item field through + * *struct) — the re.charsets-style place spelling. */ + { "cast_append_ptrfield", + "package main;\n" + "type lit = rune;\n" + "type rng = (u32, u32);\n" + "type item = (lit | rng);\n" + "type holder = struct { cs: [][]item, n: i64 };\n" + "export fn main() i32 = {\n" + " let h: holder = holder { n = 0, ... };\n" + " let p: *holder = &h;\n" + " let empty: []item;\n" + " append(p.cs, empty);\n" + " append(p.cs[len(p.cs) - 1], ((7, 9): rng));\n" + " if (len(h.cs[0]) != 1) { return 1; };\n" + " match (h.cs[0][0]) {\n" + " case let r: rng => {\n" + " if (r.0 != 7) { return 2; };\n" + " if (r.1 != 9) { return 3; };\n" + " };\n" + " case => return 4;\n" + " };\n" + " return 0;\n" + "};\n", 0, K_RUN, NULL }, + /* typed u32 LOCALS as the tuple elements — handle_bracket's + * exact `(start_b, end_b): charset_range_item` shape. */ + { "cast_ident_elems", + "package main;\n" + "type lit = rune;\n" + "type rng = (u32, u32);\n" + "type item = (lit | rng);\n" + "export fn main() i32 = {\n" + " let start_b: u32 = 97;\n" + " let end_b: u32 = 122;\n" + " let v: item = ((start_b, end_b): rng);\n" + " match (v) {\n" + " case let r: rng => {\n" + " if (r.0 != 97) { return 1; };\n" + " if (r.1 != 122) { return 2; };\n" + " };\n" + " case => return 3;\n" + " };\n" + " return 0;\n" + "};\n", 0, K_RUN, NULL }, + /* float element rides the MOVSD store branch under the cast form */ + { "cast_float_elem", + "package main;\n" + "type mf = (u32, f64);\n" + "type item = (rune | mf);\n" + "export fn main() i32 = {\n" + " let a: u32 = 9;\n" + " let x: f64 = 2.5;\n" + " let v: item = ((a, x): mf);\n" + " match (v) {\n" + " case let m: mf => {\n" + " if (m.0 != 9) { return 1; };\n" + " if (m.1 != 2.5) { return 2; };\n" + " };\n" + " case => return 3;\n" + " };\n" + " return 0;\n" + "};\n", 0, K_RUN, NULL }, + /* str element rides the 3-word wide branch under the cast form */ + { "cast_str_elem", + "package main;\n" + "type ms = (i64, str);\n" + "type item = (rune | ms);\n" + "export fn main() i32 = {\n" + " let n: i64 = 12;\n" + " let s: str = \"wxyz\";\n" + " let v: item = ((n, s): ms);\n" + " match (v) {\n" + " case let m: ms => {\n" + " if (m.0 != 12) { return 1; };\n" + " if (len(m.1) != 4) { return 2; };\n" + " };\n" + " case => return 3;\n" + " };\n" + " return 0;\n" + "};\n", 0, K_RUN, NULL }, + /* cast tuple passed DIRECTLY as a tagged call arg — pre-fix + * cg_widen_tagged_push's scalar fast path pushed word 0 only + * (and coerced an unresolved tag to 0); tuple-typed sources now + * route through the scratch store choke-point. */ + { "cast_arg_direct", + "package main;\n" + "type lit = rune;\n" + "type rng = (u32, u32);\n" + "type item = (lit | rng);\n" + "fn check(v: item) i32 = {\n" + " match (v) {\n" + " case let r: rng => {\n" + " if (r.0 != 65) { return 1; };\n" + " if (r.1 != 90) { return 2; };\n" + " return 0;\n" + " };\n" + " case => return 3;\n" + " };\n" + "};\n" + "export fn main() i32 = {\n" + " return check(((65, 90): rng));\n" + "};\n", 0, K_RUN, NULL }, + /* bare typed-ident tuple as a tagged call arg — the strconv + * `(neg, n)` shape at a CALL boundary; rides the same push + * routing, tag resolved from the typed tuple. */ + { "bare_ident_arg", + "package main;\n" + "type item = (rune | (i64, i64));\n" + "fn check(v: item) i32 = {\n" + " match (v) {\n" + " case let r: (i64, i64) => {\n" + " if (r.0 != 5) { return 1; };\n" + " if (r.1 != 6) { return 2; };\n" + " return 0;\n" + " };\n" + " case => return 3;\n" + " };\n" + "};\n" + "export fn main() i32 = {\n" + " let a: i64 = 5;\n" + " let b: i64 = 6;\n" + " return check((a, b));\n" + "};\n", 0, K_RUN, NULL }, + /* bare N_TUPLE of TYPED idents (the strconv parseint `(neg, n)` + * return shape) must keep working — no-regress on the bare arm. */ + { "bare_ident_no_regress", + "package main;\n" + "fn mk() ((i64, i64) | nomem) = {\n" + " let a: i64 = 5;\n" + " let b: i64 = 6;\n" + " return (a, b);\n" + "};\n" + "export fn main() i32 = {\n" + " match (mk()) {\n" + " case let t: (i64, i64) => {\n" + " if (t.0 != 5) { return 1; };\n" + " if (t.1 != 6) { return 2; };\n" + " };\n" + " case => return 3;\n" + " };\n" + " return 0;\n" + "};\n", 0, K_RUN, NULL }, + /* bare literal-element tuple into tagged stays the #242/#241 + * loud-stop — the cast form lifts it (the cast supplies the + * variant type), the bare form must not silently change. */ + { "bare_literal_reject", + "package main;\n" + "fn mk() ((i64, i64) | nomem) = {\n" + " return (5, 6);\n" + "};\n" + "export fn main() i32 = {\n" + " match (mk()) {\n" + " case let t: (i64, i64) => { if (t.0 != 5) { return 1; }; };\n" + " case => return 2;\n" + " };\n" + " return 0;\n" + "};\n", 0, + K_BUILDERR, "tuple-in-union variant tag unresolved" }, + /* tuple-typed IDENT source into a tagged slot: the widen + * choke-point has no word-copy arm for it — pre-#72 it fell to + * the scalar arm and SILENTLY dropped payload slot 1+; now loud + * (rule 7) until #72 wires ident/call/match-binding sources. */ + { "tuple_ident_reject", + "package main;\n" + "type lit = rune;\n" + "type rng = (u32, u32);\n" + "type item = (lit | rng);\n" + "export fn main() i32 = {\n" + " let t: rng = ((65, 90): rng);\n" + " let v: item = t;\n" + " match (v) {\n" + " case let r: rng => { if (r.0 != 65) { return 1; }; };\n" + " case => return 2;\n" + " };\n" + " return 0;\n" + "};\n", 0, + K_BUILDERR, "tuple-typed source shape unwired" }, +}; + +/* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */ +static int +run_driver(const char *driver, const struct row *r, int i) +{ + char src[96], tmpdir[96], errf[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/ttw_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/ttw_%d_d_%d", getpid(), i); + snprintf(errf, sizeof errf, "/tmp/ttw_%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 && %s build %s >/dev/null 2>%s", + tmpdir, driver, src, errf); + int brc = runwait(cmd); + if (r->kind == K_BUILDERR) { + 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 != r->want) { + fprintf(stderr, "row[%s]: %s exit %d, want %d\n", + r->label, driver, got, r->want); + return 1; + } + return 0; +} + +/* cs==ww .s byte-id (rule 10). */ +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/ttw_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/ttw_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/ttw_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++; + if (run_driver(cdrv, &rows[i], i) != 0) fail++; + } + if (access(wdrv, X_OK) == 0) { + for (int i = 0; i < n; i++) { + total++; + if (run_driver(wdrv, &rows[i], i) != 0) fail++; + } + for (int i = 0; i < n; i++) { + if (rows[i].kind == K_BUILDERR) + continue; + total++; + if (asm_byte_identical(bin, &rows[i], i) != 0) fail++; + } + } + + if (fail) { + fprintf(stderr, "tagged_tuple_widen: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("tagged_tuple_widen: %d/%d ok\n", total, total); + return 0; +}