tweag / ormolu

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

AST differs on Haddock comment on infix constructor #758

Closed amesgen closed 4 months ago

amesgen commented 3 years ago

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.

brandonchinn178 commented 11 months ago

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 commented 4 months ago

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