Skip to content

Commit 68d651a

Browse files
committed
Fix comments getting dropped by letop-punning=always
1 parent 464f4ca commit 68d651a

21 files changed

+262
-61
lines changed

lib/Extended_ast.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ module Parse = struct
241241
b.pbop_is_pun
242242
||
243243
match (b.pbop_pat.ppat_desc, b.pbop_exp.pexp_desc) with
244-
| Ppat_var {txt= v; _}, Pexp_ident {txt= Lident e; _} ->
245-
String.equal v e
244+
| Ppat_var {txt; _}, Pexp_ident {txt= Lident e; _} ->
245+
String.equal txt e
246246
| _ -> false )
247247
in
248248
{b with pbop_loc= {b.pbop_loc with loc_start; loc_end}; pbop_is_pun}
@@ -252,6 +252,8 @@ module Parse = struct
252252
let value_bindings (m : Ast_mapper.mapper) vbs =
253253
let punning is_extension vb =
254254
let is_extension =
255+
(* [and] nodes don't have extensions, so we need to track if the
256+
earlier [let] did *)
255257
is_extension || Option.is_some vb.pvb_attributes.attrs_extension
256258
in
257259
let pvb_is_pun =
@@ -264,10 +266,10 @@ module Parse = struct
264266
vb.pvb_is_pun
265267
||
266268
match (vb.pvb_pat.ppat_desc, vb.pvb_body) with
267-
| ( Ppat_var {txt= v; _}
269+
| ( Ppat_var {txt; _}
268270
, Pfunction_body {pexp_desc= Pexp_ident {txt= Lident e; _}; _}
269271
) ->
270-
String.equal v e
272+
String.equal txt e
271273
| _ -> false )
272274
in
273275
(is_extension, {vb with pvb_is_pun})

