fantomas icon indicating copy to clipboard operation
fantomas copied to clipboard

Use of `A | B as c` fails to format correctly

Open dsyme opened this issue 2 years ago • 2 comments

Found this while formatting the F# compiler. It's not a blocker, see workaround below

Issue created from fantomas-online

Code


type T = A | B

let f a  =
    match a with
    | (A | B as bi, x) ->
        1

Result

type T =
    | A
    | B

let f a =
    match a with
    | (A
       | B as bi,
       label1) -> 1

Problem description

Please describe here the Fantomas problem you encountered. Check out our Contribution Guidelines.

Workaround

The workaround is to parenthesize, e.g.


type T = A | B

let f a  =
    match a with
    | ((A | B) as bi, x) ->
        1

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.

Options

Fantomas master branch at 2022-05-30T18:11:41Z - 5e927521516445116087af12c76d83e0dec01332

Default Fantomas configuration

Did you know that you can ignore files when formatting from fantomas-tool or the FAKE targets by using a .fantomasignore file?

dsyme avatar Jun 03 '22 14:06 dsyme

Hello, this rings a bell. If I recall correctly, the main problem is that we format each SynPat.Or inside a clause slightly differently using ASTContext. ASTContext is evil, however: https://github.com/fsprojects/fantomas/blob/5e927521516445116087af12c76d83e0dec01332/src/Fantomas.Core/CodePrinter.fs#L16-L18

I think this problem could be solved by removing that record field and trying to format SynPat.Or differently when encountered in the pattern of a clause handler.

Are you interested in submitting a PR for this?

nojaf avatar Jun 07 '22 06:06 nojaf

I believe this problem is addressed due to the refactor of ASTContext in a7f5b3ea71cad3a4efa4aface2edef8da4b88c91.

nojaf avatar Aug 08 '22 07:08 nojaf