[ANNOUNCE] GHC 8.6.1-alpha2 available

Ryan Scott ryan.gl.scott at gmail.com
Sat Jul 21 13:03:13 UTC 2018


Presumably, you're compiling this program outside of a git repository,
and so the Template Haskell splice $(gitHash) evaluates to "UNKNOWN"
at compile time. Here is an even more minimal example, which does not
depend on gitrev:

    f :: ()
    f = case "UNKNOWN" of
          "UNKNOWN" -> ()
          _         -> ()

This, when compiled with GHC 8.6.1 or later, gives the same warning:

    Test.hs:4:7: warning: [-Woverlapping-patterns]
        Pattern match is redundant
        In a case alternative: _ -> ...
      |
    4 |       _         -> ()
      |       ^^^^^^^^^^^^^^^

This, I would argue, is a correct error message. The string literal
"UNKNOWN" is, well, statically known to be "UNKNOWN", so GHC knows
that that catch-all case cannot be reached. (See [1] for the commit
which introduced this check.)

Granted, this makes things slightly inconvenient for your purposes,
since in your actual program, $(gitHash) might evaluate to *different*
things at compile time depending on the directory you compile it in.
I'd recommend rewriting the commitInfo function to pattern guards:

    commitInfo :: Maybe String
    commitInfo
      | hash == "UNKNOWN" = Nothing
      | otherwise         = Just hash
      where
        hash = $(gitHash)

Since that does not trigger -Woverlapping-patterns. (Granted, it's
possible that GHC could become much smarter in the future and be able
to detect that that `otherwise` case is unreachable when $(gitHash)
evaluates to "UNKNOWN" at compile time. But GHC certainly isn't that
smart today!)

Ryan S.
-----
[1] http://git.haskell.org/ghc.git/commit/1f88f541aad1e36d01f22f9e71dfbc247e6558e2


More information about the ghc-devs mailing list