Skip to content

Initializing record with similarly named field from a different record results in warning rather than error

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 = ()}
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