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


TcPat: Typechecking patterns
-}

{-# LANGUAGE CPP, RankNTypes, TupleSections #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}

module TcPat ( tcLetPat, newLetBndr, LetBndrSpec(..)
             , tcPat, tcPat_O, tcPats
             , addDataConStupidTheta, badFieldCon, polyPatSig ) where

#include "HsVersions.h"

import GhcPrelude

import {-# SOURCE #-}   TcExpr( tcSyntaxOp, tcSyntaxOpGen, tcInferSigma )

import GHC.Hs
import TcHsSyn
import TcSigs( TcPragEnv, lookupPragEnv, addInlinePrags )
import TcRnMonad
import Inst
import Id
import Var
import Name
import RdrName
import TcEnv
import TcMType
import TcValidity( arityErr )
import TyCoPpr ( pprTyVars )
import TcType
import TcUnify
import TcHsType
import TysWiredIn
import TcEvidence
import TcOrigin
import TyCon
import DataCon
import PatSyn
import ConLike
import PrelNames
import BasicTypes hiding (SuccessFlag(..))
import DynFlags
import SrcLoc
import VarSet
import Util
import Outputable
import qualified GHC.LanguageExtensions as LangExt
import Control.Arrow  ( second )
import ListSetOps ( getNth )

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

tcLetPat :: (Name -> Maybe TcId)
         -> LetBndrSpec
         -> LPat GhcRn -> ExpSigmaType
         -> TcM a
         -> TcM (LPat GhcTcId, a)
tcLetPat :: (Name -> Maybe TcId)
-> LetBndrSpec
-> LPat GhcRn
-> ExpSigmaType
-> TcM a
-> TcM (LPat GhcTcId, a)
tcLetPat Name -> Maybe TcId
sig_fn LetBndrSpec
no_gen LPat GhcRn
pat ExpSigmaType
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 }

       ; LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat LPat GhcRn
pat ExpSigmaType
pat_ty PatEnv
penv TcM a
thing_inside }

-----------------
tcPats :: HsMatchContext Name
       -> [LPat GhcRn]            -- Patterns,
       -> [ExpSigmaType]         --   and their types
       -> TcM a                  --   and the checker for the body
       -> TcM ([LPat GhcTcId], 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 Name
-> [LPat GhcRn]
-> [ExpSigmaType]
-> TcM a
-> TcM ([LPat GhcTcId], a)
tcPats HsMatchContext Name
ctxt [LPat GhcRn]
pats [ExpSigmaType]
pat_tys TcM a
thing_inside
  = PatEnv
-> [LPat GhcRn]
-> [ExpSigmaType]
-> TcM a
-> TcM ([LPat GhcTcId], a)
forall a.
PatEnv
-> [LPat GhcRn]
-> [ExpSigmaType]
-> TcM a
-> TcM ([LPat GhcTcId], a)
tc_lpats PatEnv
penv [LPat GhcRn]
pats [ExpSigmaType]
pat_tys 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 Name -> PatCtxt
LamPat HsMatchContext Name
ctxt, pe_orig :: CtOrigin
pe_orig = CtOrigin
PatOrigin }

tcPat :: HsMatchContext Name
      -> LPat GhcRn -> ExpSigmaType
      -> TcM a                     -- Checker for body
      -> TcM (LPat GhcTcId, a)
tcPat :: HsMatchContext Name
-> LPat GhcRn -> ExpSigmaType -> TcM a -> TcM (LPat GhcTcId, a)
tcPat HsMatchContext Name
ctxt = HsMatchContext Name
-> CtOrigin
-> LPat GhcRn
-> ExpSigmaType
-> TcM a
-> TcM (LPat GhcTcId, a)
forall a.
HsMatchContext Name
-> CtOrigin
-> LPat GhcRn
-> ExpSigmaType
-> TcM a
-> TcM (LPat GhcTcId, a)
tcPat_O HsMatchContext Name
ctxt CtOrigin
PatOrigin

-- | A variant of 'tcPat' that takes a custom origin
tcPat_O :: HsMatchContext Name
        -> CtOrigin              -- ^ origin to use if the type needs inst'ing
        -> LPat GhcRn -> ExpSigmaType
        -> TcM a                 -- Checker for body
        -> TcM (LPat GhcTcId, a)
tcPat_O :: HsMatchContext Name
-> CtOrigin
-> LPat GhcRn
-> ExpSigmaType
-> TcM a
-> TcM (LPat GhcTcId, a)
tcPat_O HsMatchContext Name
ctxt CtOrigin
orig LPat GhcRn
pat ExpSigmaType
pat_ty TcM a
thing_inside
  = LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat LPat GhcRn
pat ExpSigmaType
pat_ty PatEnv
penv 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 Name -> PatCtxt
LamPat HsMatchContext Name
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 Name)

  | 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 -> ExpSigmaType -> TcM (HsWrapper, TcId)
-- (coi, xp) = tcPatBndr penv x pat_ty
-- Then coi : pat_ty ~ typeof(xp)
--
tcPatBndr :: PatEnv -> Name -> ExpSigmaType -> 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 ExpSigmaType
exp_pat_ty
  -- For the LetPat cases, see
  -- Note [Typechecking pattern bindings] in TcBinds

  | Just TcId
bndr_id <- Name -> Maybe TcId
sig_fn Name
bndr_name   -- There is a signature
  = do { HsWrapper
wrap <- PatEnv -> ExpSigmaType -> TcSigmaType -> TcM HsWrapper
tcSubTypePat PatEnv
penv ExpSigmaType
exp_pat_ty (TcId -> TcSigmaType
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
$$ TcSigmaType -> SDoc
forall a. Outputable a => a -> SDoc
ppr (TcId -> TcSigmaType
idType TcId
bndr_id) SDoc -> SDoc -> SDoc
$$ ExpSigmaType -> SDoc
forall a. Outputable a => a -> SDoc
ppr ExpSigmaType
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 { (TcCoercion
co, TcSigmaType
bndr_ty) <- case ExpSigmaType
exp_pat_ty of
             Check TcSigmaType
pat_ty    -> TcLevel
-> TcSigmaType
-> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercion, TcSigmaType)
promoteTcType TcLevel
bind_lvl TcSigmaType
pat_ty
             Infer InferResult
infer_res -> ASSERT( bind_lvl == ir_lvl infer_res )
                                -- If we were under a constructor that bumped
                                -- the level, we'd be in checking mode
                                do { TcSigmaType
bndr_ty <- InferResult -> TcM TcSigmaType
inferResultToType InferResult
infer_res
                                   ; (TcCoercion, TcSigmaType)
-> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercion, TcSigmaType)
forall (m :: * -> *) a. Monad m => a -> m a
return (TcSigmaType -> TcCoercion
mkTcNomReflCo TcSigmaType
bndr_ty, TcSigmaType
bndr_ty) }
       ; TcId
bndr_id <- LetBndrSpec -> Name -> TcSigmaType -> TcM TcId
newLetBndr LetBndrSpec
no_gen Name
bndr_name TcSigmaType
bndr_ty
       ; String -> SDoc -> TcRn ()
traceTc String
"tcPatBndr(nosig)" ([SDoc] -> SDoc
vcat [ TcLevel -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcLevel
bind_lvl
                                          , ExpSigmaType -> SDoc
forall a. Outputable a => a -> SDoc
ppr ExpSigmaType
exp_pat_ty, TcSigmaType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaType
bndr_ty, TcCoercion -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcCoercion
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 (TcCoercion -> HsWrapper
mkWpCastN TcCoercion
co, TcId
bndr_id) }

tcPatBndr PatEnv
_ Name
bndr_name ExpSigmaType
pat_ty
  = do { TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
expTypeToType ExpSigmaType
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
$$ TcSigmaType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaType
pat_ty)
       ; (HsWrapper, TcId) -> TcM (HsWrapper, TcId)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper
idHsWrapper, Name -> TcSigmaType -> TcId
mkLocalId Name
bndr_name TcSigmaType
pat_ty) }
               -- Whether or not there is a sig is irrelevant,
               -- as this is local

newLetBndr :: LetBndrSpec -> Name -> TcType -> TcM TcId
-- Make up a suitable Id for the pattern-binder.
-- See Note [Typechecking pattern bindings], item (4) in TcBinds
--
-- 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 -> TcSigmaType -> TcM TcId
newLetBndr LetBndrSpec
LetLclBndr Name
name TcSigmaType
ty
  = do { Name
mono_name <- Name -> TcM Name
cloneLocalName Name
name
       ; TcId -> TcM TcId
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> TcSigmaType -> TcId
mkLocalId Name
mono_name TcSigmaType
ty) }
newLetBndr (LetGblBndr TcPragEnv
prags) Name
name TcSigmaType
ty
  = TcId -> [LSig GhcRn] -> TcM TcId
addInlinePrags (Name -> TcSigmaType -> TcId
mkLocalId Name
name TcSigmaType
ty) (TcPragEnv -> Name -> [LSig GhcRn]
lookupPragEnv TcPragEnv
prags Name
name)

tcSubTypePat :: PatEnv -> ExpSigmaType -> TcSigmaType -> TcM HsWrapper
-- tcSubTypeET with the UserTypeCtxt specialised to GenSigCtxt
-- Used when typechecking patterns
tcSubTypePat :: PatEnv -> ExpSigmaType -> TcSigmaType -> TcM HsWrapper
tcSubTypePat PatEnv
penv ExpSigmaType
t1 TcSigmaType
t2 = CtOrigin
-> UserTypeCtxt -> ExpSigmaType -> TcSigmaType -> TcM HsWrapper
tcSubTypeET (PatEnv -> CtOrigin
pe_orig PatEnv
penv) UserTypeCtxt
GenSigCtxt ExpSigmaType
t1 TcSigmaType
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.
                          inp
                       -> PatEnv
                       -> TcM r
                       -> TcM (out, r)

tcMultiple :: Checker inp out -> Checker [inp] [out]
tcMultiple :: Checker inp out -> Checker [inp] [out]
tcMultiple Checker inp out
tc_pat [inp]
args PatEnv
penv 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))
                                <- inp -> PatEnv -> TcM ([out], r) -> TcM (out, ([out], r))
