w6c+wwstage: cgexpr materializes tuple rvalues + unwrap-shift for tuple-payload destructure (#241)

cgexpr could not produce a tuple VALUE, so a destructure / let bind of an
RVALUE tuple read garbage past the first element (cstage) or left an untyped
binder aborting wwstage's asserttyped gate — a DANGEROUS gate-blind cs!=ww,
and the strconv-int blocker (Hare's stoi64/stou64 require
`let (sign, u) = parseint(s, base)?`). Three feeders, all routed at the same
SysV register-return cursor the cgmlet/cgmassign consumers already read:

  - an N_TUPLE literal fell to the `cgexpr_int(0)` / `MOVQ $0, AX` default;
  - a tuple-typed IDENT loaded only word0 into AX (`yield t`, `return t`,
    `let q = t`), leaving DX/CX stale;
  - the `?`/`!` unwrap of a tuple-in-union payload lifted only word0->AX,
    stranding word1 in CX (the scalar/str success ABI).

Fix (both stages, byte-identical per rule 10):

  - cgexpr packs an N_TUPLE literal into the cursor (cg_tuple_lit_to_cursor /
    cgtuplelittocursor — a byte-identical reuse of cgreturn's in-register
    N_TUPLE arm) and a tuple IDENT from its slot at the register-ABI stride
    (cg_tuple_slot_to_cursor / cgtupleslottocursor);
  - the ?/! unwrap shifts a tuple success payload down one integer reg past
    the tag (cg_tagged_tuple_payload_shift / cgtaggedtuplepayloadshift),
    loud-stopping a float/slice/str payload element (the SysV per-eightbyte
    tagged-tuple-payload classification is #243);
  - wwstage's checker recovers the popped match-arm binder type for a
    `yield <binder>` operand (matchyieldtype's scope-free fallback to the
    arm's declared type), so the destructured binders stamp — cstage reads
    the operand's already-stamped ->type, wwstage caches only a tinfo.

Over-cap rvalue-tuple materialisation (no slot to sret a bare expression
value into) loud-stops both stages — the #10 follow-up.

NOT closed (distinct root, deferred to #238/task #6): single-var
`let q = (true, 9u64)` then `q.N` — the N_LET tuple-init sz==16||32 gate
drops a narrow-first mixed tuple, and the N_DOT tuple-field PACKED-offset
reader disagrees with tuple_store's 8B stride. Not the rvalue-into-cursor
fix and not a strconv blocker (strconv destructures); documented at the test
header.

Test 945_rvalue_tuple_destructure_run: literal destructure, match-yield
destructure, and the ?-call strconv shape, each run + cs==ww byte-id on both
drivers (9 checks). Embedded w6c/wwdump combined.ww regenerated.
This commit is contained in:
2026-06-01 21:27:49 +09:00
parent 6fc85f9aaf
commit 5d023c0ef0
8 changed files with 1229 additions and 22 deletions

View File

@@ -1090,30 +1090,46 @@ fn astunsized(c: *checker, t: *node) bool = {
// descend into a nested N_MATCH — each match opens its own yield
// scope. exprtype is idempotent on already-stamped nodes (tinfocache
// path at L467) so re-entering it on the yield operand here is safe.
fn matchyieldtype(c: *checker, body: *node) *node = {
// bname/btype carry the enclosing arm's case-binding name + declared type
// node. #241: when exprtype can't re-derive a `yield <binder>` operand —
// the arm binder's scope is already popped by the time exprtype(N_MATCH)
// runs post-order (resolvewalk's N_MCASE restores c.cur before this), so
// scopelookup of the bare binder ident returns nil — the yielded type IS
// the binding type (btype). cstage avoids this by reading the operand's
// already-stamped ->type (check.c:122) rather than re-running cexpr; wwstage
// caches only node.type_ (a tinfo, not a type NODE), so this binder-typed
// fallback is the node-form recovery for the dominant match-bind-then-yield
// idiom (Hare's parseint `case let t => yield t`).
fn matchyieldtype(c: *checker, body: *node, bname: str, btype: *node) *node = {
if (body == nil) { return nil; };
let k: nkind = body.kind;
if (k == nkind.N_YIELD) {
if (body.lhs == nil) { return nil; };
return exprtype(c, body.lhs, nil);
let t: *node = exprtype(c, body.lhs, nil);
if (t != nil) { return t; };
if (body.lhs.kind == nkind.N_IDENT && bname.len > 0
&& streq(body.lhs.str, bname)) {
return btype;
};
return nil;
};
if (k == nkind.N_MATCH) { return nil; };
if (k == nkind.N_BLOCK) {
let s: *node = body.list;
for (s != nil) {
let t: *node = matchyieldtype(c, s);
let t: *node = matchyieldtype(c, s, bname, btype);
if (t != nil) { return t; };
s = s.next;
};
return nil;
};
if (k == nkind.N_IF) {
let t: *node = matchyieldtype(c, body.body);
let t: *node = matchyieldtype(c, body.body, bname, btype);
if (t != nil) { return t; };
return matchyieldtype(c, body.els);
return matchyieldtype(c, body.els, bname, btype);
};
if (k == nkind.N_FOR || k == nkind.N_FORRANGE) {
return matchyieldtype(c, body.body);
return matchyieldtype(c, body.body, bname, btype);
};
return nil;
};
@@ -2904,7 +2920,10 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = {
let yt: *node = nil;
let cs: *node = e.list;
for (cs != nil) {
let t: *node = matchyieldtype(c, cs.body);
// cs.str/cs.lhs = the arm's case-binding name + declared
// type (the N_MCASE binder); fed to matchyieldtype's #241
// scope-popped `yield <binder>` fallback.
let t: *node = matchyieldtype(c, cs.body, cs.str, cs.lhs);
if (t != nil) { yt = t; break; };
cs = cs.next;
};