Files
sx/examples/errors/1046-errors-value-slot-liveness.sx
agra 989e18b760 feat: tuple syntax cutover — Tuple(...) type + .(...) value
Replace the bare-paren tuple grammar with explicit, position-unambiguous
forms, mirroring how structs work:

  type     `(A, B)`        -> `Tuple(A, B)`          (named keeps `:`)
  value    `(a, b)`        -> `.(a, b)`              (named uses `=`)
  typed    (new)           -> `Tuple(A, B).(a, b)`   (like `Point.{...}`)
  failable `-> (T, !)`     -> `-> T !`
           `-> (T1, T2, !)`-> `-> Tuple(T1, T2) !`   (channel outside Tuple)

Bare `(...)` is now grouping only, everywhere; a comma in bare parens is a
hard error with a migration hint. Grouping, function types `(A, B) -> R`,
param lists, lambdas, and match bindings are unaffected.

`Tuple(...)` is strictly a TYPE in every position (including `size_of` /
`type_info` args); a tuple VALUE comes only from `.(...)` (anonymous) or
`Tuple(...).(...)` (explicitly typed). A bare `Tuple(1, 2)` is a tuple
type with non-type elements -> rejected.

The ~110 tuple-bearing corpus files were migrated with a one-shot
AST-aware migrator (the `sx migrate` tool from the prior commit, removed
here). New examples: 0130 (new syntax), 0131 (typed construction), 1060
(named-tuple failable return). 1116 golden updated for the new hint text.
2026-06-25 17:53:57 +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: i32) -> i32 !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: i32) -> i32 {
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: i32) -> i32 !E {
v, err := parse(n);
if err { raise err; }
return v + 1; // err proven absent here
}
main :: () -> i32 {
total : i32 = 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;
}