Skip to content

Incorrect unused top binding warning when interacting with GHC.Generics

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"
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