Skip to content

Pattern match failure from GHC when compiling module with -O2 that uses mapM in TH

Summary

The following module causes GHC HEAD and 9.2 to crash with a pattern match failure. It looks like this is triggered from some code turning core into stg in the driver. Interestingly, replacing mapM with traverse causes it to disappear. It doesn't happen if we don't compile the module with -O2.

Steps to reproduce

Try to compile the following module:

{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -O2 #-}
module THBug where

data A
data B

concat <$> mapM (\_ -> (pure []))
  [ ''A
  , ''B
  ]

ghc produces the following output:

[1 of 1] Compiling THBug            ( THBug.hs, THBug.o, THBug.dyn_o )
user error (Pattern match failure in 'do' block at compiler/GHC/Driver/Main.hs:1823:5-68)

Expected behaviour

This module should probably compile without an error

Environment

  • GHC version used:
The Glorious Glasgow Haskell Compilation System, version 9.3.20210628

Optional:

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