Skip to content

GHC 9.0 or later gives duplicate -Wincomplete-uni-patterns warnings

Consider this program:

{-# OPTIONS_GHC -Wincomplete-uni-patterns #-}
module Bug where

a, b, c :: Bool
a = False
b = False
c = False

mx :: Maybe ()
mx = Just ()

f :: ()
f | a = ()
  | otherwise
  = let Just x = mx
     in x

If you compile this, you'll get an -Wincomplete-uni-patterns warning, as expected:

$ ghc-9.2 Bug.hs -fforce-recomp
[1 of 1] Compiling Bug              ( Bug.hs, Bug.o )

Bug.hs:15:9: warning: [-Wincomplete-uni-patterns]
    Pattern match(es) are non-exhaustive
    In a pattern binding:
        Patterns of type ‘Maybe ()’ not matched: Nothing
   |
15 |   = let Just x = mx
   |         ^^^^^^^^^^^

Now change the definition of f so that its first guard is predicated on both a and b:

f :: ()
f | a, b = ()
  | otherwise
  = let Just x = mx
     in x

This time, the warning will look strange:

$ ghc-9.2 Bug.hs -fforce-recomp
[1 of 1] Compiling Bug              ( Bug.hs, Bug.o )

Bug.hs:15:9: warning: [-Wincomplete-uni-patterns]
    Pattern match(es) are non-exhaustive
    In a pattern binding:
        Patterns of type ‘Maybe ()’ not matched:
            Nothing
            Nothing
   |
15 |   = let Just x = mx
   |         ^^^^^^^^^^^

Notice that there are duplicate warnings about the Nothing pattern! In fact, the more conjuncts you add, the more duplicates there will be. For instance, making the first guard be | a, b, c will result in a total of three warnings about Nothing.

Ideally, we'd only have just one warning about Nothing. In fact, this was the behavior prior to GHC 9.0:

$ ghc-8.10 Bug.hs -fforce-recomp
[1 of 1] Compiling Bug              ( Bug.hs, Bug.o )

Bug.hs:15:9: warning: [-Wincomplete-uni-patterns]
    Pattern match(es) are non-exhaustive
    In a pattern binding: Patterns not matched: Nothing
   |
15 |   = let Just x = mx
   |         ^^^^^^^^^^^

This regression was introduced at some point between GHC 8.10 and 9.0, as 9.0 is the first version to exhibit the bug.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information