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).
This commit is contained in:
agra
2026-06-10 23:05:02 +03:00
parent 12149eb548
commit 83ec2536af
42 changed files with 158 additions and 115 deletions

View File

@@ -31,7 +31,7 @@ sm_pair :: (a: s32, b: s32) -> (s32, s32, !) {
// catch with a diverging block body
sm_or_default :: (n: s32) -> s32 {
return sm_parse(n) catch e {
return sm_parse(n) catch (e) {
print(" logged {}\n", e);
return -1;
};
@@ -61,7 +61,7 @@ sm_run :: (cb: Closure(s32) -> (s32, !SmokeErr), n: s32) -> (s32, !SmokeErr) {
// 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;
return cb(n) catch (e) -1;
}
// generic ($T) value-carrying failable composition, monomorphized per call
@@ -81,11 +81,11 @@ main :: () {
if err2 == error.BadDigit { print("got: {}\n", err2); }
// catch — bare-expr body
ce := sm_parse(0) catch e 100;
ce := sm_parse(0) catch (e) 100;
print("catch-expr: {}\n", ce);
// catch — match-body per-tag dispatch
cm := sm_parse(200) catch e == {
cm := sm_parse(200) catch (e) == {
case .Overflow: 1;
case .Empty: 2;
else: 3;
@@ -105,9 +105,9 @@ main :: () {
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);
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);
p2, q2 := sm_pair(4, 5) catch (e) (0, 0);
print("pair-ok: {} {}\n", p2, q2);
// pure failable: absorb with no-binding catch
@@ -120,15 +120,15 @@ main :: () {
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;
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
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("wrap: {}\n", sm_wrap(s32, closure(() -> (s32, !SmokeErr) { return 42; })) catch (e) 0); // 42
print("errors ok\n");
}