summaryrefslogtreecommitdiff
path: root/testsuite/tests/typing-warnings/coercions.ml
blob: 874dd35d71ed7da37bf7a2400b4cc4b475f2af38 (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
(* TEST
 flags = " -w +A -strict-sequence ";
 expect;
*)

(* comment 9644 of PR#6000 *)

fun b -> if b then format_of_string "x" else "y"
[%%expect {|
- : bool -> ('a, 'b, 'c, 'd, 'd, 'a) format6 = <fun>
|}, Principal{|
Line 1, characters 45-48:
1 | fun b -> if b then format_of_string "x" else "y"
                                                 ^^^
Warning 18 [not-principal]: this coercion to format6 is not principal.

- : bool -> ('a, 'b, 'c, 'd, 'd, 'a) format6 = <fun>
|}]
;;

fun b -> if b then "x" else format_of_string "y"
[%%expect {|
Line 1, characters 28-48:
1 | fun b -> if b then "x" else format_of_string "y"
                                ^^^^^^^^^^^^^^^^^^^^
Error: This expression has type
         ('a, 'b, 'c, 'd, 'd, 'a) format6 =
           ('a, 'b, 'c, 'd, 'd, 'a) CamlinternalFormatBasics.format6
       but an expression was expected of type string
|}]
;;

fun b : (_,_,_) format -> if b then "x" else "y"
[%%expect {|
- : bool -> ('a, 'b, 'a) format = <fun>
|}]
;;

(* PR#7135 *)

module PR7135 = struct
  module M : sig type t = private int end =  struct type t = int end
  include M

  let lift2 (f : int -> int -> int) (x : t) (y : t) =
    f (x :> int) (y :> int)
end;;
[%%expect {|
module PR7135 :
  sig
    module M : sig type t = private int end
    type t = M.t
    val lift2 : (int -> int -> int) -> t -> t -> int
  end
|}]

(* example of non-ground coercion *)

module Test1 = struct
  type t = private int
  let f x = let y = if true then x else (x:t) in (y :> int)
end;;
[%%expect {|
module Test1 : sig type t = private int val f : t -> int end
|}, Principal{|
Line 3, characters 49-59:
3 |   let f x = let y = if true then x else (x:t) in (y :> int)
                                                     ^^^^^^^^^^
Warning 18 [not-principal]: this ground coercion is not principal.

module Test1 : sig type t = private int val f : t -> int end
|}]