{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections       #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE UndecidableInstances #-} -- Wrinkle in Note [Trees That Grow]
                                      -- in module Language.Haskell.Syntax.Extension
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns   #-}

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

-}

module GHC.Tc.Gen.Expr
       ( tcCheckPolyExpr, tcCheckPolyExprNC,
         tcCheckMonoExpr, tcCheckMonoExprNC,
         tcMonoExpr, tcMonoExprNC,
         tcInferRho, tcInferRhoNC,
         tcPolyExpr, tcExpr,
         tcSyntaxOp, tcSyntaxOpGen, SyntaxOpType(..), synKnownType,
         tcCheckId,
         getFixedTyVars ) where

import GHC.Prelude

import {-# SOURCE #-}   GHC.Tc.Gen.Splice( tcSpliceExpr, tcTypedBracket, tcUntypedBracket )

import GHC.Hs
import GHC.Hs.Syn.Type
import GHC.Rename.Utils
import GHC.Tc.Utils.Zonk
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.Unify
import GHC.Types.Basic
import GHC.Types.Error
import GHC.Core.Multiplicity
import GHC.Core.UsageEnv
import GHC.Tc.Errors.Types
import GHC.Tc.Utils.Concrete ( hasFixedRuntimeRep_syntactic )
import GHC.Tc.Utils.Instantiate
import GHC.Tc.Gen.App
import GHC.Tc.Gen.Head
import GHC.Tc.Gen.Bind        ( tcLocalBinds )
import GHC.Tc.Instance.Family ( tcGetFamInstEnvs )
import GHC.Core.FamInstEnv    ( FamInstEnvs )
import GHC.Rename.Env         ( addUsedGRE )
import GHC.Tc.Utils.Env
import GHC.Tc.Gen.Arrow
import GHC.Tc.Gen.Match
import GHC.Tc.Gen.HsType
import GHC.Tc.Utils.TcMType
import GHC.Tc.Types.Origin
import GHC.Tc.Utils.TcType as TcType
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Core.ConLike
import GHC.Core.DataCon
import GHC.Core.PatSyn
import GHC.Types.Name
import GHC.Types.Name.Env
import GHC.Types.Name.Set
import GHC.Types.Name.Reader
import GHC.Core.TyCon
import GHC.Core.Type
import GHC.Tc.Types.Evidence
import GHC.Types.Var.Set
import GHC.Builtin.Types
import GHC.Builtin.Names
import GHC.Driver.Session
import GHC.Types.SrcLoc
import GHC.Utils.Misc
import GHC.Data.List.SetOps
import GHC.Data.Maybe
import GHC.Utils.Outputable as Outputable
import GHC.Utils.Panic
import GHC.Utils.Panic.Plain
import Control.Monad
import GHC.Core.Class(classTyCon)
import GHC.Types.Unique.Set ( UniqSet, mkUniqSet, elementOfUniqSet, nonDetEltsUniqSet )

import Data.Function
import Data.List (partition, sortBy, groupBy, intersect)

{-
************************************************************************
*                                                                      *
\subsection{Main wrappers}
*                                                                      *
************************************************************************
-}


tcCheckPolyExpr, tcCheckPolyExprNC
  :: LHsExpr GhcRn         -- Expression to type check
  -> TcSigmaType           -- Expected type (could be a polytype)
  -> TcM (LHsExpr GhcTc) -- Generalised expr with expected type

-- tcCheckPolyExpr is a convenient place (frequent but not too frequent)
-- place to add context information.
-- The NC version does not do so, usually because the caller wants
-- to do so themselves.

tcCheckPolyExpr :: LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr   LHsExpr GhcRn
expr Type
res_ty = LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcPolyLExpr   LHsExpr GhcRn
expr (Type -> ExpRhoType
mkCheckExpType Type
res_ty)
tcCheckPolyExprNC :: LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExprNC LHsExpr GhcRn
expr Type
res_ty = LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcPolyLExprNC LHsExpr GhcRn
expr (Type -> ExpRhoType
mkCheckExpType Type
res_ty)

-- These versions take an ExpType
tcPolyLExpr, tcPolyLExprNC :: LHsExpr GhcRn -> ExpSigmaType
                           -> TcM (LHsExpr GhcTc)

tcPolyLExpr :: LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcPolyLExpr (L SrcSpanAnnA
loc HsExpr GhcRn
expr) ExpRhoType
res_ty
  = forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc   forall a b. (a -> b) -> a -> b
$  -- Set location /first/; see GHC.Tc.Utils.Monad
    forall a. HsExpr GhcRn -> TcRn a -> TcRn a
addExprCtxt HsExpr GhcRn
expr forall a b. (a -> b) -> a -> b
$  -- Note [Error contexts in generated code]
    do { HsExpr GhcTc
expr' <- HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcPolyExpr HsExpr GhcRn
expr ExpRhoType
res_ty
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc HsExpr GhcTc
expr') }

tcPolyLExprNC :: LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcPolyLExprNC (L SrcSpanAnnA
loc HsExpr GhcRn
expr) ExpRhoType
res_ty
  = forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc    forall a b. (a -> b) -> a -> b
$
    do { HsExpr GhcTc
expr' <- HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcPolyExpr HsExpr GhcRn
expr ExpRhoType
res_ty
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc HsExpr GhcTc
expr') }

---------------
tcCheckMonoExpr, tcCheckMonoExprNC
    :: LHsExpr GhcRn     -- Expression to type check
    -> TcRhoType         -- Expected type
                         -- Definitely no foralls at the top
    -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr :: LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr   LHsExpr GhcRn
expr Type
res_ty = LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcMonoExpr   LHsExpr GhcRn
expr (Type -> ExpRhoType
mkCheckExpType Type
res_ty)
tcCheckMonoExprNC :: LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckMonoExprNC LHsExpr GhcRn
expr Type
res_ty = LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcMonoExprNC LHsExpr GhcRn
expr (Type -> ExpRhoType
mkCheckExpType Type
res_ty)

tcMonoExpr, tcMonoExprNC
    :: LHsExpr GhcRn     -- Expression to type check
    -> ExpRhoType        -- Expected type
                         -- Definitely no foralls at the top
    -> TcM (LHsExpr GhcTc)

tcMonoExpr :: LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcMonoExpr (L SrcSpanAnnA
loc HsExpr GhcRn
expr) ExpRhoType
res_ty
  = forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc   forall a b. (a -> b) -> a -> b
$  -- Set location /first/; see GHC.Tc.Utils.Monad
    forall a. HsExpr GhcRn -> TcRn a -> TcRn a
addExprCtxt HsExpr GhcRn
expr forall a b. (a -> b) -> a -> b
$  -- Note [Error contexts in generated code]
    do  { HsExpr GhcTc
expr' <- HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcExpr HsExpr GhcRn
expr ExpRhoType
res_ty
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc HsExpr GhcTc
expr') }

tcMonoExprNC :: LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcMonoExprNC (L SrcSpanAnnA
loc HsExpr GhcRn
expr) ExpRhoType
res_ty
  = forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc forall a b. (a -> b) -> a -> b
$
    do  { HsExpr GhcTc
expr' <- HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcExpr HsExpr GhcRn
expr ExpRhoType
res_ty
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc HsExpr GhcTc
expr') }

---------------
tcInferRho, tcInferRhoNC :: LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcRhoType)
-- Infer a *rho*-type. The return type is always instantiated.
tcInferRho :: LHsExpr GhcRn -> TcM (LHsExpr GhcTc, Type)
tcInferRho (L SrcSpanAnnA
loc HsExpr GhcRn
expr)
  = forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc   forall a b. (a -> b) -> a -> b
$  -- Set location /first/; see GHC.Tc.Utils.Monad
    forall a. HsExpr GhcRn -> TcRn a -> TcRn a
addExprCtxt HsExpr GhcRn
expr forall a b. (a -> b) -> a -> b
$  -- Note [Error contexts in generated code]
    do { (HsExpr GhcTc
expr', Type
rho) <- forall a. (ExpRhoType -> TcM a) -> TcM (a, Type)
tcInfer (HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcExpr HsExpr GhcRn
expr)
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc HsExpr GhcTc
expr', Type
rho) }

tcInferRhoNC :: LHsExpr GhcRn -> TcM (LHsExpr GhcTc, Type)
tcInferRhoNC (L SrcSpanAnnA
loc HsExpr GhcRn
expr)
  = forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc forall a b. (a -> b) -> a -> b
$
    do { (HsExpr GhcTc
expr', Type
rho) <- forall a. (ExpRhoType -> TcM a) -> TcM (a, Type)
tcInfer (HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcExpr HsExpr GhcRn
expr)
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc HsExpr GhcTc
expr', Type
rho) }


{- *********************************************************************
*                                                                      *
        tcExpr: the main expression typechecker
*                                                                      *
********************************************************************* -}

tcPolyExpr :: HsExpr GhcRn -> ExpSigmaType -> TcM (HsExpr GhcTc)
tcPolyExpr :: HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcPolyExpr HsExpr GhcRn
expr ExpRhoType
res_ty
  = do { String -> SDoc -> TcRn ()
traceTc String
"tcPolyExpr" (forall a. Outputable a => a -> SDoc
ppr ExpRhoType
res_ty)
       ; (HsWrapper
wrap, HsExpr GhcTc
expr') <- forall result.
UserTypeCtxt
-> ExpRhoType
-> (ExpRhoType -> TcM result)
-> TcM (HsWrapper, result)
tcSkolemiseExpType UserTypeCtxt
GenSigCtxt ExpRhoType
res_ty forall a b. (a -> b) -> a -> b
$ \ ExpRhoType
res_ty ->
                          HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcExpr HsExpr GhcRn
expr ExpRhoType
res_ty
       ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
wrap HsExpr GhcTc
expr' }

tcExpr :: HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)

-- Use tcApp to typecheck appplications, which are treated specially
-- by Quick Look.  Specifically:
--   - HsVar         lone variables, to ensure that they can get an
--                     impredicative instantiation (via Quick Look
--                     driven by res_ty (in checking mode)).
--   - HsApp         value applications
--   - HsAppType     type applications
--   - ExprWithTySig (e :: type)
--   - HsRecSel      overloaded record fields
--   - HsExpanded    renamer expansions
--   - HsOpApp       operator applications
--   - HsOverLit     overloaded literals
-- These constructors are the union of
--   - ones taken apart by GHC.Tc.Gen.Head.splitHsApps
--   - ones understood by GHC.Tc.Gen.Head.tcInferAppHead_maybe
-- See Note [Application chains and heads] in GHC.Tc.Gen.App
tcExpr :: HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcExpr e :: HsExpr GhcRn
e@(HsVar {})              ExpRhoType
res_ty = HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcApp HsExpr GhcRn
e ExpRhoType
res_ty
tcExpr e :: HsExpr GhcRn
e@(HsApp {})              ExpRhoType
res_ty = HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcApp HsExpr GhcRn
e ExpRhoType
res_ty
tcExpr e :: HsExpr GhcRn
e@(OpApp {})              ExpRhoType
res_ty = HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcApp HsExpr GhcRn
e ExpRhoType
res_ty
tcExpr e :: HsExpr GhcRn
e@(HsAppType {})          ExpRhoType
res_ty = HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcApp HsExpr GhcRn
e ExpRhoType
res_ty
tcExpr e :: HsExpr GhcRn
e@(ExprWithTySig {})      ExpRhoType
res_ty = HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcApp HsExpr GhcRn
e ExpRhoType
res_ty
tcExpr e :: HsExpr GhcRn
e@(HsRecSel {})           ExpRhoType
res_ty = HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcApp HsExpr GhcRn
e ExpRhoType
res_ty
tcExpr e :: HsExpr GhcRn
e@(XExpr (HsExpanded {})) ExpRhoType
res_ty = HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcApp HsExpr GhcRn
e ExpRhoType
res_ty

tcExpr e :: HsExpr GhcRn
e@(HsOverLit XOverLitE GhcRn
_ HsOverLit GhcRn
lit) ExpRhoType
res_ty
  = do { Maybe (HsOverLit GhcTc)
mb_res <- HsOverLit GhcRn -> ExpRhoType -> TcM (Maybe (HsOverLit GhcTc))
tcShortCutLit HsOverLit GhcRn
lit ExpRhoType
res_ty
         -- See Note [Short cut for overloaded literals] in GHC.Tc.Utils.Zonk
       ; case Maybe (HsOverLit GhcTc)
mb_res of
           Just HsOverLit GhcTc
lit' -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XOverLitE p -> HsOverLit p -> HsExpr p
HsOverLit forall a. EpAnn a
noAnn HsOverLit GhcTc
lit')
           Maybe (HsOverLit GhcTc)
Nothing   -> HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcApp HsExpr GhcRn
e ExpRhoType
res_ty }

-- Typecheck an occurrence of an unbound Id
--
-- Some of these started life as a true expression hole "_".
-- Others might simply be variables that accidentally have no binding site
tcExpr (HsUnboundVar XUnboundVar GhcRn
_ OccName
occ) ExpRhoType
res_ty
  = do { Type
ty <- ExpRhoType -> TcM Type
expTypeToType ExpRhoType
res_ty    -- Allow Int# etc (#12531)
       ; HoleExprRef
her <- OccName -> Type -> TcM HoleExprRef
emitNewExprHole OccName
occ Type
ty
       ; UsageEnv -> TcRn ()
tcEmitBindingUsage UsageEnv
bottomUE   -- Holes fit any usage environment
                                       -- (#18491)
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XUnboundVar p -> OccName -> HsExpr p
HsUnboundVar HoleExprRef
her OccName
occ) }

tcExpr e :: HsExpr GhcRn
e@(HsLit XLitE GhcRn
x HsLit GhcRn
lit) ExpRhoType
res_ty
  = do { let lit_ty :: Type
lit_ty = forall (p :: Pass). HsLit (GhcPass p) -> Type
hsLitType HsLit GhcRn
lit
       ; HsExpr GhcRn
-> HsExpr GhcTc -> Type -> ExpRhoType -> TcM (HsExpr GhcTc)
tcWrapResult HsExpr GhcRn
e (forall p. XLitE p -> HsLit p -> HsExpr p
HsLit XLitE GhcRn
x (forall (p1 :: Pass) (p2 :: Pass).
HsLit (GhcPass p1) -> HsLit (GhcPass p2)
convertLit HsLit GhcRn
lit)) Type
lit_ty ExpRhoType
res_ty }

tcExpr (HsPar XPar GhcRn
x LHsToken "(" GhcRn
lpar LHsExpr GhcRn
expr LHsToken ")" GhcRn
rpar) ExpRhoType
res_ty
  = do { GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' <- LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcMonoExprNC LHsExpr GhcRn
expr ExpRhoType
res_ty
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p.
XPar p -> LHsToken "(" p -> LHsExpr p -> LHsToken ")" p -> HsExpr p
HsPar XPar GhcRn
x LHsToken "(" GhcRn
lpar GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' LHsToken ")" GhcRn
rpar) }

tcExpr (HsPragE XPragE GhcRn
x HsPragE GhcRn
prag LHsExpr GhcRn
expr) ExpRhoType
res_ty
  = do { GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' <- LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcMonoExpr LHsExpr GhcRn
expr ExpRhoType
res_ty
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XPragE p -> HsPragE p -> LHsExpr p -> HsExpr p
HsPragE XPragE GhcRn
x (HsPragE GhcRn -> HsPragE GhcTc
tcExprPrag HsPragE GhcRn
prag) GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr') }

tcExpr (NegApp XNegApp GhcRn
x LHsExpr GhcRn
expr SyntaxExpr GhcRn
neg_expr) ExpRhoType
res_ty
  = do  { (GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr', SyntaxExprTc
neg_expr')
            <- forall a.
CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> ExpRhoType
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, SyntaxExprTc)
tcSyntaxOp CtOrigin
NegateOrigin SyntaxExpr GhcRn
neg_expr [SyntaxOpType
SynAny] ExpRhoType
res_ty forall a b. (a -> b) -> a -> b
$
               \[Type
arg_ty] [Type
arg_mult] ->
               forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
arg_mult forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
expr Type
arg_ty
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XNegApp p -> LHsExpr p -> SyntaxExpr p -> HsExpr p
NegApp XNegApp GhcRn
x GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' SyntaxExprTc
neg_expr') }

tcExpr e :: HsExpr GhcRn
e@(HsIPVar XIPVar GhcRn
_ HsIPName
x) ExpRhoType
res_ty
  = do { Type
ip_ty <- Type -> TcM Type
newFlexiTyVarTy Type
liftedTypeKind
          -- Create a unification type variable of kind 'Type'.
          -- (The type of an implicit parameter must have kind 'Type'.)
       ; let ip_name :: Type
ip_name = FieldLabelString -> Type
mkStrLitTy (HsIPName -> FieldLabelString
hsIPNameFS HsIPName
x)
       ; Class
ipClass <- Name -> TcM Class
tcLookupClass Name
ipClassName
       ; EvVar
ip_var <- CtOrigin -> Type -> TcM EvVar
emitWantedEvVar CtOrigin
origin (Class -> [Type] -> Type
mkClassPred Class
ipClass [Type
ip_name, Type
ip_ty])
       ; HsExpr GhcRn
-> HsExpr GhcTc -> Type -> ExpRhoType -> TcM (HsExpr GhcTc)
tcWrapResult HsExpr GhcRn
e
                   (Class -> Type -> Type -> HsExpr GhcTc -> HsExpr GhcTc
fromDict Class
ipClass Type
ip_name Type
ip_ty (forall p. XVar p -> LIdP p -> HsExpr p
HsVar NoExtField
noExtField (forall a an. a -> LocatedAn an a
noLocA EvVar
ip_var)))
                   Type
ip_ty ExpRhoType
res_ty }
  where
  -- Coerces a dictionary for `IP "x" t` into `t`.
  fromDict :: Class -> Type -> Type -> HsExpr GhcTc -> HsExpr GhcTc
fromDict Class
ipClass Type
x Type
ty = HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap forall a b. (a -> b) -> a -> b
$ TcCoercionR -> HsWrapper
mkWpCastR forall a b. (a -> b) -> a -> b
$
                          Type -> TcCoercionR
unwrapIP forall a b. (a -> b) -> a -> b
$ Class -> [Type] -> Type
mkClassPred Class
ipClass [Type
x,Type
ty]
  origin :: CtOrigin
origin = HsIPName -> CtOrigin
IPOccOrigin HsIPName
x

tcExpr (HsLam XLam GhcRn
_ MatchGroup GhcRn (LHsExpr GhcRn)
match) ExpRhoType
res_ty
  = do  { (HsWrapper
wrap, MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
match') <- ExpectedFunTyOrigin
-> TcMatchCtxt HsExpr
-> MatchGroup GhcRn (LHsExpr GhcRn)
-> ExpRhoType
-> TcM (HsWrapper, MatchGroup GhcTc (LHsExpr GhcTc))
tcMatchLambda ExpectedFunTyOrigin
herald TcMatchCtxt HsExpr
match_ctxt MatchGroup GhcRn (LHsExpr GhcRn)
match ExpRhoType
res_ty
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
wrap (forall p. XLam p -> MatchGroup p (LHsExpr p) -> HsExpr p
HsLam NoExtField
noExtField MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
match')) }
  where
    match_ctxt :: TcMatchCtxt HsExpr
match_ctxt = MC { mc_what :: HsMatchContext GhcTc
mc_what = forall p. HsMatchContext p
LambdaExpr, mc_body :: GenLocated SrcSpanAnnA (HsExpr GhcRn)
-> ExpRhoType -> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc))
mc_body = LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcBody }
    herald :: ExpectedFunTyOrigin
herald = MatchGroup GhcRn (LHsExpr GhcRn) -> ExpectedFunTyOrigin
ExpectedFunTyLam MatchGroup GhcRn (LHsExpr GhcRn)
match

tcExpr e :: HsExpr GhcRn
e@(HsLamCase XLamCase GhcRn
x LamCaseVariant
lc_variant MatchGroup GhcRn (LHsExpr GhcRn)
matches) ExpRhoType
res_ty
  = do { (HsWrapper
wrap, MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
matches')
           <- ExpectedFunTyOrigin
-> TcMatchCtxt HsExpr
-> MatchGroup GhcRn (LHsExpr GhcRn)
-> ExpRhoType
-> TcM (HsWrapper, MatchGroup GhcTc (LHsExpr GhcTc))
tcMatchLambda ExpectedFunTyOrigin
herald TcMatchCtxt HsExpr
match_ctxt MatchGroup GhcRn (LHsExpr GhcRn)
matches ExpRhoType
res_ty
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
wrap forall a b. (a -> b) -> a -> b
$ forall p.
XLamCase p
-> LamCaseVariant -> MatchGroup p (LHsExpr p) -> HsExpr p
HsLamCase XLamCase GhcRn
x LamCaseVariant
lc_variant MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
matches') }
  where
    match_ctxt :: TcMatchCtxt HsExpr
match_ctxt = MC { mc_what :: HsMatchContext GhcTc
mc_what = forall p. LamCaseVariant -> HsMatchContext p
LamCaseAlt LamCaseVariant
lc_variant, mc_body :: GenLocated SrcSpanAnnA (HsExpr GhcRn)
-> ExpRhoType -> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc))
mc_body = LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcBody }
    herald :: ExpectedFunTyOrigin
herald = LamCaseVariant -> HsExpr GhcRn -> ExpectedFunTyOrigin
ExpectedFunTyLamCase LamCaseVariant
lc_variant HsExpr GhcRn
e



{-
************************************************************************
*                                                                      *
                Explicit lists
*                                                                      *
************************************************************************
-}

-- Explict lists [e1,e2,e3] have been expanded already in the renamer
-- The expansion includes an ExplicitList, but it is always the built-in
-- list type, so that's all we need concern ourselves with here.  See
-- GHC.Rename.Expr. Note [Handling overloaded and rebindable constructs]
tcExpr (ExplicitList XExplicitList GhcRn
_ [LHsExpr GhcRn]
exprs) ExpRhoType
res_ty
  = do  { Type
res_ty <- ExpRhoType -> TcM Type
expTypeToType ExpRhoType
res_ty
        ; (TcCoercionR
coi, Type
elt_ty) <- Type -> TcM (TcCoercionR, Type)
matchExpectedListTy Type
res_ty
        ; let tc_elt :: GenLocated SrcSpanAnnA (HsExpr GhcRn) -> TcM (LHsExpr GhcTc)
tc_elt GenLocated SrcSpanAnnA (HsExpr GhcRn)
expr = LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr GenLocated SrcSpanAnnA (HsExpr GhcRn)
expr Type
elt_ty
        ; [GenLocated SrcSpanAnnA (HsExpr GhcTc)]
exprs' <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM GenLocated SrcSpanAnnA (HsExpr GhcRn) -> TcM (LHsExpr GhcTc)
tc_elt [LHsExpr GhcRn]
exprs
        ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ TcCoercionR -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrapCo TcCoercionR
coi forall a b. (a -> b) -> a -> b
$ forall p. XExplicitList p -> [LHsExpr p] -> HsExpr p
ExplicitList Type
elt_ty [GenLocated SrcSpanAnnA (HsExpr GhcTc)]
exprs' }

tcExpr expr :: HsExpr GhcRn
expr@(ExplicitTuple XExplicitTuple GhcRn
x [HsTupArg GhcRn]
tup_args Boxity
boxity) ExpRhoType
res_ty
  | forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all forall (p :: Pass). HsTupArg (GhcPass p) -> Bool
tupArgPresent [HsTupArg GhcRn]
tup_args
  = do { let arity :: Int
arity  = forall (t :: * -> *) a. Foldable t => t a -> Int
length [HsTupArg GhcRn]
tup_args
             tup_tc :: TyCon
tup_tc = Boxity -> Int -> TyCon
tupleTyCon Boxity
boxity Int
arity
               -- NB: tupleTyCon doesn't flatten 1-tuples
               -- See Note [Don't flatten tuples from HsSyn] in GHC.Core.Make
       ; Type
res_ty <- ExpRhoType -> TcM Type
expTypeToType ExpRhoType
res_ty
       ; (TcCoercionR
coi, [Type]
arg_tys) <- TyCon -> Type -> TcM (TcCoercionR, [Type])
matchExpectedTyConApp TyCon
tup_tc Type
res_ty
                           -- Unboxed tuples have RuntimeRep vars, which we
                           -- don't care about here
                           -- See Note [Unboxed tuple RuntimeRep vars] in GHC.Core.TyCon
       ; let arg_tys' :: [Type]
arg_tys' = case Boxity
boxity of Boxity
Unboxed -> forall a. Int -> [a] -> [a]
drop Int
arity [Type]
arg_tys
                                       Boxity
Boxed   -> [Type]
arg_tys
       ; [HsTupArg GhcTc]
tup_args1 <- [HsTupArg GhcRn] -> [Type] -> TcM [HsTupArg GhcTc]
tcTupArgs [HsTupArg GhcRn]
tup_args [Type]
arg_tys'
       ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ TcCoercionR -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrapCo TcCoercionR
coi (forall p. XExplicitTuple p -> [HsTupArg p] -> Boxity -> HsExpr p
ExplicitTuple XExplicitTuple GhcRn
x [HsTupArg GhcTc]
tup_args1 Boxity
boxity) }

  | Bool
otherwise
  = -- The tup_args are a mixture of Present and Missing (for tuple sections)
    do { let arity :: Int
arity = forall (t :: * -> *) a. Foldable t => t a -> Int
length [HsTupArg GhcRn]
tup_args

       ; [Type]
arg_tys <- case Boxity
boxity of
           { Boxity
Boxed   -> Int -> Type -> IOEnv (Env TcGblEnv TcLclEnv) [Type]
newFlexiTyVarTys Int
arity Type
liftedTypeKind
           ; Boxity
Unboxed -> forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
arity TcM Type
newOpenFlexiTyVarTy }

       -- Handle tuple sections where
       ; [HsTupArg GhcTc]
tup_args1 <- [HsTupArg GhcRn] -> [Type] -> TcM [HsTupArg GhcTc]
tcTupArgs [HsTupArg GhcRn]
tup_args [Type]
arg_tys

       ; let expr' :: HsExpr GhcTc
expr'       = forall p. XExplicitTuple p -> [HsTupArg p] -> Boxity -> HsExpr p
ExplicitTuple XExplicitTuple GhcRn
x [HsTupArg GhcTc]
tup_args1 Boxity
boxity
             missing_tys :: [Scaled Type]
missing_tys = [forall a. Type -> a -> Scaled a
Scaled Type
mult Type
ty | (Missing (Scaled Type
mult Type
_), Type
ty) <- forall a b. [a] -> [b] -> [(a, b)]
zip [HsTupArg GhcTc]
tup_args1 [Type]
arg_tys]

             -- See Note [Typechecking data constructors] in GHC.Tc.Gen.Head
             -- See Note [Don't flatten tuples from HsSyn] in GHC.Core.Make
             act_res_ty :: Type
act_res_ty = [Scaled Type] -> Type -> Type
mkVisFunTys [Scaled Type]
missing_tys (Boxity -> [Type] -> Type
mkTupleTy1 Boxity
boxity [Type]
arg_tys)

       ; String -> SDoc -> TcRn ()
traceTc String
"ExplicitTuple" (forall a. Outputable a => a -> SDoc
ppr Type
act_res_ty SDoc -> SDoc -> SDoc
$$ forall a. Outputable a => a -> SDoc
ppr ExpRhoType
res_ty)

       ; HsExpr GhcRn
-> HsExpr GhcTc -> Type -> ExpRhoType -> TcM (HsExpr GhcTc)
tcWrapResultMono HsExpr GhcRn
expr HsExpr GhcTc
expr' Type
act_res_ty ExpRhoType
res_ty }

tcExpr (ExplicitSum XExplicitSum GhcRn
_ Int
alt Int
arity LHsExpr GhcRn
expr) ExpRhoType
res_ty
  = do { let sum_tc :: TyCon
sum_tc = Int -> TyCon
sumTyCon Int
arity
       ; Type
res_ty <- ExpRhoType -> TcM Type
expTypeToType ExpRhoType
res_ty
       ; (TcCoercionR
coi, [Type]
arg_tys) <- TyCon -> Type -> TcM (TcCoercionR, [Type])
matchExpectedTyConApp TyCon
sum_tc Type
res_ty
       ; -- Drop levity vars, we don't care about them here
         let arg_tys' :: [Type]
arg_tys' = forall a. Int -> [a] -> [a]
drop Int
arity [Type]
arg_tys
             arg_ty :: Type
arg_ty   = [Type]
arg_tys' forall a. Outputable a => [a] -> Int -> a
`getNth` (Int
alt forall a. Num a => a -> a -> a
- Int
1)
       ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' <- LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr Type
arg_ty
       -- Check the whole res_ty, not just the arg_ty, to avoid #20277.
       -- Example:
       --   a :: TYPE rep (representation-polymorphic)
       --   (# 17# | #) :: (# Int# | a #)
       -- This should cause an error, even though (17# :: Int#)
       -- is not representation-polymorphic: we don't know how
       -- wide the concrete representation of the sum type will be.
       ; HasDebugCallStack => FixedRuntimeRepContext -> Type -> TcRn ()
hasFixedRuntimeRep_syntactic FixedRuntimeRepContext
FRRUnboxedSum Type
res_ty
       ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ TcCoercionR -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrapCo TcCoercionR
coi (forall p. XExplicitSum p -> Int -> Int -> LHsExpr p -> HsExpr p
ExplicitSum [Type]
arg_tys' Int
alt Int
arity GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' ) }


{-
************************************************************************
*                                                                      *
                Let, case, if, do
*                                                                      *
************************************************************************
-}

tcExpr (HsLet XLet GhcRn
x LHsToken "let" GhcRn
tkLet HsLocalBinds GhcRn
binds LHsToken "in" GhcRn
tkIn LHsExpr GhcRn
expr) ExpRhoType
res_ty
  = do  { (HsLocalBinds GhcTc
binds', GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr') <- forall thing.
HsLocalBinds GhcRn -> TcM thing -> TcM (HsLocalBinds GhcTc, thing)
tcLocalBinds HsLocalBinds GhcRn
binds forall a b. (a -> b) -> a -> b
$
                             LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcMonoExpr LHsExpr GhcRn
expr ExpRhoType
res_ty
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p.
XLet p
-> LHsToken "let" p
-> HsLocalBinds p
-> LHsToken "in" p
-> LHsExpr p
-> HsExpr p
HsLet XLet GhcRn
x LHsToken "let" GhcRn
tkLet HsLocalBinds GhcTc
binds' LHsToken "in" GhcRn
tkIn GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr') }

tcExpr (HsCase XCase GhcRn
x LHsExpr GhcRn
scrut MatchGroup GhcRn (LHsExpr GhcRn)
matches) ExpRhoType
res_ty
  = do  {  -- We used to typecheck the case alternatives first.
           -- The case patterns tend to give good type info to use
           -- when typechecking the scrutinee.  For example
           --   case (map f) of
           --     (x:xs) -> ...
           -- will report that map is applied to too few arguments
           --
           -- But now, in the GADT world, we need to typecheck the scrutinee
           -- first, to get type info that may be refined in the case alternatives
          Type
mult <- Type -> TcM Type
newFlexiTyVarTy Type
multiplicityTy

          -- Typecheck the scrutinee.  We use tcInferRho but tcInferSigma
          -- would also be possible (tcMatchesCase accepts sigma-types)
          -- Interesting litmus test: do these two behave the same?
          --     case id        of {..}
          --     case (\v -> v) of {..}
          -- This design choice is discussed in #17790
        ; (GenLocated SrcSpanAnnA (HsExpr GhcTc)
scrut', Type
scrut_ty) <- forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
mult forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> TcM (LHsExpr GhcTc, Type)
tcInferRho LHsExpr GhcRn
scrut

        ; String -> SDoc -> TcRn ()
traceTc String
"HsCase" (forall a. Outputable a => a -> SDoc
ppr Type
scrut_ty)
        ; HasDebugCallStack => FixedRuntimeRepContext -> Type -> TcRn ()
hasFixedRuntimeRep_syntactic FixedRuntimeRepContext
FRRCase Type
scrut_ty
        ; MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
matches' <- forall (body :: * -> *).
AnnoBody body =>
TcMatchCtxt body
-> Scaled Type
-> MatchGroup GhcRn (LocatedA (body GhcRn))
-> ExpRhoType
-> TcM (MatchGroup GhcTc (LocatedA (body GhcTc)))
tcMatchesCase TcMatchCtxt HsExpr
match_ctxt (forall a. Type -> a -> Scaled a
Scaled Type
mult Type
scrut_ty) MatchGroup GhcRn (LHsExpr GhcRn)
matches ExpRhoType
res_ty
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p.
XCase p -> LHsExpr p -> MatchGroup p (LHsExpr p) -> HsExpr p
HsCase XCase GhcRn
x GenLocated SrcSpanAnnA (HsExpr GhcTc)
scrut' MatchGroup GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
matches') }
 where
    match_ctxt :: TcMatchCtxt HsExpr
match_ctxt = MC { mc_what :: HsMatchContext GhcTc
mc_what = forall p. HsMatchContext p
CaseAlt,
                      mc_body :: GenLocated SrcSpanAnnA (HsExpr GhcRn)
-> ExpRhoType -> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc))
mc_body = LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcBody }

tcExpr (HsIf XIf GhcRn
x LHsExpr GhcRn
pred LHsExpr GhcRn
b1 LHsExpr GhcRn
b2) ExpRhoType
res_ty
  = do { GenLocated SrcSpanAnnA (HsExpr GhcTc)
pred'    <- LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckMonoExpr LHsExpr GhcRn
pred Type
boolTy
       ; (UsageEnv
u1,GenLocated SrcSpanAnnA (HsExpr GhcTc)
b1') <- forall a. TcM a -> TcM (UsageEnv, a)
tcCollectingUsage forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcMonoExpr LHsExpr GhcRn
b1 ExpRhoType
res_ty
       ; (UsageEnv
u2,GenLocated SrcSpanAnnA (HsExpr GhcTc)
b2') <- forall a. TcM a -> TcM (UsageEnv, a)
tcCollectingUsage forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcMonoExpr LHsExpr GhcRn
b2 ExpRhoType
res_ty
       ; UsageEnv -> TcRn ()
tcEmitBindingUsage (UsageEnv -> UsageEnv -> UsageEnv
supUE UsageEnv
u1 UsageEnv
u2)
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XIf p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsIf XIf GhcRn
x GenLocated SrcSpanAnnA (HsExpr GhcTc)
pred' GenLocated SrcSpanAnnA (HsExpr GhcTc)
b1' GenLocated SrcSpanAnnA (HsExpr GhcTc)
b2') }

tcExpr (HsMultiIf XMultiIf GhcRn
_ [LGRHS GhcRn (LHsExpr GhcRn)]
alts) ExpRhoType
res_ty
  = do { [GenLocated
   (SrcAnn NoEpAnns)
   (GRHS GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
alts' <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (forall a b ann.
(a -> TcM b)
-> GenLocated (SrcSpanAnn' ann) a
-> TcRn (GenLocated (SrcSpanAnn' ann) b)
wrapLocMA forall a b. (a -> b) -> a -> b
$ forall (body :: * -> *).
TcMatchCtxt body
-> ExpRhoType
-> GRHS GhcRn (LocatedA (body GhcRn))
-> TcM (GRHS GhcTc (LocatedA (body GhcTc)))
tcGRHS TcMatchCtxt HsExpr
match_ctxt ExpRhoType
res_ty) [LGRHS GhcRn (LHsExpr GhcRn)]
alts
       ; Type
res_ty <- ExpRhoType -> TcM Type
readExpType ExpRhoType
res_ty
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XMultiIf p -> [LGRHS p (LHsExpr p)] -> HsExpr p
HsMultiIf Type
res_ty [GenLocated
   (SrcAnn NoEpAnns)
   (GRHS GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
alts') }
  where match_ctxt :: TcMatchCtxt HsExpr
match_ctxt = MC { mc_what :: HsMatchContext GhcTc
mc_what = forall p. HsMatchContext p
IfAlt, mc_body :: GenLocated SrcSpanAnnA (HsExpr GhcRn)
-> ExpRhoType -> TcM (GenLocated SrcSpanAnnA (HsExpr GhcTc))
mc_body = LHsExpr GhcRn -> ExpRhoType -> TcM (LHsExpr GhcTc)
tcBody }

tcExpr (HsDo XDo GhcRn
_ HsDoFlavour
do_or_lc XRec GhcRn [ExprLStmt GhcRn]
stmts) ExpRhoType
res_ty
  = HsDoFlavour
-> LocatedL [ExprLStmt GhcRn] -> ExpRhoType -> TcM (HsExpr GhcTc)
tcDoStmts HsDoFlavour
do_or_lc XRec GhcRn [ExprLStmt GhcRn]
stmts ExpRhoType
res_ty

tcExpr (HsProc XProc GhcRn
x LPat GhcRn
pat LHsCmdTop GhcRn
cmd) ExpRhoType
res_ty
  = do  { (GenLocated SrcSpanAnnA (Pat GhcTc)
pat', GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)
cmd', TcCoercionR
coi) <- LPat GhcRn
-> LHsCmdTop GhcRn
-> ExpRhoType
-> TcM (LPat GhcTc, LHsCmdTop GhcTc, TcCoercionR)
tcProc LPat GhcRn
pat LHsCmdTop GhcRn
cmd ExpRhoType
res_ty
        ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ TcCoercionR -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrapCo TcCoercionR
coi (forall p. XProc p -> LPat p -> LHsCmdTop p -> HsExpr p
HsProc XProc GhcRn
x GenLocated SrcSpanAnnA (Pat GhcTc)
pat' GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcTc)
cmd') }

-- Typechecks the static form and wraps it with a call to 'fromStaticPtr'.
-- See Note [Grand plan for static forms] in GHC.Iface.Tidy.StaticPtrTable for an overview.
-- To type check
--      (static e) :: p a
-- we want to check (e :: a),
-- and wrap (static e) in a call to
--    fromStaticPtr :: IsStatic p => StaticPtr a -> p a

tcExpr (HsStatic XStatic GhcRn
fvs LHsExpr GhcRn
expr) ExpRhoType
res_ty
  = do  { Type
res_ty          <- ExpRhoType -> TcM Type
expTypeToType ExpRhoType
res_ty
        ; (TcCoercionR
co, (Type
p_ty, Type
expr_ty)) <- Type -> TcM (TcCoercionR, (Type, Type))
matchExpectedAppTy Type
res_ty
        ; (GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr', WantedConstraints
lie)    <- forall a. TcM a -> TcM (a, WantedConstraints)
captureConstraints forall a b. (a -> b) -> a -> b
$
            forall a. SDoc -> TcM a -> TcM a
addErrCtxt (SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"In the body of a static form:")
                             Int
2 (forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcRn
expr)
                       ) forall a b. (a -> b) -> a -> b
$
            LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExprNC LHsExpr GhcRn
expr Type
expr_ty

        -- Check that the free variables of the static form are closed.
        -- It's OK to use nonDetEltsUniqSet here as the only side effects of
        -- checkClosedInStaticForm are error messages.
        ; forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Name -> TcRn ()
checkClosedInStaticForm forall a b. (a -> b) -> a -> b
$ forall elt. UniqSet elt -> [elt]
nonDetEltsUniqSet XStatic GhcRn
fvs

        -- Require the type of the argument to be Typeable.
        ; Class
typeableClass <- Name -> TcM Class
tcLookupClass Name
typeableClassName
        ; EvVar
typeable_ev <- CtOrigin -> Type -> TcM EvVar
emitWantedEvVar CtOrigin
StaticOrigin forall a b. (a -> b) -> a -> b
$
                  TyCon -> [Type] -> Type
mkTyConApp (Class -> TyCon
classTyCon Class
typeableClass)
                             [Type
liftedTypeKind, Type
expr_ty]

        -- Insert the constraints of the static form in a global list for later
        -- validation.
        ; WantedConstraints -> TcRn ()
emitStaticConstraints WantedConstraints
lie

        -- Wrap the static form with the 'fromStaticPtr' call.
        ; HsExpr GhcTc
fromStaticPtr <- CtOrigin -> Name -> [Type] -> TcM (HsExpr GhcTc)
newMethodFromName CtOrigin
StaticOrigin Name
fromStaticPtrName
                                             [Type
p_ty]
        ; let wrap :: HsWrapper
wrap = [EvVar] -> HsWrapper
mkWpEvVarApps [EvVar
typeable_ev] HsWrapper -> HsWrapper -> HsWrapper
<.> [Type] -> HsWrapper
mkWpTyApps [Type
expr_ty]
        ; SrcSpan
loc <- TcRn SrcSpan
getSrcSpanM
        ; TyCon
static_ptr_ty_con <- Name -> TcM TyCon
tcLookupTyCon Name
staticPtrTyConName
        ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ TcCoercionR -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrapCo TcCoercionR
co forall a b. (a -> b) -> a -> b
$ forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp EpAnnCO
noComments
                            (forall l e. l -> e -> GenLocated l e
L (forall ann. SrcSpan -> SrcAnn ann
noAnnSrcSpan SrcSpan
loc) forall a b. (a -> b) -> a -> b
$ HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
wrap HsExpr GhcTc
fromStaticPtr)
                            (forall l e. l -> e -> GenLocated l e
L (forall ann. SrcSpan -> SrcAnn ann
noAnnSrcSpan SrcSpan
loc) (forall p. XStatic p -> LHsExpr p -> HsExpr p
HsStatic (XStatic GhcRn
fvs, TyCon -> [Type] -> Type
mkTyConApp TyCon
static_ptr_ty_con [Type
expr_ty]) GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr'))
        }

{-
************************************************************************
*                                                                      *
                Record construction and update
*                                                                      *
************************************************************************
-}

tcExpr expr :: HsExpr GhcRn
expr@(RecordCon { rcon_con :: forall p. HsExpr p -> XRec p (ConLikeP p)
rcon_con = L SrcSpanAnnN
loc Name
con_name
                       , rcon_flds :: forall p. HsExpr p -> HsRecordBinds p
rcon_flds = HsRecordBinds GhcRn
rbinds }) ExpRhoType
res_ty
  = do  { ConLike
con_like <- Name -> TcM ConLike
tcLookupConLike Name
con_name

        ; (HsExpr GhcTc
con_expr, Type
con_sigma) <- Name -> TcM (HsExpr GhcTc, Type)
tcInferId Name
con_name
        ; (HsWrapper
con_wrap, Type
con_tau)   <- CtOrigin -> Type -> TcM (HsWrapper, Type)
topInstantiate CtOrigin
orig Type
con_sigma
              -- a shallow instantiation should really be enough for
              -- a data constructor.
        ; let arity :: Int
arity = ConLike -> Int
conLikeArity ConLike
con_like
              Right ([Scaled Type]
arg_tys, Type
actual_res_ty) = Int -> Type -> Either Int ([Scaled Type], Type)
tcSplitFunTysN Int
arity Type
con_tau

        ; Bool -> TcRnMessage -> TcRn ()
checkTc (ConLike -> Bool
conLikeHasBuilder ConLike
con_like) forall a b. (a -> b) -> a -> b
$
          forall name. Outputable name => name -> TcRnMessage
nonBidirectionalErr (ConLike -> Name
conLikeName ConLike
con_like)

        ; HsRecFields GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
rbinds' <- ConLike
-> [Type] -> HsRecordBinds GhcRn -> TcM (HsRecordBinds GhcTc)
tcRecordBinds ConLike
con_like (forall a b. (a -> b) -> [a] -> [b]
map forall a. Scaled a -> a
scaledThing [Scaled Type]
arg_tys) HsRecordBinds GhcRn
rbinds
                   -- It is currently not possible for a record to have
                   -- multiplicities. When they do, `tcRecordBinds` will take
                   -- scaled types instead. Meanwhile, it's safe to take
                   -- `scaledThing` above, as we know all the multiplicities are
                   -- Many.

        ; let rcon_tc :: HsExpr GhcTc
rcon_tc = HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
con_wrap HsExpr GhcTc
con_expr
              expr' :: HsExpr GhcTc
expr' = RecordCon { rcon_ext :: XRecordCon GhcTc
rcon_ext = HsExpr GhcTc
rcon_tc
                                , rcon_con :: XRec GhcTc (ConLikeP GhcTc)
rcon_con = forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
loc ConLike
con_like
                                , rcon_flds :: HsRecordBinds GhcTc
rcon_flds = HsRecFields GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
rbinds' }

        ; HsExpr GhcTc
ret <- HsExpr GhcRn
-> HsExpr GhcTc -> Type -> ExpRhoType -> TcM (HsExpr GhcTc)
tcWrapResultMono HsExpr GhcRn
expr HsExpr GhcTc
expr' Type
actual_res_ty ExpRhoType
res_ty

        -- Check for missing fields.  We do this after type-checking to get
        -- better types in error messages (cf #18869).  For example:
        --     data T a = MkT { x :: a, y :: a }
        --     r = MkT { y = True }
        -- Then we'd like to warn about a missing field `x :: True`, rather than `x :: a0`.
        --
        -- NB: to do this really properly we should delay reporting until typechecking is complete,
        -- via a new `HoleSort`.  But that seems too much work.
        ; ConLike -> HsRecordBinds GhcRn -> [Scaled Type] -> TcRn ()
checkMissingFields ConLike
con_like HsRecordBinds GhcRn
rbinds [Scaled Type]
arg_tys

        ; forall (m :: * -> *) a. Monad m => a -> m a
return HsExpr GhcTc
ret }
  where
    orig :: CtOrigin
orig = Name -> CtOrigin
OccurrenceOf Name
con_name

{-
Note [Type of a record update]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The main complication with RecordUpd is that we need to explicitly
handle the *non-updated* fields.  Consider:

        data T a b c = MkT1 { fa :: a, fb :: (b,c) }
                     | MkT2 { fa :: a, fb :: (b,c), fc :: c -> c }
                     | MkT3 { fd :: a }

        upd :: T a b c -> (b',c) -> T a b' c
        upd t x = t { fb = x}

The result type should be (T a b' c)
not (T a b c),   because 'b' *is not* mentioned in a non-updated field
not (T a b' c'), because 'c' *is*     mentioned in a non-updated field
NB that it's not good enough to look at just one constructor; we must
look at them all; cf #3219

After all, upd should be equivalent to:
        upd t x = case t of
                        MkT1 p q -> MkT1 p x
                        MkT2 a b -> MkT2 p b
                        MkT3 d   -> error ...

So we need to give a completely fresh type to the result record,
and then constrain it by the fields that are *not* updated ("p" above).
We call these the "fixed" type variables, and compute them in getFixedTyVars.

Note that because MkT3 doesn't contain all the fields being updated,
its RHS is simply an error, so it doesn't impose any type constraints.
Hence the use of 'relevant_cont'.

Note [Implicit type sharing]
~~~~~~~~~~~~~~~~~~~~~~~~~~~
We also take into account any "implicit" non-update fields.  For example
        data T a b where { MkT { f::a } :: T a a; ... }
So the "real" type of MkT is: forall ab. (a~b) => a -> T a b

Then consider
        upd t x = t { f=x }
We infer the type
        upd :: T a b -> a -> T a b
        upd (t::T a b) (x::a)
           = case t of { MkT (co:a~b) (_:a) -> MkT co x }
We can't give it the more general type
        upd :: T a b -> c -> T c b

Note [Criteria for update]
~~~~~~~~~~~~~~~~~~~~~~~~~~
We want to allow update for existentials etc, provided the updated
field isn't part of the existential. For example, this should be ok.
  data T a where { MkT { f1::a, f2::b->b } :: T a }
  f :: T a -> b -> T b
  f t b = t { f1=b }

The criterion we use is this:

  The types of the updated fields
  mention only the universally-quantified type variables
  of the data constructor

NB: this is not (quite) the same as being a "naughty" record selector
(See Note [Naughty record selectors]) in GHC.Tc.TyCl), at least
in the case of GADTs. Consider
   data T a where { MkT :: { f :: a } :: T [a] }
Then f is not "naughty" because it has a well-typed record selector.
But we don't allow updates for 'f'.  (One could consider trying to
allow this, but it makes my head hurt.  Badly.  And no one has asked
for it.)

In principle one could go further, and allow
  g :: T a -> T a
  g t = t { f2 = \x -> x }
because the expression is polymorphic...but that seems a bridge too far.

Note [Data family example]
~~~~~~~~~~~~~~~~~~~~~~~~~~
    data instance T (a,b) = MkT { x::a, y::b }
  --->
    data :TP a b = MkT { a::a, y::b }
    coTP a b :: T (a,b) ~ :TP a b

Suppose r :: T (t1,t2), e :: t3
Then  r { x=e } :: T (t3,t1)
  --->
      case r |> co1 of
        MkT x y -> MkT e y |> co2
      where co1 :: T (t1,t2) ~ :TP t1 t2
            co2 :: :TP t3 t2 ~ T (t3,t2)
The wrapping with co2 is done by the constructor wrapper for MkT

Outgoing invariants
~~~~~~~~~~~~~~~~~~~
In the outgoing (HsRecordUpd scrut binds cons in_inst_tys out_inst_tys):

  * cons are the data constructors to be updated

  * in_inst_tys, out_inst_tys have same length, and instantiate the
        *representation* tycon of the data cons.  In Note [Data
        family example], in_inst_tys = [t1,t2], out_inst_tys = [t3,t2]

Note [Mixed Record Field Updates]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider the following pattern synonym.

  data MyRec = MyRec { foo :: Int, qux :: String }

  pattern HisRec{f1, f2} = MyRec{foo = f1, qux=f2}

This allows updates such as the following

  updater :: MyRec -> MyRec
  updater a = a {f1 = 1 }

It would also make sense to allow the following update (which we reject).

  updater a = a {f1 = 1, qux = "two" } ==? MyRec 1 "two"

This leads to confusing behaviour when the selectors in fact refer the same
field.

  updater a = a {f1 = 1, foo = 2} ==? ???

For this reason, we reject a mixture of pattern synonym and normal record
selectors in the same update block. Although of course we still allow the
following.

  updater a = (a {f1 = 1}) {foo = 2}

  > updater (MyRec 0 "str")
  MyRec 2 "str"

-}

-- Record updates via dot syntax are replaced by desugared expressions
-- in the renamer. See Note [Overview of record dot syntax] in
-- GHC.Hs.Expr. This is why we match on 'rupd_flds = Left rbnds' here
-- and panic otherwise.
tcExpr expr :: HsExpr GhcRn
expr@(RecordUpd { rupd_expr :: forall p. HsExpr p -> LHsExpr p
rupd_expr = LHsExpr GhcRn
record_expr, rupd_flds :: forall p. HsExpr p -> Either [LHsRecUpdField p] [LHsRecUpdProj p]
rupd_flds = Left [LHsRecUpdField GhcRn]
rbnds }) ExpRhoType
res_ty
  = forall a. HasCallStack => Bool -> a -> a
assert (forall (f :: * -> *) a. Foldable f => f a -> Bool
notNull [LHsRecUpdField GhcRn]
rbnds) forall a b. (a -> b) -> a -> b
$
    do  { -- STEP -2: typecheck the record_expr, the record to be updated
          (GenLocated SrcSpanAnnA (HsExpr GhcTc)
record_expr', Type
record_rho) <- forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
Many forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> TcM (LHsExpr GhcTc, Type)
tcInferRho LHsExpr GhcRn
record_expr
            -- Record update drops some of the content of the record (namely the
            -- content of the field being updated). As a consequence, unless the
            -- field being updated is unrestricted in the record, or we need an
            -- unrestricted record. Currently, we simply always require an
            -- unrestricted record.
            --
            -- Consider the following example:
            --
            -- data R a = R { self :: a }
            -- bad :: a ⊸ ()
            -- bad x = let r = R x in case r { self = () } of { R x' -> x' }
            --
            -- This should definitely *not* typecheck.

        -- STEP -1  See Note [Disambiguating record fields] in GHC.Tc.Gen.Head
        -- After this we know that rbinds is unambiguous
        ; [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc))
      (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
rbinds <- LHsExpr GhcRn
-> Type
-> [LHsRecUpdField GhcRn]
-> ExpRhoType
-> TcM
     [LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
disambiguateRecordBinds LHsExpr GhcRn
record_expr Type
record_rho [LHsRecUpdField GhcRn]
rbnds ExpRhoType
res_ty
        ; let upd_flds :: [AmbiguousFieldOcc GhcTc]
upd_flds = forall a b. (a -> b) -> [a] -> [b]
map (forall l e. GenLocated l e -> e
unLoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc) [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc))
      (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
rbinds
              upd_fld_occs :: [FieldLabelString]
upd_fld_occs = forall a b. (a -> b) -> [a] -> [b]
map (OccName -> FieldLabelString
occNameFS forall b c a. (b -> c) -> (a -> b) -> a -> c
. RdrName -> OccName
rdrNameOcc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: Pass). AmbiguousFieldOcc (GhcPass p) -> RdrName
rdrNameAmbiguousFieldOcc) [AmbiguousFieldOcc GhcTc]
upd_flds
              sel_ids :: [EvVar]
sel_ids      = forall a b. (a -> b) -> [a] -> [b]
map AmbiguousFieldOcc GhcTc -> EvVar
selectorAmbiguousFieldOcc [AmbiguousFieldOcc GhcTc]
upd_flds
        -- STEP 0
        -- Check that the field names are really field names
        -- and they are all field names for proper records or
        -- all field names for pattern synonyms.
        ; let bad_guys :: [TcRn ()]
bad_guys = [ forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
loc forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
addErrTc (Name -> TcRnMessage
notSelector Name
fld_name)
                         | GenLocated
  SrcSpanAnnA
  (HsFieldBind
     (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc))
     (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
fld <- [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc))
      (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
rbinds,
                           -- Excludes class ops
                           let L SrcSpan
loc EvVar
sel_id = forall arg.
HsFieldBind (LAmbiguousFieldOcc GhcTc) arg
-> GenLocated SrcSpan EvVar
hsRecUpdFieldId (forall l e. GenLocated l e -> e
unLoc GenLocated
  SrcSpanAnnA
  (HsFieldBind
     (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc))
     (GenLocated SrcSpanAnnA (HsExpr GhcRn)))
fld),
                           Bool -> Bool
not (EvVar -> Bool
isRecordSelector EvVar
sel_id),
                           let fld_name :: Name
fld_name = EvVar -> Name
idName EvVar
sel_id ]
        ; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [TcRn ()]
bad_guys) (forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [TcRn ()]
bad_guys forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall env a. IOEnv env a
failM)
        -- See Note [Mixed Record Selectors]
        ; let ([EvVar]
data_sels, [EvVar]
pat_syn_sels) =
                forall a. (a -> Bool) -> [a] -> ([a], [a])
partition EvVar -> Bool
isDataConRecordSelector [EvVar]
sel_ids
        ; forall (m :: * -> *). (HasCallStack, Applicative m) => Bool -> m ()
massert (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all EvVar -> Bool
isPatSynRecordSelector [EvVar]
pat_syn_sels)
        ; Bool -> TcRnMessage -> TcRn ()
checkTc ( forall (t :: * -> *) a. Foldable t => t a -> Bool
null [EvVar]
data_sels Bool -> Bool -> Bool
|| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [EvVar]
pat_syn_sels )
                  ( [EvVar] -> [EvVar] -> TcRnMessage
mixedSelectors [EvVar]
data_sels [EvVar]
pat_syn_sels )

        -- STEP 1
        -- Figure out the tycon and data cons from the first field name
        ; let   -- It's OK to use the non-tc splitters here (for a selector)
              EvVar
sel_id : [EvVar]
_  = [EvVar]
sel_ids

              mtycon :: Maybe TyCon
              mtycon :: Maybe TyCon
mtycon = case EvVar -> IdDetails
idDetails EvVar
sel_id of
                          RecSelId (RecSelData TyCon
tycon) Bool
_ -> forall a. a -> Maybe a
Just TyCon
tycon
                          IdDetails
_ -> forall a. Maybe a
Nothing

              con_likes :: [ConLike]
              con_likes :: [ConLike]
con_likes = case EvVar -> IdDetails
idDetails EvVar
sel_id of
                             RecSelId (RecSelData TyCon
tc) Bool
_
                                -> forall a b. (a -> b) -> [a] -> [b]
map DataCon -> ConLike
RealDataCon (TyCon -> [DataCon]
tyConDataCons TyCon
tc)
                             RecSelId (RecSelPatSyn PatSyn
ps) Bool
_
                                -> [PatSyn -> ConLike
PatSynCon PatSyn
ps]
                             IdDetails
_  -> forall a. String -> a
panic String
"tcRecordUpd"
                -- NB: for a data type family, the tycon is the instance tycon

              relevant_cons :: [ConLike]
relevant_cons = [ConLike] -> [FieldLabelString] -> [ConLike]
conLikesWithFields [ConLike]
con_likes [FieldLabelString]
upd_fld_occs
                -- A constructor is only relevant to this process if
                -- it contains *all* the fields that are being updated
                -- Other ones will cause a runtime error if they occur

        -- Step 2
        -- Check that at least one constructor has all the named fields
        -- i.e. has an empty set of bad fields returned by badFields
        ; Bool -> TcRnMessage -> TcRn ()
checkTc (Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ConLike]
relevant_cons)) ([LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
-> [ConLike] -> TcRnMessage
badFieldsUpd [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc))
      (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
rbinds [ConLike]
con_likes)

        -- Take apart a representative constructor
        ; let con1 :: ConLike
con1 = forall a. HasCallStack => Bool -> a -> a
assert (Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ConLike]
relevant_cons) ) forall a. [a] -> a
head [ConLike]
relevant_cons
              ([EvVar]
con1_tvs, [EvVar]
_, [EqSpec]
_, [Type]
_prov_theta, [Type]
req_theta, [Scaled Type]
scaled_con1_arg_tys, Type
_)
                 = ConLike
-> ([EvVar], [EvVar], [EqSpec], [Type], [Type], [Scaled Type],
    Type)
conLikeFullSig ConLike
con1
              con1_arg_tys :: [Type]
con1_arg_tys = forall a b. (a -> b) -> [a] -> [b]
map forall a. Scaled a -> a
scaledThing [Scaled Type]
scaled_con1_arg_tys
                -- We can safely drop the fields' multiplicities because
                -- they are currently always 1: there is no syntax for record
                -- fields with other multiplicities yet. This way we don't need
                -- to handle it in the rest of the function
              con1_flds :: [FieldLabelString]
con1_flds   = forall a b. (a -> b) -> [a] -> [b]
map FieldLabel -> FieldLabelString
flLabel forall a b. (a -> b) -> a -> b
$ ConLike -> [FieldLabel]
conLikeFieldLabels ConLike
con1
              con1_tv_tys :: [Type]
con1_tv_tys = [EvVar] -> [Type]
mkTyVarTys [EvVar]
con1_tvs
              con1_res_ty :: Type
con1_res_ty = case Maybe TyCon
mtycon of
                              Just TyCon
tc -> TyCon -> [Type] -> Type
mkFamilyTyConApp TyCon
tc [Type]
con1_tv_tys
                              Maybe TyCon
Nothing -> ConLike -> [Type] -> Type
conLikeResTy ConLike
con1 [Type]
con1_tv_tys

        -- Check that we're not dealing with a unidirectional pattern
        -- synonym
        ; Bool -> TcRnMessage -> TcRn ()
checkTc (ConLike -> Bool
conLikeHasBuilder ConLike
con1) forall a b. (a -> b) -> a -> b
$
          forall name. Outputable name => name -> TcRnMessage
nonBidirectionalErr (ConLike -> Name
conLikeName ConLike
con1)

        -- STEP 3    Note [Criteria for update]
        -- Check that each updated field is polymorphic; that is, its type
        -- mentions only the universally-quantified variables of the data con
        ; let flds1_w_tys :: [(FieldLabelString, Type)]
flds1_w_tys  = forall a b. String -> [a] -> [b] -> [(a, b)]
zipEqual String
"tcExpr:RecConUpd" [FieldLabelString]
con1_flds [Type]
con1_arg_tys
              bad_upd_flds :: [(FieldLabelString, Type)]
bad_upd_flds = forall a. (a -> Bool) -> [a] -> [a]
filter (FieldLabelString, Type) -> Bool
bad_fld [(FieldLabelString, Type)]
flds1_w_tys
              con1_tv_set :: VarSet
con1_tv_set  = [EvVar] -> VarSet
mkVarSet [EvVar]
con1_tvs
              bad_fld :: (FieldLabelString, Type) -> Bool
bad_fld (FieldLabelString
fld, Type
ty) = FieldLabelString
fld forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FieldLabelString]
upd_fld_occs Bool -> Bool -> Bool
&&
                                      Bool -> Bool
not (Type -> VarSet
tyCoVarsOfType Type
ty VarSet -> VarSet -> Bool
`subVarSet` VarSet
con1_tv_set)
        ; Bool -> TcRnMessage -> TcRn ()
checkTc (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(FieldLabelString, Type)]
bad_upd_flds) ([(FieldLabelString, Type)] -> TcRnMessage
TcRnFieldUpdateInvalidType [(FieldLabelString, Type)]
bad_upd_flds)

        -- STEP 4  Note [Type of a record update]
        -- Figure out types for the scrutinee and result
        -- Both are of form (T a b c), with fresh type variables, but with
        -- common variables where the scrutinee and result must have the same type
        -- These are variables that appear in *any* arg of *any* of the
        -- relevant constructors *except* in the updated fields
        --
        ; let fixed_tvs :: VarSet
fixed_tvs = [FieldLabelString] -> [EvVar] -> [ConLike] -> VarSet
getFixedTyVars [FieldLabelString]
upd_fld_occs [EvVar]
con1_tvs [ConLike]
relevant_cons
              is_fixed_tv :: EvVar -> Bool
is_fixed_tv EvVar
tv = EvVar
tv EvVar -> VarSet -> Bool
`elemVarSet` VarSet
fixed_tvs

              mk_inst_ty :: TCvSubst -> (TyVar, TcType) -> TcM (TCvSubst, TcType)
              -- Deals with instantiation of kind variables
              --   c.f. GHC.Tc.Utils.TcMType.newMetaTyVars
              mk_inst_ty :: TCvSubst -> (EvVar, Type) -> TcM (TCvSubst, Type)
mk_inst_ty TCvSubst
subst (EvVar
tv, Type
result_inst_ty)
                | EvVar -> Bool
is_fixed_tv EvVar
tv   -- Same as result type
                = forall (m :: * -> *) a. Monad m => a -> m a
return (TCvSubst -> EvVar -> Type -> TCvSubst
extendTvSubst TCvSubst
subst EvVar
tv Type
result_inst_ty, Type
result_inst_ty)
                | Bool
otherwise        -- Fresh type, of correct kind
                = do { (TCvSubst
subst', EvVar
new_tv) <- TCvSubst -> EvVar -> TcM (TCvSubst, EvVar)
newMetaTyVarX TCvSubst
subst EvVar
tv
                     ; forall (m :: * -> *) a. Monad m => a -> m a
return (TCvSubst
subst', EvVar -> Type
mkTyVarTy EvVar
new_tv) }

        ; (TCvSubst
result_subst, [EvVar]
con1_tvs') <- [EvVar] -> TcM (TCvSubst, [EvVar])
newMetaTyVars [EvVar]
con1_tvs
        ; let result_inst_tys :: [Type]
result_inst_tys = [EvVar] -> [Type]
mkTyVarTys [EvVar]
con1_tvs'
              init_subst :: TCvSubst
init_subst = InScopeSet -> TCvSubst
mkEmptyTCvSubst (TCvSubst -> InScopeSet
getTCvInScope TCvSubst
result_subst)

        ; (TCvSubst
scrut_subst, [Type]
scrut_inst_tys) <- forall (m :: * -> *) acc x y.
Monad m =>
(acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y])
mapAccumLM TCvSubst -> (EvVar, Type) -> TcM (TCvSubst, Type)
mk_inst_ty TCvSubst
init_subst
                                                      ([EvVar]
con1_tvs forall a b. [a] -> [b] -> [(a, b)]
`zip` [Type]
result_inst_tys)

        ; let rec_res_ty :: Type
rec_res_ty    = HasDebugCallStack => TCvSubst -> Type -> Type
TcType.substTy TCvSubst
result_subst Type
con1_res_ty
              scrut_ty :: Type
scrut_ty      = HasDebugCallStack => TCvSubst -> Type -> Type
TcType.substTy TCvSubst
scrut_subst  Type
con1_res_ty
              con1_arg_tys' :: [Type]
con1_arg_tys' = forall a b. (a -> b) -> [a] -> [b]
map (HasDebugCallStack => TCvSubst -> Type -> Type
TcType.substTy TCvSubst
result_subst) [Type]
con1_arg_tys

        ; TcCoercionR
co_scrut <- Maybe TypedThing -> Type -> Type -> TcM TcCoercionR
unifyType (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsExpr GhcRn -> TypedThing
HsExprRnThing forall a b. (a -> b) -> a -> b
$ forall l e. GenLocated l e -> e
unLoc LHsExpr GhcRn
record_expr) Type
record_rho Type
scrut_ty
                -- NB: normal unification is OK here (as opposed to subsumption),
                -- because for this to work out, both record_rho and scrut_ty have
                -- to be normal datatypes -- no contravariant stuff can go on

        -- STEP 5
        -- Typecheck the bindings
        ; [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc))
      (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
rbinds'      <- ConLike
-> [Type]
-> [LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
-> TcM [LHsRecUpdField GhcTc]
tcRecordUpd ConLike
con1 [Type]
con1_arg_tys' [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc))
      (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
rbinds

        -- STEP 6: Deal with the stupid theta.
        -- See Note [The stupid context] in GHC.Core.DataCon.
        ; let theta' :: [Type]
theta' = TCvSubst -> [Type] -> [Type]
substThetaUnchecked TCvSubst
scrut_subst (ConLike -> [Type]
conLikeStupidTheta ConLike
con1)
        ; CtOrigin -> [Type] -> TcRn ()
instStupidTheta CtOrigin
RecordUpdOrigin [Type]
theta'

        -- Step 7: make a cast for the scrutinee, in the
        --         case that it's from a data family
        ; let fam_co :: HsWrapper   -- RepT t1 .. tn ~R scrut_ty
              fam_co :: HsWrapper
fam_co | Just TyCon
tycon <- Maybe TyCon
mtycon
                     , Just CoAxiom Unbranched
co_con <- TyCon -> Maybe (CoAxiom Unbranched)
tyConFamilyCoercion_maybe TyCon
tycon
                     = TcCoercionR -> HsWrapper
mkWpCastR (CoAxiom Unbranched -> [Type] -> [TcCoercionR] -> TcCoercionR
mkTcUnbranchedAxInstCo CoAxiom Unbranched
co_con [Type]
scrut_inst_tys [])
                     | Bool
otherwise
                     = HsWrapper
idHsWrapper

        -- Step 8: Check that the req constraints are satisfied
        -- For normal data constructors req_theta is empty but we must do
        -- this check for pattern synonyms.
        ; let req_theta' :: [Type]
req_theta' = TCvSubst -> [Type] -> [Type]
substThetaUnchecked TCvSubst
scrut_subst [Type]
req_theta
        ; HsWrapper
req_wrap <- CtOrigin -> [Type] -> TcM HsWrapper
instCallConstraints CtOrigin
RecordUpdOrigin [Type]
req_theta'

        -- Phew!
        ; let upd_tc :: RecordUpdTc
upd_tc = RecordUpdTc { rupd_cons :: [ConLike]
rupd_cons = [ConLike]
relevant_cons
                                   , rupd_in_tys :: [Type]
rupd_in_tys = [Type]
scrut_inst_tys
                                   , rupd_out_tys :: [Type]
rupd_out_tys = [Type]
result_inst_tys
                                   , rupd_wrap :: HsWrapper
rupd_wrap = HsWrapper
req_wrap }
              expr' :: HsExpr GhcTc
expr' = RecordUpd { rupd_expr :: LHsExpr GhcTc
rupd_expr = HsWrapper -> LHsExpr GhcTc -> LHsExpr GhcTc
mkLHsWrap HsWrapper
fam_co forall a b. (a -> b) -> a -> b
$
                                                TcCoercionR -> LHsExpr GhcTc -> LHsExpr GhcTc
mkLHsWrapCo TcCoercionR
co_scrut GenLocated SrcSpanAnnA (HsExpr GhcTc)
record_expr'
                                , rupd_flds :: Either [LHsRecUpdField GhcTc] [LHsRecUpdProj GhcTc]
rupd_flds = forall a b. a -> Either a b
Left [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc))
      (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
rbinds'
                                , rupd_ext :: XRecordUpd GhcTc
rupd_ext = RecordUpdTc
upd_tc }

        ; HsExpr GhcRn
-> HsExpr GhcTc -> Type -> ExpRhoType -> TcM (HsExpr GhcTc)
tcWrapResult HsExpr GhcRn
expr HsExpr GhcTc
expr' Type
rec_res_ty ExpRhoType
res_ty }
tcExpr (RecordUpd {}) ExpRhoType
_ = forall a. String -> a
panic String
"GHC.Tc.Gen.Expr: tcExpr: The impossible happened!"


{-
************************************************************************
*                                                                      *
        Arithmetic sequences                    e.g. [a,b..]
        and their parallel-array counterparts   e.g. [: a,b.. :]

*                                                                      *
************************************************************************
-}

tcExpr (ArithSeq XArithSeq GhcRn
_ Maybe (SyntaxExpr GhcRn)
witness ArithSeqInfo GhcRn
seq) ExpRhoType
res_ty
  = Maybe (SyntaxExpr GhcRn)
-> ArithSeqInfo GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcArithSeq Maybe (SyntaxExpr GhcRn)
witness ArithSeqInfo GhcRn
seq ExpRhoType
res_ty

{-
************************************************************************
*                                                                      *
                Record dot syntax
*                                                                      *
************************************************************************
-}

-- These terms have been replaced by desugaring in the renamer. See
-- Note [Overview of record dot syntax].
tcExpr (HsGetField XGetField GhcRn
_ LHsExpr GhcRn
_ XRec GhcRn (DotFieldOcc GhcRn)
_) ExpRhoType
_ = forall a. String -> a
panic String
"GHC.Tc.Gen.Expr: tcExpr: HsGetField: Not implemented"
tcExpr (HsProjection XProjection GhcRn
_ NonEmpty (XRec GhcRn (DotFieldOcc GhcRn))
_) ExpRhoType
_ = forall a. String -> a
panic String
"GHC.Tc.Gen.Expr: tcExpr: HsProjection: Not implemented"

{-
************************************************************************
*                                                                      *
                Template Haskell
*                                                                      *
************************************************************************
-}

-- HsSpliced is an annotation produced by 'GHC.Rename.Splice.rnSpliceExpr'.
-- Here we get rid of it and add the finalizers to the global environment.
--
-- See Note [Delaying modFinalizers in untyped splices] in GHC.Rename.Splice.
tcExpr (HsSpliceE XSpliceE GhcRn
_ (HsSpliced XSpliced GhcRn
_ ThModFinalizers
mod_finalizers (HsSplicedExpr HsExpr GhcRn
expr)))
       ExpRhoType
res_ty
  = do ThModFinalizers -> TcRn ()
addModFinalizersWithLclEnv ThModFinalizers
mod_finalizers
       HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcExpr HsExpr GhcRn
expr ExpRhoType
res_ty
tcExpr (HsSpliceE XSpliceE GhcRn
_ HsSplice GhcRn
splice)          ExpRhoType
res_ty = HsSplice GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcSpliceExpr HsSplice GhcRn
splice ExpRhoType
res_ty
tcExpr e :: HsExpr GhcRn
e@(HsTypedBracket XTypedBracket GhcRn
_ LHsExpr GhcRn
body) ExpRhoType
res_ty = HsExpr GhcRn -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcTypedBracket HsExpr GhcRn
e LHsExpr GhcRn
body ExpRhoType
res_ty
tcExpr e :: HsExpr GhcRn
e@(HsUntypedBracket XUntypedBracket GhcRn
ps HsQuote GhcRn
body) ExpRhoType
res_ty = HsExpr GhcRn
-> HsQuote GhcRn
-> [PendingRnSplice]
-> ExpRhoType
-> TcM (HsExpr GhcTc)
tcUntypedBracket HsExpr GhcRn
e HsQuote GhcRn
body XUntypedBracket GhcRn
ps ExpRhoType
res_ty

{-
************************************************************************
*                                                                      *
                Catch-all
*                                                                      *
************************************************************************
-}

tcExpr (HsOverLabel {})    ExpRhoType
ty = forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tcExpr:HsOverLabel"  (forall a. Outputable a => a -> SDoc
ppr ExpRhoType
ty)
tcExpr (SectionL {})       ExpRhoType
ty = forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tcExpr:SectionL"    (forall a. Outputable a => a -> SDoc
ppr ExpRhoType
ty)
tcExpr (SectionR {})       ExpRhoType
ty = forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tcExpr:SectionR"    (forall a. Outputable a => a -> SDoc
ppr ExpRhoType
ty)


{-
************************************************************************
*                                                                      *
                Arithmetic sequences [a..b] etc
*                                                                      *
************************************************************************
-}

tcArithSeq :: Maybe (SyntaxExpr GhcRn) -> ArithSeqInfo GhcRn -> ExpRhoType
           -> TcM (HsExpr GhcTc)

tcArithSeq :: Maybe (SyntaxExpr GhcRn)
-> ArithSeqInfo GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
tcArithSeq Maybe (SyntaxExpr GhcRn)
witness seq :: ArithSeqInfo GhcRn
seq@(From LHsExpr GhcRn
expr) ExpRhoType
res_ty
  = do { (HsWrapper
wrap, Type
elt_mult, Type
elt_ty, Maybe SyntaxExprTc
wit') <- Maybe (SyntaxExpr GhcRn)
-> ExpRhoType
-> TcM (HsWrapper, Type, Type, Maybe (SyntaxExpr GhcTc))
arithSeqEltType Maybe (SyntaxExpr GhcRn)
witness ExpRhoType
res_ty
       ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' <-forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
elt_mult forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr Type
elt_ty
       ; HsExpr GhcTc
enum_from <- CtOrigin -> Name -> [Type] -> TcM (HsExpr GhcTc)
newMethodFromName (ArithSeqInfo GhcRn -> CtOrigin
ArithSeqOrigin ArithSeqInfo GhcRn
seq)
                              Name
enumFromName [Type
elt_ty]
       ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
wrap forall a b. (a -> b) -> a -> b
$
         forall p.
XArithSeq p -> Maybe (SyntaxExpr p) -> ArithSeqInfo p -> HsExpr p
ArithSeq HsExpr GhcTc
enum_from Maybe SyntaxExprTc
wit' (forall id. LHsExpr id -> ArithSeqInfo id
From GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr') }

tcArithSeq Maybe (SyntaxExpr GhcRn)
witness seq :: ArithSeqInfo GhcRn
seq@(FromThen LHsExpr GhcRn
expr1 LHsExpr GhcRn
expr2) ExpRhoType
res_ty
  = do { (HsWrapper
wrap, Type
elt_mult, Type
elt_ty, Maybe SyntaxExprTc
wit') <- Maybe (SyntaxExpr GhcRn)
-> ExpRhoType
-> TcM (HsWrapper, Type, Type, Maybe (SyntaxExpr GhcTc))
arithSeqEltType Maybe (SyntaxExpr GhcRn)
witness ExpRhoType
res_ty
       ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr1' <- forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
elt_mult forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr1 Type
elt_ty
       ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr2' <- forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
elt_mult forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr2 Type
elt_ty
       ; HsExpr GhcTc
enum_from_then <- CtOrigin -> Name -> [Type] -> TcM (HsExpr GhcTc)
newMethodFromName (ArithSeqInfo GhcRn -> CtOrigin
ArithSeqOrigin ArithSeqInfo GhcRn
seq)
                              Name
enumFromThenName [Type
elt_ty]
       ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
wrap forall a b. (a -> b) -> a -> b
$
         forall p.
XArithSeq p -> Maybe (SyntaxExpr p) -> ArithSeqInfo p -> HsExpr p
ArithSeq HsExpr GhcTc
enum_from_then Maybe SyntaxExprTc
wit' (forall id. LHsExpr id -> LHsExpr id -> ArithSeqInfo id
FromThen GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr1' GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr2') }

tcArithSeq Maybe (SyntaxExpr GhcRn)
witness seq :: ArithSeqInfo GhcRn
seq@(FromTo LHsExpr GhcRn
expr1 LHsExpr GhcRn
expr2) ExpRhoType
res_ty
  = do { (HsWrapper
wrap, Type
elt_mult, Type
elt_ty, Maybe SyntaxExprTc
wit') <- Maybe (SyntaxExpr GhcRn)
-> ExpRhoType
-> TcM (HsWrapper, Type, Type, Maybe (SyntaxExpr GhcTc))
arithSeqEltType Maybe (SyntaxExpr GhcRn)
witness ExpRhoType
res_ty
       ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr1' <- forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
elt_mult forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr1 Type
elt_ty
       ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr2' <- forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
elt_mult forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr2 Type
elt_ty
       ; HsExpr GhcTc
enum_from_to <- CtOrigin -> Name -> [Type] -> TcM (HsExpr GhcTc)
newMethodFromName (ArithSeqInfo GhcRn -> CtOrigin
ArithSeqOrigin ArithSeqInfo GhcRn
seq)
                              Name
enumFromToName [Type
elt_ty]
       ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
wrap forall a b. (a -> b) -> a -> b
$
         forall p.
XArithSeq p -> Maybe (SyntaxExpr p) -> ArithSeqInfo p -> HsExpr p
ArithSeq HsExpr GhcTc
enum_from_to Maybe SyntaxExprTc
wit' (forall id. LHsExpr id -> LHsExpr id -> ArithSeqInfo id
FromTo GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr1' GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr2') }

tcArithSeq Maybe (SyntaxExpr GhcRn)
witness seq :: ArithSeqInfo GhcRn
seq@(FromThenTo LHsExpr GhcRn
expr1 LHsExpr GhcRn
expr2 LHsExpr GhcRn
expr3) ExpRhoType
res_ty
  = do { (HsWrapper
wrap, Type
elt_mult, Type
elt_ty, Maybe SyntaxExprTc
wit') <- Maybe (SyntaxExpr GhcRn)
-> ExpRhoType
-> TcM (HsWrapper, Type, Type, Maybe (SyntaxExpr GhcTc))
arithSeqEltType Maybe (SyntaxExpr GhcRn)
witness ExpRhoType
res_ty
        ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr1' <- forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
elt_mult forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr1 Type
elt_ty
        ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr2' <- forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
elt_mult forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr2 Type
elt_ty
        ; GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr3' <- forall a. Type -> TcM a -> TcM a
tcScalingUsage Type
elt_mult forall a b. (a -> b) -> a -> b
$ LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr3 Type
elt_ty
        ; HsExpr GhcTc
eft <- CtOrigin -> Name -> [Type] -> TcM (HsExpr GhcTc)
newMethodFromName (ArithSeqInfo GhcRn -> CtOrigin
ArithSeqOrigin ArithSeqInfo GhcRn
seq)
                              Name
enumFromThenToName [Type
elt_ty]
        ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
wrap forall a b. (a -> b) -> a -> b
$
          forall p.
XArithSeq p -> Maybe (SyntaxExpr p) -> ArithSeqInfo p -> HsExpr p
ArithSeq HsExpr GhcTc
eft Maybe SyntaxExprTc
wit' (forall id.
LHsExpr id -> LHsExpr id -> LHsExpr id -> ArithSeqInfo id
FromThenTo GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr1' GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr2' GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr3') }

-----------------
arithSeqEltType :: Maybe (SyntaxExpr GhcRn) -> ExpRhoType
                -> TcM (HsWrapper, Mult, TcType, Maybe (SyntaxExpr GhcTc))
arithSeqEltType :: Maybe (SyntaxExpr GhcRn)
-> ExpRhoType
-> TcM (HsWrapper, Type, Type, Maybe (SyntaxExpr GhcTc))
arithSeqEltType Maybe (SyntaxExpr GhcRn)
Nothing ExpRhoType
res_ty
  = do { Type
res_ty <- ExpRhoType -> TcM Type
expTypeToType ExpRhoType
res_ty
       ; (TcCoercionR
coi, Type
elt_ty) <- Type -> TcM (TcCoercionR, Type)
matchExpectedListTy Type
res_ty
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (TcCoercionR -> HsWrapper
mkWpCastN TcCoercionR
coi, Type
One, Type
elt_ty, forall a. Maybe a
Nothing) }
arithSeqEltType (Just SyntaxExpr GhcRn
fl) ExpRhoType
res_ty
  = do { ((Type
elt_mult, Type
elt_ty), SyntaxExprTc
fl')
           <- forall a.
CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> ExpRhoType
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, SyntaxExprTc)
tcSyntaxOp CtOrigin
ListOrigin SyntaxExpr GhcRn
fl [SyntaxOpType
SynList] ExpRhoType
res_ty forall a b. (a -> b) -> a -> b
$
              \ [Type
elt_ty] [Type
elt_mult] -> forall (m :: * -> *) a. Monad m => a -> m a
return (Type
elt_mult, Type
elt_ty)
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper
idHsWrapper, Type
elt_mult, Type
elt_ty, forall a. a -> Maybe a
Just SyntaxExprTc
fl') }

----------------
tcTupArgs :: [HsTupArg GhcRn]
          -> [TcSigmaType]
              -- ^ Argument types.
              -- This function ensures they all have
              -- a fixed runtime representation.
          -> TcM [HsTupArg GhcTc]
tcTupArgs :: [HsTupArg GhcRn] -> [Type] -> TcM [HsTupArg GhcTc]
tcTupArgs [HsTupArg GhcRn]
args [Type]
tys
  = do forall (m :: * -> *). (HasCallStack, Applicative m) => Bool -> m ()
massert (forall a b. [a] -> [b] -> Bool
equalLength [HsTupArg GhcRn]
args [Type]
tys)
       Int -> TcRn ()
checkTupSize (forall (t :: * -> *) a. Foldable t => t a -> Int
length [HsTupArg GhcRn]
args)
       forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d]
zipWith3M Int -> HsTupArg GhcRn -> Type -> TcM (HsTupArg GhcTc)
go [Int
1,Int
2..] [HsTupArg GhcRn]
args [Type]
tys
  where
    go :: Int -> HsTupArg GhcRn -> TcType -> TcM (HsTupArg GhcTc)
    go :: Int -> HsTupArg GhcRn -> Type -> TcM (HsTupArg GhcTc)
go Int
i (Missing {})     Type
arg_ty
      = do { Type
mult <- Type -> TcM Type
newFlexiTyVarTy Type
multiplicityTy
           ; HasDebugCallStack => FixedRuntimeRepContext -> Type -> TcRn ()
hasFixedRuntimeRep_syntactic (Int -> FixedRuntimeRepContext
FRRTupleSection Int
i) Type
arg_ty
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall id. XMissing id -> HsTupArg id
Missing (forall a. Type -> a -> Scaled a
Scaled Type
mult Type
arg_ty)) }
    go Int
i (Present XPresent GhcRn
x LHsExpr GhcRn
expr) Type
arg_ty
      = do { GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr' <- LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExpr LHsExpr GhcRn
expr Type
arg_ty
           ; HasDebugCallStack => FixedRuntimeRepContext -> Type -> TcRn ()
hasFixedRuntimeRep_syntactic (Int -> FixedRuntimeRepContext
FRRTupleArg Int
i) Type
arg_ty
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall id. XPresent id -> LHsExpr id -> HsTupArg id
Present XPresent GhcRn
x GenLocated SrcSpanAnnA (HsExpr GhcTc)
expr') }

---------------------------
-- See TcType.SyntaxOpType also for commentary
tcSyntaxOp :: CtOrigin
           -> SyntaxExprRn
           -> [SyntaxOpType]           -- ^ shape of syntax operator arguments
           -> ExpRhoType               -- ^ overall result type
           -> ([TcSigmaType] -> [Mult] -> TcM a) -- ^ Type check any arguments,
                                                 -- takes a type per hole and a
                                                 -- multiplicity per arrow in
                                                 -- the shape.
           -> TcM (a, SyntaxExprTc)
-- ^ Typecheck a syntax operator
-- The operator is a variable or a lambda at this stage (i.e. renamer
-- output)t
tcSyntaxOp :: forall a.
CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> ExpRhoType
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, SyntaxExprTc)
tcSyntaxOp CtOrigin
orig SyntaxExprRn
expr [SyntaxOpType]
arg_tys ExpRhoType
res_ty
  = forall a.
CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> SyntaxOpType
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, SyntaxExprTc)
tcSyntaxOpGen CtOrigin
orig SyntaxExprRn
expr [SyntaxOpType]
arg_tys (ExpRhoType -> SyntaxOpType
SynType ExpRhoType
res_ty)

-- | Slightly more general version of 'tcSyntaxOp' that allows the caller
-- to specify the shape of the result of the syntax operator
tcSyntaxOpGen :: CtOrigin
              -> SyntaxExprRn
              -> [SyntaxOpType]
              -> SyntaxOpType
              -> ([TcSigmaTypeFRR] -> [Mult] -> TcM a)
              -> TcM (a, SyntaxExprTc)
tcSyntaxOpGen :: forall a.
CtOrigin
-> SyntaxExprRn
-> [SyntaxOpType]
-> SyntaxOpType
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, SyntaxExprTc)
tcSyntaxOpGen CtOrigin
orig (SyntaxExprRn HsExpr GhcRn
op) [SyntaxOpType]
arg_tys SyntaxOpType
res_ty [Type] -> [Type] -> TcM a
thing_inside
  = do { (HsExpr GhcTc
expr, Type
sigma) <- (HsExpr GhcRn, AppCtxt)
-> [HsExprArg 'TcpRn] -> TcM (HsExpr GhcTc, Type)
tcInferAppHead (HsExpr GhcRn
op, HsExpr GhcRn -> Int -> SrcSpan -> AppCtxt
VACall HsExpr GhcRn
op Int
0 SrcSpan
noSrcSpan) []
             -- Ugh!! But all this code is scheduled for demolition anyway
       ; String -> SDoc -> TcRn ()
traceTc String
"tcSyntaxOpGen" (forall a. Outputable a => a -> SDoc
ppr HsExpr GhcRn
op SDoc -> SDoc -> SDoc
$$ forall a. Outputable a => a -> SDoc
ppr HsExpr GhcTc
expr SDoc -> SDoc -> SDoc
$$ forall a. Outputable a => a -> SDoc
ppr Type
sigma)
       ; (a
result, HsWrapper
expr_wrap, [HsWrapper]
arg_wraps, HsWrapper
res_wrap)
           <- forall a.
CtOrigin
-> HsExpr GhcRn
-> Type
-> [SyntaxOpType]
-> SyntaxOpType
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, HsWrapper, [HsWrapper], HsWrapper)
tcSynArgA CtOrigin
orig HsExpr GhcRn
op Type
sigma [SyntaxOpType]
arg_tys SyntaxOpType
res_ty forall a b. (a -> b) -> a -> b
$
              [Type] -> [Type] -> TcM a
thing_inside
       ; String -> SDoc -> TcRn ()
traceTc String
"tcSyntaxOpGen" (forall a. Outputable a => a -> SDoc
ppr HsExpr GhcRn
op SDoc -> SDoc -> SDoc
$$ forall a. Outputable a => a -> SDoc
ppr HsExpr GhcTc
expr SDoc -> SDoc -> SDoc
$$ forall a. Outputable a => a -> SDoc
ppr Type
sigma )
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, SyntaxExprTc { syn_expr :: HsExpr GhcTc
syn_expr = HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
expr_wrap HsExpr GhcTc
expr
                                      , syn_arg_wraps :: [HsWrapper]
syn_arg_wraps = [HsWrapper]
arg_wraps
                                      , syn_res_wrap :: HsWrapper
syn_res_wrap  = HsWrapper
res_wrap }) }
tcSyntaxOpGen CtOrigin
_ SyntaxExprRn
NoSyntaxExprRn [SyntaxOpType]
_ SyntaxOpType
_ [Type] -> [Type] -> TcM a
_ = forall a. String -> a
panic String
"tcSyntaxOpGen"

{-
Note [tcSynArg]
~~~~~~~~~~~~~~~
Because of the rich structure of SyntaxOpType, we must do the
contra-/covariant thing when working down arrows, to get the
instantiation vs. skolemisation decisions correct (and, more
obviously, the orientation of the HsWrappers). We thus have
two tcSynArgs.
-}

-- works on "expected" types, skolemising where necessary
-- See Note [tcSynArg]
tcSynArgE :: CtOrigin
          -> HsExpr GhcRn -- ^ the operator to check (for error messages only)
          -> TcSigmaType
          -> SyntaxOpType                -- ^ shape it is expected to have
          -> ([TcSigmaTypeFRR] -> [Mult] -> TcM a) -- ^ check the arguments
          -> TcM (a, HsWrapper)
           -- ^ returns a wrapper :: (type of right shape) "->" (type passed in)
tcSynArgE :: forall a.
CtOrigin
-> HsExpr GhcRn
-> Type
-> SyntaxOpType
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, HsWrapper)
tcSynArgE CtOrigin
orig HsExpr GhcRn
op Type
sigma_ty SyntaxOpType
syn_ty [Type] -> [Type] -> TcM a
thing_inside
  = do { (HsWrapper
skol_wrap, (a
result, HsWrapper
ty_wrapper))
           <- forall result.
UserTypeCtxt
-> Type -> (Type -> TcM result) -> TcM (HsWrapper, result)
tcTopSkolemise UserTypeCtxt
GenSigCtxt Type
sigma_ty
                (\ Type
rho_ty -> Type -> SyntaxOpType -> TcM (a, HsWrapper)
go Type
rho_ty SyntaxOpType
syn_ty)
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, HsWrapper
skol_wrap HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
ty_wrapper) }
    where
    go :: Type -> SyntaxOpType -> TcM (a, HsWrapper)
go Type
rho_ty SyntaxOpType
SynAny
      = do { a
result <- [Type] -> [Type] -> TcM a
thing_inside [Type
rho_ty] []
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, HsWrapper
idHsWrapper) }

    go Type
rho_ty SyntaxOpType
SynRho   -- same as SynAny, because we skolemise eagerly
      = do { a
result <- [Type] -> [Type] -> TcM a
thing_inside [Type
rho_ty] []
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, HsWrapper
idHsWrapper) }

    go Type
rho_ty SyntaxOpType
SynList
      = do { (TcCoercionR
list_co, Type
elt_ty) <- Type -> TcM (TcCoercionR, Type)
matchExpectedListTy Type
rho_ty
           ; a
result <- [Type] -> [Type] -> TcM a
thing_inside [Type
elt_ty] []
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, TcCoercionR -> HsWrapper
mkWpCastN TcCoercionR
list_co) }

    go Type
rho_ty (SynFun SyntaxOpType
arg_shape SyntaxOpType
res_shape)
      = do { ( HsWrapper
match_wrapper                         -- :: (arg_ty -> res_ty) "->" rho_ty
             , ( ( (a
result, Type
arg_ty, Type
res_ty, Type
op_mult)
                 , HsWrapper
res_wrapper )                     -- :: res_ty_out "->" res_ty
               , HsWrapper
arg_wrapper1, [], HsWrapper
arg_wrapper2 ) )  -- :: arg_ty "->" arg_ty_out
               <- forall a.
ExpectedFunTyOrigin
-> UserTypeCtxt
-> Int
-> ExpRhoType
-> ([Scaled ExpRhoType] -> ExpRhoType -> TcM a)
-> TcM (HsWrapper, a)
matchExpectedFunTys ExpectedFunTyOrigin
herald UserTypeCtxt
GenSigCtxt Int
1 (Type -> ExpRhoType
mkCheckExpType Type
rho_ty) forall a b. (a -> b) -> a -> b
$
                  \ [Scaled ExpRhoType
arg_ty] ExpRhoType
res_ty ->
                  do { Type
arg_tc_ty <- ExpRhoType -> TcM Type
expTypeToType (forall a. Scaled a -> a
scaledThing Scaled ExpRhoType
arg_ty)
                     ; Type
res_tc_ty <- ExpRhoType -> TcM Type
expTypeToType ExpRhoType
res_ty

                         -- another nested arrow is too much for now,
                         -- but I bet we'll never need this
                     ; forall (m :: * -> *).
(HasCallStack, Applicative m) =>
Bool -> SDoc -> m ()
massertPpr (case SyntaxOpType
arg_shape of
                                   SynFun {} -> Bool
False;
                                   SyntaxOpType
_         -> Bool
True)
                                  (String -> SDoc
text String
"Too many nested arrows in SyntaxOpType" SDoc -> SDoc -> SDoc
$$
                                   CtOrigin -> SDoc
pprCtOrigin CtOrigin
orig)

                     ; let arg_mult :: Type
arg_mult = forall a. Scaled a -> Type
scaledMult Scaled ExpRhoType
arg_ty
                     ; forall a.
CtOrigin
-> HsExpr GhcRn
-> Type
-> [SyntaxOpType]
-> SyntaxOpType
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, HsWrapper, [HsWrapper], HsWrapper)
tcSynArgA CtOrigin
orig HsExpr GhcRn
op Type
arg_tc_ty [] SyntaxOpType
arg_shape forall a b. (a -> b) -> a -> b
$
                       \ [Type]
arg_results [Type]
arg_res_mults ->
                       forall a.
CtOrigin
-> HsExpr GhcRn
-> Type
-> SyntaxOpType
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, HsWrapper)
tcSynArgE CtOrigin
orig HsExpr GhcRn
op Type
res_tc_ty SyntaxOpType
res_shape forall a b. (a -> b) -> a -> b
$
                       \ [Type]
res_results [Type]
res_res_mults ->
                       do { a
result <- [Type] -> [Type] -> TcM a
thing_inside ([Type]
arg_results forall a. [a] -> [a] -> [a]
++ [Type]
res_results) ([Type
arg_mult] forall a. [a] -> [a] -> [a]
++ [Type]
arg_res_mults forall a. [a] -> [a] -> [a]
++ [Type]
res_res_mults)
                          ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, Type
arg_tc_ty, Type
res_tc_ty, Type
arg_mult) }}

           ; let fun_wrap :: HsWrapper
fun_wrap = HsWrapper -> HsWrapper -> Scaled Type -> Type -> HsWrapper
mkWpFun (HsWrapper
arg_wrapper2 HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
arg_wrapper1) HsWrapper
res_wrapper
                              (forall a. Type -> a -> Scaled a
Scaled Type
op_mult Type
arg_ty) Type
res_ty
               -- NB: arg_ty comes from matchExpectedFunTys, so it has a
               -- fixed RuntimeRep, as needed to call mkWpFun.
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, HsWrapper
match_wrapper HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
fun_wrap) }
      where
        herald :: ExpectedFunTyOrigin
herald = CtOrigin -> HsExpr GhcRn -> ExpectedFunTyOrigin
ExpectedFunTySyntaxOp CtOrigin
orig HsExpr GhcRn
op

    go Type
rho_ty (SynType ExpRhoType
the_ty)
      = do { HsWrapper
wrap   <- CtOrigin -> UserTypeCtxt -> ExpRhoType -> Type -> TcM HsWrapper
tcSubTypePat CtOrigin
orig UserTypeCtxt
GenSigCtxt ExpRhoType
the_ty Type
rho_ty
           ; a
result <- [Type] -> [Type] -> TcM a
thing_inside [] []
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, HsWrapper
wrap) }

-- works on "actual" types, instantiating where necessary
-- See Note [tcSynArg]
tcSynArgA :: CtOrigin
          -> HsExpr GhcRn -- ^ the operator we are checking (for error messages)
          -> TcSigmaType
          -> [SyntaxOpType]              -- ^ argument shapes
          -> SyntaxOpType                -- ^ result shape
          -> ([TcSigmaTypeFRR] -> [Mult] -> TcM a) -- ^ check the arguments
          -> TcM (a, HsWrapper, [HsWrapper], HsWrapper)
            -- ^ returns a wrapper to be applied to the original function,
            -- wrappers to be applied to arguments
            -- and a wrapper to be applied to the overall expression
tcSynArgA :: forall a.
CtOrigin
-> HsExpr GhcRn
-> Type
-> [SyntaxOpType]
-> SyntaxOpType
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, HsWrapper, [HsWrapper], HsWrapper)
tcSynArgA CtOrigin
orig HsExpr GhcRn
op Type
sigma_ty [SyntaxOpType]
arg_shapes SyntaxOpType
res_shape [Type] -> [Type] -> TcM a
thing_inside
  = do { (HsWrapper
match_wrapper, [Scaled Type]
arg_tys, Type
res_ty)
           <- ExpectedFunTyOrigin
-> CtOrigin
-> Maybe TypedThing
-> Int
-> Type
-> TcM (HsWrapper, [Scaled Type], Type)
matchActualFunTysRho ExpectedFunTyOrigin
herald CtOrigin
orig forall a. Maybe a
Nothing
                                   (forall (t :: * -> *) a. Foldable t => t a -> Int
length [SyntaxOpType]
arg_shapes) Type
sigma_ty
              -- match_wrapper :: sigma_ty "->" (arg_tys -> res_ty)
       ; ((a
result, HsWrapper
res_wrapper), [HsWrapper]
arg_wrappers)
           <- forall a.
[Type]
-> [SyntaxOpType]
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, [HsWrapper])
tc_syn_args_e (forall a b. (a -> b) -> [a] -> [b]
map forall a. Scaled a -> a
scaledThing [Scaled Type]
arg_tys) [SyntaxOpType]
arg_shapes forall a b. (a -> b) -> a -> b
$ \ [Type]
arg_results [Type]
arg_res_mults ->
              forall a.
Type -> SyntaxOpType -> ([Type] -> TcM a) -> TcM (a, HsWrapper)
tc_syn_arg    Type
res_ty  SyntaxOpType
res_shape  forall a b. (a -> b) -> a -> b
$ \ [Type]
res_results ->
              [Type] -> [Type] -> TcM a
thing_inside ([Type]
arg_results forall a. [a] -> [a] -> [a]
++ [Type]
res_results) (forall a b. (a -> b) -> [a] -> [b]
map forall a. Scaled a -> Type
scaledMult [Scaled Type]
arg_tys forall a. [a] -> [a] -> [a]
++ [Type]
arg_res_mults)
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, HsWrapper
match_wrapper, [HsWrapper]
arg_wrappers, HsWrapper
res_wrapper) }
  where
    herald :: ExpectedFunTyOrigin
herald = CtOrigin -> HsExpr GhcRn -> ExpectedFunTyOrigin
ExpectedFunTySyntaxOp CtOrigin
orig HsExpr GhcRn
op

    tc_syn_args_e :: [TcSigmaTypeFRR] -> [SyntaxOpType]
                  -> ([TcSigmaTypeFRR] -> [Mult] -> TcM a)
                  -> TcM (a, [HsWrapper])
                    -- the wrappers are for arguments
    tc_syn_args_e :: forall a.
[Type]
-> [SyntaxOpType]
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, [HsWrapper])
tc_syn_args_e (Type
arg_ty : [Type]
arg_tys) (SyntaxOpType
arg_shape : [SyntaxOpType]
arg_shapes) [Type] -> [Type] -> TcM a
thing_inside
      = do { ((a
result, [HsWrapper]
arg_wraps), HsWrapper
arg_wrap)
               <- forall a.
CtOrigin
-> HsExpr GhcRn
-> Type
-> SyntaxOpType
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, HsWrapper)
tcSynArgE     CtOrigin
orig  HsExpr GhcRn
op Type
arg_ty  SyntaxOpType
arg_shape  forall a b. (a -> b) -> a -> b
$ \ [Type]
arg1_results [Type]
arg1_mults ->
                  forall a.
[Type]
-> [SyntaxOpType]
-> ([Type] -> [Type] -> TcM a)
-> TcM (a, [HsWrapper])
tc_syn_args_e          [Type]
arg_tys [SyntaxOpType]
arg_shapes forall a b. (a -> b) -> a -> b
$ \ [Type]
args_results [Type]
args_mults ->
                  [Type] -> [Type] -> TcM a
thing_inside ([Type]
arg1_results forall a. [a] -> [a] -> [a]
++ [Type]
args_results) ([Type]
arg1_mults forall a. [a] -> [a] -> [a]
++ [Type]
args_mults)
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, HsWrapper
arg_wrap forall a. a -> [a] -> [a]
: [HsWrapper]
arg_wraps) }
    tc_syn_args_e [Type]
_ [SyntaxOpType]
_ [Type] -> [Type] -> TcM a
thing_inside = (, []) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Type] -> [Type] -> TcM a
thing_inside [] []

    tc_syn_arg :: TcSigmaTypeFRR -> SyntaxOpType
               -> ([TcSigmaTypeFRR] -> TcM a)
               -> TcM (a, HsWrapper)
                  -- the wrapper applies to the overall result
    tc_syn_arg :: forall a.
Type -> SyntaxOpType -> ([Type] -> TcM a) -> TcM (a, HsWrapper)
tc_syn_arg Type
res_ty SyntaxOpType
SynAny [Type] -> TcM a
thing_inside
      = do { a
result <- [Type] -> TcM a
thing_inside [Type
res_ty]
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, HsWrapper
idHsWrapper) }
    tc_syn_arg Type
