GHC internal error when pattern synonym has wrong type signature
When I forgot an argument in the type signature of a pattern synonym, I got, in addition to the expected error, also a GHC internal error.
MWE:
{-# LANGUAGE PatternSynonyms #-}
data ConData = ConData { _pars :: Int }
data Decl = ConDecl ConData
pattern Con :: Decl -- The correct type would be Int -> Decl
pattern Con { pars } = ConDecl (ConData pars)
foo :: Decl -> Int
foo (Con { pars }) = pars
Reported errors are:
PatternSyn.hs:7:1: error:
• Pattern synonym ‘Con’ has one argument
but its type signature has 1 fewer arrows
• In the declaration for pattern synonym ‘Con’
|
7 | pattern Con { pars } = ConDecl (ConData pars)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PatternSyn.hs:10:6: error:
• GHC internal error: ‘pars’ is not in scope during type checking, but it passed the renamer
tcl_env of environment: [rh1 :-> Identifier[foo::Decl
-> Int, TopLevelLet {} True]]
• In the pattern: Con {pars}
In an equation for ‘foo’: foo (Con {pars}) = pars
|
10 | foo (Con { pars }) = pars
| ^^^^^^^^^^^^
Seems GHC does not recover well from the original error and then throws an internal error.
The internal error is reported by GHC 8.6 and above (tested 8.6.5, 8.8.4, 8.10.7, 9.0.2, 9.2.8, 9.4.5, 9.6.2). Evidence in CI run, e.g.: https://github.com/agda/agda/actions/runs/5155818185/jobs/9285983967#step:12:337