    {-# LANGUAGE FlexibleContexts #-}
    {-# LANGUAGE LambdaCase #-}
    {-# LANGUAGE RankNTypes #-}
    {-# LANGUAGE ScopedTypeVariables #-}
    {-# LANGUAGE TupleSections #-}
    {-# LANGUAGE TypeFamilies #-}

    {-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}

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

    -}

    -- | Typechecking patterns
    module GHC.Tc.Gen.Pat
       ( tcLetPat
       , newLetBndr
       , LetBndrSpec(..)
       , tcCheckPat, tcCheckPat_O, tcInferPat
       , tcMatchPats
       , addDataConStupidTheta
       , isIrrefutableHsPatRnTcM
       )
    where

    import GHC.Prelude

    import {-# SOURCE #-}   GHC.Tc.Gen.Expr( tcSyntaxOp, tcSyntaxOpGen, tcInferRho )

    import GHC.Hs
    import GHC.Hs.Syn.Type
    import GHC.Rename.Utils
    import GHC.Tc.Errors.Types
    import GHC.Tc.Gen.Sig( TcPragEnv, lookupPragEnv, addInlinePrags )
    import GHC.Tc.Utils.Monad
    import GHC.Tc.Utils.Instantiate
    import GHC.Types.FieldLabel
    import GHC.Types.Id
    import GHC.Types.Var
    import GHC.Types.Name
    import GHC.Types.Name.Reader
    import GHC.Core.Multiplicity
    import GHC.Tc.Utils.Concrete ( hasFixedRuntimeRep_syntactic )
    import GHC.Tc.Utils.Env
    import GHC.Tc.Utils.TcMType
    import GHC.Tc.Zonk.TcType
    import GHC.Core.TyCo.Ppr ( pprTyVars )
    import GHC.Tc.Utils.TcType
    import GHC.Tc.Utils.Unify
    import GHC.Tc.Gen.HsType
    import GHC.Builtin.Types
    import GHC.Tc.Types.Evidence
    import GHC.Tc.Types.Origin
    import GHC.Core.TyCon
    import GHC.Core.Type
    import GHC.Core.Coercion
    import GHC.Core.DataCon
    import GHC.Core.PatSyn
    import GHC.Core.ConLike
    import GHC.Builtin.Names
    import GHC.Types.Basic hiding (SuccessFlag(..))
    import GHC.Driver.DynFlags
    import GHC.Types.SrcLoc
    import GHC.Types.Var.Set
    import GHC.Utils.Misc
    import GHC.Utils.Outputable as Outputable
    import GHC.Utils.Panic
    import qualified GHC.LanguageExtensions as LangExt
    import Control.Arrow  ( second )
    import Control.Monad
    import GHC.Data.FastString
    import qualified Data.List.NonEmpty as NE

    import GHC.Data.List.SetOps ( getNth )
    import Language.Haskell.Syntax.Basic (FieldLabelString(..))

    import Data.List( partition )
    import Data.Maybe (isJust)
    import Control.Monad.Trans.Writer.CPS
    import Control.Monad.Trans.Class

    {-
    ************************************************************************
    *                                                                      *
                    External interface
    *                                                                      *
    ************************************************************************
    -}

    tcLetPat :: (Name -> Maybe TcId)
             -> LetBndrSpec
             -> LPat GhcRn -> Scaled ExpSigmaTypeFRR
             -> TcM a
             -> TcM (LPat GhcTc, a)
    tcLetPat :: forall a.
    (Name -> Maybe TyCoVar)
    -> LetBndrSpec
    -> LPat (GhcPass 'Renamed)
    -> Scaled ExpSigmaTypeFRR
    -> TcM a
    -> TcM (LPat GhcTc, a)
    tcLetPat Name -> Maybe TyCoVar
    sig_fn LetBndrSpec
    no_gen LPat (GhcPass 'Renamed)
    pat Scaled ExpSigmaTypeFRR
    pat_ty TcM a
    thing_inside
      = do { bind_lvl <- TcM TcLevel
    getTcLevel
           ; let ctxt = LetPat { pc_lvl :: TcLevel
    pc_lvl    = TcLevel
    bind_lvl
                               , pc_sig_fn :: Name -> Maybe TyCoVar
    pc_sig_fn = Name -> Maybe TyCoVar
    sig_fn
                               , pc_new :: LetBndrSpec
    pc_new    = LetBndrSpec
    no_gen }
                 penv = PE { pe_lazy :: Bool
    pe_lazy = Bool
    True
                           , pe_ctxt :: PatCtxt
    pe_ctxt = PatCtxt
    ctxt
                           , pe_orig :: CtOrigin
    pe_orig = CtOrigin
    PatOrigin }
           ; dflags <- getDynFlags
           ; mult_co_wrap <- manyIfLazy dflags pat
           -- The wrapper checks for correct multiplicities.
           -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
           ; (pat', r) <- tc_lpat pat_ty penv pat thing_inside
           ; pat_ty' <- readExpType (scaledThing pat_ty)
           ; return (mkLHsWrapPat mult_co_wrap pat' pat_ty', r) }
      where
        -- The logic is partly duplicated from decideBangHood in
        -- GHC.HsToCore.Utils. Ugh…
        manyIfLazy :: DynFlags
    -> GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)) -> TcM HsWrapper
    manyIfLazy DynFlags
    dflags GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    lpat
          | Extension -> DynFlags -> Bool
    xopt Extension
    LangExt.Strict DynFlags
    dflags = GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)) -> TcM HsWrapper
    xstrict GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    lpat
          | Bool
    otherwise = GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)) -> TcM HsWrapper
    not_xstrict GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    lpat
          where
            xstrict :: GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)) -> TcM HsWrapper
    xstrict p :: GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    p@(L SrcSpanAnnA
    _ (LazyPat XLazyPat (GhcPass 'Renamed)
    _ LPat (GhcPass 'Renamed)
    _)) = NonLinearPatternReason
    -> LPat (GhcPass 'Renamed)
    -> Scaled ExpSigmaTypeFRR
    -> TcM HsWrapper
    forall a.
    NonLinearPatternReason
    -> LPat (GhcPass 'Renamed) -> Scaled a -> TcM HsWrapper
    checkManyPattern NonLinearPatternReason
    LazyPatternReason LPat (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    p Scaled ExpSigmaTypeFRR
    pat_ty
            xstrict (L SrcSpanAnnA
    _ (ParPat XParPat (GhcPass 'Renamed)
    _ LPat (GhcPass 'Renamed)
    p)) = GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)) -> TcM HsWrapper
    xstrict LPat (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    p
            xstrict GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    _ = HsWrapper -> TcM HsWrapper
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return HsWrapper
    WpHole

            not_xstrict :: GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)) -> TcM HsWrapper
    not_xstrict (L SrcSpanAnnA
    _ (BangPat XBangPat (GhcPass 'Renamed)
    _ LPat (GhcPass 'Renamed)
    _)) = HsWrapper -> TcM HsWrapper
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return HsWrapper
    WpHole
            not_xstrict (L SrcSpanAnnA
    _ (VarPat XVarPat (GhcPass 'Renamed)
    _ LIdP (GhcPass 'Renamed)
    _)) = HsWrapper -> TcM HsWrapper
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return HsWrapper
    WpHole
            not_xstrict (L SrcSpanAnnA
    _ (ParPat XParPat (GhcPass 'Renamed)
    _ LPat (GhcPass 'Renamed)
    p)) = GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)) -> TcM HsWrapper
    not_xstrict LPat (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    p
            not_xstrict GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    p = NonLinearPatternReason
    -> LPat (GhcPass 'Renamed)
    -> Scaled ExpSigmaTypeFRR
    -> TcM HsWrapper
    forall a.
    NonLinearPatternReason
    -> LPat (GhcPass 'Renamed) -> Scaled a -> TcM HsWrapper
    checkManyPattern NonLinearPatternReason
    LazyPatternReason LPat (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    p Scaled ExpSigmaTypeFRR
    pat_ty

    -----------------
    tcMatchPats :: forall a.
                   HsMatchContextRn
                -> [LPat GhcRn]          -- ^ patterns
                -> [ExpPatType]             -- ^ types of the patterns
                -> TcM a                    -- ^ checker for the body
                -> TcM ([LPat GhcTc], a)
    -- See Note [tcMatchPats]
    --
    -- PRECONDITION:
    --    number of visible pats::[LPat GhcRn]   (p is visible, @p is invisible)
    --      ==
    --    number of visible pat_tys::[ExpPatType]   (ExpFunPatTy is visible,
    --                                               ExpForAllPatTy b is visible iff b is Required)
    --
    -- POSTCONDITION:
    --   Returns only the /value/ patterns; see Note [tcMatchPats]

    tcMatchPats :: forall a.
    HsMatchContextRn
    -> [LPat (GhcPass 'Renamed)]
    -> [ExpPatType]
    -> TcM a
    -> TcM ([LPat GhcTc], a)
    tcMatchPats HsMatchContextRn
    match_ctxt [LPat (GhcPass 'Renamed)]
    pats [ExpPatType]
    pat_tys TcM a
    thing_inside
      = Bool -> SDoc -> TcM ([LPat GhcTc], a) -> TcM ([LPat GhcTc], a)
    forall a. HasCallStack => Bool -> SDoc -> a -> a
    assertPpr ((ExpPatType -> Bool) -> [ExpPatType] -> Int
    forall a. (a -> Bool) -> [a] -> Int
    count ExpPatType -> Bool
    isVisibleExpPatType [ExpPatType]
    pat_tys Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)) -> Bool)
    -> [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))] -> Int
    forall a. (a -> Bool) -> [a] -> Int
    count (Pat (GhcPass 'Renamed) -> Bool
    forall p. Pat p -> Bool
    isVisArgPat (Pat (GhcPass 'Renamed) -> Bool)
    -> (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
        -> Pat (GhcPass 'Renamed))
    -> GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    -> Pat (GhcPass 'Renamed)
    forall l e. GenLocated l e -> e
    unLoc) [LPat (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    pats)
                  ([GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [LPat (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    pats SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ [ExpPatType] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [ExpPatType]
    pat_tys) (TcM ([LPat GhcTc], a) -> TcM ([LPat GhcTc], a))
    -> TcM ([LPat GhcTc], a) -> TcM ([LPat GhcTc], a)
    forall a b. (a -> b) -> a -> b
    $
           -- Check PRECONDITION
           -- When we get @patterns the (length pats) will change
        do { err_ctxt <- TcM [ErrCtxt]
    getErrCtxt
           ; let loop :: [LPat GhcRn] -> [ExpPatType] -> TcM ([LPat GhcTc], a)

                 -- No more patterns.  Discard excess pat_tys;
                 -- they should all be invisible.  Example:
                 --    f :: Int -> forall a b. blah
                 --    f x @p = rhs
                 -- We will call tcMatchPats with
                 --   pats = [x, @p]
                 --   pat_tys = [Int, @a, @b]
                 loop [] [ExpPatType]
    pat_tys
                   = Bool -> SDoc -> TcM ([LPat GhcTc], a) -> TcM ([LPat GhcTc], a)
    forall a. HasCallStack => Bool -> SDoc -> a -> a
    assertPpr (Bool -> Bool
    not ((ExpPatType -> Bool) -> [ExpPatType] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any ExpPatType -> Bool
    isVisibleExpPatType [ExpPatType]
    pat_tys)) ([GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [LPat (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    pats SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ [ExpPatType] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [ExpPatType]
    pat_tys) (TcM ([LPat GhcTc], a) -> TcM ([LPat GhcTc], a))
    -> TcM ([LPat GhcTc], a) -> TcM ([LPat GhcTc], a)
    forall a b. (a -> b) -> a -> b
    $
                     do { res <- [ErrCtxt] -> TcM a -> TcM a
    forall a. [ErrCtxt] -> TcM a -> TcM a
    setErrCtxt [ErrCtxt]
    err_ctxt TcM a
    thing_inside
                        ; return ([], res) }

                 -- ExpForAllPat: expecting a type pattern
                 loop all_pats :: [LPat (GhcPass 'Renamed)]
    all_pats@(LPat (GhcPass 'Renamed)
    pat : [LPat (GhcPass 'Renamed)]
    pats) (ExpForAllPatTy (Bndr TyCoVar
    tv ForAllTyFlag
    vis) : [ExpPatType]
    pat_tys)
                   | ForAllTyFlag -> Bool
    isVisibleForAllTyFlag ForAllTyFlag
    vis
                   = do { (_p, (ps, res)) <- TyCoVar -> Checker (LPat (GhcPass 'Renamed)) (LPat GhcTc)
    tc_forall_lpat TyCoVar
    tv PatEnv
    penv LPat (GhcPass 'Renamed)
    pat (TcM ([LPat GhcTc], a) -> TcM (LPat GhcTc, ([LPat GhcTc], a)))
    -> TcM ([LPat GhcTc], a) -> TcM (LPat GhcTc, ([LPat GhcTc], a))
    forall a b. (a -> b) -> a -> b
    $
                                             [LPat (GhcPass 'Renamed)] -> [ExpPatType] -> TcM ([LPat GhcTc], a)
    loop [LPat (GhcPass 'Renamed)]
    pats [ExpPatType]
    pat_tys

                        ; return (ps, res) }
                        -- This VisPat is Erased.
                        -- See Note [Invisible binders in functions] in GHC.Hs.Pat

                   -- Invisible (Specified) forall in type, and an @a type pattern
                   -- E.g.    f :: forall a. Bool -> a -> blah
                   --         f @b True  x = rhs1  -- b is bound to skolem a
                   --         f @c False y = rhs2  -- c is bound to skolem a
                   | L SrcSpanAnnA
    _ (InvisPat XInvisPat (GhcPass 'Renamed)
    _ HsTyPat (NoGhcTc (GhcPass 'Renamed))
    tp) <- LPat (GhcPass 'Renamed)
    pat
                   , ForAllTyFlag -> Bool
    isSpecifiedForAllTyFlag ForAllTyFlag
    vis
                   = do { (_p, (ps, res)) <- HsTyPat (GhcPass 'Renamed)
    -> TyCoVar
    -> TcM ([LPat GhcTc], a)
    -> TcM (Type, ([LPat GhcTc], a))
    forall r.
    HsTyPat (GhcPass 'Renamed) -> TyCoVar -> TcM r -> TcM (Type, r)
    tc_ty_pat HsTyPat (NoGhcTc (GhcPass 'Renamed))
    HsTyPat (GhcPass 'Renamed)
    tp TyCoVar
    tv (TcM ([LPat GhcTc], a) -> TcM (Type, ([LPat GhcTc], a)))
    -> TcM ([LPat GhcTc], a) -> TcM (Type, ([LPat GhcTc], a))
    forall a b. (a -> b) -> a -> b
    $
                                             [LPat (GhcPass 'Renamed)] -> [ExpPatType] -> TcM ([LPat GhcTc], a)
    loop [LPat (GhcPass 'Renamed)]
    pats [ExpPatType]
    pat_tys
                        ; return (ps, res) }

                   | Bool
    otherwise  -- Discard invisible pat_ty
                   = [LPat (GhcPass 'Renamed)] -> [ExpPatType] -> TcM ([LPat GhcTc], a)
    loop [LPat (GhcPass 'Renamed)]
    all_pats [ExpPatType]
    pat_tys

                 -- This case handles the user error when an InvisPat is used
                 -- without a corresponding invisible (Specified) forall in the type
                 -- E.g. 1.  f :: Int
                 --          f @a = ...   -- loop (InvisPat{} : _) []
                 --      2.  f :: Int -> Int
                 --          f @a x = ... -- loop (InvisPat{} : _) (ExpFunPatTy{} : _)
                 --      3.  f :: forall a -> Int
                 --          f @a t = ... -- loop (InvisPat{} : _) (ExpForAllPatTy (Bndr _ Required) : _)
                 --      4.  f :: forall {a}. Int
                 --          f @a t = ... -- loop (InvisPat{} : _) (ExpForAllPatTy (Bndr _ Inferred) : _)
                 loop (L SrcSpanAnnA
    loc (InvisPat XInvisPat (GhcPass 'Renamed)
    _ HsTyPat (NoGhcTc (GhcPass 'Renamed))
    tp) : [LPat (GhcPass 'Renamed)]
    _) [ExpPatType]
    _ =
                    SrcSpan
    -> TcRnMessage
    -> IOEnv
         (Env TcGblEnv TcLclEnv) ([GenLocated SrcSpanAnnA (Pat GhcTc)], a)
    forall a. SrcSpan -> TcRnMessage -> TcRn a
    failAt (SrcSpanAnnA -> SrcSpan
    forall a. HasLoc a => a -> SrcSpan
    locA SrcSpanAnnA
    loc) (HsTyPat (GhcPass 'Renamed) -> TcRnMessage
    TcRnInvisPatWithNoForAll HsTyPat (NoGhcTc (GhcPass 'Renamed))
    HsTyPat (GhcPass 'Renamed)
    tp)

                 -- ExpFunPatTy: expecting a value pattern
                 -- tc_lpat will error if it sees a @t type pattern
                 loop (LPat (GhcPass 'Renamed)
    pat : [LPat (GhcPass 'Renamed)]
    pats) (ExpFunPatTy Scaled ExpSigmaTypeFRR
    pat_ty : [ExpPatType]
    pat_tys)
                   = do { (p, (ps, res)) <- Scaled ExpSigmaTypeFRR
    -> Checker (LPat (GhcPass 'Renamed)) (LPat GhcTc)
    tc_lpat Scaled ExpSigmaTypeFRR
    pat_ty PatEnv
    penv LPat (GhcPass 'Renamed)
    pat (TcM ([LPat GhcTc], a) -> TcM (LPat GhcTc, ([LPat GhcTc], a)))
    -> TcM ([LPat GhcTc], a) -> TcM (LPat GhcTc, ([LPat GhcTc], a))
    forall a b. (a -> b) -> a -> b
    $
                                            [LPat (GhcPass 'Renamed)] -> [ExpPatType] -> TcM ([LPat GhcTc], a)
    loop [LPat (GhcPass 'Renamed)]
    pats [ExpPatType]
    pat_tys
                        ; return (p : ps, res) }
                        -- This VisPat is Retained.
                        -- See Note [Invisible binders in functions] in GHC.Hs.Pat

                 loop pats :: [LPat (GhcPass 'Renamed)]
    pats@(LPat (GhcPass 'Renamed)
    _:[LPat (GhcPass 'Renamed)]
    _) [] = String
    -> SDoc
    -> IOEnv
         (Env TcGblEnv TcLclEnv) ([GenLocated SrcSpanAnnA (Pat GhcTc)], a)
    forall a. HasCallStack => String -> SDoc -> a
    pprPanic String
    "tcMatchPats" ([GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [LPat (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    pats)
                        -- Failure of PRECONDITION

           ; loop pats pat_tys }
      where
        penv :: PatEnv
    penv = PE { pe_lazy :: Bool
    pe_lazy = Bool
    False, pe_ctxt :: PatCtxt
    pe_ctxt = HsMatchContextRn -> PatCtxt
    LamPat HsMatchContextRn
    match_ctxt, pe_orig :: CtOrigin
    pe_orig = CtOrigin
    PatOrigin }

    tcInferPat :: FixedRuntimeRepContext
               -> HsMatchContextRn
               -> LPat GhcRn
               -> TcM a
               -> TcM ((LPat GhcTc, a), TcSigmaTypeFRR)
    tcInferPat :: forall a.
    FixedRuntimeRepContext
    -> HsMatchContextRn
    -> LPat (GhcPass 'Renamed)
    -> TcM a
    -> TcM ((LPat GhcTc, a), Type)
    tcInferPat FixedRuntimeRepContext
    frr_orig HsMatchContextRn
    ctxt LPat (GhcPass 'Renamed)
    pat TcM a
    thing_inside
      = FixedRuntimeRepContext
    -> (ExpSigmaTypeFRR -> TcM (LPat GhcTc, a))
    -> TcM ((LPat GhcTc, a), Type)
    forall a.
    FixedRuntimeRepContext
    -> (ExpSigmaTypeFRR -> TcM a) -> TcM (a, Type)
    tcInferFRR FixedRuntimeRepContext
    frr_orig ((ExpSigmaTypeFRR -> TcM (LPat GhcTc, a))
     -> TcM ((LPat GhcTc, a), Type))
    -> (ExpSigmaTypeFRR -> TcM (LPat GhcTc, a))
    -> TcM ((LPat GhcTc, a), Type)
    forall a b. (a -> b) -> a -> b
    $ \ ExpSigmaTypeFRR
    exp_ty ->
        Scaled ExpSigmaTypeFRR
    -> Checker (LPat (GhcPass 'Renamed)) (LPat GhcTc)
    tc_lpat (ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
    forall a. a -> Scaled a
    unrestricted ExpSigmaTypeFRR
    exp_ty) PatEnv
    penv LPat (GhcPass 'Renamed)
    pat TcM a
    thing_inside
     where
        penv :: PatEnv
    penv = PE { pe_lazy :: Bool
    pe_lazy = Bool
    False, pe_ctxt :: PatCtxt
    pe_ctxt = HsMatchContextRn -> PatCtxt
    LamPat HsMatchContextRn
    ctxt, pe_orig :: CtOrigin
    pe_orig = CtOrigin
    PatOrigin }

    tcCheckPat :: HsMatchContextRn
               -> LPat GhcRn -> Scaled TcSigmaTypeFRR
               -> TcM a                     -- Checker for body
               -> TcM (LPat GhcTc, a)
    tcCheckPat :: forall a.
    HsMatchContextRn
    -> LPat (GhcPass 'Renamed)
    -> Scaled Type
    -> TcM a
    -> TcM (LPat GhcTc, a)
    tcCheckPat HsMatchContextRn
    ctxt = HsMatchContextRn
    -> CtOrigin
    -> LPat (GhcPass 'Renamed)
    -> Scaled Type
    -> TcM a
    -> TcM (LPat GhcTc, a)
    forall a.
    HsMatchContextRn
    -> CtOrigin
    -> LPat (GhcPass 'Renamed)
    -> Scaled Type
    -> TcM a
    -> TcM (LPat GhcTc, a)
    tcCheckPat_O HsMatchContextRn
    ctxt CtOrigin
    PatOrigin

    -- | A variant of 'tcPat' that takes a custom origin
    tcCheckPat_O :: HsMatchContextRn
                 -> CtOrigin              -- ^ origin to use if the type needs inst'ing
                 -> LPat GhcRn -> Scaled TcSigmaTypeFRR
                 -> TcM a                 -- Checker for body
                 -> TcM (LPat GhcTc, a)
    tcCheckPat_O :: forall a.
    HsMatchContextRn
    -> CtOrigin
    -> LPat (GhcPass 'Renamed)
    -> Scaled Type
    -> TcM a
    -> TcM (LPat GhcTc, a)
    tcCheckPat_O HsMatchContextRn
    ctxt CtOrigin
    orig LPat (GhcPass 'Renamed)
    pat (Scaled Type
    pat_mult Type
    pat_ty) TcM a
    thing_inside
      = Scaled ExpSigmaTypeFRR
    -> Checker (LPat (GhcPass 'Renamed)) (LPat GhcTc)
    tc_lpat (Type -> ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
    forall a. Type -> a -> Scaled a
    Scaled Type
    pat_mult (Type -> ExpSigmaTypeFRR
    mkCheckExpType Type
    pat_ty)) PatEnv
    penv LPat (GhcPass 'Renamed)
    pat TcM a
    thing_inside
      where
        penv :: PatEnv
    penv = PE { pe_lazy :: Bool
    pe_lazy = Bool
    False, pe_ctxt :: PatCtxt
    pe_ctxt = HsMatchContextRn -> PatCtxt
    LamPat HsMatchContextRn
    ctxt, pe_orig :: CtOrigin
    pe_orig = CtOrigin
    orig }

    {- Note [tcMatchPats]
    ~~~~~~~~~~~~~~~~~~~~~
    tcMatchPats is the externally-callable wrapper function for
      function definitions  f p1 .. pn = rhs
      lambdas               \p1 .. pn -> body
    Typecheck the patterns, extend the environment to bind the variables, do the
    thing inside, use any existentially-bound dictionaries to discharge parts of
    the returning LIE, and deal with pattern type signatures

    It takes the list of patterns writen by the user, but it returns only the
    /value/ patterns.  For example:
         f :: forall a. forall b -> a -> Mabye b -> blah
         f @a w x (Just y) = ....
    tcMatchPats returns only the /value/ patterns [x, Just y].  Why?  The
    desugarer expects only value patterns.  (We could change that, but we would
    have to do so carefullly.)  However, distinguishing value patterns from type
    patterns is a bit tricky; e.g. the `w` in this example.  So it's very
    convenient to filter them out right here.

    ************************************************************************
    *                                                                      *
                    PatEnv, PatCtxt, LetBndrSpec
    *                                                                      *
    ************************************************************************
    -}

    data PatEnv
      = PE { PatEnv -> Bool
    pe_lazy :: Bool        -- True <=> lazy context, so no existentials allowed
           , PatEnv -> PatCtxt
    pe_ctxt :: PatCtxt     -- Context in which the whole pattern appears
           , PatEnv -> CtOrigin
    pe_orig :: CtOrigin    -- origin to use if the pat_ty needs inst'ing
           }

    data PatCtxt
      = LamPat   -- Used for lambdas, case etc
          HsMatchContextRn

      | LetPat   -- Used only for let(rec) pattern bindings
                 -- See Note [Typing patterns in pattern bindings]
           { PatCtxt -> TcLevel
    pc_lvl    :: TcLevel
                       -- Level of the binding group

           , PatCtxt -> Name -> Maybe TyCoVar
    pc_sig_fn :: Name -> Maybe TcId
                       -- Tells the expected type
                       -- for binders with a signature

           , PatCtxt -> LetBndrSpec
    pc_new :: LetBndrSpec
                    -- How to make a new binder
           }        -- for binders without signatures

    data LetBndrSpec
      = LetLclBndr            -- We are going to generalise, and wrap in an AbsBinds
                              -- so clone a fresh binder for the local monomorphic Id

      | LetGblBndr TcPragEnv  -- Generalisation plan is NoGen, so there isn't going
                              -- to be an AbsBinds; So we must bind the global version
                              -- of the binder right away.
                              -- And here is the inline-pragma information

    instance Outputable LetBndrSpec where
      ppr :: LetBndrSpec -> SDoc
    ppr LetBndrSpec
    LetLclBndr      = String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "LetLclBndr"
      ppr (LetGblBndr {}) = String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "LetGblBndr"

    makeLazy :: PatEnv -> PatEnv
    makeLazy :: PatEnv -> PatEnv
    makeLazy PatEnv
    penv = PatEnv
    penv { pe_lazy = True }

    inPatBind :: PatEnv -> Bool
    inPatBind :: PatEnv -> Bool
    inPatBind (PE { pe_ctxt :: PatEnv -> PatCtxt
    pe_ctxt = LetPat {} }) = Bool
    True
    inPatBind (PE { pe_ctxt :: PatEnv -> PatCtxt
    pe_ctxt = LamPat {} }) = Bool
    False

    {- *********************************************************************
    *                                                                      *
                    Binders
    *                                                                      *
    ********************************************************************* -}

    tcPatBndr :: PatEnv -> Name -> Scaled ExpSigmaTypeFRR -> TcM (HsWrapper, TcId)
    -- (coi, xp) = tcPatBndr penv x pat_ty
    -- Then coi : pat_ty ~ typeof(xp)
    --
    tcPatBndr :: PatEnv
    -> Name -> Scaled ExpSigmaTypeFRR -> TcM (HsWrapper, TyCoVar)
    tcPatBndr penv :: PatEnv
    penv@(PE { pe_ctxt :: PatEnv -> PatCtxt
    pe_ctxt = LetPat { pc_lvl :: PatCtxt -> TcLevel
    pc_lvl    = TcLevel
    bind_lvl
                                          , pc_sig_fn :: PatCtxt -> Name -> Maybe TyCoVar
    pc_sig_fn = Name -> Maybe TyCoVar
    sig_fn
                                          , pc_new :: PatCtxt -> LetBndrSpec
    pc_new    = LetBndrSpec
    no_gen } })
              Name
    bndr_name Scaled ExpSigmaTypeFRR
    exp_pat_ty
      -- For the LetPat cases, see
      -- Note [Typechecking pattern bindings] in GHC.Tc.Gen.Bind

      | Just TyCoVar
    bndr_id <- Name -> Maybe TyCoVar
    sig_fn Name
    bndr_name   -- There is a signature
      = do { wrap <- PatEnv -> ExpSigmaTypeFRR -> Type -> TcM HsWrapper
    tc_sub_type PatEnv
    penv (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
    forall a. Scaled a -> a
    scaledThing Scaled ExpSigmaTypeFRR
    exp_pat_ty) (TyCoVar -> Type
    idType TyCoVar
    bndr_id)
               -- See Note [Subsumption check at pattern variables]
           ; traceTc "tcPatBndr(sig)" (ppr bndr_id $$ ppr (idType bndr_id) $$ ppr exp_pat_ty)
           ; return (wrap, bndr_id) }

      | Bool
    otherwise                          -- No signature
      = do { (co, bndr_ty) <- case Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
    forall a. Scaled a -> a
    scaledThing Scaled ExpSigmaTypeFRR
    exp_pat_ty of
                 Check Type
    pat_ty    -> TcLevel
    -> Type -> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, Type)
    promoteTcType TcLevel
    bind_lvl Type
    pat_ty
                 Infer InferResult
    infer_res -> Bool
    -> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, Type)
    -> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, Type)
    forall a. HasCallStack => Bool -> a -> a
    assert (TcLevel
    bind_lvl TcLevel -> TcLevel -> Bool
    forall a. Eq a => a -> a -> Bool
    == InferResult -> TcLevel
    ir_lvl InferResult
    infer_res) (IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, Type)
     -> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, Type))
    -> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, Type)
    -> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, Type)
    forall a b. (a -> b) -> a -> b
    $
                                    -- If we were under a constructor that bumped the
                                    -- level, we'd be in checking mode (see tcConArg)
                                    -- hence this assertion
                                    do { bndr_ty <- InferResult -> IOEnv (Env TcGblEnv TcLclEnv) Type
    inferResultToType InferResult
    infer_res
                                       ; return (mkNomReflCo bndr_ty, bndr_ty) }
           ; let bndr_mult = Scaled ExpSigmaTypeFRR -> Type
    forall a. Scaled a -> Type
    scaledMult Scaled ExpSigmaTypeFRR
    exp_pat_ty
           ; bndr_id <- newLetBndr no_gen bndr_name bndr_mult bndr_ty
           ; traceTc "tcPatBndr(nosig)" (vcat [ ppr bind_lvl
                                              , ppr exp_pat_ty, ppr bndr_ty, ppr co
                                              , ppr bndr_id ])
           ; return (mkWpCastN co, bndr_id) }

    tcPatBndr PatEnv
    _ Name
    bndr_name Scaled ExpSigmaTypeFRR
    pat_ty
      = do { let pat_mult :: Type
    pat_mult = Scaled ExpSigmaTypeFRR -> Type
    forall a. Scaled a -> Type
    scaledMult Scaled ExpSigmaTypeFRR
    pat_ty
           ; pat_ty <- ExpSigmaTypeFRR -> IOEnv (Env TcGblEnv TcLclEnv) Type
    expTypeToType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
    forall a. Scaled a -> a
    scaledThing Scaled ExpSigmaTypeFRR
    pat_ty)
           ; traceTc "tcPatBndr(not let)" (ppr bndr_name $$ ppr pat_ty)
           ; return (idHsWrapper, mkLocalIdOrCoVar bndr_name pat_mult pat_ty) }
                   -- We should not have "OrCoVar" here, this is a bug (#17545)
                   -- Whether or not there is a sig is irrelevant,
                   -- as this is local

    newLetBndr :: LetBndrSpec -> Name -> Mult -> TcType -> TcM TcId
    -- Make up a suitable Id for the pattern-binder.
    -- See Note [Typechecking pattern bindings], item (4) in GHC.Tc.Gen.Bind
    --
    -- In the polymorphic case when we are going to generalise
    --    (plan InferGen, no_gen = LetLclBndr), generate a "monomorphic version"
    --    of the Id; the original name will be bound to the polymorphic version
    --    by the AbsBinds
    -- In the monomorphic case when we are not going to generalise
    --    (plan NoGen, no_gen = LetGblBndr) there is no AbsBinds,
    --    and we use the original name directly
    newLetBndr :: LetBndrSpec -> Name -> Type -> Type -> TcM TyCoVar
    newLetBndr LetBndrSpec
    LetLclBndr Name
    name Type
    w Type
    ty
      = do { mono_name <- Name -> TcM Name
    cloneLocalName Name
    name
           ; return (mkLocalId mono_name w ty) }
    newLetBndr (LetGblBndr TcPragEnv
    prags) Name
    name Type
    w Type
    ty
      = TyCoVar -> [LSig (GhcPass 'Renamed)] -> TcM TyCoVar
    addInlinePrags (HasDebugCallStack => Name -> Type -> Type -> TyCoVar
    Name -> Type -> Type -> TyCoVar
    mkLocalId Name
    name Type
    w Type
    ty) (TcPragEnv -> Name -> [LSig (GhcPass 'Renamed)]
    lookupPragEnv TcPragEnv
    prags Name
    name)

    tc_sub_type :: PatEnv -> ExpSigmaType -> TcSigmaType -> TcM HsWrapper
    -- tcSubTypeET with the UserTypeCtxt specialised to GenSigCtxt
    -- Used during typechecking patterns
    tc_sub_type :: PatEnv -> ExpSigmaTypeFRR -> Type -> TcM HsWrapper
    tc_sub_type PatEnv
    penv ExpSigmaTypeFRR
    t1 Type
    t2 = CtOrigin
    -> UserTypeCtxt -> ExpSigmaTypeFRR -> Type -> TcM HsWrapper
    tcSubTypePat (PatEnv -> CtOrigin
    pe_orig PatEnv
    penv) UserTypeCtxt
    GenSigCtxt ExpSigmaTypeFRR
    t1 Type
    t2

    {- Note [Subsumption check at pattern variables]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    When we come across a variable with a type signature, we need to do a
    subsumption, not equality, check against the context type.  e.g.

        data T = MkT (forall a. a->a)
          f :: forall b. [b]->[b]
          MkT f = blah

    Since 'blah' returns a value of type T, its payload is a polymorphic
    function of type (forall a. a->a).  And that's enough to bind the
    less-polymorphic function 'f', but we need some impedance matching
    to witness the instantiation.

    ************************************************************************
    *                                                                      *
                    The main worker functions
    *                                                                      *
    ************************************************************************

    Note [Nesting]
    ~~~~~~~~~~~~~~
    tcPat takes a "thing inside" over which the pattern scopes.  This is partly
    so that tcPat can extend the environment for the thing_inside, but also
    so that constraints arising in the thing_inside can be discharged by the
    pattern.

    This does not work so well for the ErrCtxt carried by the monad: we don't
    want the error-context for the pattern to scope over the RHS.
    Hence the getErrCtxt/setErrCtxt stuff in tcMultiple
    -}

    --------------------

    type Checker inp out =  forall r.
                              PatEnv
                           -> inp
                           -> TcM r      -- Thing inside
                           -> TcM ( out
                                  , r    -- Result of thing inside
                                  )

    tcMultiple_ :: Checker inp () -> PatEnv -> [inp] -> TcM r -> TcM r
    tcMultiple_ :: forall inp r. Checker inp () -> PatEnv -> [inp] -> TcM r -> TcM r
    tcMultiple_ Checker inp ()
    tc_pat PatEnv
    penv [inp]
    args TcM r
    thing_inside
      = do { (_, res) <- Checker inp () -> Checker [inp] [()]
    forall inp out. Checker inp out -> Checker [inp] [out]
    tcMultiple PatEnv -> inp -> TcM r -> TcM ((), r)
    Checker inp ()
    tc_pat PatEnv
    penv [inp]
    args TcM r
    thing_inside
           ; return res }

    tcMultiple :: Checker inp out -> Checker [inp] [out]
    tcMultiple :: forall inp out. Checker inp out -> Checker [inp] [out]
    tcMultiple Checker inp out
    tc_pat PatEnv
    penv [inp]
    args TcM r
    thing_inside
      = do  { err_ctxt <- TcM [ErrCtxt]
    getErrCtxt
            ; let loop []
                    = do { res <- TcM r
    thing_inside
                         ; return ([], res) }

                  loop (inp
    arg:[inp]
    args)
                    = do { (p', (ps', res))
                                    <- PatEnv -> inp -> TcM ([out], r) -> TcM (out, ([out], r))
    Checker inp out
    tc_pat PatEnv
    penv inp
    arg (TcM ([out], r) -> TcM (out, ([out], r)))
    -> TcM ([out], r) -> TcM (out, ([out], r))
    forall a b. (a -> b) -> a -> b
    $
                                       [ErrCtxt] -> TcM ([out], r) -> TcM ([out], r)
    forall a. [ErrCtxt] -> TcM a -> TcM a
    setErrCtxt [ErrCtxt]
    err_ctxt (TcM ([out], r) -> TcM ([out], r))
    -> TcM ([out], r) -> TcM ([out], r)
    forall a b. (a -> b) -> a -> b
    $
                                       [inp] -> TcM ([out], r)
    loop [inp]
    args
                    -- setErrCtxt: restore context before doing the next pattern
                    -- See Note [Nesting] above

                         ; return (p':ps', res) }

            ; loop args }

    --------------------
    tc_lpat :: Scaled ExpSigmaTypeFRR
            -> Checker (LPat GhcRn) (LPat GhcTc)
    tc_lpat :: Scaled ExpSigmaTypeFRR
    -> Checker (LPat (GhcPass 'Renamed)) (LPat GhcTc)
    tc_lpat Scaled ExpSigmaTypeFRR
    pat_ty PatEnv
    penv (L SrcSpanAnnA
    span Pat (GhcPass 'Renamed)
    pat) TcM r
    thing_inside
      = SrcSpanAnnA -> TcRn (LPat GhcTc, r) -> TcRn (LPat GhcTc, r)
    forall ann a. EpAnn ann -> TcRn a -> TcRn a
    setSrcSpanA SrcSpanAnnA
    span (TcRn (LPat GhcTc, r) -> TcRn (LPat GhcTc, r))
    -> TcRn (LPat GhcTc, r) -> TcRn (LPat GhcTc, r)
    forall a b. (a -> b) -> a -> b
    $
        do  { (pat', res) <- Pat (GhcPass 'Renamed)
    -> (TcM r -> TcM (Pat GhcTc, r)) -> TcM r -> TcM (Pat GhcTc, r)
    forall a b.
    Pat (GhcPass 'Renamed) -> (TcM a -> TcM b) -> TcM a -> TcM b
    maybeWrapPatCtxt Pat (GhcPass 'Renamed)
    pat (Scaled ExpSigmaTypeFRR
    -> Checker (Pat (GhcPass 'Renamed)) (Pat GhcTc)
    tc_pat Scaled ExpSigmaTypeFRR
    pat_ty PatEnv
    penv Pat (GhcPass 'Renamed)
    pat)
                                              TcM r
    thing_inside
            ; return (L span pat', res) }

    tc_lpats :: [Scaled ExpSigmaTypeFRR]
             -> Checker [LPat GhcRn] [LPat GhcTc]
    tc_lpats :: [Scaled ExpSigmaTypeFRR]
    -> Checker [LPat (GhcPass 'Renamed)] [LPat GhcTc]
    tc_lpats [Scaled ExpSigmaTypeFRR]
    tys PatEnv
    penv [LPat (GhcPass 'Renamed)]
    pats
      = Bool
    -> SDoc
    -> (TcM r -> TcM ([LPat GhcTc], r))
    -> TcM r
    -> TcM ([LPat GhcTc], r)
    forall a. HasCallStack => Bool -> SDoc -> a -> a
    assertPpr ([GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    -> [Scaled ExpSigmaTypeFRR] -> Bool
    forall a b. [a] -> [b] -> Bool
    equalLength [LPat (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    pats [Scaled ExpSigmaTypeFRR]
    tys) ([GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [LPat (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    pats SDoc -> SDoc -> SDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ [Scaled ExpSigmaTypeFRR] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [Scaled ExpSigmaTypeFRR]
    tys) ((TcM r -> TcM ([LPat GhcTc], r))
     -> TcM r -> TcM ([LPat GhcTc], r))
    -> (TcM r -> TcM ([LPat GhcTc], r))
    -> TcM r
    -> TcM ([LPat GhcTc], r)
    forall a b. (a -> b) -> a -> b
    $
        Checker
      (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)),
       Scaled ExpSigmaTypeFRR)
      (GenLocated SrcSpanAnnA (Pat GhcTc))
    -> Checker
         [(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)),
           Scaled ExpSigmaTypeFRR)]
         [GenLocated SrcSpanAnnA (Pat GhcTc)]
    forall inp out. Checker inp out -> Checker [inp] [out]
    tcMultiple (\ PatEnv
    penv' (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    p,Scaled ExpSigmaTypeFRR
    t) -> Scaled ExpSigmaTypeFRR
    -> Checker (LPat (GhcPass 'Renamed)) (LPat GhcTc)
    tc_lpat Scaled ExpSigmaTypeFRR
    t PatEnv
    penv' LPat (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    p)
                   PatEnv
    penv
                   (String
    -> [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    -> [Scaled ExpSigmaTypeFRR]
    -> [(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)),
         Scaled ExpSigmaTypeFRR)]
    forall a b. HasDebugCallStack => String -> [a] -> [b] -> [(a, b)]
    zipEqual String
    "tc_lpats" [LPat (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    pats [Scaled ExpSigmaTypeFRR]
    tys)

    --------------------
    -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
    checkManyPattern :: NonLinearPatternReason -> LPat GhcRn -> Scaled a -> TcM HsWrapper
    checkManyPattern :: forall a.
    NonLinearPatternReason
    -> LPat (GhcPass 'Renamed) -> Scaled a -> TcM HsWrapper
    checkManyPattern NonLinearPatternReason
    reason LPat (GhcPass 'Renamed)
    pat Scaled a
    pat_ty = CtOrigin -> Type -> Type -> TcM HsWrapper
    tcSubMult (NonLinearPatternReason -> LPat (GhcPass 'Renamed) -> CtOrigin
    NonLinearPatternOrigin NonLinearPatternReason
    reason LPat (GhcPass 'Renamed)
    pat) Type
    ManyTy (Scaled a -> Type
    forall a. Scaled a -> Type
    scaledMult Scaled a
    pat_ty)

    tc_forall_lpat :: TcTyVar -> Checker (LPat GhcRn) (LPat GhcTc)
    tc_forall_lpat :: TyCoVar -> Checker (LPat (GhcPass 'Renamed)) (LPat GhcTc)
    tc_forall_lpat TyCoVar
    tv PatEnv
    penv (L SrcSpanAnnA
    span Pat (GhcPass 'Renamed)
    pat) TcM r
    thing_inside
      = SrcSpanAnnA -> TcRn (LPat GhcTc, r) -> TcRn (LPat GhcTc, r)
    forall ann a. EpAnn ann -> TcRn a -> TcRn a
    setSrcSpanA SrcSpanAnnA
    span (TcRn (LPat GhcTc, r) -> TcRn (LPat GhcTc, r))
    -> TcRn (LPat GhcTc, r) -> TcRn (LPat GhcTc, r)
    forall a b. (a -> b) -> a -> b
    $
        do  { (pat', res) <- Pat (GhcPass 'Renamed)
    -> (TcM r -> TcM (Pat GhcTc, r)) -> TcM r -> TcM (Pat GhcTc, r)
    forall a b.
    Pat (GhcPass 'Renamed) -> (TcM a -> TcM b) -> TcM a -> TcM b
    maybeWrapPatCtxt Pat (GhcPass 'Renamed)
    pat (TyCoVar -> Checker (Pat (GhcPass 'Renamed)) (Pat GhcTc)
    tc_forall_pat TyCoVar
    tv PatEnv
    penv Pat (GhcPass 'Renamed)
    pat)
                                              TcM r
    thing_inside
            ; return (L span pat', res) }

    tc_forall_pat :: TcTyVar -> Checker (Pat GhcRn) (Pat GhcTc)
    tc_forall_pat :: TyCoVar -> Checker (Pat (GhcPass 'Renamed)) (Pat GhcTc)
    tc_forall_pat TyCoVar
    tv PatEnv
    penv (ParPat XParPat (GhcPass 'Renamed)
    x LPat (GhcPass 'Renamed)
    lpat) TcM r
    thing_inside
      = do { (lpat', res) <- TyCoVar -> Checker (LPat (GhcPass 'Renamed)) (LPat GhcTc)
    tc_forall_lpat TyCoVar
    tv PatEnv
    penv LPat (GhcPass 'Renamed)
    lpat TcM r
    thing_inside
           ; return (ParPat x lpat', res) }

    tc_forall_pat TyCoVar
    tv PatEnv
    _ (EmbTyPat XEmbTyPat (GhcPass 'Renamed)
    _ HsTyPat (NoGhcTc (GhcPass 'Renamed))
    tp) TcM r
    thing_inside
      -- The entire type pattern is guarded with the `type` herald:
      --    f (type t) (x :: t) = ...
      -- This special case is not necessary for correctness but avoids
      -- a redundant `ExpansionPat` node.
      = do { (arg_ty, result) <- HsTyPat (GhcPass 'Renamed) -> TyCoVar -> TcM r -> TcM (Type, r)
    forall r.
    HsTyPat (GhcPass 'Renamed) -> TyCoVar -> TcM r -> TcM (Type, r)
    tc_ty_pat HsTyPat (NoGhcTc (GhcPass 'Renamed))
    HsTyPat (GhcPass 'Renamed)
    tp TyCoVar
    tv TcM r
    thing_inside
           ; return (EmbTyPat arg_ty tp, result) }

    tc_forall_pat TyCoVar
    tv PatEnv
    _ Pat (GhcPass 'Renamed)
    pat TcM r
    thing_inside
      -- The type pattern is not guarded with the `type` herald, or perhaps
      -- only parts of it are, e.g.
      --    f (t :: Type)        (x :: t) = ...    -- no `type` herald
      --    f ((type t) :: Type) (x :: t) = ...    -- nested `type` herald
      -- Apply a recursive T2T transformation.
      = do { tp <- Pat (GhcPass 'Renamed) -> TcM (HsTyPat (GhcPass 'Renamed))
    pat_to_type_pat Pat (GhcPass 'Renamed)
    pat
           ; (arg_ty, result) <- tc_ty_pat tp tv thing_inside
           ; let pat' = XXPat GhcTc -> Pat GhcTc
    forall p. XXPat p -> Pat p
    XPat (XXPat GhcTc -> Pat GhcTc) -> XXPat GhcTc -> Pat GhcTc
    forall a b. (a -> b) -> a -> b
    $ Pat (GhcPass 'Renamed) -> Pat GhcTc -> XXPatGhcTc
    ExpansionPat Pat (GhcPass 'Renamed)
    pat (XEmbTyPat GhcTc -> HsTyPat (NoGhcTc GhcTc) -> Pat GhcTc
    forall p. XEmbTyPat p -> HsTyPat (NoGhcTc p) -> Pat p
    EmbTyPat XEmbTyPat GhcTc
    Type
    arg_ty HsTyPat (NoGhcTc GhcTc)
    HsTyPat (GhcPass 'Renamed)
    tp)
           ; return (pat', result) }

    -- Convert a Pat into the equivalent HsTyPat.
    -- See `expr_to_type` (GHC.Tc.Gen.App) for the HsExpr counterpart.
    -- The `TcM` monad is only used to fail on ill-formed type patterns.
    pat_to_type_pat :: Pat GhcRn -> TcM (HsTyPat GhcRn)
    pat_to_type_pat :: Pat (GhcPass 'Renamed) -> TcM (HsTyPat (GhcPass 'Renamed))
    pat_to_type_pat Pat (GhcPass 'Renamed)
    pat = do
      (ty, x) <- WriterT
      HsTyPatRnBuilder
      (IOEnv (Env TcGblEnv TcLclEnv))
      (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    -> IOEnv
         (Env TcGblEnv TcLclEnv)
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)),
          HsTyPatRnBuilder)
    forall w (m :: * -> *) a. Monoid w => WriterT w m a -> m (a, w)
    runWriterT (Pat (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (LHsType (GhcPass 'Renamed))
    pat_to_type Pat (GhcPass 'Renamed)
    pat)
      pure (HsTP (buildHsTyPatRn x) ty)

    pat_to_type :: Pat GhcRn -> WriterT HsTyPatRnBuilder TcM (LHsType GhcRn)
    pat_to_type :: Pat (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (LHsType (GhcPass 'Renamed))
    pat_to_type (EmbTyPat XEmbTyPat (GhcPass 'Renamed)
    _ (HsTP XHsTP (NoGhcTc (GhcPass 'Renamed))
    x LHsType (NoGhcTc (GhcPass 'Renamed))
    t)) =
      do { HsTyPatRnBuilder
    -> WriterT HsTyPatRnBuilder (IOEnv (Env TcGblEnv TcLclEnv)) ()
    forall w (m :: * -> *). (Monoid w, Monad m) => w -> WriterT w m ()
    tell (HsTyPatRn -> HsTyPatRnBuilder
    builderFromHsTyPatRn XHsTP (NoGhcTc (GhcPass 'Renamed))
    HsTyPatRn
    x)
         ; GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    forall a.
    a -> WriterT HsTyPatRnBuilder (IOEnv (Env TcGblEnv TcLclEnv)) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return LHsType (NoGhcTc (GhcPass 'Renamed))
    GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    t }
    pat_to_type (VarPat XVarPat (GhcPass 'Renamed)
    _ LIdP (GhcPass 'Renamed)
    lname)  =
      do { HsTyPatRnBuilder
    -> WriterT HsTyPatRnBuilder (IOEnv (Env TcGblEnv TcLclEnv)) ()
    forall w (m :: * -> *). (Monoid w, Monad m) => w -> WriterT w m ()
    tell (Name -> HsTyPatRnBuilder
    tpBuilderExplicitTV (GenLocated SrcSpanAnnN Name -> Name
    forall l e. GenLocated l e -> e
    unLoc LIdP (GhcPass 'Renamed)
    GenLocated SrcSpanAnnN Name
    lname))
         ; GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    forall a.
    a -> WriterT HsTyPatRnBuilder (IOEnv (Env TcGblEnv TcLclEnv)) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    b }
      where b :: GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    b = HsType (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (XTyVar (GhcPass 'Renamed)
    -> PromotionFlag
    -> LIdP (GhcPass 'Renamed)
    -> HsType (GhcPass 'Renamed)
    forall pass.
    XTyVar pass -> PromotionFlag -> LIdP pass -> HsType pass
    HsTyVar XTyVar (GhcPass 'Renamed)
    forall a. NoAnn a => a
    noAnn PromotionFlag
    NotPromoted LIdP (GhcPass 'Renamed)
    lname)
    pat_to_type (WildPat XWildPat (GhcPass 'Renamed)
    _) = GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    forall a.
    a -> WriterT HsTyPatRnBuilder (IOEnv (Env TcGblEnv TcLclEnv)) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    b
      where b :: GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    b = HsType (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (XWildCardTy (GhcPass 'Renamed) -> HsType (GhcPass 'Renamed)
    forall pass. XWildCardTy pass -> HsType pass
    HsWildCardTy XWildCardTy (GhcPass 'Renamed)
    NoExtField
    noExtField)
    pat_to_type (SigPat XSigPat (GhcPass 'Renamed)
    _ LPat (GhcPass 'Renamed)
    pat HsPatSigType (NoGhcTc (GhcPass 'Renamed))
    sig_ty)
      = do { t <- Pat (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (LHsType (GhcPass 'Renamed))
    pat_to_type (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    -> Pat (GhcPass 'Renamed)
    forall l e. GenLocated l e -> e
    unLoc LPat (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    pat)
           ; let { !(HsPS x_hsps k) = sig_ty
                 ; b = HsType (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (XKindSig (GhcPass 'Renamed)
    -> LHsType (GhcPass 'Renamed)
    -> LHsType (GhcPass 'Renamed)
    -> HsType (GhcPass 'Renamed)
    forall pass.
    XKindSig pass -> LHsType pass -> LHsType pass -> HsType pass
    HsKindSig [AddEpAnn]
    XKindSig (GhcPass 'Renamed)
    forall a. NoAnn a => a
    noAnn LHsType (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    t LHsType (NoGhcTc (GhcPass 'Renamed))
    LHsType (GhcPass 'Renamed)
    k) }
           ; tell (tpBuilderPatSig x_hsps)
           ; return b }
    pat_to_type (ParPat XParPat (GhcPass 'Renamed)
    _ LPat (GhcPass 'Renamed)
    pat)
      = do { t <- Pat (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (LHsType (GhcPass 'Renamed))
    pat_to_type (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    -> Pat (GhcPass 'Renamed)
    forall l e. GenLocated l e -> e
    unLoc LPat (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    pat)
           ; return (noLocA (HsParTy noAnn t)) }
    pat_to_type (SplicePat (HsUntypedSpliceTop ThModFinalizers
    mod_finalizers Pat (GhcPass 'Renamed)
    pat) HsUntypedSplice (GhcPass 'Renamed)
    splice) = do
          { t <- Pat (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (LHsType (GhcPass 'Renamed))
    pat_to_type Pat (GhcPass 'Renamed)
    pat
          ; return (noLocA (HsSpliceTy (HsUntypedSpliceTop mod_finalizers t) splice)) }

    pat_to_type (TuplePat XTuplePat (GhcPass 'Renamed)
    _ [LPat (GhcPass 'Renamed)]
    pats Boxity
    Boxed)
      = do { tys <- (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
     -> WriterT
          HsTyPatRnBuilder
          (IOEnv (Env TcGblEnv TcLclEnv))
          (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))))
    -> [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         [GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))]
    forall (t :: * -> *) (f :: * -> *) a b.
    (Traversable t, Applicative f) =>
    (a -> f b) -> t a -> f (t b)
    forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> [a] -> f [b]
    traverse (Pat (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (LHsType (GhcPass 'Renamed))
    Pat (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    pat_to_type (Pat (GhcPass 'Renamed)
     -> WriterT
          HsTyPatRnBuilder
          (IOEnv (Env TcGblEnv TcLclEnv))
          (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))))
    -> (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
        -> Pat (GhcPass 'Renamed))
    -> GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    -> Pat (GhcPass 'Renamed)
    forall l e. GenLocated l e -> e
    unLoc) [LPat (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    pats
           ; let t = HsType (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (XExplicitTupleTy (GhcPass 'Renamed)
    -> [LHsType (GhcPass 'Renamed)] -> HsType (GhcPass 'Renamed)
    forall pass. XExplicitTupleTy pass -> [LHsType pass] -> HsType pass
    HsExplicitTupleTy XExplicitTupleTy (GhcPass 'Renamed)
    NoExtField
    noExtField [LHsType (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))]
    tys)
           ; pure t }
    pat_to_type (ListPat XListPat (GhcPass 'Renamed)
    _ [LPat (GhcPass 'Renamed)]
    pats)
      = do { tys <- (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
     -> WriterT
          HsTyPatRnBuilder
          (IOEnv (Env TcGblEnv TcLclEnv))
          (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))))
    -> [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         [GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))]
    forall (t :: * -> *) (f :: * -> *) a b.
    (Traversable t, Applicative f) =>
    (a -> f b) -> t a -> f (t b)
    forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> [a] -> f [b]
    traverse (Pat (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (LHsType (GhcPass 'Renamed))
    Pat (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    pat_to_type (Pat (GhcPass 'Renamed)
     -> WriterT
          HsTyPatRnBuilder
          (IOEnv (Env TcGblEnv TcLclEnv))
          (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))))
    -> (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
        -> Pat (GhcPass 'Renamed))
    -> GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    -> Pat (GhcPass 'Renamed)
    forall l e. GenLocated l e -> e
    unLoc) [LPat (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    pats
           ; let t = HsType (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (XExplicitListTy (GhcPass 'Renamed)
    -> PromotionFlag
    -> [LHsType (GhcPass 'Renamed)]
    -> HsType (GhcPass 'Renamed)
    forall pass.
    XExplicitListTy pass
    -> PromotionFlag -> [LHsType pass] -> HsType pass
    HsExplicitListTy XExplicitListTy (GhcPass 'Renamed)
    NoExtField
    NoExtField PromotionFlag
    NotPromoted [LHsType (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))]
    tys)
           ; pure t }

    pat_to_type (LitPat XLitPat (GhcPass 'Renamed)
    _ HsLit (GhcPass 'Renamed)
    lit)
      | Just HsTyLit (GhcPass 'Renamed)
    ty_lit <- HsLit (GhcPass 'Renamed) -> Maybe (HsTyLit (GhcPass 'Renamed))
    tyLitFromLit HsLit (GhcPass 'Renamed)
    lit
      = do { let t :: GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    t = HsType (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (XTyLit (GhcPass 'Renamed)
    -> HsTyLit (GhcPass 'Renamed) -> HsType (GhcPass 'Renamed)
    forall pass. XTyLit pass -> HsTyLit pass -> HsType pass
    HsTyLit XTyLit (GhcPass 'Renamed)
    NoExtField
    noExtField HsTyLit (GhcPass 'Renamed)
    ty_lit)
          ; GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    forall a.
    a -> WriterT HsTyPatRnBuilder (IOEnv (Env TcGblEnv TcLclEnv)) a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    t }
    pat_to_type (NPat XNPat (GhcPass 'Renamed)
    _ (L EpAnnCO
    _ HsOverLit (GhcPass 'Renamed)
    lit) Maybe (SyntaxExpr (GhcPass 'Renamed))
    _ SyntaxExpr (GhcPass 'Renamed)
    _)
      | Just HsTyLit (GhcPass 'Renamed)
    ty_lit <- OverLitVal -> Maybe (HsTyLit (GhcPass 'Renamed))
    tyLitFromOverloadedLit (HsOverLit (GhcPass 'Renamed) -> OverLitVal
    forall p. HsOverLit p -> OverLitVal
    ol_val HsOverLit (GhcPass 'Renamed)
    lit)
      = do { let t :: GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    t = HsType (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (XTyLit (GhcPass 'Renamed)
    -> HsTyLit (GhcPass 'Renamed) -> HsType (GhcPass 'Renamed)
    forall pass. XTyLit pass -> HsTyLit pass -> HsType pass
    HsTyLit XTyLit (GhcPass 'Renamed)
    NoExtField
    noExtField HsTyLit (GhcPass 'Renamed)
    ty_lit)
           ; GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    forall a.
    a -> WriterT HsTyPatRnBuilder (IOEnv (Env TcGblEnv TcLclEnv)) a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    t}

    pat_to_type (ConPat XConPat (GhcPass 'Renamed)
    _ XRec (GhcPass 'Renamed) (ConLikeP (GhcPass 'Renamed))
    lname (InfixCon LPat (GhcPass 'Renamed)
    left LPat (GhcPass 'Renamed)
    right))
      = do { lty <- Pat (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (LHsType (GhcPass 'Renamed))
    pat_to_type (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    -> Pat (GhcPass 'Renamed)
    forall l e. GenLocated l e -> e
    unLoc LPat (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    left)
           ; rty <- pat_to_type (unLoc right)
           ; let { t = HsType (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (XOpTy (GhcPass 'Renamed)
    -> PromotionFlag
    -> LHsType (GhcPass 'Renamed)
    -> LIdP (GhcPass 'Renamed)
    -> LHsType (GhcPass 'Renamed)
    -> HsType (GhcPass 'Renamed)
    forall pass.
    XOpTy pass
    -> PromotionFlag
    -> LHsType pass
    -> LIdP pass
    -> LHsType pass
    -> HsType pass
    HsOpTy [AddEpAnn]
    XOpTy (GhcPass 'Renamed)
    forall a. NoAnn a => a
    noAnn PromotionFlag
    NotPromoted LHsType (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    lty LIdP (GhcPass 'Renamed)
    XRec (GhcPass 'Renamed) (ConLikeP (GhcPass 'Renamed))
    lname LHsType (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    rty)}
           ; pure t }
    pat_to_type (ConPat XConPat (GhcPass 'Renamed)
    _ XRec (GhcPass 'Renamed) (ConLikeP (GhcPass 'Renamed))
    lname (PrefixCon [HsConPatTyArg (NoGhcTc (GhcPass 'Renamed))]
    invis_args [LPat (GhcPass 'Renamed)]
    vis_args))
      = do { let { appHead :: GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    appHead = HsType (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA (XTyVar (GhcPass 'Renamed)
    -> PromotionFlag
    -> LIdP (GhcPass 'Renamed)
    -> HsType (GhcPass 'Renamed)
    forall pass.
    XTyVar pass -> PromotionFlag -> LIdP pass -> HsType pass
    HsTyVar XTyVar (GhcPass 'Renamed)
    forall a. NoAnn a => a
    noAnn PromotionFlag
    NotPromoted LIdP (GhcPass 'Renamed)
    XRec (GhcPass 'Renamed) (ConLikeP (GhcPass 'Renamed))
    lname)}
           ; ty_invis <- (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
     -> HsConPatTyArg (GhcPass 'Renamed)
     -> WriterT
          HsTyPatRnBuilder
          (IOEnv (Env TcGblEnv TcLclEnv))
          (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))))
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    -> [HsConPatTyArg (GhcPass 'Renamed)]
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    forall (t :: * -> *) (m :: * -> *) b a.
    (Foldable t, Monad m) =>
    (b -> a -> m b) -> b -> t a -> m b
    foldM LHsType (GhcPass 'Renamed)
    -> HsConPatTyArg (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (LHsType (GhcPass 'Renamed))
    GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    -> HsConPatTyArg (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    apply_invis_arg GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    appHead [HsConPatTyArg (NoGhcTc (GhcPass 'Renamed))]
    [HsConPatTyArg (GhcPass 'Renamed)]
    invis_args
           ; tys_vis <- traverse (pat_to_type . unLoc) vis_args
           ; let t = (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
     -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
     -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    -> [GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))]
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    forall b a. (b -> a -> b) -> b -> [a] -> b
    forall (t :: * -> *) b a.
    Foldable t =>
    (b -> a -> b) -> b -> t a -> b
    foldl' LHsType (GhcPass 'Renamed)
    -> LHsType (GhcPass 'Renamed) -> LHsType (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    -> GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    forall (p :: Pass).
    LHsType (GhcPass p) -> LHsType (GhcPass p) -> LHsType (GhcPass p)
    mkHsAppTy GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    ty_invis [GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))]
    tys_vis
           ; pure t }
          where
            apply_invis_arg :: LHsType GhcRn -> HsConPatTyArg GhcRn -> WriterT HsTyPatRnBuilder TcM (LHsType GhcRn)
            apply_invis_arg :: LHsType (GhcPass 'Renamed)
    -> HsConPatTyArg (GhcPass 'Renamed)
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (LHsType (GhcPass 'Renamed))
    apply_invis_arg !LHsType (GhcPass 'Renamed)
    t (HsConPatTyArg XConPatTyArg (GhcPass 'Renamed)
    _ (HsTP XHsTP (GhcPass 'Renamed)
    argx LHsType (GhcPass 'Renamed)
    arg))
              = do { HsTyPatRnBuilder
    -> WriterT HsTyPatRnBuilder (IOEnv (Env TcGblEnv TcLclEnv)) ()
    forall w (m :: * -> *). (Monoid w, Monad m) => w -> WriterT w m ()
    tell (HsTyPatRn -> HsTyPatRnBuilder
    builderFromHsTyPatRn XHsTP (GhcPass 'Renamed)
    HsTyPatRn
    argx)
                   ; GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed))
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (GenLocated SrcSpanAnnA (HsType (GhcPass 'Renamed)))
    forall a.
    a -> WriterT HsTyPatRnBuilder (IOEnv (Env TcGblEnv TcLclEnv)) a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (XAppKindTy (GhcPass 'Renamed)
    -> LHsType (GhcPass 'Renamed)
    -> LHsType (GhcPass 'Renamed)
    -> LHsType (GhcPass 'Renamed)
    forall (p :: Pass).
    XAppKindTy (GhcPass p)
    -> LHsType (GhcPass p)
    -> LHsType (GhcPass p)
    -> LHsType (GhcPass p)
    mkHsAppKindTy XAppKindTy (GhcPass 'Renamed)
    NoExtField
    noExtField LHsType (GhcPass 'Renamed)
    t LHsType (GhcPass 'Renamed)
    arg)}

    pat_to_type Pat (GhcPass 'Renamed)
    pat = TcM (LHsType (GhcPass 'Renamed))
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (LHsType (GhcPass 'Renamed))
    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 (TcM (LHsType (GhcPass 'Renamed))
     -> WriterT
          HsTyPatRnBuilder
          (IOEnv (Env TcGblEnv TcLclEnv))
          (LHsType (GhcPass 'Renamed)))
    -> TcM (LHsType (GhcPass 'Renamed))
    -> WriterT
         HsTyPatRnBuilder
         (IOEnv (Env TcGblEnv TcLclEnv))
         (LHsType (GhcPass 'Renamed))
    forall a b. (a -> b) -> a -> b
    $
      TcRnMessage -> TcM (LHsType (GhcPass 'Renamed))
    forall a. TcRnMessage -> TcRn a
    failWith (TcRnMessage -> TcM (LHsType (GhcPass 'Renamed)))
    -> TcRnMessage -> TcM (LHsType (GhcPass 'Renamed))
    forall a b. (a -> b) -> a -> b
    $ Pat (GhcPass 'Renamed) -> TcRnMessage
    TcRnIllformedTypePattern Pat (GhcPass 'Renamed)
    pat
      -- This failure is the only use of the TcM monad in `pat_to_type_pat`

    {-
    Note [Pattern to type (P2T) conversion]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider this:

      data T a b where
        MkT :: forall a. forall b -> a -> b -> T a b
        -- NB: `a` is invisible, but `b` is required

      f (MkT @[Int] (Maybe Bool) x y) = ...

    The second type argument of `MkT` is Required, so we write it without
    an `@` sign in the pattern match.  So the (Maybe Bool) will be
      * parsed and renamed as a term pattern
      * converted to a type when typechecking the pattern-match: the P2T conversion

    This is the only place we have P2T. In type-lambdas, the "pattern" is always a
    type variable:

       f :: forall a -> a -> blah
       f b (x::b) = ...

    The `b` argument must be a simple variable; we can't pattern-match on types.

    The function `pat_to_type` does the P2T conversion:
       pat_to_type :: Pat GhcRn -> WriterT HsTyPatRnBuilder TcM (LHsType GhcRn)

    It is arranged as a writer monad, where the `HsTyPatRnBuilder` accumulates the
    binders bound by the type.  (We could discover these binders by a subsequent
    traversal, that would mean writing another traversal.)
    -}

    tc_ty_pat :: HsTyPat GhcRn -> TcTyVar -> TcM r -> TcM (TcType, r)
    tc_ty_pat :: forall r.
    HsTyPat (GhcPass 'Renamed) -> TyCoVar -> TcM r -> TcM (Type, r)
    tc_ty_pat HsTyPat (GhcPass 'Renamed)
    tp TyCoVar
    tv TcM r
    thing_inside
      = do { (sig_wcs, sig_ibs, arg_ty) <- HsTyPat (GhcPass 'Renamed)
    -> Type -> TcM ([(Name, TyCoVar)], [(Name, TyCoVar)], Type)
    tcHsTyPat HsTyPat (GhcPass 'Renamed)
    tp (TyCoVar -> Type
    varType TyCoVar
    tv)
           ; _ <- unifyType Nothing arg_ty (mkTyVarTy tv)
           ; result <- tcExtendNameTyVarEnv sig_wcs $
                       tcExtendNameTyVarEnv sig_ibs $
                       thing_inside
           ; return (arg_ty, result) }

    tc_pat  :: Scaled ExpSigmaTypeFRR
            -- ^ Fully refined result type
            -> Checker (Pat GhcRn) (Pat GhcTc)
            -- ^ Translated pattern

    tc_pat :: Scaled ExpSigmaTypeFRR
    -> Checker (Pat (GhcPass 'Renamed)) (Pat GhcTc)
    tc_pat Scaled ExpSigmaTypeFRR
    pat_ty PatEnv
    penv Pat (GhcPass 'Renamed)
    ps_pat TcM r
    thing_inside = case Pat (GhcPass 'Renamed)
    ps_pat of

      VarPat XVarPat (GhcPass 'Renamed)
    x (L SrcSpanAnnN
    l Name
    name) -> do
            { (wrap, id) <- PatEnv
    -> Name -> Scaled ExpSigmaTypeFRR -> TcM (HsWrapper, TyCoVar)
    tcPatBndr PatEnv
    penv Name
    name Scaled ExpSigmaTypeFRR
    pat_ty
            ; (res, mult_wrap) <- tcCheckUsage name (scaledMult pat_ty) $
                                  tcExtendIdEnv1 name id thing_inside
                -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
            ; pat_ty <- readExpType (scaledThing pat_ty)
            ; return (mkHsWrapPat (wrap <.> mult_wrap) (VarPat x (L l id)) pat_ty, res) }

      ParPat XParPat (GhcPass 'Renamed)
    x LPat (GhcPass 'Renamed)
    pat -> do
            { (pat', res) <- Scaled ExpSigmaTypeFRR
    -> Checker (LPat (GhcPass 'Renamed)) (LPat GhcTc)
    tc_lpat Scaled ExpSigmaTypeFRR
    pat_ty PatEnv
    penv LPat (GhcPass 'Renamed)
    pat TcM r
    thing_inside
            ; return (ParPat x pat', res) }

      BangPat XBangPat (GhcPass 'Renamed)
    x LPat (GhcPass 'Renamed)
    pat -> do
            { (pat', res) <- Scaled ExpSigmaTypeFRR
    -> Checker (LPat (GhcPass 'Renamed)) (LPat GhcTc)
    tc_lpat Scaled ExpSigmaTypeFRR
    pat_ty PatEnv
    penv LPat (GhcPass 'Renamed)
    pat TcM r
    thing_inside
            ; return (BangPat x pat', res) }

      LazyPat XLazyPat (GhcPass 'Renamed)
    x LPat (GhcPass 'Renamed)
    pat -> do
            { mult_wrap <- NonLinearPatternReason
    -> LPat (GhcPass 'Renamed)
    -> Scaled ExpSigmaTypeFRR
    -> TcM HsWrapper
    forall a.
    NonLinearPatternReason
    -> LPat (GhcPass 'Renamed) -> Scaled a -> TcM HsWrapper
    checkManyPattern NonLinearPatternReason
    LazyPatternReason (Pat (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA Pat (GhcPass 'Renamed)
    ps_pat) Scaled ExpSigmaTypeFRR
    pat_ty
                -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
            ; (pat', (res, pat_ct))
                    <- tc_lpat pat_ty (makeLazy penv) pat $
                       captureConstraints thing_inside
                    -- Ignore refined penv', revert to penv

            ; emitConstraints pat_ct
            -- captureConstraints/extendConstraints:
            --   see Note [Hopping the LIE in lazy patterns]

            -- Check that the expected pattern type is itself lifted
            ; pat_ty <- readExpType (scaledThing pat_ty)
            ; _ <- unifyType Nothing (typeKind pat_ty) liftedTypeKind

            ; return (mkHsWrapPat mult_wrap (LazyPat x pat') pat_ty, res) }

      WildPat XWildPat (GhcPass 'Renamed)
    _ -> do
            { mult_wrap <- NonLinearPatternReason
    -> LPat (GhcPass 'Renamed)
    -> Scaled ExpSigmaTypeFRR
    -> TcM HsWrapper
    forall a.
    NonLinearPatternReason
    -> LPat (GhcPass 'Renamed) -> Scaled a -> TcM HsWrapper
    checkManyPattern NonLinearPatternReason
    OtherPatternReason (Pat (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA Pat (GhcPass 'Renamed)
    ps_pat) Scaled ExpSigmaTypeFRR
    pat_ty
                -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
            ; res <- thing_inside
            ; pat_ty <- expTypeToType (scaledThing pat_ty)
            ; return (mkHsWrapPat mult_wrap (WildPat pat_ty) pat_ty, res) }

      AsPat XAsPat (GhcPass 'Renamed)
    x (L SrcSpanAnnN
    nm_loc Name
    name) LPat (GhcPass 'Renamed)
    pat -> do
            { mult_wrap <- NonLinearPatternReason
    -> LPat (GhcPass 'Renamed)
    -> Scaled ExpSigmaTypeFRR
    -> TcM HsWrapper
    forall a.
    NonLinearPatternReason
    -> LPat (GhcPass 'Renamed) -> Scaled a -> TcM HsWrapper
    checkManyPattern NonLinearPatternReason
    OtherPatternReason (Pat (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA Pat (GhcPass 'Renamed)
    ps_pat) Scaled ExpSigmaTypeFRR
    pat_ty
                -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
            ; (wrap, bndr_id) <- setSrcSpanA nm_loc (tcPatBndr penv name pat_ty)
            ; (pat', res) <- tcExtendIdEnv1 name bndr_id $
                             tc_lpat (pat_ty `scaledSet`(mkCheckExpType $ idType bndr_id))
                                     penv pat thing_inside
                -- NB: if we do inference on:
                --          \ (y@(x::forall a. a->a)) = e
                -- we'll fail.  The as-pattern infers a monotype for 'y', which then
                -- fails to unify with the polymorphic type for 'x'.  This could
                -- perhaps be fixed, but only with a bit more work.
                --
                -- If you fix it, don't forget the bindInstsOfPatIds!
            ; pat_ty <- readExpType (scaledThing pat_ty)
            ; return (mkHsWrapPat (wrap <.> mult_wrap) (AsPat x (L nm_loc bndr_id) pat') pat_ty, res) }

      ViewPat XViewPat (GhcPass 'Renamed)
    _ LHsExpr (GhcPass 'Renamed)
    expr LPat (GhcPass 'Renamed)
    pat -> do
            { mult_wrap <- NonLinearPatternReason
    -> LPat (GhcPass 'Renamed)
    -> Scaled ExpSigmaTypeFRR
    -> TcM HsWrapper
    forall a.
    NonLinearPatternReason
    -> LPat (GhcPass 'Renamed) -> Scaled a -> TcM HsWrapper
    checkManyPattern NonLinearPatternReason
    ViewPatternReason (Pat (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA Pat (GhcPass 'Renamed)
    ps_pat) Scaled ExpSigmaTypeFRR
    pat_ty
             -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
             --
             -- It should be possible to have view patterns at linear (or otherwise
             -- non-Many) multiplicity. But it is not clear at the moment what
             -- restriction need to be put in place, if any, for linear view
             -- patterns to desugar to type-correct Core.

            ; (expr',expr_ty) <- tcInferRho expr
                   -- Note [View patterns and polymorphism]

             -- Expression must be a function
            ; let herald = HsExpr (GhcPass 'Renamed) -> ExpectedFunTyOrigin
    ExpectedFunTyViewPat (HsExpr (GhcPass 'Renamed) -> ExpectedFunTyOrigin)
    -> HsExpr (GhcPass 'Renamed) -> ExpectedFunTyOrigin
    forall a b. (a -> b) -> a -> b
    $ GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed))
    -> HsExpr (GhcPass 'Renamed)
    forall l e. GenLocated l e -> e
    unLoc LHsExpr (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed))
    expr
            ; (expr_wrap1, Scaled _mult inf_arg_ty, inf_res_sigma)
                <- matchActualFunTy herald (Just . HsExprRnThing $ unLoc expr) (1,expr_ty) expr_ty
                   -- See Note [View patterns and polymorphism]
                   -- expr_wrap1 :: expr_ty "->" (inf_arg_ty -> inf_res_sigma)

             -- Check that overall pattern is more polymorphic than arg type
            ; expr_wrap2 <- tc_sub_type penv (scaledThing pat_ty) inf_arg_ty
                -- expr_wrap2 :: pat_ty "->" inf_arg_ty

             -- Pattern must have inf_res_sigma
            ; (pat', res) <- tc_lpat (pat_ty `scaledSet` mkCheckExpType inf_res_sigma) penv pat thing_inside

            ; let Scaled w h_pat_ty = pat_ty
            ; pat_ty <- readExpType h_pat_ty
            ; let expr_wrap2' = HsWrapper -> HsWrapper -> Scaled Type -> Type -> HsWrapper
    mkWpFun HsWrapper
    expr_wrap2 HsWrapper
    idHsWrapper
                                  (Type -> Type -> Scaled Type
    forall a. Type -> a -> Scaled a
    Scaled Type
    w Type
    pat_ty) Type
    inf_res_sigma
              -- expr_wrap2' :: (inf_arg_ty -> inf_res_sigma) "->"
              --                (pat_ty -> inf_res_sigma)
              -- NB: pat_ty comes from matchActualFunTy, so it has a
              -- fixed RuntimeRep, as needed to call mkWpFun.
            ; let
                  expr_wrap = HsWrapper
    expr_wrap2' HsWrapper -> HsWrapper -> HsWrapper
    <.> HsWrapper
    expr_wrap1 HsWrapper -> HsWrapper -> HsWrapper
    <.> HsWrapper
    mult_wrap

            ; return $ (ViewPat pat_ty (mkLHsWrap expr_wrap expr') pat', res) }

    {- Note [View patterns and polymorphism]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider this exotic example:
       pair :: forall a. Bool -> a -> forall b. b -> (a,b)

       f :: Int -> blah
       f (pair True -> x) = ...here (x :: forall b. b -> (Int,b))

    The expression (pair True) should have type
        pair True :: Int -> forall b. b -> (Int,b)
    so that it is ready to consume the incoming Int. It should be an
    arrow type (t1 -> t2); hence using (tcInferRho expr).

    Then, when taking that arrow apart we want to get a *sigma* type
    (forall b. b->(Int,b)), because that's what we want to bind 'x' to.
    Fortunately that's what matchActualFunTy returns anyway.
    -}

    -- Type signatures in patterns
    -- See Note [Pattern coercions] below
      SigPat XSigPat (GhcPass 'Renamed)
    _ LPat (GhcPass 'Renamed)
    pat HsPatSigType (NoGhcTc (GhcPass 'Renamed))
    sig_ty -> do
            { (inner_ty, tv_binds, wcs, wrap) <- Bool
    -> HsPatSigType (GhcPass 'Renamed)
    -> ExpSigmaTypeFRR
    -> TcM (Type, [(Name, TyCoVar)], [(Name, TyCoVar)], HsWrapper)
    tcPatSig (PatEnv -> Bool
    inPatBind PatEnv
    penv)
                                                                HsPatSigType (NoGhcTc (GhcPass 'Renamed))
    HsPatSigType (GhcPass 'Renamed)
    sig_ty (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
    forall a. Scaled a -> a
    scaledThing Scaled ExpSigmaTypeFRR
    pat_ty)
                    -- Using tcExtendNameTyVarEnv is appropriate here
                    -- because we're not really bringing fresh tyvars into scope.
                    -- We're *naming* existing tyvars. Note that it is OK for a tyvar
                    -- from an outer scope to mention one of these tyvars in its kind.
            ; (pat', res) <- tcExtendNameTyVarEnv wcs      $
                             tcExtendNameTyVarEnv tv_binds $
                             tc_lpat (pat_ty `scaledSet` mkCheckExpType inner_ty) penv pat thing_inside
            ; pat_ty <- readExpType (scaledThing pat_ty)
            ; return (mkHsWrapPat wrap (SigPat inner_ty pat' sig_ty) pat_ty, res) }

    ------------------------
    -- Lists, tuples, arrays

      -- Necessarily a built-in list pattern, not an overloaded list pattern.
      -- See Note [Desugaring overloaded list patterns].
      ListPat XListPat (GhcPass 'Renamed)
    _ [LPat (GhcPass 'Renamed)]
    pats -> do
            { (coi, elt_ty) <- (Type -> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, Type))
    -> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, Type)
    forall a.
    (Type -> TcM (TcCoercionN, a))
    -> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, a)
    matchExpectedPatTy Type -> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, Type)
    matchExpectedListTy PatEnv
    penv (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
    forall a. Scaled a -> a
    scaledThing Scaled ExpSigmaTypeFRR
    pat_ty)
            ; (pats', res) <- tcMultiple (tc_lpat (pat_ty `scaledSet` mkCheckExpType elt_ty))
                                         penv pats thing_inside
            ; pat_ty <- readExpType (scaledThing pat_ty)
            ; return (mkHsWrapPat coi
                             (ListPat elt_ty pats') pat_ty, res)
    }

      TuplePat XTuplePat (GhcPass 'Renamed)
    _ [LPat (GhcPass 'Renamed)]
    pats Boxity
    boxity -> do
            { let arity :: Int
    arity = [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [LPat (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    pats
                  tc :: TyCon
    tc = Boxity -> Int -> TyCon
    tupleTyCon Boxity
    boxity Int
    arity
                  -- NB: tupleTyCon does not flatten 1-tuples
                  -- See Note [Don't flatten tuples from HsSyn] in GHC.Core.Make
            ; Int -> TcRn ()
    checkTupSize Int
    arity
            ; (coi, arg_tys) <- (Type -> TcM (TcCoercionN, [Type]))
    -> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, [Type])
    forall a.
    (Type -> TcM (TcCoercionN, a))
    -> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, a)
    matchExpectedPatTy (TyCon -> Type -> TcM (TcCoercionN, [Type])
    matchExpectedTyConApp TyCon
    tc)
                                                   PatEnv
    penv (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
    forall a. Scaled a -> a
    scaledThing Scaled ExpSigmaTypeFRR
    pat_ty)
                         -- Unboxed tuples have RuntimeRep vars, which we discard:
                         -- See Note [Unboxed tuple RuntimeRep vars] in GHC.Core.TyCon
            ; let con_arg_tys = case Boxity
    boxity of Boxity
    Unboxed -> Int -> [Type] -> [Type]
    forall a. Int -> [a] -> [a]
    drop Int
    arity [Type]
    arg_tys
                                               Boxity
    Boxed   -> [Type]
    arg_tys
            ; (pats', res) <- tc_lpats (map (scaledSet pat_ty . mkCheckExpType) con_arg_tys)
                                       penv pats thing_inside

            ; dflags <- getDynFlags

            -- Under flag control turn a pattern (x,y,z) into ~(x,y,z)
            -- so that we can experiment with lazy tuple-matching.
            -- This is a pretty odd place to make the switch, but
            -- it was easy to do.
            ; let
                  unmangled_result = XTuplePat GhcTc -> [LPat GhcTc] -> Boxity -> Pat GhcTc
    forall p. XTuplePat p -> [LPat p] -> Boxity -> Pat p
    TuplePat [Type]
    XTuplePat GhcTc
    con_arg_tys [LPat GhcTc]
    [GenLocated SrcSpanAnnA (Pat GhcTc)]
    pats' Boxity
    boxity
                                     -- pat_ty /= pat_ty iff coi /= IdCo
                  possibly_mangled_result
                    | GeneralFlag -> DynFlags -> Bool
    gopt GeneralFlag
    Opt_IrrefutableTuples DynFlags
    dflags Bool -> Bool -> Bool
    &&
                      Boxity -> Bool
    isBoxed Boxity
    boxity   = XLazyPat GhcTc -> LPat GhcTc -> Pat GhcTc
    forall p. XLazyPat p -> LPat p -> Pat p
    LazyPat XLazyPat GhcTc
    NoExtField
    noExtField (Pat GhcTc -> GenLocated SrcSpanAnnA (Pat GhcTc)
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA Pat GhcTc
    unmangled_result)
                    | Bool
    otherwise        = Pat GhcTc
    unmangled_result

            ; pat_ty <- readExpType (scaledThing pat_ty)
            ; massert (con_arg_tys `equalLength` pats) -- Syntactically enforced
            ; return (mkHsWrapPat coi possibly_mangled_result pat_ty, res)
            }

      SumPat XSumPat (GhcPass 'Renamed)
    _ LPat (GhcPass 'Renamed)
    pat Int
    alt Int
    arity  -> do
            { let tc :: TyCon
    tc = Int -> TyCon
    sumTyCon Int
    arity
            ; (coi, arg_tys) <- (Type -> TcM (TcCoercionN, [Type]))
    -> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, [Type])
    forall a.
    (Type -> TcM (TcCoercionN, a))
    -> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, a)
    matchExpectedPatTy (TyCon -> Type -> TcM (TcCoercionN, [Type])
    matchExpectedTyConApp TyCon
    tc)
                                                   PatEnv
    penv (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
    forall a. Scaled a -> a
    scaledThing Scaled ExpSigmaTypeFRR
    pat_ty)
            ; -- Drop levity vars, we don't care about them here
              let con_arg_tys = Int -> [Type] -> [Type]
    forall a. Int -> [a] -> [a]
    drop Int
    arity [Type]
    arg_tys
            ; (pat', res) <- tc_lpat (pat_ty `scaledSet` mkCheckExpType (con_arg_tys `getNth` (alt - 1)))
                                     penv pat thing_inside
            ; pat_ty <- readExpType (scaledThing pat_ty)
            ; return (mkHsWrapPat coi (SumPat con_arg_tys pat' alt arity) pat_ty
                     , res)
            }

    ------------------------
    -- Data constructors
      ConPat XConPat (GhcPass 'Renamed)
    _ XRec (GhcPass 'Renamed) (ConLikeP (GhcPass 'Renamed))
    con HsConDetails
      (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
      (LPat (GhcPass 'Renamed))
      (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    arg_pats ->
        PatEnv
    -> GenLocated SrcSpanAnnN Name
    -> Scaled ExpSigmaTypeFRR
    -> HsConDetails
         (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
         (LPat (GhcPass 'Renamed))
         (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    -> TcM r
    -> TcM (Pat GhcTc, r)
    forall a.
    PatEnv
    -> GenLocated SrcSpanAnnN Name
    -> Scaled ExpSigmaTypeFRR
    -> HsConDetails
         (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
         (LPat (GhcPass 'Renamed))
         (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    -> TcM a
    -> TcM (Pat GhcTc, a)
    tcConPat PatEnv
    penv XRec (GhcPass 'Renamed) (ConLikeP (GhcPass 'Renamed))
    GenLocated SrcSpanAnnN Name
    con Scaled ExpSigmaTypeFRR
    pat_ty HsConDetails
      (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
      (LPat (GhcPass 'Renamed))
      (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    arg_pats TcM r
    thing_inside

    ------------------------
    -- Literal patterns
      LitPat XLitPat (GhcPass 'Renamed)
    x HsLit (GhcPass 'Renamed)
    simple_lit -> do
            { let lit_ty :: Type
    lit_ty = HsLit (GhcPass 'Renamed) -> Type
    forall (p :: Pass). HsLit (GhcPass p) -> Type
    hsLitType HsLit (GhcPass 'Renamed)
    simple_lit
            ; wrap   <- PatEnv -> ExpSigmaTypeFRR -> Type -> TcM HsWrapper
    tc_sub_type PatEnv
    penv (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
    forall a. Scaled a -> a
    scaledThing Scaled ExpSigmaTypeFRR
    pat_ty) Type
    lit_ty
            ; res    <- thing_inside
            ; pat_ty <- readExpType (scaledThing pat_ty)
            ; return ( mkHsWrapPat wrap (LitPat x (convertLit simple_lit)) pat_ty
                     , res) }

    ------------------------
    -- Overloaded patterns: n, and n+k

    -- In the case of a negative literal (the more complicated case),
    -- we get
    --
    --   case v of (-5) -> blah
    --
    -- becoming
    --
    --   if v == (negate (fromInteger 5)) then blah else ...
    --
    -- There are two bits of rebindable syntax:
    --   (==)   :: pat_ty -> neg_lit_ty -> Bool
    --   negate :: lit_ty -> neg_lit_ty
    -- where lit_ty is the type of the overloaded literal 5.
    --
    -- When there is no negation, neg_lit_ty and lit_ty are the same
      NPat XNPat (GhcPass 'Renamed)
    _ (L EpAnnCO
    l HsOverLit (GhcPass 'Renamed)
    over_lit) Maybe (SyntaxExpr (GhcPass 'Renamed))
    mb_neg SyntaxExpr (GhcPass 'Renamed)
    eq -> do
            { mult_wrap <- NonLinearPatternReason
    -> LPat (GhcPass 'Renamed)
    -> Scaled ExpSigmaTypeFRR
    -> TcM HsWrapper
    forall a.
    NonLinearPatternReason
    -> LPat (GhcPass 'Renamed) -> Scaled a -> TcM HsWrapper
    checkManyPattern NonLinearPatternReason
    OtherPatternReason (Pat (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA Pat (GhcPass 'Renamed)
    ps_pat) Scaled ExpSigmaTypeFRR
    pat_ty
              -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
              --
              -- It may be possible to refine linear pattern so that they work in
              -- linear environments. But it is not clear how useful this is.
            ; let orig = HsOverLit (GhcPass 'Renamed) -> CtOrigin
    LiteralOrigin HsOverLit (GhcPass 'Renamed)
    over_lit
            ; ((lit', mb_neg'), eq')
                <- tcSyntaxOp orig eq [SynType (scaledThing pat_ty), SynAny]
                              (mkCheckExpType boolTy) $
                   \ [Type
    neg_lit_ty] [Type]
    _ ->
                   let new_over_lit :: Type -> TcM (HsOverLit GhcTc)
    new_over_lit Type
    lit_ty = HsOverLit (GhcPass 'Renamed)
    -> ExpSigmaTypeFRR -> TcM (HsOverLit GhcTc)
    newOverloadedLit HsOverLit (GhcPass 'Renamed)
    over_lit
                                               (Type -> ExpSigmaTypeFRR
    mkCheckExpType Type
    lit_ty)
                   in case Maybe (SyntaxExpr (GhcPass 'Renamed))
    mb_neg of
                     Maybe (SyntaxExpr (GhcPass 'Renamed))
    Nothing  -> (, Maybe SyntaxExprTc
    forall a. Maybe a
    Nothing) (HsOverLit GhcTc -> (HsOverLit GhcTc, Maybe SyntaxExprTc))
    -> TcM (HsOverLit GhcTc)
    -> TcM (HsOverLit GhcTc, Maybe SyntaxExprTc)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Type -> TcM (HsOverLit GhcTc)
    new_over_lit Type
    neg_lit_ty
                     Just SyntaxExpr (GhcPass 'Renamed)
    neg -> -- Negative literal
                                 -- The 'negate' is re-mappable syntax
                       (SyntaxExprTc -> Maybe SyntaxExprTc)
    -> (HsOverLit GhcTc, SyntaxExprTc)
    -> (HsOverLit GhcTc, Maybe SyntaxExprTc)
    forall b c d. (b -> c) -> (d, b) -> (d, c)
    forall (a :: * -> * -> *) b c d.
    Arrow a =>
    a b c -> a (d, b) (d, c)
    second SyntaxExprTc -> Maybe SyntaxExprTc
    forall a. a -> Maybe a
    Just ((HsOverLit GhcTc, SyntaxExprTc)
     -> (HsOverLit GhcTc, Maybe SyntaxExprTc))
    -> IOEnv (Env TcGblEnv TcLclEnv) (HsOverLit GhcTc, SyntaxExprTc)
    -> TcM (HsOverLit GhcTc, Maybe SyntaxExprTc)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
                       (CtOrigin
    -> SyntaxExprRn
    -> [SyntaxOpType]
    -> ExpSigmaTypeFRR
    -> ([Type] -> [Type] -> TcM (HsOverLit GhcTc))
    -> IOEnv (Env TcGblEnv TcLclEnv) (HsOverLit GhcTc, SyntaxExprTc)
    forall a.
    CtOrigin
    -> SyntaxExprRn
    -> [SyntaxOpType]
    -> ExpSigmaTypeFRR
    -> ([Type] -> [Type] -> TcM a)
    -> TcM (a, SyntaxExprTc)
    tcSyntaxOp CtOrigin
    orig SyntaxExpr (GhcPass 'Renamed)
    SyntaxExprRn
    neg [SyntaxOpType
    SynRho] (Type -> ExpSigmaTypeFRR
    mkCheckExpType Type
    neg_lit_ty) (([Type] -> [Type] -> TcM (HsOverLit GhcTc))
     -> IOEnv (Env TcGblEnv TcLclEnv) (HsOverLit GhcTc, SyntaxExprTc))
    -> ([Type] -> [Type] -> TcM (HsOverLit GhcTc))
    -> IOEnv (Env TcGblEnv TcLclEnv) (HsOverLit GhcTc, SyntaxExprTc)
    forall a b. (a -> b) -> a -> b
    $
                        \ [Type
    lit_ty] [Type]
    _ -> Type -> TcM (HsOverLit GhcTc)
    new_over_lit Type
    lit_ty)
                         -- applied to a closed literal: linearity doesn't matter as
                         -- literals are typed in an empty environment, hence have
                         -- all multiplicities.

            ; res <- thing_inside
            ; pat_ty <- readExpType (scaledThing pat_ty)
            ; return (mkHsWrapPat mult_wrap (NPat pat_ty (L l lit') mb_neg' eq') pat_ty, res) }

    {-
    Note [NPlusK patterns]
    ~~~~~~~~~~~~~~~~~~~~~~
    From

      case v of x + 5 -> blah

    we get

      if v >= 5 then (\x -> blah) (v - 5) else ...

    There are two bits of rebindable syntax:
      (>=) :: pat_ty -> lit1_ty -> Bool
      (-)  :: pat_ty -> lit2_ty -> var_ty

    lit1_ty and lit2_ty could conceivably be different.
    var_ty is the type inferred for x, the variable in the pattern.

    Note that we need to type-check the literal twice, because it is used
    twice, and may be used at different types. The second HsOverLit stored in the
    AST is used for the subtraction operation.
    -}

    -- See Note [NPlusK patterns]
      NPlusKPat XNPlusKPat (GhcPass 'Renamed)
    _ (L SrcSpanAnnN
    nm_loc Name
    name)
                   (L EpAnnCO
    loc HsOverLit (GhcPass 'Renamed)
    lit) HsOverLit (GhcPass 'Renamed)
    _ SyntaxExpr (GhcPass 'Renamed)
    ge SyntaxExpr (GhcPass 'Renamed)
    minus -> do
            { mult_wrap <- NonLinearPatternReason
    -> LPat (GhcPass 'Renamed)
    -> Scaled ExpSigmaTypeFRR
    -> TcM HsWrapper
    forall a.
    NonLinearPatternReason
    -> LPat (GhcPass 'Renamed) -> Scaled a -> TcM HsWrapper
    checkManyPattern NonLinearPatternReason
    OtherPatternReason (Pat (GhcPass 'Renamed)
    -> GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    forall e a. HasAnnotation e => a -> GenLocated e a
    noLocA Pat (GhcPass 'Renamed)
    ps_pat) Scaled ExpSigmaTypeFRR
    pat_ty
                -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
            ; let pat_exp_ty = Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
    forall a. Scaled a -> a
    scaledThing Scaled ExpSigmaTypeFRR
    pat_ty
                  orig = HsOverLit (GhcPass 'Renamed) -> CtOrigin
    LiteralOrigin HsOverLit (GhcPass 'Renamed)
    lit
            ; (lit1', ge')
                <- tcSyntaxOp orig ge [SynType pat_exp_ty, SynRho]
                                      (mkCheckExpType boolTy) $
                   \ [Type
    lit1_ty] [Type]
    _ ->
                   HsOverLit (GhcPass 'Renamed)
    -> ExpSigmaTypeFRR -> TcM (HsOverLit GhcTc)
    newOverloadedLit HsOverLit (GhcPass 'Renamed)
    lit (Type -> ExpSigmaTypeFRR
    mkCheckExpType Type
    lit1_ty)
            ; ((lit2', minus_wrap, bndr_id), minus')
                <- tcSyntaxOpGen orig minus [SynType pat_exp_ty, SynRho] SynAny $
                   \ [Type
    lit2_ty, Type
    var_ty] [Type]
    _ ->
                   do { lit2' <- HsOverLit (GhcPass 'Renamed)
    -> ExpSigmaTypeFRR -> TcM (HsOverLit GhcTc)
    newOverloadedLit HsOverLit (GhcPass 'Renamed)
    lit (Type -> ExpSigmaTypeFRR
    mkCheckExpType Type
    lit2_ty)
                      ; (wrap, bndr_id) <- setSrcSpanA nm_loc $
                                         tcPatBndr penv name (unrestricted $ mkCheckExpType var_ty)
                               -- co :: var_ty ~ idType bndr_id

                               -- minus_wrap is applicable to minus'
                      ; return (lit2', wrap, bndr_id) }

            ; pat_ty <- readExpType pat_exp_ty

            -- The Report says that n+k patterns must be in Integral
            -- but it's silly to insist on this in the RebindableSyntax case
            ; unlessM (xoptM LangExt.RebindableSyntax) $
              do { icls <- tcLookupClass integralClassName
                 ; instStupidTheta orig [mkClassPred icls [pat_ty]] }

            ; res <- tcExtendIdEnv1 name bndr_id thing_inside

            ; let minus'' = case SyntaxExprTc
    minus' of
                              SyntaxExprTc
    NoSyntaxExprTc -> String -> SDoc -> SyntaxExprTc
    forall a. HasCallStack => String -> SDoc -> a
    pprPanic String
    "tc_pat NoSyntaxExprTc" (SyntaxExprTc -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr SyntaxExprTc
    minus')
                                       -- this should be statically avoidable
                                       -- Case (3) from Note [NoSyntaxExpr] in "GHC.Hs.Expr"
                              SyntaxExprTc { syn_expr :: SyntaxExprTc -> HsExpr GhcTc
    syn_expr = HsExpr GhcTc
    minus'_expr
                                           , syn_arg_wraps :: SyntaxExprTc -> [HsWrapper]
    syn_arg_wraps = [HsWrapper]
    minus'_arg_wraps
                                           , syn_res_wrap :: SyntaxExprTc -> HsWrapper
    syn_res_wrap = HsWrapper
    minus'_res_wrap }
                                -> SyntaxExprTc { syn_expr :: HsExpr GhcTc
    syn_expr = HsExpr GhcTc
    minus'_expr
                                                , syn_arg_wraps :: [HsWrapper]
    syn_arg_wraps = [HsWrapper]
    minus'_arg_wraps
                                                , syn_res_wrap :: HsWrapper
    syn_res_wrap = HsWrapper
    minus_wrap HsWrapper -> HsWrapper -> HsWrapper
    <.> HsWrapper
    minus'_res_wrap }
                                 -- Oy. This should really be a record update, but
                                 -- we get warnings if we try. #17783
                  pat' = XNPlusKPat GhcTc
    -> LIdP GhcTc
    -> XRec GhcTc (HsOverLit GhcTc)
    -> HsOverLit GhcTc
    -> SyntaxExpr GhcTc
    -> SyntaxExpr GhcTc
    -> Pat GhcTc
    forall p.
    XNPlusKPat p
    -> LIdP p
    -> XRec p (HsOverLit p)
    -> HsOverLit p
    -> SyntaxExpr p
    -> SyntaxExpr p
    -> Pat p
    NPlusKPat XNPlusKPat GhcTc
    Type
    pat_ty (SrcSpanAnnN -> TyCoVar -> GenLocated SrcSpanAnnN TyCoVar
    forall l e. l -> e -> GenLocated l e
    L SrcSpanAnnN
    nm_loc TyCoVar
    bndr_id) (EpAnnCO -> HsOverLit GhcTc -> GenLocated EpAnnCO (HsOverLit GhcTc)
    forall l e. l -> e -> GenLocated l e
    L EpAnnCO
    loc HsOverLit GhcTc
    lit1') HsOverLit GhcTc
    lit2'
                                   SyntaxExpr GhcTc
    SyntaxExprTc
    ge' SyntaxExpr GhcTc
    SyntaxExprTc
    minus''
            ; return (mkHsWrapPat mult_wrap pat' pat_ty, res) }

    -- Here we get rid of it and add the finalizers to the global environment.
    -- See Note [Delaying modFinalizers in untyped splices] in GHC.Rename.Splice.
      SplicePat (HsUntypedSpliceTop ThModFinalizers
    mod_finalizers Pat (GhcPass 'Renamed)
    pat) HsUntypedSplice (GhcPass 'Renamed)
    _ -> do
          { ThModFinalizers -> TcRn ()
    addModFinalizersWithLclEnv ThModFinalizers
    mod_finalizers
          ; Scaled ExpSigmaTypeFRR
    -> Checker (Pat (GhcPass 'Renamed)) (Pat GhcTc)
    tc_pat Scaled ExpSigmaTypeFRR
    pat_ty PatEnv
    penv Pat (GhcPass 'Renamed)
    pat TcM r
    thing_inside }

      SplicePat (HsUntypedSpliceNested Name
    _) HsUntypedSplice (GhcPass 'Renamed)
    _ -> String -> TcM (Pat GhcTc, r)
    forall a. HasCallStack => String -> a
    panic String
    "tc_pat: nested splice in splice pat"

      EmbTyPat XEmbTyPat (GhcPass 'Renamed)
    _ HsTyPat (NoGhcTc (GhcPass 'Renamed))
    _ -> TcRnMessage -> TcM (Pat GhcTc, r)
    forall a. TcRnMessage -> TcRn a
    failWith TcRnMessage
    TcRnIllegalTypePattern

      InvisPat XInvisPat (GhcPass 'Renamed)
    _ HsTyPat (NoGhcTc (GhcPass 'Renamed))
    _ -> String -> TcM (Pat GhcTc, r)
    forall a. HasCallStack => String -> a
    panic String
    "tc_pat: invisible pattern appears recursively in the pattern"

      XPat (HsPatExpanded Pat (GhcPass 'Renamed)
    lpat Pat (GhcPass 'Renamed)
    rpat) -> do
        { (rpat', res) <- Scaled ExpSigmaTypeFRR
    -> Checker (Pat (GhcPass 'Renamed)) (Pat GhcTc)
    tc_pat Scaled ExpSigmaTypeFRR
    pat_ty PatEnv
    penv Pat (GhcPass 'Renamed)
    rpat TcM r
    thing_inside
        ; return (XPat $ ExpansionPat lpat rpat', res) }

    {-
    Note [Hopping the LIE in lazy patterns]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    In a lazy pattern, we must *not* discharge constraints from the RHS
    from dictionaries bound in the pattern.  E.g.
            f ~(C x) = 3
    We can't discharge the Num constraint from dictionaries bound by
    the pattern C!

    So we have to make the constraints from thing_inside "hop around"
    the pattern.  Hence the captureConstraints and emitConstraints.

    The same thing ensures that equality constraints in a lazy match
    are not made available in the RHS of the match. For example
            data T a where { T1 :: Int -> T Int; ... }
            f :: T a -> Int -> a
            f ~(T1 i) y = y
    It's obviously not sound to refine a to Int in the right
    hand side, because the argument might not match T1 at all!

    Finally, a lazy pattern should not bind any existential type variables
    because they won't be in scope when we do the desugaring

    ************************************************************************
    *                                                                      *
                Pattern signatures   (pat :: type)
    *                                                                      *
    ************************************************************************
    -}

    tcPatSig :: Bool                    -- True <=> pattern binding
             -> HsPatSigType GhcRn
             -> ExpSigmaType
             -> TcM (TcType,            -- The type to use for "inside" the signature
                     [(Name,TcTyVar)],  -- The new bit of type environment, binding
                                        -- the scoped type variables
                     [(Name,TcTyVar)],  -- The wildcards
                     HsWrapper)         -- Coercion due to unification with actual ty
                                        -- Of shape:  res_ty ~ sig_ty
    tcPatSig :: Bool
    -> HsPatSigType (GhcPass 'Renamed)
    -> ExpSigmaTypeFRR
    -> TcM (Type, [(Name, TyCoVar)], [(Name, TyCoVar)], HsWrapper)
    tcPatSig Bool
    in_pat_bind HsPatSigType (GhcPass 'Renamed)
    sig ExpSigmaTypeFRR
    res_ty
     = do  { (sig_wcs, sig_tvs, sig_ty) <- UserTypeCtxt
    -> HoleMode
    -> HsPatSigType (GhcPass 'Renamed)
    -> ContextKind
    -> TcM ([(Name, TyCoVar)], [(Name, TyCoVar)], Type)
    tcHsPatSigType UserTypeCtxt
    PatSigCtxt HoleMode
    HM_Sig HsPatSigType (GhcPass 'Renamed)
    sig ContextKind
    OpenKind
            -- sig_tvs are the type variables free in 'sig',
            -- and not already in scope. These are the ones
            -- that should be brought into scope

            ; case NE.nonEmpty sig_tvs of
                Maybe (NonEmpty (Name, TyCoVar))
    Nothing -> do {
                    -- Just do the subsumption check and return
                      wrap <- (TidyEnv -> ZonkM (TidyEnv, SDoc))
    -> TcM HsWrapper -> TcM HsWrapper
    forall a. (TidyEnv -> ZonkM (TidyEnv, SDoc)) -> TcM a -> TcM a
    addErrCtxtM (Type -> TidyEnv -> ZonkM (TidyEnv, SDoc)
    mk_msg Type
    sig_ty) (TcM HsWrapper -> TcM HsWrapper) -> TcM HsWrapper -> TcM HsWrapper
    forall a b. (a -> b) -> a -> b
    $
                              CtOrigin
    -> UserTypeCtxt -> ExpSigmaTypeFRR -> Type -> TcM HsWrapper
    tcSubTypePat CtOrigin
    PatSigOrigin UserTypeCtxt
    PatSigCtxt ExpSigmaTypeFRR
    res_ty Type
    sig_ty
                    ; return (sig_ty, [], sig_wcs, wrap)
                    }
                Just NonEmpty (Name, TyCoVar)
    sig_tvs_ne -> do
                    -- Type signature binds at least one scoped type variable

                    -- A pattern binding cannot bind scoped type variables
                    -- It is more convenient to make the test here
                    -- than in the renamer
                  Bool -> TcRn () -> TcRn ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when Bool
    in_pat_bind
                    (TcRnMessage -> TcRn ()
    addErr (NonEmpty (Name, TyCoVar) -> TcRnMessage
    TcRnCannotBindScopedTyVarInPatSig NonEmpty (Name, TyCoVar)
    sig_tvs_ne))

                  -- Now do a subsumption check of the pattern signature against res_ty
                  wrap <- (TidyEnv -> ZonkM (TidyEnv, SDoc))
    -> TcM HsWrapper -> TcM HsWrapper
    forall a. (TidyEnv -> ZonkM (TidyEnv, SDoc)) -> TcM a -> TcM a
    addErrCtxtM (Type -> TidyEnv -> ZonkM (TidyEnv, SDoc)
    mk_msg Type
    sig_ty) (TcM HsWrapper -> TcM HsWrapper) -> TcM HsWrapper -> TcM HsWrapper
    forall a b. (a -> b) -> a -> b
    $
                          CtOrigin
    -> UserTypeCtxt -> ExpSigmaTypeFRR -> Type -> TcM HsWrapper
    tcSubTypePat CtOrigin
    PatSigOrigin UserTypeCtxt
    PatSigCtxt ExpSigmaTypeFRR
    res_ty Type
    sig_ty

                  -- Phew!
                  return (sig_ty, sig_tvs, sig_wcs, wrap)
           }
      where
        mk_msg :: Type -> TidyEnv -> ZonkM (TidyEnv, SDoc)
    mk_msg Type
    sig_ty TidyEnv
    tidy_env
           = do { (tidy_env, sig_ty) <- TidyEnv -> Type -> ZonkM (TidyEnv, Type)
    zonkTidyTcType TidyEnv
    tidy_env Type
    sig_ty
                ; res_ty <- readExpType res_ty   -- should be filled in by now
                ; (tidy_env, res_ty) <- zonkTidyTcType tidy_env res_ty
                ; let msg = [SDoc] -> SDoc
    forall doc. IsDoc doc => [doc] -> doc
    vcat [ SDoc -> Int -> SDoc -> SDoc
    hang (String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "When checking that the pattern signature:")
                                      Int
    4 (Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    sig_ty)
                                 , Int -> SDoc -> SDoc
    nest Int
    2 (SDoc -> Int -> SDoc -> SDoc
    hang (String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "fits the type of its context:")
                                              Int
    2 (Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Type
    res_ty)) ]
                ; return (tidy_env, msg) }

    {- *********************************************************************
    *                                                                      *
            Most of the work for constructors is here
            (the rest is in the ConPatIn case of tc_pat)
    *                                                                      *
    ************************************************************************

    [Pattern matching indexed data types]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider the following declarations:

      data family Map k :: * -> *
      data instance Map (a, b) v = MapPair (Map a (Pair b v))

    and a case expression

      case x :: Map (Int, c) w of MapPair m -> ...

    As explained by [Wrappers for data instance tycons] in GHC.Types.Id.Make, the
    worker/wrapper types for MapPair are

      $WMapPair :: forall a b v. Map a (Map a b v) -> Map (a, b) v
      $wMapPair :: forall a b v. Map a (Map a b v) -> :R123Map a b v

    So, the type of the scrutinee is Map (Int, c) w, but the tycon of MapPair is
    :R123Map, which means the straight use of boxySplitTyConApp would give a type
    error.  Hence, the smart wrapper function boxySplitTyConAppWithFamily calls
    boxySplitTyConApp with the family tycon Map instead, which gives us the family
    type list {(Int, c), w}.  To get the correct split for :R123Map, we need to
    unify the family type list {(Int, c), w} with the instance types {(a, b), v}
    (provided by tyConFamInst_maybe together with the family tycon).  This
    unification yields the substitution [a -> Int, b -> c, v -> w], which gives us
    the split arguments for the representation tycon :R123Map as {Int, c, w}

    In other words, boxySplitTyConAppWithFamily implicitly takes the coercion

      Co123Map a b v :: {Map (a, b) v ~ :R123Map a b v}

    moving between representation and family type into account.  To produce type
    correct Core, this coercion needs to be used to case the type of the scrutinee
    from the family to the representation type.  This is achieved by
    unwrapFamInstScrutinee using a CoPat around the result pattern.

    Now it might appear seem as if we could have used the previous GADT type
    refinement infrastructure of refineAlt and friends instead of the explicit
    unification and CoPat generation.  However, that would be wrong.  Why?  The
    whole point of GADT refinement is that the refinement is local to the case
    alternative.  In contrast, the substitution generated by the unification of
    the family type list and instance types needs to be propagated to the outside.
    Imagine that in the above example, the type of the scrutinee would have been
    (Map x w), then we would have unified {x, w} with {(a, b), v}, yielding the
    substitution [x -> (a, b), v -> w].  In contrast to GADT matching, the
    instantiation of x with (a, b) must be global; ie, it must be valid in *all*
    alternatives of the case expression, whereas in the GADT case it might vary
    between alternatives.

    RIP GADT refinement: refinements have been replaced by the use of explicit
    equality constraints that are used in conjunction with implication constraints
    to express the local scope of GADT refinements.

    Note [Freshen existentials]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    It is essential that these existentials are freshened.
    Otherwise, if we have something like
      case (a :: Ex, b :: Ex) of (MkEx ..., MkEx ...) -> ...
    we'll give both unpacked existential variables the
    same name, leading to shadowing.

    -}

    --      Running example:
    -- MkT :: forall a b c. (a~[b]) => b -> c -> T a
    --       with scrutinee of type (T ty)

    tcConPat :: PatEnv -> LocatedN Name
             -> Scaled ExpSigmaTypeFRR    -- Type of the pattern
             -> HsConPatDetails GhcRn -> TcM a
             -> TcM (Pat GhcTc, a)
    tcConPat :: forall a.
    PatEnv
    -> GenLocated SrcSpanAnnN Name
    -> Scaled ExpSigmaTypeFRR
    -> HsConDetails
         (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
         (LPat (GhcPass 'Renamed))
         (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    -> TcM a
    -> TcM (Pat GhcTc, a)
    tcConPat PatEnv
    penv con_lname :: GenLocated SrcSpanAnnN Name
    con_lname@(L SrcSpanAnnN
    _ Name
    con_name) Scaled ExpSigmaTypeFRR
    pat_ty HsConDetails
      (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
      (LPat (GhcPass 'Renamed))
      (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    arg_pats TcM a
    thing_inside
      = do  { con_like <- Name -> TcM ConLike
    tcLookupConLike Name
    con_name
            ; case con_like of
                RealDataCon DataCon
    data_con -> GenLocated SrcSpanAnnN Name
    -> DataCon
    -> Scaled ExpSigmaTypeFRR
    -> Checker
         (HsConDetails
            (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
            (LPat (GhcPass 'Renamed))
            (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed))))
         (Pat GhcTc)
    tcDataConPat GenLocated SrcSpanAnnN Name
    con_lname DataCon
    data_con Scaled ExpSigmaTypeFRR
    pat_ty
                                                     PatEnv
    penv HsConDetails
      (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
      (LPat (GhcPass 'Renamed))
      (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    arg_pats TcM a
    thing_inside
                PatSynCon PatSyn
    pat_syn -> GenLocated SrcSpanAnnN Name
    -> PatSyn
    -> Scaled ExpSigmaTypeFRR
    -> Checker
         (HsConDetails
            (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
            (LPat (GhcPass 'Renamed))
            (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed))))
         (Pat GhcTc)
    tcPatSynPat GenLocated SrcSpanAnnN Name
    con_lname PatSyn
    pat_syn Scaled ExpSigmaTypeFRR
    pat_ty
                                                 PatEnv
    penv HsConDetails
      (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
      (LPat (GhcPass 'Renamed))
      (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    arg_pats TcM a
    thing_inside
            }

    -- Warn when pattern matching on a GADT or a pattern synonym
    -- when MonoLocalBinds is off.
    warnMonoLocalBinds :: TcM ()
    warnMonoLocalBinds :: TcRn ()
    warnMonoLocalBinds
      = do { mono_local_binds <- Extension -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
    xoptM Extension
    LangExt.MonoLocalBinds
           ; unless mono_local_binds $
               addDiagnostic TcRnGADTMonoLocalBinds
               -- We used to require the GADTs or TypeFamilies extension
               -- to pattern match on a GADT (#2905, #7156)
               --
               -- In #20485 this was made into a warning.
           }

    tcDataConPat :: LocatedN Name -> DataCon
                 -> Scaled ExpSigmaTypeFRR        -- Type of the pattern
                 -> Checker (HsConPatDetails GhcRn) (Pat GhcTc)
    tcDataConPat :: GenLocated SrcSpanAnnN Name
    -> DataCon
    -> Scaled ExpSigmaTypeFRR
    -> Checker
         (HsConDetails
            (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
            (LPat (GhcPass 'Renamed))
            (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed))))
         (Pat GhcTc)
    tcDataConPat (L SrcSpanAnnN
    con_span Name
    con_name) DataCon
    data_con Scaled ExpSigmaTypeFRR
    pat_ty_scaled
                 PatEnv
    penv HsConDetails
      (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
      (LPat (GhcPass 'Renamed))
      (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    arg_pats TcM r
    thing_inside
      = do  { let tycon :: TyCon
    tycon = DataCon -> TyCon
    dataConTyCon DataCon
    data_con
                      -- For data families this is the representation tycon
                  ([TyCoVar]
    univ_tvs, [TyCoVar]
    ex_tvs, [EqSpec]
    eq_spec, [Type]
    theta, [Scaled Type]
    arg_tys, Type
    _)
                    = DataCon
    -> ([TyCoVar], [TyCoVar], [EqSpec], [Type], [Scaled Type], Type)
    dataConFullSig DataCon
    data_con
                  header :: GenLocated SrcSpanAnnN ConLike
    header = SrcSpanAnnN -> ConLike -> GenLocated SrcSpanAnnN ConLike
    forall l e. l -> e -> GenLocated l e
    L SrcSpanAnnN
    con_span (DataCon -> ConLike
    RealDataCon DataCon
    data_con)

              -- Instantiate the constructor type variables [a->ty]
              -- This may involve doing a family-instance coercion,
              -- and building a wrapper
            ; (wrap, ctxt_res_tys) <- PatEnv
    -> TyCon -> Scaled ExpSigmaTypeFRR -> TcM (HsWrapper, [Type])
    matchExpectedConTy PatEnv
    penv TyCon
    tycon Scaled ExpSigmaTypeFRR
    pat_ty_scaled
            ; pat_ty <- readExpType (scaledThing pat_ty_scaled)

              -- Add the stupid theta
            ; setSrcSpanA con_span $ addDataConStupidTheta data_con ctxt_res_tys

            -- Check that this isn't a GADT pattern match
            -- in situations in which that isn't allowed.
            ; let all_arg_tys = [EqSpec] -> [Type]
    eqSpecPreds [EqSpec]
    eq_spec [Type] -> [Type] -> [Type]
    forall a. [a] -> [a] -> [a]
    ++ [Type]
    theta [Type] -> [Type] -> [Type]
    forall a. [a] -> [a] -> [a]
    ++ ((Scaled Type -> Type) -> [Scaled Type] -> [Type]
    forall a b. (a -> b) -> [a] -> [b]
    map Scaled Type -> Type
    forall a. Scaled a -> a
    scaledThing [Scaled Type]
    arg_tys)
            ; checkGADT (RealDataCon data_con) ex_tvs all_arg_tys penv

            ; tenv1 <- instTyVarsWith PatOrigin univ_tvs ctxt_res_tys
                      -- NB: Do not use zipTvSubst!  See #14154
                      -- We want to create a well-kinded substitution, so
                      -- that the instantiated type is well-kinded

            ; let mc = case PatEnv -> PatCtxt
    pe_ctxt PatEnv
    penv of
                         LamPat HsMatchContextRn
    mc -> HsMatchContextRn
    mc
                         LetPat {} -> HsMatchContextRn
    HsMatchContext (GenLocated SrcSpanAnnN Name)
    forall fn. HsMatchContext fn
    PatBindRhs
            ; skol_info <- mkSkolemInfo (PatSkol (RealDataCon data_con) mc)
            ; (tenv, ex_tvs') <- tcInstSuperSkolTyVarsX skol_info tenv1 ex_tvs
                         -- Get location from monad, not from ex_tvs
                         -- This freshens: See Note [Freshen existentials]
                         -- Why "super"? See Note [Binding when looking up instances]
                         -- in GHC.Core.InstEnv.

            ; let arg_tys'       = HasDebugCallStack => Subst -> [Scaled Type] -> [Scaled Type]
    Subst -> [Scaled Type] -> [Scaled Type]
    substScaledTys Subst
    tenv [Scaled Type]
    arg_tys
                  pat_mult       = Scaled ExpSigmaTypeFRR -> Type
    forall a. Scaled a -> Type
    scaledMult Scaled ExpSigmaTypeFRR
    pat_ty_scaled
                  arg_tys_scaled = (Scaled Type -> Scaled Type) -> [Scaled Type] -> [Scaled Type]
    forall a b. (a -> b) -> [a] -> [b]
    map (Type -> Scaled Type -> Scaled Type
    forall a. Type -> Scaled a -> Scaled a
    scaleScaled Type
    pat_mult) [Scaled Type]
    arg_tys'
                  con_like       = DataCon -> ConLike
    RealDataCon DataCon
    data_con

            -- This check is necessary to uphold the invariant that 'tcConArgs'
            -- is given argument types with a fixed runtime representation.
            -- See test case T20363.
            ; checkFixedRuntimeRep data_con arg_tys'

            ; traceTc "tcConPat" (vcat [ text "con_name:" <+> ppr con_name
                                       , text "univ_tvs:" <+> pprTyVars univ_tvs
                                       , text "ex_tvs:" <+> pprTyVars ex_tvs
                                       , text "eq_spec:" <+> ppr eq_spec
                                       , text "theta:" <+> ppr theta
                                       , text "ex_tvs':" <+> pprTyVars ex_tvs'
                                       , text "ctxt_res_tys:" <+> ppr ctxt_res_tys
                                       , text "pat_ty:" <+> ppr pat_ty
                                       , text "arg_tys':" <+> ppr arg_tys'
                                       , text "arg_pats" <+> ppr arg_pats ])

            ; (univ_ty_args, ex_ty_args) <- splitConTyArgs con_like arg_pats

            ; if null ex_tvs && null eq_spec && null theta
              then do { -- The common case; no class bindings etc
                        -- (see Note [Arrows and patterns])
                        (arg_pats', res) <- tcConTyArgs tenv penv univ_ty_args $
                                            tcConValArgs con_like arg_tys_scaled
                                                         penv arg_pats thing_inside
                      ; let res_pat = ConPat { pat_con :: XRec GhcTc (ConLikeP GhcTc)
    pat_con = XRec GhcTc (ConLikeP GhcTc)
    GenLocated SrcSpanAnnN ConLike
    header
                                             , pat_args :: HsConPatDetails GhcTc
    pat_args = HsConPatDetails GhcTc
    HsConDetails
      (HsConPatTyArg (GhcPass 'Renamed))
      (GenLocated SrcSpanAnnA (Pat GhcTc))
      (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
    arg_pats'
                                             , pat_con_ext :: XConPat GhcTc
    pat_con_ext = ConPatTc
                                               { cpt_tvs :: [TyCoVar]
    cpt_tvs = [], cpt_dicts :: [TyCoVar]
    cpt_dicts = []
                                               , cpt_binds :: TcEvBinds
    cpt_binds = TcEvBinds
    emptyTcEvBinds
                                               , cpt_arg_tys :: [Type]
    cpt_arg_tys = [Type]
    ctxt_res_tys
                                               , cpt_wrap :: HsWrapper
    cpt_wrap = HsWrapper
    idHsWrapper
                                               }
                                             }

                      ; return (mkHsWrapPat wrap res_pat pat_ty, res) }

              else do   -- The general case, with existential,
                        -- and local equality constraints
            { let theta'     = HasDebugCallStack => Subst -> [Type] -> [Type]
    Subst -> [Type] -> [Type]
    substTheta Subst
    tenv ([EqSpec] -> [Type]
    eqSpecPreds [EqSpec]
    eq_spec [Type] -> [Type] -> [Type]
    forall a. [a] -> [a] -> [a]
    ++ [Type]
    theta)
                               -- order is *important* as we generate the list of
                               -- dictionary binders from theta'

            ; when (not (null eq_spec) || any isEqPred theta) warnMonoLocalBinds

            ; given <- newEvVars theta'
            ; (ev_binds, (arg_pats', res))
                 <- -- See Note [Type applications in patterns] (W4)
                    tcConTyArgs tenv penv univ_ty_args                       $
                    checkConstraints (getSkolemInfo skol_info) ex_tvs' given $
                    tcConTyArgs tenv penv ex_ty_args                         $
                    tcConValArgs con_like arg_tys_scaled penv arg_pats thing_inside

            ; let res_pat = ConPat
                    { pat_con :: XRec GhcTc (ConLikeP GhcTc)
    pat_con   = XRec GhcTc (ConLikeP GhcTc)
    GenLocated SrcSpanAnnN ConLike
    header
                    , pat_args :: HsConPatDetails GhcTc
    pat_args  = HsConPatDetails GhcTc
    HsConDetails
      (HsConPatTyArg (GhcPass 'Renamed))
      (GenLocated SrcSpanAnnA (Pat GhcTc))
      (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
    arg_pats'
                    , pat_con_ext :: XConPat GhcTc
    pat_con_ext = ConPatTc
                      { cpt_tvs :: [TyCoVar]
    cpt_tvs   = [TyCoVar]
    ex_tvs'
                      , cpt_dicts :: [TyCoVar]
    cpt_dicts = [TyCoVar]
    given
                      , cpt_binds :: TcEvBinds
    cpt_binds = TcEvBinds
    ev_binds
                      , cpt_arg_tys :: [Type]
    cpt_arg_tys = [Type]
    ctxt_res_tys
                      , cpt_wrap :: HsWrapper
    cpt_wrap  = HsWrapper
    idHsWrapper
                      }
                    }
            ; return (mkHsWrapPat wrap res_pat pat_ty, res)
            } }

    tcPatSynPat :: LocatedN Name -> PatSyn
                -> Scaled ExpSigmaType         -- ^ Type of the pattern
                -> Checker (HsConPatDetails GhcRn) (Pat GhcTc)
    tcPatSynPat :: GenLocated SrcSpanAnnN Name
    -> PatSyn
    -> Scaled ExpSigmaTypeFRR
    -> Checker
         (HsConDetails
            (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
            (LPat (GhcPass 'Renamed))
            (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed))))
         (Pat GhcTc)
    tcPatSynPat (L SrcSpanAnnN
    con_span Name
    con_name) PatSyn
    pat_syn Scaled ExpSigmaTypeFRR
    pat_ty PatEnv
    penv HsConDetails
      (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
      (LPat (GhcPass 'Renamed))
      (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    arg_pats TcM r
    thing_inside
      = do  { let ([TyCoVar]
    univ_tvs, [Type]
    req_theta, [TyCoVar]
    ex_tvs, [Type]
    prov_theta, [Scaled Type]
    arg_tys, Type
    ty) = PatSyn
    -> ([TyCoVar], [Type], [TyCoVar], [Type], [Scaled Type], Type)
    patSynSig PatSyn
    pat_syn

            ; (subst, univ_tvs') <- [TyCoVar] -> TcM (Subst, [TyCoVar])
    newMetaTyVars [TyCoVar]
    univ_tvs

            -- Check that we aren't matching on a GADT-like pattern synonym
            -- in situations in which that isn't allowed.
            ; let all_arg_tys = Type
    ty Type -> [Type] -> [Type]
    forall a. a -> [a] -> [a]
    : [Type]
    prov_theta [Type] -> [Type] -> [Type]
    forall a. [a] -> [a] -> [a]
    ++ ((Scaled Type -> Type) -> [Scaled Type] -> [Type]
    forall a b. (a -> b) -> [a] -> [b]
    map Scaled Type -> Type
    forall a. Scaled a -> a
    scaledThing [Scaled Type]
    arg_tys)
            ; checkGADT (PatSynCon pat_syn) ex_tvs all_arg_tys penv

            ; skol_info <- case pe_ctxt penv of
                                LamPat HsMatchContextRn
    mc -> SkolemInfoAnon -> IOEnv (Env TcGblEnv TcLclEnv) SkolemInfo
    forall (m :: * -> *). MonadIO m => SkolemInfoAnon -> m SkolemInfo
    mkSkolemInfo (ConLike -> HsMatchContextRn -> SkolemInfoAnon
    PatSkol (PatSyn -> ConLike
    PatSynCon PatSyn
    pat_syn) HsMatchContextRn
    mc)
                                LetPat {} -> SkolemInfo -> IOEnv (Env TcGblEnv TcLclEnv) SkolemInfo
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return SkolemInfo
    HasCallStack => SkolemInfo
    unkSkol -- Doesn't matter

            ; (tenv, ex_tvs') <- tcInstSuperSkolTyVarsX skol_info subst ex_tvs
               -- This freshens: Note [Freshen existentials]

            ; let ty'         = HasDebugCallStack => Subst -> Type -> Type
    Subst -> Type -> Type
    substTy Subst
    tenv Type
    ty
                  arg_tys'    = HasDebugCallStack => Subst -> [Scaled Type] -> [Scaled Type]
    Subst -> [Scaled Type] -> [Scaled Type]
    substScaledTys Subst
    tenv [Scaled Type]
    arg_tys
                  pat_mult    = Scaled ExpSigmaTypeFRR -> Type
    forall a. Scaled a -> Type
    scaledMult Scaled ExpSigmaTypeFRR
    pat_ty
                  arg_tys_scaled = (Scaled Type -> Scaled Type) -> [Scaled Type] -> [Scaled Type]
    forall a b. (a -> b) -> [a] -> [b]
    map (Type -> Scaled Type -> Scaled Type
    forall a. Type -> Scaled a -> Scaled a
    scaleScaled Type
    pat_mult) [Scaled Type]
    arg_tys'
                  prov_theta' = HasDebugCallStack => Subst -> [Type] -> [Type]
    Subst -> [Type] -> [Type]
    substTheta Subst
    tenv [Type]
    prov_theta
                  req_theta'  = HasDebugCallStack => Subst -> [Type] -> [Type]
    Subst -> [Type] -> [Type]
    substTheta Subst
    tenv [Type]
    req_theta
                  con_like    = PatSyn -> ConLike
    PatSynCon PatSyn
    pat_syn

            ; when (any isEqPred prov_theta) warnMonoLocalBinds

            ; mult_wrap <- checkManyPattern PatternSynonymReason nlWildPatName pat_ty
                -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.

            ; (univ_ty_args, ex_ty_args) <- splitConTyArgs con_like arg_pats

            ; wrap <- tc_sub_type penv (scaledThing pat_ty) ty'

            ; traceTc "tcPatSynPat" $
              vcat [ text "Pat syn:" <+> ppr pat_syn
                   , text "Expected type:" <+> ppr pat_ty
                   , text "Pat res ty:" <+> ppr ty'
                   , text "ex_tvs':" <+> pprTyVars ex_tvs'
                   , text "prov_theta':" <+> ppr prov_theta'
                   , text "req_theta':" <+> ppr req_theta'
                   , text "arg_tys':" <+> ppr arg_tys'
                   , text "univ_ty_args:" <+> ppr univ_ty_args
                   , text "ex_ty_args:" <+> ppr ex_ty_args ]

            ; req_wrap <- instCall (OccurrenceOf con_name) (mkTyVarTys univ_tvs') req_theta'
                          -- Origin (OccurrenceOf con_name):
                          -- see Note [Call-stack tracing of pattern synonyms]
            ; traceTc "instCall" (ppr req_wrap)

              -- Pattern synonyms can never have representation-polymorphic argument types,
              -- as checked in 'GHC.Tc.Gen.Sig.tcPatSynSig' (see use of 'FixedRuntimeRepPatSynSigArg')
              -- and 'GHC.Tc.TyCl.PatSyn.tcInferPatSynDecl'.
              -- (If you want to lift this restriction, use 'hasFixedRuntimeRep' here, to match
              -- 'tcDataConPat'.)
            ; let
                bad_arg_tys :: [(Int, Scaled Type)]
                bad_arg_tys = ((Int, Scaled Type) -> Bool)
    -> [(Int, Scaled Type)] -> [(Int, Scaled Type)]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (\ (Int
    _, Scaled Type
    _ Type
    arg_ty) -> Bool -> Bool
    not (HasDebugCallStack => Type -> Bool
    Type -> Bool
    typeHasFixedRuntimeRep Type
    arg_ty))
                            ([(Int, Scaled Type)] -> [(Int, Scaled Type)])
    -> [(Int, Scaled Type)] -> [(Int, Scaled Type)]
    forall a b. (a -> b) -> a -> b
    $ [Int] -> [Scaled Type] -> [(Int, Scaled Type)]
    forall a b. [a] -> [b] -> [(a, b)]
    zip [Int
    0..] [Scaled Type]
    arg_tys'
            ; massertPpr (null bad_arg_tys) $
                vcat [ text "tcPatSynPat: pattern arguments do not have a fixed RuntimeRep"
                     , text "bad_arg_tys:" <+> ppr bad_arg_tys ]

            ; traceTc "checkConstraints {" Outputable.empty
            ; prov_dicts' <- newEvVars prov_theta'
            ; (ev_binds, (arg_pats', res))
                 <- -- See Note [Type applications in patterns] (W4)
                    tcConTyArgs tenv penv univ_ty_args                             $
                    checkConstraints (getSkolemInfo skol_info) ex_tvs' prov_dicts' $
                    tcConTyArgs tenv penv ex_ty_args                               $
                    tcConValArgs con_like arg_tys_scaled penv arg_pats             $
                    thing_inside
            ; traceTc "checkConstraints }" (ppr ev_binds)

            ; let res_pat = ConPat { pat_con :: XRec GhcTc (ConLikeP GhcTc)
    pat_con   = SrcSpanAnnN -> ConLike -> GenLocated SrcSpanAnnN ConLike
    forall l e. l -> e -> GenLocated l e
    L SrcSpanAnnN
    con_span (ConLike -> GenLocated SrcSpanAnnN ConLike)
    -> ConLike -> GenLocated SrcSpanAnnN ConLike
    forall a b. (a -> b) -> a -> b
    $ PatSyn -> ConLike
    PatSynCon PatSyn
    pat_syn
                                   , pat_args :: HsConPatDetails GhcTc
    pat_args  = HsConPatDetails GhcTc
    HsConDetails
      (HsConPatTyArg (GhcPass 'Renamed))
      (GenLocated SrcSpanAnnA (Pat GhcTc))
      (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
    arg_pats'
                                   , pat_con_ext :: XConPat GhcTc
    pat_con_ext = ConPatTc
                                     { cpt_tvs :: [TyCoVar]
    cpt_tvs   = [TyCoVar]
    ex_tvs'
                                     , cpt_dicts :: [TyCoVar]
    cpt_dicts = [TyCoVar]
    prov_dicts'
                                     , cpt_binds :: TcEvBinds
    cpt_binds = TcEvBinds
    ev_binds
                                     , cpt_arg_tys :: [Type]
    cpt_arg_tys = [TyCoVar] -> [Type]
    mkTyVarTys [TyCoVar]
    univ_tvs'
                                     , cpt_wrap :: HsWrapper
    cpt_wrap  = HsWrapper
    req_wrap
                                     }
                                   }
            ; pat_ty <- readExpType (scaledThing pat_ty)
            ; return (mkHsWrapPat (wrap <.> mult_wrap) res_pat pat_ty, res) }

    checkFixedRuntimeRep :: DataCon -> [Scaled TcSigmaTypeFRR] -> TcM ()
    checkFixedRuntimeRep :: DataCon -> [Scaled Type] -> TcRn ()
    checkFixedRuntimeRep DataCon
    data_con [Scaled Type]
    arg_tys
      = (Int -> Scaled Type -> TcRn ())
    -> [Int] -> [Scaled Type] -> TcRn ()
    forall (m :: * -> *) a b c.
    Applicative m =>
    (a -> b -> m c) -> [a] -> [b] -> m ()
    zipWithM_ Int -> Scaled Type -> TcRn ()
    check_one [Int
    1..] [Scaled Type]
    arg_tys
      where
        check_one :: Int -> Scaled Type -> TcRn ()
    check_one Int
    i Scaled Type
    arg_ty = HasDebugCallStack => FixedRuntimeRepContext -> Type -> TcRn ()
    FixedRuntimeRepContext -> Type -> TcRn ()
    hasFixedRuntimeRep_syntactic
                                (DataCon -> Int -> FixedRuntimeRepContext
    FRRDataConPatArg DataCon
    data_con Int
    i)
                                (Scaled Type -> Type
    forall a. Scaled a -> a
    scaledThing Scaled Type
    arg_ty)

    {- Note [Call-stack tracing of pattern synonyms]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Consider
       f :: HasCallStack => blah

       pattern Annotated :: HasCallStack => (CallStack, a) -> a
       pattern Annotated x <- (f -> x)

    When we pattern-match against `Annotated` we will call `f`, and must
    pass a call-stack.  We may want `Annotated` itself to propagate the call
    stack, so we give it a HasCallStack constraint too.  But then we expect
    to see `Annotated` in the call stack.

    This is achieve easily, but a bit trickily.  When we instantiate
    Annotated's "required" constraints, in tcPatSynPat, give them a
    CtOrigin of (OccurrenceOf "Annotated"). That way the special magic
    in GHC.Tc.Solver.Dict.solveCallStack which deals with CallStack
    constraints will kick in: that logic only fires on constraints
    whose Origin is (OccurrenceOf f).

    See also Note [Overview of implicit CallStacks] in GHC.Tc.Types.Evidence
    and Note [Solving CallStack constraints] in GHC.Tc.Solver.Types
    -}
    ----------------------------
    -- | Convenient wrapper for calling a matchExpectedXXX function
    matchExpectedPatTy :: (TcRhoType -> TcM (TcCoercionN, a))
                        -> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, a)
    -- See Note [Matching polytyped patterns]
    -- Returns a wrapper : pat_ty ~R inner_ty
    matchExpectedPatTy :: forall a.
    (Type -> TcM (TcCoercionN, a))
    -> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, a)
    matchExpectedPatTy Type -> TcM (TcCoercionN, a)
    inner_match (PE { pe_orig :: PatEnv -> CtOrigin
    pe_orig = CtOrigin
    orig }) ExpSigmaTypeFRR
    pat_ty
      = do { pat_ty <- ExpSigmaTypeFRR -> IOEnv (Env TcGblEnv TcLclEnv) Type
    expTypeToType ExpSigmaTypeFRR
    pat_ty
           ; (wrap, pat_rho) <- topInstantiate orig pat_ty
           ; (co, res) <- inner_match pat_rho
           ; traceTc "matchExpectedPatTy" (ppr pat_ty $$ ppr wrap)
           ; return (mkWpCastN (mkSymCo co) <.> wrap, res) }

    ----------------------------
    matchExpectedConTy :: PatEnv
                       -> TyCon
                           -- ^ The TyCon that this data constructor actually returns.
                           -- In the case of a data family, this is
                           -- the /representation/ TyCon.
                       -> Scaled ExpSigmaTypeFRR
                           -- ^ The type of the pattern.
                           -- In the case of a data family, this would
                           -- mention the /family/ TyCon
                       -> TcM (HsWrapper, [TcSigmaType])
    -- See Note [Matching constructor patterns]
    -- Returns a wrapper : pat_ty "->" T ty1 ... tyn
    matchExpectedConTy :: PatEnv
    -> TyCon -> Scaled ExpSigmaTypeFRR -> TcM (HsWrapper, [Type])
    matchExpectedConTy (PE { pe_orig :: PatEnv -> CtOrigin
    pe_orig = CtOrigin
    orig }) TyCon
    data_tc Scaled ExpSigmaTypeFRR
    exp_pat_ty
      | Just (TyCon
    fam_tc, [Type]
    fam_args, CoAxiom Unbranched
    co_tc) <- TyCon -> Maybe (TyCon, [Type], CoAxiom Unbranched)
    tyConFamInstSig_maybe TyCon
    data_tc
             -- Comments refer to Note [Matching constructor patterns]
             -- co_tc :: forall a. T [a] ~ T7 a
      = do { pat_ty <- ExpSigmaTypeFRR -> IOEnv (Env TcGblEnv TcLclEnv) Type
    expTypeToType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
    forall a. Scaled a -> a
    scaledThing Scaled ExpSigmaTypeFRR
    exp_pat_ty)
           ; (wrap, pat_rho) <- topInstantiate orig pat_ty

           ; (subst, tvs') <- newMetaTyVars (tyConTyVars data_tc)
                 -- tys = [ty1,ty2]

           ; traceTc "matchExpectedConTy" (vcat [ppr data_tc,
                                                 ppr (tyConTyVars data_tc),
                                                 ppr fam_tc, ppr fam_args,
                                                 ppr exp_pat_ty,
                                                 ppr pat_ty,
                                                 ppr pat_rho, ppr wrap])
           ; co1 <- unifyType Nothing (mkTyConApp fam_tc (substTys subst fam_args)) pat_rho
                 -- co1 : T (ty1,ty2) ~N pat_rho
                 -- could use tcSubType here... but it's the wrong way round
                 -- for actual vs. expected in error messages.

           ; let tys' = [TyCoVar] -> [Type]
    mkTyVarTys [TyCoVar]
    tvs'
                 co2 = Role
    -> CoAxiom Unbranched -> [Type] -> [TcCoercionN] -> TcCoercionN
    mkUnbranchedAxInstCo Role
    Representational CoAxiom Unbranched
    co_tc [Type]
    tys' []
                 -- co2 : T (ty1,ty2) ~R T7 ty1 ty2

                 full_co = HasDebugCallStack => TcCoercionN -> TcCoercionN
    TcCoercionN -> TcCoercionN
    mkSubCo (TcCoercionN -> TcCoercionN
    mkSymCo TcCoercionN
    co1) TcCoercionN -> TcCoercionN -> TcCoercionN
    `mkTransCo` TcCoercionN
    co2
                 -- full_co :: pat_rho ~R T7 ty1 ty2

           ; return ( mkWpCastR full_co <.> wrap, tys') }

      | Bool
    otherwise
      = do { pat_ty <- ExpSigmaTypeFRR -> IOEnv (Env TcGblEnv TcLclEnv) Type
    expTypeToType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
    forall a. Scaled a -> a
    scaledThing Scaled ExpSigmaTypeFRR
    exp_pat_ty)
           ; (wrap, pat_rho) <- topInstantiate orig pat_ty
           ; (coi, tys) <- matchExpectedTyConApp data_tc pat_rho
           ; return (mkWpCastN (mkSymCo coi) <.> wrap, tys) }

    {-
    Note [Matching constructor patterns]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Suppose (coi, tys) = matchExpectedConType data_tc pat_ty

     * In the simple case, pat_ty = tc tys

     * If pat_ty is a polytype, we want to instantiate it
       This is like part of a subsumption check.  Eg
          f :: (forall a. [a]) -> blah
          f [] = blah

     * In a type family case, suppose we have
              data family T a
              data instance T (p,q) = A p | B q
           Then we'll have internally generated
                  data T7 p q = A p | B q
                  axiom coT7 p q :: T (p,q) ~ T7 p q

           So if pat_ty = T (ty1,ty2), we return (coi, [ty1,ty2]) such that
               coi = coi2 . coi1 : T7 t ~ pat_ty
               coi1 : T (ty1,ty2) ~ pat_ty
               coi2 : T7 ty1 ty2 ~ T (ty1,ty2)

       For families we do all this matching here, not in the unifier,
       because we never want a whisper of the data_tycon to appear in
       error messages; it's a purely internal thing
    -}

    {- Note [Type applications in patterns]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Type applications in patterns are enabled by -XTypeAbstractions.
    For example:
       f :: Either (Maybe a) [b] -> blah
       f (Left @x @[y] (v::Maybe x)) = blah

    How should we typecheck them?  The basic plan is pretty simple, and is
    all done in tcConTyArgs. For each type argument:

    * Step 1:
        * bind the newly-in-scope type variables (here `x` or `y`) to
          unification variables, say `x0` or `y0`

        * typecheck the type argument, `@x` or `@[y]` to get the
          types `x0` or `[y0]`.

        This step is done by `tcHsPatSigType`, similar to the way we
        deal with pattern signatures.

    * Step 2: Unify those types with the type arguments we expect from
      the context, in this case (Maybe a) and [b].  These unifications
      will (perhaps after the constraint solver has done its work)
      unify   x0 := Maybe a
              y0 := b
      Thus we learn that x stands for (Maybe a) and y for b.

    * Step 3: Extend the lexical context to bind `x` to `x0` and
      `y` to `y0`, and typecheck the body of the pattern match.

    However there are several quite tricky wrinkles.

    (W1) Surprisingly, we can discard the coercions arising from
         these unifications.  The *only* thing the unification does is
         to side-effect those unification variables, so that we know
         what type x and y stand for; and cause an error if the equality
         is not soluble.  It's a bit like a constraint arising
         from a functional dependency, where we don't use the evidence.

    (W2) Note that both here and in pattern signatures the unification may
         not even end up unifying the variable.  For example
           type S a b = a
           f :: Maybe a -> Bool
           f (Just @(S a b) x) = True :: b
         In Step 2 we will unify (S a0 b0 ~ a), which succeeds, but has no
         effect on the unification variable b0, to which 'b' is bound.
         Later, in the RHS, we find that b0 must be Bool, and unify it there.
         All is fine.

    (W3) The order of the arguments to the /data constructor/ may differ from
         the order of the arguments to the /type constructor/. Example
             data T a b where { MkT :: forall c d. (c,d) -> T d c }
             f :: T Int Bool -> blah
             f (MkT @x @y p) = ...
         The /first/ type argument to `MkT`, namely `@x` corresponds to the
         /second/ argument to `T` in the type `T Int Bool`.  So `x` is bound
         to `Bool` -- not to `Int`!.  That is why splitConTyArgs uses
         conLikeUserTyVarBinders to match up with the user-supplied type arguments
         in the pattern, not dataConUnivTyVars/dataConExTyVars.

    (W4) A similar story works for existentials, but it is subtly different
         (#19847).  Consider
             data T a where { MkT :: forall a b. a -> b -> T a }
             f :: T Int -> blah
             f (MkT @x @y v w) = blah
         Here we create a fresh unification variables x0,y0 for x,y and
         unify x0~Int, y0~b, where b is the fresh existential variable bound by
         the pattern. But
           * (x0~Int) must be /outside/ the implication constraint
           * (y0~b)   must be /inside/ it
         (and hence x0 and y0 themselves must have different levels).
         Thus:
             x0[1]~Int,  (forall[2] b. (y0[2]~b, ...constraints-from-blah...))

         We need (x0~Int) /outside/ so that it can influence the type of the
         pattern in an inferred setting, e.g.
             g :: T _ -> blah
             g (MkT @Int @y v w) = blah
         Here we want to infer `g` to have type `T Int -> blah`. If the
         (x0~Int) was inside the implication, and the the constructor bound
         equality constraints, `x0` would be untouchable. This was the root
         cause of #19847.

         We need (y0~b) to be /inside/ the implication, so that `b` is in
         scope.  In fact, we may actually /need/ equalities bound by the
         implication to prove the equality constraint we generate.
         Example   data T a where
                     MkT :: forall p q. T (p,q)
                   f :: T (Int,Bool) -> blah
                   f (MkT @Int @Bool) = ...
         We get the implication
            forall[2] p q. (p,q)~(Int,Bool) => (p ~ Int, q ~ Bool, ...)
         where the Given comes from the GADT match, while (p~Int, q~Bool)
         comes from matching the type arguments.

         Wow.  That's all quite subtle! See the long discussion on #19847.  We
         must treat universal and existential arguments separately, even though
         they are all mixed up (W3).  The function splitConTyArgs separates the
         universals from existentials; and we build the implication between
         typechecking the two sets:
               tcConTyArgs ... univ_ty_args    $
               checkConstraints ...            $
               tcConTyArgs ... ex_ty_args      $
               ..typecheck body..
         You can see this code shape in tcDataConPat and tcPatSynPat.

         Where pattern synonyms are involved, this two-level split may not be
         enough.  See #22328 for the story.
    -}

    tcConValArgs :: ConLike
                 -> [Scaled TcSigmaTypeFRR]
                 -> Checker (HsConPatDetails GhcRn) (HsConPatDetails GhcTc)
    tcConValArgs :: ConLike
    -> [Scaled Type]
    -> Checker
         (HsConDetails
            (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
            (LPat (GhcPass 'Renamed))
            (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed))))
         (HsConPatDetails GhcTc)
    tcConValArgs ConLike
    con_like [Scaled Type]
    arg_tys PatEnv
    penv HsConDetails
      (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
      (LPat (GhcPass 'Renamed))
      (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    con_args TcM r
    thing_inside = case HsConDetails
      (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
      (LPat (GhcPass 'Renamed))
      (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    con_args of
      PrefixCon [HsConPatTyArg (NoGhcTc (GhcPass 'Renamed))]
    type_args [LPat (GhcPass 'Renamed)]
    arg_pats -> do
            -- NB: type_args already dealt with
            -- See Note [Type applications in patterns]
            { Bool -> TcRnMessage -> TcRn ()
    checkTc (Int
    con_arity Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == Int
    no_of_args)     -- Check correct arity
                      (TyThing -> Int -> Int -> TcRnMessage
    TcRnArityMismatch (ConLike -> TyThing
    AConLike ConLike
    con_like) Int
    con_arity Int
    no_of_args)

            ; let pats_w_tys :: [(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)), Scaled Type)]
    pats_w_tys = String
    -> [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    -> [Scaled Type]
    -> [(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)), Scaled Type)]
    forall a b. HasDebugCallStack => String -> [a] -> [b] -> [(a, b)]
    zipEqual String
    "tcConArgs" [LPat (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    arg_pats [Scaled Type]
    arg_tys
            ; (arg_pats', res) <- Checker
      (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)), Scaled Type)
      (GenLocated SrcSpanAnnA (Pat GhcTc))
    -> Checker
         [(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)), Scaled Type)]
         [GenLocated SrcSpanAnnA (Pat GhcTc)]
    forall inp out. Checker inp out -> Checker [inp] [out]
    tcMultiple PatEnv
    -> (LPat (GhcPass 'Renamed), Scaled Type)
    -> TcM r
    -> TcM (LPat GhcTc, r)
    PatEnv
    -> (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)), Scaled Type)
    -> TcM r
    -> IOEnv
         (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
    Checker (LPat (GhcPass 'Renamed), Scaled Type) (LPat GhcTc)
    Checker
      (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)), Scaled Type)
      (GenLocated SrcSpanAnnA (Pat GhcTc))
    tcConArg PatEnv
    penv [(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)), Scaled Type)]
    pats_w_tys TcM r
    thing_inside

            ; return (PrefixCon type_args arg_pats', res) }
        where
          con_arity :: Int
    con_arity  = ConLike -> Int
    conLikeArity ConLike
    con_like
          no_of_args :: Int
    no_of_args = [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [LPat (GhcPass 'Renamed)]
    [GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))]
    arg_pats

      InfixCon LPat (GhcPass 'Renamed)
    p1 LPat (GhcPass 'Renamed)
    p2 -> do
            { Bool -> TcRnMessage -> TcRn ()
    checkTc (Int
    con_arity Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == Int
    2)      -- Check correct arity
                      (TyThing -> Int -> Int -> TcRnMessage
    TcRnArityMismatch (ConLike -> TyThing
    AConLike ConLike
    con_like) Int
    con_arity Int
    2)
            ; let [Scaled Type
    arg_ty1,Scaled Type
    arg_ty2] = [Scaled Type]
    arg_tys       -- This can't fail after the arity check
            ; ([p1',p2'], res) <- Checker
      (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)), Scaled Type)
      (GenLocated SrcSpanAnnA (Pat GhcTc))
    -> Checker
         [(GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)), Scaled Type)]
         [GenLocated SrcSpanAnnA (Pat GhcTc)]
    forall inp out. Checker inp out -> Checker [inp] [out]
    tcMultiple PatEnv
    -> (LPat (GhcPass 'Renamed), Scaled Type)
    -> TcM r
    -> TcM (LPat GhcTc, r)
    PatEnv
    -> (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)), Scaled Type)
    -> TcM r
    -> IOEnv
         (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
    Checker (LPat (GhcPass 'Renamed), Scaled Type) (LPat GhcTc)
    Checker
      (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)), Scaled Type)
      (GenLocated SrcSpanAnnA (Pat GhcTc))
    tcConArg PatEnv
    penv [(LPat (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    p1,Scaled Type
    arg_ty1),(LPat (GhcPass 'Renamed)
    GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    p2,Scaled Type
    arg_ty2)]
                                                      TcM r
    thing_inside
            ; return (InfixCon p1' p2', res) }
        where
          con_arity :: Int
    con_arity  = ConLike -> Int
    conLikeArity ConLike
    con_like

      RecCon (HsRecFields [LHsRecField (GhcPass 'Renamed) (LPat (GhcPass 'Renamed))]
    rpats Maybe (XRec (GhcPass 'Renamed) RecFieldsDotDot)
    dd) -> do
            { (rpats', res) <- Checker
      (GenLocated
         SrcSpanAnnA
         (HsFieldBind
            (GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed)))
            (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)))))
      (GenLocated
         SrcSpanAnnA
         (HsFieldBind
            (GenLocated SrcSpanAnnA (FieldOcc GhcTc))
            (GenLocated SrcSpanAnnA (Pat GhcTc))))
    -> Checker
         [GenLocated
            SrcSpanAnnA
            (HsFieldBind
               (GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed)))
               (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))))]
         [GenLocated
            SrcSpanAnnA
            (HsFieldBind
               (GenLocated SrcSpanAnnA (FieldOcc GhcTc))
               (GenLocated SrcSpanAnnA (Pat GhcTc)))]
    forall inp out. Checker inp out -> Checker [inp] [out]
    tcMultiple PatEnv
    -> LHsRecField (GhcPass 'Renamed) (LPat (GhcPass 'Renamed))
    -> TcM r
    -> TcM (LHsRecField GhcTc (LPat GhcTc), r)
    PatEnv
    -> GenLocated
         SrcSpanAnnA
         (HsFieldBind
            (GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed)))
            (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))))
    -> TcM r
    -> IOEnv
         (Env TcGblEnv TcLclEnv)
         (GenLocated
            SrcSpanAnnA
            (HsFieldBind
               (GenLocated SrcSpanAnnA (FieldOcc GhcTc))
               (GenLocated SrcSpanAnnA (Pat GhcTc))),
          r)
    Checker
      (LHsRecField (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
      (LHsRecField GhcTc (LPat GhcTc))
    Checker
      (GenLocated
         SrcSpanAnnA
         (HsFieldBind
            (GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed)))
            (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)))))
      (GenLocated
         SrcSpanAnnA
         (HsFieldBind
            (GenLocated SrcSpanAnnA (FieldOcc GhcTc))
            (GenLocated SrcSpanAnnA (Pat GhcTc))))
    tc_field PatEnv
    penv [LHsRecField (GhcPass 'Renamed) (LPat (GhcPass 'Renamed))]
    [GenLocated
       SrcSpanAnnA
       (HsFieldBind
          (GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed)))
          (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))))]
    rpats TcM r
    thing_inside
            ; return (RecCon (HsRecFields rpats' dd), res) }
        where
          tc_field :: Checker (LHsRecField GhcRn (LPat GhcRn))
                              (LHsRecField GhcTc (LPat GhcTc))
          tc_field :: Checker
      (LHsRecField (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
      (LHsRecField GhcTc (LPat GhcTc))
    tc_field PatEnv
    penv
                   (L SrcSpanAnnA
    l (HsFieldBind XHsFieldBind (GenLocated SrcSpanAnnA (FieldOcc (GhcPass 'Renamed)))
    ann (L SrcSpanAnnA
    loc (FieldOcc XCFieldOcc (GhcPass 'Renamed)
    sel (L SrcSpanAnnN
    lr RdrName
    rdr))) GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
    pat Bool
    pun))
                   TcM r
    thing_inside
            = do { sel'   <- Name -> TcM TyCoVar
    tcLookupId XCFieldOcc (GhcPass 'Renamed)
    Name
    sel
                 ; pat_ty <- setSrcSpanA loc $ find_field_ty sel
                                                (occNameFS $ rdrNameOcc rdr)
                 ; (pat', res) <- tcConArg penv (pat, pat_ty) thing_inside
                 ; return (L l (HsFieldBind ann (L loc (FieldOcc sel' (L lr rdr))) pat'
                                                                            pun), res) }

          find_field_ty :: Name -> FastString -> TcM (Scaled TcType)
          find_field_ty :: Name -> FastString -> TcRn (Scaled Type)
    find_field_ty Name
    sel FastString
    lbl
            = case [Scaled Type
    ty | (FieldLabel
    fl, Scaled Type
    ty) <- [(FieldLabel, Scaled Type)]
    field_tys, FieldLabel -> Name
    flSelector FieldLabel
    fl Name -> Name -> Bool
    forall a. Eq a => a -> a -> Bool
    == Name
    sel ] of

                    -- No matching field; chances are this field label comes from some
                    -- other record type (or maybe none).  If this happens, just fail,
                    -- otherwise we get crashes later (#8570), and similar:
                    --      f (R { foo = (a,b) }) = a+b
                    -- If foo isn't one of R's fields, we don't want to crash when
                    -- typechecking the "a+b".
               [] -> TcRnMessage -> TcRn (Scaled Type)
    forall a. TcRnMessage -> TcRn a
    failWith (Name -> FieldLabelString -> TcRnMessage
    badFieldConErr (ConLike -> Name
    forall a. NamedThing a => a -> Name
    getName ConLike
    con_like) (FastString -> FieldLabelString
    FieldLabelString FastString
    lbl))

                    -- The normal case, when the field comes from the right constructor
               (Scaled Type
    pat_ty : [Scaled Type]
    extras) -> do
                    String -> SDoc -> TcRn ()
    traceTc String
    "find_field" (Scaled Type -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Scaled Type
    pat_ty SDoc -> SDoc -> SDoc
    forall doc. IsLine doc => doc -> doc -> doc
    <+> [Scaled Type] -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr [Scaled Type]
    extras)
                    Bool -> TcRn (Scaled Type) -> TcRn (Scaled Type)
    forall a. HasCallStack => Bool -> a -> a
    assert ([Scaled Type] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Scaled Type]
    extras) (Scaled Type -> TcRn (Scaled Type)
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Scaled Type
    pat_ty)

          field_tys :: [(FieldLabel, Scaled TcType)]
          field_tys :: [(FieldLabel, Scaled Type)]
    field_tys = [FieldLabel] -> [Scaled Type] -> [(FieldLabel, Scaled Type)]
    forall a b. [a] -> [b] -> [(a, b)]
    zip (ConLike -> [FieldLabel]
    conLikeFieldLabels ConLike
    con_like) [Scaled Type]
    arg_tys
              -- Don't use zipEqual! If the constructor isn't really a record, then
              -- dataConFieldLabels will be empty (and each field in the pattern
              -- will generate an error below).

    splitConTyArgs :: ConLike -> HsConPatDetails GhcRn
                   -> TcM ( [(HsConPatTyArg GhcRn, TyVar)]    -- Universals
                          , [(HsConPatTyArg GhcRn, TyVar)] )  -- Existentials
    -- See Note [Type applications in patterns] (W4)
    -- This function is monadic only because of the error check
    -- for too many type arguments
    splitConTyArgs :: ConLike
    -> HsConDetails
         (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
         (LPat (GhcPass 'Renamed))
         (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    -> TcM
         ([(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)],
          [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)])
    splitConTyArgs ConLike
    con_like (PrefixCon [HsConPatTyArg (NoGhcTc (GhcPass 'Renamed))]
    type_args [LPat (GhcPass 'Renamed)]
    _)
      = do { Bool -> TcRnMessage -> TcRn ()
    checkTc ([HsConPatTyArg (NoGhcTc (GhcPass 'Renamed))]
    [HsConPatTyArg (GhcPass 'Renamed)]
    type_args [HsConPatTyArg (GhcPass 'Renamed)] -> [TyCoVar] -> Bool
    forall a b. [a] -> [b] -> Bool
    `leLength` [TyCoVar]
    con_spec_bndrs)
                     (ConLike -> Int -> Int -> TcRnMessage
    TcRnTooManyTyArgsInConPattern ConLike
    con_like
                              ([TyCoVar] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [TyCoVar]
    con_spec_bndrs) ([HsConPatTyArg (GhcPass 'Renamed)] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [HsConPatTyArg (NoGhcTc (GhcPass 'Renamed))]
    [HsConPatTyArg (GhcPass 'Renamed)]
    type_args))
           ; if [TyCoVar] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [TyCoVar]
    ex_tvs  -- Short cut common case
             then ([(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)],
     [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)])
    -> TcM
         ([(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)],
          [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)])
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ([(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)]
    bndr_ty_arg_prs, [])
             else ([(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)],
     [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)])
    -> TcM
         ([(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)],
          [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)])
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (((HsConPatTyArg (GhcPass 'Renamed), TyCoVar) -> Bool)
    -> [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)]
    -> ([(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)],
        [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    partition (HsConPatTyArg (GhcPass 'Renamed), TyCoVar) -> Bool
    is_universal [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)]
    bndr_ty_arg_prs) }
      where
        ex_tvs :: [TyCoVar]
    ex_tvs = ConLike -> [TyCoVar]
    conLikeExTyCoVars ConLike
    con_like
        con_spec_bndrs :: [TyCoVar]
    con_spec_bndrs = [ TyCoVar
    tv | Bndr TyCoVar
    tv Specificity
    SpecifiedSpec <- ConLike -> [VarBndr TyCoVar Specificity]
    conLikeUserTyVarBinders ConLike
    con_like ]
            -- conLikeUserTyVarBinders: see (W3) in
            --    Note [Type applications in patterns]
            -- SpecifiedSpec: forgetting to filter out inferred binders led to #20443

        bndr_ty_arg_prs :: [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)]
    bndr_ty_arg_prs = [HsConPatTyArg (NoGhcTc (GhcPass 'Renamed))]
    [HsConPatTyArg (GhcPass 'Renamed)]
    type_args [HsConPatTyArg (GhcPass 'Renamed)]
    -> [TyCoVar] -> [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)]
    forall a b. [a] -> [b] -> [(a, b)]
    `zip` [TyCoVar]
    con_spec_bndrs
                          -- The zip truncates to length(type_args)

        is_universal :: (HsConPatTyArg (GhcPass 'Renamed), TyCoVar) -> Bool
    is_universal (HsConPatTyArg (GhcPass 'Renamed)
    _, TyCoVar
    tv) = Bool -> Bool
    not (TyCoVar
    tv TyCoVar -> [TyCoVar] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [TyCoVar]
    ex_tvs)
             -- See Note [DataCon user type variable binders] in GHC.Core.DataCon
             -- especially INVARIANT(dataConTyVars).

    splitConTyArgs ConLike
    _ (RecCon {})   = ([(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)],
     [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)])
    -> TcM
         ([(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)],
          [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)])
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ([], []) -- No type args in RecCon
    splitConTyArgs ConLike
    _ (InfixCon {}) = ([(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)],
     [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)])
    -> TcM
         ([(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)],
          [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)])
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ([], []) -- No type args in InfixCon

    tcConTyArgs :: Subst -> PatEnv -> [(HsConPatTyArg GhcRn, TyVar)]
                -> TcM a -> TcM a
    tcConTyArgs :: forall a.
    Subst
    -> PatEnv
    -> [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)]
    -> TcM a
    -> TcM a
    tcConTyArgs Subst
    tenv PatEnv
    penv [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)]
    prs TcM a
    thing_inside
      = Checker (HsConPatTyArg (GhcPass 'Renamed), TyCoVar) ()
    -> PatEnv
    -> [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)]
    -> TcM a
    -> TcM a
    forall inp r. Checker inp () -> PatEnv -> [inp] -> TcM r -> TcM r
    tcMultiple_ (Subst -> Checker (HsConPatTyArg (GhcPass 'Renamed), TyCoVar) ()
    tcConTyArg Subst
    tenv) PatEnv
    penv [(HsConPatTyArg (GhcPass 'Renamed), TyCoVar)]
    prs TcM a
    thing_inside

    tcConTyArg :: Subst -> Checker (HsConPatTyArg GhcRn, TyVar) ()
    tcConTyArg :: Subst -> Checker (HsConPatTyArg (GhcPass 'Renamed), TyCoVar) ()
    tcConTyArg Subst
    tenv PatEnv
    penv (HsConPatTyArg XConPatTyArg (GhcPass 'Renamed)
    _ HsTyPat (GhcPass 'Renamed)
    rn_ty, TyCoVar
    con_tv) TcM r
    thing_inside
      = do { (sig_wcs, sig_ibs, arg_ty) <- HsTyPat (GhcPass 'Renamed)
    -> Type -> TcM ([(Name, TyCoVar)], [(Name, TyCoVar)], Type)
    tcHsTyPat HsTyPat (GhcPass 'Renamed)
    rn_ty (HasDebugCallStack => Subst -> Type -> Type
    Subst -> Type -> Type
    substTy Subst
    tenv (TyCoVar -> Type
    varType TyCoVar
    con_tv))

           ; case NE.nonEmpty sig_ibs of
               Just NonEmpty (Name, TyCoVar)
    sig_ibs_ne | PatEnv -> Bool
    inPatBind PatEnv
    penv ->
                 TcRnMessage -> TcRn ()
    addErr (NonEmpty (Name, TyCoVar) -> TcRnMessage
    TcRnCannotBindTyVarsInPatBind NonEmpty (Name, TyCoVar)
    sig_ibs_ne)
               Maybe (NonEmpty (Name, TyCoVar))
    _ -> () -> TcRn ()
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure ()

              -- This unification is straight from Figure 7 of
              -- "Type Variables in Patterns", Haskell'18
              -- OK to drop coercions here. These unifications are all about
              -- guiding inference based on a user-written type annotation
              -- See Note [Type applications in patterns] (W1)
           ; _ <- unifyType Nothing arg_ty (substTyVar tenv con_tv)

           ; result <- tcExtendNameTyVarEnv sig_wcs $
                       tcExtendNameTyVarEnv sig_ibs $
                       thing_inside
                 -- NB: Because we call tConTyArgs twice, once for universals and
                 --     once for existentials; so this brings things into scope
                 --     "out of left-right order". But it doesn't matter; the renamer
                 --     has dealt with all that.

           ; return ((), result) }

    tcConArg :: Checker (LPat GhcRn, Scaled TcSigmaType) (LPat GhcTc)
    tcConArg :: Checker (LPat (GhcPass 'Renamed), Scaled Type) (LPat GhcTc)
    tcConArg PatEnv
    penv (LPat (GhcPass 'Renamed)
    arg_pat, Scaled Type
    arg_mult Type
    arg_ty)
      = Scaled ExpSigmaTypeFRR
    -> Checker (LPat (GhcPass 'Renamed)) (LPat GhcTc)
    tc_lpat (Type -> ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
    forall a. Type -> a -> Scaled a
    Scaled Type
    arg_mult (Type -> ExpSigmaTypeFRR
    mkCheckExpType Type
    arg_ty)) PatEnv
    penv LPat (GhcPass 'Renamed)
    arg_pat

    addDataConStupidTheta :: DataCon -> [TcType] -> TcM ()
    -- Instantiate the "stupid theta" of the data con, and throw
    -- the constraints into the constraint set.
    -- See Note [The stupid context] in GHC.Core.DataCon.
    addDataConStupidTheta :: DataCon -> [Type] -> TcRn ()
    addDataConStupidTheta DataCon
    data_con [Type]
    inst_tys
      | [Type] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Type]
    stupid_theta = () -> TcRn ()
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
      | Bool
    otherwise         = CtOrigin -> [Type] -> TcRn ()
    instStupidTheta CtOrigin
    origin [Type]
    inst_theta
      where
        origin :: CtOrigin
    origin = Name -> CtOrigin
    OccurrenceOf (DataCon -> Name
    dataConName DataCon
    data_con)
            -- The origin should always report "occurrence of C"
            -- even when C occurs in a pattern
        stupid_theta :: [Type]
    stupid_theta = DataCon -> [Type]
    dataConStupidTheta DataCon
    data_con
        univ_tvs :: [TyCoVar]
    univ_tvs     = DataCon -> [TyCoVar]
    dataConUnivTyVars DataCon
    data_con
        tenv :: Subst
    tenv = [TyCoVar] -> [Type] -> Subst
    HasDebugCallStack => [TyCoVar] -> [Type] -> Subst
    zipTvSubst [TyCoVar]
    univ_tvs ([TyCoVar] -> [Type] -> [Type]
    forall b a. [b] -> [a] -> [a]
    takeList [TyCoVar]
    univ_tvs [Type]
    inst_tys)
             -- NB: inst_tys can be longer than the univ tyvars
             --     because the constructor might have existentials
        inst_theta :: [Type]
    inst_theta = HasDebugCallStack => Subst -> [Type] -> [Type]
    Subst -> [Type] -> [Type]
    substTheta Subst
    tenv [Type]
    stupid_theta

    {-
    Note [Arrows and patterns]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~
    (Oct 07) Arrow notation has the odd property that it involves
    "holes in the scope". For example:
      expr :: Arrow a => a () Int
      expr = proc (y,z) -> do
              x <- term -< y
              expr' -< x

    Here the 'proc (y,z)' binding scopes over the arrow tails but not the
    arrow body (e.g 'term').  As things stand (bogusly) all the
    constraints from the proc body are gathered together, so constraints
    from 'term' will be seen by the tcPat for (y,z).  But we must *not*
    bind constraints from 'term' here, because the desugarer will not make
    these bindings scope over 'term'.

    The Right Thing is not to confuse these constraints together. But for
    now the Easy Thing is to ensure that we do not have existential or
    GADT constraints in a 'proc', which we do by disallowing any
    non-vanilla pattern match (i.e. one that introduces existential
    variables or provided constraints), in tcDataConPat and tcPatSynPat.

    We also short-cut the constraint simplification for such vanilla patterns,
    so that we bind no constraints. Hence the 'fast path' in tcDataConPat;
    which applies more generally (not just within 'proc'), as it's a good
    plan in general to bypass the constraint simplification step entirely
    when it's not needed.

    Note [Pattern coercions]
    ~~~~~~~~~~~~~~~~~~~~~~~~
    In principle, these program would be reasonable:

            f :: (forall a. a->a) -> Int
            f (x :: Int->Int) = x 3

            g :: (forall a. [a]) -> Bool
            g [] = True

    In both cases, the function type signature restricts what arguments can be passed
    in a call (to polymorphic ones).  The pattern type signature then instantiates this
    type.  For example, in the first case,  (forall a. a->a) <= Int -> Int, and we
    generate the translated term
            f = \x' :: (forall a. a->a).  let x = x' Int in x 3

    From a type-system point of view, this is perfectly fine, but it's *very* seldom useful.
    And it requires a significant amount of code to implement, because we need to decorate
    the translated pattern with coercion functions (generated from the subsumption check
    by tcSub).

    So for now I'm just insisting on type *equality* in patterns.  No subsumption.

    Old notes about desugaring, at a time when pattern coercions were handled:

    A SigPat is a type coercion and must be handled one at a time.  We can't
    combine them unless the type of the pattern inside is identical, and we don't
    bother to check for that.  For example:

            data T = T1 Int | T2 Bool
            f :: (forall a. a -> a) -> T -> t
            f (g::Int->Int)   (T1 i) = T1 (g i)
            f (g::Bool->Bool) (T2 b) = T2 (g b)

    We desugar this as follows:

            f = \ g::(forall a. a->a) t::T ->
                let gi = g Int
                in case t of { T1 i -> T1 (gi i)
                               other ->
                let gb = g Bool
                in case t of { T2 b -> T2 (gb b)
                               other -> fail }}

    Note that we do not treat the first column of patterns as a
    column of variables, because the coerced variables (gi, gb)
    would be of different types.  So we get rather grotty code.
    But I don't think this is a common case, and if it was we could
    doubtless improve it.

    Meanwhile, the strategy is:
            * treat each SigPat coercion (always non-identity coercions)
                    as a separate block
            * deal with the stuff inside, and then wrap a binding round
                    the result to bind the new variable (gi, gb, etc)

    ************************************************************************
    *                                                                      *
    \subsection{Errors and contexts}
    *                                                                      *
    ************************************************************************

    Note [Existential check]
    ~~~~~~~~~~~~~~~~~~~~~~~~
    Lazy patterns can't bind existentials.  They arise in two ways:
      * Let bindings      let { C a b = e } in b
      * Twiddle patterns  f ~(C a b) = e
    The pe_lazy field of PatEnv says whether we are inside a lazy
    pattern (perhaps deeply)

    See also Note [Typechecking pattern bindings] in GHC.Tc.Gen.Bind
    -}

    maybeWrapPatCtxt :: Pat GhcRn -> (TcM a -> TcM b) -> TcM a -> TcM b
    -- Not all patterns are worth pushing a context
    maybeWrapPatCtxt :: forall a b.
    Pat (GhcPass 'Renamed) -> (TcM a -> TcM b) -> TcM a -> TcM b
    maybeWrapPatCtxt Pat (GhcPass 'Renamed)
    pat TcM a -> TcM b
    tcm TcM a
    thing_inside
      | Bool -> Bool
    not (Pat (GhcPass 'Renamed) -> Bool
    forall p. Pat p -> Bool
    worth_wrapping Pat (GhcPass 'Renamed)
    pat) = TcM a -> TcM b
    tcm TcM a
    thing_inside
      | Bool
    otherwise                = SDoc -> TcM b -> TcM b
    forall a. SDoc -> TcM a -> TcM a
    addErrCtxt SDoc
    msg (TcM b -> TcM b) -> TcM b -> TcM b
    forall a b. (a -> b) -> a -> b
    $ TcM a -> TcM b
    tcm (TcM a -> TcM b) -> TcM a -> TcM b
    forall a b. (a -> b) -> a -> b
    $ TcM a -> TcM a
    forall a. TcM a -> TcM a
    popErrCtxt TcM a
    thing_inside
                                   -- Remember to pop before doing thing_inside
      where
       worth_wrapping :: Pat p -> Bool
    worth_wrapping (VarPat {}) = Bool
    False
       worth_wrapping (ParPat {}) = Bool
    False
       worth_wrapping (AsPat {})  = Bool
    False
       worth_wrapping Pat p
    _           = Bool
    True
       msg :: SDoc
    msg = SDoc -> Int -> SDoc -> SDoc
    hang (String -> SDoc
    forall doc. IsLine doc => String -> doc
    text String
    "In the pattern:") Int
    2 (Pat (GhcPass 'Renamed) -> SDoc
    forall a. Outputable a => a -> SDoc
    ppr Pat (GhcPass 'Renamed)
    pat)

    -----------------------------------------------

    -- | Check that a pattern isn't a GADT, or doesn't have existential variables,
    -- in a situation in which that is not permitted (inside a lazy pattern, or
    -- in arrow notation).
    checkGADT :: ConLike
              -> [TyVar] -- ^ existentials
              -> [Type]  -- ^ argument types
              -> PatEnv
              -> TcM ()
    checkGADT :: ConLike -> [TyCoVar] -> [Type] -> PatEnv -> TcRn ()
    checkGADT ConLike
    conlike [TyCoVar]
    ex_tvs [Type]
    arg_tys = \case
      PE { pe_ctxt :: PatEnv -> PatCtxt
    pe_ctxt = LetPat {} }
        -> () -> TcRn ()
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
      PE { pe_ctxt :: PatEnv -> PatCtxt
    pe_ctxt = LamPat (ArrowMatchCtxt {}) }
        | Bool -> Bool
    not (Bool -> Bool) -> Bool -> Bool
    forall a b. (a -> b) -> a -> b
    $ ConLike -> Bool
    isVanillaConLike ConLike
    conlike
        -- See Note [Arrows and patterns]
        -> TcRnMessage -> TcRn ()
    forall a. TcRnMessage -> TcRn a
    failWithTc TcRnMessage
    TcRnArrowProcGADTPattern
      PE { pe_lazy :: PatEnv -> Bool
    pe_lazy = Bool
    True }
        | Bool
    has_existentials
        -- See Note [Existential check]
        -> TcRnMessage -> TcRn ()
    forall a. TcRnMessage -> TcRn a
    failWithTc TcRnMessage
    TcRnLazyGADTPattern
      PatEnv
    _ -> () -> TcRn ()
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
      where
        has_existentials :: Bool
        has_existentials :: Bool
    has_existentials = (TyCoVar -> Bool) -> [TyCoVar] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any (TyCoVar -> VarSet -> Bool
    `elemVarSet` [Type] -> VarSet
    tyCoVarsOfTypes [Type]
    arg_tys) [TyCoVar]
    ex_tvs

    -- | Very similar to GHC.Tc.Pat.isIrrefutableHsPat, but doesn't typecheck the pattern
    --   It does depend on the type checker monad (`TcM`) however as we need to check ConPat case in more detail.
    --   Specifically, we call `tcLookupGlobal` to obtain constructor details from global packages
    --   for a comprehensive irrefutability check and avoid false negatives. (testcase pattern-fails.hs)
    isIrrefutableHsPatRnTcM :: Bool -> LPat GhcRn -> TcM Bool
    isIrrefutableHsPatRnTcM :: Bool
    -> LPat (GhcPass 'Renamed) -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    isIrrefutableHsPatRnTcM Bool
    is_strict = LPatIrrefutableCheck
      (IOEnv (Env TcGblEnv TcLclEnv)) (GhcPass 'Renamed)
    forall (m :: * -> *) (p :: Pass).
    (Monad m, OutputableBndrId p) =>
    LPatIrrefutableCheck m (GhcPass p)
    isIrrefutableHsPatHelperM Bool
    is_strict Bool
    -> XRec (GhcPass 'Renamed) (ConLikeP (GhcPass 'Renamed))
    -> HsConDetails
         (HsConPatTyArg (NoGhcTc (GhcPass 'Renamed)))
         (LPat (GhcPass 'Renamed))
         (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
    -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    Bool
    -> GenLocated (Anno (ConLikeP (GhcPass 'Renamed))) Name
    -> HsConDetails
         (HsConPatTyArg (GhcPass (NoGhcTcPass 'Renamed)))
         (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed)))
         (HsRecFields
            (GhcPass 'Renamed)
            (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))))
    -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    forall {p :: Pass}.
    (ConLikeP (GhcPass p) ~ Name, OutputableBndr (IdGhcP p),
     OutputableBndr (IdGhcP (NoGhcTcPass p)), IsPass p,
     Outputable (GenLocated (Anno (IdGhcP p)) (IdGhcP p)),
     Outputable
       (GenLocated
          (Anno (IdGhcP (NoGhcTcPass p))) (IdGhcP (NoGhcTcPass p)))) =>
    Bool
    -> GenLocated (Anno (ConLikeP (GhcPass p))) Name
    -> HsConDetails
         (HsConPatTyArg (GhcPass (NoGhcTcPass p)))
         (GenLocated SrcSpanAnnA (Pat (GhcPass p)))
         (HsRecFields
            (GhcPass p) (GenLocated SrcSpanAnnA (Pat (GhcPass p))))
    -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    isConLikeIrr
      where
          doWork :: Bool
    -> GenLocated SrcSpanAnnA (Pat (GhcPass p))
    -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    doWork Bool
    is_strict = LPatIrrefutableCheck (IOEnv (Env TcGblEnv TcLclEnv)) (GhcPass p)
    forall (m :: * -> *) (p :: Pass).
    (Monad m, OutputableBndrId p) =>
    LPatIrrefutableCheck m (GhcPass p)
    isIrrefutableHsPatHelperM Bool
    is_strict Bool
    -> XRec (GhcPass p) (ConLikeP (GhcPass p))
    -> HsConDetails
         (HsConPatTyArg (NoGhcTc (GhcPass p)))
         (XRec (GhcPass p) (Pat (GhcPass p)))
         (HsRecFields (GhcPass p) (XRec (GhcPass p) (Pat (GhcPass p))))
    -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    Bool
    -> GenLocated (Anno (ConLikeP (GhcPass p))) Name
    -> HsConDetails
         (HsConPatTyArg (GhcPass (NoGhcTcPass p)))
         (GenLocated SrcSpanAnnA (Pat (GhcPass p)))
         (HsRecFields
            (GhcPass p) (GenLocated SrcSpanAnnA (Pat (GhcPass p))))
    -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    isConLikeIrr

          isConLikeIrr :: Bool
    -> GenLocated (Anno (ConLikeP (GhcPass p))) Name
    -> HsConDetails
         (HsConPatTyArg (GhcPass (NoGhcTcPass p)))
         (GenLocated SrcSpanAnnA (Pat (GhcPass p)))
         (HsRecFields
            (GhcPass p) (GenLocated SrcSpanAnnA (Pat (GhcPass p))))
    -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    isConLikeIrr Bool
    is_strict (L Anno (ConLikeP (GhcPass p))
    _ Name
    dcName) HsConDetails
      (HsConPatTyArg (GhcPass (NoGhcTcPass p)))
      (GenLocated SrcSpanAnnA (Pat (GhcPass p)))
      (HsRecFields
         (GhcPass p) (GenLocated SrcSpanAnnA (Pat (GhcPass p))))
    details =
            do { tyth <- Name -> TcM TyThing
    tcLookupGlobal Name
    dcName
               ; case tyth of
                   (ATyCon TyCon
    tycon) -> Bool
    -> TyCon
    -> HsConDetails
         (HsConPatTyArg (GhcPass (NoGhcTcPass p)))
         (GenLocated SrcSpanAnnA (Pat (GhcPass p)))
         (HsRecFields
            (GhcPass p) (GenLocated SrcSpanAnnA (Pat (GhcPass p))))
    -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    doCheck Bool
    is_strict TyCon
    tycon HsConDetails
      (HsConPatTyArg (GhcPass (NoGhcTcPass p)))
      (GenLocated SrcSpanAnnA (Pat (GhcPass p)))
      (HsRecFields
         (GhcPass p) (GenLocated SrcSpanAnnA (Pat (GhcPass p))))
    details
                   (AConLike ConLike
    cl) ->
                     case ConLike
    cl of
                       RealDataCon DataCon
    dc -> Bool
    -> TyCon
    -> HsConDetails
         (HsConPatTyArg (GhcPass (NoGhcTcPass p)))
         (GenLocated SrcSpanAnnA (Pat (GhcPass p)))
         (HsRecFields
            (GhcPass p) (GenLocated SrcSpanAnnA (Pat (GhcPass p))))
    -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    doCheck Bool
    is_strict (DataCon -> TyCon
    dataConTyCon DataCon
    dc) HsConDetails
      (HsConPatTyArg (GhcPass (NoGhcTcPass p)))
      (GenLocated SrcSpanAnnA (Pat (GhcPass p)))
      (HsRecFields
         (GhcPass p) (GenLocated SrcSpanAnnA (Pat (GhcPass p))))
    details
                       PatSynCon PatSyn
    _pat -> Bool -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    False -- conservative
                   TyThing
    _ -> Bool -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    False                  -- conservative
               }

          doCheck :: Bool
    -> TyCon
    -> HsConDetails
         (HsConPatTyArg (GhcPass (NoGhcTcPass p)))
         (GenLocated SrcSpanAnnA (Pat (GhcPass p)))
         (HsRecFields
            (GhcPass p) (GenLocated SrcSpanAnnA (Pat (GhcPass p))))
    -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    doCheck Bool
    is_strict TyCon
    tycon HsConDetails
      (HsConPatTyArg (GhcPass (NoGhcTcPass p)))
      (GenLocated SrcSpanAnnA (Pat (GhcPass p)))
      (HsRecFields
         (GhcPass p) (GenLocated SrcSpanAnnA (Pat (GhcPass p))))
    details =  do { let b :: Bool
    b = Maybe DataCon -> Bool
    forall a. Maybe a -> Bool
    isJust (TyCon -> Maybe DataCon
    tyConSingleDataCon_maybe TyCon
    tycon)
                                                ; bs <- (GenLocated SrcSpanAnnA (Pat (GhcPass p))
     -> IOEnv (Env TcGblEnv TcLclEnv) Bool)
    -> [GenLocated SrcSpanAnnA (Pat (GhcPass p))]
    -> IOEnv (Env TcGblEnv TcLclEnv) [Bool]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
    mapM (Bool
    -> GenLocated SrcSpanAnnA (Pat (GhcPass p))
    -> IOEnv (Env TcGblEnv TcLclEnv) Bool
    doWork Bool
    is_strict) (HsConDetails
      (HsConPatTyArg (NoGhcTc (GhcPass p)))
      (XRec (GhcPass p) (Pat (GhcPass p)))
      (HsRecFields (GhcPass p) (XRec (GhcPass p) (Pat (GhcPass p))))
    -> [XRec (GhcPass p) (Pat (GhcPass p))]
    forall p. UnXRec p => HsConPatDetails p -> [LPat p]
    hsConPatArgs HsConDetails
      (HsConPatTyArg (NoGhcTc (GhcPass p)))
      (XRec (GhcPass p) (Pat (GhcPass p)))
      (HsRecFields (GhcPass p) (XRec (GhcPass p) (Pat (GhcPass p))))
    HsConDetails
      (HsConPatTyArg (GhcPass (NoGhcTcPass p)))
      (GenLocated SrcSpanAnnA (Pat (GhcPass p)))
      (HsRecFields
         (GhcPass p) (GenLocated SrcSpanAnnA (Pat (GhcPass p))))
    details)
                                                ; return (b && and bs) }