Files
sx/examples/1045-errors-closure-var-bare-slot-reject.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

28 lines
1.2 KiB
Plaintext

// A closure VALUE (a pre-bound variable) cannot be passed into a bare
// function-pointer slot `(...) -> ...` (ERR E5.1). The bare ABI calls
// `fn_ptr(ctx, args)` with no env channel, so a closure's environment can't be
// carried — passing one is unsound (drops env / shifts args / segfaults on a
// capturing closure). Only a closure LITERAL crosses this boundary (lowerLambda
// emits a static adapter); a variable is rejected with a pointer to the idiom.
//
// The fix for these is either to pass the literal directly, or to type the
// parameter `Closure(...)` so the environment is carried (the idiomatic form).
#import "modules/std.sx";
E :: error { Z }
bare :: (cb: (s64) -> s64, n: s64) -> s64 { return cb(n); }
baref :: (cb: (s64) -> (s64, !E), n: s64) -> s64 { return cb(n) catch (e) -1; }
main :: () -> s32 {
inc := closure((x: s64) -> s64 => x + 1); // capture-free closure var
base := 100;
add := closure((x: s64) -> s64 => x + base); // CAPTURING closure var
_ := bare(inc, 9); // reject: closure value → bare slot
_ := baref(inc, 9); // reject: also the ∅-widening crossing
_ := bare(add, 9); // reject: capturing closure → bare slot
return 0;
}