`collectCaptures` in `src/ir/lower.zig` was the closure free-variable
analyzer that decides which names from a closure body need to be
boxed into the env struct at lambda-build time. Its switch on AST
node kind enumerated every other shape (`.call`, `.if_expr`,
`.match_expr`, `.for_expr`, etc.) but no arm for `.ffi_intrinsic_call`,
so the trailing `else => {}` quietly dropped its `args[]` and
`return_type` walks. Names referenced inside `#objc_call(T)(recv,
"sel:", ...)` from a closure body never made it into the captures
list, so when lowering bound the closure scope from env, those names
came back as "unresolved".
The fix adds the missing arm — walk `return_type` and every `args[i]`
the same way `.call` walks `callee` + `args`.
Companion changes:
- `examples/issue-0038.sx` → `examples/103-ffi-closure-capture.sx`
(out of the open-issue namespace; comment header tightened to
describe the feature, not the historical bug).
- `examples/ffi-objc-call-09-in-construct.sx` drops the
`g_hasher_recv` module-global workaround that was added for this
bug — the closure now captures `recv` from `make_hasher`'s arg
list normally.
39 lines
1.4 KiB
Plaintext
39 lines
1.4 KiB
Plaintext
// Closure free-variable capture works through `FfiIntrinsicCall`
|
|
// nodes — names referenced inside `#objc_call` / `#jni_call` /
|
|
// `#jni_static_call` argument lists from inside a closure body are
|
|
// recognized as captured variables and bound from the closure's env
|
|
// struct at call time. `passthrough_works` is the baseline (normal
|
|
// expression capture); `passthrough_via_objc_call` exercises the same
|
|
// capture through an FFI intrinsic call's arg list.
|
|
|
|
#import "modules/std.sx";
|
|
#import "modules/compiler.sx";
|
|
#import "modules/std/objc.sx";
|
|
|
|
passthrough_works :: (recv: *void) -> Closure(s32) -> *void {
|
|
closure((d: s32) -> *void => recv); // captures `recv` — fine
|
|
}
|
|
|
|
passthrough_via_objc_call :: (recv: *void) -> Closure(s32) -> s64 {
|
|
// Same `recv` capture, but inside `#objc_call(...)`'s arg list.
|
|
closure((d: s32) -> s64 => #objc_call(s64)(recv, "hash"));
|
|
}
|
|
|
|
main :: () -> s32 {
|
|
inline if OS == .macos {
|
|
f := passthrough_works(null);
|
|
p := f(0);
|
|
print("ok (passthrough works) = {}\n", p == null);
|
|
|
|
// Capture inside the `#objc_call` arg list.
|
|
ns_object := objc_getClass("NSObject".ptr);
|
|
g := passthrough_via_objc_call(ns_object);
|
|
h := g(0);
|
|
print("ok (passthrough via #objc_call) = {}\n", h != 0);
|
|
}
|
|
inline if OS != .macos {
|
|
print("skipped (not macos)\n");
|
|
}
|
|
0;
|
|
}
|