summaryrefslogtreecommitdiff
path: root/testsuite/tests/typing-unboxed-types/test_flat.ml
blob: 43791ed245149d7ac0d90c0145946223b739cf0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
(* TEST
   * flat-float-array
   ** expect
*)

(* should fail *)
type 'a abs;;
type t16 = A : 'a abs -> t16 [@@ocaml.unboxed];;
[%%expect{|
type 'a abs
Line 2, characters 0-46:
2 | type t16 = A : 'a abs -> t16 [@@ocaml.unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of the existential variable 'a.
       You should annotate it with [@@ocaml.boxed].
|}];;

(* should fail (the existential _ still occurs in an abstract type) *)
type t18 = A : _ list abs -> t18 [@@ocaml.unboxed];;
[%%expect{|
Line 1, characters 0-50:
1 | type t18 = A : _ list abs -> t18 [@@ocaml.unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of an unnamed existential variable.
       You should annotate it with [@@ocaml.boxed].
|}];;

(* regression test for PR#7511 (wrong determination of unboxability for GADTs)
*)
type 'a s = S : 'a -> 'a s [@@unboxed];;
type t = T : 'a s -> t [@@unboxed];;
[%%expect{|
type 'a s = S : 'a -> 'a s [@@unboxed]
Line 2, characters 0-34:
2 | type t = T : 'a s -> t [@@unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of the existential variable 'a.
       You should annotate it with [@@ocaml.boxed].
|}];;

(* regression test for GPR#1133 (follow-up to PR#7511) *)
type 'a s = S : 'a -> 'a option s [@@unboxed];;
type t = T : 'a s -> t [@@unboxed];;
[%%expect{|
type 'a s = S : 'a -> 'a option s [@@unboxed]
Line 2, characters 0-34:
2 | type t = T : 'a s -> t [@@unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of the existential variable 'a.
       You should annotate it with [@@ocaml.boxed].
|}];;

(* Another test for GPR#1133: abstract types *)
module M : sig
  type 'a r constraint 'a = unit -> 'b
  val inj : 'b -> (unit -> 'b) r
end = struct
  type 'a r = 'b constraint 'a = unit -> 'b
  let inj x = x
end;;
[%%expect{|
module M :
  sig type 'a r constraint 'a = unit -> 'b val inj : 'b -> (unit -> 'b) r end
|}];;

(* reject *)
type t = T : (unit -> _) M.r -> t [@@unboxed];;
[%%expect{|
Line 1, characters 0-45:
1 | type t = T : (unit -> _) M.r -> t [@@unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of an unnamed existential variable.
       You should annotate it with [@@ocaml.boxed].
|}];;

(* accept *)
type 'a s = S : (unit -> 'a) M.r -> 'a option s [@@unboxed];;
[%%expect{|
type 'a s = S : (unit -> 'a) M.r -> 'a option s [@@unboxed]
|}];;

(* reject *)
type t = T : 'a s -> t [@@unboxed];;
[%%expect{|
Line 1, characters 0-34:
1 | type t = T : 'a s -> t [@@unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of the existential variable 'a.
       You should annotate it with [@@ocaml.boxed].
|}];;

(* accept *)
type 'a t = T : 'a s -> 'a t [@@unboxed];;
[%%expect{|
type 'a t = T : 'a s -> 'a t [@@unboxed]
|}];;

(* Even without constraints, we need to mark abstract types as Deepsep:
   unboxed GADTs can introduce equations that do not appear in the signature
   (see GPR#2188) *)
module N : sig
  type 'a r
  val inj : 'b -> (unit -> 'b) r
end = struct
  type _ r = K : 'b -> (unit -> 'b) r [@@unboxed]
  let inj x = K x
end;;
[%%expect{|
module N : sig type 'a r val inj : 'b -> (unit -> 'b) r end
|}];;

(* reject *)
type t = T : (unit -> _) N.r -> t [@@unboxed];;
[%%expect{|
Line 1, characters 0-45:
1 | type t = T : (unit -> _) N.r -> t [@@unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of an unnamed existential variable.
       You should annotate it with [@@ocaml.boxed].
|}];;

(* accept *)
type 'a s = S : (unit -> 'a) N.r -> 'a option s [@@unboxed];;
[%%expect{|
type 'a s = S : (unit -> 'a) N.r -> 'a option s [@@unboxed]
|}];;

(* Another corner case from GPR#1133 *)
type _ s = S : 'a t -> _ s  [@@unboxed]
 and _ t = T : 'a -> 'a s t
;;
[%%expect{|
type _ s = S : 'a t -> 'b s [@@unboxed]
and _ t = T : 'a -> 'a s t
|}];;

(* regression test for PR#7511 (wrong determination of unboxability for GADTs)
*)
type 'a s = S : 'a -> 'a s [@@unboxed];;
type t = T : 'a s -> t [@@unboxed];;
[%%expect{|
type 'a s = S : 'a -> 'a s [@@unboxed]
Line 2, characters 0-34:
2 | type t = T : 'a s -> t [@@unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of the existential variable 'a.
       You should annotate it with [@@ocaml.boxed].
|}];;

(* regression test for GPR#1133 (follow-up to PR#7511) *)
type 'a s = S : 'a -> 'a option s [@@unboxed];;
type t = T : 'a s -> t [@@unboxed];;
[%%expect{|
type 'a s = S : 'a -> 'a option s [@@unboxed]
Line 2, characters 0-34:
2 | type t = T : 'a s -> t [@@unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of the existential variable 'a.
       You should annotate it with [@@ocaml.boxed].
|}];;

(* Another test for GPR#1133: abstract types *)
module M : sig
  type 'a r constraint 'a = unit -> 'b
  val inj : 'b -> (unit -> 'b) r
end = struct
  type 'a r = 'b constraint 'a = unit -> 'b
  let inj x = x
end;;
[%%expect{|
module M :
  sig type 'a r constraint 'a = unit -> 'b val inj : 'b -> (unit -> 'b) r end
|}];;

(* reject *)
type t = T : (unit -> _) M.r -> t [@@unboxed];;
[%%expect{|
Line 1, characters 0-45:
1 | type t = T : (unit -> _) M.r -> t [@@unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of an unnamed existential variable.
       You should annotate it with [@@ocaml.boxed].
|}];;

type 'a s = S : (unit -> 'a) M.r -> 'a option s [@@unboxed];;
[%%expect{|
type 'a s = S : (unit -> 'a) M.r -> 'a option s [@@unboxed]
|}];;

(* reject *)
type t = T : 'a s -> t [@@unboxed];;
[%%expect{|
Line 1, characters 0-34:
1 | type t = T : 'a s -> t [@@unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of the existential variable 'a.
       You should annotate it with [@@ocaml.boxed].
|}];;

(* accept *)
type 'a t = T : 'a s -> 'a t [@@unboxed];;
[%%expect{|
type 'a t = T : 'a s -> 'a t [@@unboxed]
|}];;


(* Another corner case from GPR#1133 *)
type _ s = S : 'a t -> _ s  [@@unboxed]
 and _ t = T : 'a -> 'a s t
;;
[%%expect{|
type _ s = S : 'a t -> 'b s [@@unboxed]
and _ t = T : 'a -> 'a s t
|}];;

(* GPR#2188: non-principality examples.
   One of the two declarations [valid1] and [valid2] below will fail,
   depending on the order in which GADT equality constraints
   are processed by our implementation.
   The previous unfolding implementation would accept both.

   We decided to specify that, if two parameters are equal to each other,
   then we would use the more constrained mode (Sep rather than Ind) for the
   first/leftmost parameter, and Ind for the second one. With a left-to-right
   reading of parameters, this corresponds to considering that the equality
   is on the second parameter, equal to a parameter already seen, rather than
   an equality on a not-yet-seen parameter.

   In the example below, almost_eq will thus get the mode signature
   (Sep, Ind) rather than (Ind, Sep).
*)
type (_, _) almost_eq = Almost_refl : 'a -> ('a, 'a) almost_eq [@@unboxed]
[%%expect{|
type (_, _) almost_eq = Almost_refl : 'a -> ('a, 'a) almost_eq [@@unboxed]
|}];;


type valid1 = Any : ('a, int) almost_eq -> valid1 [@@unboxed];;
[%%expect{|
Line 1, characters 0-61:
1 | type valid1 = Any : ('a, int) almost_eq -> valid1 [@@unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of the existential variable 'a.
       You should annotate it with [@@ocaml.boxed].
|}];;
type valid2 = Any : (int, 'a) almost_eq -> valid2 [@@unboxed];;
[%%expect{|
type valid2 = Any : (int, 'a) almost_eq -> valid2 [@@unboxed]
|}];;

(* rejected: equivalent to (exits 'a. 'a) *)
type danger = Any : ('a, 'a) almost_eq -> danger [@@unboxed];;
[%%expect{|
Line 1, characters 0-60:
1 | type danger = Any : ('a, 'a) almost_eq -> danger [@@unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       it might contain both float and non-float values,
       depending on the instantiation of the existential variable 'a.
       You should annotate it with [@@ocaml.boxed].
|}];;


(* GPR#2188: handling of cyclic types *)
type 'a stream = unit -> [ `Cons of 'a * 'a stream ];;
type safe = Any : 'a stream -> safe;;
[%%expect{|
type 'a stream = unit -> [ `Cons of 'a * 'a stream ]
type safe = Any : 'a stream -> safe
|}];;

type 'a infinite_full_tree = unit -> [ `Node of 'a * ('a * 'a) stream ];;
type safe_again = Any : 'a stream -> safe_again;;
[%%expect{|
type 'a infinite_full_tree = unit -> [ `Node of 'a * ('a * 'a) stream ]
type safe_again = Any : 'a stream -> safe_again
|}];;

(** Note: there are no tests of rejected cyclic types, because
    the type declarations that would be required to check these cases
    (unproductive cycles in the type declaration) are already rejected by the
    type-checker, before separability checking. See below *)
type 'a id = Id of 'a [@@unboxed]
type cycle = cycle id
[%%expect{|
type 'a id = Id of 'a [@@unboxed]
Line 2, characters 0-21:
2 | type cycle = cycle id
    ^^^^^^^^^^^^^^^^^^^^^
Error: The type abbreviation cycle is cyclic:
         cycle = cycle id,
         cycle id contains cycle
|}];;