Skip to content

Commit 4a211c1

Browse files
use special case beginend syntax in match cases when possible
1 parent 6592d7f commit 4a211c1

21 files changed

Lines changed: 336 additions & 306 deletions

lib/Params.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,12 +537,18 @@ 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 =
547553
let offset = if indent >= 2 then 2 - indent else 0 in
548554
fits_breaks " end" ~level:1 ~hint:(1000, offset) "end"

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
@@ -1146,13 +1146,13 @@ let rec get_case : type a b e.
11461146
(b, a) ty_sel -> (string * (e, b) ty_case) list -> string * (a, e) ty option =
11471147
fun sel cases ->
11481148
match cases with
1149-
| (name, TCnoarg sel') :: rem -> begin
1150-
match eq_sel sel sel' with
1149+
| (name, TCnoarg sel') :: rem ->
1150+
begin match eq_sel sel sel' with
11511151
| None -> get_case sel rem
11521152
| Some Eq -> name, None
11531153
end
1154-
| (name, TCarg (sel', ty)) :: rem -> begin
1155-
match eq_sel sel sel' with
1154+
| (name, TCarg (sel', ty)) :: rem ->
1155+
begin match eq_sel sel sel' with
11561156
| None -> get_case sel rem
11571157
| Some Eq -> name, Some ty
11581158
end
@@ -1214,8 +1214,8 @@ let rec devariantize : type t e. e ty_env -> (t, e) ty -> variant -> t =
12141214
| Econs (t, e') -> devariantize e' t v)
12151215
| Conv (s, proj, inj, t), VConv (s', v) when s = s' ->
12161216
inj (devariantize e t v)
1217-
| Sum ops, VSum (tag, a) -> begin
1218-
try
1217+
| Sum ops, VSum (tag, a) ->
1218+
begin try
12191219
match List.assoc tag ops.sum_cases, a with
12201220
| TCarg (sel, t), Some a -> ops.sum_inj (sel, devariantize e t a)
12211221
| TCnoarg sel, None -> ops.sum_inj (sel, Noarg)
@@ -1558,8 +1558,8 @@ let rec sameNat : type a b. a nat -> b nat -> (a, b) equal option =
15581558
fun a b ->
15591559
match a, b with
15601560
| NZ, NZ -> Some Eq
1561-
| NS a', NS b' -> begin
1562-
match sameNat a' b' with
1561+
| NS a', NS b' ->
1562+
begin match sameNat a' b' with
15631563
| Some Eq -> Some Eq
15641564
| None -> None
15651565
end
@@ -1754,13 +1754,13 @@ let rec del : type n. int -> n avl -> n avl_del =
17541754
| Node (bal, l, x, r) ->
17551755
if x = y then begin
17561756
match r with
1757-
| Leaf -> begin
1758-
match bal with
1757+
| Leaf ->
1758+
begin match bal with
17591759
| Same -> Ddecr (Eq, l)
17601760
| More -> Ddecr (Eq, l)
17611761
end
1762-
| Node _ -> begin
1763-
match bal, del_min r with
1762+
| Node _ ->
1763+
begin match bal, del_min r with
17641764
| _, (z, Inr r) -> Dsame (Node (bal, l, z, r))
17651765
| Same, (z, Inl r) -> Dsame (Node (More, l, z, r))
17661766
| Less, (z, Inl r) -> Ddecr (Eq, Node (Same, l, z, r))
@@ -1773,8 +1773,8 @@ let rec del : type n. int -> n avl -> n avl_del =
17731773
else if y < x then begin
17741774
match del y l with
17751775
| Dsame l -> Dsame (Node (bal, l, x, r))
1776-
| Ddecr (Eq, l) -> begin
1777-
match bal with
1776+
| Ddecr (Eq, l) ->
1777+
begin match bal with
17781778
| Same -> Dsame (Node (Less, l, x, r))
17791779
| More -> Ddecr (Eq, Node (Same, l, x, r))
17801780
| Less ->
@@ -1786,8 +1786,8 @@ let rec del : type n. int -> n avl -> n avl_del =
17861786
else begin
17871787
match del y r with
17881788
| Dsame r -> Dsame (Node (bal, l, x, r))
1789-
| Ddecr (Eq, r) -> begin
1790-
match bal with
1789+
| Ddecr (Eq, r) ->
1790+
begin match bal with
17911791
| Same -> Dsame (Node (More, l, x, r))
17921792
| Less -> Ddecr (Eq, Node (Same, l, x, r))
17931793
| More ->
@@ -1914,16 +1914,16 @@ let rec rep_equal : type a b. a rep -> b rep -> (a, b) equal option =
19141914
match ra, rb with
19151915
| Rint, Rint -> Some Eq
19161916
| Rbool, Rbool -> Some Eq
1917-
| Rpair (a1, a2), Rpair (b1, b2) -> begin
1918-
match rep_equal a1 b1 with
1917+
| Rpair (a1, a2), Rpair (b1, b2) ->
1918+
begin match rep_equal a1 b1 with
19191919
| None -> None
19201920
| Some Eq ->
19211921
match rep_equal a2 b2 with
19221922
| None -> None
19231923
| Some Eq -> Some Eq
19241924
end
1925-
| Rfun (a1, a2), Rfun (b1, b2) -> begin
1926-
match rep_equal a1 b1 with
1925+
| Rfun (a1, a2), Rfun (b1, b2) ->
1926+
begin match rep_equal a1 b1 with
19271927
| None -> None
19281928
| Some Eq ->
19291929
match rep_equal a2 b2 with
@@ -2033,8 +2033,8 @@ let rec compare : type a b. a rep -> b rep -> (string, (a, b) equal) sum =
20332033
fun a b ->
20342034
match a, b with
20352035
| I, I -> Inr Eq
2036-
| Ar (x, y), Ar (s, t) -> begin
2037-
match compare x s with
2036+
| Ar (x, y), Ar (s, t) ->
2037+
begin match compare x s with
20382038
| Inl _ as e -> e
20392039
| Inr Eq ->
20402040
match compare y t with
@@ -2073,23 +2073,23 @@ let rec tc : type n e. n nat -> e ctx -> term -> e checked =
20732073
fun n ctx t ->
20742074
match t with
20752075
| V s -> lookup s ctx
2076-
| Ap (f, x) -> begin
2077-
match tc n ctx f with
2076+
| Ap (f, x) ->
2077+
begin match tc n ctx f with
20782078
| Cerror _ as e -> e
20792079
| Cok (f', ft) ->
20802080
match tc n ctx x with
20812081
| Cerror _ as e -> e
20822082
| Cok (x', xt) ->
20832083
match ft with
2084-
| Ar (a, b) -> begin
2085-
match compare a xt with
2084+
| Ar (a, b) ->
2085+
begin match compare a xt with
20862086
| Inl s -> Cerror s
20872087
| Inr Eq -> Cok (App (f', x'), b)
20882088
end
20892089
| _ -> Cerror "Non fun in Ap"
20902090
end
2091-
| Ab (s, t, body) -> begin
2092-
match tc (NS n) (Ccons (n, s, t, ctx)) body with
2091+
| Ab (s, t, body) ->
2092+
begin match tc (NS n) (Ccons (n, s, t, ctx)) body with
20932093
| Cerror _ as e -> e
20942094
| Cok (body', et) -> Cok (Abs (n, body'), Ar (t, et))
20952095
end
@@ -2180,8 +2180,8 @@ let rec rule : type a b.
21802180
(pval, closed, (a, b) tarr) lam -> (pval, closed, a) lam -> b rlam =
21812181
fun v1 v2 ->
21822182
match v1, v2 with
2183-
| Lam (x, body), v -> begin
2184-
match subst body (Bind (x, v, Id)) with
2183+
| Lam (x, body), v ->
2184+
begin match subst body (Bind (x, v, Id)) with
21852185
| Ex term ->
21862186
match mode term with
21872187
| Pexp -> Inl term
@@ -2193,13 +2193,13 @@ let rec onestep : type m t. (m, closed, t) lam -> t rlam = function
21932193
| Const (r, v) -> Inr (Const (r, v))
21942194
| App (e1, e2) ->
21952195
match mode e1, mode e2 with
2196-
| Pexp, _ -> begin
2197-
match onestep e1 with
2196+
| Pexp, _ ->
2197+
begin match onestep e1 with
21982198
| Inl e -> Inl (App (e, e2))
21992199
| Inr v -> Inl (App (v, e2))
22002200
end
2201-
| Pval, Pexp -> begin
2202-
match onestep e2 with
2201+
| Pval, Pexp ->
2202+
begin match onestep e2 with
22032203
| Inl e -> Inl (App (e1, e))
22042204
| Inr v -> Inl (App (e1, v))
22052205
end

0 commit comments

Comments
 (0)