text
stringlengths
0
601k
let mark_loops_constructor_arguments = function | Cstr_tuple l -> List . iter mark_loops l | Cstr_record l -> List . iter ( fun l -> mark_loops l . ld_type ) l
let rec tree_of_type_decl id decl = reset_except_context ( ) ; let params = filter_params decl . type_params in begin match decl . type_manifest with | Some ty -> let vars = free_variables ty in List . iter ( function { desc = Tvar ( Some " _ " ) } as ty -> if List . memq ty vars then ty . desc <- Tvar None | _ -> ( ) ...
let constructor ppf c = reset_except_context ( ) ; ! Oprint . out_constr ppf ( tree_of_constructor c )
let label ppf l = reset_except_context ( ) ; ! Oprint . out_label ppf ( tree_of_label l )
let tree_of_type_declaration id decl rs = Osig_type ( tree_of_type_decl id decl , tree_of_rec rs )
let type_declaration id ppf decl = ! Oprint . out_sig_item ppf ( tree_of_type_declaration id decl Trec_first )
let constructor_arguments ppf a = let tys = tree_of_constructor_arguments a in ! Oprint . out_type ppf ( Otyp_tuple tys )
let extension_constructor_args_and_ret_type_subtree ext_args ext_ret_type = match ext_ret_type with | None -> ( tree_of_constructor_arguments ext_args , None ) | Some res -> let nm = ! names in names := [ ] ; let ret = tree_of_typexp false res in let args = tree_of_constructor_arguments ext_args in names := nm ; ( args...
let tree_of_extension_constructor id ext es = reset_except_context ( ) ; let ty_name = Path . name ext . ext_type_path in let ty_params = filter_params ext . ext_type_params in List . iter add_alias ty_params ; List . iter mark_loops ty_params ; List . iter check_name_of_type ( List . map proxy ty_params ) ; mark_loops...
let extension_constructor id ppf ext = ! Oprint . out_sig_item ppf ( tree_of_extension_constructor id ext Text_first )
let extension_only_constructor id ppf ext = reset_except_context ( ) ; let name = Ident . name id in let args , ret = extension_constructor_args_and_ret_type_subtree ext . ext_args ext . ext_ret_type in Format . fprintf ppf " [ @< hv >% a ] " @ ! Oprint . out_constr ( name , args , ret )
let tree_of_value_description id decl = let id = Ident . name id in let ty = tree_of_type_scheme decl . val_type in let vd = { oval_name = id ; oval_type = ty ; oval_prims = [ ] ; oval_attributes = [ ] } in let vd = match decl . val_kind with | Val_prim p -> Primitive . print p vd | _ -> vd in Osig_value vd
let value_description id ppf decl = ! Oprint . out_sig_item ppf ( tree_of_value_description id decl )
let method_type ( _ , kind , ty ) = match field_kind_repr kind , repr ty with Fpresent , { desc = Tpoly ( ty , tyl ) } -> ( ty , tyl ) | _ , ty -> ( ty , [ ] )
let tree_of_metho sch concrete csil ( lab , kind , ty ) = if lab <> dummy_method then begin let kind = field_kind_repr kind in let priv = kind <> Fpresent in let virt = not ( Concr . mem lab concrete ) in let ( ty , tyl ) = method_type ( lab , kind , ty ) in let tty = tree_of_typexp sch ty in remove_names tyl ; Ocsg_me...
let rec prepare_class_type params = function | Cty_constr ( _p , tyl , cty ) -> let sty = Ctype . self_type cty in if List . memq ( proxy sty ) ! visited_objects || not ( List . for_all is_Tvar params ) || List . exists ( deep_occur sty ) tyl then prepare_class_type params cty else List . iter mark_loops tyl | Cty_sign...
let rec tree_of_class_type sch params = function | Cty_constr ( p ' , tyl , cty ) -> let sty = Ctype . self_type cty in if List . memq ( proxy sty ) ! visited_objects || not ( List . for_all is_Tvar params ) then tree_of_class_type sch params cty else let namespace = Namespace . best_class_namespace p ' in Octy_constr ...
let class_type ppf cty = reset ( ) ; prepare_class_type [ ] cty ; ! Oprint . out_class_type ppf ( tree_of_class_type false [ ] cty )
let tree_of_class_param param variance = ( match tree_of_typexp true param with Otyp_var ( _ , s ) -> s | _ -> " " ) , ? if is_Tvar ( repr param ) then ( true , true ) else variance
let class_variance = List . map Variance . ( fun v -> mem May_pos v , mem May_neg v )
let tree_of_class_declaration id cl rs = let params = filter_params cl . cty_params in reset_except_context ( ) ; List . iter add_alias params ; prepare_class_type params cl . cty_type ; let sty = Ctype . self_type cl . cty_type in List . iter mark_loops params ; List . iter check_name_of_type ( List . map proxy params...
let class_declaration id ppf cl = ! Oprint . out_sig_item ppf ( tree_of_class_declaration id cl Trec_first )
let tree_of_cltype_declaration id cl rs = let params = List . map repr cl . clty_params in reset_except_context ( ) ; List . iter add_alias params ; prepare_class_type params cl . clty_type ; let sty = Ctype . self_type cl . clty_type in List . iter mark_loops params ; List . iter check_name_of_type ( List . map proxy ...
let cltype_declaration id ppf cl = ! Oprint . out_sig_item ppf ( tree_of_cltype_declaration id cl Trec_first )
let wrap_env fenv ftree arg = let env = ! printing_env in set_printing_env ( fenv env ) ; let tree = ftree arg in set_printing_env env ; tree
let filter_rem_sig item rem = match item , rem with | Sig_class _ , ctydecl :: tydecl1 :: tydecl2 :: rem -> ( [ ctydecl ; tydecl1 ; tydecl2 ] , rem ) | Sig_class_type _ , tydecl1 :: tydecl2 :: rem -> ( [ tydecl1 ; tydecl2 ] , rem ) | _ -> ( [ ] , rem )
let dummy = { type_params = [ ] ; type_arity = 0 ; type_kind = Type_abstract ; type_private = Public ; type_manifest = None ; type_variance = [ ] ; type_is_newtype = false ; type_expansion_scope = Btype . lowest_level ; type_loc = Location . none ; type_attributes = [ ] ; type_immediate = Unknown ; type_unboxed = unbox...
let hide ids env = List . fold_right ( fun id -> Env . add_type ~ check : false ( Ident . rename id ) dummy ) ids env
let hide_rec_items = function | Sig_type ( id , _decl , rs , _ ) :: rem when rs = Trec_first && not ! Clflags . real_paths -> let rec get_ids = function Sig_type ( id , _ , Trec_next , _ ) :: rem -> id :: get_ids rem | _ -> [ ] in let ids = id :: get_ids rem in set_printing_env ( hide ids ! printing_env ) | _ -> ( )
let recursive_sigitem = function | Sig_class ( id , _ , rs , _ ) -> Some ( id , rs , 3 ) | Sig_class_type ( id , _ , rs , _ ) -> Some ( id , rs , 2 ) | Sig_type ( id , _ , rs , _ ) | Sig_module ( id , _ , _ , rs , _ ) -> Some ( id , rs , 0 ) | _ -> None
let skip k l = snd ( Misc . Stdlib . List . split_at k l )
let protect_rec_items items = let rec get_ids recs = function | [ ] -> [ ] | item :: rem -> match recursive_sigitem item with | Some ( id , r , k ) when r = recs -> id :: get_ids Trec_next ( skip k rem ) | _ -> [ ] in List . iter Naming_context . add_protected ( get_ids Trec_first items )
let stop_type_group env = Naming_context . reset_protected ( ) ; set_printing_env env
let still_in_type_group env ' in_type_group item = match in_type_group , recursive_sigitem item with | true , Some ( _ , Trec_next , _ ) -> true | _ , Some ( _ , ( Trec_not | Trec_first ) , _ ) -> stop_type_group env ' ; true | _ -> stop_type_group env ' ; false
let rec tree_of_modtype ( ? ellipsis = false ) = function | Mty_ident p -> Omty_ident ( tree_of_path Module_type p ) | Mty_signature sg -> Omty_signature ( if ellipsis then [ Osig_ellipsis ] else tree_of_signature sg ) | Mty_functor ( param , ty_res ) -> let param , res = match param with | Unit -> None , tree_of_modty...
let modtype ppf mty = ! Oprint . out_module_type ppf ( tree_of_modtype mty )
let modtype_declaration id ppf decl = ! Oprint . out_sig_item ppf ( tree_of_modtype_declaration id decl )
let refresh_weak ( ) = let refresh t name ( m , s ) = if is_non_gen true ( repr t ) then begin TypeMap . add t name m , String . Set . add name s end else m , s in let m , s = TypeMap . fold refresh ! weak_var_map ( TypeMap . empty , String . Set . empty ) in named_weak_vars := s ; weak_var_map := m
let print_items showval env x = refresh_weak ( ) ; reset_naming_context ( ) ; Conflicts . reset ( ) ; let rec print showval in_type_group env = function | [ ] -> stop_type_group env ; [ ] | item :: rem as items -> let in_type_group = still_in_type_group env in_type_group item in let ( sg , rem ) = filter_rem_sig item r...
let print_signature ppf tree = fprintf ppf " [ @< v >% a ] " @ ! Oprint . out_signature tree
let signature ppf sg = fprintf ppf " % a " print_signature ( tree_of_signature sg )
let printed_signature sourcefile ppf sg = Conflicts . reset ( ) ; reset_naming_context ( ) ; let t = tree_of_signature sg in if Warnings . ( is_active @@ Erroneous_printed_signature " " ) && Conflicts . exists ( ) then begin let conflicts = Format . asprintf " % t " Conflicts . print_explanations in Location . prerr_wa...
let same_path t t ' = let t = repr t and t ' = repr t ' in t == t ' || match t . desc , t ' . desc with Tconstr ( p , tl , _ ) , Tconstr ( p ' , tl ' , _ ) -> let ( p1 , s1 ) = best_type_path p and ( p2 , s2 ) = best_type_path p ' in begin match s1 , s2 with Nth n1 , Nth n2 when n1 = n2 -> true | ( Id | Map _ ) , ( Id ...
type ' a diff = Same of ' a | Diff of ' a * ' a
let trees_of_type_expansion ( t , t ' ) = if same_path t t ' then begin add_delayed ( proxy t ) ; Same ( tree_of_typexp false t ) end else let t ' = if proxy t == proxy t ' then unalias t ' else t ' in let first = tree_of_typexp false t in let second = tree_of_typexp false t ' in if first = second then Same first else ...
let type_expansion ppf = function | Same t -> ! Oprint . out_type ppf t | Diff ( t , t ' ) -> fprintf ppf " [ @< 2 >% a @ =@ % a ] " @ ! Oprint . out_type t ! Oprint . out_type t '
let trees_of_trace = List . map ( Trace . map_diff trees_of_type_expansion )
let trees_of_type_path_expansion ( tp , tp ' ) = if Path . same tp tp ' then Same ( tree_of_path Type tp ) else Diff ( tree_of_path Type tp , tree_of_path Type tp ' )
let type_path_expansion ppf = function | Same p -> ! Oprint . out_ident ppf p | Diff ( p , p ' ) -> fprintf ppf " [ @< 2 >% a @ =@ % a ] " @ ! Oprint . out_ident p ! Oprint . out_ident p '
let rec trace fst txt ppf = function | { Trace . got ; expected } :: rem -> if not fst then fprintf ppf " , " ; @ fprintf ppf " [ @ Type ; @< 1 2 >% a @ % s ; @< 1 2 >% a ] @ % a " type_expansion got txt type_expansion expected ( trace false txt ) rem | _ -> ( )
type printing_status = | Discard | Keep | Optional_refinement
let printing_status = function | Trace . ( Diff { got = t1 , t1 ' ; expected = t2 , t2 ' } ) -> if is_constr_row ~ allow_ident : true t1 ' || is_constr_row ~ allow_ident : true t2 ' then Discard else if same_path t1 t1 ' && same_path t2 t2 ' then Optional_refinement else Keep | _ -> Keep
let prepare_trace f tr = let clean_trace x l = match printing_status x with | Keep -> x :: l | Optional_refinement when l = [ ] -> [ x ] | Optional_refinement | Discard -> l in match Trace . flatten f tr with | [ ] -> [ ] | elt :: rem -> elt :: List . fold_right clean_trace rem [ ]
let rec filter_trace keep_last = function | [ ] -> [ ] | [ Trace . Diff d as elt ] when printing_status elt = Optional_refinement -> if keep_last then [ d ] else [ ] | Trace . Diff d :: rem -> d :: filter_trace keep_last rem | _ :: rem -> filter_trace keep_last rem
let type_path_list = Format . pp_print_list ~ pp_sep ( : fun ppf ( ) -> Format . pp_print_break ppf 2 0 ) type_path_expansion
let hide_variant_name t = match repr t with | { desc = Tvariant row } as t when ( row_repr row ) . row_name <> None -> newty2 t . level ( Tvariant { ( row_repr row ) with row_name = None ; row_more = newvar2 ( row_more row ) . level } ) | _ -> t
let prepare_expansion ( t , t ' ) = let t ' = hide_variant_name t ' in mark_loops t ; if not ( same_path t t ' ) then mark_loops t ' ; ( t , t ' )
let may_prepare_expansion compact ( t , t ' ) = match ( repr t ' ) . desc with Tvariant _ | Tobject _ when compact -> mark_loops t ; ( t , t ) | _ -> prepare_expansion ( t , t ' )
let print_tag ppf = fprintf ppf " ` % s "
let print_tags = let comma ppf ( ) = Format . fprintf ppf " , @ " in Format . pp_print_list ~ pp_sep : comma print_tag
let is_unit env ty = match ( Ctype . expand_head env ty ) . desc with | Tconstr ( p , _ , _ ) -> Path . same p Predef . path_unit | _ -> false
let unifiable env ty1 ty2 = let snap = Btype . snapshot ( ) in let res = try Ctype . unify env ty1 ty2 ; true with Unify _ -> false in Btype . backtrack snap ; res
let explanation_diff env t3 t4 : ( Format . formatter -> unit ) option = match t3 . desc , t4 . desc with | Tarrow ( _ , ty1 , ty2 , _ ) , _ when is_unit env ty1 && unifiable env ty2 t4 -> Some ( fun ppf -> fprintf ppf " , [ @@ Hint : Did you forget to provide ` ( ) ' as argument ] " ) ?@ | _ , Tarrow ( _ , ty1 , ty2 ,...
let print_pos ppf = function | Trace . First -> fprintf ppf " first " | Trace . Second -> fprintf ppf " second "
let explain_fixed_row_case ppf = function | Trace . Cannot_be_closed -> Format . fprintf ppf " it cannot be closed " | Trace . Cannot_add_tags tags -> Format . fprintf ppf " it may not allow the tag ( s ) % a " print_tags tags
let explain_fixed_row pos expl = match expl with | Types . Fixed_private -> dprintf " The % a variant type is private " print_pos pos | Types . Univar x -> dprintf " The % a variant type is bound to the universal type variable % a " print_pos pos type_expr x | Types . Reified p -> let p = tree_of_path Type p in dprintf...
let explain_variant = function | Trace . No_intersection -> Some ( dprintf " , @ These two variant types have no intersection " ) | Trace . No_tags ( pos , fields ) -> Some ( dprintf " , [ @@ The % a variant type does not allow tag ( s ) @ [ @< hov >% a ] ] " @@ print_pos pos print_tags ( List . map fst fields ) ) | Tr...
let explain_escape intro prev ctx e = let pre = match ctx with | Some ctx -> dprintf " [ @% t ; @< 1 2 >% a ] " @ intro type_expr ctx | None -> match e , prev with | Trace . Univ _ , Some ( Trace . Incompatible_fields { name ; diff } ) -> dprintf " , [ @@ The method % s has type @ % a , @ \ but the expected method type...
let explain_object = function | Trace . Self_cannot_be_closed -> Some ( dprintf " , @ Self type cannot be unified with a closed object type " ) | Trace . Missing_field ( pos , f ) -> Some ( dprintf " , [ @@ The % a object type has no method % s ] " @ print_pos pos f ) | Trace . Abstract_row pos -> Some ( dprintf " , [ ...
let explanation intro prev env = function | Trace . Diff { Trace . got = _ , s ; expected = _ , t } -> explanation_diff env s t | Trace . Escape { kind ; context } -> explain_escape intro prev context kind | Trace . Incompatible_fields { name ; _ } -> Some ( dprintf " , @ Types for method % s are incompatible " name ) ...
let mismatch intro env trace = Trace . explain trace ( fun ~ prev h -> explanation intro prev env h )
let explain mis ppf = match mis with | None -> ( ) | Some explain -> explain ppf
let warn_on_missing_def env ppf t = match t . desc with | Tconstr ( p , _ , _ ) -> begin try ignore ( Env . find_type p env : Types . type_declaration ) with Not_found -> fprintf ppf " , [ @@% a is abstract because no corresponding cmi file was found \ in path . ] " @ path p end | _ -> ( )
let prepare_expansion_head empty_tr = function | Trace . Diff d -> Some ( Trace . map_diff ( may_prepare_expansion empty_tr ) d ) | _ -> None
let head_error_printer txt_got txt_but = function | None -> ignore | Some d -> let d = Trace . map_diff trees_of_type_expansion d in dprintf " % t ; @< 1 2 >% a @ % t ; @< 1 2 >% a " txt_got type_expansion d . Trace . got txt_but type_expansion d . Trace . expected
let warn_on_missing_defs env ppf = function | None -> ( ) | Some { Trace . got = te1 , _ ; expected = te2 , _ } -> warn_on_missing_def env ppf te1 ; warn_on_missing_def env ppf te2
let unification_error env tr txt1 ppf txt2 ty_expect_explanation = reset ( ) ; let tr = prepare_trace ( fun t t ' -> t , hide_variant_name t ' ) tr in let mis = mismatch txt1 env tr in match tr with | [ ] -> assert false | elt :: tr -> try print_labels := not ! Clflags . classic ; let tr = filter_trace ( mis = None ) t...
let report_unification_error ppf env tr ( ? type_expected_explanation = fun _ -> ( ) ) txt1 txt2 = wrap_printing_env env ( fun ( ) -> unification_error env tr txt1 ppf txt2 type_expected_explanation ) ~ error : true ; ;
let trace fst keep_last txt ppf tr = print_labels := not ! Clflags . classic ; try match tr with | elt :: tr ' -> let elt = match elt with | Trace . Diff diff -> [ Trace . map_diff trees_of_type_expansion diff ] | _ -> [ ] in let tr = trees_of_trace @@ List . map ( Trace . map_diff prepare_expansion ) @@ filter_trace k...
let report_subtyping_error ppf env tr1 txt1 tr2 = wrap_printing_env ~ error : true env ( fun ( ) -> reset ( ) ; let tr1 = prepare_trace ( fun t t ' -> prepare_expansion ( t , t ' ) ) tr1 in let tr2 = prepare_trace ( fun t t ' -> prepare_expansion ( t , t ' ) ) tr2 in let keep_first = match tr2 with | Trace . [ Obj _ | ...
let report_ambiguous_type_error ppf env tp0 tpl txt1 txt2 txt3 = wrap_printing_env ~ error : true env ( fun ( ) -> reset ( ) ; let tp0 = trees_of_type_path_expansion tp0 in match tpl with [ ] -> assert false | [ tp ] -> fprintf ppf " [ @% t ; @< 1 2 >% a @ \ % t ; @< 1 2 >% a \ ] " @ txt1 type_path_expansion ( trees_of...
let tree_of_path = tree_of_path Other
let tree_of_modtype = tree_of_modtype ~ ellipsis : false
let type_expansion ty ppf ty ' = type_expansion ppf ( trees_of_type_expansion ( ty , ty ' ) )
let tree_of_type_declaration id td rs = Naming_context . with_hidden id ( wrap_env ( hide [ id ] ) ( fun ( ) -> tree_of_type_declaration id td rs ) )
let fmt_position f l = if l . pos_lnum = - 1 then fprintf f " % s [ % d ] " l . pos_fname l . pos_cnum else fprintf f " % s [ % d , % d +% d ] " l . pos_fname l . pos_lnum l . pos_bol ( l . pos_cnum - l . pos_bol ) ; ;
let fmt_location f loc = fprintf f " ( % a . . % a ) " fmt_position loc . loc_start fmt_position loc . loc_end ; if loc . loc_ghost then fprintf f " ghost " ; ; ;
let rec fmt_longident_aux f x = match x with | Longident . Lident ( s ) -> fprintf f " % s " s ; | Longident . Ldot ( y , s ) -> fprintf f " % a . % s " fmt_longident_aux y s ; | Longident . Lapply ( y , z ) -> fprintf f " % a ( % a ) " fmt_longident_aux y fmt_longident_aux z ; ; ;
let fmt_longident f x = fprintf f " " \% a " " \ fmt_longident_aux x . txt ; ;
let fmt_modname f = function | None -> fprintf f " _ " ; | Some id -> Ident . print f id
let rec fmt_path_aux f x = match x with | Path . Pident ( s ) -> fprintf f " % a " fmt_ident s ; | Path . Pdot ( y , s ) -> fprintf f " % a . % s " fmt_path_aux y s ; | Path . Papply ( y , z ) -> fprintf f " % a ( % a ) " fmt_path_aux y fmt_path_aux z ; ; ;
let fmt_path f x = fprintf f " " \% a " " \ fmt_path_aux x ; ;
let fmt_constant f x = match x with | Const_int ( i ) -> fprintf f " Const_int % d " i ; | Const_char ( c ) -> fprintf f " Const_char % 02x " ( Char . code c ) ; | Const_string ( s , None ) -> fprintf f " Const_string ( % S , None ) " s ; | Const_string ( s , Some delim ) -> fprintf f " Const_string ( % S , Some % S ) ...
let fmt_mutable_flag f x = match x with | Immutable -> fprintf f " Immutable " ; | Mutable -> fprintf f " Mutable " ; ; ;
let fmt_virtual_flag f x = match x with | Virtual -> fprintf f " Virtual " ; | Concrete -> fprintf f " Concrete " ; ; ;
let fmt_override_flag f x = match x with | Override -> fprintf f " Override " ; | Fresh -> fprintf f " Fresh " ; ; ;
let fmt_closed_flag f x = match x with | Closed -> fprintf f " Closed " | Open -> fprintf f " Open "
let fmt_rec_flag f x = match x with | Nonrecursive -> fprintf f " Nonrec " ; | Recursive -> fprintf f " Rec " ; ; ;
let fmt_direction_flag f x = match x with | Upto -> fprintf f " Up " ; | Downto -> fprintf f " Down " ; ; ;
let fmt_private_flag f x = match x with | Public -> fprintf f " Public " ; | Private -> fprintf f " Private " ; ; ;