diff --git a/examples/closures/0314-closures-capture-failable-call.sx b/examples/closures/0314-closures-capture-failable-call.sx new file mode 100644 index 00000000..48a4f81d --- /dev/null +++ b/examples/closures/0314-closures-capture-failable-call.sx @@ -0,0 +1,44 @@ +// A captured FAILABLE closure stays failable when CALLED inside a nested +// closure body. The free-variable capture analysis must descend into the +// error-handling expressions (`catch`, `try`) that the nested closure uses to +// consume the captured worker's error channel — otherwise the worker is never +// captured into the env, resolves against an empty scope inside the lambda, and +// the call types as `unresolved` (so `catch`/`try` reject it). +// +// Regression (PLAN-IO-UNIFY Phase 3 blocker): the async completion closure +// `() => { f.value = worker() catch {…} }` captures a `Closure() -> ($R, !)` +// worker and consumes its error channel — exactly this shape. +#import "modules/std.sx"; + +Box :: struct { run: Closure() -> void; } + +// `catch` path: the nested closure absorbs the worker's error. +run_catch :: (worker: Closure() -> (i64, !)) { + b : Box = ---; + b.run = () => { + v := worker() catch { + print("caught\n"); + return; + }; + print("ok {}\n", v); + }; + b.run(); +} + +// `try` path: the nested closure is itself failable and propagates. +mk_trier :: (worker: Closure() -> (i64, !)) -> Closure() -> (i64, !) { + return () -> (i64, !) => { + v := try worker(); + v + 100 + }; +} + +main :: () -> i64 { + run_catch(() -> (i64, !) => { 7 }); // ok 7 + run_catch(() -> (i64, !) => { raise error.Bad; }); // caught + + t := mk_trier(() -> (i64, !) => { 5 }); + r := t() catch { return 1; }; + print("try {}\n", r); // try 105 + return 0; +} diff --git a/examples/closures/expected/0314-closures-capture-failable-call.exit b/examples/closures/expected/0314-closures-capture-failable-call.exit new file mode 100644 index 00000000..573541ac --- /dev/null +++ b/examples/closures/expected/0314-closures-capture-failable-call.exit @@ -0,0 +1 @@ +0 diff --git a/examples/closures/expected/0314-closures-capture-failable-call.stderr b/examples/closures/expected/0314-closures-capture-failable-call.stderr new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/examples/closures/expected/0314-closures-capture-failable-call.stderr @@ -0,0 +1 @@ + diff --git a/examples/closures/expected/0314-closures-capture-failable-call.stdout b/examples/closures/expected/0314-closures-capture-failable-call.stdout new file mode 100644 index 00000000..118168ae --- /dev/null +++ b/examples/closures/expected/0314-closures-capture-failable-call.stdout @@ -0,0 +1,3 @@ +ok 7 +caught +try 105 diff --git a/src/ir/lower/closure.zig b/src/ir/lower/closure.zig index 5b7e8daf..8d66a84a 100644 --- a/src/ir/lower/closure.zig +++ b/src/ir/lower/closure.zig @@ -699,6 +699,55 @@ pub fn collectCaptures(self: *Lowering, node: *const Node, param_names: *std.Str self.collectCaptures(arg, param_names, captures); } }, + // Error-handling expressions/statements carry sub-expressions that may + // reference captured variables (e.g. a captured failable closure called + // as `worker() catch { … }` inside a nested lambda body). Without these + // arms the operand never gets captured, so inside the lambda the call + // resolves against an empty scope and types as `.unresolved`. + .try_expr => |te| { + self.collectCaptures(te.operand, param_names, captures); + }, + .catch_expr => |ce| { + self.collectCaptures(ce.operand, param_names, captures); + self.collectCaptures(ce.body, param_names, captures); + }, + .onfail_stmt => |os| { + self.collectCaptures(os.body, param_names, captures); + }, + .raise_stmt => |rs| { + self.collectCaptures(rs.tag, param_names, captures); + }, + .multi_assign => |ma| { + for (ma.targets) |t| self.collectCaptures(t, param_names, captures); + for (ma.values) |v| self.collectCaptures(v, param_names, captures); + }, + // A `push Context { … }` block inside a lambda body is a nested scope + // whose statements can reference captures (the install-the-scheduler + // pattern `push Context.{ io = … } { worker() }`). Descend into both + // the context expression and the body. + .push_stmt => |ps| { + self.collectCaptures(ps.context_expr, param_names, captures); + self.collectCaptures(ps.body, param_names, captures); + }, + .comptime_expr => |ce| { + self.collectCaptures(ce.expr, param_names, captures); + }, + .insert_expr => |ie| { + self.collectCaptures(ie.expr, param_names, captures); + }, + .spread_expr => |se| { + self.collectCaptures(se.operand, param_names, captures); + }, + // Inline-asm operand payloads carry input/place expressions that can + // reference captured variables (`asm { … [v] "+m" -> @(p.*) }` where `p` + // is an outer var). `out_value` payloads are Type nodes — descending is + // harmless (the identifier arm filters type/fn names from capture). + .asm_expr => |ae| { + self.collectCaptures(ae.template, param_names, captures); + for (ae.operands) |op| { + self.collectCaptures(op.payload, param_names, captures); + } + }, else => {}, } }