Skip to content

Commit 407712f

Browse files
committed
Add examples from reference.md to documentation tests
1 parent 4f307d8 commit 407712f

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

jane/doc/extensions/uniqueness/reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ val store : t @ aliased -> unit
2020
let okay t =
2121
if flip_coin ()
2222
then free t
23-
else store t; store t
23+
else (store t; store t)
2424
```
2525

2626
This might be surprising coming from a language that uses ownership like Rust.

testsuite/tests/typing-unique/unique_documentation.ml

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
(*******************************)
88
(* Examples from documentation *)
99

10-
type t = Con of { field : int }
10+
(*************)
11+
(* intro.md *)
12+
13+
type t = Con of { field : int list }
1114

1215
let free : t @ unique -> unit = fun t -> ()
1316
let free_field (unique_ i) = ()
1417
let store : t @ aliased -> unit = fun t -> ()
1518
let store_field i = ()
1619
let flip_coin () = true
1720
[%%expect{|
18-
type t = Con of { field : int; }
21+
type t = Con of { field : int list; }
1922
val free : unique_ t -> unit @@ global many = <fun>
2023
val free_field : ('a : value_or_null). unique_ 'a -> unit @@ global many =
2124
<fun>
@@ -97,8 +100,11 @@ let okay t =
97100
val okay : t -> unit @@ global many = <fun>
98101
|}]
99102

103+
(****************)
104+
(* pitfalls.md *)
105+
100106
let module_ret_unique =
101-
let mk () = Con { field = 1 } in
107+
let mk () = Con { field = [1] } in
102108
let use () = free (mk ()) in
103109
()
104110
[%%expect{|
@@ -111,7 +117,7 @@ val module_ret_unique : unit @@ global many = ()
111117
|}]
112118

113119
module Mk = struct
114-
let mk () = Con { field = 1 }
120+
let mk () = Con { field = [1] }
115121
end
116122

117123
let module_ret_unique =
@@ -178,6 +184,79 @@ val set_all_zero : ('a : value_or_null). unique_ 'a -> 'a @@ global many =
178184
<fun>
179185
|}]
180186

187+
(****************)
188+
(* reference.md *)
189+
190+
type t = { field1 : t; field2 : t }
191+
192+
let free : t @ unique -> unit = fun t -> ()
193+
let free_field (unique_ i) = ()
194+
let store : t @ aliased -> unit = fun t -> ()
195+
let store_field i = ()
196+
let flip_coin () = true
197+
[%%expect{|
198+
type t = { field1 : t; field2 : t; }
199+
val free : unique_ t -> unit @@ global many = <fun>
200+
val free_field : ('a : value_or_null). unique_ 'a -> unit @@ global many =
201+
<fun>
202+
val store : t -> unit @@ global many = <fun>
203+
val store_field : ('a : value_or_null). 'a -> unit @@ global many = <fun>
204+
val flip_coin : unit -> bool @@ global many = <fun>
205+
|}]
206+
207+
let okay t =
208+
if flip_coin ()
209+
then free t
210+
else (store t; store t)
211+
[%%expect{|
212+
val okay : unique_ t -> unit @@ global many = <fun>
213+
|}]
214+
215+
let okay r =
216+
free r.field1;
217+
match r with
218+
| { field2 } -> free field2
219+
[%%expect{|
220+
val okay : unique_ t -> unit @@ global many = <fun>
221+
|}]
222+
223+
let bad r =
224+
free r.field1;
225+
match r with
226+
| { field2 } -> free r
227+
[%%expect{|
228+
Line 4, characters 23-24:
229+
4 | | { field2 } -> free r
230+
^
231+
Error: This value is used here,
232+
but part of it has already been used as unique:
233+
Line 2, characters 7-15:
234+
2 | free r.field1;
235+
^^^^^^^^
236+
237+
|}]
238+
239+
let okay r =
240+
let x = r in
241+
free x.field1;
242+
match r with
243+
| { field2 } -> free field2
244+
[%%expect{|
245+
val okay : unique_ t -> unit @@ global many = <fun>
246+
|}]
247+
248+
let bad r =
249+
let x = Fun.id r in
250+
free x.field1;
251+
match r with
252+
| { field2 } -> free field2
253+
[%%expect{|
254+
Line 3, characters 7-15:
255+
3 | free x.field1;
256+
^^^^^^^^
257+
Error: This value is "aliased" but expected to be "unique".
258+
|}]
259+
181260
let check_tuple x y z =
182261
let m =
183262
match x, y, z with

0 commit comments

Comments
 (0)