tweag / ormolu

A formatter for Haskell source code
https://ormolu-live.tweag.io
Other
958 stars 83 forks source link

AST differs with multiline splice #707

Closed waj closed 3 years ago

waj commented 3 years ago

Describe the bug A multiline splice in a do block followed with an argument fails with a different AST

To Reproduce Try to format this code:

foo = do
  $( bar
   ) baz

Environment

amesgen commented 3 years ago

I pushed a fix for this specific problem in amesgen/issue-707, but the problem goes deeper:


Consider this:

```haskell foo = case 1 of 1 -> (+1) 1 ``` ```haskell foo = do case 1 of 1 -> (+1) 1 ```

Note that both are valid Haskell code with BlockArguments (foo == 2). Formatting them (the second snippet needs --unsafe) results in

```haskell foo = case 1 of 1 -> (+1) 1 ``` ```haskell foo = do case 1 of 1 -> (+1) 1 ```

and one can see the problem here.


This seems to be a bit similar to #488, and indeed, we could fix this by formatting case blocks with four spaces of indentation and its arguments with two spaces:

```haskell foo = case 1 of 1 -> (+1) 1 ``` ```haskell foo = do case 1 of 1 -> (+1) 1 ```

But this would cause an enormous amount of reformatting, so I am not sure if this is a good idea.

amesgen commented 3 years ago

Another instance of the same problem:

foo = do 
  do
    (+1)
   1
amesgen commented 3 years ago

The least bad solution for the examples above seems to be indenting the arguments by 1, so e.g.

foo = do 
  do
    (+1)
   1

is a fixed point. I implemented this in #731, let's see how it goes.