tweag / ormolu

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

Ormolu doesn't work in certain cases when comments contain $ #586

Closed tomjaguarpaw closed 4 years ago

tomjaguarpaw commented 4 years ago

Ormolu doesn't work in certain cases when comments contain $. To reproduce, run ormolu on a file containing

foo = ()
 -- $

(the space before -- is essential to demonstrate the bug). Ormolu says

The GHC parser (in Haddock mode) failed:
  ormolu.hs:2:2-5
  parse error on input `-- $'

I expected it to be able to parse this file successfully. It is a valid Haskell file. This is version

ormolu 0.0.5.0 UNKNOWN UNKNOWN
using ghc-lib-parser 8.10.1.20200412
aspiwack commented 4 years ago

The -- $ comment is parsed as a Haddock reference, I assume, which Haddock deems misplaced. There is really nothing Ormolu can do, since it needs to care about the placement of Haddock comments.

But you have workarounds: such as adding an extra space between -- and $, it should suffice.

tomjaguarpaw commented 4 years ago

Ah yes, that's true. That workaround is OK for now.

But why is Ormolu parsing in Haddock mode? Does it also format Haddock comments?

mrkkrp commented 4 years ago

Does it also format Haddock comments?

It may change how they are placed. For example -- ^ may be reformatted as -- | this is why we need to parse in Haddock mode.

tomjaguarpaw commented 4 years ago

Interesting, thanks. And thanks for writing Ormolu!

aspiwack commented 4 years ago

More importantly, Ormolu needs to understand Haddock comments, otherwise, they could easily become detached from the definition they refer to, hence change the generated html documentation (probably to something nonsensical). A problem which, I believe, early versions of Ormolu had.

The best way to understand Haddock comments is to share Haddock's parser. When you get there, it also gives you the freedom to manipulate them a smidge, which @mrkkrp is speaking of above.

tomjaguarpaw commented 4 years ago

I suspect I ought to consider my code incorrect if it doesn't satisfy Haddock. I don't actually have any Haddock docs in it, but it's probably a good habit to get into. On the other hand, I didn't realise that Haddock was actually part of GHC!