Skip to content

Empty record construction for record-less constructor gives surprising runtime error (and surprisingly few warnings)

The crux of this ticket is this sort of code:

module Main where

f :: Maybe Int
f = Just{}

main :: IO ()
main = print f
$ runghc --ghc-arg=-Wall Bug.hs
Just Bug.hs: Bug.hs:4:5-10: Missing field in record construction

Yikes. There are a couple of very surprising things happening here.

First, the message Missing field in record construction is very misleading. After all, Just has no records! We really should give a more specific error which highlights this fact.

(The fact that you can even use record construction syntax with a record-less constructor in the first place is a bit baffling, but the language standard does allow it, so I suppose we have to live with this.)

The second surprising bit is the fact that is program compiled with no warnings whatsoever. This is in contrast to, say, the Identity datatype, which has a record (runIdentity):

module Main where

import Data.Functor.Identity

f :: Identity Int
f = Identity{}

main :: IO ()
main = print f
$ runghc --ghc-arg=-Wall Bug.hs

Bug.hs:6:5: warning: [-Wmissing-fields]
    • Fields of ‘Identity’ not initialised: runIdentity
    • In the expression: Identity {}
      In an equation for ‘f’: f = Identity {}
Identity Bug.hs: Bug.hs:6:5-14: Missing field in record construction runIdentity

Here, GHC warned me that I was doing something stupid. GHC ought to be warning me with equivalent fervor when I use Just{}. The warning would obviously have to be tweaked a bit, since warning that Fields of ‘Just’ not initialised doesn't make any sense, but you get the idea.

Trac metadata
Trac field Value
Version 8.0.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information