[GHC] #14783: Initializing record with similarly named field from a different record results in warning rather than error

GHC ghc-devs at haskell.org
Fri Feb 9 10:07:41 UTC 2018


#14783: Initializing record with similarly named field from a different record
results in warning rather than error
-------------------------------------+-------------------------------------
           Reporter:                 |             Owner:  (none)
  ulrikrasmussen                     |
               Type:  bug            |            Status:  new
           Priority:  normal         |         Milestone:
          Component:  Compiler       |           Version:  8.0.1
           Keywords:  records        |  Operating System:  Unknown/Multiple
  DuplicateRecordFields              |
       Architecture:                 |   Type of failure:  None/Unknown
  Unknown/Multiple                   |
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 When a record is initialized using a similarly named field from another
 record, the compiler ignores the initialization and generates a warning
 instead of throwing an error. I believe this is a bug introduced in GHC
 8.0.1 (and probably related to `DuplicateRecordFields`), since earlier
 versions of GHC would refuse to compile the code.

 Consider the following example:

 {{{
 -- A.hs
 module A where
   data A = A { a :: (), b :: () }

 -- B.hs
 module B where
   data B = B { a :: (), b :: () }

 -- Main.hs
 module Main where
   import A hiding (a)
   import B

   x = A { a = (), b = () }

   main = case x of
     A () () -> return ()
 }}}

 On GHC 8.0.1, this compiles (with a warning), and the program throws an
 exception when run:

 {{{
 $ stack ghc --compiler ghc-8.0.1 -- --make Main.hs
 [1 of 3] Compiling B                ( B.hs, B.o )
 [2 of 3] Compiling A                ( A.hs, A.o )
 [3 of 3] Compiling Main             ( Main.hs, Main.o )

 Main.hs:5:7: warning: [-Wmissing-fields]
     • Fields of ‘A’ not initialised: a
     • In the expression: A {a = (), b = ()}
       In an equation for ‘x’: x = A {a = (), b = ()}
 Linking Main ...

 $ ./Main
 Main: Main.hs:5:7-26: Missing field in record construction a
 }}}

 On GHC 7.10.3, the program will not compile:
 {{{
 $ stack ghc --compiler ghc-7.10.3 -- --make Main.hs
 [1 of 3] Compiling B                ( B.hs, B.o )
 [2 of 3] Compiling A                ( A.hs, A.o )
 [3 of 3] Compiling Main             ( Main.hs, Main.o )

 Main.hs:5:7:
     Constructor ‘A’ does not have field ‘a’
     In the expression: A {a = (), b = ()}
     In an equation for ‘x’: x = A {a = (), b = ()}
 }}}

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


More information about the ghc-tickets mailing list