Skip to content

Commit 6592d7f

Browse files
authored
Missing parens in module type (M with type ..) -> N (#2759)
* Missing parens in module type `(M with type ..) -> N` Fix the parenthesing rules for `with type` inside functors and functors inside `with type`. * Fix short-syntax functor formatting
1 parent cb01918 commit 6592d7f

File tree

17 files changed

+367
-49
lines changed

17 files changed

+367
-49
lines changed

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ profile. This started with version 0.26.0.
2323
only use punning when it exists in the source.
2424
This also applies to `let%ext` bindings (#2747, @WardBrian).
2525

26-
- Support the unnamed functor parameters syntax in module types (#2755, @Julow)
26+
- Support the unnamed functor parameters syntax in module types
27+
(#2755, #2759, @Julow)
2728
```ocaml
2829
module type F = ARG -> S
2930
```

lib/Ast.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,12 +1914,9 @@ end = struct
19141914
Mty.has_trailing_attributes mty
19151915
||
19161916
match (ctx, mty.pmty_desc) with
1917-
| Mty {pmty_desc= Pmty_with _; _}, Pmty_with _ -> true
1918-
| ( Mty
1919-
{ pmty_desc=
1920-
Pmty_with (lhs, _) | Pmty_functor (Pfunctorty_unnamed lhs, _)
1921-
; _ }
1922-
, Pmty_functor _ )
1917+
| Mty {pmty_desc= Pmty_with _; _}, (Pmty_with _ | Pmty_functor _) -> true
1918+
| ( Mty {pmty_desc= Pmty_functor (Pfunctorty_unnamed lhs, _); _}
1919+
, (Pmty_with _ | Pmty_functor _) )
19231920
when lhs == mty ->
19241921
true
19251922
| _ -> false

lib/Fmt_ast.ml

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3870,7 +3870,7 @@ and fmt_functor_param_type c ctx = function
38703870
| Pfunctorty_unnamed arg ->
38713871
compose_module (fmt_module_type c (sub_mty ~ctx arg)) ~f:Fn.id
38723872

3873-
and fmt_module_type c ?(rec_ = false) ({ast= mty; _} as xmty) =
3873+
and fmt_module_type c ?(rec_ = false) ({ast= mty; ctx= ctx0} as xmty) =
38743874
let ctx = Mty mty in
38753875
let {pmty_desc; pmty_loc; pmty_attributes} = mty in
38763876
update_config_maybe_disabled_block c pmty_loc pmty_attributes
@@ -3916,8 +3916,19 @@ and fmt_module_type c ?(rec_ = false) ({ast= mty; _} as xmty) =
39163916
$ fmt_attributes_and_docstrings c pmty_attributes ) }
39173917
| Pmty_functor (paramty, mt) ->
39183918
let blk = fmt_module_type c (sub_mty ~ctx mt) in
3919+
let opn, cls =
3920+
match ctx0 with
3921+
(* Functor argument might not be boxed. Force a box when using the
3922+
short syntax. *)
3923+
| Mty {pmty_desc= Pmty_functor (Pfunctorty_unnamed lhs, _); _}
3924+
when phys_equal lhs mty ->
3925+
(Some (open_hovbox 2), close_box)
3926+
| _ -> (blk.opn, blk.cls)
3927+
in
39193928
{ blk with
3920-
pro=
3929+
opn
3930+
; cls
3931+
; pro=
39213932
Some
39223933
( Cmts.fmt_before c pmty_loc
39233934
$ fmt_if parens (str "(")
@@ -4162,10 +4173,23 @@ and fmt_module c ctx ?rec_ ?epi ?(can_sparse = false) keyword ?(eqty = "=")
41624173
let ext = attrs.attrs_extension in
41634174
let blk_t =
41644175
Option.value_map xmty ~default:empty ~f:(fun xmty ->
4176+
let break_before_ty =
4177+
match xmty.ast.pmty_desc with
4178+
(* Break functor types that use the short syntax and avoid
4179+
misaligning the parameter types. *)
4180+
| Pmty_functor
4181+
( Pfunctorty_unnamed
4182+
{pmty_desc= Pmty_with _ | Pmty_functor _; _}
4183+
, _ ) ->
4184+
break 1 2
4185+
| _ -> str " "
4186+
in
41654187
let blk = fmt_module_type ?rec_ c xmty in
4188+
let pro =
4189+
str " " $ str eqty $ opt blk.pro (fun pro -> break_before_ty $ pro)
4190+
in
41664191
{ blk with
4167-
pro=
4168-
Some (str " " $ str eqty $ opt blk.pro (fun pro -> str " " $ pro))
4192+
pro= Some pro
41694193
; psp= fmt_if (Option.is_none blk.pro) (break 1 2) $ blk.psp } )
41704194
in
41714195
let blk_b = Option.value_map xbody ~default:empty ~f:(fmt_module_expr c) in

test/passing/refs.ahrefs/functor-414.ml.ref

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,40 @@ functor
132132

133133
module M : (_ : X) -> X = Y
134134

135-
[@@@ocamlformat "break-struct=natural"]
135+
module M = struct
136+
[@@@ocamlformat "break-struct=natural"]
136137

137-
module M = F (struct type t end : sig type t end)
138+
module M = F (struct type t end : sig type t end)
138139

139-
module M = struct type t end
140+
module M = struct type t end
140141

141-
module type S = sig type t end
142+
module type S = sig type t end
143+
end
144+
145+
module Simple : (Parameters with type update_result := state) -> S = M
146+
module Simple : S -> Parameters with type update_result := state = M
147+
148+
module Left_variadic :
149+
(Parameters with type update_result := state * left array)
150+
-> S =
151+
M
152+
module Left_variadic : S ->
153+
Parameters with type update_result := state * left array =
154+
M
155+
module Left_variadic :
156+
(A -> B)
157+
-> (Parameters with type update_result := state * left array)
158+
-> S =
159+
M
160+
module Left_variadic : S
161+
-> (Parameters with type update_result := state * left array)
162+
-> S =
163+
M
164+
module Left_variadic : sig
165+
type t
166+
end
167+
-> (Parameters with type update_result := state * left array)
168+
-> S =
169+
M
170+
171+
module N : S with module type T = (U -> U) = struct end

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,40 @@ functor
132132

133133
module M : (_ : X) -> X = Y
134134

135-
[@@@ocamlformat "break-struct=natural"]
135+
module M = struct
136+
[@@@ocamlformat "break-struct=natural"]
136137

137-
module M = F (struct type t end : sig type t end)
138+
module M = F (struct type t end : sig type t end)
138139

139-
module M = struct type t end
140+
module M = struct type t end
140141

141-
module type S = sig type t end
142+
module type S = sig type t end
143+
end
144+
145+
module Simple : (Parameters with type update_result := state) -> S = M
146+
module Simple : S -> Parameters with type update_result := state = M
147+
148+
module Left_variadic :
149+
(Parameters with type update_result := state * left array)
150+
-> S =
151+
M
152+
module Left_variadic : S ->
153+
Parameters with type update_result := state * left array =
154+
M
155+
module Left_variadic :
156+
(A -> B)
157+
-> (Parameters with type update_result := state * left array)
158+
-> S =
159+
M
160+
module Left_variadic : S
161+
-> (Parameters with type update_result := state * left array)
162+
-> S =
163+
M
164+
module Left_variadic : sig
165+
type t
166+
end
167+
-> (Parameters with type update_result := state * left array)
168+
-> S =
169+
M
170+
171+
module N : S with module type T = (U -> U) = struct end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
module F (* test *) (M : sig
22
type t
33
end) : S
4+
5+
module Simple : (Parameters with type update_result := state) -> S
6+
7+
module Left_variadic :
8+
(Parameters with type update_result := state * left array)
9+
-> S

test/passing/refs.default/functor-414.ml.ref

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,43 @@ functor
113113

114114
module M : (_ : X) -> X = Y
115115

116-
[@@@ocamlformat "break-struct=natural"]
116+
module M = struct
117+
[@@@ocamlformat "break-struct=natural"]
117118

118-
module M = F (struct type t end : sig type t end)
119-
module M = struct type t end
119+
module M = F (struct type t end : sig type t end)
120+
module M = struct type t end
120121

121-
module type S = sig type t end
122+
module type S = sig type t end
123+
end
124+
125+
module Simple : (Parameters with type update_result := state) -> S = M
126+
module Simple : S -> Parameters with type update_result := state = M
127+
128+
module Left_variadic :
129+
(Parameters with type update_result := state * left array)
130+
-> S =
131+
M
132+
133+
module Left_variadic : S ->
134+
Parameters with type update_result := state * left array =
135+
M
136+
137+
module Left_variadic :
138+
(A -> B)
139+
-> (Parameters with type update_result := state * left array)
140+
-> S =
141+
M
142+
143+
module Left_variadic : S
144+
-> (Parameters with type update_result := state * left array)
145+
-> S =
146+
M
147+
148+
module Left_variadic : sig
149+
type t
150+
end
151+
-> (Parameters with type update_result := state * left array)
152+
-> S =
153+
M
154+
155+
module N : S with module type T = (U -> U) = struct end

test/passing/refs.default/functor.ml.ref

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,43 @@ functor
113113

114114
module M : (_ : X) -> X = Y
115115

116-
[@@@ocamlformat "break-struct=natural"]
116+
module M = struct
117+
[@@@ocamlformat "break-struct=natural"]
117118

118-
module M = F (struct type t end : sig type t end)
119-
module M = struct type t end
119+
module M = F (struct type t end : sig type t end)
120+
module M = struct type t end
120121

121-
module type S = sig type t end
122+
module type S = sig type t end
123+
end
124+
125+
module Simple : (Parameters with type update_result := state) -> S = M
126+
module Simple : S -> Parameters with type update_result := state = M
127+
128+
module Left_variadic :
129+
(Parameters with type update_result := state * left array)
130+
-> S =
131+
M
132+
133+
module Left_variadic : S ->
134+
Parameters with type update_result := state * left array =
135+
M
136+
137+
module Left_variadic :
138+
(A -> B)
139+
-> (Parameters with type update_result := state * left array)
140+
-> S =
141+
M
142+
143+
module Left_variadic : S
144+
-> (Parameters with type update_result := state * left array)
145+
-> S =
146+
M
147+
148+
module Left_variadic : sig
149+
type t
150+
end
151+
-> (Parameters with type update_result := state * left array)
152+
-> S =
153+
M
154+
155+
module N : S with module type T = (U -> U) = struct end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
module F (* test *) (M : sig
22
type t
33
end) : S
4+
5+
module Simple : (Parameters with type update_result := state) -> S
6+
7+
module Left_variadic :
8+
(Parameters with type update_result := state * left array)
9+
-> S

test/passing/refs.janestreet/functor-414.ml.ref

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,37 @@ functor
110110

111111
module M : (_ : X) -> X = Y
112112

113-
[@@@ocamlformat "break-struct=natural"]
113+
module M = struct
114+
[@@@ocamlformat "break-struct=natural"]
114115

115-
module M = F (
116-
struct type t end : sig type t end)
116+
module M = F (
117+
struct type t end : sig type t end)
117118

118-
module M = struct type t end
119+
module M = struct type t end
119120

120-
module type S = sig type t end
121+
module type S = sig type t end
122+
end
123+
124+
module Simple : (Parameters with type update_result := state) -> S = M
125+
module Simple : S -> Parameters with type update_result := state = M
126+
module Left_variadic : (Parameters with type update_result := state * left array) -> S = M
127+
module Left_variadic : S -> Parameters with type update_result := state * left array = M
128+
129+
module Left_variadic :
130+
(A -> B)
131+
-> (Parameters with type update_result := state * left array)
132+
-> S =
133+
M
134+
135+
module Left_variadic : S -> (Parameters with type update_result := state * left array) ->
136+
S =
137+
M
138+
139+
module Left_variadic : sig
140+
type t
141+
end
142+
-> (Parameters with type update_result := state * left array)
143+
-> S =
144+
M
145+
146+
module N : S with module type T = (U -> U) = struct end

0 commit comments

Comments
 (0)