summaryrefslogtreecommitdiff
path: root/testsuite/tests/typing-warnings/exhaustiveness.ml
blob: bd40fb4375ccb1607c970c7e86587aa3f23461b0 (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
378
379
380
381
382
(* TEST
   flags = " -w +A -strict-sequence "
   * expect
*)

let f = function
    None, None -> 1
  | Some _, Some _ -> 2;;
[%%expect {|
Lines 1-3, characters 8-23:
1 | ........function
2 |     None, None -> 1
3 |   | Some _, Some _ -> 2..
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
(None, Some _)
val f : 'a option * 'b option -> int = <fun>
|}]

type _ t =
  A : int t | B : bool t | C : char t | D : float t
type (_,_,_,_) u = U : (int, int, int, int) u
type v = E | F | G
;;
[%%expect {|
type _ t = A : int t | B : bool t | C : char t | D : float t
type (_, _, _, _) u = U : (int, int, int, int) u
type v = E | F | G
|}]

(* Unused cases *)
let f (x : int t) = match x with A -> 1 | _ -> 2;; (* warn *)
[%%expect {|
Line 1, characters 20-48:
1 | let f (x : int t) = match x with A -> 1 | _ -> 2;; (* warn *)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 4 [fragile-match]: this pattern-matching is fragile.
It will remain exhaustive when constructors are added to type t.

Line 1, characters 42-43:
1 | let f (x : int t) = match x with A -> 1 | _ -> 2;; (* warn *)
                                              ^
Warning 56 [unreachable-case]: this match case is unreachable.
Consider replacing it with a refutation case '<pat> -> .'
val f : int t -> int = <fun>
|}]

let f (x : unit t option) = match x with None -> 1 | _ -> 2 ;; (* warn? *)
[%%expect {|
Line 1, characters 53-54:
1 | let f (x : unit t option) = match x with None -> 1 | _ -> 2 ;; (* warn? *)
                                                         ^
Warning 56 [unreachable-case]: this match case is unreachable.
Consider replacing it with a refutation case '<pat> -> .'
val f : unit t option -> int = <fun>
|}]

let f (x : unit t option) = match x with None -> 1 | Some _ -> 2 ;; (* warn *)
[%%expect {|
Line 1, characters 53-59:
1 | let f (x : unit t option) = match x with None -> 1 | Some _ -> 2 ;; (* warn *)
                                                         ^^^^^^
Warning 56 [unreachable-case]: this match case is unreachable.
Consider replacing it with a refutation case '<pat> -> .'
val f : unit t option -> int = <fun>
|}]

let f (x : int t option) = match x with None -> 1 | _ -> 2;;
[%%expect {|
val f : int t option -> int = <fun>
|}]

let f (x : int t option) = match x with None -> 1;; (* warn *)
[%%expect {|
Line 1, characters 27-49:
1 | let f (x : int t option) = match x with None -> 1;; (* warn *)
                               ^^^^^^^^^^^^^^^^^^^^^^
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
Some A
val f : int t option -> int = <fun>
|}]

(* Example with record, type, single case *)

type 'a box = Box of 'a
type 'a pair = {left: 'a; right: 'a};;
[%%expect {|
type 'a box = Box of 'a
type 'a pair = { left : 'a; right : 'a; }
|}]

let f : (int t box pair * bool) option -> unit = function None -> ();;
[%%expect {|
Line 1, characters 49-68:
1 | let f : (int t box pair * bool) option -> unit = function None -> ();;
                                                     ^^^^^^^^^^^^^^^^^^^
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
Some ({left=Box A; right=Box A}, _)
val f : (int t box pair * bool) option -> unit = <fun>
|}]

let f : (string t box pair * bool) option -> unit = function None -> ();;
[%%expect {|
val f : (string t box pair * bool) option -> unit = <fun>
|}]

let f = function {left=Box 0; _ } -> ();;
[%%expect {|
Line 1, characters 8-39:
1 | let f = function {left=Box 0; _ } -> ();;
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
{left=Box 1; _ }
val f : int box pair -> unit = <fun>
|}]

let f = function {left=Box 0;right=Box 1} -> ();;
[%%expect {|
Line 1, characters 8-47:
1 | let f = function {left=Box 0;right=Box 1} -> ();;
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
{left=Box 0; right=Box 0}
val f : int box pair -> unit = <fun>
|}]

(* Examples from ML2015 paper *)

type _ t =
  | Int : int t
  | Bool : bool t
;;
[%%expect {|
type _ t = Int : int t | Bool : bool t
|}]

let f : type a. a t -> a = function
  | Int -> 1
  | Bool -> true
;;
[%%expect {|
val f : 'a t -> 'a = <fun>
|}]

let g : int t -> int = function
  | Int -> 1
;;
[%%expect {|
val g : int t -> int = <fun>
|}]

let h : type a. a t -> a t -> bool =
  fun x y -> match x, y with
  | Int, Int -> true
  | Bool, Bool -> true
;;
[%%expect {|
val h : 'a t -> 'a t -> bool = <fun>
|}]

type (_, _) cmp =
 | Eq : ('a, 'a) cmp
 | Any: ('a, 'b) cmp
module A : sig type a type b val eq : (a, b) cmp end
  = struct type a type b = a let eq = Eq end
;;
[%%expect {|
type (_, _) cmp = Eq : ('a, 'a) cmp | Any : ('a, 'b) cmp
module A : sig type a type b val eq : (a, b) cmp end
|}]

let f : (A.a, A.b) cmp -> unit = function Any -> ()
;;
[%%expect {|
Line 1, characters 33-51:
1 | let f : (A.a, A.b) cmp -> unit = function Any -> ()
                                     ^^^^^^^^^^^^^^^^^^
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
Eq
val f : (A.a, A.b) cmp -> unit = <fun>
|}]

let deep : char t option -> char =
  function None -> 'c'
;;
[%%expect {|
val deep : char t option -> char = <fun>
|}]

type zero = Zero
type _ succ = Succ
;;
[%%expect {|
type zero = Zero
type _ succ = Succ
|}]

type (_,_,_) plus =
  | Plus0 : (zero, 'a, 'a) plus
  | PlusS : ('a, 'b, 'c) plus ->
       ('a succ, 'b, 'c succ) plus
;;
[%%expect {|
type (_, _, _) plus =
    Plus0 : (zero, 'a, 'a) plus
  | PlusS : ('a, 'b, 'c) plus -> ('a succ, 'b, 'c succ) plus
|}]

let trivial : (zero succ, zero, zero) plus option -> bool =
  function None -> false
;;
[%%expect {|
val trivial : (zero succ, zero, zero) plus option -> bool = <fun>
|}]

let easy : (zero, zero succ, zero) plus option -> bool =
  function None -> false
;;
[%%expect {|
val easy : (zero, zero succ, zero) plus option -> bool = <fun>
|}]

let harder : (zero succ, zero succ, zero succ) plus option -> bool =
  function None -> false
;;
[%%expect {|
Line 2, characters 2-24:
2 |   function None -> false
      ^^^^^^^^^^^^^^^^^^^^^^
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
Some (PlusS _)
val harder : (zero succ, zero succ, zero succ) plus option -> bool = <fun>
|}]

let harder : (zero succ, zero succ, zero succ) plus option  -> bool =
  function None -> false | Some (PlusS _) -> .
;;
[%%expect {|
val harder : (zero succ, zero succ, zero succ) plus option -> bool = <fun>
|}]

let inv_zero : type a b c d. (a,b,c) plus -> (c,d,zero) plus -> bool =
  fun p1 p2 ->
    match p1, p2 with
    | Plus0, Plus0 -> true
;;
[%%expect {|
val inv_zero : ('a, 'b, 'c) plus -> ('c, 'd, zero) plus -> bool = <fun>
|}]


(* Empty match *)

type _ t = Int : int t;;
[%%expect {|
type _ t = Int : int t
|}]

let f (x : bool t) = match x with _ -> . ;; (* ok *)
[%%expect {|
val f : bool t -> 'a = <fun>
|}]


(* trefis in PR#6437 *)

let f () = match None with _ -> .;; (* error *)
[%%expect {|
Line 1, characters 27-28:
1 | let f () = match None with _ -> .;; (* error *)
                               ^
Error: This match case could not be refuted.
       Here is an example of a value that would reach it: _
|}]

let g () = match None with _ -> () | exception _ -> .;; (* error *)
[%%expect {|
Line 1, characters 47-48:
1 | let g () = match None with _ -> () | exception _ -> .;; (* error *)
                                                   ^
Error: This match case could not be refuted.
       Here is an example of a value that would reach it: _
|}]

let h () = match None with _ -> .  | exception _ -> .;; (* error *)
[%%expect {|
Line 1, characters 27-28:
1 | let h () = match None with _ -> .  | exception _ -> .;; (* error *)
                               ^
Error: This match case could not be refuted.
       Here is an example of a value that would reach it: _
|}]

let f x = match x with _ -> () | None -> .;; (* do not warn *)
[%%expect {|
val f : 'a option -> unit = <fun>
|}]

(* #7059, all clauses guarded *)

let f x y = match 1 with 1 when x = y -> 1;;
[%%expect {|
Line 1, characters 12-42:
1 | let f x y = match 1 with 1 when x = y -> 1;;
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
All clauses in this pattern-matching are guarded.
val f : 'a -> 'a -> int = <fun>
|}]

(* #7504, Example with no constraints on a record *)
let f = function {contents=_}, 0 -> 0;;
[%%expect {|
Line 1, characters 8-37:
1 | let f = function {contents=_}, 0 -> 0;;
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
(_, 1)
val f : 'a ref * int -> int = <fun>
|}]

(* inexhaustive however some guarded clause might match *)
let f = function
  | None -> ()
  | Some x when x > 0 -> ()
  | Some x when x <= 0 -> ()
;;
[%%expect {|
Lines 1-4, characters 8-28:
1 | ........function
2 |   | None -> ()
3 |   | Some x when x > 0 -> ()
4 |   | Some x when x <= 0 -> ()
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
Some _
(However, some guarded clause may match this value.)
val f : int option -> unit = <fun>
|}]

(* in the single-row case we can generate more compact witnesses *)
module Single_row_optim = struct
type t = A | B

(* This synthetic program is representative of user-written programs
   that try to distinguish the cases "only A" and "at least one B"
   while avoiding a fragile pattern-matching (using just _ in the last
   row would be fragile).

   It is a "single row" program from the point of view of
   exhaustiveness checking because the first row is subsumed by the
   second and thus removed by the [get_mins] preprocessing of
   Parmatch.

   With the single-row optimization implemented in the compiler, it
   generates a single counter-example that contains
   or-patterns. Without this optimization, it would generate 2^(N-1)
   counter-examples (here N=4 so 8), one for each possible expansion
   of the or-patterns.
*)
let non_exhaustive : t * t * t * t -> unit = function
| A, A, A, A -> ()
| (A|B), (A|B), (A|B), A (*missing B here*) -> ()
end;;
[%%expect {|
Lines 20-22, characters 45-49:
20 | .............................................function
21 | | A, A, A, A -> ()
22 | | (A|B), (A|B), (A|B), A (*missing B here*) -> ()
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
((A|B), (A|B), (A|B), B)
module Single_row_optim :
  sig type t = A | B val non_exhaustive : t * t * t * t -> unit end
|}]