#13919 closed bug (fixed)

Incorrect unused top binding warning when interacting with GHC.Generics

Reported by: Taneb Owned by:
Priority: normal Milestone: 8.4.1
Component: Compiler Version: 8.0.1
Keywords: Generics Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Incorrect error/warning at compile-time Test Case:
Blocked By: Blocking:
Related Tickets: #1954 Differential Rev(s): Phab:D3704
Wiki Page:

Description

The program

{-# LANGUAGE DeriveGeneric #-}
module Main (main) where

import Data.Aeson
import GHC.Generics

data Foo = Foo {bar :: String} deriving Generic

instance ToJSON Foo where
  toJSON = genericToJSON defaultOptions

main :: IO ()
main = print (toJSON (Foo "hello"))

emits the warning

UTB.hs:7:17: warning: [-Wunused-top-binds]
    Defined but not used: ‘bar’

but removing that binding changes the behaviour of the program, as the above prints

Object (fromList [("bar",String "hello")])

but

{-# LANGUAGE DeriveGeneric #-}
module Main (main) where

import Data.Aeson
import GHC.Generics

data Foo = Foo String deriving Generic

instance ToJSON Foo where
  toJSON = genericToJSON defaultOptions

main :: IO ()
main = print (toJSON (Foo "hello"))

prints

String "hello"

Change History (3)

comment:1 Changed 14 months ago by RyanGlScott

Differential Rev(s): Phab:D3704
Keywords: Generics added
Status: newpatch

comment:2 Changed 13 months ago by Ben Gamari <ben@…>

In 15fcd9ad/ghc:

Suppress unused warnings for selectors for some derived classes

Although derived `Read`, `Show`, and `Generic` instances technically
don't //use// the record selectors of the data type for which an
instance is being derived, the derived code is affected by the
//presence// of record selectors. As a result, we should suppress
`-Wunused-binds` for those record selectors when deriving these classes.
This is accomplished by threading through more information from
`hasStockDeriving`.

Test Plan: make test TEST=T13919

Reviewers: austin, bgamari

Reviewed By: bgamari

Subscribers: rwbarton, thomie

GHC Trac Issues: #13919

Differential Revision: https://phabricator.haskell.org/D3704

comment:3 Changed 13 months ago by bgamari

Milestone: 8.4.1
Resolution: fixed
Status: patchclosed
Note: See TracTickets for help on using tickets.