res_ty SyntaxOpType
SynRho [Type] -> TcM a
thing_inside
      = do { (HsWrapper
inst_wrap, Type
rho_ty) <- CtOrigin -> Type -> TcM (HsWrapper, Type)
topInstantiate CtOrigin
orig Type
res_ty
               -- inst_wrap :: res_ty "->" rho_ty
           ; a
result <- [Type] -> TcM a
thing_inside [Type
rho_ty]
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, HsWrapper
inst_wrap) }
    tc_syn_arg Type
res_ty SyntaxOpType
SynList [Type] -> TcM a
thing_inside
      = do { (HsWrapper
inst_wrap, Type
rho_ty) <- CtOrigin -> Type -> TcM (HsWrapper, Type)
topInstantiate CtOrigin
orig Type
res_ty
               -- inst_wrap :: res_ty "->" rho_ty
           ; (TcCoercionR
list_co, Type
elt_ty)   <- Type -> TcM (TcCoercionR, Type)
matchExpectedListTy Type
rho_ty
               -- list_co :: [elt_ty] ~N rho_ty
           ; a
result <- [Type] -> TcM a
thing_inside [Type
elt_ty]
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, TcCoercionR -> HsWrapper
mkWpCastN (TcCoercionR -> TcCoercionR
mkTcSymCo TcCoercionR
list_co) HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
inst_wrap) }
    tc_syn_arg Type
