Opened 10 months ago

Closed 10 months ago

Last modified 5 months ago

#15884 closed bug (duplicate)

Completeness of View Patterns With a Complete Set of Output Patterns

Reported by: Shayan-Najd Owned by:
Priority: normal Milestone: 8.6.3
Component: Compiler Version: 8.6.2
Keywords: PatternMatchWarnings Cc: alanz, bgamari
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #15753 Differential Rev(s):
Wiki Page:


For example, the code

{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE ViewPatterns #-}
f :: Maybe a -> Bool
f (id->Nothing)  = False
f (id->(Just _)) = True

mistakenly returns the warning

warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for ‘f’: Patterns not matched: _
4 | f (id->Nothing)  = False
  | ^^^^^^^^^^^^^^^^^^^^^^^^...

Change History (3)

comment:1 Changed 10 months ago by simonpj

Keywords: PatternMatchWarnings added

comment:2 Changed 10 months ago by RyanGlScott

Resolution: duplicate
Status: newclosed

This is a duplicate of #15753 (see, so closing. In particular, note that this bug has nothing to do with ViewPatterns, as you can achieve the same erroneous warning with just ordinary PatternGuards:

{-# OPTIONS_GHC -Wall #-}
module Bug where

 f :: Maybe a -> Bool                                                                              
 f x                                                                                               
   | Nothing <- id x                                                                               
   = False                                                                                         
   | Just _ <- id x                                                                                
   = True
$ /opt/ghc/8.6.2/bin/ghci Bug.hs                                                                   
GHCi, version 8.6.2:  :? for help                                      
Loaded GHCi configuration from /home/ryanglscott/.ghci                                             
[1 of 1] Compiling Bug              ( Bug.hs, interpreted )                                        

Bug.hs:5:2: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for ‘f’: Patterns not matched: _
5 |  f x
  |  ^^^...

Removing the use of the id function, however, makes the warnings go away.

Last edited 10 months ago by RyanGlScott (previous) (diff)

comment:3 Changed 5 months ago by Marge Bot <ben+marge-bot@…>

In d236d9d0/ghc:

Make `singleConstructor` cope with pattern synonyms

Previously, `singleConstructor` didn't handle singleton `COMPLETE` sets
of a single pattern synonym, resulting in incomplete pattern warnings
in #15753.

This is fixed by making `singleConstructor` (now named
`singleMatchConstructor`) query `allCompleteMatches`, necessarily making
it effectful. As a result, most of this patch is concerned with
threading the side-effect through to `singleMatchConstructor`.

Unfortunately, this is not enough to completely fix the original
reproduction from #15753 and #15884, which are related to function
applications in pattern guards being translated too conservatively.
Note: See TracTickets for help on using tickets.