Skip to content

Confusing parser error message

Summary

This code has bad indentation, and GHC reports the error in the wrong place.

test = do
  case undefined of
    -- (a)
    Just _ -> do -- GHC says the error is here
      putStrLn "Hi"
    -- (b)
    Nothing -> do
      -- (c)
      let sayHi = do
        () <- pure () -- the real error is here: the indentation is wrong
        putStrLn "Hi"
      sayHi

GHC says:

t.hs:4:15: error:
    do-notation in pattern
    Possibly caused by a missing 'do'?
  |
4 |     Just _ -> do -- GHC says the error is here
  |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

Steps to reproduce

Load the above code with GHC.

  • The initial do is necessary
  • Any number of lines that don't use do can be added at (a)
  • Any number of lines can be inserted at (b) or (c), separating the reported error from the real error by an arbitrarily far distance

In my original code, the error was reported nearly 20 lines before the actual error, so it was hard to find.

Expected behavior

GHC should ideally report the error on the line () <- pure ().

Environment

  • GHC version used: 8.10.3
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information