Why is Haskell parsing comments?

Viewed 274

I have a really simple code (in GHC 8.10.4/stack ghci 17.12):

module T where

data D = A | B | C deriving (Eq, Show)

fn :: D -> Int
fn x =
  case x of
    A ->
      -- | Test
      1
    B -> 2
    C -> 3

The thing is, whether I replace | by ^ or use the multi-lines comment, I get this error:

T.hs:9:7: error: parse error on input ‘-- | Test’
  |
9 |       -- | Test
  |       ^^^^^^^^^

Does the parser mismatch my comment with something else? or is there a special syntax that look likes comments?

1 Answers

A comment starts with --. Comments that start with -- | are a special kind of comment for the documentation tool haddock, and are only allowed in certain places (and the location you put it is not one of them). GHC actually accepts your code. It is likely that the way you're building your program involves an invocation of haddock, which rejects misplaced -- | comments.

Solution: use -- for plain comments, not -- |, which are for haddock.

    -- Test
Related