Checker inp out
tc_pat inp
arg PatEnv
penv (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 :: LPat GhcRn
        -> ExpSigmaType
        -> PatEnv
        -> TcM a
        -> TcM (LPat GhcTcId, a)
tc_lpat :: LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat (LPat GhcRn -> Located (SrcSpanLess (Located (Pat GhcRn)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L SrcSpan
span SrcSpanLess (Located (Pat GhcRn))
pat) ExpSigmaType
pat_ty PatEnv
penv TcM a
thing_inside
  = SrcSpan
-> TcRn (Located (Pat GhcTcId), a)
-> TcRn (Located (Pat GhcTcId), a)
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
span (TcRn (Located (Pat GhcTcId), a)
 -> TcRn (Located (Pat GhcTcId), a))
-> TcRn (Located (Pat GhcTcId), a)
-> TcRn (Located (Pat GhcTcId), a)
forall a b. (a -> b) -> a -> b
$
    do  { (Pat GhcTcId
pat', a
res) <- Pat GhcRn
-> (TcM a -> TcM (Pat GhcTcId, a)) -> TcM a -> TcM (Pat GhcTcId, a)
forall a b. Pat GhcRn -> (TcM a -> TcM b) -> TcM a -> TcM b
maybeWrapPatCtxt SrcSpanLess (Located (Pat GhcRn))
Pat GhcRn
pat (PatEnv
-> Pat GhcRn -> ExpSigmaType -> TcM a -> TcM (Pat GhcTcId, a)
forall a.
PatEnv
-> Pat GhcRn -> ExpSigmaType -> TcM a -> TcM (Pat GhcTcId, a)
tc_pat PatEnv
penv SrcSpanLess (Located (Pat GhcRn))
Pat GhcRn
pat ExpSigmaType
pat_ty)
                                          TcM a
thing_inside
        ; (Located (Pat GhcTcId), a) -> TcRn (Located (Pat GhcTcId), a)
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpan
-> SrcSpanLess (Located (Pat GhcTcId)) -> Located (Pat GhcTcId)
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
span SrcSpanLess (Located (Pat GhcTcId))
Pat GhcTcId
pat', a
res) }

tc_lpats :: PatEnv
         -> [LPat GhcRn] -> [ExpSigmaType]
         -> TcM a
         -> TcM ([LPat GhcTcId], a)
tc_lpats :: PatEnv
-> [LPat GhcRn]
-> [ExpSigmaType]
-> TcM a
-> TcM ([LPat GhcTcId], a)
tc_lpats PatEnv
penv [LPat GhcRn]
pats [ExpSigmaType]
tys TcM a
thing_inside
  = ASSERT2( equalLength pats tys, ppr pats $$ ppr tys )
    Checker (Located (Pat GhcRn), ExpSigmaType) (Located (Pat GhcTcId))
-> [(Located (Pat GhcRn), ExpSigmaType)]
-> PatEnv
-> TcM a
-> TcM ([Located (Pat GhcTcId)], a)
forall inp out. Checker inp out -> Checker [inp] [out]
tcMultiple (\(p,t) -> LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM r -> TcM (LPat GhcTcId, r)
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat Located (Pat GhcRn)
LPat GhcRn
p ExpSigmaType
t)
                (String
-> [Located (Pat GhcRn)]
-> [ExpSigmaType]
-> [(Located (Pat GhcRn), ExpSigmaType)]
forall a b. String -> [a] -> [b] -> [(a, b)]
zipEqual String
"tc_lpats" [Located (Pat GhcRn)]
[LPat GhcRn]
pats [ExpSigmaType]
tys)
                PatEnv
penv TcM a
thing_inside

--------------------
tc_pat  :: PatEnv
        -> Pat GhcRn
        -> ExpSigmaType  -- Fully refined result type
        -> TcM a                -- Thing inside
        -> TcM (Pat GhcTcId,    -- Translated pattern
                a)              -- Result of thing inside

tc_pat :: PatEnv
-> Pat GhcRn -> ExpSigmaType -> TcM a -> TcM (Pat GhcTcId, a)
tc_pat PatEnv
penv (VarPat XVarPat GhcRn
x (Located (IdP GhcRn) -> Located (SrcSpanLess (Located Name))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L SrcSpan
l SrcSpanLess (Located Name)
name)) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { (HsWrapper
wrap, TcId
id) <- PatEnv -> Name -> ExpSigmaType -> TcM (HsWrapper, TcId)
tcPatBndr PatEnv
penv Name
SrcSpanLess (Located Name)
name ExpSigmaType
pat_ty
        ; a
res <- Name -> TcId -> TcM a -> TcM a
forall a. Name -> TcId -> TcM a -> TcM a
tcExtendIdEnv1 Name
SrcSpanLess (Located Name)
name TcId
id TcM a
thing_inside
        ; TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
readExpType ExpSigmaType
pat_ty
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTcId -> TcSigmaType -> Pat GhcTcId
forall (id :: Pass).
HsWrapper -> Pat (GhcPass id) -> TcSigmaType -> Pat (GhcPass id)
mkHsWrapPat HsWrapper
wrap (XVarPat GhcTcId -> Located (IdP GhcTcId) -> Pat GhcTcId
forall p. XVarPat p -> Located (IdP p) -> Pat p
VarPat XVarPat GhcRn
XVarPat GhcTcId
x (SrcSpan -> SrcSpanLess (Located TcId) -> Located TcId
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
l SrcSpanLess (Located TcId)
TcId
id)) TcSigmaType
pat_ty, a
res) }

tc_pat PatEnv
penv (ParPat XParPat GhcRn
x LPat GhcRn
pat) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { (Located (Pat GhcTcId)
pat', a
res) <- LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat LPat GhcRn
pat ExpSigmaType
pat_ty PatEnv
penv TcM a
thing_inside
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (XParPat GhcTcId -> LPat GhcTcId -> Pat GhcTcId
forall p. XParPat p -> LPat p -> Pat p
ParPat XParPat GhcRn
XParPat GhcTcId
x Located (Pat GhcTcId)
LPat GhcTcId
pat', a
res) }

tc_pat PatEnv
penv (BangPat XBangPat GhcRn
x LPat GhcRn
pat) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { (Located (Pat GhcTcId)
pat', a
res) <- LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat LPat GhcRn
pat ExpSigmaType
pat_ty PatEnv
penv TcM a
thing_inside
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (XBangPat GhcTcId -> LPat GhcTcId -> Pat GhcTcId
forall p. XBangPat p -> LPat p -> Pat p
BangPat XBangPat GhcRn
XBangPat GhcTcId
x Located (Pat GhcTcId)
LPat GhcTcId
pat', a
res) }

tc_pat PatEnv
penv (LazyPat XLazyPat GhcRn
x LPat GhcRn
pat) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { (Located (Pat GhcTcId)
pat', (a
res, WantedConstraints
pat_ct))
                <- LPat GhcRn
-> ExpSigmaType
-> PatEnv
-> TcM (a, WantedConstraints)
-> TcM (LPat GhcTcId, (a, WantedConstraints))
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat LPat GhcRn
pat ExpSigmaType
pat_ty (PatEnv -> PatEnv
makeLazy PatEnv
penv) (TcM (a, WantedConstraints)
 -> TcM (LPat GhcTcId, (a, WantedConstraints)))
-> TcM (a, WantedConstraints)
-> TcM (LPat GhcTcId, (a, WantedConstraints))
forall a b. (a -> b) -> a -> b
$
                   TcM a -> TcM (a, WantedConstraints)
forall a. TcM a -> TcM (a, WantedConstraints)
captureConstraints TcM a
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
        ; TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
readExpType ExpSigmaType
pat_ty
        ; TcCoercion
_ <- Maybe (HsExpr GhcRn)
-> TcSigmaType -> TcSigmaType -> TcM TcCoercion
unifyType Maybe (HsExpr GhcRn)
forall a. Maybe a
Nothing (HasDebugCallStack => TcSigmaType -> TcSigmaType
TcSigmaType -> TcSigmaType
tcTypeKind TcSigmaType
pat_ty) TcSigmaType
liftedTypeKind

        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (XLazyPat GhcTcId -> LPat GhcTcId -> Pat GhcTcId
forall p. XLazyPat p -> LPat p -> Pat p
LazyPat XLazyPat GhcRn
XLazyPat GhcTcId
x Located (Pat GhcTcId)
LPat GhcTcId
pat', a
res) }

tc_pat PatEnv
_ (WildPat XWildPat GhcRn
_) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { a
res <- TcM a
thing_inside
        ; TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
expTypeToType ExpSigmaType
pat_ty
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (XWildPat GhcTcId -> Pat GhcTcId
forall p. XWildPat p -> Pat p
WildPat TcSigmaType
XWildPat GhcTcId
pat_ty, a
res) }

tc_pat PatEnv
penv (AsPat XAsPat GhcRn
x (Located (IdP GhcRn) -> Located (SrcSpanLess (Located Name))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L SrcSpan
nm_loc SrcSpanLess (Located Name)
name) LPat GhcRn
pat) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { (HsWrapper
wrap, TcId
bndr_id) <- SrcSpan -> TcM (HsWrapper, TcId) -> TcM (HsWrapper, TcId)
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
nm_loc (PatEnv -> Name -> ExpSigmaType -> TcM (HsWrapper, TcId)
tcPatBndr PatEnv
penv Name
SrcSpanLess (Located Name)
name ExpSigmaType
pat_ty)
        ; (Located (Pat GhcTcId)
pat', a
res) <- Name
-> TcId
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
forall a. Name -> TcId -> TcM a -> TcM a
tcExtendIdEnv1 Name
SrcSpanLess (Located Name)
name TcId
bndr_id (IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
 -> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a))
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
forall a b. (a -> b) -> a -> b
$
                         LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat LPat GhcRn
pat (TcSigmaType -> ExpSigmaType
mkCheckExpType (TcSigmaType -> ExpSigmaType) -> TcSigmaType -> ExpSigmaType
forall a b. (a -> b) -> a -> b
$ TcId -> TcSigmaType
idType TcId
bndr_id)
                                 PatEnv
penv TcM a
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!
        ; TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
readExpType ExpSigmaType
pat_ty
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTcId -> TcSigmaType -> Pat GhcTcId
forall (id :: Pass).
HsWrapper -> Pat (GhcPass id) -> TcSigmaType -> Pat (GhcPass id)
mkHsWrapPat HsWrapper
wrap (XAsPat GhcTcId
-> Located (IdP GhcTcId) -> LPat GhcTcId -> Pat GhcTcId
forall p. XAsPat p -> Located (IdP p) -> LPat p -> Pat p
AsPat XAsPat GhcRn
XAsPat GhcTcId
x (SrcSpan -> SrcSpanLess (Located TcId) -> Located TcId
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
nm_loc SrcSpanLess (Located TcId)
TcId
bndr_id) Located (Pat GhcTcId)
LPat GhcTcId
pat') TcSigmaType
pat_ty,
                  a
res) }

tc_pat PatEnv
penv (ViewPat XViewPat GhcRn
_ LHsExpr GhcRn
expr LPat GhcRn
pat) ExpSigmaType
overall_pat_ty TcM a
thing_inside
  = do  {
         -- Expr must have type `forall a1...aN. OPT' -> B`
         -- where overall_pat_ty is an instance of OPT'.
        ; (LHsExpr GhcTcId
expr',TcSigmaType
expr'_inferred) <- LHsExpr GhcRn -> TcM (LHsExpr GhcTcId, TcSigmaType)
tcInferSigma LHsExpr GhcRn
expr

         -- expression must be a function
        ; let expr_orig :: CtOrigin
expr_orig = LHsExpr GhcRn -> CtOrigin
lexprCtOrigin LHsExpr GhcRn
expr
              herald :: SDoc
herald    = String -> SDoc
text String
"A view pattern expression expects"
        ; (HsWrapper
expr_wrap1, [TcSigmaType
inf_arg_ty], TcSigmaType
inf_res_ty)
            <- SDoc
-> CtOrigin
-> Maybe (HsExpr GhcRn)
-> Int
-> TcSigmaType
-> TcM (HsWrapper, [TcSigmaType], TcSigmaType)
matchActualFunTys SDoc
herald CtOrigin
expr_orig (HsExpr GhcRn -> Maybe (HsExpr GhcRn)
forall a. a -> Maybe a
Just (LHsExpr GhcRn -> SrcSpanLess (LHsExpr GhcRn)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc LHsExpr GhcRn
expr)) Int
1 TcSigmaType
expr'_inferred
            -- expr_wrap1 :: expr'_inferred "->" (inf_arg_ty -> inf_res_ty)

         -- check that overall pattern is more polymorphic than arg type
        ; HsWrapper
expr_wrap2 <- PatEnv -> ExpSigmaType -> TcSigmaType -> TcM HsWrapper
tcSubTypePat PatEnv
penv ExpSigmaType
overall_pat_ty TcSigmaType
inf_arg_ty
            -- expr_wrap2 :: overall_pat_ty "->" inf_arg_ty

         -- pattern must have inf_res_ty
        ; (Located (Pat GhcTcId)
pat', a
res) <- LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat LPat GhcRn
pat (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
inf_res_ty) PatEnv
penv TcM a
thing_inside

        ; TcSigmaType
overall_pat_ty <- ExpSigmaType -> TcM TcSigmaType
readExpType ExpSigmaType
overall_pat_ty
        ; let expr_wrap2' :: HsWrapper
expr_wrap2' = HsWrapper
-> HsWrapper -> TcSigmaType -> TcSigmaType -> SDoc -> HsWrapper
mkWpFun HsWrapper
expr_wrap2 HsWrapper
idHsWrapper
                                    TcSigmaType
overall_pat_ty TcSigmaType
inf_res_ty SDoc
doc
               -- expr_wrap2' :: (inf_arg_ty -> inf_res_ty) "->"
               --                (overall_pat_ty -> inf_res_ty)
              expr_wrap :: HsWrapper
expr_wrap = HsWrapper
expr_wrap2' HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
expr_wrap1
              doc :: SDoc
doc = String -> SDoc
text String
"When checking the view pattern function:" SDoc -> SDoc -> SDoc
<+> (LHsExpr GhcRn -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcRn
expr)
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (XViewPat GhcTcId -> LHsExpr GhcTcId -> LPat GhcTcId -> Pat GhcTcId
forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat TcSigmaType
XViewPat GhcTcId
overall_pat_ty (HsWrapper -> LHsExpr GhcTcId -> LHsExpr GhcTcId
forall (id :: Pass).
HsWrapper -> LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
mkLHsWrap HsWrapper
expr_wrap LHsExpr GhcTcId
expr') Located (Pat GhcTcId)
LPat GhcTcId
pat', a
res)}

-- Type signatures in patterns
-- See Note [Pattern coercions] below
tc_pat PatEnv
penv (SigPat XSigPat GhcRn
_ LPat GhcRn
pat LHsSigWcType (NoGhcTc GhcRn)
sig_ty) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { (TcSigmaType
inner_ty, [(Name, TcId)]
tv_binds, [(Name, TcId)]
wcs, HsWrapper
wrap) <- Bool
-> LHsSigWcType GhcRn
-> ExpSigmaType
-> TcM (TcSigmaType, [(Name, TcId)], [(Name, TcId)], HsWrapper)
tcPatSig (PatEnv -> Bool
inPatBind PatEnv
penv)
                                                            LHsSigWcType (NoGhcTc GhcRn)
LHsSigWcType GhcRn
sig_ty ExpSigmaType
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.
        ; (Located (Pat GhcTcId)
pat', a
res) <- [(Name, TcId)]
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
forall r. [(Name, TcId)] -> TcM r -> TcM r
tcExtendNameTyVarEnv [(Name, TcId)]
wcs      (IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
 -> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a))
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
forall a b. (a -> b) -> a -> b
$
                         [(Name, TcId)]
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
forall r. [(Name, TcId)] -> TcM r -> TcM r
tcExtendNameTyVarEnv [(Name, TcId)]
tv_binds (IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
 -> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a))
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
-> IOEnv (Env TcGblEnv TcLclEnv) (Located (Pat GhcTcId), a)
forall a b. (a -> b) -> a -> b
$
                         LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat LPat GhcRn
pat (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
inner_ty) PatEnv
penv TcM a
thing_inside
        ; TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
readExpType ExpSigmaType
pat_ty
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTcId -> TcSigmaType -> Pat GhcTcId
forall (id :: Pass).
HsWrapper -> Pat (GhcPass id) -> TcSigmaType -> Pat (GhcPass id)
mkHsWrapPat HsWrapper
wrap (XSigPat GhcTcId
-> LPat GhcTcId -> LHsSigWcType (NoGhcTc GhcTcId) -> Pat GhcTcId
forall p. XSigPat p -> LPat p -> LHsSigWcType (NoGhcTc p) -> Pat p
SigPat TcSigmaType
XSigPat GhcTcId
inner_ty Located (Pat GhcTcId)
LPat GhcTcId
pat' LHsSigWcType (NoGhcTc GhcRn)
LHsSigWcType (NoGhcTc GhcTcId)
sig_ty) TcSigmaType
pat_ty, a
res) }

