1(* Use type information *) 2module M1 = struct 3 type t = {x: int; y: int} 4 type u = {x: bool; y: bool} 5end;; 6 7module OK = struct 8 open M1 9 let f1 (r:t) = r.x (* ok *) 10 let f2 r = ignore (r:t); r.x (* non principal *) 11 12 let f3 (r: t) = 13 match r with {x; y} -> y + y (* ok *) 14end;; 15 16module F1 = struct 17 open M1 18 let f r = match r with {x; y} -> y + y 19end;; (* fails *) 20 21module F2 = struct 22 open M1 23 let f r = 24 ignore (r: t); 25 match r with 26 {x; y} -> y + y 27end;; (* fails for -principal *) 28 29(* Use type information with modules*) 30module M = struct 31 type t = {x:int} 32 type u = {x:bool} 33end;; 34let f (r:M.t) = r.M.x;; (* ok *) 35let f (r:M.t) = r.x;; (* warning *) 36let f ({x}:M.t) = x;; (* warning *) 37 38module M = struct 39 type t = {x: int; y: int} 40end;; 41module N = struct 42 type u = {x: bool; y: bool} 43end;; 44module OK = struct 45 open M 46 open N 47 let f (r:M.t) = r.x 48end;; 49 50module M = struct 51 type t = {x:int} 52 module N = struct type s = t = {x:int} end 53 type u = {x:bool} 54end;; 55module OK = struct 56 open M.N 57 let f (r:M.t) = r.x 58end;; 59 60(* Use field information *) 61module M = struct 62 type u = {x:bool;y:int;z:char} 63 type t = {x:int;y:bool} 64end;; 65module OK = struct 66 open M 67 let f {x;z} = x,z 68end;; (* ok *) 69module F3 = struct 70 open M 71 let r = {x=true;z='z'} 72end;; (* fail for missing label *) 73 74module OK = struct 75 type u = {x:int;y:bool} 76 type t = {x:bool;y:int;z:char} 77 let r = {x=3; y=true} 78end;; (* ok *) 79 80(* Corner cases *) 81 82module F4 = struct 83 type foo = {x:int; y:int} 84 type bar = {x:int} 85 let b : bar = {x=3; y=4} 86end;; (* fail but don't warn *) 87 88module M = struct type foo = {x:int;y:int} end;; 89module N = struct type bar = {x:int;y:int} end;; 90let r = { M.x = 3; N.y = 4; };; (* error: different definitions *) 91 92module MN = struct include M include N end 93module NM = struct include N include M end;; 94let r = {MN.x = 3; NM.y = 4};; (* error: type would change with order *) 95 96(* Lpw25 *) 97 98module M = struct 99 type foo = { x: int; y: int } 100 type bar = { x:int; y: int; z: int} 101end;; 102module F5 = struct 103 open M 104 let f r = ignore (r: foo); {r with x = 2; z = 3} 105end;; 106module M = struct 107 include M 108 type other = { a: int; b: int } 109end;; 110module F6 = struct 111 open M 112 let f r = ignore (r: foo); { r with x = 3; a = 4 } 113end;; 114module F7 = struct 115 open M 116 let r = {x=1; y=2} 117 let r: other = {x=1; y=2} 118end;; 119 120module A = struct type t = {x: int} end 121module B = struct type t = {x: int} end;; 122let f (r : B.t) = r.A.x;; (* fail *) 123 124(* Spellchecking *) 125 126module F8 = struct 127 type t = {x:int; yyy:int} 128 let a : t = {x=1;yyz=2} 129end;; 130 131(* PR#6004 *) 132 133type t = A 134type s = A 135 136class f (_ : t) = object end;; 137class g = f A;; (* ok *) 138 139class f (_ : 'a) (_ : 'a) = object end;; 140class g = f (A : t) A;; (* warn with -principal *) 141 142 143(* PR#5980 *) 144 145module Shadow1 = struct 146 type t = {x: int} 147 module M = struct 148 type s = {x: string} 149 end 150 open M (* this open is unused, it isn't reported as shadowing 'x' *) 151 let y : t = {x = 0} 152end;; 153module Shadow2 = struct 154 type t = {x: int} 155 module M = struct 156 type s = {x: string} 157 end 158 open M (* this open shadows label 'x' *) 159 let y = {x = ""} 160end;; 161 162(* PR#6235 *) 163 164module P6235 = struct 165 type t = { loc : string; } 166 type v = { loc : string; x : int; } 167 type u = [ `Key of t ] 168 let f (u : u) = match u with `Key {loc} -> loc 169end;; 170 171(* Remove interaction between branches *) 172 173module P6235' = struct 174 type t = { loc : string; } 175 type v = { loc : string; x : int; } 176 type u = [ `Key of t ] 177 let f = function 178 | (_ : u) when false -> "" 179 |`Key {loc} -> loc 180end;; 181