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

Weird Haddock comments can't be formatted without --unsafe #726

Closed
amesgen opened this issue Jul 26, 2021 · 2 comments · Fixed by #818
Closed

Weird Haddock comments can't be formatted without --unsafe #726

amesgen opened this issue Jul 26, 2021 · 2 comments · Fixed by #818
Labels
bug Something isn't working

Comments

@amesgen
Copy link
Member

amesgen commented Jul 26, 2021

Describe the bug
Since #722, certain weird Haddock comments can not be formatted without --unsafe.

To Reproduce
Try to format BufferMode.hs from leksah, or this minimized snippet:

-- |
--
---
module Test where

Expected behavior
Not yet decided, but it should not error.

@amesgen amesgen added the bug Something isn't working label Jul 26, 2021
@amesgen amesgen mentioned this issue Jul 26, 2021
@amesgen
Copy link
Member Author

amesgen commented Aug 25, 2021

EDIT This analysis is incorrect, see comment below.


This appears to be a regression in ghc-lib-parser-9.0 and should be fixed in the upgrade to ghc-lib-parser-9.2 (which should be released in August according to the GHC status update 🎉).


The third line (the --- ) is actually not necessary for this issue. Right now, with --unsafe, the snippet

-- |
--
module Test where

is formatted into

-- |
module Test where

and again into

module Test where

(this is similiar to empty Haddock comments on function arguments not appearing in the AST since ghc-lib-parser-9.0).

Previously, in 0.1.4.1 with ghc-lib-parser-8.10, the first snippet would be formatted into the second one idempotently, and the AST did not differ as we drop trailing blank lines when printing/comparing Haddock comments. But now, with ghc-lib-parser-9.0, the parsed AST of the second snippet does no longer contain a Haddock comment, in contrast to the first snippet, so the AST differs.

@amesgen
Copy link
Member Author

amesgen commented Oct 21, 2021

This issue is actually caused by invalid Haddock comments (cf. #803). Namely, this is a valid Haddock comment:

-- |
--
module Test where

which is formatted to

-- |
module Test where

as blank lines are collapsed. But in the second snippet, the Haddock comment is now invalid for some reason.

I think the best approach to fix this is to just drop (valid and invalid) Haddock comments if they only contain spaces.


For complete madness, consider how the GHC parser handles empty Haddock comments at different places:

-- |
module Test
  ( -- |
    test,
  )
where

-- |
test ::
  -- |
  test

Here, confusingly, the second and third Haddock comments are valid, but the first and second one are invalid. Hence, Ormolu formats this as

--
module Test
  ( -- |
    test,
  )
where

-- |
test ::
  --
  test

which seems very random.
With the proposed change, it would be formatted as

module Test
  ( test,
  )
where

test ::
  test

which is a clear improvement IMO.

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

Successfully merging a pull request may close this issue.

1 participant