`lhs or rhs` with failable operands now lowers as a full short-circuit chain (was a loud bail). Each failing attempt routes to the next operand; the chain resolves when an operand succeeds or a value terminator absorbs; total failure propagates to the function — or, when the chain is the operand of a `catch`, to the handler. All in ir/lower.zig. - Dispatch (lowerBinaryOp .or_op): structural `orIsFailableChain` (an operand is a `try`, error-channel-typed, or a nested failable `or` chain) instead of the type-only `exprIsFailable(lhs)`, which missed nested chains (a try-chain's value type is non-failable T). - inferExprType .or_op: a failable chain reports its success type via `orChainSuccessType` (was `.bool`). - lowerFailableOr rewritten: flatten the left-assoc chain, lower operands left-to-right. Non-final failure → push frame + fall to next operand block (no function exit, so onfail doesn't fire). Success → clear trace + merge. Final failure → push frame + route to a `catch` target (chain_fail_target field) if set, else propagate (cleanup + error return). Value terminator → clear + merge the terminator value. Subsumes the E2.4a path. Widening factored into `checkEscapeWidening`, checked only at a propagating final operand. - Catch-over-chain: lowerCatchOverChain sets chain_fail_target so the chain's total failure reaches the handler (binds the final tag, may inspect the trace, clears on non-diverging exit). Verified JIT + AOT: 2-/3-operand chains, bare chain + value terminator, void chains, all-fail propagation (exit 1 + trace), catch-over-chain, trace clear-on-absorb, onfail gating. examples/246-failable-or-chain.sx (exit 120), 247-failable-or-chain-propagate.sx (exit 1 + trace).
40 lines
1.7 KiB
Plaintext
40 lines
1.7 KiB
Plaintext
// Failable `or` chains (ERR step E2.4b). `lhs or rhs` with failable operands
|
|
// is a left-to-right, short-circuit chain: each failing attempt routes to the
|
|
// next operand; the chain resolves when an operand succeeds (or a value
|
|
// terminator absorbs). `try` marks an operand whose failure is visible routing
|
|
// (path-marker rule); a bare failable operand is allowed when a downstream
|
|
// terminator absorbs it. A `catch` over a parenthesized chain redirects the
|
|
// chain's total failure to the handler instead of the function. Absorbed
|
|
// failures clear the trace buffer; `onfail` does not fire for a failure that
|
|
// never leaves its block. This run takes only absorbed paths → exit 120.
|
|
|
|
#import "modules/std.sx";
|
|
|
|
E :: error { A, B };
|
|
|
|
fa :: (n: s32) -> (s32, !E) {
|
|
if n == 0 { raise error.A; }
|
|
if n < 0 { raise error.B; }
|
|
return n;
|
|
}
|
|
|
|
fv :: (n: s32) -> !E { // void (pure) failable
|
|
if n == 0 { raise error.A; }
|
|
return;
|
|
}
|
|
|
|
main :: () -> (s32, !E) {
|
|
onfail print("onfail fired (BUG)\n"); // must NOT fire — every chain below absorbs
|
|
|
|
r : s32 = 0;
|
|
r = r + (try fa(0) or try fa(7)); // a fails → b succeeds → 7
|
|
r = r + (try fa(0) or try fa(0) or try fa(3)); // first two fail → third → +3 = 10
|
|
r = r + (fa(0) or fa(0) or 96); // bare chain + value terminator → +96 = 106
|
|
r = r + ((try fa(0) or try fa(0)) catch e 5); // both fail → catch handler → +5 = 111
|
|
r = r + ((try fa(0) or try fa(9)) catch e 0); // second succeeds → catch skipped → +9 = 120
|
|
|
|
try fv(0) or try fv(1); // void chain: first fails → second succeeds
|
|
|
|
return r; // success → exit 120; onfail skipped
|
|
}
|