{-# LANGUAGE TypeApplications           #-}
{-# LANGUAGE DeriveFunctor              #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE RankNTypes                 #-}
{-# LANGUAGE ScopedTypeVariables        #-}
{-# LANGUAGE TypeFamilies               #-}
{-# LANGUAGE ViewPatterns               #-}
{-# LANGUAGE DisambiguateRecordFields   #-}
{-# LANGUAGE NamedFieldPuns             #-}
{-# LANGUAGE MultiWayIf                 #-}
{-# LANGUAGE DerivingStrategies         #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

{-
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998

Renaming of patterns

Basically dependency analysis.

Handles @Match@, @GRHSs@, @HsExpr@, and @Qualifier@ datatypes.  In
general, all of these functions return a renamed thing, and a set of
free variables.
-}
module GHC.Rename.Pat (-- main entry points
              rnPat, rnPats, rnBindPat,

              NameMaker, applyNameMaker,     -- a utility for making names:
              localRecNameMaker, topRecNameMaker,  --   sometimes we want to make local names,
                                             --   sometimes we want to make top (qualified) names.
              isTopRecNameMaker,

              rnHsRecFields, HsRecFieldContext(..),
              rnHsRecUpdFields,

              -- CpsRn monad
              CpsRn, liftCps, liftCpsWithCont,

              -- Literals
              rnLit, rnOverLit,
             ) where

-- ENH: thin imports to only what is necessary for patterns

import GHC.Prelude

import {-# SOURCE #-} GHC.Rename.Expr ( rnLExpr )
import {-# SOURCE #-} GHC.Rename.Splice ( rnSplicePat, rnSpliceTyPat )

import GHC.Hs
import GHC.Tc.Errors.Types
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.TcMType ( hsOverLitName )
import GHC.Rename.Env
import GHC.Rename.Fixity
import GHC.Rename.Utils    ( newLocalBndrRn, bindLocalNames
                           , warnUnusedMatches, newLocalBndrRn
                           , checkUnusedRecordWildcard
                           , checkDupNames, checkDupAndShadowedNames
                           , wrapGenSpan, genHsApps, genLHsVar, genHsIntegralLit, delLocalNames, typeAppErr )
import GHC.Rename.HsType
import GHC.Builtin.Names

import GHC.Types.Name
import GHC.Types.Name.Set
import GHC.Types.Name.Reader
import GHC.Types.Unique.Set

import GHC.Types.Basic
import GHC.Types.SourceText
import GHC.Utils.Misc
import GHC.Data.FastString ( uniqCompareFS )
import GHC.Data.List.SetOps( removeDups )
import GHC.Data.Bag ( Bag, unitBag, unionBags, emptyBag, listToBag, bagToList )
import GHC.Utils.Outputable
import GHC.Utils.Panic.Plain
import GHC.Types.SrcLoc
import GHC.Types.Literal   ( inCharRange )
import GHC.Types.GREInfo   ( ConInfo(..), conInfoFields )
import GHC.Builtin.Types   ( nilDataCon )
import GHC.Core.DataCon
import GHC.Core.TyCon      ( isKindName )
import qualified GHC.LanguageExtensions as LangExt

import Control.Monad       ( when, ap, guard, unless )
import Data.Foldable
import Data.Function       ( on )
import Data.Functor.Identity ( Identity (..) )
import qualified Data.List.NonEmpty as NE
import Data.Maybe
import Data.Ratio
import qualified Data.Semigroup as S
import Control.Monad.Trans.Writer.CPS
import Control.Monad.Trans.Class
import Control.Monad.Trans.Reader
import Data.Functor ((<&>))
import GHC.Rename.Doc (rnLHsDoc)
import GHC.Types.Hint
import GHC.Types.Fixity (LexicalFixity(..))
import Data.Coerce

{-
*********************************************************
*                                                      *
        The CpsRn Monad
*                                                      *
*********************************************************

Note [CpsRn monad]
~~~~~~~~~~~~~~~~~~
The CpsRn monad uses continuation-passing style to support this
style of programming:

        do { ...
           ; ns <- bindNames rs
           ; ...blah... }

   where rs::[RdrName], ns::[Name]

The idea is that '...blah...'
  a) sees the bindings of ns
  b) returns the free variables it mentions
     so that bindNames can report unused ones

In particular,
    mapM rnPatAndThen [p1, p2, p3]
has a *left-to-right* scoping: it makes the binders in
p1 scope over p2,p3.
-}

newtype CpsRn b = CpsRn { forall b.
CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
unCpsRn :: forall r. (b -> RnM (r, FreeVars))
                                            -> RnM (r, FreeVars) }
        deriving ((forall a b. (a -> b) -> CpsRn a -> CpsRn b)
-> (forall a b. a -> CpsRn b -> CpsRn a) -> Functor CpsRn
forall a b. a -> CpsRn b -> CpsRn a
forall a b. (a -> b) -> CpsRn a -> CpsRn b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> CpsRn a -> CpsRn b
fmap :: forall a b. (a -> b) -> CpsRn a -> CpsRn b
$c<$ :: forall a b. a -> CpsRn b -> CpsRn a
<$ :: forall a b. a -> CpsRn b -> CpsRn a
Functor)
        -- See Note [CpsRn monad]

instance Applicative CpsRn where
    pure :: forall a. a -> CpsRn a
pure a
x = (forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn a
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\a -> RnM (r, FreeVars)
k -> a -> RnM (r, FreeVars)
k a
x)
    <*> :: forall a b. CpsRn (a -> b) -> CpsRn a -> CpsRn b
(<*>) = CpsRn (a -> b) -> CpsRn a -> CpsRn b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Monad CpsRn where
  (CpsRn forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
m) >>= :: forall a b. CpsRn a -> (a -> CpsRn b) -> CpsRn b
>>= a -> CpsRn b
mk = (forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\b -> RnM (r, FreeVars)
k -> (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
m (\a
v -> CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
forall b.
CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
unCpsRn (a -> CpsRn b
mk a
v) b -> RnM (r, FreeVars)
k))

runCps :: CpsRn a -> RnM (a, FreeVars)
runCps :: forall a. CpsRn a -> RnM (a, FreeVars)
runCps (CpsRn forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
m) = (a -> RnM (a, FreeVars)) -> RnM (a, FreeVars)
forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
m (\a
r -> (a, FreeVars) -> RnM (a, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
r, FreeVars
emptyFVs))

liftCps :: RnM a -> CpsRn a
liftCps :: forall a. RnM a -> CpsRn a
liftCps RnM a
rn_thing = (forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn a
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\a -> RnM (r, FreeVars)
k -> RnM a
rn_thing RnM a -> (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> (a -> IOEnv (Env TcGblEnv TcLclEnv) b)
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> RnM (r, FreeVars)
k)

liftCpsFV :: RnM (a, FreeVars) -> CpsRn a
liftCpsFV :: forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV RnM (a, FreeVars)
rn_thing = (forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn a
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\a -> RnM (r, FreeVars)
k -> do { (a
v,FreeVars
fvs1) <- RnM (a, FreeVars)
rn_thing
                                     ; (r
r,FreeVars
fvs2) <- a -> RnM (r, FreeVars)
k a
v
                                     ; (r, FreeVars) -> RnM (r, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (r
r, FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) })

liftCpsWithCont :: (forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)) -> CpsRn b
liftCpsWithCont :: forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
liftCpsWithCont = (forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn

wrapSrcSpanCps :: (a -> CpsRn b) -> LocatedA a -> CpsRn (LocatedA b)
-- Set the location, and also wrap it around the value returned
wrapSrcSpanCps :: forall a b. (a -> CpsRn b) -> LocatedA a -> CpsRn (LocatedA b)
wrapSrcSpanCps a -> CpsRn b
fn (L EpAnn AnnListItem
loc a
a)
  = (forall r. (LocatedA b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn (LocatedA b)
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\LocatedA b -> RnM (r, FreeVars)
k -> EpAnn AnnListItem -> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall ann a. EpAnn ann -> TcRn a -> TcRn a
setSrcSpanA EpAnn AnnListItem
loc (RnM (r, FreeVars) -> RnM (r, FreeVars))
-> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a b. (a -> b) -> a -> b
$
                 CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
forall b.
CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
unCpsRn (a -> CpsRn b
fn a
a) ((b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
forall a b. (a -> b) -> a -> b
$ \b
v ->
                 LocatedA b -> RnM (r, FreeVars)
k (EpAnn AnnListItem -> b -> LocatedA b
forall l e. l -> e -> GenLocated l e
L EpAnn AnnListItem
loc b
v))

lookupConCps :: LocatedN RdrName -> CpsRn (LocatedN Name)
lookupConCps :: GenLocated SrcSpanAnnN RdrName -> CpsRn (LocatedN Name)
lookupConCps GenLocated SrcSpanAnnN RdrName
con_rdr
  = (forall r.
 (LocatedN Name -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn (LocatedN Name)
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\LocatedN Name -> RnM (r, FreeVars)
k -> do { LocatedN Name
con_name <- GenLocated SrcSpanAnnN RdrName -> TcRn (LocatedN Name)
forall ann.
GenLocated (EpAnn ann) RdrName
-> TcRn (GenLocated (EpAnn ann) Name)
lookupLocatedOccRnConstr GenLocated SrcSpanAnnN RdrName
con_rdr
                    ; (r
r, FreeVars
fvs) <- LocatedN Name -> RnM (r, FreeVars)
k LocatedN Name
con_name
                    ; (r, FreeVars) -> RnM (r, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (r
r, FreeVars -> Name -> FreeVars
addOneFV FreeVars
fvs (LocatedN Name -> Name
forall l e. GenLocated l e -> e
unLoc LocatedN Name
con_name)) })
    -- We add the constructor name to the free vars
    -- See Note [Patterns are uses]

{-
Note [Patterns are uses]
~~~~~~~~~~~~~~~~~~~~~~~~
Consider
  module Foo( f, g ) where
  data T = T1 | T2

  f T1 = True
  f T2 = False

  g _ = T1

Arguably we should report T2 as unused, even though it appears in a
pattern, because it never occurs in a constructed position.
See #7336.
However, implementing this in the face of pattern synonyms would be
less straightforward, since given two pattern synonyms

  pattern P1 <- P2
  pattern P2 <- ()

we need to observe the dependency between P1 and P2 so that type
checking can be done in the correct order (just like for value
bindings). Dependencies between bindings is analyzed in the renamer,
where we don't know yet whether P2 is a constructor or a pattern
synonym. So for now, we do report conid occurrences in patterns as
uses.

*********************************************************
*                                                      *
        Name makers
*                                                      *
*********************************************************

Externally abstract type of name makers,
which is how you go from a RdrName to a Name
-}

data NameMaker
  = LamMk       -- Lambdas
      Bool      -- True <=> report unused bindings
                --   (even if True, the warning only comes out
                --    if -Wunused-matches is on)

  | LetMk       -- Let bindings, incl top level
                -- Do *not* check for unused bindings
      TopLevelFlag
      MiniFixityEnv

topRecNameMaker :: MiniFixityEnv -> NameMaker
topRecNameMaker :: MiniFixityEnv -> NameMaker
topRecNameMaker MiniFixityEnv
fix_env = TopLevelFlag -> MiniFixityEnv -> NameMaker
LetMk TopLevelFlag
TopLevel MiniFixityEnv
fix_env

isTopRecNameMaker :: NameMaker -> Bool
isTopRecNameMaker :: NameMaker -> Bool
isTopRecNameMaker (LetMk TopLevelFlag
TopLevel MiniFixityEnv
_) = Bool
True
isTopRecNameMaker NameMaker
_ = Bool
False

localRecNameMaker :: MiniFixityEnv -> NameMaker
localRecNameMaker :: MiniFixityEnv -> NameMaker
localRecNameMaker MiniFixityEnv
fix_env = TopLevelFlag -> MiniFixityEnv -> NameMaker
LetMk TopLevelFlag
NotTopLevel MiniFixityEnv
fix_env

matchNameMaker :: HsMatchContext fn -> NameMaker
matchNameMaker :: forall fn. HsMatchContext fn -> NameMaker
matchNameMaker HsMatchContext fn
ctxt = Bool -> NameMaker
LamMk Bool
report_unused
  where
    -- Do not report unused names in interactive contexts
    -- i.e. when you type 'x <- e' at the GHCi prompt
    report_unused :: Bool
report_unused = case HsMatchContext fn
ctxt of
                      StmtCtxt (HsDoStmt HsDoFlavour
GhciStmtCtxt) -> Bool
False
                      -- also, don't warn in pattern quotes, as there
                      -- is no RHS where the variables can be used!
                      HsMatchContext fn
ThPatQuote            -> Bool
False
                      HsMatchContext fn
_                     -> Bool
True

newPatLName :: NameMaker -> LocatedN RdrName -> CpsRn (LocatedN Name)
newPatLName :: NameMaker
-> GenLocated SrcSpanAnnN RdrName -> CpsRn (LocatedN Name)
newPatLName NameMaker
name_maker rdr_name :: GenLocated SrcSpanAnnN RdrName
rdr_name@(L SrcSpanAnnN
loc RdrName
_)
  = do { Name
name <- NameMaker -> GenLocated SrcSpanAnnN RdrName -> CpsRn Name
newPatName NameMaker
name_maker GenLocated SrcSpanAnnN RdrName
rdr_name
       ; LocatedN Name -> CpsRn (LocatedN Name)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpanAnnN -> Name -> LocatedN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
loc Name
name) }

newPatName :: NameMaker -> LocatedN RdrName -> CpsRn Name
newPatName :: NameMaker -> GenLocated SrcSpanAnnN RdrName -> CpsRn Name
newPatName (LamMk Bool
report_unused) GenLocated SrcSpanAnnN RdrName
rdr_name
  = (forall r. (Name -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn Name
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\ Name -> RnM (r, FreeVars)
thing_inside ->
        do { Name
name <- GenLocated SrcSpanAnnN RdrName -> RnM Name
newLocalBndrRn GenLocated SrcSpanAnnN RdrName
rdr_name
           ; (r
res, FreeVars
fvs) <- [Name] -> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a. [Name] -> RnM a -> RnM a
bindLocalNames [Name
name] (Name -> RnM (r, FreeVars)
thing_inside Name
name)
           ; Bool
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
report_unused (IOEnv (Env TcGblEnv TcLclEnv) ()
 -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$ [Name] -> FreeVars -> IOEnv (Env TcGblEnv TcLclEnv) ()
warnUnusedMatches [Name
name] FreeVars
fvs
           ; (r, FreeVars) -> RnM (r, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (r
res, Name
name Name -> FreeVars -> FreeVars
`delFV` FreeVars
fvs) })

newPatName (LetMk TopLevelFlag
is_top MiniFixityEnv
fix_env) GenLocated SrcSpanAnnN RdrName
rdr_name
  = (forall r. (Name -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn Name
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\ Name -> RnM (r, FreeVars)
thing_inside ->
        do { Name
name <- case TopLevelFlag
is_top of
                       TopLevelFlag
NotTopLevel -> GenLocated SrcSpanAnnN RdrName -> RnM Name
newLocalBndrRn GenLocated SrcSpanAnnN RdrName
rdr_name
                       TopLevelFlag
TopLevel    -> GenLocated SrcSpanAnnN RdrName -> RnM Name
newTopSrcBinder GenLocated SrcSpanAnnN RdrName
rdr_name
           ; [Name] -> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a. [Name] -> RnM a -> RnM a
bindLocalNames [Name
name] (RnM (r, FreeVars) -> RnM (r, FreeVars))
-> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a b. (a -> b) -> a -> b
$
                 -- Do *not* use bindLocalNameFV here;
                 --   see Note [View pattern usage]
                 -- For the TopLevel case
                 --   see Note [bindLocalNames for an External name]
             MiniFixityEnv -> [Name] -> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a. MiniFixityEnv -> [Name] -> RnM a -> RnM a
addLocalFixities MiniFixityEnv
fix_env [Name
name] (RnM (r, FreeVars) -> RnM (r, FreeVars))
-> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a b. (a -> b) -> a -> b
$
             Name -> RnM (r, FreeVars)
thing_inside Name
name })

{- Note [bindLocalNames for an External name]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the TopLevel case, the use of bindLocalNames here is somewhat
suspicious because it binds a top-level External name in the
LocalRdrEnv.  c.f. Note [LocalRdrEnv] in GHC.Types.Name.Reader.

However, this only happens when renaming the LHS (only) of a top-level
pattern binding.  Even though this only the LHS, we need to bring the
binder into scope in the pattern itself in case the binder is used in
subsequent view patterns.  A bit bizarre, something like
  (x, Just y <- f x) = e

Anyway, bindLocalNames does work, and the binding only exists for the
duration of the pattern; then the top-level name is added to the
global env before going on to the RHSes (see GHC.Rename.Module).

Note [View pattern usage]
~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
  let (r, (r -> x)) = x in ...
Here the pattern binds 'r', and then uses it *only* in the view pattern.
We want to "see" this use, and in let-bindings we collect all uses and
report unused variables at the binding level. So we must use bindLocalNames
here, *not* bindLocalNameFV.  #3943.


Note [Don't report shadowing for pattern synonyms]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is one special context where a pattern doesn't introduce any new binders -
pattern synonym declarations. Therefore we don't check to see if pattern
variables shadow existing identifiers as they are never bound to anything
and have no scope.

Without this check, there would be quite a cryptic warning that the `x`
in the RHS of the pattern synonym declaration shadowed the top level `x`.

```
x :: ()
x = ()

pattern P x = Just x
```

See #12615 for some more examples.

Note [Handling overloaded and rebindable patterns]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Overloaded patterns and rebindable patterns are desugared in the renamer
using the HsPatExpansion mechanism detailed in:
Note [Rebindable syntax and XXExprGhcRn]
The approach is similar to that of expressions, which is further detailed
in Note [Handling overloaded and rebindable constructs] in GHC.Rename.Expr.

Here are the patterns that are currently desugared in this way:

* ListPat (list patterns [p1,p2,p3])
  When (and only when) OverloadedLists is on, desugar to a view pattern:
    [p1, p2, p3]
  ==>
    toList -> [p1, p2, p3]
              ^^^^^^^^^^^^ built-in (non-overloaded) list pattern
  NB: the type checker and desugarer still see ListPat,
      but to them it always means the built-in list pattern.
  See Note [Desugaring overloaded list patterns] below for more details.

We expect to add to this list as we deal with more patterns via the expansion
mechanism.

Note [Desugaring overloaded list patterns]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If OverloadedLists is enabled, we desugar a list pattern to a view pattern:

  [p1, p2, p3]
==>
  toList -> [p1, p2, p3]

This happens directly in the renamer, using the HsPatExpansion mechanism
detailed in Note [Rebindable syntax and XXExprGhcRn].

Note that we emit a special view pattern: we additionally keep track of an
inverse to the pattern.
See Note [Invertible view patterns] in GHC.Tc.TyCl.PatSyn for details.

== Wrinkle ==

This is all fine, except in one very specific case:
  - when RebindableSyntax is off,
  - and the type being matched on is already a list type.

In this case, it is undesirable to desugar an overloaded list pattern into
a view pattern. To illustrate, consider the following program:

> {-# LANGUAGE OverloadedLists #-}
>
> f []    = True
> f (_:_) = False

Without any special logic, the pattern `[]` is desugared to `(toList -> [])`,
whereas `(_:_)` remains a constructor pattern. This implies that the argument
of `f` is necessarily a list (even though `OverloadedLists` is enabled).
After desugaring the overloaded list pattern `[]`, and type-checking, we obtain:

> f :: [a] -> Bool
> f (toList -> []) = True
> f (_:_)          = False

The pattern match checker then warns that the pattern `[]` is not covered,
as it isn't able to look through view patterns.
We can see that this is silly: as we are matching on a list, `toList` doesn't
actually do anything. So we ignore it, and desugar the pattern to an explicit
list pattern, instead of a view pattern.

Note however that this is not necessarily sound, because it is possible to have
a list `l` such that `toList l` is not the same as `l`.
This can happen with an overlapping instance, such as the following:

instance {-# OVERLAPPING #-} IsList [Int] where
  type Item [Int] = Int
  toList = reverse
  fromList = reverse

We make the assumption that no such instance exists, in order to avoid worsening
pattern-match warnings (see #14547).

*********************************************************
*                                                      *
        External entry points
*                                                      *
*********************************************************

There are various entry points to renaming patterns, depending on
 (1) whether the names created should be top-level names or local names
 (2) whether the scope of the names is entirely given in a continuation
     (e.g., in a case or lambda, but not in a let or at the top-level,
      because of the way mutually recursive bindings are handled)
 (3) whether the a type signature in the pattern can bind
        lexically-scoped type variables (for unpacking existential
        type vars in data constructors)
 (4) whether we do duplicate and unused variable checking
 (5) whether there are fixity declarations associated with the names
     bound by the patterns that need to be brought into scope with them.

 Rather than burdening the clients of this module with all of these choices,
 we export the three points in this design space that we actually need:
-}

-- ----------- Entry point 1: rnPats -------------------
-- Binds local names; the scope of the bindings is entirely in the thing_inside
--   * allows type sigs to bind type vars
--   * local namemaker
--   * unused and duplicate checking
--   * no fixities

-- rn_pats_general is the generalisation of two functions:
--    rnPats, rnPat
-- Those are the only call sites, so we inline it for improved performance.
-- Kind of like a macro.
{-# INLINE rn_pats_general #-}
rn_pats_general :: Traversable f => HsMatchContextRn
  -> f (LPat GhcPs)
  -> (f (LPat GhcRn) -> RnM (r, FreeVars))
  -> RnM (r, FreeVars)
rn_pats_general :: forall (f :: * -> *) r.
Traversable f =>
HsMatchContextRn
-> f (LPat GhcPs)
-> (f (LPat GhcRn) -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
rn_pats_general HsMatchContextRn
ctxt f (LPat GhcPs)
pats f (LPat GhcRn) -> RnM (r, FreeVars)
thing_inside = do
  (GlobalRdrEnv, LocalRdrEnv)
envs_before <- TcRn (GlobalRdrEnv, LocalRdrEnv)
getRdrEnvs

  -- (1) rename the patterns, bringing into scope all of the term variables
  -- (2) then do the thing inside.
  CpsRn (f (LocatedA (Pat GhcRn)))
-> forall r.
   (f (LocatedA (Pat GhcRn)) -> RnM (r, FreeVars))
   -> RnM (r, FreeVars)
forall b.
CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
unCpsRn (NameMaker
-> f (LocatedA (Pat GhcPs)) -> CpsRn (f (LocatedA (Pat GhcRn)))
rn_pats_fun (HsMatchContext (LocatedN Name) -> NameMaker
forall fn. HsMatchContext fn -> NameMaker
matchNameMaker HsMatchContextRn
HsMatchContext (LocatedN Name)
ctxt) f (LPat GhcPs)
f (LocatedA (Pat GhcPs))
pats) ((f (LocatedA (Pat GhcRn)) -> RnM (r, FreeVars))
 -> RnM (r, FreeVars))
-> (f (LocatedA (Pat GhcRn)) -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
forall a b. (a -> b) -> a -> b
$ \ f (LocatedA (Pat GhcRn))
pats' -> do
    -- Check for duplicated and shadowed names
    -- Must do this *after* renaming the patterns
    -- See Note [Collect binders only after renaming] in GHC.Hs.Utils
    -- Because we don't bind the vars all at once, we can't
    --    check incrementally for duplicates;
    -- Nor can we check incrementally for shadowing, else we'll
    --    complain *twice* about duplicates e.g. f (x,x) = ...
    --
    -- See Note [Don't report shadowing for pattern synonyms]
    let bndrs :: [IdP GhcRn]
bndrs = CollectFlag GhcRn -> [LPat GhcRn] -> [IdP GhcRn]
forall p. CollectPass p => CollectFlag p -> [LPat p] -> [IdP p]
collectPatsBinders CollectFlag GhcRn
CollVarTyVarBinders (f (LocatedA (Pat GhcRn)) -> [LocatedA (Pat GhcRn)]
forall a. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList f (LocatedA (Pat GhcRn))
pats')
    SDoc
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a. SDoc -> TcM a -> TcM a
addErrCtxt SDoc
doc_pat (IOEnv (Env TcGblEnv TcLclEnv) ()
 -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$
      if HsMatchContext (LocatedN Name) -> Bool
forall fn. HsMatchContext fn -> Bool
isPatSynCtxt HsMatchContextRn
HsMatchContext (LocatedN Name)
ctxt
         then [Name] -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkDupNames [IdP GhcRn]
[Name]
bndrs
         else (GlobalRdrEnv, LocalRdrEnv)
-> [Name] -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkDupAndShadowedNames (GlobalRdrEnv, LocalRdrEnv)
envs_before [IdP GhcRn]
[Name]
bndrs
    f (LPat GhcRn) -> RnM (r, FreeVars)
thing_inside f (LPat GhcRn)
f (LocatedA (Pat GhcRn))
pats'
  where
    doc_pat :: SDoc
doc_pat = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"In" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> HsMatchContext (LocatedN Name) -> SDoc
forall fn. Outputable fn => HsMatchContext fn -> SDoc
pprMatchContext HsMatchContextRn
HsMatchContext (LocatedN Name)
ctxt

    -- See Note [Invisible binders in functions] in GHC.Hs.Pat
    --
    -- BTW, Or-patterns would be awesome here
    rn_pats_fun :: NameMaker
-> f (LocatedA (Pat GhcPs)) -> CpsRn (f (LocatedA (Pat GhcRn)))
rn_pats_fun = case HsMatchContextRn
ctxt of
      FunRhs{} -> (LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
-> f (LocatedA (Pat GhcPs)) -> CpsRn (f (LocatedA (Pat GhcRn)))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> f a -> m (f b)
mapM ((LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
 -> f (LocatedA (Pat GhcPs)) -> CpsRn (f (LocatedA (Pat GhcRn))))
-> (NameMaker
    -> LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
-> NameMaker
-> f (LocatedA (Pat GhcPs))
-> CpsRn (f (LocatedA (Pat GhcRn)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NameMaker -> LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn))
rnLArgPatAndThen
      LamAlt HsLamVariant
LamSingle -> (LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
-> f (LocatedA (Pat GhcPs)) -> CpsRn (f (LocatedA (Pat GhcRn)))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> f a -> m (f b)
mapM ((LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
 -> f (LocatedA (Pat GhcPs)) -> CpsRn (f (LocatedA (Pat GhcRn))))
-> (NameMaker
    -> LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
-> NameMaker
-> f (LocatedA (Pat GhcPs))
-> CpsRn (f (LocatedA (Pat GhcRn)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NameMaker -> LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn))
rnLArgPatAndThen
      LamAlt HsLamVariant
LamCases -> (LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
-> f (LocatedA (Pat GhcPs)) -> CpsRn (f (LocatedA (Pat GhcRn)))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> f a -> m (f b)
mapM ((LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
 -> f (LocatedA (Pat GhcPs)) -> CpsRn (f (LocatedA (Pat GhcRn))))
-> (NameMaker
    -> LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
-> NameMaker
-> f (LocatedA (Pat GhcPs))
-> CpsRn (f (LocatedA (Pat GhcRn)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NameMaker -> LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn))
rnLArgPatAndThen
      HsMatchContextRn
_ -> (LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
-> f (LocatedA (Pat GhcPs)) -> CpsRn (f (LocatedA (Pat GhcRn)))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> f a -> m (f b)
mapM ((LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
 -> f (LocatedA (Pat GhcPs)) -> CpsRn (f (LocatedA (Pat GhcRn))))
-> (NameMaker
    -> LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
-> NameMaker
-> f (LocatedA (Pat GhcPs))
-> CpsRn (f (LocatedA (Pat GhcRn)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
NameMaker -> LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn))
rnLPatAndThen

rnPats :: HsMatchContextRn   -- For error messages and choosing if @-patterns are allowed
       -> [LPat GhcPs]
       -> ([LPat GhcRn] -> RnM (a, FreeVars))
       -> RnM (a, FreeVars)
rnPats :: forall a.
HsMatchContextRn
-> [LPat GhcPs]
-> ([LPat GhcRn] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnPats = HsMatchContextRn
-> [LPat GhcPs]
-> ([LPat GhcRn] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
forall (f :: * -> *) r.
Traversable f =>
HsMatchContextRn
-> f (LPat GhcPs)
-> (f (LPat GhcRn) -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
rn_pats_general

rnPat :: forall a. HsMatchContextRn      -- For error messages and choosing if @-patterns are allowed
      -> LPat GhcPs
      -> (LPat GhcRn -> RnM (a, FreeVars))
      -> RnM (a, FreeVars)     -- Variables bound by pattern do not
                               -- appear in the result FreeVars
rnPat :: forall a.
HsMatchContextRn
-> LPat GhcPs
-> (LPat GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnPat
       = (HsMatchContext (LocatedN Name)
 -> Identity (LocatedA (Pat GhcPs))
 -> (Identity (LocatedA (Pat GhcRn)) -> RnM (a, FreeVars))
 -> RnM (a, FreeVars))
-> HsMatchContext (LocatedN Name)
-> LocatedA (Pat GhcPs)
-> (LocatedA (Pat GhcRn) -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
forall a b. Coercible a b => a -> b
coerce (forall (f :: * -> *) r.
Traversable f =>
HsMatchContextRn
-> f (LPat GhcPs)
-> (f (LPat GhcRn) -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
rn_pats_general @Identity @a)

applyNameMaker :: NameMaker -> LocatedN RdrName -> RnM (LocatedN Name)
applyNameMaker :: NameMaker -> GenLocated SrcSpanAnnN RdrName -> TcRn (LocatedN Name)
applyNameMaker NameMaker
mk GenLocated SrcSpanAnnN RdrName
rdr = do { (LocatedN Name
n, FreeVars
_fvs) <- CpsRn (LocatedN Name) -> RnM (LocatedN Name, FreeVars)
forall a. CpsRn a -> RnM (a, FreeVars)
runCps (NameMaker
-> GenLocated SrcSpanAnnN RdrName -> CpsRn (LocatedN Name)
newPatLName NameMaker
mk GenLocated SrcSpanAnnN RdrName
rdr)
                           ; LocatedN Name -> TcRn (LocatedN Name)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return LocatedN Name
n }

-- ----------- Entry point 2: rnBindPat -------------------
-- Binds local names; in a recursive scope that involves other bound vars
--      e.g let { (x, Just y) = e1; ... } in ...
--   * does NOT allows type sig to bind type vars
--   * local namemaker
--   * no unused and duplicate checking
--   * fixities might be coming in
rnBindPat :: NameMaker
          -> LPat GhcPs
          -> RnM (LPat GhcRn, FreeVars)
   -- Returned FreeVars are the free variables of the pattern,
   -- of course excluding variables bound by this pattern

rnBindPat :: NameMaker -> LPat GhcPs -> RnM (LPat GhcRn, FreeVars)
rnBindPat NameMaker
name_maker LPat GhcPs
pat = CpsRn (LocatedA (Pat GhcRn))
-> RnM (LocatedA (Pat GhcRn), FreeVars)
forall a. CpsRn a -> RnM (a, FreeVars)
runCps (NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
name_maker LPat GhcPs
pat)

{-
*********************************************************
*                                                      *
        The main event
*                                                      *
*********************************************************
-}


rnLArgPatAndThen :: NameMaker -> LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn))
rnLArgPatAndThen :: NameMaker -> LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn))
rnLArgPatAndThen NameMaker
mk = (Pat GhcPs -> CpsRn (Pat GhcRn))
-> LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn))
forall a b. (a -> CpsRn b) -> LocatedA a -> CpsRn (LocatedA b)
wrapSrcSpanCps Pat GhcPs -> CpsRn (Pat GhcRn)
rnArgPatAndThen where

  rnArgPatAndThen :: Pat GhcPs -> CpsRn (Pat GhcRn)
rnArgPatAndThen (InvisPat XInvisPat GhcPs
_ HsTyPat (NoGhcTc GhcPs)
tp) = do
    IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a. RnM a -> CpsRn a
liftCps (IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ())
-> IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a b. (a -> b) -> a -> b
$ Extension
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall gbl lcl. Extension -> TcRnIf gbl lcl () -> TcRnIf gbl lcl ()
unlessXOptM Extension
LangExt.TypeAbstractions (IOEnv (Env TcGblEnv TcLclEnv) ()
 -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$
      TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr (HsTyPat GhcPs -> TcRnMessage
TcRnIllegalInvisibleTypePattern HsTyPat (NoGhcTc GhcPs)
HsTyPat GhcPs
tp)
    HsTyPat GhcRn
tp' <- HsDocContext -> HsTyPat GhcPs -> CpsRn (HsTyPat GhcRn)
rnHsTyPat HsDocContext
HsTypePatCtx HsTyPat (NoGhcTc GhcPs)
HsTyPat GhcPs
tp
    Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XInvisPat GhcRn -> HsTyPat (NoGhcTc GhcRn) -> Pat GhcRn
forall p. XInvisPat p -> HsTyPat (NoGhcTc p) -> Pat p
InvisPat XInvisPat GhcRn
NoExtField
noExtField HsTyPat (NoGhcTc GhcRn)
HsTyPat GhcRn
tp')
  rnArgPatAndThen Pat GhcPs
p = NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen NameMaker
mk Pat GhcPs
p

-- ----------- Entry point 3: rnLPatAndThen -------------------
-- General version: parameterized by how you make new names

rnLPatsAndThen :: NameMaker -> [LPat GhcPs] -> CpsRn [LPat GhcRn]
rnLPatsAndThen :: NameMaker -> [LPat GhcPs] -> CpsRn [LPat GhcRn]
rnLPatsAndThen NameMaker
mk = (LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn)))
-> [LocatedA (Pat GhcPs)] -> CpsRn [LocatedA (Pat GhcRn)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk)
  -- Despite the map, the monad ensures that each pattern binds
  -- variables that may be mentioned in subsequent patterns in the list

--------------------
-- The workhorse
rnLPatAndThen :: NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen :: NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
nm LPat GhcPs
lpat = (Pat GhcPs -> CpsRn (Pat GhcRn))
-> LocatedA (Pat GhcPs) -> CpsRn (LocatedA (Pat GhcRn))
forall a b. (a -> CpsRn b) -> LocatedA a -> CpsRn (LocatedA b)
wrapSrcSpanCps (NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen NameMaker
nm) LPat GhcPs
LocatedA (Pat GhcPs)
lpat

rnPatAndThen :: NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen :: NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen NameMaker
_  (WildPat XWildPat GhcPs
_)   = Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XWildPat GhcRn -> Pat GhcRn
forall p. XWildPat p -> Pat p
WildPat XWildPat GhcRn
NoExtField
noExtField)
rnPatAndThen NameMaker
mk (ParPat XParPat GhcPs
_ LPat GhcPs
pat) =
  do { LocatedA (Pat GhcRn)
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
     ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XParPat GhcRn -> LPat GhcRn -> Pat GhcRn
forall p. XParPat p -> LPat p -> Pat p
ParPat XParPat GhcRn
NoExtField
noExtField LPat GhcRn
LocatedA (Pat GhcRn)
pat') }
rnPatAndThen NameMaker
mk (LazyPat XLazyPat GhcPs
_ LPat GhcPs
pat) = do { LocatedA (Pat GhcRn)
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
                                     ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XLazyPat GhcRn -> LPat GhcRn -> Pat GhcRn
forall p. XLazyPat p -> LPat p -> Pat p
LazyPat XLazyPat GhcRn
NoExtField
noExtField LPat GhcRn
LocatedA (Pat GhcRn)
pat') }
rnPatAndThen NameMaker
mk (BangPat XBangPat GhcPs
_ LPat GhcPs
pat) = do { LocatedA (Pat GhcRn)
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
                                     ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XBangPat GhcRn -> LPat GhcRn -> Pat GhcRn
forall p. XBangPat p -> LPat p -> Pat p
BangPat XBangPat GhcRn
NoExtField
noExtField LPat GhcRn
LocatedA (Pat GhcRn)
pat') }
rnPatAndThen NameMaker
mk (VarPat XVarPat GhcPs
x (L SrcSpanAnnN
l RdrName
rdr))
    = do { SrcSpan
loc <- RnM SrcSpan -> CpsRn SrcSpan
forall a. RnM a -> CpsRn a
liftCps RnM SrcSpan
getSrcSpanM
         ; Name
name <- NameMaker -> GenLocated SrcSpanAnnN RdrName -> CpsRn Name
newPatName NameMaker
mk (SrcSpanAnnN -> RdrName -> GenLocated SrcSpanAnnN RdrName
forall l e. l -> e -> GenLocated l e
L (SrcSpan -> SrcSpanAnnN
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
loc) RdrName
rdr)
         ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XVarPat GhcRn -> LIdP GhcRn -> Pat GhcRn
forall p. XVarPat p -> LIdP p -> Pat p
VarPat XVarPat GhcPs
XVarPat GhcRn
x (SrcSpanAnnN -> Name -> LocatedN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
l Name
name)) }
     -- we need to bind pattern variables for view pattern expressions
     -- (e.g. in the pattern (x, x -> y) x needs to be bound in the rhs of the tuple)

rnPatAndThen NameMaker
mk (SigPat XSigPat GhcPs
_ LPat GhcPs
pat HsPatSigType (NoGhcTc GhcPs)
sig)
  -- When renaming a pattern type signature (e.g. f (a :: T) = ...), it is
  -- important to rename its type signature _before_ renaming the rest of the
  -- pattern, so that type variables are first bound by the _outermost_ pattern
  -- type signature they occur in. This keeps the type checker happy when
  -- pattern type signatures happen to be nested (#7827)
  --
  -- f ((Just (x :: a) :: Maybe a)
  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~^       `a' is first bound here
  -- ~~~~~~~~~~~~~~~^                   the same `a' then used here
  = do { HsPatSigType GhcRn
sig' <- HsPatSigType GhcPs -> CpsRn (HsPatSigType GhcRn)
rnHsPatSigTypeAndThen HsPatSigType (NoGhcTc GhcPs)
HsPatSigType GhcPs
sig
       ; LocatedA (Pat GhcRn)
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
       ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XSigPat GhcRn
-> LPat GhcRn -> HsPatSigType (NoGhcTc GhcRn) -> Pat GhcRn
forall p. XSigPat p -> LPat p -> HsPatSigType (NoGhcTc p) -> Pat p
SigPat XSigPat GhcRn
NoExtField
noExtField LPat GhcRn
LocatedA (Pat GhcRn)
pat' HsPatSigType (NoGhcTc GhcRn)
HsPatSigType GhcRn
sig' ) }
  where
    rnHsPatSigTypeAndThen :: HsPatSigType GhcPs -> CpsRn (HsPatSigType GhcRn)
    rnHsPatSigTypeAndThen :: HsPatSigType GhcPs -> CpsRn (HsPatSigType GhcRn)
rnHsPatSigTypeAndThen HsPatSigType GhcPs
sig = (forall r.
 (HsPatSigType GhcRn -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn (HsPatSigType GhcRn)
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
liftCpsWithCont (HsPatSigTypeScoping
-> HsDocContext
-> HsPatSigType GhcPs
-> (HsPatSigType GhcRn -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
forall a.
HsPatSigTypeScoping
-> HsDocContext
-> HsPatSigType GhcPs
-> (HsPatSigType GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnHsPatSigType HsPatSigTypeScoping
AlwaysBind HsDocContext
PatCtx HsPatSigType GhcPs
sig)

rnPatAndThen NameMaker
mk (LitPat XLitPat GhcPs
x HsLit GhcPs
lit)
  | HsString XHsString GhcPs
src FastString
s <- HsLit GhcPs
lit
  = do { Bool
ovlStr <- RnM Bool -> CpsRn Bool
forall a. RnM a -> CpsRn a
liftCps (Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.OverloadedStrings)
       ; if Bool
ovlStr
         then NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen NameMaker
mk
                           (LocatedAn NoEpAnns (HsOverLit GhcPs)
-> Maybe (SyntaxExpr GhcPs) -> [AddEpAnn] -> Pat GhcPs
mkNPat (HsOverLit GhcPs -> LocatedAn NoEpAnns (HsOverLit GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (SourceText -> FastString -> HsOverLit GhcPs
mkHsIsString XHsString GhcPs
SourceText
src FastString
s))
                                      Maybe NoExtField
Maybe (SyntaxExpr GhcPs)
forall a. Maybe a
Nothing [AddEpAnn]
forall a. NoAnn a => a
noAnn)
         else CpsRn (Pat GhcRn)
normal_lit }
  | Bool
otherwise = CpsRn (Pat GhcRn)
normal_lit
  where
    normal_lit :: CpsRn (Pat GhcRn)
normal_lit = do { IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a. RnM a -> CpsRn a
liftCps (HsLit GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall p. HsLit p -> IOEnv (Env TcGblEnv TcLclEnv) ()
rnLit HsLit GhcPs
lit); Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XLitPat GhcRn -> HsLit GhcRn -> Pat GhcRn
forall p. XLitPat p -> HsLit p -> Pat p
LitPat XLitPat GhcPs
XLitPat GhcRn
x (HsLit GhcPs -> HsLit GhcRn
forall (p1 :: Pass) (p2 :: Pass).
HsLit (GhcPass p1) -> HsLit (GhcPass p2)
convertLit HsLit GhcPs
lit)) }

rnPatAndThen NameMaker
_ (NPat XNPat GhcPs
x (L EpAnn NoEpAnns
l HsOverLit GhcPs
lit) Maybe (SyntaxExpr GhcPs)
mb_neg SyntaxExpr GhcPs
_eq)
  = do { (HsOverLit GhcRn
lit', Maybe (HsExpr GhcRn)
mb_neg') <- RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
-> CpsRn (HsOverLit GhcRn, Maybe (HsExpr GhcRn))
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
 -> CpsRn (HsOverLit GhcRn, Maybe (HsExpr GhcRn)))
-> RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
-> CpsRn (HsOverLit GhcRn, Maybe (HsExpr GhcRn))
forall a b. (a -> b) -> a -> b
$ HsOverLit GhcPs
-> RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
forall t.
(XXOverLit t ~ DataConCantHappen) =>
HsOverLit t
-> RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
rnOverLit HsOverLit GhcPs
lit
       ; Maybe SyntaxExprRn
mb_neg' -- See Note [Negative zero]
           <- let negative :: IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
negative = do { (SyntaxExprRn
neg, FreeVars
fvs) <- Name -> RnM (SyntaxExpr GhcRn, FreeVars)
lookupSyntax Name
negateName
                                ; (Maybe SyntaxExprRn, FreeVars)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (SyntaxExprRn -> Maybe SyntaxExprRn
forall a. a -> Maybe a
Just SyntaxExprRn
neg, FreeVars
fvs) }
                  positive :: IOEnv (Env TcGblEnv TcLclEnv) (Maybe a, FreeVars)
positive = (Maybe a, FreeVars)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe a, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a
forall a. Maybe a
Nothing, FreeVars
emptyFVs)
              in IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
-> CpsRn (Maybe SyntaxExprRn)
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
 -> CpsRn (Maybe SyntaxExprRn))
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
-> CpsRn (Maybe SyntaxExprRn)
forall a b. (a -> b) -> a -> b
$ case (Maybe NoExtField
Maybe (SyntaxExpr GhcPs)
mb_neg , Maybe (HsExpr GhcRn)
mb_neg') of
                                  (Maybe NoExtField
Nothing, Just HsExpr GhcRn
_ ) -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
negative
                                  (Just NoExtField
_ , Maybe (HsExpr GhcRn)
Nothing) -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
negative
                                  (Maybe NoExtField
Nothing, Maybe (HsExpr GhcRn)
Nothing) -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
forall {a}. IOEnv (Env TcGblEnv TcLclEnv) (Maybe a, FreeVars)
positive
                                  (Just NoExtField
_ , Just HsExpr GhcRn
_ ) -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
forall {a}. IOEnv (Env TcGblEnv TcLclEnv) (Maybe a, FreeVars)
positive
       ; SyntaxExprRn
eq' <- RnM (SyntaxExpr GhcRn, FreeVars) -> CpsRn (SyntaxExpr GhcRn)
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (RnM (SyntaxExpr GhcRn, FreeVars) -> CpsRn (SyntaxExpr GhcRn))
-> RnM (SyntaxExpr GhcRn, FreeVars) -> CpsRn (SyntaxExpr GhcRn)
forall a b. (a -> b) -> a -> b
$ Name -> RnM (SyntaxExpr GhcRn, FreeVars)
lookupSyntax Name
eqName
       ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XNPat GhcRn
-> XRec GhcRn (HsOverLit GhcRn)
-> Maybe (SyntaxExpr GhcRn)
-> SyntaxExpr GhcRn
-> Pat GhcRn
forall p.
XNPat p
-> XRec p (HsOverLit p)
-> Maybe (SyntaxExpr p)
-> SyntaxExpr p
-> Pat p
NPat XNPat GhcPs
XNPat GhcRn
x (EpAnn NoEpAnns
-> HsOverLit GhcRn -> GenLocated (EpAnn NoEpAnns) (HsOverLit GhcRn)
forall l e. l -> e -> GenLocated l e
L EpAnn NoEpAnns
l HsOverLit GhcRn
lit') Maybe (SyntaxExpr GhcRn)
Maybe SyntaxExprRn
mb_neg' SyntaxExpr GhcRn
SyntaxExprRn
eq') }

rnPatAndThen NameMaker
mk (NPlusKPat XNPlusKPat GhcPs
_ XRec GhcPs (IdP GhcPs)
rdr (L EpAnn NoEpAnns
l HsOverLit GhcPs
lit) HsOverLit GhcPs
_ SyntaxExpr GhcPs
_ SyntaxExpr GhcPs
_ )
  = do { Name
new_name <- NameMaker -> GenLocated SrcSpanAnnN RdrName -> CpsRn Name
newPatName NameMaker
mk (GenLocated SrcSpanAnnN RdrName -> GenLocated SrcSpanAnnN RdrName
forall l l2 a.
(HasLoc l, HasAnnotation l2) =>
GenLocated l a -> GenLocated l2 a
la2la XRec GhcPs (IdP GhcPs)
GenLocated SrcSpanAnnN RdrName
rdr)
       ; (HsOverLit GhcRn
lit', Maybe (HsExpr GhcRn)
_) <- RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
-> CpsRn (HsOverLit GhcRn, Maybe (HsExpr GhcRn))
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
 -> CpsRn (HsOverLit GhcRn, Maybe (HsExpr GhcRn)))
-> RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
-> CpsRn (HsOverLit GhcRn, Maybe (HsExpr GhcRn))
forall a b. (a -> b) -> a -> b
$ HsOverLit GhcPs
-> RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
forall t.
(XXOverLit t ~ DataConCantHappen) =>
HsOverLit t
-> RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
rnOverLit HsOverLit GhcPs
lit -- See Note [Negative zero]
                                                -- We skip negateName as
                                                -- negative zero doesn't make
                                                -- sense in n + k patterns
       ; SyntaxExprRn
minus <- RnM (SyntaxExpr GhcRn, FreeVars) -> CpsRn (SyntaxExpr GhcRn)
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (RnM (SyntaxExpr GhcRn, FreeVars) -> CpsRn (SyntaxExpr GhcRn))
-> RnM (SyntaxExpr GhcRn, FreeVars) -> CpsRn (SyntaxExpr GhcRn)
forall a b. (a -> b) -> a -> b
$ Name -> RnM (SyntaxExpr GhcRn, FreeVars)
lookupSyntax Name
minusName
       ; SyntaxExprRn
ge    <- RnM (SyntaxExpr GhcRn, FreeVars) -> CpsRn (SyntaxExpr GhcRn)
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (RnM (SyntaxExpr GhcRn, FreeVars) -> CpsRn (SyntaxExpr GhcRn))
-> RnM (SyntaxExpr GhcRn, FreeVars) -> CpsRn (SyntaxExpr GhcRn)
forall a b. (a -> b) -> a -> b
$ Name -> RnM (SyntaxExpr GhcRn, FreeVars)
lookupSyntax Name
geName
       ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XNPlusKPat GhcRn
-> LIdP GhcRn
-> XRec GhcRn (HsOverLit GhcRn)
-> HsOverLit GhcRn
-> SyntaxExpr GhcRn
-> SyntaxExpr GhcRn
-> Pat GhcRn
forall p.
XNPlusKPat p
-> LIdP p
-> XRec p (HsOverLit p)
-> HsOverLit p
-> SyntaxExpr p
-> SyntaxExpr p
-> Pat p
NPlusKPat XNPlusKPat GhcRn
NoExtField
noExtField (SrcSpanAnnN -> Name -> LocatedN Name
forall l e. l -> e -> GenLocated l e
L (SrcSpan -> SrcSpanAnnN
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan (SrcSpan -> SrcSpanAnnN) -> SrcSpan -> SrcSpanAnnN
forall a b. (a -> b) -> a -> b
$ Name -> SrcSpan
nameSrcSpan Name
new_name) Name
new_name)
                                      (EpAnn NoEpAnns
-> HsOverLit GhcRn -> GenLocated (EpAnn NoEpAnns) (HsOverLit GhcRn)
forall l e. l -> e -> GenLocated l e
L EpAnn NoEpAnns
l HsOverLit GhcRn
lit') HsOverLit GhcRn
lit' SyntaxExpr GhcRn
SyntaxExprRn
ge SyntaxExpr GhcRn
SyntaxExprRn
minus) }
                -- The Report says that n+k patterns must be in Integral

rnPatAndThen NameMaker
mk (AsPat XAsPat GhcPs
_ XRec GhcPs (IdP GhcPs)
rdr LPat GhcPs
pat)
  = do { LocatedN Name
new_name <- NameMaker
-> GenLocated SrcSpanAnnN RdrName -> CpsRn (LocatedN Name)
newPatLName NameMaker
mk XRec GhcPs (IdP GhcPs)
GenLocated SrcSpanAnnN RdrName
rdr
       ; LocatedA (Pat GhcRn)
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
       ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XAsPat GhcRn -> LIdP GhcRn -> LPat GhcRn -> Pat GhcRn
forall p. XAsPat p -> LIdP p -> LPat p -> Pat p
AsPat XAsPat GhcRn
NoExtField
noExtField LIdP GhcRn
LocatedN Name
new_name LPat GhcRn
LocatedA (Pat GhcRn)
pat') }

rnPatAndThen NameMaker
mk p :: Pat GhcPs
p@(ViewPat XViewPat GhcPs
_ LHsExpr GhcPs
expr LPat GhcPs
pat)
  = do { IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a. RnM a -> CpsRn a
liftCps (IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ())
-> IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a b. (a -> b) -> a -> b
$ do { Bool
vp_flag <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.ViewPatterns
                      ; Bool -> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkErr Bool
vp_flag (Pat GhcPs -> TcRnMessage
TcRnIllegalViewPattern Pat GhcPs
p) }
         -- Because of the way we're arranging the recursive calls,
         -- this will be in the right context
       ; GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)
expr' <- RnM (LHsExpr GhcRn, FreeVars) -> CpsRn (LHsExpr GhcRn)
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (RnM (LHsExpr GhcRn, FreeVars) -> CpsRn (LHsExpr GhcRn))
-> RnM (LHsExpr GhcRn, FreeVars) -> CpsRn (LHsExpr GhcRn)
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcPs -> RnM (LHsExpr GhcRn, FreeVars)
rnLExpr LHsExpr GhcPs
expr
       ; LocatedA (Pat GhcRn)
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
       -- Note: at this point the PreTcType in ty can only be a placeHolder
       -- ; return (ViewPat expr' pat' ty) }

       -- Note: we can't cook up an inverse for an arbitrary view pattern,
       -- so we pass 'Nothing'.
       ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XViewPat GhcRn -> LHsExpr GhcRn -> LPat GhcRn -> Pat GhcRn
forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat Maybe (HsExpr GhcRn)
XViewPat GhcRn
forall a. Maybe a
Nothing LHsExpr GhcRn
GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)
expr' LPat GhcRn
LocatedA (Pat GhcRn)
pat') }

rnPatAndThen NameMaker
mk (ConPat XConPat GhcPs
_ XRec GhcPs (ConLikeP GhcPs)
con HsConPatDetails GhcPs
args)
   -- rnConPatAndThen takes care of reconstructing the pattern
   -- The pattern for the empty list needs to be replaced by an empty explicit list pattern when overloaded lists is turned on.
  = case GenLocated SrcSpanAnnN RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc XRec GhcPs (ConLikeP GhcPs)
GenLocated SrcSpanAnnN RdrName
con RdrName -> RdrName -> Bool
forall a. Eq a => a -> a -> Bool
== Name -> RdrName
nameRdrName (DataCon -> Name
dataConName DataCon
nilDataCon) of
      Bool
True    -> do { Bool
ol_flag <- RnM Bool -> CpsRn Bool
forall a. RnM a -> CpsRn a
liftCps (RnM Bool -> CpsRn Bool) -> RnM Bool -> CpsRn Bool
forall a b. (a -> b) -> a -> b
$ Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.OverloadedLists
                    ; if Bool
ol_flag then NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen NameMaker
mk (XListPat GhcPs -> [LPat GhcPs] -> Pat GhcPs
forall p. XListPat p -> [LPat p] -> Pat p
ListPat XListPat GhcPs
AnnList
forall a. NoAnn a => a
noAnn [])
                                 else NameMaker
-> GenLocated SrcSpanAnnN RdrName
-> HsConPatDetails GhcPs
-> CpsRn (Pat GhcRn)
rnConPatAndThen NameMaker
mk XRec GhcPs (ConLikeP GhcPs)
GenLocated SrcSpanAnnN RdrName
con HsConPatDetails GhcPs
args}
      Bool
False   -> NameMaker
-> GenLocated SrcSpanAnnN RdrName
-> HsConPatDetails GhcPs
-> CpsRn (Pat GhcRn)
rnConPatAndThen NameMaker
mk XRec GhcPs (ConLikeP GhcPs)
GenLocated SrcSpanAnnN RdrName
con HsConPatDetails GhcPs
args

rnPatAndThen NameMaker
mk (ListPat XListPat GhcPs
_ [LPat GhcPs]
pats)
  = do { Bool
opt_OverloadedLists  <- RnM Bool -> CpsRn Bool
forall a. RnM a -> CpsRn a
liftCps (RnM Bool -> CpsRn Bool) -> RnM Bool -> CpsRn Bool
forall a b. (a -> b) -> a -> b
$ Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.OverloadedLists
       ; [LocatedA (Pat GhcRn)]
pats' <- NameMaker -> [LPat GhcPs] -> CpsRn [LPat GhcRn]
rnLPatsAndThen NameMaker
mk [LPat GhcPs]
pats
       ; if Bool -> Bool
not Bool
opt_OverloadedLists
         then Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XListPat GhcRn -> [LPat GhcRn] -> Pat GhcRn
forall p. XListPat p -> [LPat p] -> Pat p
ListPat XListPat GhcRn
NoExtField
noExtField [LPat GhcRn]
[LocatedA (Pat GhcRn)]
pats')
         else
    -- If OverloadedLists is enabled, desugar to a view pattern.
    -- See Note [Desugaring overloaded list patterns]
    do { (Name
to_list_name,FreeVars
_)     <- RnM (Name, FreeVars) -> CpsRn (Name, FreeVars)
forall a. RnM a -> CpsRn a
liftCps (RnM (Name, FreeVars) -> CpsRn (Name, FreeVars))
-> RnM (Name, FreeVars) -> CpsRn (Name, FreeVars)
forall a b. (a -> b) -> a -> b
$ Name -> RnM (Name, FreeVars)
lookupSyntaxName Name
toListName
       -- Use 'fromList' as proof of invertibility of the view pattern.
       -- See Note [Invertible view patterns] in GHC.Tc.TyCl.PatSyn
       ; (Name
from_list_n_name,FreeVars
_) <- RnM (Name, FreeVars) -> CpsRn (Name, FreeVars)
forall a. RnM a -> CpsRn a
liftCps (RnM (Name, FreeVars) -> CpsRn (Name, FreeVars))
-> RnM (Name, FreeVars) -> CpsRn (Name, FreeVars)
forall a b. (a -> b) -> a -> b
$ Name -> RnM (Name, FreeVars)
lookupSyntaxName Name
fromListNName
       ; let
           lit_n :: IntegralLit
lit_n   = Int -> IntegralLit
forall a. Integral a => a -> IntegralLit
mkIntegralLit ([LocatedA (Pat GhcPs)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LPat GhcPs]
[LocatedA (Pat GhcPs)]
pats)
           hs_lit :: GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)
hs_lit  = IntegralLit -> GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)
forall an. NoAnn an => IntegralLit -> LocatedAn an (HsExpr GhcRn)
genHsIntegralLit IntegralLit
lit_n
           inverse :: HsExpr GhcRn
inverse = Name -> [LHsExpr GhcRn] -> HsExpr GhcRn
genHsApps Name
from_list_n_name [LHsExpr GhcRn
GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)
hs_lit]
           rn_list_pat :: Pat GhcRn
rn_list_pat  = XListPat GhcRn -> [LPat GhcRn] -> Pat GhcRn
forall p. XListPat p -> [LPat p] -> Pat p
ListPat XListPat GhcRn
NoExtField
noExtField [LPat GhcRn]
[LocatedA (Pat GhcRn)]
pats'
           exp_expr :: LHsExpr GhcRn
exp_expr     = Name -> LHsExpr GhcRn
genLHsVar Name
to_list_name
           exp_list_pat :: Pat GhcRn
exp_list_pat = XViewPat GhcRn -> LHsExpr GhcRn -> LPat GhcRn -> Pat GhcRn
forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat (HsExpr GhcRn -> Maybe (HsExpr GhcRn)
forall a. a -> Maybe a
Just HsExpr GhcRn
inverse) LHsExpr GhcRn
exp_expr (Pat GhcRn -> LocatedA (Pat GhcRn)
forall an a. NoAnn an => a -> LocatedAn an a
wrapGenSpan Pat GhcRn
rn_list_pat)
       ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (Pat GhcRn -> CpsRn (Pat GhcRn)) -> Pat GhcRn -> CpsRn (Pat GhcRn)
forall a b. (a -> b) -> a -> b
$ Pat GhcRn -> Pat GhcRn -> Pat GhcRn
mkExpandedPat Pat GhcRn
rn_list_pat Pat GhcRn
exp_list_pat }}

rnPatAndThen NameMaker
mk (TuplePat XTuplePat GhcPs
_ [LPat GhcPs]
pats Boxity
boxed)
  = do { [LocatedA (Pat GhcRn)]
pats' <- NameMaker -> [LPat GhcPs] -> CpsRn [LPat GhcRn]
rnLPatsAndThen NameMaker
mk [LPat GhcPs]
pats
       ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XTuplePat GhcRn -> [LPat GhcRn] -> Boxity -> Pat GhcRn
forall p. XTuplePat p -> [LPat p] -> Boxity -> Pat p
TuplePat XTuplePat GhcRn
NoExtField
noExtField [LPat GhcRn]
[LocatedA (Pat GhcRn)]
pats' Boxity
boxed) }

rnPatAndThen NameMaker
mk (SumPat XSumPat GhcPs
_ LPat GhcPs
pat Int
alt Int
arity)
  = do { LocatedA (Pat GhcRn)
pat <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
       ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XSumPat GhcRn -> LPat GhcRn -> Int -> Int -> Pat GhcRn
forall p. XSumPat p -> LPat p -> Int -> Int -> Pat p
SumPat XSumPat GhcRn
NoExtField
noExtField LPat GhcRn
LocatedA (Pat GhcRn)
pat Int
alt Int
arity)
       }

rnPatAndThen NameMaker
mk (SplicePat XSplicePat GhcPs
_ HsUntypedSplice GhcPs
splice)
  = do { (HsUntypedSplice GhcRn,
 HsUntypedSpliceResult (LocatedA (Pat GhcPs)))
eith <- RnM
  ((HsUntypedSplice GhcRn, HsUntypedSpliceResult (LPat GhcPs)),
   FreeVars)
-> CpsRn
     (HsUntypedSplice GhcRn, HsUntypedSpliceResult (LPat GhcPs))
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (RnM
   ((HsUntypedSplice GhcRn, HsUntypedSpliceResult (LPat GhcPs)),
    FreeVars)
 -> CpsRn
      (HsUntypedSplice GhcRn, HsUntypedSpliceResult (LPat GhcPs)))
-> RnM
     ((HsUntypedSplice GhcRn, HsUntypedSpliceResult (LPat GhcPs)),
      FreeVars)
-> CpsRn
     (HsUntypedSplice GhcRn, HsUntypedSpliceResult (LPat GhcPs))
forall a b. (a -> b) -> a -> b
$ HsUntypedSplice GhcPs
-> RnM
     ((HsUntypedSplice GhcRn, HsUntypedSpliceResult (LPat GhcPs)),
      FreeVars)
rnSplicePat HsUntypedSplice GhcPs
splice
       ; case (HsUntypedSplice GhcRn,
 HsUntypedSpliceResult (LocatedA (Pat GhcPs)))
eith of   -- See Note [rnSplicePat] in GHC.Rename.Splice
           (HsUntypedSplice GhcRn
rn_splice, HsUntypedSpliceTop ThModFinalizers
mfs LocatedA (Pat GhcPs)
pat) -> -- Splice was top-level and thus run, creating Pat GhcPs
               LPat GhcRn -> Pat GhcRn
LocatedA (Pat GhcRn) -> Pat GhcRn
forall (p :: Pass). IsPass p => LPat (GhcPass p) -> Pat (GhcPass p)
gParPat (LocatedA (Pat GhcRn) -> Pat GhcRn)
-> (LocatedA (Pat GhcRn) -> LocatedA (Pat GhcRn))
-> LocatedA (Pat GhcRn)
-> Pat GhcRn
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Pat GhcRn -> Pat GhcRn)
-> LocatedA (Pat GhcRn) -> LocatedA (Pat GhcRn)
forall a b.
(a -> b)
-> GenLocated (EpAnn AnnListItem) a
-> GenLocated (EpAnn AnnListItem) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((HsUntypedSpliceResult (Pat GhcRn)
 -> HsUntypedSplice GhcRn -> Pat GhcRn)
-> HsUntypedSplice GhcRn
-> HsUntypedSpliceResult (Pat GhcRn)
-> Pat GhcRn
forall a b c. (a -> b -> c) -> b -> a -> c
flip XSplicePat GhcRn -> HsUntypedSplice GhcRn -> Pat GhcRn
HsUntypedSpliceResult (Pat GhcRn)
-> HsUntypedSplice GhcRn -> Pat GhcRn
forall p. XSplicePat p -> HsUntypedSplice p -> Pat p
SplicePat HsUntypedSplice GhcRn
rn_splice (HsUntypedSpliceResult (Pat GhcRn) -> Pat GhcRn)
-> (Pat GhcRn -> HsUntypedSpliceResult (Pat GhcRn))
-> Pat GhcRn
-> Pat GhcRn
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ThModFinalizers -> Pat GhcRn -> HsUntypedSpliceResult (Pat GhcRn)
forall thing.
ThModFinalizers -> thing -> HsUntypedSpliceResult thing
HsUntypedSpliceTop ThModFinalizers
mfs)) (LocatedA (Pat GhcRn) -> Pat GhcRn)
-> CpsRn (LocatedA (Pat GhcRn)) -> CpsRn (Pat GhcRn)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
LocatedA (Pat GhcPs)
pat
           (HsUntypedSplice GhcRn
rn_splice, HsUntypedSpliceNested Name
splice_name) -> Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XSplicePat GhcRn -> HsUntypedSplice GhcRn -> Pat GhcRn
forall p. XSplicePat p -> HsUntypedSplice p -> Pat p
SplicePat (Name -> HsUntypedSpliceResult (Pat GhcRn)
forall thing. Name -> HsUntypedSpliceResult thing
HsUntypedSpliceNested Name
splice_name) HsUntypedSplice GhcRn
rn_splice) -- Splice was nested and thus already renamed
       }

rnPatAndThen NameMaker
_ (EmbTyPat XEmbTyPat GhcPs
_ HsTyPat (NoGhcTc GhcPs)
tp)
  = do { HsTyPat GhcRn
tp' <- HsDocContext -> HsTyPat GhcPs -> CpsRn (HsTyPat GhcRn)
rnHsTyPat HsDocContext
HsTypePatCtx HsTyPat (NoGhcTc GhcPs)
HsTyPat GhcPs
tp
       ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XEmbTyPat GhcRn -> HsTyPat (NoGhcTc GhcRn) -> Pat GhcRn
forall p. XEmbTyPat p -> HsTyPat (NoGhcTc p) -> Pat p
EmbTyPat XEmbTyPat GhcRn
NoExtField
noExtField HsTyPat (NoGhcTc GhcRn)
HsTyPat GhcRn
tp') }
rnPatAndThen NameMaker
_ (InvisPat XInvisPat GhcPs
_ HsTyPat (NoGhcTc GhcPs)
tp)
  = do { IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a. RnM a -> CpsRn a
liftCps (IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ())
-> IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr (HsTyPat GhcPs -> TcRnMessage
TcRnMisplacedInvisPat HsTyPat (NoGhcTc GhcPs)
HsTyPat GhcPs
tp)
         -- Invisible patterns are handled in `rnLArgPatAndThen`
         -- so unconditionally emit error here
       ; HsTyPat GhcRn
tp' <- HsDocContext -> HsTyPat GhcPs -> CpsRn (HsTyPat GhcRn)
rnHsTyPat HsDocContext
HsTypePatCtx HsTyPat (NoGhcTc GhcPs)
HsTyPat GhcPs
tp
       ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XInvisPat GhcRn -> HsTyPat (NoGhcTc GhcRn) -> Pat GhcRn
forall p. XInvisPat p -> HsTyPat (NoGhcTc p) -> Pat p
InvisPat XInvisPat GhcRn
NoExtField
noExtField HsTyPat (NoGhcTc GhcRn)
HsTyPat GhcRn
tp')
       }

--------------------
rnConPatAndThen :: NameMaker
                -> LocatedN RdrName    -- the constructor
                -> HsConPatDetails GhcPs
                -> CpsRn (Pat GhcRn)

rnConPatAndThen :: NameMaker
-> GenLocated SrcSpanAnnN RdrName
-> HsConPatDetails GhcPs
-> CpsRn (Pat GhcRn)
rnConPatAndThen NameMaker
mk GenLocated SrcSpanAnnN RdrName
con (PrefixCon [HsConPatTyArg (NoGhcTc GhcPs)]
tyargs [LPat GhcPs]
pats)
  = do  { LocatedN Name
con' <- GenLocated SrcSpanAnnN RdrName -> CpsRn (LocatedN Name)
lookupConCps GenLocated SrcSpanAnnN RdrName
con
        ; IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a. RnM a -> CpsRn a
liftCps IOEnv (Env TcGblEnv TcLclEnv) ()
check_lang_exts
        ; [HsConPatTyArg GhcRn]
tyargs' <- (HsConPatTyArg GhcPs -> CpsRn (HsConPatTyArg GhcRn))
-> [HsConPatTyArg GhcPs] -> CpsRn [HsConPatTyArg GhcRn]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM HsConPatTyArg GhcPs -> CpsRn (HsConPatTyArg GhcRn)
rnConPatTyArg [HsConPatTyArg (NoGhcTc GhcPs)]
[HsConPatTyArg GhcPs]
tyargs
        ; [LocatedA (Pat GhcRn)]
pats' <- NameMaker -> [LPat GhcPs] -> CpsRn [LPat GhcRn]
rnLPatsAndThen NameMaker
mk [LPat GhcPs]
pats
        ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (Pat GhcRn -> CpsRn (Pat GhcRn)) -> Pat GhcRn -> CpsRn (Pat GhcRn)
forall a b. (a -> b) -> a -> b
$ ConPat
            { pat_con_ext :: XConPat GhcRn
pat_con_ext = XConPat GhcRn
NoExtField
noExtField
            , pat_con :: XRec GhcRn (ConLikeP GhcRn)
pat_con = XRec GhcRn (ConLikeP GhcRn)
LocatedN Name
con'
            , pat_args :: HsConPatDetails GhcRn
pat_args = [HsConPatTyArg GhcRn]
-> [LocatedA (Pat GhcRn)]
-> HsConDetails
     (HsConPatTyArg GhcRn)
     (LocatedA (Pat GhcRn))
     (HsRecFields GhcRn (LocatedA (Pat GhcRn)))
forall tyarg arg rec.
[tyarg] -> [arg] -> HsConDetails tyarg arg rec
PrefixCon [HsConPatTyArg GhcRn]
tyargs' [LocatedA (Pat GhcRn)]
pats'
            }
        }
  where
    check_lang_exts :: RnM ()
    check_lang_exts :: IOEnv (Env TcGblEnv TcLclEnv) ()
check_lang_exts =
      Maybe (HsConPatTyArg GhcPs)
-> (HsConPatTyArg GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ ([HsConPatTyArg GhcPs] -> Maybe (HsConPatTyArg GhcPs)
forall a. [a] -> Maybe a
listToMaybe [HsConPatTyArg (NoGhcTc GhcPs)]
[HsConPatTyArg GhcPs]
tyargs) ((HsConPatTyArg GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) ())
 -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> (HsConPatTyArg GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$ \ HsConPatTyArg GhcPs
arg ->
        do { Bool
type_abs   <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.TypeAbstractions
           ; Bool
type_app   <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.TypeApplications
           ; Bool
scoped_tvs <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.ScopedTypeVariables
           -- See Note [Deprecated type abstractions in constructor patterns]
           ; if | Bool
type_abs -> () -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                | Bool
type_app Bool -> Bool -> Bool
&& Bool
scoped_tvs -> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addDiagnostic TcRnMessage
TcRnDeprecatedInvisTyArgInConPat
                | Bool
otherwise -> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErrTc (TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$ TypeApplication -> TcRnMessage
TcRnTypeApplicationsDisabled (HsConPatTyArg GhcPs -> TypeApplication
TypeApplicationInPattern HsConPatTyArg GhcPs
arg)
           }

    rnConPatTyArg :: HsConPatTyArg GhcPs -> CpsRn (HsConPatTyArg GhcRn)
rnConPatTyArg (HsConPatTyArg XConPatTyArg GhcPs
_ HsTyPat GhcPs
t) = do
      HsTyPat GhcRn
t' <- HsDocContext -> HsTyPat GhcPs -> CpsRn (HsTyPat GhcRn)
rnHsTyPat HsDocContext
HsTypePatCtx HsTyPat GhcPs
t
      HsConPatTyArg GhcRn -> CpsRn (HsConPatTyArg GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (XConPatTyArg GhcRn -> HsTyPat GhcRn -> HsConPatTyArg GhcRn
forall p. XConPatTyArg p -> HsTyPat p -> HsConPatTyArg p
HsConPatTyArg NoExtField
XConPatTyArg GhcRn
noExtField HsTyPat GhcRn
t')

rnConPatAndThen NameMaker
mk GenLocated SrcSpanAnnN RdrName
con (InfixCon LPat GhcPs
pat1 LPat GhcPs
pat2)
  = do  { LocatedN Name
con' <- GenLocated SrcSpanAnnN RdrName -> CpsRn (LocatedN Name)
lookupConCps GenLocated SrcSpanAnnN RdrName
con
        ; LocatedA (Pat GhcRn)
pat1' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat1
        ; LocatedA (Pat GhcRn)
pat2' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat2
        ; Fixity
fixity <- RnM Fixity -> CpsRn Fixity
forall a. RnM a -> CpsRn a
liftCps (RnM Fixity -> CpsRn Fixity) -> RnM Fixity -> CpsRn Fixity
forall a b. (a -> b) -> a -> b
$ Name -> RnM Fixity
lookupFixityRn (LocatedN Name -> Name
forall l e. GenLocated l e -> e
unLoc LocatedN Name
con')
        ; RnM (Pat GhcRn) -> CpsRn (Pat GhcRn)
forall a. RnM a -> CpsRn a
liftCps (RnM (Pat GhcRn) -> CpsRn (Pat GhcRn))
-> RnM (Pat GhcRn) -> CpsRn (Pat GhcRn)
forall a b. (a -> b) -> a -> b
$ LocatedN Name
-> Fixity -> LPat GhcRn -> LPat GhcRn -> RnM (Pat GhcRn)
mkConOpPatRn LocatedN Name
con' Fixity
fixity LPat GhcRn
LocatedA (Pat GhcRn)
pat1' LPat GhcRn
LocatedA (Pat GhcRn)
pat2' }

rnConPatAndThen NameMaker
mk GenLocated SrcSpanAnnN RdrName
con (RecCon HsRecFields GhcPs (LPat GhcPs)
rpats)
  = do  { LocatedN Name
con' <- GenLocated SrcSpanAnnN RdrName -> CpsRn (LocatedN Name)
lookupConCps GenLocated SrcSpanAnnN RdrName
con
        ; HsRecFields GhcRn (LocatedA (Pat GhcRn))
rpats' <- NameMaker
-> LocatedN Name
-> HsRecFields GhcPs (LPat GhcPs)
-> CpsRn (HsRecFields GhcRn (LPat GhcRn))
rnHsRecPatsAndThen NameMaker
mk LocatedN Name
con' HsRecFields GhcPs (LPat GhcPs)
rpats
        ; Pat GhcRn -> CpsRn (Pat GhcRn)
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (Pat GhcRn -> CpsRn (Pat GhcRn)) -> Pat GhcRn -> CpsRn (Pat GhcRn)
forall a b. (a -> b) -> a -> b
$ ConPat
            { pat_con_ext :: XConPat GhcRn
pat_con_ext = XConPat GhcRn
NoExtField
noExtField
            , pat_con :: XRec GhcRn (ConLikeP GhcRn)
pat_con = XRec GhcRn (ConLikeP GhcRn)
LocatedN Name
con'
            , pat_args :: HsConPatDetails GhcRn
pat_args = HsRecFields GhcRn (LocatedA (Pat GhcRn))
-> HsConDetails
     (HsConPatTyArg GhcRn)
     (LocatedA (Pat GhcRn))
     (HsRecFields GhcRn (LocatedA (Pat GhcRn)))
forall tyarg arg rec. rec -> HsConDetails tyarg arg rec
RecCon HsRecFields GhcRn (LocatedA (Pat GhcRn))
rpats'
            }
        }

{- Note [Deprecated type abstractions in constructor patterns]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Type abstractions in constructor patterns allow the user to bind
existential type variables:

    import Type.Reflection (Typeable, typeRep)
    data Ex = forall e. (Typeable e, Show e) => MkEx e
    showEx (MkEx @e a) = show a ++ " :: " ++ show (typeRep @e)

Note the pattern `MkEx @e a`, and specifically the `@e` binder.

For historical reasons, using this feature only required TypeApplications
and ScopedTypeVariables to be enabled. As per GHC Proposal #448 (and especially
its amendment #604) we are now transitioning towards guarding this feature
behind TypeAbstractions instead.

As a compatibility measure, we continue to support old programs that use
TypeApplications with ScopedTypeVariables instead of TypeAbstractions,
but emit the appropriate compatibility warning, -Wdeprecated-type-abstractions.
This warning is scheduled to become an error in GHC 9.14, at which point
we can simply require TypeAbstractions.
-}

checkUnusedRecordWildcardCps :: SrcSpan
                             -> Maybe [ImplicitFieldBinders]
                             -> CpsRn ()
checkUnusedRecordWildcardCps :: SrcSpan -> Maybe [ImplicitFieldBinders] -> CpsRn ()
checkUnusedRecordWildcardCps SrcSpan
loc Maybe [ImplicitFieldBinders]
dotdot_names =
  (forall r. (() -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn ()
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\() -> RnM (r, FreeVars)
thing -> do
                    (r
r, FreeVars
fvs) <- () -> RnM (r, FreeVars)
thing ()
                    SrcSpan
-> FreeVars
-> Maybe [ImplicitFieldBinders]
-> IOEnv (Env TcGblEnv TcLclEnv) ()
checkUnusedRecordWildcard SrcSpan
loc FreeVars
fvs Maybe [ImplicitFieldBinders]
dotdot_names
                    (r, FreeVars) -> RnM (r, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (r
r, FreeVars
fvs) )

--------------------
rnHsRecPatsAndThen :: NameMaker
                   -> LocatedN Name      -- Constructor
                   -> HsRecFields GhcPs (LPat GhcPs)
                   -> CpsRn (HsRecFields GhcRn (LPat GhcRn))
rnHsRecPatsAndThen :: NameMaker
-> LocatedN Name
-> HsRecFields GhcPs (LPat GhcPs)
-> CpsRn (HsRecFields GhcRn (LPat GhcRn))
rnHsRecPatsAndThen NameMaker
mk (L SrcSpanAnnN
_ Name
con)
     hs_rec_fields :: HsRecFields GhcPs (LPat GhcPs)
hs_rec_fields@(HsRecFields { rec_dotdot :: forall p arg. HsRecFields p arg -> Maybe (XRec p RecFieldsDotDot)
rec_dotdot = Maybe (XRec GhcPs RecFieldsDotDot)
dd })
  = do { [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
      (LocatedA (Pat GhcPs)))]
flds <- RnM
  ([GenLocated
      (EpAnn AnnListItem)
      (HsFieldBind
         (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
         (LocatedA (Pat GhcPs)))],
   FreeVars)
-> CpsRn
     [GenLocated
        (EpAnn AnnListItem)
        (HsFieldBind
           (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
           (LocatedA (Pat GhcPs)))]
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (RnM
   ([GenLocated
       (EpAnn AnnListItem)
       (HsFieldBind
          (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
          (LocatedA (Pat GhcPs)))],
    FreeVars)
 -> CpsRn
      [GenLocated
         (EpAnn AnnListItem)
         (HsFieldBind
            (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
            (LocatedA (Pat GhcPs)))])
-> RnM
     ([GenLocated
         (EpAnn AnnListItem)
         (HsFieldBind
            (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
            (LocatedA (Pat GhcPs)))],
      FreeVars)
-> CpsRn
     [GenLocated
        (EpAnn AnnListItem)
        (HsFieldBind
           (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
           (LocatedA (Pat GhcPs)))]
forall a b. (a -> b) -> a -> b
$ HsRecFieldContext
-> (SrcSpan -> RdrName -> Pat GhcPs)
-> HsRecFields GhcPs (LocatedA (Pat GhcPs))
-> RnM ([LHsRecField GhcRn (LocatedA (Pat GhcPs))], FreeVars)
forall arg.
HsRecFieldContext
-> (SrcSpan -> RdrName -> arg)
-> HsRecFields GhcPs (LocatedA arg)
-> RnM ([LHsRecField GhcRn (LocatedA arg)], FreeVars)
rnHsRecFields (Name -> HsRecFieldContext
HsRecFieldPat Name
con) SrcSpan -> IdP GhcPs -> Pat GhcPs
SrcSpan -> RdrName -> Pat GhcPs
forall {p} {l}.
(XVarPat p ~ NoExtField, XRec p (IdP p) ~ GenLocated l (IdP p),
 HasAnnotation l) =>
SrcSpan -> IdP p -> Pat p
mkVarPat
                                            HsRecFields GhcPs (LPat GhcPs)
HsRecFields GhcPs (LocatedA (Pat GhcPs))
hs_rec_fields
       ; [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
      (LocatedA (Pat GhcRn)))]
flds' <- ((GenLocated
    (EpAnn AnnListItem)
    (HsFieldBind
       (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
       (LocatedA (Pat GhcPs))),
  Int)
 -> CpsRn
      (GenLocated
         (EpAnn AnnListItem)
         (HsFieldBind
            (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
            (LocatedA (Pat GhcRn)))))
-> [(GenLocated
       (EpAnn AnnListItem)
       (HsFieldBind
          (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
          (LocatedA (Pat GhcPs))),
     Int)]
-> CpsRn
     [GenLocated
        (EpAnn AnnListItem)
        (HsFieldBind
           (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
           (LocatedA (Pat GhcRn)))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
      (LocatedA (Pat GhcPs))),
 Int)
-> CpsRn
     (GenLocated
        (EpAnn AnnListItem)
        (HsFieldBind
           (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
           (LocatedA (Pat GhcRn))))
rn_field ([GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
      (LocatedA (Pat GhcPs)))]
flds [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
      (LocatedA (Pat GhcPs)))]
-> [Int]
-> [(GenLocated
       (EpAnn AnnListItem)
       (HsFieldBind
          (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
          (LocatedA (Pat GhcPs))),
     Int)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Int
1..])
       ; Maybe [ImplicitFieldBinders] -> CpsRn ()
check_unused_wildcard ([LHsRecField GhcRn (LPat GhcRn)]
-> RecFieldsDotDot -> [ImplicitFieldBinders]
lHsRecFieldsImplicits [LHsRecField GhcRn (LPat GhcRn)]
[GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
      (LocatedA (Pat GhcRn)))]
flds' (RecFieldsDotDot -> [ImplicitFieldBinders])
-> (GenLocated EpaLocation RecFieldsDotDot -> RecFieldsDotDot)
-> GenLocated EpaLocation RecFieldsDotDot
-> [ImplicitFieldBinders]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenLocated EpaLocation RecFieldsDotDot -> RecFieldsDotDot
forall l e. GenLocated l e -> e
unLoc (GenLocated EpaLocation RecFieldsDotDot -> [ImplicitFieldBinders])
-> Maybe (GenLocated EpaLocation RecFieldsDotDot)
-> Maybe [ImplicitFieldBinders]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (XRec GhcPs RecFieldsDotDot)
Maybe (GenLocated EpaLocation RecFieldsDotDot)
dd)
       ; HsRecFields GhcRn (LocatedA (Pat GhcRn))
-> CpsRn (HsRecFields GhcRn (LocatedA (Pat GhcRn)))
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsRecFields { rec_flds :: [LHsRecField GhcRn (LocatedA (Pat GhcRn))]
rec_flds = [LHsRecField GhcRn (LocatedA (Pat GhcRn))]
[GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
      (LocatedA (Pat GhcRn)))]
flds', rec_dotdot :: Maybe (XRec GhcRn RecFieldsDotDot)
rec_dotdot = Maybe (XRec GhcPs RecFieldsDotDot)
Maybe (XRec GhcRn RecFieldsDotDot)
dd }) }
  where
    mkVarPat :: SrcSpan -> IdP p -> Pat p
mkVarPat SrcSpan
l IdP p
n = XVarPat p -> XRec p (IdP p) -> Pat p
forall p. XVarPat p -> LIdP p -> Pat p
VarPat XVarPat p
NoExtField
noExtField (l -> IdP p -> GenLocated l (IdP p)
forall l e. l -> e -> GenLocated l e
L (SrcSpan -> l
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
l) IdP p
n)
    rn_field :: (GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
      (LocatedA (Pat GhcPs))),
 Int)
-> CpsRn
     (GenLocated
        (EpAnn AnnListItem)
        (HsFieldBind
           (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
           (LocatedA (Pat GhcRn))))
rn_field (L EpAnn AnnListItem
l HsFieldBind
  (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
  (LocatedA (Pat GhcPs))
fld, Int
n') =
      do { LocatedA (Pat GhcRn)
arg' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen (Maybe (GenLocated EpaLocation RecFieldsDotDot)
-> NameMaker -> RecFieldsDotDot -> NameMaker
forall {a} {l}.
Ord a =>
Maybe (GenLocated l a) -> NameMaker -> a -> NameMaker
nested_mk Maybe (XRec GhcPs RecFieldsDotDot)
Maybe (GenLocated EpaLocation RecFieldsDotDot)
dd NameMaker
mk (Int -> RecFieldsDotDot
RecFieldsDotDot Int
n')) (HsFieldBind
  (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
  (LocatedA (Pat GhcPs))
-> LocatedA (Pat GhcPs)
forall lhs rhs. HsFieldBind lhs rhs -> rhs
hfbRHS HsFieldBind
  (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
  (LocatedA (Pat GhcPs))
fld)
         ; GenLocated
  (EpAnn AnnListItem)
  (HsFieldBind
     (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
     (LocatedA (Pat GhcRn)))
-> CpsRn
     (GenLocated
        (EpAnn AnnListItem)
        (HsFieldBind
           (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
           (LocatedA (Pat GhcRn))))
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return (EpAnn AnnListItem
-> HsFieldBind
     (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
     (LocatedA (Pat GhcRn))
-> GenLocated
     (EpAnn AnnListItem)
     (HsFieldBind
        (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
        (LocatedA (Pat GhcRn)))
forall l e. l -> e -> GenLocated l e
L EpAnn AnnListItem
l (HsFieldBind
  (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
  (LocatedA (Pat GhcPs))
fld { hfbRHS = arg' })) }

    loc :: SrcSpan
loc = SrcSpan
-> (GenLocated EpaLocation RecFieldsDotDot -> SrcSpan)
-> Maybe (GenLocated EpaLocation RecFieldsDotDot)
-> SrcSpan
forall b a. b -> (a -> b) -> Maybe a -> b
maybe SrcSpan
noSrcSpan GenLocated EpaLocation RecFieldsDotDot -> SrcSpan
forall a e. HasLoc a => GenLocated a e -> SrcSpan
getLocA Maybe (XRec GhcPs RecFieldsDotDot)
Maybe (GenLocated EpaLocation RecFieldsDotDot)
dd

    -- Don't warn for let P{..} = ... in ...
    check_unused_wildcard :: Maybe [ImplicitFieldBinders] -> CpsRn ()
check_unused_wildcard = case NameMaker
mk of
                              LetMk{} -> CpsRn () -> Maybe [ImplicitFieldBinders] -> CpsRn ()
forall a b. a -> b -> a
const (() -> CpsRn ()
forall a. a -> CpsRn a
forall (m :: * -> *) a. Monad m => a -> m a
return ())
                              LamMk{} -> SrcSpan -> Maybe [ImplicitFieldBinders] -> CpsRn ()
checkUnusedRecordWildcardCps SrcSpan
loc

        -- Suppress unused-match reporting for fields introduced by ".."
    nested_mk :: Maybe (GenLocated l a) -> NameMaker -> a -> NameMaker
nested_mk Maybe (GenLocated l a)
Nothing  NameMaker
mk                    a
_  = NameMaker
mk
    nested_mk (Just GenLocated l a
_) mk :: NameMaker
mk@(LetMk {})         a
_  = NameMaker
mk
    nested_mk (Just (GenLocated l a -> a
forall l e. GenLocated l e -> e
unLoc -> a
n)) (LamMk Bool
report_unused) a
n'
      = Bool -> NameMaker
LamMk (Bool
report_unused Bool -> Bool -> Bool
&& (a
n' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
n))


{- *********************************************************************
*                                                                      *
              Generating code for HsPatExpanded
      See Note [Handling overloaded and rebindable constructs]
*                                                                      *
********************************************************************* -}

-- | Build a 'HsPatExpansion' out of an extension constructor,
--   and the two components of the expansion: original and
--   desugared patterns
mkExpandedPat
  :: Pat GhcRn -- ^ source pattern
  -> Pat GhcRn -- ^ expanded pattern
  -> Pat GhcRn -- ^ suitably wrapped 'HsPatExpansion'
mkExpandedPat :: Pat GhcRn -> Pat GhcRn -> Pat GhcRn
mkExpandedPat Pat GhcRn
a Pat GhcRn
b = XXPat GhcRn -> Pat GhcRn
forall p. XXPat p -> Pat p
XPat (Pat GhcRn -> Pat GhcRn -> HsPatExpansion (Pat GhcRn) (Pat GhcRn)
forall a b. a -> b -> HsPatExpansion a b
HsPatExpanded Pat GhcRn
a Pat GhcRn
b)

{-
************************************************************************
*                                                                      *
        Record fields
*                                                                      *
************************************************************************
-}

data HsRecFieldContext
  = HsRecFieldCon Name
  | HsRecFieldPat Name
  | HsRecFieldUpd

rnHsRecFields
    :: forall arg.
       HsRecFieldContext
    -> (SrcSpan -> RdrName -> arg)
         -- When punning, use this to build a new field
    -> HsRecFields GhcPs (LocatedA arg)
    -> RnM ([LHsRecField GhcRn (LocatedA arg)], FreeVars)

-- This surprisingly complicated pass
--   a) looks up the field name (possibly using disambiguation)
--   b) fills in puns and dot-dot stuff
-- When we've finished, we've renamed the LHS, but not the RHS,
-- of each x=e binding
--
-- This is used for record construction and pattern-matching, but not updates.

rnHsRecFields :: forall arg.
HsRecFieldContext
-> (SrcSpan -> RdrName -> arg)
-> HsRecFields GhcPs (LocatedA arg)
-> RnM ([LHsRecField GhcRn (LocatedA arg)], FreeVars)
rnHsRecFields HsRecFieldContext
ctxt SrcSpan -> RdrName -> arg
mk_arg (HsRecFields { rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField GhcPs (LocatedA arg)]
flds, rec_dotdot :: forall p arg. HsRecFields p arg -> Maybe (XRec p RecFieldsDotDot)
rec_dotdot = Maybe (XRec GhcPs RecFieldsDotDot)
dotdot })
  = do { Bool
pun_ok      <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.NamedFieldPuns
       ; Bool
disambig_ok <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DisambiguateRecordFields
       ; let parent :: Maybe Name
parent = Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
disambig_ok Maybe () -> Maybe Name -> Maybe Name
forall a b. Maybe a -> Maybe b -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe Name
mb_con
       ; [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
flds1  <- (GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcPs)) (LocatedA arg))
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (GenLocated
         (EpAnn AnnListItem)
         (HsFieldBind
            (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))))
-> [GenLocated
      (EpAnn AnnListItem)
      (HsFieldBind
         (GenLocated (EpAnn AnnListItem) (FieldOcc GhcPs)) (LocatedA arg))]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     [GenLocated
        (EpAnn AnnListItem)
        (HsFieldBind
           (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Bool
-> Maybe Name
-> LHsRecField GhcPs (LocatedA arg)
-> RnM (LHsRecField GhcRn (LocatedA arg))
rn_fld Bool
pun_ok Maybe Name
parent) [LHsRecField GhcPs (LocatedA arg)]
[GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcPs)) (LocatedA arg))]
flds
       ; (NonEmpty RdrName -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> [NonEmpty RdrName] -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr (TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> (NonEmpty RdrName -> TcRnMessage)
-> NonEmpty RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecFieldContext -> NonEmpty RdrName -> TcRnMessage
dupFieldErr HsRecFieldContext
ctxt) [NonEmpty RdrName]
dup_flds
       ; [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
dotdot_flds <- Maybe (GenLocated EpaLocation RecFieldsDotDot)
-> Maybe Name
-> [LHsRecField GhcRn (LocatedA arg)]
-> RnM [LHsRecField GhcRn (LocatedA arg)]
rn_dotdot Maybe (XRec GhcPs RecFieldsDotDot)
Maybe (GenLocated EpaLocation RecFieldsDotDot)
dotdot Maybe Name
mb_con [LHsRecField GhcRn (LocatedA arg)]
[GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
flds1
       ; let all_flds :: [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
all_flds | [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
-> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
dotdot_flds = [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
flds1
                      | Bool
otherwise        = [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
flds1 [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
-> [GenLocated
      (EpAnn AnnListItem)
      (HsFieldBind
         (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
-> [GenLocated
      (EpAnn AnnListItem)
      (HsFieldBind
         (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
forall a. [a] -> [a] -> [a]
++ [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
dotdot_flds
       ; ([GenLocated
    (EpAnn AnnListItem)
    (HsFieldBind
       (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))],
 FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     ([GenLocated
         (EpAnn AnnListItem)
         (HsFieldBind
            (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))],
      FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
all_flds, [Name] -> FreeVars
mkFVs ([LHsRecField GhcRn (LocatedA arg)] -> [Name]
forall arg. [LHsRecField GhcRn arg] -> [Name]
getFieldIds [LHsRecField GhcRn (LocatedA arg)]
[GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
all_flds)) }
  where
    mb_con :: Maybe Name
mb_con = case HsRecFieldContext
ctxt of
                HsRecFieldCon Name
con  -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
con
                HsRecFieldPat Name
con  -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
con
                HsRecFieldContext
HsRecFieldUpd      -> Maybe Name
forall a. Maybe a
Nothing

    rn_fld :: Bool -> Maybe Name -> LHsRecField GhcPs (LocatedA arg)
           -> RnM (LHsRecField GhcRn (LocatedA arg))
    rn_fld :: Bool
-> Maybe Name
-> LHsRecField GhcPs (LocatedA arg)
-> RnM (LHsRecField GhcRn (LocatedA arg))
rn_fld Bool
pun_ok Maybe Name
parent (L EpAnn AnnListItem
l
                           (HsFieldBind
                              { hfbLHS :: forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS = L EpAnn AnnListItem
loc (FieldOcc XCFieldOcc GhcPs
_ (L SrcSpanAnnN
ll RdrName
lbl))
                              , hfbRHS :: forall lhs rhs. HsFieldBind lhs rhs -> rhs
hfbRHS = LocatedA arg
arg
                              , hfbPun :: forall lhs rhs. HsFieldBind lhs rhs -> Bool
hfbPun = Bool
pun }))
      = do { Name
sel <- EpAnn AnnListItem -> RnM Name -> RnM Name
forall ann a. EpAnn ann -> TcRn a -> TcRn a
setSrcSpanA EpAnn AnnListItem
loc (RnM Name -> RnM Name) -> RnM Name -> RnM Name
forall a b. (a -> b) -> a -> b
$ Maybe Name -> RdrName -> RnM Name
lookupRecFieldOcc Maybe Name
parent RdrName
lbl
           ; let arg_rdr :: RdrName
arg_rdr = OccName -> RdrName
mkRdrUnqual (OccName -> RdrName) -> OccName -> RdrName
forall a b. (a -> b) -> a -> b
$ (() :: Constraint) => OccName -> OccName
OccName -> OccName
recFieldToVarOcc (OccName -> OccName) -> OccName -> OccName
forall a b. (a -> b) -> a -> b
$ Name -> OccName
forall name. HasOccName name => name -> OccName
occName Name
sel
                 -- Discard any module qualifier (#11662)
           ; LocatedA arg
arg' <- if Bool
pun
                     then do { Bool -> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkErr Bool
pun_ok (TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$
                                Located RdrName -> TcRnMessage
TcRnIllegalFieldPunning (SrcSpan -> RdrName -> Located RdrName
forall l e. l -> e -> GenLocated l e
L (EpAnn AnnListItem -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA EpAnn AnnListItem
loc) RdrName
arg_rdr)
                             ; LocatedA arg -> IOEnv (Env TcGblEnv TcLclEnv) (LocatedA arg)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (LocatedA arg -> IOEnv (Env TcGblEnv TcLclEnv) (LocatedA arg))
-> LocatedA arg -> IOEnv (Env TcGblEnv TcLclEnv) (LocatedA arg)
forall a b. (a -> b) -> a -> b
$ EpAnn AnnListItem -> arg -> LocatedA arg
forall l e. l -> e -> GenLocated l e
L (EpAnn AnnListItem -> EpAnn AnnListItem
forall a b. (HasLoc a, HasAnnotation b) => a -> b
l2l EpAnn AnnListItem
loc) (arg -> LocatedA arg) -> arg -> LocatedA arg
forall a b. (a -> b) -> a -> b
$
                                 SrcSpan -> RdrName -> arg
mk_arg (EpAnn AnnListItem -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA EpAnn AnnListItem
loc) RdrName
arg_rdr }
                     else LocatedA arg -> IOEnv (Env TcGblEnv TcLclEnv) (LocatedA arg)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return LocatedA arg
arg
           ; GenLocated
  (EpAnn AnnListItem)
  (HsFieldBind
     (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        (EpAnn AnnListItem)
        (HsFieldBind
           (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg)))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (GenLocated
         (EpAnn AnnListItem)
         (HsFieldBind
            (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))))
-> GenLocated
     (EpAnn AnnListItem)
     (HsFieldBind
        (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        (EpAnn AnnListItem)
        (HsFieldBind
           (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg)))
forall a b. (a -> b) -> a -> b
$ EpAnn AnnListItem
-> HsFieldBind
     (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg)
-> GenLocated
     (EpAnn AnnListItem)
     (HsFieldBind
        (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))
forall l e. l -> e -> GenLocated l e
L EpAnn AnnListItem
l (HsFieldBind
   (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg)
 -> GenLocated
      (EpAnn AnnListItem)
      (HsFieldBind
         (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg)))
-> HsFieldBind
     (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg)
-> GenLocated
     (EpAnn AnnListItem)
     (HsFieldBind
        (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))
forall a b. (a -> b) -> a -> b
$
               HsFieldBind
                 { hfbAnn :: XHsFieldBind (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
hfbAnn = [AddEpAnn]
XHsFieldBind (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
forall a. NoAnn a => a
noAnn
                 , hfbLHS :: GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)
hfbLHS = EpAnn AnnListItem
-> FieldOcc GhcRn
-> GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)
forall l e. l -> e -> GenLocated l e
L EpAnn AnnListItem
loc (XCFieldOcc GhcRn -> XRec GhcRn RdrName -> FieldOcc GhcRn
forall pass. XCFieldOcc pass -> XRec pass RdrName -> FieldOcc pass
FieldOcc XCFieldOcc GhcRn
Name
sel (SrcSpanAnnN -> RdrName -> GenLocated SrcSpanAnnN RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
ll RdrName
arg_rdr))
                 , hfbRHS :: LocatedA arg
hfbRHS = LocatedA arg
arg'
                 , hfbPun :: Bool
hfbPun = Bool
pun } }

    rn_dotdot :: Maybe (LocatedE RecFieldsDotDot)     -- See Note [DotDot fields] in GHC.Hs.Pat
              -> Maybe Name -- The constructor (Nothing for an
                                --    out of scope constructor)
              -> [LHsRecField GhcRn (LocatedA arg)] -- Explicit fields
              -> RnM ([LHsRecField GhcRn (LocatedA arg)])   -- Field Labels we need to fill in
    rn_dotdot :: Maybe (GenLocated EpaLocation RecFieldsDotDot)
-> Maybe Name
-> [LHsRecField GhcRn (LocatedA arg)]
-> RnM [LHsRecField GhcRn (LocatedA arg)]
rn_dotdot (Just (L EpaLocation
loc_e (RecFieldsDotDot Int
n))) (Just Name
con) [LHsRecField GhcRn (LocatedA arg)]
flds -- ".." on record construction / pat match
      | Bool -> Bool
not (Name -> Bool
isUnboundName Name
con) -- This test is because if the constructor
                                -- isn't in scope the constructor lookup will add
                                -- an error but still return an unbound name. We
                                -- don't want that to screw up the dot-dot fill-in stuff.
      = Bool
-> RnM [LHsRecField GhcRn (LocatedA arg)]
-> RnM [LHsRecField GhcRn (LocatedA arg)]
forall a. HasCallStack => Bool -> a -> a
assert ([LHsRecField GhcRn (LocatedA arg)]
[GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
flds [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
-> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthIs` Int
n) (RnM [LHsRecField GhcRn (LocatedA arg)]
 -> RnM [LHsRecField GhcRn (LocatedA arg)])
-> RnM [LHsRecField GhcRn (LocatedA arg)]
-> RnM [LHsRecField GhcRn (LocatedA arg)]
forall a b. (a -> b) -> a -> b
$
        do { Bool
dd_flag <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.RecordWildCards
           ; Bool -> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkErr Bool
dd_flag (HsRecFieldContext -> TcRnMessage
needFlagDotDot HsRecFieldContext
ctxt)
           ; (GlobalRdrEnv
rdr_env, LocalRdrEnv
lcl_env) <- TcRn (GlobalRdrEnv, LocalRdrEnv)
getRdrEnvs
           ; ConInfo
conInfo <- (() :: Constraint) => Name -> RnM ConInfo
Name -> RnM ConInfo
lookupConstructorInfo Name
con
           ; Bool
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ConInfo
conInfo ConInfo -> ConInfo -> Bool
forall a. Eq a => a -> a -> Bool
== ConInfo
ConHasPositionalArgs) (TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr (Name -> TcRnMessage
TcRnIllegalWildcardsInConstructor Name
con))
           ; let present_flds :: OccSet
present_flds = [OccName] -> OccSet
mkOccSet ([OccName] -> OccSet) -> [OccName] -> OccSet
forall a b. (a -> b) -> a -> b
$ (RdrName -> OccName) -> [RdrName] -> [OccName]
forall a b. (a -> b) -> [a] -> [b]
map RdrName -> OccName
rdrNameOcc ([LHsRecField GhcRn (LocatedA arg)] -> [RdrName]
forall p arg. UnXRec p => [LHsRecField p arg] -> [RdrName]
getFieldLbls [LHsRecField GhcRn (LocatedA arg)]
flds)

                   -- For constructor uses (but not patterns)
                   -- the arg should be in scope locally;
                   -- i.e. not top level or imported
                   -- Eg.  data R = R { x,y :: Int }
                   --      f x = R { .. }   -- Should expand to R {x=x}, not R{x=x,y=y}
                 arg_in_scope :: OccName -> Bool
arg_in_scope OccName
lbl = OccName -> RdrName
mkRdrUnqual OccName
lbl RdrName -> LocalRdrEnv -> Bool
`elemLocalRdrEnv` LocalRdrEnv
lcl_env

                 ([FieldLabel]
dot_dot_fields, [FieldGlobalRdrElt]
dot_dot_gres) =
                   [(FieldLabel, FieldGlobalRdrElt)]
-> ([FieldLabel], [FieldGlobalRdrElt])
forall a b. [(a, b)] -> ([a], [b])
unzip [ (FieldLabel
fl, FieldGlobalRdrElt
gre)
                         | FieldLabel
fl <- ConInfo -> [FieldLabel]
conInfoFields ConInfo
conInfo
                         , let lbl :: OccName
lbl = (() :: Constraint) => OccName -> OccName
OccName -> OccName
recFieldToVarOcc (OccName -> OccName) -> OccName -> OccName
forall a b. (a -> b) -> a -> b
$ Name -> OccName
forall name. HasOccName name => name -> OccName
occName (Name -> OccName) -> Name -> OccName
forall a b. (a -> b) -> a -> b
$ FieldLabel -> Name
flSelector FieldLabel
fl
                         , Bool -> Bool
not (OccName
lbl OccName -> OccSet -> Bool
`elemOccSet` OccSet
present_flds)
                         , Just FieldGlobalRdrElt
gre <- [GlobalRdrEnv -> FieldLabel -> Maybe FieldGlobalRdrElt
lookupGRE_FieldLabel GlobalRdrEnv
rdr_env FieldLabel
fl]
                                       -- Check selector is in scope
                         , case HsRecFieldContext
ctxt of
                             HsRecFieldCon {} -> OccName -> Bool
arg_in_scope OccName
lbl
                             HsRecFieldContext
_other           -> Bool
True ]

           ; DeprecationWarnings
-> [FieldGlobalRdrElt] -> IOEnv (Env TcGblEnv TcLclEnv) ()
addUsedGREs DeprecationWarnings
NoDeprecationWarnings [FieldGlobalRdrElt]
dot_dot_gres
           ; let loc :: SrcSpan
loc = EpaLocation -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA EpaLocation
loc_e
           ; let locn :: EpAnn AnnListItem
locn = SrcSpan -> EpAnn AnnListItem
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
loc
           ; [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     [GenLocated
        (EpAnn AnnListItem)
        (HsFieldBind
           (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [ EpAnn AnnListItem
-> HsFieldBind
     (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg)
-> GenLocated
     (EpAnn AnnListItem)
     (HsFieldBind
        (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))
forall l e. l -> e -> GenLocated l e
L (SrcSpan -> EpAnn AnnListItem
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
loc) (HsFieldBind
                        { hfbAnn :: XHsFieldBind (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
hfbAnn = [AddEpAnn]
XHsFieldBind (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn))
forall a. NoAnn a => a
noAnn
                        , hfbLHS :: GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)
hfbLHS
                           = EpAnn AnnListItem
-> FieldOcc GhcRn
-> GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)
forall l e. l -> e -> GenLocated l e
L (SrcSpan -> EpAnn AnnListItem
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
loc) (XCFieldOcc GhcRn -> XRec GhcRn RdrName -> FieldOcc GhcRn
forall pass. XCFieldOcc pass -> XRec pass RdrName -> FieldOcc pass
FieldOcc XCFieldOcc GhcRn
Name
sel (SrcSpanAnnN -> RdrName -> GenLocated SrcSpanAnnN RdrName
forall l e. l -> e -> GenLocated l e
L (SrcSpan -> SrcSpanAnnN
forall e. HasAnnotation e => SrcSpan -> e
noAnnSrcSpan SrcSpan
loc) RdrName
arg_rdr))
                        , hfbRHS :: LocatedA arg
hfbRHS = EpAnn AnnListItem -> arg -> LocatedA arg
forall l e. l -> e -> GenLocated l e
L EpAnn AnnListItem
locn (SrcSpan -> RdrName -> arg
mk_arg SrcSpan
loc RdrName
arg_rdr)
                        , hfbPun :: Bool
hfbPun = Bool
False })
                    | FieldLabel
fl <- [FieldLabel]
dot_dot_fields
                    , let sel :: Name
sel     = FieldLabel -> Name
flSelector FieldLabel
fl
                          arg_rdr :: RdrName
arg_rdr = OccName -> RdrName
mkRdrUnqual
                                  (OccName -> RdrName) -> OccName -> RdrName
forall a b. (a -> b) -> a -> b
$ (() :: Constraint) => OccName -> OccName
OccName -> OccName
recFieldToVarOcc
                                  (OccName -> OccName) -> OccName -> OccName
forall a b. (a -> b) -> a -> b
$ Name -> OccName
nameOccName Name
sel ] }

    rn_dotdot Maybe (GenLocated EpaLocation RecFieldsDotDot)
_dotdot Maybe Name
_mb_con [LHsRecField GhcRn (LocatedA arg)]
_flds
      = [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     [GenLocated
        (EpAnn AnnListItem)
        (HsFieldBind
           (GenLocated (EpAnn AnnListItem) (FieldOcc GhcRn)) (LocatedA arg))]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return []
      -- _dotdot = Nothing => No ".." at all
      -- _mb_con = Nothing => Record update
      -- _mb_con = Just unbound => Out of scope data constructor

    dup_flds :: [NE.NonEmpty RdrName]
        -- Each list represents a RdrName that occurred more than once
        -- (the list contains all occurrences)
        -- Each list in dup_fields is non-empty
    ([RdrName]
_, [NonEmpty RdrName]
dup_flds) = (RdrName -> RdrName -> Ordering)
-> [RdrName] -> ([RdrName], [NonEmpty RdrName])
forall a. (a -> a -> Ordering) -> [a] -> ([a], [NonEmpty a])
removeDups (FastString -> FastString -> Ordering
uniqCompareFS (FastString -> FastString -> Ordering)
-> (RdrName -> FastString) -> RdrName -> RdrName -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (OccName -> FastString
occNameFS (OccName -> FastString)
-> (RdrName -> OccName) -> RdrName -> FastString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RdrName -> OccName
rdrNameOcc)) ([LHsRecField GhcPs (LocatedA arg)] -> [RdrName]
forall p arg. UnXRec p => [LHsRecField p arg] -> [RdrName]
getFieldLbls [LHsRecField GhcPs (LocatedA arg)]
flds)
      -- See the same duplicate handling logic in rnHsRecUpdFields below for further context.

-- | Rename a regular (non-overloaded) record field update,
-- disambiguating the fields if necessary.
rnHsRecUpdFields
    :: [LHsRecUpdField GhcPs GhcPs]
    -> RnM (XLHsRecUpdLabels GhcRn, [LHsRecUpdField GhcRn GhcRn], FreeVars)
rnHsRecUpdFields :: [LHsRecUpdField GhcPs GhcPs]
-> RnM
     (XLHsRecUpdLabels GhcRn, [LHsRecUpdField GhcRn GhcRn], FreeVars)
rnHsRecUpdFields [LHsRecUpdField GhcPs GhcPs]
flds
  = do { Bool
pun_ok <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.NamedFieldPuns

       -- Check for an empty record update:  e {}
       -- NB: don't complain about e { .. }, because rn_dotdot has done that already
       ; case [LHsRecUpdField GhcPs GhcPs]
flds of
          { [] -> TcRnMessage
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (NonEmpty (HsRecUpdParent GhcRn),
      [GenLocated
         (EpAnn AnnListItem)
         (HsFieldBind
            (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
            (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))],
      FreeVars)
forall a. TcRnMessage -> TcM a
failWithTc TcRnMessage
TcRnEmptyRecordUpdate
          ; LHsRecUpdField GhcPs GhcPs
fld:[LHsRecUpdField GhcPs GhcPs]
other_flds ->
    do { let dup_lbls :: [NE.NonEmpty RdrName]
             ([RdrName]
_, [NonEmpty RdrName]
dup_lbls) = (RdrName -> RdrName -> Ordering)
-> [RdrName] -> ([RdrName], [NonEmpty RdrName])
forall a. (a -> a -> Ordering) -> [a] -> ([a], [NonEmpty a])
removeDups (FastString -> FastString -> Ordering
uniqCompareFS (FastString -> FastString -> Ordering)
-> (RdrName -> FastString) -> RdrName -> RdrName -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (OccName -> FastString
occNameFS (OccName -> FastString)
-> (RdrName -> OccName) -> RdrName -> FastString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RdrName -> OccName
rdrNameOcc))
                              ((GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcPs))
      (LHsExpr GhcPs))
 -> RdrName)
-> [GenLocated
      (EpAnn AnnListItem)
      (HsFieldBind
         (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcPs))
         (LHsExpr GhcPs))]
-> [RdrName]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (GenLocated SrcSpanAnnN RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc (GenLocated SrcSpanAnnN RdrName -> RdrName)
-> (GenLocated
      (EpAnn AnnListItem)
      (HsFieldBind
         (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcPs))
         (LHsExpr GhcPs))
    -> GenLocated SrcSpanAnnN RdrName)
-> GenLocated
     (EpAnn AnnListItem)
     (HsFieldBind
        (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcPs))
        (LHsExpr GhcPs))
-> RdrName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsRecUpdField GhcPs GhcPs -> GenLocated SrcSpanAnnN RdrName
GenLocated
  (EpAnn AnnListItem)
  (HsFieldBind
     (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcPs))
     (LHsExpr GhcPs))
-> GenLocated SrcSpanAnnN RdrName
forall (p :: Pass) q.
LHsRecUpdField (GhcPass p) q -> GenLocated SrcSpanAnnN RdrName
getFieldUpdLbl) [LHsRecUpdField GhcPs GhcPs]
[GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcPs))
      (LHsExpr GhcPs))]
flds)
               -- NB: we compare using the underlying field label FastString,
               -- in order to catch duplicates involving qualified names,
               -- as in the record update `r { fld = x, Mod.fld = y }`.
               -- See #21959.
               -- Note that this test doesn't correctly handle exact Names, but those
               -- aren't handled properly by the rest of the compiler anyway. See #22122.
       ; (NonEmpty RdrName -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> [NonEmpty RdrName] -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr (TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> (NonEmpty RdrName -> TcRnMessage)
-> NonEmpty RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecFieldContext -> NonEmpty RdrName -> TcRnMessage
dupFieldErr HsRecFieldContext
HsRecFieldUpd) [NonEmpty RdrName]
dup_lbls

         -- See Note [Disambiguating record updates]
       ; NonEmpty (HsRecUpdParent GhcRn)
possible_parents <- NonEmpty (LHsRecUpdField GhcPs GhcPs)
-> RnM (NonEmpty (HsRecUpdParent GhcRn))
lookupRecUpdFields (LHsRecUpdField GhcPs GhcPs
GenLocated
  (EpAnn AnnListItem)
  (HsFieldBind
     (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcPs))
     (GenLocated (EpAnn AnnListItem) (HsExpr GhcPs)))
fld GenLocated
  (EpAnn AnnListItem)
  (HsFieldBind
     (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcPs))
     (GenLocated (EpAnn AnnListItem) (HsExpr GhcPs)))
-> [GenLocated
      (EpAnn AnnListItem)
      (HsFieldBind
         (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcPs))
         (GenLocated (EpAnn AnnListItem) (HsExpr GhcPs)))]
-> NonEmpty
     (GenLocated
        (EpAnn AnnListItem)
        (HsFieldBind
           (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcPs))
           (GenLocated (EpAnn AnnListItem) (HsExpr GhcPs))))
forall a. a -> [a] -> NonEmpty a
NE.:| [LHsRecUpdField GhcPs GhcPs]
[GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcPs))
      (GenLocated (EpAnn AnnListItem) (HsExpr GhcPs)))]
other_flds)
       ; let  mb_unambig_lbls :: Maybe [FieldLabel]
              fvs :: FreeVars
              (Maybe [FieldLabel]
mb_unambig_lbls, FreeVars
fvs) =
               case NonEmpty (HsRecUpdParent GhcRn)
possible_parents of
                  RnRecUpdParent { rnRecUpdLabels :: HsRecUpdParent GhcRn -> NonEmpty FieldGlobalRdrElt
rnRecUpdLabels = NonEmpty FieldGlobalRdrElt
gres } NE.:| []
                    | let lbls :: [FieldLabel]
lbls = (FieldGlobalRdrElt -> FieldLabel)
-> [FieldGlobalRdrElt] -> [FieldLabel]
forall a b. (a -> b) -> [a] -> [b]
map (() :: Constraint) => FieldGlobalRdrElt -> FieldLabel
FieldGlobalRdrElt -> FieldLabel
fieldGRELabel ([FieldGlobalRdrElt] -> [FieldLabel])
-> [FieldGlobalRdrElt] -> [FieldLabel]
forall a b. (a -> b) -> a -> b
$ NonEmpty FieldGlobalRdrElt -> [FieldGlobalRdrElt]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty FieldGlobalRdrElt
gres
                    -> ( [FieldLabel] -> Maybe [FieldLabel]
forall a. a -> Maybe a
Just [FieldLabel]
lbls, [Name] -> FreeVars
mkFVs ([Name] -> FreeVars) -> [Name] -> FreeVars
forall a b. (a -> b) -> a -> b
$ (FieldLabel -> Name) -> [FieldLabel] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map FieldLabel -> Name
flSelector [FieldLabel]
lbls)
                  NonEmpty (HsRecUpdParent GhcRn)
_ -> ( Maybe [FieldLabel]
forall a. Maybe a
Nothing
                       , [FreeVars] -> FreeVars
plusFVs ([FreeVars] -> FreeVars) -> [FreeVars] -> FreeVars
forall a b. (a -> b) -> a -> b
$ (HsRecUpdParent GhcRn -> FreeVars)
-> [HsRecUpdParent GhcRn] -> [FreeVars]
forall a b. (a -> b) -> [a] -> [b]
map ([FreeVars] -> FreeVars
plusFVs ([FreeVars] -> FreeVars)
-> (HsRecUpdParent GhcRn -> [FreeVars])
-> HsRecUpdParent GhcRn
-> FreeVars
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FieldGlobalRdrElt -> FreeVars)
-> [FieldGlobalRdrElt] -> [FreeVars]
forall a b. (a -> b) -> [a] -> [b]
map FieldGlobalRdrElt -> FreeVars
pat_syn_free_vars ([FieldGlobalRdrElt] -> [FreeVars])
-> (HsRecUpdParent GhcRn -> [FieldGlobalRdrElt])
-> HsRecUpdParent GhcRn
-> [FreeVars]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty FieldGlobalRdrElt -> [FieldGlobalRdrElt]
forall a. NonEmpty a -> [a]
NE.toList (NonEmpty FieldGlobalRdrElt -> [FieldGlobalRdrElt])
-> (HsRecUpdParent GhcRn -> NonEmpty FieldGlobalRdrElt)
-> HsRecUpdParent GhcRn
-> [FieldGlobalRdrElt]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecUpdParent GhcRn -> NonEmpty FieldGlobalRdrElt
rnRecUpdLabels)
                                 ([HsRecUpdParent GhcRn] -> [FreeVars])
-> [HsRecUpdParent GhcRn] -> [FreeVars]
forall a b. (a -> b) -> a -> b
$ NonEmpty (HsRecUpdParent GhcRn) -> [HsRecUpdParent GhcRn]
forall a. NonEmpty a -> [a]
NE.toList NonEmpty (HsRecUpdParent GhcRn)
possible_parents
                         -- See Note [Using PatSyn FreeVars]
                       )

        -- Rename each field.
        ; ([GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
      (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))]
upd_flds, FreeVars
fvs') <- Bool
-> Maybe [FieldLabel]
-> [LHsRecUpdField GhcPs GhcPs]
-> RnM ([LHsRecUpdField GhcRn GhcRn], FreeVars)
rn_flds Bool
pun_ok Maybe [FieldLabel]
mb_unambig_lbls [LHsRecUpdField GhcPs GhcPs]
flds
        ; let all_fvs :: FreeVars
all_fvs = FreeVars
fvs FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs'
        ; (NonEmpty (HsRecUpdParent GhcRn),
 [GenLocated
    (EpAnn AnnListItem)
    (HsFieldBind
       (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
       (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))],
 FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (NonEmpty (HsRecUpdParent GhcRn),
      [GenLocated
         (EpAnn AnnListItem)
         (HsFieldBind
            (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
            (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))],
      FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (NonEmpty (HsRecUpdParent GhcRn)
possible_parents, [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
      (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))]
upd_flds, FreeVars
all_fvs) } } }

    where

      -- For an ambiguous record update involving pattern synonym record fields,
      -- we must add all the possibly-relevant field selector names to ensure that
      -- we typecheck the record update **after** we typecheck the pattern synonym
      -- definition. See Note [Using PatSyn FreeVars].
      pat_syn_free_vars :: FieldGlobalRdrElt -> FreeVars
      pat_syn_free_vars :: FieldGlobalRdrElt -> FreeVars
pat_syn_free_vars (GRE { gre_info :: forall info. GlobalRdrEltX info -> info
gre_info = GREInfo
info })
        | IAmRecField RecFieldInfo
fld_info <- GREInfo
info
        , RecFieldInfo { recFieldLabel :: RecFieldInfo -> FieldLabel
recFieldLabel = FieldLabel
fl, recFieldCons :: RecFieldInfo -> UniqSet ConLikeName
recFieldCons = UniqSet ConLikeName
cons } <- RecFieldInfo
fld_info
        , (ConLikeName -> Bool) -> UniqSet ConLikeName -> Bool
forall a. (a -> Bool) -> UniqSet a -> Bool
uniqSetAny ConLikeName -> Bool
is_PS UniqSet ConLikeName
cons
        = Name -> FreeVars
unitFV (FieldLabel -> Name
flSelector FieldLabel
fl)
      pat_syn_free_vars FieldGlobalRdrElt
_
        = FreeVars
emptyFVs

      is_PS :: ConLikeName -> Bool
      is_PS :: ConLikeName -> Bool
is_PS (PatSynName  {}) = Bool
True
      is_PS (DataConName {}) = Bool
False

      rn_flds :: Bool -> Maybe [FieldLabel]
              -> [LHsRecUpdField GhcPs GhcPs]
              -> RnM ([LHsRecUpdField GhcRn GhcRn], FreeVars)
      rn_flds :: Bool
-> Maybe [FieldLabel]
-> [LHsRecUpdField GhcPs GhcPs]
-> RnM ([LHsRecUpdField GhcRn GhcRn], FreeVars)
rn_flds Bool
_ Maybe [FieldLabel]
_ [] = ([GenLocated
    (EpAnn AnnListItem)
    (HsFieldBind
       (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
       (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))],
 FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     ([GenLocated
         (EpAnn AnnListItem)
         (HsFieldBind
            (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
            (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))],
      FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([], FreeVars
emptyFVs)
      rn_flds Bool
pun_ok Maybe [FieldLabel]
mb_unambig_lbls
              ((L EpAnn AnnListItem
l (HsFieldBind { hfbLHS :: forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS = L EpAnn AnnListItem
loc AmbiguousFieldOcc GhcPs
f
                                 , hfbRHS :: forall lhs rhs. HsFieldBind lhs rhs -> rhs
hfbRHS = GenLocated (EpAnn AnnListItem) (HsExpr GhcPs)
arg
                                 , hfbPun :: forall lhs rhs. HsFieldBind lhs rhs -> Bool
hfbPun = Bool
pun })):[LHsRecUpdField GhcPs GhcPs]
flds)
        = do { let lbl :: RdrName
lbl = AmbiguousFieldOcc GhcPs -> RdrName
forall (p :: Pass). AmbiguousFieldOcc (GhcPass p) -> RdrName
ambiguousFieldOccRdrName AmbiguousFieldOcc GhcPs
f
             ; (LHsExpr GhcPs
arg' :: LHsExpr GhcPs) <- if Bool
pun
                       then do { EpAnn AnnListItem
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall ann a. EpAnn ann -> TcRn a -> TcRn a
setSrcSpanA EpAnn AnnListItem
loc (IOEnv (Env TcGblEnv TcLclEnv) ()
 -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$
                                 Bool -> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkErr Bool
pun_ok (Located RdrName -> TcRnMessage
TcRnIllegalFieldPunning (SrcSpan -> RdrName -> Located RdrName
forall l e. l -> e -> GenLocated l e
L (EpAnn AnnListItem -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA EpAnn AnnListItem
loc) RdrName
lbl))
                                 -- Discard any module qualifier (#11662)
                               ; let arg_rdr :: RdrName
arg_rdr = OccName -> RdrName
mkRdrUnqual (RdrName -> OccName
rdrNameOcc RdrName
lbl)
                               ; GenLocated (EpAnn AnnListItem) (HsExpr GhcPs)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated (EpAnn AnnListItem) (HsExpr GhcPs))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (EpAnn AnnListItem
-> HsExpr GhcPs -> GenLocated (EpAnn AnnListItem) (HsExpr GhcPs)
forall l e. l -> e -> GenLocated l e
L (EpAnn AnnListItem -> EpAnn AnnListItem
forall a b. (HasLoc a, HasAnnotation b) => a -> b
l2l EpAnn AnnListItem
loc) (XVar GhcPs -> XRec GhcPs (IdP GhcPs) -> HsExpr GhcPs
forall p. XVar p -> LIdP p -> HsExpr p
HsVar XVar GhcPs
NoExtField
noExtField (SrcSpanAnnN -> RdrName -> GenLocated SrcSpanAnnN RdrName
forall l e. l -> e -> GenLocated l e
L (EpAnn AnnListItem -> SrcSpanAnnN
forall a b. (HasLoc a, HasAnnotation b) => a -> b
l2l EpAnn AnnListItem
loc) RdrName
arg_rdr))) }
                       else GenLocated (EpAnn AnnListItem) (HsExpr GhcPs)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated (EpAnn AnnListItem) (HsExpr GhcPs))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return GenLocated (EpAnn AnnListItem) (HsExpr GhcPs)
arg
             ; (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)
arg'', FreeVars
fvs) <- LHsExpr GhcPs -> RnM (LHsExpr GhcRn, FreeVars)
rnLExpr LHsExpr GhcPs
arg'
             ; let lbl' :: AmbiguousFieldOcc GhcRn
                   lbl' :: AmbiguousFieldOcc GhcRn
lbl' = case Maybe [FieldLabel]
mb_unambig_lbls of
                            { Just (FieldLabel
fl:[FieldLabel]
_) ->
                                let sel_name :: Name
sel_name = FieldLabel -> Name
flSelector FieldLabel
fl
                                in XUnambiguous GhcRn -> XRec GhcRn RdrName -> AmbiguousFieldOcc GhcRn
forall pass.
XUnambiguous pass -> XRec pass RdrName -> AmbiguousFieldOcc pass
Unambiguous XUnambiguous GhcRn
Name
sel_name   (SrcSpanAnnN -> RdrName -> GenLocated SrcSpanAnnN RdrName
forall l e. l -> e -> GenLocated l e
L (EpAnn AnnListItem -> SrcSpanAnnN
forall a b. (HasLoc a, HasAnnotation b) => a -> b
l2l EpAnn AnnListItem
loc) RdrName
lbl)
                            ; Maybe [FieldLabel]
_ ->   XAmbiguous GhcRn -> XRec GhcRn RdrName -> AmbiguousFieldOcc GhcRn
forall pass.
XAmbiguous pass -> XRec pass RdrName -> AmbiguousFieldOcc pass
Ambiguous XAmbiguous GhcRn
NoExtField
noExtField (SrcSpanAnnN -> RdrName -> GenLocated SrcSpanAnnN RdrName
forall l e. l -> e -> GenLocated l e
L (EpAnn AnnListItem -> SrcSpanAnnN
forall a b. (HasLoc a, HasAnnotation b) => a -> b
l2l EpAnn AnnListItem
loc) RdrName
lbl) }
                   fld' :: LHsRecUpdField GhcRn GhcRn
                   fld' :: LHsRecUpdField GhcRn GhcRn
fld' = EpAnn AnnListItem
-> HsFieldBind
     (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
     (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn))
-> GenLocated
     (EpAnn AnnListItem)
     (HsFieldBind
        (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
        (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))
forall l e. l -> e -> GenLocated l e
L EpAnn AnnListItem
l (HsFieldBind { hfbAnn :: XHsFieldBind
  (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
hfbAnn = [AddEpAnn]
XHsFieldBind
  (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
forall a. NoAnn a => a
noAnn
                                           , hfbLHS :: GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn)
hfbLHS = EpAnn AnnListItem
-> AmbiguousFieldOcc GhcRn
-> GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn)
forall l e. l -> e -> GenLocated l e
L EpAnn AnnListItem
loc AmbiguousFieldOcc GhcRn
lbl'
                                           , hfbRHS :: GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)
hfbRHS = GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)
arg''
                                           , hfbPun :: Bool
hfbPun = Bool
pun })
             ; ([GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
      (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))]
flds', FreeVars
fvs') <- Bool
-> Maybe [FieldLabel]
-> [LHsRecUpdField GhcPs GhcPs]
-> RnM ([LHsRecUpdField GhcRn GhcRn], FreeVars)
rn_flds Bool
pun_ok ([FieldLabel] -> [FieldLabel]
forall a. HasCallStack => [a] -> [a]
tail ([FieldLabel] -> [FieldLabel])
-> Maybe [FieldLabel] -> Maybe [FieldLabel]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe [FieldLabel]
mb_unambig_lbls) [LHsRecUpdField GhcPs GhcPs]
flds
             ; ([GenLocated
    (EpAnn AnnListItem)
    (HsFieldBind
       (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
       (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))],
 FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     ([GenLocated
         (EpAnn AnnListItem)
         (HsFieldBind
            (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
            (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))],
      FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsRecUpdField GhcRn GhcRn
GenLocated
  (EpAnn AnnListItem)
  (HsFieldBind
     (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
     (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))
fld' GenLocated
  (EpAnn AnnListItem)
  (HsFieldBind
     (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
     (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))
-> [GenLocated
      (EpAnn AnnListItem)
      (HsFieldBind
         (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
         (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))]
-> [GenLocated
      (EpAnn AnnListItem)
      (HsFieldBind
         (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
         (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))]
forall a. a -> [a] -> [a]
: [GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind
      (GenLocated (EpAnn AnnListItem) (AmbiguousFieldOcc GhcRn))
      (GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)))]
flds', FreeVars
fvs FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs') }

getFieldIds :: [LHsRecField GhcRn arg] -> [Name]
getFieldIds :: forall arg. [LHsRecField GhcRn arg] -> [Name]
getFieldIds [LHsRecField GhcRn arg]
flds = (GenLocated
   (EpAnn AnnListItem) (HsFieldBind (XRec GhcRn (FieldOcc GhcRn)) arg)
 -> Name)
-> [GenLocated
      (EpAnn AnnListItem)
      (HsFieldBind (XRec GhcRn (FieldOcc GhcRn)) arg)]
-> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (HsFieldBind (XRec GhcRn (FieldOcc GhcRn)) arg -> XCFieldOcc GhcRn
HsFieldBind (XRec GhcRn (FieldOcc GhcRn)) arg -> Name
forall p arg. UnXRec p => HsRecField p arg -> XCFieldOcc p
hsRecFieldSel (HsFieldBind (XRec GhcRn (FieldOcc GhcRn)) arg -> Name)
-> (GenLocated
      (EpAnn AnnListItem) (HsFieldBind (XRec GhcRn (FieldOcc GhcRn)) arg)
    -> HsFieldBind (XRec GhcRn (FieldOcc GhcRn)) arg)
-> GenLocated
     (EpAnn AnnListItem) (HsFieldBind (XRec GhcRn (FieldOcc GhcRn)) arg)
-> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated
  (EpAnn AnnListItem) (HsFieldBind (XRec GhcRn (FieldOcc GhcRn)) arg)
-> HsFieldBind (XRec GhcRn (FieldOcc GhcRn)) arg
forall l e. GenLocated l e -> e
unLoc) [LHsRecField GhcRn arg]
[GenLocated
   (EpAnn AnnListItem)
   (HsFieldBind (XRec GhcRn (FieldOcc GhcRn)) arg)]
flds

getFieldLbls :: forall p arg . UnXRec p => [LHsRecField p arg] -> [RdrName]
getFieldLbls :: forall p arg. UnXRec p => [LHsRecField p arg] -> [RdrName]
getFieldLbls [LHsRecField p arg]
flds
  = (LHsRecField p arg -> RdrName) -> [LHsRecField p arg] -> [RdrName]
forall a b. (a -> b) -> [a] -> [b]
map (forall p a. UnXRec p => XRec p a -> a
unXRec @p (XRec p RdrName -> RdrName)
-> (LHsRecField p arg -> XRec p RdrName)
-> LHsRecField p arg
-> RdrName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldOcc p -> XRec p RdrName
forall pass. FieldOcc pass -> XRec pass RdrName
foLabel (FieldOcc p -> XRec p RdrName)
-> (LHsRecField p arg -> FieldOcc p)
-> LHsRecField p arg
-> XRec p RdrName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall p a. UnXRec p => XRec p a -> a
unXRec @p (XRec p (FieldOcc p) -> FieldOcc p)
-> (LHsRecField p arg -> XRec p (FieldOcc p))
-> LHsRecField p arg
-> FieldOcc p
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsFieldBind (XRec p (FieldOcc p)) arg -> XRec p (FieldOcc p)
forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS (HsFieldBind (XRec p (FieldOcc p)) arg -> XRec p (FieldOcc p))
-> (LHsRecField p arg -> HsFieldBind (XRec p (FieldOcc p)) arg)
-> LHsRecField p arg
-> XRec p (FieldOcc p)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall p a. UnXRec p => XRec p a -> a
unXRec @p) [LHsRecField p arg]
flds

needFlagDotDot :: HsRecFieldContext -> TcRnMessage
needFlagDotDot :: HsRecFieldContext -> TcRnMessage
needFlagDotDot = RecordFieldPart -> TcRnMessage
TcRnIllegalWildcardsInRecord (RecordFieldPart -> TcRnMessage)
-> (HsRecFieldContext -> RecordFieldPart)
-> HsRecFieldContext
-> TcRnMessage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecFieldContext -> RecordFieldPart
toRecordFieldPart

dupFieldErr :: HsRecFieldContext -> NE.NonEmpty RdrName -> TcRnMessage
dupFieldErr :: HsRecFieldContext -> NonEmpty RdrName -> TcRnMessage
dupFieldErr HsRecFieldContext
ctxt = RecordFieldPart -> NonEmpty RdrName -> TcRnMessage
TcRnDuplicateFieldName (HsRecFieldContext -> RecordFieldPart
toRecordFieldPart HsRecFieldContext
ctxt)

toRecordFieldPart :: HsRecFieldContext -> RecordFieldPart
toRecordFieldPart :: HsRecFieldContext -> RecordFieldPart
toRecordFieldPart (HsRecFieldCon Name
n)  = Name -> RecordFieldPart
RecordFieldConstructor Name
n
toRecordFieldPart (HsRecFieldPat Name
n)  = Name -> RecordFieldPart
RecordFieldPattern     Name
n
toRecordFieldPart (HsRecFieldUpd {}) = RecordFieldPart
RecordFieldUpdate

{- Note [Disambiguating record updates]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When the -XDuplicateRecordFields extension is used, to rename and typecheck
a non-overloaded record update, we might need to disambiguate the field labels.

Consider the following definitions:

   {-# LANGUAGE DuplicateRecordFields #-}

    data R = MkR1 { fld1 :: Int, fld2 :: Char }
           | MKR2 { fld1 :: Int, fld2 :: Char, fld3 :: Bool }
    data S = MkS1 { fld1 :: Int } | MkS2 { fld2 :: Char }

In a record update, the `lookupRecUpdFields` function tries to determine
the parent datatype by computing the parents (TyCon/PatSyn) which have
at least one constructor (DataCon/PatSyn) with all of the fields.

For example, in the (non-overloaded) record update

    r { fld1 = 3, fld2 = 'x' }

only the TyCon R contains at least one DataCon which has both of the fields
being updated: in this case, MkR1 and MkR2 have both of the updated fields.
The TyCon S also has both fields fld1 and fld2, but no single constructor
has both of those fields, so S is not a valid parent for this record update.

Note that this check is namespace-aware, so that a record update such as

    import qualified M ( R (fld1, fld2) )
    f r = r { M.fld1 = 3 }

is unambiguous, as only R contains the field fld1 in the M namespace.
(See however #22122 for issues relating to the usage of exact Names in
record fields.)

See also Note [Type-directed record disambiguation] in GHC.Tc.Gen.Expr.

Note [Using PatSyn FreeVars]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When we are disambiguating a non-overloaded record update, as per
Note [Disambiguating record updates], and have determined that this
record update might involve pattern synonym record fields, it is important
to declare usage of all these pattern synonyms record fields in the returned
FreeVars of rnHsRecUpdFields. This ensures that the typechecker sees
that the typechecking of the record update depends on the typechecking
of the pattern synonym, and typechecks the pattern synonyms first.
Not doing so caused #21898.

Note that this can be removed once GHC proposal #366 is implemented,
as we will be able to fully disambiguate the record update in the renamer,
and can immediately declare the correct used FreeVars instead of having
to over-estimate in case of ambiguity.

************************************************************************
*                                                                      *
\subsubsection{Literals}
*                                                                      *
************************************************************************

When literals occur we have to make sure
that the types and classes they involve
are made available.
-}

rnLit :: HsLit p -> RnM ()
rnLit :: forall p. HsLit p -> IOEnv (Env TcGblEnv TcLclEnv) ()
rnLit (HsChar XHsChar p
_ Char
c) = Bool -> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkErr (Char -> Bool
inCharRange Char
c) (Char -> TcRnMessage
TcRnCharLiteralOutOfRange Char
c)
rnLit HsLit p
_ = () -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Turn a Fractional-looking literal which happens to be an integer into an
-- Integer-looking literal.
-- We only convert numbers where the exponent is between 0 and 100 to avoid
-- converting huge numbers and incurring long compilation times. See #15646.
generalizeOverLitVal :: OverLitVal -> OverLitVal
generalizeOverLitVal :: OverLitVal -> OverLitVal
generalizeOverLitVal (HsFractional fl :: FractionalLit
fl@(FL {fl_text :: FractionalLit -> SourceText
fl_text=SourceText
src,fl_neg :: FractionalLit -> Bool
fl_neg=Bool
neg,fl_exp :: FractionalLit -> Integer
fl_exp=Integer
e}))
    | Integer
e Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= -Integer
100 Bool -> Bool -> Bool
&& Integer
e Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
100
    , let val :: Rational
val = FractionalLit -> Rational
rationalFromFractionalLit FractionalLit
fl
    , Rational -> Integer
forall a. Ratio a -> a
denominator Rational
val Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1 = IntegralLit -> OverLitVal
HsIntegral (IL {il_text :: SourceText
il_text=SourceText
src,il_neg :: Bool
il_neg=Bool
neg,il_value :: Integer
il_value=Rational -> Integer
forall a. Ratio a -> a
numerator Rational
val})
generalizeOverLitVal OverLitVal
lit = OverLitVal
lit

isNegativeZeroOverLit :: (XXOverLit t ~ DataConCantHappen) => HsOverLit t -> Bool
isNegativeZeroOverLit :: forall t. (XXOverLit t ~ DataConCantHappen) => HsOverLit t -> Bool
isNegativeZeroOverLit HsOverLit t
lit
 = case HsOverLit t -> OverLitVal
forall p. HsOverLit p -> OverLitVal
ol_val HsOverLit t
lit of
        HsIntegral IntegralLit
i    -> Integer
0 Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== IntegralLit -> Integer
il_value IntegralLit
i Bool -> Bool -> Bool
&& IntegralLit -> Bool
il_neg IntegralLit
i
        -- For HsFractional, the value of fl is n * (b ^^ e) so it is sufficient
        -- to check if n = 0. b is equal to either 2 or 10. We don't call
        -- rationalFromFractionalLit here as it is expensive when e is big.
        HsFractional FractionalLit
fl -> Rational
0 Rational -> Rational -> Bool
forall a. Eq a => a -> a -> Bool
== FractionalLit -> Rational
fl_signi FractionalLit
fl Bool -> Bool -> Bool
&& FractionalLit -> Bool
fl_neg FractionalLit
fl
        OverLitVal
_               -> Bool
False

{-
Note [Negative zero]
~~~~~~~~~~~~~~~~~~~~~~~~~
There were problems with negative zero in conjunction with Negative Literals
extension. Numeric literal value is contained in Integer and Rational types
inside IntegralLit and FractionalLit. These types cannot represent negative
zero value. So we had to add explicit field 'neg' which would hold information
about literal sign. Here in rnOverLit we use it to detect negative zeroes and
in this case return not only literal itself but also negateName so that users
can apply it explicitly. In this case it stays negative zero.  #13211
-}

rnOverLit :: (XXOverLit t ~ DataConCantHappen) => HsOverLit t ->
             RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
rnOverLit :: forall t.
(XXOverLit t ~ DataConCantHappen) =>
HsOverLit t
-> RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
rnOverLit HsOverLit t
origLit
  = do  { Bool
opt_NumDecimals <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.NumDecimals
        ; let { lit :: HsOverLit t
lit@(OverLit {ol_val :: forall p. HsOverLit p -> OverLitVal
ol_val=OverLitVal
val})
            | Bool
opt_NumDecimals = HsOverLit t
origLit {ol_val = generalizeOverLitVal (ol_val origLit)}
            | Bool
otherwise       = HsOverLit t
origLit
          }
        ; let std_name :: Name
std_name = OverLitVal -> Name
hsOverLitName OverLitVal
val
        ; (Name
from_thing_name, FreeVars
fvs1) <- Name -> RnM (Name, FreeVars)
lookupSyntaxName Name
std_name
        ; SrcSpan
loc <- RnM SrcSpan
getSrcSpanM -- See Note [Source locations for implicit function calls] in GHC.Iface.Ext.Ast
        ; let rebindable :: Bool
rebindable = Name
from_thing_name Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
/= Name
std_name
              lit' :: HsOverLit GhcRn
lit' = HsOverLit t
lit { ol_ext = OverLitRn { ol_rebindable = rebindable
                                              , ol_from_fun = L (noAnnSrcSpan loc) from_thing_name } }
        ; if HsOverLit GhcRn -> Bool
forall t. (XXOverLit t ~ DataConCantHappen) => HsOverLit t -> Bool
isNegativeZeroOverLit HsOverLit GhcRn
lit'
          then do { (HsExpr GhcRn
negate_name, FreeVars
fvs2) <- Name -> RnM (HsExpr GhcRn, FreeVars)
lookupSyntaxExpr Name
negateName
                  ; ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
-> RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((HsOverLit GhcRn
lit' { ol_val = negateOverLitVal val }, HsExpr GhcRn -> Maybe (HsExpr GhcRn)
forall a. a -> Maybe a
Just HsExpr GhcRn
negate_name)
                                  , FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) }
          else ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
-> RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((HsOverLit GhcRn
lit', Maybe (HsExpr GhcRn)
forall a. Maybe a
Nothing), FreeVars
fvs1) }


rnHsTyPat :: HsDocContext
          -> HsTyPat GhcPs
          -> CpsRn (HsTyPat GhcRn)
rnHsTyPat :: HsDocContext -> HsTyPat GhcPs -> CpsRn (HsTyPat GhcRn)
rnHsTyPat HsDocContext
ctxt HsTyPat GhcPs
sigType = case HsTyPat GhcPs
sigType of
  (HsTP { hstp_body :: forall pass. HsTyPat pass -> LHsType pass
hstp_body = LHsType GhcPs
hs_ty }) -> do
    (GenLocated (EpAnn AnnListItem) (HsType GhcRn)
hs_ty', HsTyPatRnBuilder
tpb) <- TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn))
-> HsDocContext
-> CpsRn
     (GenLocated (EpAnn AnnListItem) (HsType GhcRn), HsTyPatRnBuilder)
forall a. TPRnM a -> HsDocContext -> CpsRn (a, HsTyPatRnBuilder)
runTPRnM (LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
hs_ty) HsDocContext
ctxt
    HsTyPat GhcRn -> CpsRn (HsTyPat GhcRn)
forall a. a -> CpsRn a
forall (f :: * -> *) a. Applicative f => a -> f a
pure HsTP
          { hstp_body :: LHsType GhcRn
hstp_body = LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
hs_ty'
          , hstp_ext :: XHsTP GhcRn
hstp_ext = HsTyPatRnBuilder -> HsTyPatRn
buildHsTyPatRn HsTyPatRnBuilder
tpb
          }

-- | Type pattern renaming monad
-- For the OccSet in the ReaderT, see Note [Locally bound names in type patterns]
-- For the HsTyPatRnBuilderRn in the WriterT, see Note [Implicit and explicit type variable binders]
-- For the CpsRn base monad, see Note [CpsRn monad]
-- For why we need CpsRn in TPRnM see Note [Left-to-right scoping of type patterns]
newtype TPRnM a =
  MkTPRnM (ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a)
  deriving newtype ((forall a b. (a -> b) -> TPRnM a -> TPRnM b)
-> (forall a b. a -> TPRnM b -> TPRnM a) -> Functor TPRnM
forall a b. a -> TPRnM b -> TPRnM a
forall a b. (a -> b) -> TPRnM a -> TPRnM b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> TPRnM a -> TPRnM b
fmap :: forall a b. (a -> b) -> TPRnM a -> TPRnM b
$c<$ :: forall a b. a -> TPRnM b -> TPRnM a
<$ :: forall a b. a -> TPRnM b -> TPRnM a
Functor, Functor TPRnM
Functor TPRnM =>
(forall a. a -> TPRnM a)
-> (forall a b. TPRnM (a -> b) -> TPRnM a -> TPRnM b)
-> (forall a b c. (a -> b -> c) -> TPRnM a -> TPRnM b -> TPRnM c)
-> (forall a b. TPRnM a -> TPRnM b -> TPRnM b)
-> (forall a b. TPRnM a -> TPRnM b -> TPRnM a)
-> Applicative TPRnM
forall a. a -> TPRnM a
forall a b. TPRnM a -> TPRnM b -> TPRnM a
forall a b. TPRnM a -> TPRnM b -> TPRnM b
forall a b. TPRnM (a -> b) -> TPRnM a -> TPRnM b
forall a b c. (a -> b -> c) -> TPRnM a -> TPRnM b -> TPRnM c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall a. a -> TPRnM a
pure :: forall a. a -> TPRnM a
$c<*> :: forall a b. TPRnM (a -> b) -> TPRnM a -> TPRnM b
<*> :: forall a b. TPRnM (a -> b) -> TPRnM a -> TPRnM b
$cliftA2 :: forall a b c. (a -> b -> c) -> TPRnM a -> TPRnM b -> TPRnM c
liftA2 :: forall a b c. (a -> b -> c) -> TPRnM a -> TPRnM b -> TPRnM c
$c*> :: forall a b. TPRnM a -> TPRnM b -> TPRnM b
*> :: forall a b. TPRnM a -> TPRnM b -> TPRnM b
$c<* :: forall a b. TPRnM a -> TPRnM b -> TPRnM a
<* :: forall a b. TPRnM a -> TPRnM b -> TPRnM a
Applicative, Applicative TPRnM
Applicative TPRnM =>
(forall a b. TPRnM a -> (a -> TPRnM b) -> TPRnM b)
-> (forall a b. TPRnM a -> TPRnM b -> TPRnM b)
-> (forall a. a -> TPRnM a)
-> Monad TPRnM
forall a. a -> TPRnM a
forall a b. TPRnM a -> TPRnM b -> TPRnM b
forall a b. TPRnM a -> (a -> TPRnM b) -> TPRnM b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall a b. TPRnM a -> (a -> TPRnM b) -> TPRnM b
>>= :: forall a b. TPRnM a -> (a -> TPRnM b) -> TPRnM b
$c>> :: forall a b. TPRnM a -> TPRnM b -> TPRnM b
>> :: forall a b. TPRnM a -> TPRnM b -> TPRnM b
$creturn :: forall a. a -> TPRnM a
return :: forall a. a -> TPRnM a
Monad)

runTPRnM :: TPRnM a -> HsDocContext -> CpsRn (a, HsTyPatRnBuilder)
runTPRnM :: forall a. TPRnM a -> HsDocContext -> CpsRn (a, HsTyPatRnBuilder)
runTPRnM (MkTPRnM ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
thing_inside) HsDocContext
doc_ctxt = WriterT HsTyPatRnBuilder CpsRn a -> CpsRn (a, HsTyPatRnBuilder)
forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
runWriterT (WriterT HsTyPatRnBuilder CpsRn a -> CpsRn (a, HsTyPatRnBuilder))
-> WriterT HsTyPatRnBuilder CpsRn a -> CpsRn (a, HsTyPatRnBuilder)
forall a b. (a -> b) -> a -> b
$ ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
-> (HsDocContext, OccSet) -> WriterT HsTyPatRnBuilder CpsRn a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
thing_inside (HsDocContext
doc_ctxt, OccSet
emptyOccSet)

askLocals :: TPRnM OccSet
askLocals :: TPRnM OccSet
askLocals = ReaderT
  (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) OccSet
-> TPRnM OccSet
forall a.
ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
-> TPRnM a
MkTPRnM (((HsDocContext, OccSet) -> OccSet)
-> ReaderT
     (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) OccSet
forall (m :: * -> *) r a. Monad m => (r -> a) -> ReaderT r m a
asks (HsDocContext, OccSet) -> OccSet
forall a b. (a, b) -> b
snd)

askDocContext :: TPRnM HsDocContext
askDocContext :: TPRnM HsDocContext
askDocContext = ReaderT
  (HsDocContext, OccSet)
  (WriterT HsTyPatRnBuilder CpsRn)
  HsDocContext
-> TPRnM HsDocContext
forall a.
ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
-> TPRnM a
MkTPRnM (((HsDocContext, OccSet) -> HsDocContext)
-> ReaderT
     (HsDocContext, OccSet)
     (WriterT HsTyPatRnBuilder CpsRn)
     HsDocContext
forall (m :: * -> *) r a. Monad m => (r -> a) -> ReaderT r m a
asks (HsDocContext, OccSet) -> HsDocContext
forall a b. (a, b) -> a
fst)

tellTPB :: HsTyPatRnBuilder -> TPRnM ()
tellTPB :: HsTyPatRnBuilder -> TPRnM ()
tellTPB = ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) ()
-> TPRnM ()
forall a.
ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
-> TPRnM a
MkTPRnM (ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) ()
 -> TPRnM ())
-> (HsTyPatRnBuilder
    -> ReaderT
         (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) ())
-> HsTyPatRnBuilder
-> TPRnM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterT HsTyPatRnBuilder CpsRn ()
-> ReaderT
     (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) ()
forall (m :: * -> *) a.
Monad m =>
m a -> ReaderT (HsDocContext, OccSet) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (WriterT HsTyPatRnBuilder CpsRn ()
 -> ReaderT
      (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) ())
-> (HsTyPatRnBuilder -> WriterT HsTyPatRnBuilder CpsRn ())
-> HsTyPatRnBuilder
-> ReaderT
     (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsTyPatRnBuilder -> WriterT HsTyPatRnBuilder CpsRn ()
forall w (m :: * -> *). (Monoid w, Monad m) => w -> WriterT w m ()
tell

liftRnFV :: RnM (a, FreeVars) -> TPRnM a
liftRnFV :: forall a. RnM (a, FreeVars) -> TPRnM a
liftRnFV = CpsRn a -> TPRnM a
forall a. CpsRn a -> TPRnM a
liftTPRnCps (CpsRn a -> TPRnM a)
-> (RnM (a, FreeVars) -> CpsRn a) -> RnM (a, FreeVars) -> TPRnM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RnM (a, FreeVars) -> CpsRn a
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV

liftRn :: RnM a -> TPRnM a
liftRn :: forall a. RnM a -> TPRnM a
liftRn = CpsRn a -> TPRnM a
forall a. CpsRn a -> TPRnM a
liftTPRnCps (CpsRn a -> TPRnM a) -> (RnM a -> CpsRn a) -> RnM a -> TPRnM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RnM a -> CpsRn a
forall a. RnM a -> CpsRn a
liftCps

liftRnWithCont :: (forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)) -> TPRnM b
liftRnWithCont :: forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> TPRnM b
liftRnWithCont forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
cont = CpsRn b -> TPRnM b
forall a. CpsRn a -> TPRnM a
liftTPRnCps ((forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
liftCpsWithCont (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
cont)

liftTPRnCps :: CpsRn a -> TPRnM a
liftTPRnCps :: forall a. CpsRn a -> TPRnM a
liftTPRnCps = ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
-> TPRnM a
forall a.
ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
-> TPRnM a
MkTPRnM (ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
 -> TPRnM a)
-> (CpsRn a
    -> ReaderT
         (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a)
-> CpsRn a
-> TPRnM a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterT HsTyPatRnBuilder CpsRn a
-> ReaderT
     (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
forall (m :: * -> *) a.
Monad m =>
m a -> ReaderT (HsDocContext, OccSet) m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (WriterT HsTyPatRnBuilder CpsRn a
 -> ReaderT
      (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a)
-> (CpsRn a -> WriterT HsTyPatRnBuilder CpsRn a)
-> CpsRn a
-> ReaderT
     (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CpsRn a -> WriterT HsTyPatRnBuilder CpsRn a
forall (m :: * -> *) a.
Monad m =>
m a -> WriterT HsTyPatRnBuilder m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift

liftTPRnRaw ::
  ( forall r .
    HsDocContext ->
    OccSet ->
    ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars)) ->
    RnM (r, FreeVars)
  ) -> TPRnM a
liftTPRnRaw :: forall a.
(forall r.
 HsDocContext
 -> OccSet
 -> ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars))
 -> RnM (r, FreeVars))
-> TPRnM a
liftTPRnRaw forall r.
HsDocContext
-> OccSet
-> ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
cont = ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
-> TPRnM a
forall a.
ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
-> TPRnM a
MkTPRnM (ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
 -> TPRnM a)
-> ReaderT
     (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
-> TPRnM a
forall a b. (a -> b) -> a -> b
$ ((HsDocContext, OccSet) -> WriterT HsTyPatRnBuilder CpsRn a)
-> ReaderT
     (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT (((HsDocContext, OccSet) -> WriterT HsTyPatRnBuilder CpsRn a)
 -> ReaderT
      (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a)
-> ((HsDocContext, OccSet) -> WriterT HsTyPatRnBuilder CpsRn a)
-> ReaderT
     (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
forall a b. (a -> b) -> a -> b
$ \(HsDocContext
doc_ctxt, OccSet
locals) -> CpsRn (a, HsTyPatRnBuilder) -> WriterT HsTyPatRnBuilder CpsRn a
forall (m :: * -> *) w a.
(Functor m, Monoid w) =>
m (a, w) -> WriterT w m a
writerT (CpsRn (a, HsTyPatRnBuilder) -> WriterT HsTyPatRnBuilder CpsRn a)
-> CpsRn (a, HsTyPatRnBuilder) -> WriterT HsTyPatRnBuilder CpsRn a
forall a b. (a -> b) -> a -> b
$ (forall r.
 ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn (a, HsTyPatRnBuilder)
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
liftCpsWithCont (HsDocContext
-> OccSet
-> ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
forall r.
HsDocContext
-> OccSet
-> ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
cont HsDocContext
doc_ctxt OccSet
locals)

unTPRnRaw ::
  TPRnM a ->
  HsDocContext ->
  OccSet ->
  ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars)) ->
  RnM (r, FreeVars)
unTPRnRaw :: forall a r.
TPRnM a
-> HsDocContext
-> OccSet
-> ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
unTPRnRaw (MkTPRnM ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
m) HsDocContext
doc_ctxt OccSet
locals = CpsRn (a, HsTyPatRnBuilder)
-> forall r.
   ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
forall b.
CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
unCpsRn (CpsRn (a, HsTyPatRnBuilder)
 -> forall r.
    ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn (a, HsTyPatRnBuilder)
-> forall r.
   ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
forall a b. (a -> b) -> a -> b
$ WriterT HsTyPatRnBuilder CpsRn a -> CpsRn (a, HsTyPatRnBuilder)
forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
runWriterT (WriterT HsTyPatRnBuilder CpsRn a -> CpsRn (a, HsTyPatRnBuilder))
-> WriterT HsTyPatRnBuilder CpsRn a -> CpsRn (a, HsTyPatRnBuilder)
forall a b. (a -> b) -> a -> b
$ ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
-> (HsDocContext, OccSet) -> WriterT HsTyPatRnBuilder CpsRn a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT ReaderT (HsDocContext, OccSet) (WriterT HsTyPatRnBuilder CpsRn) a
m (HsDocContext
doc_ctxt, OccSet
locals)

wrapSrcSpanTPRnM :: (a -> TPRnM b) -> LocatedAn ann a -> TPRnM (LocatedAn ann b)
wrapSrcSpanTPRnM :: forall a b ann.
(a -> TPRnM b) -> LocatedAn ann a -> TPRnM (LocatedAn ann b)
wrapSrcSpanTPRnM a -> TPRnM b
fn (L EpAnn ann
loc a
a) = do
  b
a' <- a -> TPRnM b
fn a
a
  LocatedAn ann b -> TPRnM (LocatedAn ann b)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EpAnn ann -> b -> LocatedAn ann b
forall l e. l -> e -> GenLocated l e
L EpAnn ann
loc b
a')

lookupTypeOccTPRnM :: RdrName -> TPRnM Name
lookupTypeOccTPRnM :: RdrName -> TPRnM Name
lookupTypeOccTPRnM RdrName
rdr_name = RnM (Name, FreeVars) -> TPRnM Name
forall a. RnM (a, FreeVars) -> TPRnM a
liftRnFV (RnM (Name, FreeVars) -> TPRnM Name)
-> RnM (Name, FreeVars) -> TPRnM Name
forall a b. (a -> b) -> a -> b
$ do
  Name
name <- RdrName -> RnM Name
lookupTypeOccRn RdrName
rdr_name
  (Name, FreeVars) -> RnM (Name, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Name
name, Name -> FreeVars
unitFV Name
name)

-- | A variant of HsTyPatRn that uses Bags for efficient concatenation.
-- See Note [Implicit and explicit type variable binders]
data HsTyPatRnBuilder =
  HsTPRnB {
    HsTyPatRnBuilder -> Bag Name
hstpb_nwcs :: Bag Name,
    HsTyPatRnBuilder -> Bag Name
hstpb_imp_tvs :: Bag Name,
    HsTyPatRnBuilder -> Bag Name
hstpb_exp_tvs :: Bag Name
  }

tpb_exp_tv :: Name -> HsTyPatRnBuilder
tpb_exp_tv :: Name -> HsTyPatRnBuilder
tpb_exp_tv Name
name = HsTyPatRnBuilder
forall a. Monoid a => a
mempty {hstpb_exp_tvs = unitBag name}

tpb_hsps :: HsPSRn -> HsTyPatRnBuilder
tpb_hsps :: HsPSRn -> HsTyPatRnBuilder
tpb_hsps HsPSRn {[Name]
hsps_nwcs :: [Name]
hsps_nwcs :: HsPSRn -> [Name]
hsps_nwcs, [Name]
hsps_imp_tvs :: [Name]
hsps_imp_tvs :: HsPSRn -> [Name]
hsps_imp_tvs} =
  HsTyPatRnBuilder
forall a. Monoid a => a
mempty {
    hstpb_nwcs = listToBag hsps_nwcs,
    hstpb_imp_tvs = listToBag hsps_imp_tvs
  }

instance Semigroup HsTyPatRnBuilder where
  HsTPRnB Bag Name
nwcs1 Bag Name
imp_tvs1 Bag Name
exptvs1 <> :: HsTyPatRnBuilder -> HsTyPatRnBuilder -> HsTyPatRnBuilder
<> HsTPRnB Bag Name
nwcs2 Bag Name
imp_tvs2 Bag Name
exptvs2 =
    Bag Name -> Bag Name -> Bag Name -> HsTyPatRnBuilder
HsTPRnB
      (Bag Name
nwcs1    Bag Name -> Bag Name -> Bag Name
forall a. Bag a -> Bag a -> Bag a
`unionBags` Bag Name
nwcs2)
      (Bag Name
imp_tvs1 Bag Name -> Bag Name -> Bag Name
forall a. Bag a -> Bag a -> Bag a
`unionBags` Bag Name
imp_tvs2)
      (Bag Name
exptvs1  Bag Name -> Bag Name -> Bag Name
forall a. Bag a -> Bag a -> Bag a
`unionBags` Bag Name
exptvs2)

instance Monoid HsTyPatRnBuilder where
  mempty :: HsTyPatRnBuilder
mempty = Bag Name -> Bag Name -> Bag Name -> HsTyPatRnBuilder
HsTPRnB Bag Name
forall a. Bag a
emptyBag Bag Name
forall a. Bag a
emptyBag Bag Name
forall a. Bag a
emptyBag

buildHsTyPatRn :: HsTyPatRnBuilder -> HsTyPatRn
buildHsTyPatRn :: HsTyPatRnBuilder -> HsTyPatRn
buildHsTyPatRn HsTPRnB {Bag Name
hstpb_nwcs :: HsTyPatRnBuilder -> Bag Name
hstpb_nwcs :: Bag Name
hstpb_nwcs, Bag Name
hstpb_imp_tvs :: HsTyPatRnBuilder -> Bag Name
hstpb_imp_tvs :: Bag Name
hstpb_imp_tvs, Bag Name
hstpb_exp_tvs :: HsTyPatRnBuilder -> Bag Name
hstpb_exp_tvs :: Bag Name
hstpb_exp_tvs} =
  HsTPRn {
    hstp_nwcs :: [Name]
hstp_nwcs =    Bag Name -> [Name]
forall a. Bag a -> [a]
bagToList Bag Name
hstpb_nwcs,
    hstp_imp_tvs :: [Name]
hstp_imp_tvs = Bag Name -> [Name]
forall a. Bag a -> [a]
bagToList Bag Name
hstpb_imp_tvs,
    hstp_exp_tvs :: [Name]
hstp_exp_tvs = Bag Name -> [Name]
forall a. Bag a -> [a]
bagToList Bag Name
hstpb_exp_tvs
  }

rn_lty_pat :: LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat :: LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat (L EpAnn AnnListItem
l HsType GhcPs
hs_ty) = do
  HsType GhcRn
hs_ty' <- HsType GhcPs -> TPRnM (HsType GhcRn)
rn_ty_pat HsType GhcPs
hs_ty
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
-> TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn))
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EpAnn AnnListItem
-> HsType GhcRn -> GenLocated (EpAnn AnnListItem) (HsType GhcRn)
forall l e. l -> e -> GenLocated l e
L EpAnn AnnListItem
l HsType GhcRn
hs_ty')

rn_ty_pat_var :: LocatedN RdrName -> TPRnM (LocatedN Name)
rn_ty_pat_var :: GenLocated SrcSpanAnnN RdrName -> TPRnM (LocatedN Name)
rn_ty_pat_var lrdr :: GenLocated SrcSpanAnnN RdrName
lrdr@(L SrcSpanAnnN
l RdrName
rdr) = do
  OccSet
locals <- TPRnM OccSet
askLocals
  if RdrName -> Bool
isRdrTyVar RdrName
rdr
    Bool -> Bool -> Bool
&& Bool -> Bool
not (OccName -> OccSet -> Bool
elemOccSet (RdrName -> OccName
forall name. HasOccName name => name -> OccName
occName RdrName
rdr) OccSet
locals) -- See Note [Locally bound names in type patterns]

    then do -- binder
      Name
name <- CpsRn Name -> TPRnM Name
forall a. CpsRn a -> TPRnM a
liftTPRnCps (CpsRn Name -> TPRnM Name) -> CpsRn Name -> TPRnM Name
forall a b. (a -> b) -> a -> b
$ NameMaker -> GenLocated SrcSpanAnnN RdrName -> CpsRn Name
newPatName (Bool -> NameMaker
LamMk Bool
True) GenLocated SrcSpanAnnN RdrName
lrdr
      HsTyPatRnBuilder -> TPRnM ()
tellTPB (Name -> HsTyPatRnBuilder
tpb_exp_tv Name
name)
      LocatedN Name -> TPRnM (LocatedN Name)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SrcSpanAnnN -> Name -> LocatedN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
l Name
name)

    else do -- usage
      Name
name <- RdrName -> TPRnM Name
lookupTypeOccTPRnM RdrName
rdr
      LocatedN Name -> TPRnM (LocatedN Name)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SrcSpanAnnN -> Name -> LocatedN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
l Name
name)

-- | Rename type patterns
--
-- For the difference between `rn_ty_pat` and `rnHsTyKi` see Note [CpsRn monad]
-- and Note [Implicit and explicit type variable binders]
rn_ty_pat :: HsType GhcPs -> TPRnM (HsType GhcRn)
rn_ty_pat :: HsType GhcPs -> TPRnM (HsType GhcRn)
rn_ty_pat tv :: HsType GhcPs
tv@(HsTyVar XTyVar GhcPs
an PromotionFlag
prom XRec GhcPs (IdP GhcPs)
lrdr) = do
  lname :: LocatedN Name
lname@(L SrcSpanAnnN
_ Name
name) <- GenLocated SrcSpanAnnN RdrName -> TPRnM (LocatedN Name)
rn_ty_pat_var XRec GhcPs (IdP GhcPs)
GenLocated SrcSpanAnnN RdrName
lrdr
  Bool -> TPRnM () -> TPRnM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Name -> Bool
isDataConName Name
name Bool -> Bool -> Bool
&& Bool -> Bool
not (Name -> Bool
isKindName Name
name)) (TPRnM () -> TPRnM ()) -> TPRnM () -> TPRnM ()
forall a b. (a -> b) -> a -> b
$
    -- Any use of a promoted data constructor name (that is not specifically
    -- exempted by isKindName) is illegal without the use of DataKinds.
    -- See Note [Checking for DataKinds] in GHC.Tc.Validity.
    HsType GhcPs -> TPRnM ()
check_data_kinds HsType GhcPs
tv
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XTyVar GhcRn -> PromotionFlag -> LIdP GhcRn -> HsType GhcRn
forall pass.
XTyVar pass -> PromotionFlag -> LIdP pass -> HsType pass
HsTyVar XTyVar GhcPs
XTyVar GhcRn
an PromotionFlag
prom LIdP GhcRn
LocatedN Name
lname)

rn_ty_pat (HsForAllTy XForAllTy GhcPs
an HsForAllTelescope GhcPs
tele LHsType GhcPs
body) = (forall r.
 HsDocContext
 -> OccSet
 -> ((HsType GhcRn, HsTyPatRnBuilder) -> RnM (r, FreeVars))
 -> RnM (r, FreeVars))
-> TPRnM (HsType GhcRn)
forall a.
(forall r.
 HsDocContext
 -> OccSet
 -> ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars))
 -> RnM (r, FreeVars))
-> TPRnM a
liftTPRnRaw ((forall r.
  HsDocContext
  -> OccSet
  -> ((HsType GhcRn, HsTyPatRnBuilder) -> RnM (r, FreeVars))
  -> RnM (r, FreeVars))
 -> TPRnM (HsType GhcRn))
-> (forall r.
    HsDocContext
    -> OccSet
    -> ((HsType GhcRn, HsTyPatRnBuilder) -> RnM (r, FreeVars))
    -> RnM (r, FreeVars))
-> TPRnM (HsType GhcRn)
forall a b. (a -> b) -> a -> b
$ \HsDocContext
ctxt OccSet
locals (HsType GhcRn, HsTyPatRnBuilder) -> RnM (r, FreeVars)
thing_inside ->
  HsDocContext
-> HsForAllTelescope GhcPs
-> (HsForAllTelescope GhcRn -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
forall a.
HsDocContext
-> HsForAllTelescope GhcPs
-> (HsForAllTelescope GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
bindHsForAllTelescope HsDocContext
ctxt HsForAllTelescope GhcPs
tele ((HsForAllTelescope GhcRn -> RnM (r, FreeVars))
 -> RnM (r, FreeVars))
-> (HsForAllTelescope GhcRn -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
forall a b. (a -> b) -> a -> b
$ \HsForAllTelescope GhcRn
tele' -> do
    let
      tele_names :: [IdP GhcRn]
tele_names = HsForAllTelescope GhcRn -> [IdP GhcRn]
forall (p :: Pass).
HsForAllTelescope (GhcPass p) -> [IdP (GhcPass p)]
hsForAllTelescopeNames HsForAllTelescope GhcRn
tele'
      locals' :: OccSet
locals' = OccSet
locals OccSet -> [OccName] -> OccSet
`extendOccSetList` (Name -> OccName) -> [Name] -> [OccName]
forall a b. (a -> b) -> [a] -> [b]
map Name -> OccName
forall name. HasOccName name => name -> OccName
occName [IdP GhcRn]
[Name]
tele_names

    TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn))
-> HsDocContext
-> OccSet
-> ((GenLocated (EpAnn AnnListItem) (HsType GhcRn),
     HsTyPatRnBuilder)
    -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
forall a r.
TPRnM a
-> HsDocContext
-> OccSet
-> ((a, HsTyPatRnBuilder) -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
unTPRnRaw (LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
body) HsDocContext
ctxt OccSet
locals' (((GenLocated (EpAnn AnnListItem) (HsType GhcRn), HsTyPatRnBuilder)
  -> RnM (r, FreeVars))
 -> RnM (r, FreeVars))
-> ((GenLocated (EpAnn AnnListItem) (HsType GhcRn),
     HsTyPatRnBuilder)
    -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
forall a b. (a -> b) -> a -> b
$ \(GenLocated (EpAnn AnnListItem) (HsType GhcRn)
body', HsTyPatRnBuilder
tpb) ->
      [Name] -> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a. [Name] -> RnM a -> RnM a
delLocalNames [IdP GhcRn]
[Name]
tele_names (RnM (r, FreeVars) -> RnM (r, FreeVars))
-> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a b. (a -> b) -> a -> b
$ -- locally bound names do not scope over the continuation
        (HsType GhcRn, HsTyPatRnBuilder) -> RnM (r, FreeVars)
thing_inside ((XForAllTy GhcRn
-> HsForAllTelescope GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass.
XForAllTy pass
-> HsForAllTelescope pass -> LHsType pass -> HsType pass
HsForAllTy XForAllTy GhcPs
XForAllTy GhcRn
an HsForAllTelescope GhcRn
tele' LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
body'), HsTyPatRnBuilder
tpb)

rn_ty_pat (HsQualTy XQualTy GhcPs
an LHsContext GhcPs
lctx LHsType GhcPs
body) = do
  LocatedAn
  AnnContext [GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
lctx' <- ([GenLocated (EpAnn AnnListItem) (HsType GhcPs)]
 -> TPRnM [GenLocated (EpAnn AnnListItem) (HsType GhcRn)])
-> LocatedAn
     AnnContext [GenLocated (EpAnn AnnListItem) (HsType GhcPs)]
-> TPRnM
     (LocatedAn
        AnnContext [GenLocated (EpAnn AnnListItem) (HsType GhcRn)])
forall a b ann.
(a -> TPRnM b) -> LocatedAn ann a -> TPRnM (LocatedAn ann b)
wrapSrcSpanTPRnM ((GenLocated (EpAnn AnnListItem) (HsType GhcPs)
 -> TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn)))
-> [GenLocated (EpAnn AnnListItem) (HsType GhcPs)]
-> TPRnM [GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM LHsType GhcPs -> TPRnM (LHsType GhcRn)
GenLocated (EpAnn AnnListItem) (HsType GhcPs)
-> TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn))
rn_lty_pat) LHsContext GhcPs
LocatedAn
  AnnContext [GenLocated (EpAnn AnnListItem) (HsType GhcPs)]
lctx
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
body' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
body
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XQualTy GhcRn -> LHsContext GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass.
XQualTy pass -> LHsContext pass -> LHsType pass -> HsType pass
HsQualTy XQualTy GhcPs
XQualTy GhcRn
an LHsContext GhcRn
LocatedAn
  AnnContext [GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
lctx' LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
body')

rn_ty_pat (HsAppTy XAppTy GhcPs
_ LHsType GhcPs
fun_ty LHsType GhcPs
arg_ty) = do
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
fun_ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
fun_ty
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
arg_ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
arg_ty
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XAppTy GhcRn -> LHsType GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass.
XAppTy pass -> LHsType pass -> LHsType pass -> HsType pass
HsAppTy XAppTy GhcRn
NoExtField
noExtField LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
fun_ty' LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
arg_ty')

rn_ty_pat (HsAppKindTy XAppKindTy GhcPs
_ LHsType GhcPs
ty LHsType GhcPs
ki) = do
  Bool
kind_app <- RnM Bool -> TPRnM Bool
forall a. RnM a -> TPRnM a
liftRn (RnM Bool -> TPRnM Bool) -> RnM Bool -> TPRnM Bool
forall a b. (a -> b) -> a -> b
$ Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.TypeApplications
  Bool -> TPRnM () -> TPRnM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
kind_app (IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a. RnM a -> TPRnM a
liftRn (IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ())
-> IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr (TypeOrKind -> LHsType GhcPs -> TcRnMessage
typeAppErr TypeOrKind
KindLevel LHsType GhcPs
ki))
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
ty
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ki' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
ki
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XAppKindTy GhcRn -> LHsType GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass.
XAppKindTy pass -> LHsType pass -> LHsType pass -> HsType pass
HsAppKindTy XAppKindTy GhcRn
NoExtField
noExtField LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty' LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ki')

rn_ty_pat (HsFunTy XFunTy GhcPs
an HsArrow GhcPs
mult LHsType GhcPs
lhs LHsType GhcPs
rhs) = do
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
lhs' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
lhs
  HsArrow GhcRn
mult' <- HsArrow GhcPs -> TPRnM (HsArrow GhcRn)
rn_ty_pat_arrow HsArrow GhcPs
mult
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
rhs' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
rhs
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XFunTy GhcRn
-> HsArrow GhcRn -> LHsType GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass.
XFunTy pass
-> HsArrow pass -> LHsType pass -> LHsType pass -> HsType pass
HsFunTy XFunTy GhcPs
XFunTy GhcRn
an HsArrow GhcRn
mult' LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
lhs' LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
rhs')

rn_ty_pat (HsListTy XListTy GhcPs
an LHsType GhcPs
ty) = do
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
ty
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XListTy GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass. XListTy pass -> LHsType pass -> HsType pass
HsListTy XListTy GhcPs
XListTy GhcRn
an LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty')

rn_ty_pat (HsTupleTy XTupleTy GhcPs
an HsTupleSort
con [LHsType GhcPs]
tys) = do
  [GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
tys' <- (GenLocated (EpAnn AnnListItem) (HsType GhcPs)
 -> TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn)))
-> [GenLocated (EpAnn AnnListItem) (HsType GhcPs)]
-> TPRnM [GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM LHsType GhcPs -> TPRnM (LHsType GhcRn)
GenLocated (EpAnn AnnListItem) (HsType GhcPs)
-> TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn))
rn_lty_pat [LHsType GhcPs]
[GenLocated (EpAnn AnnListItem) (HsType GhcPs)]
tys
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XTupleTy GhcRn -> HsTupleSort -> [LHsType GhcRn] -> HsType GhcRn
forall pass.
XTupleTy pass -> HsTupleSort -> [LHsType pass] -> HsType pass
HsTupleTy XTupleTy GhcPs
XTupleTy GhcRn
an HsTupleSort
con [LHsType GhcRn]
[GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
tys')

rn_ty_pat (HsSumTy XSumTy GhcPs
an [LHsType GhcPs]
tys) = do
  [GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
tys' <- (GenLocated (EpAnn AnnListItem) (HsType GhcPs)
 -> TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn)))
-> [GenLocated (EpAnn AnnListItem) (HsType GhcPs)]
-> TPRnM [GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM LHsType GhcPs -> TPRnM (LHsType GhcRn)
GenLocated (EpAnn AnnListItem) (HsType GhcPs)
-> TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn))
rn_lty_pat [LHsType GhcPs]
[GenLocated (EpAnn AnnListItem) (HsType GhcPs)]
tys
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XSumTy GhcRn -> [LHsType GhcRn] -> HsType GhcRn
forall pass. XSumTy pass -> [LHsType pass] -> HsType pass
HsSumTy XSumTy GhcPs
XSumTy GhcRn
an [LHsType GhcRn]
[GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
tys')

rn_ty_pat (HsOpTy XOpTy GhcPs
_ PromotionFlag
prom LHsType GhcPs
ty1 XRec GhcPs (IdP GhcPs)
l_op LHsType GhcPs
ty2) = do
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty1' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
ty1
  LocatedN Name
l_op' <- GenLocated SrcSpanAnnN RdrName -> TPRnM (LocatedN Name)
rn_ty_pat_var XRec GhcPs (IdP GhcPs)
GenLocated SrcSpanAnnN RdrName
l_op
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty2' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
ty2
  Fixity
fix  <- RnM Fixity -> TPRnM Fixity
forall a. RnM a -> TPRnM a
liftRn (RnM Fixity -> TPRnM Fixity) -> RnM Fixity -> TPRnM Fixity
forall a b. (a -> b) -> a -> b
$ LocatedN Name -> RnM Fixity
lookupTyFixityRn LocatedN Name
l_op'
  let op_name :: Name
op_name = LocatedN Name -> Name
forall l e. GenLocated l e -> e
unLoc LocatedN Name
l_op'
  Bool -> TPRnM () -> TPRnM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Name -> Bool
isDataConName Name
op_name Bool -> Bool -> Bool
&& Bool -> Bool
not (PromotionFlag -> Bool
isPromoted PromotionFlag
prom)) (TPRnM () -> TPRnM ()) -> TPRnM () -> TPRnM ()
forall a b. (a -> b) -> a -> b
$
    IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a. RnM a -> TPRnM a
liftRn (IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ())
-> IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addDiagnostic (UntickedPromotedThing -> TcRnMessage
TcRnUntickedPromotedThing (UntickedPromotedThing -> TcRnMessage)
-> UntickedPromotedThing -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ LexicalFixity -> Name -> UntickedPromotedThing
UntickedConstructor LexicalFixity
Infix Name
op_name)
  RnM (HsType GhcRn) -> TPRnM (HsType GhcRn)
forall a. RnM a -> TPRnM a
liftRn (RnM (HsType GhcRn) -> TPRnM (HsType GhcRn))
-> RnM (HsType GhcRn) -> TPRnM (HsType GhcRn)
forall a b. (a -> b) -> a -> b
$ PromotionFlag
-> LocatedN Name
-> Fixity
-> LHsType GhcRn
-> LHsType GhcRn
-> RnM (HsType GhcRn)
mkHsOpTyRn PromotionFlag
prom LocatedN Name
l_op' Fixity
fix LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty1' LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty2'

rn_ty_pat (HsParTy XParTy GhcPs
an LHsType GhcPs
ty) = do
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
ty
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XParTy GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass. XParTy pass -> LHsType pass -> HsType pass
HsParTy XParTy GhcPs
XParTy GhcRn
an LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty')

rn_ty_pat (HsIParamTy XIParamTy GhcPs
an XRec GhcPs HsIPName
n LHsType GhcPs
ty) = do
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
ty
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XIParamTy GhcRn
-> XRec GhcRn HsIPName -> LHsType GhcRn -> HsType GhcRn
forall pass.
XIParamTy pass -> XRec pass HsIPName -> LHsType pass -> HsType pass
HsIParamTy XIParamTy GhcPs
XIParamTy GhcRn
an XRec GhcPs HsIPName
XRec GhcRn HsIPName
n LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty')

rn_ty_pat (HsStarTy XStarTy GhcPs
an Bool
unicode) =
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XStarTy GhcRn -> Bool -> HsType GhcRn
forall pass. XStarTy pass -> Bool -> HsType pass
HsStarTy XStarTy GhcPs
XStarTy GhcRn
an Bool
unicode)

rn_ty_pat (HsDocTy XDocTy GhcPs
an LHsType GhcPs
ty LHsDoc GhcPs
haddock_doc) = do
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
ty
  LHsDoc GhcRn
haddock_doc' <- RnM (LHsDoc GhcRn) -> TPRnM (LHsDoc GhcRn)
forall a. RnM a -> TPRnM a
liftRn (RnM (LHsDoc GhcRn) -> TPRnM (LHsDoc GhcRn))
-> RnM (LHsDoc GhcRn) -> TPRnM (LHsDoc GhcRn)
forall a b. (a -> b) -> a -> b
$ LHsDoc GhcPs -> RnM (LHsDoc GhcRn)
rnLHsDoc LHsDoc GhcPs
haddock_doc
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XDocTy GhcRn -> LHsType GhcRn -> LHsDoc GhcRn -> HsType GhcRn
forall pass.
XDocTy pass -> LHsType pass -> LHsDoc pass -> HsType pass
HsDocTy XDocTy GhcPs
XDocTy GhcRn
an LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty' LHsDoc GhcRn
haddock_doc')

rn_ty_pat ty :: HsType GhcPs
ty@(HsExplicitListTy XExplicitListTy GhcPs
_ PromotionFlag
prom [LHsType GhcPs]
tys) = do
  HsType GhcPs -> TPRnM ()
check_data_kinds HsType GhcPs
ty

  Bool -> TPRnM () -> TPRnM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (PromotionFlag -> Bool
isPromoted PromotionFlag
prom) (TPRnM () -> TPRnM ()) -> TPRnM () -> TPRnM ()
forall a b. (a -> b) -> a -> b
$
    IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a. RnM a -> TPRnM a
liftRn (IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ())
-> IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addDiagnostic (UntickedPromotedThing -> TcRnMessage
TcRnUntickedPromotedThing (UntickedPromotedThing -> TcRnMessage)
-> UntickedPromotedThing -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ UntickedPromotedThing
UntickedExplicitList)

  [GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
tys' <- (GenLocated (EpAnn AnnListItem) (HsType GhcPs)
 -> TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn)))
-> [GenLocated (EpAnn AnnListItem) (HsType GhcPs)]
-> TPRnM [GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM LHsType GhcPs -> TPRnM (LHsType GhcRn)
GenLocated (EpAnn AnnListItem) (HsType GhcPs)
-> TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn))
rn_lty_pat [LHsType GhcPs]
[GenLocated (EpAnn AnnListItem) (HsType GhcPs)]
tys
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XExplicitListTy GhcRn
-> PromotionFlag -> [LHsType GhcRn] -> HsType GhcRn
forall pass.
XExplicitListTy pass
-> PromotionFlag -> [LHsType pass] -> HsType pass
HsExplicitListTy XExplicitListTy GhcRn
NoExtField
noExtField PromotionFlag
prom [LHsType GhcRn]
[GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
tys')

rn_ty_pat ty :: HsType GhcPs
ty@(HsExplicitTupleTy XExplicitTupleTy GhcPs
_ [LHsType GhcPs]
tys) = do
  HsType GhcPs -> TPRnM ()
check_data_kinds HsType GhcPs
ty
  [GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
tys' <- (GenLocated (EpAnn AnnListItem) (HsType GhcPs)
 -> TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn)))
-> [GenLocated (EpAnn AnnListItem) (HsType GhcPs)]
-> TPRnM [GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM LHsType GhcPs -> TPRnM (LHsType GhcRn)
GenLocated (EpAnn AnnListItem) (HsType GhcPs)
-> TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn))
rn_lty_pat [LHsType GhcPs]
[GenLocated (EpAnn AnnListItem) (HsType GhcPs)]
tys
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XExplicitTupleTy GhcRn -> [LHsType GhcRn] -> HsType GhcRn
forall pass. XExplicitTupleTy pass -> [LHsType pass] -> HsType pass
HsExplicitTupleTy XExplicitTupleTy GhcRn
NoExtField
noExtField [LHsType GhcRn]
[GenLocated (EpAnn AnnListItem) (HsType GhcRn)]
tys')

rn_ty_pat tyLit :: HsType GhcPs
tyLit@(HsTyLit XTyLit GhcPs
src HsTyLit GhcPs
t) = do
  HsType GhcPs -> TPRnM ()
check_data_kinds HsType GhcPs
tyLit
  HsTyLit GhcRn
t' <- RnM (HsTyLit GhcRn) -> TPRnM (HsTyLit GhcRn)
forall a. RnM a -> TPRnM a
liftRn (RnM (HsTyLit GhcRn) -> TPRnM (HsTyLit GhcRn))
-> RnM (HsTyLit GhcRn) -> TPRnM (HsTyLit GhcRn)
forall a b. (a -> b) -> a -> b
$ HsTyLit GhcPs -> RnM (HsTyLit GhcRn)
rnHsTyLit HsTyLit GhcPs
t
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XTyLit GhcRn -> HsTyLit GhcRn -> HsType GhcRn
forall pass. XTyLit pass -> HsTyLit pass -> HsType pass
HsTyLit XTyLit GhcPs
XTyLit GhcRn
src HsTyLit GhcRn
t')

rn_ty_pat (HsWildCardTy XWildCardTy GhcPs
_) =
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XWildCardTy GhcRn -> HsType GhcRn
forall pass. XWildCardTy pass -> HsType pass
HsWildCardTy XWildCardTy GhcRn
NoExtField
noExtField)

rn_ty_pat (HsKindSig XKindSig GhcPs
an LHsType GhcPs
ty LHsType GhcPs
ki) = do
  HsDocContext
ctxt <- TPRnM HsDocContext
askDocContext
  Bool
kind_sigs_ok <- RnM Bool -> TPRnM Bool
forall a. RnM a -> TPRnM a
liftRn (RnM Bool -> TPRnM Bool) -> RnM Bool -> TPRnM Bool
forall a b. (a -> b) -> a -> b
$ Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.KindSignatures
  Bool -> TPRnM () -> TPRnM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
kind_sigs_ok (IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a. RnM a -> TPRnM a
liftRn (IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ())
-> IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a b. (a -> b) -> a -> b
$ HsDocContext -> LHsType GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) ()
badKindSigErr HsDocContext
ctxt LHsType GhcPs
ki)
  ~(HsPS XHsPS GhcRn
hsps LHsType GhcRn
ki') <- (forall r.
 (HsPatSigType GhcRn -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> TPRnM (HsPatSigType GhcRn)
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> TPRnM b
liftRnWithCont ((forall r.
  (HsPatSigType GhcRn -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
 -> TPRnM (HsPatSigType GhcRn))
-> (forall r.
    (HsPatSigType GhcRn -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> TPRnM (HsPatSigType GhcRn)
forall a b. (a -> b) -> a -> b
$
                      HsPatSigTypeScoping
-> HsDocContext
-> HsPatSigType GhcPs
-> (HsPatSigType GhcRn
    -> IOEnv (Env TcGblEnv TcLclEnv) (r, FreeVars))
-> IOEnv (Env TcGblEnv TcLclEnv) (r, FreeVars)
forall a.
HsPatSigTypeScoping
-> HsDocContext
-> HsPatSigType GhcPs
-> (HsPatSigType GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnHsPatSigKind HsPatSigTypeScoping
AlwaysBind HsDocContext
ctxt (XHsPS GhcPs -> LHsType GhcPs -> HsPatSigType GhcPs
forall pass. XHsPS pass -> LHsType pass -> HsPatSigType pass
HsPS XHsPS GhcPs
EpAnn NoEpAnns
forall a. NoAnn a => a
noAnn LHsType GhcPs
ki)
  GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
ty
  HsTyPatRnBuilder -> TPRnM ()
tellTPB (HsPSRn -> HsTyPatRnBuilder
tpb_hsps XHsPS GhcRn
HsPSRn
hsps)
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XKindSig GhcRn -> LHsType GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass.
XKindSig pass -> LHsType pass -> LHsType pass -> HsType pass
HsKindSig XKindSig GhcPs
XKindSig GhcRn
an LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
ty' LHsType GhcRn
ki')

rn_ty_pat (HsSpliceTy XSpliceTy GhcPs
_ HsUntypedSplice GhcPs
splice) = do
  (HsUntypedSplice GhcRn,
 HsUntypedSpliceResult
   (GenLocated (EpAnn AnnListItem) (HsType GhcPs)))
res <- RnM
  ((HsUntypedSplice GhcRn, HsUntypedSpliceResult (LHsType GhcPs)),
   FreeVars)
-> TPRnM
     (HsUntypedSplice GhcRn, HsUntypedSpliceResult (LHsType GhcPs))
forall a. RnM (a, FreeVars) -> TPRnM a
liftRnFV (RnM
   ((HsUntypedSplice GhcRn, HsUntypedSpliceResult (LHsType GhcPs)),
    FreeVars)
 -> TPRnM
      (HsUntypedSplice GhcRn, HsUntypedSpliceResult (LHsType GhcPs)))
-> RnM
     ((HsUntypedSplice GhcRn, HsUntypedSpliceResult (LHsType GhcPs)),
      FreeVars)
-> TPRnM
     (HsUntypedSplice GhcRn, HsUntypedSpliceResult (LHsType GhcPs))
forall a b. (a -> b) -> a -> b
$ HsUntypedSplice GhcPs
-> RnM
     ((HsUntypedSplice GhcRn, HsUntypedSpliceResult (LHsType GhcPs)),
      FreeVars)
rnSpliceTyPat HsUntypedSplice GhcPs
splice
  case (HsUntypedSplice GhcRn,
 HsUntypedSpliceResult
   (GenLocated (EpAnn AnnListItem) (HsType GhcPs)))
res of
    (HsUntypedSplice GhcRn
rn_splice, HsUntypedSpliceTop ThModFinalizers
mfs GenLocated (EpAnn AnnListItem) (HsType GhcPs)
pat) -> do -- Splice was top-level and thus run, creating LHsType GhcPs
        GenLocated (EpAnn AnnListItem) (HsType GhcRn)
pat' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
GenLocated (EpAnn AnnListItem) (HsType GhcPs)
pat
        HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XSpliceTy GhcRn -> HsUntypedSplice GhcRn -> HsType GhcRn
forall pass. XSpliceTy pass -> HsUntypedSplice pass -> HsType pass
HsSpliceTy (ThModFinalizers
-> GenLocated (EpAnn AnnListItem) (HsType GhcRn)
-> HsUntypedSpliceResult
     (GenLocated (EpAnn AnnListItem) (HsType GhcRn))
forall thing.
ThModFinalizers -> thing -> HsUntypedSpliceResult thing
HsUntypedSpliceTop ThModFinalizers
mfs (LHsType GhcRn -> LHsType GhcRn
mb_paren LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
pat')) HsUntypedSplice GhcRn
rn_splice)
    (HsUntypedSplice GhcRn
rn_splice, HsUntypedSpliceNested Name
splice_name) ->
        HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XSpliceTy GhcRn -> HsUntypedSplice GhcRn -> HsType GhcRn
forall pass. XSpliceTy pass -> HsUntypedSplice pass -> HsType pass
HsSpliceTy (Name
-> HsUntypedSpliceResult
     (GenLocated (EpAnn AnnListItem) (HsType GhcRn))
forall thing. Name -> HsUntypedSpliceResult thing
HsUntypedSpliceNested Name
splice_name) HsUntypedSplice GhcRn
rn_splice) -- Splice was nested and thus already renamed
  where
    mb_paren :: LHsType GhcRn -> LHsType GhcRn
    mb_paren :: LHsType GhcRn -> LHsType GhcRn
mb_paren lhs_ty :: LHsType GhcRn
lhs_ty@(L EpAnn AnnListItem
loc HsType GhcRn
hs_ty)
      | PprPrec -> HsType GhcRn -> Bool
forall (p :: Pass). PprPrec -> HsType (GhcPass p) -> Bool
hsTypeNeedsParens PprPrec
maxPrec HsType GhcRn
hs_ty = EpAnn AnnListItem
-> HsType GhcRn -> GenLocated (EpAnn AnnListItem) (HsType GhcRn)
forall l e. l -> e -> GenLocated l e
L EpAnn AnnListItem
loc (XParTy GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass. XParTy pass -> LHsType pass -> HsType pass
HsParTy XParTy GhcRn
AnnParen
forall a. NoAnn a => a
noAnn LHsType GhcRn
lhs_ty)
      | Bool
otherwise                       = LHsType GhcRn
lhs_ty

rn_ty_pat (HsBangTy XBangTy GhcPs
an HsSrcBang
bang_src LHsType GhcPs
lty) = do
  HsDocContext
ctxt <- TPRnM HsDocContext
askDocContext
  lty' :: GenLocated (EpAnn AnnListItem) (HsType GhcRn)
lty'@(L EpAnn AnnListItem
_ HsType GhcRn
ty') <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
lty
  IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a. RnM a -> TPRnM a
liftRn (IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ())
-> IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr (TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$
    HsDocContext -> TcRnMessage -> TcRnMessage
TcRnWithHsDocContext HsDocContext
ctxt (TcRnMessage -> TcRnMessage) -> TcRnMessage -> TcRnMessage
forall a b. (a -> b) -> a -> b
$
    HsType GhcRn -> HsSrcBang -> TcRnMessage
TcRnUnexpectedAnnotation HsType GhcRn
ty' HsSrcBang
bang_src
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XBangTy GhcRn -> HsSrcBang -> LHsType GhcRn -> HsType GhcRn
forall pass.
XBangTy pass -> HsSrcBang -> LHsType pass -> HsType pass
HsBangTy XBangTy GhcPs
XBangTy GhcRn
an HsSrcBang
bang_src LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
lty')

rn_ty_pat ty :: HsType GhcPs
ty@HsRecTy{} = do
  HsDocContext
ctxt <- TPRnM HsDocContext
askDocContext
  IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a. RnM a -> TPRnM a
liftRn (IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ())
-> IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr (TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$
    HsDocContext -> TcRnMessage -> TcRnMessage
TcRnWithHsDocContext HsDocContext
ctxt (TcRnMessage -> TcRnMessage) -> TcRnMessage -> TcRnMessage
forall a b. (a -> b) -> a -> b
$
    Either (HsType GhcPs) (HsType GhcRn) -> TcRnMessage
TcRnIllegalRecordSyntax (HsType GhcPs -> Either (HsType GhcPs) (HsType GhcRn)
forall a b. a -> Either a b
Left HsType GhcPs
ty)
  HsType GhcRn -> TPRnM (HsType GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XWildCardTy GhcRn -> HsType GhcRn
forall pass. XWildCardTy pass -> HsType pass
HsWildCardTy XWildCardTy GhcRn
NoExtField
noExtField) -- trick to avoid `failWithTc`

rn_ty_pat ty :: HsType GhcPs
ty@(XHsType{}) = do
  HsDocContext
ctxt <- TPRnM HsDocContext
askDocContext
  RnM (HsType GhcRn, FreeVars) -> TPRnM (HsType GhcRn)
forall a. RnM (a, FreeVars) -> TPRnM a
liftRnFV (RnM (HsType GhcRn, FreeVars) -> TPRnM (HsType GhcRn))
-> RnM (HsType GhcRn, FreeVars) -> TPRnM (HsType GhcRn)
forall a b. (a -> b) -> a -> b
$ HsDocContext -> HsType GhcPs -> RnM (HsType GhcRn, FreeVars)
rnHsType HsDocContext
ctxt HsType GhcPs
ty

rn_ty_pat_arrow :: HsArrow GhcPs -> TPRnM (HsArrow GhcRn)
rn_ty_pat_arrow :: HsArrow GhcPs -> TPRnM (HsArrow GhcRn)
rn_ty_pat_arrow (HsUnrestrictedArrow XUnrestrictedArrow GhcPs
_) = HsArrow GhcRn -> TPRnM (HsArrow GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XUnrestrictedArrow GhcRn -> HsArrow GhcRn
forall pass. XUnrestrictedArrow pass -> HsArrow pass
HsUnrestrictedArrow NoExtField
XUnrestrictedArrow GhcRn
noExtField)
rn_ty_pat_arrow (HsLinearArrow XLinearArrow GhcPs
_) = HsArrow GhcRn -> TPRnM (HsArrow GhcRn)
forall a. a -> TPRnM a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (XLinearArrow GhcRn -> HsArrow GhcRn
forall pass. XLinearArrow pass -> HsArrow pass
HsLinearArrow NoExtField
XLinearArrow GhcRn
noExtField)
rn_ty_pat_arrow (HsExplicitMult XExplicitMult GhcPs
_ LHsType GhcPs
p)
  = LHsType GhcPs -> TPRnM (LHsType GhcRn)
rn_lty_pat LHsType GhcPs
p TPRnM (GenLocated (EpAnn AnnListItem) (HsType GhcRn))
-> (GenLocated (EpAnn AnnListItem) (HsType GhcRn) -> HsArrow GhcRn)
-> TPRnM (HsArrow GhcRn)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (\GenLocated (EpAnn AnnListItem) (HsType GhcRn)
mult -> XExplicitMult GhcRn -> LHsType GhcRn -> HsArrow GhcRn
forall pass. XExplicitMult pass -> LHsType pass -> HsArrow pass
HsExplicitMult NoExtField
XExplicitMult GhcRn
noExtField LHsType GhcRn
GenLocated (EpAnn AnnListItem) (HsType GhcRn)
mult)

check_data_kinds :: HsType GhcPs -> TPRnM ()
check_data_kinds :: HsType GhcPs -> TPRnM ()
check_data_kinds HsType GhcPs
thing = IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a. RnM a -> TPRnM a
liftRn (IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ())
-> IOEnv (Env TcGblEnv TcLclEnv) () -> TPRnM ()
forall a b. (a -> b) -> a -> b
$ do
  Bool
data_kinds <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DataKinds
  Bool
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
data_kinds (IOEnv (Env TcGblEnv TcLclEnv) ()
 -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$
    TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr (TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> TcRnMessage -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$ TypeOrKind -> Either (HsType GhcPs) Type -> TcRnMessage
TcRnDataKindsError TypeOrKind
TypeLevel (Either (HsType GhcPs) Type -> TcRnMessage)
-> Either (HsType GhcPs) Type -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ HsType GhcPs -> Either (HsType GhcPs) Type
forall a b. a -> Either a b
Left HsType GhcPs
thing

{- Note [Locally bound names in type patterns]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Type patterns can bind local names using forall. Compare the following examples:
  f (Proxy @(Either a b)) = ...
  g (Proxy @(forall a . Either a b)) = ...

In `f` both `a` and `b` are bound by the pattern and scope over the RHS of f.
In `g` only `b` is bound by the pattern, whereas `a` is locally bound in the pattern
and does not scope over the RHS of `g`.

We track locally bound names in the `OccSet` in `TPRnM` monad, and use it to
decide whether occurrences of type variables are usages or bindings.

The check is done in `rn_ty_pat_var`

Note [Implicit and explicit type variable binders]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Type patterns are renamed differently from ordinary types.
  * Types are renamed by `rnHsType` where all type variable occurrences are considered usages
  * Type patterns are renamed by `rnHsTyPat` where some type variable occurrences are usages
    and other are bindings

Here is an example:
  {-# LANGUAGE ScopedTypeVariables #-}
  f :: forall b. Proxy _ -> ...
  f (Proxy @(x :: (a, b))) = ...

In the (x :: (a,b)) type pattern
  * `x` is a type variable explicitly bound by type pattern
  * `a` is a type variable implicitly bound in a pattern signature
  * `b` is a usage of type variable bound by the outer forall

This classification is clear to us in `rnHsTyPat`, but it is also useful in later passes, such
as `collectPatBinders` and `tcHsTyPat`, so we store it in the extension field of `HsTyPat`, namely
`HsTyPatRn`.

To collect lists of those variables efficiently we use `HsTyPatRnBuilder` which is exactly like
`HsTyPatRn`, but uses Bags.

Note [Left-to-right scoping of type patterns]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In term-level patterns, we use continuation passing to implement left-to-right
scoping, see Note [CpsRn monad]. Left-to-right scoping manifests itself when
e.g. view patterns are involved:

  f (x, g x -> Just y) = ...

Here the first occurrence of `x` is a binder, and the second occurrence is a
use of `x` in a view pattern. This example does not work if we swap the
components of the tuple:

  f (g x -> Just y, x) = ...
  --  ^^^
  -- Variable not in scope: x

In type patterns there are no view patterns, but there is a different feature
that is served well by left-to-right scoping: kind annotations. Compare:

  f (Proxy @(T k (a :: k))) = ...
  g (Proxy @(T (a :: k) k)) = ...

In `f`, the first occurrence of `k` is an explicit binder,
  and the second occurrence is a usage. Simple.
In `g`, the first occurrence of `k` is an implicit binder,
  and then the second occurrence is an explicit binder that shadows it.

So we get two different results after renaming:

  f (Proxy @(T k1 (a :: k1))) = ...
  g (Proxy @(T (a :: k1) k2)) = ...

This makes GHC accept the first example but rejects the second example with an
error about duplicate binders.

One could argue that we don't want order-sensitivity here. Historically, we
used a different principle when renaming types: collect all free variables,
bind them on the outside, and then rename all occurrences as usages.
This approach does not scale to multiple patterns. Consider:

  f' (MkP @k @(a :: k)) = ...
  g' (MkP @(a :: k) @k) = ...

Here a difference in behavior is inevitable, as we rename type patterns
one at a time. Could we perhaps concatenate the free variables from all
type patterns in a ConPat? But then we still get the same problem one level up,
when we have multiple patterns in a function LHS

  f'' (Proxy @k) (Proxy @(a :: k)) = ...
  g'' (Proxy @(a :: k)) (Proxy @k) = ...

And if we tried to avoid order sensitivity at this level, then we'd still be left
with lambdas:

  f''' (Proxy @k)        = \(Proxy @(a :: k)) -> ...
  g''' (Proxy @(a :: k)) = \(Proxy @k)        -> ...


So we have at least three options where we could do free variable extraction:
HsConPatTyArg, ConPat, or a Match (used to represent a function LHS). And none
of those would be general enough. Rather than make an arbitrary choice, we
embrace left-to-right scoping in types and implement it with CPS, just like
it's done for view patterns in terms.
-}