fix: capture failable closures called via error-handling exprs

collectCaptures did not descend into catch/try/onfail/raise/multi_assign/
push/comptime/insert/spread/asm nodes, so a free variable referenced only
inside them (e.g. a failable worker called as `worker() catch {…}` in a
nested lambda) was never captured into the env struct — inside the lambda
it resolved against an empty scope and typed as 'unresolved'. Add the
missing traversal arms. The push_stmt arm also closes the noted
'free-var analysis does not descend into a nested push Context {…}' gap.

Unblocks the PLAN-IO-UNIFY Phase 3 async completion closure shape.
Lock: examples/closures/0314-closures-capture-failable-call.sx.
This commit is contained in:
agra
2026-06-28 09:18:15 +03:00
parent 213cedf0b5
commit 69a6ecfb57
5 changed files with 98 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -0,0 +1,3 @@
ok 7
caught
try 105

View File

@@ -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 => {},
}
}