Skip to content

Comment after 'when' on its own line is lost #3521

Closed

Description

@nojaf

Issue created from fantomas-online

Code

match x with
| _
    when // c
        a -> b

Result

match x with
| _ when a -> b

Problem description

A line comment directly after when is lost when when starts its own line below the pattern. The output is valid F#, but the comment is gone.

It needs only the default settings. It does not happen when:

  • when is on the same line as the pattern (| _ when // c keeps the comment),
  • the comment is on its own line after when,
  • the comment is after the pattern and before when (| _ // c then when a -> b).

The same happens in function and in try ... with.

It is not a regression: 8.0.7, 7.0.6, 6.3.16 and 5.2.4 drop the comment too, and so does main.

The untyped tree gives when no range (SynMatchClauseTrivia has ArrowRange and BarRange only), so the Oak has no node on the comment's line to attach it to. The pattern ends on the line above and the guard starts on the line below. #1851 was the same problem for a comment after match.

The code comes from the F# compiler, src/Compiler/Checking/ConstraintSolver.fs (two when // Ignore any explicit ... overloads guards).

Extra information

  • The formatted result breaks my code.
  • The formatted result gives compiler warnings.
  • I or my company would be willing to help fix this.
  • I would like a release if this problem is solved.

Options

Fantomas main branch at 2026-10-06T23:08:00Z - 6276e9e

Default Fantomas configuration

Did you know that you can ignore files when formatting by using a .fantomasignore file?
PS: It's unlikely that someone else will solve your specific issue, as it's something that you have a personal stake in.

Activity

  1. added a commit that references this issue on Oct 16, 2026
    de77b27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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