Skip to content

Commit 6adcf09

Browse files
use special case beginend syntax in match cases when possible
1 parent a325ecc commit 6adcf09

21 files changed

+337
-309
lines changed

lib/Params.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,16 +537,20 @@ let get_cases (c : Conf.t) ~fmt_infix_ext_attrs ~ctx ~first ~last
537537
else (parenze_exp xast && not body_has_parens, Some false)
538538
in
539539
let indent = if align_nested_match then 0 else indent in
540+
let nested_exp_has_special_beginend exp =
541+
match exp with
542+
| Pexp_match _ | Pexp_try _ | Pexp_function _ | Pexp_ifthenelse _ -> true
543+
| _ -> false
544+
in
540545
let open_paren_branch, close_paren_branch, branch_expr =
541546
match ast with
542547
| { pexp_desc= Pexp_beginend (nested_exp, infix_ext_attrs)
543548
; pexp_attributes= []
544549
; _ }
545-
when not cmts_before ->
550+
when (not cmts_before)
551+
&& not (nested_exp_has_special_beginend nested_exp.pexp_desc) ->
546552
let close_paren =
547-
let offset =
548-
if indent >= 2 then 2 - indent else 0
549-
in
553+
let offset = if indent >= 2 then 2 - indent else 0 in
550554
fits_breaks " end" ~level:1 ~hint:(1000, offset) "end"
551555
in
552556
( break 1 0 $ fmt_infix_ext_attrs ~pro:(str "begin") infix_ext_attrs

test/passing/refs.ahrefs/cases_exp_grouping.ml.ref

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
let _ =
22
match x with
3-
| A -> begin match B with A -> fooooooooooooo end
4-
| A -> begin match B with A -> fooooooooooooo | B -> fooooooooooooo end
5-
| A -> begin
6-
match B with
3+
| A ->
4+
begin match B with A -> fooooooooooooo
5+
end
6+
| A ->
7+
begin match B with A -> fooooooooooooo | B -> fooooooooooooo
8+
end
9+
| A ->
10+
begin match B with
711
| A -> fooooooooooooo
812
| B -> fooooooooooooo
913
| C -> fooooooooooooo
@@ -13,14 +17,14 @@ let _ =
1317

1418
let _ =
1519
match x with
16-
| A -> begin
17-
match B with A -> fooooooooooooo
18-
end
19-
| A -> begin
20-
match B with A -> fooooooooooooo | B -> fooooooooooooo
21-
end
22-
| A -> begin
23-
match B with
20+
| A ->
21+
begin match B with A -> fooooooooooooo
22+
end
23+
| A ->
24+
begin match B with A -> fooooooooooooo | B -> fooooooooooooo
25+
end
26+
| A ->
27+
begin match B with
2428
| A ->
2529
fooooooooooooo
2630
| B ->
@@ -34,17 +38,17 @@ let _ =
3438

3539
let _ =
3640
match x with
37-
| A -> begin
38-
match B with
41+
| A ->
42+
begin match B with
3943
| A -> fooooooooooooo
4044
end
41-
| A -> begin
42-
match B with
45+
| A ->
46+
begin match B with
4347
| A -> fooooooooooooo
4448
| B -> fooooooooooooo
4549
end
46-
| A -> begin
47-
match B with
50+
| A ->
51+
begin match B with
4852
| A -> fooooooooooooo
4953
| B -> fooooooooooooo
5054
| C -> fooooooooooooo
@@ -54,17 +58,17 @@ let _ =
5458

5559
let _ =
5660
match x with
57-
| A -> begin
58-
match B with
61+
| A ->
62+
begin match B with
5963
| A -> fooooooooooooo
6064
end
61-
| A -> begin
62-
match B with
65+
| A ->
66+
begin match B with
6367
| A -> fooooooooooooo
6468
| B -> fooooooooooooo
6569
end
66-
| A -> begin
67-
match B with
70+
| A ->
71+
begin match B with
6872
| A -> fooooooooooooo
6973
| B -> fooooooooooooo
7074
| C -> fooooooooooooo
@@ -74,17 +78,17 @@ let _ =
7478

7579
let _ =
7680
match x with
77-
| A -> begin
78-
match B with
81+
| A ->
82+
begin match B with
7983
| A -> fooooooooooooo
8084
end
81-
| A -> begin
82-
match B with
85+
| A ->
86+
begin match B with
8387
| A -> fooooooooooooo
8488
| B -> fooooooooooooo
8589
end
86-
| A -> begin
87-
match B with
90+
| A ->
91+
begin match B with
8892
| A -> fooooooooooooo
8993
| B -> fooooooooooooo
9094
| C -> fooooooooooooo

test/passing/refs.ahrefs/cases_exp_indent.ml.ref

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ let () =
99
| () ->
1010
aaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaa aaaaaaaaaaaaaa aaaaaaaaaaaa
1111
aaaaaaaaaaaaa ()
12-
| () -> begin
13-
match () with
12+
| () ->
13+
begin match () with
1414
| () -> ()
15-
end
15+
end
1616
| () ->
1717
(match () with
1818
| () -> ())
19-
| () -> begin
20-
match () with
19+
| () ->
20+
begin match () with
2121
| () -> ()
22-
end
22+
end
2323
end

test/passing/refs.ahrefs/effects.ml.ref

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ let run (main : unit -> unit) : unit =
3333
| effect Fork f, k ->
3434
enqueue k ();
3535
spawn f
36-
| effect Xchg n, k -> begin
37-
match !exchanger with
36+
| effect Xchg n, k ->
37+
begin match !exchanger with
3838
| Some (n', k') ->
3939
exchanger := None;
4040
enqueue k' n;

test/passing/refs.ahrefs/exp_grouping.ml.ref

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,17 @@ let _ =
281281

282282
let _ =
283283
match x with
284-
| A -> begin
285-
match B with
284+
| A ->
285+
begin match B with
286286
| A -> fooooooooooooo
287287
end
288-
| A -> begin
289-
match B with
288+
| A ->
289+
begin match B with
290290
| A -> fooooooooooooo
291291
| B -> fooooooooooooo
292292
end
293-
| A -> begin
294-
match B with
293+
| A ->
294+
begin match B with
295295
| A -> fooooooooooooo
296296
| B -> fooooooooooooo
297297
| C -> fooooooooooooo
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
let _ = if cond1 && cond2 then _
22

33
let _ = function
4-
| _ when x = 2 && y = 3 -> begin if a = b || (b = c && c = d) then _ end
4+
| _ when x = 2 && y = 3 ->
5+
begin if a = b || (b = c && c = d) then _
6+
end

test/passing/refs.ahrefs/source.ml.ref

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,13 +1148,13 @@ let rec get_case : type a b e.
11481148
(b, a) ty_sel -> (string * (e, b) ty_case) list -> string * (a, e) ty option =
11491149
fun sel cases ->
11501150
match cases with
1151-
| (name, TCnoarg sel') :: rem -> begin
1152-
match eq_sel sel sel' with
1151+
| (name, TCnoarg sel') :: rem ->
1152+
begin match eq_sel sel sel' with
11531153
| None -> get_case sel rem
11541154
| Some Eq -> name, None
11551155
end
1156-
| (name, TCarg (sel', ty)) :: rem -> begin
1157-
match eq_sel sel sel' with
1156+
| (name, TCarg (sel', ty)) :: rem ->
1157+
begin match eq_sel sel sel' with
11581158
| None -> get_case sel rem
11591159
| Some Eq -> name, Some ty
11601160
end
@@ -1216,8 +1216,8 @@ let rec devariantize : type t e. e ty_env -> (t, e) ty -> variant -> t =
12161216
| Econs (t, e') -> devariantize e' t v)
12171217
| Conv (s, proj, inj, t), VConv (s', v) when s = s' ->
12181218
inj (devariantize e t v)
1219-
| Sum ops, VSum (tag, a) -> begin
1220-
try
1219+
| Sum ops, VSum (tag, a) ->
1220+
begin try
12211221
match List.assoc tag ops.sum_cases, a with
12221222
| TCarg (sel, t), Some a -> ops.sum_inj (sel, devariantize e t a)
12231223
| TCnoarg sel, None -> ops.sum_inj (sel, Noarg)
@@ -1560,8 +1560,8 @@ let rec sameNat : type a b. a nat -> b nat -> (a, b) equal option =
15601560
fun a b ->
15611561
match a, b with
15621562
| NZ, NZ -> Some Eq
1563-
| NS a', NS b' -> begin
1564-
match sameNat a' b' with
1563+
| NS a', NS b' ->
1564+
begin match sameNat a' b' with
15651565
| Some Eq -> Some Eq
15661566
| None -> None
15671567
end
@@ -1756,13 +1756,13 @@ let rec del : type n. int -> n avl -> n avl_del =
17561756
| Node (bal, l, x, r) ->
17571757
if x = y then begin
17581758
match r with
1759-
| Leaf -> begin
1760-
match bal with
1759+
| Leaf ->
1760+
begin match bal with
17611761
| Same -> Ddecr (Eq, l)
17621762
| More -> Ddecr (Eq, l)
17631763
end
1764-
| Node _ -> begin
1765-
match bal, del_min r with
1764+
| Node _ ->
1765+
begin match bal, del_min r with
17661766
| _, (z, Inr r) -> Dsame (Node (bal, l, z, r))
17671767
| Same, (z, Inl r) -> Dsame (Node (More, l, z, r))
17681768
| Less, (z, Inl r) -> Ddecr (Eq, Node (Same, l, z, r))
@@ -1775,8 +1775,8 @@ let rec del : type n. int -> n avl -> n avl_del =
17751775
else if y < x then begin
17761776
match del y l with
17771777
| Dsame l -> Dsame (Node (bal, l, x, r))
1778-
| Ddecr (Eq, l) -> begin
1779-
match bal with
1778+
| Ddecr (Eq, l) ->
1779+
begin match bal with
17801780
| Same -> Dsame (Node (Less, l, x, r))
17811781
| More -> Ddecr (Eq, Node (Same, l, x, r))
17821782
| Less ->
@@ -1788,8 +1788,8 @@ let rec del : type n. int -> n avl -> n avl_del =
17881788
else begin
17891789
match del y r with
17901790
| Dsame r -> Dsame (Node (bal, l, x, r))
1791-
| Ddecr (Eq, r) -> begin
1792-
match bal with
1791+
| Ddecr (Eq, r) ->
1792+
begin match bal with
17931793
| Same -> Dsame (Node (More, l, x, r))
17941794
| Less -> Ddecr (Eq, Node (Same, l, x, r))
17951795
| More ->
@@ -1916,16 +1916,16 @@ let rec rep_equal : type a b. a rep -> b rep -> (a, b) equal option =
19161916
match ra, rb with
19171917
| Rint, Rint -> Some Eq
19181918
| Rbool, Rbool -> Some Eq
1919-
| Rpair (a1, a2), Rpair (b1, b2) -> begin
1920-
match rep_equal a1 b1 with
1919+
| Rpair (a1, a2), Rpair (b1, b2) ->
1920+
begin match rep_equal a1 b1 with
19211921
| None -> None
19221922
| Some Eq ->
19231923
match rep_equal a2 b2 with
19241924
| None -> None
19251925
| Some Eq -> Some Eq
19261926
end
1927-
| Rfun (a1, a2), Rfun (b1, b2) -> begin
1928-
match rep_equal a1 b1 with
1927+
| Rfun (a1, a2), Rfun (b1, b2) ->
1928+
begin match rep_equal a1 b1 with
19291929
| None -> None
19301930
| Some Eq ->
19311931
match rep_equal a2 b2 with
@@ -2035,8 +2035,8 @@ let rec compare : type a b. a rep -> b rep -> (string, (a, b) equal) sum =
20352035
fun a b ->
20362036
match a, b with
20372037
| I, I -> Inr Eq
2038-
| Ar (x, y), Ar (s, t) -> begin
2039-
match compare x s with
2038+
| Ar (x, y), Ar (s, t) ->
2039+
begin match compare x s with
20402040
| Inl _ as e -> e
20412041
| Inr Eq ->
20422042
match compare y t with
@@ -2075,23 +2075,23 @@ let rec tc : type n e. n nat -> e ctx -> term -> e checked =
20752075
fun n ctx t ->
20762076
match t with
20772077
| V s -> lookup s ctx
2078-
| Ap (f, x) -> begin
2079-
match tc n ctx f with
2078+
| Ap (f, x) ->
2079+
begin match tc n ctx f with
20802080
| Cerror _ as e -> e
20812081
| Cok (f', ft) ->
20822082
match tc n ctx x with
20832083
| Cerror _ as e -> e
20842084
| Cok (x', xt) ->
20852085
match ft with
2086-
| Ar (a, b) -> begin
2087-
match compare a xt with
2086+
| Ar (a, b) ->
2087+
begin match compare a xt with
20882088
| Inl s -> Cerror s
20892089
| Inr Eq -> Cok (App (f', x'), b)
20902090
end
20912091
| _ -> Cerror "Non fun in Ap"
20922092
end
2093-
| Ab (s, t, body) -> begin
2094-
match tc (NS n) (Ccons (n, s, t, ctx)) body with
2093+
| Ab (s, t, body) ->
2094+
begin match tc (NS n) (Ccons (n, s, t, ctx)) body with
20952095
| Cerror _ as e -> e
20962096
| Cok (body', et) -> Cok (Abs (n, body'), Ar (t, et))
20972097
end
@@ -2182,8 +2182,8 @@ let rec rule : type a b.
21822182
(pval, closed, (a, b) tarr) lam -> (pval, closed, a) lam -> b rlam =
21832183
fun v1 v2 ->
21842184
match v1, v2 with
2185-
| Lam (x, body), v -> begin
2186-
match subst body (Bind (x, v, Id)) with
2185+
| Lam (x, body), v ->
2186+
begin match subst body (Bind (x, v, Id)) with
21872187
| Ex term ->
21882188
match mode term with
21892189
| Pexp -> Inl term
@@ -2195,13 +2195,13 @@ let rec onestep : type m t. (m, closed, t) lam -> t rlam = function
21952195
| Const (r, v) -> Inr (Const (r, v))
21962196
| App (e1, e2) ->
21972197
match mode e1, mode e2 with
2198-
| Pexp, _ -> begin
2199-
match onestep e1 with
2198+
| Pexp, _ ->
2199+
begin match onestep e1 with
22002200
| Inl e -> Inl (App (e, e2))
22012201
| Inr v -> Inl (App (v, e2))
22022202
end
2203-
| Pval, Pexp -> begin
2204-
match onestep e2 with
2203+
| Pval, Pexp ->
2204+
begin match onestep e2 with
22052205
| Inl e -> Inl (App (e1, e))
22062206
| Inr v -> Inl (App (e1, v))
22072207
end

0 commit comments

Comments
 (0)