text
stringlengths
12
786k
module Wrap = struct let ( <: ) name type_ = create ( name |> Symbol . intern ) type_ include ( Value . Type : Value . Type . S ) end
let symbol_as_value t = t . symbol |> Symbol . to_value
let default_value = Funcall . Wrap . ( " default - value " <: Symbol . t @-> return value )
let default_value_exn t = default_value t . symbol |> Value . Type . of_value_exn t . type_
let default_boundp = Funcall . Wrap . ( " default - boundp " <: Symbol . t @-> return bool )
let default_value_is_defined t = default_boundp t . symbol
let set_default = Funcall . Wrap . ( " set - default " <: Symbol . t @-> value @-> return nil )
let set_default_value t a = set_default t . symbol ( a |> Value . Type . to_value t . type_ )
let make_variable_buffer_local = Funcall . Wrap . ( " make - variable - buffer - local " <: Symbol . t @-> return nil ) ; ;
let make_buffer_local_always t = add_gc_root ( symbol_as_value t ) ; make_variable_buffer_local t . symbol ; ;
let local_variable_if_set_p = Funcall . Wrap . ( " local - variable - if - set - p " <: Symbol . t @-> Buffer . t @-> return bool ) ; ;
let is_buffer_local_if_set t buffer = local_variable_if_set_p t . symbol buffer
let is_buffer_local_always var = let buffer = Buffer . create ~ name " :* for [ Var . is_buffer_local_always ] " * in let result = is_buffer_local_if_set var buffer in Buffer . Blocking . kill buffer ; result ; ;
module And_value = struct type t = T : ' a var * ' a -> t [ @@ deriving sexp_of ] end
module And_value_option = struct type t = T : ' a var * ' a option -> t [ @@ deriving sexp_of ] end
let hash_variant s = let accu = ref 0 in for i = 0 to String . length s - 1 do accu := 223 * ! accu + Char . code s . [ i ] done ; accu := ! accu land ( 1 lsl 31 - 1 ) ; if ! accu > 0x3FFFFFFF then ! accu - 1 lsl 31 else ! accu
let lexer = make_lexer [ " " ; -> " " ] $$
let main ( ) = let s = lexer ( Stream . of_channel stdin ) in let tags = Hashtbl . create 57 in try while true do let ( strm__ : _ Stream . t ) = s in match Stream . peek strm__ with Some ( Ident tag ) -> Stream . junk strm__ ; print_string " # define MLTAG_ " ; print_string tag ; print_string " \ tVal_int ( " ; let hash = hash_variant tag in begin try failwith ( String . concat ~ sep " : " [ " Doublon ~ tag " ; : tag ; " and " ; Hashtbl . find tags hash ] ) with Not_found -> Hashtbl . add tags hash tag end ; print_int hash ; print_string " ) \ n " | Some ( Kwd " " ) -> -> Stream . junk strm__ ; begin match Stream . peek strm__ with Some ( Ident _ ) -> Stream . junk strm__ ; ( ) | _ -> raise ( Stream . Error " " ) end | Some ( Kwd " " ) $$ -> Stream . junk strm__ ; ( ) | _ -> raise End_of_file done with End_of_file -> ( )
let _ = Printexc . print main ( )
type t = ( Name . t * Kind . t ) t list
let to_string ( env : t ) t : string = [ " " ^ List . fold_left ( fun s ( name , k ) k -> s ^ " , " ^ Name . to_string name ^ " : " ^ Kind . to_string k ) k " " env ^ " ] "
let rec union ( env1 : t ) t ( env2 : t ) t : t = match env1 with | [ ] -> env2 | ( name , kind ) kind :: env -> ( match List . assoc_opt name env2 with | None -> ( name , kind ) kind :: union env env2 | Some kind ' -> let env2 = List . remove_assoc name env2 in let kind = Kind . union kind kind ' in ( name , kind ) kind :: union env env2 ) env2
let unions ( envs : t list ) list : t = List . fold_left union [ ] envs
let reorg ( names : Name . t list ) list ( env : t ) t : t = names |> List . filter_map ( fun name -> match List . assoc_opt name env with | None -> None | Some kind -> Some ( name , kind ) kind ) kind
let rec group_by_kind_aux ( env : t ) t ( kind : Kind . t ) t : ( Kind . t * Name . t list ) list list * Name . t list * Kind . t = match env with | [ ] -> ( [ ] , [ ] , kind ) kind | [ ( name , k ) k ] -> ( [ ] , [ name ] , k ) k | ( name , k ) k :: ls -> let ls , names , k ' = group_by_kind_aux ls k in if k = k ' then ( ls , names @ [ name ] , k ) k else ( ( k ' , names ) names :: ls , [ name ] , k ) k
let group_by_kind ( env : t ) t : ( Kind . t * Name . t list ) list list = match env with | [ ] -> [ ] | [ ( name , k ) k ] -> [ ( k , [ name ] ) ] | ( name , k ) k :: ls -> let ls , names , k ' = group_by_kind_aux ls k in if k = k ' then ( k , names @ [ name ] ) :: ls else if List . length names = 0 then ( k , [ name ] ) :: ls else ( k , [ name ] ) :: ( k ' , names ) names :: ls
let remove ( keys : Name . t list ) list ( env : t ) t : t = env |> List . filter ( fun ( name , _ ) _ -> not ( List . mem name keys ) keys ) keys
let keep_only ( keys : Name . t list ) list ( env : t ) t : t = env |> List . filter ( fun ( name , _ ) _ -> List . mem name keys ) keys
module Spec = struct exception Top include Analyses . DefaultSpec module D = struct include PartitionDomain . ExpPartitions let invariant c ss = fold ( fun s a -> if B . mem MyCFG . unknown_exp s then a else let module B_prod = BatSet . Make2 ( Exp ) ( Exp ) in let s_prod = B_prod . cartesian_product s s in let i = B_prod . Product . fold ( fun ( x , y ) a -> if Exp . compare x y < 0 && not ( InvariantCil . exp_contains_tmp x ) && not ( InvariantCil . exp_contains_tmp y ) && InvariantCil . exp_is_in_scope c . Invariant . scope x && InvariantCil . exp_is_in_scope c . Invariant . scope y then let eq = BinOp ( Eq , x , y , intType ) in Invariant . ( a && of_exp eq ) else a ) s_prod Invariant . none in Invariant . ( a && i ) ) ss Invariant . none end module C = D let name ( ) = " var_eq " let startstate v = D . top ( ) let threadenter ctx lval f args = [ D . top ( ) ] let threadspawn ctx lval f args fctx = ctx . local let exitstate v = D . top ( ) let const_equal c1 c2 = match c1 , c2 with | CStr ( s1 , _ ) , CStr ( s2 , _ ) -> s1 = s2 | CWStr ( is1 , _ ) , CWStr ( is2 , _ ) -> is1 = is2 | CChr c1 , CChr c2 -> c1 = c2 | CInt ( v1 , k1 , _ ) , CInt ( v2 , k2 , _ ) -> Cilint . compare_cilint v1 v2 = 0 && k1 = k2 | CReal ( f1 , k1 , _ ) , CReal ( f2 , k2 , _ ) -> f1 = f2 && k1 = k2 | CEnum ( _ , n1 , e1 ) , CEnum ( _ , n2 , e2 ) -> n1 = n2 && e1 . ename = e2 . ename | _ -> false let option_eq f x y = match x , y with | Some x , Some y -> f x y | None , None -> true | _ -> false let rec typ_equal t1 t2 = let args_eq ( s1 , t1 , _ ) ( s2 , t2 , _ ) = s1 = s2 && typ_equal t1 t2 in let eitem_eq ( s1 , e1 , l1 ) ( s2 , e2 , l2 ) = s1 = s2 && l1 = l2 && exp_equal e1 e2 in match t1 , t2 with | TVoid _ , TVoid _ -> true | TInt ( k1 , _ ) , TInt ( k2 , _ ) -> k1 = k2 | TFloat ( k1 , _ ) , TFloat ( k2 , _ ) -> k1 = k2 | TPtr ( t1 , _ ) , TPtr ( t2 , _ ) -> typ_equal t1 t2 | TArray ( t1 , d1 , _ ) , TArray ( t2 , d2 , _ ) -> option_eq exp_equal d1 d2 && typ_equal t1 t2 | TFun ( rt1 , arg1 , _ , b1 ) , TFun ( rt2 , arg2 , _ , b2 ) -> b1 = b2 && typ_equal rt1 rt2 && option_eq ( GobList . equal args_eq ) arg1 arg2 | TNamed ( ti1 , _ ) , TNamed ( ti2 , _ ) -> ti1 . tname = ti2 . tname && typ_equal ti1 . ttype ti2 . ttype | TComp ( c1 , _ ) , TComp ( c2 , _ ) -> CilType . Compinfo . equal c1 c2 | TEnum ( e1 , _ ) , TEnum ( e2 , _ ) -> e1 . ename = e2 . ename && GobList . equal eitem_eq e1 . eitems e2 . eitems | TBuiltin_va_list _ , TBuiltin_va_list _ -> true | _ -> false and lval_equal ( l1 , o1 ) ( l2 , o2 ) = let rec offs_equal o1 o2 = match o1 , o2 with | NoOffset , NoOffset -> true | Field ( f1 , o1 ) , Field ( f2 , o2 ) -> CilType . Fieldinfo . equal f1 f2 && ( match Cil . unrollType f1 . ftype with | TArray ( TFloat _ , _ , _ ) | TFloat _ -> false | _ -> true ) && offs_equal o1 o2 | Index ( i1 , o1 ) , Index ( i2 , o2 ) -> exp_equal i1 i2 && offs_equal o1 o2 | _ -> false in offs_equal o1 o2 && match l1 , l2 with | Var v1 , Var v2 -> CilType . Varinfo . equal v1 v2 && ( match Cil . unrollTypeDeep v1 . vtype with | TArray ( TFloat _ , _ , _ ) | TFloat _ -> false | _ -> true ) | Mem m1 , Mem m2 -> exp_equal m1 m2 | _ -> false and exp_equal e1 e2 = match e1 , e2 with | Const c1 , Const c2 -> const_equal c1 c2 | AddrOf l1 , AddrOf l2 | StartOf l1 , StartOf l2 | Lval l1 , Lval l2 -> lval_equal l1 l2 | SizeOf t1 , SizeOf t2 -> typ_equal t1 t2 | SizeOfE e1 , SizeOfE e2 -> exp_equal e1 e2 | SizeOfStr s1 , SizeOfStr s2 -> s1 = s2 | AlignOf t1 , AlignOf t2 -> typ_equal t1 t2 | AlignOfE e1 , AlignOfE e2 -> exp_equal e1 e2 | UnOp ( o1 , e1 , t1 ) , UnOp ( o2 , e2 , t2 ) -> o1 = o2 && typ_equal t1 t2 && exp_equal e1 e2 | BinOp ( o1 , e11 , e21 , t1 ) , BinOp ( o2 , e12 , e22 , t2 ) -> o1 = o2 && typ_equal t1 t2 && exp_equal e11 e12 && exp_equal e21 e22 | CastE ( t1 , e1 ) , CastE ( t2 , e2 ) -> typ_equal t1 t2 && exp_equal e1 e2 | _ -> false let rec interesting x = match x with | SizeOf _ | SizeOfE _ | SizeOfStr _ | AlignOf _ | AlignOfE _ | UnOp _ | BinOp _ -> false | Const _ -> true | AddrOf ( Var v2 , _ ) | StartOf ( Var v2 , _ ) | Lval ( Var v2 , _ ) -> true | AddrOf ( Mem e , _ ) | StartOf ( Mem e , _ ) | Lval ( Mem e , _ ) | CastE ( _ , e ) -> interesting e | Question _ -> failwith " Logical operations should be compiled away by CIL . " | _ -> failwith " Unmatched pattern . " let query_exp_equal ask e1 e2 g s = let e1 = constFold false ( stripCasts e1 ) in let e2 = constFold false ( stripCasts e2 ) in if exp_equal e1 e2 then true else match D . find_class e1 s with | Some ss when D . B . mem e2 ss -> true | _ -> false let may_change_t ( b : exp ) ( a : exp ) : bool = let rec type_may_change_t a bt = let rec may_change_t_offset o = match o with | NoOffset -> false | Index ( e , o ) -> type_may_change_t e bt || may_change_t_offset o | Field ( _ , o ) -> may_change_t_offset o in let at = Cilfacade . typeOf a in ( isIntegralType at && isIntegralType bt ) || ( typ_equal at bt ) || match a with | Const _ | SizeOf _ | SizeOfE _ | SizeOfStr _ | AlignOf _ | AlignOfE _ -> false | UnOp ( _ , e , _ ) -> type_may_change_t e bt | BinOp ( _ , e1 , e2 , _ ) -> type_may_change_t e1 bt || type_may_change_t e2 bt | Lval ( Var _ , o ) | AddrOf ( Var _ , o ) | StartOf ( Var _ , o ) -> may_change_t_offset o | Lval ( Mem e , o ) | AddrOf ( Mem e , o ) | StartOf ( Mem e , o ) -> may_change_t_offset o || type_may_change_t e bt | CastE ( t , e ) -> type_may_change_t e bt | Question _ -> failwith " Logical operations should be compiled away by CIL . " | _ -> failwith " Unmatched pattern . " in let bt = unrollTypeDeep ( Cilfacade . typeOf b ) in type_may_change_t a bt let may_change_pt ask ( b : exp ) ( a : exp ) : bool = let pt e = ask ( Queries . MayPointTo e ) in let rec lval_may_change_pt a bl : bool = let rec may_change_pt_offset o = match o with | NoOffset -> false | Index ( e , o ) -> lval_may_change_pt e bl || may_change_pt_offset o | Field ( _ , o ) -> may_change_pt_offset o in let als = pt a in Queries . LS . is_top als || Queries . LS . mem ( dummyFunDec . svar , ` NoOffset ) als || Queries . LS . mem bl als || match a with | Const _ | SizeOf _ | SizeOfE _ | SizeOfStr _ | AlignOf _ | AlignOfE _ -> false | UnOp ( _ , e , _ ) -> lval_may_change_pt e bl | BinOp ( _ , e1 , e2 , _ ) -> lval_may_change_pt e1 bl || lval_may_change_pt e2 bl | Lval ( Var _ , o ) | AddrOf ( Var _ , o ) | StartOf ( Var _ , o ) -> may_change_pt_offset o | Lval ( Mem e , o ) | AddrOf ( Mem e , o ) | StartOf ( Mem e , o ) -> may_change_pt_offset o || lval_may_change_pt e bl | CastE ( t , e ) -> lval_may_change_pt e bl | Question _ -> failwith " Logical operations should be compiled away by CIL . " | _ -> failwith " Unmatched pattern . " in let bls = pt b in if Queries . LS . is_top bls then true else Queries . LS . exists ( lval_may_change_pt a ) bls let may_change ( ask : Queries . ask ) ( b : exp ) ( a : exp ) : bool = let pt e = ask . f ( Queries . MayPointTo e ) in let bls = pt b in let bt = match unrollTypeDeep ( Cilfacade . typeOf b ) with | TPtr ( t , _ ) -> t | exception Cilfacade . TypeOfError _ | _ -> voidType in let rec type_may_change_apt a = match a , b with | Lval ( Var _ , NoOffset ) , AddrOf ( Mem ( Lval _ ) , Field ( _ , _ ) ) -> false false ) * | _ -> type_may_change_t false a and type_may_change_t deref a = let rec may_change_t_offset o = match o with | NoOffset -> false | Index ( e , o ) -> type_may_change_apt e || may_change_t_offset o | Field ( _ , o ) -> may_change_t_offset o in let at = match unrollTypeDeep ( Cilfacade . typeOf a ) with | TPtr ( t , a ) -> t | at -> at in bt = voidType || ( isIntegralType at && isIntegralType bt ) || ( deref && typ_equal ( TPtr ( at , [ ] ) ) bt ) || typ_equal at bt || match a with | Const _ | SizeOf _ | SizeOfE _ | SizeOfStr _ | AlignOf _ | AlignOfE _ -> false | UnOp ( _ , e , _ ) -> type_may_change_t deref e | BinOp ( _ , e1 , e2 , _ ) -> type_may_change_t deref e1 || type_may_change_t deref e2 | Lval ( Var _ , o ) | AddrOf ( Var _ , o ) | StartOf ( Var _ , o ) -> may_change_t_offset o | Lval ( Mem e , o ) -> may_change_t_offset o || type_may_change_t true e | AddrOf ( Mem e , o ) -> may_change_t_offset o || type_may_change_t false e | StartOf ( Mem e , o ) -> may_change_t_offset o || type_may_change_t false e | CastE ( t , e ) -> type_may_change_t deref e | Question _ -> failwith " Logical operations should be compiled away by CIL . " | _ -> failwith " Unmatched pattern . " and lval_may_change_pt a bl : bool = let rec may_change_pt_offset o = match o with | NoOffset -> false | Index ( e , o ) -> lval_may_change_pt e bl || may_change_pt_offset o | Field ( _ , o ) -> may_change_pt_offset o in let rec addrOfExp e = match e with | Lval ( Var v , o ) -> Some ( AddrOf ( Var v , o ) ) | AddrOf ( Var _ , _ ) -> None | StartOf ( Var _ , _ ) -> None | Lval ( Mem e , o ) -> Some ( AddrOf ( Mem e , o ) ) | AddrOf ( Mem e , o ) -> ( match addrOfExp e with Some e -> Some ( AddrOf ( Mem e , o ) ) | x -> x ) | StartOf ( Mem e , o ) -> ( match addrOfExp e with Some e -> Some ( AddrOf ( Mem e , o ) ) | x -> x ) | CastE ( t , e ) -> addrOfExp e | _ -> None in let lval_is_not_disjoint ( v , o ) als = let rec oleq o s = match o , s with | ` NoOffset , _ -> true | ` Field ( f1 , o ) , ` Field ( f2 , s ) when CilType . Fieldinfo . equal f1 f2 -> oleq o s | ` Index ( i1 , o ) , ` Index ( i2 , s ) when exp_equal i1 i2 -> oleq o s | _ -> false in if Queries . LS . is_top als then false else Queries . LS . exists ( fun ( u , s ) -> CilType . Varinfo . equal v u && oleq o s ) als in let ( als , test ) = match addrOfExp a with | None -> ( Queries . LS . bot ( ) , false ) | Some e -> let als = pt e in ( als , lval_is_not_disjoint bl als ) in if ( Queries . LS . is_top als ) || Queries . LS . mem ( dummyFunDec . svar , ` NoOffset ) als then type_may_change_apt a else test || match a with | Const _ | SizeOf _ | SizeOfE _ | SizeOfStr _ | AlignOf _ | AlignOfE _ -> false | UnOp ( _ , e , _ ) -> lval_may_change_pt e bl | BinOp ( _ , e1 , e2 , _ ) -> lval_may_change_pt e1 bl || lval_may_change_pt e2 bl | Lval ( Var _ , o ) | AddrOf ( Var _ , o ) | StartOf ( Var _ , o ) -> may_change_pt_offset o | Lval ( Mem e , o ) | AddrOf ( Mem e , o ) | StartOf ( Mem e , o ) -> may_change_pt_offset o || lval_may_change_pt e bl | CastE ( t , e ) -> lval_may_change_pt e bl | Question _ -> failwith " Logical operations should be compiled away by CIL . " | _ -> failwith " Unmatched pattern . " in let r = if Queries . LS . is_top bls || Queries . LS . mem ( dummyFunDec . svar , ` NoOffset ) bls then ( type_may_change_apt a ) else Queries . LS . exists ( lval_may_change_pt a ) bls in r let remove_exp ask ( e : exp ) ( st : D . t ) : D . t = D . filter ( fun x -> not ( may_change ask e x ) ) st let remove ask ( e : lval ) ( st : D . t ) : D . t = remove_exp ask ( mkAddrOf e ) st let rec is_global_var ( ask : Queries . ask ) x = match x with | SizeOf _ | SizeOfE _ | SizeOfStr _ | AlignOf _ | AlignOfE _ | UnOp _ | BinOp _ -> None | Const _ -> Some false | Lval ( Var v , _ ) -> Some ( v . vglob || ( ask . f ( Queries . IsMultiple v ) ) ) | Lval ( Mem e , _ ) -> begin match ask . f ( Queries . MayPointTo e ) with | ls when not ( Queries . LS . is_top ls ) && not ( Queries . LS . mem ( dummyFunDec . svar , ` NoOffset ) ls ) -> Some ( Queries . LS . exists ( fun ( v , _ ) -> is_global_var ask ( Lval ( var v ) ) = Some true ) ls ) | _ -> Some true end | CastE ( t , e ) -> is_global_var ask e | AddrOf ( Var v , _ ) -> Some ( ask . f ( Queries . IsMultiple v ) ) | AddrOf lv -> Some false | StartOf ( Var v , _ ) -> Some ( ask . f ( Queries . IsMultiple v ) ) | StartOf lv -> Some false | Question _ -> failwith " Logical operations should be compiled away by CIL . " | _ -> failwith " Unmatched pattern . " let add_eq ask ( lv : lval ) ( rv : Exp . t ) st = let lvt = unrollType @@ Cilfacade . typeOfLval lv in if is_global_var ask ( Lval lv ) = Some false && interesting rv && is_global_var ask rv = Some false && ( ( isArithmeticType lvt && match lvt with | TFloat _ -> false | _ -> true ) || isPointerType lvt ) then D . add_eq ( rv , Lval lv ) ( remove ask lv st ) else remove ask lv st let reachables ( ask : Queries . ask ) es = let reachable e st = match st with | None -> None | Some st -> let vs = ask . f ( Queries . ReachableFrom e ) in if Queries . LS . is_top vs then None else Some ( Queries . LS . join vs st ) in List . fold_right reachable es ( Some ( Queries . LS . empty ( ) ) ) let body ctx f = ctx . local let branch ctx exp tv = ctx . local let return ctx exp fundec = let rm v = remove ( Analyses . ask_of_ctx ctx ) ( Var v , NoOffset ) in List . fold_right rm ( fundec . sformals @ fundec . slocals ) ctx . local let assign ctx ( lval : lval ) ( rval : exp ) : D . t = let rval = constFold true ( stripCasts rval ) in add_eq ( Analyses . ask_of_ctx ctx ) lval rval ctx . local let enter ctx lval f args = let rec fold_left2 f r xs ys = match xs , ys with | x :: xs , y :: ys -> fold_left2 f ( f r x y ) xs ys | _ -> r in let assign_one_param st lv exp = let rm = remove ( Analyses . ask_of_ctx ctx ) ( Var lv , NoOffset ) st in add_eq ( Analyses . ask_of_ctx ctx ) ( Var lv , NoOffset ) exp rm in let nst = try fold_left2 assign_one_param ctx . local f . sformals args with SetDomain . Unsupported _ -> D . top ( ) in match D . is_bot ctx . local with | true -> raise Analyses . Deadcode | false -> [ ctx . local , nst ] let combine ctx lval fexp f args fc st2 = match D . is_bot ctx . local with | true -> raise Analyses . Deadcode | false -> match lval with | Some lval -> remove ( Analyses . ask_of_ctx ctx ) lval st2 | None -> st2 let remove_reachable ctx es = let ask = Analyses . ask_of_ctx ctx in match reachables ask es with | None -> D . top ( ) | Some rs -> Queries . LS . fold ( fun lval st -> remove ask ( Lval . CilLval . to_lval lval ) st ) rs ctx . local let unknown_fn ctx lval f args = let args = match LF . get_invalidate_action f . vname with | Some fnc -> fnc ` Write args | None -> if GobConfig . get_bool " sem . unknown_function . invalidate . args " then args else [ ] in let es = match lval with | Some l -> mkAddrOf l :: args | None -> args in match D . is_bot ctx . local with | true -> raise Analyses . Deadcode | false -> remove_reachable ctx es let safe_fn = function | " memcpy " -> true | " __builtin_return_address " -> true | _ -> false let special ctx lval f args = match LibraryFunctions . classify f . vname args with | ` Unknown " spinlock_check " -> begin match lval with | Some x -> assign ctx x ( List . hd args ) | None -> unknown_fn ctx lval f args end | ` Unknown x when safe_fn x -> ctx . local | ` ThreadCreate ( _ , _ , arg ) -> begin match D . is_bot ctx . local with | true -> raise Analyses . Deadcode | false -> remove_reachable ctx [ arg ] end | _ -> unknown_fn ctx lval f args let eq_set ( e : exp ) s = match D . find_class e s with | None -> Queries . ES . empty ( ) | Some es when D . B . is_bot es -> Queries . ES . bot ( ) | Some es -> let et = Cilfacade . typeOf e in let add x xs = Queries . ES . add ( CastE ( et , x ) ) xs in D . B . fold add es ( Queries . ES . empty ( ) ) let rec eq_set_clos e s = match e with | SizeOf _ | SizeOfE _ | SizeOfStr _ | AlignOf _ | Const _ | AlignOfE _ | UnOp _ | BinOp _ | AddrOf ( Var _ , _ ) | StartOf ( Var _ , _ ) | Lval ( Var _ , _ ) -> eq_set e s | AddrOf ( Mem e , ofs ) -> Queries . ES . map ( fun e -> mkAddrOf ( mkMem ~ addr : e ~ off : ofs ) ) ( eq_set_clos e s ) | StartOf ( Mem e , ofs ) -> Queries . ES . map ( fun e -> mkAddrOrStartOf ( mkMem ~ addr : e ~ off : ofs ) ) ( eq_set_clos e s ) | Lval ( Mem e , ofs ) -> Queries . ES . map ( fun e -> Lval ( mkMem ~ addr : e ~ off : ofs ) ) ( eq_set_clos e s ) | CastE ( t , e ) -> Queries . ES . map ( fun e -> CastE ( t , e ) ) ( eq_set_clos e s ) | Question _ -> failwith " Logical operations should be compiled away by CIL . " | _ -> failwith " Unmatched pattern . " let query ctx ( type a ) ( x : a Queries . t ) : a Queries . result = match x with | Queries . MustBeEqual ( e1 , e2 ) when query_exp_equal ( Analyses . ask_of_ctx ctx ) e1 e2 ctx . global ctx . local -> true | Queries . EqualSet e -> let r = eq_set_clos e ctx . local in r | _ -> Queries . Result . top x end
let _ = MCP . register_analysis ( module Spec : MCPSpec )
type t = { compilation_unit : Compilation_unit . t ; name : string ; name_stamp : int ; } type nonrec t = t let compare t1 t2 = if t1 == t2 then 0 else let c = t1 . name_stamp - t2 . name_stamp in if c <> 0 then c else Compilation_unit . compare t1 . compilation_unit t2 . compilation_unit let equal t1 t2 = if t1 == t2 then true else t1 . name_stamp = t2 . name_stamp && Compilation_unit . equal t1 . compilation_unit t2 . compilation_unit let output chan t = output_string chan t . name ; output_string chan " _ " ; output_string chan ( Int . to_string t . name_stamp ) let hash t = t . name_stamp lxor ( Compilation_unit . hash t . compilation_unit ) let print ppf t = if Compilation_unit . equal t . compilation_unit ( Compilation_unit . get_current_exn ( ) ) then begin Format . fprintf ppf " % s /% d " t . name t . name_stamp end else begin Format . fprintf ppf " % a . % s /% d " Compilation_unit . print t . compilation_unit t . name t . name_stamp end end )
let previous_name_stamp = ref ( - 1 )
let create_with_name_string ? current_compilation_unit name = let compilation_unit = match current_compilation_unit with | Some compilation_unit -> compilation_unit | None -> Compilation_unit . get_current_exn ( ) in let name_stamp = incr previous_name_stamp ; ! previous_name_stamp in { compilation_unit ; name ; name_stamp ; }
let create ? current_compilation_unit name = let name = ( name : Internal_variable_names . t :> string ) in create_with_name_string ? current_compilation_unit name
let create_with_same_name_as_ident ident = create_with_name_string ( Ident . name ident )
let rename ? current_compilation_unit t = create_with_name_string ? current_compilation_unit t . name
let in_compilation_unit t cu = Compilation_unit . equal cu t . compilation_unit
let get_compilation_unit t = t . compilation_unit
let name t = t . name
let unique_name t = t . name ^ " _ " ^ ( Int . to_string t . name_stamp )
let print_list ppf ts = List . iter ( fun t -> Format . fprintf ppf " @ % a " print t ) ts
let debug_when_stamp_matches t ~ stamp ~ f = if t . name_stamp = stamp then f ( )
let print_opt ppf = function | None -> Format . fprintf ppf " < no var " > | Some t -> print ppf t
type pair = t * t
module Pair = Identifiable . Make ( Identifiable . Pair ( T ) ( T ) )
let compare_lists l1 l2 = Misc . Stdlib . List . compare compare l1 l2
let output_full chan t = Compilation_unit . output chan t . compilation_unit ; output_string chan " . " ; output chan t
type exporter = value -> string
type t = { variable_name : string ; variable_description : string ; variable_exporter : exporter }
let compare v1 v2 = String . compare v1 . variable_name v2 . variable_name
let default_exporter varname value = Printf . sprintf " % s =% s " varname value
let make ( name , description ) = if name " " = then raise Empty_variable_name else { variable_name = name ; variable_description = description ; variable_exporter = default_exporter name }
let make_with_exporter exporter ( name , description ) = if name " " = then raise Empty_variable_name else { variable_name = name ; variable_description = description ; variable_exporter = exporter }
let name_of_variable v = v . variable_name
let description_of_variable v = v . variable_description
let ( variables : ( string , t ) Hashtbl . t ) = Hashtbl . create 10
let register_variable variable = if Hashtbl . mem variables variable . variable_name then raise ( Variable_already_registered variable . variable_name ) else Hashtbl . add variables variable . variable_name variable
let find_variable variable_name = try Some ( Hashtbl . find variables variable_name ) with Not_found -> None
let string_of_binding variable value = variable . variable_exporter value
let get_registered_variables ( ) = let f _variable_name variable variable_list = variable :: variable_list in List . sort compare ( Hashtbl . fold f variables [ ] )
let suite : ( string * [ ` > Quick ] * ( unit -> unit ) ) list = [ ( " nullable " , ` Quick , fun ( ) -> let _ , mk_variables , _ = [ % graphql { | query Foo ( $ int : Int , $ string : String , $ bool : Boolean , $ float : Float , $ id : ID , $ enum : COLOR ) { non_nullable_int } } ] | in let variables = mk_variables id ~ int : 1 ~ string " : 2 " ~ bool : true ~ float : 12 . 3 ~ id " : 42 " ~ enum ` : RED ( ) in let expected = ` Assoc [ " int " , ` Int 1 ; " string " , ` String " 2 " ; " bool " , ` Bool true ; " float " , ` Float 12 . 3 ; " id " , ` String " 42 " ; " enum " , ` String " RED " ] in Alcotest . ( check yojson ) " nullable " expected variables ; ) ; ( " non - nullable " , ` Quick , fun ( ) -> let _ , mk_variables , _ = [ % graphql { | query Foo ( $ int : Int , ! $ string : String , ! $ bool : Boolean , ! $ float : Float , ! $ id : ID , ! $ enum : COLOR ) ! { non_nullable_int } } ] | in let variables = mk_variables id ~ int : 1 ~ string " : 2 " ~ bool : true ~ float : 12 . 3 ~ id " : 42 " ~ enum ` : RED ( ) in let expected = ` Assoc [ " int " , ` Int 1 ; " string " , ` String " 2 " ; " bool " , ` Bool true ; " float " , ` Float 12 . 3 ; " id " , ` String " 42 " ; " enum " , ` String " RED " ] in Alcotest . ( check yojson ) " non_nullable " expected variables ; ) ; ( " list " , ` Quick , fun ( ) -> let _ , mk_variables , _ = [ % graphql { | query Foo ( $ list : [ Int ] ) { non_nullable_int } } ] | in let variables = mk_variables id ~ list [ : Some 1 ; None ] ( ) in let expected = ` Assoc [ " list " , ` List [ ` Int 1 ; ` Null ] ] in Alcotest . ( check yojson ) " list " expected variables ; ) ; ]
let test_query variables = Test_common . test_query Echo_schema . schema ( ) ~ variables
let suite : ( string * [ > ` Quick ] * ( unit -> unit ) ) list = [ ( " string variable " , ` Quick , fun ( ) -> let variables = [ ( " x " , ` String " foo bar baz " ) ] in let query = " { string ( x : $ x ) } " in test_query variables query ( ` Assoc [ ( " data " , ` Assoc [ ( " string " , ` String " foo bar baz " ) ] ) ] ) ) ; ( " float variable " , ` Quick , fun ( ) -> let variables = [ ( " x " , ` Float 42 . 5 ) ] in let query = " { float ( x : $ x ) } " in test_query variables query ( ` Assoc [ ( " data " , ` Assoc [ ( " float " , ` Float 42 . 5 ) ] ) ] ) ) ; ( " int variable " , ` Quick , fun ( ) -> let variables = [ ( " x " , ` Int 42 ) ] in let query = " { int ( x : $ x ) } " in test_query variables query ( ` Assoc [ ( " data " , ` Assoc [ ( " int " , ` Int 42 ) ] ) ] ) ) ; ( " bool variable " , ` Quick , fun ( ) -> let variables = [ ( " x " , ` Bool false ) ] in let query = " { bool ( x : $ x ) } " in test_query variables query ( ` Assoc [ ( " data " , ` Assoc [ ( " bool " , ` Bool false ) ] ) ] ) ) ; ( " enum variable " , ` Quick , fun ( ) -> let variables = [ ( " x " , ` Enum " RED " ) ] in let query = " { enum ( x : $ x ) } " in test_query variables query ( ` Assoc [ ( " data " , ` Assoc [ ( " enum " , ` String " RED " ) ] ) ] ) ) ; ( " list variable " , ` Quick , fun ( ) -> let variables = [ ( " x " , ` List [ ` Bool true ; ` Bool false ] ) ] in let query = " { bool_list ( x : [ false , true ] ) } " in test_query variables query ( ` Assoc [ ( " data " , ` Assoc [ ( " bool_list " , ` List [ ` Bool false ; ` Bool true ] ) ] ) ; ] ) ) ; ( " input object variable " , ` Quick , fun ( ) -> let obj = ` Assoc [ ( " title " , ` String " Mr " ) ; ( " first_name " , ` String " John " ) ; ( " last_name " , ` String " Doe " ) ; ] in let variables = [ ( " x " , obj ) ] in let query = " { input_obj ( x : { title : " \ Mr " , \ first_name : " \ John " , \ last_name : \ " \ Doe " } ) \ } " in test_query variables query ( ` Assoc [ ( " data " , ` Assoc [ ( " input_obj " , ` String " John Doe " ) ] ) ] ) ) ; ( " null for optional variable " , ` Quick , fun ( ) -> test_query [ ( " x " , ` Null ) ] " { string ( x : $ x ) } " ( ` Assoc [ ( " data " , ` Assoc [ ( " string " , ` Null ) ] ) ] ) ) ; ( " null for required variable " , ` Quick , fun ( ) -> let variables = [ ( " x " , ` Null ) ] in let query = " { input_obj ( x : $ x ) } " in test_query variables query ( ` Assoc [ ( " errors " , ` List [ ` Assoc [ ( " message " , ` String " Argument ` x ` of type ` person ` ! expected on field \ ` input_obj ` , found null . " ) ; ] ; ] ) ; ( " data " , ` Null ) ; ] ) ) ; ( " missing required variable " , ` Quick , fun ( ) -> let variables = [ ] in let query = " { input_obj ( x : $ x ) } " in test_query variables query ( ` Assoc [ ( " errors " , ` List [ ` Assoc [ ( " message " , ` String " Argument ` x ` of type ` person ` ! expected on field \ ` input_obj ` , found null . " ) ; ] ; ] ) ; ( " data " , ` Null ) ; ] ) ) ; ( " missing nullable variable " , ` Quick , fun ( ) -> let variables = [ ] in let query = " { string ( x : $ x ) } " in test_query variables query ( ` Assoc [ " data " , ` Assoc [ " string " , ` Null ] ] ) ) ; ( " variable coercion : single value to list " , ` Quick , fun ( ) -> let variables = [ ( " x " , ` Bool false ) ] in let query = " { bool_list ( x : $ x ) } " in test_query variables query ( ` Assoc [ ( " data " , ` Assoc [ ( " bool_list " , ` List [ ` Bool false ] ) ] ) ] ) ) ; ( " variable coercion : int to float " , ` Quick , fun ( ) -> let variables = [ ( " x " , ` Int 42 ) ] in let query = " { float ( x : $ x ) } " in test_query variables query ( ` Assoc [ ( " data " , ` Assoc [ ( " float " , ` Float 42 . 0 ) ] ) ] ) ) ; ( " variable coercion : int to ID " , ` Quick , fun ( ) -> let variables = [ ( " x " , ` Int 42 ) ] in let query = " { id ( x : $ x ) } " in test_query variables query ( ` Assoc [ ( " data " , ` Assoc [ ( " id " , ` String " 42 " ) ] ) ] ) ) ; ( " variable coercion : string to ID " , ` Quick , fun ( ) -> let variables = [ ( " x " , ` String " 42 " ) ] in let query = " { id ( x : $ x ) } " in test_query variables query ( ` Assoc [ ( " data " , ` Assoc [ ( " id " , ` String " 42 " ) ] ) ] ) ) ; ( " default variable " , ` Quick , fun ( ) -> let query = " query has_defaults ( $ x : Int ! = 42 ) { int ( x : $ x ) } " in test_query [ ] query ( ` Assoc [ ( " data " , ` Assoc [ ( " int " , ` Int 42 ) ] ) ] ) ) ; ( " variable overrides default variable " , ` Quick , fun ( ) -> let variables = [ ( " x " , ` Int 43 ) ] in let query = " query has_defaults ( $ x : Int ! = 42 ) { int ( x : $ x ) } " in test_query variables query ( ` Assoc [ ( " data " , ` Assoc [ ( " int " , ` Int 43 ) ] ) ] ) ) ; ]
module Operation = struct module T = struct type t = | Set | Let | Unlet | Makunbound | Defvaralias [ @@ deriving enumerate , sexp_of ] end include T let type_ = Value . Type . enum [ % message " variable - change - operation " ] ( module T ) ( ( function | Set -> " set " | Let -> " let " | Unlet -> " unlet " | Makunbound -> " makunbound " | Defvaralias -> " defvaralias " ) >> Symbol . intern >> Symbol . to_value ) ; ; end
module Event = struct type t = { local_to_buffer : Buffer . t option ; new_value : Value . t ; operation : Operation . t ; variable_changed : Symbol . t } [ @@ deriving sexp_of ] end
let build_function here f = Defun . lambda here ( Returns Value . Type . unit ) ( let % map_open . Defun ( ) = return ( ) and symbol = required " symbol " Symbol . type_ and newval = required " newval " Value . Type . value and operation = required " operation " Operation . type_ and where = required " where " ( Value . Type . option Buffer . type_ ) in f { Event . local_to_buffer = where ; new_value = newval ; operation ; variable_changed = symbol } ) ; ;
type t = { symbol : Symbol . t ; watcher : Function . t }
let add_variable_watcher = Funcall . Wrap . ( " add - variable - watcher " <: Symbol . type_ @-> Function . type_ @-> return nil ) ; ;
let add here var ~ f = let watcher = build_function here f in add_variable_watcher ( Var . symbol var ) watcher ; { symbol = Var . symbol var ; watcher } ; ;
let remove_variable_watcher = Funcall . Wrap . ( " remove - variable - watcher " <: Symbol . type_ @-> Function . type_ @-> return nil ) ; ;
let remove { symbol ; watcher } = remove_variable_watcher symbol watcher
let test_check_variance context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | def narnia ( ) : pass def foo ( ) -> None : [ narnia ( ) ] + [ 2 ] } | [ " Missing return annotation [ 3 ] : Returning ` None ` but no return type is specified . " ] ; assert_type_errors { | import typing def foo ( input : str ) -> typing . List [ int ] : return typing . cast ( typing . List [ float ] , input ) } | [ " Incompatible return type [ 7 ] : Expected ` List [ int ] ` but got ` List [ float ] ` . " ] ; assert_type_errors { | import typing def foo ( input ) -> typing . List [ int ] : return typing . cast ( typing . List [ unknown ] , input ) } | [ " Missing parameter annotation [ 2 ] : Parameter ` input ` has no type specified . " ; " Incompatible return type [ 7 ] : Expected ` List [ int ] ` but got ` unknown ` . " ; " Unbound name [ 10 ] : Name ` unknown ` is used but not defined in the current scope . " ; ] ; assert_type_errors { | import typing def foo ( a : typing . Mapping [ str , float ] ) -> float : return a [ " a " ] def bar ( x : typing . Dict [ str , int ] ) -> float : return foo ( x ) } | [ ] ; assert_type_errors { | import typing def foo ( d : typing . Dict [ int , typing . Any ] ) -> None : d . update ( { 1 : 1 } ) } | [ " Missing parameter annotation [ 2 ] : Parameter ` d ` must have a type " ^ " that does not contain ` Any ` . " ; ] ; assert_type_errors { | from typing import TypeVar , Generic IV = TypeVar ( ' IV ' ) CV = TypeVar ( ' CV ' , covariant = True ) class A ( Generic [ IV ] ) : pass class B ( A [ CV ] , Generic [ CV ] ) : pass } | [ " Invalid type variance [ 46 ] : The type variable ` Variable [ CV ] ( covariant ) ` is incompatible \ with parent class type variable ` Variable [ IV ] ` because subclasses cannot use more \ permissive type variables than their superclasses . " ; ] ; assert_type_errors { | from typing import TypeVar , Generic IV = TypeVar ( ' IV ' ) CV = TypeVar ( ' CV ' , contravariant = True ) class A ( Generic [ IV ] ) : pass class B ( A [ CV ] , Generic [ CV ] ) : pass } | [ " Invalid type variance [ 46 ] : The type variable ` Variable [ CV ] ( contravariant ) ` is incompatible \ with parent class type variable ` Variable [ IV ] ` because subclasses cannot use more \ permissive type variables than their superclasses . " ; ] ; assert_type_errors { | from typing import TypeVar , Generic CV = TypeVar ( ' CV ' , covariant = True ) CNV = TypeVar ( ' CNV ' , contravariant = True ) class A ( Generic [ CV ] ) : pass class B ( A [ CNV ] , Generic [ CNV ] ) : pass } | [ " Invalid type variance [ 46 ] : The type variable ` Variable [ CNV ] ( contravariant ) ` is \ incompatible with parent class type variable ` Variable [ CV ] ( covariant ) ` because subclasses \ cannot use more permissive type variables than their superclasses . " ; ] ; assert_type_errors { | from typing import TypeVar , Generic CV = TypeVar ( ' CV ' , covariant = True ) CNV = TypeVar ( ' CNV ' , contravariant = True ) class A ( Generic [ CNV ] ) : pass class B ( A [ CV ] , Generic [ CV ] ) : pass } | [ " Invalid type variance [ 46 ] : The type variable ` Variable [ CV ] ( covariant ) ` is incompatible \ with parent class type variable ` Variable [ CNV ] ( contravariant ) ` because subclasses cannot \ use more permissive type variables than their superclasses . " ; ] ; assert_type_errors { | from typing import TypeVar , Generic IV = TypeVar ( ' IV ' ) CV = TypeVar ( ' CV ' , covariant = True ) class A ( Generic [ CV ] ) : pass class B ( A [ IV ] , Generic [ IV ] ) : pass } | [ ] ; assert_type_errors { | from typing import TypeVar , Generic IV = TypeVar ( ' IV ' ) CV = TypeVar ( ' CV ' , contravariant = True ) class A ( Generic [ CV ] ) : pass class B ( A [ IV ] , Generic [ IV ] ) : pass } | [ ] ; assert_type_errors { | from typing import TypeVar , Generic T = TypeVar ( ' T ' ) U = TypeVar ( ' U ' ) V = TypeVar ( ' V ' ) class A ( Generic [ T ] ) : pass class B ( A [ U , V ] , Generic [ U , V ] ) : pass } | [ " Invalid type parameters [ 24 ] : Generic type ` A ` expects 1 type parameter , received 2 . " ] ; assert_type_errors { | from typing import TypeVar , Generic T = TypeVar ( ' T ' ) U = TypeVar ( ' U ' ) V = TypeVar ( ' V ' ) class A ( Generic [ T , U ] ) : pass class B ( A [ V ] , Generic [ V ] ) : pass } | [ " Invalid type parameters [ 24 ] : Generic type ` A ` expects 2 type parameters , received 1 . " ]
let test_check_literal_variance context = let assert_type_errors = assert_type_errors ~ context in assert_type_errors { | import typing x : typing . List [ float ] = [ ] x = [ 1 ] } | [ ] ; assert_type_errors { | import typing x : typing . List [ float ] = [ ] x = [ y for y in [ 1 , 2 , 3 , 4 ] ] } | [ ] ; assert_type_errors { | import typing def foo ( x : typing . List [ float ] = [ 1 ] ) -> typing . List [ float ] : return x } | [ " Incompatible variable type [ 9 ] : x is declared to have type ` List [ float ] ` but is " ^ " used as type ` List [ int ] ` . " ; ] ; assert_type_errors { | import typing x : typing . List [ float ] = [ ] y : typing . List [ int ] = [ 1 ] x = y } | [ " Incompatible variable type [ 9 ] : x is declared to have type ` List [ float ] ` but is " ^ " used as type ` List [ int ] ` . " ; ] ; assert_type_errors { | import typing x : typing . Dict [ str , float ] = { } x = { " s " : 1 } } | [ ] ; assert_type_errors { | import typing x : typing . Dict [ str , float ] = { } x = { " s " : value for value in [ 1 , 2 , 3 ] } } | [ ] ; assert_type_errors { | import typing x : typing . Dict [ str , float ] = { } x = { " s " : " " } } | [ " Incompatible variable type [ 9 ] : x is declared to have type ` Dict [ str , float ] ` but " ^ " is used as type ` Dict [ str , str ] ` . " ; ] ; assert_type_errors { | import typing x : typing . Dict [ str , float ] = { " s " : 1 } y : typing . Dict [ str , int ] = { " s " : 1 } x = y } | [ " Incompatible variable type [ 9 ] : x is declared to have type ` Dict [ str , float ] ` but " ^ " is used as type ` Dict [ str , int ] ` . " ; ] ; assert_type_errors { | import typing class Foo ( ) : x : typing . List [ float ] = [ ] y : typing . List [ int ] = [ 1 ] z = Foo ( ) z . x = y } | [ " Incompatible attribute type [ 8 ] : Attribute ` x ` declared in class ` Foo ` has type \ ` List [ float ] ` but is used as type ` List [ int ] ` . " ; ] ; assert_type_errors { | import typing def foo ( ) -> typing . List [ float ] : return [ 1 ] } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . List [ float ] : a = [ 1 ] return a } | [ " Incompatible return type [ 7 ] : Expected ` List [ float ] ` but got ` List [ int ] ` . " ] ; assert_type_errors { | import typing def foo ( ) -> typing . Dict [ float , float ] : return { 1 : 1 } } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . Dict [ float , float ] : a = { 1 : 1 } return a } | [ " Incompatible return type [ 7 ] : Expected ` Dict [ float , float ] ` but got ` Dict [ int , int ] ` . " ] ; assert_type_errors { | import typing def foo ( ) -> typing . Set [ float ] : return { 1 } } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . Set [ float ] : return { x for x in [ 1 , 2 , 3 ] } } | [ ] ; assert_type_errors { | import typing def foo ( ) -> typing . Set [ float ] : a = { 1 } return a } | [ " Incompatible return type [ 7 ] : Expected ` Set [ float ] ` but got ` Set [ int ] ` . " ] ; assert_type_errors { | import typing def foo ( a : typing . List [ float ] ) -> float : return a [ 0 ] def bar ( ) -> float : return foo ( [ 1 , 2 , 3 ] ) } | [ ] ; assert_type_errors { | import typing def foo ( a : typing . List [ float ] ) -> float : return a [ 0 ] def bar ( ) -> float : a = [ 1 , 2 , 3 ] return foo ( a ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` List [ float ] ` but got ` List [ int ] ` . " ; ] ; assert_type_errors ~ show_error_traces : true { | import typing def foo ( a : typing . List [ float ] ) -> float : return a [ 0 ] def bar ( ) -> float : a = [ 1 , 2 , 3 ] return foo ( a ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` List [ float ] ` but got ` List [ int ] ` . This call might modify the type of the parameter . See \ https :// pyre - check . org / docs / errors # covariance - and - contravariance for mutable container \ errors . " ; ] ; assert_type_errors { | import typing def foo ( a : typing . Dict [ str , float ] ) -> float : return a [ " a " ] def bar ( ) -> float : return foo ( { " a " : 1 } ) } | [ ] ; assert_type_errors { | import typing def foo ( a : typing . Dict [ str , float ] ) -> float : return a [ " a " ] def bar ( ) -> float : a = { " a " : 1 } return foo ( a ) } | [ " Incompatible parameter type [ 6 ] : In call ` foo ` , for 1st positional only parameter expected \ ` Dict [ str , float ] ` but got ` Dict [ str , int ] ` . " ; ]
let ( ) = " variance " >::: [ " check_variance " >:: test_check_variance ; " check_literal_variance " >:: test_check_literal_variance ; ] |> Test . run
module rec X : sig type t = int * bool type t = A | B let f = function A | B -> 0 end ; ; [ %% expect { | Modules do not match : sig type t = X . t = A | B val f : t -> int end is not included in sig type t = int * bool end Type declarations do not match : type t = X . t = A | B is not included in type t = int * bool } ] ; ; |
module Make ( X : sig val f : [ ` A ] -> unit end ) = struct let make f1 f2 arg = match arg with ` A -> f1 arg ; f2 arg let f = make X . f ( fun _ -> ( ) ) end ; ; [ %% expect { |
module Make : functor ( X : sig val f : [ ` A ] -> unit end ) -> sig val make : ( ( [ < ` A ] as ' a ) -> ' b ) -> ( ' a -> ' c ) -> ' a -> ' c val f : [ ` A ] -> unit end } ] |
type ( ' a , ' b ) def = X of int constraint ' b = [ > ` A ]
type arity = ( int , [ ` A ] ) def = X of int ; ; [ %% expect { |
type ( ' a , ' b ) def = X of int constraint ' b = [ > ` A ] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ( int , [ ` A ] ) def They have different arities . } ] |
type ( ' a , ' b ) ct = ( int , ' b ) def = X of int ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ( int , [ > ` A ] ) def Their constraints differ . } ] |
type ( ' a , ' b ) kind = ( ' a , ' b ) def = { a : int } constraint ' b = [ > ` A ] ; ; [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ( ' a , [ > ` A ] ) def Their kinds differ . } ] |
type d = X of int | Y of int
type missing = d = X of int [ %% expect { |
type d = X of int | Y of int ^^^^^^^^^^^^^^^^^^^^^^^^^^^ The constructor Y is only present in the original definition . } ] |
type wrong_type = d = X of float [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Constructors do not match : X of int is not compatible with : X of float The types are not equal . } ] |
type unboxed = d = X of float [ @@ unboxed ] [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Their internal representations differ : this definition uses unboxed representation . } ] |
type perm = d = Y of int | X of int [ %% expect { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Constructors number 1 have different names , X and Y . } ] |
module M : sig type t = Foo of int type t = Foo : int -> t end ; ; [ %% expect { | Modules do not match : sig type t = Foo : int -> t end is not included in sig type t = Foo of int end Type declarations do not match : type t = Foo : int -> t is not included in type t = Foo of int Constructors do not match : Foo : int -> t is not compatible with : Foo of int The first has explicit return type and the second doesn ' t . } ] |
type ' a u = [ < ` a of int | ` b of bool & int & string > ` a ] as ' a
type ' a u1 = [ > ` a of int ] as ' a
type u2 = [ ` a of int ]
type ' a u3 = [ < ` b | ` a of & int & bool ] as ' a
type ' a u4 = [ < ` b | ` a of int & bool ] as ' a
module M1 : sig type t = | Foo of int * int type t = | Foo of float * int end ; ; [ %% expect { | Modules do not match : sig type t = Foo of float * int end is not included in sig type t = Foo of int * int end Type declarations do not match : type t = Foo of float * int is not included in type t = Foo of int * int Constructors do not match : Foo of float * int is not compatible with : Foo of int * int The types are not equal . } ] ; ; |
module M2 : sig type t = | Foo of int * int type t = | Foo of float end ; ; [ %% expect { | Modules do not match : sig type t = Foo of float end is not included in sig type t = Foo of int * int end Type declarations do not match : type t = Foo of float is not included in type t = Foo of int * int Constructors do not match : Foo of float is not compatible with : Foo of int * int They have different arities . } ] ; ; |
module M3 : sig type t = | Foo of { x : int ; y : int } type t = | Foo of { x : float ; y : int } end ; ; [ %% expect { | Modules do not match : sig type t = Foo of { x : float ; y : int ; } end is not included in sig type t = Foo of { x : int ; y : int ; } end Type declarations do not match : type t = Foo of { x : float ; y : int ; } is not included in type t = Foo of { x : int ; y : int ; } Constructors do not match : Foo of { x : float ; y : int ; } is not compatible with : Foo of { x : int ; y : int ; } Fields do not match : x : float ; is not compatible with : x : int ; The types are not equal . } ] ; ; |
module M4 : sig type t = | Foo of { x : int ; y : int } type t = | Foo of float end ; ; [ %% expect { | Modules do not match : sig type t = Foo of float end is not included in sig type t = Foo of { x : int ; y : int ; } end Type declarations do not match : type t = Foo of float is not included in type t = Foo of { x : int ; y : int ; } Constructors do not match : Foo of float is not compatible with : Foo of { x : int ; y : int ; } The second uses inline records and the first doesn ' t . } ] ; ; |
module M5 : sig type ' a t = | Foo : int -> int t type ' a t = | Foo of ' a end ; ; [ %% expect { | Modules do not match : sig type ' a t = Foo of ' a end is not included in sig type ' a t = Foo : int -> int t end Type declarations do not match : type ' a t = Foo of ' a is not included in type ' a t = Foo : int -> int t Constructors do not match : Foo of ' a is not compatible with : Foo : int -> int t The second has explicit return type and the first doesn ' t . } ] ; ; |
module M : sig type ( ' a , ' b ) t = A of ' a type ( ' a , ' b ) t = A of ' b end ; ; Modules do not match : sig type ( ' a , ' b ) t = A of ' b end is not included in sig type ( ' a , ' b ) t = A of ' a end Type declarations do not match : type ( ' a , ' b ) t = A of ' b is not included in type ( ' a , ' b ) t = A of ' a Constructors do not match : A of ' b is not compatible with : A of ' a The types are not equal . } ] ; ; |