Ticket #5813 (new feature request)
Offer a compiler warning for failable pattern matches
| Reported by: | snoyberg | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.6.2 |
| Component: | Compiler | Version: | 7.2.2 |
| Keywords: | Cc: | Christian.Maeder@… | |
| Operating System: | Unknown/Multiple | Architecture: | Unknown/Multiple |
| Type of failure: | None/Unknown | Difficulty: | Unknown |
| Test Case: | Blocked By: | ||
| Blocking: | Related Tickets: |
Description
This started off with a mailing list discussion: http://www.mail-archive.com/haskell-cafe@haskell.org/msg96517.html. The problem is that the following code produces no compile-time warning and results in a runtime error:
data MyType = Foo | Bar
deriving Show
test :: Monad m => m MyType -> m ()
test myType = do
Foo <- myType
return ()
main :: IO ()
main = test $ return Bar
This is in contrast to the rest of pattern matching, which would warn about unmatched patterns, e.g.:
data MyType = Foo | Bar
deriving Show
test :: Monad m => m MyType -> m ()
test myType = do
x <- myType
case x of
Foo -> return ()
main :: IO ()
main = test $ return Bar
I understand that this style of code may be very useful in some circumstances when paired with a Monad providing a sensible fail implementation, and is especially used in list comprehensions. However, this is allowing an easily catchable static error to slip through our fingers.
I recommend we add a new compiler warning to catch incomplete patterns in do-notation binding. I believe this warning should not apply to list comprehensions. Ideally, this warning would be turned on by -Wall.
