Skip to content

Bug: Formatting instability on long if conditions with if-then-else=fit-or-vertical #2770

@TKristof09

Description

@TKristof09

ocamlformat fails with "BUG: formatting did not stabilize after 10 iterations" when using `if-then-else=fit-or-vertical. It seems to happen with the following combination of conditions

  • Long enough condition to create a line break
  • Comment after the then keyword and before the expression of the branch

The formatter seems to oscillate between two comment placements:

(* Format A *)
then (* comment *)
  expr

(* Format B *)
then
  (* comment *)
  expr

How to Reproduce

let test =
  if
    long_function_name_to_force_break a b c d e f g h i j k l m n o p q r s t
  then 
    (* comment *)
    1
  else
    2

Environment

  • ocamlformat version: 0.28.1
  • OS: Linux

Configuration

The bug is triggered by having only if-then-else=fit-or-vertical in the .ocamlformat file. But here is the full config from --print-config:

comment-check=true
debug=false
disable=false
margin-check=false
max-iters=10
ocaml-version=4.04.0
quiet=false
disable-conf-attrs=false
version-check=true
assignment-operator=end-line
break-before-in=fit-or-vertical
break-cases=fit
break-collection-expressions=fit-or-vertical
break-colon=after
break-fun-decl=wrap
break-fun-sig=wrap
break-infix=wrap
break-infix-before-func=false
break-separators=after
break-sequences=true
break-string-literals=auto
break-struct=force
cases-exp-indent=4
cases-matching-exp-indent=normal
disambiguate-non-breaking-match=false
doc-comments=after-when-possible
doc-comments-padding=2
doc-comments-tag-only=default
dock-collection-brackets=true
exp-grouping=preserve
extension-indent=2
field-space=loose
function-indent=2
function-indent-nested=never
if-then-else=fit-or-vertical (file .ocamlformat:1)
indent-after-in=0
indicate-multiline-delimiters=no
indicate-nested-or-patterns=unsafe-no
infix-precedence=indent
leading-nested-match-parens=false
let-and=compact
let-binding-indent=2
let-binding-deindent-fun=true
let-binding-spacing=compact
let-module=compact
line-endings=lf
margin=80
match-indent=0
match-indent-nested=never
max-indent=68
module-indent=2
module-item-spacing=compact
nested-match=wrap
ocp-indent-compat=false
parens-ite=false
parens-tuple=always
parens-tuple-patterns=multi-line-only
parse-docstrings=true
parse-toplevel-phrases=false
sequence-blank-line=preserve-one
sequence-style=terminator
single-case=compact
space-around-arrays=true
space-around-lists=true
space-around-records=true
space-around-variants=true
stritem-extension-indent=0
type-decl=compact
type-decl-indent=2
wrap-comments=false
wrap-fun-args=true
profile=default

Potentially related to issue #2749 as that also has a similar instability with comments but with different config.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions