`lhs or value` where `lhs` is a value-carrying failable (`-> (T, !E)`): on
success the result is the LHS value, on failure the LHS error is discarded and
the result is the terminator value — the whole expression is non-failable (T).
Unblocked by the value ABI (E2.1); needs no fallback-routing (it's a 2-operand,
non-chained `or`).
- lowerBinaryOp `.or_op`: a failable LHS now routes to lowerFailableOr instead
of the E1.4a loud bail; non-failable `or` (boolean / optional-unwrap)
unchanged.
- lowerFailableOr: chain form (a `try`-marked LHS, whose own type is its
success value, or a failable RHS) bails → E2.4b (fallback routing). Pure
failable `or value` rejected ("no success value to fall back to — use
catch"). Value-carrying: tuple_get the value/error, condBr, merge the LHS
value (success) or the terminator (failure) through a block-param phi.
Multi-value bails (E2).
- inferExprType `.or_op`: a failable `or value` types as the LHS success type
(was always `.bool`); non-failable `or` still `.bool`.
Tests: examples/231-failable-or.sx (success + Bad + Empty terminators; exit
116), examples/232-failable-or-reject.sx (pure-failable `or value` rejected;
exit 1). Gates: zig build, zig build test, 270/270 examples.
26 lines
913 B
Plaintext
26 lines
913 B
Plaintext
// Failable `or` value-terminator (ERR step E2.4a). `lhs or value` where `lhs`
|
|
// is a value-carrying failable (`-> (T, !E)`): on success the result is the
|
|
// LHS value; on failure the LHS error is discarded and the result is the
|
|
// terminator value. The whole expression is non-failable (type T). The chain
|
|
// form (`try a or try b`) needs fallback-target routing and lands in E2.4b.
|
|
// Rejections: `examples/232-failable-or-reject.sx`.
|
|
|
|
#import "modules/std.sx";
|
|
|
|
E :: error { Bad, Empty }
|
|
|
|
parse :: (n: s32) -> (s32, !E) {
|
|
if n < 0 { raise error.Bad; }
|
|
if n == 0 { raise error.Empty; }
|
|
return n * 2;
|
|
}
|
|
|
|
main :: () -> s32 {
|
|
a := parse(5) or 0; // success → 10
|
|
b := parse(-1) or 99; // Bad → 99 (terminator)
|
|
c := parse(0) or 7; // Empty → 7 (terminator)
|
|
r := a + b + c; // 10 + 99 + 7 = 116
|
|
print("or result: {}\n", r);
|
|
return r;
|
|
}
|