_ (SynFun {}) [Type] -> TcM a
_
      = forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"tcSynArgA hits a SynFun" (forall a. Outputable a => a -> SDoc
ppr CtOrigin
orig)
    tc_syn_arg Type
res_ty (SynType ExpRhoType
the_ty) [Type] -> TcM a
thing_inside
      = do { HsWrapper
wrap   <- CtOrigin -> UserTypeCtxt -> Type -> ExpRhoType -> TcM HsWrapper
tcSubType CtOrigin
orig UserTypeCtxt
GenSigCtxt Type
res_ty ExpRhoType
the_ty
           ; a
result <- [Type] -> TcM a
thing_inside []
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (a
result, HsWrapper
wrap) }

{-
Note [Push result type in]
~~~~~~~~~~~~~~~~~~~~~~~~~~
Unify with expected result before type-checking the args so that the
info from res_ty percolates to args.  This is when we might detect a
too-few args situation.  (One can think of cases when the opposite
order would give a better error message.)
experimenting with putting this first.

Here's an example where it actually makes a real difference

   class C t a b | t a -> b
   instance C Char a Bool

   data P t a = forall b. (C t a b) => MkP b
   data Q t   = MkQ (forall a. P t a)

   f1, f2 :: Q Char;
   f1 = MkQ (MkP True)
   f2 = MkQ (MkP True :: forall a. P Char a)

With the change, f1 will type-check, because the 'Char' info from
the signature is propagated into MkQ's argument. With the check
in the other order, the extra signature in f2 is reqd.
-}