------------------------
-- Lists, tuples, arrays
tc_pat PatEnv
penv (ListPat XListPat GhcRn
Nothing [LPat GhcRn]
pats) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { (HsWrapper
coi, TcSigmaType
elt_ty) <- (TcSigmaType
 -> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercion, TcSigmaType))
-> PatEnv -> ExpSigmaType -> TcM (HsWrapper, TcSigmaType)
forall a.
(TcSigmaType -> TcM (TcCoercion, a))
-> PatEnv -> ExpSigmaType -> TcM (HsWrapper, a)
matchExpectedPatTy TcSigmaType
-> IOEnv (Env TcGblEnv TcLclEnv) (TcCoercion, TcSigmaType)
matchExpectedListTy PatEnv
penv ExpSigmaType
pat_ty
        ; ([Located (Pat GhcTcId)]
pats', a
res) <- Checker (Located (Pat GhcRn)) (Located (Pat GhcTcId))
-> [Located (Pat GhcRn)]
-> PatEnv
-> TcM a
-> TcM ([Located (Pat GhcTcId)], a)
forall inp out. Checker inp out -> Checker [inp] [out]
tcMultiple (\Located (Pat GhcRn)
p -> LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM r -> TcM (LPat GhcTcId, r)
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat Located (Pat GhcRn)
LPat GhcRn
p (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
elt_ty))
                                     [Located (Pat GhcRn)]
[LPat GhcRn]
pats PatEnv
penv TcM a
thing_inside
        ; TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
readExpType ExpSigmaType
pat_ty
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTcId -> TcSigmaType -> Pat GhcTcId
forall (id :: Pass).
HsWrapper -> Pat (GhcPass id) -> TcSigmaType -> Pat (GhcPass id)
mkHsWrapPat HsWrapper
coi
                         (XListPat GhcTcId -> [LPat GhcTcId] -> Pat GhcTcId
forall p. XListPat p -> [LPat p] -> Pat p
ListPat (TcSigmaType -> Maybe (TcSigmaType, SyntaxExpr GhcTcId) -> ListPatTc
ListPatTc TcSigmaType
elt_ty Maybe (TcSigmaType, SyntaxExpr GhcTcId)
forall a. Maybe a
Nothing) [Located (Pat GhcTcId)]
[LPat GhcTcId]
pats') TcSigmaType
pat_ty, a
res)
}

