ormolu icon indicating copy to clipboard operation
ormolu copied to clipboard

AST differs on Haddock comment on infix constructor

Open amesgen opened this issue 3 years ago • 1 comments

Describe the bug This snippet can't be formatted without --unsafe (minimized from Agda):

data A = A :# A -- ^ foo

With --unsafe, it is formatted to

data A
  = -- | foo
    A :# A

In the first case, the Haddock comment is attached to the constructor :#, but in the second case, it is attached to the first argument of :#.

Expected behavior It could be formatted like this to preserve the AST:

data A
  = A
      -- | foo
      :# A

But this is not really nice from a sylistic perspective (we should not have to break apart an existing single line constructor variant).

Environment Ormolu 0.2.0.0

Additional context This changed in GHC 9.0; previously, both snippets had the same AST.

amesgen avatar Aug 11 '21 09:08 amesgen

I opened a GHC ticket for this: https://gitlab.haskell.org/ghc/ghc/-/issues/24221

But aside from that issue, it seems like Ormolu doesn't format a fully documented infix constructor correctly anyway. Here's an example:

data Foo
  = Int -- ^ Docs for left arg
    :*: -- ^ Docs for constructor
    Int -- ^ Docs for right arg

This fails to output valid Haskell:

<input>
@@ -1,4 +1,8 @@
  data Foo
-   = Int -- ^ Docs for left arg
-     :*: -- ^ Docs for constructor
-     Int -- ^ Docs for right arg
+   = -- | Docs for constructor
+
+     -- | Docs for left arg
+     Int
+       :*: -- | Docs for right arg
+       Int
+

  AST of input and AST of formatted code differ.
    at <input>:2:5-7
    at <input>:2:9-30
  Please, consider reporting the bug.
  To format anyway, use --unsafe.

brandonchinn178 avatar Nov 26 '23 01:11 brandonchinn178

FYI GHC 9.10 fixed the issue originally documented. But I'll fix the issue I brought up in the thread

brandonchinn178 avatar Jun 02 '24 01:06 brandonchinn178