Skip to content

Fix match in if-then-else branches expanding to vertical with fit-or-vertical#2798

Merged
Julow merged 1 commit into
ocaml-ppx:mainfrom
MisterDA:fix-2766
Apr 23, 2026
Merged

Fix match in if-then-else branches expanding to vertical with fit-or-vertical#2798
Julow merged 1 commit into
ocaml-ppx:mainfrom
MisterDA:fix-2766

Conversation

@MisterDA
Copy link
Copy Markdown
Contributor

When a match expr with appeared as a branch of if-then-else with if-then-else=fit-or-vertical, the match keyword, scrutinee, and with were broken onto separate lines even when they easily fit on one line:

if true then
  match
    expr
  with
  | true -> ()
  | false -> ()

The regression was introduced by 8056fa5 ("Overhaul begin match formatting"), which changed fmt_match to wrap pro inside a nested hvbox 0 (pro_inner $ keyword). When the match is inside an if-then-else branch, the pro carries a break_unless_newline 1000 2 (from the Fit_or_vertical branch_pro). This inflates the inner hvbox's size to 1000+, causing the Format engine to classify the parent hovbox as non-fitting, which forces all breaks in the match .. expr .. with box to break.

Fix: in match_inner_pro, return false for Pexp_ifthenelse context (same as already done for Pexp_infix), so that pro is placed outside the match's inner hvbox as pro_outer, where it does not interfere with the match keyword/expr/with fitting on one line.

Fix #2766. This fix was vibe-clauded.

…vertical

When a `match expr with` appeared as a branch of `if-then-else` with
`if-then-else=fit-or-vertical`, the match keyword, scrutinee, and
`with` were broken onto separate lines even when they easily fit on one
line:

    if true then
      match
        expr
      with
      | true -> ()
      | false -> ()

The regression was introduced by 8056fa5 ("Overhaul begin match
formatting"), which changed `fmt_match` to wrap `pro` inside a nested
`hvbox 0 (pro_inner $ keyword)`. When the match is inside an
if-then-else branch, the `pro` carries a `break_unless_newline 1000 2`
(from the Fit_or_vertical branch_pro). This inflates the inner hvbox's
size to 1000+, causing the Format engine to classify the parent hovbox
as non-fitting, which forces all breaks in the `match .. expr .. with`
box to break.

Fix: in `match_inner_pro`, return `false` for `Pexp_ifthenelse` context
(same as already done for `Pexp_infix`), so that `pro` is placed
outside the match's inner hvbox as `pro_outer`, where it does not
interfere with the match keyword/expr/with fitting on one line.
Copy link
Copy Markdown
Collaborator

@Julow Julow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the right fix, thanks !

@Julow Julow merged commit 5ebeb8e into ocaml-ppx:main Apr 23, 2026
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: match expr with gets spread into 3 lines in 0.28.1

2 participants