summaryrefslogtreecommitdiff
path: root/testsuite/tests/typing-unboxed-types/test.ml
blob: 34e0035d9bee9ca80af8521897183ec4a1539fe5 (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
(* TEST
 expect;
*)

(* Check the unboxing *)

(* For concrete types *)
type t1 = A of string [@@ocaml.unboxed];;
[%%expect{|
type t1 = A of string [@@unboxed]
|}];;

let x = A "foo" in
Obj.repr x == Obj.repr (match x with A s -> s)
;;
[%%expect{|
- : bool = true
|}];;

(* For records *)
type t2 = { f : string } [@@ocaml.unboxed];;
[%%expect{|
type t2 = { f : string; } [@@unboxed]
|}];;

let x = { f = "foo" } in
Obj.repr x == Obj.repr x.f
;;
[%%expect{|
- : bool = true
|}];;

(* For inline records *)
type t3 = B of { g : string } [@@ocaml.unboxed];;
[%%expect{|
type t3 = B of { g : string; } [@@unboxed]
|}];;

let x = B { g = "foo" } in
Obj.repr x == Obj.repr (match x with B {g} -> g)
;;
[%%expect{|
- : bool = true
|}];;

(* Check unboxable types *)
type t4 = C [@@ocaml.unboxed];;  (* no argument *)
[%%expect{|
Line 1, characters 0-29:
1 | type t4 = C [@@ocaml.unboxed];;  (* no argument *)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because its constructor has no argument.
|}];;
type t5 = D of int * string [@@ocaml.unboxed];; (* more than one argument *)
[%%expect{|
Line 1, characters 0-45:
1 | type t5 = D of int * string [@@ocaml.unboxed];; (* more than one argument *)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       its constructor has more than one argument.
|}];;
type t5 = E | F [@@ocaml.unboxed];;          (* more than one constructor *)
[%%expect{|
Line 1, characters 0-33:
1 | type t5 = E | F [@@ocaml.unboxed];;          (* more than one constructor *)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because it has more than one constructor.
|}];;
type t6 = G of int | H [@@ocaml.unboxed];;
[%%expect{|
Line 1, characters 0-40:
1 | type t6 = G of int | H [@@ocaml.unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because it has more than one constructor.
|}];;
type t7 = I of string | J of bool [@@ocaml.unboxed];;

type t8 = { h : bool; i : int } [@@ocaml.unboxed];;  (* more than one field *)
[%%expect{|
Line 1, characters 0-51:
1 | type t7 = I of string | J of bool [@@ocaml.unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because it has more than one constructor.
|}];;
type t9 = K of { j : string; l : int } [@@ocaml.unboxed];;
[%%expect{|
Line 1, characters 0-56:
1 | type t9 = K of { j : string; l : int } [@@ocaml.unboxed];;
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This type cannot be unboxed because
       its constructor has more than one field.
|}];;

(* let rec must be rejected *)
type t10 = A of t10 [@@ocaml.unboxed];;
[%%expect{|
type t10 = A of t10 [@@unboxed]
|}];;
let rec x = A x;;
[%%expect{|
Line 1, characters 12-15:
1 | let rec x = A x;;
                ^^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

(* Representation mismatch between module and signature must be rejected *)
module M : sig
  type t = A of string
end = struct
  type t = A of string [@@ocaml.unboxed]
end;;
[%%expect{|
Lines 3-5, characters 6-3:
3 | ......struct
4 |   type t = A of string [@@ocaml.unboxed]
5 | end..
Error: Signature mismatch:
       Modules do not match:
         sig type t = A of string [@@unboxed] end
       is not included in
         sig type t = A of string end
       Type declarations do not match:
         type t = A of string [@@unboxed]
       is not included in
         type t = A of string
       Their internal representations differ:
       the first declaration uses unboxed representation.
|}];;

module N : sig
  type t = A of string [@@ocaml.unboxed]
end = struct
  type t = A of string
end;;
[%%expect{|
Lines 3-5, characters 6-3:
3 | ......struct
4 |   type t = A of string
5 | end..
Error: Signature mismatch:
       Modules do not match:
         sig type t = A of string end
       is not included in
         sig type t = A of string [@@unboxed] end
       Type declarations do not match:
         type t = A of string
       is not included in
         type t = A of string [@@unboxed]
       Their internal representations differ:
       the second declaration uses unboxed representation.
|}];;

module O : sig
  type t = { f : string }
end = struct
  type t = { f : string } [@@ocaml.unboxed]
end;;
[%%expect{|
Lines 3-5, characters 6-3:
3 | ......struct
4 |   type t = { f : string } [@@ocaml.unboxed]
5 | end..
Error: Signature mismatch:
       Modules do not match:
         sig type t = { f : string; } [@@unboxed] end
       is not included in
         sig type t = { f : string; } end
       Type declarations do not match:
         type t = { f : string; } [@@unboxed]
       is not included in
         type t = { f : string; }
       Their internal representations differ:
       the first declaration uses unboxed representation.
|}];;

module P : sig
  type t = { f : string } [@@ocaml.unboxed]
end = struct
  type t = { f : string }
end;;
[%%expect{|
Lines 3-5, characters 6-3:
3 | ......struct
4 |   type t = { f : string }
5 | end..
Error: Signature mismatch:
       Modules do not match:
         sig type t = { f : string; } end
       is not included in
         sig type t = { f : string; } [@@unboxed] end
       Type declarations do not match:
         type t = { f : string; }
       is not included in
         type t = { f : string; } [@@unboxed]
       Their internal representations differ:
       the second declaration uses unboxed representation.
|}];;

module Q : sig
  type t = A of { f : string }
end = struct
  type t = A of { f : string } [@@ocaml.unboxed]
end;;
[%%expect{|
Lines 3-5, characters 6-3:
3 | ......struct
4 |   type t = A of { f : string } [@@ocaml.unboxed]
5 | end..
Error: Signature mismatch:
       Modules do not match:
         sig type t = A of { f : string; } [@@unboxed] end
       is not included in
         sig type t = A of { f : string; } end
       Type declarations do not match:
         type t = A of { f : string; } [@@unboxed]
       is not included in
         type t = A of { f : string; }
       Their internal representations differ:
       the first declaration uses unboxed representation.
|}];;

module R : sig
  type t = A of { f : string } [@@ocaml.unboxed]
end = struct
  type t = A of { f : string }
end;;
[%%expect{|
Lines 3-5, characters 6-3:
3 | ......struct
4 |   type t = A of { f : string }
5 | end..
Error: Signature mismatch:
       Modules do not match:
         sig type t = A of { f : string; } end
       is not included in
         sig type t = A of { f : string; } [@@unboxed] end
       Type declarations do not match:
         type t = A of { f : string; }
       is not included in
         type t = A of { f : string; } [@@unboxed]
       Their internal representations differ:
       the second declaration uses unboxed representation.
|}];;


(* Check interference with representation of float arrays. *)
type t11 = L of float [@@ocaml.unboxed];;
[%%expect{|
type t11 = L of float [@@unboxed]
|}];;
let x = Array.make 10 (L 3.14)   (* represented as a flat array *)
and f (a : t11 array) = a.(0)    (* might wrongly assume an array of pointers *)
in assert (f x = L 3.14);;
[%%expect{|
- : unit = ()
|}];;


(* Check for a potential infinite loop in the typing algorithm. *)
type 'a t12 = M of 'a t12 [@@ocaml.unboxed];;
[%%expect{|
type 'a t12 = M of 'a t12 [@@unboxed]
|}];;
let f (a : int t12 array) = a.(0);;
[%%expect{|
val f : int t12 array -> int t12 = <fun>
|}];;

(* Check for another possible loop *)
type t13 = A : _ t12 -> t13 [@@ocaml.unboxed];;
[%%expect{|
type t13 = A : 'a t12 -> t13 [@@unboxed]
|}];;


(* should work *)
type t14;;
type t15 = A of t14 [@@ocaml.unboxed];;
[%%expect{|
type t14
type t15 = A of t14 [@@unboxed]
|}];;

(* should fail because the compiler knows that t is actually float and
   optimizes the record's representation *)
module S : sig
  type t
  type u = { f1 : t; f2 : t }
end = struct
  type t = A of float [@@ocaml.unboxed]
  type u = { f1 : t; f2 : t }
end;;
[%%expect{|
Lines 4-7, characters 6-3:
4 | ......struct
5 |   type t = A of float [@@ocaml.unboxed]
6 |   type u = { f1 : t; f2 : t }
7 | end..
Error: Signature mismatch:
       Modules do not match:
         sig type t = A of float [@@unboxed] type u = { f1 : t; f2 : t; } end
       is not included in
         sig type t type u = { f1 : t; f2 : t; } end
       Type declarations do not match:
         type u = { f1 : t; f2 : t; }
       is not included in
         type u = { f1 : t; f2 : t; }
       Their internal representations differ:
       the first declaration uses unboxed float representation.
|}];;

(* implementing [@@immediate] with [@@ocaml.unboxed]: this works because the
   representation of [t] is [int]
 *)
module T : sig
  type t [@@immediate]
end = struct
  type t = A of int [@@ocaml.unboxed]
end;;
[%%expect{|
module T : sig type t [@@immediate] end
|}];;

(* Another corner case *)
type 'a s
type ('a, 'p) t = private 'a s
type 'a packed = T : ('a, _) t -> 'a packed [@@unboxed]
;;
[%%expect{|
type 'a s
type ('a, 'p) t = private 'a s
type 'a packed = T : ('a, 'b) t -> 'a packed [@@unboxed]
|}];;

(* MPR#7682 *)
type f = {field: 'a. 'a list} [@@unboxed];;
let g = Array.make 10 { field=[] };;
let h = g.(5);;
[%%expect{|
type f = { field : 'a. 'a list; } [@@unboxed]
val g : f array =
  [|{field = []}; {field = []}; {field = []}; {field = []}; {field = []};
    {field = []}; {field = []}; {field = []}; {field = []}; {field = []}|]
val h : f = {field = []}
|}];;

(* Using [@@immediate] information (GPR#1469) *)
type 'a t [@@immediate];;
type u = U : 'a t -> u [@@unboxed];;
[%%expect{|
type 'a t [@@immediate]
type u = U : 'a t -> u [@@unboxed]
|}];;

(* This could not be accepted without using a fixpoint to check unboxed declarations
   (GPR#2188) *)
type ('a, 'b) t = K : 'c -> (bool, 'c) t [@@unboxed]
and t1 = T1 : (bool, int) t -> t1 [@@unboxed]
[%%expect{|
type ('a, 'b) t = K : 'c -> (bool, 'c) t [@@unboxed]
and t1 = T1 : (bool, int) t -> t1 [@@unboxed]
|}];;

(* This real-world example of recursive declaration comes from Markus Mottl
   -- see MPR#7361 *)
type ('a, 'kind) tree =
  | Root : { mutable value : 'a; mutable rank : int } -> ('a, [ `root ]) tree
  | Inner : { mutable parent : 'a node } -> ('a, [ `inner ]) tree
and 'a node = Node : ('a, _) tree -> 'a node [@@ocaml.unboxed]
[%%expect{|
type ('a, 'kind) tree =
    Root : { mutable value : 'a; mutable rank : int;
    } -> ('a, [ `root ]) tree
  | Inner : { mutable parent : 'a node; } -> ('a, [ `inner ]) tree
and 'a node = Node : ('a, 'b) tree -> 'a node [@@unboxed]
|}];;