tc_pat PatEnv
penv (ListPat (Just e) [LPat GhcRn]
pats) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { TcSigmaType
tau_pat_ty <- ExpSigmaType -> TcM TcSigmaType
expTypeToType ExpSigmaType
pat_ty
        ; (([Located (Pat GhcTcId)]
pats', a
res, TcSigmaType
elt_ty), SyntaxExpr GhcTcId
e')
            <- CtOrigin
-> SyntaxExpr GhcRn
-> [SyntaxOpType]
-> SyntaxOpType
-> ([TcSigmaType] -> TcM ([Located (Pat GhcTcId)], a, TcSigmaType))
-> TcM
     (([Located (Pat GhcTcId)], a, TcSigmaType), SyntaxExpr GhcTcId)
forall a.
CtOrigin
-> SyntaxExpr GhcRn
-> [SyntaxOpType]
-> SyntaxOpType
-> ([TcSigmaType] -> TcM a)
-> TcM (a, SyntaxExpr GhcTcId)
tcSyntaxOpGen CtOrigin
ListOrigin SyntaxExpr GhcRn
e [ExpSigmaType -> SyntaxOpType
SynType (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
tau_pat_ty)]
                                          SyntaxOpType
SynList (([TcSigmaType] -> TcM ([Located (Pat GhcTcId)], a, TcSigmaType))
 -> TcM
      (([Located (Pat GhcTcId)], a, TcSigmaType), SyntaxExpr GhcTcId))
-> ([TcSigmaType] -> TcM ([Located (Pat GhcTcId)], a, TcSigmaType))
-> TcM
     (([Located (Pat GhcTcId)], a, TcSigmaType), SyntaxExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$
                 \ [TcSigmaType
elt_ty] ->
                 do { ([Located (Pat GhcTcId)]
pats', a
res) <- Checker (Located (Pat GhcRn)) (Located (Pat GhcTcId))
-> [Located (Pat GhcRn)]
-> PatEnv
-> TcM a
-> TcM ([Located (Pat GhcTcId)], a)
forall inp out. Checker inp out -> Checker [inp] [out]
tcMultiple (\Located (Pat GhcRn)
p -> LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM r -> TcM (LPat GhcTcId, r)
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat Located (Pat GhcRn)
LPat GhcRn
p (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
elt_ty))
                                                 [Located (Pat GhcRn)]
[LPat GhcRn]
pats PatEnv
penv TcM a
thing_inside
                    ; ([Located (Pat GhcTcId)], a, TcSigmaType)
-> TcM ([Located (Pat GhcTcId)], a, TcSigmaType)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Located (Pat GhcTcId)]
pats', a
res, TcSigmaType
elt_ty) }
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (XListPat GhcTcId -> [LPat GhcTcId] -> Pat GhcTcId
forall p. XListPat p -> [LPat p] -> Pat p
ListPat (TcSigmaType -> Maybe (TcSigmaType, SyntaxExpr GhcTcId) -> ListPatTc
ListPatTc TcSigmaType
elt_ty ((TcSigmaType, SyntaxExpr GhcTcId)
-> Maybe (TcSigmaType, SyntaxExpr GhcTcId)
forall a. a -> Maybe a
Just (TcSigmaType
tau_pat_ty,SyntaxExpr GhcTcId
e'))) [Located (Pat GhcTcId)]
[LPat GhcTcId]
pats', a
res)
}

tc_pat PatEnv
penv (TuplePat XTuplePat GhcRn
_ [LPat GhcRn]
pats Boxity
boxity) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { let arity :: Int
arity = [Located (Pat GhcRn)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Located (Pat GhcRn)]
[LPat GhcRn]
pats
              tc :: TyCon
tc = Boxity -> Int -> TyCon
tupleTyCon Boxity
boxity Int
arity
              -- NB: tupleTyCon does not flatten 1-tuples
              -- See Note [Don't flatten tuples from HsSyn] in MkCore
        ; (HsWrapper
coi, [TcSigmaType]
arg_tys) <- (TcSigmaType -> TcM (TcCoercion, [TcSigmaType]))
-> PatEnv -> ExpSigmaType -> TcM (HsWrapper, [TcSigmaType])
forall a.
(TcSigmaType -> TcM (TcCoercion, a))
-> PatEnv -> ExpSigmaType -> TcM (HsWrapper, a)
matchExpectedPatTy (TyCon -> TcSigmaType -> TcM (TcCoercion, [TcSigmaType])
matchExpectedTyConApp TyCon
tc)
                                               PatEnv
penv ExpSigmaType
pat_ty
                     -- Unboxed tuples have RuntimeRep vars, which we discard:
                     -- See Note [Unboxed tuple RuntimeRep vars] in TyCon
        ; let con_arg_tys :: [TcSigmaType]
con_arg_tys = case Boxity
boxity of Boxity
Unboxed -> Int -> [TcSigmaType] -> [TcSigmaType]
forall a. Int -> [a] -> [a]
drop Int
arity [TcSigmaType]
arg_tys
                                           Boxity
Boxed   -> [TcSigmaType]
arg_tys
        ; ([Located (Pat GhcTcId)]
pats', a
res) <- PatEnv
-> [LPat GhcRn]
-> [ExpSigmaType]
-> TcM a
-> TcM ([LPat GhcTcId], a)
forall a.
PatEnv
-> [LPat GhcRn]
-> [ExpSigmaType]
-> TcM a
-> TcM ([LPat GhcTcId], a)
tc_lpats PatEnv
penv [LPat GhcRn]
pats ((TcSigmaType -> ExpSigmaType) -> [TcSigmaType] -> [ExpSigmaType]
forall a b. (a -> b) -> [a] -> [b]
map TcSigmaType -> ExpSigmaType
mkCheckExpType [TcSigmaType]
con_arg_tys)
                                   TcM a
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 GhcTcId
unmangled_result = XTuplePat GhcTcId -> [LPat GhcTcId] -> Boxity -> Pat GhcTcId
forall p. XTuplePat p -> [LPat p] -> Boxity -> Pat p
TuplePat [TcSigmaType]
XTuplePat GhcTcId
con_arg_tys [Located (Pat GhcTcId)]
[LPat GhcTcId]
pats' Boxity
boxity
                                 -- pat_ty /= pat_ty iff coi /= IdCo
              possibly_mangled_result :: Pat GhcTcId
possibly_mangled_result
                | GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_IrrefutableTuples DynFlags
dflags Bool -> Bool -> Bool
&&
                  Boxity -> Bool
isBoxed Boxity
boxity      = XLazyPat GhcTcId -> LPat GhcTcId -> Pat GhcTcId
forall p. XLazyPat p -> LPat p -> Pat p
LazyPat XLazyPat GhcTcId
NoExtField
noExtField (SrcSpanLess (Located (Pat GhcTcId)) -> Located (Pat GhcTcId)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc SrcSpanLess (Located (Pat GhcTcId))
Pat GhcTcId
unmangled_result)
                | Bool
otherwise           = Pat GhcTcId
unmangled_result

        ; TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
readExpType ExpSigmaType
pat_ty
        ; ASSERT( con_arg_tys `equalLength` pats ) -- Syntactically enforced
          (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTcId -> TcSigmaType -> Pat GhcTcId
forall (id :: Pass).
HsWrapper -> Pat (GhcPass id) -> TcSigmaType -> Pat (GhcPass id)
mkHsWrapPat HsWrapper
coi Pat GhcTcId
possibly_mangled_result TcSigmaType
pat_ty, a
res)
        }

tc_pat PatEnv
penv (SumPat XSumPat GhcRn
_ LPat GhcRn
pat Int
alt Int
arity ) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { let tc :: TyCon
tc = Int -> TyCon
sumTyCon Int
arity
        ; (HsWrapper
coi, [TcSigmaType]
arg_tys) <- (TcSigmaType -> TcM (TcCoercion, [TcSigmaType]))
-> PatEnv -> ExpSigmaType -> TcM (HsWrapper, [TcSigmaType])
forall a.
(TcSigmaType -> TcM (TcCoercion, a))
-> PatEnv -> ExpSigmaType -> TcM (HsWrapper, a)
matchExpectedPatTy (TyCon -> TcSigmaType -> TcM (TcCoercion, [TcSigmaType])
matchExpectedTyConApp TyCon
tc)
                                               PatEnv
penv ExpSigmaType
pat_ty
        ; -- Drop levity vars, we don't care about them here
          let con_arg_tys :: [TcSigmaType]
con_arg_tys = Int -> [TcSigmaType] -> [TcSigmaType]
forall a. Int -> [a] -> [a]
drop Int
arity [TcSigmaType]
arg_tys
        ; (Located (Pat GhcTcId)
pat', a
res) <- LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat LPat GhcRn
pat (TcSigmaType -> ExpSigmaType
mkCheckExpType ([TcSigmaType]
con_arg_tys [TcSigmaType] -> Int -> TcSigmaType
forall a. Outputable a => [a] -> Int -> a
`getNth` (Int
alt Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)))
                                 PatEnv
penv TcM a
thing_inside
        ; TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
readExpType ExpSigmaType
pat_ty
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTcId -> TcSigmaType -> Pat GhcTcId
forall (id :: Pass).
HsWrapper -> Pat (GhcPass id) -> TcSigmaType -> Pat (GhcPass id)
mkHsWrapPat HsWrapper
coi (XSumPat GhcTcId -> LPat GhcTcId -> Int -> Int -> Pat GhcTcId
forall p. XSumPat p -> LPat p -> Int -> Int -> Pat p
SumPat [TcSigmaType]
XSumPat GhcTcId
con_arg_tys Located (Pat GhcTcId)
LPat GhcTcId
pat' Int
alt Int
arity) TcSigmaType
pat_ty
                 , a
res)
        }

------------------------
-- Data constructors
tc_pat PatEnv
penv (ConPatIn Located (IdP GhcRn)
con HsConPatDetails GhcRn
arg_pats) ExpSigmaType
pat_ty TcM a
thing_inside
  = PatEnv
-> Located Name
-> ExpSigmaType
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTcId, a)
forall a.
PatEnv
-> Located Name
-> ExpSigmaType
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTcId, a)
tcConPat PatEnv
penv Located Name
Located (IdP GhcRn)
con ExpSigmaType
pat_ty HsConPatDetails GhcRn
arg_pats TcM a
thing_inside

------------------------
-- Literal patterns
tc_pat PatEnv
penv (LitPat XLitPat GhcRn
x HsLit GhcRn
simple_lit) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { let lit_ty :: TcSigmaType
lit_ty = HsLit GhcRn -> TcSigmaType
forall (p :: Pass). HsLit (GhcPass p) -> TcSigmaType
hsLitType HsLit GhcRn
simple_lit
        ; HsWrapper
wrap   <- PatEnv -> ExpSigmaType -> TcSigmaType -> TcM HsWrapper
tcSubTypePat PatEnv
penv ExpSigmaType
pat_ty TcSigmaType
lit_ty
        ; a
res    <- TcM a
thing_inside
        ; TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
readExpType ExpSigmaType
pat_ty
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return ( HsWrapper -> Pat GhcTcId -> TcSigmaType -> Pat GhcTcId
forall (id :: Pass).
HsWrapper -> Pat (GhcPass id) -> TcSigmaType -> Pat (GhcPass id)
mkHsWrapPat HsWrapper
wrap (XLitPat GhcTcId -> HsLit GhcTcId -> Pat GhcTcId
forall p. XLitPat p -> HsLit p -> Pat p
LitPat XLitPat GhcRn
XLitPat GhcTcId
x (HsLit GhcRn -> HsLit GhcTcId
forall a b. ConvertIdX a b => HsLit a -> HsLit b
convertLit HsLit GhcRn
simple_lit)) TcSigmaType
pat_ty
                 , a
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
tc_pat PatEnv
_ (NPat XNPat GhcRn
_ (Located (HsOverLit GhcRn)
-> Located (SrcSpanLess (Located (HsOverLit GhcRn)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L SrcSpan
l SrcSpanLess (Located (HsOverLit GhcRn))
over_lit) Maybe (SyntaxExpr GhcRn)
mb_neg SyntaxExpr GhcRn
eq) ExpSigmaType
pat_ty TcM a
thing_inside
  = do  { let orig :: CtOrigin
orig = HsOverLit GhcRn -> CtOrigin
LiteralOrigin SrcSpanLess (Located (HsOverLit GhcRn))
HsOverLit GhcRn
over_lit
        ; ((HsOverLit GhcTcId
lit', Maybe (SyntaxExpr GhcTcId)
mb_neg'), SyntaxExpr GhcTcId
eq')
            <- CtOrigin
-> SyntaxExpr GhcRn
-> [SyntaxOpType]
-> ExpSigmaType
-> ([TcSigmaType]
    -> TcM (HsOverLit GhcTcId, Maybe (SyntaxExpr GhcTcId)))
-> TcM
     ((HsOverLit GhcTcId, Maybe (SyntaxExpr GhcTcId)),
      SyntaxExpr GhcTcId)
forall a.
CtOrigin
-> SyntaxExpr GhcRn
-> [SyntaxOpType]
-> ExpSigmaType
-> ([TcSigmaType] -> TcM a)
-> TcM (a, SyntaxExpr GhcTcId)
tcSyntaxOp CtOrigin
orig SyntaxExpr GhcRn
eq [ExpSigmaType -> SyntaxOpType
SynType ExpSigmaType
pat_ty, SyntaxOpType
SynAny]
                          (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
boolTy) (([TcSigmaType]
  -> TcM (HsOverLit GhcTcId, Maybe (SyntaxExpr GhcTcId)))
 -> TcM
      ((HsOverLit GhcTcId, Maybe (SyntaxExpr GhcTcId)),
       SyntaxExpr GhcTcId))
-> ([TcSigmaType]
    -> TcM (HsOverLit GhcTcId, Maybe (SyntaxExpr GhcTcId)))
-> TcM
     ((HsOverLit GhcTcId, Maybe (SyntaxExpr GhcTcId)),
      SyntaxExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$
               \ [TcSigmaType
neg_lit_ty] ->
               let new_over_lit :: TcSigmaType -> TcM (HsOverLit GhcTcId)
new_over_lit TcSigmaType
lit_ty = HsOverLit GhcRn -> ExpSigmaType -> TcM (HsOverLit GhcTcId)
newOverloadedLit SrcSpanLess (Located (HsOverLit GhcRn))
HsOverLit GhcRn
over_lit
                                           (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
lit_ty)
               in case Maybe (SyntaxExpr GhcRn)
mb_neg of
                 Maybe (SyntaxExpr GhcRn)
Nothing  -> (, Maybe (SyntaxExpr GhcTcId)
forall a. Maybe a
Nothing) (HsOverLit GhcTcId
 -> (HsOverLit GhcTcId, Maybe (SyntaxExpr GhcTcId)))
-> TcM (HsOverLit GhcTcId)
-> TcM (HsOverLit GhcTcId, Maybe (SyntaxExpr GhcTcId))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcSigmaType -> TcM (HsOverLit GhcTcId)
new_over_lit TcSigmaType
neg_lit_ty
                 Just SyntaxExpr GhcRn
neg -> -- Negative literal
                             -- The 'negate' is re-mappable syntax
                   (SyntaxExpr GhcTcId -> Maybe (SyntaxExpr GhcTcId))
-> (HsOverLit GhcTcId, SyntaxExpr GhcTcId)
-> (HsOverLit GhcTcId, Maybe (SyntaxExpr GhcTcId))
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second SyntaxExpr GhcTcId -> Maybe (SyntaxExpr GhcTcId)
forall a. a -> Maybe a
Just ((HsOverLit GhcTcId, SyntaxExpr GhcTcId)
 -> (HsOverLit GhcTcId, Maybe (SyntaxExpr GhcTcId)))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (HsOverLit GhcTcId, SyntaxExpr GhcTcId)
-> TcM (HsOverLit GhcTcId, Maybe (SyntaxExpr GhcTcId))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                   (CtOrigin
-> SyntaxExpr GhcRn
-> [SyntaxOpType]
-> ExpSigmaType
-> ([TcSigmaType] -> TcM (HsOverLit GhcTcId))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (HsOverLit GhcTcId, SyntaxExpr GhcTcId)
forall a.
CtOrigin
-> SyntaxExpr GhcRn
-> [SyntaxOpType]
-> ExpSigmaType
-> ([TcSigmaType] -> TcM a)
-> TcM (a, SyntaxExpr GhcTcId)
tcSyntaxOp CtOrigin
orig SyntaxExpr GhcRn
neg [SyntaxOpType
SynRho] (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
neg_lit_ty) (([TcSigmaType] -> TcM (HsOverLit GhcTcId))
 -> IOEnv
      (Env TcGblEnv TcLclEnv) (HsOverLit GhcTcId, SyntaxExpr GhcTcId))
-> ([TcSigmaType] -> TcM (HsOverLit GhcTcId))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (HsOverLit GhcTcId, SyntaxExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$
                    \ [TcSigmaType
lit_ty] -> TcSigmaType -> TcM (HsOverLit GhcTcId)
new_over_lit TcSigmaType
lit_ty)

        ; a
res <- TcM a
thing_inside
        ; TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
readExpType ExpSigmaType
pat_ty
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (XNPat GhcTcId
-> Located (HsOverLit GhcTcId)
-> Maybe (SyntaxExpr GhcTcId)
-> SyntaxExpr GhcTcId
-> Pat GhcTcId
forall p.
XNPat p
-> Located (HsOverLit p)
-> Maybe (SyntaxExpr p)
-> SyntaxExpr p
-> Pat p
NPat TcSigmaType
XNPat GhcTcId
pat_ty (SrcSpan
-> SrcSpanLess (Located (HsOverLit GhcTcId))
-> Located (HsOverLit GhcTcId)
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
l SrcSpanLess (Located (HsOverLit GhcTcId))
HsOverLit GhcTcId
lit') Maybe (SyntaxExpr GhcTcId)
mb_neg' SyntaxExpr GhcTcId
eq', a
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.

If the pushed-down pattern type isn't a tau-type, the two pat_ty's above
could conceivably be different specializations. But this is very much
like the situation in Note [Case branches must be taus] in TcMatches.
So we tauify the pat_ty before proceeding.

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]
tc_pat PatEnv
penv (NPlusKPat XNPlusKPat GhcRn
_ (Located (IdP GhcRn) -> Located (SrcSpanLess (Located Name))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L SrcSpan
nm_loc SrcSpanLess (Located Name)
name)
               (Located (HsOverLit GhcRn)
-> Located (SrcSpanLess (Located (HsOverLit GhcRn)))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L SrcSpan
loc SrcSpanLess (Located (HsOverLit GhcRn))
lit) HsOverLit GhcRn
_ SyntaxExpr GhcRn
ge SyntaxExpr GhcRn
minus) ExpSigmaType
pat_ty
              TcM a
thing_inside
  = do  { TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
expTypeToType ExpSigmaType
pat_ty
        ; let orig :: CtOrigin
orig = HsOverLit GhcRn -> CtOrigin
LiteralOrigin SrcSpanLess (Located (HsOverLit GhcRn))
HsOverLit GhcRn
lit
        ; (HsOverLit GhcTcId
lit1', SyntaxExpr GhcTcId
ge')
            <- CtOrigin
-> SyntaxExpr GhcRn
-> [SyntaxOpType]
-> ExpSigmaType
-> ([TcSigmaType] -> TcM (HsOverLit GhcTcId))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (HsOverLit GhcTcId, SyntaxExpr GhcTcId)
forall a.
CtOrigin
-> SyntaxExpr GhcRn
-> [SyntaxOpType]
-> ExpSigmaType
-> ([TcSigmaType] -> TcM a)
-> TcM (a, SyntaxExpr GhcTcId)
tcSyntaxOp CtOrigin
orig SyntaxExpr GhcRn
ge [TcSigmaType -> SyntaxOpType
synKnownType TcSigmaType
pat_ty, SyntaxOpType
SynRho]
                                  (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
boolTy) (([TcSigmaType] -> TcM (HsOverLit GhcTcId))
 -> IOEnv
      (Env TcGblEnv TcLclEnv) (HsOverLit GhcTcId, SyntaxExpr GhcTcId))
-> ([TcSigmaType] -> TcM (HsOverLit GhcTcId))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (HsOverLit GhcTcId, SyntaxExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$
               \ [TcSigmaType
lit1_ty] ->
               HsOverLit GhcRn -> ExpSigmaType -> TcM (HsOverLit GhcTcId)
newOverloadedLit SrcSpanLess (Located (HsOverLit GhcRn))
HsOverLit GhcRn
lit (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
lit1_ty)
        ; ((HsOverLit GhcTcId
lit2', HsWrapper
minus_wrap, TcId
bndr_id), SyntaxExpr GhcTcId
minus')
            <- CtOrigin
-> SyntaxExpr GhcRn
-> [SyntaxOpType]
-> SyntaxOpType
-> ([TcSigmaType] -> TcM (HsOverLit GhcTcId, HsWrapper, TcId))
-> TcM ((HsOverLit GhcTcId, HsWrapper, TcId), SyntaxExpr GhcTcId)
forall a.
CtOrigin
-> SyntaxExpr GhcRn
-> [SyntaxOpType]
-> SyntaxOpType
-> ([TcSigmaType] -> TcM a)
-> TcM (a, SyntaxExpr GhcTcId)
tcSyntaxOpGen CtOrigin
orig SyntaxExpr GhcRn
minus [TcSigmaType -> SyntaxOpType
synKnownType TcSigmaType
pat_ty, SyntaxOpType
SynRho] SyntaxOpType
SynAny (([TcSigmaType] -> TcM (HsOverLit GhcTcId, HsWrapper, TcId))
 -> TcM ((HsOverLit GhcTcId, HsWrapper, TcId), SyntaxExpr GhcTcId))
-> ([TcSigmaType] -> TcM (HsOverLit GhcTcId, HsWrapper, TcId))
-> TcM ((HsOverLit GhcTcId, HsWrapper, TcId), SyntaxExpr GhcTcId)
forall a b. (a -> b) -> a -> b
$
               \ [TcSigmaType
lit2_ty, TcSigmaType
var_ty] ->
               do { HsOverLit GhcTcId
lit2' <- HsOverLit GhcRn -> ExpSigmaType -> TcM (HsOverLit GhcTcId)
newOverloadedLit SrcSpanLess (Located (HsOverLit GhcRn))
HsOverLit GhcRn
lit (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
lit2_ty)
                  ; (HsWrapper
wrap, TcId
bndr_id) <- SrcSpan -> TcM (HsWrapper, TcId) -> TcM (HsWrapper, TcId)
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
nm_loc (TcM (HsWrapper, TcId) -> TcM (HsWrapper, TcId))
-> TcM (HsWrapper, TcId) -> TcM (HsWrapper, TcId)
forall a b. (a -> b) -> a -> b
$
                                     PatEnv -> Name -> ExpSigmaType -> TcM (HsWrapper, TcId)
tcPatBndr PatEnv
penv Name
SrcSpanLess (Located Name)
name (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
var_ty)
                           -- co :: var_ty ~ idType bndr_id

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

        -- 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 -> [TcSigmaType] -> TcRn ()
instStupidTheta CtOrigin
orig [Class -> [TcSigmaType] -> TcSigmaType
mkClassPred Class
icls [TcSigmaType
pat_ty]] }

        ; a
res <- Name -> TcId -> TcM a -> TcM a
forall a. Name -> TcId -> TcM a -> TcM a
tcExtendIdEnv1 Name
SrcSpanLess (Located Name)
name TcId
bndr_id TcM a
thing_inside

        ; let minus'' :: SyntaxExpr GhcTcId
minus'' = SyntaxExpr GhcTcId
minus' { syn_res_wrap :: HsWrapper
syn_res_wrap =
                                    HsWrapper
minus_wrap HsWrapper -> HsWrapper -> HsWrapper
<.> SyntaxExpr GhcTcId -> HsWrapper
forall p. SyntaxExpr p -> HsWrapper
syn_res_wrap SyntaxExpr GhcTcId
minus' }
              pat' :: Pat GhcTcId
pat' = XNPlusKPat GhcTcId
-> Located (IdP GhcTcId)
-> Located (HsOverLit GhcTcId)
-> HsOverLit GhcTcId
-> SyntaxExpr GhcTcId
-> SyntaxExpr GhcTcId
-> Pat GhcTcId
forall p.
XNPlusKPat p
-> Located (IdP p)
-> Located (HsOverLit p)
-> HsOverLit p
-> SyntaxExpr p
-> SyntaxExpr p
-> Pat p
NPlusKPat TcSigmaType
XNPlusKPat GhcTcId
pat_ty (SrcSpan -> SrcSpanLess (Located TcId) -> Located TcId
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
nm_loc SrcSpanLess (Located TcId)
TcId
bndr_id) (SrcSpan
-> SrcSpanLess (Located (HsOverLit GhcTcId))
-> Located (HsOverLit GhcTcId)
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
loc SrcSpanLess (Located (HsOverLit GhcTcId))
HsOverLit GhcTcId
lit1') HsOverLit GhcTcId
lit2'
                               SyntaxExpr GhcTcId
ge' SyntaxExpr GhcTcId
minus''
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Pat GhcTcId
pat', a
res) }

-- HsSpliced is an annotation produced by 'RnSplice.rnSplicePat'.
-- Here we get rid of it and add the finalizers to the global environment.
--
-- See Note [Delaying modFinalizers in untyped splices] in RnSplice.
tc_pat PatEnv
penv (SplicePat XSplicePat GhcRn
_ (HsSpliced XSpliced GhcRn
_ ThModFinalizers
mod_finalizers (HsSplicedPat Pat GhcRn
pat)))
            ExpSigmaType
pat_ty TcM a
thing_inside
  = do ThModFinalizers -> TcRn ()
addModFinalizersWithLclEnv ThModFinalizers
mod_finalizers
       PatEnv
-> Pat GhcRn -> ExpSigmaType -> TcM a -> TcM (Pat GhcTcId, a)
forall a.
PatEnv
-> Pat GhcRn -> ExpSigmaType -> TcM a -> TcM (Pat GhcTcId, a)
tc_pat PatEnv
penv Pat GhcRn
pat ExpSigmaType
pat_ty TcM a
thing_inside

tc_pat PatEnv
_ Pat GhcRn
_other_pat ExpSigmaType
_ TcM a
_ = String -> TcM (Pat GhcTcId, a)
forall a. String -> a
panic String
"tc_pat"        -- ConPatOut, SigPatOut


{-
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


************************************************************************
*                                                                      *
        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 MkIds.hs, 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.
-}

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

tcConPat :: PatEnv -> Located Name
         -> ExpSigmaType           -- Type of the pattern
         -> HsConPatDetails GhcRn -> TcM a
         -> TcM (Pat GhcTcId, a)
tcConPat :: PatEnv
-> Located Name
-> ExpSigmaType
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTcId, a)
tcConPat PatEnv
penv con_lname :: Located Name
con_lname@(Located Name -> Located (SrcSpanLess (Located Name))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L SrcSpan
_ SrcSpanLess (Located Name)
con_name) ExpSigmaType
pat_ty HsConPatDetails GhcRn
arg_pats TcM a
thing_inside
  = do  { ConLike
con_like <- Name -> TcM ConLike
tcLookupConLike Name
SrcSpanLess (Located Name)
con_name
        ; case ConLike
con_like of
            RealDataCon DataCon
data_con -> PatEnv
-> Located Name
-> DataCon
-> ExpSigmaType
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTcId, a)
forall a.
PatEnv
-> Located Name
-> DataCon
-> ExpSigmaType
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTcId, a)
tcDataConPat PatEnv
penv Located Name
con_lname DataCon
data_con
                                                 ExpSigmaType
pat_ty HsConPatDetails GhcRn
arg_pats TcM a
thing_inside
            PatSynCon PatSyn
pat_syn -> PatEnv
-> Located Name
-> PatSyn
-> ExpSigmaType
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTcId, a)
forall a.
PatEnv
-> Located Name
-> PatSyn
-> ExpSigmaType
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTcId, a)
tcPatSynPat PatEnv
penv Located Name
con_lname PatSyn
pat_syn
                                             ExpSigmaType
pat_ty HsConPatDetails GhcRn
arg_pats TcM a
thing_inside
        }

tcDataConPat :: PatEnv -> Located Name -> DataCon
             -> ExpSigmaType               -- Type of the pattern
             -> HsConPatDetails GhcRn -> TcM a
             -> TcM (Pat GhcTcId, a)
tcDataConPat :: PatEnv
-> Located Name
-> DataCon
-> ExpSigmaType
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTcId, a)
tcDataConPat PatEnv
penv (Located Name -> Located (SrcSpanLess (Located Name))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L SrcSpan
con_span SrcSpanLess (Located Name)
con_name) DataCon
data_con ExpSigmaType
pat_ty
             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, [TcSigmaType]
theta, [TcSigmaType]
arg_tys, TcSigmaType
_)
                = DataCon
-> ([TcId], [TcId], [EqSpec], [TcSigmaType], [TcSigmaType],
    TcSigmaType)
dataConFullSig DataCon
data_con
              header :: Located ConLike
header = SrcSpan -> SrcSpanLess (Located ConLike) -> Located ConLike
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
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, [TcSigmaType]
ctxt_res_tys) <- PatEnv -> TyCon -> ExpSigmaType -> TcM (HsWrapper, [TcSigmaType])
matchExpectedConTy PatEnv
penv TyCon
tycon ExpSigmaType
pat_ty
        ; TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
readExpType ExpSigmaType
pat_ty

          -- Add the stupid theta
        ; SrcSpan -> TcRn () -> TcRn ()
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
con_span (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ DataCon -> [TcSigmaType] -> TcRn ()
addDataConStupidTheta DataCon
data_con [TcSigmaType]
ctxt_res_tys

        ; let all_arg_tys :: [TcSigmaType]
all_arg_tys = [EqSpec] -> [TcSigmaType]
eqSpecPreds [EqSpec]
eq_spec [TcSigmaType] -> [TcSigmaType] -> [TcSigmaType]
forall a. [a] -> [a] -> [a]
++ [TcSigmaType]
theta [TcSigmaType] -> [TcSigmaType] -> [TcSigmaType]
forall a. [a] -> [a] -> [a]
++ [TcSigmaType]
arg_tys
        ; [TcId] -> [TcSigmaType] -> PatEnv -> TcRn ()
checkExistentials [TcId]
ex_tvs [TcSigmaType]
all_arg_tys PatEnv
penv

        ; TCvSubst
tenv <- CtOrigin -> [TcId] -> [TcSigmaType] -> TcM TCvSubst
instTyVarsWith CtOrigin
PatOrigin [TcId]
univ_tvs [TcSigmaType]
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

        ; (TCvSubst
tenv, [TcId]
ex_tvs') <- TCvSubst -> [TcId] -> TcM (TCvSubst, [TcId])
tcInstSuperSkolTyVarsX TCvSubst
tenv [TcId]
ex_tvs
                     -- Get location from monad, not from ex_tvs

        ; let -- pat_ty' = mkTyConApp tycon ctxt_res_tys
              -- pat_ty' is type of the actual constructor application
              -- pat_ty' /= pat_ty iff coi /= IdCo

              arg_tys' :: [TcSigmaType]
arg_tys' = HasCallStack => TCvSubst -> [TcSigmaType] -> [TcSigmaType]
TCvSubst -> [TcSigmaType] -> [TcSigmaType]
substTys TCvSubst
tenv [TcSigmaType]
arg_tys

        ; String -> SDoc -> TcRn ()
traceTc String
"tcConPat" ([SDoc] -> SDoc
vcat [ Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
SrcSpanLess (Located Name)
con_name
                                   , [TcId] -> SDoc
pprTyVars [TcId]
univ_tvs
                                   , [TcId] -> SDoc
pprTyVars [TcId]
ex_tvs
                                   , [EqSpec] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [EqSpec]
eq_spec
                                   , [TcSigmaType] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcSigmaType]
theta
                                   , [TcId] -> SDoc
pprTyVars [TcId]
ex_tvs'
                                   , [TcSigmaType] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcSigmaType]
ctxt_res_tys
                                   , [TcSigmaType] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcSigmaType]
arg_tys'
                                   , HsConDetails
  (Located (Pat GhcRn)) (HsRecFields GhcRn (Located (Pat GhcRn)))
-> SDoc
forall a. Outputable a => a -> SDoc
ppr HsConDetails
  (Located (Pat GhcRn)) (HsRecFields GhcRn (Located (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
&& [TcSigmaType] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TcSigmaType]
theta
          then do { -- The common case; no class bindings etc
                    -- (see Note [Arrows and patterns])
                    (HsConDetails
  (Located (Pat GhcTcId))
  (HsRecFields GhcTcId (Located (Pat GhcTcId)))
arg_pats', a
res) <- ConLike
-> [TcSigmaType]
-> HsConPatDetails GhcRn
-> PatEnv
-> TcM a
-> TcM (HsConPatDetails GhcTcId, a)
ConLike
-> [TcSigmaType]
-> Checker (HsConPatDetails GhcRn) (HsConPatDetails GhcTcId)
tcConArgs (DataCon -> ConLike
RealDataCon DataCon
data_con) [TcSigmaType]
arg_tys'
                                                  HsConPatDetails GhcRn
arg_pats PatEnv
penv TcM a
thing_inside
                  ; let res_pat :: Pat GhcTcId
res_pat = ConPatOut :: forall p.
Located ConLike
-> [TcSigmaType]
-> [TcId]
-> [TcId]
-> TcEvBinds
-> HsConPatDetails p
-> HsWrapper
-> Pat p
ConPatOut { pat_con :: Located ConLike
pat_con = Located ConLike
header,
                                              pat_tvs :: [TcId]
pat_tvs = [], pat_dicts :: [TcId]
pat_dicts = [],
                                              pat_binds :: TcEvBinds
pat_binds = TcEvBinds
emptyTcEvBinds,
                                              pat_args :: HsConPatDetails GhcTcId
pat_args = HsConDetails
  (Located (Pat GhcTcId))
  (HsRecFields GhcTcId (Located (Pat GhcTcId)))
HsConPatDetails GhcTcId
arg_pats',
                                              pat_arg_tys :: [TcSigmaType]
pat_arg_tys = [TcSigmaType]
ctxt_res_tys,
                                              pat_wrap :: HsWrapper
pat_wrap = HsWrapper
idHsWrapper }

                  ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTcId -> TcSigmaType -> Pat GhcTcId
forall (id :: Pass).
HsWrapper -> Pat (GhcPass id) -> TcSigmaType -> Pat (GhcPass id)
mkHsWrapPat HsWrapper
wrap Pat GhcTcId
res_pat TcSigmaType
pat_ty, a
res) }

          else do   -- The general case, with existential,
                    -- and local equality constraints
        { let theta' :: [TcSigmaType]
theta'     = HasCallStack => TCvSubst -> [TcSigmaType] -> [TcSigmaType]
TCvSubst -> [TcSigmaType] -> [TcSigmaType]
substTheta TCvSubst
tenv ([EqSpec] -> [TcSigmaType]
eqSpecPreds [EqSpec]
eq_spec [TcSigmaType] -> [TcSigmaType] -> [TcSigmaType]
forall a. [a] -> [a] -> [a]
++ [TcSigmaType]
theta)
                           -- order is *important* as we generate the list of
                           -- dictionary binders from theta'
              no_equalities :: Bool
no_equalities = [EqSpec] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [EqSpec]
eq_spec Bool -> Bool -> Bool
&& Bool -> Bool
not ((TcSigmaType -> Bool) -> [TcSigmaType] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any TcSigmaType -> Bool
isEqPred [TcSigmaType]
theta)
              skol_info :: SkolemInfo
skol_info = ConLike -> HsMatchContext Name -> SkolemInfo
PatSkol (DataCon -> ConLike
RealDataCon DataCon
data_con) HsMatchContext Name
mc
              mc :: HsMatchContext Name
mc = case PatEnv -> PatCtxt
pe_ctxt PatEnv
penv of
                     LamPat HsMatchContext Name
mc -> HsMatchContext Name
mc
                     LetPat {} -> HsMatchContext Name
forall id. HsMatchContext id
PatBindRhs

        ; Bool
gadts_on    <- Extension -> IOEnv (Env TcGblEnv TcLclEnv) Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.GADTs
        ; Bool
families_on <- Extension -> IOEnv (Env TcGblEnv TcLclEnv) Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.TypeFamilies
        ; Bool -> SDoc -> TcRn ()
checkTc (Bool
no_equalities Bool -> Bool -> Bool
|| Bool
gadts_on Bool -> Bool -> Bool
|| Bool
families_on)
                  (String -> SDoc
text String
"A pattern match on a GADT requires the" SDoc -> SDoc -> SDoc
<+>
                   String -> SDoc
text String
"GADTs or TypeFamilies language extension")
                  -- #2905 decided that a *pattern-match* of a GADT
                  -- should require the GADT language flag.
                  -- Re TypeFamilies see also #7156

        ; [TcId]
given <- [TcSigmaType] -> TcM [TcId]
newEvVars [TcSigmaType]
theta'
        ; (TcEvBinds
ev_binds, (HsConDetails
  (Located (Pat GhcTcId))
  (HsRecFields GhcTcId (Located (Pat GhcTcId)))
arg_pats', a
res))
             <- SkolemInfo
-> [TcId]
-> [TcId]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsConDetails
        (Located (Pat GhcTcId))
        (HsRecFields GhcTcId (Located (Pat GhcTcId))),
      a)
-> TcM
     (TcEvBinds,
      (HsConDetails
         (Located (Pat GhcTcId))
         (HsRecFields GhcTcId (Located (Pat GhcTcId))),
       a))
forall result.
SkolemInfo
-> [TcId] -> [TcId] -> TcM result -> TcM (TcEvBinds, result)
checkConstraints SkolemInfo
skol_info [TcId]
ex_tvs' [TcId]
given (IOEnv
   (Env TcGblEnv TcLclEnv)
   (HsConDetails
      (Located (Pat GhcTcId))
      (HsRecFields GhcTcId (Located (Pat GhcTcId))),
    a)
 -> TcM
      (TcEvBinds,
       (HsConDetails
          (Located (Pat GhcTcId))
          (HsRecFields GhcTcId (Located (Pat GhcTcId))),
        a)))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsConDetails
        (Located (Pat GhcTcId))
        (HsRecFields GhcTcId (Located (Pat GhcTcId))),
      a)
-> TcM
     (TcEvBinds,
      (HsConDetails
         (Located (Pat GhcTcId))
         (HsRecFields GhcTcId (Located (Pat GhcTcId))),
       a))
forall a b. (a -> b) -> a -> b
$
                ConLike
-> [TcSigmaType]
-> HsConPatDetails GhcRn
-> PatEnv
-> TcM a
-> TcM (HsConPatDetails GhcTcId, a)
ConLike
-> [TcSigmaType]
-> Checker (HsConPatDetails GhcRn) (HsConPatDetails GhcTcId)
tcConArgs (DataCon -> ConLike
RealDataCon DataCon
data_con) [TcSigmaType]
arg_tys' HsConPatDetails GhcRn
arg_pats PatEnv
penv TcM a
thing_inside

        ; let res_pat :: Pat GhcTcId
res_pat = ConPatOut :: forall p.
Located ConLike
-> [TcSigmaType]
-> [TcId]
-> [TcId]
-> TcEvBinds
-> HsConPatDetails p
-> HsWrapper
-> Pat p
ConPatOut { pat_con :: Located ConLike
pat_con   = Located ConLike
header,
                                    pat_tvs :: [TcId]
pat_tvs   = [TcId]
ex_tvs',
                                    pat_dicts :: [TcId]
pat_dicts = [TcId]
given,
                                    pat_binds :: TcEvBinds
pat_binds = TcEvBinds
ev_binds,
                                    pat_args :: HsConPatDetails GhcTcId
pat_args  = HsConDetails
  (Located (Pat GhcTcId))
  (HsRecFields GhcTcId (Located (Pat GhcTcId)))
HsConPatDetails GhcTcId
arg_pats',
                                    pat_arg_tys :: [TcSigmaType]
pat_arg_tys = [TcSigmaType]
ctxt_res_tys,
                                    pat_wrap :: HsWrapper
pat_wrap  = HsWrapper
idHsWrapper }
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTcId -> TcSigmaType -> Pat GhcTcId
forall (id :: Pass).
HsWrapper -> Pat (GhcPass id) -> TcSigmaType -> Pat (GhcPass id)
mkHsWrapPat HsWrapper
wrap Pat GhcTcId
res_pat TcSigmaType
pat_ty, a
res)
        } }

tcPatSynPat :: PatEnv -> Located Name -> PatSyn
            -> ExpSigmaType                -- Type of the pattern
            -> HsConPatDetails GhcRn -> TcM a
            -> TcM (Pat GhcTcId, a)
tcPatSynPat :: PatEnv
-> Located Name
-> PatSyn
-> ExpSigmaType
-> HsConPatDetails GhcRn
-> TcM a
-> TcM (Pat GhcTcId, a)
tcPatSynPat PatEnv
penv (Located Name -> Located (SrcSpanLess (Located Name))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L SrcSpan
con_span SrcSpanLess (Located Name)
_) PatSyn
pat_syn ExpSigmaType
pat_ty HsConPatDetails GhcRn
arg_pats TcM a
thing_inside
  = do  { let ([TcId]
univ_tvs, [TcSigmaType]
req_theta, [TcId]
ex_tvs, [TcSigmaType]
prov_theta, [TcSigmaType]
arg_tys, TcSigmaType
ty) = PatSyn
-> ([TcId], [TcSigmaType], [TcId], [TcSigmaType], [TcSigmaType],
    TcSigmaType)
patSynSig PatSyn
pat_syn

        ; (TCvSubst
subst, [TcId]
univ_tvs') <- [TcId] -> TcM (TCvSubst, [TcId])
newMetaTyVars [TcId]
univ_tvs

        ; let all_arg_tys :: [TcSigmaType]
all_arg_tys = TcSigmaType
ty TcSigmaType -> [TcSigmaType] -> [TcSigmaType]
forall a. a -> [a] -> [a]
: [TcSigmaType]
prov_theta [TcSigmaType] -> [TcSigmaType] -> [TcSigmaType]
forall a. [a] -> [a] -> [a]
++ [TcSigmaType]
arg_tys
        ; [TcId] -> [TcSigmaType] -> PatEnv -> TcRn ()
checkExistentials [TcId]
ex_tvs [TcSigmaType]
all_arg_tys PatEnv
penv
        ; (TCvSubst
tenv, [TcId]
ex_tvs') <- TCvSubst -> [TcId] -> TcM (TCvSubst, [TcId])
tcInstSuperSkolTyVarsX TCvSubst
subst [TcId]
ex_tvs
        ; let ty' :: TcSigmaType
ty'         = HasCallStack => TCvSubst -> TcSigmaType -> TcSigmaType
TCvSubst -> TcSigmaType -> TcSigmaType
substTy TCvSubst
tenv TcSigmaType
ty
              arg_tys' :: [TcSigmaType]
arg_tys'    = HasCallStack => TCvSubst -> [TcSigmaType] -> [TcSigmaType]
TCvSubst -> [TcSigmaType] -> [TcSigmaType]
substTys TCvSubst
tenv [TcSigmaType]
arg_tys
              prov_theta' :: [TcSigmaType]
prov_theta' = HasCallStack => TCvSubst -> [TcSigmaType] -> [TcSigmaType]
TCvSubst -> [TcSigmaType] -> [TcSigmaType]
substTheta TCvSubst
tenv [TcSigmaType]
prov_theta
              req_theta' :: [TcSigmaType]
req_theta'  = HasCallStack => TCvSubst -> [TcSigmaType] -> [TcSigmaType]
TCvSubst -> [TcSigmaType] -> [TcSigmaType]
substTheta TCvSubst
tenv [TcSigmaType]
req_theta

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

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

        ; let skol_info :: SkolemInfo
skol_info = case PatEnv -> PatCtxt
pe_ctxt PatEnv
penv of
                            LamPat HsMatchContext Name
mc -> ConLike -> HsMatchContext Name -> SkolemInfo
PatSkol (PatSyn -> ConLike
PatSynCon PatSyn
pat_syn) HsMatchContext Name
mc
                            LetPat {} -> SkolemInfo
UnkSkol -- Doesn't matter

        ; HsWrapper
req_wrap <- CtOrigin -> [TcSigmaType] -> [TcSigmaType] -> TcM HsWrapper
instCall CtOrigin
PatOrigin ([TcId] -> [TcSigmaType]
mkTyVarTys [TcId]
univ_tvs') [TcSigmaType]
req_theta'
        ; String -> SDoc -> TcRn ()
traceTc String
"instCall" (HsWrapper -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsWrapper
req_wrap)

        ; String -> SDoc -> TcRn ()
traceTc String
"checkConstraints {" SDoc
Outputable.empty
        ; (TcEvBinds
ev_binds, (HsConDetails
  (Located (Pat GhcTcId))
  (HsRecFields GhcTcId (Located (Pat GhcTcId)))
arg_pats', a
res))
             <- SkolemInfo
-> [TcId]
-> [TcId]
-> TcM
     (HsConDetails
        (Located (Pat GhcTcId))
        (HsRecFields GhcTcId (Located (Pat GhcTcId))),
      a)
-> TcM
     (TcEvBinds,
      (HsConDetails
         (Located (Pat GhcTcId))
         (HsRecFields GhcTcId (Located (Pat GhcTcId))),
       a))
forall result.
SkolemInfo
-> [TcId] -> [TcId] -> TcM result -> TcM (TcEvBinds, result)
checkConstraints SkolemInfo
skol_info [TcId]
ex_tvs' [TcId]
prov_dicts' (TcM
   (HsConDetails
      (Located (Pat GhcTcId))
      (HsRecFields GhcTcId (Located (Pat GhcTcId))),
    a)
 -> TcM
      (TcEvBinds,
       (HsConDetails
          (Located (Pat GhcTcId))
          (HsRecFields GhcTcId (Located (Pat GhcTcId))),
        a)))
-> TcM
     (HsConDetails
        (Located (Pat GhcTcId))
        (HsRecFields GhcTcId (Located (Pat GhcTcId))),
      a)
-> TcM
     (TcEvBinds,
      (HsConDetails
         (Located (Pat GhcTcId))
         (HsRecFields GhcTcId (Located (Pat GhcTcId))),
       a))
forall a b. (a -> b) -> a -> b
$
                ConLike
-> [TcSigmaType]
-> HsConPatDetails GhcRn
-> PatEnv
-> TcM a
-> TcM (HsConPatDetails GhcTcId, a)
ConLike
-> [TcSigmaType]
-> Checker (HsConPatDetails GhcRn) (HsConPatDetails GhcTcId)
tcConArgs (PatSyn -> ConLike
PatSynCon PatSyn
pat_syn) [TcSigmaType]
arg_tys' HsConPatDetails GhcRn
arg_pats PatEnv
penv 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 GhcTcId
res_pat = ConPatOut :: forall p.
Located ConLike
-> [TcSigmaType]
-> [TcId]
-> [TcId]
-> TcEvBinds
-> HsConPatDetails p
-> HsWrapper
-> Pat p
ConPatOut { pat_con :: Located ConLike
pat_con   = SrcSpan -> SrcSpanLess (Located ConLike) -> Located ConLike
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
con_span (SrcSpanLess (Located ConLike) -> Located ConLike)
-> SrcSpanLess (Located ConLike) -> Located ConLike
forall a b. (a -> b) -> a -> b
$ PatSyn -> ConLike
PatSynCon PatSyn
pat_syn,
                                    pat_tvs :: [TcId]
pat_tvs   = [TcId]
ex_tvs',
                                    pat_dicts :: [TcId]
pat_dicts = [TcId]
prov_dicts',
                                    pat_binds :: TcEvBinds
pat_binds = TcEvBinds
ev_binds,
                                    pat_args :: HsConPatDetails GhcTcId
pat_args  = HsConDetails
  (Located (Pat GhcTcId))
  (HsRecFields GhcTcId (Located (Pat GhcTcId)))
HsConPatDetails GhcTcId
arg_pats',
                                    pat_arg_tys :: [TcSigmaType]
pat_arg_tys = [TcId] -> [TcSigmaType]
mkTyVarTys [TcId]
univ_tvs',
                                    pat_wrap :: HsWrapper
pat_wrap  = HsWrapper
req_wrap }
        ; TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
readExpType ExpSigmaType
pat_ty
        ; (Pat GhcTcId, a) -> TcM (Pat GhcTcId, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> Pat GhcTcId -> TcSigmaType -> Pat GhcTcId
forall (id :: Pass).
HsWrapper -> Pat (GhcPass id) -> TcSigmaType -> Pat (GhcPass id)
mkHsWrapPat HsWrapper
wrap Pat GhcTcId
res_pat TcSigmaType
pat_ty, a
res) }

----------------------------
-- | Convenient wrapper for calling a matchExpectedXXX function
matchExpectedPatTy :: (TcRhoType -> TcM (TcCoercionN, a))
                    -> PatEnv -> ExpSigmaType -> TcM (HsWrapper, a)
-- See Note [Matching polytyped patterns]
-- Returns a wrapper : pat_ty ~R inner_ty
matchExpectedPatTy :: (TcSigmaType -> TcM (TcCoercion, a))
-> PatEnv -> ExpSigmaType -> TcM (HsWrapper, a)
matchExpectedPatTy TcSigmaType -> TcM (TcCoercion, a)
inner_match (PE { pe_orig :: PatEnv -> CtOrigin
pe_orig = CtOrigin
orig }) ExpSigmaType
pat_ty
  = do { TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
expTypeToType ExpSigmaType
pat_ty
       ; (HsWrapper
wrap, TcSigmaType
pat_rho) <- CtOrigin -> TcSigmaType -> TcM (HsWrapper, TcSigmaType)
topInstantiate CtOrigin
orig TcSigmaType
pat_ty
       ; (TcCoercion
co, a
res) <- TcSigmaType -> TcM (TcCoercion, a)
inner_match TcSigmaType
pat_rho
       ; String -> SDoc -> TcRn ()
traceTc String
"matchExpectedPatTy" (TcSigmaType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaType
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 (TcCoercion -> HsWrapper
mkWpCastN (TcCoercion -> TcCoercion
mkTcSymCo TcCoercion
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
                   -> ExpSigmaType  -- 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 -> ExpSigmaType -> TcM (HsWrapper, [TcSigmaType])
matchExpectedConTy (PE { pe_orig :: PatEnv -> CtOrigin
pe_orig = CtOrigin
orig }) TyCon
data_tc ExpSigmaType
exp_pat_ty
  | Just (TyCon
fam_tc, [TcSigmaType]
fam_args, CoAxiom Unbranched
co_tc) <- TyCon -> Maybe (TyCon, [TcSigmaType], CoAxiom Unbranched)
tyConFamInstSig_maybe TyCon
data_tc
         -- Comments refer to Note [Matching constructor patterns]
         -- co_tc :: forall a. T [a] ~ T7 a
  = do { TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
expTypeToType ExpSigmaType
exp_pat_ty
       ; (HsWrapper
wrap, TcSigmaType
pat_rho) <- CtOrigin -> TcSigmaType -> TcM (HsWrapper, TcSigmaType)
topInstantiate CtOrigin
orig TcSigmaType
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, [TcSigmaType] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcSigmaType]
fam_args,
                                             ExpSigmaType -> SDoc
forall a. Outputable a => a -> SDoc
ppr ExpSigmaType
exp_pat_ty,
                                             TcSigmaType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaType
pat_ty,
                                             TcSigmaType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaType
pat_rho, HsWrapper -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsWrapper
wrap])
       ; TcCoercion
co1 <- Maybe (HsExpr GhcRn)
-> TcSigmaType -> TcSigmaType -> TcM TcCoercion
unifyType Maybe (HsExpr GhcRn)
forall a. Maybe a
Nothing (TyCon -> [TcSigmaType] -> TcSigmaType
mkTyConApp TyCon
fam_tc (HasCallStack => TCvSubst -> [TcSigmaType] -> [TcSigmaType]
TCvSubst -> [TcSigmaType] -> [TcSigmaType]
substTys TCvSubst
subst [TcSigmaType]
fam_args)) TcSigmaType
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' :: [TcSigmaType]
tys' = [TcId] -> [TcSigmaType]
mkTyVarTys [TcId]
tvs'
             co2 :: TcCoercion
co2 = CoAxiom Unbranched -> [TcSigmaType] -> [TcCoercion] -> TcCoercion
mkTcUnbranchedAxInstCo CoAxiom Unbranched
co_tc [TcSigmaType]
tys' []
             -- co2 : T (ty1,ty2) ~R T7 ty1 ty2

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

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

  | Bool
otherwise
  = do { TcSigmaType
pat_ty <- ExpSigmaType -> TcM TcSigmaType
expTypeToType ExpSigmaType
exp_pat_ty
       ; (HsWrapper
wrap, TcSigmaType
pat_rho) <- CtOrigin -> TcSigmaType -> TcM (HsWrapper, TcSigmaType)
topInstantiate CtOrigin
orig TcSigmaType
pat_ty
       ; (TcCoercion
coi, [TcSigmaType]
tys) <- TyCon -> TcSigmaType -> TcM (TcCoercion, [TcSigmaType])
matchExpectedTyConApp TyCon
data_tc TcSigmaType
pat_rho
       ; (HsWrapper, [TcSigmaType]) -> TcM (HsWrapper, [TcSigmaType])
forall (m :: * -> *) a. Monad m => a -> m a
return (TcCoercion -> HsWrapper
mkWpCastN (TcCoercion -> TcCoercion
mkTcSymCo TcCoercion
coi) HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
wrap, [TcSigmaType]
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
-}

tcConArgs :: ConLike -> [TcSigmaType]
          -> Checker (HsConPatDetails GhcRn) (HsConPatDetails GhcTc)

tcConArgs :: ConLike
-> [TcSigmaType]
-> Checker (HsConPatDetails GhcRn) (HsConPatDetails GhcTcId)
tcConArgs ConLike
con_like [TcSigmaType]
arg_tys (PrefixCon [LPat GhcRn]
arg_pats) PatEnv
penv TcM r
thing_inside
  = do  { Bool -> SDoc -> TcRn ()
checkTc (Int
con_arity Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
no_of_args)     -- Check correct arity
                  (SDoc -> ConLike -> Int -> Int -> SDoc
forall a. Outputable a => SDoc -> a -> Int -> Int -> SDoc
arityErr (String -> SDoc
text String
"constructor") ConLike
con_like Int
con_arity Int
no_of_args)
        ; let pats_w_tys :: [(Located (Pat GhcRn), TcSigmaType)]
pats_w_tys = String
-> [Located (Pat GhcRn)]
-> [TcSigmaType]
-> [(Located (Pat GhcRn), TcSigmaType)]
forall a b. String -> [a] -> [b] -> [(a, b)]
zipEqual String
"tcConArgs" [Located (Pat GhcRn)]
[LPat GhcRn]
arg_pats [TcSigmaType]
arg_tys
        ; ([Located (Pat GhcTcId)]
arg_pats', r
res) <- Checker (Located (Pat GhcRn), TcSigmaType) (Located (Pat GhcTcId))
-> [(Located (Pat GhcRn), TcSigmaType)]
-> PatEnv
-> TcM r
-> TcM ([Located (Pat GhcTcId)], r)
forall inp out. Checker inp out -> Checker [inp] [out]
tcMultiple Checker (Located (Pat GhcRn), TcSigmaType) (Located (Pat GhcTcId))
Checker (LPat GhcRn, TcSigmaType) (LPat GhcTcId)
tcConArg [(Located (Pat GhcRn), TcSigmaType)]
pats_w_tys
                                              PatEnv
penv TcM r
thing_inside
        ; (HsConDetails
   (Located (Pat GhcTcId))
   (HsRecFields GhcTcId (Located (Pat GhcTcId))),
 r)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsConDetails
        (Located (Pat GhcTcId))
        (HsRecFields GhcTcId (Located (Pat GhcTcId))),
      r)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Located (Pat GhcTcId)]
-> HsConDetails
     (Located (Pat GhcTcId))
     (HsRecFields GhcTcId (Located (Pat GhcTcId)))
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [Located (Pat GhcTcId)]
arg_pats', r
res) }
  where
    con_arity :: Int
con_arity  = ConLike -> Int
conLikeArity ConLike
con_like
    no_of_args :: Int
no_of_args = [Located (Pat GhcRn)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Located (Pat GhcRn)]
[LPat GhcRn]
arg_pats

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

tcConArgs ConLike
con_like [TcSigmaType]
arg_tys (RecCon (HsRecFields [LHsRecField GhcRn (LPat GhcRn)]
rpats Maybe (Located Int)
dd)) PatEnv
penv TcM r
thing_inside
  = do  { ([LHsRecField GhcTcId (Located (Pat GhcTcId))]
rpats', r
res) <- Checker
  (LHsRecField GhcRn (Located (Pat GhcRn)))
  (LHsRecField GhcTcId (Located (Pat GhcTcId)))
-> [LHsRecField GhcRn (Located (Pat GhcRn))]
-> PatEnv
-> TcM r
-> TcM ([LHsRecField GhcTcId (Located (Pat GhcTcId))], r)
forall inp out. Checker inp out -> Checker [inp] [out]
tcMultiple Checker
  (LHsRecField GhcRn (Located (Pat GhcRn)))
  (LHsRecField GhcTcId (Located (Pat GhcTcId)))
Checker
  (LHsRecField GhcRn (LPat GhcRn))
  (LHsRecField GhcTcId (LPat GhcTcId))
tc_field [LHsRecField GhcRn (Located (Pat GhcRn))]
[LHsRecField GhcRn (LPat GhcRn)]
rpats PatEnv
penv TcM r
thing_inside
        ; (HsConDetails
   (Located (Pat GhcTcId))
   (HsRecFields GhcTcId (Located (Pat GhcTcId))),
 r)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsConDetails
        (Located (Pat GhcTcId))
        (HsRecFields GhcTcId (Located (Pat GhcTcId))),
      r)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsRecFields GhcTcId (Located (Pat GhcTcId))
-> HsConDetails
     (Located (Pat GhcTcId))
     (HsRecFields GhcTcId (Located (Pat GhcTcId)))
forall arg rec. rec -> HsConDetails arg rec
RecCon ([LHsRecField GhcTcId (Located (Pat GhcTcId))]
-> Maybe (Located Int)
-> HsRecFields GhcTcId (Located (Pat GhcTcId))
forall p arg.
[LHsRecField p arg] -> Maybe (Located Int) -> HsRecFields p arg
HsRecFields [LHsRecField GhcTcId (Located (Pat GhcTcId))]
rpats' Maybe (Located Int)
dd), r
res) }
  where
    tc_field :: Checker (LHsRecField GhcRn (LPat GhcRn))
                        (LHsRecField GhcTcId (LPat GhcTcId))
    tc_field :: LHsRecField GhcRn (LPat GhcRn)
-> PatEnv -> TcM r -> TcM (LHsRecField GhcTcId (LPat GhcTcId), r)
tc_field (LHsRecField GhcRn (LPat GhcRn)
-> Located (SrcSpanLess (LHsRecField GhcRn (Located (Pat GhcRn))))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L SrcSpan
l (HsRecField (dL->L loc
                                    (FieldOcc sel (dL->L lr rdr))) pat pun))
             PatEnv
penv TcM r
thing_inside
      = do { TcId
sel'   <- Name -> TcM TcId
tcLookupId Name
XCFieldOcc GhcRn
sel
           ; TcSigmaType
pat_ty <- SrcSpan -> TcM TcSigmaType -> TcM TcSigmaType
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
loc (TcM TcSigmaType -> TcM TcSigmaType)
-> TcM TcSigmaType -> TcM TcSigmaType
forall a b. (a -> b) -> a -> b
$ Name -> FieldLabelString -> TcM TcSigmaType
find_field_ty Name
XCFieldOcc GhcRn
sel
                                          (OccName -> FieldLabelString
occNameFS (OccName -> FieldLabelString) -> OccName -> FieldLabelString
forall a b. (a -> b) -> a -> b
$ RdrName -> OccName
rdrNameOcc SrcSpanLess (Located RdrName)
RdrName
rdr)
           ; (Located (Pat GhcTcId)
pat', r
res) <- (LPat GhcRn, TcSigmaType)
-> PatEnv -> TcM r -> TcM (LPat GhcTcId, r)
Checker (LPat GhcRn, TcSigmaType) (LPat GhcTcId)
tcConArg (Located (Pat GhcRn)
LPat GhcRn
pat, TcSigmaType
pat_ty) PatEnv
penv TcM r
thing_inside
           ; (LHsRecField GhcTcId (Located (Pat GhcTcId)), r)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (LHsRecField GhcTcId (Located (Pat GhcTcId)), r)
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpan
-> SrcSpanLess (LHsRecField GhcTcId (Located (Pat GhcTcId)))
-> LHsRecField GhcTcId (Located (Pat GhcTcId))
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
l (Located (FieldOcc GhcTcId)
-> Located (Pat GhcTcId)
-> Bool
-> HsRecField' (FieldOcc GhcTcId) (Located (Pat GhcTcId))
forall id arg. Located id -> arg -> Bool -> HsRecField' id arg
HsRecField (SrcSpan
-> SrcSpanLess (Located (FieldOcc GhcTcId))
-> Located (FieldOcc GhcTcId)
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
loc (XCFieldOcc GhcTcId -> Located RdrName -> FieldOcc GhcTcId
forall pass. XCFieldOcc pass -> Located RdrName -> FieldOcc pass
FieldOcc TcId
XCFieldOcc GhcTcId
sel' (SrcSpan -> SrcSpanLess (Located RdrName) -> Located RdrName
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
lr SrcSpanLess (Located RdrName)
rdr))) Located (Pat GhcTcId)
pat'
                                                                    Bool
pun), r
res) }
    tc_field (LHsRecField GhcRn (LPat GhcRn)
-> Located (SrcSpanLess (LHsRecField GhcRn (Located (Pat GhcRn))))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L SrcSpan
_ (HsRecField (dL->L _ (XFieldOcc _)) _ _)) PatEnv
_ TcM r
_
           = String
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (LHsRecField GhcTcId (Located (Pat GhcTcId)), r)
forall a. String -> a
panic String
"tcConArgs"
    tc_field LHsRecField GhcRn (LPat GhcRn)
_ PatEnv
_ TcM r
_ = String
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (LHsRecField GhcTcId (Located (Pat GhcTcId)), r)
forall a. String -> a
panic String
"tc_field: Impossible Match"
                             -- due to #15884


    find_field_ty :: Name -> FieldLabelString -> TcM TcType
    find_field_ty :: Name -> FieldLabelString -> TcM TcSigmaType
find_field_ty Name
sel FieldLabelString
lbl
        = case [TcSigmaType
ty | (FieldLabel
fl, TcSigmaType
ty) <- [(FieldLabel, TcSigmaType)]
field_tys, FieldLabel -> Name
forall a. FieldLbl a -> a
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".
           [] -> SDoc -> TcM TcSigmaType
forall a. SDoc -> TcRn a
failWith (ConLike -> FieldLabelString -> SDoc
badFieldCon ConLike
con_like FieldLabelString
lbl)

                -- The normal case, when the field comes from the right constructor
           (TcSigmaType
pat_ty : [TcSigmaType]
extras) -> do
                String -> SDoc -> TcRn ()
traceTc String
"find_field" (TcSigmaType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcSigmaType
pat_ty SDoc -> SDoc -> SDoc
<+> [TcSigmaType] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcSigmaType]
extras)
                ASSERT( null extras ) (return pat_ty)

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

tcConArg :: Checker (LPat GhcRn, TcSigmaType) (LPat GhcTc)
tcConArg :: (LPat GhcRn, TcSigmaType)
-> PatEnv -> TcM r -> TcM (LPat GhcTcId, r)
tcConArg (LPat GhcRn
arg_pat, TcSigmaType
arg_ty) PatEnv
penv TcM r
thing_inside
  = LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM r -> TcM (LPat GhcTcId, r)
forall a.
LPat GhcRn
-> ExpSigmaType -> PatEnv -> TcM a -> TcM (LPat GhcTcId, a)
tc_lpat LPat GhcRn
arg_pat (TcSigmaType -> ExpSigmaType
mkCheckExpType TcSigmaType
arg_ty) PatEnv
penv TcM r
thing_inside

addDataConStupidTheta :: DataCon -> [TcType] -> TcM ()
-- Instantiate the "stupid theta" of the data con, and throw
-- the constraints into the constraint set
addDataConStupidTheta :: DataCon -> [TcSigmaType] -> TcRn ()
addDataConStupidTheta DataCon
data_con [TcSigmaType]
inst_tys
  | [TcSigmaType] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TcSigmaType]
stupid_theta = () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  | Bool
otherwise         = CtOrigin -> [TcSigmaType] -> TcRn ()
instStupidTheta CtOrigin
origin [TcSigmaType]
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 :: [TcSigmaType]
stupid_theta = DataCon -> [TcSigmaType]
dataConStupidTheta DataCon
data_con
    univ_tvs :: [TcId]
univ_tvs     = DataCon -> [TcId]
dataConUnivTyVars DataCon
data_con
    tenv :: TCvSubst
tenv = [TcId] -> [TcSigmaType] -> TCvSubst
HasDebugCallStack => [TcId] -> [TcSigmaType] -> TCvSubst
zipTvSubst [TcId]
univ_tvs ([TcId] -> [TcSigmaType] -> [TcSigmaType]
forall b a. [b] -> [a] -> [a]
takeList [TcId]
univ_tvs [TcSigmaType]
inst_tys)
         -- NB: inst_tys can be longer than the univ tyvars
         --     because the constructor might have existentials
    inst_theta :: [TcSigmaType]
inst_theta = HasCallStack => TCvSubst -> [TcSigmaType] -> [TcSigmaType]
TCvSubst -> [TcSigmaType] -> [TcSigmaType]
substTheta TCvSubst
tenv [TcSigmaType]
stupid_theta

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

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

The Right Thing is not to confuse these constraints together. But for
now the Easy Thing is to ensure that we do not have existential or
GADT constraints in a 'proc', and to short-cut the constraint
simplification for such vanilla patterns so that it binds no
constraints. Hence the 'fast path' in tcConPat; but it's also a good
plan for ordinary vanilla patterns to bypass the constraint
simplification step.

************************************************************************
*                                                                      *
                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 at 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 TcBinds
-}

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 -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"In the pattern:") Int
2 (Pat GhcRn -> SDoc
forall a. Outputable a => a -> SDoc
ppr Pat GhcRn
pat)

