issue-0038 fixed: closure capture through FfiIntrinsicCall args
`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.
This commit is contained in:
38
examples/103-ffi-closure-capture.sx
Normal file
38
examples/103-ffi-closure-capture.sx
Normal file
@@ -0,0 +1,38 @@
|
||||
// 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;
|
||||
}
|
||||
@@ -34,14 +34,11 @@ impl Hashable for Probe {
|
||||
}
|
||||
|
||||
// ── 3. Closure body invoking #objc_call ─────────────────────────────
|
||||
// Closure-captured `recv` isn't traced through the `#objc_call` AST
|
||||
// node by sema today, so we reach the receiver via a module-level
|
||||
// global. The lemma we lock here is that lowering routes the call
|
||||
// the same way inside a closure body as it does at top level.
|
||||
g_hasher_recv : *void = null;
|
||||
|
||||
make_hasher :: () -> Closure(s32) -> s64 {
|
||||
closure((dummy: s32) -> s64 => #objc_call(s64)(g_hasher_recv, "hash"));
|
||||
// The closure captures `recv` from its enclosing function and
|
||||
// references it inside the `#objc_call` arg list. Locked in by
|
||||
// `examples/103-ffi-closure-capture.sx`.
|
||||
make_hasher :: (recv: *void) -> Closure(s32) -> s64 {
|
||||
closure((dummy: s32) -> s64 => #objc_call(s64)(recv, "hash"));
|
||||
}
|
||||
|
||||
// ── 4. Generic function body — instantiated per call site ───────────
|
||||
@@ -66,11 +63,9 @@ main :: () -> s32 {
|
||||
print("protocol h2 = {}\n", h2 == h1 * 2);
|
||||
|
||||
// 3. closure (receives a dummy arg to keep the `Closure(T) -> R`
|
||||
// arity matching 35-closures.sx; recv comes via a global —
|
||||
// closure capture through `#objc_call` AST nodes isn't
|
||||
// traced by sema today and would error "unresolved").
|
||||
g_hasher_recv = ns_object;
|
||||
hasher := make_hasher();
|
||||
// arity matching 35-closures.sx; `recv` is captured from
|
||||
// `make_hasher`'s arg list and used inside the `#objc_call`).
|
||||
hasher := make_hasher(ns_object);
|
||||
h3 := hasher(0);
|
||||
print("closure h3 = {}\n", h3 == h1);
|
||||
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
// Closure capture analysis doesn't trace into the `FfiIntrinsicCall`
|
||||
// AST node — identifiers used inside `#objc_call` / `#jni_call` /
|
||||
// `#jni_static_call` from a closure body aren't recognized as
|
||||
// captured variables. Surfaced when writing `ffi-objc-call-09-in-
|
||||
// construct.sx`.
|
||||
//
|
||||
// Reduced repro: capture in a closure body works fine for
|
||||
// "normal" expressions (see `passthrough_works`), but the same
|
||||
// capture inside `#objc_call`'s arg list trips
|
||||
// "unresolved: 'recv'" (see `passthrough_via_objc_call` — would
|
||||
// fail at parse time, so it's commented out).
|
||||
//
|
||||
// Likely fix: in the closure free-variable analyzer (sema.zig /
|
||||
// lower.zig), add a recursive arm for `ffi_intrinsic_call` that
|
||||
// visits `return_type` + every `args[i]` the same way the `.call`
|
||||
// arm walks `callee` + `args`.
|
||||
|
||||
#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(...)`.
|
||||
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);
|
||||
|
||||
// After the fix, capture in an FfiIntrinsicCall arg list works.
|
||||
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;
|
||||
}
|
||||
@@ -5073,6 +5073,12 @@ pub const Lowering = struct {
|
||||
.defer_stmt => |ds| {
|
||||
self.collectCaptures(ds.expr, param_names, captures);
|
||||
},
|
||||
.ffi_intrinsic_call => |fic| {
|
||||
self.collectCaptures(fic.return_type, param_names, captures);
|
||||
for (fic.args) |arg| {
|
||||
self.collectCaptures(arg, param_names, captures);
|
||||
}
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
1
tests/expected/103-ffi-closure-capture.exit
Normal file
1
tests/expected/103-ffi-closure-capture.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
2
tests/expected/103-ffi-closure-capture.txt
Normal file
2
tests/expected/103-ffi-closure-capture.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
ok (passthrough works) = true
|
||||
ok (passthrough via #objc_call) = true
|
||||
@@ -1 +0,0 @@
|
||||
1
|
||||
@@ -1 +0,0 @@
|
||||
/Users/agra/projects/sx/examples/issue-0038.sx:28:48: error: unresolved: 'recv'
|
||||
Reference in New Issue
Block a user