Skip to content

ConstrainedClassMethods can trigger un-actionable redundant constraint warnings together with GND

Summary

If you use GeneralizedNewtypeDeriving (or indeed, DerivingVia) to derive an instance of a type class whose methods have constraints that aren't in the type class head, it is possible to trigger redundant constraint warnings if the generated code would cause the constraints to be satisfied by whatever the type variables are set to. See the example given in 'steps to reproduce' for a demonstration. These warnings can't be actioned - the only option currently is to disable redundant constraint warnings completely.

Steps to reproduce

Attempt to compile the following code with -Wredundant-constraints enabled:

{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}

module Baz where

class Foo a where
  bar :: (Num a) => a -> a
  bar = (+ 1)

instance Foo Int

newtype Quux a = Quux a

deriving newtype instance Foo (Quux Int)

This will emit a redundant constraint warning, as Int is already an instance of Num.

Expected behavior

Should compile, emitting no warnings.

Environment

  • GHC version used: 8.10.1

Optional:

  • Operating System: GNU/Linux
  • System Architecture: x86_64
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information