Cannot derive instance for non-stock-deriveable class [GHC-00158]

GHC cannot create derived instances for classes that are not deriveable through its stock deriving mechanism.

This restriction can be relaxed by enabling the DeriveAnyClass language extension, in which case GHC will generate an empty instance declaration, with no explicitly-defined methods.

Additionally, this error can appear when incorrectly specifying the stock strategy for a deriving clause if the DerivingStrategies language extension is enabled.

Examples

Cannot derive instance for non-stock-deriveable class

GHC cannot derive an instance for MyClass, as it is not stock deriveable. Enabling the DeriveAnyClass language extension allows the example to compile.

Error Message

NotStockDeriveable.hs:6:12: error: [GHC-00158]
Can't make a derived instance ofMyClass MyType:
MyClass’ is not a stock derivable class (Eq, Show, etc.)
In the data declaration for ‘MyType
    Suggested fix: Perhaps you intended to use DeriveAnyClass
  |
6 |   deriving MyClass
  |            ^^^^^^^
NotStockDeriveable.hs
Before
module NotStockDeriveable where

class MyClass a

data MyType = MyType
  deriving MyClass
After
{-# LANGUAGE DeriveAnyClass #-}

module NotStockDeriveable where

class MyClass a

data MyType = MyType
  deriving MyClass
Specifying an incorrect deriving strategy

In this example, the stock strategy is incorrectly specified when deriving the Num class (which is not stock deriveable). The solution is to specify the newtype deriving strategy.

Error Message

IncorrectDerivingStrategy.hs:6:18: error: [GHC-00158]
Can't make a derived instance of
Num IntWrapper’ with the stock strategy:
Num’ is not a stock derivable class (Eq, Show, etc.)
In the newtype declaration for ‘IntWrapper
  |
6 |   deriving stock Num
  |                  ^^^
IncorrectDerivingStrategy.hs
Before
{-# LANGUAGE DerivingStrategies #-}

module IncorrectDerivingStrategy where

newtype IntWrapper = Wrap { unwrap :: Int }
  deriving stock Num
After
{-# LANGUAGE DerivingStrategies #-}

module IncorrectDerivingStrategy where

newtype IntWrapper = Wrap { unwrap :: Int }
  deriving newtype Num