Debian Codegraph @

test/examples.mlt

raw
1 open Ppx_hash_lib.Std
2 open Hash.Builtin;;
3
4 #verbose true
5
6 module type Interface_for_types_named_t = sig
7 type t [@@deriving hash]
8 end
9
10 [%%expect
11 {|
12 module type Interface_for_types_named_t =
13 sig
14 type t
15 val hash_fold_t :
16 t Base.Exported_for_specific_uses.Ppx_hash_lib.hash_fold
17 val hash : t -> int
18 end
19 |}]
20
21 module type Interface_for_types_named_other_than_t = sig
22 type my_type [@@deriving hash]
23 end
24
25 [%%expect
26 {|
27 module type Interface_for_types_named_other_than_t =
28 sig
29 type my_type
30 val hash_fold_my_type :
31 Base_internalhash_types.state ->
32 my_type -> Base_internalhash_types.state
33 val hash_my_type : my_type -> int
34 end
35 |}]
36
37 module type Interface_for_poly_types = sig
38 type 'a my_container [@@deriving hash]
39 end
40
41 [%%expect
42 {|
43 module type Interface_for_poly_types =
44 sig
45 type 'a my_container
46 val hash_fold_my_container :
47 (Base_internalhash_types.state -> 'a -> Base_internalhash_types.state) ->
48 Base_internalhash_types.state ->
49 'a my_container -> Base_internalhash_types.state
50 end
51 |}]
52
53 module type Interface_for_poly_types_with_unnamed_argument = sig
54 type _ my_container [@@deriving hash]
55 end
56
57 [%%expect
58 {|
59 module type Interface_for_poly_types_with_unnamed_argument =
60 sig
61 type _ my_container
62 val hash_fold_my_container :
63 (Base_internalhash_types.state ->
64 'a__001_ -> Base_internalhash_types.state) ->
65 Base_internalhash_types.state ->
66 'a__001_ my_container -> Base_internalhash_types.state
67 end
68 |}]
69
70 module Use_of_hash_fold_syntax_extension = struct
71 let f = [%hash_fold: (int * string) list]
72 end
73
74 [%%expect
75 {|
76 module Use_of_hash_fold_syntax_extension :
77 sig
78 val f :
79 Base_internalhash_types.state ->
80 (int * string) list -> Base_internalhash_types.state
81 end
82 |}]
83
84 module Support_for_builtins = struct
85 let f =
86 [%hash_fold:
87 (nativeint * int64 * int32 * char * int * bool * string * float * unit) option list
88 lazy_t]
89 ;;
90 end
91
92 [%%expect
93 {|
94 module Support_for_builtins :
95 sig
96 val f :
97 Base_internalhash_types.state ->
98 (nativeint * int64 * int32 * char * int * bool * string * float * unit)
99 option list lazy_t -> Base_internalhash_types.state
100 end
101 |}]
102
103 (* negative tests... *)
104
105 module No_builtin_support_for_array = struct
106 type fail = int array [@@deriving hash]
107 end
108
109 [%%expect
110 {|
111 Line _, characters _-_:
112 Error: Unbound value hash_fold_array
113 |}]
114
115 module No_builtin_support_for_ref = struct
116 type fail = int ref [@@deriving hash]
117 end
118
119 [%%expect
120 {|
121 Line _, characters _-_:
122 Error: Unbound value hash_fold_ref
123 Hint: Did you mean hash_fold_int?
124 |}]
125
126 type fail = int -> int [@@deriving hash]
127
128 [%%expect
129 {|
130 Line _, characters _-_:
131 Error: ppx_hash: functions can not be hashed.
132 |}]
133
134 type fail = < f : int > [@@deriving hash]
135
136 [%%expect
137 {|
138 Line _, characters _-_:
139 Error: ppx_hash: unsupported type: < f: int >
140 |}]
141
142 type fail = .. [@@deriving hash]
143
144 [%%expect
145 {|
146 Line _, characters _-_:
147 Error: ppx_hash: open types are not supported
148 |}]
149
150 type fail = private [> `Foo ] [@@deriving hash]
151
152 [%%expect
153 {|
154 Line _, characters _-_:
155 Error: ppx_hash: cannot hash open polymorphic variant types
156 |}]
157
158 type fail =
159 { mutable u : int
160 ; s : string
161 }
162 [@@deriving hash]
163
164 [%%expect
165 {|
166 Line _, characters _-_:
167 Error: ppx_hash: require [@hash.ignore] or [@compare.ignore] on mutable
168 record field
169 |}]
170
171 type unhashable
172
173 type ok =
174 { mutable u : unhashable [@compare.ignore]
175 ; s : string
176 }
177 [@@deriving hash]
178
179 [%%expect
180 {|
181 type unhashable
182 type ok = { mutable u : unhashable; s : string; }
183 val hash_fold_ok :
184 Base_internalhash_types.state -> ok -> Base_internalhash_types.state =
185 <fun>
186 val hash_ok : ok -> int = <fun>
187 |}]
188
189 type ok_nested =
190 { u : (unhashable[@ignore]) * int
191 ; s : string
192 }
193 [@@deriving hash]
194
195 [%%expect
196 {|
197 type ok_nested = { u : unhashable * int; s : string; }
198 val hash_fold_ok_nested :
199 Base_internalhash_types.state -> ok_nested -> Base_internalhash_types.state =
200 <fun>
201 val hash_ok_nested : ok_nested -> int = <fun>
202 |}]
203
204 module type Type_extension = sig
205 val f : [%hash_fold: 'a] -> [%hash_fold: 'a * 'a]
206 val g : [%hash_fold: _] -> [%hash_fold: _]
207 end
208
209 [%%expect
210 {|
211 module type Type_extension =
212 sig
213 val f :
214 (Base_internalhash_types.state -> 'a -> Base_internalhash_types.state) ->
215 Base_internalhash_types.state ->
216 'a * 'a -> Base_internalhash_types.state
217 val g :
218 (Base_internalhash_types.state -> 'a -> Base_internalhash_types.state) ->
219 Base_internalhash_types.state -> 'b -> Base_internalhash_types.state
220 end
221 |}]
222
223 type warn = { foo : string [@no_hashing] } [@@deriving hash]
224
225 [%%expect
226 {|
227 Line _, characters _-_:
228 Error (warning 22 [preprocessor]): [@hash.no_hashing] is deprecated. Use [@hash.ignore].
229 |}]