Skip to content

Commit 2842c4a

Browse files
committed
Fix formatting of short-syntax functors inside functors
1 parent eff3a27 commit 2842c4a

File tree

10 files changed

+165
-3
lines changed

10 files changed

+165
-3
lines changed

lib/Fmt_ast.ml

Lines changed: 18 additions & 3 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 "(")
@@ -4166,7 +4177,11 @@ and fmt_module c ctx ?rec_ ?epi ?(can_sparse = false) keyword ?(eqty = "=")
41664177
match xmty.ast.pmty_desc with
41674178
(* Break functor types that use the short syntax and avoid
41684179
misaligning the parameter types. *)
4169-
| Pmty_functor (Pfunctorty_unnamed _, _) -> break 1 2
4180+
| Pmty_functor
4181+
( Pfunctorty_unnamed
4182+
{pmty_desc= Pmty_with _ | Pmty_functor _; _}
4183+
, _ ) ->
4184+
break 1 2
41704185
| _ -> str " "
41714186
in
41724187
let blk = fmt_module_type ?rec_ c xmty in

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,20 @@ module Left_variadic :
152152
module Left_variadic : S ->
153153
Parameters with type update_result := state * left array =
154154
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
155170

156171
module N : S with module type T = (U -> U) = struct end

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,20 @@ module Left_variadic :
152152
module Left_variadic : S ->
153153
Parameters with type update_result := state * left array =
154154
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
155170

156171
module N : S with module type T = (U -> U) = struct end

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,22 @@ module Left_variadic : S ->
134134
Parameters with type update_result := state * left array =
135135
M
136136

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+
137155
module N : S with module type T = (U -> U) = struct end

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,22 @@ module Left_variadic : S ->
134134
Parameters with type update_result := state * left array =
135135
M
136136

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+
137155
module N : S with module type T = (U -> U) = struct end

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,22 @@ module Simple : (Parameters with type update_result := state) -> S = M
125125
module Simple : S -> Parameters with type update_result := state = M
126126
module Left_variadic : (Parameters with type update_result := state * left array) -> S = M
127127
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+
128146
module N : S with module type T = (U -> U) = struct end

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,22 @@ module Simple : (Parameters with type update_result := state) -> S = M
125125
module Simple : S -> Parameters with type update_result := state = M
126126
module Left_variadic : (Parameters with type update_result := state * left array) -> S = M
127127
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+
128146
module N : S with module type T = (U -> U) = struct end

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,22 @@ module Left_variadic : S ->
170170
Parameters with type update_result := state * left array =
171171
M
172172

173+
module Left_variadic :
174+
(A -> B)
175+
-> (Parameters with type update_result := state * left array)
176+
-> S =
177+
M
178+
179+
module Left_variadic : S
180+
-> (Parameters with type update_result := state * left array)
181+
-> S =
182+
M
183+
184+
module Left_variadic : sig
185+
type t
186+
end
187+
-> (Parameters with type update_result := state * left array)
188+
-> S =
189+
M
190+
173191
module N : S with module type T = (U -> U) = struct end

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,22 @@ module Left_variadic : S ->
170170
Parameters with type update_result := state * left array =
171171
M
172172

173+
module Left_variadic :
174+
(A -> B)
175+
-> (Parameters with type update_result := state * left array)
176+
-> S =
177+
M
178+
179+
module Left_variadic : S
180+
-> (Parameters with type update_result := state * left array)
181+
-> S =
182+
M
183+
184+
module Left_variadic : sig
185+
type t
186+
end
187+
-> (Parameters with type update_result := state * left array)
188+
-> S =
189+
M
190+
173191
module N : S with module type T = (U -> U) = struct end

test/passing/tests/functor.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,14 @@ module Left_variadic:
148148
(Parameters with type update_result := state * left array) -> S = M
149149
module Left_variadic:
150150
S -> (Parameters with type update_result := state * left array) = M
151+
module Left_variadic:
152+
(A -> B) ->
153+
(Parameters with type update_result := state * left array) -> S = M
154+
module Left_variadic:
155+
S ->
156+
(Parameters with type update_result := state * left array) -> S = M
157+
module Left_variadic:
158+
sig type t end ->
159+
(Parameters with type update_result := state * left array) -> S = M
151160

152161
module N : S with module type T = (U -> U) = struct end

0 commit comments

Comments
 (0)