Skip to content

Commit baa6c98

Browse files
authored
Fix dropped attributes on begin-end in an if-then-else branch (#2436)
1 parent 253323e commit baa6c98

File tree

7 files changed

+39
-23
lines changed

7 files changed

+39
-23
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Tags:
2929
### Fixed
3030

3131
- Fix dropped attributes on a begin-end in a match case (#2421, @Julow)
32+
- Fix dropped attributes on begin-end in an if-then-else branch (#2436, @gpetiot)
3233
- Fix non-stabilizing comments before a functor type argument (#2420, @Julow)
3334
- Fix crash caused by module types with nested `with module` (#2419, @Julow)
3435
- Fix ';;' formatting between doc-comments and toplevel directives (#2432, @gpetiot)

lib/Fmt_ast.ml

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,7 +2232,7 @@ and fmt_expression c ?(box = true) ?pro ?epi ?eol ?parens ?(indent_wrap = 0)
22322232
$ p.wrap_parens
22332233
( fmt_expression c ?box:p.box_expr
22342234
~parens:parens_exp ?pro:p.expr_pro
2235-
?eol:p.expr_eol xbch
2235+
?eol:p.expr_eol p.branch_expr
22362236
$ p.break_end_branch ) ) )
22372237
$ fmt_if_k (not last) p.space_between_branches ) ) )
22382238
$ fmt_atrs )
@@ -2655,24 +2655,11 @@ and fmt_expression c ?(box = true) ?pro ?epi ?eol ?parens ?(indent_wrap = 0)
26552655
impossible "only used for methods, handled during method formatting"
26562656
| Pexp_hole -> hvbox 0 (fmt_hole () $ fmt_atrs)
26572657
| Pexp_beginend e ->
2658-
let wrap_beginend =
2659-
match ctx0 with
2660-
(* begin-end keywords are handled when printing if-then-else
2661-
branch *)
2662-
| Exp {pexp_desc= Pexp_ifthenelse (_, Some z); _}
2663-
when Base.phys_equal xexp.ast z ->
2664-
Fn.id
2665-
| Exp {pexp_desc= Pexp_ifthenelse (eN, _); _}
2666-
when List.exists eN ~f:(fun x ->
2667-
Base.phys_equal xexp.ast x.if_body ) ->
2668-
Fn.id
2669-
| _ ->
2670-
fun k ->
2671-
let opn = str "begin" $ fmt_extension_suffix c ext
2672-
and cls = str "end" in
2673-
hvbox 0
2674-
( wrap_k opn cls (wrap_k (break 1 2) (break 1000 0) k)
2675-
$ fmt_atrs )
2658+
let wrap_beginend k =
2659+
let opn = str "begin" $ fmt_extension_suffix c ext
2660+
and cls = str "end" in
2661+
hvbox 0
2662+
(wrap_k opn cls (wrap_k (break 1 2) (break 1000 0) k) $ fmt_atrs)
26762663
in
26772664
wrap_beginend
26782665
@@ fmt_expression c ~box ?pro ?epi ?eol ~parens:false ~indent_wrap ?ext

lib/Params.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,16 +462,19 @@ type if_then_else =
462462
; box_expr: bool option
463463
; expr_pro: Fmt.t option
464464
; expr_eol: Fmt.t option
465+
; branch_expr: expression Ast.xt
465466
; break_end_branch: Fmt.t
466467
; space_between_branches: Fmt.t }
467468

468469
let get_if_then_else (c : Conf.t) ~first ~last ~parens_bch ~parens_prev_bch
469470
~xcond ~xbch ~expr_loc ~fmt_extension_suffix ~fmt_attributes ~fmt_cond =
470471
let imd = c.fmt_opts.indicate_multiline_delimiters.v in
471-
let beginend =
472-
match xbch.Ast.ast with
473-
| {pexp_desc= Pexp_beginend _; _} -> true
474-
| _ -> false
472+
let beginend, branch_expr =
473+
let ast = xbch.Ast.ast in
474+
match ast with
475+
| {pexp_desc= Pexp_beginend nested_exp; pexp_attributes= []; _} ->
476+
(true, sub_exp ~ctx:(Exp ast) nested_exp)
477+
| _ -> (false, xbch)
475478
in
476479
let wrap_parens ~wrap_breaks k =
477480
if beginend then wrap "begin" "end" (wrap_breaks k)
@@ -517,6 +520,7 @@ let get_if_then_else (c : Conf.t) ~first ~last ~parens_bch ~parens_prev_bch
517520
; box_expr= Some false
518521
; expr_pro= None
519522
; expr_eol= None
523+
; branch_expr
520524
; break_end_branch= noop
521525
; space_between_branches= fmt "@ " }
522526
| `K_R ->
@@ -528,6 +532,7 @@ let get_if_then_else (c : Conf.t) ~first ~last ~parens_bch ~parens_prev_bch
528532
; box_expr= Some false
529533
; expr_pro= None
530534
; expr_eol= Some (fmt "@;<1 2>")
535+
; branch_expr
531536
; break_end_branch=
532537
fmt_if_k (parens_bch || beginend || not last) (break 1000 0)
533538
; space_between_branches= fmt_if (beginend || parens_bch) " " }
@@ -552,6 +557,7 @@ let get_if_then_else (c : Conf.t) ~first ~last ~parens_bch ~parens_prev_bch
552557
(not (Location.is_single_line expr_loc c.fmt_opts.margin.v))
553558
(break_unless_newline 1000 2) )
554559
; expr_eol= Some (fmt "@;<1 2>")
560+
; branch_expr
555561
; break_end_branch= noop
556562
; space_between_branches=
557563
fmt
@@ -571,6 +577,7 @@ let get_if_then_else (c : Conf.t) ~first ~last ~parens_bch ~parens_prev_bch
571577
; box_expr= None
572578
; expr_pro= Some (break_unless_newline 1000 2)
573579
; expr_eol= None
580+
; branch_expr
574581
; break_end_branch= noop
575582
; space_between_branches=
576583
fmt
@@ -600,6 +607,7 @@ let get_if_then_else (c : Conf.t) ~first ~last ~parens_bch ~parens_prev_bch
600607
; box_expr= Some false
601608
; expr_pro= None
602609
; expr_eol= None
610+
; branch_expr
603611
; break_end_branch= noop
604612
; space_between_branches= fmt "@ " }
605613

lib/Params.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ type if_then_else =
139139
; box_expr: bool option
140140
; expr_pro: Fmt.t option
141141
; expr_eol: Fmt.t option
142+
; branch_expr: expression Ast.xt
142143
; break_end_branch: Fmt.t
143144
; space_between_branches: Fmt.t }
144145

test/passing/tests/exp_grouping-parens.ml.ref

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,10 @@ let x =
328328
| pd -> pd
329329
in
330330
()
331+
332+
let _ =
333+
if something_changed then
334+
begin
335+
loop
336+
end
337+
[@attr]

test/passing/tests/exp_grouping.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,8 @@ let x =
267267
| pd -> pd
268268
in
269269
()
270+
271+
let _ =
272+
if something_changed then begin[@attr]
273+
loop
274+
end

test/passing/tests/exp_grouping.ml.ref

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,10 @@ let x =
382382
| pd -> pd
383383
in
384384
()
385+
386+
let _ =
387+
if something_changed then
388+
begin
389+
loop
390+
end
391+
[@attr]

0 commit comments

Comments
 (0)