Skip to content

`-Wredundant-constraints` give different results with and without `-O`

Summary

On the same piece of code, -Wredundant-constraints raise a warning when building with -O but not with -O0

Steps to reproduce

Consider the (slightly contrieved) piece of code:

{-# LANGUAGE GADTs #-}
{-# OPTIONS_GHC -Wredundant-constraints #-}
{-# OPTIONS_GHC -Werror #-}

module Test where

-- |
-- @a@ in this context will always be a 'String'
data AlwaysAStringInside a where
  AString :: String -> AlwaysAStringInside String

-- |
-- The @Show a@ constraint is redundant since @a@ can only be 'String', but
-- GHC only nows it when building with @-O@
myShow :: Show a => AlwaysAStringInside a -> String
myShow (AString x) = show x

building it with -O0 will succeed, but with -O, it will fail with

▶ ghc Test.hs -O
[1 of 1] Compiling Test             ( Test.hs, Test.o )

Test.hs:15:1: error: [-Wredundant-constraints, -Werror=redundant-constraints]
    • Redundant constraint: Show a
    • In the type signature for:
           myShow :: forall a. Show a => AlwaysAStringInside a -> String
   |
15 | myShow :: Show a => AlwaysAStringInside a -> String
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expected behavior

The warnings raised shouldn't depend on the level of optimization used (so ideally warn in either cases or if not possible never warn)

Environment

  • GHC version used: 8.6.5

Optional:

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