Skip to content

MIN_VERSION_GLASGOW_HASKELL with `-Wcpp-undef` generates spurious warning

Summary

The MIN_VERSION_GLASGOW_HASKELL macro defined in the generated header file ghcversion.h references macro variables __GLASGOW_HASKELL_PATCHLEVEL1__ and __GLASGOW_HASKELL_PATCHLEVEL2__ that are expected to be defined in the same header file. However, definitions for these variables are only written if the corresponding patch levels parsed from the version string in the autoconf code are not blank. In practice, for released versions, __GLASGOW_HASKELL_PATCHLEVEL1__ is defined while __GLASGOW_HASKELL_PATCHLEVEL2__ is not.

This normally doesn't cause a problem. However, if the compiler flag -Wcpp-undef is turned on and the GHC version matches the MIN_VERSION_GLASGOW_HASKELL up to the first patch level, triggering a comparison on the second patch level, a CPP warning is generated. The displayed message suggests it's an error rather than a warning, but compilation appears to complete anyway.

Steps to reproduce

Compile the following sample program with GHC 8.10.7 or GHC 9.2.1 (or any other GHC version after adding a check specifically against that version):

{-# OPTIONS_GHC -Wcpp-undef #-}
{-# LANGUAGE CPP #-}

-- crash 8.10.7
#if MIN_VERSION_GLASGOW_HASKELL(8,10,7,0)
#endif

-- crash 9.2.1
#if MIN_VERSION_GLASGOW_HASKELL(9,2,1,0)
#endif

main = putStrLn "Hello, world!"

Expected behavior

The resulting program should compile without errors or warnings. Instead, it produces the following apparent error, though compilation does complete:

$ ghc-8.10.7 MacroTest.hs

MacroTest.hs:5:0: error:
     warning: "__GLASGOW_HASKELL_PATCHLEVEL2__" is not defined, evaluates to 0 [-Wundef]
        5 | #if MIN_VERSION_GLASGOW_HASKELL(8,10,7,0)
          | 
  |
5 | #if MIN_VERSION_GLASGOW_HASKELL(8,10,7,0)
  | ^
[1 of 1] Compiling Main             ( MacroTest.hs, MacroTest.o )
Linking MacroTest ...

Environment

  • GHC version used: 8.10.7

Optional:

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