{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# 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
   , tcPats
   , addDataConStupidTheta
   , polyPatSig
   )
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.Utils.Zonk
import GHC.Tc.Gen.Sig( TcPragEnv, lookupPragEnv, addInlinePrags )
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.Instantiate
import GHC.Types.Error
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.Validity( arityErr )
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.DataCon
import GHC.Core.PatSyn
import GHC.Core.ConLike
import GHC.Builtin.Names
import GHC.Types.Basic hiding (SuccessFlag(..))
import GHC.Driver.Session
import GHC.Types.SrcLoc
import GHC.Types.Var.Set
import GHC.Utils.Misc
import GHC.Utils.Outputable as Outputable
import GHC.Utils.Panic
import GHC.Utils.Panic.Plain
import qualified GHC.LanguageExtensions as LangExt
import Control.Arrow  ( second )
import Control.Monad
import GHC.Data.List.SetOps ( getNth )

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

tcLetPat :: (Name -> Maybe TcId)
         -> LetBndrSpec
         -> LPat GhcRn -> Scaled ExpSigmaTypeFRR
         -> TcM a
         -> TcM (LPat GhcTc, a)
tcLetPat :: (Name -> Maybe TcId)
-> LetBndrSpec
-> LPat GhcRn
-> Scaled ExpSigmaTypeFRR
-> TcM a
-> TcM (LPat GhcTc, a)
tcLetPat Name -> Maybe TcId
sig_fn LetBndrSpec
no_gen LPat GhcRn
pat Scaled ExpSigmaTypeFRR
pat_ty TcM a
thing_inside
  = do { TcLevel
bind_lvl <- TcM TcLevel
getTcLevel
       ; let ctxt :: PatCtxt
ctxt = LetPat :: TcLevel -> (Name -> Maybe TcId) -> LetBndrSpec -> PatCtxt
LetPat { pc_lvl :: TcLevel
pc_lvl    = TcLevel
bind_lvl
                           , pc_sig_fn :: Name -> Maybe TcId
pc_sig_fn = Name -> Maybe TcId
sig_fn
                           , pc_new :: LetBndrSpec
pc_new    = LetBndrSpec
no_gen }
             penv :: PatEnv
penv = PE :: Bool -> PatCtxt -> CtOrigin -> PatEnv
PE { pe_lazy :: Bool
pe_lazy = Bool
True
                       , pe_ctxt :: PatCtxt
pe_ctxt = PatCtxt
ctxt
                       , pe_orig :: CtOrigin
pe_orig = CtOrigin
PatOrigin }

       ; Scaled ExpSigmaTypeFRR
-> PatEnv -> LPat GhcRn -> TcM a -> TcM (LPat GhcTc, a)
Scaled ExpSigmaTypeFRR -> Checker (LPat GhcRn) (LPat GhcTc)
tc_lpat Scaled ExpSigmaTypeFRR
pat_ty PatEnv
penv LPat GhcRn
pat TcM a
thing_inside }

-----------------
tcPats :: HsMatchContext GhcTc
       -> [LPat GhcRn]             -- ^ atterns
       -> [Scaled ExpSigmaTypeFRR] -- ^ types of the patterns
       -> TcM a                    -- ^ checker for the body
       -> TcM ([LPat GhcTc], a)

-- This is the externally-callable wrapper function
-- 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

--   1. Initialise the PatState
--   2. Check the patterns
--   3. Check the body
--   4. Check that no existentials escape

tcPats :: HsMatchContext GhcTc
-> [LPat GhcRn]
-> [Scaled ExpSigmaTypeFRR]
-> TcM a
-> TcM ([LPat GhcTc], a)
tcPats HsMatchContext GhcTc
ctxt [LPat GhcRn]
pats [Scaled ExpSigmaTypeFRR]
pat_tys TcM a
thing_inside
  = [Scaled ExpSigmaTypeFRR]
-> PatEnv -> [LPat GhcRn] -> TcM a -> TcM ([LPat GhcTc], a)
[Scaled ExpSigmaTypeFRR] -> Checker [LPat GhcRn] [LPat GhcTc]
tc_lpats [Scaled ExpSigmaTypeFRR]
pat_tys PatEnv
penv [LPat GhcRn]
pats TcM a
thing_inside
  where
    penv :: PatEnv
penv = PE :: Bool -> PatCtxt -> CtOrigin -> PatEnv
PE { pe_lazy :: Bool
pe_lazy = Bool
False, pe_ctxt :: PatCtxt
pe_ctxt = HsMatchContext GhcTc -> PatCtxt
LamPat HsMatchContext GhcTc
ctxt, pe_orig :: CtOrigin
pe_orig = CtOrigin
PatOrigin }

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

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

-- | A variant of 'tcPat' that takes a custom origin
tcCheckPat_O :: HsMatchContext GhcTc
             -> 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 :: HsMatchContext GhcTc
-> CtOrigin
-> LPat GhcRn
-> Scaled TcSigmaTypeFRR
-> TcM a
-> TcM (LPat GhcTc, a)
tcCheckPat_O HsMatchContext GhcTc
ctxt CtOrigin
orig LPat GhcRn
pat (Scaled TcSigmaTypeFRR
pat_mult TcSigmaTypeFRR
pat_ty) TcM a
thing_inside
  = Scaled ExpSigmaTypeFRR
-> PatEnv -> LPat GhcRn -> TcM a -> TcM (LPat GhcTc, a)
Scaled ExpSigmaTypeFRR -> Checker (LPat GhcRn) (LPat GhcTc)
tc_lpat (TcSigmaTypeFRR -> ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
forall a. TcSigmaTypeFRR -> a -> Scaled a
Scaled TcSigmaTypeFRR
pat_mult (TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType TcSigmaTypeFRR
pat_ty)) PatEnv
penv LPat GhcRn
pat TcM a
thing_inside
  where
    penv :: PatEnv
penv = PE :: Bool -> PatCtxt -> CtOrigin -> PatEnv
PE { pe_lazy :: Bool
pe_lazy = Bool
False, pe_ctxt :: PatCtxt
pe_ctxt = HsMatchContext GhcTc -> PatCtxt
LamPat HsMatchContext GhcTc
ctxt, pe_orig :: CtOrigin
pe_orig = CtOrigin
orig }


{-
************************************************************************
*                                                                      *
                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
       (HsMatchContext GhcTc)

  | 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 TcId
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
text String
"LetLclBndr"
  ppr (LetGblBndr {}) = String -> SDoc
text String
"LetGblBndr"

makeLazy :: PatEnv -> PatEnv
makeLazy :: PatEnv -> PatEnv
makeLazy PatEnv
penv = PatEnv
penv { pe_lazy :: Bool
pe_lazy = Bool
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, TcId)
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 TcId
pc_sig_fn = Name -> Maybe TcId
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 TcId
bndr_id <- Name -> Maybe TcId
sig_fn Name
bndr_name   -- There is a signature
  = do { HsWrapper
wrap <- PatEnv -> ExpSigmaTypeFRR -> TcSigmaTypeFRR -> TcM HsWrapper
tc_sub_type PatEnv
penv (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
exp_pat_ty) (TcId -> TcSigmaTypeFRR
idType TcId
bndr_id)
           -- See Note [Subsumption check at pattern variables]
       ; String -> SDoc -> TcRn ()
traceTc String
"tcPatBndr(sig)" (TcId -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcId
bndr_id SDoc -> SDoc -> SDoc
$$ TcSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr (TcId -> TcSigmaTypeFRR
idType TcId
bndr_id) SDoc -> SDoc -> SDoc
$$ Scaled ExpSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr Scaled ExpSigmaTypeFRR
exp_pat_ty)
       ; (HsWrapper, TcId) -> TcM (HsWrapper, TcId)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper
wrap, TcId
bndr_id) }

  | Bool
otherwise                          -- No signature
  = do { (TcCoercionN
co, TcSigmaTypeFRR
bndr_ty) <- case Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
exp_pat_ty of
             Check TcSigmaTypeFRR
pat_ty    -> TcLevel
-> TcSigmaTypeFRR
-> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, TcSigmaTypeFRR)
promoteTcType TcLevel
bind_lvl TcSigmaTypeFRR
pat_ty
             Infer InferResult
infer_res -> Bool
-> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, TcSigmaTypeFRR)
-> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, TcSigmaTypeFRR)
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, TcSigmaTypeFRR)
 -> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, TcSigmaTypeFRR))
-> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, TcSigmaTypeFRR)
-> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, TcSigmaTypeFRR)
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 { TcSigmaTypeFRR
bndr_ty <- InferResult -> TcM TcSigmaTypeFRR
inferResultToType InferResult
infer_res
                                   ; (TcCoercionN, TcSigmaTypeFRR)
-> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, TcSigmaTypeFRR)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcSigmaTypeFRR -> TcCoercionN
mkTcNomReflCo TcSigmaTypeFRR
bndr_ty, TcSigmaTypeFRR
bndr_ty) }
       ; let bndr_mult :: TcSigmaTypeFRR
bndr_mult = Scaled ExpSigmaTypeFRR -> TcSigmaTypeFRR
forall a. Scaled a -> TcSigmaTypeFRR
scaledMult Scaled ExpSigmaTypeFRR
exp_pat_ty
       ; TcId
bndr_id <- LetBndrSpec -> Name -> TcSigmaTypeFRR -> TcSigmaTypeFRR -> TcM TcId
newLetBndr LetBndrSpec
no_gen Name
bndr_name TcSigmaTypeFRR
bndr_mult TcSigmaTypeFRR
bndr_ty
       ; String -> SDoc -> TcRn ()
traceTc String
"tcPatBndr(nosig)" ([SDoc] -> SDoc
vcat [ TcLevel -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcLevel
bind_lvl
                                          , Scaled ExpSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr Scaled ExpSigmaTypeFRR
exp_pat_ty, TcSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaTypeFRR
bndr_ty, TcCoercionN -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcCoercionN
co
                                          , TcId -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcId
bndr_id ])
       ; (HsWrapper, TcId) -> TcM (HsWrapper, TcId)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcCoercionN -> HsWrapper
mkWpCastN TcCoercionN
co, TcId
bndr_id) }