lib/Fmt_ast.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,14 +2572,16 @@ and fmt_expression c ?(box = true) ?(pro = noop) ?eol ?parens
25722572
$ fmt_atrs ) )
25732573
| Pexp_let (lbs, body, loc_in) ->
25742574
let bindings =
2575-
Sugar.Let_binding.of_let_bindings ~ctx lbs.pvbs_bindings
2575+
Sugar.Let_binding.of_let_bindings ~ctx ~cmts:c.cmts lbs.pvbs_bindings
25762576
in
25772577
let fmt_expr = fmt_expression c (sub_exp ~ctx body) in
25782578
pro
25792579
$ fmt_let_bindings c ~ctx0:ctx ~parens ~fmt_atrs ~fmt_expr ~has_attr
25802580
~loc_in lbs.pvbs_rec bindings body
25812581
| Pexp_letop {let_; ands; body; loc_in} ->
2582-
let bd = Sugar.Let_binding.of_binding_ops (let_ :: ands) in
2582+
let bd =
2583+
Sugar.Let_binding.of_binding_ops ~cmts:c.cmts (let_ :: ands)
2584+
in
25832585
let fmt_expr = fmt_expression c (sub_exp ~ctx body) in
25842586
pro
25852587
$ fmt_let_bindings c ~ctx0:ctx ~parens ~fmt_atrs ~fmt_expr ~has_attr
@@ -3268,7 +3270,7 @@ and fmt_class_expr c ({ast= exp; ctx= ctx0} as xexp) =
32683270
| _ -> c.conf.fmt_opts.indent_after_in.v
32693271
in
32703272
let bindings =
3271-
Sugar.Let_binding.of_let_bindings ~ctx lbs.pvbs_bindings
3273+
Sugar.Let_binding.of_let_bindings ~ctx ~cmts:c.cmts lbs.pvbs_bindings
32723274
in
32733275
let fmt_expr = fmt_class_expr c (sub_cl ~ctx body) in
32743276
let has_attr = not (List.is_empty pcl_attributes) in
@@ -4696,7 +4698,9 @@ and fmt_structure_item c ~last:last_item ~semisemi {ctx= parent_ctx; ast= si}
46964698
let fmt_item c ctx ~prev ~next b =
46974699
let first = Option.is_none prev in
46984700
let last = Option.is_none next in
4699-
let b = Sugar.Let_binding.of_let_binding ~ctx ~first b in
4701+
let b =
4702+
Sugar.Let_binding.of_let_binding ~ctx ~first ~cmts:c.cmts b
4703+
in
47004704
let epi =
47014705
match c.conf.fmt_opts.let_binding_spacing.v with
47024706
| `Compact -> None

lib/Sugar.ml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,15 @@ module Let_binding = struct
123123
; lb_attrs: ext_attrs
124124
; lb_loc: Location.t }
125125

126-
let of_let_binding ~ctx ~first vb =
126+
let of_let_binding ~ctx ~first ~cmts vb =
127+
if vb.pvb_is_pun then
128+
Cmts.relocate cmts
129+
~src:
130+
( match vb.pvb_body with
131+
| Pfunction_body e -> e.pexp_loc
132+
| Pfunction_cases (_, l, _) ->
133+
(* NB: should be impossible for puns *) l )
134+
~before:vb.pvb_pat.ppat_loc ~after:vb.pvb_pat.ppat_loc ;
127135
{ lb_op= Location.{txt= (if first then "let" else "and"); loc= none}
128136
; lb_pat= sub_pat ~ctx vb.pvb_pat
129137
; lb_args= vb.pvb_args
@@ -133,11 +141,14 @@ module Let_binding = struct
133141
; lb_attrs= vb.pvb_attributes
134142
; lb_loc= vb.pvb_loc }
135143

136-
let of_let_bindings ~ctx =
137-
List.mapi ~f:(fun i -> of_let_binding ~ctx ~first:(i = 0))
144+
let of_let_bindings ~ctx ~cmts =
145+
List.mapi ~f:(fun i -> of_let_binding ~ctx ~first:(i = 0) ~cmts)
138146

139-
let of_binding_ops bos =
147+
let of_binding_ops ~cmts bos =
140148
List.map bos ~f:(fun bo ->
149+
if bo.pbop_is_pun then
150+
Cmts.relocate cmts ~src:bo.pbop_exp.pexp_loc
151+
~before:bo.pbop_pat.ppat_loc ~after:bo.pbop_pat.ppat_loc ;
141152
let ctx = Bo bo in
142153
{ lb_op= bo.pbop_op
143154
; lb_pat= sub_pat ~ctx bo.pbop_pat

lib/Sugar.mli

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ module Let_binding : sig
4747
; lb_attrs: ext_attrs
4848
; lb_loc: Location.t }
4949

50-
val of_let_binding : ctx:Ast.t -> first:bool -> value_binding -> t
50+
val of_let_binding :
51+
ctx:Ast.t -> first:bool -> cmts:Cmts.t -> value_binding -> t
5152

52-
val of_let_bindings : ctx:Ast.t -> value_binding list -> t list
53+
val of_let_bindings :
54+
ctx:Ast.t -> cmts:Cmts.t -> value_binding list -> t list
5355

54-
val of_binding_ops : binding_op list -> t list
56+
val of_binding_ops : cmts:Cmts.t -> binding_op list -> t list
5557
end

test/failing/dune.inc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,6 @@
181181
(package ocamlformat)
182182
(action (diff tests/js_test.ml.broken-ref js_test.ml.output)))
183183

184-
(rule
185-
(deps tests/.ocamlformat )
186-
(package ocamlformat)
187-
(action
188-
(with-outputs-to letop_broken.ml.output
189-
(with-accepted-exit-codes 1
190-
(run %{bin:ocamlformat} --ocaml-version=4.14 --letop-punning=always %{dep:tests/letop_broken.ml})))))
191-
192-
(rule
193-
(alias runtest)
194-
(package ocamlformat)
195-
(action (diff tests/letop_broken.ml.broken-ref letop_broken.ml.output)))
196-
197184
(rule
198185
(deps tests/.ocamlformat )
199186
(package ocamlformat)

test/failing/tests/letop_broken.ml

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/failing/tests/letop_broken.ml.broken-ref

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/failing/tests/letop_broken.ml.opts

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,19 @@ let q =
1919
and y = y
2020
and z = z in
2121
x, y, z
22+
23+
let r =
24+
let* (* 1 *) x (* 2 *) = (* 3 *) x (* 4 *)
25+
and* (* 5 *) y =
26+
y
27+
(* 6 *)
28+
in
29+
x, y
30+
31+
let s =
32+
let%foo (* 1 *) x (* 2 *) = (* 3 *) x (* 4 *)
33+
and (* 5 *) y =
34+
y
35+
(* 6 *)
36+
in
37+
x, y

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,25 @@ let q =
1919
and y
2020
and z in
2121
x, y, z
22+
23+
let r =
24+
let*
25+
(* 1 *)
26+
(* 3 *) x
27+
(* 2 *)
28+
(* 4 *)
29+
and* (* 5 *) y
30+
(* 6 *)
31+
in
32+
x, y
33+
34+
let s =
35+
let%foo
36+
(* 1 *)
37+
(* 3 *) x
38+
(* 2 *)
39+
(* 4 *)
40+
and (* 5 *) y
41+
(* 6 *)
42+
in
43+
x, y

0 commit comments

Comments
 (0)