Skip to content

Parse error oddity

I recently came across an error message from GHC in a large chunk of code that was very confusing. I was able to distill it down to the following: (Note that the below program isn't supposed to be meaningful in any way.)

foo n = do case n of
             0 -> error "urk" [foo 0]
             _ -> y <- foo n
                  pure 4

The issue is that I forgot to put a do before y <- foo n, and it'd have been nice for GHC to point that out for me. Instead, it says:

a.hs:2:32: error:
    Parse error in pattern: foo
    Possibly caused by a missing 'do'?
  |
2 |              0 -> error "urk" [foo 0]
  |                                ^^^^^

This was a large chunk of code and the error message is really pointing to a location far from the actual location of the issue. And why would foo 0 be a pattern? That was the most confusing part.

Perhaps not a big deal, but I thought I'd file a ticket to see if it can be improved. I noticed that the error message wildly changes by modifications to this source: For instance, if you delete the do in the very first line, you get a much better error message instead:

a.hs:3:21: error:
    parse error on input ‘<-’
    Perhaps this statement should be within a 'do' block?
  |
3 |              _ -> y <- foo n
  |                     ^^
Edited by Levent Erkök
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information