diff --git a/examples/103-ffi-closure-capture.sx b/examples/103-ffi-closure-capture.sx new file mode 100644 index 0000000..6fae2cb --- /dev/null +++ b/examples/103-ffi-closure-capture.sx @@ -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; +} diff --git a/examples/ffi-objc-call-09-in-construct.sx b/examples/ffi-objc-call-09-in-construct.sx index f77b1c3..63ce79d 100644 --- a/examples/ffi-objc-call-09-in-construct.sx +++ b/examples/ffi-objc-call-09-in-construct.sx @@ -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); diff --git a/examples/issue-0038.sx b/examples/issue-0038.sx deleted file mode 100644 index 7f7083e..0000000 --- a/examples/issue-0038.sx +++ /dev/null @@ -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; -} diff --git a/src/ir/lower.zig b/src/ir/lower.zig index f98a4e2..89efb5b 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -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 => {}, } } diff --git a/tests/expected/103-ffi-closure-capture.exit b/tests/expected/103-ffi-closure-capture.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/tests/expected/103-ffi-closure-capture.exit @@ -0,0 +1 @@ +0 diff --git a/tests/expected/103-ffi-closure-capture.txt b/tests/expected/103-ffi-closure-capture.txt new file mode 100644 index 0000000..211a2f2 --- /dev/null +++ b/tests/expected/103-ffi-closure-capture.txt @@ -0,0 +1,2 @@ +ok (passthrough works) = true +ok (passthrough via #objc_call) = true diff --git a/tests/expected/issue-0038.exit b/tests/expected/issue-0038.exit deleted file mode 100644 index d00491f..0000000 --- a/tests/expected/issue-0038.exit +++ /dev/null @@ -1 +0,0 @@ -1 diff --git a/tests/expected/issue-0038.txt b/tests/expected/issue-0038.txt deleted file mode 100644 index b420371..0000000 --- a/tests/expected/issue-0038.txt +++ /dev/null @@ -1 +0,0 @@ -/Users/agra/projects/sx/examples/issue-0038.sx:28:48: error: unresolved: 'recv'