{- *********************************************************************
*                                                                      *
                 Record bindings
*                                                                      *
********************************************************************* -}

getFixedTyVars :: [FieldLabelString] -> [TyVar] -> [ConLike] -> TyVarSet
-- These tyvars must not change across the updates
getFixedTyVars :: [FieldLabelString] -> [EvVar] -> [ConLike] -> VarSet
getFixedTyVars [FieldLabelString]
upd_fld_occs [EvVar]
univ_tvs [ConLike]
cons
      = [EvVar] -> VarSet
mkVarSet [EvVar
tv1 | ConLike
con <- [ConLike]
cons
                      , let ([EvVar]
u_tvs, [EvVar]
_, [EqSpec]
eqspec, [Type]
prov_theta
                             , [Type]
req_theta, [Scaled Type]
arg_tys, Type
_)
                              = ConLike
-> ([EvVar], [EvVar], [EqSpec], [Type], [Type], [Scaled Type],
    Type)
conLikeFullSig ConLike
con
                            theta :: [Type]
theta = [EqSpec] -> [Type]
eqSpecPreds [EqSpec]
eqspec
                                     forall a. [a] -> [a] -> [a]
++ [Type]
prov_theta
                                     forall a. [a] -> [a] -> [a]
++ [Type]
req_theta
                            flds :: [FieldLabel]
flds = ConLike -> [FieldLabel]
conLikeFieldLabels ConLike
con
                            fixed_tvs :: VarSet
fixed_tvs = [Type] -> VarSet
exactTyCoVarsOfTypes (forall a b. (a -> b) -> [a] -> [b]
map forall a. Scaled a -> a
scaledThing [Scaled Type]
fixed_tys)
                                    -- fixed_tys: See Note [Type of a record update]
                                        VarSet -> VarSet -> VarSet
`unionVarSet` [Type] -> VarSet
tyCoVarsOfTypes [Type]
theta
                                    -- Universally-quantified tyvars that
                                    -- appear in any of the *implicit*
                                    -- arguments to the constructor are fixed
                                    -- See Note [Implicit type sharing]

                            fixed_tys :: [Scaled Type]
fixed_tys = [Scaled Type
ty | (FieldLabel
fl, Scaled Type
ty) <- forall a b. [a] -> [b] -> [(a, b)]
zip [FieldLabel]
flds [Scaled Type]
arg_tys
                                            , Bool -> Bool
not (FieldLabel -> FieldLabelString
flLabel FieldLabel
fl forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FieldLabelString]
upd_fld_occs)]
                      , (EvVar
tv1,EvVar
tv) <- [EvVar]
univ_tvs forall a b. [a] -> [b] -> [(a, b)]
`zip` [EvVar]
u_tvs
                      , EvVar
