tcLookupId panic with RequiredTypeArguments and PatternSynonyms
The following example leads to a compiler panic:
```haskell
{-# LANGUAGE GADTs, RequiredTypeArguments, PatternSynonyms #-}
data T a where
MkT :: forall a -> T a
pattern P :: Int -> T Int
pattern P x = MkT x
```
GHC's error message is as follows:
```
<no location info>: error:
panic! (the 'impossible' happened)
GHC version 9.14.1:
tcLookupId
x_awF
Call stack:
CallStack (from HasCallStack):
pprPanic, called at compiler/GHC/Tc/Utils/Env.hs:592:20 in ghc-9.14.1-bcbf:GHC.Tc.Utils.Env
```
Analysis and additional reproducers by Claude Opus 5:
<details>
<summary>Click to expand</summary>
The renamer binds `x` as a term variable, since it cannot know that the first argument of
`MkT` is a required type argument. The T2T conversion in `zip_pats_bndrs` then rebinds it as
a type variable, so `tc_arg` in `compiler/GHC/Tc/TyCl/PatSyn.hs`, which does `tcLookupId` on
each declared argument, finds an `ATyVar` and panics.
Omitting the type signature panics too, at the corresponding `tcLookupId` in
`tcInferPatSynDecl`:
```haskell
{-# LANGUAGE GADTs, RequiredTypeArguments, PatternSynonyms #-}
data T a where
MkT :: forall a -> T a
pattern P x = MkT x
```
```
<no location info>: error:
panic! (the 'impossible' happened)
GHC version 9.14.1:
tcLookupId
x_aho
```
A unidirectional synonym panics as well, so the builder is not involved and this is
unrelated to #27440 and #27583:
```haskell
{-# LANGUAGE GADTs, RequiredTypeArguments, PatternSynonyms #-}
data T a where
MkT :: forall a -> T a
pattern P :: Int -> T Int
pattern P x <- MkT x
```
```
<no location info>: error:
panic! (the 'impossible' happened)
GHC version 9.14.1:
tcLookupId
x_awF
```
Spelling the type argument explicitly produces the right error, because then the renamer
knows `x` is not a term variable:
```haskell
{-# LANGUAGE GADTs, RequiredTypeArguments, PatternSynonyms #-}
data T a where
MkT :: forall a -> T a
pattern P :: Int -> T Int
pattern P x = MkT (type x)
```
```
Bug.hs:6:11: error: [GHC-76037] Not in scope: ‘x’
|
6 | pattern P x = MkT (type x)
| ^
```
The same situation in an ordinary function is handled properly too:
```haskell
{-# LANGUAGE GADTs, RequiredTypeArguments #-}
data T a where
MkT :: forall a -> T a
f :: T Int -> Int
f (MkT x) = x
```
```
Bug.hs:6:13: error: [GHC-01928]
• Illegal term-level use of the type variable ‘x’
• bound at Bug.hs:6:8
• In the expression: x
In an equation for ‘f’: f (MkT x) = x
|
6 | f (MkT x) = x
| ^
```
Since `MkT x` and `MkT (type x)` are the same program after the T2T conversion, the expected
behaviour is `[GHC-76037]`, reported at the binding site of `x` on the LHS of the synonym.
</details>
issue