Files
sx/examples/1020-errors-cleanup-body-restrictions.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

27 lines
1.0 KiB
Plaintext

// Cleanup-body control-flow restrictions (ERR step E1.7 follow-up). A `defer`
// or `onfail` body runs while the block/function is already exiting, so it has
// no target to transfer control to: `raise` / `try` / `return` / `break` /
// `continue` are all rejected inside one. The ban is transitive through nested
// `catch` bodies and loops, but NOT through a nested closure (its own function
// boundary). `raise` was already banned (E1.3); this adds the other four.
// This file is expected to FAIL compilation (exit 1).
//
// Run: ./zig-out/bin/sx run examples/237-cleanup-body-restrictions.sx
#import "modules/std.sx";
E :: error { Bad }
g :: () -> !E { return; }
f :: () -> !E {
defer { return; } // ERROR: return in defer body
onfail { try g(); } // ERROR: try in onfail body
defer { for 0..1 (i) { break; } } // ERROR: break in defer body (transitive through loop)
onfail (e) { if e == error.Bad { continue; } } // ERROR: continue in onfail body
try g();
return;
}
main :: () -> s32 { return 0; }