Ticket #7169 (new feature request)

Opened 9 months ago

Last modified 4 weeks ago

Warning for incomplete record field label used as function

Reported by: goldfire Owned by:
Priority: normal Milestone: 7.8.1
Component: Compiler Version: 7.7
Keywords: Warnings Cc: the.dead.shall.rise@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

Consider the following definition:

data Foo = Bar { frob :: Int }
         | Baz { frob :: Int, quux :: Bool }

It would be great if GHC could produce a warning if I use quux as a function somewhere in my code, as calling quux on a Foo constructed with Bar will cause a runtime error. On the other hand, every use of frob is OK.

Currently, to find such problems, I have to first figure out which field labels are incomplete and then search for those, ignoring their uses as a proper field label (in construction or pattern matching).

Change History

Changed 9 months ago by simonpj

  • difficulty set to Unknown

But there are lots of partial functions (eg head). Why warn for quux but not for head.

Changed 9 months ago by goldfire

Because I think it's easier to make quux partial by mistake during refactoring. The particular example that motivated this post was refactoring TyClDecl in GHC to make family declarations a separate type. Much later, I noticed that the refactor made tcdLName into a partial function and I had to perform the search mentioned above.

Changed 9 months ago by igloo

We do warn when you define

head (x : _) = x

though. It would be nice if we also warned about

data Foo = Bar { frob :: Int }
         | Baz { frob :: Int, quux :: Bool }

but unfortunately

data Foo = Bar { frob :: Int }
         | Baz { frob :: Int, _ :: Bool }

is currently a parse error. Perhaps it shouldn't be?

Changed 9 months ago by goldfire

I don't think that last question directly relates to this issue... We want to be able to have fields such as quux that are used only with some (but not all) constructors of a type, but I think a warning when such field names are used as functions would be helpful.

Perhaps I'm misunderstanding your post?

Changed 7 months ago by igloo

  • milestone set to 7.8.1

Changed 4 weeks ago by refold

  • cc the.dead.shall.rise@… added
Note: See TracTickets for help on using tickets.