Skip to content

Commit e78ed52

Browse files
committed
Fix record patterns
1 parent 0e8907b commit e78ed52

File tree

12 files changed

+157
-17
lines changed

12 files changed

+157
-17
lines changed

lib/Fmt_ast.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ and fmt_pattern ?ext c ?pro ?parens ?(box = false)
11591159
in
11601160
hvbox 0 @@ Cmts.fmt c ppat_loc @@ fmt_record_field c ?typ1 ?rhs lid
11611161
in
1162-
let p = Params.get_record_pat c.conf ~ctx:ctx0 in
1162+
let p = Params.get_record_pat c.conf ~ctx:ctx0 pat in
11631163
let last_sep, fmt_underscore =
11641164
match closed_flag with
11651165
| OClosed -> (true, noop)
@@ -1188,13 +1188,13 @@ and fmt_pattern ?ext c ?pro ?parens ?(box = false)
11881188
hvbox 0
11891189
(wrap_fits_breaks c.conf "[|" "|]" (Cmts.fmt_within c ppat_loc))
11901190
| Ppat_array pats ->
1191-
let p = Params.get_array_pat c.conf ~ctx:ctx0 in
1191+
let p = Params.get_array_pat c.conf ~ctx:ctx0 pat in
11921192
p.box
11931193
(fmt_elements_collection c p Pat.location ppat_loc
11941194
(sub_pat ~ctx >> fmt_pattern c >> hvbox 0)
11951195
pats )
11961196
| Ppat_list pats ->
1197-
let p = Params.get_list_pat c.conf ~ctx:ctx0 in
1197+
let p = Params.get_list_pat c.conf ~ctx:ctx0 pat in
11981198
p.box
11991199
(fmt_elements_collection c p Pat.location ppat_loc
12001200
(sub_pat ~ctx >> fmt_pattern c >> hvbox 0)

lib/Params.ml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -663,44 +663,48 @@ let get_list_expr (c : Conf.t) =
663663
let get_array_expr (c : Conf.t) =
664664
collection_expr c ~space_around:c.fmt_opts.space_around_arrays.v "[|" "|]"
665665

666-
let box_pattern_docked (c : Conf.t) ~ctx ~space_around opn cls k =
666+
let box_pattern_docked (c : Conf.t) ~ctx ~space_around ~pat opn cls k =
667667
let space = if space_around then 1 else 0 in
668668
let indent_opn, indent_cls =
669669
match (ctx, c.fmt_opts.break_separators.v) with
670670
| Ast.Exp {pexp_desc= Pexp_match _ | Pexp_try _; _}, `Before ->
671671
(String.length opn - 3, 1 - String.length opn)
672672
| Ast.Exp {pexp_desc= Pexp_match _ | Pexp_try _; _}, `After -> (-3, 1)
673-
| Ast.Exp {pexp_desc= Pexp_let _; _}, _ -> (-4, 0)
673+
| Ast.Exp {pexp_desc= Pexp_let ({pvbs_bindings; _}, _, _); _}, _
674+
when List.exists pvbs_bindings ~f:(fun b -> phys_equal b.pvb_pat pat)
675+
->
676+
(-4, 0)
674677
| _ -> (0, 0)
675678
in
676679
hvbox indent_opn
677680
(wrap (str opn) (str cls) (break space 2 $ k $ break space indent_cls))
678681

679-
let get_record_pat (c : Conf.t) ~ctx =
682+
let get_record_pat (c : Conf.t) ~ctx pat =
680683
let params, _ = get_record_expr c in
681684
let box =
682685
if c.fmt_opts.dock_collection_brackets.v then
683686
box_pattern_docked c ~ctx
684-
~space_around:c.fmt_opts.space_around_records.v "{" "}"
687+
~space_around:c.fmt_opts.space_around_records.v ~pat "{" "}"
685688
else params.box
686689
in
687690
{params with box}
688691

689-
let collection_pat (c : Conf.t) ~ctx ~space_around opn cls =
692+
let collection_pat (c : Conf.t) ~ctx ~space_around ~pat opn cls =
690693
let params = collection_expr c ~space_around opn cls in
691694
let box =
692695
if c.fmt_opts.dock_collection_brackets.v then
693-
box_collec c 0 >> box_pattern_docked c ~ctx ~space_around opn cls
696+
box_collec c 0 >> box_pattern_docked c ~ctx ~space_around ~pat opn cls
694697
else params.box
695698
in
696699
{params with box}
697700

698-
let get_list_pat (c : Conf.t) ~ctx =
699-
collection_pat c ~ctx ~space_around:c.fmt_opts.space_around_lists.v "[" "]"
701+
let get_list_pat (c : Conf.t) ~ctx pat =
702+
collection_pat c ~ctx ~space_around:c.fmt_opts.space_around_lists.v ~pat
703+
"[" "]"
700704

701-
let get_array_pat (c : Conf.t) ~ctx =
702-
collection_pat c ~ctx ~space_around:c.fmt_opts.space_around_arrays.v "[|"
703-
"|]"
705+
let get_array_pat (c : Conf.t) ~ctx pat =
706+
collection_pat c ~ctx ~space_around:c.fmt_opts.space_around_arrays.v ~pat
707+
"[|" "|]"
704708

705709
type if_then_else =
706710
{ box_branch: Fmt.t -> Fmt.t

lib/Params.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ val get_list_expr : Conf.t -> elements_collection
163163

164164
val get_array_expr : Conf.t -> elements_collection
165165

166-
val get_record_pat : Conf.t -> ctx:Ast.t -> elements_collection
166+
val get_record_pat : Conf.t -> ctx:Ast.t -> pattern -> elements_collection
167167

168-
val get_list_pat : Conf.t -> ctx:Ast.t -> elements_collection
168+
val get_list_pat : Conf.t -> ctx:Ast.t -> pattern -> elements_collection
169169

170-
val get_array_pat : Conf.t -> ctx:Ast.t -> elements_collection
170+
val get_array_pat : Conf.t -> ctx:Ast.t -> pattern -> elements_collection
171171

172172
type if_then_else =
173173
{ box_branch: Fmt.t -> Fmt.t

test/passing/dune.inc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4711,6 +4711,24 @@
47114711
(package ocamlformat)
47124712
(action (diff tests/record-402.ml.err record-402.ml.stderr)))
47134713

4714+
(rule
4715+
(deps tests/.ocamlformat )
4716+
(package ocamlformat)
4717+
(action
4718+
(with-stdout-to record-default.ml.stdout
4719+
(with-stderr-to record-default.ml.stderr
4720+
(run %{bin:ocamlformat} --margin-check --profile=default --max-iter=3 %{dep:tests/record.ml})))))
4721+
4722+
(rule
4723+
(alias runtest)
4724+
(package ocamlformat)
4725+
(action (diff tests/record-default.ml.ref record-default.ml.stdout)))
4726+
4727+
(rule
4728+
(alias runtest)
4729+
(package ocamlformat)
4730+
(action (diff tests/record-default.ml.err record-default.ml.stderr)))
4731+
47144732
(rule
47154733
(deps tests/.ocamlformat )
47164734
(package ocamlformat)

test/passing/tests/record-402.ml.ref

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,12 @@ type t =
5858
{ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:
5959
YYYYYYYYYYYYYYYYYYYYY.t
6060
(* ____________________________________ *) }
61+
62+
let _ =
63+
let _ = function
64+
| { foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
65+
; foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo }
66+
->
67+
()
68+
in
69+
()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Warning: tests/record.ml:8 exceeds the margin
2+
Warning: tests/record.ml:16 exceeds the margin
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--profile=default
2+
--max-iter=3
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
type t = { x : int; y : int }
2+
3+
let _ = { x = 1; y = 2 }
4+
let _ = { !e with a; b = c }
5+
let _ = { !(f e) with a; b = c }
6+
7+
let _ =
8+
{
9+
!looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
10+
with
11+
a;
12+
b = c;
13+
}
14+
15+
let _ =
16+
{
17+
!looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
18+
with
19+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
20+
b = c;
21+
}
22+
23+
let _ = { (a : t) with a; b; c }
24+
let _ = { (f a) with a; b; c }
25+
26+
let _ =
27+
{
28+
(a;
29+
a)
30+
with
31+
a;
32+
b;
33+
c;
34+
}
35+
36+
let _ = { (if x then e else e) with e1; e2 }
37+
let _ = { (match x with x -> e) with e1; e2 }
38+
let _ = { (x : x) with e1; e2 }
39+
let _ = { (x :> x) with e1; e2 }
40+
let _ = { (x#x) with e1; e2 }
41+
let f ~l:{ f; g } = e
42+
let f ?l:({ f; g }) = e
43+
let _ = { a; b = (match b with `A -> A | `B -> B | `C -> C : c); c }
44+
let a () = A { A.a : t }
45+
let x = { aaaaaaaaaa (* b *); b }
46+
let x = { aaaaaaaaaa (* b *); b }
47+
48+
type t = { a : (module S); b : (module S) }
49+
50+
let _ = { a = (module M : S); b = (module M : S) }
51+
let to_string { x; _ (* we should print y *) } = string_of_int x
52+
let { x : t } = x
53+
54+
type t = {
55+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx :
56+
YYYYYYYYYYYYYYYYYYYYY.t;
57+
(* ____________________________________ *)
58+
}
59+
60+
let _ =
61+
let _ = function
62+
| {
63+
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo;
64+
foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo;
65+
} ->
66+
()
67+
in
68+
()

test/passing/tests/record-loose.ml.ref

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,12 @@ type t =
5858
{ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx :
5959
YYYYYYYYYYYYYYYYYYYYY.t
6060
(* ____________________________________ *) }
61+
62+
let _ =
63+
let _ = function
64+
| { foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
65+
; foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo }
66+
->
67+
()
68+
in
69+
()

test/passing/tests/record-tight_decl.ml.ref

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,12 @@ type t =
5858
{ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:
5959
YYYYYYYYYYYYYYYYYYYYY.t
6060
(* ____________________________________ *) }
61+
62+
let _ =
63+
let _ = function
64+
| { foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
65+
; foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo }
66+
->
67+
()
68+
in
69+
()

0 commit comments

Comments
 (0)