lspitzner / brittany

haskell source code formatter
GNU Affero General Public License v3.0
690 stars 72 forks source link

List in last line of do-block prevents formatting #296

Open gutjuri opened 4 years ago

gutjuri commented 4 years ago

I have the following function:

wnums :: [Double] -> [Double]
wnums nums = do
  a  <- nums
  b  <- nums \\ [a]
  c  <- nums \\ [a, b]
  d  <- nums \\ [a, b, c]
  o1 <- opts
  o2 <- opts
  o3 <- opts
  [   (a `o1` b) `o2` (c `o3` d)
    , ((a `o1` b) `o2` c) `o3` d
    , a `o1` (b `o2` (c `o3` d))
    , (a `o1` (b `o2` c)) `o3` d
    , a `o1` ((b `o2` c) `o3` d)
    ]

This function compiles but when i run brittany on the source file then it errors out with ERROR: brittany pretty printer returned syntactically invalid result.

Without this function the source file can be reformatted just fine.

tfausak commented 4 years ago

Sounds related to #290, in that formatting things in do notation is different that regular code. Here's what Brittany 0.12.1.1 outputs (using --output-on-errors):

ERROR: brittany pretty printer returned syntactically invalid result.
wnums :: [Double] -> [Double]
wnums nums = do
  a  <- nums
  b  <- nums \\ [a]
  c  <- nums \\ [a, b]
  d  <- nums \\ [a, b, c]
  o1 <- opts
  o2 <- opts
  o3 <- opts
  [ (a `o1` b) `o2` (c `o3` d)
  , ((a `o1` b) `o2` c) `o3` d
  , a `o1` (b `o2` (c `o3` d))
  , (a `o1` (b `o2` c)) `o3` d
  , a `o1` ((b `o2` c) `o3` d)
  ]

As a workaround, you can pass the list to a function like id. Or you can explicitly build the list using (:).