Files
sx/examples/optionals/0923-optionals-narrowing-no-closure-leak.sx
agra 959845bd30 style: migrate arrow-block lambdas () => { .. } to () { .. }
The canonical sx block-body lambda is `(params) { stmts }` (and
`(params) -> Ret { stmts }`); the arrow form `=>` is for EXPRESSION bodies
(`(params) => expr`). The arrow-block hybrid `(params) => { .. }` was being
used in 33 files — convert all of them by dropping the `=>`. The two forms are
exactly equivalent (verified: identical IR and identical runtime values — the
block tail is the value with or without a `-> Ret`), so this is a pure source
cleanup: no `.ir` churn, and the only snapshot change is 0923's diagnostic
COLUMN (a negative narrowing test whose error span shifted by the removed `=> `).

Arrow EXPRESSION bodies (`=> expr`, `=> .{..}`, `=> [..]`) and `=>` inside
comments/strings were left untouched. Migrated across examples/concurrency,
examples/{closures,ffi-objc,generics,optionals,types}, issues/, and the stdlib
(io.sx, sched.sx). Suite 855/0.
2026-06-28 16:39:51 +03:00

24 lines
1.0 KiB
Plaintext

// Flow narrowing (issue 0179) does NOT cross into a nested function body. A
// closure capturing a local that is narrowed in the ENCLOSING scope still sees
// it as `?T` inside the closure — using it unguarded is a compile error.
//
// This guards the soundness fix from the adversarial review of 0179/0185: the
// narrowing gate is keyed by per-function SSA `Ref`, and a closure body's `Ref`
// space overlaps the enclosing function's, so without isolation an outer
// narrowed `Ref` would falsely permit unwrapping a not-proven-present optional
// inside the closure (`Lowering.NarrowGuard`). The closure could run later when
// the captured value is null, so the reject is mandatory — write `n!` to assert.
#import "modules/std.sx";
takes_i64 :: (x: i64) { print("{}\n", x); }
main :: () {
n : ?i64 = 7;
if n != null {
// `n` is narrowed HERE, but the closure body is a separate function:
// `n` is `?i64` inside it, so this implicit unwrap must be rejected.
g := () { takes_i64(n); };
g();
}
}