Skip to content

Error messages about "do" statements contain false information

When GHC complains about a type-error in a "do" block, it says, for example:

main = putChar $ do
  getLine
  return 'x'

Results in an error:

    Couldn't match type `IO Char' with `Char'
    Expected type: IO String -> IO Char -> Char
      Actual type: IO String -> IO Char -> IO Char
    In a stmt of a 'do' block: getLine

I think this error message is pretty directly saying: The Actual type of "getLine" is:

IO String -> IO Char -> IO Char

But that is of course non-sense! It is actually talking about the type of the (>>=) binding the "getLine" to the rest of the statements. But the (>>=) is not "in the statement" at all, so the error message is plainly wrong.

It would be much better to talk about the type of the entire do block, or maybe the type of the last stmt in the do block (which is the same).

For example, the error could be replaced by:

    Couldn't match type `IO Char' with `Char'
    Expected type: Char
      Actual type: IO Char
    In a stmt of a 'do' block: return 'x'

Or by:

    Couldn't match type `IO Char' with `Char'
    Expected type: Char
      Actual type: IO Char
    In 'do' block: `do { getLine;
                         return 'x' }'
Trac metadata
Trac field Value
Version 7.6.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information