ERR/E1.3: raise sema + pure-failable lowering
`raise EXPR` now terminates a failable function via the error channel. Scope (Option 2): full raise sema checks + lowering for the pure-failable shape (`-> !` / `-> !Named`); the value-carrying `-> (T..., !)` shape bails loudly, deferred to E2's error-channel tuple ABI. - lowerStmt + tryLowerAsExpr: `.raise_stmt` -> lowerRaise (also routes a raise that is a block's last statement, which previously hit unknown_expr) - lowerRaise: failable-context check (effectiveReturnType + errorChannelOf); literal membership via lowerErrorTagLiteral; variable form subset-checked via checkErrorSetSubset; pure-failable emits ret(tag) - lowerErrorTagLiteral skips membership for the bare-`!` inferred placeholder - plain `return;` in a pure-failable fn emits ret(0) (success / no error) - parser: in_defer_body flag rejects `raise` inside a `defer` body Tests: examples/219-raise.sx (positive, exit 8), examples/220-raise-rejections.sx (3 sema rejections, exit 1), inline parser test for raise-in-defer. Gates: zig build, zig build test, 258/258 examples.
This commit is contained in:
26
examples/219-raise.sx
Normal file
26
examples/219-raise.sx
Normal file
@@ -0,0 +1,26 @@
|
||||
// First runnable `raise` (ERR step E1.3). A `-> !Named` (pure failable)
|
||||
// function terminates via the error channel with `raise error.X`; a plain
|
||||
// `return;` is the success exit (error slot 0). The caller binds the result
|
||||
// and inspects it with the enum-like `==`. The value-carrying `-> (T, !)`
|
||||
// shape lands with the error-channel tuple ABI in ERR phase E2.
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
ParseErr :: error { BadDigit, Overflow, Empty }
|
||||
|
||||
// Pure failable: raises on bad input, otherwise succeeds (error slot 0).
|
||||
check :: (n: s32) -> !ParseErr {
|
||||
if n < 0 { raise error.BadDigit; }
|
||||
return; // success — no error
|
||||
}
|
||||
|
||||
main :: () -> s32 {
|
||||
good := check(7); // success path -> no error
|
||||
bad := check(-1); // raise path -> BadDigit
|
||||
r : s32 = 0;
|
||||
if bad == error.BadDigit { r = r + 8; } // true -> +8
|
||||
if good == error.BadDigit { r = r + 1; } // false (success = no error)
|
||||
if bad == error.Overflow { r = r + 2; } // false (raised BadDigit)
|
||||
print("raise result: {}\n", r); // -> 8
|
||||
return r;
|
||||
}
|
||||
32
examples/220-raise-rejections.sx
Normal file
32
examples/220-raise-rejections.sx
Normal file
@@ -0,0 +1,32 @@
|
||||
// `raise` rejections (ERR step E1.3):
|
||||
// - `raise` is only valid inside a failable function,
|
||||
// - a literal `raise error.X` must name a tag in the function's set,
|
||||
// - a variable `raise e` must carry a set that is a subset of the
|
||||
// function's set.
|
||||
// The positive case lives in `examples/219-raise.sx`. Parse-time rejections
|
||||
// (`raise` in expression position / inside `defer` / `onfail`) are covered by
|
||||
// the inline parser tests.
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
ParseErr :: error { BadDigit, Overflow }
|
||||
OtherErr :: error { Weird }
|
||||
|
||||
// Literal tag not in the declared set.
|
||||
bad_tag :: () -> !ParseErr {
|
||||
raise error.NotInSet; // error: NotInSet not in ParseErr
|
||||
}
|
||||
|
||||
// Variable whose error set is not a subset of the function's set.
|
||||
makes_other :: () -> !OtherErr { return; }
|
||||
relay :: () -> !ParseErr {
|
||||
e := makes_other(); // e : OtherErr
|
||||
raise e; // error: OtherErr not subset of ParseErr
|
||||
}
|
||||
|
||||
main :: () -> s32 {
|
||||
x := bad_tag(); // force bad_tag to lower
|
||||
y := relay(); // force relay to lower
|
||||
raise error.BadDigit; // error: main (-> s32) is not failable
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user