ERR/E2.4a: failable or value-terminator
`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.
This commit is contained in:
25
examples/231-failable-or.sx
Normal file
25
examples/231-failable-or.sx
Normal file
@@ -0,0 +1,25 @@
|
||||
// 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;
|
||||
}
|
||||
19
examples/232-failable-or-reject.sx
Normal file
19
examples/232-failable-or-reject.sx
Normal file
@@ -0,0 +1,19 @@
|
||||
// Failable `or` rejection (ERR step E2.4a): the value-terminator form
|
||||
// (`lhs or value`) requires a value-carrying failable LHS — a pure failable
|
||||
// (`-> !`) has no success value to fall back to, so `or value` is rejected
|
||||
// (use `catch` to absorb the error). The positive cases live in
|
||||
// `examples/231-failable-or.sx`.
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
E :: error { Bad }
|
||||
|
||||
must :: (n: s32) -> !E {
|
||||
if n < 0 { raise error.Bad; }
|
||||
return;
|
||||
}
|
||||
|
||||
main :: () -> s32 {
|
||||
x := must(-1) or 0; // error: `-> !` has no success value to fall back to
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user