tcPatBndr PatEnv
_ Name
bndr_name Scaled ExpSigmaTypeFRR
pat_ty
  = do { let pat_mult :: TcSigmaTypeFRR
pat_mult = Scaled ExpSigmaTypeFRR -> TcSigmaTypeFRR
forall a. Scaled a -> TcSigmaTypeFRR
scaledMult Scaled ExpSigmaTypeFRR
pat_ty
       ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
expTypeToType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
       ; String -> SDoc -> TcRn ()
traceTc String
"tcPatBndr(not let)" (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
bndr_name SDoc -> SDoc -> SDoc
$$ TcSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaTypeFRR
pat_ty)
       ; (HsWrapper, TcId) -> TcM (HsWrapper, TcId)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper
idHsWrapper, Name -> TcSigmaTypeFRR -> TcSigmaTypeFRR -> TcId
mkLocalIdOrCoVar Name
bndr_name TcSigmaTypeFRR
pat_mult TcSigmaTypeFRR
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 -> TcSigmaTypeFRR -> TcSigmaTypeFRR -> TcM TcId
newLetBndr LetBndrSpec
LetLclBndr Name
name TcSigmaTypeFRR
w TcSigmaTypeFRR
ty
  = do { Name
mono_name <- Name -> TcM Name
cloneLocalName Name
name
       ; TcId -> TcM TcId
forall (m :: * -> *) a. Monad m => a -> m a
return (HasDebugCallStack =>
Name -> TcSigmaTypeFRR -> TcSigmaTypeFRR -> TcId
Name -> TcSigmaTypeFRR -> TcSigmaTypeFRR -> TcId
mkLocalId Name
mono_name TcSigmaTypeFRR
w TcSigmaTypeFRR
ty) }
newLetBndr (LetGblBndr TcPragEnv
prags) Name
name TcSigmaTypeFRR
w TcSigmaTypeFRR
ty
  = TcId -> [LSig GhcRn] -> TcM TcId
addInlinePrags (HasDebugCallStack =>
Name -> TcSigmaTypeFRR -> TcSigmaTypeFRR -> TcId
Name -> TcSigmaTypeFRR -> TcSigmaTypeFRR -> TcId
mkLocalId Name
name TcSigmaTypeFRR
w TcSigmaTypeFRR
ty) (TcPragEnv -> Name -> [LSig GhcRn]
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 -> TcSigmaTypeFRR -> TcM HsWrapper
tc_sub_type PatEnv
penv ExpSigmaTypeFRR
t1 TcSigmaTypeFRR
t2 = CtOrigin
-> UserTypeCtxt
-> ExpSigmaTypeFRR
-> TcSigmaTypeFRR
-> TcM HsWrapper
tcSubTypePat (PatEnv -> CtOrigin
pe_orig PatEnv
penv) UserTypeCtxt
GenSigCtxt ExpSigmaTypeFRR
t1 TcSigmaTypeFRR
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 out -> Checker [inp] [out]
tcMultiple :: Checker inp out -> Checker [inp] [out]
tcMultiple Checker inp out
tc_pat PatEnv
penv [inp]
args TcM r
thing_inside
  = do  { [ErrCtxt]
err_ctxt <- TcM [ErrCtxt]
getErrCtxt
        ; let loop :: PatEnv -> [inp] -> TcM ([out], r)
loop PatEnv
_ []
                = do { r
res <- TcM r
thing_inside
                     ; ([out], r) -> TcM ([out], r)
forall (m :: * -> *) a. Monad m => a -> m a
return ([], r
res) }

              loop PatEnv
penv (inp
arg:[inp]
args)
                = do { (out
p', ([out]
ps', r
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
$
                                   PatEnv -> [inp] -> TcM ([out], r)
loop PatEnv
penv [inp]
args
                -- setErrCtxt: restore context before doing the next pattern
                -- See Note [Nesting] above

                     ; ([out], r) -> TcM ([out], r)
forall (m :: * -> *) a. Monad m => a -> m a
return (out
p'out -> [out] -> [out]
forall a. a -> [a] -> [a]
:[out]
ps', r
res) }

        ; PatEnv -> [inp] -> TcM ([out], r)
loop PatEnv
penv [inp]
args }

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

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

--------------------
-- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
checkManyPattern :: Scaled a -> TcM HsWrapper
checkManyPattern :: Scaled a -> TcM HsWrapper
checkManyPattern Scaled a
pat_ty = CtOrigin -> TcSigmaTypeFRR -> TcSigmaTypeFRR -> TcM HsWrapper
tcSubMult CtOrigin
NonLinearPatternOrigin TcSigmaTypeFRR
Many (Scaled a -> TcSigmaTypeFRR
forall a. Scaled a -> TcSigmaTypeFRR
scaledMult Scaled a
pat_ty)

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

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

  VarPat XVarPat GhcRn
x (L l name) -> do
        { (HsWrapper
wrap, TcId
id) <- PatEnv -> Name -> Scaled ExpSigmaTypeFRR -> TcM (HsWrapper, TcId)
tcPatBndr PatEnv
penv Name
name Scaled ExpSigmaTypeFRR
pat_ty
        ; (r
res, HsWrapper
mult_wrap) <- Name -> TcSigmaTypeFRR -> TcM r -> TcM (r, HsWrapper)
forall a. Name -> TcSigmaTypeFRR -> TcM a -> TcM (a, HsWrapper)
tcCheckUsage Name
name (Scaled ExpSigmaTypeFRR -> TcSigmaTypeFRR
forall a. Scaled a -> TcSigmaTypeFRR
scaledMult Scaled ExpSigmaTypeFRR
pat_ty) (TcM r -> TcM (r, HsWrapper)) -> TcM r -> TcM (r, HsWrapper)
forall a b. (a -> b) -> a -> b
$
                              Name -> TcId -> TcM r -> TcM r
forall a. Name -> TcId -> TcM a -> TcM a
tcExtendIdEnv1 Name
name TcId
id TcM r
thing_inside
            -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat (HsWrapper
wrap HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
mult_wrap) (XVarPat GhcTc -> LIdP GhcTc -> Pat GhcTc
forall p. XVarPat p -> LIdP p -> Pat p
VarPat XVarPat GhcTc
XVarPat GhcRn
x (SrcSpanAnnN -> TcId -> GenLocated SrcSpanAnnN TcId
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
l TcId
id)) TcSigmaTypeFRR
pat_ty, r
res) }

  ParPat XParPat GhcRn
x LHsToken "(" GhcRn
lpar LPat GhcRn
pat LHsToken ")" GhcRn
rpar -> do
        { (GenLocated SrcSpanAnnA (Pat GhcTc)
pat', r
res) <- Scaled ExpSigmaTypeFRR
-> PatEnv -> LPat GhcRn -> TcM r -> TcM (LPat GhcTc, r)
Scaled ExpSigmaTypeFRR -> Checker (LPat GhcRn) (LPat GhcTc)
tc_lpat Scaled ExpSigmaTypeFRR
pat_ty PatEnv
penv LPat GhcRn
pat TcM r
thing_inside
        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (XParPat GhcTc
-> LHsToken "(" GhcTc
-> LPat GhcTc
-> LHsToken ")" GhcTc
-> Pat GhcTc
forall p.
XParPat p -> LHsToken "(" p -> LPat p -> LHsToken ")" p -> Pat p
ParPat XParPat GhcTc
XParPat GhcRn
x LHsToken "(" GhcTc
LHsToken "(" GhcRn
lpar LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat' LHsToken ")" GhcTc
LHsToken ")" GhcRn
rpar, r
res) }

  BangPat XBangPat GhcRn
x LPat GhcRn
pat -> do
        { (GenLocated SrcSpanAnnA (Pat GhcTc)
pat', r
res) <- Scaled ExpSigmaTypeFRR
-> PatEnv -> LPat GhcRn -> TcM r -> TcM (LPat GhcTc, r)
Scaled ExpSigmaTypeFRR -> Checker (LPat GhcRn) (LPat GhcTc)
tc_lpat Scaled ExpSigmaTypeFRR
pat_ty PatEnv
penv LPat GhcRn
pat TcM r
thing_inside
        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (XBangPat GhcTc -> LPat GhcTc -> Pat GhcTc
forall p. XBangPat p -> LPat p -> Pat p
BangPat XBangPat GhcTc
XBangPat GhcRn
x LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat', r
res) }

  LazyPat XLazyPat GhcRn
x LPat GhcRn
pat -> do
        { HsWrapper
mult_wrap <- Scaled ExpSigmaTypeFRR -> TcM HsWrapper
forall a. Scaled a -> TcM HsWrapper
checkManyPattern Scaled ExpSigmaTypeFRR
pat_ty
            -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
        ; (GenLocated SrcSpanAnnA (Pat GhcTc)
pat', (r
res, WantedConstraints
pat_ct))
                <- Scaled ExpSigmaTypeFRR
-> PatEnv
-> LPat GhcRn
-> TcM (r, WantedConstraints)
-> TcM (LPat GhcTc, (r, WantedConstraints))
Scaled ExpSigmaTypeFRR -> Checker (LPat GhcRn) (LPat GhcTc)
tc_lpat Scaled ExpSigmaTypeFRR
pat_ty (PatEnv -> PatEnv
makeLazy PatEnv
penv) LPat GhcRn
pat (TcM (r, WantedConstraints)
 -> TcM (LPat GhcTc, (r, WantedConstraints)))
-> TcM (r, WantedConstraints)
-> TcM (LPat GhcTc, (r, WantedConstraints))
forall a b. (a -> b) -> a -> b
$
                   TcM r -> TcM (r, WantedConstraints)
forall a. TcM a -> TcM (a, WantedConstraints)
captureConstraints TcM r
thing_inside
                -- Ignore refined penv', revert to penv

        ; WantedConstraints -> TcRn ()
emitConstraints WantedConstraints
pat_ct
        -- captureConstraints/extendConstraints:
        --   see Note [Hopping the LIE in lazy patterns]

        -- Check that the expected pattern type is itself lifted
        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
        ; TcCoercionN
_ <- Maybe TypedThing
-> TcSigmaTypeFRR -> TcSigmaTypeFRR -> TcM TcCoercionN
unifyType Maybe TypedThing
forall a. Maybe a
Nothing (HasDebugCallStack => TcSigmaTypeFRR -> TcSigmaTypeFRR
TcSigmaTypeFRR -> TcSigmaTypeFRR
tcTypeKind TcSigmaTypeFRR
pat_ty) TcSigmaTypeFRR
liftedTypeKind

        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat HsWrapper
mult_wrap (XLazyPat GhcTc -> LPat GhcTc -> Pat GhcTc
forall p. XLazyPat p -> LPat p -> Pat p
LazyPat XLazyPat GhcTc
XLazyPat GhcRn
x LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat') TcSigmaTypeFRR
pat_ty, r
res) }

  WildPat XWildPat GhcRn
_ -> do
        { HsWrapper
mult_wrap <- Scaled ExpSigmaTypeFRR -> TcM HsWrapper
forall a. Scaled a -> TcM HsWrapper
checkManyPattern Scaled ExpSigmaTypeFRR
pat_ty
            -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
        ; r
res <- TcM r
thing_inside
        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
expTypeToType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat HsWrapper
mult_wrap (XWildPat GhcTc -> Pat GhcTc
forall p. XWildPat p -> Pat p
WildPat XWildPat GhcTc
TcSigmaTypeFRR
pat_ty) TcSigmaTypeFRR
pat_ty, r
res) }

  AsPat XAsPat GhcRn
x (L nm_loc name) LPat GhcRn
pat -> do
        { HsWrapper
mult_wrap <- Scaled ExpSigmaTypeFRR -> TcM HsWrapper
forall a. Scaled a -> TcM HsWrapper
checkManyPattern Scaled ExpSigmaTypeFRR
pat_ty
            -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
        ; (HsWrapper
wrap, TcId
bndr_id) <- SrcSpanAnnN -> TcM (HsWrapper, TcId) -> TcM (HsWrapper, TcId)
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnN
nm_loc (PatEnv -> Name -> Scaled ExpSigmaTypeFRR -> TcM (HsWrapper, TcId)
tcPatBndr PatEnv
penv Name
name Scaled ExpSigmaTypeFRR
pat_ty)
        ; (GenLocated SrcSpanAnnA (Pat GhcTc)
pat', r
res) <- Name
-> TcId
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
forall a. Name -> TcId -> TcM a -> TcM a
tcExtendIdEnv1 Name
name TcId
bndr_id (IOEnv
   (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
 -> IOEnv
      (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
forall a b. (a -> b) -> a -> b
$
                         Scaled ExpSigmaTypeFRR
-> PatEnv -> LPat GhcRn -> TcM r -> TcM (LPat GhcTc, r)
Scaled ExpSigmaTypeFRR -> Checker (LPat GhcRn) (LPat GhcTc)
tc_lpat (Scaled ExpSigmaTypeFRR
pat_ty Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
forall a b. Scaled a -> b -> Scaled b
`scaledSet`(TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType (TcSigmaTypeFRR -> ExpSigmaTypeFRR)
-> TcSigmaTypeFRR -> ExpSigmaTypeFRR
forall a b. (a -> b) -> a -> b
$ TcId -> TcSigmaTypeFRR
idType TcId
bndr_id))
                                 PatEnv
penv LPat GhcRn
pat TcM r
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!
        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat (HsWrapper
wrap HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
mult_wrap) (XAsPat GhcTc -> LIdP GhcTc -> LPat GhcTc -> Pat GhcTc
forall p. XAsPat p -> LIdP p -> LPat p -> Pat p
AsPat XAsPat GhcTc
XAsPat GhcRn
x (SrcSpanAnnN -> TcId -> GenLocated SrcSpanAnnN TcId
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
nm_loc TcId
bndr_id) LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat') TcSigmaTypeFRR
pat_ty, r
res) }

  ViewPat XViewPat GhcRn
_ LHsExpr GhcRn
expr LPat GhcRn
pat -> do
        { HsWrapper
mult_wrap <- Scaled ExpSigmaTypeFRR -> TcM HsWrapper
forall a. Scaled a -> TcM HsWrapper
checkManyPattern 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.

        ; (GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr',TcSigmaTypeFRR
expr_ty) <- LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcSigmaTypeFRR)
tcInferRho LHsExpr GhcRn
expr
               -- Note [View patterns and polymorphism]

         -- Expression must be a function
        ; let herald :: ExpectedFunTyOrigin
herald = HsExpr GhcRn -> ExpectedFunTyOrigin
ExpectedFunTyViewPat (HsExpr GhcRn -> ExpectedFunTyOrigin)
-> HsExpr GhcRn -> ExpectedFunTyOrigin
forall a b. (a -> b) -> a -> b
$ GenLocated SrcSpanAnnA (HsExpr GhcRn) -> HsExpr GhcRn
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
expr
        ; (HsWrapper
expr_wrap1, Scaled TcSigmaTypeFRR
_mult TcSigmaTypeFRR
inf_arg_ty, TcSigmaTypeFRR
inf_res_sigma)
            <- ExpectedFunTyOrigin
-> Maybe TypedThing
-> (Arity, [Scaled TcSigmaTypeFRR])
-> TcSigmaTypeFRR
-> TcM (HsWrapper, Scaled TcSigmaTypeFRR, TcSigmaTypeFRR)
matchActualFunTySigma ExpectedFunTyOrigin
herald (TypedThing -> Maybe TypedThing
forall a. a -> Maybe a
Just (TypedThing -> Maybe TypedThing)
-> (HsExpr GhcRn -> TypedThing) -> HsExpr GhcRn -> Maybe TypedThing
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsExpr GhcRn -> TypedThing
HsExprRnThing (HsExpr GhcRn -> Maybe TypedThing)
-> HsExpr GhcRn -> Maybe TypedThing
forall a b. (a -> b) -> a -> b
$ GenLocated SrcSpanAnnA (HsExpr GhcRn) -> HsExpr GhcRn
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
expr) (Arity
1,[]) TcSigmaTypeFRR
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
        ; HsWrapper
expr_wrap2 <- PatEnv -> ExpSigmaTypeFRR -> TcSigmaTypeFRR -> TcM HsWrapper
tc_sub_type PatEnv
penv (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty) TcSigmaTypeFRR
inf_arg_ty
            -- expr_wrap2 :: pat_ty "->" inf_arg_ty

         -- Pattern must have inf_res_sigma
        ; (GenLocated SrcSpanAnnA (Pat GhcTc)
pat', r
res) <- Scaled ExpSigmaTypeFRR
-> PatEnv -> LPat GhcRn -> TcM r -> TcM (LPat GhcTc, r)
Scaled ExpSigmaTypeFRR -> Checker (LPat GhcRn) (LPat GhcTc)
tc_lpat (Scaled ExpSigmaTypeFRR
pat_ty Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
forall a b. Scaled a -> b -> Scaled b
`scaledSet` TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType TcSigmaTypeFRR
inf_res_sigma) PatEnv
penv LPat GhcRn
pat TcM r
thing_inside

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

        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Pat GhcTc, r) -> TcM (Pat GhcTc, r))
-> (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall a b. (a -> b) -> a -> b
$ (XViewPat GhcTc -> LHsExpr GhcTc -> LPat GhcTc -> Pat GhcTc
forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat XViewPat GhcTc
TcSigmaTypeFRR
pat_ty (HsWrapper -> LHsExpr GhcTc -> LHsExpr GhcTc
mkLHsWrap HsWrapper
expr_wrap LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr') LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat', r
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 matchActualFunTySigma returns anyway.
-}

-- Type signatures in patterns
-- See Note [Pattern coercions] below
  SigPat XSigPat GhcRn
_ LPat GhcRn
pat HsPatSigType (NoGhcTc GhcRn)
sig_ty -> do
        { (TcSigmaTypeFRR
inner_ty, [(Name, TcId)]
tv_binds, [(Name, TcId)]
wcs, HsWrapper
wrap) <- Bool
-> HsPatSigType GhcRn
-> ExpSigmaTypeFRR
-> TcM (TcSigmaTypeFRR, [(Name, TcId)], [(Name, TcId)], HsWrapper)
tcPatSig (PatEnv -> Bool
inPatBind PatEnv
penv)
                                                            HsPatSigType GhcRn
HsPatSigType (NoGhcTc GhcRn)
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.
        ; (GenLocated SrcSpanAnnA (Pat GhcTc)
pat', r
res) <- [(Name, TcId)]
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
forall r. [(Name, TcId)] -> TcM r -> TcM r
tcExtendNameTyVarEnv [(Name, TcId)]
wcs      (IOEnv
   (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
 -> IOEnv
      (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
forall a b. (a -> b) -> a -> b
$
                         [(Name, TcId)]
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
forall r. [(Name, TcId)] -> TcM r -> TcM r
tcExtendNameTyVarEnv [(Name, TcId)]
tv_binds (IOEnv
   (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
 -> IOEnv
      (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (Pat GhcTc), r)
forall a b. (a -> b) -> a -> b
$
                         Scaled ExpSigmaTypeFRR
-> PatEnv -> LPat GhcRn -> TcM r -> TcM (LPat GhcTc, r)
Scaled ExpSigmaTypeFRR -> Checker (LPat GhcRn) (LPat GhcTc)
tc_lpat (Scaled ExpSigmaTypeFRR
pat_ty Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
forall a b. Scaled a -> b -> Scaled b
`scaledSet` TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType TcSigmaTypeFRR
inner_ty) PatEnv
penv LPat GhcRn
pat TcM r
thing_inside
        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat HsWrapper
wrap (XSigPat GhcTc
-> LPat GhcTc -> HsPatSigType (NoGhcTc GhcTc) -> Pat GhcTc
forall p. XSigPat p -> LPat p -> HsPatSigType (NoGhcTc p) -> Pat p
SigPat XSigPat GhcTc
TcSigmaTypeFRR
inner_ty LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat' HsPatSigType (NoGhcTc GhcTc)
HsPatSigType (NoGhcTc GhcRn)
sig_ty) TcSigmaTypeFRR
pat_ty, r
res) }

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

  -- Necessarily a built-in list pattern, not an overloaded list pattern.
  -- See Note [Desugaring overloaded list patterns].
  ListPat XListPat GhcRn
_ [LPat GhcRn]
pats -> do
        { (HsWrapper
coi, TcSigmaTypeFRR
elt_ty) <- (TcSigmaTypeFRR
 -> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, TcSigmaTypeFRR))
-> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, TcSigmaTypeFRR)
forall a.
(TcSigmaTypeFRR -> TcM (TcCoercionN, a))
-> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, a)
matchExpectedPatTy TcSigmaTypeFRR
-> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercionN, TcSigmaTypeFRR)
matchExpectedListTy PatEnv
penv (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
        ; ([GenLocated SrcSpanAnnA (Pat GhcTc)]
pats', r
res) <- Checker
  (GenLocated SrcSpanAnnA (Pat GhcRn))
  (GenLocated SrcSpanAnnA (Pat GhcTc))
-> PatEnv
-> [GenLocated SrcSpanAnnA (Pat GhcRn)]
-> TcM r
-> TcM ([GenLocated SrcSpanAnnA (Pat GhcTc)], r)
forall inp out. Checker inp out -> Checker [inp] [out]
tcMultiple (Scaled ExpSigmaTypeFRR -> Checker (LPat GhcRn) (LPat GhcTc)
tc_lpat (Scaled ExpSigmaTypeFRR
pat_ty Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
forall a b. Scaled a -> b -> Scaled b
`scaledSet` TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType TcSigmaTypeFRR
elt_ty))
                                     PatEnv
penv [LPat GhcRn]
[GenLocated SrcSpanAnnA (Pat GhcRn)]
pats TcM r
thing_inside
        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat HsWrapper
coi
                         (XListPat GhcTc -> [LPat GhcTc] -> Pat GhcTc
forall p. XListPat p -> [LPat p] -> Pat p
ListPat XListPat GhcTc
TcSigmaTypeFRR
elt_ty [LPat GhcTc]
[GenLocated SrcSpanAnnA (Pat GhcTc)]
pats') TcSigmaTypeFRR
pat_ty, r
res)
}

  TuplePat XTuplePat GhcRn
_ [LPat GhcRn]
pats Boxity
boxity -> do
        { let arity :: Arity
arity = [GenLocated SrcSpanAnnA (Pat GhcRn)] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [LPat GhcRn]
[GenLocated SrcSpanAnnA (Pat GhcRn)]
pats
              tc :: TyCon
tc = Boxity -> Arity -> TyCon
tupleTyCon Boxity
boxity Arity
arity
              -- NB: tupleTyCon does not flatten 1-tuples
              -- See Note [Don't flatten tuples from HsSyn] in GHC.Core.Make
        ; Arity -> TcRn ()
checkTupSize Arity
arity
        ; (HsWrapper
coi, [TcSigmaTypeFRR]
arg_tys) <- (TcSigmaTypeFRR -> TcM (TcCoercionN, [TcSigmaTypeFRR]))
-> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, [TcSigmaTypeFRR])
forall a.
(TcSigmaTypeFRR -> TcM (TcCoercionN, a))
-> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, a)
matchExpectedPatTy (TyCon -> TcSigmaTypeFRR -> TcM (TcCoercionN, [TcSigmaTypeFRR])
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 :: [TcSigmaTypeFRR]
con_arg_tys = case Boxity
boxity of Boxity
Unboxed -> Arity -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
forall a. Arity -> [a] -> [a]
drop Arity
arity [TcSigmaTypeFRR]
arg_tys
                                           Boxity
Boxed   -> [TcSigmaTypeFRR]
arg_tys
        ; ([GenLocated SrcSpanAnnA (Pat GhcTc)]
pats', r
res) <- [Scaled ExpSigmaTypeFRR]
-> PatEnv -> [LPat GhcRn] -> TcM r -> TcM ([LPat GhcTc], r)
[Scaled ExpSigmaTypeFRR] -> Checker [LPat GhcRn] [LPat GhcTc]
tc_lpats ((TcSigmaTypeFRR -> Scaled ExpSigmaTypeFRR)
-> [TcSigmaTypeFRR] -> [Scaled ExpSigmaTypeFRR]
forall a b. (a -> b) -> [a] -> [b]
map (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
forall a b. Scaled a -> b -> Scaled b
scaledSet Scaled ExpSigmaTypeFRR
pat_ty (ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR)
-> (TcSigmaTypeFRR -> ExpSigmaTypeFRR)
-> TcSigmaTypeFRR
-> Scaled ExpSigmaTypeFRR
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType) [TcSigmaTypeFRR]
con_arg_tys)
                                   PatEnv
penv [LPat GhcRn]
pats TcM r
thing_inside

        ; DynFlags
dflags <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
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 :: Pat GhcTc
unmangled_result = XTuplePat GhcTc -> [LPat GhcTc] -> Boxity -> Pat GhcTc
forall p. XTuplePat p -> [LPat p] -> Boxity -> Pat p
TuplePat [TcSigmaTypeFRR]
XTuplePat GhcTc
con_arg_tys [LPat GhcTc]
[GenLocated SrcSpanAnnA (Pat GhcTc)]
pats' Boxity
boxity
                                 -- pat_ty /= pat_ty iff coi /= IdCo
              possibly_mangled_result :: Pat GhcTc
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 NoExtField
XLazyPat GhcTc
noExtField (Pat GhcTc -> GenLocated SrcSpanAnnA (Pat GhcTc)
forall a an. a -> LocatedAn an a
noLocA Pat GhcTc
unmangled_result)
                | Bool
otherwise        = Pat GhcTc
unmangled_result

        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
        ; Bool -> TcRn ()
forall (m :: * -> *). (HasCallStack, Applicative m) => Bool -> m ()
massert ([TcSigmaTypeFRR]
con_arg_tys [TcSigmaTypeFRR] -> [GenLocated SrcSpanAnnA (Pat GhcRn)] -> Bool
forall a b. [a] -> [b] -> Bool
`equalLength` [LPat GhcRn]
[GenLocated SrcSpanAnnA (Pat GhcRn)]
pats) -- Syntactically enforced
        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat HsWrapper
coi Pat GhcTc
possibly_mangled_result TcSigmaTypeFRR
pat_ty, r
res)
        }

  SumPat XSumPat GhcRn
_ LPat GhcRn
pat Arity
alt Arity
arity  -> do
        { let tc :: TyCon
tc = Arity -> TyCon
sumTyCon Arity
arity
        ; (HsWrapper
coi, [TcSigmaTypeFRR]
arg_tys) <- (TcSigmaTypeFRR -> TcM (TcCoercionN, [TcSigmaTypeFRR]))
-> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, [TcSigmaTypeFRR])
forall a.
(TcSigmaTypeFRR -> TcM (TcCoercionN, a))
-> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, a)
matchExpectedPatTy (TyCon -> TcSigmaTypeFRR -> TcM (TcCoercionN, [TcSigmaTypeFRR])
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 :: [TcSigmaTypeFRR]
con_arg_tys = Arity -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
forall a. Arity -> [a] -> [a]
drop Arity
arity [TcSigmaTypeFRR]
arg_tys
        ; (GenLocated SrcSpanAnnA (Pat GhcTc)
pat', r
res) <- Scaled ExpSigmaTypeFRR
-> PatEnv -> LPat GhcRn -> TcM r -> TcM (LPat GhcTc, r)
Scaled ExpSigmaTypeFRR -> Checker (LPat GhcRn) (LPat GhcTc)
tc_lpat (Scaled ExpSigmaTypeFRR
pat_ty Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
forall a b. Scaled a -> b -> Scaled b
`scaledSet` TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType ([TcSigmaTypeFRR]
con_arg_tys [TcSigmaTypeFRR] -> Arity -> TcSigmaTypeFRR
forall a. Outputable a => [a] -> Arity -> a
`getNth` (Arity
alt Arity -> Arity -> Arity
forall a. Num a => a -> a -> a
- Arity
1)))
                                 PatEnv
penv LPat GhcRn
pat TcM r
thing_inside
        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat HsWrapper
coi (XSumPat GhcTc -> LPat GhcTc -> Arity -> Arity -> Pat GhcTc
forall p. XSumPat p -> LPat p -> Arity -> Arity -> Pat p
SumPat [TcSigmaTypeFRR]
XSumPat GhcTc
con_arg_tys LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat' Arity
alt Arity
arity) TcSigmaTypeFRR
pat_ty
                 , r
res)
        }

------------------------
-- Data constructors
  ConPat XConPat GhcRn
_ XRec GhcRn (ConLikeP GhcRn)
con HsConPatDetails GhcRn
arg_pats ->
    PatEnv
-> LocatedN Name
-> Scaled ExpSigmaTypeFRR
-> HsConPatDetails GhcRn
-> TcM r
-> TcM (Pat GhcTc, r)
forall a.
PatEnv
-> LocatedN Name
-> Scaled ExpSigmaTypeFRR
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTc, a)
tcConPat PatEnv
penv XRec GhcRn (ConLikeP GhcRn)
LocatedN Name
con Scaled ExpSigmaTypeFRR
pat_ty HsConPatDetails GhcRn
arg_pats TcM r
thing_inside

