Opened 8 years ago

Closed 4 years ago

Last modified 20 months ago

#5728 closed bug (duplicate)

Warnings from -fwarn-incomplete-record-updates even with all constructors matched

Reported by: mjo Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.4.1
Keywords: warnings, PatternMatchWarnings Cc: george.karachalias@…, tom.schrijvers@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Incorrect warning at compile-time Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

I don't think that the following should be considered an incomplete record update, since I match on the other constructor before the record update:

data Foo = None | Bar { baz :: Int }

quux :: Foo -> Foo
quux None = None
quux x = x { baz = 2 }

main :: IO ()
main = return ()

But it still throws a warning:

$ ghc -fwarn-incomplete-record-updates test.hs
[1 of 1] Compiling Main             ( test.hs, test.o )

test.hs:6:10:
    Warning: Pattern match(es) are non-exhaustive
             In a record-update construct: Patterns not matched: None
Linking test ...

Change History (12)

comment:1 Changed 8 years ago by igloo

difficulty: Unknown
Keywords: warnings added
Milestone: _|_

Warnings are never going to be completely accurate, but this is certainly an example of a case that could be fixed.

I'll set the milestone to _|_ as it would make sense to do it as part of a larger warnings overhaul.

comment:2 Changed 8 years ago by maeder

Version: 7.2.17.4.1

Even for an explicit @-Pattern I get this warning.

{-# OPTIONS -fwarn-incomplete-record-updates #-}
data Rec = Mt | Rec {a, b, c, d, e, f, g :: Int}

updA :: Int -> Rec -> Rec
updA i r@(Rec {}) = r { a = i }
updA _ Mt = Mt

How should I change just "updA" (without mentioning the other fields) to avoid the warning?

comment:3 Changed 8 years ago by simonpj

Using record wild-cards you can write (nb untested)

updA :: Int -> Rec -> Rec
updA i (Rec { .. }) = Rec { a = i, .. }
updA _ Mt = Mt

which, I suppose, has the merit of making it obvious that you are returning a Rec constructor.

Simon

comment:4 Changed 8 years ago by maeder

This language extension has the disadvantage that:

  • the actual selector functions are shadowed and cannot be applied to other values in subexpressions
  • ".." binds variables that may conflict with other pattern variables, so that extending the record may break my update-code (in separate modules), like in:
{-# LANGUAGE RecordWildCards #-}
data Rec = Mt | Rec {a, b, c, d, e, f, g, h, i :: Int}

updA :: Int -> Rec -> Rec
updA i Rec { .. } = Rec { a = i, .. }
updA _ Mt = Mt
Rec.hs:5:6:
    Conflicting definitions for `i'
    Bound at: Rec.hs:5:6
              Rec.hs:5:8-17
    In an equation for `updA'

comment:5 Changed 5 years ago by thomie

This must have been fixed in the 7.8 release series. Only a regression test is missing.

comment:6 Changed 5 years ago by simonpj

Cc: george.karachalias@… tom.schrijvers@… added

Actually not. The warning was being suppressed by

    handleWarnings = if isGenerated origin
                     then discardWarningsDs
                     else id

in matchWrapper, which is really intended to suppress warnings in code generate by deriving etc. I've fixed this which makes the warning come back.

It's really is part of the pattern-match-fixup problem. Fortunately, Geroge K and Tom S are working on this.

Simon

comment:7 Changed 5 years ago by simonpj

Also you need -fwarn-incomplete-record-updates to see the warning; for some reason this flag is explicitly not in -Wall according to the user manual.

Simon

comment:8 Changed 5 years ago by Simon Peyton Jones <simonpj@…>

In a67ebbecfb10c91bb2793cb2f7d91f25aa23e493/ghc:

Resume reporting incomplete pattern matches for record updates

They were being inadvertently suppressed, even if you said -fwarn-incomplete-record-updates

See Trac #5728

comment:9 Changed 4 years ago by thomie

Resolution: duplicate
Status: newclosed

Because the existence of duplicate tickets makes doing a BugSweep of the bug tracker more cumbersome, I'm closing these tickets as duplicate. Don't worry, they're still listed on PatternMatchCheck, and will hopefully all be addressed by the work on #595 ("Overhaul GHC's overlapping/non-exhaustive pattern checking").

comment:10 Changed 3 years ago by mjo

For what it's worth, this is still present in 8.0.1.

comment:11 Changed 3 years ago by mpickering

I think this is out of scope of the pattern match checker.

comment:12 Changed 20 months ago by simonpj

Keywords: PatternMatchWarnings added
Note: See TracTickets for help on using tickets.