// `Send` is an auto trait (ADR 0004, Revision 6): the compiler decides it
// from a type's structure, generic code says `where Send`, and it
// cannot be implemented.
import std/ffi.{type Resource}
import std/process.{type Subject}
import std/ref.{type Ref}
fn need :: x: a -> () where Send
{
()
}
type Point { Point(x: Int, y: Int) }
type Tree { Leaf, Node(Tree, a, Tree) }
type Counter { Counter(count: Ref) }
type Handle { Handle }
@thread_safe
type SharedHandle { SharedHandle }
// Values, recursive types, tuples, functions, thread-safe resources.
fn sendable :: shared: Resource -> ()
{
need 1
need "text"
need Point(1, 2)
need [Point(1, 2)]
need (1, "a", [True])
need Node(Leaf, 1, Leaf)
need fn x { x + 1 }
need shared
need []
}
fn a_ref :: r: Ref -> ()
{
need r //~ ERROR E0704 `Ref` cannot be sent
}
fn a_counter :: c: Counter -> ()
{
need c //~ ERROR E0704 `Counter` cannot be sent
}
fn a_tree_of_refs :: t: Tree[> -> ()
{
need t //~ ERROR E0704 `Tree][>` cannot be sent
}
fn a_handle :: h: Resource -> ()
{
need h //~ ERROR E0704 `Resource` cannot be sent
}
// Generic code needs the bound, written or inferred.
fn unbounded :: x: a -> ()
{
need x //~ ERROR E0402 `a` is not known to implement `Send`
}
fn bounded :: x: a -> () where Send
{
need [x]
}
fn inferred :: x
{
need x
}
//~ TYPE inferred: fn(a) -> () where Send
fn through_inferred :: r: Ref -> ()
{
inferred r //~ ERROR E0704 `Ref` cannot be sent
}
// A recursive group requires what any of its members needs.
fn ping :: x n
{
if n == 0
{
need x
}
else
{
pong x (n - 1)
}
}
fn pong :: x n
{
ping x n
}
//~ TYPE pong: fn(a, Int) -> () where Send
// A hidden type is sendable only if its constructor says so.
type Anything { Anything(b) -> Anything }
type AnySendable { AnySendable(b) -> AnySendable where Send }
fn hidden :: x: Anything -> ()
{
need x //~ ERROR E0704 `Anything` cannot be sent
}
fn hidden_but_sendable :: x: AnySendable -> ()
{
need x
}
// A hidden type no field stores a value of is no obstacle: a subject is a
// pid and a tag, and `Index`'s constructors hold only `Int`s.
type Index { IntAt(Int) -> Index, TextAt(Int) -> Index }
type Question { Question(Index, Subject) -> Question }
fn hidden_but_never_stored :: q: Question -> ()
{
need q
}
// A resource of an unknown kind needs `Send` of the resource itself: the
// kind type may be `Send` without being `@thread_safe`.
fn same_resource :: r: Resource -> Resource
{
r
}
fn resource_relay :: r
{
need (same_resource r)
}
//~ TYPE resource_relay: fn(Resource) -> () where Send>
fn relay_a_handle :: h: Resource -> ()
{
resource_relay h //~ ERROR E0704 `Resource` cannot be sent
}
fn generic_resource :: r: Resource -> ()
{
need r //~ ERROR E0402 `Resource` is not known to implement `Send`
}
// Each type is checked once per query, however many paths reach it: here
// 2^30 (issue #233).
type Level0 { Level0(Level1, Level1) }
type Level1 { Level1(Level2, Level2) }
type Level2 { Level2(Level3, Level3) }
type Level3 { Level3(Level4, Level4) }
type Level4 { Level4(Level5, Level5) }
type Level5 { Level5(Level6, Level6) }
type Level6 { Level6(Level7, Level7) }
type Level7 { Level7(Level8, Level8) }
type Level8 { Level8(Level9, Level9) }
type Level9 { Level9(Level10, Level10) }
type Level10 { Level10(Level11, Level11) }
type Level11 { Level11(Level12, Level12) }
type Level12 { Level12(Level13, Level13) }
type Level13 { Level13(Level14, Level14) }
type Level14 { Level14(Level15, Level15) }
type Level15 { Level15(Level16, Level16) }
type Level16 { Level16(Level17, Level17) }
type Level17 { Level17(Level18, Level18) }
type Level18 { Level18(Level19, Level19) }
type Level19 { Level19(Level20, Level20) }
type Level20 { Level20(Level21, Level21) }
type Level21 { Level21(Level22, Level22) }
type Level22 { Level22(Level23, Level23) }
type Level23 { Level23(Level24, Level24) }
type Level24 { Level24(Level25, Level25) }
type Level25 { Level25(Level26, Level26) }
type Level26 { Level26(Level27, Level27) }
type Level27 { Level27(Level28, Level28) }
type Level28 { Level28(Level29, Level29) }
type Level29 { Level29(Level30, Level30) }
type Level30 { Level30(Int) }
fn many_paths :: x: Level0 -> ()
{
need x
}
impl Send {} //~ ERROR E0413 cannot be implemented
]