[GHC] #13870: Empty record construction for record-less constructor gives surprising runtime error (and surprisingly few warnings)

GHC ghc-devs at haskell.org
Fri Jun 23 22:31:56 UTC 2017


#13870: Empty record construction for record-less constructor gives surprising
runtime error (and surprisingly few warnings)
-------------------------------------+-------------------------------------
           Reporter:  RyanGlScott    |             Owner:  (none)
               Type:  bug            |            Status:  new
           Priority:  normal         |         Milestone:
          Component:  Compiler       |           Version:  8.0.1
           Keywords:  newcomer       |  Operating System:  Unknown/Multiple
       Architecture:                 |   Type of failure:  Poor/confusing
  Unknown/Multiple                   |  error message
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 The crux of this ticket is this sort of code:

 {{{#!hs
 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
 [https://www.haskell.org/onlinereport/haskell2010/haskellch3.html#x8-520003.15.2
 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`):

 {{{#!hs
 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.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/13870>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list