tweag / ormolu

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

SCC annotation in do block causes parse error in formatted code #925

Closed cdsmith closed 2 years ago

cdsmith commented 2 years ago

Describe the bug Ormolu introduces a parse error when SCC annotations are used in do blocks

To Reproduce

module BugReport where

foo :: IO Int
foo = do
  {-# SCC "foo" #-} fmap succ $ do
    {-# SCC "bar" #-} pure 1

Run Ormolu:

$ ormolu -i --no-cabal BugReport.hs 
BugReport.hs:6:3
  Parsing of formatted code failed:  parse error (possibly incorrect indentation or mismatched brackets)

Try harder:

$ ormolu -i -u --no-cabal BugReport.hs 
module BugReport where

foo :: IO Int
foo = do
  {-# SCC "foo" #-}
  fmap succ $ do
    {-# SCC "bar" #-} pure 1

The latter code fails to compile because an SCC annotation is part of an expression, not a separate statement in the do block.

Expected behavior I'd expect the code after the SCC annotation to be indented as line-wrapped statements in a do block typically are.

module BugReport where

foo :: IO Int
foo = do
  {-# SCC "foo" #-}
    fmap succ $ do
      {-# SCC "bar" #-} pure 1

This compiles correctly. However, rerunning Ormolu restores the broken version.

Environment

$ uname -a
Linux csmith 5.4.0-1086-gcp #94~18.04.1-Ubuntu SMP Fri Aug 5 18:26:39 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ ormolu --version
ormolu 0.5.0.1 UNKNOWN UNKNOWN
using ghc-lib-parser 9.2.4.20220729