tv EvVar -> VarSet -> Bool
`elemVarSet` VarSet
fixed_tvs ]

-- Disambiguate the fields in a record update.
-- See Note [Disambiguating record fields] in GHC.Tc.Gen.Head
disambiguateRecordBinds :: LHsExpr GhcRn -> TcRhoType
                 -> [LHsRecUpdField GhcRn] -> ExpRhoType
                 -> TcM [LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
disambiguateRecordBinds :: LHsExpr GhcRn
-> Type
-> [LHsRecUpdField GhcRn]
-> ExpRhoType
-> TcM
     [LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
disambiguateRecordBinds LHsExpr GhcRn
record_expr Type
record_rho [LHsRecUpdField GhcRn]
rbnds ExpRhoType
res_ty
    -- Are all the fields unambiguous?
  = case forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM LHsRecUpdField GhcRn -> Maybe (LHsRecUpdField GhcRn, Name)
isUnambiguous [LHsRecUpdField GhcRn]
rbnds of
                     -- If so, just skip to looking up the Ids
                     -- Always the case if DuplicateRecordFields is off
      Just [(GenLocated
    SrcSpanAnnA
    (HsFieldBind
       (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
       (GenLocated SrcSpanAnnA (HsExpr GhcRn))),
  Name)]
rbnds' -> forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (LHsRecUpdField GhcRn, Name)
-> TcM
     (LHsFieldBind GhcRn (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn))
lookupSelector [(GenLocated
    SrcSpanAnnA
    (HsFieldBind
       (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
       (GenLocated SrcSpanAnnA (HsExpr GhcRn))),
  Name)]
rbnds'
      Maybe
  [(GenLocated
      SrcSpanAnnA
      (HsFieldBind
         (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
         (GenLocated SrcSpanAnnA (HsExpr GhcRn))),
    Name)]
Nothing     -> -- If not, try to identify a single parent
        do { FamInstEnvs
fam_inst_envs <- TcM FamInstEnvs
tcGetFamInstEnvs
             -- Look up the possible parents for each field
           ; [(GenLocated
    SrcSpanAnnA
    (HsFieldBind
       (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
       (GenLocated SrcSpanAnnA (HsExpr GhcRn))),
  [(RecSelParent, GlobalRdrElt)])]
rbnds_with_parents <- TcM [(LHsRecUpdField GhcRn, [(RecSelParent, GlobalRdrElt)])]
getUpdFieldsParents
           ; let possible_parents :: [[RecSelParent]]
possible_parents = forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(GenLocated
    SrcSpanAnnA
    (HsFieldBind
       (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
       (GenLocated SrcSpanAnnA (HsExpr GhcRn))),
  [(RecSelParent, GlobalRdrElt)])]
rbnds_with_parents
             -- Identify a single parent
           ; RecSelParent
p <- FamInstEnvs -> [[RecSelParent]] -> TcM RecSelParent
identifyParent FamInstEnvs
fam_inst_envs [[RecSelParent]]
possible_parents
             -- Pick the right selector with that parent for each field
           ; forall r. TcM r -> TcM r
checkNoErrs forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (RecSelParent
-> (LHsRecUpdField GhcRn, [(RecSelParent, GlobalRdrElt)])
-> TcM
     (LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn))
pickParent RecSelParent
p) [(GenLocated
    SrcSpanAnnA
    (HsFieldBind
       (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
       (GenLocated SrcSpanAnnA (HsExpr GhcRn))),
  [(RecSelParent, GlobalRdrElt)])]
rbnds_with_parents }
  where
    -- Extract the selector name of a field update if it is unambiguous
    isUnambiguous :: LHsRecUpdField GhcRn -> Maybe (LHsRecUpdField GhcRn,Name)
    isUnambiguous :: LHsRecUpdField GhcRn -> Maybe (LHsRecUpdField GhcRn, Name)
isUnambiguous LHsRecUpdField GhcRn
x = case forall l e. GenLocated l e -> e
unLoc (forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS (forall l e. GenLocated l e -> e
unLoc LHsRecUpdField GhcRn
x)) of
                        Unambiguous XUnambiguous GhcRn
sel_name LocatedN RdrName
_ -> forall a. a -> Maybe a
Just (LHsRecUpdField GhcRn
x, XUnambiguous GhcRn
sel_name)
                        Ambiguous{}            -> forall a. Maybe a
Nothing

    -- Look up the possible parents and selector GREs for each field
    getUpdFieldsParents :: TcM [(LHsRecUpdField GhcRn
                                , [(RecSelParent, GlobalRdrElt)])]
    getUpdFieldsParents :: TcM [(LHsRecUpdField GhcRn, [(RecSelParent, GlobalRdrElt)])]
getUpdFieldsParents
      = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. [a] -> [b] -> [(a, b)]
zip [LHsRecUpdField GhcRn]
rbnds) forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM
          (Bool
-> RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) [(RecSelParent, GlobalRdrElt)]
lookupParents Bool
False forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: Pass).
HsRecUpdField (GhcPass p) -> GenLocated SrcSpan RdrName
hsRecUpdFieldRdr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc)
          [LHsRecUpdField GhcRn]
rbnds

    -- Given a the lists of possible parents for each field,
    -- identify a single parent
    identifyParent :: FamInstEnvs -> [[RecSelParent]] -> TcM RecSelParent
    identifyParent :: FamInstEnvs -> [[RecSelParent]] -> TcM RecSelParent
identifyParent FamInstEnvs
fam_inst_envs [[RecSelParent]]
possible_parents
      = case forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 forall a. Eq a => [a] -> [a] -> [a]
intersect [[RecSelParent]]
possible_parents of
        -- No parents for all fields: record update is ill-typed
        []  -> forall a. TcRnMessage -> TcM a
failWithTc ([LHsRecUpdField GhcRn] -> TcRnMessage
TcRnNoPossibleParentForFields [LHsRecUpdField GhcRn]
rbnds)

        -- Exactly one datatype with all the fields: use that
        [RecSelParent
p] -> forall (m :: * -> *) a. Monad m => a -> m a
return RecSelParent
p

        -- Multiple possible parents: try harder to disambiguate
        -- Can we get a parent TyCon from the pushed-in type?
        RecSelParent
_:[RecSelParent]
_ | Just TyCon
p <- FamInstEnvs -> ExpRhoType -> Maybe TyCon
tyConOfET FamInstEnvs
fam_inst_envs ExpRhoType
res_ty ->
              do { TyCon -> TcRn ()
reportAmbiguousField TyCon
p
                 ; forall (m :: * -> *) a. Monad m => a -> m a
return (TyCon -> RecSelParent
RecSelData TyCon
p) }

        -- Does the expression being updated have a type signature?
        -- If so, try to extract a parent TyCon from it
            | Just {} <- HsExpr GhcRn -> Maybe (LHsSigWcType GhcRn)
obviousSig (forall l e. GenLocated l e -> e
unLoc LHsExpr GhcRn
record_expr)
            , Just TyCon
tc <- FamInstEnvs -> Type -> Maybe TyCon
tyConOf FamInstEnvs
fam_inst_envs Type
record_rho
            -> do { TyCon -> TcRn ()
reportAmbiguousField TyCon
tc
                  ; forall (m :: * -> *) a. Monad m => a -> m a
return (TyCon -> RecSelParent
RecSelData TyCon
tc) }

        -- Nothing else we can try...
        [RecSelParent]
_ -> forall a. TcRnMessage -> TcM a
failWithTc ([LHsRecUpdField GhcRn] -> TcRnMessage
TcRnBadOverloadedRecordUpdate [LHsRecUpdField GhcRn]
rbnds)

    -- Make a field unambiguous by choosing the given parent.
    -- Emits an error if the field cannot have that parent,
    -- e.g. if the user writes
    --     r { x = e } :: T
    -- where T does not have field x.
    pickParent :: RecSelParent
               -> (LHsRecUpdField GhcRn, [(RecSelParent, GlobalRdrElt)])
               -> TcM (LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn))
    pickParent :: RecSelParent
-> (LHsRecUpdField GhcRn, [(RecSelParent, GlobalRdrElt)])
-> TcM
     (LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn))
pickParent RecSelParent
p (LHsRecUpdField GhcRn
upd, [(RecSelParent, GlobalRdrElt)]
xs)
      = case forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup RecSelParent
p [(RecSelParent, GlobalRdrElt)]
xs of
                      -- Phew! The parent is valid for this field.
                      -- Previously ambiguous fields must be marked as
                      -- used now that we know which one is meant, but
                      -- unambiguous ones shouldn't be recorded again
                      -- (giving duplicate deprecation warnings).
          Just GlobalRdrElt
gre -> do { forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> Bool
null (forall a. [a] -> [a]
tail [(RecSelParent, GlobalRdrElt)]
xs)) forall a b. (a -> b) -> a -> b
$ do
                             let L SrcAnn NoEpAnns
loc AmbiguousFieldOcc GhcRn
_ = forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS (forall l e. GenLocated l e -> e
unLoc LHsRecUpdField GhcRn
upd)
                             forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcAnn NoEpAnns
loc forall a b. (a -> b) -> a -> b
$ Bool -> GlobalRdrElt -> TcRn ()
addUsedGRE Bool
True GlobalRdrElt
gre
                         ; (LHsRecUpdField GhcRn, Name)
-> TcM
     (LHsFieldBind GhcRn (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn))
lookupSelector (LHsRecUpdField GhcRn
upd, GlobalRdrElt -> Name
greMangledName GlobalRdrElt
gre) }
                      -- The field doesn't belong to this parent, so report
                      -- an error but keep going through all the fields
          Maybe GlobalRdrElt
