From 75baecdb6cd567b7cf5d34f4d6d294e4d9b1feaa Mon Sep 17 00:00:00 2001 From: EmileTrotignon Date: Fri, 6 Jun 2025 18:58:09 +0200 Subject: [PATCH 1/2] makes exp-grouping=preserve the default --- lib/Conf.ml | 12 +- test/cli/print_config.t | 6 +- test/passing/refs.default/effects.ml.ref | 8 +- test/passing/refs.default/ifand.ml.ref | 4 +- test/passing/refs.default/js_begin.ml.ref | 7 +- test/passing/refs.default/js_pattern.ml.ref | 5 +- .../refs.default/reformat_string.ml.ref | 13 +- .../refs.default/sequence-preserve.ml.ref | 8 +- test/passing/refs.default/sequence.ml.ref | 8 +- test/passing/refs.default/source.ml.err | 4 +- test/passing/refs.default/source.ml.ref | 129 +++++++++------ test/passing/refs.ocamlformat/effects.ml.ref | 8 +- test/passing/refs.ocamlformat/ifand.ml.ref | 5 +- test/passing/refs.ocamlformat/js_begin.ml.ref | 6 +- .../refs.ocamlformat/js_pattern.ml.ref | 4 +- .../refs.ocamlformat/reformat_string.ml.ref | 12 +- .../refs.ocamlformat/sequence-preserve.ml.ref | 8 +- test/passing/refs.ocamlformat/sequence.ml.ref | 8 +- test/passing/refs.ocamlformat/source.ml.err | 6 +- test/passing/refs.ocamlformat/source.ml.ref | 152 ++++++++++-------- test/unit/test_fmt_ast.ml | 2 +- 21 files changed, 255 insertions(+), 160 deletions(-) diff --git a/lib/Conf.ml b/lib/Conf.ml index c1ab15d13f..def15f781d 100644 --- a/lib/Conf.ml +++ b/lib/Conf.ml @@ -70,7 +70,7 @@ let conventional_profile from = ; doc_comments_padding= elt 2 ; doc_comments_tag_only= elt `Default ; dock_collection_brackets= elt true - ; exp_grouping= elt `Parens + ; exp_grouping= elt `Preserve ; extension_indent= elt 2 ; field_space= elt `Loose ; function_indent= elt 2 @@ -139,7 +139,7 @@ let ocamlformat_profile from = ; doc_comments_padding= elt 2 ; doc_comments_tag_only= elt `Default ; dock_collection_brackets= elt false - ; exp_grouping= elt `Parens + ; exp_grouping= elt `Preserve ; extension_indent= elt 2 ; field_space= elt `Tight ; function_indent= elt 2 @@ -742,11 +742,11 @@ module Formatting = struct let doc = "Style of expression grouping." in let names = ["exp-grouping"] in let all = - [ Decl.Value.make ~name:"parens" `Parens - "$(b,parens) groups expressions using parentheses." - ; Decl.Value.make ~name:"preserve" `Preserve + [ Decl.Value.make ~name:"preserve" `Preserve "$(b,preserve) preserves the original grouping syntax \ - (parentheses or $(i,begin)/$(i,end))." ] + (parentheses or $(i,begin)/$(i,end))." + ; Decl.Value.make ~name:"parens" `Parens + "$(b,parens) groups expressions using parentheses." ] in Decl.choice ~names ~all ~default ~doc ~kind ~allow_inline:false (fun conf elt -> update conf ~f:(fun f -> {f with exp_grouping= elt})) diff --git a/test/cli/print_config.t b/test/cli/print_config.t index 1f608ef9d9..def057d484 100644 --- a/test/cli/print_config.t +++ b/test/cli/print_config.t @@ -42,7 +42,7 @@ No redundant values: doc-comments-padding=2 (profile conventional (file .ocamlformat:1)) doc-comments-tag-only=default (profile conventional (file .ocamlformat:1)) dock-collection-brackets=true (profile conventional (file .ocamlformat:1)) - exp-grouping=parens (profile conventional (file .ocamlformat:1)) + exp-grouping=preserve (profile conventional (file .ocamlformat:1)) extension-indent=2 (profile conventional (file .ocamlformat:1)) field-space=tight (file .ocamlformat:2) function-indent=2 (profile conventional (file .ocamlformat:1)) @@ -121,7 +121,7 @@ Redundant values from the conventional profile: doc-comments-padding=2 (profile conventional (file .ocamlformat:1)) doc-comments-tag-only=default (profile conventional (file .ocamlformat:1)) dock-collection-brackets=true (profile conventional (file .ocamlformat:1)) - exp-grouping=parens (profile conventional (file .ocamlformat:1)) + exp-grouping=preserve (profile conventional (file .ocamlformat:1)) extension-indent=2 (profile conventional (file .ocamlformat:1)) field-space=loose (profile conventional (file .ocamlformat:1)) function-indent=2 (profile conventional (file .ocamlformat:1)) @@ -200,7 +200,7 @@ Redundant values from the ocamlformat profile: doc-comments-padding=2 (profile ocamlformat (file .ocamlformat:1)) doc-comments-tag-only=default (profile ocamlformat (file .ocamlformat:1)) dock-collection-brackets=false (profile ocamlformat (file .ocamlformat:1)) - exp-grouping=parens (profile ocamlformat (file .ocamlformat:1)) + exp-grouping=preserve (profile ocamlformat (file .ocamlformat:1)) extension-indent=2 (profile ocamlformat (file .ocamlformat:1)) field-space=tight (profile ocamlformat (file .ocamlformat:1)) function-indent=2 (profile ocamlformat (file .ocamlformat:1)) diff --git a/test/passing/refs.default/effects.ml.ref b/test/passing/refs.default/effects.ml.ref index 6754d37966..a4b3fda1ca 100644 --- a/test/passing/refs.default/effects.ml.ref +++ b/test/passing/refs.default/effects.ml.ref @@ -16,9 +16,10 @@ let run (main : unit -> unit) : unit = in let dequeue () = if Queue.is_empty run_q then () (* done *) - else + else begin let task = Queue.pop run_q in task () + end in let rec spawn (f : unit -> unit) : unit = match f () with @@ -32,7 +33,7 @@ let run (main : unit -> unit) : unit = | effect Fork f, k -> enqueue k (); spawn f - | effect Xchg n, k -> ( + | effect Xchg n, k -> begin match !exchanger with | Some (n', k') -> exchanger := None; @@ -40,7 +41,8 @@ let run (main : unit -> unit) : unit = continue k n' | None -> exchanger := Some (n, k); - dequeue ()) + dequeue () + end in spawn main diff --git a/test/passing/refs.default/ifand.ml.ref b/test/passing/refs.default/ifand.ml.ref index 1424110fdb..3d1c0c0816 100644 --- a/test/passing/refs.default/ifand.ml.ref +++ b/test/passing/refs.default/ifand.ml.ref @@ -1,2 +1,4 @@ let _ = if cond1 && cond2 then _ -let _ = function _ when x = 2 && y = 3 -> if a = b || (b = c && c = d) then _ + +let _ = function + | _ when x = 2 && y = 3 -> begin if a = b || (b = c && c = d) then _ end diff --git a/test/passing/refs.default/js_begin.ml.ref b/test/passing/refs.default/js_begin.ml.ref index 43cb1a7e09..36e2430e79 100644 --- a/test/passing/refs.default/js_begin.ml.ref +++ b/test/passing/refs.default/js_begin.ml.ref @@ -1,11 +1,14 @@ let f = function - | zoo -> + | zoo -> begin foo; bar + end let g = function | zoo -> foo; bar -let () = match foo with Bar -> snoo +let () = + begin match foo with Bar -> snoo + end diff --git a/test/passing/refs.default/js_pattern.ml.ref b/test/passing/refs.default/js_pattern.ml.ref index c59b7c9b06..31b486bc70 100644 --- a/test/passing/refs.default/js_pattern.ml.ref +++ b/test/passing/refs.default/js_pattern.ml.ref @@ -2,7 +2,10 @@ let f = function _ -> 0 let f x = match x with _ -> 0 let f = function _ -> 0 let f x = match x with _ -> 0 -let f x = match x with _ -> 0 + +let f x = + begin match x with _ -> 0 + end let check_price t = function | { Exec.trade_at_settlement = None | Some false } -> () diff --git a/test/passing/refs.default/reformat_string.ml.ref b/test/passing/refs.default/reformat_string.ml.ref index edef79e0e9..8a4c2d022b 100644 --- a/test/passing/refs.default/reformat_string.ml.ref +++ b/test/passing/refs.default/reformat_string.ml.ref @@ -2,8 +2,17 @@ let _ = 'a' let _ = 'a' let _ = (* test *) "asd" let _ = "asd" -let _ = (* te""st *) "asd" -let _ = "asd" + +let _ = + begin + (* te""st *) "asd" + end + +let _ = + begin + "asd" + end + let _ = 'a' let _ = 'a' let _ = function 'a' .. 'z' -> () diff --git a/test/passing/refs.default/sequence-preserve.ml.ref b/test/passing/refs.default/sequence-preserve.ml.ref index 62dcb05c1b..044a555db9 100644 --- a/test/passing/refs.default/sequence-preserve.ml.ref +++ b/test/passing/refs.default/sequence-preserve.ml.ref @@ -1,8 +1,10 @@ let read_traces filename = let ic = open_in_bin filename in - read_hashtable ~t:[%t: contracts_trace] 0 40 ic tbl1; - read_hashtable ~t:[%t: variables_trace] 40 70 ic tbl2; - read_hashtable ~t:[%t: expressions_trace] 70 100 ic tbl3; + begin + read_hashtable ~t:[%t: contracts_trace] 0 40 ic tbl1; + read_hashtable ~t:[%t: variables_trace] 40 70 ic tbl2; + read_hashtable ~t:[%t: expressions_trace] 70 100 ic tbl3 + end; close_in ic let foo x y = diff --git a/test/passing/refs.default/sequence.ml.ref b/test/passing/refs.default/sequence.ml.ref index b84db9cf51..790e1b9b72 100644 --- a/test/passing/refs.default/sequence.ml.ref +++ b/test/passing/refs.default/sequence.ml.ref @@ -1,8 +1,10 @@ let read_traces filename = let ic = open_in_bin filename in - read_hashtable ~t:[%t: contracts_trace] 0 40 ic tbl1; - read_hashtable ~t:[%t: variables_trace] 40 70 ic tbl2; - read_hashtable ~t:[%t: expressions_trace] 70 100 ic tbl3; + begin + read_hashtable ~t:[%t: contracts_trace] 0 40 ic tbl1; + read_hashtable ~t:[%t: variables_trace] 40 70 ic tbl2; + read_hashtable ~t:[%t: expressions_trace] 70 100 ic tbl3 + end; close_in ic let foo x y = diff --git a/test/passing/refs.default/source.ml.err b/test/passing/refs.default/source.ml.err index eb0c35513d..70d6121ff4 100644 --- a/test/passing/refs.default/source.ml.err +++ b/test/passing/refs.default/source.ml.err @@ -1,4 +1,4 @@ Warning: source.ml:919 exceeds the margin Warning: source.ml:994 exceeds the margin -Warning: source.ml:6616 exceeds the margin -Warning: source.ml:7074 exceeds the margin +Warning: source.ml:6640 exceeds the margin +Warning: source.ml:7099 exceeds the margin diff --git a/test/passing/refs.default/source.ml.ref b/test/passing/refs.default/source.ml.ref index 559d3e27c4..46ac2f09dd 100644 --- a/test/passing/refs.default/source.ml.ref +++ b/test/passing/refs.default/source.ml.ref @@ -1096,14 +1096,16 @@ let rec get_case : type a b e. = fun sel cases -> match cases with - | (name, TCnoarg sel') :: rem -> ( + | (name, TCnoarg sel') :: rem -> begin match eq_sel sel sel' with | None -> get_case sel rem - | Some Eq -> (name, None)) - | (name, TCarg (sel', ty)) :: rem -> ( + | Some Eq -> (name, None) + end + | (name, TCarg (sel', ty)) :: rem -> begin match eq_sel sel sel' with | None -> get_case sel rem - | Some Eq -> (name, Some ty)) + | Some Eq -> (name, Some ty) + end | [] -> raise Not_found (* Untyped representation of values *) @@ -1147,13 +1149,14 @@ let rec devariantize : type t e. e ty_env -> (t, e) ty -> variant -> t = | Var, _ -> ( match e with Econs (t, e') -> devariantize e' t v) | Conv (s, proj, inj, t), VConv (s', v) when s = s' -> inj (devariantize e t v) - | Sum ops, VSum (tag, a) -> ( + | Sum ops, VSum (tag, a) -> begin try match (List.assoc tag ops.sum_cases, a) with | TCarg (sel, t), Some a -> ops.sum_inj (sel, devariantize e t a) | TCnoarg sel, None -> ops.sum_inj (sel, Noarg) | _ -> raise VariantMismatch - with Not_found -> raise VariantMismatch) + with Not_found -> raise VariantMismatch + end | _ -> raise VariantMismatch (* First attempt: represent 1-constructor variants using Conv *) @@ -1483,8 +1486,9 @@ let rec sameNat : type a b. a nat -> b nat -> (a, b) equal option = fun a b -> match (a, b) with | NZ, NZ -> Some Eq - | NS a', NS b' -> ( - match sameNat a' b' with Some Eq -> Some Eq | None -> None) + | NS a', NS b' -> begin + match sameNat a' b' with Some Eq -> Some Eq | None -> None + end | _ -> None (* Extra: associativity of addition *) @@ -1627,9 +1631,9 @@ let rec ins : type n. int -> n avl -> (n avl, n succ avl) sum = fun x t -> match t with | Leaf -> Inr (Node (Same, Leaf, x, Leaf)) - | Node (bal, a, y, b) -> ( + | Node (bal, a, y, b) -> if x = y then Inl t - else if x < y then + else if x < y then begin match ins x a with | Inl a -> Inl (Node (bal, a, y, b)) | Inr a -> ( @@ -1637,14 +1641,16 @@ let rec ins : type n. int -> n avl -> (n avl, n succ avl) sum = | Less -> Inl (Node (Same, a, y, b)) | Same -> Inr (Node (More, a, y, b)) | More -> rotr a y b) - else + end + else begin match ins x b with | Inl b -> Inl (Node (bal, a, y, b) : n avl) | Inr b -> ( match bal with | More -> Inl (Node (Same, a, y, b) : n avl) | Same -> Inr (Node (Less, a, y, b) : n succ avl) - | Less -> rotl a y b)) + | Less -> rotl a y b) + end let insert x (Avl t) = match ins x t with Inl t -> Avl t | Inr t -> Avl t @@ -1669,12 +1675,13 @@ let rec del : type n. int -> n avl -> n avl_del = fun y t -> match t with | Leaf -> Dsame Leaf - | Node (bal, l, x, r) -> ( - if x = y then + | Node (bal, l, x, r) -> + if x = y then begin match r with - | Leaf -> ( - match bal with Same -> Ddecr (Eq, l) | More -> Ddecr (Eq, l)) - | Node _ -> ( + | Leaf -> begin + match bal with Same -> Ddecr (Eq, l) | More -> Ddecr (Eq, l) + end + | Node _ -> begin match (bal, del_min r) with | _, (z, Inr r) -> Dsame (Node (bal, l, z, r)) | Same, (z, Inl r) -> Dsame (Node (More, l, z, r)) @@ -1682,29 +1689,35 @@ let rec del : type n. int -> n avl -> n avl_del = | More, (z, Inl r) -> ( match rotr l z r with | Inl t -> Ddecr (Eq, t) - | Inr t -> Dsame t)) - else if y < x then + | Inr t -> Dsame t) + end + end + else if y < x then begin match del y l with | Dsame l -> Dsame (Node (bal, l, x, r)) - | Ddecr (Eq, l) -> ( + | Ddecr (Eq, l) -> begin match bal with | Same -> Dsame (Node (Less, l, x, r)) | More -> Ddecr (Eq, Node (Same, l, x, r)) | Less -> ( match rotl l x r with | Inl t -> Ddecr (Eq, t) - | Inr t -> Dsame t)) - else + | Inr t -> Dsame t) + end + end + else begin match del y r with | Dsame r -> Dsame (Node (bal, l, x, r)) - | Ddecr (Eq, r) -> ( + | Ddecr (Eq, r) -> begin match bal with | Same -> Dsame (Node (More, l, x, r)) | Less -> Ddecr (Eq, Node (Same, l, x, r)) | More -> ( match rotr l x r with | Inl t -> Ddecr (Eq, t) - | Inr t -> Dsame t))) + | Inr t -> Dsame t) + end + end let delete x (Avl t) = match del x t with Dsame t -> Avl t | Ddecr (_, t) -> Avl t @@ -1821,16 +1834,18 @@ let rec rep_equal : type a b. a rep -> b rep -> (a, b) equal option = match (ra, rb) with | Rint, Rint -> Some Eq | Rbool, Rbool -> Some Eq - | Rpair (a1, a2), Rpair (b1, b2) -> ( + | Rpair (a1, a2), Rpair (b1, b2) -> begin match rep_equal a1 b1 with | None -> None | Some Eq -> ( - match rep_equal a2 b2 with None -> None | Some Eq -> Some Eq)) - | Rfun (a1, a2), Rfun (b1, b2) -> ( + match rep_equal a2 b2 with None -> None | Some Eq -> Some Eq) + end + | Rfun (a1, a2), Rfun (b1, b2) -> begin match rep_equal a1 b1 with | None -> None | Some Eq -> ( - match rep_equal a2 b2 with None -> None | Some Eq -> Some Eq)) + match rep_equal a2 b2 with None -> None | Some Eq -> Some Eq) + end | _ -> None type assoc = Assoc : string * 'a rep * 'a -> assoc @@ -1928,10 +1943,11 @@ let rec compare : type a b. a rep -> b rep -> (string, (a, b) equal) sum = fun a b -> match (a, b) with | I, I -> Inr Eq - | Ar (x, y), Ar (s, t) -> ( + | Ar (x, y), Ar (s, t) -> begin match compare x s with | Inl _ as e -> e - | Inr Eq -> ( match compare y t with Inl _ as e -> e | Inr Eq as e -> e)) + | Inr Eq -> ( match compare y t with Inl _ as e -> e | Inr Eq as e -> e) + end | I, Ar _ -> Inl "I <> Ar _" | Ar _, I -> Inl "Ar _ <> I" @@ -1962,7 +1978,7 @@ let rec tc : type n e. n nat -> e ctx -> term -> e checked = fun n ctx t -> match t with | V s -> lookup s ctx - | Ap (f, x) -> ( + | Ap (f, x) -> begin match tc n ctx f with | Cerror _ as e -> e | Cok (f', ft) -> ( @@ -1970,15 +1986,18 @@ let rec tc : type n e. n nat -> e ctx -> term -> e checked = | Cerror _ as e -> e | Cok (x', xt) -> ( match ft with - | Ar (a, b) -> ( + | Ar (a, b) -> begin match compare a xt with | Inl s -> Cerror s - | Inr Eq -> Cok (App (f', x'), b)) - | _ -> Cerror "Non fun in Ap"))) - | Ab (s, t, body) -> ( + | Inr Eq -> Cok (App (f', x'), b) + end + | _ -> Cerror "Non fun in Ap")) + end + | Ab (s, t, body) -> begin match tc (NS n) (Ccons (n, s, t, ctx)) body with | Cerror _ as e -> e - | Cok (body', et) -> Cok (Abs (n, body'), Ar (t, et))) + | Cok (body', et) -> Cok (Abs (n, body'), Ar (t, et)) + end | C m -> Cok (Const m, I) let ctx0 = @@ -2058,9 +2077,10 @@ let rec rule : type a b. (pval, closed, (a, b) tarr) lam -> (pval, closed, a) lam -> b rlam = fun v1 v2 -> match (v1, v2) with - | Lam (x, body), v -> ( + | Lam (x, body), v -> begin match subst body (Bind (x, v, Id)) with - | Ex term -> ( match mode term with Pexp -> Inl term | Pval -> Inr term)) + | Ex term -> ( match mode term with Pexp -> Inl term | Pval -> Inr term) + end | Const (IntTo b, f), Const (IntR, x) -> Inr (Const (b, f x)) let rec onestep : type m t. (m, closed, t) lam -> t rlam = function @@ -2068,14 +2088,16 @@ let rec onestep : type m t. (m, closed, t) lam -> t rlam = function | Const (r, v) -> Inr (Const (r, v)) | App (e1, e2) -> ( match (mode e1, mode e2) with - | Pexp, _ -> ( + | Pexp, _ -> begin match onestep e1 with | Inl e -> Inl (App (e, e2)) - | Inr v -> Inl (App (v, e2))) - | Pval, Pexp -> ( + | Inr v -> Inl (App (v, e2)) + end + | Pval, Pexp -> begin match onestep e2 with | Inl e -> Inl (App (e1, e)) - | Inr v -> Inl (App (e1, v))) + | Inr v -> Inl (App (e1, v)) + end | Pval, Pval -> rule e1 e2) type ('env, 'a) var = @@ -5765,10 +5787,12 @@ module UText = struct let set_buf s i u = let n = UChar.uint_code u in - s.![i] <- Char.chr (n lsr 24); - s.![i + 1] <- Char.chr ((n lsr 16) lor 0xff); - s.![i + 2] <- Char.chr ((n lsr 8) lor 0xff); - s.![i + 3] <- Char.chr (n lor 0xff) + begin + s.![i] <- Char.chr (n lsr 24); + s.![i + 1] <- Char.chr ((n lsr 16) lor 0xff); + s.![i + 2] <- Char.chr ((n lsr 8) lor 0xff); + s.![i + 3] <- Char.chr (n lor 0xff) + end let init_buf buf pos init = if init#len = 0 then () @@ -7053,14 +7077,15 @@ module Bootstrap let deleteMin = function | BE.E -> raise Not_found - | BE.H (x, p) -> ( + | BE.H (x, p) -> if PrimH.isEmpty p then BE.E - else + else begin match PrimH.findMin p with | BE.H (y, p1) -> let p2 = PrimH.deleteMin p in BE.H (y, PrimH.merge p1 p2) - | BE.E -> assert false) + | BE.E -> assert false + end end module LeftistHeap (Element : ORDERED) : HEAP with module Elem = Element = @@ -8485,19 +8510,21 @@ let g = function let h = fun ?l:(p = 1) ?y:u ?(x = 3) -> 2 let _ = function - | a, s, ba1, ba2, ba3, bg -> + | a, s, ba1, ba2, ba3, bg -> begin ignore (Array.get x 1 + Array.get [||] 0 + Array.get [| 1 |] 1 + Array.get [| 1; 2 |] 2); ignore [ String.get s 1; String.get "" 2; String.get "123" 3 ]; ignore (ba1.{0} + ba2.{1, 2} + ba3.{3, 4, 5}) ignore bg.{1, 2, 3, 4} - | b, s, ba1, ba2, ba3, bg -> + end + | b, s, ba1, ba2, ba3, bg -> begin y.(0) <- 1; s.![1] <- 'c'; ba1.{1} <- 2; ba2.{1, 2} <- 3; ba3.{1, 2, 3} <- 4; bg.{1, 2, 3, 4, 5} <- 0 + end let f (type t) () = let exception F of t in diff --git a/test/passing/refs.ocamlformat/effects.ml.ref b/test/passing/refs.ocamlformat/effects.ml.ref index 5936d52a45..2bde4e3d64 100644 --- a/test/passing/refs.ocamlformat/effects.ml.ref +++ b/test/passing/refs.ocamlformat/effects.ml.ref @@ -18,9 +18,10 @@ let run (main : unit -> unit) : unit = in let dequeue () = if Queue.is_empty run_q then () (* done *) - else + else begin let task = Queue.pop run_q in task () + end in let rec spawn (f : unit -> unit) : unit = match f () with @@ -33,7 +34,7 @@ let run (main : unit -> unit) : unit = enqueue k () ; dequeue () | effect Fork f, k -> enqueue k () ; spawn f - | effect Xchg n, k -> ( + | effect Xchg n, k -> begin match !exchanger with | Some (n', k') -> exchanger := None ; @@ -41,7 +42,8 @@ let run (main : unit -> unit) : unit = continue k n' | None -> exchanger := Some (n, k) ; - dequeue () ) + dequeue () + end in spawn main diff --git a/test/passing/refs.ocamlformat/ifand.ml.ref b/test/passing/refs.ocamlformat/ifand.ml.ref index 62c595ad79..686d37ff5f 100644 --- a/test/passing/refs.ocamlformat/ifand.ml.ref +++ b/test/passing/refs.ocamlformat/ifand.ml.ref @@ -1,3 +1,6 @@ let _ = if cond1 && cond2 then _ -let _ = function _ when x = 2 && y = 3 -> if a = b || (b = c && c = d) then _ +let _ = function + | _ when x = 2 && y = 3 -> begin + if a = b || (b = c && c = d) then _ + end diff --git a/test/passing/refs.ocamlformat/js_begin.ml.ref b/test/passing/refs.ocamlformat/js_begin.ml.ref index 933da252be..87e266fd96 100644 --- a/test/passing/refs.ocamlformat/js_begin.ml.ref +++ b/test/passing/refs.ocamlformat/js_begin.ml.ref @@ -1,5 +1,7 @@ -let f = function zoo -> foo ; bar +let f = function zoo -> begin foo ; bar end let g = function zoo -> foo ; bar -let () = match foo with Bar -> snoo +let () = + begin match foo with Bar -> snoo + end diff --git a/test/passing/refs.ocamlformat/js_pattern.ml.ref b/test/passing/refs.ocamlformat/js_pattern.ml.ref index cf5f26c2cd..b126b25d05 100644 --- a/test/passing/refs.ocamlformat/js_pattern.ml.ref +++ b/test/passing/refs.ocamlformat/js_pattern.ml.ref @@ -6,7 +6,9 @@ let f = function _ -> 0 let f x = match x with _ -> 0 -let f x = match x with _ -> 0 +let f x = + begin match x with _ -> 0 + end let check_price t = function | {Exec.trade_at_settlement= None | Some false} -> diff --git a/test/passing/refs.ocamlformat/reformat_string.ml.ref b/test/passing/refs.ocamlformat/reformat_string.ml.ref index 77022b49eb..c2dfa0fb5c 100644 --- a/test/passing/refs.ocamlformat/reformat_string.ml.ref +++ b/test/passing/refs.ocamlformat/reformat_string.ml.ref @@ -6,9 +6,15 @@ let _ = (* test *) "asd" let _ = "asd" -let _ = (* te""st *) "asd" - -let _ = "asd" +let _ = + begin + (* te""st *) "asd" + end + +let _ = + begin + "asd" + end let _ = 'a' diff --git a/test/passing/refs.ocamlformat/sequence-preserve.ml.ref b/test/passing/refs.ocamlformat/sequence-preserve.ml.ref index 3227f24467..2faba92b5d 100644 --- a/test/passing/refs.ocamlformat/sequence-preserve.ml.ref +++ b/test/passing/refs.ocamlformat/sequence-preserve.ml.ref @@ -1,8 +1,10 @@ let read_traces filename = let ic = open_in_bin filename in - read_hashtable ~t:[%t: contracts_trace] 0 40 ic tbl1 ; - read_hashtable ~t:[%t: variables_trace] 40 70 ic tbl2 ; - read_hashtable ~t:[%t: expressions_trace] 70 100 ic tbl3 ; + begin + read_hashtable ~t:[%t: contracts_trace] 0 40 ic tbl1 ; + read_hashtable ~t:[%t: variables_trace] 40 70 ic tbl2 ; + read_hashtable ~t:[%t: expressions_trace] 70 100 ic tbl3 + end ; close_in ic let foo x y = diff --git a/test/passing/refs.ocamlformat/sequence.ml.ref b/test/passing/refs.ocamlformat/sequence.ml.ref index 2744af3beb..8da0925f57 100644 --- a/test/passing/refs.ocamlformat/sequence.ml.ref +++ b/test/passing/refs.ocamlformat/sequence.ml.ref @@ -1,8 +1,10 @@ let read_traces filename = let ic = open_in_bin filename in - read_hashtable ~t:[%t: contracts_trace] 0 40 ic tbl1 ; - read_hashtable ~t:[%t: variables_trace] 40 70 ic tbl2 ; - read_hashtable ~t:[%t: expressions_trace] 70 100 ic tbl3 ; + begin + read_hashtable ~t:[%t: contracts_trace] 0 40 ic tbl1 ; + read_hashtable ~t:[%t: variables_trace] 40 70 ic tbl2 ; + read_hashtable ~t:[%t: expressions_trace] 70 100 ic tbl3 + end ; close_in ic let foo x y = diff --git a/test/passing/refs.ocamlformat/source.ml.err b/test/passing/refs.ocamlformat/source.ml.err index e754796ab8..4a4fc6f8f8 100644 --- a/test/passing/refs.ocamlformat/source.ml.err +++ b/test/passing/refs.ocamlformat/source.ml.err @@ -1,3 +1,3 @@ -Warning: source.ml:6474 exceeds the margin -Warning: source.ml:7348 exceeds the margin -Warning: source.ml:7865 exceeds the margin +Warning: source.ml:6497 exceeds the margin +Warning: source.ml:7371 exceeds the margin +Warning: source.ml:7889 exceeds the margin diff --git a/test/passing/refs.ocamlformat/source.ml.ref b/test/passing/refs.ocamlformat/source.ml.ref index 361e6558e7..ba335076bb 100644 --- a/test/passing/refs.ocamlformat/source.ml.ref +++ b/test/passing/refs.ocamlformat/source.ml.ref @@ -1235,18 +1235,20 @@ let rec get_case : type a b e. = fun sel cases -> match cases with - | (name, TCnoarg sel') :: rem -> ( + | (name, TCnoarg sel') :: rem -> begin match eq_sel sel sel' with | None -> get_case sel rem | Some Eq -> - (name, None) ) - | (name, TCarg (sel', ty)) :: rem -> ( + (name, None) + end + | (name, TCarg (sel', ty)) :: rem -> begin match eq_sel sel sel' with | None -> get_case sel rem | Some Eq -> - (name, Some ty) ) + (name, Some ty) + end | [] -> raise Not_found @@ -1306,7 +1308,7 @@ let rec devariantize : type t e. e ty_env -> (t, e) ty -> variant -> t = match e with Econs (t, e') -> devariantize e' t v ) | Conv (s, proj, inj, t), VConv (s', v) when s = s' -> inj (devariantize e t v) - | Sum ops, VSum (tag, a) -> ( + | Sum ops, VSum (tag, a) -> begin try match (List.assoc tag ops.sum_cases, a) with | TCarg (sel, t), Some a -> @@ -1315,7 +1317,8 @@ let rec devariantize : type t e. e ty_env -> (t, e) ty -> variant -> t = ops.sum_inj (sel, Noarg) | _ -> raise VariantMismatch - with Not_found -> raise VariantMismatch ) + with Not_found -> raise VariantMismatch + end | _ -> raise VariantMismatch @@ -1692,8 +1695,9 @@ let rec sameNat : type a b. a nat -> b nat -> (a, b) equal option = match (a, b) with | NZ, NZ -> Some Eq - | NS a', NS b' -> ( - match sameNat a' b' with Some Eq -> Some Eq | None -> None ) + | NS a', NS b' -> begin + match sameNat a' b' with Some Eq -> Some Eq | None -> None + end | _ -> None @@ -1853,9 +1857,9 @@ let rec ins : type n. int -> n avl -> (n avl, n succ avl) sum = match t with | Leaf -> Inr (Node (Same, Leaf, x, Leaf)) - | Node (bal, a, y, b) -> ( + | Node (bal, a, y, b) -> if x = y then Inl t - else if x < y then + else if x < y then begin match ins x a with | Inl a -> Inl (Node (bal, a, y, b)) @@ -1867,7 +1871,8 @@ let rec ins : type n. int -> n avl -> (n avl, n succ avl) sum = Inr (Node (More, a, y, b)) | More -> rotr a y b ) - else + end + else begin match ins x b with | Inl b -> Inl (Node (bal, a, y, b) : n avl) @@ -1878,7 +1883,8 @@ let rec ins : type n. int -> n avl -> (n avl, n succ avl) sum = | Same -> Inr (Node (Less, a, y, b) : n succ avl) | Less -> - rotl a y b ) ) + rotl a y b ) + end let insert x (Avl t) = match ins x t with Inl t -> Avl t | Inr t -> Avl t @@ -1910,12 +1916,13 @@ let rec del : type n. int -> n avl -> n avl_del = match t with | Leaf -> Dsame Leaf - | Node (bal, l, x, r) -> ( - if x = y then + | Node (bal, l, x, r) -> + if x = y then begin match r with - | Leaf -> ( - match bal with Same -> Ddecr (Eq, l) | More -> Ddecr (Eq, l) ) - | Node _ -> ( + | Leaf -> begin + match bal with Same -> Ddecr (Eq, l) | More -> Ddecr (Eq, l) + end + | Node _ -> begin match (bal, del_min r) with | _, (z, Inr r) -> Dsame (Node (bal, l, z, r)) @@ -1924,32 +1931,37 @@ let rec del : type n. int -> n avl -> n avl_del = | Less, (z, Inl r) -> Ddecr (Eq, Node (Same, l, z, r)) | More, (z, Inl r) -> ( - match rotr l z r with Inl t -> Ddecr (Eq, t) | Inr t -> Dsame t ) ) - else if y < x then + match rotr l z r with Inl t -> Ddecr (Eq, t) | Inr t -> Dsame t ) + end + end + else if y < x then begin match del y l with | Dsame l -> Dsame (Node (bal, l, x, r)) - | Ddecr (Eq, l) -> ( + | Ddecr (Eq, l) -> begin match bal with | Same -> Dsame (Node (Less, l, x, r)) | More -> Ddecr (Eq, Node (Same, l, x, r)) | Less -> ( - match rotl l x r with Inl t -> Ddecr (Eq, t) | Inr t -> Dsame t ) ) - else + match rotl l x r with Inl t -> Ddecr (Eq, t) | Inr t -> Dsame t ) + end + end + else begin match del y r with | Dsame r -> Dsame (Node (bal, l, x, r)) - | Ddecr (Eq, r) -> ( + | Ddecr (Eq, r) -> begin match bal with | Same -> Dsame (Node (More, l, x, r)) | Less -> Ddecr (Eq, Node (Same, l, x, r)) | More -> ( - match rotr l x r with Inl t -> Ddecr (Eq, t) | Inr t -> Dsame t ) ) - ) + match rotr l x r with Inl t -> Ddecr (Eq, t) | Inr t -> Dsame t ) + end + end let delete x (Avl t) = match del x t with Dsame t -> Avl t | Ddecr (_, t) -> Avl t @@ -2098,18 +2110,20 @@ let rec rep_equal : type a b. a rep -> b rep -> (a, b) equal option = Some Eq | Rbool, Rbool -> Some Eq - | Rpair (a1, a2), Rpair (b1, b2) -> ( + | Rpair (a1, a2), Rpair (b1, b2) -> begin match rep_equal a1 b1 with | None -> None | Some Eq -> ( - match rep_equal a2 b2 with None -> None | Some Eq -> Some Eq ) ) - | Rfun (a1, a2), Rfun (b1, b2) -> ( + match rep_equal a2 b2 with None -> None | Some Eq -> Some Eq ) + end + | Rfun (a1, a2), Rfun (b1, b2) -> begin match rep_equal a1 b1 with | None -> None | Some Eq -> ( - match rep_equal a2 b2 with None -> None | Some Eq -> Some Eq ) ) + match rep_equal a2 b2 with None -> None | Some Eq -> Some Eq ) + end | _ -> None @@ -2239,12 +2253,13 @@ let rec compare : type a b. a rep -> b rep -> (string, (a, b) equal) sum = match (a, b) with | I, I -> Inr Eq - | Ar (x, y), Ar (s, t) -> ( + | Ar (x, y), Ar (s, t) -> begin match compare x s with | Inl _ as e -> e | Inr Eq -> ( - match compare y t with Inl _ as e -> e | Inr Eq as e -> e ) ) + match compare y t with Inl _ as e -> e | Inr Eq as e -> e ) + end | I, Ar _ -> Inl "I <> Ar _" | Ar _, I -> @@ -2281,7 +2296,7 @@ let rec tc : type n e. n nat -> e ctx -> term -> e checked = match t with | V s -> lookup s ctx - | Ap (f, x) -> ( + | Ap (f, x) -> begin match tc n ctx f with | Cerror _ as e -> e @@ -2291,20 +2306,23 @@ let rec tc : type n e. n nat -> e ctx -> term -> e checked = e | Cok (x', xt) -> ( match ft with - | Ar (a, b) -> ( + | Ar (a, b) -> begin match compare a xt with | Inl s -> Cerror s | Inr Eq -> - Cok (App (f', x'), b) ) + Cok (App (f', x'), b) + end | _ -> - Cerror "Non fun in Ap" ) ) ) - | Ab (s, t, body) -> ( + Cerror "Non fun in Ap" ) ) + end + | Ab (s, t, body) -> begin match tc (NS n) (Ccons (n, s, t, ctx)) body with | Cerror _ as e -> e | Cok (body', et) -> - Cok (Abs (n, body'), Ar (t, et)) ) + Cok (Abs (n, body'), Ar (t, et)) + end | C m -> Cok (Const m, I) @@ -2407,10 +2425,11 @@ let rec rule : type a b. (pval, closed, (a, b) tarr) lam -> (pval, closed, a) lam -> b rlam = fun v1 v2 -> match (v1, v2) with - | Lam (x, body), v -> ( + | Lam (x, body), v -> begin match subst body (Bind (x, v, Id)) with | Ex term -> ( - match mode term with Pexp -> Inl term | Pval -> Inr term ) ) + match mode term with Pexp -> Inl term | Pval -> Inr term ) + end | Const (IntTo b, f), Const (IntR, x) -> Inr (Const (b, f x)) @@ -2421,18 +2440,20 @@ let rec onestep : type m t. (m, closed, t) lam -> t rlam = function Inr (Const (r, v)) | App (e1, e2) -> ( match (mode e1, mode e2) with - | Pexp, _ -> ( + | Pexp, _ -> begin match onestep e1 with | Inl e -> Inl (App (e, e2)) | Inr v -> - Inl (App (v, e2)) ) - | Pval, Pexp -> ( + Inl (App (v, e2)) + end + | Pval, Pexp -> begin match onestep e2 with | Inl e -> Inl (App (e1, e)) | Inr v -> - Inl (App (e1, v)) ) + Inl (App (e1, v)) + end | Pval, Pval -> rule e1 e2 ) @@ -6437,10 +6458,12 @@ module UText = struct let set_buf s i u = let n = UChar.uint_code u in - s.![i] <- Char.chr (n lsr 24) ; - s.![i + 1] <- Char.chr ((n lsr 16) lor 0xff) ; - s.![i + 2] <- Char.chr ((n lsr 8) lor 0xff) ; - s.![i + 3] <- Char.chr (n lor 0xff) + begin + s.![i] <- Char.chr (n lsr 24) ; + s.![i + 1] <- Char.chr ((n lsr 16) lor 0xff) ; + s.![i + 2] <- Char.chr ((n lsr 8) lor 0xff) ; + s.![i + 3] <- Char.chr (n lor 0xff) + end let init_buf buf pos init = if init#len = 0 then () @@ -7843,15 +7866,16 @@ module Bootstrap let deleteMin = function | BE.E -> raise Not_found - | BE.H (x, p) -> ( + | BE.H (x, p) -> if PrimH.isEmpty p then BE.E - else + else begin match PrimH.findMin p with | BE.H (y, p1) -> let p2 = PrimH.deleteMin p in BE.H (y, PrimH.merge p1 p2) | BE.E -> - assert false ) + assert false + end end module LeftistHeap (Element : ORDERED) : HEAP with module Elem = Element = @@ -9410,19 +9434,21 @@ let g = function let h = fun ?l:(p = 1) ?y:u ?(x = 3) -> 2 let _ = function - | a, s, ba1, ba2, ba3, bg -> - ignore - ( Array.get x 1 + Array.get [||] 0 + Array.get [|1|] 1 - + Array.get [|1; 2|] 2 ) ; - ignore [String.get s 1; String.get "" 2; String.get "123" 3] ; - ignore (ba1.{0} + ba2.{1, 2} + ba3.{3, 4, 5}) ignore bg.{1, 2, 3, 4} - | b, s, ba1, ba2, ba3, bg -> - y.(0) <- 1 ; - s.![1] <- 'c' ; - ba1.{1} <- 2 ; - ba2.{1, 2} <- 3 ; - ba3.{1, 2, 3} <- 4 ; - bg.{1, 2, 3, 4, 5} <- 0 + | a, s, ba1, ba2, ba3, bg -> begin + ignore + ( Array.get x 1 + Array.get [||] 0 + Array.get [|1|] 1 + + Array.get [|1; 2|] 2 ) ; + ignore [String.get s 1; String.get "" 2; String.get "123" 3] ; + ignore (ba1.{0} + ba2.{1, 2} + ba3.{3, 4, 5}) ignore bg.{1, 2, 3, 4} + end + | b, s, ba1, ba2, ba3, bg -> begin + y.(0) <- 1 ; + s.![1] <- 'c' ; + ba1.{1} <- 2 ; + ba2.{1, 2} <- 3 ; + ba3.{1, 2, 3} <- 4 ; + bg.{1, 2, 3, 4, 5} <- 0 + end let f (type t) () = let exception F of t in diff --git a/test/unit/test_fmt_ast.ml b/test/unit/test_fmt_ast.ml index 6a0abe3dad..071a5e7c5c 100644 --- a/test/unit/test_fmt_ast.ml +++ b/test/unit/test_fmt_ast.ml @@ -114,7 +114,7 @@ let updated_ast_tests = check_updated_test "[%extension 1]" "[%extension 1]" ; check_updated_test "function _ -> ." "function _ -> ." ; check_updated_test "_" "_" ; - check_updated_test "begin () end" "()" ; + check_updated_test "begin () end" "begin\n ()\nend" ; check_updated_test "a :: b" "a :: b" ; check_updated_test "a.!(b)" "a.!(b)" ; check_updated_test "a.!(b) <- c" "a.!(b) <- c" ; From 7944ed7fd855ef3d6335f8ba414449cdd295f6af Mon Sep 17 00:00:00 2001 From: EmileTrotignon Date: Fri, 6 Jun 2025 19:07:48 +0200 Subject: [PATCH 2/2] changelog --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 5363e58e83..519e48e421 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -129,6 +129,9 @@ profile. This started with version 0.26.0. `@@ match` can now also be on one line. (#2694, @EmileTrotignon) +- `exp-grouping=preserve` is now the default in `default` and `ocamlformat` + profiles. This means that its now possible to use `begin ... end` without + tweaking ocamlformat. (#2716, @EmileTrotignon) ## 0.27.0 ### Highlight