Redundant constraint triggered by deriving via
Summary
redundant-constraints
warning is triggered in a deriving via
context when it should not.
Steps to reproduce
The following example:
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS -Wall -Wredundant-constraints #-}
import Data.Kind
newtype New t = New t
deriving (Show)
class Def a where
def :: a
class Foo a where
localDef :: Needs a => a
type Needs a :: Constraint
instance Foo (New a) where
localDef = New def
type Needs (New a) = Def a
data A = A
deriving (Show)
instance Def A where
def = A
deriving via (New A) instance Foo A
The deriving via
statement leads to the following warning:
Need.hs:33:1: warning: [-Wredundant-constraints]
• Redundant constraint: Needs A
• In an expression type signature:
Needs A => A
In the expression:
GHC.Prim.coerce @(New A) @A (localDef @(New A)) :: Needs A => A
In an equation for ‘localDef’:
localDef
= GHC.Prim.coerce @(New A) @A (localDef @(New A)) :: Needs A => A
When typechecking the code for ‘localDef’
in a derived instance for ‘Foo A’:
To see the code I am typechecking, use -ddump-deriv
|
33 | deriving via (New A) instance Foo A
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The message shows the generated code, it is GHC.Prim.coerce @(New A) @A (localDef @(New A)) :: Needs A => A
. I tried to manually write the same code, and the warning is still there. However, it compiles without warning if I remove the Needs
constraint, i.e. coerce @(New A) @A (localDef @(New A)) :: A
Expected behavior
I'd like to not have any warning when using deriving via
. Writing manually the instance like:
instance Foo A where
localDef = A
type Needs A = Def A
Does indeed not generate the warning.
Environment
- GHC version used: 9.0.1 and 8.8.4