Nothing  -> do { TcRnMessage -> TcRn ()
addErrTc (RecSelParent -> RdrName -> TcRnMessage
fieldNotInType RecSelParent
p
                                      (forall l e. GenLocated l e -> e
unLoc (forall (p :: Pass).
HsRecUpdField (GhcPass p) -> GenLocated SrcSpan RdrName
hsRecUpdFieldRdr (forall l e. GenLocated l e -> e
unLoc LHsRecUpdField GhcRn
upd))))
                         ; (LHsRecUpdField GhcRn, Name)
-> TcM
     (LHsFieldBind GhcRn (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn))
lookupSelector (LHsRecUpdField GhcRn
upd, GlobalRdrElt -> Name
greMangledName (forall a b. (a, b) -> b
snd (forall a. [a] -> a
head [(RecSelParent, GlobalRdrElt)]
xs))) }

    -- Given a (field update, selector name) pair, look up the
    -- selector to give a field update with an unambiguous Id
    lookupSelector :: (LHsRecUpdField GhcRn, Name)
                 -> TcM (LHsFieldBind GhcRn (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn))
    lookupSelector :: (LHsRecUpdField GhcRn, Name)
-> TcM
     (LHsFieldBind GhcRn (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn))
lookupSelector (L SrcSpanAnnA
l HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
upd, Name
n)
      = do { EvVar
i <- Name -> TcM EvVar
tcLookupId Name
n
           ; let L SrcAnn NoEpAnns
loc AmbiguousFieldOcc GhcRn
af = forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
upd
                 lbl :: RdrName
lbl      = forall (p :: Pass). AmbiguousFieldOcc (GhcPass p) -> RdrName
rdrNameAmbiguousFieldOcc AmbiguousFieldOcc GhcRn
af
           ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l HsFieldBind
               { hfbAnn :: XHsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc))
