tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

187 lignes · 4514 octetsBrut · Historique
1// `Send` is an auto trait (ADR 0004, Revision 6): the compiler decides it
2// from a type's structure, generic code says `where Send<a>`, and it
3// cannot be implemented.
4
5import std/ffi.{type Resource}
6import std/process.{type Subject}
7import std/ref.{type Ref}
8
9fn need :: x: a -> () where Send<a>
10{
11 ()
12}
13
14type Point { Point(x: Int, y: Int) }
15
16type Tree<a> { Leaf, Node(Tree<a>, a, Tree<a>) }
17
18type Counter<h> { Counter(count: Ref<h, Int>) }
19
20type Handle { Handle }
21
22@thread_safe
23type SharedHandle { SharedHandle }
24
25// Values, recursive types, tuples, functions, thread-safe resources.
26fn sendable :: shared: Resource<SharedHandle> -> ()
27{
28 need 1
29 need "text"
30 need Point(1, 2)
31 need [Point(1, 2)]
32 need (1, "a", [True])
33 need Node(Leaf, 1, Leaf)
34 need fn x { x + 1 }
35 need shared
36 need []
37}
38
39fn a_ref :: r: Ref<h, Int> -> ()
40{
41 need r //~ ERROR E0704 `Ref<h, Int>` cannot be sent
42}
43
44fn a_counter :: c: Counter<h> -> ()
45{
46 need c //~ ERROR E0704 `Counter<h>` cannot be sent
47}
48
49fn a_tree_of_refs :: t: Tree<Ref<h, Int>> -> ()
50{
51 need t //~ ERROR E0704 `Tree<Ref<h, Int>>` cannot be sent
52}
53
54fn a_handle :: h: Resource<Handle> -> ()
55{
56 need h //~ ERROR E0704 `Resource<Handle>` cannot be sent
57}
58
59// Generic code needs the bound, written or inferred.
60fn unbounded :: x: a -> ()
61{
62 need x //~ ERROR E0402 `a` is not known to implement `Send`
63}
64
65fn bounded :: x: a -> () where Send<a>
66{
67 need [x]
68}
69
70fn inferred :: x
71{
72 need x
73}
74//~ TYPE inferred: fn(a) -> () where Send<a>
75
76fn through_inferred :: r: Ref<h, Int> -> ()
77{
78 inferred r //~ ERROR E0704 `Ref<h, Int>` cannot be sent
79}
80
81// A recursive group requires what any of its members needs.
82fn ping :: x n
83{
84 if n == 0
85 {
86 need x
87 }
88 else
89 {
90 pong x (n - 1)
91 }
92}
93fn pong :: x n
94{
95 ping x n
96}
97//~ TYPE pong: fn(a, Int) -> () where Send<a>
98
99// A hidden type is sendable only if its constructor says so.
100type Anything { Anything(b) -> Anything }
101
102type AnySendable { AnySendable(b) -> AnySendable where Send<b> }
103
104fn hidden :: x: Anything -> ()
105{
106 need x //~ ERROR E0704 `Anything` cannot be sent
107}
108
109fn hidden_but_sendable :: x: AnySendable -> ()
110{
111 need x
112}
113
114// A hidden type no field stores a value of is no obstacle: a subject is a
115// pid and a tag, and `Index`'s constructors hold only `Int`s.
116type Index<r> { IntAt(Int) -> Index<Int>, TextAt(Int) -> Index<String> }
117
118type Question { Question(Index<r>, Subject<r>) -> Question }
119
120fn hidden_but_never_stored :: q: Question -> ()
121{
122 need q
123}
124
125// A resource of an unknown kind needs `Send` of the resource itself: the
126// kind type may be `Send` without being `@thread_safe`.
127fn same_resource :: r: Resource<k> -> Resource<k>
128{
129 r
130}
131
132fn resource_relay :: r
133{
134 need (same_resource r)
135}
136//~ TYPE resource_relay: fn(Resource<a>) -> () where Send<Resource<a>>
137
138fn relay_a_handle :: h: Resource<Handle> -> ()
139{
140 resource_relay h //~ ERROR E0704 `Resource<Handle>` cannot be sent
141}
142
143fn generic_resource :: r: Resource<k> -> ()
144{
145 need r //~ ERROR E0402 `Resource<k>` is not known to implement `Send`
146}
147
148// Each type is checked once per query, however many paths reach it: here
149// 2^30 (issue #233).
150type Level0 { Level0(Level1, Level1) }
151type Level1 { Level1(Level2, Level2) }
152type Level2 { Level2(Level3, Level3) }
153type Level3 { Level3(Level4, Level4) }
154type Level4 { Level4(Level5, Level5) }
155type Level5 { Level5(Level6, Level6) }
156type Level6 { Level6(Level7, Level7) }
157type Level7 { Level7(Level8, Level8) }
158type Level8 { Level8(Level9, Level9) }
159type Level9 { Level9(Level10, Level10) }
160type Level10 { Level10(Level11, Level11) }
161type Level11 { Level11(Level12, Level12) }
162type Level12 { Level12(Level13, Level13) }
163type Level13 { Level13(Level14, Level14) }
164type Level14 { Level14(Level15, Level15) }
165type Level15 { Level15(Level16, Level16) }
166type Level16 { Level16(Level17, Level17) }
167type Level17 { Level17(Level18, Level18) }
168type Level18 { Level18(Level19, Level19) }
169type Level19 { Level19(Level20, Level20) }
170type Level20 { Level20(Level21, Level21) }
171type Level21 { Level21(Level22, Level22) }
172type Level22 { Level22(Level23, Level23) }
173type Level23 { Level23(Level24, Level24) }
174type Level24 { Level24(Level25, Level25) }
175type Level25 { Level25(Level26, Level26) }
176type Level26 { Level26(Level27, Level27) }
177type Level27 { Level27(Level28, Level28) }
178type Level28 { Level28(Level29, Level29) }
179type Level29 { Level29(Level30, Level30) }
180type Level30 { Level30(Int) }
181
182fn many_paths :: x: Level0 -> ()
183{
184 need x
185}
186
187impl Send<Point> {} //~ ERROR E0413 cannot be implemented