// What a closure captured is checked when it is sent (ADR 0004, Revision // 6): function types carry a capture row, `send fn` is a function that can // be sent, and a plain `fn` field or result may hold any function. import std/ffi.{type Resource} import std/process.{type Subject, type proc} import std/ref.{type Ref} effect note { fn note :: text: String -> () } type Holder { Holder(callback: fn() -> Int) } type Task { Task(run: send fn() -> Int) } type Handle { Handle } // A spawned function is sent: it may capture only sendable values. fn spawns_fine :: n: Int reply: Subject -> () / { let _ = process.spawn fn { process.send reply (n + 1) } () } fn spawns_a_ref :: counter: Ref -> () / { let _ = process.spawn fn //~ ERROR E0704 `Ref` cannot be sent { let _ = counter () } () } // A pinned resource is no better: a spawned closure may not capture one. fn spawns_a_resource :: h: Resource -> () / { let _ = process.spawn fn //~ ERROR E0704 `Resource` cannot be sent { let _ = h () } () } // A closure using a handler of this actor cannot leave it. fn sends_a_handler :: -> () / { handle { let mine = process.subject () process.send mine fn { note "hi" } //~ ERROR E0704 this function cannot be sent } with { note _ -> () } } // Generic captures need the bound, written or inferred. fn spawns_generic :: x: a reply: Subject -> () / { let _ = process.spawn fn //~ ERROR E0402 `a` is not known to implement `Send` { let _ = x process.send reply 0 } () } fn spawns_generic_bounded :: x: a reply: Subject -> () / where Send { let _ = process.spawn fn { process.send reply x } () } fn spawns_inferred :: x reply { process.spawn fn { process.send reply x } } //~ TYPE spawns_inferred: fn(a, Subject) -> Pid / where Send // A closure sent as a message. fn sends_closures :: to: Subject Int> n: Int r: Ref -> () / { process.send to fn { n * 2 } process.send to fn //~ ERROR E0704 `Ref` cannot be sent { let _ = r n } } // A sent closure may not capture a pinned resource either. fn sends_a_resource :: to: Subject Int> h: Resource -> () / { process.send to fn //~ ERROR E0704 `Resource` cannot be sent { let _ = h 1 } } // A plain `fn` parameter may be given any function: it cannot be sent. fn forwards :: f: fn() -> Int to: Subject Int> -> () / { process.send to f //~ ERROR E0202 type mismatch } fn forwards_sendable :: f: send fn() -> Int to: Subject Int> -> () / { process.send to f } // A plain `fn` field holds any function, so its type is never `Send`. fn holders :: counter: Ref to: Subject -> () / { let _ = Holder( fn { let _ = counter 1 } ) process.send to Holder(fn { 1 }) //~ ERROR E0704 `Holder` cannot be sent } fn tasks :: n: Int to: Subject -> () / { process.send to Task(fn { n }) } fn task_with_a_ref :: counter: Ref -> Task { Task( fn //~ ERROR E0704 `Ref` cannot be sent { let _ = counter 1 } ) } // A closure kept by this actor may capture anything, and be returned. fn kept :: counter: Ref -> fn() -> Ref { fn { counter } } // A closure returning a checked closure is sendable as a whole. fn nested :: n: Int to: Subject send fn() -> Int> -> () / { process.send to fn { fn { n } } }