Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AST differs on Haddock comment on infix constructor #758

Closed
amesgen opened this issue Aug 11, 2021 · 2 comments · Fixed by #1118
Closed

AST differs on Haddock comment on infix constructor #758

amesgen opened this issue Aug 11, 2021 · 2 comments · Fixed by #1118
Labels
bug Something isn't working comments Issues related to comment placement

Comments

@amesgen
Copy link
Member

amesgen commented Aug 11, 2021

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 amesgen added bug Something isn't working comments Issues related to comment placement labels Aug 11, 2021
@brandonchinn178
Copy link
Collaborator

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working comments Issues related to comment placement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants