Search code examples
haskellghcpreprocessor

Why does MIN_VERSION_GLASGOW_HASKELL not work in GHC 9.2?


Per GHC guide we expect the MIN_VERSION_GLASGOW_HASKELL(x,y,z,z') CPP to work on the corresponding GHC version. However, the following fails to compile on GHC 9.2

#if MIN_VERSION_GLASGOW_HASKELL(9,2,1,0)

enter image description here

What's happening here?


To resolve this, I find myself using:

#if MIN_VERSION_base(4,16,0)

But why would MIN_VERSION_GLASGOW_HASKELL not work?


Solution

  • GHC versions consist of a major version, minor version, first patchlevel, and optional second patchlevel. So GHC 9.2.1 has major 9, minor 2, first patchlevel 1, and no second patch level.

    There's a bug in GHC, possibly in existence since the macro MIN_VERSION_GLASGOW_HASKELL was introduced, where that macro depends on a macro variable __GLASGOW_HASKELL_PATCHLEVEL2__ which is not defined when, as is usual for release versions, the GHC version has only one patch level (like 9.2.1).

    This usually doesn't matter, except if the -Wcpp-undef flag is in force AND the test is performed on a version that matches on the major, minor, and first patch level. Then, the check on the second patch level will generate a compiler warning message. (Despite the fact that it looks like an "error" rather than a warning, the compilation appears to complete.)

    You can work around this by:

    • ignoring it -- despite appearances, it looks like it's only a warning, not an error, and compilation should complete

    • shutting off -Wcpp-undef by either removing the flag or, if feasible, adding a -Wno-cpp-undef flag;

    • adding some code to define the missing patchlevel before calling the macro:

       #ifndef __GLASGOW_HASKELL_PATCHLEVEL2__
       #define __GLASGOW_HASKELL_PATCHLEVEL2__ 0
       #endif