{-# LANGUAGE CPP                 #-}
{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections       #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE UndecidableInstances #-} -- Wrinkle in Note [Trees That Grow]

{-# 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.Head
       ( HsExprArg(..), EValArg(..), TcPass(..), Rebuilder
       , splitHsApps
       , addArgWrap, eValArgExpr, isHsValArg, setSrcSpanFromArgs
       , countLeadingValArgs, isVisibleArg, pprHsExprArgTc, rebuildPrefixApps

       , tcInferAppHead, tcInferAppHead_maybe
       , tcInferId, tcCheckId
       , obviousSig, addAmbiguousNameErr
       , tyConOf, tyConOfET, lookupParents, fieldNotInType
       , notSelector, nonBidirectionalErr

       , addExprCtxt, addLExprCtxt, addFunResCtxt ) where

import {-# SOURCE #-} GHC.Tc.Gen.Expr( tcExpr, tcCheckMonoExprNC, tcCheckPolyExprNC )

import GHC.Tc.Gen.HsType
import GHC.Tc.Gen.Pat
import GHC.Tc.Gen.Bind( chooseInferredQuantifiers )
import GHC.Tc.Gen.Sig( tcUserTypeSig, tcInstSig )
import GHC.Tc.TyCl.PatSyn( patSynBuilderOcc )
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.Unify
import GHC.Types.Basic
import GHC.Tc.Utils.Instantiate
import GHC.Tc.Instance.Family ( tcGetFamInstEnvs, tcLookupDataFamInst )
import GHC.Core.FamInstEnv    ( FamInstEnvs )
import GHC.Core.UsageEnv      ( unitUE )
import GHC.Rename.Env         ( addUsedGRE )
import GHC.Rename.Utils       ( addNameClashErrRn, unknownSubordinateErr )
import GHC.Tc.Solver          ( InferMode(..), simplifyInfer )
import GHC.Tc.Utils.Env
import GHC.Tc.Utils.TcMType
import GHC.Tc.Types.Origin
import GHC.Tc.Utils.TcType as TcType
import GHC.Hs
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Core.ConLike
import GHC.Core.DataCon
import GHC.Types.Name
import GHC.Types.Name.Reader
import GHC.Core.TyCon
import GHC.Core.TyCo.Rep
import GHC.Core.Type
import GHC.Tc.Types.Evidence
import GHC.Builtin.Types( multiplicityTy )
import GHC.Builtin.Names
import GHC.Builtin.Names.TH( liftStringName, liftName )
import GHC.Driver.Session
import GHC.Types.SrcLoc
import GHC.Utils.Misc
import GHC.Data.Maybe
import GHC.Utils.Outputable as Outputable
import GHC.Utils.Panic
import Control.Monad

import Data.Function

#include "GhclibHsVersions.h"

import GHC.Prelude


{- *********************************************************************
*                                                                      *
              HsExprArg: auxiliary data type
*                                                                      *
********************************************************************* -}

{- Note [HsExprArg]
~~~~~~~~~~~~~~~~~~~
The data type HsExprArg :: TcPass -> Type
is a very local type, used only within this module and GHC.Tc.Gen.App

* It's really a zipper for an application chain
  See Note [Application chains and heads] in GHC.Tc.Gen.App for
  what an "application chain" is.

* It's a GHC-specific type, so using TTG only where necessary

* It is indexed by TcPass, meaning
  - HsExprArg TcpRn:
      The result of splitHsApps, which decomposes a HsExpr GhcRn

  - HsExprArg TcpInst:
      The result of tcInstFun, which instantiates the function type
      Adds EWrap nodes, the argument type in EValArg,
      and the kind-checked type in ETypeArg

  - HsExprArg TcpTc:
      The result of tcArg, which typechecks the value args
      In EValArg we now have a (LHsExpr GhcTc)

* rebuildPrefixApps is dual to splitHsApps, and zips an application
  back into a HsExpr

Note [EValArg]
~~~~~~~~~~~~~~
The data type EValArg is the payload of the EValArg constructor of
HsExprArg; i.e. a value argument of the application.  EValArg has two
forms:

* ValArg: payload is just the expression itself. Simple.

* ValArgQL: captures the results of applying quickLookArg to the
  argument in a ValArg.  When we later want to typecheck that argument
  we can just carry on from where quick-look left off.  The fields of
  ValArgQL exactly capture what is needed to complete the job.

Invariants:

1. With QL switched off, all arguments are ValArg; no ValArgQL

2. With QL switched on, tcInstFun converts some ValArgs to ValArgQL,
   under the conditions when quick-look should happen (eg the argument
   type is guarded) -- see quickLookArg

Note [splitHsApps and Rebuilder]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The key function
  splitHsApps :: HsExpr GhcRn -> (HsExpr GhcRn, [HsExprArg 'TcpRn], Rebuilder)
takes apart either an HsApp, or an infix OpApp, returning

* The "head" of the application, an expression that is often a variable

* A list of HsExprArg, the arguments

* A Rebuilder function which reconstructs the original form, given the
  head and arguments.  This allows us to reconstruct infix
  applications (OpApp) as well as prefix applications (HsApp),
  thereby retaining the structure of the original tree.
-}

data TcPass = TcpRn     -- Arguments decomposed
            | TcpInst   -- Function instantiated
            | TcpTc     -- Typechecked

data HsExprArg (p :: TcPass)
  = -- See Note [HsExprArg]
    EValArg  { HsExprArg p -> SrcSpan
eva_loc    :: SrcSpan        -- Of the function
             , HsExprArg p -> EValArg p
eva_arg    :: EValArg p
             , HsExprArg p -> XEVAType p
eva_arg_ty :: !(XEVAType p) }

  | ETypeArg { eva_loc   :: SrcSpan          -- Of the function
             , HsExprArg p -> LHsWcType GhcRn
eva_hs_ty :: LHsWcType GhcRn  -- The type arg
             , HsExprArg p -> XETAType p
eva_ty    :: !(XETAType p) }  -- Kind-checked type arg

  | EPrag    SrcSpan
             (HsPragE (GhcPass (XPass p)))

  | EPar     SrcSpan         -- Of the nested expr

  | EWrap    !(XEWrap p)     -- Wrapper, after instantiation

data EValArg (p :: TcPass) where  -- See Note [EValArg]
  ValArg   :: LHsExpr (GhcPass (XPass p))
           -> EValArg p
  ValArgQL :: { EValArg 'TcpInst -> LHsExpr GhcRn
va_expr :: LHsExpr GhcRn        -- Original expression
                                                -- For location and error msgs
              , EValArg 'TcpInst -> HsExpr GhcTc
va_fun  :: HsExpr GhcTc         -- Function, typechecked
              , EValArg 'TcpInst -> [HsExprArg 'TcpInst]
va_args :: [HsExprArg 'TcpInst] -- Args, instantiated
              , EValArg 'TcpInst -> TcRhoType
va_ty   :: TcRhoType            -- Result type
              , EValArg 'TcpInst -> Rebuilder
va_rebuild :: Rebuilder }       -- How to reassemble
           -> EValArg 'TcpInst  -- Only exists in TcpInst phase

type Rebuilder = HsExpr GhcTc -> [HsExprArg 'TcpTc]-> HsExpr GhcTc
-- See Note [splitHsApps and Rebuilder]

type family XPass p where
  XPass 'TcpRn   = 'Renamed
  XPass 'TcpInst = 'Renamed
  XPass 'TcpTc   = 'Typechecked

type family XETAType p where  -- Type arguments
  XETAType 'TcpRn = NoExtField
  XETAType _      = Type

type family XEVAType p where  -- Value arguments
  XEVAType 'TcpRn = NoExtField
  XEVAType _      = Scaled Type

type family XEWrap p where
  XEWrap 'TcpRn = NoExtCon
  XEWrap _      = HsWrapper

mkEValArg :: SrcSpan -> LHsExpr GhcRn -> HsExprArg 'TcpRn
mkEValArg :: SrcSpan -> LHsExpr GhcRn -> HsExprArg 'TcpRn
mkEValArg SrcSpan
l LHsExpr GhcRn
e = EValArg :: forall (p :: TcPass).
SrcSpan -> EValArg p -> XEVAType p -> HsExprArg p
EValArg { eva_loc :: SrcSpan
eva_loc = SrcSpan
l, eva_arg :: EValArg 'TcpRn
eva_arg = LHsExpr (GhcPass (XPass 'TcpRn)) -> EValArg 'TcpRn
forall (p :: TcPass). LHsExpr (GhcPass (XPass p)) -> EValArg p
ValArg LHsExpr GhcRn
LHsExpr (GhcPass (XPass 'TcpRn))
e
                        , eva_arg_ty :: XEVAType 'TcpRn
eva_arg_ty = NoExtField
XEVAType 'TcpRn
noExtField }

mkETypeArg :: SrcSpan -> LHsWcType GhcRn -> HsExprArg 'TcpRn
mkETypeArg :: SrcSpan -> LHsWcType GhcRn -> HsExprArg 'TcpRn
mkETypeArg SrcSpan
l LHsWcType GhcRn
hs_ty = ETypeArg :: forall (p :: TcPass).
SrcSpan -> LHsWcType GhcRn -> XETAType p -> HsExprArg p
ETypeArg { eva_loc :: SrcSpan
eva_loc = SrcSpan
l, eva_hs_ty :: LHsWcType GhcRn
eva_hs_ty = LHsWcType GhcRn
hs_ty
                              , eva_ty :: XETAType 'TcpRn
eva_ty = NoExtField
XETAType 'TcpRn
noExtField }

eValArgExpr :: EValArg 'TcpInst -> LHsExpr GhcRn
eValArgExpr :: EValArg 'TcpInst -> LHsExpr GhcRn
eValArgExpr (ValArg LHsExpr (GhcPass (XPass 'TcpInst))
e)                 = LHsExpr GhcRn
LHsExpr (GhcPass (XPass 'TcpInst))
e
eValArgExpr (ValArgQL { va_expr :: EValArg 'TcpInst -> LHsExpr GhcRn
va_expr = LHsExpr GhcRn
e }) = LHsExpr GhcRn
e

addArgWrap :: HsWrapper -> [HsExprArg 'TcpInst] -> [HsExprArg 'TcpInst]
addArgWrap :: HsWrapper -> [HsExprArg 'TcpInst] -> [HsExprArg 'TcpInst]
addArgWrap HsWrapper
wrap [HsExprArg 'TcpInst]
args
 | HsWrapper -> Bool
isIdHsWrapper HsWrapper
wrap = [HsExprArg 'TcpInst]
args
 | Bool
otherwise          = XEWrap 'TcpInst -> HsExprArg 'TcpInst
forall (p :: TcPass). XEWrap p -> HsExprArg p
EWrap HsWrapper
XEWrap 'TcpInst
wrap HsExprArg 'TcpInst -> [HsExprArg 'TcpInst] -> [HsExprArg 'TcpInst]
forall a. a -> [a] -> [a]
: [HsExprArg 'TcpInst]
args

splitHsApps :: HsExpr GhcRn -> (HsExpr GhcRn, [HsExprArg 'TcpRn], Rebuilder)
-- See Note [splitHsApps and Rebuilder]
splitHsApps :: HsExpr GhcRn -> (HsExpr GhcRn, [HsExprArg 'TcpRn], Rebuilder)
splitHsApps HsExpr GhcRn
e
  = HsExpr GhcRn
-> [HsExprArg 'TcpRn]
-> (HsExpr GhcRn, [HsExprArg 'TcpRn], Rebuilder)
go HsExpr GhcRn
e []
  where
    go :: HsExpr GhcRn
-> [HsExprArg 'TcpRn]
-> (HsExpr GhcRn, [HsExprArg 'TcpRn], Rebuilder)
go (HsPar XPar GhcRn
_     (L l fun))       [HsExprArg 'TcpRn]
args = HsExpr GhcRn
-> [HsExprArg 'TcpRn]
-> (HsExpr GhcRn, [HsExprArg 'TcpRn], Rebuilder)
go HsExpr GhcRn
fun (SrcSpan -> HsExprArg 'TcpRn
forall (p :: TcPass). SrcSpan -> HsExprArg p
EPar       SrcSpan
l       HsExprArg 'TcpRn -> [HsExprArg 'TcpRn] -> [HsExprArg 'TcpRn]
forall a. a -> [a] -> [a]
: [HsExprArg 'TcpRn]
args)
    go (HsPragE XPragE GhcRn
_ HsPragE GhcRn
p (L l fun))       [HsExprArg 'TcpRn]
args = HsExpr GhcRn
-> [HsExprArg 'TcpRn]
-> (HsExpr GhcRn, [HsExprArg 'TcpRn], Rebuilder)
go HsExpr GhcRn
fun (SrcSpan -> HsPragE (GhcPass (XPass 'TcpRn)) -> HsExprArg 'TcpRn
forall (p :: TcPass).
SrcSpan -> HsPragE (GhcPass (XPass p)) -> HsExprArg p
EPrag      SrcSpan
l HsPragE GhcRn
HsPragE (GhcPass (XPass 'TcpRn))
p     HsExprArg 'TcpRn -> [HsExprArg 'TcpRn] -> [HsExprArg 'TcpRn]
forall a. a -> [a] -> [a]
: [HsExprArg 'TcpRn]
args)
    go (HsAppType XAppTypeE GhcRn
_ (L l fun) LHsWcType (NoGhcTc GhcRn)
hs_ty) [HsExprArg 'TcpRn]
args = HsExpr GhcRn
-> [HsExprArg 'TcpRn]
-> (HsExpr GhcRn, [HsExprArg 'TcpRn], Rebuilder)
go HsExpr GhcRn
fun (SrcSpan -> LHsWcType GhcRn -> HsExprArg 'TcpRn
mkETypeArg SrcSpan
l LHsWcType GhcRn
LHsWcType (NoGhcTc GhcRn)
hs_ty HsExprArg 'TcpRn -> [HsExprArg 'TcpRn] -> [HsExprArg 'TcpRn]
forall a. a -> [a] -> [a]
: [HsExprArg 'TcpRn]
args)
    go (HsApp XApp GhcRn
_     (L l fun) LHsExpr GhcRn
arg)   [HsExprArg 'TcpRn]
args = HsExpr GhcRn
-> [HsExprArg 'TcpRn]
-> (HsExpr GhcRn, [HsExprArg 'TcpRn], Rebuilder)
go HsExpr GhcRn
fun (SrcSpan -> LHsExpr GhcRn -> HsExprArg 'TcpRn
mkEValArg  SrcSpan
l LHsExpr GhcRn
arg   HsExprArg 'TcpRn -> [HsExprArg 'TcpRn] -> [HsExprArg 'TcpRn]
forall a. a -> [a] -> [a]
: [HsExprArg 'TcpRn]
args)

    go (OpApp XOpApp GhcRn
fix LHsExpr GhcRn
arg1 (L l op) LHsExpr GhcRn
arg2) [HsExprArg 'TcpRn]
args
      = (HsExpr GhcRn
op, SrcSpan -> LHsExpr GhcRn -> HsExprArg 'TcpRn
mkEValArg SrcSpan
l LHsExpr GhcRn
arg1 HsExprArg 'TcpRn -> [HsExprArg 'TcpRn] -> [HsExprArg 'TcpRn]
forall a. a -> [a] -> [a]
: SrcSpan -> LHsExpr GhcRn -> HsExprArg 'TcpRn
mkEValArg SrcSpan
l LHsExpr GhcRn
arg2 HsExprArg 'TcpRn -> [HsExprArg 'TcpRn] -> [HsExprArg 'TcpRn]
forall a. a -> [a] -> [a]
: [HsExprArg 'TcpRn]
args, Fixity -> Rebuilder
rebuild_infix XOpApp GhcRn
Fixity
fix)

    go HsExpr GhcRn
e [HsExprArg 'TcpRn]
args = (HsExpr GhcRn
e, [HsExprArg 'TcpRn]
args, Rebuilder
rebuildPrefixApps)

    rebuild_infix :: Fixity -> Rebuilder
    rebuild_infix :: Fixity -> Rebuilder
rebuild_infix Fixity
fix HsExpr GhcTc
fun [HsExprArg 'TcpTc]
args
      = Rebuilder
go HsExpr GhcTc
fun [HsExprArg 'TcpTc]
args
      where
        go :: Rebuilder
go HsExpr GhcTc
fun (EValArg { eva_arg :: forall (p :: TcPass). HsExprArg p -> EValArg p
eva_arg = ValArg LHsExpr (GhcPass (XPass 'TcpTc))
arg1, eva_loc :: forall (p :: TcPass). HsExprArg p -> SrcSpan
eva_loc = SrcSpan
l } :
                EValArg { eva_arg :: forall (p :: TcPass). HsExprArg p -> EValArg p
eva_arg = ValArg LHsExpr (GhcPass (XPass 'TcpTc))
arg2 } : [HsExprArg 'TcpTc]
args)
                                   = Rebuilder
rebuildPrefixApps (XOpApp GhcTc
-> LHsExpr GhcTc -> LHsExpr GhcTc -> LHsExpr GhcTc -> HsExpr GhcTc
forall p.
XOpApp p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
OpApp XOpApp GhcTc
Fixity
fix LHsExpr GhcTc
LHsExpr (GhcPass (XPass 'TcpTc))
arg1 (SrcSpan -> HsExpr GhcTc -> GenLocated SrcSpan (HsExpr GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpan
l HsExpr GhcTc
fun) LHsExpr GhcTc
LHsExpr (GhcPass (XPass 'TcpTc))
arg2) [HsExprArg 'TcpTc]
args
        go HsExpr GhcTc
fun (EWrap XEWrap 'TcpTc
wrap : [HsExprArg 'TcpTc]
args) = Rebuilder
go (HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
XEWrap 'TcpTc
wrap HsExpr GhcTc
fun) [HsExprArg 'TcpTc]
args
        go HsExpr GhcTc
fun [HsExprArg 'TcpTc]
args                = Rebuilder
rebuildPrefixApps HsExpr GhcTc
fun [HsExprArg 'TcpTc]
args
           -- This last case fails to rebuild a OpApp, which is sad.
           -- It can happen if we have (e1 `op` e2),
           -- and op :: Int -> forall a. a -> Int, and e2 :: Bool
           -- Then we'll get   [ e1, @Bool, e2 ]
           -- Could be fixed with WpFun, but extra complexity.

rebuildPrefixApps :: Rebuilder
rebuildPrefixApps :: Rebuilder
rebuildPrefixApps HsExpr GhcTc
fun [HsExprArg 'TcpTc]
args
  = Rebuilder
forall (p :: TcPass).
(XEWrap p ~ HsWrapper, XPass p ~ 'Typechecked,
 XETAType p ~ TcRhoType) =>
HsExpr GhcTc -> [HsExprArg p] -> HsExpr GhcTc
go HsExpr GhcTc
fun [HsExprArg 'TcpTc]
args
  where
    go :: HsExpr GhcTc -> [HsExprArg p] -> HsExpr GhcTc
go HsExpr GhcTc
fun [] = HsExpr GhcTc
fun
    go HsExpr GhcTc
fun (EWrap XEWrap p
wrap : [HsExprArg p]
args)               = HsExpr GhcTc -> [HsExprArg p] -> HsExpr GhcTc
go (HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
XEWrap p
wrap HsExpr GhcTc
fun) [HsExprArg p]
args
    go HsExpr GhcTc
fun (EValArg { eva_arg :: forall (p :: TcPass). HsExprArg p -> EValArg p
eva_arg = ValArg LHsExpr (GhcPass (XPass p))
arg
                    , eva_loc :: forall (p :: TcPass). HsExprArg p -> SrcSpan
eva_loc = SrcSpan
l } : [HsExprArg p]
args)  = HsExpr GhcTc -> [HsExprArg p] -> HsExpr GhcTc
go (XApp GhcTc -> LHsExpr GhcTc -> LHsExpr GhcTc -> HsExpr GhcTc
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp NoExtField
XApp GhcTc
noExtField (SrcSpan -> HsExpr GhcTc -> GenLocated SrcSpan (HsExpr GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpan
l HsExpr GhcTc
fun) LHsExpr GhcTc
LHsExpr (GhcPass (XPass p))
arg) [HsExprArg p]
args
    go HsExpr GhcTc
fun (ETypeArg { eva_hs_ty :: forall (p :: TcPass). HsExprArg p -> LHsWcType GhcRn
eva_hs_ty = LHsWcType GhcRn
hs_ty
                     , eva_ty :: forall (p :: TcPass). HsExprArg p -> XETAType p
eva_ty  = XETAType p
ty
                     , eva_loc :: forall (p :: TcPass). HsExprArg p -> SrcSpan
eva_loc = SrcSpan
l } : [HsExprArg p]
args) = HsExpr GhcTc -> [HsExprArg p] -> HsExpr GhcTc
go (XAppTypeE GhcTc
-> LHsExpr GhcTc -> LHsWcType (NoGhcTc GhcTc) -> HsExpr GhcTc
forall p.
XAppTypeE p -> LHsExpr p -> LHsWcType (NoGhcTc p) -> HsExpr p
HsAppType XAppTypeE GhcTc
XETAType p
ty (SrcSpan -> HsExpr GhcTc -> GenLocated SrcSpan (HsExpr GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpan
l HsExpr GhcTc
fun) LHsWcType GhcRn
LHsWcType (NoGhcTc GhcTc)
hs_ty) [HsExprArg p]
args
    go HsExpr GhcTc
fun (EPar SrcSpan
l : [HsExprArg p]
args)                   = HsExpr GhcTc -> [HsExprArg p] -> HsExpr GhcTc
go (XPar GhcTc -> LHsExpr GhcTc -> HsExpr GhcTc
forall p. XPar p -> LHsExpr p -> HsExpr p
HsPar NoExtField
XPar GhcTc
noExtField (SrcSpan -> HsExpr GhcTc -> GenLocated SrcSpan (HsExpr GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpan
l HsExpr GhcTc
fun)) [HsExprArg p]
args
    go HsExpr GhcTc
fun (EPrag SrcSpan
l HsPragE (GhcPass (XPass p))
p : [HsExprArg p]
args)                = HsExpr GhcTc -> [HsExprArg p] -> HsExpr GhcTc
go (XPragE GhcTc -> HsPragE GhcTc -> LHsExpr GhcTc -> HsExpr GhcTc
forall p. XPragE p -> HsPragE p -> LHsExpr p -> HsExpr p
HsPragE NoExtField
XPragE GhcTc
noExtField HsPragE GhcTc
HsPragE (GhcPass (XPass p))
p (SrcSpan -> HsExpr GhcTc -> GenLocated SrcSpan (HsExpr GhcTc)
forall l e. l -> e -> GenLocated l e
L SrcSpan
l HsExpr GhcTc
fun)) [HsExprArg p]
args

isHsValArg :: HsExprArg id -> Bool
isHsValArg :: HsExprArg id -> Bool
isHsValArg (EValArg {}) = Bool
True
isHsValArg HsExprArg id
_            = Bool
False

countLeadingValArgs :: [HsExprArg id] -> Int
countLeadingValArgs :: [HsExprArg id] -> Int
countLeadingValArgs (EValArg {} : [HsExprArg id]
args) = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [HsExprArg id] -> Int
forall (id :: TcPass). [HsExprArg id] -> Int
countLeadingValArgs [HsExprArg id]
args
countLeadingValArgs (EPar {}    : [HsExprArg id]
args) = [HsExprArg id] -> Int
forall (id :: TcPass). [HsExprArg id] -> Int
countLeadingValArgs [HsExprArg id]
args
countLeadingValArgs (EPrag {}   : [HsExprArg id]
args) = [HsExprArg id] -> Int
forall (id :: TcPass). [HsExprArg id] -> Int
countLeadingValArgs [HsExprArg id]
args
countLeadingValArgs [HsExprArg id]
_                   = Int
0

isValArg :: HsExprArg id -> Bool
isValArg :: HsExprArg id -> Bool
isValArg (EValArg {}) = Bool
True
isValArg HsExprArg id
_            = Bool
False

isVisibleArg :: HsExprArg id -> Bool
isVisibleArg :: HsExprArg id -> Bool
isVisibleArg (EValArg {})  = Bool
True
isVisibleArg (ETypeArg {}) = Bool
True
isVisibleArg HsExprArg id
_             = Bool
False

setSrcSpanFromArgs :: [HsExprArg 'TcpRn] -> TcM a -> TcM a
setSrcSpanFromArgs :: [HsExprArg 'TcpRn] -> TcM a -> TcM a
setSrcSpanFromArgs [] TcM a
thing_inside
  = TcM a
thing_inside
setSrcSpanFromArgs (HsExprArg 'TcpRn
arg:[HsExprArg 'TcpRn]
_) TcM a
thing_inside
  = SrcSpan -> TcM a -> TcM a
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan (HsExprArg 'TcpRn -> SrcSpan
argFunLoc HsExprArg 'TcpRn
arg) TcM a
thing_inside

argFunLoc :: HsExprArg 'TcpRn -> SrcSpan
argFunLoc :: HsExprArg 'TcpRn -> SrcSpan
argFunLoc (EValArg { eva_loc :: forall (p :: TcPass). HsExprArg p -> SrcSpan
eva_loc = SrcSpan
l }) = SrcSpan
l
argFunLoc (ETypeArg { eva_loc :: forall (p :: TcPass). HsExprArg p -> SrcSpan
eva_loc = SrcSpan
l}) = SrcSpan
l
argFunLoc (EPrag SrcSpan
l HsPragE (GhcPass (XPass 'TcpRn))
_)               = SrcSpan
l
argFunLoc (EPar SrcSpan
l)                  = SrcSpan
l

instance OutputableBndrId (XPass p) => Outputable (HsExprArg p) where
  ppr :: HsExprArg p -> SDoc
ppr (EValArg { eva_arg :: forall (p :: TcPass). HsExprArg p -> EValArg p
eva_arg = EValArg p
arg })      = String -> SDoc
text String
"EValArg" SDoc -> SDoc -> SDoc
<+> EValArg p -> SDoc
forall a. Outputable a => a -> SDoc
ppr EValArg p
arg
  ppr (EPrag SrcSpan
_ HsPragE (GhcPass (XPass p))
p)                      = String -> SDoc
text String
"EPrag" SDoc -> SDoc -> SDoc
<+> HsPragE (GhcPass (XPass p)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsPragE (GhcPass (XPass p))
p
  ppr (ETypeArg { eva_hs_ty :: forall (p :: TcPass). HsExprArg p -> LHsWcType GhcRn
eva_hs_ty = LHsWcType GhcRn
hs_ty }) = Char -> SDoc
char Char
'@' SDoc -> SDoc -> SDoc
<> HsWildCardBndrs GhcRn (Located (HsType GhcRn)) -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsWildCardBndrs GhcRn (Located (HsType GhcRn))
LHsWcType GhcRn
hs_ty
  ppr (EPar SrcSpan
_)                         = String -> SDoc
text String
"EPar"
  ppr (EWrap XEWrap p
_)                        = String -> SDoc
text String
"EWrap"
  -- ToDo: to print the wrapper properly we'll need to work harder
  -- "Work harder" = replicate the ghcPass approach, but I didn't
  -- think it was worth the effort to do so.

instance OutputableBndrId (XPass p) => Outputable (EValArg p) where
  ppr :: EValArg p -> SDoc
ppr (ValArg LHsExpr (GhcPass (XPass p))
e) = Located (HsExpr (GhcPass (XPass p))) -> SDoc
forall a. Outputable a => a -> SDoc
ppr Located (HsExpr (GhcPass (XPass p)))
LHsExpr (GhcPass (XPass p))
e
  ppr (ValArgQL { va_fun :: EValArg 'TcpInst -> HsExpr GhcTc
va_fun = HsExpr GhcTc
fun, va_args :: EValArg 'TcpInst -> [HsExprArg 'TcpInst]
va_args = [HsExprArg 'TcpInst]
args, va_ty :: EValArg 'TcpInst -> TcRhoType
va_ty = TcRhoType
ty})
    = SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"ValArgQL" SDoc -> SDoc -> SDoc
<+> HsExpr GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsExpr GhcTc
fun)
         Int
2 ([SDoc] -> SDoc
vcat [ [HsExprArg 'TcpInst] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [HsExprArg 'TcpInst]
args, String -> SDoc
text String
"va_ty:" SDoc -> SDoc -> SDoc
<+> TcRhoType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcRhoType
ty ])

pprHsExprArgTc :: HsExprArg 'TcpInst -> SDoc
pprHsExprArgTc :: HsExprArg 'TcpInst -> SDoc
pprHsExprArgTc (EValArg { eva_arg :: forall (p :: TcPass). HsExprArg p -> EValArg p
eva_arg = EValArg 'TcpInst
tm, eva_arg_ty :: forall (p :: TcPass). HsExprArg p -> XEVAType p
eva_arg_ty = XEVAType 'TcpInst
ty })
  = String -> SDoc
text String
"EValArg" SDoc -> SDoc -> SDoc
<+> SDoc -> Int -> SDoc -> SDoc
hang (EValArg 'TcpInst -> SDoc
forall a. Outputable a => a -> SDoc
ppr EValArg 'TcpInst
tm) Int
2 (SDoc
dcolon SDoc -> SDoc -> SDoc
<+> Scaled TcRhoType -> SDoc
forall a. Outputable a => a -> SDoc
ppr Scaled TcRhoType
XEVAType 'TcpInst
ty)
pprHsExprArgTc HsExprArg 'TcpInst
arg = HsExprArg 'TcpInst -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsExprArg 'TcpInst
arg


{- *********************************************************************
*                                                                      *
                 tcInferAppHead
*                                                                      *
********************************************************************* -}

tcInferAppHead :: HsExpr GhcRn
               -> [HsExprArg 'TcpRn] -> Maybe TcRhoType
               -- These two args are solely for tcInferRecSelId
               -> TcM (HsExpr GhcTc, TcSigmaType)
-- Infer type of the head of an application
--   i.e. the 'f' in (f e1 ... en)
-- See Note [Application chains and heads] in GHC.Tc.Gen.App
-- We get back a /SigmaType/ because we have special cases for
--   * A bare identifier (just look it up)
--     This case also covers a record selector HsRecFld
--   * An expression with a type signature (e :: ty)
-- See Note [Application chains and heads] in GHC.Tc.Gen.App
--
-- Why do we need the arguments to infer the type of the head of
-- the application?  For two reasons:
--   * (Legitimate) The first arg has the source location of the head
--   * (Disgusting) Needed for record disambiguation; see tcInferRecSelId
--
-- Note that [] and (,,) are both HsVar:
--   see Note [Empty lists] and [ExplicitTuple] in GHC.Hs.Expr
--
-- NB: 'e' cannot be HsApp, HsTyApp, HsPrag, HsPar, because those
--     cases are dealt with by splitHsApps.
--
-- See Note [tcApp: typechecking applications] in GHC.Tc.Gen.App
tcInferAppHead :: HsExpr GhcRn
-> [HsExprArg 'TcpRn]
-> Maybe TcRhoType
-> TcM (HsExpr GhcTc, TcRhoType)
tcInferAppHead HsExpr GhcRn
fun [HsExprArg 'TcpRn]
args Maybe TcRhoType
mb_res_ty
  = [HsExprArg 'TcpRn]
-> TcM (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType)
forall a. [HsExprArg 'TcpRn] -> TcM a -> TcM a
setSrcSpanFromArgs [HsExprArg 'TcpRn]
args (TcM (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType))
-> TcM (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType)
forall a b. (a -> b) -> a -> b
$
    do { Maybe (HsExpr GhcTc, TcRhoType)
mb_tc_fun <- HsExpr GhcRn
-> [HsExprArg 'TcpRn]
-> Maybe TcRhoType
-> TcM (Maybe (HsExpr GhcTc, TcRhoType))
tcInferAppHead_maybe HsExpr GhcRn
fun [HsExprArg 'TcpRn]
args Maybe TcRhoType
mb_res_ty
       ; case Maybe (HsExpr GhcTc, TcRhoType)
mb_tc_fun of
            Just (HsExpr GhcTc
fun', TcRhoType
fun_sigma) -> (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsExpr GhcTc
fun', TcRhoType
fun_sigma)
            Maybe (HsExpr GhcTc, TcRhoType)
Nothing -> HsExpr GhcRn
-> [HsExprArg 'TcpRn]
-> TcM (HsExpr GhcTc, TcRhoType)
-> TcM (HsExpr GhcTc, TcRhoType)
forall a. HsExpr GhcRn -> [HsExprArg 'TcpRn] -> TcM a -> TcM a
add_head_ctxt HsExpr GhcRn
fun [HsExprArg 'TcpRn]
args (TcM (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType))
-> TcM (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType)
forall a b. (a -> b) -> a -> b
$
                       (ExpSigmaType -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc, TcRhoType)
forall a. (ExpSigmaType -> TcM a) -> TcM (a, TcRhoType)
tcInfer (HsExpr GhcRn -> ExpSigmaType -> TcM (HsExpr GhcTc)
tcExpr HsExpr GhcRn
fun) }

tcInferAppHead_maybe :: HsExpr GhcRn
                     -> [HsExprArg 'TcpRn] -> Maybe TcRhoType
                        -- These two args are solely for tcInferRecSelId
                     -> TcM (Maybe (HsExpr GhcTc, TcSigmaType))
-- See Note [Application chains and heads] in GHC.Tc.Gen.App
-- Returns Nothing for a complicated head
tcInferAppHead_maybe :: HsExpr GhcRn
-> [HsExprArg 'TcpRn]
-> Maybe TcRhoType
-> TcM (Maybe (HsExpr GhcTc, TcRhoType))
tcInferAppHead_maybe HsExpr GhcRn
fun [HsExprArg 'TcpRn]
args Maybe TcRhoType
mb_res_ty
  = case HsExpr GhcRn
fun of
      HsVar XVar GhcRn
_ (L _ nm)          -> (HsExpr GhcTc, TcRhoType) -> Maybe (HsExpr GhcTc, TcRhoType)
forall a. a -> Maybe a
Just ((HsExpr GhcTc, TcRhoType) -> Maybe (HsExpr GhcTc, TcRhoType))
-> TcM (HsExpr GhcTc, TcRhoType)
-> TcM (Maybe (HsExpr GhcTc, TcRhoType))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Name -> TcM (HsExpr GhcTc, TcRhoType)
tcInferId Name
nm
      HsRecFld XRecFld GhcRn
_ AmbiguousFieldOcc GhcRn
f              -> (HsExpr GhcTc, TcRhoType) -> Maybe (HsExpr GhcTc, TcRhoType)
forall a. a -> Maybe a
Just ((HsExpr GhcTc, TcRhoType) -> Maybe (HsExpr GhcTc, TcRhoType))
-> TcM (HsExpr GhcTc, TcRhoType)
-> TcM (Maybe (HsExpr GhcTc, TcRhoType))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AmbiguousFieldOcc GhcRn
-> [HsExprArg 'TcpRn]
-> Maybe TcRhoType
-> TcM (HsExpr GhcTc, TcRhoType)
tcInferRecSelId AmbiguousFieldOcc GhcRn
f [HsExprArg 'TcpRn]
args Maybe TcRhoType
mb_res_ty
      ExprWithTySig XExprWithTySig GhcRn
_ LHsExpr GhcRn
e LHsSigWcType (NoGhcTc GhcRn)
hs_ty   -> HsExpr GhcRn
-> [HsExprArg 'TcpRn]
-> TcM (Maybe (HsExpr GhcTc, TcRhoType))
-> TcM (Maybe (HsExpr GhcTc, TcRhoType))
forall a. HsExpr GhcRn -> [HsExprArg 'TcpRn] -> TcM a -> TcM a
add_head_ctxt HsExpr GhcRn
fun [HsExprArg 'TcpRn]
args (TcM (Maybe (HsExpr GhcTc, TcRhoType))
 -> TcM (Maybe (HsExpr GhcTc, TcRhoType)))
-> TcM (Maybe (HsExpr GhcTc, TcRhoType))
-> TcM (Maybe (HsExpr GhcTc, TcRhoType))
forall a b. (a -> b) -> a -> b
$
                                   (HsExpr GhcTc, TcRhoType) -> Maybe (HsExpr GhcTc, TcRhoType)
forall a. a -> Maybe a
Just ((HsExpr GhcTc, TcRhoType) -> Maybe (HsExpr GhcTc, TcRhoType))
-> TcM (HsExpr GhcTc, TcRhoType)
-> TcM (Maybe (HsExpr GhcTc, TcRhoType))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LHsExpr GhcRn
-> LHsSigWcType (NoGhcTc GhcRn) -> TcM (HsExpr GhcTc, TcRhoType)
tcExprWithSig LHsExpr GhcRn
e LHsSigWcType (NoGhcTc GhcRn)
hs_ty
      HsExpr GhcRn
_                         -> Maybe (HsExpr GhcTc, TcRhoType)
-> TcM (Maybe (HsExpr GhcTc, TcRhoType))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (HsExpr GhcTc, TcRhoType)
forall a. Maybe a
Nothing

add_head_ctxt :: HsExpr GhcRn -> [HsExprArg 'TcpRn] -> TcM a -> TcM a
-- Don't push an expression context if the arguments are empty,
-- because it has already been pushed by tcExpr
add_head_ctxt :: HsExpr GhcRn -> [HsExprArg 'TcpRn] -> TcM a -> TcM a
add_head_ctxt HsExpr GhcRn
fun [HsExprArg 'TcpRn]
args TcM a
thing_inside
  | [HsExprArg 'TcpRn] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [HsExprArg 'TcpRn]
args = TcM a
thing_inside
  | Bool
otherwise = HsExpr GhcRn -> TcM a -> TcM a
forall a. HsExpr GhcRn -> TcRn a -> TcRn a
addExprCtxt HsExpr GhcRn
fun TcM a
thing_inside


{- *********************************************************************
*                                                                      *
                 Record selectors
*                                                                      *
********************************************************************* -}

{- Note [Disambiguating record fields]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When the -XDuplicateRecordFields extension is used, and the renamer
encounters a record selector or update that it cannot immediately
disambiguate (because it involves fields that belong to multiple
datatypes), it will defer resolution of the ambiguity to the
typechecker.  In this case, the `Ambiguous` constructor of
`AmbiguousFieldOcc` is used.

Consider the following definitions:

        data S = MkS { foo :: Int }
        data T = MkT { foo :: Int, bar :: Int }
        data U = MkU { bar :: Int, baz :: Int }

When the renamer sees `foo` as a selector or an update, it will not
know which parent datatype is in use.

For selectors, there are two possible ways to disambiguate:

1. Check if the pushed-in type is a function whose domain is a
   datatype, for example:

       f s = (foo :: S -> Int) s

       g :: T -> Int
       g = foo

    This is checked by `tcCheckRecSelId` when checking `HsRecFld foo`.

2. Check if the selector is applied to an argument that has a type
   signature, for example:

       h = foo (s :: S)

    This is checked by `tcInferRecSelId`.


Updates are slightly more complex.  The `disambiguateRecordBinds`
function tries to determine the parent datatype in three ways:

1. Check for types that have all the fields being updated. For example:

        f x = x { foo = 3, bar = 2 }

   Here `f` must be updating `T` because neither `S` nor `U` have
   both fields. This may also discover that no possible type exists.
   For example the following will be rejected:

        f' x = x { foo = 3, baz = 3 }

2. Use the type being pushed in, if it is already a TyConApp. The
   following are valid updates to `T`:

        g :: T -> T
        g x = x { foo = 3 }

        g' x = x { foo = 3 } :: T

3. Use the type signature of the record expression, if it exists and
   is a TyConApp. Thus this is valid update to `T`:

        h x = (x :: T) { foo = 3 }


Note that we do not look up the types of variables being updated, and
no constraint-solving is performed, so for example the following will
be rejected as ambiguous:

     let bad (s :: S) = foo s

     let r :: T
         r = blah
     in r { foo = 3 }

     \r. (r { foo = 3 },  r :: T )

We could add further tests, of a more heuristic nature. For example,
rather than looking for an explicit signature, we could try to infer
the type of the argument to a selector or the record expression being
updated, in case we are lucky enough to get a TyConApp straight
away. However, it might be hard for programmers to predict whether a
particular update is sufficiently obvious for the signature to be
omitted. Moreover, this might change the behaviour of typechecker in
non-obvious ways.

See also Note [HsRecField and HsRecUpdField] in GHC.Hs.Pat.
-}

tcInferRecSelId :: AmbiguousFieldOcc GhcRn
                -> [HsExprArg 'TcpRn] -> Maybe TcRhoType
                -> TcM (HsExpr GhcTc, TcSigmaType)
tcInferRecSelId :: AmbiguousFieldOcc GhcRn
-> [HsExprArg 'TcpRn]
-> Maybe TcRhoType
-> TcM (HsExpr GhcTc, TcRhoType)
tcInferRecSelId (Unambiguous XUnambiguous GhcRn
sel_name Located RdrName
lbl) [HsExprArg 'TcpRn]
_args Maybe TcRhoType
_mb_res_ty
   = do { TcId
sel_id <- Located RdrName -> Name -> TcM TcId
tc_rec_sel_id Located RdrName
lbl Name
XUnambiguous GhcRn
sel_name
        ; let expr :: HsExpr GhcTc
expr = XRecFld GhcTc -> AmbiguousFieldOcc GhcTc -> HsExpr GhcTc
forall p. XRecFld p -> AmbiguousFieldOcc p -> HsExpr p
HsRecFld NoExtField
XRecFld GhcTc
noExtField (XUnambiguous GhcTc -> Located RdrName -> AmbiguousFieldOcc GhcTc
forall pass.
XUnambiguous pass -> Located RdrName -> AmbiguousFieldOcc pass
Unambiguous TcId
XUnambiguous GhcTc
sel_id Located RdrName
lbl)
        ; (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsExpr GhcTc
expr, TcId -> TcRhoType
idType TcId
sel_id) }

tcInferRecSelId (Ambiguous XAmbiguous GhcRn
_ Located RdrName
lbl) [HsExprArg 'TcpRn]
args Maybe TcRhoType
mb_res_ty
   = do { Name
sel_name <- Located RdrName
-> [HsExprArg 'TcpRn] -> Maybe TcRhoType -> TcM Name
tcInferAmbiguousRecSelId Located RdrName
lbl [HsExprArg 'TcpRn]
args Maybe TcRhoType
mb_res_ty
        ; TcId
sel_id   <- Located RdrName -> Name -> TcM TcId
tc_rec_sel_id Located RdrName
lbl Name
sel_name
        ; let expr :: HsExpr GhcTc
expr = XRecFld GhcTc -> AmbiguousFieldOcc GhcTc -> HsExpr GhcTc
forall p. XRecFld p -> AmbiguousFieldOcc p -> HsExpr p
HsRecFld NoExtField
XRecFld GhcTc
noExtField (XAmbiguous GhcTc -> Located RdrName -> AmbiguousFieldOcc GhcTc
forall pass.
XAmbiguous pass -> Located RdrName -> AmbiguousFieldOcc pass
Ambiguous TcId
XAmbiguous GhcTc
sel_id Located RdrName
lbl)
        ; (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsExpr GhcTc
expr, TcId -> TcRhoType
idType TcId
sel_id) }

------------------------
tc_rec_sel_id :: Located RdrName -> Name -> TcM TcId
-- Like tc_infer_id, but returns an Id not a HsExpr,
-- so we can wrap it back up into a HsRecFld
tc_rec_sel_id :: Located RdrName -> Name -> TcM TcId
tc_rec_sel_id Located RdrName
lbl Name
sel_name
  = do { TcTyThing
thing <- Name -> TcM TcTyThing
tcLookup Name
sel_name
       ; case TcTyThing
thing of
             ATcId { tct_id :: TcTyThing -> TcId
tct_id = TcId
id }
               -> do { OccName -> TcId -> TcM ()
check_local_id OccName
occ TcId
id
                     ; TcId -> TcM TcId
forall (m :: * -> *) a. Monad m => a -> m a
return TcId
id }

             AGlobal (AnId TcId
id)
               -> do { OccName -> TcId -> TcM ()
check_global_id OccName
occ TcId
id
                     ; TcId -> TcM TcId
forall (m :: * -> *) a. Monad m => a -> m a
return TcId
id }
                    -- A global cannot possibly be ill-staged
                    -- nor does it need the 'lifting' treatment
                    -- hence no checkTh stuff here

             TcTyThing
_ -> SDoc -> TcM TcId
forall a. SDoc -> TcM a
failWithTc (SDoc -> TcM TcId) -> SDoc -> TcM TcId
forall a b. (a -> b) -> a -> b
$
                  TcTyThing -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyThing
thing SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"used where a value identifier was expected" }
  where
    occ :: OccName
occ = RdrName -> OccName
rdrNameOcc (Located RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc Located RdrName
lbl)

------------------------
tcInferAmbiguousRecSelId :: Located RdrName
                         -> [HsExprArg 'TcpRn] -> Maybe TcRhoType
                         -> TcM Name
-- Disgusting special case for ambiguous record selectors
-- Given a RdrName that refers to multiple record fields, and the type
-- of its argument, try to determine the name of the selector that is
-- meant.
-- See Note [Disambiguating record fields]
tcInferAmbiguousRecSelId :: Located RdrName
-> [HsExprArg 'TcpRn] -> Maybe TcRhoType -> TcM Name
tcInferAmbiguousRecSelId Located RdrName
lbl [HsExprArg 'TcpRn]
args Maybe TcRhoType
mb_res_ty
  | HsExprArg 'TcpRn
arg1 : [HsExprArg 'TcpRn]
_ <- (HsExprArg 'TcpRn -> Bool)
-> [HsExprArg 'TcpRn] -> [HsExprArg 'TcpRn]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Bool -> Bool
not (Bool -> Bool)
-> (HsExprArg 'TcpRn -> Bool) -> HsExprArg 'TcpRn -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsExprArg 'TcpRn -> Bool
forall (id :: TcPass). HsExprArg id -> Bool
isVisibleArg) [HsExprArg 'TcpRn]
args -- A value arg is first
  , EValArg { eva_arg :: forall (p :: TcPass). HsExprArg p -> EValArg p
eva_arg = ValArg (L _ arg) } <- HsExprArg 'TcpRn
arg1
  , Just LHsSigWcType GhcRn
sig_ty <- HsExpr GhcRn -> Maybe (LHsSigWcType GhcRn)
obviousSig HsExpr GhcRn
arg  -- A type sig on the arg disambiguates
  = do { TcRhoType
sig_tc_ty <- UserTypeCtxt -> LHsSigWcType GhcRn -> TcM TcRhoType
tcHsSigWcType UserTypeCtxt
ExprSigCtxt LHsSigWcType GhcRn
sig_ty
       ; Located RdrName -> TcRhoType -> TcM Name
finish_ambiguous_selector Located RdrName
lbl TcRhoType
sig_tc_ty }

  | Just TcRhoType
res_ty <- Maybe TcRhoType
mb_res_ty
  , Just (Scaled TcRhoType
arg_ty,TcRhoType
_) <- TcRhoType -> Maybe (Scaled TcRhoType, TcRhoType)
tcSplitFunTy_maybe TcRhoType
res_ty
  = Located RdrName -> TcRhoType -> TcM Name
finish_ambiguous_selector Located RdrName
lbl (Scaled TcRhoType -> TcRhoType
forall a. Scaled a -> a
scaledThing Scaled TcRhoType
arg_ty)

  | Bool
otherwise
  = Located RdrName -> TcM Name
forall a. Located RdrName -> TcM a
ambiguousSelector Located RdrName
lbl

finish_ambiguous_selector :: Located RdrName -> Type -> TcM Name
finish_ambiguous_selector :: Located RdrName -> TcRhoType -> TcM Name
finish_ambiguous_selector lr :: Located RdrName
lr@(L SrcSpan
_ RdrName
rdr) TcRhoType
parent_type
 = do { FamInstEnvs
fam_inst_envs <- TcM FamInstEnvs
tcGetFamInstEnvs
      ; case FamInstEnvs -> TcRhoType -> Maybe TyCon
tyConOf FamInstEnvs
fam_inst_envs TcRhoType
parent_type of {
          Maybe TyCon
Nothing -> Located RdrName -> TcM Name
forall a. Located RdrName -> TcM a
ambiguousSelector Located RdrName
lr ;
          Just TyCon
p  ->

    do { [(RecSelParent, GlobalRdrElt)]
xs <- RdrName -> RnM [(RecSelParent, GlobalRdrElt)]
lookupParents RdrName
rdr
       ; let parent :: RecSelParent
parent = TyCon -> RecSelParent
RecSelData TyCon
p
       ; case RecSelParent
-> [(RecSelParent, GlobalRdrElt)] -> Maybe GlobalRdrElt
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup RecSelParent
parent [(RecSelParent, GlobalRdrElt)]
xs of {
           Maybe GlobalRdrElt
Nothing  -> SDoc -> TcM Name
forall a. SDoc -> TcM a
failWithTc (RecSelParent -> RdrName -> SDoc
fieldNotInType RecSelParent
parent RdrName
rdr) ;
           Just GlobalRdrElt
gre ->

    do { Bool -> GlobalRdrElt -> TcM ()
addUsedGRE Bool
True GlobalRdrElt
gre
       ; Name -> TcM Name
forall (m :: * -> *) a. Monad m => a -> m a
return (GlobalRdrElt -> Name
gre_name GlobalRdrElt
gre) } } } } }

-- This field name really is ambiguous, so add a suitable "ambiguous
-- occurrence" error, then give up.
ambiguousSelector :: Located RdrName -> TcM a
ambiguousSelector :: Located RdrName -> TcM a
ambiguousSelector (L SrcSpan
_ RdrName
rdr)
  = do { RdrName -> TcM ()
addAmbiguousNameErr RdrName
rdr
       ; TcM a
forall env a. IOEnv env a
failM }

-- | This name really is ambiguous, so add a suitable "ambiguous
-- occurrence" error, then continue
addAmbiguousNameErr :: RdrName -> TcM ()
addAmbiguousNameErr :: RdrName -> TcM ()
addAmbiguousNameErr RdrName
rdr
  = do { GlobalRdrEnv
env <- TcRn GlobalRdrEnv
getGlobalRdrEnv
       ; let gres :: [GlobalRdrElt]
gres = RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
lookupGRE_RdrName RdrName
rdr GlobalRdrEnv
env
       ; [ErrCtxt] -> TcM () -> TcM ()
forall a. [ErrCtxt] -> TcM a -> TcM a
setErrCtxt [] (TcM () -> TcM ()) -> TcM () -> TcM ()
forall a b. (a -> b) -> a -> b
$ RdrName -> [GlobalRdrElt] -> TcM ()
addNameClashErrRn RdrName
rdr [GlobalRdrElt]
gres}

-- A type signature on the argument of an ambiguous record selector or
-- the record expression in an update must be "obvious", i.e. the
-- outermost constructor ignoring parentheses.
obviousSig :: HsExpr GhcRn -> Maybe (LHsSigWcType GhcRn)
obviousSig :: HsExpr GhcRn -> Maybe (LHsSigWcType GhcRn)
obviousSig (ExprWithTySig XExprWithTySig GhcRn
_ LHsExpr GhcRn
_ LHsSigWcType (NoGhcTc GhcRn)
ty) = LHsSigWcType GhcRn -> Maybe (LHsSigWcType GhcRn)
forall a. a -> Maybe a
Just LHsSigWcType GhcRn
LHsSigWcType (NoGhcTc GhcRn)
ty
obviousSig (HsPar XPar GhcRn
_ LHsExpr GhcRn
p)            = HsExpr GhcRn -> Maybe (LHsSigWcType GhcRn)
obviousSig (GenLocated SrcSpan (HsExpr GhcRn) -> HsExpr GhcRn
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpan (HsExpr GhcRn)
LHsExpr GhcRn
p)
obviousSig (HsPragE XPragE GhcRn
_ HsPragE GhcRn
_ LHsExpr GhcRn
p)        = HsExpr GhcRn -> Maybe (LHsSigWcType GhcRn)
obviousSig (GenLocated SrcSpan (HsExpr GhcRn) -> HsExpr GhcRn
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpan (HsExpr GhcRn)
LHsExpr GhcRn
p)
obviousSig HsExpr GhcRn
_                      = Maybe (LHsSigWcType GhcRn)
forall a. Maybe a
Nothing

-- Extract the outermost TyCon of a type, if there is one; for
-- data families this is the representation tycon (because that's
-- where the fields live).
tyConOf :: FamInstEnvs -> TcSigmaType -> Maybe TyCon
tyConOf :: FamInstEnvs -> TcRhoType -> Maybe TyCon
tyConOf FamInstEnvs
fam_inst_envs TcRhoType
ty0
  = case HasCallStack => TcRhoType -> Maybe (TyCon, [TcRhoType])
TcRhoType -> Maybe (TyCon, [TcRhoType])
tcSplitTyConApp_maybe TcRhoType
ty of
      Just (TyCon
tc, [TcRhoType]
tys) -> TyCon -> Maybe TyCon
forall a. a -> Maybe a
Just ((TyCon, [TcRhoType], Coercion) -> TyCon
forall a b c. (a, b, c) -> a
fstOf3 (FamInstEnvs
-> TyCon -> [TcRhoType] -> (TyCon, [TcRhoType], Coercion)
tcLookupDataFamInst FamInstEnvs
fam_inst_envs TyCon
tc [TcRhoType]
tys))
      Maybe (TyCon, [TcRhoType])
Nothing        -> Maybe TyCon
forall a. Maybe a
Nothing
  where
    ([TcId]
_, [TcRhoType]
_, TcRhoType
ty) = TcRhoType -> ([TcId], [TcRhoType], TcRhoType)
tcSplitSigmaTy TcRhoType
ty0

-- Variant of tyConOf that works for ExpTypes
tyConOfET :: FamInstEnvs -> ExpRhoType -> Maybe TyCon
tyConOfET :: FamInstEnvs -> ExpSigmaType -> Maybe TyCon
tyConOfET FamInstEnvs
fam_inst_envs ExpSigmaType
ty0 = FamInstEnvs -> TcRhoType -> Maybe TyCon
tyConOf FamInstEnvs
fam_inst_envs (TcRhoType -> Maybe TyCon) -> Maybe TcRhoType -> Maybe TyCon
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ExpSigmaType -> Maybe TcRhoType
checkingExpType_maybe ExpSigmaType
ty0


-- For an ambiguous record field, find all the candidate record
-- selectors (as GlobalRdrElts) and their parents.
lookupParents :: RdrName -> RnM [(RecSelParent, GlobalRdrElt)]
lookupParents :: RdrName -> RnM [(RecSelParent, GlobalRdrElt)]
lookupParents RdrName
rdr
  = do { GlobalRdrEnv
env <- TcRn GlobalRdrEnv
getGlobalRdrEnv
       ; let gres :: [GlobalRdrElt]
gres = RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
lookupGRE_RdrName RdrName
rdr GlobalRdrEnv
env
       ; (GlobalRdrElt
 -> IOEnv (Env TcGblEnv TcLclEnv) (RecSelParent, GlobalRdrElt))
-> [GlobalRdrElt] -> RnM [(RecSelParent, GlobalRdrElt)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM GlobalRdrElt
-> IOEnv (Env TcGblEnv TcLclEnv) (RecSelParent, GlobalRdrElt)
lookupParent [GlobalRdrElt]
gres }
  where
    lookupParent :: GlobalRdrElt -> RnM (RecSelParent, GlobalRdrElt)
    lookupParent :: GlobalRdrElt
-> IOEnv (Env TcGblEnv TcLclEnv) (RecSelParent, GlobalRdrElt)
lookupParent GlobalRdrElt
gre = do { TcId
id <- Name -> TcM TcId
tcLookupId (GlobalRdrElt -> Name
gre_name GlobalRdrElt
gre)
                          ; case TcId -> Maybe RecSelParent
recordSelectorTyCon_maybe TcId
id of
                              Just RecSelParent
rstc -> (RecSelParent, GlobalRdrElt)
-> IOEnv (Env TcGblEnv TcLclEnv) (RecSelParent, GlobalRdrElt)
forall (m :: * -> *) a. Monad m => a -> m a
return (RecSelParent
rstc, GlobalRdrElt
gre)
                              Maybe RecSelParent
Nothing -> SDoc -> IOEnv (Env TcGblEnv TcLclEnv) (RecSelParent, GlobalRdrElt)
forall a. SDoc -> TcM a
failWithTc (Name -> SDoc
notSelector (GlobalRdrElt -> Name
gre_name GlobalRdrElt
gre)) }


fieldNotInType :: RecSelParent -> RdrName -> SDoc
fieldNotInType :: RecSelParent -> RdrName -> SDoc
fieldNotInType RecSelParent
p RdrName
rdr
  = SDoc -> RdrName -> SDoc
unknownSubordinateErr (String -> SDoc
text String
"field of type" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (RecSelParent -> SDoc
forall a. Outputable a => a -> SDoc
ppr RecSelParent
p)) RdrName
rdr

notSelector :: Name -> SDoc
notSelector :: Name -> SDoc
notSelector Name
field
  = [SDoc] -> SDoc
hsep [SDoc -> SDoc
quotes (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
field), String -> SDoc
text String
"is not a record selector"]

naughtyRecordSel :: OccName -> SDoc
naughtyRecordSel :: OccName -> SDoc
naughtyRecordSel OccName
lbl
  = String -> SDoc
text String
"Cannot use record selector" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (OccName -> SDoc
forall a. Outputable a => a -> SDoc
ppr OccName
lbl) SDoc -> SDoc -> SDoc
<+>
    String -> SDoc
text String
"as a function due to escaped type variables" SDoc -> SDoc -> SDoc
$$
    String -> SDoc
text String
"Probable fix: use pattern-matching syntax instead"


{- *********************************************************************
*                                                                      *
                Expressions with a type signature
                        expr :: type
*                                                                      *
********************************************************************* -}

tcExprWithSig :: LHsExpr GhcRn -> LHsSigWcType (NoGhcTc GhcRn)
              -> TcM (HsExpr GhcTc, TcSigmaType)
tcExprWithSig :: LHsExpr GhcRn
-> LHsSigWcType (NoGhcTc GhcRn) -> TcM (HsExpr GhcTc, TcRhoType)
tcExprWithSig LHsExpr GhcRn
expr LHsSigWcType (NoGhcTc GhcRn)
hs_ty
  = do { TcIdSigInfo
sig_info <- TcM TcIdSigInfo -> TcM TcIdSigInfo
forall r. TcM r -> TcM r
checkNoErrs (TcM TcIdSigInfo -> TcM TcIdSigInfo)
-> TcM TcIdSigInfo -> TcM TcIdSigInfo
forall a b. (a -> b) -> a -> b
$  -- Avoid error cascade
                     SrcSpan -> LHsSigWcType GhcRn -> Maybe Name -> TcM TcIdSigInfo
tcUserTypeSig SrcSpan
loc LHsSigWcType GhcRn
LHsSigWcType (NoGhcTc GhcRn)
hs_ty Maybe Name
forall a. Maybe a
Nothing
       ; (GenLocated SrcSpan (HsExpr GhcTc)
expr', TcRhoType
poly_ty) <- LHsExpr GhcRn -> TcIdSigInfo -> TcM (LHsExpr GhcTc, TcRhoType)
tcExprSig LHsExpr GhcRn
expr TcIdSigInfo
sig_info
       ; (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType)
forall (m :: * -> *) a. Monad m => a -> m a
return (XExprWithTySig GhcTc
-> LHsExpr GhcTc -> LHsSigWcType (NoGhcTc GhcTc) -> HsExpr GhcTc
forall p.
XExprWithTySig p
-> LHsExpr p -> LHsSigWcType (NoGhcTc p) -> HsExpr p
ExprWithTySig NoExtField
XExprWithTySig GhcTc
noExtField GenLocated SrcSpan (HsExpr GhcTc)
LHsExpr GhcTc
expr' LHsSigWcType (NoGhcTc GhcRn)
LHsSigWcType (NoGhcTc GhcTc)
hs_ty, TcRhoType
poly_ty) }
  where
    loc :: SrcSpan
loc = GenLocated SrcSpan (HsSigType GhcRn) -> SrcSpan
forall l e. GenLocated l e -> l
getLoc (LHsSigWcType GhcRn -> GenLocated SrcSpan (HsSigType GhcRn)
forall pass. LHsSigWcType pass -> LHsSigType pass
dropWildCards LHsSigWcType GhcRn
LHsSigWcType (NoGhcTc GhcRn)
hs_ty)

tcExprSig :: LHsExpr GhcRn -> TcIdSigInfo -> TcM (LHsExpr GhcTc, TcType)
tcExprSig :: LHsExpr GhcRn -> TcIdSigInfo -> TcM (LHsExpr GhcTc, TcRhoType)
tcExprSig LHsExpr GhcRn
expr (CompleteSig { sig_bndr :: TcIdSigInfo -> TcId
sig_bndr = TcId
poly_id, sig_loc :: TcIdSigInfo -> SrcSpan
sig_loc = SrcSpan
loc })
  = SrcSpan
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
loc (IOEnv
   (Env TcGblEnv TcLclEnv)
   (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
forall a b. (a -> b) -> a -> b
$   -- Sets the location for the implication constraint
    do { let poly_ty :: TcRhoType
poly_ty = TcId -> TcRhoType
idType TcId
poly_id
       ; (HsWrapper
wrap, GenLocated SrcSpan (HsExpr GhcTc)
expr') <- UserTypeCtxt
-> TcRhoType
-> (TcRhoType -> TcM (GenLocated SrcSpan (HsExpr GhcTc)))
-> TcM (HsWrapper, GenLocated SrcSpan (HsExpr GhcTc))
forall result.
UserTypeCtxt
-> TcRhoType
-> (TcRhoType -> TcM result)
-> TcM (HsWrapper, result)
tcSkolemiseScoped UserTypeCtxt
ExprSigCtxt TcRhoType
poly_ty ((TcRhoType -> TcM (GenLocated SrcSpan (HsExpr GhcTc)))
 -> TcM (HsWrapper, GenLocated SrcSpan (HsExpr GhcTc)))
-> (TcRhoType -> TcM (GenLocated SrcSpan (HsExpr GhcTc)))
-> TcM (HsWrapper, GenLocated SrcSpan (HsExpr GhcTc))
forall a b. (a -> b) -> a -> b
$ \TcRhoType
rho_ty ->
                          LHsExpr GhcRn -> TcRhoType -> TcM (LHsExpr GhcTc)
tcCheckMonoExprNC LHsExpr GhcRn
expr TcRhoType
rho_ty
       ; (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> LHsExpr GhcTc -> LHsExpr GhcTc
mkLHsWrap HsWrapper
wrap GenLocated SrcSpan (HsExpr GhcTc)
LHsExpr GhcTc
expr', TcRhoType
poly_ty) }

tcExprSig LHsExpr GhcRn
expr sig :: TcIdSigInfo
sig@(PartialSig { psig_name :: TcIdSigInfo -> Name
psig_name = Name
name, sig_loc :: TcIdSigInfo -> SrcSpan
sig_loc = SrcSpan
loc })
  = SrcSpan
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
loc (IOEnv
   (Env TcGblEnv TcLclEnv)
   (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
forall a b. (a -> b) -> a -> b
$   -- Sets the location for the implication constraint
    do { (TcLevel
tclvl, WantedConstraints
wanted, (GenLocated SrcSpan (HsExpr GhcTc)
expr', TcIdSigInst
sig_inst))
             <- TcM (GenLocated SrcSpan (HsExpr GhcTc), TcIdSigInst)
-> TcM
     (TcLevel, WantedConstraints,
      (GenLocated SrcSpan (HsExpr GhcTc), TcIdSigInst))
forall a. TcM a -> TcM (TcLevel, WantedConstraints, a)
pushLevelAndCaptureConstraints  (TcM (GenLocated SrcSpan (HsExpr GhcTc), TcIdSigInst)
 -> TcM
      (TcLevel, WantedConstraints,
       (GenLocated SrcSpan (HsExpr GhcTc), TcIdSigInst)))
-> TcM (GenLocated SrcSpan (HsExpr GhcTc), TcIdSigInst)
-> TcM
     (TcLevel, WantedConstraints,
      (GenLocated SrcSpan (HsExpr GhcTc), TcIdSigInst))
forall a b. (a -> b) -> a -> b
$
                do { TcIdSigInst
sig_inst <- TcIdSigInfo -> TcM TcIdSigInst
tcInstSig TcIdSigInfo
sig
                   ; GenLocated SrcSpan (HsExpr GhcTc)
expr' <- [(Name, TcId)]
-> TcM (GenLocated SrcSpan (HsExpr GhcTc))
-> TcM (GenLocated SrcSpan (HsExpr GhcTc))
forall r. [(Name, TcId)] -> TcM r -> TcM r
tcExtendNameTyVarEnv ((VarBndr TcId Specificity -> TcId)
-> [(Name, VarBndr TcId Specificity)] -> [(Name, TcId)]
forall b c a. (b -> c) -> [(a, b)] -> [(a, c)]
mapSnd VarBndr TcId Specificity -> TcId
forall tv argf. VarBndr tv argf -> tv
binderVar ([(Name, VarBndr TcId Specificity)] -> [(Name, TcId)])
-> [(Name, VarBndr TcId Specificity)] -> [(Name, TcId)]
forall a b. (a -> b) -> a -> b
$ TcIdSigInst -> [(Name, VarBndr TcId Specificity)]
sig_inst_skols TcIdSigInst
sig_inst) (TcM (GenLocated SrcSpan (HsExpr GhcTc))
 -> TcM (GenLocated SrcSpan (HsExpr GhcTc)))
-> TcM (GenLocated SrcSpan (HsExpr GhcTc))
-> TcM (GenLocated SrcSpan (HsExpr GhcTc))
forall a b. (a -> b) -> a -> b
$
                              [(Name, TcId)]
-> TcM (GenLocated SrcSpan (HsExpr GhcTc))
-> TcM (GenLocated SrcSpan (HsExpr GhcTc))
forall r. [(Name, TcId)] -> TcM r -> TcM r
tcExtendNameTyVarEnv (TcIdSigInst -> [(Name, TcId)]
sig_inst_wcs   TcIdSigInst
sig_inst) (TcM (GenLocated SrcSpan (HsExpr GhcTc))
 -> TcM (GenLocated SrcSpan (HsExpr GhcTc)))
-> TcM (GenLocated SrcSpan (HsExpr GhcTc))
-> TcM (GenLocated SrcSpan (HsExpr GhcTc))
forall a b. (a -> b) -> a -> b
$
                              LHsExpr GhcRn -> TcRhoType -> TcM (LHsExpr GhcTc)
tcCheckPolyExprNC LHsExpr GhcRn
expr (TcIdSigInst -> TcRhoType
sig_inst_tau TcIdSigInst
sig_inst)
                   ; (GenLocated SrcSpan (HsExpr GhcTc), TcIdSigInst)
-> TcM (GenLocated SrcSpan (HsExpr GhcTc), TcIdSigInst)
forall (m :: * -> *) a. Monad m => a -> m a
return (GenLocated SrcSpan (HsExpr GhcTc)
expr', TcIdSigInst
sig_inst) }
       -- See Note [Partial expression signatures]
       ; let tau :: TcRhoType
tau = TcIdSigInst -> TcRhoType
sig_inst_tau TcIdSigInst
sig_inst
             infer_mode :: InferMode
infer_mode | [TcRhoType] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (TcIdSigInst -> [TcRhoType]
sig_inst_theta TcIdSigInst
sig_inst)
                        , Maybe TcRhoType -> Bool
forall a. Maybe a -> Bool
isNothing (TcIdSigInst -> Maybe TcRhoType
sig_inst_wcx TcIdSigInst
sig_inst)
                        = InferMode
ApplyMR
                        | Bool
otherwise
                        = InferMode
NoRestrictions
       ; ([TcId]
qtvs, [TcId]
givens, TcEvBinds
ev_binds, WantedConstraints
residual, Bool
_)
                 <- TcLevel
-> InferMode
-> [TcIdSigInst]
-> [(Name, TcRhoType)]
-> WantedConstraints
-> TcM ([TcId], [TcId], TcEvBinds, WantedConstraints, Bool)
simplifyInfer TcLevel
tclvl InferMode
infer_mode [TcIdSigInst
sig_inst] [(Name
name, TcRhoType
tau)] WantedConstraints
wanted
       ; WantedConstraints -> TcM ()
emitConstraints WantedConstraints
residual

       ; TcRhoType
tau <- TcRhoType -> TcM TcRhoType
zonkTcType TcRhoType
tau
       ; let inferred_theta :: [TcRhoType]
inferred_theta = (TcId -> TcRhoType) -> [TcId] -> [TcRhoType]
forall a b. (a -> b) -> [a] -> [b]
map TcId -> TcRhoType
evVarPred [TcId]
givens
             tau_tvs :: TyCoVarSet
tau_tvs        = TcRhoType -> TyCoVarSet
tyCoVarsOfType TcRhoType
tau
       ; ([VarBndr TcId Specificity]
binders, [TcRhoType]
my_theta) <- [TcRhoType]
-> TyCoVarSet
-> [TcId]
-> Maybe TcIdSigInst
-> TcM ([VarBndr TcId Specificity], [TcRhoType])
chooseInferredQuantifiers [TcRhoType]
inferred_theta
                                   TyCoVarSet
tau_tvs [TcId]
qtvs (TcIdSigInst -> Maybe TcIdSigInst
forall a. a -> Maybe a
Just TcIdSigInst
sig_inst)
       ; let inferred_sigma :: TcRhoType
inferred_sigma = [TcId] -> [TcRhoType] -> TcRhoType -> TcRhoType
mkInfSigmaTy [TcId]
qtvs [TcRhoType]
inferred_theta TcRhoType
tau
             my_sigma :: TcRhoType
my_sigma       = [VarBndr TcId Specificity] -> TcRhoType -> TcRhoType
mkInvisForAllTys [VarBndr TcId Specificity]
binders ([TcRhoType] -> TcRhoType -> TcRhoType
mkPhiTy  [TcRhoType]
my_theta TcRhoType
tau)
       ; HsWrapper
wrap <- if TcRhoType
inferred_sigma TcRhoType -> TcRhoType -> Bool
`eqType` TcRhoType
my_sigma -- NB: eqType ignores vis.
                 then HsWrapper -> IOEnv (Env TcGblEnv TcLclEnv) HsWrapper
forall (m :: * -> *) a. Monad m => a -> m a
return HsWrapper
idHsWrapper  -- Fast path; also avoids complaint when we infer
                                          -- an ambiguous type and have AllowAmbiguousType
                                          -- e..g infer  x :: forall a. F a -> Int
                 else UserTypeCtxt
-> TcRhoType
-> TcRhoType
-> IOEnv (Env TcGblEnv TcLclEnv) HsWrapper
tcSubTypeSigma UserTypeCtxt
ExprSigCtxt TcRhoType
inferred_sigma TcRhoType
my_sigma

       ; String -> SDoc -> TcM ()
traceTc String
"tcExpSig" ([TcId] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcId]
qtvs SDoc -> SDoc -> SDoc
$$ [TcId] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [TcId]
givens SDoc -> SDoc -> SDoc
$$ TcRhoType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcRhoType
inferred_sigma SDoc -> SDoc -> SDoc
$$ TcRhoType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcRhoType
my_sigma)
       ; let poly_wrap :: HsWrapper
poly_wrap = HsWrapper
wrap
                         HsWrapper -> HsWrapper -> HsWrapper
<.> [TcId] -> HsWrapper
mkWpTyLams [TcId]
qtvs
                         HsWrapper -> HsWrapper -> HsWrapper
<.> [TcId] -> HsWrapper
mkWpLams [TcId]
givens
                         HsWrapper -> HsWrapper -> HsWrapper
<.> TcEvBinds -> HsWrapper
mkWpLet  TcEvBinds
ev_binds
       ; (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpan (HsExpr GhcTc), TcRhoType)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> LHsExpr GhcTc -> LHsExpr GhcTc
mkLHsWrap HsWrapper
poly_wrap GenLocated SrcSpan (HsExpr GhcTc)
LHsExpr GhcTc
expr', TcRhoType
my_sigma) }


{- Note [Partial expression signatures]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Partial type signatures on expressions are easy to get wrong.  But
here is a guiding principile
    e :: ty
should behave like
    let x :: ty
        x = e
    in x

So for partial signatures we apply the MR if no context is given.  So
   e :: IO _          apply the MR
   e :: _ => IO _     do not apply the MR
just like in GHC.Tc.Gen.Bind.decideGeneralisationPlan

This makes a difference (#11670):
   peek :: Ptr a -> IO CLong
   peek ptr = peekElemOff undefined 0 :: _
from (peekElemOff undefined 0) we get
          type: IO w
   constraints: Storable w

We must NOT try to generalise over 'w' because the signature specifies
no constraints so we'll complain about not being able to solve
Storable w.  Instead, don't generalise; then _ gets instantiated to
CLong, as it should.
-}


{- *********************************************************************
*                                                                      *
                 tcInferId, tcCheckId
*                                                                      *
********************************************************************* -}

tcCheckId :: Name -> ExpRhoType -> TcM (HsExpr GhcTc)
tcCheckId :: Name -> ExpSigmaType -> TcM (HsExpr GhcTc)
tcCheckId Name
name ExpSigmaType
res_ty
  = do { (HsExpr GhcTc
expr, TcRhoType
actual_res_ty) <- Name -> TcM (HsExpr GhcTc, TcRhoType)
tcInferId Name
name
       ; String -> SDoc -> TcM ()
traceTc String
"tcCheckId" ([SDoc] -> SDoc
vcat [Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
name, TcRhoType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcRhoType
actual_res_ty, ExpSigmaType -> SDoc
forall a. Outputable a => a -> SDoc
ppr ExpSigmaType
res_ty])
       ; HsExpr GhcTc
-> [HsExprArg 'TcpTc]
-> TcRhoType
-> ExpSigmaType
-> TcM (HsExpr GhcTc)
-> TcM (HsExpr GhcTc)
forall a.
HsExpr GhcTc
-> [HsExprArg 'TcpTc]
-> TcRhoType
-> ExpSigmaType
-> TcM a
-> TcM a
addFunResCtxt HsExpr GhcTc
expr [] TcRhoType
actual_res_ty ExpSigmaType
res_ty (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
         CtOrigin
-> HsExpr GhcRn
-> HsExpr GhcTc
-> TcRhoType
-> ExpSigmaType
-> TcM (HsExpr GhcTc)
tcWrapResultO (Name -> CtOrigin
OccurrenceOf Name
name) (XVar GhcRn -> LIdP GhcRn -> HsExpr GhcRn
forall p. XVar p -> LIdP p -> HsExpr p
HsVar NoExtField
XVar GhcRn
noExtField (Name -> Located Name
forall e. e -> Located e
noLoc Name
name)) HsExpr GhcTc
expr
                                           TcRhoType
actual_res_ty ExpSigmaType
res_ty }

------------------------
tcInferId :: Name -> TcM (HsExpr GhcTc, TcSigmaType)
-- Look up an occurrence of an Id
-- Do not instantiate its type
tcInferId :: Name -> TcM (HsExpr GhcTc, TcRhoType)
tcInferId Name
id_name
  | Name
id_name Name -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
assertIdKey
  = do { DynFlags
dflags <- IOEnv (Env TcGblEnv TcLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
       ; if GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_IgnoreAsserts DynFlags
dflags
         then Name -> TcM (HsExpr GhcTc, TcRhoType)
tc_infer_id Name
id_name
         else Name -> TcM (HsExpr GhcTc, TcRhoType)
tc_infer_assert Name
id_name }

  | Bool
otherwise
  = do { (HsExpr GhcTc
expr, TcRhoType
ty) <- Name -> TcM (HsExpr GhcTc, TcRhoType)
tc_infer_id Name
id_name
       ; String -> SDoc -> TcM ()
traceTc String
"tcInferId" (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
id_name SDoc -> SDoc -> SDoc
<+> SDoc
dcolon SDoc -> SDoc -> SDoc
<+> TcRhoType -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcRhoType
ty)
       ; (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsExpr GhcTc
expr, TcRhoType
ty) }

tc_infer_assert :: Name -> TcM (HsExpr GhcTc, TcSigmaType)
-- Deal with an occurrence of 'assert'
-- See Note [Adding the implicit parameter to 'assert']
tc_infer_assert :: Name -> TcM (HsExpr GhcTc, TcRhoType)
tc_infer_assert Name
assert_name
  = do { TcId
assert_error_id <- Name -> TcM TcId
tcLookupId Name
assertErrorName
       ; (HsWrapper
wrap, TcRhoType
id_rho) <- CtOrigin -> TcRhoType -> TcM (HsWrapper, TcRhoType)
topInstantiate (Name -> CtOrigin
OccurrenceOf Name
assert_name)
                                          (TcId -> TcRhoType
idType TcId
assert_error_id)
       ; (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap HsWrapper
wrap (XVar GhcTc -> LIdP GhcTc -> HsExpr GhcTc
forall p. XVar p -> LIdP p -> HsExpr p
HsVar NoExtField
XVar GhcTc
noExtField (TcId -> Located TcId
forall e. e -> Located e
noLoc TcId
assert_error_id)), TcRhoType
id_rho)
       }

tc_infer_id :: Name -> TcM (HsExpr GhcTc, TcSigmaType)
tc_infer_id :: Name -> TcM (HsExpr GhcTc, TcRhoType)
tc_infer_id Name
id_name
 = do { TcTyThing
thing <- Name -> TcM TcTyThing
tcLookup Name
id_name
      ; GlobalRdrEnv
global_env <- TcRn GlobalRdrEnv
getGlobalRdrEnv
      ; case TcTyThing
thing of
             ATcId { tct_id :: TcTyThing -> TcId
tct_id = TcId
id }
               -> do { OccName -> TcId -> TcM ()
check_local_id OccName
occ TcId
id
                     ; TcId -> TcM (HsExpr GhcTc, TcRhoType)
forall (m :: * -> *) p.
(Monad m, XVar p ~ NoExtField, XRec p (IdP p) ~ Located TcId) =>
TcId -> m (HsExpr p, TcRhoType)
return_id TcId
id }

             AGlobal (AnId TcId
id)
               -> do { OccName -> TcId -> TcM ()
check_global_id OccName
occ TcId
id
                     ; TcId -> TcM (HsExpr GhcTc, TcRhoType)
forall (m :: * -> *) p.
(Monad m, XVar p ~ NoExtField, XRec p (IdP p) ~ Located TcId) =>
TcId -> m (HsExpr p, TcRhoType)
return_id TcId
id }

             AGlobal (AConLike ConLike
cl) -> case ConLike
cl of
                 RealDataCon DataCon
con -> DataCon -> TcM (HsExpr GhcTc, TcRhoType)
return_data_con DataCon
con
                 PatSynCon PatSyn
ps
                   | Just (HsExpr GhcTc
expr, TcRhoType
ty) <- PatSyn -> Maybe (HsExpr GhcTc, TcRhoType)
patSynBuilderOcc PatSyn
ps
                   -> (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType)
forall (m :: * -> *) a. Monad m => a -> m a
return (HsExpr GhcTc
expr, TcRhoType
ty)
                   | Bool
otherwise
                   -> Name -> TcM (HsExpr GhcTc, TcRhoType)
forall name a. Outputable name => name -> TcM a
nonBidirectionalErr Name
id_name

             AGlobal (ATyCon TyCon
ty_con)
               -> GlobalRdrEnv -> TyCon -> TcM (HsExpr GhcTc, TcRhoType)
forall a. GlobalRdrEnv -> TyCon -> TcM a
fail_tycon GlobalRdrEnv
global_env TyCon
ty_con

             ATyVar Name
name TcId
_
                -> SDoc -> TcM (HsExpr GhcTc, TcRhoType)
forall a. SDoc -> TcM a
failWithTc (SDoc -> TcM (HsExpr GhcTc, TcRhoType))
-> SDoc -> TcM (HsExpr GhcTc, TcRhoType)
forall a b. (a -> b) -> a -> b
$
                     String -> SDoc
text String
"Illegal term-level use of the type variable"
                       SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
name)
                       SDoc -> SDoc -> SDoc
$$ Int -> SDoc -> SDoc
nest Int
2 (String -> SDoc
text String
"bound at" SDoc -> SDoc -> SDoc
<+> SrcLoc -> SDoc
forall a. Outputable a => a -> SDoc
ppr (Name -> SrcLoc
forall a. NamedThing a => a -> SrcLoc
getSrcLoc Name
name))

             ATcTyCon TyCon
ty_con
               -> GlobalRdrEnv -> TyCon -> TcM (HsExpr GhcTc, TcRhoType)
forall a. GlobalRdrEnv -> TyCon -> TcM a
fail_tycon GlobalRdrEnv
global_env TyCon
ty_con

             TcTyThing
_ -> SDoc -> TcM (HsExpr GhcTc, TcRhoType)
forall a. SDoc -> TcM a
failWithTc (SDoc -> TcM (HsExpr GhcTc, TcRhoType))
-> SDoc -> TcM (HsExpr GhcTc, TcRhoType)
forall a b. (a -> b) -> a -> b
$
                  TcTyThing -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcTyThing
thing SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"used where a value identifier was expected" }
  where
    fail_tycon :: GlobalRdrEnv -> TyCon -> TcM a
fail_tycon GlobalRdrEnv
global_env TyCon
ty_con =
      let pprov :: SDoc
pprov = case GlobalRdrEnv -> Name -> Maybe GlobalRdrElt
lookupGRE_Name GlobalRdrEnv
global_env (TyCon -> Name
tyConName TyCon
ty_con) of
            Just GlobalRdrElt
gre -> Int -> SDoc -> SDoc
nest Int
2 (GlobalRdrElt -> SDoc
pprNameProvenance GlobalRdrElt
gre)
            Maybe GlobalRdrElt
Nothing  -> SDoc
empty
      in SDoc -> TcM a
forall a. SDoc -> TcM a
failWithTc (TyCon -> SDoc
term_level_tycons TyCon
ty_con SDoc -> SDoc -> SDoc
$$ SDoc
pprov)

    term_level_tycons :: TyCon -> SDoc
term_level_tycons TyCon
ty_con
      = String -> SDoc
text String
"Illegal term-level use of the type constructor"
          SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr (TyCon -> Name
tyConName TyCon
ty_con))

    occ :: OccName
occ = Name -> OccName
nameOccName Name
id_name

    return_id :: TcId -> m (HsExpr p, TcRhoType)
return_id TcId
id = (HsExpr p, TcRhoType) -> m (HsExpr p, TcRhoType)
forall (m :: * -> *) a. Monad m => a -> m a
return (XVar p -> XRec p (IdP p) -> HsExpr p
forall p. XVar p -> LIdP p -> HsExpr p
HsVar NoExtField
XVar p
noExtField (TcId -> Located TcId
forall e. e -> Located e
noLoc TcId
id), TcId -> TcRhoType
idType TcId
id)

    return_data_con :: DataCon -> TcM (HsExpr GhcTc, TcRhoType)
return_data_con DataCon
con
      = do { let tvs :: [VarBndr TcId Specificity]
tvs = DataCon -> [VarBndr TcId Specificity]
dataConUserTyVarBinders DataCon
con
                 theta :: [TcRhoType]
theta = DataCon -> [TcRhoType]
dataConOtherTheta DataCon
con
                 args :: [Scaled TcRhoType]
args = DataCon -> [Scaled TcRhoType]
dataConOrigArgTys DataCon
con
                 res :: TcRhoType
res = DataCon -> TcRhoType
dataConOrigResTy DataCon
con

           -- See Note [Linear fields generalization]
           ; [TcRhoType]
mul_vars <- Int -> TcRhoType -> TcM [TcRhoType]
newFlexiTyVarTys ([Scaled TcRhoType] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Scaled TcRhoType]
args) TcRhoType
multiplicityTy
           ; let scaleArgs :: [Scaled TcRhoType] -> [Scaled TcRhoType]
scaleArgs [Scaled TcRhoType]
args' = String
-> (TcRhoType -> Scaled TcRhoType -> Scaled TcRhoType)
-> [TcRhoType]
-> [Scaled TcRhoType]
-> [Scaled TcRhoType]
forall a b c. String -> (a -> b -> c) -> [a] -> [b] -> [c]
zipWithEqual String
"return_data_con" TcRhoType -> Scaled TcRhoType -> Scaled TcRhoType
forall a. TcRhoType -> Scaled a -> Scaled a
combine [TcRhoType]
mul_vars [Scaled TcRhoType]
args'
                 combine :: TcRhoType -> Scaled a -> Scaled a
combine TcRhoType
var (Scaled TcRhoType
One a
ty) = TcRhoType -> a -> Scaled a
forall a. TcRhoType -> a -> Scaled a
Scaled TcRhoType
var a
ty
                 combine TcRhoType
_   Scaled a
scaled_ty       = Scaled a
scaled_ty
                   -- The combine function implements the fact that, as
                   -- described in Note [Linear fields generalization], if a
                   -- field is not linear (last line) it isn't made polymorphic.

                 etaWrapper :: t (Scaled TcRhoType) -> HsWrapper
etaWrapper t (Scaled TcRhoType)
arg_tys = (Scaled TcRhoType -> HsWrapper -> HsWrapper)
-> HsWrapper -> t (Scaled TcRhoType) -> HsWrapper
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Scaled TcRhoType
scaled_ty HsWrapper
wr -> HsWrapper -> HsWrapper -> Scaled TcRhoType -> SDoc -> HsWrapper
WpFun HsWrapper
WpHole HsWrapper
wr Scaled TcRhoType
scaled_ty SDoc
empty) HsWrapper
WpHole t (Scaled TcRhoType)
arg_tys

           -- See Note [Instantiating stupid theta]
           ; let shouldInstantiate :: Bool
shouldInstantiate = (Bool -> Bool
not ([TcRhoType] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (DataCon -> [TcRhoType]
dataConStupidTheta DataCon
con)) Bool -> Bool -> Bool
||
                                      TcRhoType -> Bool
isKindLevPoly (TyCon -> TcRhoType
tyConResKind (DataCon -> TyCon
dataConTyCon DataCon
con)))
           ; case Bool
shouldInstantiate of
               Bool
True -> do { (TCvSubst
subst, [TcId]
tvs') <- [TcId] -> TcM (TCvSubst, [TcId])
newMetaTyVars ([VarBndr TcId Specificity] -> [TcId]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [VarBndr TcId Specificity]
tvs)
                           ; let tys' :: [TcRhoType]
tys'   = [TcId] -> [TcRhoType]
mkTyVarTys [TcId]
tvs'
                                 theta' :: [TcRhoType]
theta' = HasCallStack => TCvSubst -> [TcRhoType] -> [TcRhoType]
TCvSubst -> [TcRhoType] -> [TcRhoType]
substTheta TCvSubst
subst [TcRhoType]
theta
                                 args' :: [Scaled TcRhoType]
args'  = HasCallStack =>
TCvSubst -> [Scaled TcRhoType] -> [Scaled TcRhoType]
TCvSubst -> [Scaled TcRhoType] -> [Scaled TcRhoType]
substScaledTys TCvSubst
subst [Scaled TcRhoType]
args
                                 res' :: TcRhoType
res'   = HasCallStack => TCvSubst -> TcRhoType -> TcRhoType
TCvSubst -> TcRhoType -> TcRhoType
substTy TCvSubst
subst TcRhoType
res
                           ; HsWrapper
wrap <- CtOrigin
-> [TcRhoType]
-> [TcRhoType]
-> IOEnv (Env TcGblEnv TcLclEnv) HsWrapper
instCall (Name -> CtOrigin
OccurrenceOf Name
id_name) [TcRhoType]
tys' [TcRhoType]
theta'
                           ; let scaled_arg_tys :: [Scaled TcRhoType]
scaled_arg_tys = [Scaled TcRhoType] -> [Scaled TcRhoType]
scaleArgs [Scaled TcRhoType]
args'
                                 eta_wrap :: HsWrapper
eta_wrap = [Scaled TcRhoType] -> HsWrapper
forall (t :: * -> *).
Foldable t =>
t (Scaled TcRhoType) -> HsWrapper
etaWrapper [Scaled TcRhoType]
scaled_arg_tys
                           ; DataCon -> [TcRhoType] -> TcM ()
addDataConStupidTheta DataCon
con [TcRhoType]
tys'
                           ; (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType)
forall (m :: * -> *) a. Monad m => a -> m a
return ( HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap (HsWrapper
eta_wrap HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
wrap)
                                               (XConLikeOut GhcTc -> ConLike -> HsExpr GhcTc
forall p. XConLikeOut p -> ConLike -> HsExpr p
HsConLikeOut NoExtField
XConLikeOut GhcTc
noExtField (DataCon -> ConLike
RealDataCon DataCon
con))
                                    , [Scaled TcRhoType] -> TcRhoType -> TcRhoType
mkVisFunTys [Scaled TcRhoType]
scaled_arg_tys TcRhoType
res')
                           }
               Bool
False -> let scaled_arg_tys :: [Scaled TcRhoType]
scaled_arg_tys = [Scaled TcRhoType] -> [Scaled TcRhoType]
scaleArgs [Scaled TcRhoType]
args
                            wrap1 :: HsWrapper
wrap1 = [TcRhoType] -> HsWrapper
mkWpTyApps ([TcId] -> [TcRhoType]
mkTyVarTys ([TcId] -> [TcRhoType]) -> [TcId] -> [TcRhoType]
forall a b. (a -> b) -> a -> b
$ [VarBndr TcId Specificity] -> [TcId]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [VarBndr TcId Specificity]
tvs)
                            eta_wrap :: HsWrapper
eta_wrap = [Scaled TcRhoType] -> HsWrapper
forall (t :: * -> *).
Foldable t =>
t (Scaled TcRhoType) -> HsWrapper
etaWrapper ((TcRhoType -> Scaled TcRhoType)
-> [TcRhoType] -> [Scaled TcRhoType]
forall a b. (a -> b) -> [a] -> [b]
map TcRhoType -> Scaled TcRhoType
forall a. a -> Scaled a
unrestricted [TcRhoType]
theta [Scaled TcRhoType] -> [Scaled TcRhoType] -> [Scaled TcRhoType]
forall a. [a] -> [a] -> [a]
++ [Scaled TcRhoType]
scaled_arg_tys)
                            wrap2 :: HsWrapper
wrap2 = [TcId] -> HsWrapper
mkWpTyLams ([TcId] -> HsWrapper) -> [TcId] -> HsWrapper
forall a b. (a -> b) -> a -> b
$ [VarBndr TcId Specificity] -> [TcId]
forall tv argf. [VarBndr tv argf] -> [tv]
binderVars [VarBndr TcId Specificity]
tvs
                        in (HsExpr GhcTc, TcRhoType) -> TcM (HsExpr GhcTc, TcRhoType)
forall (m :: * -> *) a. Monad m => a -> m a
return ( HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
mkHsWrap (HsWrapper
wrap2 HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
eta_wrap HsWrapper -> HsWrapper -> HsWrapper
<.> HsWrapper
wrap1)
                                             (XConLikeOut GhcTc -> ConLike -> HsExpr GhcTc
forall p. XConLikeOut p -> ConLike -> HsExpr p
HsConLikeOut NoExtField
XConLikeOut GhcTc
noExtField (DataCon -> ConLike
RealDataCon DataCon
con))
                                  , [VarBndr TcId Specificity] -> TcRhoType -> TcRhoType
mkInvisForAllTys [VarBndr TcId Specificity]
tvs (TcRhoType -> TcRhoType) -> TcRhoType -> TcRhoType
forall a b. (a -> b) -> a -> b
$ [TcRhoType] -> TcRhoType -> TcRhoType
mkInvisFunTysMany [TcRhoType]
theta (TcRhoType -> TcRhoType) -> TcRhoType -> TcRhoType
forall a b. (a -> b) -> a -> b
$ [Scaled TcRhoType] -> TcRhoType -> TcRhoType
mkVisFunTys [Scaled TcRhoType]
scaled_arg_tys TcRhoType
res)
           }

check_local_id :: OccName -> Id -> TcM ()
check_local_id :: OccName -> TcId -> TcM ()
check_local_id OccName
occ TcId
id
  = do { OccName -> TcId -> TcM ()
check_naughty OccName
occ TcId
id  -- See Note [HsVar: naughty record selectors]
       ; TcId -> TcM ()
checkThLocalId TcId
id
       ; UsageEnv -> TcM ()
tcEmitBindingUsage (UsageEnv -> TcM ()) -> UsageEnv -> TcM ()
forall a b. (a -> b) -> a -> b
$ Name -> TcRhoType -> UsageEnv
forall n. NamedThing n => n -> TcRhoType -> UsageEnv
unitUE (TcId -> Name
idName TcId
id) TcRhoType
One }

check_global_id :: OccName -> Id -> TcM ()
check_global_id :: OccName -> TcId -> TcM ()
check_global_id OccName
occ TcId
id
  = OccName -> TcId -> TcM ()
check_naughty OccName
occ TcId
id  -- See Note [HsVar: naughty record selectors]
  -- A global cannot possibly be ill-staged
  -- nor does it need the 'lifting' treatment
  -- Hence no checkTh stuff here

check_naughty :: OccName -> TcId -> TcM ()
check_naughty :: OccName -> TcId -> TcM ()
check_naughty OccName
lbl TcId
id
  | TcId -> Bool
isNaughtyRecordSelector TcId
id = SDoc -> TcM ()
forall a. SDoc -> TcM a
failWithTc (OccName -> SDoc
naughtyRecordSel OccName
lbl)
  | Bool
otherwise                  = () -> TcM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

nonBidirectionalErr :: Outputable name => name -> TcM a
nonBidirectionalErr :: name -> TcM a
nonBidirectionalErr name
name = SDoc -> TcM a
forall a. SDoc -> TcM a
failWithTc (SDoc -> TcM a) -> SDoc -> TcM a
forall a b. (a -> b) -> a -> b
$
    String -> SDoc
text String
"non-bidirectional pattern synonym"
    SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (name -> SDoc
forall a. Outputable a => a -> SDoc
ppr name
name) SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"used in an expression"

{- Note [HsVar: naughty record selectors]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All record selectors should really be HsRecFld (ambiguous or
unambiguous), but currently not all of them are: see #18452.  So we
need to check for naughty record selectors in tc_infer_id, as well as
in tc_rec_sel_id.

Remove this code when fixing #18452.

Note [Linear fields generalization]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As per Note [Polymorphisation of linear fields], linear field of data
constructors get a polymorphic type when the data constructor is used as a term.

    Just :: forall {p} a. a #p-> Maybe a

This rule is known only to the typechecker: Just keeps its linear type in Core.

In order to desugar this generalised typing rule, we simply eta-expand:

    \a (x # p :: a) -> Just @a x

has the appropriate type. We insert these eta-expansion with WpFun wrappers.

A small hitch: if the constructor is levity-polymorphic (unboxed tuples, sums,
certain newtypes with -XUnliftedNewtypes) then this strategy produces

    \r1 r2 a b (x # p :: a) (y # q :: b) -> (# a, b #)

Which has type

    forall r1 r2 a b. a #p-> b #q-> (# a, b #)

Which violates the levity-polymorphism restriction see Note [Levity polymorphism
checking] in DsMonad.

So we really must instantiate r1 and r2 rather than quantify over them.  For
simplicity, we just instantiate the entire type, as described in Note
[Instantiating stupid theta]. It breaks visible type application with unboxed
tuples, sums and levity-polymorphic newtypes, but this doesn't appear to be used
anywhere.

A better plan: let's force all representation variable to be *inferred*, so that
they are not subject to visible type applications. Then we can instantiate
inferred argument eagerly.

Note [Adding the implicit parameter to 'assert']
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The typechecker transforms (assert e1 e2) to (assertError e1 e2).
This isn't really the Right Thing because there's no way to "undo"
if you want to see the original source code in the typechecker
output.  We'll have fix this in due course, when we care more about
being able to reconstruct the exact original program.


Note [Instantiating stupid theta]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Normally, when we infer the type of an Id, we don't instantiate,
because we wish to allow for visible type application later on.
But if a datacon has a stupid theta, we're a bit stuck. We need
to emit the stupid theta constraints with instantiated types. It's
difficult to defer this to the lazy instantiation, because a stupid
theta has no spot to put it in a type. So we just instantiate eagerly
in this case. Thus, users cannot use visible type application with
a data constructor sporting a stupid theta. I won't feel so bad for
the users that complain.
-}

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

checkThLocalId :: Id -> TcM ()
-- The renamer has already done checkWellStaged,
--   in RnSplice.checkThLocalName, so don't repeat that here.
-- Here we just add constraints for cross-stage lifting
checkThLocalId :: TcId -> TcM ()
checkThLocalId TcId
id
  = do  { Maybe (TopLevelFlag, Int, ThStage)
mb_local_use <- Name -> TcRn (Maybe (TopLevelFlag, Int, ThStage))
getStageAndBindLevel (TcId -> Name
idName TcId
id)
        ; case Maybe (TopLevelFlag, Int, ThStage)
mb_local_use of
             Just (TopLevelFlag
top_lvl, Int
bind_lvl, ThStage
use_stage)
                | ThStage -> Int
thLevel ThStage
use_stage Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
bind_lvl
                -> TopLevelFlag -> TcId -> ThStage -> TcM ()
checkCrossStageLifting TopLevelFlag
top_lvl TcId
id ThStage
use_stage
             Maybe (TopLevelFlag, Int, ThStage)
_  -> () -> TcM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()   -- Not a locally-bound thing, or
                               -- no cross-stage link
    }

--------------------------------------
checkCrossStageLifting :: TopLevelFlag -> Id -> ThStage -> TcM ()
-- If we are inside typed brackets, and (use_lvl > bind_lvl)
-- we must check whether there's a cross-stage lift to do
-- Examples   \x -> [|| x ||]
--            [|| map ||]
--
-- This is similar to checkCrossStageLifting in GHC.Rename.Splice, but
-- this code is applied to *typed* brackets.

checkCrossStageLifting :: TopLevelFlag -> TcId -> ThStage -> TcM ()
checkCrossStageLifting TopLevelFlag
top_lvl TcId
id (Brack ThStage
_ (TcPending TcRef [PendingTcSplice]
ps_var TcRef WantedConstraints
lie_var QuoteWrapper
q))
  | TopLevelFlag -> Bool
isTopLevel TopLevelFlag
top_lvl
  = Bool -> TcM () -> TcM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Name -> Bool
isExternalName Name
id_name) (Name -> TcM ()
keepAlive Name
id_name)
    -- See Note [Keeping things alive for Template Haskell] in GHC.Rename.Splice

  | Bool
otherwise
  =     -- Nested identifiers, such as 'x' in
        -- E.g. \x -> [|| h x ||]
        -- We must behave as if the reference to x was
        --      h $(lift x)
        -- We use 'x' itself as the splice proxy, used by
        -- the desugarer to stitch it all back together.
        -- If 'x' occurs many times we may get many identical
        -- bindings of the same splice proxy, but that doesn't
        -- matter, although it's a mite untidy.
    do  { let id_ty :: TcRhoType
id_ty = TcId -> TcRhoType
idType TcId
id
        ; Bool -> SDoc -> TcM ()
checkTc (TcRhoType -> Bool
isTauTy TcRhoType
id_ty) (TcId -> SDoc
polySpliceErr TcId
id)
               -- If x is polymorphic, its occurrence sites might
               -- have different instantiations, so we can't use plain
               -- 'x' as the splice proxy name.  I don't know how to
               -- solve this, and it's probably unimportant, so I'm
               -- just going to flag an error for now

        ; HsExpr GhcTc
lift <- if TcRhoType -> Bool
isStringTy TcRhoType
id_ty then
                     do { TcId
sid <- Name -> TcM TcId
tcLookupId Name
GHC.Builtin.Names.TH.liftStringName
                                     -- See Note [Lifting strings]
                        ; HsExpr GhcTc -> TcM (HsExpr GhcTc)
forall (m :: * -> *) a. Monad m => a -> m a
return (XVar GhcTc -> LIdP GhcTc -> HsExpr GhcTc
forall p. XVar p -> LIdP p -> HsExpr p
HsVar NoExtField
XVar GhcTc
noExtField (TcId -> Located TcId
forall e. e -> Located e
noLoc TcId
sid)) }
                  else
                     TcRef WantedConstraints -> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a. TcRef WantedConstraints -> TcM a -> TcM a
setConstraintVar TcRef WantedConstraints
lie_var   (TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc))
-> TcM (HsExpr GhcTc) -> TcM (HsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
                          -- Put the 'lift' constraint into the right LIE
                     CtOrigin -> Name -> [TcRhoType] -> TcM (HsExpr GhcTc)
newMethodFromName (Name -> CtOrigin
OccurrenceOf Name
id_name)
                                       Name
GHC.Builtin.Names.TH.liftName
                                       [HasDebugCallStack => TcRhoType -> TcRhoType
TcRhoType -> TcRhoType
getRuntimeRep TcRhoType
id_ty, TcRhoType
id_ty]

                   -- Update the pending splices
        ; [PendingTcSplice]
ps <- TcRef [PendingTcSplice]
-> IOEnv (Env TcGblEnv TcLclEnv) [PendingTcSplice]
forall a env. IORef a -> IOEnv env a
readMutVar TcRef [PendingTcSplice]
ps_var
        ; let pending_splice :: PendingTcSplice
pending_splice = Name -> LHsExpr GhcTc -> PendingTcSplice
PendingTcSplice Name
id_name
                                 (LHsExpr GhcTc -> LHsExpr GhcTc -> LHsExpr GhcTc
forall (id :: Pass).
IsPass id =>
LHsExpr (GhcPass id)
-> LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
nlHsApp (HsWrapper -> LHsExpr GhcTc -> LHsExpr GhcTc
mkLHsWrap (QuoteWrapper -> HsWrapper
applyQuoteWrapper QuoteWrapper
q) (HsExpr GhcTc -> GenLocated SrcSpan (HsExpr GhcTc)
forall e. e -> Located e
noLoc HsExpr GhcTc
lift))
                                          (IdP GhcTc -> LHsExpr GhcTc
forall (id :: Pass). IdP (GhcPass id) -> LHsExpr (GhcPass id)
nlHsVar TcId
IdP GhcTc
id))
        ; TcRef [PendingTcSplice] -> [PendingTcSplice] -> TcM ()
forall a env. IORef a -> a -> IOEnv env ()
writeMutVar TcRef [PendingTcSplice]
ps_var (PendingTcSplice
pending_splice PendingTcSplice -> [PendingTcSplice] -> [PendingTcSplice]
forall a. a -> [a] -> [a]
: [PendingTcSplice]
ps)

        ; () -> TcM ()
forall (m :: * -> *) a. Monad m => a -> m a
return () }
  where
    id_name :: Name
id_name = TcId -> Name
idName TcId
id

checkCrossStageLifting TopLevelFlag
_ TcId
_ ThStage
_ = () -> TcM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

polySpliceErr :: Id -> SDoc
polySpliceErr :: TcId -> SDoc
polySpliceErr TcId
id
  = String -> SDoc
text String
"Can't splice the polymorphic local variable" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (TcId -> SDoc
forall a. Outputable a => a -> SDoc
ppr TcId
id)

{-
Note [Lifting strings]
~~~~~~~~~~~~~~~~~~~~~~
If we see $(... [| s |] ...) where s::String, we don't want to
generate a mass of Cons (CharL 'x') (Cons (CharL 'y') ...)) etc.
So this conditional short-circuits the lifting mechanism to generate
(liftString "xy") in that case.  I didn't want to use overlapping instances
for the Lift class in TH.Syntax, because that can lead to overlapping-instance
errors in a polymorphic situation.

If this check fails (which isn't impossible) we get another chance; see
Note [Converting strings] in Convert.hs

Local record selectors
~~~~~~~~~~~~~~~~~~~~~~
Record selectors for TyCons in this module are ordinary local bindings,
which show up as ATcIds rather than AGlobals.  So we need to check for
naughtiness in both branches.  c.f. TcTyClsBindings.mkAuxBinds.
-}


{- *********************************************************************
*                                                                      *
         Error reporting for function result mis-matches
*                                                                      *
********************************************************************* -}

addFunResCtxt :: HsExpr GhcTc -> [HsExprArg 'TcpTc]
              -> TcType -> ExpRhoType
              -> TcM a -> TcM a
-- When we have a mis-match in the return type of a function
-- try to give a helpful message about too many/few arguments
addFunResCtxt :: HsExpr GhcTc
-> [HsExprArg 'TcpTc]
-> TcRhoType
-> ExpSigmaType
-> TcM a
-> TcM a
addFunResCtxt HsExpr GhcTc
fun [HsExprArg 'TcpTc]
args TcRhoType
fun_res_ty ExpSigmaType
env_ty
  = (TidyEnv -> TcM (TidyEnv, SDoc)) -> TcM a -> TcM a
forall a. (TidyEnv -> TcM (TidyEnv, SDoc)) -> TcM a -> TcM a
addLandmarkErrCtxtM (\TidyEnv
env -> (TidyEnv
env, ) (SDoc -> (TidyEnv, SDoc))
-> IOEnv (Env TcGblEnv TcLclEnv) SDoc -> TcM (TidyEnv, SDoc)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) SDoc
mk_msg)
      -- NB: use a landmark error context, so that an empty context
      -- doesn't suppress some more useful context
  where
    mk_msg :: IOEnv (Env TcGblEnv TcLclEnv) SDoc
mk_msg
      = do { Maybe TcRhoType
mb_env_ty <- ExpSigmaType -> TcM (Maybe TcRhoType)
readExpType_maybe ExpSigmaType
env_ty
                     -- by the time the message is rendered, the ExpType
                     -- will be filled in (except if we're debugging)
           ; TcRhoType
fun_res' <- TcRhoType -> TcM TcRhoType
zonkTcType TcRhoType
fun_res_ty
           ; TcRhoType
env'     <- case Maybe TcRhoType
mb_env_ty of
                           Just TcRhoType
env_ty -> TcRhoType -> TcM TcRhoType
zonkTcType TcRhoType
env_ty
                           Maybe TcRhoType
Nothing     ->
                             do { Bool
dumping <- DumpFlag -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. DumpFlag -> TcRnIf gbl lcl Bool
doptM DumpFlag
Opt_D_dump_tc_trace
                                ; MASSERT( dumping )
                                ; TcRhoType -> TcM TcRhoType
newFlexiTyVarTy TcRhoType
liftedTypeKind }
           ; let -- See Note [Splitting nested sigma types in mismatched
                 --           function types]
                 ([TcId]
_, [TcRhoType]
_, TcRhoType
fun_tau) = TcRhoType -> ([TcId], [TcRhoType], TcRhoType)
tcSplitNestedSigmaTys TcRhoType
fun_res'
                 -- No need to call tcSplitNestedSigmaTys here, since env_ty is
                 -- an ExpRhoTy, i.e., it's already instantiated.
                 ([TcId]
_, [TcRhoType]
_, TcRhoType
env_tau) = TcRhoType -> ([TcId], [TcRhoType], TcRhoType)
tcSplitSigmaTy TcRhoType
env'
                 ([Scaled TcRhoType]
args_fun, TcRhoType
res_fun) = TcRhoType -> ([Scaled TcRhoType], TcRhoType)
tcSplitFunTys TcRhoType
fun_tau
                 ([Scaled TcRhoType]
args_env, TcRhoType
res_env) = TcRhoType -> ([Scaled TcRhoType], TcRhoType)
tcSplitFunTys TcRhoType
env_tau
                 n_fun :: Int
n_fun = [Scaled TcRhoType] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Scaled TcRhoType]
args_fun
                 n_env :: Int
n_env = [Scaled TcRhoType] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Scaled TcRhoType]
args_env
                 info :: SDoc
info  | -- Check for too few args
                         --  fun_tau = a -> b, res_tau = Int
                         Int
n_fun Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n_env
                       , TcRhoType -> Bool
not_fun TcRhoType
res_env
                       = String -> SDoc
text String
"Probable cause:" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (HsExpr GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsExpr GhcTc
fun)
                         SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"is applied to too few arguments"

                       | -- Check for too many args
                         -- fun_tau = a -> Int,   res_tau = a -> b -> c -> d
                         -- The final guard suppresses the message when there
                         -- aren't enough args to drop; eg. the call is (f e1)
                         Int
n_fun Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n_env
                       , TcRhoType -> Bool
not_fun TcRhoType
res_fun
                       , (Int
n_fun Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (HsExprArg 'TcpTc -> Bool) -> [HsExprArg 'TcpTc] -> Int
forall a. (a -> Bool) -> [a] -> Int
count HsExprArg 'TcpTc -> Bool
forall (id :: TcPass). HsExprArg id -> Bool
isValArg [HsExprArg 'TcpTc]
args) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n_env
                          -- Never suggest that a naked variable is
                                           -- applied to too many args!
                       = String -> SDoc
text String
"Possible cause:" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (HsExpr GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsExpr GhcTc
fun)
                         SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"is applied to too many arguments"

                       | Bool
otherwise
                       = SDoc
Outputable.empty

           ; SDoc -> IOEnv (Env TcGblEnv TcLclEnv) SDoc
forall (m :: * -> *) a. Monad m => a -> m a
return SDoc
info }
      where
        not_fun :: TcRhoType -> Bool
not_fun TcRhoType
ty   -- ty is definitely not an arrow type,
                     -- and cannot conceivably become one
          = case HasCallStack => TcRhoType -> Maybe (TyCon, [TcRhoType])
TcRhoType -> Maybe (TyCon, [TcRhoType])
tcSplitTyConApp_maybe TcRhoType
ty of
              Just (TyCon
tc, [TcRhoType]
_) -> TyCon -> Bool
isAlgTyCon TyCon
tc
              Maybe (TyCon, [TcRhoType])
Nothing      -> Bool
False

{-
Note [Splitting nested sigma types in mismatched function types]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When one applies a function to too few arguments, GHC tries to determine this
fact if possible so that it may give a helpful error message. It accomplishes
this by checking if the type of the applied function has more argument types
than supplied arguments.

Previously, GHC computed the number of argument types through tcSplitSigmaTy.
This is incorrect in the face of nested foralls, however!
This caused Ticket #13311, for instance:

  f :: forall a. (Monoid a) => forall b. (Monoid b) => Maybe a -> Maybe b

If one uses `f` like so:

  do { f; putChar 'a' }

Then tcSplitSigmaTy will decompose the type of `f` into:

  Tyvars: [a]
  Context: (Monoid a)
  Argument types: []
  Return type: forall b. Monoid b => Maybe a -> Maybe b

That is, it will conclude that there are *no* argument types, and since `f`
was given no arguments, it won't print a helpful error message. On the other
hand, tcSplitNestedSigmaTys correctly decomposes `f`'s type down to:

  Tyvars: [a, b]
  Context: (Monoid a, Monoid b)
  Argument types: [Maybe a]
  Return type: Maybe b

So now GHC recognizes that `f` has one more argument type than it was actually
provided.
-}


{- *********************************************************************
*                                                                      *
             Misc utility functions
*                                                                      *
********************************************************************* -}

addLExprCtxt :: LHsExpr GhcRn -> TcRn a -> TcRn a
addLExprCtxt :: LHsExpr GhcRn -> TcRn a -> TcRn a
addLExprCtxt (L _ e) TcRn a
thing_inside = HsExpr GhcRn -> TcRn a -> TcRn a
forall a. HsExpr GhcRn -> TcRn a -> TcRn a
addExprCtxt HsExpr GhcRn
e TcRn a
thing_inside

addExprCtxt :: HsExpr GhcRn -> TcRn a -> TcRn a
addExprCtxt :: HsExpr GhcRn -> TcRn a -> TcRn a
addExprCtxt HsExpr GhcRn
e TcRn a
thing_inside
  = case HsExpr GhcRn
e of
      HsUnboundVar {} -> TcRn a
thing_inside
      HsExpr GhcRn
_ -> SDoc -> TcRn a -> TcRn a
forall a. SDoc -> TcM a -> TcM a
addErrCtxt (HsExpr GhcRn -> SDoc
exprCtxt HsExpr GhcRn
e) TcRn a
thing_inside
   -- The HsUnboundVar special case addresses situations like
   --    f x = _
   -- when we don't want to say "In the expression: _",
   -- because it is mentioned in the error message itself

exprCtxt :: HsExpr GhcRn -> SDoc
exprCtxt :: HsExpr GhcRn -> SDoc
exprCtxt HsExpr GhcRn
expr = SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"In the expression:") Int
2 (HsExpr GhcRn -> SDoc
forall a. Outputable a => a -> SDoc
ppr (HsExpr GhcRn -> HsExpr GhcRn
forall (p :: Pass). HsExpr (GhcPass p) -> HsExpr (GhcPass p)
stripParensHsExpr HsExpr GhcRn
expr))