hfbAnn = forall lhs rhs. HsFieldBind lhs rhs -> XHsFieldBind lhs
hfbAnn HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
upd
               , hfbLHS :: GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc)
hfbLHS
                       = forall l e. l -> e -> GenLocated l e
L (forall a ann. SrcSpanAnn' a -> SrcAnn ann
l2l SrcAnn NoEpAnns
loc) (forall pass.
XUnambiguous pass -> LocatedN RdrName -> AmbiguousFieldOcc pass
Unambiguous EvVar
i (forall l e. l -> e -> GenLocated l e
L (forall a ann. SrcSpanAnn' a -> SrcAnn ann
l2l SrcAnn NoEpAnns
loc) RdrName
lbl))
               , hfbRHS :: GenLocated SrcSpanAnnA (HsExpr GhcRn)
hfbRHS = forall lhs rhs. HsFieldBind lhs rhs -> rhs
hfbRHS HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
upd
               , hfbPun :: Bool
hfbPun = forall lhs rhs. HsFieldBind lhs rhs -> Bool
hfbPun HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
upd
               }
           }

    -- See Note [Deprecating ambiguous fields] in GHC.Tc.Gen.Head
    reportAmbiguousField :: TyCon -> TcM ()
    reportAmbiguousField :: TyCon -> TcRn ()
reportAmbiguousField TyCon
parent_type =
        forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
loc forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
addDiagnostic forall a b. (a -> b) -> a -> b
$ HsExpr GhcRn -> TyCon -> TcRnMessage
TcRnAmbiguousField HsExpr GhcRn
rupd TyCon
parent_type
      where
        rupd :: HsExpr GhcRn
rupd = RecordUpd { rupd_expr :: LHsExpr GhcRn
rupd_expr = LHsExpr GhcRn
record_expr, rupd_flds :: Either [LHsRecUpdField GhcRn] [LHsRecUpdProj GhcRn]
rupd_flds = forall a b. a -> Either a b
Left [LHsRecUpdField GhcRn]
rbnds, rupd_ext :: XRecordUpd GhcRn
rupd_ext = NoExtField
noExtField }
        loc :: SrcSpan
loc  = forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA (forall a. [a] -> a
head [LHsRecUpdField GhcRn]
rbnds)

{-
Game plan for record bindings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. Find the TyCon for the bindings, from the first field label.

2. Instantiate its tyvars and unify (T a1 .. an) with expected_ty.

For each binding field = value

3. Instantiate the field type (from the field label) using the type
   envt from step 2.

4  Type check the value using tcCheckPolyExprNC (in tcRecordField),
   passing the field type as the expected argument type.

This extends OK when the field types are universally quantified.
-}

tcRecordBinds
        :: ConLike
        -> [TcType]     -- Expected type for each field
        -> HsRecordBinds GhcRn
        -> TcM (HsRecordBinds GhcTc)

tcRecordBinds :: ConLike
-> [Type] -> HsRecordBinds GhcRn -> TcM (HsRecordBinds GhcTc)
tcRecordBinds ConLike
con_like [Type]
arg_tys (HsRecFields [LHsRecField GhcRn (LHsExpr GhcRn)]
rbinds Maybe (Located Int)
dd)
  = do  { [Maybe
   (GenLocated
      SrcSpanAnnA
      (HsFieldBind
         (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
         (GenLocated SrcSpanAnnA (HsExpr GhcTc))))]
mb_binds <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM LHsRecField GhcRn (LHsExpr GhcRn)
-> TcM (Maybe (LHsRecField GhcTc (LHsExpr GhcTc)))
do_bind [LHsRecField GhcRn (LHsExpr GhcRn)]
rbinds
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p arg.
[LHsRecField p arg] -> Maybe (Located Int) -> HsRecFields p arg
HsRecFields (forall a. [Maybe a] -> [a]
catMaybes [Maybe
   (GenLocated
      SrcSpanAnnA
      (HsFieldBind
         (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
         (GenLocated SrcSpanAnnA (HsExpr GhcTc))))]
mb_binds) Maybe (Located Int)
dd) }
  where
    fields :: [Name]
fields = forall a b. (a -> b) -> [a] -> [b]
map FieldLabel -> Name
flSelector forall a b. (a -> b) -> a -> b
$ ConLike -> [FieldLabel]
conLikeFieldLabels ConLike
con_like
    flds_w_tys :: [(Name, Type)]
flds_w_tys = forall a b. String -> [a] -> [b] -> [(a, b)]
zipEqual String
"tcRecordBinds" [Name]
fields [Type]
arg_tys

    do_bind :: LHsRecField GhcRn (LHsExpr GhcRn)
            -> TcM (Maybe (LHsRecField GhcTc (LHsExpr GhcTc)))
    do_bind :: LHsRecField GhcRn (LHsExpr GhcRn)
-> TcM (Maybe (LHsRecField GhcTc (LHsExpr GhcTc)))
do_bind (L SrcSpanAnnA
l fld :: HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
fld@(HsFieldBind { hfbLHS :: forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS = GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)
f
                                 , hfbRHS :: forall lhs rhs. HsFieldBind lhs rhs -> rhs
hfbRHS = GenLocated SrcSpanAnnA (HsExpr GhcRn)
rhs }))

      = do { Maybe
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc),
   GenLocated SrcSpanAnnA (HsExpr GhcTc))
mb <- ConLike
-> [(Name, Type)]
-> LFieldOcc GhcRn
-> LHsExpr GhcRn
-> TcM (Maybe (LFieldOcc GhcTc, LHsExpr GhcTc))
tcRecordField ConLike
con_like [(Name, Type)]
flds_w_tys GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)
f GenLocated SrcSpanAnnA (HsExpr GhcRn)
rhs
           ; case Maybe
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc),
   GenLocated SrcSpanAnnA (HsExpr GhcTc))
mb of
               Maybe
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc),
   GenLocated SrcSpanAnnA (HsExpr GhcTc))
Nothing         -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
               -- Just (f', rhs') -> return (Just (L l (fld { hfbLHS = f'
               --                                            , hfbRHS = rhs' }))) }
               Just (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc)
f', GenLocated SrcSpanAnnA (HsExpr GhcTc)
rhs') -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (HsFieldBind
                                                     { hfbAnn :: XHsFieldBind (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc))
hfbAnn = forall lhs rhs. HsFieldBind lhs rhs -> XHsFieldBind lhs
hfbAnn HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
fld
                                                     , hfbLHS :: GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc)
hfbLHS = GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc)
f'
                                                     , hfbRHS :: GenLocated SrcSpanAnnA (HsExpr GhcTc)
hfbRHS = GenLocated SrcSpanAnnA (HsExpr GhcTc)
rhs'
                                                     , hfbPun :: Bool
hfbPun = forall lhs rhs. HsFieldBind lhs rhs -> Bool
hfbPun HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
fld}))) }

tcRecordUpd
        :: ConLike
        -> [TcType]     -- Expected type for each field
        -> [LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
        -> TcM [LHsRecUpdField GhcTc]

tcRecordUpd :: ConLike
-> [Type]
-> [LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
-> TcM [LHsRecUpdField GhcTc]
tcRecordUpd ConLike
con_like [Type]
arg_tys [LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
rbinds = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. [Maybe a] -> [a]
catMaybes forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)
-> TcM (Maybe (LHsRecUpdField GhcTc))
do_bind [LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
rbinds
  where
    fields :: [Name]
fields = forall a b. (a -> b) -> [a] -> [b]
map FieldLabel -> Name
flSelector forall a b. (a -> b) -> a -> b
$ ConLike -> [FieldLabel]
conLikeFieldLabels ConLike
con_like
    flds_w_tys :: [(Name, Type)]
flds_w_tys = forall a b. String -> [a] -> [b] -> [(a, b)]
zipEqual String
"tcRecordUpd" [Name]
fields [Type]
arg_tys

    do_bind :: LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)
            -> TcM (Maybe (LHsRecUpdField GhcTc))
    do_bind :: LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)
-> TcM (Maybe (LHsRecUpdField GhcTc))
do_bind (L SrcSpanAnnA
l fld :: HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
fld@(HsFieldBind { hfbLHS :: forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS = L SrcAnn NoEpAnns
loc AmbiguousFieldOcc GhcTc
af
                                 , hfbRHS :: forall lhs rhs. HsFieldBind lhs rhs -> rhs
hfbRHS = GenLocated SrcSpanAnnA (HsExpr GhcRn)
rhs }))
      = do { let lbl :: RdrName
lbl = forall (p :: Pass). AmbiguousFieldOcc (GhcPass p) -> RdrName
rdrNameAmbiguousFieldOcc AmbiguousFieldOcc GhcTc
af
                 sel_id :: EvVar
sel_id = AmbiguousFieldOcc GhcTc -> EvVar
selectorAmbiguousFieldOcc AmbiguousFieldOcc GhcTc
af
                 f :: GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)
f = forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
loc (forall pass. XCFieldOcc pass -> XRec pass RdrName -> FieldOcc pass
FieldOcc (EvVar -> Name
idName EvVar
sel_id) (forall l e. l -> e -> GenLocated l e
L (forall a ann. SrcSpanAnn' a -> SrcAnn ann
l2l SrcAnn NoEpAnns
loc) RdrName
lbl))
           ; Maybe
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc),
   GenLocated SrcSpanAnnA (HsExpr GhcTc))
mb <- ConLike
-> [(Name, Type)]
-> LFieldOcc GhcRn
-> LHsExpr GhcRn
-> TcM (Maybe (LFieldOcc GhcTc, LHsExpr GhcTc))
tcRecordField ConLike
con_like [(Name, Type)]
flds_w_tys GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)
f GenLocated SrcSpanAnnA (HsExpr GhcRn)
rhs
           ; case Maybe
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc),
   GenLocated SrcSpanAnnA (HsExpr GhcTc))
mb of
               Maybe
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc),
   GenLocated SrcSpanAnnA (HsExpr GhcTc))
Nothing         -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
               Just (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc)
f', GenLocated SrcSpanAnnA (HsExpr GhcTc)
rhs') ->
                 forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just
                         (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc))
  (GenLocated SrcSpanAnnA (HsExpr GhcRn))
fld { hfbLHS :: GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcTc)
hfbLHS
                                      = forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
loc (forall pass.
XUnambiguous pass -> LocatedN RdrName -> AmbiguousFieldOcc pass
Unambiguous
                                               (forall pass. FieldOcc pass -> XCFieldOcc pass
foExt (forall l e. GenLocated l e -> e
unLoc GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcTc)
f'))
                                               (forall l e. l -> e -> GenLocated l e
L (forall a ann. SrcSpanAnn' a -> SrcAnn ann
l2l SrcAnn NoEpAnns
loc) RdrName
lbl))
                                   , hfbRHS :: GenLocated SrcSpanAnnA (HsExpr GhcTc)
hfbRHS = GenLocated SrcSpanAnnA (HsExpr GhcTc)
rhs' }))) }

tcRecordField :: ConLike -> Assoc Name Type
              -> LFieldOcc GhcRn -> LHsExpr GhcRn
              -> TcM (Maybe (LFieldOcc GhcTc, LHsExpr GhcTc))
tcRecordField :: ConLike
-> [(Name, Type)]
-> LFieldOcc GhcRn
-> LHsExpr GhcRn
-> TcM (Maybe (LFieldOcc GhcTc, LHsExpr GhcTc))
tcRecordField ConLike
con_like [(Name, Type)]
flds_w_tys (L SrcAnn NoEpAnns
loc (FieldOcc XCFieldOcc GhcRn
sel_name XRec GhcRn RdrName
lbl)) LHsExpr GhcRn
rhs
  | Just Type
field_ty <- forall a b. Eq a => Assoc a b -> a -> Maybe b
assocMaybe [(Name, Type)]
flds_w_tys XCFieldOcc GhcRn
sel_name
      = forall a. SDoc -> TcM a -> TcM a
addErrCtxt (FieldLabelString -> SDoc
fieldCtxt FieldLabelString
field_lbl) forall a b. (a -> b) -> a -> b
$
        do { GenLocated SrcSpanAnnA (HsExpr GhcTc)
rhs' <- LHsExpr GhcRn -> Type -> TcM (LHsExpr GhcTc)
tcCheckPolyExprNC LHsExpr GhcRn
rhs Type
field_ty
           ; HasDebugCallStack => FixedRuntimeRepContext -> Type -> TcRn ()
hasFixedRuntimeRep_syntactic (RdrName -> HsExpr GhcTc -> FixedRuntimeRepContext
FRRRecordUpdate (forall l e. GenLocated l e -> e
unLoc XRec GhcRn RdrName
lbl) (forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnA (HsExpr GhcTc)
rhs'))
                Type
field_ty
           ; let field_id :: EvVar
field_id = OccName -> Unique -> Type -> Type -> SrcSpan -> EvVar
mkUserLocal (Name -> OccName
nameOccName XCFieldOcc GhcRn
sel_name)
                                        (Name -> Unique
nameUnique XCFieldOcc GhcRn
sel_name)
                                        Type
Many Type
field_ty (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcAnn NoEpAnns
loc)
                -- Yuk: the field_id has the *unique* of the selector Id
                --          (so we can find it easily)
                --      but is a LocalId with the appropriate type of the RHS
                --          (so the desugarer knows the type of local binder to make)
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just (forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
loc (forall pass. XCFieldOcc pass -> XRec pass RdrName -> FieldOcc pass
FieldOcc EvVar
field_id XRec GhcRn RdrName
lbl), GenLocated SrcSpanAnnA (HsExpr GhcTc)
rhs')) }
      | Bool
otherwise
      = do { TcRnMessage -> TcRn ()
addErrTc (Name -> FieldLabelString -> TcRnMessage
badFieldConErr (forall a. NamedThing a => a -> Name
getName ConLike
con_like) FieldLabelString
field_lbl)
           ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing }
  where
        field_lbl :: FieldLabelString
field_lbl = OccName -> FieldLabelString
occNameFS forall a b. (a -> b) -> a -> b
$ RdrName -> OccName
rdrNameOcc (forall l e. GenLocated l e -> e
unLoc XRec GhcRn RdrName
lbl)


checkMissingFields ::  ConLike -> HsRecordBinds GhcRn -> [Scaled TcType] -> TcM ()
checkMissingFields :: ConLike -> HsRecordBinds GhcRn -> [Scaled Type] -> TcRn ()
checkMissingFields ConLike
con_like HsRecordBinds GhcRn
rbinds [Scaled Type]
arg_tys
  | forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FieldLabel]
field_labels   -- Not declared as a record;
                        -- But C{} is still valid if no strict fields
  = if forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any HsImplBang -> Bool
isBanged [HsImplBang]
field_strs then
        -- Illegal if any arg is strict
        TcRnMessage -> TcRn ()
addErrTc (ConLike -> [(FieldLabelString, Type)] -> TcRnMessage
TcRnMissingStrictFields ConLike
con_like [])
    else do
        forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall (f :: * -> *) a. Foldable f => f a -> Bool
notNull [HsImplBang]
field_strs Bool -> Bool -> Bool
&& forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FieldLabel]
field_labels) forall a b. (a -> b) -> a -> b
$ do
          let msg :: TcRnMessage
msg = ConLike -> [(FieldLabelString, Type)] -> TcRnMessage
TcRnMissingFields ConLike
con_like []
          (Bool -> TcRnMessage -> TcRn ()
diagnosticTc Bool
True TcRnMessage
msg)

  | Bool
otherwise = do              -- A record
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(FieldLabelString, Type)]
missing_s_fields) forall a b. (a -> b) -> a -> b
$ do
        [(FieldLabelString, Type)]
