Files
sx/examples/1046-errors-value-slot-liveness.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

61 lines
2.1 KiB
Plaintext

// Path-sensitive value-slot liveness (ERR step E1.8). After `v, err := f()`, the
// value slot `v` is "live only where `err` is proven absent". Every read of `v`
// below sits on a path where the compiler can prove `err == null`:
//
// • `if !err { … v … }` — proven inside the guard
// • `if err { return } … v …` — proven on the fall-through
// • `if err { raise } … v …` — fall-through in a failable function
// • `if err { … } else { … v … }` — proven in the else branch
// • `!err and <reads v>` — short-circuit keeps the proof
//
// A bare tag-compare (`if err == error.X`) proves NOTHING about absence — see the
// rejection regression in 1047. (Regression for the E1.8 path-sensitive slice.)
#import "modules/std.sx";
E :: error { Bad, Empty }
parse :: (n: s32) -> (s32, !E) {
if n < 0 { raise error.Bad; }
if n == 0 { raise error.Empty; }
return n * 10;
}
// Early-return guard: the fall-through proves `err` absent.
guarded :: (n: s32) -> s32 {
v, err := parse(n);
if err { return -1; }
return v; // err proven absent here
}
// `if err { raise }` in a failable function: same fall-through proof.
relay :: (n: s32) -> (s32, !E) {
v, err := parse(n);
if err { raise err; }
return v + 1; // err proven absent here
}
main :: () -> s32 {
total : s32 = 0;
// (1) proven inside `if !err`
v1, e1 := parse(5);
if !e1 { total = total + v1; } // +50
// (2) proven in the else branch
v2, e2 := parse(7);
if e2 { total = total + 1; } else { total = total + v2; } // +70
// (3) short-circuit `&&` keeps the proof for the rhs
v3, e3 := parse(3);
if !e3 and v3 > 0 { total = total + v3; } // +30
// (4) early-return / raise helpers
total = total + guarded(4); // +40
total = total + guarded(-1); // -1
total = total + (relay(2) catch (e) 0); // parse(2)=20 → +1 = 21
print("liveness total: {}\n", total); // 50+70+30+40-1+21 = 210
return total;
}