text
stringlengths
12
786k
let extract_concrete_variant env ty = match extract_concrete_typedecl env ty with ( p0 , p , { type_kind = Type_variant cstrs } ) -> ( p0 , p , cstrs ) | ( p0 , p , { type_kind = Type_open } ) -> ( p0 , p , [ ] ) | _ -> raise Not_found
let extract_label_names env ty = try let ( _ , _ , fields ) = extract_concrete_record env ty in List . map ( fun l -> l . Types . ld_id ) fields with Not_found -> assert false
let unify_exp_types loc env ty expected_ty = try unify env ty expected_ty with Unify trace -> raise ( Error ( loc , env , Expr_type_clash ( trace , None , None ) ) ) | Tags ( l1 , l2 ) -> raise ( Typetexp . Error ( loc , env , Typetexp . Variant_tags ( l1 , l2 ) ) )
let gadt_equations_level = ref None
let get_gadt_equations_level ( ) = match ! gadt_equations_level with Some y -> y | None -> assert false
let unify_pat_types ( ? refine = false ) loc env ty ty ' = try if refine then unify_gadt ~ equations_level ( : get_gadt_equations_level ( ) ) env ty ty ' else unify ! env ty ty ' with | Unify trace -> raise ( Error ( loc , ! env , Pattern_type_clash ( trace , None ) ) ) | T...
let unify_pat ? refine env pat expected_ty = try unify_pat_types ? refine pat . pat_loc env pat . pat_type expected_ty with Error ( loc , env , Pattern_type_clash ( trace , None ) ) -> raise ( Error ( loc , env , Pattern_type_clash ( trace , Some pat . pat_desc ) ) )
let finalize_variant pat = match pat . pat_desc with Tpat_variant ( tag , opat , r ) -> let row = match expand_head pat . pat_env pat . pat_type with { desc = Tvariant row } -> r := row ; row_repr row | _ -> assert false in begin match row_field tag row with | Rabsent -> ( ) | Reithe...
let has_variants p = exists_pattern ( function { pat_desc = Tpat_variant _ } -> true | _ -> false ) p
type pattern_variable = { pv_id : Ident . t ; pv_type : type_expr ; pv_loc : Location . t ; pv_as_var : bool ; pv_attributes : attributes ; }
type module_variable = string loc * Location . t
let pattern_variables = ref ( [ ] : pattern_variable list )
let pattern_force = ref ( [ ] : ( unit -> unit ) list )
let pattern_scope = ref ( None : Annot . ident option ) ; ;
let allow_modules = ref false
let module_variables = ref ( [ ] : module_variable list )
let reset_pattern scope allow = pattern_variables := [ ] ; pattern_force := [ ] ; pattern_scope := scope ; allow_modules := allow ; module_variables := [ ] ; ; ;
let maybe_add_pattern_variables_ghost loc_let env pv = List . fold_right ( fun { pv_id ; _ } env -> let name = Ident . name pv_id in if Env . bound_value name env then env else begin Env . enter_unbound_value name ( Val_unbound_ghost_recursive loc_let ) env end ) pv env
let enter_variable ( ? is_module = false ) ( ? is_as_variable = false ) loc name ty attrs = if List . exists ( fun { pv_id ; _ } -> Ident . name pv_id = name . txt ) ! pattern_variables then raise ( Error ( loc , Env . empty , Multiply_bound_variable name . txt ) ) ; l...
let sort_pattern_variables vs = List . sort ( fun { pv_id = x ; _ } { pv_id = y ; _ } -> Stdlib . compare ( Ident . name x ) ( Ident . name y ) ) vs
let enter_orpat_variables loc env p1_vs p2_vs = let p1_vs = sort_pattern_variables p1_vs and p2_vs = sort_pattern_variables p2_vs in let rec unify_vars p1_vs p2_vs = let vars vs = List . map ( fun { pv_id ; _ } -> pv_id ) vs in match p1_vs , p2_vs with | { pv_id = x1 ; pv_type = t1 ; _ } ...
let rec build_as_type env p = match p . pat_desc with Tpat_alias ( p1 , _ , _ ) -> build_as_type env p1 | Tpat_tuple pl -> let tyl = List . map ( build_as_type env ) pl in newty ( Ttuple tyl ) | Tpat_construct ( _ , cstr , pl ) -> let keep = cstr . cstr_private = Private || cs...
let build_or_pat env loc lid = let path , decl = Env . lookup_type ~ loc : lid . loc lid . txt env in let tyl = List . map ( fun _ -> newvar ( ) ) decl . type_params in let row0 = let ty = expand_head env ( newty ( Tconstr ( path , tyl , ref Mnil ) ) ) in match ty . desc w...
let split_cases env cases = let add_case lst case = function | None -> lst | Some c_lhs -> { case with c_lhs } :: lst in List . fold_right ( fun ( { c_lhs ; c_guard } as case ) ( vals , exns ) -> match split_pattern c_lhs with | Some _ , Some _ when c_guard <> None -> raise ( Er...
let rec expand_path env p = let decl = try Some ( Env . find_type p env ) with Not_found -> None in match decl with Some { type_manifest = Some ty } -> begin match repr ty with { desc = Tconstr ( p , _ , _ ) } -> expand_path env p | _ -> assert false end | _ -> let p ' = Env . ...
let compare_type_path env tpath1 tpath2 = Path . same ( expand_path env tpath1 ) ( expand_path env tpath2 )
let label_of_kind kind = if kind = " record " then " field " else " constructor "
module NameChoice ( Name : sig type t type usage val type_kind : string val get_name : t -> string val get_type : t -> type_expr val lookup_all_from_type : Location . t -> usage -> Path . t -> Env . t -> ( t * ( unit -> unit ) ) list val in_env : t -> bool open Name let get_type_path d =...
let wrap_disambiguate kind ty f x = try f x with Error ( loc , env , Wrong_name ( " " , _ , tk , tp , name , valid_names ) ) -> raise ( Error ( loc , env , Wrong_name ( kind , ty , tk , tp , name , valid_names ) ) )
module Label = NameChoice ( struct type t = label_description type usage = unit let type_kind = " record " let get_name lbl = lbl . lbl_name let get_type lbl = lbl . lbl_res let lookup_all_from_type loc ( ) path env = Env . lookup_all_labels_from_type ~ loc path env let in_env lbl = match lbl ....
let disambiguate_label_by_ids keep closed ids labels = let check_ids ( lbl , _ ) = let lbls = Hashtbl . create 8 in Array . iter ( fun lbl -> Hashtbl . add lbls lbl . lbl_name ( ) ) lbl . lbl_all ; List . for_all ( Hashtbl . mem lbls ) ids and check_closed ( lbl , _ ) = (...
let disambiguate_lid_a_list loc closed env opath lid_a_list = let ids = List . map ( fun ( lid , _ ) -> Longident . last lid . txt ) lid_a_list in let w_pr = ref false and w_amb = ref [ ] and w_scope = ref [ ] and w_scope_ty = ref " " in let warn loc msg = let open Warnings in match...
let rec find_record_qual = function | [ ] -> None | ( { txt = Longident . Ldot ( modname , _ ) } , _ ) :: _ -> Some modname | _ :: rest -> find_record_qual rest
let map_fold_cont f xs k = List . fold_right ( fun x k ys -> f x ( fun y -> k ( y :: ys ) ) ) xs ( fun ys -> k ( List . rev ys ) ) [ ]
let type_label_a_list ? labels loc closed env type_lbl_a opath lid_a_list k = let lbl_a_list = match lid_a_list , labels with ( { txt = Longident . Lident s } , _ ) :: _ , Some labels when Hashtbl . mem labels s -> List . map ( function lid , a -> match lid . txt with Longident . Li...
let check_recordpat_labels loc lbl_pat_list closed = match lbl_pat_list with | [ ] -> ( ) | ( _ , label1 , _ ) :: _ -> let all = label1 . lbl_all in let defined = Array . make ( Array . length all ) false in let check_defined ( _ , label , _ ) = if defined . ( label ....
module Constructor = NameChoice ( struct type t = constructor_description type usage = Env . constructor_usage let type_kind = " variant " let get_name cstr = cstr . cstr_name let get_type cstr = cstr . cstr_res let lookup_all_from_type loc usage path env = Env . lookup_all_constructors_from_type ...
let unify_head_only ~ refine loc env ty constr = let ( _ , ty_res ) = instance_constructor constr in let ty_res = repr ty_res in match ty_res . desc with | Tconstr ( p , args , m ) -> ty_res . desc <- Tconstr ( p , List . map ( fun _ -> newvar ( ) ) args , m ) ; enforce_c...
type half_typed_case = { typed_pat : pattern ; pat_type_for_unif : type_expr ; untyped_case : Parsetree . case ; branch_env : Env . t ; pat_vars : pattern_variable list ; unpacks : module_variable list ; contains_gadt : bool ; }
let rec has_literal_pattern p = match p . ppat_desc with | Ppat_constant _ | Ppat_interval _ -> true | Ppat_any | Ppat_variant ( _ , None ) | Ppat_construct ( _ , None ) | Ppat_type _ | Ppat_var _ | Ppat_unpack _ | Ppat_extension _ -> false | Ppat_exception p | Ppat_variant ( _ , ...
let check_scope_escape loc env level ty = try Ctype . check_scope_escape env level ty with Unify trace -> raise ( Error ( loc , env , Pattern_type_clash ( trace , None ) ) )
type pattern_checking_mode = | Normal | Counter_example of counter_example_checking_info explosion_fuel : int ; splitting_mode : splitting_mode ; constrs : ( string , Types . constructor_description ) Hashtbl . t ; labels : ( string , Types . label_description ) Hashtbl . t ; } | B...
type state = { snapshot : Btype . snapshot ; levels : Ctype . levels ; env : Env . t ; }
let save_state env = { snapshot = Btype . snapshot ( ) ; levels = Ctype . save_levels ( ) ; env = ! env ; }
let set_state s env = Btype . backtrack s . snapshot ; Ctype . set_levels s . levels ; env := s . env
let rec find_valid_alternative f pat = match pat . ppat_desc with | Ppat_or ( p1 , p2 ) -> ( try find_valid_alternative f p1 with Error _ -> find_valid_alternative f p2 ) | _ -> f pat
let no_explosion = function | Normal -> Normal | Counter_example info -> Counter_example { info with explosion_fuel = 0 }
let get_splitting_mode = function | Normal -> None | Counter_example { splitting_mode } -> Some splitting_mode
let enter_nonsplit_or mode = match mode with | Normal -> Normal | Counter_example info -> let splitting_mode = match info . splitting_mode with | Backtrack_or -> assert false | Refine_or _ -> Refine_or { inside_nonsplit_or = true } in Counter_example { info with splitting_mode }
let rec type_pat ( ? exception_allowed = false ) ~ no_existentials ~ mode ~ env sp expected_ty k = Builtin_attributes . warning_scope sp . ppat_attributes ( fun ( ) -> type_pat_aux ~ exception_allowed ~ no_existentials ~ mode ~ env sp expected_ty k ) ~ env sp expected_ty k = let type_pat ( ...
let type_pat ? exception_allowed ? no_existentials ( ? mode = Normal ) ( ? lev = get_current_level ( ) ) env sp expected_ty = Misc . protect_refs [ Misc . R ( gadt_equations_level , Some lev ) ] ( fun ( ) -> let r = type_pat ? exception_allowed ~ no_existentials ~ mode ~ env...
let partial_pred ~ lev ~ splitting_mode ( ? explode = 0 ) env expected_ty constrs labels p = let env = ref env in let state = save_state env in let mode = Counter_example { splitting_mode ; explosion_fuel = explode ; constrs ; labels ; } in try reset_pattern None true ; let typed_p = Ctype ...
let check_partial ( ? lev = get_current_level ( ) ) env expected_ty loc cases = let explode = match cases with [ _ ] -> 5 | _ -> 0 in let splitting_mode = Refine_or { inside_nonsplit_or = false } in Parmatch . check_partial ( partial_pred ~ lev ~ splitting_mode ~ explode env expected...
let check_unused ( ? lev = get_current_level ( ) ) env expected_ty cases = Parmatch . check_unused ( fun refute constrs labels spat -> match partial_pred ~ lev ~ splitting_mode : Backtrack_or ~ explode : 5 env expected_ty constrs labels spat with Some pat when refute -> raise ( Error ( spat ....
let iter_pattern_variables_type f : pattern_variable list -> unit = List . iter ( fun { pv_type ; _ } -> f pv_type )
let add_pattern_variables ? check ? check_as env pv = List . fold_right ( fun { pv_id ; pv_type ; pv_loc ; pv_as_var ; pv_attributes } env -> let check = if pv_as_var then check_as else check in Env . add_value ? check pv_id { val_type = pv_type ; val_kind = Val_reg ; Types . val_loc ...
let type_pattern ? exception_allowed ~ lev env spat scope expected_ty = reset_pattern scope true ; let new_env = ref env in let pat = type_pat ? exception_allowed ~ lev new_env spat expected_ty in let pvs = get_ref pattern_variables in let unpacks = get_ref module_variables in ( pat , ! new_env , get_...
let type_pattern_list no_existentials env spatl scope expected_tys allow = reset_pattern scope allow ; let new_env = ref env in let type_pat ( attrs , pat ) ty = Builtin_attributes . warning_scope ~ ppwarning : false attrs ( fun ( ) -> type_pat ~ no_existentials new_env pat ty ) in let patl = ...
let type_class_arg_pattern cl_num val_env met_env l spat = reset_pattern None false ; let nv = newvar ( ) in let pat = type_pat ~ no_existentials : In_class_args ( ref val_env ) spat nv in if has_variants pat then begin Parmatch . pressure_variants val_env [ pat ] ; iter_pattern finalize_variant ...
let type_self_pattern cl_num privty val_env met_env par_env spat = let open Ast_helper in let spat = Pat . mk ( Ppat_alias ( Pat . mk ( Ppat_alias ( spat , mknoloc " selfpat " ) ) , -* mknoloc ( " selfpat " - ^ cl_num ) ) ) in reset_pattern None false ; let nv = newvar ( ) ...
let delayed_checks = ref [ ]
let reset_delayed_checks ( ) = delayed_checks := [ ]
let add_delayed_check f = delayed_checks := ( f , Warnings . backup ( ) ) :: ! delayed_checks
let force_delayed_checks ( ) = let snap = Btype . snapshot ( ) in let w_old = Warnings . backup ( ) in List . iter ( fun ( f , w ) -> Warnings . restore w ; f ( ) ) ( List . rev ! delayed_checks ) ; Warnings . restore w_old ; reset_delayed_checks ( ) ; Btype . ...
let rec final_subexpression exp = match exp . exp_desc with Texp_let ( _ , _ , e ) | Texp_sequence ( _ , e ) | Texp_try ( e , _ ) | Texp_ifthenelse ( _ , e , _ ) | Texp_match ( _ , { c_rhs = e } :: _ , _ ) | Texp_letmodule ( _ , _ , _ , _ , e ) ...
let rec is_nonexpansive exp = match exp . exp_desc with | Texp_ident _ | Texp_constant _ | Texp_unreachable | Texp_function _ | Texp_array [ ] -> true | Texp_let ( _rec_flag , pat_exp_list , body ) -> List . for_all ( fun vb -> is_nonexpansive vb . vb_expr ) pat_exp_list && is_nonex...
let maybe_expansive e = not ( is_nonexpansive e )
let check_recursive_bindings env valbinds = let ids = let_bound_idents valbinds in List . iter ( fun { vb_expr } -> if not ( Rec_check . is_valid_recursive_expression ids vb_expr ) then raise ( Error ( vb_expr . exp_loc , env , Illegal_letrec_expr ) ) ) valbinds
let check_recursive_class_bindings env ids exprs = List . iter ( fun expr -> if not ( Rec_check . is_valid_class_expr ids expr ) then raise ( Error ( expr . cl_loc , env , Illegal_class_expr ) ) ) exprs
let rec approx_type env sty = match sty . ptyp_desc with Ptyp_arrow ( p , _ , sty ) -> let ty1 = if is_optional p then type_option ( newvar ( ) ) else newvar ( ) in newty ( Tarrow ( p , ty1 , approx_type env sty , Cok ) ) | Ptyp_tuple args -> newty ( Ttuple ( List . map (...
let rec type_approx env sexp = match sexp . pexp_desc with Pexp_let ( _ , _ , e ) -> type_approx env e | Pexp_fun ( p , _ , _ , e ) -> let ty = if is_optional p then type_option ( newvar ( ) ) else newvar ( ) in newty ( Tarrow ( p , ty , type_approx env e , Cok ) ) ...
let rec list_labels_aux env visited ls ty_fun = let ty = expand_head env ty_fun in if List . memq ty visited then List . rev ls , false else match ty . desc with Tarrow ( l , _ , ty_res , _ ) -> list_labels_aux env ( ty :: visited ) ( l :: ls ) ty_res | _ -> List . rev ls , is_Tv...
let list_labels env ty = wrap_trace_gadt_instances env ( list_labels_aux env [ ] [ ] ) ty
let check_univars env expans kind exp ty_expected vars = if expans && maybe_expansive exp then lower_contravariant env exp . exp_type ; let vars = List . map ( expand_head env ) vars in let vars = List . map ( expand_head env ) vars in let vars ' = List . filter ( fun t -> let t = repr t in...
let check_partial_application statement exp = let rec f delay = let ty = ( expand_head exp . exp_env exp . exp_type ) . desc in let check_statement ( ) = match ty with | Tconstr ( p , _ , _ ) when Path . same p Predef . path_unit -> ( ) | _ -> if statement then let rec loop { ...
let generalizable level ty = let rec check ty = let ty = repr ty in if ty . level < lowest_level then ( ) else if ty . level <= level then raise Exit else ( mark_type_node ty ; iter_type_expr check ty ) in try check ty ; unmark_type ty ; true with Exit -> unmark_type ty ; false
let self_coercion = ref ( [ ] : ( Path . t * Location . t list ref ) list )
let create_package_type loc env ( p , l ) = let s = ! Typetexp . transl_modtype_longident loc env p in let fields = List . map ( fun ( name , ct ) -> name , Typetexp . transl_simple_type env false ct ) l in let ty = newty ( Tpackage ( s , List . map fst l , List . map ( fun (...
let contains_variant_either ty = let rec loop ty = let ty = repr ty in if ty . level >= lowest_level then begin mark_type_node ty ; match ty . desc with Tvariant row -> let row = row_repr row in if not ( is_fixed row ) then List . iter ( fun ( _ , f ) -> match row_field_repr f with Reither _...
let shallow_iter_ppat f p = match p . ppat_desc with | Ppat_any | Ppat_var _ | Ppat_constant _ | Ppat_interval _ | Ppat_extension _ | Ppat_type _ | Ppat_unpack _ -> ( ) | Ppat_array pats -> List . iter f pats | Ppat_or ( p1 , p2 ) -> f p1 ; f p2 | Ppat_variant ( _ , arg ) | P...
let exists_ppat f p = let exception Found in let rec loop p = if f p then raise Found else ( ) ; shallow_iter_ppat loop p in match loop p with | exception Found -> true | ( ) -> false
let contains_polymorphic_variant p = exists_ppat ( function | { ppat_desc = ( Ppat_variant _ | Ppat_type _ ) } -> true | _ -> false ) p
let contains_gadt cp = exists_pattern ( function | { pat_desc = Tpat_construct ( _ , cd , _ ) } when cd . cstr_generalized -> true | _ -> false ) cp
let may_contain_gadts p = exists_ppat ( function | { ppat_desc = Ppat_construct ( _ , _ ) } -> true | _ -> false ) p
let check_absent_variant env = iter_pattern ( function { pat_desc = Tpat_variant ( s , arg , row ) } as pat -> let row = row_repr ! row in if List . exists ( fun ( s ' , fi ) -> s = s ' && row_field_repr fi <> Rabsent ) row . row_fields || not ( is_fixed row ) && not ( s...
let proper_exp_loc exp = let rec aux = function | [ ] -> exp . exp_loc | ( ( Texp_constraint _ | Texp_coerce _ ) , loc , _ ) :: _ -> loc | _ :: rest -> aux rest in aux exp . exp_extra
let rec name_pattern default = function [ ] -> Ident . create_local default | p :: rem -> match p . pat_desc with Tpat_var ( id , _ ) -> id | Tpat_alias ( _ , id , _ ) -> id | _ -> name_pattern default rem
let name_cases default lst = name_pattern default ( List . map ( fun c -> c . c_lhs ) lst )
let unify_exp env exp expected_ty = let loc = proper_exp_loc exp in try unify_exp_types loc env exp . exp_type expected_ty with Error ( loc , env , Expr_type_clash ( trace , tfc , None ) ) -> raise ( Error ( loc , env , Expr_type_clash ( trace , tfc , Some exp . exp_desc ) ) )
let rec type_exp ? recarg env sexp = type_expect ? recarg env sexp ( mk_expected ( newvar ( ) ) ) let previous_saved_types = Cmt_format . get_saved_types ( ) in let exp = Builtin_attributes . warning_scope sexp . pexp_attributes ( fun ( ) -> type_expect_ ? in_function ? recarg env se...
let type_binding env rec_flag spat_sexp_list scope = Typetexp . reset_type_variables ( ) ; let ( pat_exp_list , new_env , _unpacks ) = type_let ~ check ( : fun s -> Warnings . Unused_value_declaration s ) ~ check_strict ( : fun s -> Warnings . Unused_value_declaration s ) At_topleve...
let type_let existential_ctx env rec_flag spat_sexp_list scope = let ( pat_exp_list , new_env , _unpacks ) = type_let existential_ctx env rec_flag spat_sexp_list scope false in ( pat_exp_list , new_env )
let type_expression env sexp = Typetexp . reset_type_variables ( ) ; begin_def ( ) ; let exp = type_exp env sexp in end_def ( ) ; if maybe_expansive exp then lower_contravariant env exp . exp_type ; generalize exp . exp_type ; match sexp . pexp_desc with Pexp_ident lid -> let loc = sex...
let spellcheck ppf unbound_name valid_names = Misc . did_you_mean ppf ( fun ( ) -> Misc . spellcheck valid_names unbound_name )
let spellcheck_idents ppf unbound valid_idents = spellcheck ppf ( Ident . name unbound ) ( List . map Ident . name valid_idents )
let type_clash_of_trace trace = Ctype . Unification_trace . ( explain trace ( fun ~ prev : _ -> function | Diff diff -> Some diff | _ -> None ) )
let report_literal_type_constraint expected_type const = let const_str = match const with | Const_int n -> Some ( Int . to_string n ) | Const_int32 n -> Some ( Int32 . to_string n ) | Const_int64 n -> Some ( Int64 . to_string n ) | Const_nativeint n -> Some ( Nativeint . to_string n ) ...
let report_literal_type_constraint const = function | Some Unification_trace . { expected = { t = { desc = Tconstr ( typ , [ ] , _ ) } } } -> report_literal_type_constraint typ const | Some _ | None -> [ ]
let report_expr_type_clash_hints exp diff = match exp with | Some ( Texp_constant const ) -> report_literal_type_constraint const diff | _ -> [ ]
let report_pattern_type_clash_hints pat diff = match pat with | Some ( Tpat_constant const ) -> report_literal_type_constraint const diff | _ -> [ ]