fourmolu icon indicating copy to clipboard operation
fourmolu copied to clipboard

Formatting changes AST in haddocks with multiple lines

Open brandon-leapyear opened this issue 3 years ago • 1 comments

the following code causes an error "AST of input and AST of formatted code differ."

module Foo where

-- |
-- asdf
--
--
-- asdf
foo :: Int
foo = 1

Possibly solved if https://github.com/parsonsmatt/fourmolu/issues/65 is implemented

brandon-leapyear avatar Mar 04 '21 01:03 brandon-leapyear

I recently bumped into this bug as well and was going to open an issue.

I tested a few cases and noticed what works and what doesn't work.

In the "broken" cases, I get the following message:

Loaded config from: /Users/gustavoavena/formatter/fourmolu/../fourmolu.yaml
AST of input and AST of formatted code differ.
  at ../TestFourmoluCommentIssues.hs:(30,1)-(34,33)
  at ../TestFourmoluCommentIssues.hs:(39,1)-(43,2)
Please, consider reporting the bug.

FWIU, it does look like implementing #65 could solve this as well.

-- |
-- This works fine.
works1 :: Int -> Int
works1 n = n * 2

{- | This works
  with two lines.
-}
works2 :: Int -> Int
works2 n = n * 2

{- | This surprisingly works

  with one blank line.
-}
works3 :: Int -> Int
works3 n = n * 2

-- -------------------------- Broken below --------------------------
-- The cases below can't be handled by fourmolu.

-- |
-- This doesn't work...
--
--
-- ...because of the blank lines.
doesntWork1 :: Int -> Int
doesntWork1 n = n * 2


{- | If you have 2 or more blank lines


  Fourmolu fails.
-}
doesntWork2 :: Int -> Int
doesntWork2 n = n * 2

gustavoavena avatar May 13 '21 10:05 gustavoavena

module FourmoluBug (
  item1,
  -- * Section
  --
  -- $sectionDesc

  -- comment that is not part of the docs
  item2
)
where

item1 = ()
item2 = ()

-- $sectionDesc
-- Blah blah!

Another repro on

fourmolu 0.7.0.1 UNKNOWN UNKNOWN
using ghc-lib-parser 9.2.4.20220729

Output:

» fourmolu FourmoluBug.hs
FourmoluBug.hs
@@ -1,14 +1,16 @@
- module FourmoluBug (
-   item1,
-   -- * Section
-   --
-   -- $sectionDesc
+ module FourmoluBug
+   ( item1,

-   -- comment that is not part of the docs
-   item2
- )
+     -- * Section
+
+     --
+     -- $sectionDesc
+     -- comment that is not part of the docs
+     item2,
+   )
  where

  item1 = ()
+
  -- $sectionDesc
  -- Blah blah!

  AST of input and AST of formatted code differ.
  Please, consider reporting the bug.
  To format anyway, use --unsafe.

lf- avatar Sep 27 '22 18:09 lf-

The initial bug reported here is fixed with https://github.com/fourmolu/fourmolu/pull/173. Indeed, running the two examples above with fourmolu 0.7.0.1 works now. I'll be closing this issue.

@lf- thanks for the report, but it seems like a different bug. Please open a separate issue.

brandonchinn178 avatar Sep 27 '22 18:09 brandonchinn178