    {-# 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 { (v,fvs1) <- RnM (a, FreeVars)
    rn_thing
                                         ; (r,fvs2) <- k v
                                         ; return (r, fvs1 `plusFV` 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 { 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, fvs) <- k con_name
                        ; return (r, addOneFV fvs (unLoc 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 <- NameMaker -> GenLocated SrcSpanAnnN RdrName -> CpsRn Name
    newPatName NameMaker
    name_maker GenLocated SrcSpanAnnN RdrName
    rdr_name
           ; return (L loc 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 <- GenLocated SrcSpanAnnN RdrName -> RnM Name
    newLocalBndrRn GenLocated SrcSpanAnnN RdrName
    rdr_name
               ; (res, fvs) <- bindLocalNames [name] (thing_inside name)
               ; when report_unused $ warnUnusedMatches [name] fvs
               ; return (res, name `delFV` 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 <- 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
               ; bindLocalNames [name] $
                     -- Do *not* use bindLocalNameFV here;
                     --   see Note [View pattern usage]
                     -- For the TopLevel case
                     --   see Note [bindLocalNames for an External name]
                 addLocalFixities fix_env [name] $
                 thing_inside 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
      envs_before <- TcRn (GlobalRdrEnv, LocalRdrEnv)
    getRdrEnvs

      -- (1) rename the patterns, bringing into scope all of the term variables
      -- (2) then do the thing inside.
      unCpsRn (rn_pats_fun (matchNameMaker ctxt) pats) $ \ 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 { (n, _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)
                               ; return 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)
        tp' <- HsDocContext -> HsTyPat GhcPs -> CpsRn (HsTyPat GhcRn)
    rnHsTyPat HsDocContext
    HsTypePatCtx HsTyPat (NoGhcTc GhcPs)
    HsTyPat GhcPs
    tp
        pure (InvisPat noExtField 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 { pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
    rnLPatAndThen NameMaker
    mk LPat GhcPs
    pat
         ; return (ParPat noExtField pat') }
    rnPatAndThen NameMaker
    mk (LazyPat XLazyPat GhcPs
    _ LPat GhcPs
    pat) = do { pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
    rnLPatAndThen NameMaker
    mk LPat GhcPs
    pat
                                         ; return (LazyPat noExtField pat') }
    rnPatAndThen NameMaker
    mk (BangPat XBangPat GhcPs
    _ LPat GhcPs
    pat) = do { pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
    rnLPatAndThen NameMaker
    mk LPat GhcPs
    pat
                                         ; return (BangPat noExtField pat') }
    rnPatAndThen NameMaker
    mk (VarPat XVarPat GhcPs
    x (L SrcSpanAnnN
    l RdrName
    rdr))
        = do { loc <- RnM SrcSpan -> CpsRn SrcSpan
    forall a. RnM a -> CpsRn a
    liftCps RnM SrcSpan
    getSrcSpanM
             ; name <- newPatName mk (L (noAnnSrcSpan loc) rdr)
             ; return (VarPat x (L l 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 { sig' <- HsPatSigType GhcPs -> CpsRn (HsPatSigType GhcRn)
    rnHsPatSigTypeAndThen HsPatSigType (NoGhcTc GhcPs)
    HsPatSigType GhcPs
    sig
           ; pat' <- rnLPatAndThen mk pat
           ; return (SigPat noExtField pat' 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 { 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 ovlStr
             then rnPatAndThen mk
                               (mkNPat (noLocA (mkHsIsString src s))
                                          Nothing noAnn)
             else 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 { (lit', 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
           ; mb_neg' -- See Note [Negative zero]
               <- let negative = do { (neg, fvs) <- Name -> RnM (SyntaxExpr GhcRn, FreeVars)
    lookupSyntax Name
    negateName
                                    ; return (Just neg, fvs) }
                      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 liftCpsFV $ case (mb_neg , 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
           ; eq' <- liftCpsFV $ lookupSyntax eqName
           ; return (NPat x (L l lit') mb_neg' 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 { 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)
           ; (lit', _) <- liftCpsFV $ rnOverLit lit -- See Note [Negative zero]
                                                    -- We skip negateName as
                                                    -- negative zero doesn't make
                                                    -- sense in n + k patterns
           ; minus <- liftCpsFV $ lookupSyntax minusName
           ; ge    <- liftCpsFV $ lookupSyntax geName
           ; return (NPlusKPat noExtField (L (noAnnSrcSpan $ nameSrcSpan new_name) new_name)
                                          (L l lit') lit' ge 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 { new_name <- NameMaker
    -> GenLocated SrcSpanAnnN RdrName -> CpsRn (LocatedN Name)
    newPatLName NameMaker
    mk XRec GhcPs (IdP GhcPs)
    GenLocated SrcSpanAnnN RdrName
    rdr
           ; pat' <- rnLPatAndThen mk pat
           ; return (AsPat noExtField new_name 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 { vp_flag <- Extension -> RnM Bool
    forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
    xoptM Extension
    LangExt.ViewPatterns
                          ; checkErr vp_flag (TcRnIllegalViewPattern p) }
             -- Because of the way we're arranging the recursive calls,
             -- this will be in the right context
           ; 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
           ; pat' <- rnLPatAndThen mk 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'.
           ; return (ViewPat Nothing expr' 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 { 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 ol_flag then rnPatAndThen mk (ListPat noAnn [])
                                     else rnConPatAndThen mk con 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 { 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
           ; pats' <- rnLPatsAndThen mk pats
           ; if not opt_OverloadedLists
             then return (ListPat noExtField pats')
             else
        -- If OverloadedLists is enabled, desugar to a view pattern.
        -- See Note [Desugaring overloaded list patterns]
        do { (to_list_name,_)     <- liftCps $ lookupSyntaxName toListName
           -- Use 'fromList' as proof of invertibility of the view pattern.
           -- See Note [Invertible view patterns] in GHC.Tc.TyCl.PatSyn
           ; (from_list_n_name,_) <- liftCps $ lookupSyntaxName fromListNName
           ; let
               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  = IntegralLit -> GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)
    forall an. NoAnn an => IntegralLit -> LocatedAn an (HsExpr GhcRn)
    genHsIntegralLit IntegralLit
    lit_n
               inverse = Name -> [LHsExpr GhcRn] -> HsExpr GhcRn
    genHsApps Name
    from_list_n_name [LHsExpr GhcRn
    GenLocated (EpAnn AnnListItem) (HsExpr GhcRn)
    hs_lit]
               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     = Name -> LHsExpr GhcRn
    genLHsVar Name
    to_list_name
               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)
           ; return $ mkExpandedPat rn_list_pat exp_list_pat }}

    rnPatAndThen NameMaker
    mk (TuplePat XTuplePat GhcPs
    _ [LPat GhcPs]
    pats Boxity
    boxed)
      = do { pats' <- NameMaker -> [LPat GhcPs] -> CpsRn [LPat GhcRn]
    rnLPatsAndThen NameMaker
    mk [LPat GhcPs]
    pats
           ; return (TuplePat noExtField pats' boxed) }

    rnPatAndThen NameMaker
    mk (SumPat XSumPat GhcPs
    _ LPat GhcPs
    pat Int
    alt Int
    arity)
      = do { pat <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
    rnLPatAndThen NameMaker
    mk LPat GhcPs
    pat
           ; return (SumPat noExtField pat alt arity)
           }

    rnPatAndThen NameMaker
    mk (SplicePat XSplicePat GhcPs
    _ HsUntypedSplice GhcPs
    splice)
      = do { 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 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 { tp' <- HsDocContext -> HsTyPat GhcPs -> CpsRn (HsTyPat GhcRn)
    rnHsTyPat HsDocContext
    HsTypePatCtx HsTyPat (NoGhcTc GhcPs)
    HsTyPat GhcPs
    tp
           ; return (EmbTyPat noExtField 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
           ; tp' <- HsDocContext -> HsTyPat GhcPs -> CpsRn (HsTyPat GhcRn)
    rnHsTyPat HsDocContext
    HsTypePatCtx HsTyPat (NoGhcTc GhcPs)
    HsTyPat GhcPs
    tp
           ; return (InvisPat noExtField 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  { con' <- GenLocated SrcSpanAnnN RdrName -> CpsRn (LocatedN Name)
    lookupConCps GenLocated SrcSpanAnnN RdrName
    con
            ; liftCps check_lang_exts
            ; tyargs' <- mapM rnConPatTyArg tyargs
            ; pats' <- rnLPatsAndThen mk pats
            ; return $ ConPat
                { pat_con_ext = noExtField
                , pat_con = con'
                , pat_args = PrefixCon tyargs' 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 { type_abs   <- Extension -> RnM Bool
    forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
    xoptM Extension
    LangExt.TypeAbstractions
               ; type_app   <- xoptM LangExt.TypeApplications
               ; scoped_tvs <- xoptM LangExt.ScopedTypeVariables
               -- See Note [Deprecated type abstractions in constructor patterns]
               ; if | type_abs -> return ()
                    | type_app && scoped_tvs -> addDiagnostic TcRnDeprecatedInvisTyArgInConPat
                    | otherwise -> addErrTc $ TcRnTypeApplicationsDisabled (TypeApplicationInPattern arg)
               }

        rnConPatTyArg :: HsConPatTyArg GhcPs -> CpsRn (HsConPatTyArg GhcRn)
    rnConPatTyArg (HsConPatTyArg XConPatTyArg GhcPs
    _ HsTyPat GhcPs
    t) = do
          t' <- HsDocContext -> HsTyPat GhcPs -> CpsRn (HsTyPat GhcRn)
    rnHsTyPat HsDocContext
    HsTypePatCtx HsTyPat GhcPs
    t
          return (HsConPatTyArg noExtField t')

    rnConPatAndThen NameMaker
    mk GenLocated SrcSpanAnnN RdrName
    con (InfixCon LPat GhcPs
    pat1 LPat GhcPs
    pat2)
      = do  { con' <- GenLocated SrcSpanAnnN RdrName -> CpsRn (LocatedN Name)
    lookupConCps GenLocated SrcSpanAnnN RdrName
    con
            ; pat1' <- rnLPatAndThen mk pat1
            ; pat2' <- rnLPatAndThen mk pat2
            ; fixity <- liftCps $ lookupFixityRn (unLoc con')
            ; liftCps $ mkConOpPatRn con' fixity pat1' pat2' }

    rnConPatAndThen NameMaker
    mk GenLocated SrcSpanAnnN RdrName
    con (RecCon HsRecFields GhcPs (LPat GhcPs)
    rpats)
      = do  { con' <- GenLocated SrcSpanAnnN RdrName -> CpsRn (LocatedN Name)
    lookupConCps GenLocated SrcSpanAnnN RdrName
    con
            ; rpats' <- rnHsRecPatsAndThen mk con' rpats
            ; return $ ConPat
                { pat_con_ext = noExtField
                , pat_con = con'
                , pat_args = RecCon 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, fvs) <- () -> RnM (r, FreeVars)
    thing ()
                        checkUnusedRecordWildcard loc fvs dotdot_names
                        return (r, 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 { 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
           ; flds' <- mapM rn_field (flds `zip` [1..])
           ; check_unused_wildcard (lHsRecFieldsImplicits flds' <$> unLoc <$> dd)
           ; return (HsRecFields { rec_flds = flds', rec_dotdot = 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 { 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)
             ; return (L l (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 { pun_ok      <- Extension -> RnM Bool
    forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
    xoptM Extension
    LangExt.NamedFieldPuns
           ; disambig_ok <- xoptM LangExt.DisambiguateRecordFields
           ; let 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
           ; flds1  <- mapM (rn_fld pun_ok parent) flds
           ; mapM_ (addErr . dupFieldErr ctxt) dup_flds
           ; dotdot_flds <- rn_dotdot dotdot mb_con flds1
           ; let 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
           ; return (all_flds, mkFVs (getFieldIds 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 (XRec GhcRn (HsFieldBind (LFieldOcc 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 { 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 = OccName -> RdrName
    mkRdrUnqual (OccName -> RdrName) -> OccName -> RdrName
    forall a b. (a -> b) -> a -> b
    $ HasDebugCallStack => 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)
               ; arg' <- if pun
                         then do { checkErr pun_ok $
                                    TcRnIllegalFieldPunning (L (locA loc) arg_rdr)
                                 ; return $ L (l2l loc) $
                                     mk_arg (locA loc) arg_rdr }
                         else return arg
               ; return $ L l $
                   HsFieldBind
                     { hfbAnn = noAnn
                     , hfbLHS = L loc (FieldOcc sel (L ll arg_rdr))
                     , hfbRHS = arg'
                     , hfbPun = 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
    -> [XRec GhcRn (HsFieldBind (LFieldOcc GhcRn) (LocatedA arg))]
    -> RnM [XRec GhcRn (HsFieldBind (LFieldOcc GhcRn) (LocatedA arg))]
    rn_dotdot (Just (L EpaLocation
    loc_e (RecFieldsDotDot Int
    n))) (Just Name
    con) [XRec GhcRn (HsFieldBind (LFieldOcc 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 [XRec GhcRn (HsFieldBind (LFieldOcc GhcRn) (LocatedA arg))]
    -> RnM [XRec GhcRn (HsFieldBind (LFieldOcc GhcRn) (LocatedA arg))]
    forall a. HasCallStack => Bool -> a -> a
    assert ([XRec GhcRn (HsFieldBind (LFieldOcc 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 [XRec GhcRn (HsFieldBind (LFieldOcc GhcRn) (LocatedA arg))]
     -> RnM [XRec GhcRn (HsFieldBind (LFieldOcc GhcRn) (LocatedA arg))])
    -> RnM [XRec GhcRn (HsFieldBind (LFieldOcc GhcRn) (LocatedA arg))]
    -> RnM [XRec GhcRn (HsFieldBind (LFieldOcc GhcRn) (LocatedA arg))]
    forall a b. (a -> b) -> a -> b
    $
            do { dd_flag <- Extension -> RnM Bool
    forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
    xoptM Extension
    LangExt.RecordWildCards
               ; checkErr dd_flag (needFlagDotDot ctxt)
               ; (rdr_env, lcl_env) <- getRdrEnvs
               ; conInfo <- lookupConstructorInfo con
               ; when (conInfo == ConHasPositionalArgs) (addErr (TcRnIllegalWildcardsInConstructor con))
               ; let 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 ([XRec GhcRn (HsFieldBind (LFieldOcc GhcRn) (LocatedA arg))]
    -> [RdrName]
    forall p arg. UnXRec p => [LHsRecField p arg] -> [RdrName]
    getFieldLbls [XRec GhcRn (HsFieldBind (LFieldOcc 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
    lbl = OccName -> RdrName
    mkRdrUnqual OccName
    lbl RdrName -> LocalRdrEnv -> Bool
    `elemLocalRdrEnv` LocalRdrEnv
    lcl_env

                     (dot_dot_fields, dot_dot_gres) =
                       unzip [ (fl, gre)
                             | fl <- conInfoFields conInfo
                             , let lbl = HasDebugCallStack => 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
                             , not (lbl `elemOccSet` present_flds)
                             , Just gre <- [lookupGRE_FieldLabel rdr_env fl]
                                           -- Check selector is in scope
                             , case ctxt of
                                 HsRecFieldCon {} -> OccName -> Bool
    arg_in_scope OccName
    lbl
                                 HsRecFieldContext
    _other           -> Bool
    True ]

               ; addUsedGREs NoDeprecationWarnings dot_dot_gres
               ; let loc = EpaLocation -> SrcSpan
    forall a. HasLoc a => a -> SrcSpan
    locA EpaLocation
    loc_e
               ; let locn = SrcSpan -> EpAnn AnnListItem
    forall e. HasAnnotation e => SrcSpan -> e
    noAnnSrcSpan SrcSpan
    loc
               ; return [ L (noAnnSrcSpan loc) (HsFieldBind
                            { hfbAnn = noAnn
                            , hfbLHS
                               = L (noAnnSrcSpan loc) (FieldOcc sel (L (noAnnSrcSpan loc) arg_rdr))
                            , hfbRHS = L locn (mk_arg loc arg_rdr)
                            , hfbPun = False })
                        | fl <- dot_dot_fields
                        , let sel     = FieldLabel -> Name
    flSelector FieldLabel
    fl
                              arg_rdr = OccName -> RdrName
    mkRdrUnqual
                                      (OccName -> RdrName) -> OccName -> RdrName
    forall a b. (a -> b) -> a -> b
    $ HasDebugCallStack => 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 [XRec GhcRn (HsFieldBind (LFieldOcc 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 { 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 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]
           ; 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
                  (mb_unambig_lbls, fvs) =
                   case 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 HasDebugCallStack => 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.
            ; (upd_flds, fvs') <- rn_flds pun_ok mb_unambig_lbls flds
            ; let all_fvs = FreeVars
    fvs FreeVars -> FreeVars -> FreeVars
    `plusFV` FreeVars
    fvs'
            ; return (possible_parents, upd_flds, 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
                 ; (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
                 ; (arg'', fvs) <- rnLExpr arg'
                 ; let 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' = 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 })
                 ; (flds', fvs') <- rn_flds pun_ok (tail <$> mb_unambig_lbls) flds
                 ; return (fld' : flds', fvs `plusFV` fvs') }

    getFieldIds :: [LHsRecField GhcRn arg] -> [Name]
    getFieldIds :: forall arg. [LHsRecField GhcRn arg] -> [Name]
    getFieldIds [LHsRecField GhcRn arg]
    flds = (GenLocated (EpAnn AnnListItem) (HsFieldBind (LFieldOcc GhcRn) arg)
     -> Name)
    -> [GenLocated
          (EpAnn AnnListItem) (HsFieldBind (LFieldOcc GhcRn) arg)]
    -> [Name]
    forall a b. (a -> b) -> [a] -> [b]
    map (HsFieldBind (LFieldOcc GhcRn) arg -> XCFieldOcc GhcRn
    HsFieldBind (LFieldOcc GhcRn) arg -> Name
    forall p arg. UnXRec p => HsRecField p arg -> XCFieldOcc p
    hsRecFieldSel (HsFieldBind (LFieldOcc GhcRn) arg -> Name)
    -> (GenLocated
          (EpAnn AnnListItem) (HsFieldBind (LFieldOcc GhcRn) arg)
        -> HsFieldBind (LFieldOcc GhcRn) arg)
    -> GenLocated
         (EpAnn AnnListItem) (HsFieldBind (LFieldOcc GhcRn) arg)
    -> Name
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GenLocated (EpAnn AnnListItem) (HsFieldBind (LFieldOcc GhcRn) arg)
    -> HsFieldBind (LFieldOcc GhcRn) arg
    forall l e. GenLocated l e -> e
    unLoc) [LHsRecField GhcRn arg]
    [GenLocated
       (EpAnn AnnListItem) (HsFieldBind (LFieldOcc 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  { opt_NumDecimals <- Extension -> RnM Bool
    forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
    xoptM Extension
    LangExt.NumDecimals
            ; let { lit@(OverLit {ol_val=val})
                | opt_NumDecimals = origLit {ol_val = generalizeOverLitVal (ol_val origLit)}
                | otherwise       = origLit
              }
            ; let std_name = OverLitVal -> Name
    hsOverLitName OverLitVal
    val
            ; (from_thing_name, fvs1) <- lookupSyntaxName std_name
            ; loc <- getSrcSpanM -- See Note [Source locations for implicit function calls] in GHC.Iface.Ext.Ast
            ; let rebindable = Name
    from_thing_name Name -> Name -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Name
    std_name
                  lit' = HsOverLit t
    lit { ol_ext = OverLitRn { ol_rebindable = rebindable
                                                  , ol_from_fun = L (noAnnSrcSpan loc) from_thing_name } }
            ; if isNegativeZeroOverLit lit'
              then do { (negate_name, fvs2) <- lookupSyntaxExpr negateName
                      ; return ((lit' { ol_val = negateOverLitVal val }, Just negate_name)
                                      , fvs1 `plusFV` fvs2) }
              else return ((lit', Nothing), 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
        (hs_ty', 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
        pure HsTP
              { hstp_body = hs_ty'
              , hstp_ext = buildHsTyPatRn 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
      a' <- a -> TPRnM b
    fn a
    a
      pure (L loc 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 <- RdrName -> RnM Name
    lookupTypeOccRn RdrName
    rdr_name
      pure (name, unitFV 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
      hs_ty' <- HsType GhcPs -> TPRnM (HsType GhcRn)
    rn_ty_pat HsType GhcPs
    hs_ty
      pure (L l 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
      locals <- TPRnM OccSet
    askLocals
      if isRdrTyVar rdr
        && not (elemOccSet (occName rdr) locals) -- See Note [Locally bound names in type patterns]

        then do -- binder
          name <- liftTPRnCps $ newPatName (LamMk True) lrdr
          tellTPB (tpb_exp_tv name)
          pure (L l name)

        else do -- usage
          name <- lookupTypeOccTPRnM rdr
          pure (L l 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@(L _ name) <- GenLocated SrcSpanAnnN RdrName -> TPRnM (LocatedN Name)
    rn_ty_pat_var XRec GhcPs (IdP GhcPs)
    GenLocated SrcSpanAnnN RdrName
    lrdr
      when (isDataConName name && not (isKindName name)) $
        -- 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.
        check_data_kinds tv
      pure (HsTyVar an prom 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
      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
      body' <- rn_lty_pat body
      pure (HsQualTy an lctx' body')

    rn_ty_pat (HsAppTy XAppTy GhcPs
    _ LHsType GhcPs
    fun_ty LHsType GhcPs
    arg_ty) = do
      fun_ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
    rn_lty_pat LHsType GhcPs
    fun_ty
      arg_ty' <- rn_lty_pat arg_ty
      pure (HsAppTy noExtField fun_ty' arg_ty')

    rn_ty_pat (HsAppKindTy XAppKindTy GhcPs
    _ LHsType GhcPs
    ty LHsType GhcPs
    ki) = do
      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
      unless kind_app (liftRn $ addErr (typeAppErr KindLevel ki))
      ty' <- rn_lty_pat ty
      ki' <- rn_lty_pat ki
      pure (HsAppKindTy noExtField ty' ki')

    rn_ty_pat (HsFunTy XFunTy GhcPs
    an HsArrow GhcPs
    mult LHsType GhcPs
    lhs LHsType GhcPs
    rhs) = do
      lhs' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
    rn_lty_pat LHsType GhcPs
    lhs
      mult' <- rn_ty_pat_arrow mult
      rhs' <- rn_lty_pat rhs
      pure (HsFunTy an mult' lhs' rhs')

    rn_ty_pat (HsListTy XListTy GhcPs
    an LHsType GhcPs
    ty) = do
      ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
    rn_lty_pat LHsType GhcPs
    ty
      pure (HsListTy an ty')

    rn_ty_pat (HsTupleTy XTupleTy GhcPs
    an HsTupleSort
    con [LHsType GhcPs]
    tys) = do
      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
      pure (HsTupleTy an con tys')

    rn_ty_pat (HsSumTy XSumTy GhcPs
    an [LHsType GhcPs]
    tys) = do
      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
      pure (HsSumTy an tys')

    rn_ty_pat (HsOpTy XOpTy GhcPs
    _ PromotionFlag
    prom LHsType GhcPs
    ty1 XRec GhcPs (IdP GhcPs)
    l_op LHsType GhcPs
    ty2) = do
      ty1' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
    rn_lty_pat LHsType GhcPs
    ty1
      l_op' <- rn_ty_pat_var l_op
      ty2' <- rn_lty_pat ty2
      fix  <- liftRn $ lookupTyFixityRn l_op'
      let op_name = LocatedN Name -> Name
    forall l e. GenLocated l e -> e
    unLoc LocatedN Name
    l_op'
      when (isDataConName op_name && not (isPromoted prom)) $
        liftRn $ addDiagnostic (TcRnUntickedPromotedThing $ UntickedConstructor Infix op_name)
      liftRn $ mkHsOpTyRn prom l_op' fix ty1' ty2'

    rn_ty_pat (HsParTy XParTy GhcPs
    an LHsType GhcPs
    ty) = do
      ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
    rn_lty_pat LHsType GhcPs
    ty
      pure (HsParTy an ty')

    rn_ty_pat (HsIParamTy XIParamTy GhcPs
    an XRec GhcPs HsIPName
    n LHsType GhcPs
    ty) = do
      ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
    rn_lty_pat LHsType GhcPs
    ty
      pure (HsIParamTy an n 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
      ty' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
    rn_lty_pat LHsType GhcPs
    ty
      haddock_doc' <- liftRn $ rnLHsDoc haddock_doc
      pure (HsDocTy an ty' 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)

      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
      pure (HsExplicitListTy noExtField prom tys')

    rn_ty_pat ty :: HsType GhcPs
    ty@(HsExplicitTupleTy XExplicitTupleTy GhcPs
    _ [LHsType GhcPs]
    tys) = do
      HsType GhcPs -> TPRnM ()
    check_data_kinds HsType GhcPs
    ty
      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
      pure (HsExplicitTupleTy noExtField 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
      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
      pure (HsTyLit src 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
      ctxt <- TPRnM HsDocContext
    askDocContext
      kind_sigs_ok <- liftRn $ xoptM LangExt.KindSignatures
      unless kind_sigs_ok (liftRn $ badKindSigErr ctxt ki)
      ~(HsPS hsps ki') <- liftRnWithCont $
                          rnHsPatSigKind AlwaysBind ctxt (HsPS noAnn ki)
      ty' <- rn_lty_pat ty
      tellTPB (tpb_hsps hsps)
      pure (HsKindSig an ty' ki')

    rn_ty_pat (HsSpliceTy XSpliceTy GhcPs
    _ HsUntypedSplice GhcPs
    splice) = do
      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 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
            pat' <- LHsType GhcPs -> TPRnM (LHsType GhcRn)
    rn_lty_pat LHsType GhcPs
    GenLocated (EpAnn AnnListItem) (HsType GhcPs)
    pat
            pure (HsSpliceTy (HsUntypedSpliceTop mfs (mb_paren pat')) 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
      ctxt <- TPRnM HsDocContext
    askDocContext
      lty'@(L _ ty') <- rn_lty_pat lty
      liftRn $ addErr $
        TcRnWithHsDocContext ctxt $
        TcRnUnexpectedAnnotation ty' bang_src
      pure (HsBangTy an bang_src lty')

    rn_ty_pat ty :: HsType GhcPs
    ty@HsRecTy{} = do
      ctxt <- TPRnM HsDocContext
    askDocContext
      liftRn $ addErr $
        TcRnWithHsDocContext ctxt $
        TcRnIllegalRecordSyntax (Left ty)
      pure (HsWildCardTy noExtField) -- trick to avoid `failWithTc`

    rn_ty_pat ty :: HsType GhcPs
    ty@(XHsType{}) = do
      ctxt <- TPRnM HsDocContext
    askDocContext
      liftRnFV $ rnHsType ctxt 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
      data_kinds <- Extension -> RnM Bool
    forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
    xoptM Extension
    LangExt.DataKinds
      unless data_kinds $
        addErr $ TcRnDataKindsError TypeLevel $ Left 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.
    -}