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.
Dernier runRéussi#115 · main · 21 min 41 s
0Étoiles
0Forks
40Issues
| 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 | |
| 5 | import std/ffi.{type Resource} |
| 6 | import std/process.{type Subject} |
| 7 | import std/ref.{type Ref} |
| 8 | |
| 9 | fn need :: x: a -> () where Send<a> |
| 10 | { |
| 11 | () |
| 12 | } |
| 13 | |
| 14 | type Point { Point(x: Int, y: Int) } |
| 15 | |
| 16 | type Tree<a> { Leaf, Node(Tree<a>, a, Tree<a>) } |
| 17 | |
| 18 | type Counter<h> { Counter(count: Ref<h, Int>) } |
| 19 | |
| 20 | type Handle { Handle } |
| 21 | |
| 22 | @thread_safe |
| 23 | type SharedHandle { SharedHandle } |
| 24 | |
| 25 | // Values, recursive types, tuples, functions, thread-safe resources. |
| 26 | fn 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 | |
| 39 | fn a_ref :: r: Ref<h, Int> -> () |
| 40 | { |
| 41 | need r //~ ERROR E0704 `Ref<h, Int>` cannot be sent |
| 42 | } |
| 43 | |
| 44 | fn a_counter :: c: Counter<h> -> () |
| 45 | { |
| 46 | need c //~ ERROR E0704 `Counter<h>` cannot be sent |
| 47 | } |
| 48 | |
| 49 | fn a_tree_of_refs :: t: Tree<Ref<h, Int>> -> () |
| 50 | { |
| 51 | need t //~ ERROR E0704 `Tree<Ref<h, Int>>` cannot be sent |
| 52 | } |
| 53 | |
| 54 | fn 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. |
| 60 | fn unbounded :: x: a -> () |
| 61 | { |
| 62 | need x //~ ERROR E0402 `a` is not known to implement `Send` |
| 63 | } |
| 64 | |
| 65 | fn bounded :: x: a -> () where Send<a> |
| 66 | { |
| 67 | need [x] |
| 68 | } |
| 69 | |
| 70 | fn inferred :: x |
| 71 | { |
| 72 | need x |
| 73 | } |
| 74 | //~ TYPE inferred: fn(a) -> () where Send<a> |
| 75 | |
| 76 | fn 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. |
| 82 | fn ping :: x n |
| 83 | { |
| 84 | if n == 0 |
| 85 | { |
| 86 | need x |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | pong x (n - 1) |
| 91 | } |
| 92 | } |
| 93 | fn 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. |
| 100 | type Anything { Anything(b) -> Anything } |
| 101 | |
| 102 | type AnySendable { AnySendable(b) -> AnySendable where Send<b> } |
| 103 | |
| 104 | fn hidden :: x: Anything -> () |
| 105 | { |
| 106 | need x //~ ERROR E0704 `Anything` cannot be sent |
| 107 | } |
| 108 | |
| 109 | fn 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. |
| 116 | type Index<r> { IntAt(Int) -> Index<Int>, TextAt(Int) -> Index<String> } |
| 117 | |
| 118 | type Question { Question(Index<r>, Subject<r>) -> Question } |
| 119 | |
| 120 | fn 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`. |
| 127 | fn same_resource :: r: Resource<k> -> Resource<k> |
| 128 | { |
| 129 | r |
| 130 | } |
| 131 | |
| 132 | fn resource_relay :: r |
| 133 | { |
| 134 | need (same_resource r) |
| 135 | } |
| 136 | //~ TYPE resource_relay: fn(Resource<a>) -> () where Send<Resource<a>> |
| 137 | |
| 138 | fn relay_a_handle :: h: Resource<Handle> -> () |
| 139 | { |
| 140 | resource_relay h //~ ERROR E0704 `Resource<Handle>` cannot be sent |
| 141 | } |
| 142 | |
| 143 | fn 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). |
| 150 | type Level0 { Level0(Level1, Level1) } |
| 151 | type Level1 { Level1(Level2, Level2) } |
| 152 | type Level2 { Level2(Level3, Level3) } |
| 153 | type Level3 { Level3(Level4, Level4) } |
| 154 | type Level4 { Level4(Level5, Level5) } |
| 155 | type Level5 { Level5(Level6, Level6) } |
| 156 | type Level6 { Level6(Level7, Level7) } |
| 157 | type Level7 { Level7(Level8, Level8) } |
| 158 | type Level8 { Level8(Level9, Level9) } |
| 159 | type Level9 { Level9(Level10, Level10) } |
| 160 | type Level10 { Level10(Level11, Level11) } |
| 161 | type Level11 { Level11(Level12, Level12) } |
| 162 | type Level12 { Level12(Level13, Level13) } |
| 163 | type Level13 { Level13(Level14, Level14) } |
| 164 | type Level14 { Level14(Level15, Level15) } |
| 165 | type Level15 { Level15(Level16, Level16) } |
| 166 | type Level16 { Level16(Level17, Level17) } |
| 167 | type Level17 { Level17(Level18, Level18) } |
| 168 | type Level18 { Level18(Level19, Level19) } |
| 169 | type Level19 { Level19(Level20, Level20) } |
| 170 | type Level20 { Level20(Level21, Level21) } |
| 171 | type Level21 { Level21(Level22, Level22) } |
| 172 | type Level22 { Level22(Level23, Level23) } |
| 173 | type Level23 { Level23(Level24, Level24) } |
| 174 | type Level24 { Level24(Level25, Level25) } |
| 175 | type Level25 { Level25(Level26, Level26) } |
| 176 | type Level26 { Level26(Level27, Level27) } |
| 177 | type Level27 { Level27(Level28, Level28) } |
| 178 | type Level28 { Level28(Level29, Level29) } |
| 179 | type Level29 { Level29(Level30, Level30) } |
| 180 | type Level30 { Level30(Int) } |
| 181 | |
| 182 | fn many_paths :: x: Level0 -> () |
| 183 | { |
| 184 | need x |
| 185 | } |
| 186 | |
| 187 | impl Send<Point> {} //~ ERROR E0413 cannot be implemented |