Files
sx/examples/1036-errors-failable-smoke.sx
agra 83ec2536af lang: catch/onfail error bindings take parens
try foo() catch (e) { }   // legal
try foo() catch e { }     // parse error with a migration hint

Same capture style as the for-loop. All four catch shapes keep working
with the parenthesized binding — block, bare-expression body, and the
== match sugar — and the no-binding forms are unchanged. onfail follows
the same rule (onfail (e) { }); its expression-cleanup form is
disambiguated by the paren-group-before-brace lookahead, so
onfail (f()); stays an expression cleanup.

AST unchanged; the printer renders the parens; the #run escape help
text updated. Corpus migrated (57 catch + 3 onfail bindings, in-source
parser test strings, specs incl. grammar rules, readme untouched —
no catch examples there).

Regression: examples/1157-diagnostics-catch-binding-needs-parens.sx;
re-captured stderr for 1010/1013/1037/1123 (migrated source echoed in
carets + help text).
2026-06-10 23:05:02 +03:00

135 lines
4.5 KiB
Plaintext

// End-to-end error handling: failable functions (named + inferred sets) consumed
// through every absorbing form — destructure, `try` (in helpers), `catch` (bare-
// expr / match-body / diverging block / no-binding), `or` value-terminator,
// `onfail` cleanup interleaved with `defer`, plus `error.X` as a value and `{}`
// tag-name interpolation.
#import "modules/std.sx";
SmokeErr :: error { Empty, BadDigit, Overflow }
// value-carrying, named set
sm_parse :: (n: s32) -> (s32, !SmokeErr) {
if n < 0 { raise error.BadDigit; }
if n == 0 { raise error.Empty; }
if n > 99 { raise error.Overflow; }
return n * 2;
}
// pure failable, inferred set (ad-hoc tag)
sm_check :: (ok: bool) -> ! {
if !ok { raise error.NotReady; }
return;
}
// multi-value, inferred set: `try` propagates; SCC absorbs SmokeErr
sm_pair :: (a: s32, b: s32) -> (s32, s32, !) {
x := try sm_parse(a);
y := try sm_parse(b);
return (x, y);
}
// catch with a diverging block body
sm_or_default :: (n: s32) -> s32 {
return sm_parse(n) catch (e) {
print(" logged {}\n", e);
return -1;
};
}
// onfail + defer interleave: cleanup runs only on the error path
sm_acquire :: (fail: bool) -> (s32, !) {
defer print(" defer A\n");
onfail print(" onfail B\n");
if fail { raise error.Acquire; }
return 7;
}
// or-chain: try a, fall to try b; propagate if both fail
sm_first :: (a: s32, b: s32) -> (s32, !) {
v := try sm_parse(a) or try sm_parse(b);
return v;
}
// --- Composition (ERR E5.1): failable closures, widening, generics ---
// Closure(...) param, try-propagated (the env is carried)
sm_run :: (cb: Closure(s32) -> (s32, !SmokeErr), n: s32) -> (s32, !SmokeErr) {
return try cb(n);
}
// bare fn-type param: a NON-failable closure literal widens into the failable
// slot (the ∅-widening adapter wraps `{value, 0}`)
sm_widen :: (cb: (s32) -> (s32, !SmokeErr), n: s32) -> s32 {
return cb(n) catch (e) -1;
}
// generic ($T) value-carrying failable composition, monomorphized per call
sm_wrap :: ($T: Type, f: Closure() -> (T, !SmokeErr)) -> (T, !SmokeErr) {
return try f();
}
main :: () {
// error.X as a typed value + {} interpolation renders the tag name
e0 : SmokeErr = error.BadDigit;
print("tag: {}\n", e0);
// success destructure + error inspect
v1, err1 := sm_parse(5);
if !err1 { print("parsed: {}\n", v1); }
v2, err2 := sm_parse(-1);
if err2 == error.BadDigit { print("got: {}\n", err2); }
// catch — bare-expr body
ce := sm_parse(0) catch (e) 100;
print("catch-expr: {}\n", ce);
// catch — match-body per-tag dispatch
cm := sm_parse(200) catch (e) == {
case .Overflow: 1;
case .Empty: 2;
else: 3;
};
print("catch-match: {}\n", cm);
// catch — diverging block (in helper)
print("or-default ok: {}\n", sm_or_default(5));
print("or-default err: {}\n", sm_or_default(-5));
// or — value terminator (bare LHS; non-failable result)
ov := sm_parse(0) or 55;
print("or-value: {}\n", ov);
// or-chain via helper (first ok wins; propagation absorbed by destructure)
g, gerr := sm_first(0, 8);
if !gerr { print("or-chain: {}\n", g); }
// multi-value failable consumed by catch (tuple body)
p, q := sm_pair(0, 3) catch (e) (0, 0);
print("pair-catch: {} {}\n", p, q);
p2, q2 := sm_pair(4, 5) catch (e) (0, 0);
print("pair-ok: {} {}\n", p2, q2);
// pure failable: absorb with no-binding catch
sm_check(false) catch { print("check absorbed\n"); };
// onfail/defer interleave on error vs success
print("acquire fail:\n");
hv, herr := sm_acquire(true);
print("acquire ok:\n");
iv, ierr := sm_acquire(false);
// composition: inline failable closure literal through a Closure(...) param
cl := sm_run(closure((x: s32) -> (s32, !SmokeErr) { if x < 0 { raise error.BadDigit; } return x * 2; }), 6) catch (e) -1;
print("closure-run: {}\n", cl); // 12
print("closure-run-err: {}\n", sm_run(closure((x: s32) -> (s32, !SmokeErr) { raise error.Empty; }), 1) catch (e) -9); // -9
// non-failable closure literal widened into the failable bare slot
print("widen: {}\n", sm_widen(closure((x: s32) -> s32 => x + 1), 9)); // 10
// generic failable composition (monomorphized at s32)
print("wrap: {}\n", sm_wrap(s32, closure(() -> (s32, !SmokeErr) { return 42; })) catch (e) 0); // 42
print("errors ok\n");
}