The consumer side of the error-channel tuple ABI. A value-carrying `-> (T, !E)`
failable can now be consumed by `try` and `catch` (not just destructured).
Single-value; multi-value `-> (T1, T2, !)` consumers bail (E2).
- lowerTry: a value-carrying callee returns `{v, err}`. Extract `err`
(tuple_get field 1), branch; on success the try value is `tuple_get(field 0)`,
on error propagate via emitErrorReturn (pure caller → `ret(tag)`;
value-carrying caller → `ret {undef..., tag}`). Widening now runs for
value-carrying callees too. Retires the two value-carrying bails.
- lowerCatch: a value-carrying LHS merges through a block-param phi — the
success edge feeds `tuple_get(field 0)`, the handler edge feeds the body's
value (coerced to the success type). runCatchBody factors the bound-tag body
lowering (force_block_value for the value case). Pure-failable catch
unchanged.
- A non-diverging value-carrying catch body that yields no value is now a
clean diagnostic ("`catch` body must produce a value … or diverge") instead
of coercing `void` into a bad ref / failing LLVM verification — caught by an
adversarial review of the lowering.
Tests: examples/229-value-failable-consume.sx (try in value-carrying + pure
callers, catch block/bare/match-body/diverging bodies; exit 32),
examples/230-value-failable-reject.sx (void catch body rejected; exit 1).
Gates: zig build, zig build test, 268/268 examples.
20 lines
648 B
Plaintext
20 lines
648 B
Plaintext
// Value-carrying `catch` rejection (ERR step E2.1b): when the failable LHS
|
|
// carries a value, a non-diverging catch handler must produce a value of the
|
|
// success type — a value-less (void) body is a type error (otherwise the
|
|
// success and error paths couldn't merge to one value). Diverge instead
|
|
// (`return` / `raise`) or yield a value. Positives: `examples/229-value-failable-consume.sx`.
|
|
|
|
#import "modules/std.sx";
|
|
|
|
E :: error { Bad }
|
|
|
|
parse :: (n: s32) -> (s32, !E) {
|
|
if n < 0 { raise error.Bad; }
|
|
return n;
|
|
}
|
|
|
|
main :: () -> s32 {
|
|
x := parse(-1) catch e { print("oops\n"); }; // error: body yields no value
|
|
return x;
|
|
}
|