// `Wire` (ADR 0072): what can cross hosts, decided by the compiler from // a type's structure like `Send`. Generic code says `where Wire`, and // passes the type's description; it cannot be implemented. import std/array.{type Array} import std/bytes.{type Builder} import std/dec.{type Dec} import std/ffi.{type Resource} import std/process.{type Dynamic, type Pid, type Subject} import std/ref.{type Ref} import std/u64.{type U64} fn cross :: x: a -> () where Wire { () } fn send_only :: x: a -> () where Send { () } type Point { Point(x: Int, y: Int) } type Tree { Leaf, Node(Tree, a, Tree) } type Message { Ping(reply: Subject), Points(points: Array, label: String) } // No field stores a value of `a`. type Proxy { Proxy } type Box { Box(value: a) } type Wrapper { Wrapper(inner: Box) } type Callback { Callback(run: fn(Int) -> Int) } type Counter { Counter(count: Ref) } @thread_safe type SharedHandle { SharedHandle } type Socket { Socket(fd: Resource) } // Each holds the other: one function makes both fail. type Left { Left(right: Option) } type Right { Right(left: Left, reply: fn() -> ()) } // Recursive through each other, and `Wire`. type Even { Zero, Succ(Odd) } type Odd { OddSucc(Even) } type AnyWire { AnyWire(b) -> AnyWire where Wire } type AnySendable { AnySendable(b) -> AnySendable where Send } type Expr { IntLit(Int) -> Expr, BoolLit(Bool) -> Expr } // Nested (non-regular): `Nest` holds a `Nest<(Int, Int)>`, which // holds a `Nest<((Int, Int), (Int, Int))>`, ... type Nest { NestNil, NestCons(a, Nest<(a, a)>) } type HoldsNest { HoldsNest(nest: Nest) } // Regular: the recursion passes its parameter unchanged, whatever wraps it. type Rose { Rose(a, List<(a, Rose)>) } // Through a phantom argument, which descriptors erase: not a recursion. type Phantom { Phantom(Proxy>) } // Written with result types, but neither hiding nor refining one. type Stack { Empty -> Stack, Push(a, Stack) -> Stack } // The built-in values, tuples, records, recursive types, subjects. fn crossing :: p: Pid s: Subject st: Stack r: Subject a: Array u: U64 b: Bytes pts: Array -> () { cross 1 cross 1.5 cross "text" cross b cross True cross () cross (1, "a", [True]) cross Point(1, 2) cross Node(Leaf, Point(1, 2), Leaf) cross p cross s cross a cross u cross (Some(Ping(r)), [Points(pts, "")]) cross Succ(OddSucc(Zero)) cross Wrapper(Box(Point(0, 0))) cross st cross Rose(1, []) cross Phantom(Proxy) let phantom: Proxy ()> = Proxy cross phantom } // A type with no known element: no description to pass. fn unknown :: -> () { cross [] //~ ERROR E0403 cannot tell which type should implement `Wire` } fn a_function :: -> () { cross fn x { x + 1 } //~ ERROR E0706 a function's code } fn a_callback :: c: Callback -> () { cross c //~ ERROR E0706 `Callback` has a field `run` that cannot cross hosts } fn a_boxed_function :: w: Wrapper ()> -> () { cross w //~^ ERROR E0706 `Wrapper` has a field `inner` that cannot cross hosts: `Box` has a field `value` } fn a_tree_of_functions :: t: Tree ()> -> () { cross t //~ ERROR E0706 `Tree` has a field that cannot cross hosts } fn a_subject_of_functions :: s: Subject ()> -> () { cross s //~ ERROR E0706 a `Subject`'s messages must cross hosts too } fn a_ref :: r: Ref -> () { cross r //~ ERROR E0706 a `Ref` belongs to its region } fn a_counter :: c: Counter -> () { cross c //~ ERROR E0706 `Counter` has a field `count` that cannot cross hosts } // Even a thread-safe resource: it is a handle to this process's state. fn a_resource :: r: Resource -> () { send_only r cross r //~ ERROR E0706 a `Resource` is a handle to foreign state } fn a_socket :: s: Socket -> () { cross s //~ ERROR E0706 `Socket` has a field `fd` that cannot cross hosts } fn a_dynamic :: d: Dynamic -> () { cross d //~ ERROR E0706 a `Dynamic` value's type is known only } fn a_builder :: b: Builder -> () { cross b //~ ERROR E0706 send the `Bytes` it builds } fn mutually_recursive :: l: Left -> () { cross l //~^ ERROR E0706 `Left` has a field `right` that cannot cross hosts: `Option` has a field that cannot cross hosts: `Right` has a field `reply` } fn nested :: n: Nest h: HoldsNest -> () { cross n //~^ ERROR E0706 `Nest` is a nested type: it recurses through `Nest<(a, a)>` cross h //~^ ERROR E0706 `HoldsNest` has a field `nest` that cannot cross hosts: `Nest` is a nested type } // A constructor that stores a dictionary, or hides or refines a type. fn existential :: x: AnyWire -> () { cross x //~^ ERROR E0706 constructor `AnyWire` stores a dictionary (`where Wire`) } fn hidden :: x: AnySendable y: Expr -> () { cross x //~ ERROR E0706 constructor `AnySendable` hides or refines a type cross y //~ ERROR E0706 constructor `IntLit` hides or refines a type } // What a matched constructor stores describes its hidden type. fn opened :: x: AnyWire -> () { match x { AnyWire(b) -> cross [b] } } // Generic code needs the bound, written or inferred. fn unbounded :: x: a -> () { cross x //~ ERROR E0402 `a` is not known to implement `Wire` } fn bounded :: x: a -> () where Wire { cross [(x, 1)] } fn inferred :: x { cross (Some(x), 1) } //~ TYPE inferred: fn(a) -> () where Wire fn through_inferred :: f: fn() -> () -> () { inferred f //~ ERROR E0706 a function's code } // A phantom argument need not be `Wire`, even unknown. fn phantom :: p: Proxy -> () { cross p } // `Wire` implies `Send`: what can cross hosts can cross actors. fn implies_send :: x: a -> () where Wire { send_only x send_only (x, [x]) } // A stored `Wire` too: the value was built with it. fn stored_implies_send :: x: AnyWire -> () { send_only x } fn a_stack_of_functions :: s: Stack ()> -> () { cross s //~ ERROR E0706 `Stack` has a field that cannot cross hosts } impl Wire {} //~ ERROR E0413 `Wire` cannot be implemented