fs <- forall {t :: * -> *} {a}.
Traversable t =>
t (a, Type) -> IOEnv (Env TcGblEnv TcLclEnv) (t (a, Type))
zonk_fields [(FieldLabelString, Type)]
missing_s_fields
        -- It is an error to omit a strict field, because
        -- we can't substitute it with (error "Missing field f")
        TcRnMessage -> TcRn ()
addErrTc (ConLike -> [(FieldLabelString, Type)] -> TcRnMessage
TcRnMissingStrictFields ConLike
con_like [(FieldLabelString, Type)]
fs)

    Bool
warn <- forall gbl lcl. WarningFlag -> TcRnIf gbl lcl Bool
woptM WarningFlag
Opt_WarnMissingFields
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
warn Bool -> Bool -> Bool
&& forall (f :: * -> *) a. Foldable f => f a -> Bool
notNull [(FieldLabelString, Type)]
missing_ns_fields) forall a b. (a -> b) -> a -> b
$ do
        [(FieldLabelString, Type)]
fs <- forall {t :: * -> *} {a}.
Traversable t =>
t (a, Type) -> IOEnv (Env TcGblEnv TcLclEnv) (t (a, Type))
zonk_fields [(FieldLabelString, Type)]
missing_ns_fields
        -- It is not an error (though we may want) to omit a
        -- lazy field, because we can always use
        -- (error "Missing field f") instead.
        let msg :: TcRnMessage
msg = ConLike -> [(FieldLabelString, Type)] -> TcRnMessage
TcRnMissingFields ConLike
con_like [(FieldLabelString, Type)]
fs
        Bool -> TcRnMessage -> TcRn ()
diagnosticTc Bool
True TcRnMessage
msg

  where
    -- we zonk the fields to get better types in error messages (#18869)
    zonk_fields :: t (a, Type) -> IOEnv (Env TcGblEnv TcLclEnv) (t (a, Type))
zonk_fields t (a, Type)
fs = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM t (a, Type)
fs forall a b. (a -> b) -> a -> b
$ \(a
str,Type
ty) -> do
        Type
ty' <- Type -> TcM Type
zonkTcType Type
ty
        forall (m :: * -> *) a. Monad m => a -> m a
return (a
str,Type
ty')
    missing_s_fields :: [(FieldLabelString, Type)]
missing_s_fields
        = [ (FieldLabel -> FieldLabelString
flLabel FieldLabel
fl, forall a. Scaled a -> a
scaledThing Scaled Type
ty) | (FieldLabel
fl,HsImplBang
str,Scaled Type
ty) <- [(FieldLabel, HsImplBang, Scaled Type)]
field_info,
                 HsImplBang -> Bool
isBanged HsImplBang
str,
                 Bool -> Bool
not (FieldLabel
fl forall {t :: * -> *}. Foldable t => FieldLabel -> t Name -> Bool
`elemField` [XCFieldOcc GhcRn]
field_names_used)
          ]
    missing_ns_fields :: [(FieldLabelString, Type)]
missing_ns_fields
        = [ (FieldLabel -> FieldLabelString
flLabel FieldLabel
fl, forall a. Scaled a -> a
scaledThing Scaled Type
ty) | (FieldLabel
fl,HsImplBang
str,Scaled Type
ty) <- [(FieldLabel, HsImplBang, Scaled Type)]
field_info,
                 Bool -> Bool
not (HsImplBang -> Bool
isBanged HsImplBang
str),
                 Bool -> Bool
not (FieldLabel
fl forall {t :: * -> *}. Foldable t => FieldLabel -> t Name -> Bool
`elemField` [XCFieldOcc GhcRn]
field_names_used)
          ]

    field_names_used :: [XCFieldOcc GhcRn]
field_names_used = forall p arg. UnXRec p => HsRecFields p arg -> [XCFieldOcc p]
hsRecFields HsRecordBinds GhcRn
rbinds
    field_labels :: [FieldLabel]
field_labels     = ConLike -> [FieldLabel]
conLikeFieldLabels ConLike
con_like

    field_info :: [(FieldLabel, HsImplBang, Scaled Type)]
field_info = forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [FieldLabel]
field_labels [HsImplBang]
field_strs [Scaled Type]
arg_tys

    field_strs :: [HsImplBang]
field_strs = ConLike -> [HsImplBang]
conLikeImplBangs ConLike
con_like

    FieldLabel
fl elemField :: FieldLabel -> t Name -> Bool
`elemField` t Name
flds = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\ Name
fl' -> FieldLabel -> Name
flSelector FieldLabel
fl forall a. Eq a => a -> a -> Bool
== Name
fl') t Name
flds

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

Boring and alphabetical:
-}

fieldCtxt :: FieldLabelString -> SDoc
fieldCtxt :: FieldLabelString -> SDoc
fieldCtxt FieldLabelString
field_name
  = String -> SDoc
text String
"In the" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr FieldLabelString
field_name) SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"field of a record"

badFieldsUpd
  :: [LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
               -- Field names that don't belong to a single datacon
  -> [ConLike] -- Data cons of the type which the first field name belongs to
  -> TcRnMessage
badFieldsUpd :: [LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
-> [ConLike] -> TcRnMessage
badFieldsUpd [LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
rbinds [ConLike]
data_cons
  = [FieldLabelString] -> TcRnMessage
TcRnNoConstructorHasAllFields [FieldLabelString]
conflictingFields
          -- See Note [Finding the conflicting fields]
  where
    -- A (preferably small) set of fields such that no constructor contains
    -- all of them.  See Note [Finding the conflicting fields]
    conflictingFields :: [FieldLabelString]
conflictingFields = case [(FieldLabelString, [Bool])]
nonMembers of
        -- nonMember belongs to a different type.
        (FieldLabelString
nonMember, [Bool]
_) : [(FieldLabelString, [Bool])]
_ -> [FieldLabelString
aMember, FieldLabelString
nonMember]
        [] -> let
            -- All of rbinds belong to one type. In this case, repeatedly add
            -- a field to the set until no constructor contains the set.

            -- Each field, together with a list indicating which constructors
            -- have all the fields so far.
            growingSets :: [(FieldLabelString, [Bool])]
            growingSets :: [(FieldLabelString, [Bool])]
growingSets = forall a. (a -> a -> a) -> [a] -> [a]
scanl1 forall {a} {a}. (a, [Bool]) -> (a, [Bool]) -> (a, [Bool])
combine [(FieldLabelString, [Bool])]
membership
            combine :: (a, [Bool]) -> (a, [Bool]) -> (a, [Bool])
combine (a
_, [Bool]
setMem) (a
field, [Bool]
fldMem)
              = (a
field, forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Bool -> Bool -> Bool
(&&) [Bool]
setMem [Bool]
fldMem)
            in
            -- Fields that don't change the membership status of the set
            -- are redundant and can be dropped.
            forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> a
head) forall a b. (a -> b) -> a -> b
$ forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (forall a. Eq a => a -> a -> Bool
(==) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a b. (a, b) -> b
snd) [(FieldLabelString, [Bool])]
growingSets

    aMember :: FieldLabelString
aMember = forall a. HasCallStack => Bool -> a -> a
assert (Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(FieldLabelString, [Bool])]
members) ) forall a b. (a, b) -> a
fst (forall a. [a] -> a
head [(FieldLabelString, [Bool])]
members)
    ([(FieldLabelString, [Bool])]
members, [(FieldLabelString, [Bool])]
nonMembers) = forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (forall (t :: * -> *). Foldable t => t Bool -> Bool
or forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> b
snd) [(FieldLabelString, [Bool])]
membership

    -- For each field, which constructors contain the field?
    membership :: [(FieldLabelString, [Bool])]
    membership :: [(FieldLabelString, [Bool])]
membership = forall {a}. [(a, [Bool])] -> [(a, [Bool])]
sortMembership forall a b. (a -> b) -> a -> b
$
        forall a b. (a -> b) -> [a] -> [b]
map (\FieldLabelString
fld -> (FieldLabelString
fld, forall a b. (a -> b) -> [a] -> [b]
map (FieldLabelString
fld forall a. Uniquable a => a -> UniqSet a -> Bool
`elementOfUniqSet`) [UniqSet FieldLabelString]
fieldLabelSets)) forall a b. (a -> b) -> a -> b
$
          forall a b. (a -> b) -> [a] -> [b]
map (OccName -> FieldLabelString
occNameFS forall b c a. (b -> c) -> (a -> b) -> a -> c
. RdrName -> OccName
rdrNameOcc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: Pass). AmbiguousFieldOcc (GhcPass p) -> RdrName
rdrNameAmbiguousFieldOcc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc) [LHsFieldBind GhcTc (LAmbiguousFieldOcc GhcTc) (LHsExpr GhcRn)]
rbinds

    fieldLabelSets :: [UniqSet FieldLabelString]
    fieldLabelSets :: [UniqSet FieldLabelString]
fieldLabelSets = forall a b. (a -> b) -> [a] -> [b]
map (forall a. Uniquable a => [a] -> UniqSet a
mkUniqSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map FieldLabel -> FieldLabelString
flLabel forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConLike -> [FieldLabel]
conLikeFieldLabels) [ConLike]
data_cons

    -- Sort in order of increasing number of True, so that a smaller
    -- conflicting set can be found.
    sortMembership :: [(a, [Bool])] -> [(a, [Bool])]
sortMembership =
      forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (forall a. Ord a => a -> a -> Ordering
compare forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a b. (a, b) -> a
fst) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      forall a b. (a -> b) -> [a] -> [b]
map (\ item :: (a, [Bool])
item@(a
_, [Bool]
membershipRow) -> ([Bool] -> Int
countTrue [Bool]
membershipRow, (a, [Bool])
item))

    countTrue :: [Bool] -> Int
countTrue = forall a. (a -> Bool) -> [a] -> Int
count forall a. a -> a
id

{-
Note [Finding the conflicting fields]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Suppose we have
  data A = A {a0, a1 :: Int}
         | B {b0, b1 :: Int}
and we see a record update
  x { a0 = 3, a1 = 2, b0 = 4, b1 = 5 }
Then we'd like to find the smallest subset of fields that no
constructor has all of.  Here, say, {a0,b0}, or {a0,b1}, etc.
We don't really want to report that no constructor has all of
{a0,a1,b0,b1}, because when there are hundreds of fields it's
hard to see what was really wrong.

We may need more than two fields, though; eg
  data T = A { x,y :: Int, v::Int }
          | B { y,z :: Int, v::Int }
          | C { z,x :: Int, v::Int }
with update
   r { x=e1, y=e2, z=e3 }, we

Finding the smallest subset is hard, so the code here makes
a decent stab, no more.  See #7989.
-}

mixedSelectors :: [Id] -> [Id] -> TcRnMessage
mixedSelectors :: [EvVar] -> [EvVar] -> TcRnMessage
mixedSelectors data_sels :: [EvVar]
data_sels@(EvVar
dc_rep_id:[EvVar]
_) pat_syn_sels :: [EvVar]
pat_syn_sels@(EvVar
ps_rep_id:[EvVar]
_)
  = Name -> [EvVar] -> Name -> [EvVar] -> TcRnMessage
TcRnMixedSelectors (TyCon -> Name
tyConName TyCon
rep_dc) [EvVar]
data_sels (PatSyn -> Name
patSynName PatSyn
rep_ps) [EvVar]
pat_syn_sels
  where
    RecSelPatSyn PatSyn
rep_ps = EvVar -> RecSelParent
recordSelectorTyCon EvVar
ps_rep_id
    RecSelData TyCon
rep_dc = EvVar -> RecSelParent
recordSelectorTyCon EvVar
dc_rep_id
mixedSelectors [EvVar]
_ [EvVar]
_ = forall a. String -> a
panic String
"GHC.Tc.Gen.Expr: mixedSelectors emptylists"

{-
************************************************************************
*                                                                      *
\subsection{Static Pointers}
*                                                                      *
************************************************************************
-}

-- | Checks if the given name is closed and emits an error if not.
--
-- See Note [Not-closed error messages].
checkClosedInStaticForm :: Name -> TcM ()
checkClosedInStaticForm :: Name -> TcRn ()
checkClosedInStaticForm Name
name = do
    TcTypeEnv
type_env <- TcM TcTypeEnv
getLclTypeEnv
    case TcTypeEnv -> Name -> Maybe NotClosedReason
checkClosed TcTypeEnv
type_env Name
name of
      Maybe NotClosedReason
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
      Just NotClosedReason
reason -> TcRnMessage -> TcRn ()
addErrTc forall a b. (a -> b) -> a -> b
$ Name -> NotClosedReason -> TcRnMessage
explain Name
name NotClosedReason
reason
  where
    -- See Note [Checking closedness].
    checkClosed :: TcTypeEnv -> Name -> Maybe NotClosedReason
    checkClosed :: TcTypeEnv -> Name -> Maybe NotClosedReason
checkClosed TcTypeEnv
type_env Name
n = TcTypeEnv -> NameSet -> Name -> Maybe NotClosedReason
checkLoop TcTypeEnv
type_env (Name -> NameSet
unitNameSet Name
n) Name
n

    checkLoop :: TcTypeEnv -> NameSet -> Name -> Maybe NotClosedReason
    checkLoop :: TcTypeEnv -> NameSet -> Name -> Maybe NotClosedReason
checkLoop TcTypeEnv
type_env NameSet
visited Name
n =
      -- The @visited@ set is an accumulating parameter that contains the set of
      -- visited nodes, so we avoid repeating cycles in the traversal.
      case forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv TcTypeEnv
type_env Name
n of
        Just (ATcId { tct_id :: TcTyThing -> EvVar
tct_id = EvVar
tcid, tct_info :: TcTyThing -> IdBindingInfo
tct_info = IdBindingInfo
info }) -> case IdBindingInfo
info of
          IdBindingInfo
ClosedLet   -> forall a. Maybe a
Nothing
          IdBindingInfo
NotLetBound -> forall a. a -> Maybe a
Just NotClosedReason
NotLetBoundReason
          NonClosedLet NameSet
fvs Bool
type_closed -> forall a. [a] -> Maybe a
listToMaybe forall a b. (a -> b) -> a -> b
$
            -- Look for a non-closed variable in fvs
            [ Name -> NotClosedReason -> NotClosedReason
NotClosed Name
n' NotClosedReason
reason
            | Name
n' <- NameSet -> [Name]
nameSetElemsStable NameSet
fvs
            , Bool -> Bool
not (Name -> NameSet -> Bool
elemNameSet Name
n' NameSet
visited)
            , Just NotClosedReason
reason <- [TcTypeEnv -> NameSet -> Name -> Maybe NotClosedReason
checkLoop TcTypeEnv
type_env (NameSet -> Name -> NameSet
extendNameSet NameSet
visited Name
n') Name
n']
            ] forall a. [a] -> [a] -> [a]
++
            if Bool
type_closed then
              []
            else
              -- We consider non-let-bound variables easier to figure out than
              -- non-closed types, so we report non-closed types to the user
              -- only if we cannot spot the former.
              [ VarSet -> NotClosedReason
NotTypeClosed forall a b. (a -> b) -> a -> b
$ Type -> VarSet
tyCoVarsOfType (EvVar -> Type
idType EvVar
tcid) ]
        -- The binding is closed.
        Maybe TcTyThing
_ -> forall a. Maybe a
Nothing

    -- Converts a reason into a human-readable sentence.
    --
    -- @explain name reason@ starts with
    --
    -- "<name> is used in a static form but it is not closed because it"
    --
    -- and then follows a list of causes. For each id in the path, the text
    --
    -- "uses <id> which"
    --
    -- is appended, yielding something like
    --
    -- "uses <id> which uses <id1> which uses <id2> which"
    --
    -- until the end of the path is reached, which is reported as either
    --
    -- "is not let-bound"
    --
    -- when the final node is not let-bound, or
    --
    -- "has a non-closed type because it contains the type variables:
    -- v1, v2, v3"
    --
    -- when the final node has a non-closed type.
    --
    explain :: Name -> NotClosedReason -> TcRnMessage
    explain :: Name -> NotClosedReason -> TcRnMessage
explain = Name -> NotClosedReason -> TcRnMessage
TcRnStaticFormNotClosed

-- Note [Not-closed error messages]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
-- When variables in a static form are not closed, we go through the trouble
-- of explaining why they aren't.
--
-- Thus, the following program
--
-- > {-# LANGUAGE StaticPointers #-}
-- > module M where
-- >
-- > f x = static g
-- >   where
-- >     g = h
-- >     h = x
--
-- produces the error
--
--    'g' is used in a static form but it is not closed because it
--    uses 'h' which uses 'x' which is not let-bound.
--
-- And a program like
--
-- > {-# LANGUAGE StaticPointers #-}
-- > module M where
-- >
-- > import Data.Typeable
-- > import GHC.StaticPtr
-- >
-- > f :: Typeable a => a -> StaticPtr TypeRep
-- > f x = const (static (g undefined)) (h x)
-- >   where
-- >     g = h
-- >     h = typeOf
--
-- produces the error
--
--    'g' is used in a static form but it is not closed because it
--    uses 'h' which has a non-closed type because it contains the
--    type variables: 'a'
--

-- Note [Checking closedness]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~
--
-- @checkClosed@ checks if a binding is closed and returns a reason if it is
-- not.
--
-- The bindings define a graph where the nodes are ids, and there is an edge
-- from @id1@ to @id2@ if the rhs of @id1@ contains @id2@ among its free
-- variables.
--
-- When @n@ is not closed, it has to exist in the graph some node reachable
-- from @n@ that it is not a let-bound variable or that it has a non-closed
-- type. Thus, the "reason" is a path from @n@ to this offending node.
--
-- When @n@ is not closed, we traverse the graph reachable from @n@ to build
-- the reason.
--