Skip to content

Comment between a match pattern and its when guard gives invalid code #3560

Closed

Description

@nojaf

Issue created from fantomas-online

Code

match x with
| a
// c
    when a -> 1
| _ -> 0

Result

match x with
| a
// c
when a -> 1
| _ -> 0

Problem description

Fantomas found this wrong with its result:

  • The result is not valid F#
    • Fantomas cannot parse the code it produced. The diagnostics are about that code.

A comment on its own line between a match clause's pattern and its when guard puts when on the column of the |, where it ends the clause, so the result does not parse. It does not matter whether the comment is on the | column or indented. Without the line break, | a // c followed by when a -> 1 on the next line, the result is valid, but the comment moves after when.

Regression in 8.0.8: 8.0.7 formats it validly (as | a when / // c / a / -> / 1). 8.0.8, 8.0.10, main and the 9.0.0-alpha-001 preview all fail. 8.0.8 is the release that fixed #3521 (comment after when on its own line is lost), by adding the range of the when keyword to the syntax tree, so that change looks related.

Found by formatting the vendored F# compiler's src/Compiler/SyntaxTree/LexFilter.fs, which has this shape (the CtxtFun offside rule).

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-20T12:59:18Z - 79babfa

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 Dec 1, 2026
    53e396a
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