Skip to content

Commit 7cb8a33

Browse files
committed
consider case when a labeled tuple element has an infix expression so that it is wrapped in parentheses
only wrap with parentheses if the tuple element has a label, instead of wrapping any infix expression bc of some other tuple element label add cram test adding test case to the end of labeled_tuples.ml
1 parent ca45251 commit 7cb8a33

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

lib/Fmt_ast.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,12 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
22192219
; _ }
22202220
when not (Std_longident.is_infix id) ->
22212221
has_attr && parens
2222+
| Exp {pexp_desc= Pexp_tuple tuples; _}
2223+
when List.exists tuples ~f:(fun tuple ->
2224+
match tuple with
2225+
| Lte_simple {lte_label= Some _; lte_elt} -> phys_equal lte_elt exp
2226+
| _ -> false ) ->
2227+
true
22222228
| Lb {pvb_body= Pfunction_body body; _} when phys_equal body exp ->
22232229
has_attr && parens
22242230
| _ -> has_attr && not parens

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,11 @@ let
629629
(* after y *),
630630
(* bz *)
631631
~z:(*zv*) 0 (*end*) )
632+
633+
(** With infix expressions **)
634+
let labeled_tuple () = ~x:1, ~y:(1 + 2)
635+
636+
let labeled_tuple2 () = 1 + 2, ~y:1
637+
let labeled_tuple3 () = 1 + 2, ~y:(1 + 2)
638+
let ~x, .. = labeled_tuple ()
639+
let ~y, .. = labeled_tuple2 ()

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,3 +568,11 @@ let
568568
(* after y *),
569569
(* bz *)
570570
~z:(*zv*) 0 (*end*) )
571+
572+
(** With infix expressions **)
573+
let labeled_tuple () = (~x:1, ~y:(1 + 2))
574+
575+
let labeled_tuple2 () = (1 + 2, ~y:1)
576+
let labeled_tuple3 () = (1 + 2, ~y:(1 + 2))
577+
let ~x, .. = labeled_tuple ()
578+
let ~y, .. = labeled_tuple2 ()

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,11 @@ let
628628
, (* bz *)
629629
~z:(*zv*) 0 (*end*) )
630630
;;
631+
632+
(** With infix expressions **)
633+
let labeled_tuple () = ~x:1, ~y:(1 + 2)
634+
635+
let labeled_tuple2 () = 1 + 2, ~y:1
636+
let labeled_tuple3 () = 1 + 2, ~y:(1 + 2)
637+
let ~x, .. = labeled_tuple ()
638+
let ~y, .. = labeled_tuple2 ()

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,14 @@ let
634634
(* after y *)
635635
, (* bz *)
636636
~z:(*zv*) 0 (*end*) )
637+
638+
(** With infix expressions **)
639+
let labeled_tuple () = (~x:1, ~y:(1 + 2))
640+
641+
let labeled_tuple2 () = (1 + 2, ~y:1)
642+
643+
let labeled_tuple3 () = (1 + 2, ~y:(1 + 2))
644+
645+
let ~x, .. = labeled_tuple ()
646+
647+
let ~y, .. = labeled_tuple2 ()

test/passing/tests/labeled_tuples.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,10 @@ let (*before*) ~(x (*l-ty*):int (*after-ty*)) (*after x*),
657657
(*before y*) ~y (* after y *), (* bz *) ~z:(*zv*)0 (*end*) =
658658
(*before*) ~(x (*l-ty*):int (*after-ty*)) (*after x*),
659659
(*before y*) ~y (* after y *), (* bz *)~z:(*zv*)0 (*end*)
660+
661+
(** With infix expressions **)
662+
let labeled_tuple () = (~x:1, ~y:(1 + 2))
663+
let labeled_tuple2 () = (1 + 2, ~y:1)
664+
let labeled_tuple3 () = (1 + 2, ~y:(1 + 2))
665+
let ~x, .. = labeled_tuple ()
666+
let ~y, .. = labeled_tuple2 ()

0 commit comments

Comments
 (0)