------------------------
-- Literal patterns
  LitPat XLitPat GhcRn
x HsLit GhcRn
simple_lit -> do
        { let lit_ty :: TcSigmaTypeFRR
lit_ty = HsLit GhcRn -> TcSigmaTypeFRR
forall (p :: Pass). HsLit (GhcPass p) -> TcSigmaTypeFRR
hsLitType HsLit GhcRn
simple_lit
        ; HsWrapper
wrap   <- PatEnv -> ExpSigmaTypeFRR -> TcSigmaTypeFRR -> TcM HsWrapper
tc_sub_type PatEnv
penv (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty) TcSigmaTypeFRR
lit_ty
        ; r
res    <- TcM r
thing_inside
        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return ( HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat HsWrapper
wrap (XLitPat GhcTc -> HsLit GhcTc -> Pat GhcTc
forall p. XLitPat p -> HsLit p -> Pat p
LitPat XLitPat GhcTc
XLitPat GhcRn
x (HsLit GhcRn -> HsLit GhcTc
forall (p1 :: Pass) (p2 :: Pass).
HsLit (GhcPass p1) -> HsLit (GhcPass p2)
convertLit HsLit GhcRn
simple_lit)) TcSigmaTypeFRR
pat_ty
                 , r
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 GhcRn
_ (L l over_lit) Maybe (SyntaxExpr GhcRn)
mb_neg SyntaxExpr GhcRn
eq -> do
        { HsWrapper
mult_wrap <- Scaled ExpSigmaTypeFRR -> TcM HsWrapper
forall a. Scaled a -> TcM HsWrapper
checkManyPattern 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 :: CtOrigin
orig = HsOverLit GhcRn -> CtOrigin
LiteralOrigin HsOverLit GhcRn
over_lit
        ; ((HsOverLit GhcTc
lit', Maybe SyntaxExprTc
mb_neg'), SyntaxExprTc
eq')
            <- CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> ExpSigmaTypeFRR
-> ([TcSigmaTypeFRR]
    -> [TcSigmaTypeFRR] -> TcM (HsOverLit GhcTc, Maybe SyntaxExprTc))
-> TcM ((HsOverLit GhcTc, Maybe SyntaxExprTc), SyntaxExprTc)
forall a.
CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> ExpSigmaTypeFRR
-> ([TcSigmaTypeFRR] -> [TcSigmaTypeFRR] -> TcM a)
-> TcM (a, SyntaxExprTc)
tcSyntaxOp CtOrigin
orig SyntaxExprRn
SyntaxExpr GhcRn
eq [ExpSigmaTypeFRR -> SyntaxOpType
SynType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty), SyntaxOpType
SynAny]
                          (TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType TcSigmaTypeFRR
boolTy) (([TcSigmaTypeFRR]
  -> [TcSigmaTypeFRR] -> TcM (HsOverLit GhcTc, Maybe SyntaxExprTc))
 -> TcM ((HsOverLit GhcTc, Maybe SyntaxExprTc), SyntaxExprTc))
-> ([TcSigmaTypeFRR]
    -> [TcSigmaTypeFRR] -> TcM (HsOverLit GhcTc, Maybe SyntaxExprTc))
-> TcM ((HsOverLit GhcTc, Maybe SyntaxExprTc), SyntaxExprTc)
forall a b. (a -> b) -> a -> b
$
               \ [TcSigmaTypeFRR
neg_lit_ty] [TcSigmaTypeFRR]
_ ->
               let new_over_lit :: TcSigmaTypeFRR -> TcM (HsOverLit GhcTc)
new_over_lit TcSigmaTypeFRR
lit_ty = HsOverLit GhcRn -> ExpSigmaTypeFRR -> TcM (HsOverLit GhcTc)
newOverloadedLit HsOverLit GhcRn
over_lit
                                           (TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType TcSigmaTypeFRR
lit_ty)
               in case Maybe (SyntaxExpr GhcRn)
mb_neg of
                 Maybe (SyntaxExpr GhcRn)
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
<$> TcSigmaTypeFRR -> TcM (HsOverLit GhcTc)
new_over_lit TcSigmaTypeFRR
neg_lit_ty
                 Just SyntaxExpr GhcRn
neg -> -- Negative literal
                             -- The 'negate' is re-mappable syntax
                   (SyntaxExprTc -> Maybe SyntaxExprTc)
-> (HsOverLit GhcTc, SyntaxExprTc)
-> (HsOverLit GhcTc, Maybe SyntaxExprTc)
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
-> ([TcSigmaTypeFRR] -> [TcSigmaTypeFRR] -> TcM (HsOverLit GhcTc))
-> IOEnv (Env TcGblEnv TcLclEnv) (HsOverLit GhcTc, SyntaxExprTc)
forall a.
CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> ExpSigmaTypeFRR
-> ([TcSigmaTypeFRR] -> [TcSigmaTypeFRR] -> TcM a)
-> TcM (a, SyntaxExprTc)
tcSyntaxOp CtOrigin
orig SyntaxExprRn
SyntaxExpr GhcRn
neg [SyntaxOpType
SynRho] (TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType TcSigmaTypeFRR
neg_lit_ty) (([TcSigmaTypeFRR] -> [TcSigmaTypeFRR] -> TcM (HsOverLit GhcTc))
 -> IOEnv (Env TcGblEnv TcLclEnv) (HsOverLit GhcTc, SyntaxExprTc))
-> ([TcSigmaTypeFRR] -> [TcSigmaTypeFRR] -> TcM (HsOverLit GhcTc))
-> IOEnv (Env TcGblEnv TcLclEnv) (HsOverLit GhcTc, SyntaxExprTc)
forall a b. (a -> b) -> a -> b
$
                    \ [TcSigmaTypeFRR
lit_ty] [TcSigmaTypeFRR]
_ -> TcSigmaTypeFRR -> TcM (HsOverLit GhcTc)
new_over_lit TcSigmaTypeFRR
lit_ty)
                     -- applied to a closed literal: linearity doesn't matter as
                     -- literals are typed in an empty environment, hence have
                     -- all multiplicities.

        ; r
res <- TcM r
thing_inside
        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat HsWrapper
mult_wrap (XNPat GhcTc
-> XRec GhcTc (HsOverLit GhcTc)
-> Maybe (SyntaxExpr GhcTc)
-> SyntaxExpr GhcTc
-> Pat GhcTc
forall p.
XNPat p
-> XRec p (HsOverLit p)
-> Maybe (SyntaxExpr p)
-> SyntaxExpr p
-> Pat p
NPat XNPat GhcTc
TcSigmaTypeFRR
pat_ty (SrcAnn NoEpAnns
-> HsOverLit GhcTc
-> GenLocated (SrcAnn NoEpAnns) (HsOverLit GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
l HsOverLit GhcTc
lit') Maybe SyntaxExprTc
Maybe (SyntaxExpr GhcTc)
mb_neg' SyntaxExprTc
SyntaxExpr GhcTc
eq') TcSigmaTypeFRR
pat_ty, r
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 GhcRn
_ (L nm_loc name)
               (L loc lit) HsOverLit GhcRn
_ SyntaxExpr GhcRn
ge SyntaxExpr GhcRn
minus -> do
        { HsWrapper
mult_wrap <- Scaled ExpSigmaTypeFRR -> TcM HsWrapper
forall a. Scaled a -> TcM HsWrapper
checkManyPattern Scaled ExpSigmaTypeFRR
pat_ty
            -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.
        ; let pat_exp_ty :: ExpSigmaTypeFRR
pat_exp_ty = Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty
              orig :: CtOrigin
orig = HsOverLit GhcRn -> CtOrigin
LiteralOrigin HsOverLit GhcRn
lit
        ; (HsOverLit GhcTc
lit1', SyntaxExprTc
ge')
            <- CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> ExpSigmaTypeFRR
-> ([TcSigmaTypeFRR] -> [TcSigmaTypeFRR] -> TcM (HsOverLit GhcTc))
-> IOEnv (Env TcGblEnv TcLclEnv) (HsOverLit GhcTc, SyntaxExprTc)
forall a.
CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> ExpSigmaTypeFRR
-> ([TcSigmaTypeFRR] -> [TcSigmaTypeFRR] -> TcM a)
-> TcM (a, SyntaxExprTc)
tcSyntaxOp CtOrigin
orig SyntaxExprRn
SyntaxExpr GhcRn
ge [ExpSigmaTypeFRR -> SyntaxOpType
SynType ExpSigmaTypeFRR
pat_exp_ty, SyntaxOpType
SynRho]
                                  (TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType TcSigmaTypeFRR
boolTy) (([TcSigmaTypeFRR] -> [TcSigmaTypeFRR] -> TcM (HsOverLit GhcTc))
 -> IOEnv (Env TcGblEnv TcLclEnv) (HsOverLit GhcTc, SyntaxExprTc))
-> ([TcSigmaTypeFRR] -> [TcSigmaTypeFRR] -> TcM (HsOverLit GhcTc))
-> IOEnv (Env TcGblEnv TcLclEnv) (HsOverLit GhcTc, SyntaxExprTc)
forall a b. (a -> b) -> a -> b
$
               \ [TcSigmaTypeFRR
lit1_ty] [TcSigmaTypeFRR]
_ ->
               HsOverLit GhcRn -> ExpSigmaTypeFRR -> TcM (HsOverLit GhcTc)
newOverloadedLit HsOverLit GhcRn
lit (TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType TcSigmaTypeFRR
lit1_ty)
        ; ((HsOverLit GhcTc
lit2', HsWrapper
minus_wrap, TcId
bndr_id), SyntaxExprTc
minus')
            <- CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> SyntaxOpType
-> ([TcSigmaTypeFRR]
    -> [TcSigmaTypeFRR] -> TcM (HsOverLit GhcTc, HsWrapper, TcId))
-> TcM ((HsOverLit GhcTc, HsWrapper, TcId), SyntaxExprTc)
forall a.
CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> SyntaxOpType
-> ([TcSigmaTypeFRR] -> [TcSigmaTypeFRR] -> TcM a)
-> TcM (a, SyntaxExprTc)
tcSyntaxOpGen CtOrigin
orig SyntaxExprRn
SyntaxExpr GhcRn
minus [ExpSigmaTypeFRR -> SyntaxOpType
SynType ExpSigmaTypeFRR
pat_exp_ty, SyntaxOpType
SynRho] SyntaxOpType
SynAny (([TcSigmaTypeFRR]
  -> [TcSigmaTypeFRR] -> TcM (HsOverLit GhcTc, HsWrapper, TcId))
 -> TcM ((HsOverLit GhcTc, HsWrapper, TcId), SyntaxExprTc))
-> ([TcSigmaTypeFRR]
    -> [TcSigmaTypeFRR] -> TcM (HsOverLit GhcTc, HsWrapper, TcId))
-> TcM ((HsOverLit GhcTc, HsWrapper, TcId), SyntaxExprTc)
forall a b. (a -> b) -> a -> b
$
               \ [TcSigmaTypeFRR
lit2_ty, TcSigmaTypeFRR
var_ty] [TcSigmaTypeFRR]
_ ->
               do { HsOverLit GhcTc
lit2' <- HsOverLit GhcRn -> ExpSigmaTypeFRR -> TcM (HsOverLit GhcTc)
newOverloadedLit HsOverLit GhcRn
lit (TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType TcSigmaTypeFRR
lit2_ty)
                  ; (HsWrapper
wrap, TcId
bndr_id) <- SrcSpanAnnN -> TcM (HsWrapper, TcId) -> TcM (HsWrapper, TcId)
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnN
nm_loc (TcM (HsWrapper, TcId) -> TcM (HsWrapper, TcId))
-> TcM (HsWrapper, TcId) -> TcM (HsWrapper, TcId)
forall a b. (a -> b) -> a -> b
$
                                     PatEnv -> Name -> Scaled ExpSigmaTypeFRR -> TcM (HsWrapper, TcId)
tcPatBndr PatEnv
penv Name
name (ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
forall a. a -> Scaled a
unrestricted (ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR)
-> ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
forall a b. (a -> b) -> a -> b
$ TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType TcSigmaTypeFRR
var_ty)
                           -- co :: var_ty ~ idType bndr_id

                           -- minus_wrap is applicable to minus'
                  ; (HsOverLit GhcTc, HsWrapper, TcId)
-> TcM (HsOverLit GhcTc, HsWrapper, TcId)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsOverLit GhcTc
lit2', HsWrapper
wrap, TcId
bndr_id) }

        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType ExpSigmaTypeFRR
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
        ; IOEnv (Env TcGblEnv TcLclEnv) Bool -> TcRn () -> TcRn ()
forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
unlessM (Extension -> IOEnv (Env TcGblEnv TcLclEnv) Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.RebindableSyntax) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
          do { Class
icls <- Name -> TcM Class
tcLookupClass Name
integralClassName
             ; CtOrigin -> [TcSigmaTypeFRR] -> TcRn ()
instStupidTheta CtOrigin
orig [Class -> [TcSigmaTypeFRR] -> TcSigmaTypeFRR
mkClassPred Class
icls [TcSigmaTypeFRR
pat_ty]] }

        ; r
res <- Name -> TcId -> TcM r -> TcM r
forall a. Name -> TcId -> TcM a -> TcM a
tcExtendIdEnv1 Name
name TcId
bndr_id TcM r
thing_inside

        ; let minus'' :: SyntaxExprTc
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 :: HsExpr GhcTc -> [HsWrapper] -> HsWrapper -> SyntaxExprTc
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' :: Pat GhcTc
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
TcSigmaTypeFRR
pat_ty (SrcSpanAnnN -> TcId -> GenLocated SrcSpanAnnN TcId
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
nm_loc TcId
bndr_id) (SrcAnn NoEpAnns
-> HsOverLit GhcTc
-> GenLocated (SrcAnn NoEpAnns) (HsOverLit GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
loc HsOverLit GhcTc
lit1') HsOverLit GhcTc
lit2'
                               SyntaxExprTc
SyntaxExpr GhcTc
ge' SyntaxExprTc
SyntaxExpr GhcTc
minus''
        ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat HsWrapper
mult_wrap Pat GhcTc
pat' TcSigmaTypeFRR
pat_ty, r
res) }

-- HsSpliced is an annotation produced by 'GHC.Rename.Splice.rnSplicePat'.
-- 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 XSplicePat GhcRn
_ HsSplice GhcRn
splice -> case HsSplice GhcRn
splice of
    (HsSpliced XSpliced GhcRn
_ ThModFinalizers
mod_finalizers (HsSplicedPat Pat GhcRn
pat)) -> do
      { ThModFinalizers -> TcRn ()
addModFinalizersWithLclEnv ThModFinalizers
mod_finalizers
      ; Scaled ExpSigmaTypeFRR
-> PatEnv -> Pat GhcRn -> TcM r -> TcM (Pat GhcTc, r)
Scaled ExpSigmaTypeFRR -> Checker (Pat GhcRn) (Pat GhcTc)
tc_pat Scaled ExpSigmaTypeFRR
pat_ty PatEnv
penv Pat GhcRn
pat TcM r
thing_inside }
    HsSplice GhcRn
_ -> String -> TcM (Pat GhcTc, r)
forall a. String -> a
panic String
"invalid splice in splice pat"

  XPat (HsPatExpanded lpat rpat) -> do
    { (Pat GhcTc
rpat', r
res) <- Scaled ExpSigmaTypeFRR
-> PatEnv -> Pat GhcRn -> TcM r -> TcM (Pat GhcTc, r)
Scaled ExpSigmaTypeFRR -> Checker (Pat GhcRn) (Pat GhcTc)
tc_pat Scaled ExpSigmaTypeFRR
pat_ty PatEnv
penv Pat GhcRn
rpat TcM r
thing_inside
    ; (Pat GhcTc, r) -> TcM (Pat GhcTc, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (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 GhcRn -> Pat GhcTc -> XXPatGhcTc
ExpansionPat Pat GhcRn
lpat Pat GhcTc
rpat', r
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 GhcRn
-> ExpSigmaTypeFRR
-> TcM (TcSigmaTypeFRR, [(Name, TcId)], [(Name, TcId)], HsWrapper)
tcPatSig Bool
in_pat_bind HsPatSigType GhcRn
sig ExpSigmaTypeFRR
res_ty
 = do  { ([(Name, TcId)]
sig_wcs, [(Name, TcId)]
sig_tvs, TcSigmaTypeFRR
sig_ty) <- UserTypeCtxt
-> HoleMode
-> HsPatSigType GhcRn
-> ContextKind
-> TcM ([(Name, TcId)], [(Name, TcId)], TcSigmaTypeFRR)
tcHsPatSigType UserTypeCtxt
PatSigCtxt HoleMode
HM_Sig HsPatSigType GhcRn
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

        ; if [(Name, TcId)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Name, TcId)]
sig_tvs then do {
                -- Just do the subsumption check and return
                  HsWrapper
wrap <- (TidyEnv -> TcM (TidyEnv, SDoc)) -> TcM HsWrapper -> TcM HsWrapper
forall a. (TidyEnv -> TcM (TidyEnv, SDoc)) -> TcM a -> TcM a
addErrCtxtM (TcSigmaTypeFRR -> TidyEnv -> TcM (TidyEnv, SDoc)
mk_msg TcSigmaTypeFRR
sig_ty) (TcM HsWrapper -> TcM HsWrapper) -> TcM HsWrapper -> TcM HsWrapper
forall a b. (a -> b) -> a -> b
$
                          CtOrigin
-> UserTypeCtxt
-> ExpSigmaTypeFRR
-> TcSigmaTypeFRR
-> TcM HsWrapper
tcSubTypePat CtOrigin
PatSigOrigin UserTypeCtxt
PatSigCtxt ExpSigmaTypeFRR
res_ty TcSigmaTypeFRR
sig_ty
                ; (TcSigmaTypeFRR, [(Name, TcId)], [(Name, TcId)], HsWrapper)
-> TcM (TcSigmaTypeFRR, [(Name, TcId)], [(Name, TcId)], HsWrapper)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcSigmaTypeFRR
sig_ty, [], [(Name, TcId)]
sig_wcs, HsWrapper
wrap)
        } else 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 ([(Name, TcId)] -> TcRnMessage
patBindSigErr [(Name, TcId)]
sig_tvs))

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

        -- Phew!
        ; (TcSigmaTypeFRR, [(Name, TcId)], [(Name, TcId)], HsWrapper)
-> TcM (TcSigmaTypeFRR, [(Name, TcId)], [(Name, TcId)], HsWrapper)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcSigmaTypeFRR
sig_ty, [(Name, TcId)]
sig_tvs, [(Name, TcId)]
sig_wcs, HsWrapper
wrap)
        } }
  where
    mk_msg :: TcSigmaTypeFRR -> TidyEnv -> TcM (TidyEnv, SDoc)
mk_msg TcSigmaTypeFRR
sig_ty TidyEnv
tidy_env
       = do { (TidyEnv
tidy_env, TcSigmaTypeFRR
sig_ty) <- TidyEnv -> TcSigmaTypeFRR -> TcM (TidyEnv, TcSigmaTypeFRR)
zonkTidyTcType TidyEnv
tidy_env TcSigmaTypeFRR
sig_ty
            ; TcSigmaTypeFRR
res_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType ExpSigmaTypeFRR
res_ty   -- should be filled in by now
            ; (TidyEnv
tidy_env, TcSigmaTypeFRR
res_ty) <- TidyEnv -> TcSigmaTypeFRR -> TcM (TidyEnv, TcSigmaTypeFRR)
zonkTidyTcType TidyEnv
tidy_env TcSigmaTypeFRR
res_ty
            ; let msg :: SDoc
msg = [SDoc] -> SDoc
vcat [ SDoc -> Arity -> SDoc -> SDoc
hang (String -> SDoc
text String
"When checking that the pattern signature:")
                                  Arity
4 (TcSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaTypeFRR
sig_ty)
                             , Arity -> SDoc -> SDoc
nest Arity
2 (SDoc -> Arity -> SDoc -> SDoc
hang (String -> SDoc
text String
"fits the type of its context:")
                                          Arity
2 (TcSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaTypeFRR
res_ty)) ]
            ; (TidyEnv, SDoc) -> TcM (TidyEnv, SDoc)
forall (m :: * -> *) a. Monad m => a -> m a
return (TidyEnv
tidy_env, SDoc
msg) }

patBindSigErr :: [(Name,TcTyVar)] -> TcRnMessage
patBindSigErr :: [(Name, TcId)] -> TcRnMessage
patBindSigErr [(Name, TcId)]
sig_tvs
  = DiagnosticMessage -> TcRnMessage
forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage (DiagnosticMessage -> TcRnMessage)
-> DiagnosticMessage -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints (SDoc -> DiagnosticMessage) -> SDoc -> DiagnosticMessage
forall a b. (a -> b) -> a -> b
$
    SDoc -> Arity -> SDoc -> SDoc
hang (String -> SDoc
text String
"You cannot bind scoped type variable" SDoc -> SDoc -> SDoc
<> [(Name, TcId)] -> SDoc
forall a. [a] -> SDoc
plural [(Name, TcId)]
sig_tvs
          SDoc -> SDoc -> SDoc
<+> [Name] -> SDoc
forall a. Outputable a => [a] -> SDoc
pprQuotedList (((Name, TcId) -> Name) -> [(Name, TcId)] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name, TcId) -> Name
forall a b. (a, b) -> a
fst [(Name, TcId)]
sig_tvs))
       Arity
2 (String -> SDoc
text String
"in a pattern binding signature")


{- *********************************************************************
*                                                                      *
        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 :: PatEnv
-> LocatedN Name
-> Scaled ExpSigmaTypeFRR
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTc, a)
tcConPat PatEnv
penv con_lname :: LocatedN Name
con_lname@(L SrcSpanAnnN
_ Name
con_name) Scaled ExpSigmaTypeFRR
pat_ty HsConPatDetails GhcRn
arg_pats TcM a
thing_inside
  = do  { ConLike
con_like <- Name -> TcM ConLike
tcLookupConLike Name
con_name
        ; case ConLike
con_like of
            RealDataCon DataCon
data_con -> PatEnv
-> LocatedN Name
-> DataCon
-> Scaled ExpSigmaTypeFRR
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTc, a)
forall a.
PatEnv
-> LocatedN Name
-> DataCon
-> Scaled ExpSigmaTypeFRR
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTc, a)
tcDataConPat PatEnv
penv LocatedN Name
con_lname DataCon
data_con
                                                 Scaled ExpSigmaTypeFRR
pat_ty HsConPatDetails GhcRn
arg_pats TcM a
thing_inside
            PatSynCon PatSyn
pat_syn -> PatEnv
-> LocatedN Name
-> PatSyn
-> Scaled ExpSigmaTypeFRR
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTc, a)
forall a.
PatEnv
-> LocatedN Name
-> PatSyn
-> Scaled ExpSigmaTypeFRR
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTc, a)
tcPatSynPat PatEnv
penv LocatedN Name
con_lname PatSyn
pat_syn
                                             Scaled ExpSigmaTypeFRR
pat_ty HsConPatDetails GhcRn
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 { Bool
mono_local_binds <- Extension -> IOEnv (Env TcGblEnv TcLclEnv) Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.MonoLocalBinds
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
mono_local_binds (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
           TcRnMessage -> TcRn ()
addDiagnostic TcRnMessage
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 :: PatEnv -> LocatedN Name -> DataCon
             -> Scaled ExpSigmaTypeFRR        -- Type of the pattern
             -> HsConPatDetails GhcRn -> TcM a
             -> TcM (Pat GhcTc, a)
tcDataConPat :: PatEnv
-> LocatedN Name
-> DataCon
-> Scaled ExpSigmaTypeFRR
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTc, a)
tcDataConPat PatEnv
penv (L SrcSpanAnnN
con_span Name
con_name) DataCon
data_con Scaled ExpSigmaTypeFRR
pat_ty_scaled
             HsConPatDetails GhcRn
arg_pats TcM a
thing_inside
  = do  { let tycon :: TyCon
tycon = DataCon -> TyCon
dataConTyCon DataCon
data_con
                  -- For data families this is the representation tycon
              ([TcId]
univ_tvs, [TcId]
ex_tvs, [EqSpec]
eq_spec, [TcSigmaTypeFRR]
theta, [Scaled TcSigmaTypeFRR]
arg_tys, TcSigmaTypeFRR
_)
                = DataCon
-> ([TcId], [TcId], [EqSpec], [TcSigmaTypeFRR],
    [Scaled TcSigmaTypeFRR], TcSigmaTypeFRR)
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
        ; (HsWrapper
wrap, [TcSigmaTypeFRR]
ctxt_res_tys) <- PatEnv
-> TyCon
-> Scaled ExpSigmaTypeFRR
-> TcM (HsWrapper, [TcSigmaTypeFRR])
matchExpectedConTy PatEnv
penv TyCon
tycon Scaled ExpSigmaTypeFRR
pat_ty_scaled
        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty_scaled)

          -- Add the stupid theta
        ; SrcSpanAnnN -> TcRn () -> TcRn ()
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnN
con_span (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ DataCon -> [TcSigmaTypeFRR] -> TcRn ()
addDataConStupidTheta DataCon
data_con [TcSigmaTypeFRR]
ctxt_res_tys

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

        ; TCvSubst
tenv1 <- CtOrigin -> [TcId] -> [TcSigmaTypeFRR] -> TcM TCvSubst
instTyVarsWith CtOrigin
PatOrigin [TcId]
univ_tvs [TcSigmaTypeFRR]
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 :: HsMatchContext GhcTc
mc = case PatEnv -> PatCtxt
pe_ctxt PatEnv
penv of
                     LamPat HsMatchContext GhcTc
mc -> HsMatchContext GhcTc
mc
                     LetPat {} -> HsMatchContext GhcTc
forall p. HsMatchContext p
PatBindRhs
        ; SkolemInfo
skol_info <- SkolemInfoAnon -> IOEnv (Env TcGblEnv TcLclEnv) SkolemInfo
forall (m :: * -> *). MonadIO m => SkolemInfoAnon -> m SkolemInfo
mkSkolemInfo (ConLike -> HsMatchContext GhcTc -> SkolemInfoAnon
PatSkol (DataCon -> ConLike
RealDataCon DataCon
data_con) HsMatchContext GhcTc
mc)
        ; (TCvSubst
tenv, [TcId]
ex_tvs') <- SkolemInfo -> TCvSubst -> [TcId] -> TcM (TCvSubst, [TcId])
tcInstSuperSkolTyVarsX SkolemInfo
skol_info TCvSubst
tenv1 [TcId]
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' :: [Scaled TcSigmaTypeFRR]
arg_tys' = HasDebugCallStack =>
TCvSubst -> [Scaled TcSigmaTypeFRR] -> [Scaled TcSigmaTypeFRR]
TCvSubst -> [Scaled TcSigmaTypeFRR] -> [Scaled TcSigmaTypeFRR]
substScaledTys TCvSubst
tenv [Scaled TcSigmaTypeFRR]
arg_tys
              pat_mult :: TcSigmaTypeFRR
pat_mult = Scaled ExpSigmaTypeFRR -> TcSigmaTypeFRR
forall a. Scaled a -> TcSigmaTypeFRR
scaledMult Scaled ExpSigmaTypeFRR
pat_ty_scaled
              arg_tys_scaled :: [Scaled TcSigmaTypeFRR]
arg_tys_scaled = (Scaled TcSigmaTypeFRR -> Scaled TcSigmaTypeFRR)
-> [Scaled TcSigmaTypeFRR] -> [Scaled TcSigmaTypeFRR]
forall a b. (a -> b) -> [a] -> [b]
map (TcSigmaTypeFRR -> Scaled TcSigmaTypeFRR -> Scaled TcSigmaTypeFRR
forall a. TcSigmaTypeFRR -> Scaled a -> Scaled a
scaleScaled TcSigmaTypeFRR
pat_mult) [Scaled TcSigmaTypeFRR]
arg_tys'

        -- This check is necessary to uphold the invariant that 'tcConArgs'
        -- is given argument types with a fixed runtime representation.
        -- See test case T20363.
        ; (Arity -> Scaled TcSigmaTypeFRR -> TcRn ())
-> [Arity] -> [Scaled TcSigmaTypeFRR] -> TcRn ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_
            ( \ Arity
i Scaled TcSigmaTypeFRR
arg_sty ->
              HasDebugCallStack =>
FixedRuntimeRepContext -> TcSigmaTypeFRR -> TcRn ()
FixedRuntimeRepContext -> TcSigmaTypeFRR -> TcRn ()
hasFixedRuntimeRep_syntactic
                (ExprOrPat -> DataCon -> Arity -> FixedRuntimeRepContext
FRRDataConArg ExprOrPat
Pattern DataCon
data_con Arity
i)
                (Scaled TcSigmaTypeFRR -> TcSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled TcSigmaTypeFRR
arg_sty)
            )
            [Arity
1..]
            [Scaled TcSigmaTypeFRR]
arg_tys'

        ; String -> SDoc -> TcRn ()
traceTc String
"tcConPat" ([SDoc] -> SDoc
vcat [ String -> SDoc
text String
"con_name:" SDoc -> SDoc -> SDoc
<+> Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
con_name
                                   , String -> SDoc
text String
"univ_tvs:" SDoc -> SDoc -> SDoc
<+> [TcId] -> SDoc
pprTyVars [TcId]
univ_tvs
                                   , String -> SDoc
text String
"ex_tvs:" SDoc -> SDoc -> SDoc
<+> [TcId] -> SDoc
pprTyVars [TcId]
ex_tvs
                                   , String -> SDoc
text String
"eq_spec:" SDoc -> SDoc -> SDoc
<+> [EqSpec] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [EqSpec]
eq_spec
                                   , String -> SDoc
text String
"theta:" SDoc -> SDoc -> SDoc
<+> [TcSigmaTypeFRR] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcSigmaTypeFRR]
theta
                                   , String -> SDoc
text String
"ex_tvs':" SDoc -> SDoc -> SDoc
<+> [TcId] -> SDoc
pprTyVars [TcId]
ex_tvs'
                                   , String -> SDoc
text String
"ctxt_res_tys:" SDoc -> SDoc -> SDoc
<+> [TcSigmaTypeFRR] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcSigmaTypeFRR]
ctxt_res_tys
                                   , String -> SDoc
text String
"pat_ty:" SDoc -> SDoc -> SDoc
<+> TcSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaTypeFRR
pat_ty
                                   , String -> SDoc
text String
"arg_tys':" SDoc -> SDoc -> SDoc
<+> [Scaled TcSigmaTypeFRR] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Scaled TcSigmaTypeFRR]
arg_tys'
                                   , String -> SDoc
text String
"arg_pats" SDoc -> SDoc -> SDoc
<+> HsConDetails
  (HsPatSigType GhcRn)
  (GenLocated SrcSpanAnnA (Pat GhcRn))
  (HsRecFields GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn)))
-> SDoc
forall a. Outputable a => a -> SDoc
ppr HsConDetails
  (HsPatSigType GhcRn)
  (GenLocated SrcSpanAnnA (Pat GhcRn))
  (HsRecFields GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn)))
