Skip to content

Commit b8b6652

Browse files
Boris Yakobowskiclaude
andcommitted
Fix comment before in forcing unnecessary line break in let bindings
`let x = value (* comment *) in` was being broken across multiple lines regardless of configuration because comments near the `in` keyword were formatted with `force_break` as their prefix, which unconditionally forced the containing hvbox to vertical mode. Fix: change `~pro:force_break` to `~pro:(break 1 0)` in two places in fmt_value_binding — the comment is now a regular break that becomes a space when the hvbox fits horizontally. When the content exceeds the margin, the box still goes vertical naturally. This also fixes a non-idempotency: previously, `let x = v (* c *) in` and its pre-broken equivalent produced different outputs. The bug affects all profiles (default, ocamlformat, janestreet, ahrefs). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6a4b647 commit b8b6652

29 files changed

Lines changed: 60 additions & 222 deletions

lib/Fmt_ast.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4886,7 +4886,7 @@ and fmt_value_binding c ~ctx0 ~rec_flag ?in_ ?epi
48864886
, fmt_item_attributes c ~pre:(Break (1, 2)) at_at_attrs $ in_ indent
48874887
, fmt_opt epi
48884888
, Cmts.fmt_before c lb_loc
4889-
, Cmts.fmt_after c lb_loc ~pro:force_break )
4889+
, Cmts.fmt_after c lb_loc ~pro:(break 1 0) )
48904890
| None ->
48914891
let epi =
48924892
fmt_item_attributes c ~pre:(Break (1, 0)) at_at_attrs $ fmt_opt epi
@@ -4965,7 +4965,7 @@ and fmt_value_binding c ~ctx0 ~rec_flag ?in_ ?epi
49654965
( hvbox_if toplevel indent decl_and_body
49664966
$ cmts_after
49674967
$ opt loc_in
4968-
(Cmts.fmt_before c ~pro:force_break ~epi:noop ~eol:noop) )
4968+
(Cmts.fmt_before c ~pro:(break 1 0) ~epi:noop ~eol:noop) )
49694969
$ in_ )
49704970
$ opt loc_in (Cmts.fmt_after ~pro:force_break c)
49714971
$ epi )

test/passing/refs.ahrefs/let_binding-deindent-fun.ml.ref

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,12 @@ let rewrite_export =
303303

304304
(* Comment before in should stay on same line when it fits *)
305305
let () =
306-
let threshold =
307-
3
308-
(* max depth *)
309-
in
306+
let threshold = 3 (* max depth *) in
310307
use threshold
311308
;;
312309

313310
let () =
314-
let left _ =
315-
true
316-
(* value vs nothing *)
317-
in
311+
let left _ = true (* value vs nothing *) in
318312
use left
319313
;;
320314

test/passing/refs.ahrefs/let_binding-in_indent.ml.ref

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,12 @@ let rewrite_export =
303303

304304
(* Comment before in should stay on same line when it fits *)
305305
let () =
306-
let threshold =
307-
3
308-
(* max depth *)
309-
in
306+
let threshold = 3 (* max depth *) in
310307
use threshold
311308
;;
312309

313310
let () =
314-
let left _ =
315-
true
316-
(* value vs nothing *)
317-
in
311+
let left _ = true (* value vs nothing *) in
318312
use left
319313
;;
320314

test/passing/refs.ahrefs/let_binding-indent.ml.ref

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,12 @@ let rewrite_export =
303303

304304
(* Comment before in should stay on same line when it fits *)
305305
let () =
306-
let threshold =
307-
3
308-
(* max depth *)
309-
in
306+
let threshold = 3 (* max depth *) in
310307
use threshold
311308
;;
312309

313310
let () =
314-
let left _ =
315-
true
316-
(* value vs nothing *)
317-
in
311+
let left _ = true (* value vs nothing *) in
318312
use left
319313
;;
320314

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,12 @@ let rewrite_export =
303303

304304
(* Comment before in should stay on same line when it fits *)
305305
let () =
306-
let threshold =
307-
3
308-
(* max depth *)
309-
in
306+
let threshold = 3 (* max depth *) in
310307
use threshold
311308
;;
312309

313310
let () =
314-
let left _ =
315-
true
316-
(* value vs nothing *)
317-
in
311+
let left _ = true (* value vs nothing *) in
318312
use left
319313
;;
320314

test/passing/refs.ahrefs/let_punning-denied.ml.ref

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,10 @@ let q =
2222

2323
let r =
2424
let* (* 1 *) x (* 2 *) = (* 3 *) x (* 4 *)
25-
and* (* 5 *) y =
26-
y
27-
(* 6 *)
28-
in
25+
and* (* 5 *) y = y (* 6 *) in
2926
x, y
3027

3128
let s =
3229
let%foo (* 1 *) x (* 2 *) = (* 3 *) x (* 4 *)
33-
and (* 5 *) y =
34-
y
35-
(* 6 *)
36-
in
30+
and (* 5 *) y = y (* 6 *) in
3731
x, y

test/passing/refs.ahrefs/let_punning-preferred.ml.ref

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ let r =
2626
(* 3 *) x
2727
(* 2 *)
2828
(* 4 *)
29-
and* (* 5 *) y
30-
(* 6 *)
31-
in
29+
and* (* 5 *) y (* 6 *) in
3230
x, y
3331

3432
let s =
@@ -37,7 +35,5 @@ let s =
3735
(* 3 *) x
3836
(* 2 *)
3937
(* 4 *)
40-
and (* 5 *) y
41-
(* 6 *)
42-
in
38+
and (* 5 *) y (* 6 *) in
4339
x, y

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@ let q =
2222

2323
let r =
2424
let* (* 1 *) x (* 2 *) = (* 3 *) x (* 4 *)
25-
and* (* 5 *) y
26-
(* 6 *)
27-
in
25+
and* (* 5 *) y (* 6 *) in
2826
x, y
2927

3028
let s =
3129
let%foo (* 1 *) x (* 2 *) = (* 3 *) x (* 4 *)
32-
and (* 5 *) y
33-
(* 6 *)
34-
in
30+
and (* 5 *) y (* 6 *) in
3531
x, y

test/passing/refs.default/let_binding-deindent-fun.ml.ref

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,12 @@ let rewrite_export =
276276

277277
(* Comment before in should stay on same line when it fits *)
278278
let () =
279-
let threshold =
280-
3
281-
(* max depth *)
282-
in
279+
let threshold = 3 (* max depth *) in
283280
use threshold
284281
;;
285282

286283
let () =
287-
let left _ =
288-
true
289-
(* value vs nothing *)
290-
in
284+
let left _ = true (* value vs nothing *) in
291285
use left
292286
;;
293287

test/passing/refs.default/let_binding-in_indent.ml.ref

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,12 @@ let rewrite_export =
276276

277277
(* Comment before in should stay on same line when it fits *)
278278
let () =
279-
let threshold =
280-
3
281-
(* max depth *)
282-
in
279+
let threshold = 3 (* max depth *) in
283280
use threshold
284281
;;
285282

286283
let () =
287-
let left _ =
288-
true
289-
(* value vs nothing *)
290-
in
284+
let left _ = true (* value vs nothing *) in
291285
use left
292286
;;
293287

0 commit comments

Comments
 (0)