diff --git a/CHANGES.md b/CHANGES.md index 9c893009a7..a59d6a7144 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,7 +14,6 @@ - Protect match after `fun _ : _ ->` (#2352, @Julow) - Fix invalid formatting of `(::)` (#2347, @Julow) - Fix formatting of string literals in code blocks (#2338, #2349, @Julow) -- Improve formatting of module arguments (#2322, @Julow) - Consistent indentation of `@@ let+ x = ...` (#2315, #2396, @Julow) - Remove double parenthesis around tuple in a match (#2308, @Julow) - Consistent indentation of `fun (type a) ->` that follow `fun x ->` (#2294, @Julow) @@ -28,6 +27,7 @@ ### Changes +- More consistent formatting of module types (#2322, #2395, @Julow) - Improve formatting of doc-comments (#2376, #2377, #2379, #2378, @Julow) - Disable reporting of deprecated alerts while formatting code blocks (#2373, @Julow) - Improve indentation of `as`-patterns (#2359, @Julow) diff --git a/lib-rpc/protocol.mli b/lib-rpc/protocol.mli index 7db30b222d..d67fa37bc5 100644 --- a/lib-rpc/protocol.mli +++ b/lib-rpc/protocol.mli @@ -37,23 +37,18 @@ module Make (IO : IO.S) : sig end (** Version used to set the protocol version *) - module Init : - Command_S with type t = [`Halt | `Unknown | `Version of string] - - module V1 : - Command_S - with type t = - [ `Halt - | `Unknown - | `Error of string - | `Config of (string * string) list - | `Format of string ] - - module V2 : - Command_S - with type t = - [ `Halt - | `Unknown - | `Error of string - | `Format of string * format_args ] + module Init : Command_S + with type t = [`Halt | `Unknown | `Version of string] + + module V1 : Command_S + with type t = + [ `Halt + | `Unknown + | `Error of string + | `Config of (string * string) list + | `Format of string ] + + module V2 : Command_S + with type t = + [`Halt | `Unknown | `Error of string | `Format of string * format_args] end diff --git a/lib/Ast.ml b/lib/Ast.ml index 63e5f31831..5c824722cc 100644 --- a/lib/Ast.ml +++ b/lib/Ast.ml @@ -625,7 +625,7 @@ module T = struct | Exp of expression | Lb of let_binding | Mb of module_binding - | Md of module_declaration + | Md of module_declaration * bool (** Recursive *) | Cl of class_expr | Mty of module_type | Mod of module_expr @@ -645,7 +645,7 @@ module T = struct | Exp e -> Format.fprintf fs "Exp:@\n%a" Printast.expression e | Lb b -> Format.fprintf fs "Lb:@\n%a" Printast.let_binding b | Mb m -> Format.fprintf fs "Mb:@\n%a" Printast.module_binding m - | Md m -> Format.fprintf fs "Md:@\n%a" Printast.module_declaration m + | Md (m, _) -> Format.fprintf fs "Md:@\n%a" Printast.module_declaration m | Cl cl -> Format.fprintf fs "Cl:@\n%a" Printast.class_expr cl | Mty mt -> Format.fprintf fs "Mty:@\n%a" Printast.module_type mt | Cty cty -> Format.fprintf fs "Cty:@\n%a" Printast.class_type cty @@ -675,7 +675,7 @@ let attributes = function | Exp x -> x.pexp_attributes | Lb x -> x.lb_attributes | Mb x -> x.pmb_attributes - | Md x -> x.pmd_attributes + | Md (x, _) -> x.pmd_attributes | Cl x -> x.pcl_attributes | Mty x -> x.pmty_attributes | Mod x -> x.pmod_attributes @@ -696,7 +696,7 @@ let location = function | Exp x -> x.pexp_loc | Lb x -> x.lb_loc | Mb x -> x.pmb_loc - | Md x -> x.pmd_loc + | Md (x, _) -> x.pmd_loc | Cl x -> x.pcl_loc | Mty x -> x.pmty_loc | Mod x -> x.pmod_loc @@ -725,7 +725,7 @@ let break_between s cc (i1, c1) (i2, c2) = | Sig i1, Sig i2 -> Signature_item.break_between s cc (i1, c1) (i2, c2) | Lb i1, Lb i2 -> Lb.break_between s cc (i1, c1) (i2, c2) | Mb i1, Mb i2 -> Mb.break_between s cc (i1, c1) (i2, c2) - | Md i1, Md i2 -> Md.break_between s cc (i1, c1) (i2, c2) + | Md (i1, _), Md (i2, _) -> Md.break_between s cc (i1, c1) (i2, c2) | Mty _, Mty _ -> break_between_modules s cc (i1, c1) (i2, c2) | Mod _, Mod _ -> break_between_modules s cc (i1, c1) (i2, c2) | Tli (`Item i1), Tli (`Item i2) -> @@ -1781,7 +1781,16 @@ end = struct (** [parenze_mty {ctx; ast}] holds when module type [ast] should be parenthesized in context [ctx]. *) - let parenze_mty {ctx= _; ast= mty} = Mty.has_trailing_attributes mty + let parenze_mty {ctx; ast= mty} = + match (ctx, mty.pmty_desc) with + | _, (Pmty_ident _ | Pmty_extension _ | Pmty_signature _) -> false + (* Currently, attributes on a [Pmty_functor] are always in short form. *) + | _, Pmty_functor _ -> false + (* [Pmty_with] must be parenthesed when on the RHS of a module decl + followed by an [and]. This is an over-approximation. *) + | Md (_, true), Pmty_with _ -> true + | (Str _ | Sig _), _ -> false + | _ -> Mty.has_trailing_attributes mty (** [parenze_mod {ctx; ast}] holds when module expr [ast] should be parenthesized in context [ctx]. *) diff --git a/lib/Ast.mli b/lib/Ast.mli index f261826ede..a16dcc5ae6 100644 --- a/lib/Ast.mli +++ b/lib/Ast.mli @@ -112,7 +112,7 @@ type t = | Exp of expression | Lb of let_binding | Mb of module_binding - | Md of module_declaration + | Md of module_declaration * bool (** Recursive *) | Cl of class_expr | Mty of module_type | Mod of module_expr diff --git a/lib/Fmt_ast.ml b/lib/Fmt_ast.ml index 3176c2b5af..6d8a70e933 100644 --- a/lib/Fmt_ast.ml +++ b/lib/Fmt_ast.ml @@ -3401,131 +3401,117 @@ and fmt_extension_constructor c ctx ec = | Pext_rebind lid -> str " = " $ fmt_longident_loc c lid ) $ fmt_attributes_and_docstrings c pext_attributes ) -and fmt_functor_param c ctx {loc; txt= arg} = - match arg with - | Unit -> Cmts.fmt c loc (wrap "(" ")" (Cmts.fmt_within c loc)) - | Named (name, mt) -> - let xmt = sub_mty ~ctx mt in - Cmts.fmt c loc - (wrap "(" ")" - (hovbox 0 - ( hovbox 0 (fmt_str_loc_opt c name $ fmt "@ : ") - $ compose_module (fmt_module_type c xmt) ~f:Fn.id ) ) ) +and fmt_functor_params c ~ctx ?(pro = noop) ?(epi = noop) args = + let args_p = Params.Mod.get_args c.conf ~ctx args in + let box_arg = + let indent = if args_p.align then 1 else 0 in + hvbox_if ((not args_p.dock) && args_p.align) indent + in + let fmt_name_and_mt ~pro ~loc name mt = + let pro = pro $ Cmts.fmt_before c loc $ str "(" in + let pro_inner, pro_outer = + if args_p.dock then (pro, noop) else (noop, pro) + in + let intro = pro_inner $ fmt_str_loc_opt c name $ str " : " + and epi = str ")" in + pro_outer + $ box_arg (fmt_module_type ~pro:intro ~epi c (sub_mty ~ctx mt)) + $ Cmts.fmt_after c loc + in + let fmt_arg ~pro {loc; txt= arg} = + let pro = pro $ args_p.arg_psp in + match arg with + | Unit -> pro $ Cmts.fmt c loc (wrap "(" ")" (Cmts.fmt_within c loc)) + | Named (name, mt) -> fmt_name_and_mt ~pro ~loc name mt + in + let rec fmt_args_docked ~pro = function + | [] -> pro + | hd :: tl -> fmt_args_docked ~pro:(fmt_arg ~pro hd) tl + in + let rec fmt_args_wrapped = function + | [] -> noop + | hd :: tl -> fmt_arg ~pro:noop hd $ fmt_args_wrapped tl + in + hvbox ~name:"args" args_p.indent + ( ( if args_p.dock then fmt_args_docked ~pro args + else pro $ fmt_args_wrapped args ) + $ epi ) -and fmt_module_type c ?(rec_ = false) ({ast= mty; _} as xmty) = +and fmt_module_type c ?(box = true) ?pro ?epi ({ast= mty; _} as xmty) : Fmt.t + = let ctx = Mty mty in let {pmty_desc; pmty_loc; pmty_attributes} = mty in - update_config_maybe_disabled_block c pmty_loc pmty_attributes + update_config_maybe_disabled c pmty_loc pmty_attributes @@ fun c -> - let parens = - parenze_mty xmty - || match pmty_desc with Pmty_with _ when rec_ -> true | _ -> false + let parens = parenze_mty xmty in + let pro = fmt_opt pro $ fmt_if parens "(" $ Cmts.fmt_before c pmty_loc + and epi ~attr = + Cmts.fmt_after ~pro:(str " ") c pmty_loc + $ fmt_if parens ")" + $ fmt_if_k attr (fmt_attributes c pmty_attributes ~pre:(Break (1, 0))) + $ fmt_opt epi in match pmty_desc with - | Pmty_ident lid -> - { empty with - bdy= fmt_longident_loc c lid - ; epi= Some (fmt_attributes c pmty_attributes ~pre:(Break (1, 0))) } + | Pmty_ident lid -> pro $ fmt_longident_loc c lid $ epi ~attr:true | Pmty_signature s -> let empty = List.is_empty s && not (Cmts.has_within c.cmts pmty_loc) in - let before = Cmts.fmt_before c pmty_loc in - let within = Cmts.fmt_within c ~pro:noop pmty_loc in - let after = Cmts.fmt_after c pmty_loc in - { opn= None - ; pro= Some (before $ str "sig" $ fmt_if empty " ") - ; psp= fmt_if (not empty) "@;<1000 2>" - ; bdy= within $ fmt_signature c ctx s - ; cls= noop - ; esp= fmt_if (not empty) "@;<1000 0>" - ; epi= - Some - ( str "end" $ after - $ fmt_attributes_and_docstrings c pmty_attributes ) } + (* Side effect before [epi ~attr] is important. *) + let cmts_within = Cmts.fmt_within ~pro:noop c pmty_loc in + let has_attrs = not (List.is_empty pmty_attributes) in + hvbox_if box 2 + ( pro $ str "sig" + $ (if empty then str " " else break 1000 0) + $ cmts_within $ fmt_signature c ctx s + $ (if empty then noop else break 1000 ~-2) + $ hvbox_if has_attrs 0 + ( str "end" + $ fmt_attributes_and_docstrings c pmty_attributes + $ epi ~attr:false ) ) | Pmty_functor (args, mt) -> - let blk = fmt_module_type c (sub_mty ~ctx mt) in - { blk with - pro= - Some - ( Cmts.fmt_before c pmty_loc - $ str "functor" - $ fmt_attributes c ~pre:Blank pmty_attributes - $ fmt "@;<1 2>" - $ list args "@;<1 2>" (fmt_functor_param c ctx) - $ fmt "@;<1 2>->" - $ opt blk.pro (fun pro -> str " " $ pro) ) - ; epi= Some (fmt_opt blk.epi $ Cmts.fmt_after c pmty_loc) - ; psp= - fmt_or_k (Option.is_none blk.pro) - (fits_breaks " " ~hint:(1, 2) "") - blk.psp } + let fmt_args = + let pro = + pro $ str "functor" $ fmt_attributes c ~pre:Blank pmty_attributes + and epi = str " ->" in + fmt_functor_params c ~ctx ~pro ~epi args + and epi = epi ~attr:false in + if Params.Mty.dock_functor_rhs c.conf ~rhs:mt then + let pro = fmt_args $ str " " in + fmt_module_type c ~box ~pro ~epi (sub_mty ~ctx mt) + else + let pro = fmt_args $ fmt "@ " in + hovbox_if box 2 (pro $ fmt_module_type c (sub_mty ~ctx mt) $ epi) | Pmty_gen (gen_loc, mt) -> - let blk = fmt_module_type c (sub_mty ~ctx mt) in - { blk with - pro= - Some - ( Cmts.fmt_before c pmty_loc - $ Cmts.fmt c gen_loc (wrap "(" ")" (Cmts.fmt_within c gen_loc)) - $ fmt "@;<1 2>->" - $ opt blk.pro (fun pro -> str " " $ pro) ) - ; epi= Some (fmt_opt blk.epi $ Cmts.fmt_after c pmty_loc) - ; psp= - fmt_or_k (Option.is_none blk.pro) - (fits_breaks " " ~hint:(1, 2) "") - blk.psp } + let pro = + pro + $ Cmts.fmt c gen_loc (wrap "(" ")" (Cmts.fmt_within c gen_loc)) + $ fmt "@;<1 2>-> " + in + fmt_module_type c ~box ~pro (sub_mty ~ctx mt) $ epi ~attr:true | Pmty_with _ -> let wcs, mt = Sugar.mod_with (sub_mty ~ctx mty) in let fmt_cstr ~first ~last:_ wc = let pre = if first then "with" else " and" in - fmt_or first "@ " "@," $ fmt_with_constraint c ctx ~pre wc + fmt_if (not first) "@," $ fmt_with_constraint c ctx ~pre wc in let fmt_cstrs ~first:_ ~last:_ (wcs_and, loc, attr) = - Cmts.fmt c loc - ( list_fl wcs_and fmt_cstr - $ fmt_attributes c ~pre:(Break (1, -1)) attr ) + fmt "@ " + $ hvbox 0 + (Cmts.fmt c loc + ( list_fl wcs_and fmt_cstr + $ fmt_attributes c ~pre:(Break (1, -1)) attr ) ) in - let {pro; psp; bdy; esp; epi; opn= _; cls= _} = fmt_module_type c mt in - { empty with - pro= - Option.map pro ~f:(fun pro -> - open_hvbox 0 $ fmt_if parens "(" $ pro ) - ; psp - ; bdy= - fmt_if_k (Option.is_none pro) (open_hvbox 2 $ fmt_if parens "(") - $ hvbox 0 bdy - $ fmt_if_k (Option.is_some epi) esp - $ fmt_opt epi $ list_fl wcs fmt_cstrs $ fmt_if parens ")" - $ close_box - ; esp= fmt_if_k (Option.is_none epi) esp - ; epi= Some (Cmts.fmt_after c pmty_loc) } - | Pmty_typeof me -> ( - let blk = fmt_module_expr c (sub_mod ~ctx me) in - let epi = - fmt_opt blk.epi $ Cmts.fmt_after c pmty_loc $ fmt_if parens ")" - $ fmt_attributes c pmty_attributes ~pre:(Break (1, 0)) + Params.Mty.box_with c.conf ~box ~lhs:mt.ast + ( fmt_module_type c ~pro mt $ list_fl wcs fmt_cstrs + $ epi ~attr:false (* Handled by [Sugar.mod_with]. *) ) + | Pmty_typeof me -> + let break = + if Params.Mty.dock_typeof c.conf ~rhs:me then str " " else break 1 2 in - match blk.pro with - | Some pro -> - { blk with - pro= - Some - ( Cmts.fmt_before c pmty_loc - $ fmt_if parens "(" $ str "module type of " $ pro ) - ; epi= Some epi } - | _ -> - { blk with - bdy= - Cmts.fmt c pmty_loc - @@ hvbox 2 - (fmt_if parens "(" $ fmt "module type of@ " $ blk.bdy) - ; epi= Some epi } ) - | Pmty_extension ext -> - { empty with - bdy= fmt_extension c ctx ext - ; epi= Some (fmt_attributes c pmty_attributes ~pre:(Break (1, 0))) } - | Pmty_alias lid -> - { empty with - bdy= fmt_longident_loc c lid - ; epi= Some (fmt_attributes c pmty_attributes ~pre:(Break (1, 0))) } + let pro = pro $ fmt "module type of" $ break in + let me_blk = fmt_module_expr c (sub_mod ~ctx me) in + hvbox_if box 0 (compose_module ~pro me_blk ~f:Fn.id $ epi ~attr:true) + | Pmty_extension ext -> pro $ fmt_extension c ctx ext $ epi ~attr:true + | Pmty_alias lid -> pro $ fmt_longident_loc c lid $ epi ~attr:true and fmt_signature c ctx itms = let update_config c i = @@ -3571,25 +3557,11 @@ and fmt_signature_item c ?ext {ast= si; _} = let force_before = not (Mty.is_simple pincl_mod) in fmt_docstring_around_item c ~force_before ~fit:true pincl_attributes in - let keyword, ({pro; psp; bdy; esp; epi; _} as blk) = - let kwd = str "include" $ fmt_extension_suffix c ext in - match pincl_mod with - | {pmty_desc= Pmty_typeof me; pmty_loc; pmty_attributes= _} -> - ( kwd - $ Cmts.fmt c ~pro:(str " ") ~epi:noop pmty_loc - (fmt "@ module type of") - , fmt_module_expr c (sub_mod ~ctx me) ) - | _ -> (kwd, fmt_module_type c (sub_mty ~ctx pincl_mod)) - in - let box = blk_box blk in + let pro = str "include" $ fmt_extension_suffix c ext $ str " " in hvbox 0 ( doc_before $ hvbox 0 - ( box - ( hvbox 2 (keyword $ opt pro (fun pro -> str " " $ pro)) - $ fmt_or_k (Option.is_some pro) psp (fmt "@;<1 2>") - $ bdy ) - $ esp $ fmt_opt epi + ( fmt_module_type ~pro c (sub_mty ~ctx pincl_mod) $ fmt_item_attributes c ~pre:(Break (1, 0)) atrs ) $ doc_after ) | Psig_modtype mtd -> fmt_module_type_declaration ?ext c ctx mtd @@ -3604,7 +3576,7 @@ and fmt_signature_item c ?ext {ast= si; _} = | Psig_recmodule mds -> fmt_recmodule c ctx mds (fmt_module_declaration ?ext) - (fun x -> Md x) + (fun x -> Md (x, true)) sub_md | Psig_type (rec_flag, decls) -> fmt_type c ?ext rec_flag decls ctx | Psig_typext te -> fmt_type_extension ?ext c ctx te @@ -3683,49 +3655,10 @@ and fmt_class_exprs ?ext c ctx cls = $ hovbox 0 @@ Cmts.fmt c cl.pci_loc (doc_before $ class_exprs $ doc_after) ) -and fmt_module c ctx ?rec_ ?ext ?epi ?(can_sparse = false) keyword - ?(eqty = "=") name xargs xbody xmty attributes ~rec_flag = - let blk_t = - Option.value_map xmty ~default:empty ~f:(fun xmty -> - let blk = fmt_module_type ?rec_ c xmty in - { blk with - pro= - Some (str " " $ str eqty $ opt blk.pro (fun pro -> str " " $ pro)) - ; psp= fmt_if (Option.is_none blk.pro) "@;<1 2>" $ blk.psp } ) - in +and fmt_module c ctx ?ext ?epi ?(can_sparse = false) keyword ?(eqty = "=") + name xargs xbody xmty attributes ~rec_flag = let blk_b = Option.value_map xbody ~default:empty ~f:(fmt_module_expr c) in - let fmt_name_and_mt ~pro ~loc name mt = - let xmt = sub_mty ~ctx mt in - let blk = fmt_module_type c ?rec_ xmt in - let pro = - pro $ Cmts.fmt_before c loc $ str "(" $ fmt_str_loc_opt c name - $ str " : " - and epi = str ")" $ Cmts.fmt_after c loc in - compose_module' ~box:false ~pro ~epi blk - in - let args_p = Params.Mod.get_args c.conf xargs in - (* Carry the [epi] to be placed in the next argument's box. *) - let fmt_arg ~pro {loc; txt} = - let pro = pro $ args_p.arg_psp in - match txt with - | Unit -> - (pro $ Cmts.fmt c loc (wrap "(" ")" (Cmts.fmt_within c loc)), noop) - | Named (name, mt) -> - if args_p.dock then - (* All signatures, put the [epi] into the box of the next arg and - don't break. *) - fmt_name_and_mt ~pro ~loc name mt - else - let bdy, epi = fmt_name_and_mt ~pro:noop ~loc name mt in - let bdy_indent = if args_p.align then 1 else 0 in - (pro $ hvbox bdy_indent bdy $ epi, noop) - in - let rec fmt_args ~pro = function - | [] -> pro - | hd :: tl -> - let bdy, epi = fmt_arg ~pro hd in - bdy $ fmt_args ~pro:epi tl - in + let args_p = Params.Mod.get_args c.conf ~ctx xargs in let intro = str keyword $ fmt_extension_suffix c ext @@ -3744,19 +3677,27 @@ and fmt_module c ctx ?rec_ ?ext ?epi ?(can_sparse = false) keyword fmt_docstring_around_item c ~force_before:(not single_line) ~fit:true attributes in + let fmt_mty ~epi = + let args ~epi = fmt_functor_params c ~ctx ~pro:intro ~epi xargs in + let epi = fmt_if (Option.is_some xbody) " =" $ epi in + match xmty with + | Some xmty -> + let break = + if + Params.Mty.dock_module_sig c.conf ~args_are_docked:args_p.dock + xmty.ast + then str " " + else fmt "@ " + in + let pro = args ~epi:(str " " $ str eqty $ break) in + hovbox 0 (fmt_module_type ~pro c xmty $ epi) + | None -> hvbox 0 (args ~epi:noop $ epi) + in hvbox (if compact then 0 else 2) ( doc_before $ blk_box blk_b - ( (if Option.is_some blk_t.epi then hovbox else hvbox) - 0 - ( blk_box blk_t - ( hvbox args_p.indent - (fmt_args ~pro:intro xargs $ fmt_opt blk_t.pro) - $ blk_t.psp $ blk_t.bdy ) - $ blk_t.esp $ fmt_opt blk_t.epi - $ fmt_if (Option.is_some xbody) " =" - $ fmt_if_k compact fmt_pro ) + ( fmt_mty ~epi:(fmt_if_k compact fmt_pro) $ fmt_if_k (not compact) fmt_pro $ blk_b.psp $ fmt_if (Option.is_none blk_b.pro && Option.is_some xbody) "@ " @@ -3774,12 +3715,12 @@ and fmt_module c ctx ?rec_ ?ext ?epi ?(can_sparse = false) keyword $ epi ) ) and fmt_module_declaration ?ext c ~rec_flag ~first {ast= pmd; _} = - protect c (Md pmd) + let ctx = Md (pmd, rec_flag) in + protect c ctx @@ let {pmd_name; pmd_args; pmd_type; pmd_attributes; pmd_loc} = pmd in update_config_maybe_disabled c pmd_loc pmd_attributes @@ fun c -> - let ctx = Md pmd in let ext = if first then ext else None in let keyword = if first then "module" else "and" in let xmty = sub_mty ~ctx pmd_type in @@ -3787,8 +3728,8 @@ and fmt_module_declaration ?ext c ~rec_flag ~first {ast= pmd; _} = match xmty.ast.pmty_desc with Pmty_alias _ -> None | _ -> Some ":" in Cmts.fmt c pmd_loc - (fmt_module ~rec_:rec_flag ?ext c ctx keyword pmd_name pmd_args None - ?eqty (Some xmty) ~rec_flag:(rec_flag && first) pmd_attributes ) + (fmt_module ?ext c ctx keyword pmd_name pmd_args None ?eqty (Some xmty) + ~rec_flag:(rec_flag && first) pmd_attributes ) and fmt_module_substitution ?ext c ctx pms = let {pms_name; pms_manifest; pms_attributes; pms_loc} = pms in @@ -3980,22 +3921,19 @@ and fmt_module_expr ?(dock_struct = true) c ({ast= m; _} as xmod) = (`Block (blk_a, Mod.is_simple me_a)) | Pmod_constraint (me, mt) -> let blk_e = fmt_module_expr c (sub_mod ~ctx me) in - let blk_t = fmt_module_type c (sub_mty ~ctx mt) in let has_epi = Cmts.has_after c.cmts pmod_loc || not (List.is_empty pmod_attributes) in - { opn= Some (fmt_opt blk_t.opn $ fmt_opt blk_e.opn $ open_hovbox 2) + { opn= Some (fmt_opt blk_e.opn $ open_hovbox 2) ; pro= Some (Cmts.fmt_before c pmod_loc $ str "(") ; psp= fmt "@," ; bdy= hvbox 0 ( fmt_opt blk_e.pro $ blk_e.psp $ blk_e.bdy $ blk_e.esp $ fmt_opt blk_e.epi $ fmt " :@;<1 2>" - $ hvbox 0 - ( fmt_opt blk_t.pro $ blk_t.psp $ blk_t.bdy $ blk_t.esp - $ fmt_opt blk_t.epi ) ) + $ hvbox 0 (fmt_module_type c (sub_mty ~ctx mt)) ) $ closing_paren c ~offset:(-2) - ; cls= close_box $ blk_e.cls $ blk_t.cls + ; cls= close_box $ blk_e.cls ; esp= noop ; epi= Option.some_if has_epi @@ -4003,17 +3941,19 @@ and fmt_module_expr ?(dock_struct = true) c ({ast= m; _} as xmod) = $ fmt_attributes_and_docstrings c pmod_attributes ) } | Pmod_functor (args, me) -> let doc, atrs = doc_atrs pmod_attributes in + let fmt_args = + let pro = str "functor" $ fmt_attributes c ~pre:Blank atrs in + fmt_functor_params c ~ctx ~pro args + in { empty with - bdy= + opn= Some (open_hvbox 2) + ; cls= close_box + ; bdy= Cmts.fmt c pmod_loc ( fmt_docstring c ~epi:(fmt "@,") doc - $ hvbox 0 + $ hvbox 2 (wrap_if parens "(" ")" - ( str "functor" - $ fmt_attributes c ~pre:Blank atrs - $ fmt "@;<1 2>" - $ list args "@;<1 2>" (fmt_functor_param c ctx) - $ fmt "@;<1 2>->@;<1 2>" + ( fmt_args $ fmt " ->@ " $ compose_module (fmt_module_expr c (sub_mod ~ctx me)) ~f:(hvbox 0) ) ) ) } @@ -4349,9 +4289,8 @@ and fmt_module_binding ?ext c ~rec_flag ~first {ast= pmb; _} = | _ -> (xbody, None) in Cmts.fmt c pmb.pmb_loc - (fmt_module ~rec_:rec_flag ?ext c ctx keyword - ~rec_flag:(rec_flag && first) ~eqty:":" pmb.pmb_name pmb.pmb_args - (Some xbody) xmty pmb.pmb_attributes ) + (fmt_module ?ext c ctx keyword ~rec_flag:(rec_flag && first) ~eqty:":" + pmb.pmb_name pmb.pmb_args (Some xbody) xmty pmb.pmb_attributes ) let fmt_toplevel_directive c ~semisemi dir = let fmt_dir_arg = function @@ -4467,9 +4406,7 @@ let fmt_file (type a) ~ctx ~fmt_code ~debug (fragment : a Extended_ast.t) | Signature, l -> Chunk.split_and_fmt Signature c ctx l | Use_file, l -> Chunk.split_and_fmt Use_file c ctx l | Core_type, ty -> fmt_core_type c (sub_typ ~ctx:(Pld (PTyp ty)) ty) - | Module_type, mty -> - compose_module ~f:Fn.id - (fmt_module_type c (sub_mty ~ctx:(Mty mty) mty)) + | Module_type, mty -> fmt_module_type c (sub_mty ~ctx:(Mty mty) mty) | Expression, e -> fmt_expression c (sub_exp ~ctx:(Str (Ast_helper.Str.eval e)) e) | Repl_file, l -> fmt_repl_file c ctx l diff --git a/lib/Params.ml b/lib/Params.ml index 9d64a1d444..525d0fdf62 100644 --- a/lib/Params.ml +++ b/lib/Params.ml @@ -92,17 +92,43 @@ module Mod = struct true | _ -> false - let get_args (c : Conf.t) args = - let indent, psp_indent = if ocp c then (2, 2) else (0, 4) in + let get_args (c : Conf.t) ~ctx args = let dock = (* ocp-indent-compat: Dock only one argument to avoid alignment of - subsequent arguments. *) - if ocp c then match args with [arg] -> arg_is_sig arg | _ -> false + subsequent arguments. Avoid docking for module exprs and module type + exprs to also avoid alignment. *) + if ocp c then + match ctx, args with + | Ast.Mb _, [arg] -> arg_is_sig arg + | _ -> false else List.for_all ~f:arg_is_sig args in - let arg_psp = if dock then str " " else break 1 psp_indent in + let arg_psp = if dock then str " " else break 1 2 in let align = ocp c in - {dock; arg_psp; indent; align} + {dock; arg_psp; indent= 2; align} +end + +module Mty = struct + let dock_functor_rhs (_c : Conf.t) ~rhs = + match rhs.pmty_desc with + | Pmty_signature _ | Pmty_with _ -> true + | _ -> false + + let dock_module_sig (_c : Conf.t) ~args_are_docked mty = + match mty.pmty_desc with + | Pmty_signature _ -> true + | Pmty_ident _ + |Pmty_with ({pmty_desc= Pmty_signature _ | Pmty_ident _; _}, _) -> + args_are_docked + | _ -> false + + let dock_typeof _c ~rhs = + match rhs.pmod_desc with Pmod_structure _ -> true | _ -> false + + let box_with _c ~box ~lhs = + match lhs.pmty_desc with + | Pmty_signature _ -> hvbox_if ~name:"with" box 0 + | _ -> hovbox_if ~name:"with" box 2 end let get_or_pattern_sep ?(cmts_before = false) ?(space = false) (c : Conf.t) diff --git a/lib/Params.mli b/lib/Params.mli index 0f7f31d1de..04b96e68c8 100644 --- a/lib/Params.mli +++ b/lib/Params.mli @@ -45,7 +45,25 @@ module Mod : sig ; align: bool (** Whether to align argument types inside their parenthesis. *) } - val get_args : Conf.t -> functor_parameter loc list -> args + (** Can be called from [Pmty_functor], [Pmod_functor] or [Pstr_module]. *) + val get_args : Conf.t -> ctx:Ast.t -> functor_parameter loc list -> args +end + +module Mty : sig + val dock_functor_rhs : Conf.t -> rhs:module_type -> bool + (** Whether functor types should be docked on the same line or break after + the [->]. *) + + val dock_module_sig : Conf.t -> args_are_docked:bool -> module_type -> bool + (** Whether the signature of a module decl should be docked after the [:]. + [~args_are_docked] expects the [dock] field returned by + {!Mod.get_args}. *) + + val dock_typeof : Conf.t -> rhs:module_expr -> bool + (** Whether to dock the RHS of a [module type of]. *) + + val box_with : Conf.t -> box:bool -> lhs:module_type -> Fmt.t -> Fmt.t + (** The box around a [Pmty_with]. *) end val get_or_pattern_sep : diff --git a/test/passing/tests/apply_functor.ml b/test/passing/tests/apply_functor.ml index 4850426595..809b6b881e 100644 --- a/test/passing/tests/apply_functor.ml +++ b/test/passing/tests/apply_functor.ml @@ -1,7 +1,5 @@ module _ = F (functor (X : T) -> X) module _ = F - (functor - (X____________________________ : T) - -> + (functor (X____________________________ : T) -> X____________________________) diff --git a/test/passing/tests/attributes.ml b/test/passing/tests/attributes.ml index d37d0b8be8..34994374fc 100644 --- a/test/passing/tests/attributes.ml +++ b/test/passing/tests/attributes.ml @@ -51,13 +51,12 @@ module type M = sig module T : (S with type t = (r[@test3]) [@test4]) - module T : - (S - with type t = t - and type u := u - and module R = R - and module S := S - [@test]) + module T : (S + with type t = t + and type u := u + and module R = R + and module S := S + [@test]) module T : module type of X [@test5] diff --git a/test/passing/tests/attributes.ml.err b/test/passing/tests/attributes.ml.err index 5d08534f53..275347abc5 100644 --- a/test/passing/tests/attributes.ml.err +++ b/test/passing/tests/attributes.ml.err @@ -1 +1 @@ -Warning: tests/attributes.ml:339 exceeds the margin +Warning: tests/attributes.ml:338 exceeds the margin diff --git a/test/passing/tests/break_struct.ml.ref b/test/passing/tests/break_struct.ml.ref index 6424689ae2..a196ea246e 100644 --- a/test/passing/tests/break_struct.ml.ref +++ b/test/passing/tests/break_struct.ml.ref @@ -79,7 +79,5 @@ end include ( Ast_407 : - module type of struct - include Ast_407 - end - with module Location := Ast_407.Location ) + module type of struct include Ast_407 end + with module Location := Ast_407.Location ) diff --git a/test/passing/tests/functor.ml b/test/passing/tests/functor.ml index 52d911a6b0..01568b90f6 100644 --- a/test/passing/tests/functor.ml +++ b/test/passing/tests/functor.ml @@ -13,9 +13,8 @@ module type M = functor (S : S) (T : T) -> U module type M = functor (S : S) () -> sig end module type M = functor - (SSSSS : SSSSSSSSSSSSSS) - (TTTTT : TTTTTTTTTTTTTTTT) - -> sig + (SSSSS : SSSSSSSSSSSSSS) + (TTTTT : TTTTTTTTTTTTTTTT) -> sig val t1 : a val t2 : b @@ -40,10 +39,7 @@ module type S = sig end module M = - (functor - (SSSSS : sssssSSSSSSSSSSSSSS) - (TTTTT : TTTTTTTTTTTTTTTTTTTTT) - -> + (functor (SSSSS : sssssSSSSSSSSSSSSSS) (TTTTT : TTTTTTTTTTTTTTTTTTTTT) -> struct let x = 2 @@ -60,24 +56,23 @@ module type Module_type_fail = sig include S end -module type KV_MAKER = functor (G : Irmin_git.G) (C : Irmin.Contents.S) -> - S - with type key = string list - and type step = string - and type contents = C.t - and type branch = string - and module Git = G +module type KV_MAKER = functor (G : Irmin_git.G) (C : Irmin.Contents.S) -> S + with type key = string list + and type step = string + and type contents = C.t + and type branch = string + and module Git = G module Make (TT : TableFormat.TABLES) (IT : InspectionTableFormat.TABLES with type 'a lr1state = int) (ET : EngineTypes.TABLE - with type terminal = int - and type nonterminal = int - and type semantic_value = Obj.t) + with type terminal = int + and type nonterminal = int + and type semantic_value = Obj.t) (E : sig - type 'a env = (ET.state, ET.semantic_value, ET.token) EngineTypes.env - end) = + type 'a env = (ET.state, ET.semantic_value, ET.token) EngineTypes.env + end) = struct type t = t end diff --git a/test/passing/tests/functor.ml.err b/test/passing/tests/functor.ml.err new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/passing/tests/js_source.ml.err b/test/passing/tests/js_source.ml.err index 0e73dc9f96..f0ca9abf10 100644 --- a/test/passing/tests/js_source.ml.err +++ b/test/passing/tests/js_source.ml.err @@ -1,5 +1,4 @@ -Warning: tests/js_source.ml:155 exceeds the margin -Warning: tests/js_source.ml:3556 exceeds the margin +Warning: tests/js_source.ml:3555 exceeds the margin Warning: tests/js_source.ml:9522 exceeds the margin Warning: tests/js_source.ml:9625 exceeds the margin Warning: tests/js_source.ml:9644 exceeds the margin diff --git a/test/passing/tests/js_source.ml.ocp b/test/passing/tests/js_source.ml.ocp index 281aa3bb3e..7e8052b863 100644 --- a/test/passing/tests/js_source.ml.ocp +++ b/test/passing/tests/js_source.ml.ocp @@ -14,7 +14,7 @@ end [@foo] [@@foo] module type S = sig - include ((module type of M [@foo]) [@foo] with type t := M.t [@foo]) [@@foo] + include (module type of M [@foo]) [@foo] with type t := M.t [@foo] [@@foo] [@@@foo] end [@foo] @@ -153,8 +153,8 @@ type t = [%foo: ((module M)[@foo])] module M = (functor [@foo] (M : S) -> (val x) [@foo] (struct end [@foo])) (* Module type expression *) -module type S = functor [@foo] (M : S) -> functor (_ : (module type of M) [@foo]) -> sig end - [@foo] +module type S = functor [@foo] (M : S) -> + functor (_ : (module type of M) [@foo]) -> sig end [@foo] module type S = functor (_ : S) (_ : S) -> S module type S = functor (_ : functor (_ : S) -> S) -> S @@ -3023,11 +3023,10 @@ let magic : 'a 'b. 'a -> 'b = (functor (T : sig type 'a t - end) - -> - struct - let f (Refl : (a T.t, b T.t) eq) = (x :> b) - end) + end) -> + struct + let f (Refl : (a T.t, b T.t) eq) = (x :> b) + end) (struct type 'a t = unit end) @@ -4796,8 +4795,8 @@ module type PR6513 = sig type uri end - module Make : functor (Html5 : T with type 'a wrap = 'a) -> - S with type u = < foo : Html5.uri > + module Make : functor (Html5 : T with type 'a wrap = 'a) -> S + with type u = < foo : Html5.uri > end (* Requires -package tyxml @@ -7064,8 +7063,8 @@ module PR_4261 = struct type t = D.t end - module rec U : (T with module D = U') = U - and U' : (S with type t = U'.t) = U + module rec U : T with module D = U' = U + and U' : S with type t = U'.t = U end (* Bad - PR 4512 *) @@ -7073,7 +7072,7 @@ module type S' = sig type t = int end -module rec M : (S' with type t = M.t) = struct +module rec M : S' with type t = M.t = struct type t = M.t end @@ -7165,7 +7164,7 @@ module Bootstrap2 ;; end - and Diet : (SET with type t = Elt.t tree and type elt = Elt.t) = MakeDiet (Elt) + and Diet : SET with type t = Elt.t tree and type elt = Elt.t = MakeDiet (Elt) type t = Diet.t @@ -7248,7 +7247,7 @@ module PR_4557 = struct let compare x y = 0 end - and ModSet : (Set.S with type elt = Mod.t) = Set.Make (Mod) + and ModSet : Set.S with type elt = Mod.t = Set.Make (Mod) end end @@ -7278,7 +7277,7 @@ module F (X : Set.OrderedType) = struct let compare x y = 0 end - and ModSet : (Set.S with type elt = Mod.t) = Set.Make (Mod) + and ModSet : Set.S with type elt = Mod.t = Set.Make (Mod) end (* Tests for recursive modules *) @@ -7311,7 +7310,7 @@ end = struct ;; end -and ASet : (Set.S with type elt = A.t) = Set.Make (A) +and ASet : Set.S with type elt = A.t = Set.Make (A) let _ = let x = A.Node (ASet.add (A.Leaf 3) (ASet.singleton (A.Leaf 2))) in @@ -7572,7 +7571,8 @@ end module Bootstrap (MakeH : functor (Element : ORDERED) -> HEAP with module Elem = Element) - (Element : ORDERED) : HEAP with module Elem = Element = struct + (Element : ORDERED) : + HEAP with module Elem = Element = struct module Elem = Element module rec BE : sig @@ -7613,7 +7613,7 @@ module Bootstrap ;; end - and PrimH : (HEAP with type Elem.t = BE.t) = MakeH (BE) + and PrimH : HEAP with type Elem.t = BE.t = MakeH (BE) type heap = BE.t @@ -8273,8 +8273,8 @@ module type BARECODE = sig end module USERCODE (X : TYPEVIEW) = struct - module type F = functor (C : CORE with type V.usert = X.combined) -> - BARECODE with type state := C.V.state + module type F = functor (C : CORE with type V.usert = X.combined) -> BARECODE + with type state := C.V.state end module Weapon = struct diff --git a/test/passing/tests/js_source.ml.ref b/test/passing/tests/js_source.ml.ref index 91272d38d4..35c4d206db 100644 --- a/test/passing/tests/js_source.ml.ref +++ b/test/passing/tests/js_source.ml.ref @@ -14,7 +14,7 @@ end [@foo] [@@foo] module type S = sig - include ((module type of M [@foo]) [@foo] with type t := M.t [@foo]) [@@foo] + include (module type of M [@foo]) [@foo] with type t := M.t [@foo] [@@foo] [@@@foo] end [@foo] @@ -153,8 +153,8 @@ type t = [%foo: ((module M)[@foo])] module M = (functor [@foo] (M : S) -> (val x) [@foo] (struct end [@foo])) (* Module type expression *) -module type S = functor [@foo] (M : S) -> functor (_ : (module type of M) [@foo]) -> sig end -[@foo] +module type S = functor [@foo] (M : S) -> + functor (_ : (module type of M) [@foo]) -> sig end [@foo] module type S = functor (_ : S) (_ : S) -> S module type S = functor (_ : functor (_ : S) -> S) -> S @@ -2363,8 +2363,8 @@ let inlineseq_from_astseq seq = ;; module Add (T : sig - type two - end) = + type two +end) = struct type _ t = | One : [ `One ] t @@ -2449,8 +2449,8 @@ let example6 : type a. a wrapPoly -> a -> int = let _ = example6 (WrapPoly AandBTags) `TagB (* This causes a seg fault *) module F (S : sig - type 'a t - end) = + type 'a t +end) = struct type _ ab = | A : int S.t ab @@ -2464,8 +2464,8 @@ struct end module F (S : sig - type 'a t - end) = + type 'a t +end) = struct type a = int * int type b = int -> int @@ -2567,9 +2567,9 @@ let f : (int s, int t) eq -> unit = function ;; module M (S : sig - type 'a t = T of 'a - type 'a s = T of 'a - end) = + type 'a t = T of 'a + type 'a s = T of 'a +end) = struct let f : ('a S.s, 'a S.t) eq -> unit = function | Refl -> () @@ -2809,8 +2809,8 @@ let f (type a) (Neq n : (a, a t) eq) = n (* warn! *) module F (T : sig - type _ t - end) = + type _ t +end) = struct let f (type a) (Neq n : (a, a T.t) eq) = n (* warn! *) end @@ -3021,10 +3021,9 @@ let magic : 'a 'b. 'a -> 'b = fun (type a b) (x : a) -> let module M = (functor - (T : sig - type 'a t - end) - -> + (T : sig + type 'a t + end) -> struct let f (Refl : (a T.t, b T.t) eq) = (x :> b) end) @@ -4689,11 +4688,11 @@ let x = (3 : X2.F(DUMMY)(DUMMY).t) let x = (3 : X2.F(DUMMY)(DUMMY).t') module F (M : sig - type 'a t - type 'a u = string + type 'a t + type 'a u = string - val f : unit -> _ u t - end) = + val f : unit -> _ u t +end) = struct let t = M.f () end @@ -4796,8 +4795,8 @@ module type PR6513 = sig type uri end - module Make : functor (Html5 : T with type 'a wrap = 'a) -> - S with type u = < foo : Html5.uri > + module Make : functor (Html5 : T with type 'a wrap = 'a) -> S + with type u = < foo : Html5.uri > end (* Requires -package tyxml @@ -5012,8 +5011,8 @@ type (_, _) eq = Eq : ('a, 'a) eq let cast : type a b. (a, b) eq -> a -> b = fun Eq x -> x module Fix (F : sig - type 'a f - end) = + type 'a f +end) = struct type 'a fix = ('a, 'a F.f) eq @@ -5535,8 +5534,8 @@ C.one.Complex.re include C module F (X : sig - module C = Char - end) = + module C = Char +end) = struct module C = X.C end @@ -6005,7 +6004,7 @@ module Y = functor (X : sig end) (Y : sig end) (Z : sig end) -> struct end module Z = functor (_ : sig end) (_ : sig end) (_ : sig end) -> struct end module GZ : functor (X : sig end) () (Z : sig end) -> sig end = -functor (X : sig end) () (Z : sig end) -> struct end + functor (X : sig end) () (Z : sig end) -> struct end module F (X : sig end) = struct type t = int @@ -6682,8 +6681,8 @@ class ['a] s3object r : ['a] s3 = end module M (T : sig - type t - end) = + type t +end) = struct type t = private { t : T.t } end @@ -7064,8 +7063,8 @@ module PR_4261 = struct type t = D.t end - module rec U : (T with module D = U') = U - and U' : (S with type t = U'.t) = U + module rec U : T with module D = U' = U + and U' : S with type t = U'.t = U end (* Bad - PR 4512 *) @@ -7073,7 +7072,7 @@ module type S' = sig type t = int end -module rec M : (S' with type t = M.t) = struct +module rec M : S' with type t = M.t = struct type t = M.t end @@ -7165,7 +7164,7 @@ module Bootstrap2 ;; end - and Diet : (SET with type t = Elt.t tree and type elt = Elt.t) = MakeDiet (Elt) + and Diet : SET with type t = Elt.t tree and type elt = Elt.t = MakeDiet (Elt) type t = Diet.t @@ -7248,7 +7247,7 @@ module PR_4557 = struct let compare x y = 0 end - and ModSet : (Set.S with type elt = Mod.t) = Set.Make (Mod) + and ModSet : Set.S with type elt = Mod.t = Set.Make (Mod) end end @@ -7278,7 +7277,7 @@ module F (X : Set.OrderedType) = struct let compare x y = 0 end - and ModSet : (Set.S with type elt = Mod.t) = Set.Make (Mod) + and ModSet : Set.S with type elt = Mod.t = Set.Make (Mod) end (* Tests for recursive modules *) @@ -7311,7 +7310,7 @@ end = struct ;; end -and ASet : (Set.S with type elt = A.t) = Set.Make (A) +and ASet : Set.S with type elt = A.t = Set.Make (A) let _ = let x = A.Node (ASet.add (A.Leaf 3) (ASet.singleton (A.Leaf 2))) in @@ -7572,7 +7571,8 @@ end module Bootstrap (MakeH : functor (Element : ORDERED) -> HEAP with module Elem = Element) - (Element : ORDERED) : HEAP with module Elem = Element = struct + (Element : ORDERED) : + HEAP with module Elem = Element = struct module Elem = Element module rec BE : sig @@ -7613,7 +7613,7 @@ module Bootstrap ;; end - and PrimH : (HEAP with type Elem.t = BE.t) = MakeH (BE) + and PrimH : HEAP with type Elem.t = BE.t = MakeH (BE) type heap = BE.t @@ -7804,8 +7804,8 @@ and Coerce3 : sig end = struct end let _ = test 81 (Coerce2.f1 ()) 1 module Coerce4 (A : sig - val f : int -> int - end) = + val f : int -> int +end) = struct let x = 0 let at a = A.f a @@ -7852,8 +7852,8 @@ let _ = (* PR#4316 *) module G (S : sig - val x : int Lazy.t - end) = + val x : int Lazy.t +end) = struct include S end @@ -7946,8 +7946,8 @@ module type S = sig end module F (X : sig - val x : (module S) - end) = + val x : (module S) +end) = struct module A = (val X.x) end @@ -8273,8 +8273,8 @@ module type BARECODE = sig end module USERCODE (X : TYPEVIEW) = struct - module type F = functor (C : CORE with type V.usert = X.combined) -> - BARECODE with type state := C.V.state + module type F = functor (C : CORE with type V.usert = X.combined) -> BARECODE + with type state := C.V.state end module Weapon = struct @@ -8296,11 +8296,11 @@ module M = struct end module type S = sig - module M : sig - type t - end + module M : sig + type t end - with module M = M +end +with module M = M module type Printable = sig type t @@ -8373,9 +8373,9 @@ end module type S' = S with module T := M module type S = sig - type 'a t - end - with type 'a t := unit + type 'a t +end +with type 'a t := unit (* Fails *) let property (type t) () = diff --git a/test/passing/tests/let_module-sparse.ml.ref b/test/passing/tests/let_module-sparse.ml.ref index 66a42f904a..4dae3fecc9 100644 --- a/test/passing/tests/let_module-sparse.ml.ref +++ b/test/passing/tests/let_module-sparse.ml.ref @@ -57,5 +57,5 @@ let () = let f () = let module (* comment *) - M = struct end in + M = struct end in () diff --git a/test/passing/tests/let_module.ml.ref b/test/passing/tests/let_module.ml.ref index e484db2ded..3d7ad2f82c 100644 --- a/test/passing/tests/let_module.ml.ref +++ b/test/passing/tests/let_module.ml.ref @@ -49,5 +49,5 @@ let () = let f () = let module (* comment *) - M = struct end in + M = struct end in () diff --git a/test/passing/tests/mod_type_subst.ml b/test/passing/tests/mod_type_subst.ml index 7114c58323..1b116a3149 100644 --- a/test/passing/tests/mod_type_subst.ml +++ b/test/passing/tests/mod_type_subst.ml @@ -13,19 +13,17 @@ module type t' = t with module type x = x module type t'' = t with module type x := x -module type t3 = - t - with - module type x = sig - type t - end - -module type t4 = - t - with - module type x := sig - type t - end +module type t3 = t + with + module type x = sig + type t + end + +module type t4 = t + with + module type x := sig + type t + end (** nested *) @@ -83,11 +81,11 @@ module type u = sig module M : t end -module type r = - u with type x = int and type y = float and module type t = base +module type r = u + with type x = int and type y = float and module type t = base -module type r = - u with type x = int and type y = float and module type t := base +module type r = u + with type x = int and type y = float and module type t := base (** First class module types require an identity *) diff --git a/test/passing/tests/module.ml b/test/passing/tests/module.ml index 778a4ff22b..ecb3bd2f47 100644 --- a/test/passing/tests/module.ml +++ b/test/passing/tests/module.ml @@ -39,8 +39,7 @@ end module O : sig type t end -with type t := t - and type s := s = struct +with type t := t and type s := s = struct let () = () end @@ -56,10 +55,10 @@ let x : (module S) = (module struct end) let x = (module struct end : S) -module rec A : (sig +module rec A : sig type t end -with type t = int) = struct +with type t = int = struct type t = int end diff --git a/test/passing/tests/module_attributes.ml.ref b/test/passing/tests/module_attributes.ml.ref index 11cac6e12c..839c4050d8 100644 --- a/test/passing/tests/module_attributes.ml.ref +++ b/test/passing/tests/module_attributes.ml.ref @@ -24,19 +24,14 @@ include ( include ( List : - (module type of Foo - with module A := A - [@warning "-3"] [@warning "-3"] - with module B := B - [@warning "-3"]) ) + (module type of Foo with module A := A [@warning "-3"] [@warning "-3"] + with module B := B [@warning "-3"]) ) include ( List : (module type of Pervasives - with module A := A - [@warning "-3"] [@warning "-3"] - with module B := B - [@warning "-3"] [@warning "-3"]) ) + with module A := A [@warning "-3"] [@warning "-3"] + with module B := B [@warning "-3"] [@warning "-3"]) ) [@warning "-3"] module My_module_name : sig end = struct end diff --git a/test/passing/tests/module_type.ml b/test/passing/tests/module_type.ml index a13548bcb8..e8f5344077 100644 --- a/test/passing/tests/module_type.ml +++ b/test/passing/tests/module_type.ml @@ -24,43 +24,31 @@ module type BAR = sig and B : FOO end -module type M = - module type of M - with module A := A - (*test*) - and module A = A - (*test*) - and module A = A - with module A = A - (*test*) - with module A = A - -module U : - S - with type ttttttttt = int - and type uuuuuuuu = int - and type vvvvvvvvvvv = int = struct end - -module U : - S - with type ttttttttt = int - and type uuuuuuu = int - with type vvvvvvvvv = int = struct end - -module U : - S - with type Command.t = - [ `Halt - | `Unknown - | `Error of string - | `Config of (string * string) list - | `Format of string ] - and type Command.t = - [ `Halt - | `Unknown - | `Error of string - | `Config of (string * string) list - | `Format of string ] = struct end +module type M = module type of M + with module A := A (*test*) and module A = A (*test*) and module A = A + with module A = A (*test*) with module A = A + +module U : S + with type ttttttttt = int + and type uuuuuuuu = int + and type vvvvvvvvvvv = int = struct end + +module U : S with type ttttttttt = int and type uuuuuuu = int + with type vvvvvvvvv = int = struct end + +module U : S + with type Command.t = + [ `Halt + | `Unknown + | `Error of string + | `Config of (string * string) list + | `Format of string ] + and type Command.t = + [ `Halt + | `Unknown + | `Error of string + | `Config of (string * string) list + | `Format of string ] = struct end module U = (val S : S with type t = int and type u = int) @@ -73,15 +61,13 @@ module type S = sig end module type S' = functor - (A : A) - (B : sig - type t - end) - (Cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc - : sig - type t - end) - -> S with type t = B.t + (A : A) + (B : sig + type t + end) + (Cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc : sig + type t + end) -> S with type t = B.t module M : sig include (* foo *) module type of K @@ -90,7 +76,8 @@ module M : sig Fooooooooooooooooooooooooooo (Foooooooooo.Foo) (Fooooooooooooo) (Fooooooooooooo) - include (* fooooooooo *) module type of + include (* fooooooooo *) + module type of Fooooooooooooooooooooooooooo (Foooooooooo.Foo) (Fooooooooooooo) (Fooooooooooooo) end = struct end diff --git a/test/passing/tests/module_type.ml.err b/test/passing/tests/module_type.ml.err new file mode 100644 index 0000000000..adbc84ff18 --- /dev/null +++ b/test/passing/tests/module_type.ml.err @@ -0,0 +1 @@ +Warning: tests/module_type.ml:67 exceeds the margin diff --git a/test/passing/tests/open-closing-on-separate-line.ml.ref b/test/passing/tests/open-closing-on-separate-line.ml.ref index 4ccac15195..77604369f8 100644 --- a/test/passing/tests/open-closing-on-separate-line.ml.ref +++ b/test/passing/tests/open-closing-on-separate-line.ml.ref @@ -210,17 +210,13 @@ open struct end open - functor - (A : S) - -> + functor (A : S) -> struct type t end open - functor - (_ : S) - -> + functor (_ : S) -> struct type t end @@ -240,16 +236,12 @@ let _ = type t end in let open - functor - (A : S) - -> + functor (A : S) -> struct type t end in let open - functor - (_ : S) - -> + functor (_ : S) -> struct type t end in @@ -267,17 +259,13 @@ open struct end [@@attr] open - functor - (A : S) - -> + functor (A : S) -> struct type t end [@@attr] open - functor - (_ : S) - -> + functor (_ : S) -> struct type t end [@@attr] diff --git a/test/passing/tests/open.ml.ref b/test/passing/tests/open.ml.ref index 26e0737169..32ee78ac4b 100644 --- a/test/passing/tests/open.ml.ref +++ b/test/passing/tests/open.ml.ref @@ -202,17 +202,13 @@ open struct end open - functor - (A : S) - -> + functor (A : S) -> struct type t end open - functor - (_ : S) - -> + functor (_ : S) -> struct type t end @@ -232,16 +228,12 @@ let _ = type t end in let open - functor - (A : S) - -> + functor (A : S) -> struct type t end in let open - functor - (_ : S) - -> + functor (_ : S) -> struct type t end in @@ -259,17 +251,13 @@ open struct end [@@attr] open - functor - (A : S) - -> + functor (A : S) -> struct type t end [@@attr] open - functor - (_ : S) - -> + functor (_ : S) -> struct type t end [@@attr] diff --git a/test/passing/tests/shortcut_ext_attr.ml b/test/passing/tests/shortcut_ext_attr.ml index ba22919dad..81e198b967 100644 --- a/test/passing/tests/shortcut_ext_attr.ml +++ b/test/passing/tests/shortcut_ext_attr.ml @@ -78,11 +78,8 @@ type t = [%foo: ((module M)[@foo])] module M = (functor [@foo] (M : S) -> (val x) [@foo] (struct end [@foo])) (* Module type expression *) -module type S = functor [@foo1] - (M : S) - -> functor - (_ : (module type of M) [@foo2]) - -> sig end [@foo3] +module type S = functor [@foo1] (M : S) -> + functor (_ : (module type of M) [@foo2]) -> sig end [@foo3] (* Structure items *) let%foo[@foo] x = 4 diff --git a/test/passing/tests/source.ml b/test/passing/tests/source.ml index 7d764f9e4d..90f0fe1f92 100644 --- a/test/passing/tests/source.ml +++ b/test/passing/tests/source.ml @@ -7423,3 +7423,6 @@ let () = | _ -> ()) | _ -> () ;; + +(* Long-form attribute on a functor module type expr. *) +module type S = functor (A : B) -> (functor (C : D) -> S') [@foo] diff --git a/test/passing/tests/source.ml.err b/test/passing/tests/source.ml.err index 50f7e55a5d..a1caa61d09 100644 --- a/test/passing/tests/source.ml.err +++ b/test/passing/tests/source.ml.err @@ -1,2 +1,3 @@ -Warning: tests/source.ml:702 exceeds the margin -Warning: tests/source.ml:2318 exceeds the margin +Warning: tests/source.ml:697 exceeds the margin +Warning: tests/source.ml:2313 exceeds the margin +Warning: tests/source.ml:8019 exceeds the margin diff --git a/test/passing/tests/source.ml.ref b/test/passing/tests/source.ml.ref index 29234fddd3..d28b0e632a 100644 --- a/test/passing/tests/source.ml.ref +++ b/test/passing/tests/source.ml.ref @@ -14,9 +14,7 @@ end [@foo] [@@foo] module type S = sig - include - ((module type of M [@foo]) [@foo] with type t := M.t [@foo]) - [@@foo] + include (module type of M [@foo]) [@foo] with type t := M.t [@foo] [@@foo] [@@@foo] end [@foo] @@ -182,11 +180,8 @@ type t = [%foo: ((module M)[@foo])] module M = (functor [@foo] (M : S) -> (val x) [@foo] (struct end [@foo])) (* Module type expression *) -module type S = functor [@foo] - (M : S) - -> functor - (_ : (module type of M) [@foo]) - -> sig end [@foo] +module type S = functor [@foo] (M : S) -> + functor (_ : (module type of M) [@foo]) -> sig end [@foo] module type S = functor (_ : S) (_ : S) -> S @@ -2888,11 +2883,9 @@ type (_, _) eq = Refl : ('a, 'a) eq let magic : 'a 'b. 'a -> 'b = fun (type a b) (x : a) -> let module M = - (functor - (T : sig - type 'a t - end) - -> + (functor (T : sig + type 'a t + end) -> struct let f (Refl : (a T.t, b T.t) eq) = (x :> b) end) @@ -4503,8 +4496,8 @@ let flag = ref false module F (S : sig - module type T - end) + module type T + end) (A : S.T) (B : S.T) = struct @@ -4555,8 +4548,8 @@ module type PR6513 = sig type uri end - module Make : functor (Html5 : T with type 'a wrap = 'a) -> - S with type u = < foo: Html5.uri > + module Make : functor (Html5 : T with type 'a wrap = 'a) -> S + with type u = < foo: Html5.uri > end (* Requires -package tyxml module type PR6513_orig = sig module type S = sig @@ -5772,7 +5765,7 @@ module Y = functor (X : sig end) (Y : sig end) (Z : sig end) -> struct end module Z = functor (_ : sig end) (_ : sig end) (_ : sig end) -> struct end module GZ : functor (X : sig end) () (Z : sig end) -> sig end = -functor (X : sig end) () (Z : sig end) -> struct end + functor (X : sig end) () (Z : sig end) -> struct end module F (X : sig end) = struct type t = int @@ -6838,9 +6831,9 @@ module PR_4261 = struct type t = D.t end - module rec U : (T with module D = U') = U + module rec U : T with module D = U' = U - and U' : (S with type t = U'.t) = U + and U' : S with type t = U'.t = U end (* Bad - PR 4512 *) @@ -6848,7 +6841,7 @@ module type S' = sig type t = int end -module rec M : (S' with type t = M.t) = struct +module rec M : S' with type t = M.t = struct type t = M.t end @@ -6916,9 +6909,8 @@ end type 'a tree = E | N of 'a tree * 'a * 'a tree module Bootstrap2 - (MakeDiet : functor - (X : ORD) - -> SET with type t = X.t tree and type elt = X.t) : + (MakeDiet : functor (X : ORD) -> SET + with type t = X.t tree and type elt = X.t) : SET with type elt = int = struct type elt = int @@ -6941,7 +6933,7 @@ module Bootstrap2 | D (_, d, _) -> Diet.iter (iter f) d end - and Diet : (SET with type t = Elt.t tree and type elt = Elt.t) = + and Diet : SET with type t = Elt.t tree and type elt = Elt.t = MakeDiet (Elt) type t = Diet.t @@ -7025,7 +7017,7 @@ module PR_4557 = struct let compare x y = 0 end - and ModSet : (Set.S with type elt = Mod.t) = Set.Make (Mod) + and ModSet : Set.S with type elt = Mod.t = Set.Make (Mod) end end @@ -7059,7 +7051,7 @@ module F (X : Set.OrderedType) = struct let compare x y = 0 end - and ModSet : (Set.S with type elt = Mod.t) = Set.Make (Mod) + and ModSet : Set.S with type elt = Mod.t = Set.Make (Mod) end (* Tests for recursive modules *) @@ -7085,7 +7077,7 @@ end = struct | Node s, Node t -> ASet.compare s t end -and ASet : (Set.S with type elt = A.t) = Set.Make (A) +and ASet : Set.S with type elt = A.t = Set.Make (A) let _ = let x = A.Node (ASet.add (A.Leaf 3) (ASet.singleton (A.Leaf 2))) in @@ -7342,7 +7334,8 @@ end module Bootstrap (MakeH : functor (Element : ORDERED) -> HEAP with module Elem = Element) - (Element : ORDERED) : HEAP with module Elem = Element = struct + (Element : ORDERED) : + HEAP with module Elem = Element = struct module Elem = Element module rec BE : sig @@ -7378,7 +7371,7 @@ module Bootstrap | E, E -> false end - and PrimH : (HEAP with type Elem.t = BE.t) = MakeH (BE) + and PrimH : HEAP with type Elem.t = BE.t = MakeH (BE) type heap = BE.t @@ -8024,8 +8017,8 @@ module type BARECODE = sig end module USERCODE (X : TYPEVIEW) = struct - module type F = functor (C : CORE with type V.usert = X.combined) -> - BARECODE with type state := C.V.state + module type F = functor (C : CORE with type V.usert = X.combined) -> BARECODE + with type state := C.V.state end module Weapon = struct @@ -9189,3 +9182,6 @@ let eradicate_meta_class_is_nullsafe = let () = match () with _ -> ( fun _ : _ -> match () with _ -> () ) | _ -> () + +(* Long-form attribute on a functor module type expr. *) +module type S = functor (A : B) -> functor [@foo] (C : D) -> S' diff --git a/test/passing/tests/wrapping_functor_args.ml b/test/passing/tests/wrapping_functor_args.ml index 276274dbab..62abde5ae7 100644 --- a/test/passing/tests/wrapping_functor_args.ml +++ b/test/passing/tests/wrapping_functor_args.ml @@ -15,26 +15,23 @@ module OauthClient = module F1 (G : functor (_ : T) -> T) (A : sig - val x : int - end) = + val x : int + end) = struct end module F2 - (G : functor - (_ : T) - -> - T_________________________________________________________________________) + (G : functor (_ : T) -> + T_________________________________________________________________________) (A : sig - val x : int - end) = + val x : int + end) = struct end module F3 (G : functor - (_ : T____________________________________________) - (_ : T____________________________________________) - -> T) + (_ : T____________________________________________) + (_ : T____________________________________________) -> T) (A : sig - val x : int - end) = + val x : int + end) = struct end diff --git a/test/passing/tests/wrapping_functor_args.ml.err b/test/passing/tests/wrapping_functor_args.ml.err index 6ccd438d50..a42c68e69d 100644 --- a/test/passing/tests/wrapping_functor_args.ml.err +++ b/test/passing/tests/wrapping_functor_args.ml.err @@ -1 +1 @@ -Warning: tests/wrapping_functor_args.ml:25 exceeds the margin +Warning: tests/wrapping_functor_args.ml:23 exceeds the margin