HsConPatDetails GhcRn
arg_pats ])
        ; if [TcId] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TcId]
ex_tvs Bool -> Bool -> Bool
&& [EqSpec] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [EqSpec]
eq_spec Bool -> Bool -> Bool
&& [TcSigmaTypeFRR] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TcSigmaTypeFRR]
theta
          then do { -- The common case; no class bindings etc
                    -- (see Note [Arrows and patterns])
                    (HsConDetails
  (HsPatSigType GhcRn)
  (GenLocated SrcSpanAnnA (Pat GhcTc))
  (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
arg_pats', a
res) <- ConLike
-> [Scaled TcSigmaTypeFRR]
-> TCvSubst
-> PatEnv
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (HsConPatDetails GhcTc, a)
ConLike
-> [Scaled TcSigmaTypeFRR]
-> TCvSubst
-> Checker (HsConPatDetails GhcRn) (HsConPatDetails GhcTc)
tcConArgs (DataCon -> ConLike
RealDataCon DataCon
data_con) [Scaled TcSigmaTypeFRR]
arg_tys_scaled
                                                  TCvSubst
tenv PatEnv
penv HsConPatDetails GhcRn
arg_pats TcM a
thing_inside
                  ; let res_pat :: Pat GhcTc
res_pat = ConPat :: forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat { pat_con :: XRec GhcTc (ConLikeP GhcTc)
pat_con = XRec GhcTc (ConLikeP GhcTc)
GenLocated SrcSpanAnnN ConLike
header
                                         , pat_args :: HsConPatDetails GhcTc
pat_args = HsConDetails
  (HsPatSigType GhcRn)
  (GenLocated SrcSpanAnnA (Pat GhcTc))
  (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
HsConPatDetails GhcTc
arg_pats'
                                         , pat_con_ext :: XConPat GhcTc
pat_con_ext = ConPatTc :: [TcSigmaTypeFRR]
-> [TcId] -> [TcId] -> TcEvBinds -> HsWrapper -> ConPatTc
ConPatTc
                                           { cpt_tvs :: [TcId]
cpt_tvs = [], cpt_dicts :: [TcId]
cpt_dicts = []
                                           , cpt_binds :: TcEvBinds
cpt_binds = TcEvBinds
emptyTcEvBinds
                                           , cpt_arg_tys :: [TcSigmaTypeFRR]
cpt_arg_tys = [TcSigmaTypeFRR]
ctxt_res_tys
                                           , cpt_wrap :: HsWrapper
cpt_wrap = HsWrapper
idHsWrapper
                                           }
                                         }

                  ; (Pat GhcTc, a) -> TcM (Pat GhcTc, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat HsWrapper
wrap Pat GhcTc
res_pat TcSigmaTypeFRR
pat_ty, a
res) }

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

        ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not ([EqSpec] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [EqSpec]
eq_spec) Bool -> Bool -> Bool
|| (TcSigmaTypeFRR -> Bool) -> [TcSigmaTypeFRR] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any TcSigmaTypeFRR -> Bool
isEqPred [TcSigmaTypeFRR]
theta) TcRn ()
warnMonoLocalBinds

        ; [TcId]
given <- [TcSigmaTypeFRR] -> TcM [TcId]
newEvVars [TcSigmaTypeFRR]
theta'
        ; (TcEvBinds
ev_binds, (HsConDetails
  (HsPatSigType GhcRn)
  (GenLocated SrcSpanAnnA (Pat GhcTc))
  (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
arg_pats', a
res))
             <- SkolemInfoAnon
-> [TcId]
-> [TcId]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsConDetails
        (HsPatSigType GhcRn)
        (GenLocated SrcSpanAnnA (Pat GhcTc))
        (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
      a)
-> TcM
     (TcEvBinds,
      (HsConDetails
         (HsPatSigType GhcRn)
         (GenLocated SrcSpanAnnA (Pat GhcTc))
         (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
       a))
forall result.
SkolemInfoAnon
-> [TcId] -> [TcId] -> TcM result -> TcM (TcEvBinds, result)
checkConstraints (SkolemInfo -> SkolemInfoAnon
getSkolemInfo SkolemInfo
skol_info) [TcId]
ex_tvs' [TcId]
given (IOEnv
   (Env TcGblEnv TcLclEnv)
   (HsConDetails
      (HsPatSigType GhcRn)
      (GenLocated SrcSpanAnnA (Pat GhcTc))
      (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
    a)
 -> TcM
      (TcEvBinds,
       (HsConDetails
          (HsPatSigType GhcRn)
          (GenLocated SrcSpanAnnA (Pat GhcTc))
          (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
        a)))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsConDetails
        (HsPatSigType GhcRn)
        (GenLocated SrcSpanAnnA (Pat GhcTc))
        (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
      a)
-> TcM
     (TcEvBinds,
      (HsConDetails
         (HsPatSigType GhcRn)
         (GenLocated SrcSpanAnnA (Pat GhcTc))
         (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
       a))
forall a b. (a -> b) -> a -> b
$
                ConLike
-> [Scaled TcSigmaTypeFRR]
-> TCvSubst
-> PatEnv
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (HsConPatDetails GhcTc, a)
ConLike
-> [Scaled TcSigmaTypeFRR]
-> TCvSubst
-> Checker (HsConPatDetails GhcRn) (HsConPatDetails GhcTc)
tcConArgs (DataCon -> ConLike
RealDataCon DataCon
data_con) [Scaled TcSigmaTypeFRR]
arg_tys_scaled TCvSubst
tenv PatEnv
penv HsConPatDetails GhcRn
arg_pats TcM a
thing_inside

        ; let res_pat :: Pat GhcTc
res_pat = ConPat :: forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat
                { pat_con :: XRec GhcTc (ConLikeP GhcTc)
pat_con   = XRec GhcTc (ConLikeP GhcTc)
GenLocated SrcSpanAnnN ConLike
header
                , pat_args :: HsConPatDetails GhcTc
pat_args  = HsConDetails
  (HsPatSigType GhcRn)
  (GenLocated SrcSpanAnnA (Pat GhcTc))
  (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
HsConPatDetails GhcTc
arg_pats'
                , pat_con_ext :: XConPat GhcTc
pat_con_ext = ConPatTc :: [TcSigmaTypeFRR]
-> [TcId] -> [TcId] -> TcEvBinds -> HsWrapper -> ConPatTc
ConPatTc
                  { cpt_tvs :: [TcId]
cpt_tvs   = [TcId]
ex_tvs'
                  , cpt_dicts :: [TcId]
cpt_dicts = [TcId]
given
                  , cpt_binds :: TcEvBinds
cpt_binds = TcEvBinds
ev_binds
                  , cpt_arg_tys :: [TcSigmaTypeFRR]
cpt_arg_tys = [TcSigmaTypeFRR]
ctxt_res_tys
                  , cpt_wrap :: HsWrapper
cpt_wrap  = HsWrapper
idHsWrapper
                  }
                }
        ; (Pat GhcTc, a) -> TcM (Pat GhcTc, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat HsWrapper
wrap Pat GhcTc
res_pat TcSigmaTypeFRR
pat_ty, a
res)
        } }

tcPatSynPat :: PatEnv -> LocatedN Name -> PatSyn
            -> Scaled ExpSigmaType         -- ^ Type of the pattern
            -> HsConPatDetails GhcRn -> TcM a
            -> TcM (Pat GhcTc, a)
tcPatSynPat :: PatEnv
-> LocatedN Name
-> PatSyn
-> Scaled ExpSigmaTypeFRR
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTc, a)
tcPatSynPat PatEnv
penv (L SrcSpanAnnN
con_span Name
con_name) PatSyn
pat_syn Scaled ExpSigmaTypeFRR
pat_ty HsConPatDetails GhcRn
arg_pats TcM a
thing_inside
  = do  { let ([TcId]
univ_tvs, [TcSigmaTypeFRR]
req_theta, [TcId]
ex_tvs, [TcSigmaTypeFRR]
prov_theta, [Scaled TcSigmaTypeFRR]
arg_tys, TcSigmaTypeFRR
ty) = PatSyn
-> ([TcId], [TcSigmaTypeFRR], [TcId], [TcSigmaTypeFRR],
    [Scaled TcSigmaTypeFRR], TcSigmaTypeFRR)
patSynSig PatSyn
pat_syn

        ; (TCvSubst
subst, [TcId]
univ_tvs') <- [TcId] -> TcM (TCvSubst, [TcId])
newMetaTyVars [TcId]
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 :: [TcSigmaTypeFRR]
all_arg_tys = TcSigmaTypeFRR
ty TcSigmaTypeFRR -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
forall a. a -> [a] -> [a]
: [TcSigmaTypeFRR]
prov_theta [TcSigmaTypeFRR] -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
forall a. [a] -> [a] -> [a]
++ ((Scaled TcSigmaTypeFRR -> TcSigmaTypeFRR)
-> [Scaled TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
forall a b. (a -> b) -> [a] -> [b]
map Scaled TcSigmaTypeFRR -> TcSigmaTypeFRR
forall a. Scaled a -> a
scaledThing [Scaled TcSigmaTypeFRR]
arg_tys)
        ; ConLike -> [TcId] -> [TcSigmaTypeFRR] -> PatEnv -> TcRn ()
checkGADT (PatSyn -> ConLike
PatSynCon PatSyn
pat_syn) [TcId]
ex_tvs [TcSigmaTypeFRR]
all_arg_tys PatEnv
penv

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

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

        ; let ty' :: TcSigmaTypeFRR
ty'         = HasDebugCallStack => TCvSubst -> TcSigmaTypeFRR -> TcSigmaTypeFRR
TCvSubst -> TcSigmaTypeFRR -> TcSigmaTypeFRR
substTy TCvSubst
tenv TcSigmaTypeFRR
ty
              arg_tys' :: [Scaled TcSigmaTypeFRR]
arg_tys'    = HasDebugCallStack =>
TCvSubst -> [Scaled TcSigmaTypeFRR] -> [Scaled TcSigmaTypeFRR]
TCvSubst -> [Scaled TcSigmaTypeFRR] -> [Scaled TcSigmaTypeFRR]
substScaledTys TCvSubst
tenv [Scaled TcSigmaTypeFRR]
arg_tys
              pat_mult :: TcSigmaTypeFRR
pat_mult    = Scaled ExpSigmaTypeFRR -> TcSigmaTypeFRR
forall a. Scaled a -> TcSigmaTypeFRR
scaledMult Scaled ExpSigmaTypeFRR
pat_ty
              arg_tys_scaled :: [Scaled TcSigmaTypeFRR]
arg_tys_scaled = (Scaled TcSigmaTypeFRR -> Scaled TcSigmaTypeFRR)
-> [Scaled TcSigmaTypeFRR] -> [Scaled TcSigmaTypeFRR]
forall a b. (a -> b) -> [a] -> [b]
map (TcSigmaTypeFRR -> Scaled TcSigmaTypeFRR -> Scaled TcSigmaTypeFRR
forall a. TcSigmaTypeFRR -> Scaled a -> Scaled a
scaleScaled TcSigmaTypeFRR
pat_mult) [Scaled TcSigmaTypeFRR]
arg_tys'
              prov_theta' :: [TcSigmaTypeFRR]
prov_theta' = HasDebugCallStack =>
TCvSubst -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
TCvSubst -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
substTheta TCvSubst
tenv [TcSigmaTypeFRR]
prov_theta
              req_theta' :: [TcSigmaTypeFRR]
req_theta'  = HasDebugCallStack =>
TCvSubst -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
TCvSubst -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
substTheta TCvSubst
tenv [TcSigmaTypeFRR]
req_theta

        ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ((TcSigmaTypeFRR -> Bool) -> [TcSigmaTypeFRR] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any TcSigmaTypeFRR -> Bool
isEqPred [TcSigmaTypeFRR]
prov_theta) TcRn ()
warnMonoLocalBinds

        ; HsWrapper
mult_wrap <- Scaled ExpSigmaTypeFRR -> TcM HsWrapper
forall a. Scaled a -> TcM HsWrapper
checkManyPattern Scaled ExpSigmaTypeFRR
pat_ty
            -- See Note [Wrapper returned from tcSubMult] in GHC.Tc.Utils.Unify.

        ; HsWrapper
wrap <- PatEnv -> ExpSigmaTypeFRR -> TcSigmaTypeFRR -> TcM HsWrapper
tc_sub_type PatEnv
penv (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty) TcSigmaTypeFRR
ty'
        ; String -> SDoc -> TcRn ()
traceTc String
"tcPatSynPat" (PatSyn -> SDoc
forall a. Outputable a => a -> SDoc
ppr PatSyn
pat_syn SDoc -> SDoc -> SDoc
$$
                                 Scaled ExpSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr Scaled ExpSigmaTypeFRR
pat_ty SDoc -> SDoc -> SDoc
$$
                                 TcSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaTypeFRR
ty' SDoc -> SDoc -> SDoc
$$
                                 [TcId] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcId]
ex_tvs' SDoc -> SDoc -> SDoc
$$
                                 [TcSigmaTypeFRR] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcSigmaTypeFRR]
prov_theta' SDoc -> SDoc -> SDoc
$$
                                 [TcSigmaTypeFRR] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcSigmaTypeFRR]
req_theta' SDoc -> SDoc -> SDoc
$$
                                 [Scaled TcSigmaTypeFRR] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Scaled TcSigmaTypeFRR]
arg_tys')

        ; [TcId]
prov_dicts' <- [TcSigmaTypeFRR] -> TcM [TcId]
newEvVars [TcSigmaTypeFRR]
prov_theta'


        ; HsWrapper
req_wrap <- CtOrigin -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR] -> TcM HsWrapper
instCall (Name -> CtOrigin
OccurrenceOf Name
con_name) ([TcId] -> [TcSigmaTypeFRR]
mkTyVarTys [TcId]
univ_tvs') [TcSigmaTypeFRR]
req_theta'
                      -- Origin (OccurrenceOf con_name):
                      -- see Note [Call-stack tracing of pattern synonyms]
        ; String -> SDoc -> TcRn ()
traceTc String
"instCall" (HsWrapper -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsWrapper
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 :: [(Arity, Scaled TcSigmaTypeFRR)]
bad_arg_tys = ((Arity, Scaled TcSigmaTypeFRR) -> Bool)
-> [(Arity, Scaled TcSigmaTypeFRR)]
-> [(Arity, Scaled TcSigmaTypeFRR)]
forall a. (a -> Bool) -> [a] -> [a]
filter (\ (Arity
_, Scaled TcSigmaTypeFRR
_ TcSigmaTypeFRR
arg_ty) -> HasDebugCallStack => TcSigmaTypeFRR -> Maybe Levity
TcSigmaTypeFRR -> Maybe Levity
typeLevity_maybe TcSigmaTypeFRR
arg_ty Maybe Levity -> Maybe Levity -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe Levity
forall a. Maybe a
Nothing)
                        ([(Arity, Scaled TcSigmaTypeFRR)]
 -> [(Arity, Scaled TcSigmaTypeFRR)])
-> [(Arity, Scaled TcSigmaTypeFRR)]
-> [(Arity, Scaled TcSigmaTypeFRR)]
forall a b. (a -> b) -> a -> b
$ [Arity]
-> [Scaled TcSigmaTypeFRR] -> [(Arity, Scaled TcSigmaTypeFRR)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Arity
0..] [Scaled TcSigmaTypeFRR]
arg_tys'
        ; Bool -> SDoc -> TcRn ()
forall (m :: * -> *).
(HasCallStack, Applicative m) =>
Bool -> SDoc -> m ()
massertPpr ([(Arity, Scaled TcSigmaTypeFRR)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Arity, Scaled TcSigmaTypeFRR)]
bad_arg_tys) (SDoc -> TcRn ()) -> SDoc -> TcRn ()
forall a b. (a -> b) -> a -> b
$
            [SDoc] -> SDoc
vcat [ String -> SDoc
text String
"tcPatSynPat: pattern arguments do not have a fixed RuntimeRep"
                 , String -> SDoc
text String
"bad_arg_tys:" SDoc -> SDoc -> SDoc
<+> [(Arity, Scaled TcSigmaTypeFRR)] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [(Arity, Scaled TcSigmaTypeFRR)]
bad_arg_tys ]

        ; String -> SDoc -> TcRn ()
traceTc String
"checkConstraints {" SDoc
Outputable.empty
        ; (TcEvBinds
ev_binds, (HsConDetails
  (HsPatSigType GhcRn)
  (GenLocated SrcSpanAnnA (Pat GhcTc))
  (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
arg_pats', a
res))
             <- SkolemInfoAnon
-> [TcId]
-> [TcId]
-> TcM
     (HsConDetails
        (HsPatSigType GhcRn)
        (GenLocated SrcSpanAnnA (Pat GhcTc))
        (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
      a)
-> TcM
     (TcEvBinds,
      (HsConDetails
         (HsPatSigType GhcRn)
         (GenLocated SrcSpanAnnA (Pat GhcTc))
         (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
       a))
forall result.
SkolemInfoAnon
-> [TcId] -> [TcId] -> TcM result -> TcM (TcEvBinds, result)
checkConstraints (SkolemInfo -> SkolemInfoAnon
getSkolemInfo SkolemInfo
skol_info) [TcId]
ex_tvs' [TcId]
prov_dicts' (TcM
   (HsConDetails
      (HsPatSigType GhcRn)
      (GenLocated SrcSpanAnnA (Pat GhcTc))
      (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
    a)
 -> TcM
      (TcEvBinds,
       (HsConDetails
          (HsPatSigType GhcRn)
          (GenLocated SrcSpanAnnA (Pat GhcTc))
          (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
        a)))
-> TcM
     (HsConDetails
        (HsPatSigType GhcRn)
        (GenLocated SrcSpanAnnA (Pat GhcTc))
        (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
      a)
-> TcM
     (TcEvBinds,
      (HsConDetails
         (HsPatSigType GhcRn)
         (GenLocated SrcSpanAnnA (Pat GhcTc))
         (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
       a))
forall a b. (a -> b) -> a -> b
$
                ConLike
-> [Scaled TcSigmaTypeFRR]
-> TCvSubst
-> PatEnv
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (HsConPatDetails GhcTc, a)
ConLike
-> [Scaled TcSigmaTypeFRR]
-> TCvSubst
-> Checker (HsConPatDetails GhcRn) (HsConPatDetails GhcTc)
tcConArgs (PatSyn -> ConLike
PatSynCon PatSyn
pat_syn) [Scaled TcSigmaTypeFRR]
arg_tys_scaled TCvSubst
tenv PatEnv
penv HsConPatDetails GhcRn
arg_pats TcM a
thing_inside

        ; String -> SDoc -> TcRn ()
traceTc String
"checkConstraints }" (TcEvBinds -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcEvBinds
ev_binds)
        ; let res_pat :: Pat GhcTc
res_pat = ConPat :: forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
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  = HsConDetails
  (HsPatSigType GhcRn)
  (GenLocated SrcSpanAnnA (Pat GhcTc))
  (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
HsConPatDetails GhcTc
arg_pats'
                               , pat_con_ext :: XConPat GhcTc
pat_con_ext = ConPatTc :: [TcSigmaTypeFRR]
-> [TcId] -> [TcId] -> TcEvBinds -> HsWrapper -> ConPatTc
ConPatTc
                                 { cpt_tvs :: [TcId]
cpt_tvs   = [TcId]
ex_tvs'
                                 , cpt_dicts :: [TcId]
cpt_dicts = [TcId]
prov_dicts'
                                 , cpt_binds :: TcEvBinds
cpt_binds = TcEvBinds
ev_binds
                                 , cpt_arg_tys :: [TcSigmaTypeFRR]
cpt_arg_tys = [TcId] -> [TcSigmaTypeFRR]
mkTyVarTys [TcId]
univ_tvs'
                                 , cpt_wrap :: HsWrapper
cpt_wrap  = HsWrapper
req_wrap
                                 }
                               }
        ; TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
readExpType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
pat_ty)
        ; (Pat GhcTc, a) -> TcM (Pat GhcTc, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTc -> TcSigmaTypeFRR -> Pat GhcTc
mkHsWrapPat (HsWrapper
wrap HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
mult_wrap) Pat GhcTc
res_pat TcSigmaTypeFRR
pat_ty, a
res) }

{- 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.Canonical.canClassNC 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 :: (TcSigmaTypeFRR -> TcM (TcCoercionN, a))
-> PatEnv -> ExpSigmaTypeFRR -> TcM (HsWrapper, a)
matchExpectedPatTy TcSigmaTypeFRR -> TcM (TcCoercionN, a)
inner_match (PE { pe_orig :: PatEnv -> CtOrigin
pe_orig = CtOrigin
orig }) ExpSigmaTypeFRR
pat_ty
  = do { TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
expTypeToType ExpSigmaTypeFRR
pat_ty
       ; (HsWrapper
wrap, TcSigmaTypeFRR
pat_rho) <- CtOrigin -> TcSigmaTypeFRR -> TcM (HsWrapper, TcSigmaTypeFRR)
topInstantiate CtOrigin
orig TcSigmaTypeFRR
pat_ty
       ; (TcCoercionN
co, a
res) <- TcSigmaTypeFRR -> TcM (TcCoercionN, a)
inner_match TcSigmaTypeFRR
pat_rho
       ; String -> SDoc -> TcRn ()
traceTc String
"matchExpectedPatTy" (TcSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaTypeFRR
pat_ty SDoc -> SDoc -> SDoc
$$ HsWrapper -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsWrapper
wrap)
       ; (HsWrapper, a) -> TcM (HsWrapper, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcCoercionN -> HsWrapper
mkWpCastN (TcCoercionN -> TcCoercionN
mkTcSymCo TcCoercionN
co) HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
wrap, a
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, [TcSigmaTypeFRR])
matchExpectedConTy (PE { pe_orig :: PatEnv -> CtOrigin
pe_orig = CtOrigin
orig }) TyCon
data_tc Scaled ExpSigmaTypeFRR
exp_pat_ty
  | Just (TyCon
fam_tc, [TcSigmaTypeFRR]
fam_args, CoAxiom Unbranched
co_tc) <- TyCon -> Maybe (TyCon, [TcSigmaTypeFRR], CoAxiom Unbranched)
tyConFamInstSig_maybe TyCon
data_tc
         -- Comments refer to Note [Matching constructor patterns]
         -- co_tc :: forall a. T [a] ~ T7 a
  = do { TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
expTypeToType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
exp_pat_ty)
       ; (HsWrapper
wrap, TcSigmaTypeFRR
pat_rho) <- CtOrigin -> TcSigmaTypeFRR -> TcM (HsWrapper, TcSigmaTypeFRR)
topInstantiate CtOrigin
orig TcSigmaTypeFRR
pat_ty

       ; (TCvSubst
subst, [TcId]
tvs') <- [TcId] -> TcM (TCvSubst, [TcId])
newMetaTyVars (TyCon -> [TcId]
tyConTyVars TyCon
data_tc)
             -- tys = [ty1,ty2]

       ; String -> SDoc -> TcRn ()
traceTc String
"matchExpectedConTy" ([SDoc] -> SDoc
vcat [TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
data_tc,
                                             [TcId] -> SDoc
forall a. Outputable a => a -> SDoc
ppr (TyCon -> [TcId]
tyConTyVars TyCon
data_tc),
                                             TyCon -> SDoc
forall a. Outputable a => a -> SDoc
ppr TyCon
fam_tc, [TcSigmaTypeFRR] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcSigmaTypeFRR]
fam_args,
                                             Scaled ExpSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr Scaled ExpSigmaTypeFRR
exp_pat_ty,
                                             TcSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaTypeFRR
pat_ty,
                                             TcSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaTypeFRR
pat_rho, HsWrapper -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsWrapper
wrap])
       ; TcCoercionN
co1 <- Maybe TypedThing
-> TcSigmaTypeFRR -> TcSigmaTypeFRR -> TcM TcCoercionN
unifyType Maybe TypedThing
forall a. Maybe a
Nothing (TyCon -> [TcSigmaTypeFRR] -> TcSigmaTypeFRR
mkTyConApp TyCon
fam_tc (HasDebugCallStack =>
TCvSubst -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
TCvSubst -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
substTys TCvSubst
subst [TcSigmaTypeFRR]
fam_args)) TcSigmaTypeFRR
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' :: [TcSigmaTypeFRR]
tys' = [TcId] -> [TcSigmaTypeFRR]
mkTyVarTys [TcId]
tvs'
             co2 :: TcCoercionN
co2 = CoAxiom Unbranched
-> [TcSigmaTypeFRR] -> [TcCoercionN] -> TcCoercionN
mkTcUnbranchedAxInstCo CoAxiom Unbranched
co_tc [TcSigmaTypeFRR]
tys' []
             -- co2 : T (ty1,ty2) ~R T7 ty1 ty2

             full_co :: TcCoercionN
full_co = HasDebugCallStack => TcCoercionN -> TcCoercionN
TcCoercionN -> TcCoercionN
mkTcSubCo (TcCoercionN -> TcCoercionN
mkTcSymCo TcCoercionN
co1) TcCoercionN -> TcCoercionN -> TcCoercionN
`mkTcTransCo` TcCoercionN
co2
             -- full_co :: pat_rho ~R T7 ty1 ty2

       ; (HsWrapper, [TcSigmaTypeFRR]) -> TcM (HsWrapper, [TcSigmaTypeFRR])
forall (m :: * -> *) a. Monad m => a -> m a
return ( TcCoercionN -> HsWrapper
mkWpCastR TcCoercionN
full_co HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
wrap, [TcSigmaTypeFRR]
tys') }

  | Bool
otherwise
  = do { TcSigmaTypeFRR
pat_ty <- ExpSigmaTypeFRR -> TcM TcSigmaTypeFRR
expTypeToType (Scaled ExpSigmaTypeFRR -> ExpSigmaTypeFRR
forall a. Scaled a -> a
scaledThing Scaled ExpSigmaTypeFRR
exp_pat_ty)
       ; (HsWrapper
wrap, TcSigmaTypeFRR
pat_rho) <- CtOrigin -> TcSigmaTypeFRR -> TcM (HsWrapper, TcSigmaTypeFRR)
topInstantiate CtOrigin
orig TcSigmaTypeFRR
pat_ty
       ; (TcCoercionN
coi, [TcSigmaTypeFRR]
tys) <- TyCon -> TcSigmaTypeFRR -> TcM (TcCoercionN, [TcSigmaTypeFRR])
matchExpectedTyConApp TyCon
data_tc TcSigmaTypeFRR
pat_rho
       ; (HsWrapper, [TcSigmaTypeFRR]) -> TcM (HsWrapper, [TcSigmaTypeFRR])
forall (m :: * -> *) a. Monad m => a -> m a
return (TcCoercionN -> HsWrapper
mkWpCastN (TcCoercionN -> TcCoercionN
mkTcSymCo TcCoercionN
coi) HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
wrap, [TcSigmaTypeFRR]
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 [Typechecking type applications in patterns]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
How should we typecheck type applications in patterns, such as
   f :: Either (Maybe a) [b] -> blah
   f (Left @x @[y] (v::Maybe x)) = blah

It's quite straightforward, and very similar to the treatment of
pattern signatures.

* Step 1: bind the newly-in-scope type variables x and y to fresh
  unification variables, say x0 and y0.

* Step 2: typecheck those type arguments, @x and @[y], to get the
  types x0 and [y0].

* Step 3: Unify those types with the type arguments we expect,
  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.

Wrinkles:

* 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.

* Exactly the same works for existential arguments
     data T where
        MkT :: a -> a -> T
     f :: T -> blah
     f (MkT @x v w) = ...
   Here we create a fresh unification variable x0 for x, and
   unify it with the fresh existential variable bound by the pattern.

* 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 3 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.
-}

tcConArgs :: ConLike
          -> [Scaled TcSigmaTypeFRR]
          -> TCvSubst            -- Instantiating substitution for constructor type
          -> Checker (HsConPatDetails GhcRn) (HsConPatDetails GhcTc)
tcConArgs :: ConLike
-> [Scaled TcSigmaTypeFRR]
-> TCvSubst
-> Checker (HsConPatDetails GhcRn) (HsConPatDetails GhcTc)
tcConArgs ConLike
con_like [Scaled TcSigmaTypeFRR]
arg_tys TCvSubst
tenv PatEnv
penv HsConPatDetails GhcRn
con_args TcM r
thing_inside = case HsConPatDetails GhcRn
con_args of
  PrefixCon [HsPatSigType (NoGhcTc GhcRn)]
type_args [LPat GhcRn]
arg_pats -> do
        { Bool -> TcRnMessage -> TcRn ()
checkTc (Arity
con_arity Arity -> Arity -> Bool
forall a. Eq a => a -> a -> Bool
== Arity
no_of_args)     -- Check correct arity
                  (SDoc -> ConLike -> Arity -> Arity -> TcRnMessage
forall a.
Outputable a =>
SDoc -> a -> Arity -> Arity -> TcRnMessage
arityErr (String -> SDoc
text String
"constructor") ConLike
con_like Arity
con_arity Arity
no_of_args)

              -- forgetting to filter out inferred binders led to #20443
        ; let con_spec_binders :: [VarBndr TcId Specificity]
con_spec_binders = (VarBndr TcId Specificity -> Bool)
-> [VarBndr TcId Specificity] -> [VarBndr TcId Specificity]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Specificity -> Specificity -> Bool
forall a. Eq a => a -> a -> Bool
== Specificity
SpecifiedSpec) (Specificity -> Bool)
-> (VarBndr TcId Specificity -> Specificity)
-> VarBndr TcId Specificity
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VarBndr TcId Specificity -> Specificity
forall tv argf. VarBndr tv argf -> argf
binderArgFlag) ([VarBndr TcId Specificity] -> [VarBndr TcId Specificity])
-> [VarBndr TcId Specificity] -> [VarBndr TcId Specificity]
forall a b. (a -> b) -> a -> b
$
                                 ConLike -> [VarBndr TcId Specificity]
conLikeUserTyVarBinders ConLike
con_like
        ; Bool -> TcRnMessage -> TcRn ()
checkTc ([HsPatSigType GhcRn]
[HsPatSigType (NoGhcTc GhcRn)]
type_args [HsPatSigType GhcRn] -> [VarBndr TcId Specificity] -> Bool
forall a b. [a] -> [b] -> Bool
`leLength` [VarBndr TcId Specificity]
con_spec_binders)
                  (ConLike -> Arity -> Arity -> TcRnMessage
conTyArgArityErr ConLike
con_like ([VarBndr TcId Specificity] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [VarBndr TcId Specificity]
con_spec_binders) ([HsPatSigType GhcRn] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [HsPatSigType GhcRn]
[HsPatSigType (NoGhcTc GhcRn)]
type_args))

        ; let pats_w_tys :: [(GenLocated SrcSpanAnnA (Pat GhcRn), Scaled TcSigmaTypeFRR)]
pats_w_tys = String
-> [GenLocated SrcSpanAnnA (Pat GhcRn)]
-> [Scaled TcSigmaTypeFRR]
-> [(GenLocated SrcSpanAnnA (Pat GhcRn), Scaled TcSigmaTypeFRR)]
forall a b. String -> [a] -> [b] -> [(a, b)]
zipEqual String
"tcConArgs" [LPat GhcRn]
[GenLocated SrcSpanAnnA (Pat GhcRn)]
arg_pats [Scaled TcSigmaTypeFRR]
arg_tys
        ; ([TcSigmaTypeFRR]
type_args', ([GenLocated SrcSpanAnnA (Pat GhcTc)]
arg_pats', r
res))
            <- Checker (HsPatSigType GhcRn) TcSigmaTypeFRR
-> PatEnv
-> [HsPatSigType GhcRn]
-> TcM ([GenLocated SrcSpanAnnA (Pat GhcTc)], r)
-> TcM
     ([TcSigmaTypeFRR], ([GenLocated SrcSpanAnnA (Pat GhcTc)], r))
forall inp out. Checker inp out -> Checker [inp] [out]
tcMultiple Checker (HsPatSigType GhcRn) TcSigmaTypeFRR
tcConTyArg PatEnv
penv [HsPatSigType GhcRn]
[HsPatSigType (NoGhcTc GhcRn)]
type_args (TcM ([GenLocated SrcSpanAnnA (Pat GhcTc)], r)
 -> TcM
      ([TcSigmaTypeFRR], ([GenLocated SrcSpanAnnA (Pat GhcTc)], r)))
-> TcM ([GenLocated SrcSpanAnnA (Pat GhcTc)], r)
-> TcM
     ([TcSigmaTypeFRR], ([GenLocated SrcSpanAnnA (Pat GhcTc)], r))
forall a b. (a -> b) -> a -> b
$
               Checker
  (GenLocated SrcSpanAnnA (Pat GhcRn), Scaled TcSigmaTypeFRR)
  (GenLocated SrcSpanAnnA (Pat GhcTc))
-> PatEnv
-> [(GenLocated SrcSpanAnnA (Pat GhcRn), Scaled TcSigmaTypeFRR)]
-> TcM r
-> TcM ([GenLocated SrcSpanAnnA (Pat GhcTc)], r)
forall inp out. Checker inp out -> Checker [inp] [out]
tcMultiple Checker (LPat GhcRn, Scaled TcSigmaTypeFRR) (LPat GhcTc)
Checker
  (GenLocated SrcSpanAnnA (Pat GhcRn), Scaled TcSigmaTypeFRR)
  (GenLocated SrcSpanAnnA (Pat GhcTc))
tcConArg PatEnv
penv [(GenLocated SrcSpanAnnA (Pat GhcRn), Scaled TcSigmaTypeFRR)]
pats_w_tys TcM r
thing_inside

          -- This unification is straight from Figure 7 of
          -- "Type Variables in Patterns", Haskell'18
        ; [TcCoercionN]
_ <- (TcSigmaTypeFRR -> TcSigmaTypeFRR -> TcM TcCoercionN)
-> [TcSigmaTypeFRR]
-> [TcSigmaTypeFRR]
-> IOEnv (Env TcGblEnv TcLclEnv) [TcCoercionN]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM (Maybe TypedThing
-> TcSigmaTypeFRR -> TcSigmaTypeFRR -> TcM TcCoercionN
unifyType Maybe TypedThing
forall a. Maybe a
Nothing) [TcSigmaTypeFRR]
type_args' (TCvSubst -> [TcId] -> [TcSigmaTypeFRR]
substTyVars TCvSubst
tenv ([TcId] -> [TcSigmaTypeFRR]) -> [TcId] -> [TcSigmaTypeFRR]
forall a b. (a -> b) -> a -> b
$
                                                        [VarBndr TcId Specificity] -> [TcId]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [VarBndr TcId Specificity]
con_spec_binders)
          -- OK to drop coercions here. These unifications are all about
          -- guiding inference based on a user-written type annotation
          -- See Note [Typechecking type applications in patterns]

        ; (HsConDetails
   (HsPatSigType GhcRn)
   (GenLocated SrcSpanAnnA (Pat GhcTc))
   (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
 r)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsConDetails
        (HsPatSigType GhcRn)
        (GenLocated SrcSpanAnnA (Pat GhcTc))
        (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
      r)
forall (m :: * -> *) a. Monad m => a -> m a
return ([HsPatSigType GhcRn]
-> [GenLocated SrcSpanAnnA (Pat GhcTc)]
-> HsConDetails
     (HsPatSigType GhcRn)
     (GenLocated SrcSpanAnnA (Pat GhcTc))
     (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
forall tyarg arg rec.
[tyarg] -> [arg] -> HsConDetails tyarg arg rec
PrefixCon [HsPatSigType GhcRn]
[HsPatSigType (NoGhcTc GhcRn)]
type_args [GenLocated SrcSpanAnnA (Pat GhcTc)]
arg_pats', r
res) }
    where
      con_arity :: Arity
con_arity  = ConLike -> Arity
conLikeArity ConLike
con_like
      no_of_args :: Arity
no_of_args = [GenLocated SrcSpanAnnA (Pat GhcRn)] -> Arity
forall (t :: * -> *) a. Foldable t => t a -> Arity
length [LPat GhcRn]
[GenLocated SrcSpanAnnA (Pat GhcRn)]
arg_pats

  InfixCon LPat GhcRn
p1 LPat GhcRn
p2 -> do
        { Bool -> TcRnMessage -> TcRn ()
checkTc (Arity
con_arity Arity -> Arity -> Bool
forall a. Eq a => a -> a -> Bool
== Arity
2)      -- Check correct arity
                  (SDoc -> ConLike -> Arity -> Arity -> TcRnMessage
forall a.
Outputable a =>
SDoc -> a -> Arity -> Arity -> TcRnMessage
arityErr (String -> SDoc
text String
"constructor") ConLike
con_like Arity
con_arity Arity
2)
        ; let [Scaled TcSigmaTypeFRR
arg_ty1,Scaled TcSigmaTypeFRR
arg_ty2] = [Scaled TcSigmaTypeFRR]
arg_tys       -- This can't fail after the arity check
        ; ([GenLocated SrcSpanAnnA (Pat GhcTc)
p1',GenLocated SrcSpanAnnA (Pat GhcTc)
p2'], r
res) <- Checker
  (GenLocated SrcSpanAnnA (Pat GhcRn), Scaled TcSigmaTypeFRR)
  (GenLocated SrcSpanAnnA (Pat GhcTc))
-> PatEnv
-> [(GenLocated SrcSpanAnnA (Pat GhcRn), Scaled TcSigmaTypeFRR)]
-> TcM r
-> TcM ([GenLocated SrcSpanAnnA (Pat GhcTc)], r)
forall inp out. Checker inp out -> Checker [inp] [out]
tcMultiple Checker (LPat GhcRn, Scaled TcSigmaTypeFRR) (LPat GhcTc)
Checker
  (GenLocated SrcSpanAnnA (Pat GhcRn), Scaled TcSigmaTypeFRR)
  (GenLocated SrcSpanAnnA (Pat GhcTc))
tcConArg PatEnv
penv [(LPat GhcRn
GenLocated SrcSpanAnnA (Pat GhcRn)
p1,Scaled TcSigmaTypeFRR
arg_ty1),(LPat GhcRn
GenLocated SrcSpanAnnA (Pat GhcRn)
p2,Scaled TcSigmaTypeFRR
arg_ty2)]
                                                  TcM r
thing_inside
        ; (HsConDetails
   (HsPatSigType GhcRn)
   (GenLocated SrcSpanAnnA (Pat GhcTc))
   (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
 r)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsConDetails
        (HsPatSigType GhcRn)
        (GenLocated SrcSpanAnnA (Pat GhcTc))
        (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
      r)
forall (m :: * -> *) a. Monad m => a -> m a
return (GenLocated SrcSpanAnnA (Pat GhcTc)
-> GenLocated SrcSpanAnnA (Pat GhcTc)
-> HsConDetails
     (HsPatSigType GhcRn)
     (GenLocated SrcSpanAnnA (Pat GhcTc))
     (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
forall tyarg arg rec. arg -> arg -> HsConDetails tyarg arg rec
InfixCon GenLocated SrcSpanAnnA (Pat GhcTc)
p1' GenLocated SrcSpanAnnA (Pat GhcTc)
p2', r
res) }
    where
      con_arity :: Arity
con_arity  = ConLike -> Arity
conLikeArity ConLike
con_like

  RecCon (HsRecFields [LHsRecField GhcRn (LPat GhcRn)]
rpats Maybe (Located Arity)
dd) -> do
        { ([GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
      (GenLocated SrcSpanAnnA (Pat GhcTc)))]
rpats', r
res) <- Checker
  (GenLocated
     SrcSpanAnnA
     (HsFieldBind
        (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
        (GenLocated SrcSpanAnnA (Pat GhcRn))))
  (GenLocated
     SrcSpanAnnA
     (HsFieldBind
        (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
        (GenLocated SrcSpanAnnA (Pat GhcTc))))
-> PatEnv
-> [GenLocated
      SrcSpanAnnA
      (HsFieldBind
         (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
         (GenLocated SrcSpanAnnA (Pat GhcRn)))]
-> TcM r
-> TcM
     ([GenLocated
         SrcSpanAnnA
         (HsFieldBind
            (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
            (GenLocated SrcSpanAnnA (Pat GhcTc)))],
      r)
forall inp out. Checker inp out -> Checker [inp] [out]
tcMultiple Checker
  (LHsRecField GhcRn (LPat GhcRn)) (LHsRecField GhcTc (LPat GhcTc))
Checker
  (GenLocated
     SrcSpanAnnA
     (HsFieldBind
        (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
        (GenLocated SrcSpanAnnA (Pat GhcRn))))
  (GenLocated
     SrcSpanAnnA
     (HsFieldBind
        (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
        (GenLocated SrcSpanAnnA (Pat GhcTc))))
tc_field PatEnv
penv [LHsRecField GhcRn (LPat GhcRn)]
[GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
      (GenLocated SrcSpanAnnA (Pat GhcRn)))]
rpats TcM r
thing_inside
        ; (HsConDetails
   (HsPatSigType GhcRn)
   (GenLocated SrcSpanAnnA (Pat GhcTc))
   (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
 r)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsConDetails
        (HsPatSigType GhcRn)
        (GenLocated SrcSpanAnnA (Pat GhcTc))
        (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))),
      r)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))
-> HsConDetails
     (HsPatSigType GhcRn)
     (GenLocated SrcSpanAnnA (Pat GhcTc))
     (HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc)))
forall tyarg arg rec. rec -> HsConDetails tyarg arg rec
RecCon ([LHsRecField GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))]
-> Maybe (Located Arity)
-> HsRecFields GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))
forall p arg.
[LHsRecField p arg] -> Maybe (Located Arity) -> HsRecFields p arg
HsRecFields [LHsRecField GhcTc (GenLocated SrcSpanAnnA (Pat GhcTc))]
[GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
      (GenLocated SrcSpanAnnA (Pat GhcTc)))]
rpats' Maybe (Located Arity)
dd), r
res) }
    where
      tc_field :: Checker (LHsRecField GhcRn (LPat GhcRn))
                          (LHsRecField GhcTc (LPat GhcTc))
      tc_field :: PatEnv
-> LHsRecField GhcRn (LPat GhcRn)
-> TcM r
-> TcM (LHsRecField GhcTc (LPat GhcTc), r)
tc_field PatEnv
penv
               (L l (HsFieldBind ann (L loc (FieldOcc sel (L lr rdr))) pat pun))
               TcM r
thing_inside
        = do { TcId
sel'   <- Name -> TcM TcId
tcLookupId XCFieldOcc GhcRn
Name
sel
             ; Scaled TcSigmaTypeFRR
pat_ty <- SrcAnn NoEpAnns
-> TcRn (Scaled TcSigmaTypeFRR) -> TcRn (Scaled TcSigmaTypeFRR)
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcAnn NoEpAnns
loc (TcRn (Scaled TcSigmaTypeFRR) -> TcRn (Scaled TcSigmaTypeFRR))
-> TcRn (Scaled TcSigmaTypeFRR) -> TcRn (Scaled TcSigmaTypeFRR)
forall a b. (a -> b) -> a -> b
$ Name -> FieldLabelString -> TcRn (Scaled TcSigmaTypeFRR)
find_field_ty XCFieldOcc GhcRn
Name
sel
                                            (OccName -> FieldLabelString
occNameFS (OccName -> FieldLabelString) -> OccName -> FieldLabelString
forall a b. (a -> b) -> a -> b
$ RdrName -> OccName
rdrNameOcc RdrName
rdr)
             ; (GenLocated SrcSpanAnnA (Pat GhcTc)
pat', r
res) <- PatEnv
-> (LPat GhcRn, Scaled TcSigmaTypeFRR)
-> TcM r
-> TcM (LPat GhcTc, r)
Checker (LPat GhcRn, Scaled TcSigmaTypeFRR) (LPat GhcTc)
tcConArg PatEnv
penv (LPat GhcRn
GenLocated SrcSpanAnnA (Pat GhcRn)
pat, Scaled TcSigmaTypeFRR
pat_ty) TcM r
thing_inside
             ; (GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
      (GenLocated SrcSpanAnnA (Pat GhcTc))),
 r)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated
        SrcSpanAnnA
        (HsFieldBind
           (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
           (GenLocated SrcSpanAnnA (Pat GhcTc))),
      r)
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpanAnnA
-> HsFieldBind
     (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
     (GenLocated SrcSpanAnnA (Pat GhcTc))
-> GenLocated
     SrcSpanAnnA
     (HsFieldBind
        (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
        (GenLocated SrcSpanAnnA (Pat GhcTc)))
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (XHsFieldBind (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
-> GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc)
-> GenLocated SrcSpanAnnA (Pat GhcTc)
-> Bool
-> HsFieldBind
     (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
     (GenLocated SrcSpanAnnA (Pat GhcTc))
forall lhs rhs.
XHsFieldBind lhs -> lhs -> rhs -> Bool -> HsFieldBind lhs rhs
HsFieldBind XHsFieldBind (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
XHsFieldBind (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
ann (SrcAnn NoEpAnns
-> FieldOcc GhcTc -> GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
loc (XCFieldOcc GhcTc -> XRec GhcTc RdrName -> FieldOcc GhcTc
forall pass. XCFieldOcc pass -> XRec pass RdrName -> FieldOcc pass
FieldOcc XCFieldOcc GhcTc
TcId
sel' (SrcSpanAnnN -> RdrName -> GenLocated SrcSpanAnnN RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
lr RdrName
rdr))) GenLocated SrcSpanAnnA (Pat GhcTc)
pat'
                                                                        Bool
pun), r
res) }


      find_field_ty :: Name -> FieldLabelString -> TcM (Scaled TcType)
      find_field_ty :: Name -> FieldLabelString -> TcRn (Scaled TcSigmaTypeFRR)
find_field_ty Name
sel FieldLabelString
lbl
        = case [Scaled TcSigmaTypeFRR
ty | (FieldLabel
fl, Scaled TcSigmaTypeFRR
ty) <- [(FieldLabel, Scaled TcSigmaTypeFRR)]
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 TcSigmaTypeFRR)
forall a. TcRnMessage -> TcRn a
failWith (Name -> FieldLabelString -> TcRnMessage
badFieldConErr (ConLike -> Name
forall a. NamedThing a => a -> Name
getName ConLike
con_like) FieldLabelString
lbl)

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

      field_tys :: [(FieldLabel, Scaled TcType)]
      field_tys :: [(FieldLabel, Scaled TcSigmaTypeFRR)]
field_tys = [FieldLabel]
-> [Scaled TcSigmaTypeFRR] -> [(FieldLabel, Scaled TcSigmaTypeFRR)]
forall a b. [a] -> [b] -> [(a, b)]
zip (ConLike -> [FieldLabel]
conLikeFieldLabels ConLike
con_like) [Scaled TcSigmaTypeFRR]
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).

tcConTyArg :: Checker (HsPatSigType GhcRn) TcType
tcConTyArg :: PatEnv -> HsPatSigType GhcRn -> TcM r -> TcM (TcSigmaTypeFRR, r)
tcConTyArg PatEnv
penv HsPatSigType GhcRn
rn_ty TcM r
thing_inside
  = do { ([(Name, TcId)]
sig_wcs, [(Name, TcId)]
sig_ibs, TcSigmaTypeFRR
arg_ty) <- UserTypeCtxt
-> HoleMode
-> HsPatSigType GhcRn
-> ContextKind
-> TcM ([(Name, TcId)], [(Name, TcId)], TcSigmaTypeFRR)
tcHsPatSigType UserTypeCtxt
TypeAppCtxt HoleMode
HM_TyAppPat HsPatSigType GhcRn
rn_ty ContextKind
AnyKind
               -- AnyKind is a bit suspect: it really should be the kind gotten
               -- from instantiating the constructor type. But this would be
               -- hard to get right, because earlier type patterns might influence
               -- the kinds of later patterns. In any case, it all gets checked
               -- by the calls to unifyType in tcConArgs, which will also unify
               -- kinds.
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not ([(Name, TcId)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Name, TcId)]
sig_ibs) Bool -> Bool -> Bool
&& PatEnv -> Bool
inPatBind PatEnv
penv) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
           TcRnMessage -> TcRn ()
addErr (DiagnosticMessage -> TcRnMessage
forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage (DiagnosticMessage -> TcRnMessage)
-> DiagnosticMessage -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints (SDoc -> DiagnosticMessage) -> SDoc -> DiagnosticMessage
forall a b. (a -> b) -> a -> b
$
                     String -> SDoc
text String
"Binding type variables is not allowed in pattern bindings")
       ; r
result <- [(Name, TcId)] -> TcM r -> TcM r
forall r. [(Name, TcId)] -> TcM r -> TcM r
tcExtendNameTyVarEnv [(Name, TcId)]
sig_wcs (TcM r -> TcM r) -> TcM r -> TcM r
forall a b. (a -> b) -> a -> b
$
                   [(Name, TcId)] -> TcM r -> TcM r
forall r. [(Name, TcId)] -> TcM r -> TcM r
tcExtendNameTyVarEnv [(Name, TcId)]
sig_ibs (TcM r -> TcM r) -> TcM r -> TcM r
forall a b. (a -> b) -> a -> b
$
                   TcM r
thing_inside
       ; (TcSigmaTypeFRR, r) -> TcM (TcSigmaTypeFRR, r)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcSigmaTypeFRR
arg_ty, r
result) }

tcConArg :: Checker (LPat GhcRn, Scaled TcSigmaType) (LPat GhcTc)
tcConArg :: PatEnv
-> (LPat GhcRn, Scaled TcSigmaTypeFRR)
-> TcM r
-> TcM (LPat GhcTc, r)
tcConArg PatEnv
penv (LPat GhcRn
arg_pat, Scaled TcSigmaTypeFRR
arg_mult TcSigmaTypeFRR
arg_ty)
  = Scaled ExpSigmaTypeFRR
-> PatEnv -> LPat GhcRn -> TcM r -> TcM (LPat GhcTc, r)
Scaled ExpSigmaTypeFRR -> Checker (LPat GhcRn) (LPat GhcTc)
tc_lpat (TcSigmaTypeFRR -> ExpSigmaTypeFRR -> Scaled ExpSigmaTypeFRR
forall a. TcSigmaTypeFRR -> a -> Scaled a
Scaled TcSigmaTypeFRR
arg_mult (TcSigmaTypeFRR -> ExpSigmaTypeFRR
mkCheckExpType TcSigmaTypeFRR
arg_ty)) PatEnv
penv LPat GhcRn
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 -> [TcSigmaTypeFRR] -> TcRn ()
addDataConStupidTheta DataCon
data_con [TcSigmaTypeFRR]
inst_tys
  | [TcSigmaTypeFRR] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TcSigmaTypeFRR]
stupid_theta = () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  | Bool
otherwise         = CtOrigin -> [TcSigmaTypeFRR] -> TcRn ()
instStupidTheta CtOrigin
origin [TcSigmaTypeFRR]
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 :: [TcSigmaTypeFRR]
stupid_theta = DataCon -> [TcSigmaTypeFRR]
dataConStupidTheta DataCon
data_con
    univ_tvs :: [TcId]
univ_tvs     = DataCon -> [TcId]
dataConUnivTyVars DataCon
data_con
    tenv :: TCvSubst
tenv = [TcId] -> [TcSigmaTypeFRR] -> TCvSubst
HasDebugCallStack => [TcId] -> [TcSigmaTypeFRR] -> TCvSubst
zipTvSubst [TcId]
univ_tvs ([TcId] -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
forall b a. [b] -> [a] -> [a]
takeList [TcId]
univ_tvs [TcSigmaTypeFRR]
inst_tys)
         -- NB: inst_tys can be longer than the univ tyvars
         --     because the constructor might have existentials
    inst_theta :: [TcSigmaTypeFRR]
inst_theta = HasDebugCallStack =>
TCvSubst -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
TCvSubst -> [TcSigmaTypeFRR] -> [TcSigmaTypeFRR]
substTheta TCvSubst
tenv [TcSigmaTypeFRR]
stupid_theta

conTyArgArityErr :: ConLike
                 -> Int   -- expected # of arguments
                 -> Int   -- actual # of arguments
                 -> TcRnMessage
conTyArgArityErr :: ConLike -> Arity -> Arity -> TcRnMessage
conTyArgArityErr ConLike
con_like Arity
expected_number Arity
actual_number
  = DiagnosticMessage -> TcRnMessage
forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage (DiagnosticMessage -> TcRnMessage)
-> DiagnosticMessage -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints (SDoc -> DiagnosticMessage) -> SDoc -> DiagnosticMessage
forall a b. (a -> b) -> a -> b
$
    String -> SDoc
text String
"Too many type arguments in constructor pattern for" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (ConLike -> SDoc
forall a. Outputable a => a -> SDoc
ppr ConLike
con_like) SDoc -> SDoc -> SDoc
$$
    String -> SDoc
text String
"Expected no more than" SDoc -> SDoc -> SDoc
<+> Arity -> SDoc
forall a. Outputable a => a -> SDoc
ppr Arity
expected_number SDoc -> SDoc -> SDoc
<> SDoc
semi SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"got" SDoc -> SDoc -> SDoc
<+> Arity -> SDoc
forall a. Outputable a => a -> SDoc
ppr Arity
actual_number

{-
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 :: Pat GhcRn -> (TcM a -> TcM b) -> TcM a -> TcM b
maybeWrapPatCtxt Pat GhcRn
pat TcM a -> TcM b
tcm TcM a
thing_inside
  | Bool -> Bool
not (Pat GhcRn -> Bool
forall p. Pat p -> Bool
worth_wrapping Pat GhcRn
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 -> Arity -> SDoc -> SDoc
hang (String -> SDoc
text String
"In the pattern:") Arity
2 (Pat GhcRn -> SDoc
forall a. Outputable a => a -> SDoc
ppr Pat GhcRn
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 -> [TcId] -> [TcSigmaTypeFRR] -> PatEnv -> TcRn ()
checkGADT ConLike
conlike [TcId]
ex_tvs [TcSigmaTypeFRR]
arg_tys = \case
  PE { pe_ctxt :: PatEnv -> PatCtxt
pe_ctxt = LetPat {} }
    -> () -> TcRn ()
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 (m :: * -> *) a. Monad m => a -> m a
return ()
  where
    has_existentials :: Bool
    has_existentials :: Bool
has_existentials = (TcId -> Bool) -> [TcId] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (TcId -> VarSet -> Bool
`elemVarSet` [TcSigmaTypeFRR] -> VarSet
tyCoVarsOfTypes [TcSigmaTypeFRR]
arg_tys) [TcId]
ex_tvs

polyPatSig :: TcType -> SDoc
polyPatSig :: TcSigmaTypeFRR -> SDoc
polyPatSig TcSigmaTypeFRR
sig_ty
  = SDoc -> Arity -> SDoc -> SDoc
hang (String -> SDoc
text String
"Illegal polymorphic type signature in pattern:")
       Arity
2 (TcSigmaTypeFRR -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaTypeFRR
sig_ty)