summaryrefslogtreecommitdiff
path: root/testsuite/tests/typing-misc/build_as_type.ml
blob: 51aa4e23386b50ee5d668fb21598490c4c20151c (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
(* TEST
   * expect
*)

let f = function
  | ([] : int list) as x -> x
  | _ :: _ -> assert false;;
[%%expect{|
val f : int list -> int list = <fun>
|}]

let f =
  let f' = function
    | ([] : 'a list) as x -> x
    | _ :: _ -> assert false
  in
  f', f';;
[%%expect{|
val f : ('a list -> 'a list) * ('a list -> 'a list) = (<fun>, <fun>)
|}]

let f =
  let f' = function
    | ([] : _ list) as x -> x
    | _ :: _ -> assert false
  in
  f', f';;
[%%expect{|
val f : ('a list -> 'b list) * ('c list -> 'd list) = (<fun>, <fun>)
|}]

let f =
  let f' (type a) = function
    | ([] : a list) as x -> x
    | _ :: _ -> assert false
  in
  f', f';;
[%%expect{|
val f : ('a list -> 'a list) * ('b list -> 'b list) = (<fun>, <fun>)
|}]

type t = [ `A | `B ];;
[%%expect{|
type t = [ `A | `B ]
|}]

let f = function `A as x -> x | `B -> `A;;
[%%expect{|
val f : [< `A | `B ] -> [> `A ] = <fun>
|}]

let f = function (`A : t) as x -> x | `B -> `A;;
[%%expect{|
val f : t -> t = <fun>
|}]

let f : t -> _ = function `A as x -> x | `B -> `A;;
[%%expect{|
val f : t -> [> `A ] = <fun>
|}]

let f = function
  | (`A : t) as x ->
    (* This should be flagged as non-exhaustive: because of the constraint [x]
       is of type [t]. *)
    begin match x with
    | `A -> ()
    end
  | `B -> ();;
[%%expect{|
Lines 5-7, characters 4-7:
5 | ....begin match x with
6 |     | `A -> ()
7 |     end
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
`B

val f : t -> unit = <fun>
|}]


let f = function
  | (`A : t) as x ->
    begin match x with
    | `A -> ()
    | `B -> ()
    end
  | `B -> ();;
[%%expect{|
val f : t -> unit = <fun>
|}]


let f = function
  | (`A : t) as x ->
    begin match x with
    | `A -> ()
    | `B -> ()
    | `C -> ()
    end
  | `B -> ();;
[%%expect{|
Line 6, characters 6-8:
6 |     | `C -> ()
          ^^
Error: This pattern matches values of type [? `C ]
       but a pattern was expected which matches values of type t
       The second variant type does not allow tag(s) `C
|}]

let f = function (`A, _ : _ * int) as x -> x;;
[%%expect{|
val f : [< `A ] * int -> [> `A ] * int = <fun>
|}]

(* Make sure *all* the constraints are respected: *)

let f = function
  | ((`A : _) : t) as x ->
    (* This should be flagged as non-exhaustive: because of the constraint [x]
       is of type [t]. *)
    begin match x with
    | `A -> ()
    end
  | `B -> ();;
[%%expect{|
Lines 5-7, characters 4-7:
5 | ....begin match x with
6 |     | `A -> ()
7 |     end
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
`B

val f : t -> unit = <fun>
|}]

let f = function
  | ((`A : t) : _) as x ->
    (* This should be flagged as non-exhaustive: because of the constraint [x]
       is of type [t]. *)
    begin match x with
    | `A -> ()
    end
  | `B -> ();;

[%%expect{|
Lines 5-7, characters 4-7:
5 | ....begin match x with
6 |     | `A -> ()
7 |     end
Warning 8 [partial-match]: this pattern-matching is not exhaustive.
Here is an example of a case that is not matched:
`B

val f : t -> unit = <fun>
|}]