Skip to content

Issues with the -Wempty-enumerations flag

Summary

The -Wempty-enumerations flag does not always generate an empty-enumeration warning.

Steps to reproduce

The users_guide states about the flag -Wempty-enumerations:

-Wempty-enumerations - warn about enumerations that are empty

For some cases this works:

GHCi, version 8.8.3: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/roland/.ghci
Prelude> import Data.Word
Prelude Data.Word> [2..4]::[Int]
[2,3,4]
Prelude Data.Word> [4..2]::[Int]

<interactive>:3:1: warning: [-Wempty-enumerations]
    Enumeration is empty
[]
Prelude Data.Word> [4..2]::[Integer]

<interactive>:4:1: warning: [-Wempty-enumerations]
    Enumeration is empty
[]
Prelude Data.Word> [4..2]::[Word8]

<interactive>:5:1: warning: [-Wempty-enumerations]
    Enumeration is empty
[]

However I don't get a warning for an empty-enumeration of characters or an empty-enumeration of a custom type:

Prelude Data.Word> ['a'..'d']
"abcd"
Prelude Data.Word> ['d'..'a']
""
Prelude Data.Word> :{
Prelude Data.Word| data MyEnum = A | B | C | D | E
Prelude Data.Word|     deriving (Eq, Ord, Enum, Show)
Prelude Data.Word| :}
Prelude Data.Word> [(A)..(D)]
[A,B,C,D]
Prelude Data.Word> [(D)..(A)]
[]

A short inspection of the code in the function warnAboutEmptyEnumerations in the module compiler/GHC/HsToCore/Match/Literal.hs shows, that the warning is only implemented for Integral numbers.

But even for integral numbers, there is a case where the warning is missing: When I don't specify the type of an integer enumeration, I don't get a warning:

GHCi, version 8.8.3: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/roland/.ghci
Prelude> :set -Wempty-enumerations
Prelude> [4..2]::[Integer]

<interactive>:2:1: warning: [-Wempty-enumerations]
    Enumeration is empty
[]
Prelude> [4..2]
[]
Prelude>

Expected behavior

The following examples should also give an empty-enumerations warning

['d'..'a']
[(D)..(A)]
[4..2]

Environment

  • GHC 8.8.3

Optional:

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