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

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