Opened 9 months ago

Closed 5 months ago

#14783 closed bug (duplicate)

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

Reported by: ulrikrasmussen Owned by:
Priority: normal Milestone: 8.2.3
Component: Compiler Version: 8.0.1
Keywords: records DuplicateRecordFields ORF Cc: adamgundry
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC accepts invalid program Test Case:
Blocked By: Blocking:
Related Tickets: #13847 Differential Rev(s):
Wiki Page:

Description

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

Attachments (3)

A.hs (48 bytes) - added by ulrikrasmussen 9 months ago.
B.hs (48 bytes) - added by ulrikrasmussen 9 months ago.
Main.hs (124 bytes) - added by ulrikrasmussen 9 months ago.

Download all attachments as: .zip

Change History (6)

Changed 9 months ago by ulrikrasmussen

Attachment: A.hs added

Changed 9 months ago by ulrikrasmussen

Attachment: B.hs added

Changed 9 months ago by ulrikrasmussen

Attachment: Main.hs added

comment:1 Changed 5 months ago by RyanGlScott

Keywords: ORF added

comment:2 Changed 5 months ago by adamgundry

Cc: adamgundry added

comment:3 Changed 5 months ago by adamgundry

Milestone: 8.2.3
Resolution: duplicate
Status: newclosed
Type of failure: None/UnknownGHC accepts invalid program

Thanks for the report! This bug exists in 8.0.x and 8.2.x but is fixed in the 8.4.x series.

Note: See TracTickets for help on using tickets.