-----------------------------------------------
checkExistentials :: [TyVar]   -- existentials
                  -> [Type]    -- argument types
                  -> PatEnv -> TcM ()
    -- See Note [Existential check]]
    -- See Note [Arrows and patterns]
checkExistentials :: [TcId] -> [TcSigmaType] -> PatEnv -> TcRn ()
checkExistentials [TcId]
ex_tvs [TcSigmaType]
tys PatEnv
_
  | (TcId -> Bool) -> [TcId] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Bool -> Bool
not (Bool -> Bool) -> (TcId -> Bool) -> TcId -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TcId -> VarSet -> Bool
`elemVarSet` [TcSigmaType] -> VarSet
tyCoVarsOfTypes [TcSigmaType]
tys)) [TcId]
ex_tvs = () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkExistentials [TcId]
_ [TcSigmaType]
_ (PE { pe_ctxt :: PatEnv -> PatCtxt
pe_ctxt = LetPat {}})         = () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
checkExistentials [TcId]
_ [TcSigmaType]
_ (PE { pe_ctxt :: PatEnv -> PatCtxt
pe_ctxt = LamPat HsMatchContext Name
ProcExpr })  = SDoc -> TcRn ()
forall a. SDoc -> TcRn a
failWithTc SDoc
existentialProcPat
checkExistentials [TcId]
_ [TcSigmaType]
_ (PE { pe_lazy :: PatEnv -> Bool
pe_lazy = Bool
True })             = SDoc -> TcRn ()
forall a. SDoc -> TcRn a
failWithTc SDoc
existentialLazyPat
checkExistentials [TcId]
_ [TcSigmaType]
_ PatEnv
_                                   = () -> TcRn ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

existentialLazyPat :: SDoc
existentialLazyPat :: SDoc
existentialLazyPat
  = SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"An existential or GADT data constructor cannot be used")
       Int
2 (String -> SDoc
text String
"inside a lazy (~) pattern")

existentialProcPat :: SDoc
existentialProcPat :: SDoc
existentialProcPat
  = String -> SDoc
text String
"Proc patterns cannot use existential or GADT data constructors"

badFieldCon :: ConLike -> FieldLabelString -> SDoc
badFieldCon :: ConLike -> FieldLabelString -> SDoc
badFieldCon ConLike
con FieldLabelString
field
  = [SDoc] -> SDoc
hsep [String -> SDoc
text String
"Constructor" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (ConLike -> SDoc
forall a. Outputable a => a -> SDoc
ppr ConLike
con),
          String -> SDoc
text String
"does not have field", SDoc -> SDoc
quotes (FieldLabelString -> SDoc
forall a. Outputable a => a -> SDoc
ppr FieldLabelString
field)]

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