Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insertion of a newline changes precedence of the ||> operator #2866

Closed
1 of 3 tasks
ljb opened this issue Apr 26, 2023 · 1 comment · Fixed by #3094
Closed
1 of 3 tasks

Insertion of a newline changes precedence of the ||> operator #2866

ljb opened this issue Apr 26, 2023 · 1 comment · Fixed by #3094

Comments

@ljb
Copy link

ljb commented Apr 26, 2023

Issue created from fantomas-online

Code

module Test

let createTuple (a: string) (b: string) = a, b

let value =
    match "string" with
    | "value" -> "1", "2"
    | _ -> "111111111111111111111111111111111111111111111111111111111111111111111", "22222222222222222222222222222222222"
    ||> createTuple

Result

module Test

let createTuple (a: string) (b: string) = a, b

let value =
    match "string" with
    | "value" -> "1", "2"
    | _ ->
        "111111111111111111111111111111111111111111111111111111111111111111111",
        "22222222222222222222222222222222222" ||> createTuple

Problem description

Running version 6.0.1 of Fantomas locally results in a slighly different formating compared to the online version:

module Test

let createTuple (a: string) (b: string) = a, b

let value =
    match "string" with
    | "value" -> "1", "2"
    | _ ->
        "111111111111111111111111111111111111111111111111111111111111111111111", "22222222222222222222222222222222222"
    ||> createTuple

Both the code generated by Fantomas online and localy results in the same compilation error. Running dotnet build with the .NET SDK 7.0.203 will give the following error:

C:\repos\test\test.fs(10,9): error FS0001: This expression was expected to have type↔    ''a * 'b'    ↔but here has type↔    'string' [C:\repos\test\test.fsproj]
C:\repos\test\test.fs(10,51): error FS0001: The type 'string' does not match the type 'string * string' [C:\repos\test\test.fsproj]

The problem is that the call to ||> createTuple will be run on the entire match-expression when there isn't a newline. When there is a newline, it will only be run against the line above.

Adding parenthesis around the entire match expression before calling createTuple is a workaround.

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 main branch at 04/26/2023

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?

@nojaf
Copy link
Contributor

nojaf commented Apr 28, 2023

Hello, thank you for reporting this issue.

We have seen this problem pop up as well when the indent_size is 2.
As a workaround there we have wrapped the match expression in parentheses (as a last resort).

https://github.com/nojaf/fantomas/blob/f144756d7849a29438c84872f61d043be95fcc67/src/Fantomas.Core/CodePrinter.fs#L2083-L2103

I think we could extend this check to ctx.Config.IndentSize - 1 (as seems to be the case for IsIfThenElse _).

Are you interested in submitting a PR for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants