ERR/E1.8: reject dropping a failable's error slot on destructure

The error slot of a value-carrying failable can no longer be silently dropped
on a bare destructure. In lowerDestructureDecl, when the RHS is failable
(errorChannelOf(ty) != null), the error slot (always the last tuple field)
must be bound to a non-`_` name. Reject when it is omitted entirely (fewer
names than slots — e.g. `a, c := inc(5)` for `inc: -> (s32,s32,!E)`) or bound
to `_` (`v, _ := parse(5)`).

The `try` / `catch` / `or value` consumer forms all strip the error channel
(their result type is non-failable), so the check never fires on them — only a
bare failable destructure is rejected. Value-slot `_` discards stay legal
(`a, _, ae := pair()` binds the error).

This is the discard-rejection slice of E1.8; the path-sensitive flow-check
(value live only where err==null is provable) is a separate follow-up.

examples/236-failable-discard-reject.sx covers both rejected shapes (exit 1).
Gates: zig build, zig build test, 274/274 examples.
This commit is contained in:
agra
2026-06-01 00:40:05 +03:00
parent f96bcc4fe4
commit 66740fa95b
4 changed files with 59 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
// Failable error-slot discard rejection (ERR step E1.8 — discard slice). The
// error slot of a value-carrying failable cannot be dropped on a bare
// destructure: it must be bound (`v, err := …`) and handled, or the failure
// routed through `try` / `catch` / `or value` (all of which strip the error
// channel, so they don't reach this check). Two rejected shapes here:
// (1) omitting the error slot entirely (fewer names than slots), and
// (2) binding it to `_`.
// This file is expected to FAIL compilation (exit 1).
//
// Run: ./zig-out/bin/sx run examples/236-failable-discard-reject.sx
#import "modules/std.sx";
E :: error { Bad, Empty }
pair :: (n: s32) -> (s32, s32, !E) {
if n < 0 { raise error.Bad; }
return (n, n + 1);
}
parse :: (n: s32) -> (s32, !E) {
if n < 0 { raise error.Bad; }
return n * 2;
}
main :: () -> s32 {
a, b := pair(5); // ERROR: error slot omitted (3 slots, 2 names)
v, _ := parse(5); // ERROR: error slot discarded with `_`
return a + b + v;
}