@@ -93,18 +93,29 @@ let has_trailing_comments tbl loc =
9393 | None -> false
9494 | _ -> true
9595
96- (* Match the full argument location used by comment attachment. *)
97- let argument_loc (lbl , (arg : Parsetree.expression )) =
98- match lbl with
99- | Asttypes. Labelled {loc} | Optional {loc} ->
100- {loc with loc_end = arg.pexp_loc.loc_end}
101- | Nolabel -> arg.pexp_loc
102-
10396let has_leading_comments tbl loc =
10497 match Hashtbl. find_opt tbl.Comment_table. leading loc with
10598 | None -> false
10699 | _ -> true
107100
101+ (* Compact layouts print expression comments but not comments on the full
102+ * labeled argument. For unlabeled arguments, leading expression comments
103+ * already work in the compact layout; forcing a different layout can make
104+ * comments exposed by removing parameter parentheses unstable. *)
105+ let argument_requires_regular_layout cmt_tbl ((lbl , arg ) as argument ) =
106+ let loc = Parsetree_viewer. argument_loc argument in
107+ let has_leading_label_comments =
108+ match lbl with
109+ | Asttypes. Nolabel -> false
110+ | Labelled _ | Optional _ ->
111+ has_leading_comments cmt_tbl loc
112+ || has_leading_comments cmt_tbl arg.Parsetree. pexp_loc
113+ in
114+ (* Trailing comments can escape compact layouts when the body breaks. *)
115+ has_leading_label_comments
116+ || has_trailing_comments cmt_tbl loc
117+ || has_trailing_comments cmt_tbl arg.pexp_loc
118+
108119let print_multiline_comment_content txt =
109120 (* Turns
110121 * |* first line
@@ -4533,35 +4544,19 @@ and print_pexp_apply ~state expr cmt_tbl =
45334544 | Braced braces -> print_braces doc call_expr braces
45344545 | Nothing -> doc
45354546 in
4536- (* Use the regular layout for comments attached to arguments. Compact
4537- * callback layouts can detach trailing comments when the body breaks and
4538- * skip leading comments attached to the full labeled argument. *)
4539- let args_have_comments =
4540- List. exists
4541- (fun (lbl , (arg : Parsetree.expression )) ->
4542- let loc = argument_loc (lbl, arg) in
4543- let has_leading_label_comments =
4544- match lbl with
4545- | Asttypes. Nolabel -> false
4546- | Labelled _ | Optional _ ->
4547- has_leading_comments cmt_tbl loc
4548- || has_leading_comments cmt_tbl arg.pexp_loc
4549- in
4550- has_leading_label_comments
4551- || has_trailing_comments cmt_tbl loc
4552- || has_trailing_comments cmt_tbl arg.pexp_loc)
4553- args
4547+ let requires_regular_layout =
4548+ List. exists (argument_requires_regular_layout cmt_tbl) args
45544549 in
45554550 let args_doc, maybe_break_parent =
45564551 if
4557- (not args_have_comments )
4552+ (not requires_regular_layout )
45584553 && Parsetree_viewer. requires_special_callback_printing_first_arg args
45594554 then
45604555 ( print_arguments_with_callback_in_first_position ~state ~partial args
45614556 cmt_tbl,
45624557 Doc. nil )
45634558 else if
4564- (not args_have_comments )
4559+ (not requires_regular_layout )
45654560 && Parsetree_viewer. requires_special_callback_printing_last_arg args
45664561 then
45674562 let args_doc =
@@ -5137,7 +5132,8 @@ and print_arguments ~state ~partial
51375132 List. exists
51385133 (fun ((_ , arg ) as argument ) ->
51395134 Parsetree_viewer. is_fun_expr arg
5140- && (has_any_trailing_line_comment cmt_tbl (argument_loc argument)
5135+ && (has_any_trailing_line_comment cmt_tbl
5136+ (Parsetree_viewer. argument_loc argument)
51415137 || has_any_trailing_line_comment cmt_tbl arg.pexp_loc))
51425138 args
51435139 in
0 commit comments