{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}

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

-}

module GHC.Rename.HsType (
        -- Type related stuff
        rnHsType, rnLHsType, rnLHsTypes, rnContext, rnMaybeContext,
        rnLHsKind, rnLHsTypeArgs,
        rnHsSigType, rnHsWcType, rnHsPatSigTypeBindingVars,
        HsPatSigTypeScoping(..), rnHsSigWcType, rnHsPatSigType,
        newTyVarNameRn,
        rnConDeclFields,
        lookupField,
        rnLTyVar,

        rnScaledLHsType,

        -- Precence related stuff
        NegationHandling(..),
        mkOpAppRn, mkNegAppRn, mkOpFormRn, mkConOpPatRn,
        checkPrecMatch, checkSectionPrec,

        -- Binding related stuff
        bindHsOuterTyVarBndrs, bindHsForAllTelescope,
        bindLHsTyVarBndr, bindLHsTyVarBndrs, WarnUnusedForalls(..),
        rnImplicitTvOccs, bindSigTyVarsFV, bindHsQTyVars,
        FreeKiTyVars, filterInScopeM,
        extractHsTyRdrTyVars, extractHsTyRdrTyVarsKindVars,
        extractHsTysRdrTyVars, extractRdrKindSigVars,
        extractConDeclGADTDetailsTyVars, extractDataDefnKindVars,
        extractHsOuterTvBndrs, extractHsTyArgRdrKiTyVars,
        nubL, nubN
  ) where

import GHC.Prelude

import {-# SOURCE #-} GHC.Rename.Splice( rnSpliceType )

import GHC.Core.TyCo.FVs ( tyCoVarsOfTypeList )
import GHC.Hs
import GHC.Rename.Env
import GHC.Rename.Doc
import GHC.Rename.Utils  ( mapFvRn, bindLocalNamesFV
                         , typeAppErr, newLocalBndrRn, checkDupRdrNames
                         , checkShadowedRdrNames, warnForallIdentifier )
import GHC.Rename.Fixity ( lookupFieldFixityRn, lookupFixityRn
                         , lookupTyFixityRn )
import GHC.Rename.Unbound ( notInScopeErr, WhereLooking(WL_LocalOnly) )
import GHC.Tc.Errors.Types
import GHC.Tc.Errors.Ppr ( pprHsDocContext )
import GHC.Tc.Utils.Monad
import GHC.Types.Name.Reader
import GHC.Builtin.Names
import GHC.Types.Hint ( UntickedPromotedThing(..) )
import GHC.Types.Name
import GHC.Types.SrcLoc
import GHC.Types.Name.Set
import GHC.Types.FieldLabel
import GHC.Types.Error

import GHC.Utils.Misc
import GHC.Types.Fixity ( compareFixity, negateFixity
                        , Fixity(..), FixityDirection(..), LexicalFixity(..) )
import GHC.Types.Basic  ( TypeOrKind(..) )
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Utils.Panic.Plain
import GHC.Data.Maybe
import qualified GHC.LanguageExtensions as LangExt

import Language.Haskell.Syntax.Basic (FieldLabelString(..))

import Data.List (nubBy, partition)
import qualified Data.List.NonEmpty as NE
import Data.List.NonEmpty (NonEmpty(..))
import Control.Monad

{-
These type renamers are in a separate module, rather than in (say) GHC.Rename.Module,
to break several loops.

*********************************************************
*                                                       *
    HsSigWcType and HsPatSigType (i.e with wildcards)
*                                                       *
*********************************************************
-}

data HsPatSigTypeScoping
  = AlwaysBind
    -- ^ Always bind any free tyvars of the given type, regardless of whether we
    -- have a forall at the top.
    --
    -- For pattern type sigs, we /do/ want to bring those type
    -- variables into scope, even if there's a forall at the top which usually
    -- stops that happening, e.g:
    --
    -- > \ (x :: forall a. a -> b) -> e
    --
    -- Here we do bring 'b' into scope.
    --
    -- RULES can also use 'AlwaysBind', such as in the following example:
    --
    -- > {-# RULES \"f\" forall (x :: forall a. a -> b). f x = ... b ... #-}
    --
    -- This only applies to RULES that do not explicitly bind their type
    -- variables. If a RULE explicitly quantifies its type variables, then
    -- 'NeverBind' is used instead. See also
    -- @Note [Pattern signature binders and scoping]@ in "GHC.Hs.Type".
  | NeverBind
    -- ^ Never bind any free tyvars. This is used for RULES that have both
    -- explicit type and term variable binders, e.g.:
    --
    -- > {-# RULES \"const\" forall a. forall (x :: a) y. const x y = x #-}
    --
    -- The presence of the type variable binder @forall a.@ implies that the
    -- free variables in the types of the term variable binders @x@ and @y@
    -- are /not/ bound. In the example above, there are no such free variables,
    -- but if the user had written @(y :: b)@ instead of @y@ in the term
    -- variable binders, then @b@ would be rejected for being out of scope.
    -- See also @Note [Pattern signature binders and scoping]@ in
    -- "GHC.Hs.Type".

rnHsSigWcType :: HsDocContext
              -> LHsSigWcType GhcPs
              -> RnM (LHsSigWcType GhcRn, FreeVars)
rnHsSigWcType :: HsDocContext
-> LHsSigWcType GhcPs -> RnM (LHsSigWcType GhcRn, FreeVars)
rnHsSigWcType HsDocContext
doc (HsWC { hswc_body :: forall pass thing. HsWildCardBndrs pass thing -> thing
hswc_body =
    sig_ty :: LHsSigType GhcPs
sig_ty@(L SrcSpanAnnA
loc (HsSig{sig_bndrs :: forall pass. HsSigType pass -> HsOuterSigTyVarBndrs pass
sig_bndrs = HsOuterSigTyVarBndrs GhcPs
outer_bndrs, sig_body :: forall pass. HsSigType pass -> LHsType pass
sig_body = LHsType GhcPs
body_ty })) })
  = do { FreeKiTyVars
free_vars <- FreeKiTyVars -> RnM FreeKiTyVars
filterInScopeM (LHsSigType GhcPs -> FreeKiTyVars
extract_lhs_sig_ty LHsSigType GhcPs
sig_ty)
       ; (FreeKiTyVars
nwc_rdrs', FreeKiTyVars
imp_tv_nms) <- FreeKiTyVars -> RnM (FreeKiTyVars, FreeKiTyVars)
partition_nwcs FreeKiTyVars
free_vars
       ; let nwc_rdrs :: FreeKiTyVars
nwc_rdrs = FreeKiTyVars -> FreeKiTyVars
forall a l. Eq a => [GenLocated l a] -> [GenLocated l a]
nubL FreeKiTyVars
nwc_rdrs'
       ; HsDocContext
-> Maybe Any
-> FreeKiTyVars
-> HsOuterSigTyVarBndrs GhcPs
-> (HsOuterTyVarBndrs Specificity GhcRn
    -> RnM
         (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn)),
          FreeVars))
-> RnM
     (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn)),
      FreeVars)
forall flag assoc a.
OutputableBndrFlag flag 'Renamed =>
HsDocContext
-> Maybe assoc
-> FreeKiTyVars
-> HsOuterTyVarBndrs flag GhcPs
-> (HsOuterTyVarBndrs flag GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
bindHsOuterTyVarBndrs HsDocContext
doc Maybe Any
forall a. Maybe a
Nothing FreeKiTyVars
imp_tv_nms HsOuterSigTyVarBndrs GhcPs
outer_bndrs ((HsOuterTyVarBndrs Specificity GhcRn
  -> RnM
       (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn)),
        FreeVars))
 -> RnM
      (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn)),
       FreeVars))
-> (HsOuterTyVarBndrs Specificity GhcRn
    -> RnM
         (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn)),
          FreeVars))
-> RnM
     (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn)),
      FreeVars)
forall a b. (a -> b) -> a -> b
$ \HsOuterTyVarBndrs Specificity GhcRn
outer_bndrs' ->
    do { ([Name]
wcs, GenLocated SrcSpanAnnA (HsType GhcRn)
body_ty', FreeVars
fvs) <- HsDocContext
-> FreeKiTyVars
-> LHsType GhcPs
-> RnM ([Name], LHsType GhcRn, FreeVars)
rnWcBody HsDocContext
doc FreeKiTyVars
nwc_rdrs LHsType GhcPs
body_ty
       ; (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn)),
 FreeVars)
-> RnM
     (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn)),
      FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ( HsWC  { hswc_ext :: XHsWC GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn))
hswc_ext = [Name]
XHsWC GhcRn (GenLocated SrcSpanAnnA (HsSigType GhcRn))
wcs, hswc_body :: GenLocated SrcSpanAnnA (HsSigType GhcRn)
hswc_body = SrcSpanAnnA
-> HsSigType GhcRn -> GenLocated SrcSpanAnnA (HsSigType GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc (HsSigType GhcRn -> GenLocated SrcSpanAnnA (HsSigType GhcRn))
-> HsSigType GhcRn -> GenLocated SrcSpanAnnA (HsSigType GhcRn)
forall a b. (a -> b) -> a -> b
$
                HsSig { sig_ext :: XHsSig GhcRn
sig_ext = XHsSig GhcRn
NoExtField
noExtField
                      , sig_bndrs :: HsOuterTyVarBndrs Specificity GhcRn
sig_bndrs = HsOuterTyVarBndrs Specificity GhcRn
outer_bndrs', sig_body :: LHsType GhcRn
sig_body = LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
body_ty' }}
              , FreeVars
fvs) } }

rnHsPatSigType :: HsPatSigTypeScoping
               -> HsDocContext
               -> HsPatSigType GhcPs
               -> (HsPatSigType GhcRn -> RnM (a, FreeVars))
               -> RnM (a, FreeVars)
-- Used for
--   - Pattern type signatures, which are only allowed with ScopedTypeVariables
--   - Signatures on binders in a RULE, which are allowed even if
--     ScopedTypeVariables isn't enabled
-- Wildcards are allowed
--
-- See Note [Pattern signature binders and scoping] in GHC.Hs.Type
rnHsPatSigType :: forall a.
HsPatSigTypeScoping
-> HsDocContext
-> HsPatSigType GhcPs
-> (HsPatSigType GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnHsPatSigType HsPatSigTypeScoping
scoping HsDocContext
ctx HsPatSigType GhcPs
sig_ty HsPatSigType GhcRn -> RnM (a, FreeVars)
thing_inside
  = do { Bool
ty_sig_okay <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.ScopedTypeVariables
       ; Bool -> TcRnMessage -> TcRn ()
checkErr Bool
ty_sig_okay (HsPatSigType GhcPs -> TcRnMessage
unexpectedPatSigTypeErr HsPatSigType GhcPs
sig_ty)
       ; FreeKiTyVars
free_vars <- FreeKiTyVars -> RnM FreeKiTyVars
filterInScopeM (LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVars LHsType GhcPs
pat_sig_ty)
       ; (FreeKiTyVars
nwc_rdrs', FreeKiTyVars
tv_rdrs) <- FreeKiTyVars -> RnM (FreeKiTyVars, FreeKiTyVars)
partition_nwcs FreeKiTyVars
free_vars
       ; let nwc_rdrs :: FreeKiTyVars
nwc_rdrs = FreeKiTyVars -> FreeKiTyVars
forall a. Eq a => [LocatedN a] -> [LocatedN a]
nubN FreeKiTyVars
nwc_rdrs'
             implicit_bndrs :: FreeKiTyVars
implicit_bndrs = case HsPatSigTypeScoping
scoping of
               HsPatSigTypeScoping
AlwaysBind -> FreeKiTyVars
tv_rdrs
               HsPatSigTypeScoping
NeverBind  -> []
       ; Maybe Any
-> FreeKiTyVars
-> ([Name] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
forall assoc a.
Maybe assoc
-> FreeKiTyVars
-> ([Name] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnImplicitTvOccs Maybe Any
forall a. Maybe a
Nothing FreeKiTyVars
implicit_bndrs (([Name] -> RnM (a, FreeVars)) -> RnM (a, FreeVars))
-> ([Name] -> RnM (a, FreeVars)) -> RnM (a, FreeVars)
forall a b. (a -> b) -> a -> b
$ \ [Name]
imp_tvs ->
    do { ([Name]
nwcs, GenLocated SrcSpanAnnA (HsType GhcRn)
pat_sig_ty', FreeVars
fvs1) <- HsDocContext
-> FreeKiTyVars
-> LHsType GhcPs
-> RnM ([Name], LHsType GhcRn, FreeVars)
rnWcBody HsDocContext
ctx FreeKiTyVars
nwc_rdrs LHsType GhcPs
pat_sig_ty
       ; let sig_names :: HsPSRn
sig_names = HsPSRn { hsps_nwcs :: [Name]
hsps_nwcs = [Name]
nwcs, hsps_imp_tvs :: [Name]
hsps_imp_tvs = [Name]
imp_tvs }
             sig_ty' :: HsPatSigType GhcRn
sig_ty'   = HsPS { hsps_ext :: XHsPS GhcRn
hsps_ext = XHsPS GhcRn
HsPSRn
sig_names, hsps_body :: LHsType GhcRn
hsps_body = LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
pat_sig_ty' }
       ; (a
res, FreeVars
fvs2) <- HsPatSigType GhcRn -> RnM (a, FreeVars)
thing_inside HsPatSigType GhcRn
sig_ty'
       ; (a, FreeVars) -> RnM (a, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
res, FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) } }
  where
    pat_sig_ty :: LHsType GhcPs
pat_sig_ty = HsPatSigType GhcPs -> LHsType GhcPs
forall pass. HsPatSigType pass -> LHsType pass
hsPatSigType HsPatSigType GhcPs
sig_ty

rnHsWcType :: HsDocContext -> LHsWcType GhcPs -> RnM (LHsWcType GhcRn, FreeVars)
rnHsWcType :: HsDocContext -> LHsWcType GhcPs -> RnM (LHsWcType GhcRn, FreeVars)
rnHsWcType HsDocContext
ctxt (HsWC { hswc_body :: forall pass thing. HsWildCardBndrs pass thing -> thing
hswc_body = LHsType GhcPs
hs_ty })
  = do { FreeKiTyVars
free_vars <- FreeKiTyVars -> RnM FreeKiTyVars
filterInScopeM (LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVars LHsType GhcPs
hs_ty)
       ; (FreeKiTyVars
nwc_rdrs', FreeKiTyVars
_) <- FreeKiTyVars -> RnM (FreeKiTyVars, FreeKiTyVars)
partition_nwcs FreeKiTyVars
free_vars
       ; let nwc_rdrs :: FreeKiTyVars
nwc_rdrs = FreeKiTyVars -> FreeKiTyVars
forall a l. Eq a => [GenLocated l a] -> [GenLocated l a]
nubL FreeKiTyVars
nwc_rdrs'
       ; ([Name]
wcs, GenLocated SrcSpanAnnA (HsType GhcRn)
hs_ty', FreeVars
fvs) <- HsDocContext
-> FreeKiTyVars
-> LHsType GhcPs
-> RnM ([Name], LHsType GhcRn, FreeVars)
rnWcBody HsDocContext
ctxt FreeKiTyVars
nwc_rdrs LHsType GhcPs
hs_ty
       ; let sig_ty' :: HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
sig_ty' = HsWC { hswc_ext :: XHsWC GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
hswc_ext = [Name]
XHsWC GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
wcs, hswc_body :: GenLocated SrcSpanAnnA (HsType GhcRn)
hswc_body = GenLocated SrcSpanAnnA (HsType GhcRn)
hs_ty' }
       ; (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn)),
 FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn)),
      FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsWildCardBndrs GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
sig_ty', FreeVars
fvs) }

-- Similar to rnHsWcType, but rather than requiring free variables in the type to
-- already be in scope, we are going to require them not to be in scope,
-- and we bind them.
rnHsPatSigTypeBindingVars :: HsDocContext
                          -> HsPatSigType GhcPs
                          -> (HsPatSigType GhcRn -> RnM (r, FreeVars))
                          -> RnM (r, FreeVars)
rnHsPatSigTypeBindingVars :: forall r.
HsDocContext
-> HsPatSigType GhcPs
-> (HsPatSigType GhcRn -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
rnHsPatSigTypeBindingVars HsDocContext
ctxt HsPatSigType GhcPs
sigType HsPatSigType GhcRn -> RnM (r, FreeVars)
thing_inside = case HsPatSigType GhcPs
sigType of
  (HsPS { hsps_body :: forall pass. HsPatSigType pass -> LHsType pass
hsps_body = LHsType GhcPs
hs_ty }) -> do
    LocalRdrEnv
rdr_env <- RnM LocalRdrEnv
getLocalRdrEnv
    let (FreeKiTyVars
varsInScope, FreeKiTyVars
varsNotInScope) =
          (GenLocated SrcSpanAnnN RdrName -> Bool)
-> FreeKiTyVars -> (FreeKiTyVars, FreeKiTyVars)
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (LocalRdrEnv -> RdrName -> Bool
inScope LocalRdrEnv
rdr_env (RdrName -> Bool)
-> (GenLocated SrcSpanAnnN RdrName -> RdrName)
-> GenLocated SrcSpanAnnN RdrName
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnN RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc) (LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVars LHsType GhcPs
hs_ty)
    -- TODO: Resolve and remove this comment.
    -- This next bit is in some contention. The original proposal #126
    -- (https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0126-type-applications-in-patterns.rst)
    -- says that in-scope variables are fine here: don't bind them, just use
    -- the existing vars, like in type signatures. An amendment #291
    -- (https://github.com/ghc-proposals/ghc-proposals/pull/291) says that the
    -- use of an in-scope variable should *shadow* an in-scope tyvar, like in
    -- terms. In an effort to make forward progress, the current implementation
    -- just rejects any use of an in-scope variable, meaning GHC will accept
    -- a subset of programs common to both variants. If this comment still exists
    -- in mid-to-late 2021 or thereafter, we have done a poor job on following
    -- up on this point.
    -- Example:
    --   f :: forall a. ...
    --   f (MkT @a ...) = ...
    -- Should the inner `a` refer to the outer one? shadow it? We are, as yet, undecided,
    -- so we currently reject.
    Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (FreeKiTyVars -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null FreeKiTyVars
varsInScope)) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
      TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ FreeKiTyVars -> TcRnMessage
TcRnBindVarAlreadyInScope FreeKiTyVars
varsInScope
    (FreeKiTyVars
wcVars, FreeKiTyVars
ibVars) <- FreeKiTyVars -> RnM (FreeKiTyVars, FreeKiTyVars)
partition_nwcs FreeKiTyVars
varsNotInScope
    HsDocContext
-> Maybe Any
-> FreeKiTyVars
-> ([Name] -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
forall assoc a.
HsDocContext
-> Maybe assoc
-> FreeKiTyVars
-> ([Name] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnImplicitTvBndrs HsDocContext
ctxt Maybe Any
forall a. Maybe a
Nothing FreeKiTyVars
ibVars (([Name] -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> ([Name] -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
forall a b. (a -> b) -> a -> b
$ \ [Name]
ibVars' -> do
      ([Name]
wcVars', GenLocated SrcSpanAnnA (HsType GhcRn)
hs_ty', FreeVars
fvs) <- HsDocContext
-> FreeKiTyVars
-> LHsType GhcPs
-> RnM ([Name], LHsType GhcRn, FreeVars)
rnWcBody HsDocContext
ctxt FreeKiTyVars
wcVars LHsType GhcPs
hs_ty
      let sig_ty :: HsPatSigType GhcRn
sig_ty = HsPS
            { hsps_body :: LHsType GhcRn
hsps_body = LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
hs_ty'
            , hsps_ext :: XHsPS GhcRn
hsps_ext = HsPSRn
              { hsps_nwcs :: [Name]
hsps_nwcs    = [Name]
wcVars'
              , hsps_imp_tvs :: [Name]
hsps_imp_tvs = [Name]
ibVars'
              }
            }
      (r
res, FreeVars
fvs') <- HsPatSigType GhcRn -> RnM (r, FreeVars)
thing_inside HsPatSigType GhcRn
sig_ty
      (r, FreeVars) -> RnM (r, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (r
res, FreeVars
fvs FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs')

rnWcBody :: HsDocContext -> [LocatedN RdrName] -> LHsType GhcPs
         -> RnM ([Name], LHsType GhcRn, FreeVars)
rnWcBody :: HsDocContext
-> FreeKiTyVars
-> LHsType GhcPs
-> RnM ([Name], LHsType GhcRn, FreeVars)
rnWcBody HsDocContext
ctxt FreeKiTyVars
nwc_rdrs LHsType GhcPs
hs_ty
  = do { [Name]
nwcs <- (GenLocated SrcSpanAnnN RdrName
 -> IOEnv (Env TcGblEnv TcLclEnv) Name)
-> FreeKiTyVars -> IOEnv (Env TcGblEnv TcLclEnv) [Name]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
newLocalBndrRn FreeKiTyVars
nwc_rdrs
       ; let env :: RnTyKiEnv
env = RTKE { rtke_level :: TypeOrKind
rtke_level = TypeOrKind
TypeLevel
                        , rtke_what :: RnTyKiWhat
rtke_what  = RnTyKiWhat
RnTypeBody
                        , rtke_nwcs :: FreeVars
rtke_nwcs  = [Name] -> FreeVars
mkNameSet [Name]
nwcs
                        , rtke_ctxt :: HsDocContext
rtke_ctxt  = HsDocContext
ctxt }
       ; (GenLocated SrcSpanAnnA (HsType GhcRn)
hs_ty', FreeVars
fvs) <- [Name]
-> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
-> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
forall a. [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
bindLocalNamesFV [Name]
nwcs (RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
 -> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars))
-> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
-> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
forall a b. (a -> b) -> a -> b
$
                          RnTyKiEnv
-> GenLocated SrcSpanAnnA (HsType GhcPs)
-> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
forall {ann}.
RnTyKiEnv
-> GenLocated (SrcSpanAnn' ann) (HsType GhcPs)
-> TcRn (GenLocated (SrcSpanAnn' ann) (HsType GhcRn), FreeVars)
rn_lty RnTyKiEnv
env LHsType GhcPs
GenLocated SrcSpanAnnA (HsType GhcPs)
hs_ty
       ; ([Name], GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     ([Name], GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Name]
nwcs, GenLocated SrcSpanAnnA (HsType GhcRn)
hs_ty', FreeVars
fvs) }
  where
    rn_lty :: RnTyKiEnv
-> GenLocated (SrcSpanAnn' ann) (HsType GhcPs)
-> TcRn (GenLocated (SrcSpanAnn' ann) (HsType GhcRn), FreeVars)
rn_lty RnTyKiEnv
env (L SrcSpanAnn' ann
loc HsType GhcPs
hs_ty)
      = SrcSpanAnn' ann
-> TcRn (GenLocated (SrcSpanAnn' ann) (HsType GhcRn), FreeVars)
-> TcRn (GenLocated (SrcSpanAnn' ann) (HsType GhcRn), FreeVars)
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnn' ann
loc (TcRn (GenLocated (SrcSpanAnn' ann) (HsType GhcRn), FreeVars)
 -> TcRn (GenLocated (SrcSpanAnn' ann) (HsType GhcRn), FreeVars))
-> TcRn (GenLocated (SrcSpanAnn' ann) (HsType GhcRn), FreeVars)
-> TcRn (GenLocated (SrcSpanAnn' ann) (HsType GhcRn), FreeVars)
forall a b. (a -> b) -> a -> b
$
        do { (HsType GhcRn
hs_ty', FreeVars
fvs) <- RnTyKiEnv -> HsType GhcPs -> RnM (HsType GhcRn, FreeVars)
rn_ty RnTyKiEnv
env HsType GhcPs
hs_ty
           ; (GenLocated (SrcSpanAnn' ann) (HsType GhcRn), FreeVars)
-> TcRn (GenLocated (SrcSpanAnn' ann) (HsType GhcRn), FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpanAnn' ann
-> HsType GhcRn -> GenLocated (SrcSpanAnn' ann) (HsType GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnn' ann
loc HsType GhcRn
hs_ty', FreeVars
fvs) }

    rn_ty :: RnTyKiEnv -> HsType GhcPs -> RnM (HsType GhcRn, FreeVars)
    -- A lot of faff just to allow the extra-constraints wildcard to appear
    rn_ty :: RnTyKiEnv -> HsType GhcPs -> RnM (HsType GhcRn, FreeVars)
rn_ty RnTyKiEnv
env (HsForAllTy { hst_tele :: forall pass. HsType pass -> HsForAllTelescope pass
hst_tele = HsForAllTelescope GhcPs
tele, hst_body :: forall pass. HsType pass -> LHsType pass
hst_body = LHsType GhcPs
hs_body })
      = HsDocContext
-> HsForAllTelescope GhcPs
-> (HsForAllTelescope GhcRn -> RnM (HsType GhcRn, FreeVars))
-> RnM (HsType GhcRn, FreeVars)
forall a.
HsDocContext
-> HsForAllTelescope GhcPs
-> (HsForAllTelescope GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
bindHsForAllTelescope (RnTyKiEnv -> HsDocContext
rtke_ctxt RnTyKiEnv
env) HsForAllTelescope GhcPs
tele ((HsForAllTelescope GhcRn -> RnM (HsType GhcRn, FreeVars))
 -> RnM (HsType GhcRn, FreeVars))
-> (HsForAllTelescope GhcRn -> RnM (HsType GhcRn, FreeVars))
-> RnM (HsType GhcRn, FreeVars)
forall a b. (a -> b) -> a -> b
$ \ HsForAllTelescope GhcRn
tele' ->
        do { (GenLocated SrcSpanAnnA (HsType GhcRn)
hs_body', FreeVars
fvs) <- RnTyKiEnv
-> GenLocated SrcSpanAnnA (HsType GhcPs)
-> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
forall {ann}.
RnTyKiEnv
-> GenLocated (SrcSpanAnn' ann) (HsType GhcPs)
-> TcRn (GenLocated (SrcSpanAnn' ann) (HsType GhcRn), FreeVars)
rn_lty RnTyKiEnv
env LHsType GhcPs
GenLocated SrcSpanAnnA (HsType GhcPs)
hs_body
           ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsForAllTy { hst_xforall :: XForAllTy GhcRn
hst_xforall = XForAllTy GhcRn
NoExtField
noExtField
                                , hst_tele :: HsForAllTelescope GhcRn
hst_tele = HsForAllTelescope GhcRn
tele', hst_body :: LHsType GhcRn
hst_body = LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
hs_body' }
                    , FreeVars
fvs) }

    rn_ty RnTyKiEnv
env (HsQualTy { hst_ctxt :: forall pass. HsType pass -> LHsContext pass
hst_ctxt = L SrcSpanAnnC
cx [GenLocated SrcSpanAnnA (HsType GhcPs)]
hs_ctxt
                        , hst_body :: forall pass. HsType pass -> LHsType pass
hst_body = LHsType GhcPs
hs_ty })
      | Just ([GenLocated SrcSpanAnnA (HsType GhcPs)]
hs_ctxt1, GenLocated SrcSpanAnnA (HsType GhcPs)
hs_ctxt_last) <- [GenLocated SrcSpanAnnA (HsType GhcPs)]
-> Maybe
     ([GenLocated SrcSpanAnnA (HsType GhcPs)],
      GenLocated SrcSpanAnnA (HsType GhcPs))
forall a. [a] -> Maybe ([a], a)
snocView [GenLocated SrcSpanAnnA (HsType GhcPs)]
hs_ctxt
      , L SrcSpanAnnA
lx (HsWildCardTy XWildCardTy GhcPs
_)  <- LHsType GhcPs -> LHsType GhcPs
forall (p :: Pass). LHsType (GhcPass p) -> LHsType (GhcPass p)
ignoreParens LHsType GhcPs
GenLocated SrcSpanAnnA (HsType GhcPs)
hs_ctxt_last
      = do { ([GenLocated SrcSpanAnnA (HsType GhcRn)]
hs_ctxt1', FreeVars
fvs1) <- (GenLocated SrcSpanAnnA (HsType GhcPs)
 -> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars))
-> [GenLocated SrcSpanAnnA (HsType GhcPs)]
-> RnM ([GenLocated SrcSpanAnnA (HsType GhcRn)], FreeVars)
forall (f :: * -> *) a b.
Traversable f =>
(a -> RnM (b, FreeVars)) -> f a -> RnM (f b, FreeVars)
mapFvRn (RnTyKiEnv
-> GenLocated SrcSpanAnnA (HsType GhcPs)
-> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
rn_top_constraint RnTyKiEnv
env) [GenLocated SrcSpanAnnA (HsType GhcPs)]
hs_ctxt1
           ; SrcSpanAnnA -> TcRn () -> TcRn ()
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
lx (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ RnTyKiEnv -> HsContext GhcPs -> TcRn ()
checkExtraConstraintWildCard RnTyKiEnv
env HsContext GhcPs
[GenLocated SrcSpanAnnA (HsType GhcPs)]
hs_ctxt1
           ; let hs_ctxt' :: [GenLocated SrcSpanAnnA (HsType GhcRn)]
hs_ctxt' = [GenLocated SrcSpanAnnA (HsType GhcRn)]
hs_ctxt1' [GenLocated SrcSpanAnnA (HsType GhcRn)]
-> [GenLocated SrcSpanAnnA (HsType GhcRn)]
-> [GenLocated SrcSpanAnnA (HsType GhcRn)]
forall a. [a] -> [a] -> [a]
++ [SrcSpanAnnA
-> HsType GhcRn -> GenLocated SrcSpanAnnA (HsType GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
lx (XWildCardTy GhcRn -> HsType GhcRn
forall pass. XWildCardTy pass -> HsType pass
HsWildCardTy XWildCardTy GhcRn
NoExtField
noExtField)]
           ; (GenLocated SrcSpanAnnA (HsType GhcRn)
hs_ty', FreeVars
fvs2) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
hs_ty
           ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsQualTy { hst_xqual :: XQualTy GhcRn
hst_xqual = XQualTy GhcRn
NoExtField
noExtField
                              , hst_ctxt :: LHsContext GhcRn
hst_ctxt = SrcSpanAnnC
-> [GenLocated SrcSpanAnnA (HsType GhcRn)]
-> GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnC
cx [GenLocated SrcSpanAnnA (HsType GhcRn)]
hs_ctxt'
                              , hst_body :: LHsType GhcRn
hst_body = LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
hs_ty' }
                    , FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) }

      | Bool
otherwise
      = do { ([GenLocated SrcSpanAnnA (HsType GhcRn)]
hs_ctxt', FreeVars
fvs1) <- (GenLocated SrcSpanAnnA (HsType GhcPs)
 -> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars))
-> [GenLocated SrcSpanAnnA (HsType GhcPs)]
-> RnM ([GenLocated SrcSpanAnnA (HsType GhcRn)], FreeVars)
forall (f :: * -> *) a b.
Traversable f =>
(a -> RnM (b, FreeVars)) -> f a -> RnM (f b, FreeVars)
mapFvRn (RnTyKiEnv
-> GenLocated SrcSpanAnnA (HsType GhcPs)
-> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
rn_top_constraint RnTyKiEnv
env) [GenLocated SrcSpanAnnA (HsType GhcPs)]
hs_ctxt
           ; (GenLocated SrcSpanAnnA (HsType GhcRn)
hs_ty', FreeVars
fvs2)   <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
hs_ty
           ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsQualTy { hst_xqual :: XQualTy GhcRn
hst_xqual = XQualTy GhcRn
NoExtField
noExtField
                              , hst_ctxt :: LHsContext GhcRn
hst_ctxt = SrcSpanAnnC
-> [GenLocated SrcSpanAnnA (HsType GhcRn)]
-> GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnC
cx [GenLocated SrcSpanAnnA (HsType GhcRn)]
hs_ctxt'
                              , hst_body :: LHsType GhcRn
hst_body = LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
hs_ty' }
                    , FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) }


    rn_ty RnTyKiEnv
env HsType GhcPs
hs_ty = RnTyKiEnv -> HsType GhcPs -> RnM (HsType GhcRn, FreeVars)
rnHsTyKi RnTyKiEnv
env HsType GhcPs
hs_ty

    rn_top_constraint :: RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rn_top_constraint RnTyKiEnv
env = RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi (RnTyKiEnv
env { rtke_what = RnTopConstraint })


checkExtraConstraintWildCard :: RnTyKiEnv -> HsContext GhcPs -> RnM ()
-- Rename the extra-constraint spot in a type signature
--    (blah, _) => type
-- Check that extra-constraints are allowed at all, and
-- if so that it's an anonymous wildcard
checkExtraConstraintWildCard :: RnTyKiEnv -> HsContext GhcPs -> TcRn ()
checkExtraConstraintWildCard RnTyKiEnv
env HsContext GhcPs
hs_ctxt
  = RnTyKiEnv -> Maybe Name -> Maybe BadAnonWildcardContext -> TcRn ()
checkWildCard RnTyKiEnv
env Maybe Name
forall a. Maybe a
Nothing Maybe BadAnonWildcardContext
mb_bad
  where
    mb_bad :: Maybe BadAnonWildcardContext
mb_bad | Bool -> Bool
not (RnTyKiEnv -> Bool
extraConstraintWildCardsAllowed RnTyKiEnv
env)
           = BadAnonWildcardContext -> Maybe BadAnonWildcardContext
forall a. a -> Maybe a
Just (BadAnonWildcardContext -> Maybe BadAnonWildcardContext)
-> BadAnonWildcardContext -> Maybe BadAnonWildcardContext
forall a b. (a -> b) -> a -> b
$ SoleExtraConstraintWildcardAllowed -> BadAnonWildcardContext
ExtraConstraintWildcardNotAllowed
                      SoleExtraConstraintWildcardAllowed
SoleExtraConstraintWildcardNotAllowed
             -- Currently, we do not allow wildcards in their full glory in
             -- standalone deriving declarations. We only allow a single
             -- extra-constraints wildcard à la:
             --
             --   deriving instance _ => Eq (Foo a)
             --
             -- i.e., we don't support things like
             --
             --   deriving instance (Eq a, _) => Eq (Foo a)
           | DerivDeclCtx {} <- RnTyKiEnv -> HsDocContext
rtke_ctxt RnTyKiEnv
env
           , Bool -> Bool
not ([GenLocated SrcSpanAnnA (HsType GhcPs)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null HsContext GhcPs
[GenLocated SrcSpanAnnA (HsType GhcPs)]
hs_ctxt)
           = BadAnonWildcardContext -> Maybe BadAnonWildcardContext
forall a. a -> Maybe a
Just (BadAnonWildcardContext -> Maybe BadAnonWildcardContext)
-> BadAnonWildcardContext -> Maybe BadAnonWildcardContext
forall a b. (a -> b) -> a -> b
$ SoleExtraConstraintWildcardAllowed -> BadAnonWildcardContext
ExtraConstraintWildcardNotAllowed
                      SoleExtraConstraintWildcardAllowed
SoleExtraConstraintWildcardAllowed
           | Bool
otherwise
           = Maybe BadAnonWildcardContext
forall a. Maybe a
Nothing

extraConstraintWildCardsAllowed :: RnTyKiEnv -> Bool
extraConstraintWildCardsAllowed :: RnTyKiEnv -> Bool
extraConstraintWildCardsAllowed RnTyKiEnv
env
  = case RnTyKiEnv -> HsDocContext
rtke_ctxt RnTyKiEnv
env of
      TypeSigCtx {}       -> Bool
True
      ExprWithTySigCtx {} -> Bool
True
      DerivDeclCtx {}     -> Bool
True
      StandaloneKindSigCtx {} -> Bool
False  -- See Note [Wildcards in standalone kind signatures] in "GHC.Hs.Decls"
      HsDocContext
_                   -> Bool
False

-- | When the NamedWildCards extension is enabled, partition_nwcs
-- removes type variables that start with an underscore from the
-- FreeKiTyVars in the argument and returns them in a separate list.
-- When the extension is disabled, the function returns the argument
-- and empty list.  See Note [Renaming named wild cards]
partition_nwcs :: FreeKiTyVars -> RnM ([LocatedN RdrName], FreeKiTyVars)
partition_nwcs :: FreeKiTyVars -> RnM (FreeKiTyVars, FreeKiTyVars)
partition_nwcs FreeKiTyVars
free_vars
  = do { Bool
wildcards_enabled <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.NamedWildCards
       ; (FreeKiTyVars, FreeKiTyVars) -> RnM (FreeKiTyVars, FreeKiTyVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((FreeKiTyVars, FreeKiTyVars) -> RnM (FreeKiTyVars, FreeKiTyVars))
-> (FreeKiTyVars, FreeKiTyVars) -> RnM (FreeKiTyVars, FreeKiTyVars)
forall a b. (a -> b) -> a -> b
$
           if Bool
wildcards_enabled
           then (GenLocated SrcSpanAnnN RdrName -> Bool)
-> FreeKiTyVars -> (FreeKiTyVars, FreeKiTyVars)
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition GenLocated SrcSpanAnnN RdrName -> Bool
is_wildcard FreeKiTyVars
free_vars
           else ([], FreeKiTyVars
free_vars) }
  where
     is_wildcard :: LocatedN RdrName -> Bool
     is_wildcard :: GenLocated SrcSpanAnnN RdrName -> Bool
is_wildcard GenLocated SrcSpanAnnN RdrName
rdr = OccName -> Bool
startsWithUnderscore (RdrName -> OccName
rdrNameOcc (GenLocated SrcSpanAnnN RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnN RdrName
rdr))

{- Note [Renaming named wild cards]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Identifiers starting with an underscore are always parsed as type variables.
It is only here in the renamer that we give the special treatment.
See Note [The wildcard story for types] in GHC.Hs.Type.

It's easy!  When we collect the implicitly bound type variables, ready
to bring them into scope, and NamedWildCards is on, we partition the
variables into the ones that start with an underscore (the named
wildcards) and the rest. Then we just add them to the hswc_wcs field
of the HsWildCardBndrs structure, and we are done.


*********************************************************
*                                                       *
           HsSigType (i.e. no wildcards)
*                                                       *
****************************************************** -}

rnHsSigType :: HsDocContext
            -> TypeOrKind
            -> LHsSigType GhcPs
            -> RnM (LHsSigType GhcRn, FreeVars)
-- Used for source-language type signatures
-- that cannot have wildcards
rnHsSigType :: HsDocContext
-> TypeOrKind
-> LHsSigType GhcPs
-> RnM (LHsSigType GhcRn, FreeVars)
rnHsSigType HsDocContext
ctx TypeOrKind
level
    (L SrcSpanAnnA
loc sig_ty :: HsSigType GhcPs
sig_ty@(HsSig { sig_bndrs :: forall pass. HsSigType pass -> HsOuterSigTyVarBndrs pass
sig_bndrs = HsOuterSigTyVarBndrs GhcPs
outer_bndrs, sig_body :: forall pass. HsSigType pass -> LHsType pass
sig_body = LHsType GhcPs
body }))
  = SrcSpanAnnA
-> RnM (LHsSigType GhcRn, FreeVars)
-> RnM (LHsSigType GhcRn, FreeVars)
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc (RnM (LHsSigType GhcRn, FreeVars)
 -> RnM (LHsSigType GhcRn, FreeVars))
-> RnM (LHsSigType GhcRn, FreeVars)
-> RnM (LHsSigType GhcRn, FreeVars)
forall a b. (a -> b) -> a -> b
$
    do { String -> SDoc -> TcRn ()
traceRn String
"rnHsSigType" (HsSigType GhcPs -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsSigType GhcPs
sig_ty)
       ; case HsOuterSigTyVarBndrs GhcPs
outer_bndrs of
           HsOuterExplicit{} -> RnTyKiEnv -> HsTypeOrSigType GhcPs -> TcRn ()
checkPolyKinds RnTyKiEnv
env (HsSigType GhcPs -> HsTypeOrSigType GhcPs
forall p. HsSigType p -> HsTypeOrSigType p
HsSigType HsSigType GhcPs
sig_ty)
           HsOuterImplicit{} -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
       ; FreeKiTyVars
imp_vars <- FreeKiTyVars -> RnM FreeKiTyVars
filterInScopeM (FreeKiTyVars -> RnM FreeKiTyVars)
-> FreeKiTyVars -> RnM FreeKiTyVars
forall a b. (a -> b) -> a -> b
$ LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVars LHsType GhcPs
body
       ; HsDocContext
-> Maybe Any
-> FreeKiTyVars
-> HsOuterSigTyVarBndrs GhcPs
-> (HsOuterTyVarBndrs Specificity GhcRn
    -> RnM (GenLocated SrcSpanAnnA (HsSigType GhcRn), FreeVars))
-> RnM (GenLocated SrcSpanAnnA (HsSigType GhcRn), FreeVars)
forall flag assoc a.
OutputableBndrFlag flag 'Renamed =>
HsDocContext
-> Maybe assoc
-> FreeKiTyVars
-> HsOuterTyVarBndrs flag GhcPs
-> (HsOuterTyVarBndrs flag GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
bindHsOuterTyVarBndrs HsDocContext
ctx Maybe Any
forall a. Maybe a
Nothing FreeKiTyVars
imp_vars HsOuterSigTyVarBndrs GhcPs
outer_bndrs ((HsOuterTyVarBndrs Specificity GhcRn
  -> RnM (GenLocated SrcSpanAnnA (HsSigType GhcRn), FreeVars))
 -> RnM (GenLocated SrcSpanAnnA (HsSigType GhcRn), FreeVars))
-> (HsOuterTyVarBndrs Specificity GhcRn
    -> RnM (GenLocated SrcSpanAnnA (HsSigType GhcRn), FreeVars))
-> RnM (GenLocated SrcSpanAnnA (HsSigType GhcRn), FreeVars)
forall a b. (a -> b) -> a -> b
$ \HsOuterTyVarBndrs Specificity GhcRn
outer_bndrs' ->
    do { (GenLocated SrcSpanAnnA (HsType GhcRn)
body', FreeVars
fvs) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
body

       ; (GenLocated SrcSpanAnnA (HsSigType GhcRn), FreeVars)
-> RnM (GenLocated SrcSpanAnnA (HsSigType GhcRn), FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ( SrcSpanAnnA
-> HsSigType GhcRn -> GenLocated SrcSpanAnnA (HsSigType GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc (HsSigType GhcRn -> GenLocated SrcSpanAnnA (HsSigType GhcRn))
-> HsSigType GhcRn -> GenLocated SrcSpanAnnA (HsSigType GhcRn)
forall a b. (a -> b) -> a -> b
$ HsSig { sig_ext :: XHsSig GhcRn
sig_ext = XHsSig GhcRn
NoExtField
noExtField
                                , sig_bndrs :: HsOuterTyVarBndrs Specificity GhcRn
sig_bndrs = HsOuterTyVarBndrs Specificity GhcRn
outer_bndrs', sig_body :: LHsType GhcRn
sig_body = LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
body' }
                , FreeVars
fvs ) } }
  where
    env :: RnTyKiEnv
env = HsDocContext -> TypeOrKind -> RnTyKiWhat -> RnTyKiEnv
mkTyKiEnv HsDocContext
ctx TypeOrKind
level RnTyKiWhat
RnTypeBody

-- | Create new renamed type variables corresponding to source-level ones.
-- Duplicates are permitted, but will be removed. This is intended especially for
-- the case of handling the implicitly bound free variables of a type signature.
rnImplicitTvOccs :: Maybe assoc
                 -- ^ @'Just' _@ => an associated type decl
                 -> FreeKiTyVars
                 -- ^ Surface-syntax free vars that we will implicitly bind.
                 -- May have duplicates, which are removed here.
                 -> ([Name] -> RnM (a, FreeVars))
                 -> RnM (a, FreeVars)
rnImplicitTvOccs :: forall assoc a.
Maybe assoc
-> FreeKiTyVars
-> ([Name] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnImplicitTvOccs Maybe assoc
mb_assoc FreeKiTyVars
implicit_vs_with_dups [Name] -> RnM (a, FreeVars)
thing_inside
  = do { let implicit_vs :: FreeKiTyVars
implicit_vs = FreeKiTyVars -> FreeKiTyVars
forall a. Eq a => [LocatedN a] -> [LocatedN a]
nubN FreeKiTyVars
implicit_vs_with_dups

       ; String -> SDoc -> TcRn ()
traceRn String
"rnImplicitTvOccs" (SDoc -> TcRn ()) -> SDoc -> TcRn ()
forall a b. (a -> b) -> a -> b
$
         [SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat [ FreeKiTyVars -> SDoc
forall a. Outputable a => a -> SDoc
ppr FreeKiTyVars
implicit_vs_with_dups, FreeKiTyVars -> SDoc
forall a. Outputable a => a -> SDoc
ppr FreeKiTyVars
implicit_vs ]

         -- Use the currently set SrcSpan as the new source location for each Name.
         -- See Note [Source locations for implicitly bound type variables].
       ; SrcSpan
loc <- TcRn SrcSpan
getSrcSpanM
       ; let loc' :: SrcSpanAnnN
loc' = SrcSpan -> SrcSpanAnnN
forall ann. SrcSpan -> SrcAnn ann
noAnnSrcSpan SrcSpan
loc
       ; [Name]
vars <- (GenLocated SrcSpanAnnN RdrName
 -> IOEnv (Env TcGblEnv TcLclEnv) Name)
-> FreeKiTyVars -> IOEnv (Env TcGblEnv TcLclEnv) [Name]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Maybe assoc
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
forall a.
Maybe a
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
newTyVarNameRnImplicit Maybe assoc
mb_assoc (GenLocated SrcSpanAnnN RdrName
 -> IOEnv (Env TcGblEnv TcLclEnv) Name)
-> (GenLocated SrcSpanAnnN RdrName
    -> GenLocated SrcSpanAnnN RdrName)
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SrcSpanAnnN -> RdrName -> GenLocated SrcSpanAnnN RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
loc' (RdrName -> GenLocated SrcSpanAnnN RdrName)
-> (GenLocated SrcSpanAnnN RdrName -> RdrName)
-> GenLocated SrcSpanAnnN RdrName
-> GenLocated SrcSpanAnnN RdrName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnN RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc) FreeKiTyVars
implicit_vs

       ; [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
forall a. [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
bindLocalNamesFV [Name]
vars (RnM (a, FreeVars) -> RnM (a, FreeVars))
-> RnM (a, FreeVars) -> RnM (a, FreeVars)
forall a b. (a -> b) -> a -> b
$
         [Name] -> RnM (a, FreeVars)
thing_inside [Name]
vars }

{-
Note [Source locations for implicitly bound type variables]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When bringing implicitly bound type variables into scope (in rnImplicitTvOccs),
we do something peculiar: we drop the original SrcSpan attached to each
variable and replace it with the currently set SrcSpan. Moreover, this new
SrcSpan is usually /less/ precise than the original one, and that's OK. To see
why this is done, consider the following example:

  f :: a -> b -> a

Suppose that a warning or error message needs to point to the SrcSpans of the
binding sites for `a` and `b`. But where /are/ they bound, anyway? Technically,
they're bound by an unwritten `forall` at the front of the type signature, but
there is no SrcSpan for that. We could point to the first occurrence of `a` as
the binding site for `a`, but that would make the first occurrence of `a`
special. Moreover, we don't want IDEs to confuse binding sites and occurrences.

As a result, we make the `SrcSpan`s for `a` and `b` span the entirety of the
type signature, since the type signature implicitly carries their binding
sites. This is less precise, but more accurate.
-}

-- | Create fresh type variables for binders, disallowing multiple occurrences of the same variable. Similar to `rnImplicitTvOccs` except that duplicate occurrences will
-- result in an error, and the source locations of the variables are not adjusted, as these variable occurrences are themselves the binding sites for the type variables,
-- rather than the variables being implicitly bound by a signature.
rnImplicitTvBndrs :: HsDocContext
                  -> Maybe assoc
                  -- ^ @'Just' _@ => an associated type decl
                  -> FreeKiTyVars
                  -- ^ Surface-syntax free vars that we will implicitly bind.
                  -- Duplicate variables will cause a compile-time error regarding repeated bindings.
                  -> ([Name] -> RnM (a, FreeVars))
                  -> RnM (a, FreeVars)
rnImplicitTvBndrs :: forall assoc a.
HsDocContext
-> Maybe assoc
-> FreeKiTyVars
-> ([Name] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnImplicitTvBndrs HsDocContext
ctx Maybe assoc
mb_assoc FreeKiTyVars
implicit_vs_with_dups [Name] -> RnM (a, FreeVars)
thing_inside
  = do { FreeKiTyVars
implicit_vs <- [NonEmpty (GenLocated SrcSpanAnnN RdrName)]
-> (NonEmpty (GenLocated SrcSpanAnnN RdrName)
    -> IOEnv (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnN RdrName))
-> RnM FreeKiTyVars
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM ((GenLocated SrcSpanAnnN RdrName -> RdrName)
-> FreeKiTyVars -> [NonEmpty (GenLocated SrcSpanAnnN RdrName)]
forall b a. Ord b => (a -> b) -> [a] -> [NonEmpty a]
NE.groupAllWith GenLocated SrcSpanAnnN RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc (FreeKiTyVars -> [NonEmpty (GenLocated SrcSpanAnnN RdrName)])
-> FreeKiTyVars -> [NonEmpty (GenLocated SrcSpanAnnN RdrName)]
forall a b. (a -> b) -> a -> b
$ FreeKiTyVars
implicit_vs_with_dups) ((NonEmpty (GenLocated SrcSpanAnnN RdrName)
  -> IOEnv (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnN RdrName))
 -> RnM FreeKiTyVars)
-> (NonEmpty (GenLocated SrcSpanAnnN RdrName)
    -> IOEnv (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnN RdrName))
-> RnM FreeKiTyVars
forall a b. (a -> b) -> a -> b
$ \case
           (GenLocated SrcSpanAnnN RdrName
x :| []) -> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnN RdrName)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return GenLocated SrcSpanAnnN RdrName
x
           (GenLocated SrcSpanAnnN RdrName
x :| FreeKiTyVars
_) -> do
             TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ HsDocContext -> GenLocated SrcSpanAnnN RdrName -> TcRnMessage
TcRnBindMultipleVariables HsDocContext
ctx GenLocated SrcSpanAnnN RdrName
x
             GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnN RdrName)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return GenLocated SrcSpanAnnN RdrName
x

       ; String -> SDoc -> TcRn ()
traceRn String
"rnImplicitTvBndrs" (SDoc -> TcRn ()) -> SDoc -> TcRn ()
forall a b. (a -> b) -> a -> b
$
         [SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat [ FreeKiTyVars -> SDoc
forall a. Outputable a => a -> SDoc
ppr FreeKiTyVars
implicit_vs_with_dups, FreeKiTyVars -> SDoc
forall a. Outputable a => a -> SDoc
ppr FreeKiTyVars
implicit_vs ]

       ; [Name]
vars <- (GenLocated SrcSpanAnnN RdrName
 -> IOEnv (Env TcGblEnv TcLclEnv) Name)
-> FreeKiTyVars -> IOEnv (Env TcGblEnv TcLclEnv) [Name]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Maybe assoc
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
forall a.
Maybe a
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
newTyVarNameRn Maybe assoc
mb_assoc) FreeKiTyVars
implicit_vs

       ; [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
forall a. [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
bindLocalNamesFV [Name]
vars (RnM (a, FreeVars) -> RnM (a, FreeVars))
-> RnM (a, FreeVars) -> RnM (a, FreeVars)
forall a b. (a -> b) -> a -> b
$
         [Name] -> RnM (a, FreeVars)
thing_inside [Name]
vars }

{- ******************************************************
*                                                       *
           LHsType and HsType
*                                                       *
****************************************************** -}

{-
rnHsType is here because we call it from loadInstDecl, and I didn't
want a gratuitous knot.
-}

data RnTyKiEnv
  = RTKE { RnTyKiEnv -> HsDocContext
rtke_ctxt  :: HsDocContext
         , RnTyKiEnv -> TypeOrKind
rtke_level :: TypeOrKind  -- Am I renaming a type or a kind?
         , RnTyKiEnv -> RnTyKiWhat
rtke_what  :: RnTyKiWhat  -- And within that what am I renaming?
         , RnTyKiEnv -> FreeVars
rtke_nwcs  :: NameSet     -- These are the in-scope named wildcards
    }

data RnTyKiWhat = RnTypeBody
                | RnTopConstraint   -- Top-level context of HsSigWcTypes
                | RnConstraint      -- All other constraints

instance Outputable RnTyKiEnv where
  ppr :: RnTyKiEnv -> SDoc
ppr (RTKE { rtke_level :: RnTyKiEnv -> TypeOrKind
rtke_level = TypeOrKind
lev, rtke_what :: RnTyKiEnv -> RnTyKiWhat
rtke_what = RnTyKiWhat
what
            , rtke_nwcs :: RnTyKiEnv -> FreeVars
rtke_nwcs = FreeVars
wcs, rtke_ctxt :: RnTyKiEnv -> HsDocContext
rtke_ctxt = HsDocContext
ctxt })
    = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"RTKE"
      SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc
braces ([SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
sep [ TypeOrKind -> SDoc
forall a. Outputable a => a -> SDoc
ppr TypeOrKind
lev, RnTyKiWhat -> SDoc
forall a. Outputable a => a -> SDoc
ppr RnTyKiWhat
what, FreeVars -> SDoc
forall a. Outputable a => a -> SDoc
ppr FreeVars
wcs
                      , HsDocContext -> SDoc
pprHsDocContext HsDocContext
ctxt ])

instance Outputable RnTyKiWhat where
  ppr :: RnTyKiWhat -> SDoc
ppr RnTyKiWhat
RnTypeBody      = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"RnTypeBody"
  ppr RnTyKiWhat
RnTopConstraint = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"RnTopConstraint"
  ppr RnTyKiWhat
RnConstraint    = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"RnConstraint"

mkTyKiEnv :: HsDocContext -> TypeOrKind -> RnTyKiWhat -> RnTyKiEnv
mkTyKiEnv :: HsDocContext -> TypeOrKind -> RnTyKiWhat -> RnTyKiEnv
mkTyKiEnv HsDocContext
cxt TypeOrKind
level RnTyKiWhat
what
 = RTKE { rtke_level :: TypeOrKind
rtke_level = TypeOrKind
level, rtke_nwcs :: FreeVars
rtke_nwcs = FreeVars
emptyNameSet
        , rtke_what :: RnTyKiWhat
rtke_what = RnTyKiWhat
what, rtke_ctxt :: HsDocContext
rtke_ctxt = HsDocContext
cxt }

isRnKindLevel :: RnTyKiEnv -> Bool
isRnKindLevel :: RnTyKiEnv -> Bool
isRnKindLevel (RTKE { rtke_level :: RnTyKiEnv -> TypeOrKind
rtke_level = TypeOrKind
KindLevel }) = Bool
True
isRnKindLevel RnTyKiEnv
_                                 = Bool
False

--------------
rnLHsType  :: HsDocContext -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsType :: HsDocContext -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsType HsDocContext
ctxt LHsType GhcPs
ty = RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi (HsDocContext -> TypeOrKind -> RnTyKiWhat -> RnTyKiEnv
mkTyKiEnv HsDocContext
ctxt TypeOrKind
TypeLevel RnTyKiWhat
RnTypeBody) LHsType GhcPs
ty

rnLHsTypes :: HsDocContext -> [LHsType GhcPs] -> RnM ([LHsType GhcRn], FreeVars)
rnLHsTypes :: HsDocContext -> HsContext GhcPs -> RnM ([LHsType GhcRn], FreeVars)
rnLHsTypes HsDocContext
doc HsContext GhcPs
tys = (GenLocated SrcSpanAnnA (HsType GhcPs)
 -> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars))
-> [GenLocated SrcSpanAnnA (HsType GhcPs)]
-> RnM ([GenLocated SrcSpanAnnA (HsType GhcRn)], FreeVars)
forall (f :: * -> *) a b.
Traversable f =>
(a -> RnM (b, FreeVars)) -> f a -> RnM (f b, FreeVars)
mapFvRn (HsDocContext -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsType HsDocContext
doc) HsContext GhcPs
[GenLocated SrcSpanAnnA (HsType GhcPs)]
tys

rnScaledLHsType :: HsDocContext -> HsScaled GhcPs (LHsType GhcPs)
                                  -> RnM (HsScaled GhcRn (LHsType GhcRn), FreeVars)
rnScaledLHsType :: HsDocContext
-> HsScaled GhcPs (LHsType GhcPs)
-> RnM (HsScaled GhcRn (LHsType GhcRn), FreeVars)
rnScaledLHsType HsDocContext
doc (HsScaled HsArrow GhcPs
w LHsType GhcPs
ty) = do
  (HsArrow GhcRn
w' , FreeVars
fvs_w) <- RnTyKiEnv -> HsArrow GhcPs -> RnM (HsArrow GhcRn, FreeVars)
rnHsArrow (HsDocContext -> TypeOrKind -> RnTyKiWhat -> RnTyKiEnv
mkTyKiEnv HsDocContext
doc TypeOrKind
TypeLevel RnTyKiWhat
RnTypeBody) HsArrow GhcPs
w
  (GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
fvs) <- HsDocContext -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsType HsDocContext
doc LHsType GhcPs
ty
  (HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn)), FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsArrow GhcRn
-> GenLocated SrcSpanAnnA (HsType GhcRn)
-> HsScaled GhcRn (GenLocated SrcSpanAnnA (HsType GhcRn))
forall pass a. HsArrow pass -> a -> HsScaled pass a
HsScaled HsArrow GhcRn
w' GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
fvs FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs_w)


rnHsType  :: HsDocContext -> HsType GhcPs -> RnM (HsType GhcRn, FreeVars)
rnHsType :: HsDocContext -> HsType GhcPs -> RnM (HsType GhcRn, FreeVars)
rnHsType HsDocContext
ctxt HsType GhcPs
ty = RnTyKiEnv -> HsType GhcPs -> RnM (HsType GhcRn, FreeVars)
rnHsTyKi (HsDocContext -> TypeOrKind -> RnTyKiWhat -> RnTyKiEnv
mkTyKiEnv HsDocContext
ctxt TypeOrKind
TypeLevel RnTyKiWhat
RnTypeBody) HsType GhcPs
ty

rnLHsKind  :: HsDocContext -> LHsKind GhcPs -> RnM (LHsKind GhcRn, FreeVars)
rnLHsKind :: HsDocContext -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsKind HsDocContext
ctxt LHsType GhcPs
kind = RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi (HsDocContext -> TypeOrKind -> RnTyKiWhat -> RnTyKiEnv
mkTyKiEnv HsDocContext
ctxt TypeOrKind
KindLevel RnTyKiWhat
RnTypeBody) LHsType GhcPs
kind

-- renaming a type only, not a kind
rnLHsTypeArg :: HsDocContext -> LHsTypeArg GhcPs
                -> RnM (LHsTypeArg GhcRn, FreeVars)
rnLHsTypeArg :: HsDocContext
-> LHsTypeArg GhcPs -> RnM (LHsTypeArg GhcRn, FreeVars)
rnLHsTypeArg HsDocContext
ctxt (HsValArg LHsType GhcPs
ty)
   = do { (GenLocated SrcSpanAnnA (HsType GhcRn)
tys_rn, FreeVars
fvs) <- HsDocContext -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsType HsDocContext
ctxt LHsType GhcPs
ty
        ; (HsArg
   GhcRn
   (GenLocated SrcSpanAnnA (HsType GhcRn))
   (GenLocated SrcSpanAnnA (HsType GhcRn)),
 FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsArg
        GhcRn
        (GenLocated SrcSpanAnnA (HsType GhcRn))
        (GenLocated SrcSpanAnnA (HsType GhcRn)),
      FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (GenLocated SrcSpanAnnA (HsType GhcRn)
-> HsArg
     GhcRn
     (GenLocated SrcSpanAnnA (HsType GhcRn))
     (GenLocated SrcSpanAnnA (HsType GhcRn))
forall p tm ty. tm -> HsArg p tm ty
HsValArg GenLocated SrcSpanAnnA (HsType GhcRn)
tys_rn, FreeVars
fvs) }
rnLHsTypeArg HsDocContext
ctxt (HsTypeArg LHsToken "@" GhcPs
l LHsType GhcPs
ki)
   = do { (GenLocated SrcSpanAnnA (HsType GhcRn)
kis_rn, FreeVars
fvs) <- HsDocContext -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsKind HsDocContext
ctxt LHsType GhcPs
ki
        ; (HsArg
   GhcRn
   (GenLocated SrcSpanAnnA (HsType GhcRn))
   (GenLocated SrcSpanAnnA (HsType GhcRn)),
 FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsArg
        GhcRn
        (GenLocated SrcSpanAnnA (HsType GhcRn))
        (GenLocated SrcSpanAnnA (HsType GhcRn)),
      FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsToken "@" GhcRn
-> GenLocated SrcSpanAnnA (HsType GhcRn)
-> HsArg
     GhcRn
     (GenLocated SrcSpanAnnA (HsType GhcRn))
     (GenLocated SrcSpanAnnA (HsType GhcRn))
forall p tm ty. LHsToken "@" p -> ty -> HsArg p tm ty
HsTypeArg LHsToken "@" GhcPs
LHsToken "@" GhcRn
l GenLocated SrcSpanAnnA (HsType GhcRn)
kis_rn, FreeVars
fvs) }
rnLHsTypeArg HsDocContext
_ (HsArgPar SrcSpan
sp)
   = (HsArg
   GhcRn
   (GenLocated SrcSpanAnnA (HsType GhcRn))
   (GenLocated SrcSpanAnnA (HsType GhcRn)),
 FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (HsArg
        GhcRn
        (GenLocated SrcSpanAnnA (HsType GhcRn))
        (GenLocated SrcSpanAnnA (HsType GhcRn)),
      FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpan
-> HsArg
     GhcRn
     (GenLocated SrcSpanAnnA (HsType GhcRn))
     (GenLocated SrcSpanAnnA (HsType GhcRn))
forall p tm ty. SrcSpan -> HsArg p tm ty
HsArgPar SrcSpan
sp, FreeVars
emptyFVs)

rnLHsTypeArgs :: HsDocContext -> [LHsTypeArg GhcPs]
                 -> RnM ([LHsTypeArg GhcRn], FreeVars)
rnLHsTypeArgs :: HsDocContext
-> [LHsTypeArg GhcPs] -> RnM ([LHsTypeArg GhcRn], FreeVars)
rnLHsTypeArgs HsDocContext
doc [LHsTypeArg GhcPs]
args = (HsArg
   GhcPs
   (GenLocated SrcSpanAnnA (HsType GhcPs))
   (GenLocated SrcSpanAnnA (HsType GhcPs))
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (HsArg
         GhcRn
         (GenLocated SrcSpanAnnA (HsType GhcRn))
         (GenLocated SrcSpanAnnA (HsType GhcRn)),
       FreeVars))
-> [HsArg
      GhcPs
      (GenLocated SrcSpanAnnA (HsType GhcPs))
      (GenLocated SrcSpanAnnA (HsType GhcPs))]
-> RnM
     ([HsArg
         GhcRn
         (GenLocated SrcSpanAnnA (HsType GhcRn))
         (GenLocated SrcSpanAnnA (HsType GhcRn))],
      FreeVars)
forall (f :: * -> *) a b.
Traversable f =>
(a -> RnM (b, FreeVars)) -> f a -> RnM (f b, FreeVars)
mapFvRn (HsDocContext
-> LHsTypeArg GhcPs -> RnM (LHsTypeArg GhcRn, FreeVars)
rnLHsTypeArg HsDocContext
doc) [LHsTypeArg GhcPs]
[HsArg
   GhcPs
   (GenLocated SrcSpanAnnA (HsType GhcPs))
   (GenLocated SrcSpanAnnA (HsType GhcPs))]
args

--------------
rnTyKiContext :: RnTyKiEnv -> LHsContext GhcPs
              -> RnM (LHsContext GhcRn, FreeVars)
rnTyKiContext :: RnTyKiEnv -> LHsContext GhcPs -> RnM (LHsContext GhcRn, FreeVars)
rnTyKiContext RnTyKiEnv
env (L SrcSpanAnnC
loc [GenLocated SrcSpanAnnA (HsType GhcPs)]
cxt)
  = do { String -> SDoc -> TcRn ()
traceRn String
"rncontext" ([GenLocated SrcSpanAnnA (HsType GhcPs)] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [GenLocated SrcSpanAnnA (HsType GhcPs)]
cxt)
       ; let env' :: RnTyKiEnv
env' = RnTyKiEnv
env { rtke_what = RnConstraint }
       ; ([GenLocated SrcSpanAnnA (HsType GhcRn)]
cxt', FreeVars
fvs) <- (GenLocated SrcSpanAnnA (HsType GhcPs)
 -> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars))
-> [GenLocated SrcSpanAnnA (HsType GhcPs)]
-> RnM ([GenLocated SrcSpanAnnA (HsType GhcRn)], FreeVars)
forall (f :: * -> *) a b.
Traversable f =>
(a -> RnM (b, FreeVars)) -> f a -> RnM (f b, FreeVars)
mapFvRn (RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env') [GenLocated SrcSpanAnnA (HsType GhcPs)]
cxt
       ; (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)],
 FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)],
      FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpanAnnC
-> [GenLocated SrcSpanAnnA (HsType GhcRn)]
-> GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnC
loc [GenLocated SrcSpanAnnA (HsType GhcRn)]
cxt', FreeVars
fvs) }

rnContext :: HsDocContext -> LHsContext GhcPs
          -> RnM (LHsContext GhcRn, FreeVars)
rnContext :: HsDocContext
-> LHsContext GhcPs -> RnM (LHsContext GhcRn, FreeVars)
rnContext HsDocContext
doc LHsContext GhcPs
theta = RnTyKiEnv -> LHsContext GhcPs -> RnM (LHsContext GhcRn, FreeVars)
rnTyKiContext (HsDocContext -> TypeOrKind -> RnTyKiWhat -> RnTyKiEnv
mkTyKiEnv HsDocContext
doc TypeOrKind
TypeLevel RnTyKiWhat
RnConstraint) LHsContext GhcPs
theta

rnMaybeContext :: HsDocContext -> Maybe (LHsContext GhcPs)
          -> RnM (Maybe (LHsContext GhcRn), FreeVars)
rnMaybeContext :: HsDocContext
-> Maybe (LHsContext GhcPs)
-> RnM (Maybe (LHsContext GhcRn), FreeVars)
rnMaybeContext HsDocContext
_ Maybe (LHsContext GhcPs)
Nothing = (Maybe
   (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]),
 FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (Maybe
        (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]),
      FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe
  (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)])
forall a. Maybe a
Nothing, FreeVars
emptyFVs)
rnMaybeContext HsDocContext
doc (Just LHsContext GhcPs
theta)
  = do { (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]
theta', FreeVars
fvs) <- HsDocContext
-> LHsContext GhcPs -> RnM (LHsContext GhcRn, FreeVars)
rnContext HsDocContext
doc LHsContext GhcPs
theta
       ; (Maybe
   (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]),
 FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (Maybe
        (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]),
      FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]
-> Maybe
     (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)])
forall a. a -> Maybe a
Just GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]
theta', FreeVars
fvs)
       }


--------------
rnLHsTyKi  :: RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi :: RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env (L SrcSpanAnnA
loc HsType GhcPs
ty)
  = SrcSpanAnnA
-> RnM (LHsType GhcRn, FreeVars) -> RnM (LHsType GhcRn, FreeVars)
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc (RnM (LHsType GhcRn, FreeVars) -> RnM (LHsType GhcRn, FreeVars))
-> RnM (LHsType GhcRn, FreeVars) -> RnM (LHsType GhcRn, FreeVars)
forall a b. (a -> b) -> a -> b
$
    do { (HsType GhcRn
ty', FreeVars
fvs) <- RnTyKiEnv -> HsType GhcPs -> RnM (HsType GhcRn, FreeVars)
rnHsTyKi RnTyKiEnv
env HsType GhcPs
ty
       ; (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
-> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpanAnnA
-> HsType GhcRn -> GenLocated SrcSpanAnnA (HsType GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc HsType GhcRn
ty', FreeVars
fvs) }

rnHsTyKi :: RnTyKiEnv -> HsType GhcPs -> RnM (HsType GhcRn, FreeVars)

rnHsTyKi :: RnTyKiEnv -> HsType GhcPs -> RnM (HsType GhcRn, FreeVars)
rnHsTyKi RnTyKiEnv
env ty :: HsType GhcPs
ty@(HsForAllTy { hst_tele :: forall pass. HsType pass -> HsForAllTelescope pass
hst_tele = HsForAllTelescope GhcPs
tele, hst_body :: forall pass. HsType pass -> LHsType pass
hst_body = LHsType GhcPs
tau })
  = do { RnTyKiEnv -> HsTypeOrSigType GhcPs -> TcRn ()
checkPolyKinds RnTyKiEnv
env (HsType GhcPs -> HsTypeOrSigType GhcPs
forall p. HsType p -> HsTypeOrSigType p
HsType HsType GhcPs
ty)
       ; HsDocContext
-> HsForAllTelescope GhcPs
-> (HsForAllTelescope GhcRn -> RnM (HsType GhcRn, FreeVars))
-> RnM (HsType GhcRn, FreeVars)
forall a.
HsDocContext
-> HsForAllTelescope GhcPs
-> (HsForAllTelescope GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
bindHsForAllTelescope (RnTyKiEnv -> HsDocContext
rtke_ctxt RnTyKiEnv
env) HsForAllTelescope GhcPs
tele ((HsForAllTelescope GhcRn -> RnM (HsType GhcRn, FreeVars))
 -> RnM (HsType GhcRn, FreeVars))
-> (HsForAllTelescope GhcRn -> RnM (HsType GhcRn, FreeVars))
-> RnM (HsType GhcRn, FreeVars)
forall a b. (a -> b) -> a -> b
$ \ HsForAllTelescope GhcRn
tele' ->
    do { (GenLocated SrcSpanAnnA (HsType GhcRn)
tau',  FreeVars
fvs) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
tau
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ( HsForAllTy { hst_xforall :: XForAllTy GhcRn
hst_xforall = XForAllTy GhcRn
NoExtField
noExtField
                             , hst_tele :: HsForAllTelescope GhcRn
hst_tele = HsForAllTelescope GhcRn
tele' , hst_body :: LHsType GhcRn
hst_body =  LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
tau' }
                , FreeVars
fvs) } }

rnHsTyKi RnTyKiEnv
env (HsQualTy { hst_ctxt :: forall pass. HsType pass -> LHsContext pass
hst_ctxt = LHsContext GhcPs
lctxt, hst_body :: forall pass. HsType pass -> LHsType pass
hst_body = LHsType GhcPs
tau })
  = do { -- no need to check type vs kind level here; this is
         -- checked in the type checker. See
         -- Note [No constraints in kinds] in GHC.Tc.Validity
         (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]
ctxt', FreeVars
fvs1) <- RnTyKiEnv -> LHsContext GhcPs -> RnM (LHsContext GhcRn, FreeVars)
rnTyKiContext RnTyKiEnv
env LHsContext GhcPs
lctxt
       ; (GenLocated SrcSpanAnnA (HsType GhcRn)
tau',  FreeVars
fvs2) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
tau
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsQualTy { hst_xqual :: XQualTy GhcRn
hst_xqual = XQualTy GhcRn
NoExtField
noExtField, hst_ctxt :: LHsContext GhcRn
hst_ctxt = LHsContext GhcRn
GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcRn)]
ctxt'
                          , hst_body :: LHsType GhcRn
hst_body =  LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
tau' }
                , FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) }

rnHsTyKi RnTyKiEnv
env (HsTyVar XTyVar GhcPs
_ PromotionFlag
ip (L SrcSpanAnnN
loc RdrName
rdr_name))
  = do { Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RnTyKiEnv -> Bool
isRnKindLevel RnTyKiEnv
env Bool -> Bool -> Bool
&& RdrName -> Bool
isRdrTyVar RdrName
rdr_name) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
         Extension -> TcRn () -> TcRn ()
forall gbl lcl. Extension -> TcRnIf gbl lcl () -> TcRnIf gbl lcl ()
unlessXOptM Extension
LangExt.PolyKinds (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$
         HsDocContext -> TcRnMessage -> TcRnMessage
TcRnWithHsDocContext (RnTyKiEnv -> HsDocContext
rtke_ctxt RnTyKiEnv
env) (TcRnMessage -> TcRnMessage) -> TcRnMessage -> TcRnMessage
forall a b. (a -> b) -> a -> b
$
         RdrName -> TcRnMessage
TcRnUnexpectedKindVar RdrName
rdr_name
           -- Any type variable at the kind level is illegal without the use
           -- of PolyKinds (see #14710)
       ; Name
name <- RnTyKiEnv -> RdrName -> IOEnv (Env TcGblEnv TcLclEnv) Name
rnTyVar RnTyKiEnv
env RdrName
rdr_name
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Name -> Bool
isDataConName Name
name Bool -> Bool -> Bool
&& Bool -> Bool
not (PromotionFlag -> Bool
isPromoted PromotionFlag
ip)) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
         -- NB: a prefix symbolic operator such as (:) is represented as HsTyVar.
            TcRnMessage -> TcRn ()
addDiagnostic (UntickedPromotedThing -> TcRnMessage
TcRnUntickedPromotedThing (UntickedPromotedThing -> TcRnMessage)
-> UntickedPromotedThing -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ LexicalFixity -> Name -> UntickedPromotedThing
UntickedConstructor LexicalFixity
Prefix Name
name)
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XTyVar GhcRn -> PromotionFlag -> LIdP GhcRn -> HsType GhcRn
forall pass.
XTyVar pass -> PromotionFlag -> LIdP pass -> HsType pass
HsTyVar XTyVar GhcRn
EpAnn [AddEpAnn]
forall a. EpAnn a
noAnn PromotionFlag
ip (SrcSpanAnnN -> Name -> GenLocated SrcSpanAnnN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
loc Name
name), Name -> FreeVars
unitFV Name
name) }

rnHsTyKi RnTyKiEnv
env ty :: HsType GhcPs
ty@(HsOpTy XOpTy GhcPs
_ PromotionFlag
prom LHsType GhcPs
ty1 LIdP GhcPs
l_op LHsType GhcPs
ty2)
  = SrcSpan
-> RnM (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan (GenLocated SrcSpanAnnN RdrName -> SrcSpan
forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA LIdP GhcPs
GenLocated SrcSpanAnnN RdrName
l_op) (RnM (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars))
-> RnM (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a b. (a -> b) -> a -> b
$
    do  { (GenLocated SrcSpanAnnN Name
l_op', FreeVars
fvs1) <- RnTyKiEnv
-> SDoc
-> GenLocated SrcSpanAnnN RdrName
-> RnM (GenLocated SrcSpanAnnN Name, FreeVars)
rnHsTyOp RnTyKiEnv
env (HsType GhcPs -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsType GhcPs
ty) LIdP GhcPs
GenLocated SrcSpanAnnN RdrName
l_op
        ; let op_name :: Name
op_name = GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnN Name
l_op'
        ; Fixity
fix   <- GenLocated SrcSpanAnnN Name -> RnM Fixity
lookupTyFixityRn GenLocated SrcSpanAnnN Name
l_op'
        ; (GenLocated SrcSpanAnnA (HsType GhcRn)
ty1', FreeVars
fvs2) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty1
        ; (GenLocated SrcSpanAnnA (HsType GhcRn)
ty2', FreeVars
fvs3) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty2
        ; HsType GhcRn
res_ty <- PromotionFlag
-> GenLocated SrcSpanAnnN Name
-> Fixity
-> LHsType GhcRn
-> LHsType GhcRn
-> RnM (HsType GhcRn)
mkHsOpTyRn PromotionFlag
prom GenLocated SrcSpanAnnN Name
l_op' Fixity
fix LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty1' LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty2'
        ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Name -> Bool
isDataConName Name
op_name Bool -> Bool -> Bool
&& Bool -> Bool
not (PromotionFlag -> Bool
isPromoted PromotionFlag
prom)) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
            TcRnMessage -> TcRn ()
addDiagnostic (UntickedPromotedThing -> TcRnMessage
TcRnUntickedPromotedThing (UntickedPromotedThing -> TcRnMessage)
-> UntickedPromotedThing -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ LexicalFixity -> Name -> UntickedPromotedThing
UntickedConstructor LexicalFixity
Infix Name
op_name)
        ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsType GhcRn
res_ty, [FreeVars] -> FreeVars
plusFVs [FreeVars
fvs1, FreeVars
fvs2, FreeVars
fvs3]) }

rnHsTyKi RnTyKiEnv
env (HsParTy XParTy GhcPs
_ LHsType GhcPs
ty)
  = do { (GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
fvs) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XParTy GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass. XParTy pass -> LHsType pass -> HsType pass
HsParTy XParTy GhcRn
EpAnn AnnParen
forall a. EpAnn a
noAnn LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
fvs) }

rnHsTyKi RnTyKiEnv
env (HsBangTy XBangTy GhcPs
x HsSrcBang
b LHsType GhcPs
ty)
  = do { (GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
fvs) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XBangTy GhcRn -> HsSrcBang -> LHsType GhcRn -> HsType GhcRn
forall pass.
XBangTy pass -> HsSrcBang -> LHsType pass -> HsType pass
HsBangTy XBangTy GhcPs
XBangTy GhcRn
x HsSrcBang
b LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
fvs) }

rnHsTyKi RnTyKiEnv
env ty :: HsType GhcPs
ty@(HsRecTy XRecTy GhcPs
_ [LConDeclField GhcPs]
flds)
  = do { let ctxt :: HsDocContext
ctxt = RnTyKiEnv -> HsDocContext
rtke_ctxt RnTyKiEnv
env
       ; [FieldLabel]
fls          <- HsDocContext -> IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel]
get_fields HsDocContext
ctxt
       ; ([GenLocated SrcSpanAnnA (ConDeclField GhcRn)]
flds', FreeVars
fvs) <- HsDocContext
-> [FieldLabel]
-> [LConDeclField GhcPs]
-> RnM ([LConDeclField GhcRn], FreeVars)
rnConDeclFields HsDocContext
ctxt [FieldLabel]
fls [LConDeclField GhcPs]
flds
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XRecTy GhcRn -> [LConDeclField GhcRn] -> HsType GhcRn
forall pass. XRecTy pass -> [LConDeclField pass] -> HsType pass
HsRecTy XRecTy GhcRn
NoExtField
noExtField [LConDeclField GhcRn]
[GenLocated SrcSpanAnnA (ConDeclField GhcRn)]
flds', FreeVars
fvs) }
  where
    get_fields :: HsDocContext -> IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel]
get_fields (ConDeclCtx [GenLocated SrcSpanAnnN Name]
names)
      = do [FieldLabel]
res <- (GenLocated SrcSpanAnnN Name
 -> IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel])
-> [GenLocated SrcSpanAnnN Name]
-> IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel]
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Traversable f) =>
(a -> m [b]) -> f a -> m [b]
concatMapM (HasDebugCallStack =>
Name -> IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel]
Name -> IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel]
lookupConstructorFields (Name -> IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel])
-> (GenLocated SrcSpanAnnN Name -> Name)
-> GenLocated SrcSpanAnnN Name
-> IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc) [GenLocated SrcSpanAnnN Name]
names
           if [FieldLabel] -> [GenLocated SrcSpanAnnN Name] -> Bool
forall a b. [a] -> [b] -> Bool
equalLength [FieldLabel]
res [GenLocated SrcSpanAnnN Name]
names
           -- Lookup can fail when the record syntax is incorrect, e.g.
           -- data D = D Int { fld :: Bool }. See T7943.
           then [FieldLabel] -> IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [FieldLabel]
res
           else IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel]
err
    get_fields HsDocContext
_ = IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel]
err

    err :: IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel]
err =
      do { TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ Either (HsType GhcPs) (HsType GhcRn) -> TcRnMessage
TcRnIllegalRecordSyntax (HsType GhcPs -> Either (HsType GhcPs) (HsType GhcRn)
forall a b. a -> Either a b
Left HsType GhcPs
ty)
         ; [FieldLabel] -> IOEnv (Env TcGblEnv TcLclEnv) [FieldLabel]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [] }

rnHsTyKi RnTyKiEnv
env (HsFunTy XFunTy GhcPs
u HsArrow GhcPs
mult LHsType GhcPs
ty1 LHsType GhcPs
ty2)
  = do { (GenLocated SrcSpanAnnA (HsType GhcRn)
ty1', FreeVars
fvs1) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty1
       ; (GenLocated SrcSpanAnnA (HsType GhcRn)
ty2', FreeVars
fvs2) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty2
       ; (HsArrow GhcRn
mult', FreeVars
w_fvs) <- RnTyKiEnv -> HsArrow GhcPs -> RnM (HsArrow GhcRn, FreeVars)
rnHsArrow RnTyKiEnv
env HsArrow GhcPs
mult
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XFunTy GhcRn
-> HsArrow GhcRn -> LHsType GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass.
XFunTy pass
-> HsArrow pass -> LHsType pass -> LHsType pass -> HsType pass
HsFunTy XFunTy GhcPs
XFunTy GhcRn
u HsArrow GhcRn
mult' LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty1' LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty2'
                , [FreeVars] -> FreeVars
plusFVs [FreeVars
fvs1, FreeVars
fvs2, FreeVars
w_fvs]) }

rnHsTyKi RnTyKiEnv
env listTy :: HsType GhcPs
listTy@(HsListTy XListTy GhcPs
x LHsType GhcPs
ty)
  = do { Bool
data_kinds <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DataKinds
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
data_kinds Bool -> Bool -> Bool
&& RnTyKiEnv -> Bool
isRnKindLevel RnTyKiEnv
env)
              (TcRnMessage -> TcRn ()
addErr (RnTyKiEnv -> HsType GhcPs -> TcRnMessage
dataKindsErr RnTyKiEnv
env HsType GhcPs
listTy))
       ; (GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
fvs) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XListTy GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass. XListTy pass -> LHsType pass -> HsType pass
HsListTy XListTy GhcPs
XListTy GhcRn
x LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
fvs) }

rnHsTyKi RnTyKiEnv
env (HsKindSig XKindSig GhcPs
x LHsType GhcPs
ty LHsType GhcPs
k)
  = do { Bool
kind_sigs_ok <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.KindSignatures
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
kind_sigs_ok (HsDocContext -> LHsType GhcPs -> TcRn ()
badKindSigErr (RnTyKiEnv -> HsDocContext
rtke_ctxt RnTyKiEnv
env) LHsType GhcPs
ty)
       ; (GenLocated SrcSpanAnnA (HsType GhcRn)
k', FreeVars
sig_fvs)  <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi (RnTyKiEnv
env { rtke_level = KindLevel }) LHsType GhcPs
k
       ; (GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
lhs_fvs) <- [Name]
-> RnM (LHsType GhcRn, FreeVars) -> RnM (LHsType GhcRn, FreeVars)
forall a. [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
bindSigTyVarsFV (LHsType GhcRn -> [Name]
hsScopedKvs LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
k') (RnM (LHsType GhcRn, FreeVars) -> RnM (LHsType GhcRn, FreeVars))
-> RnM (LHsType GhcRn, FreeVars) -> RnM (LHsType GhcRn, FreeVars)
forall a b. (a -> b) -> a -> b
$
                           RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XKindSig GhcRn -> LHsType GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass.
XKindSig pass -> LHsType pass -> LHsType pass -> HsType pass
HsKindSig XKindSig GhcPs
XKindSig GhcRn
x LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty' LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
k', FreeVars
lhs_fvs FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
sig_fvs) }

-- Unboxed tuples are allowed to have poly-typed arguments.  These
-- sometimes crop up as a result of CPR worker-wrappering dictionaries.
rnHsTyKi RnTyKiEnv
env tupleTy :: HsType GhcPs
tupleTy@(HsTupleTy XTupleTy GhcPs
x HsTupleSort
tup_con HsContext GhcPs
tys)
  = do { Bool
data_kinds <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DataKinds
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
data_kinds Bool -> Bool -> Bool
&& RnTyKiEnv -> Bool
isRnKindLevel RnTyKiEnv
env)
              (TcRnMessage -> TcRn ()
addErr (RnTyKiEnv -> HsType GhcPs -> TcRnMessage
dataKindsErr RnTyKiEnv
env HsType GhcPs
tupleTy))
       ; ([GenLocated SrcSpanAnnA (HsType GhcRn)]
tys', FreeVars
fvs) <- (GenLocated SrcSpanAnnA (HsType GhcPs)
 -> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars))
-> [GenLocated SrcSpanAnnA (HsType GhcPs)]
-> RnM ([GenLocated SrcSpanAnnA (HsType GhcRn)], FreeVars)
forall (f :: * -> *) a b.
Traversable f =>
(a -> RnM (b, FreeVars)) -> f a -> RnM (f b, FreeVars)
mapFvRn (RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env) HsContext GhcPs
[GenLocated SrcSpanAnnA (HsType GhcPs)]
tys
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XTupleTy GhcRn -> HsTupleSort -> [LHsType GhcRn] -> HsType GhcRn
forall pass.
XTupleTy pass -> HsTupleSort -> [LHsType pass] -> HsType pass
HsTupleTy XTupleTy GhcPs
XTupleTy GhcRn
x HsTupleSort
tup_con [LHsType GhcRn]
[GenLocated SrcSpanAnnA (HsType GhcRn)]
tys', FreeVars
fvs) }

rnHsTyKi RnTyKiEnv
env sumTy :: HsType GhcPs
sumTy@(HsSumTy XSumTy GhcPs
x HsContext GhcPs
tys)
  = do { Bool
data_kinds <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DataKinds
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not Bool
data_kinds Bool -> Bool -> Bool
&& RnTyKiEnv -> Bool
isRnKindLevel RnTyKiEnv
env)
              (TcRnMessage -> TcRn ()
addErr (RnTyKiEnv -> HsType GhcPs -> TcRnMessage
dataKindsErr RnTyKiEnv
env HsType GhcPs
sumTy))
       ; ([GenLocated SrcSpanAnnA (HsType GhcRn)]
tys', FreeVars
fvs) <- (GenLocated SrcSpanAnnA (HsType GhcPs)
 -> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars))
-> [GenLocated SrcSpanAnnA (HsType GhcPs)]
-> RnM ([GenLocated SrcSpanAnnA (HsType GhcRn)], FreeVars)
forall (f :: * -> *) a b.
Traversable f =>
(a -> RnM (b, FreeVars)) -> f a -> RnM (f b, FreeVars)
mapFvRn (RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env) HsContext GhcPs
[GenLocated SrcSpanAnnA (HsType GhcPs)]
tys
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XSumTy GhcRn -> [LHsType GhcRn] -> HsType GhcRn
forall pass. XSumTy pass -> [LHsType pass] -> HsType pass
HsSumTy XSumTy GhcPs
XSumTy GhcRn
x [LHsType GhcRn]
[GenLocated SrcSpanAnnA (HsType GhcRn)]
tys', FreeVars
fvs) }

-- Ensure that a type-level integer is nonnegative (#8306, #8412)
rnHsTyKi RnTyKiEnv
env tyLit :: HsType GhcPs
tyLit@(HsTyLit XTyLit GhcPs
src HsTyLit GhcPs
t)
  = do { Bool
data_kinds <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DataKinds
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
data_kinds (TcRnMessage -> TcRn ()
addErr (RnTyKiEnv -> HsType GhcPs -> TcRnMessage
dataKindsErr RnTyKiEnv
env HsType GhcPs
tyLit))
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (HsTyLit GhcPs -> Bool
forall (p :: Pass). HsTyLit (GhcPass p) -> Bool
negLit HsTyLit GhcPs
t) (TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ HsType GhcPs -> TcRnMessage
TcRnNegativeNumTypeLiteral HsType GhcPs
tyLit)
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XTyLit GhcRn -> HsTyLit GhcRn -> HsType GhcRn
forall pass. XTyLit pass -> HsTyLit pass -> HsType pass
HsTyLit XTyLit GhcPs
XTyLit GhcRn
src (HsTyLit GhcPs -> HsTyLit GhcRn
rnHsTyLit HsTyLit GhcPs
t), FreeVars
emptyFVs) }
  where
    negLit :: HsTyLit (GhcPass p) -> Bool
    negLit :: forall (p :: Pass). HsTyLit (GhcPass p) -> Bool
negLit (HsStrTy XStrTy (GhcPass p)
_ FastString
_) = Bool
False
    negLit (HsNumTy XNumTy (GhcPass p)
_ Integer
i) = Integer
i Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0
    negLit (HsCharTy XCharTy (GhcPass p)
_ Char
_) = Bool
False

rnHsTyKi RnTyKiEnv
env (HsAppTy XAppTy GhcPs
_ LHsType GhcPs
ty1 LHsType GhcPs
ty2)
  = do { (GenLocated SrcSpanAnnA (HsType GhcRn)
ty1', FreeVars
fvs1) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty1
       ; (GenLocated SrcSpanAnnA (HsType GhcRn)
ty2', FreeVars
fvs2) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty2
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XAppTy GhcRn -> LHsType GhcRn -> LHsType GhcRn -> HsType GhcRn
forall pass.
XAppTy pass -> LHsType pass -> LHsType pass -> HsType pass
HsAppTy XAppTy GhcRn
NoExtField
noExtField LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty1' LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty2', FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) }

rnHsTyKi RnTyKiEnv
env (HsAppKindTy XAppKindTy GhcPs
_ LHsType GhcPs
ty LHsToken "@" GhcPs
at LHsType GhcPs
k)
  = do { Bool
kind_app <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.TypeApplications
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
kind_app (TcRnMessage -> TcRn ()
addErr (TypeOrKind -> LHsType GhcPs -> TcRnMessage
typeAppErr TypeOrKind
KindLevel LHsType GhcPs
k))
       ; (GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
fvs1) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty
       ; (GenLocated SrcSpanAnnA (HsType GhcRn)
k', FreeVars
fvs2) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi (RnTyKiEnv
env {rtke_level = KindLevel }) LHsType GhcPs
k
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XAppKindTy GhcRn
-> LHsType GhcRn
-> LHsToken "@" GhcRn
-> LHsType GhcRn
-> HsType GhcRn
forall pass.
XAppKindTy pass
-> LHsType pass -> LHsToken "@" pass -> LHsType pass -> HsType pass
HsAppKindTy XAppKindTy GhcRn
NoExtField
noExtField LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty' LHsToken "@" GhcPs
LHsToken "@" GhcRn
at LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
k', FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) }

rnHsTyKi RnTyKiEnv
env t :: HsType GhcPs
t@(HsIParamTy XIParamTy GhcPs
x XRec GhcPs HsIPName
n LHsType GhcPs
ty)
  = do { RnTyKiEnv -> HsType GhcPs -> TcRn ()
notInKinds RnTyKiEnv
env HsType GhcPs
t
       ; (GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
fvs) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XIParamTy GhcRn
-> XRec GhcRn HsIPName -> LHsType GhcRn -> HsType GhcRn
forall pass.
XIParamTy pass -> XRec pass HsIPName -> LHsType pass -> HsType pass
HsIParamTy XIParamTy GhcPs
XIParamTy GhcRn
x XRec GhcPs HsIPName
XRec GhcRn HsIPName
n LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
fvs) }

rnHsTyKi RnTyKiEnv
_ (HsStarTy XStarTy GhcPs
_ Bool
isUni)
  = (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XStarTy GhcRn -> Bool -> HsType GhcRn
forall pass. XStarTy pass -> Bool -> HsType pass
HsStarTy XStarTy GhcRn
NoExtField
noExtField Bool
isUni, FreeVars
emptyFVs)

rnHsTyKi RnTyKiEnv
_ (HsSpliceTy XSpliceTy GhcPs
_ HsUntypedSplice GhcPs
sp)
  = HsUntypedSplice GhcPs -> RnM (HsType GhcRn, FreeVars)
rnSpliceType HsUntypedSplice GhcPs
sp

rnHsTyKi RnTyKiEnv
env (HsDocTy XDocTy GhcPs
x LHsType GhcPs
ty LHsDoc GhcPs
haddock_doc)
  = do { (GenLocated SrcSpanAnnA (HsType GhcRn)
ty', FreeVars
fvs) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty
       ; LHsDoc GhcRn
haddock_doc' <- LHsDoc GhcPs -> RnM (LHsDoc GhcRn)
rnLHsDoc LHsDoc GhcPs
haddock_doc
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XDocTy GhcRn -> LHsType GhcRn -> LHsDoc GhcRn -> HsType GhcRn
forall pass.
XDocTy pass -> LHsType pass -> LHsDoc pass -> HsType pass
HsDocTy XDocTy GhcPs
XDocTy GhcRn
x LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty' LHsDoc GhcRn
haddock_doc', FreeVars
fvs) }

-- See Note [Renaming HsCoreTys]
rnHsTyKi RnTyKiEnv
env (XHsType XXType GhcPs
ty)
  = do (Name -> TcRn ()) -> [Name] -> TcRn ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (RdrName -> TcRn ()
check_in_scope (RdrName -> TcRn ()) -> (Name -> RdrName) -> Name -> TcRn ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> RdrName
nameRdrName) [Name]
fvs_list
       (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XXType GhcRn -> HsType GhcRn
forall pass. XXType pass -> HsType pass
XHsType XXType GhcPs
XXType GhcRn
ty, FreeVars
fvs)
  where
    fvs_list :: [Name]
fvs_list = (TyCoVar -> Name) -> [TyCoVar] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map TyCoVar -> Name
forall a. NamedThing a => a -> Name
getName ([TyCoVar] -> [Name]) -> [TyCoVar] -> [Name]
forall a b. (a -> b) -> a -> b
$ Type -> [TyCoVar]
tyCoVarsOfTypeList XXType GhcPs
Type
ty
    fvs :: FreeVars
fvs = [Name] -> FreeVars
mkFVs [Name]
fvs_list

    check_in_scope :: RdrName -> RnM ()
    check_in_scope :: RdrName -> TcRn ()
check_in_scope RdrName
rdr_name = do
      Maybe Name
mb_name <- RdrName -> RnM (Maybe Name)
lookupLocalOccRn_maybe RdrName
rdr_name
      Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe Name -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Name
mb_name) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
        TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$
          HsDocContext -> TcRnMessage -> TcRnMessage
TcRnWithHsDocContext (RnTyKiEnv -> HsDocContext
rtke_ctxt RnTyKiEnv
env) (TcRnMessage -> TcRnMessage) -> TcRnMessage -> TcRnMessage
forall a b. (a -> b) -> a -> b
$
            NotInScopeError
-> RdrName -> [ImportError] -> [GhcHint] -> TcRnMessage
TcRnNotInScope (WhereLooking -> RdrName -> NotInScopeError
notInScopeErr WhereLooking
WL_LocalOnly RdrName
rdr_name) RdrName
rdr_name [] []

rnHsTyKi RnTyKiEnv
env ty :: HsType GhcPs
ty@(HsExplicitListTy XExplicitListTy GhcPs
_ PromotionFlag
ip HsContext GhcPs
tys)
  = do { Bool
data_kinds <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DataKinds
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
data_kinds (TcRnMessage -> TcRn ()
addErr (RnTyKiEnv -> HsType GhcPs -> TcRnMessage
dataKindsErr RnTyKiEnv
env HsType GhcPs
ty))
       ; ([GenLocated SrcSpanAnnA (HsType GhcRn)]
tys', FreeVars
fvs) <- (GenLocated SrcSpanAnnA (HsType GhcPs)
 -> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars))
-> [GenLocated SrcSpanAnnA (HsType GhcPs)]
-> RnM ([GenLocated SrcSpanAnnA (HsType GhcRn)], FreeVars)
forall (f :: * -> *) a b.
Traversable f =>
(a -> RnM (b, FreeVars)) -> f a -> RnM (f b, FreeVars)
mapFvRn (RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env) HsContext GhcPs
[GenLocated SrcSpanAnnA (HsType GhcPs)]
tys
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (PromotionFlag -> Bool
isPromoted PromotionFlag
ip) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
           TcRnMessage -> TcRn ()
addDiagnostic (UntickedPromotedThing -> TcRnMessage
TcRnUntickedPromotedThing (UntickedPromotedThing -> TcRnMessage)
-> UntickedPromotedThing -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ UntickedPromotedThing
UntickedExplicitList)
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XExplicitListTy GhcRn
-> PromotionFlag -> [LHsType GhcRn] -> HsType GhcRn
forall pass.
XExplicitListTy pass
-> PromotionFlag -> [LHsType pass] -> HsType pass
HsExplicitListTy XExplicitListTy GhcRn
NoExtField
noExtField PromotionFlag
ip [LHsType GhcRn]
[GenLocated SrcSpanAnnA (HsType GhcRn)]
tys', FreeVars
fvs) }

rnHsTyKi RnTyKiEnv
env ty :: HsType GhcPs
ty@(HsExplicitTupleTy XExplicitTupleTy GhcPs
_ HsContext GhcPs
tys)
  = do { Bool
data_kinds <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DataKinds
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
data_kinds (TcRnMessage -> TcRn ()
addErr (RnTyKiEnv -> HsType GhcPs -> TcRnMessage
dataKindsErr RnTyKiEnv
env HsType GhcPs
ty))
       ; ([GenLocated SrcSpanAnnA (HsType GhcRn)]
tys', FreeVars
fvs) <- (GenLocated SrcSpanAnnA (HsType GhcPs)
 -> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars))
-> [GenLocated SrcSpanAnnA (HsType GhcPs)]
-> RnM ([GenLocated SrcSpanAnnA (HsType GhcRn)], FreeVars)
forall (f :: * -> *) a b.
Traversable f =>
(a -> RnM (b, FreeVars)) -> f a -> RnM (f b, FreeVars)
mapFvRn (RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env) HsContext GhcPs
[GenLocated SrcSpanAnnA (HsType GhcPs)]
tys
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XExplicitTupleTy GhcRn -> [LHsType GhcRn] -> HsType GhcRn
forall pass. XExplicitTupleTy pass -> [LHsType pass] -> HsType pass
HsExplicitTupleTy XExplicitTupleTy GhcRn
NoExtField
noExtField [LHsType GhcRn]
[GenLocated SrcSpanAnnA (HsType GhcRn)]
tys', FreeVars
fvs) }

rnHsTyKi RnTyKiEnv
env (HsWildCardTy XWildCardTy GhcPs
_)
  = do { RnTyKiEnv -> TcRn ()
checkAnonWildCard RnTyKiEnv
env
       ; (HsType GhcRn, FreeVars) -> RnM (HsType GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XWildCardTy GhcRn -> HsType GhcRn
forall pass. XWildCardTy pass -> HsType pass
HsWildCardTy XWildCardTy GhcRn
NoExtField
noExtField, FreeVars
emptyFVs) }


rnHsTyLit :: HsTyLit GhcPs -> HsTyLit GhcRn
rnHsTyLit :: HsTyLit GhcPs -> HsTyLit GhcRn
rnHsTyLit (HsStrTy XStrTy GhcPs
x FastString
s) = XStrTy GhcRn -> FastString -> HsTyLit GhcRn
forall pass. XStrTy pass -> FastString -> HsTyLit pass
HsStrTy XStrTy GhcPs
XStrTy GhcRn
x FastString
s
rnHsTyLit (HsNumTy XNumTy GhcPs
x Integer
i) = XNumTy GhcRn -> Integer -> HsTyLit GhcRn
forall pass. XNumTy pass -> Integer -> HsTyLit pass
HsNumTy XNumTy GhcPs
XNumTy GhcRn
x Integer
i
rnHsTyLit (HsCharTy XCharTy GhcPs
x Char
c) = XCharTy GhcRn -> Char -> HsTyLit GhcRn
forall pass. XCharTy pass -> Char -> HsTyLit pass
HsCharTy XCharTy GhcPs
XCharTy GhcRn
x Char
c


rnHsArrow :: RnTyKiEnv -> HsArrow GhcPs -> RnM (HsArrow GhcRn, FreeVars)
rnHsArrow :: RnTyKiEnv -> HsArrow GhcPs -> RnM (HsArrow GhcRn, FreeVars)
rnHsArrow RnTyKiEnv
_env (HsUnrestrictedArrow LHsUniToken "->" "\8594" GhcPs
arr) = (HsArrow GhcRn, FreeVars) -> RnM (HsArrow GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsUniToken "->" "\8594" GhcRn -> HsArrow GhcRn
forall pass. LHsUniToken "->" "\8594" pass -> HsArrow pass
HsUnrestrictedArrow LHsUniToken "->" "\8594" GhcPs
LHsUniToken "->" "\8594" GhcRn
arr, FreeVars
emptyFVs)
rnHsArrow RnTyKiEnv
_env (HsLinearArrow (HsPct1 LHsToken "%1" GhcPs
pct1 LHsUniToken "->" "\8594" GhcPs
arr)) = (HsArrow GhcRn, FreeVars) -> RnM (HsArrow GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsLinearArrowTokens GhcRn -> HsArrow GhcRn
forall pass. HsLinearArrowTokens pass -> HsArrow pass
HsLinearArrow (LHsToken "%1" GhcRn
-> LHsUniToken "->" "\8594" GhcRn -> HsLinearArrowTokens GhcRn
forall pass.
LHsToken "%1" pass
-> LHsUniToken "->" "\8594" pass -> HsLinearArrowTokens pass
HsPct1 LHsToken "%1" GhcPs
LHsToken "%1" GhcRn
pct1 LHsUniToken "->" "\8594" GhcPs
LHsUniToken "->" "\8594" GhcRn
arr), FreeVars
emptyFVs)
rnHsArrow RnTyKiEnv
_env (HsLinearArrow (HsLolly LHsToken "\8888" GhcPs
arr)) = (HsArrow GhcRn, FreeVars) -> RnM (HsArrow GhcRn, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsLinearArrowTokens GhcRn -> HsArrow GhcRn
forall pass. HsLinearArrowTokens pass -> HsArrow pass
HsLinearArrow (LHsToken "\8888" GhcRn -> HsLinearArrowTokens GhcRn
forall pass. LHsToken "\8888" pass -> HsLinearArrowTokens pass
HsLolly LHsToken "\8888" GhcPs
LHsToken "\8888" GhcRn
arr), FreeVars
emptyFVs)
rnHsArrow RnTyKiEnv
env (HsExplicitMult LHsToken "%" GhcPs
pct LHsType GhcPs
p LHsUniToken "->" "\8594" GhcPs
arr)
  = (\(GenLocated SrcSpanAnnA (HsType GhcRn)
mult, FreeVars
fvs) -> (LHsToken "%" GhcRn
-> LHsType GhcRn -> LHsUniToken "->" "\8594" GhcRn -> HsArrow GhcRn
forall pass.
LHsToken "%" pass
-> LHsType pass -> LHsUniToken "->" "\8594" pass -> HsArrow pass
HsExplicitMult LHsToken "%" GhcPs
LHsToken "%" GhcRn
pct LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
mult LHsUniToken "->" "\8594" GhcPs
LHsUniToken "->" "\8594" GhcRn
arr, FreeVars
fvs)) ((GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
 -> (HsArrow GhcRn, FreeVars))
-> RnM (GenLocated SrcSpanAnnA (HsType GhcRn), FreeVars)
-> RnM (HsArrow GhcRn, FreeVars)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
p

{-
Note [Renaming HsCoreTys]
~~~~~~~~~~~~~~~~~~~~~~~~~
HsCoreTy is an escape hatch that allows embedding Core Types in HsTypes.
As such, there's not much to be done in order to rename an HsCoreTy,
since it's already been renamed to some extent. However, in an attempt to
detect ill-formed HsCoreTys, the renamer checks to see if all free type
variables in an HsCoreTy are in scope. To see why this can matter, consider
this example from #18914:

  type T f = forall a. f a

  class C f where
    m :: T f

  newtype N f a = MkN (f a)
    deriving C

Because of #18914, a previous GHC would generate the following code:

  instance C f => C (N f) where
    m :: T (N f)
    m = coerce @(f a)   -- The type within @(...) is an HsCoreTy
               @(N f a) -- So is this
               (m @f)

There are two HsCoreTys in play—(f a) and (N f a)—both of which have
`f` and `a` as free type variables. The `f` is in scope from the instance head,
but `a` is completely unbound, which is what led to #18914. To avoid this sort
of mistake going forward, the renamer will now detect that `a` is unbound and
throw an error accordingly.
-}

--------------
rnTyVar :: RnTyKiEnv -> RdrName -> RnM Name
rnTyVar :: RnTyKiEnv -> RdrName -> IOEnv (Env TcGblEnv TcLclEnv) Name
rnTyVar RnTyKiEnv
env RdrName
rdr_name
  = do { Name
name <- RdrName -> IOEnv (Env TcGblEnv TcLclEnv) Name
lookupTypeOccRn RdrName
rdr_name
       ; RnTyKiEnv -> Name -> TcRn ()
checkNamedWildCard RnTyKiEnv
env Name
name
       ; Name -> IOEnv (Env TcGblEnv TcLclEnv) Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Name
name }

rnLTyVar :: LocatedN RdrName -> RnM (LocatedN Name)
-- Called externally; does not deal with wildcards
rnLTyVar :: GenLocated SrcSpanAnnN RdrName -> RnM (GenLocated SrcSpanAnnN Name)
rnLTyVar (L SrcSpanAnnN
loc RdrName
rdr_name)
  = do { Name
tyvar <- RdrName -> IOEnv (Env TcGblEnv TcLclEnv) Name
lookupTypeOccRn RdrName
rdr_name
       ; GenLocated SrcSpanAnnN Name -> RnM (GenLocated SrcSpanAnnN Name)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpanAnnN -> Name -> GenLocated SrcSpanAnnN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
loc Name
tyvar) }

--------------
rnHsTyOp :: RnTyKiEnv -> SDoc -> LocatedN RdrName
         -> RnM (LocatedN Name, FreeVars)
rnHsTyOp :: RnTyKiEnv
-> SDoc
-> GenLocated SrcSpanAnnN RdrName
-> RnM (GenLocated SrcSpanAnnN Name, FreeVars)
rnHsTyOp RnTyKiEnv
env SDoc
overall_ty (L SrcSpanAnnN
loc RdrName
op)
  = do { Name
op' <- RnTyKiEnv -> RdrName -> IOEnv (Env TcGblEnv TcLclEnv) Name
rnTyVar RnTyKiEnv
env RdrName
op
       ; Extension -> TcRn () -> TcRn ()
forall gbl lcl. Extension -> TcRnIf gbl lcl () -> TcRnIf gbl lcl ()
unlessXOptM Extension
LangExt.TypeOperators (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
           if (Name
op' Name -> Unique -> Bool
forall a. Uniquable a => a -> Unique -> Bool
`hasKey` Unique
eqTyConKey) -- See [eqTyCon (~) compatibility fallback] in GHC.Rename.Env
           then TcRnMessage -> TcRn ()
addDiagnostic TcRnMessage
TcRnTypeEqualityRequiresOperators
           else TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ SDoc -> RdrName -> TcRnMessage
TcRnIllegalTypeOperator SDoc
overall_ty RdrName
op
       ; (GenLocated SrcSpanAnnN Name, FreeVars)
-> RnM (GenLocated SrcSpanAnnN Name, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpanAnnN -> Name -> GenLocated SrcSpanAnnN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
loc Name
op', Name -> FreeVars
unitFV Name
op') }

--------------
checkWildCard :: RnTyKiEnv
              -> Maybe Name -- ^ name of the wildcard,
                            -- or 'Nothing' for an anonymous wildcard
              -> Maybe BadAnonWildcardContext
              -> RnM ()
checkWildCard :: RnTyKiEnv -> Maybe Name -> Maybe BadAnonWildcardContext -> TcRn ()
checkWildCard RnTyKiEnv
env Maybe Name
mb_name (Just BadAnonWildcardContext
bad)
  = TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ HsDocContext -> TcRnMessage -> TcRnMessage
TcRnWithHsDocContext (RnTyKiEnv -> HsDocContext
rtke_ctxt RnTyKiEnv
env) (TcRnMessage -> TcRnMessage) -> TcRnMessage -> TcRnMessage
forall a b. (a -> b) -> a -> b
$
             Maybe Name -> BadAnonWildcardContext -> TcRnMessage
TcRnIllegalWildcardInType Maybe Name
mb_name BadAnonWildcardContext
bad
checkWildCard RnTyKiEnv
_ Maybe Name
_ Maybe BadAnonWildcardContext
Nothing
  = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

checkAnonWildCard :: RnTyKiEnv -> RnM ()
-- Report an error if an anonymous wildcard is illegal here
checkAnonWildCard :: RnTyKiEnv -> TcRn ()
checkAnonWildCard RnTyKiEnv
env
  = RnTyKiEnv -> Maybe Name -> Maybe BadAnonWildcardContext -> TcRn ()
checkWildCard RnTyKiEnv
env Maybe Name
forall a. Maybe a
Nothing Maybe BadAnonWildcardContext
mb_bad
  where
    mb_bad :: Maybe BadAnonWildcardContext
    mb_bad :: Maybe BadAnonWildcardContext
mb_bad | Bool -> Bool
not (RnTyKiEnv -> Bool
wildCardsAllowed RnTyKiEnv
env)
           = BadAnonWildcardContext -> Maybe BadAnonWildcardContext
forall a. a -> Maybe a
Just BadAnonWildcardContext
WildcardsNotAllowedAtAll
           | Bool
otherwise
           = case RnTyKiEnv -> RnTyKiWhat
rtke_what RnTyKiEnv
env of
               RnTyKiWhat
RnTypeBody      -> Maybe BadAnonWildcardContext
forall a. Maybe a
Nothing
               RnTyKiWhat
RnTopConstraint -> BadAnonWildcardContext -> Maybe BadAnonWildcardContext
forall a. a -> Maybe a
Just BadAnonWildcardContext
WildcardNotLastInConstraint
               RnTyKiWhat
RnConstraint    -> BadAnonWildcardContext -> Maybe BadAnonWildcardContext
forall a. a -> Maybe a
Just BadAnonWildcardContext
WildcardNotLastInConstraint

checkNamedWildCard :: RnTyKiEnv -> Name -> RnM ()
-- Report an error if a named wildcard is illegal here
checkNamedWildCard :: RnTyKiEnv -> Name -> TcRn ()
checkNamedWildCard RnTyKiEnv
env Name
name
  = RnTyKiEnv -> Maybe Name -> Maybe BadAnonWildcardContext -> TcRn ()
checkWildCard RnTyKiEnv
env (Name -> Maybe Name
forall a. a -> Maybe a
Just Name
name) Maybe BadAnonWildcardContext
mb_bad
  where
    mb_bad :: Maybe BadAnonWildcardContext
mb_bad | Bool -> Bool
not (Name
name Name -> FreeVars -> Bool
`elemNameSet` RnTyKiEnv -> FreeVars
rtke_nwcs RnTyKiEnv
env)
           = Maybe BadAnonWildcardContext
forall a. Maybe a
Nothing  -- Not a wildcard
           | Bool -> Bool
not (RnTyKiEnv -> Bool
wildCardsAllowed RnTyKiEnv
env)
           = BadAnonWildcardContext -> Maybe BadAnonWildcardContext
forall a. a -> Maybe a
Just BadAnonWildcardContext
WildcardsNotAllowedAtAll
           | Bool
otherwise
           = case RnTyKiEnv -> RnTyKiWhat
rtke_what RnTyKiEnv
env of
               RnTyKiWhat
RnTypeBody      -> Maybe BadAnonWildcardContext
forall a. Maybe a
Nothing   -- Allowed
               RnTyKiWhat
RnTopConstraint -> Maybe BadAnonWildcardContext
forall a. Maybe a
Nothing   -- Allowed; e.g.
                  -- f :: (Eq _a) => _a -> Int
                  -- g :: (_a, _b) => T _a _b -> Int
                  -- The named tyvars get filled in from elsewhere
               RnTyKiWhat
RnConstraint    -> BadAnonWildcardContext -> Maybe BadAnonWildcardContext
forall a. a -> Maybe a
Just BadAnonWildcardContext
WildcardNotLastInConstraint

wildCardsAllowed :: RnTyKiEnv -> Bool
-- ^ In what contexts are wildcards permitted
wildCardsAllowed :: RnTyKiEnv -> Bool
wildCardsAllowed RnTyKiEnv
env
   = case RnTyKiEnv -> HsDocContext
rtke_ctxt RnTyKiEnv
env of
       TypeSigCtx {}       -> Bool
True
       TypBrCtx {}         -> Bool
True   -- Template Haskell quoted type
       SpliceTypeCtx {}    -> Bool
True   -- Result of a Template Haskell splice
       ExprWithTySigCtx {} -> Bool
True
       PatCtx {}           -> Bool
True
       RuleCtx {}          -> Bool
True
       FamPatCtx {}        -> Bool
True   -- Not named wildcards though
       GHCiCtx {}          -> Bool
True
       HsTypeCtx {}        -> Bool
True
       StandaloneKindSigCtx {} -> Bool
False  -- See Note [Wildcards in standalone kind signatures] in "GHC.Hs.Decls"
       HsDocContext
_                   -> Bool
False



---------------
-- | Ensures either that we're in a type or that -XPolyKinds is set
checkPolyKinds :: RnTyKiEnv
               -> HsTypeOrSigType GhcPs
               -> RnM ()
checkPolyKinds :: RnTyKiEnv -> HsTypeOrSigType GhcPs -> TcRn ()
checkPolyKinds RnTyKiEnv
env HsTypeOrSigType GhcPs
ty
  | RnTyKiEnv -> Bool
isRnKindLevel RnTyKiEnv
env
  = do { Bool
polykinds <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.PolyKinds
       ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
polykinds (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
         TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ HsTypeOrSigType GhcPs -> Bool -> TcRnMessage
TcRnIllegalKind HsTypeOrSigType GhcPs
ty Bool
True }
checkPolyKinds RnTyKiEnv
_ HsTypeOrSigType GhcPs
_ = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

notInKinds :: RnTyKiEnv
           -> HsType GhcPs
           -> RnM ()
notInKinds :: RnTyKiEnv -> HsType GhcPs -> TcRn ()
notInKinds RnTyKiEnv
env HsType GhcPs
ty
  | RnTyKiEnv -> Bool
isRnKindLevel RnTyKiEnv
env
  = TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ HsTypeOrSigType GhcPs -> Bool -> TcRnMessage
TcRnIllegalKind (HsType GhcPs -> HsTypeOrSigType GhcPs
forall p. HsType p -> HsTypeOrSigType p
HsType HsType GhcPs
ty) Bool
False
notInKinds RnTyKiEnv
_ HsType GhcPs
_ = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

{- *****************************************************
*                                                      *
          Binding type variables
*                                                      *
***************************************************** -}

bindSigTyVarsFV :: [Name]
                -> RnM (a, FreeVars)
                -> RnM (a, FreeVars)
-- Used just before renaming the defn of a function
-- with a separate type signature, to bring its tyvars into scope
-- With no -XScopedTypeVariables, this is a no-op
bindSigTyVarsFV :: forall a. [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
bindSigTyVarsFV [Name]
tvs RnM (a, FreeVars)
thing_inside
  = do  { Bool
scoped_tyvars <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.ScopedTypeVariables
        ; if Bool -> Bool
not Bool
scoped_tyvars then
                RnM (a, FreeVars)
thing_inside
          else
                [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
forall a. [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
bindLocalNamesFV [Name]
tvs RnM (a, FreeVars)
thing_inside }

---------------
bindHsQTyVars :: forall a b.
                 HsDocContext
              -> Maybe a            -- Just _  => an associated type decl
              -> FreeKiTyVars       -- Kind variables from scope
              -> LHsQTyVars GhcPs
              -> (LHsQTyVars GhcRn -> FreeKiTyVars -> RnM (b, FreeVars))
                  -- The FreeKiTyVars is null <=> all kind variables used in the
                  -- kind signature are bound on the left.  Reason:
                  -- the last clause of Note [CUSKs: complete user-supplied kind signatures]
                  -- in GHC.Hs.Decls
              -> RnM (b, FreeVars)

-- See Note [bindHsQTyVars examples]
-- (a) Bring kind variables into scope
--     both (i)  passed in body_kv_occs
--     and  (ii) mentioned in the kinds of hsq_bndrs
-- (b) Bring type variables into scope
--
bindHsQTyVars :: forall a b.
HsDocContext
-> Maybe a
-> FreeKiTyVars
-> LHsQTyVars GhcPs
-> (LHsQTyVars GhcRn -> FreeKiTyVars -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
bindHsQTyVars HsDocContext
doc Maybe a
mb_assoc FreeKiTyVars
body_kv_occs LHsQTyVars GhcPs
hsq_bndrs LHsQTyVars GhcRn -> FreeKiTyVars -> RnM (b, FreeVars)
thing_inside
  = do { let bndr_kv_occs :: FreeKiTyVars
bndr_kv_occs = [LHsTyVarBndr (HsBndrVis GhcPs) GhcPs] -> FreeKiTyVars
forall flag. [LHsTyVarBndr flag GhcPs] -> FreeKiTyVars
extractHsTyVarBndrsKVs [LHsTyVarBndr (HsBndrVis GhcPs) GhcPs]
hs_tv_bndrs

       ; let -- See Note [bindHsQTyVars examples] for what
             -- all these various things are doing
             bndrs, implicit_kvs :: [LocatedN RdrName]
             bndrs :: FreeKiTyVars
bndrs        = (GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
 -> GenLocated SrcSpanAnnN RdrName)
-> [GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)]
-> FreeKiTyVars
forall a b. (a -> b) -> [a] -> [b]
map LHsTyVarBndr (HsBndrVis GhcPs) GhcPs -> LocatedN (IdP GhcPs)
GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
-> GenLocated SrcSpanAnnN RdrName
forall (p :: Pass) flag.
(Anno (IdGhcP p) ~ SrcSpanAnnN) =>
LHsTyVarBndr flag (GhcPass p) -> LocatedN (IdP (GhcPass p))
hsLTyVarLocName [LHsTyVarBndr (HsBndrVis GhcPs) GhcPs]
[GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)]
hs_tv_bndrs
             implicit_kvs :: FreeKiTyVars
implicit_kvs = FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
filterFreeVarsToBind FreeKiTyVars
bndrs (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
               FreeKiTyVars
bndr_kv_occs FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
forall a. [a] -> [a] -> [a]
++ FreeKiTyVars
body_kv_occs
             body_remaining :: FreeKiTyVars
body_remaining = FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
filterFreeVarsToBind FreeKiTyVars
bndr_kv_occs (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
              FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
filterFreeVarsToBind FreeKiTyVars
bndrs FreeKiTyVars
body_kv_occs

       ; String -> SDoc -> TcRn ()
traceRn String
"checkMixedVars3" (SDoc -> TcRn ()) -> SDoc -> TcRn ()
forall a b. (a -> b) -> a -> b
$
           [SDoc] -> SDoc
forall doc. IsDoc doc => [doc] -> doc
vcat [ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"bndrs"   SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> [GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)]
-> SDoc
forall a. Outputable a => a -> SDoc
ppr [LHsTyVarBndr (HsBndrVis GhcPs) GhcPs]
[GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)]
hs_tv_bndrs
                , String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"bndr_kv_occs"   SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> FreeKiTyVars -> SDoc
forall a. Outputable a => a -> SDoc
ppr FreeKiTyVars
bndr_kv_occs
                , String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"body_kv_occs"   SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> FreeKiTyVars -> SDoc
forall a. Outputable a => a -> SDoc
ppr FreeKiTyVars
body_kv_occs
                , String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"implicit_kvs"   SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> FreeKiTyVars -> SDoc
forall a. Outputable a => a -> SDoc
ppr FreeKiTyVars
implicit_kvs
                , String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"body_remaining" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> FreeKiTyVars -> SDoc
forall a. Outputable a => a -> SDoc
ppr FreeKiTyVars
body_remaining
                ]

       ; Maybe a
-> FreeKiTyVars
-> ([Name] -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
forall assoc a.
Maybe assoc
-> FreeKiTyVars
-> ([Name] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnImplicitTvOccs Maybe a
mb_assoc FreeKiTyVars
implicit_kvs (([Name] -> RnM (b, FreeVars)) -> RnM (b, FreeVars))
-> ([Name] -> RnM (b, FreeVars)) -> RnM (b, FreeVars)
forall a b. (a -> b) -> a -> b
$ \ [Name]
implicit_kv_nms' ->
         HsDocContext
-> WarnUnusedForalls
-> Maybe a
-> [LHsTyVarBndr (HsBndrVis GhcPs) GhcPs]
-> ([LHsTyVarBndr (HsBndrVis GhcPs) GhcRn] -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
forall flag a b.
OutputableBndrFlag flag 'Renamed =>
HsDocContext
-> WarnUnusedForalls
-> Maybe a
-> [LHsTyVarBndr flag GhcPs]
-> ([LHsTyVarBndr flag GhcRn] -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
bindLHsTyVarBndrs HsDocContext
doc WarnUnusedForalls
NoWarnUnusedForalls Maybe a
mb_assoc [LHsTyVarBndr (HsBndrVis GhcPs) GhcPs]
hs_tv_bndrs (([LHsTyVarBndr (HsBndrVis GhcPs) GhcRn] -> RnM (b, FreeVars))
 -> RnM (b, FreeVars))
-> ([LHsTyVarBndr (HsBndrVis GhcPs) GhcRn] -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
forall a b. (a -> b) -> a -> b
$ \ [LHsTyVarBndr (HsBndrVis GhcPs) GhcRn]
rn_bndrs ->
           -- This is the only call site for bindLHsTyVarBndrs where we pass
           -- NoWarnUnusedForalls, which suppresses -Wunused-foralls warnings.
           -- See Note [Suppress -Wunused-foralls when binding LHsQTyVars].
    do { [GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn)]
rn_bndrs <- (GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcRn)
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn)))
-> [GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcRn)]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     [GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse LHsTyVarBndr (HsBndrVis GhcPs) GhcRn
-> RnM (LHsTyVarBndr (HsBndrVis GhcRn) GhcRn)
GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcRn)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn))
rnLHsTyVarBndrVisFlag [LHsTyVarBndr (HsBndrVis GhcPs) GhcRn]
[GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcRn)]
rn_bndrs
       ; let -- The SrcSpan that rnImplicitTvOccs will attach to each Name will
             -- span the entire declaration to which the LHsQTyVars belongs,
             -- which will be reflected in warning and error messages. We can
             -- be a little more precise than that by pointing to the location
             -- of the LHsQTyVars instead, which is what bndrs_loc
             -- corresponds to.
             implicit_kv_nms :: [Name]
implicit_kv_nms = (Name -> Name) -> [Name] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Name -> SrcSpan -> Name
`setNameLoc` SrcSpan
bndrs_loc) [Name]
implicit_kv_nms'

       ; String -> SDoc -> TcRn ()
traceRn String
"bindHsQTyVars" (LHsQTyVars GhcPs -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsQTyVars GhcPs
hsq_bndrs SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ [Name] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Name]
implicit_kv_nms SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ [GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn)]
-> SDoc
forall a. Outputable a => a -> SDoc
ppr [GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn)]
rn_bndrs)
       ; LHsQTyVars GhcRn -> FreeKiTyVars -> RnM (b, FreeVars)
thing_inside (HsQTvs { hsq_ext :: XHsQTvs GhcRn
hsq_ext = [Name]
XHsQTvs GhcRn
implicit_kv_nms
                              , hsq_explicit :: [LHsTyVarBndr (HsBndrVis GhcRn) GhcRn]
hsq_explicit  = [LHsTyVarBndr (HsBndrVis GhcRn) GhcRn]
[GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn)]
rn_bndrs })
                      FreeKiTyVars
body_remaining } }
  where
    hs_tv_bndrs :: [LHsTyVarBndr (HsBndrVis GhcPs) GhcPs]
hs_tv_bndrs = LHsQTyVars GhcPs -> [LHsTyVarBndr (HsBndrVis GhcPs) GhcPs]
forall pass.
LHsQTyVars pass -> [LHsTyVarBndr (HsBndrVis pass) pass]
hsQTvExplicit LHsQTyVars GhcPs
hsq_bndrs

    -- The SrcSpan of the LHsQTyVars. For example, bndrs_loc would be the
    -- highlighted part in the class below:
    --
    --   class C (a :: j) (b :: k) where
    --            ^^^^^^^^^^^^^^^
    bndrs_loc :: SrcSpan
bndrs_loc = case (GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
 -> SrcSpan)
-> [GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)]
-> [SrcSpan]
forall a b. (a -> b) -> [a] -> [b]
map LHsTyVarBndr (HsBndrVis GhcPs) GhcPs -> SrcSpan
GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
-> SrcSpan
forall flag. LHsTyVarBndr flag GhcPs -> SrcSpan
get_bndr_loc [LHsTyVarBndr (HsBndrVis GhcPs) GhcPs]
[GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)]
hs_tv_bndrs [SrcSpan] -> [SrcSpan] -> [SrcSpan]
forall a. [a] -> [a] -> [a]
++ (GenLocated SrcSpanAnnN RdrName -> SrcSpan)
-> FreeKiTyVars -> [SrcSpan]
forall a b. (a -> b) -> [a] -> [b]
map GenLocated SrcSpanAnnN RdrName -> SrcSpan
forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA FreeKiTyVars
body_kv_occs of
      []         -> String -> SrcSpan
forall a. HasCallStack => String -> a
panic String
"bindHsQTyVars.bndrs_loc"
      [SrcSpan
loc]      -> SrcSpan
loc
      (SrcSpan
loc:[SrcSpan]
locs) -> SrcSpan
loc SrcSpan -> SrcSpan -> SrcSpan
`combineSrcSpans` [SrcSpan] -> SrcSpan
forall a. HasCallStack => [a] -> a
last [SrcSpan]
locs

    -- The in-tree API annotations extend the LHsTyVarBndr location to
    -- include surrounding parens. for error messages to be
    -- compatible, we recreate the location from the contents
    get_bndr_loc :: LHsTyVarBndr flag GhcPs -> SrcSpan
    get_bndr_loc :: forall flag. LHsTyVarBndr flag GhcPs -> SrcSpan
get_bndr_loc (L SrcSpanAnnA
_ (UserTyVar   XUserTyVar GhcPs
_ flag
_ LIdP GhcPs
ln)) = GenLocated SrcSpanAnnN RdrName -> SrcSpan
forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA LIdP GhcPs
GenLocated SrcSpanAnnN RdrName
ln
    get_bndr_loc (L SrcSpanAnnA
_ (KindedTyVar XKindedTyVar GhcPs
_ flag
_ LIdP GhcPs
ln LHsType GhcPs
lk))
      = SrcSpan -> SrcSpan -> SrcSpan
combineSrcSpans (GenLocated SrcSpanAnnN RdrName -> SrcSpan
forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA LIdP GhcPs
GenLocated SrcSpanAnnN RdrName
ln) (GenLocated SrcSpanAnnA (HsType GhcPs) -> SrcSpan
forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA LHsType GhcPs
GenLocated SrcSpanAnnA (HsType GhcPs)
lk)

{- Note [bindHsQTyVars examples]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Suppose we have
   data T k (a::k1) (b::k) :: k2 -> k1 -> *

Then:
  hs_tv_bndrs = [k, a::k1, b::k], the explicitly-bound variables
  bndrs       = [k,a,b]

  bndr_kv_occs = [k,k1], kind variables free in kind signatures
                         of hs_tv_bndrs

  body_kv_occs = [k2,k1], kind variables free in the
                          result kind signature

  implicit_kvs = [k1,k2,k1], kind variables free in kind signatures
                             of hs_tv_bndrs, and not bound by bndrs

* We want to quantify add implicit bindings for implicit_kvs

* If body_kv_occs is non-empty, then there is a kind variable
  mentioned in the kind signature that is not bound "on the left".
  That's one of the rules for a CUSK, so we pass that info on
  as the second argument to thing_inside.

* Order is not important in these lists.  All we are doing is
  bring Names into scope.

* bndr_kv_occs, body_kv_occs, and implicit_kvs can contain duplicates. All
  duplicate occurrences are removed when we bind them with rnImplicitTvOccs.

Finally, you may wonder why filterFreeVarsToBind removes in-scope variables
from bndr/body_kv_occs.  How can anything be in scope?  Answer:
HsQTyVars is /also/ used (slightly oddly) for Haskell-98 syntax
ConDecls
   data T a = forall (b::k). MkT a b
The ConDecl has a LHsQTyVars in it; but 'a' scopes over the entire
ConDecl.  Hence the local RdrEnv may be non-empty and we must filter
out 'a' from the free vars.  (Mind you, in this situation all the
implicit kind variables are bound at the data type level, so there
are none to bind in the ConDecl, so there are no implicitly bound
variables at all.

Note [Kind variable scoping]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If we have
  data T (a :: k) k = ...
we report "k is out of scope" for (a::k).  Reason: k is not brought
into scope until the explicit k-binding that follows.  It would be
terribly confusing to bring into scope an /implicit/ k for a's kind
and a distinct, shadowing explicit k that follows, something like
  data T {k1} (a :: k1) k = ...

So the rule is:

   the implicit binders never include any
   of the explicit binders in the group

Note that in the denerate case
  data T (a :: a) = blah
we get a complaint the second 'a' is not in scope.

That applies to foralls too: e.g.
   forall (a :: k) k . blah

But if the foralls are split, we treat the two groups separately:
   forall (a :: k). forall k. blah
Here we bring into scope an implicit k, which is later shadowed
by the explicit k.

In implementation terms

* In bindHsQTyVars 'k' is free in bndr_kv_occs; then we delete
  the binders {a,k}, and so end with no implicit binders.  Then we
  rename the binders left-to-right, and hence see that 'k' is out of
  scope in the kind of 'a'.

* Similarly in extract_hs_tv_bndrs

Note [Variables used as both types and kinds]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We bind the type variables tvs, and kvs is the set of free variables of the
kinds in the scope of the binding. Here is one typical example:

   forall a b. a -> (b::k) -> (c::a)

Here, tvs will be {a,b}, and kvs {k,a}.

We must make sure that kvs includes all of variables in the kinds of type
variable bindings. For instance:

   forall k (a :: k). Proxy a

If we only look in the body of the `forall` type, we will mistakenly conclude
that kvs is {}. But in fact, the type variable `k` is also used as a kind
variable in (a :: k), later in the binding. (This mistake lead to #14710.)
So tvs is {k,a} and kvs is {k}.

NB: we do this only at the binding site of 'tvs'.

Note [Suppress -Wunused-foralls when binding LHsQTyVars]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The WarnUnusedForalls flag controls whether bindLHsTyVarBndrs should warn about
explicit type variable binders that go unused (e.g., the `a` in
`forall a. Int`). We almost always want to warn about these, since unused type
variables can usually be deleted without any repercussions. There is one
exception to this rule, however: binding LHsQTyVars. Consider this example:

  data Proxy a = Proxy

The `a` in `Proxy a` is bound by an LHsQTyVars, and the code which brings it
into scope, bindHsQTyVars, will invoke bindLHsTyVarBndrs in turn. As such, it
has a choice to make about whether to emit -Wunused-foralls warnings or not.
If it /did/ emit warnings, then the `a` would be flagged as unused. However,
this is not what we want! Removing the `a` in `Proxy a` would change its kind
entirely, which is a huge price to pay for fixing a warning.

Unlike other forms of type variable binders, dropping "unused" variables in
an LHsQTyVars can be semantically significant. As a result, we suppress
-Wunused-foralls warnings in exactly one place: in bindHsQTyVars.
-}

bindHsOuterTyVarBndrs :: OutputableBndrFlag flag 'Renamed
                      => HsDocContext
                      -> Maybe assoc
                         -- ^ @'Just' _@ => an associated type decl
                      -> FreeKiTyVars
                      -> HsOuterTyVarBndrs flag GhcPs
                      -> (HsOuterTyVarBndrs flag GhcRn -> RnM (a, FreeVars))
                      -> RnM (a, FreeVars)
bindHsOuterTyVarBndrs :: forall flag assoc a.
OutputableBndrFlag flag 'Renamed =>
HsDocContext
-> Maybe assoc
-> FreeKiTyVars
-> HsOuterTyVarBndrs flag GhcPs
-> (HsOuterTyVarBndrs flag GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
bindHsOuterTyVarBndrs HsDocContext
doc Maybe assoc
mb_cls FreeKiTyVars
implicit_vars HsOuterTyVarBndrs flag GhcPs
outer_bndrs HsOuterTyVarBndrs flag GhcRn -> RnM (a, FreeVars)
thing_inside =
  case HsOuterTyVarBndrs flag GhcPs
outer_bndrs of
    HsOuterImplicit{} ->
      Maybe assoc
-> FreeKiTyVars
-> ([Name] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
forall assoc a.
Maybe assoc
-> FreeKiTyVars
-> ([Name] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnImplicitTvOccs Maybe assoc
mb_cls FreeKiTyVars
implicit_vars (([Name] -> RnM (a, FreeVars)) -> RnM (a, FreeVars))
-> ([Name] -> RnM (a, FreeVars)) -> RnM (a, FreeVars)
forall a b. (a -> b) -> a -> b
$ \[Name]
implicit_vars' ->
        HsOuterTyVarBndrs flag GhcRn -> RnM (a, FreeVars)
thing_inside (HsOuterTyVarBndrs flag GhcRn -> RnM (a, FreeVars))
-> HsOuterTyVarBndrs flag GhcRn -> RnM (a, FreeVars)
forall a b. (a -> b) -> a -> b
$ HsOuterImplicit { hso_ximplicit :: XHsOuterImplicit GhcRn
hso_ximplicit = [Name]
XHsOuterImplicit GhcRn
implicit_vars' }
    HsOuterExplicit{hso_bndrs :: forall flag pass.
HsOuterTyVarBndrs flag pass -> [LHsTyVarBndr flag (NoGhcTc pass)]
hso_bndrs = [LHsTyVarBndr flag (NoGhcTc GhcPs)]
exp_bndrs} ->
      -- Note: If we pass mb_cls instead of Nothing below, bindLHsTyVarBndrs
      -- will use class variables for any names the user meant to bring in
      -- scope here. This is an explicit forall, so we want fresh names, not
      -- class variables. Thus: always pass Nothing.
      HsDocContext
-> WarnUnusedForalls
-> Maybe Any
-> [LHsTyVarBndr flag GhcPs]
-> ([LHsTyVarBndr flag GhcRn] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
forall flag a b.
OutputableBndrFlag flag 'Renamed =>
HsDocContext
-> WarnUnusedForalls
-> Maybe a
-> [LHsTyVarBndr flag GhcPs]
-> ([LHsTyVarBndr flag GhcRn] -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
bindLHsTyVarBndrs HsDocContext
doc WarnUnusedForalls
WarnUnusedForalls Maybe Any
forall a. Maybe a
Nothing [LHsTyVarBndr flag (NoGhcTc GhcPs)]
[LHsTyVarBndr flag GhcPs]
exp_bndrs (([LHsTyVarBndr flag GhcRn] -> RnM (a, FreeVars))
 -> RnM (a, FreeVars))
-> ([LHsTyVarBndr flag GhcRn] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
forall a b. (a -> b) -> a -> b
$ \[LHsTyVarBndr flag GhcRn]
exp_bndrs' ->
        HsOuterTyVarBndrs flag GhcRn -> RnM (a, FreeVars)
thing_inside (HsOuterTyVarBndrs flag GhcRn -> RnM (a, FreeVars))
-> HsOuterTyVarBndrs flag GhcRn -> RnM (a, FreeVars)
forall a b. (a -> b) -> a -> b
$ HsOuterExplicit { hso_xexplicit :: XHsOuterExplicit GhcRn flag
hso_xexplicit = XHsOuterExplicit GhcRn flag
NoExtField
noExtField
                                       , hso_bndrs :: [LHsTyVarBndr flag (NoGhcTc GhcRn)]
hso_bndrs     = [LHsTyVarBndr flag (NoGhcTc GhcRn)]
[LHsTyVarBndr flag GhcRn]
exp_bndrs' }

-- See Note [Term variable capture and implicit quantification]
warn_term_var_capture :: LocatedN RdrName -> RnM ()
warn_term_var_capture :: GenLocated SrcSpanAnnN RdrName -> TcRn ()
warn_term_var_capture GenLocated SrcSpanAnnN RdrName
lVar = do
    GlobalRdrEnv
gbl_env <- TcRn GlobalRdrEnv
getGlobalRdrEnv
    LocalRdrEnv
local_env <- RnM LocalRdrEnv
getLocalRdrEnv
    case RdrName -> Maybe RdrName
demoteRdrNameTv (RdrName -> Maybe RdrName) -> RdrName -> Maybe RdrName
forall a b. (a -> b) -> a -> b
$ GenLocated SrcSpanAnnN RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnN RdrName
lVar of
      Maybe RdrName
Nothing           -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      Just RdrName
demoted_name -> do
        let global_vars :: [GlobalRdrEltX GREInfo]
global_vars = GlobalRdrEnv -> LookupGRE GREInfo -> [GlobalRdrEltX GREInfo]
forall info.
GlobalRdrEnvX info -> LookupGRE info -> [GlobalRdrEltX info]
lookupGRE GlobalRdrEnv
gbl_env (RdrName -> WhichGREs GREInfo -> LookupGRE GREInfo
forall info. RdrName -> WhichGREs info -> LookupGRE info
LookupRdrName RdrName
demoted_name WhichGREs GREInfo
forall info. WhichGREs info
SameNameSpace)
        let mlocal_var :: Maybe Name
mlocal_var  = LocalRdrEnv -> RdrName -> Maybe Name
lookupLocalRdrEnv LocalRdrEnv
local_env RdrName
demoted_name
        case Maybe Name
mlocal_var of
          Just Name
name -> GenLocated SrcSpanAnnN RdrName
-> Either [GlobalRdrEltX GREInfo] Name -> TcRn ()
warnCapturedTerm GenLocated SrcSpanAnnN RdrName
lVar (Name -> Either [GlobalRdrEltX GREInfo] Name
forall a b. b -> Either a b
Right Name
name)
          Maybe Name
Nothing   -> Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([GlobalRdrEltX GREInfo] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [GlobalRdrEltX GREInfo]
global_vars) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
                         GenLocated SrcSpanAnnN RdrName
-> Either [GlobalRdrEltX GREInfo] Name -> TcRn ()
warnCapturedTerm GenLocated SrcSpanAnnN RdrName
lVar ([GlobalRdrEltX GREInfo] -> Either [GlobalRdrEltX GREInfo] Name
forall a b. a -> Either a b
Left [GlobalRdrEltX GREInfo]
global_vars)

bindHsForAllTelescope :: HsDocContext
                      -> HsForAllTelescope GhcPs
                      -> (HsForAllTelescope GhcRn -> RnM (a, FreeVars))
                      -> RnM (a, FreeVars)
bindHsForAllTelescope :: forall a.
HsDocContext
-> HsForAllTelescope GhcPs
-> (HsForAllTelescope GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
bindHsForAllTelescope HsDocContext
doc HsForAllTelescope GhcPs
tele HsForAllTelescope GhcRn -> RnM (a, FreeVars)
thing_inside =
  case HsForAllTelescope GhcPs
tele of
    HsForAllVis { hsf_vis_bndrs :: forall pass. HsForAllTelescope pass -> [LHsTyVarBndr () pass]
hsf_vis_bndrs = [LHsTyVarBndr () GhcPs]
bndrs } ->
      HsDocContext
-> WarnUnusedForalls
-> Maybe Any
-> [LHsTyVarBndr () GhcPs]
-> ([LHsTyVarBndr () GhcRn] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
forall flag a b.
OutputableBndrFlag flag 'Renamed =>
HsDocContext
-> WarnUnusedForalls
-> Maybe a
-> [LHsTyVarBndr flag GhcPs]
-> ([LHsTyVarBndr flag GhcRn] -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
bindLHsTyVarBndrs HsDocContext
doc WarnUnusedForalls
WarnUnusedForalls Maybe Any
forall a. Maybe a
Nothing [LHsTyVarBndr () GhcPs]
bndrs (([LHsTyVarBndr () GhcRn] -> RnM (a, FreeVars))
 -> RnM (a, FreeVars))
-> ([LHsTyVarBndr () GhcRn] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
forall a b. (a -> b) -> a -> b
$ \[LHsTyVarBndr () GhcRn]
bndrs' ->
        HsForAllTelescope GhcRn -> RnM (a, FreeVars)
thing_inside (HsForAllTelescope GhcRn -> RnM (a, FreeVars))
-> HsForAllTelescope GhcRn -> RnM (a, FreeVars)
forall a b. (a -> b) -> a -> b
$ EpAnnForallTy -> [LHsTyVarBndr () GhcRn] -> HsForAllTelescope GhcRn
forall (p :: Pass).
EpAnnForallTy
-> [LHsTyVarBndr () (GhcPass p)] -> HsForAllTelescope (GhcPass p)
mkHsForAllVisTele EpAnnForallTy
forall a. EpAnn a
noAnn [LHsTyVarBndr () GhcRn]
bndrs'
    HsForAllInvis { hsf_invis_bndrs :: forall pass.
HsForAllTelescope pass -> [LHsTyVarBndr Specificity pass]
hsf_invis_bndrs = [LHsTyVarBndr Specificity GhcPs]
bndrs } ->
      HsDocContext
-> WarnUnusedForalls
-> Maybe Any
-> [LHsTyVarBndr Specificity GhcPs]
-> ([LHsTyVarBndr Specificity GhcRn] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
forall flag a b.
OutputableBndrFlag flag 'Renamed =>
HsDocContext
-> WarnUnusedForalls
-> Maybe a
-> [LHsTyVarBndr flag GhcPs]
-> ([LHsTyVarBndr flag GhcRn] -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
bindLHsTyVarBndrs HsDocContext
doc WarnUnusedForalls
WarnUnusedForalls Maybe Any
forall a. Maybe a
Nothing [LHsTyVarBndr Specificity GhcPs]
bndrs (([LHsTyVarBndr Specificity GhcRn] -> RnM (a, FreeVars))
 -> RnM (a, FreeVars))
-> ([LHsTyVarBndr Specificity GhcRn] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
forall a b. (a -> b) -> a -> b
$ \[LHsTyVarBndr Specificity GhcRn]
bndrs' ->
        HsForAllTelescope GhcRn -> RnM (a, FreeVars)
thing_inside (HsForAllTelescope GhcRn -> RnM (a, FreeVars))
-> HsForAllTelescope GhcRn -> RnM (a, FreeVars)
forall a b. (a -> b) -> a -> b
$ EpAnnForallTy
-> [LHsTyVarBndr Specificity GhcRn] -> HsForAllTelescope GhcRn
forall (p :: Pass).
EpAnnForallTy
-> [LHsTyVarBndr Specificity (GhcPass p)]
-> HsForAllTelescope (GhcPass p)
mkHsForAllInvisTele EpAnnForallTy
forall a. EpAnn a
noAnn [LHsTyVarBndr Specificity GhcRn]
bndrs'

-- | Should GHC warn if a quantified type variable goes unused? Usually, the
-- answer is \"yes\", but in the particular case of binding 'LHsQTyVars', we
-- avoid emitting warnings.
-- See @Note [Suppress -Wunused-foralls when binding LHsQTyVars]@.
data WarnUnusedForalls
  = WarnUnusedForalls
  | NoWarnUnusedForalls

instance Outputable WarnUnusedForalls where
  ppr :: WarnUnusedForalls -> SDoc
ppr WarnUnusedForalls
wuf = String -> SDoc
forall doc. IsLine doc => String -> doc
text (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ case WarnUnusedForalls
wuf of
    WarnUnusedForalls
WarnUnusedForalls   -> String
"WarnUnusedForalls"
    WarnUnusedForalls
NoWarnUnusedForalls -> String
"NoWarnUnusedForalls"

bindLHsTyVarBndrs :: (OutputableBndrFlag flag 'Renamed)
                  => HsDocContext
                  -> WarnUnusedForalls
                  -> Maybe a               -- Just _  => an associated type decl
                  -> [LHsTyVarBndr flag GhcPs]  -- User-written tyvars
                  -> ([LHsTyVarBndr flag GhcRn] -> RnM (b, FreeVars))
                  -> RnM (b, FreeVars)
bindLHsTyVarBndrs :: forall flag a b.
OutputableBndrFlag flag 'Renamed =>
HsDocContext
-> WarnUnusedForalls
-> Maybe a
-> [LHsTyVarBndr flag GhcPs]
-> ([LHsTyVarBndr flag GhcRn] -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
bindLHsTyVarBndrs HsDocContext
doc WarnUnusedForalls
wuf Maybe a
mb_assoc [LHsTyVarBndr flag GhcPs]
tv_bndrs [LHsTyVarBndr flag GhcRn] -> RnM (b, FreeVars)
thing_inside
  = do { Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe a -> Bool
forall a. Maybe a -> Bool
isNothing Maybe a
mb_assoc) (FreeKiTyVars -> TcRn ()
checkShadowedRdrNames FreeKiTyVars
tv_names_w_loc)
       ; FreeKiTyVars -> TcRn ()
checkDupRdrNames FreeKiTyVars
tv_names_w_loc
       ; [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)]
-> ([GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
    -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
go [LHsTyVarBndr flag GhcPs]
[GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)]
tv_bndrs [LHsTyVarBndr flag GhcRn] -> RnM (b, FreeVars)
[GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
-> RnM (b, FreeVars)
thing_inside }
  where
    tv_names_w_loc :: FreeKiTyVars
tv_names_w_loc = (GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)
 -> GenLocated SrcSpanAnnN RdrName)
-> [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)]
-> FreeKiTyVars
forall a b. (a -> b) -> [a] -> [b]
map LHsTyVarBndr flag GhcPs -> LocatedN (IdP GhcPs)
GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)
-> GenLocated SrcSpanAnnN RdrName
forall (p :: Pass) flag.
(Anno (IdGhcP p) ~ SrcSpanAnnN) =>
LHsTyVarBndr flag (GhcPass p) -> LocatedN (IdP (GhcPass p))
hsLTyVarLocName [LHsTyVarBndr flag GhcPs]
[GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)]
tv_bndrs

    go :: [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)]
-> ([GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
    -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
go []     [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
-> RnM (b, FreeVars)
thing_inside = [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
-> RnM (b, FreeVars)
thing_inside []
    go (GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)
b:[GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)]
bs) [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
-> RnM (b, FreeVars)
thing_inside = HsDocContext
-> Maybe a
-> LHsTyVarBndr flag GhcPs
-> (LHsTyVarBndr flag GhcRn -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
forall a flag b.
HsDocContext
-> Maybe a
-> LHsTyVarBndr flag GhcPs
-> (LHsTyVarBndr flag GhcRn -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
bindLHsTyVarBndr HsDocContext
doc Maybe a
mb_assoc LHsTyVarBndr flag GhcPs
GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)
b ((LHsTyVarBndr flag GhcRn -> RnM (b, FreeVars))
 -> RnM (b, FreeVars))
-> (LHsTyVarBndr flag GhcRn -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
forall a b. (a -> b) -> a -> b
$ \ LHsTyVarBndr flag GhcRn
b' ->
                             do { (b
res, FreeVars
fvs) <- [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)]
-> ([GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
    -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
go [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)]
bs (([GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
  -> RnM (b, FreeVars))
 -> RnM (b, FreeVars))
-> ([GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
    -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
forall a b. (a -> b) -> a -> b
$ \ [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
bs' ->
                                                [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
-> RnM (b, FreeVars)
thing_inside (LHsTyVarBndr flag GhcRn
GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)
b' GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)
-> [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
-> [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
forall a. a -> [a] -> [a]
: [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)]
bs')
                                ; GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)
-> FreeVars -> TcRn ()
warn_unused LHsTyVarBndr flag GhcRn
GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)
b' FreeVars
fvs
                                ; (b, FreeVars) -> RnM (b, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
res, FreeVars
fvs) }

    warn_unused :: GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)
-> FreeVars -> TcRn ()
warn_unused GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)
tv_bndr FreeVars
fvs = case WarnUnusedForalls
wuf of
      WarnUnusedForalls
WarnUnusedForalls   -> HsDocContext -> LHsTyVarBndr flag GhcRn -> FreeVars -> TcRn ()
forall flag.
OutputableBndrFlag flag 'Renamed =>
HsDocContext -> LHsTyVarBndr flag GhcRn -> FreeVars -> TcRn ()
warnUnusedForAll HsDocContext
doc LHsTyVarBndr flag GhcRn
GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)
tv_bndr FreeVars
fvs
      WarnUnusedForalls
NoWarnUnusedForalls -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

bindLHsTyVarBndr :: HsDocContext
                 -> Maybe a   -- associated class
                 -> LHsTyVarBndr flag GhcPs
                 -> (LHsTyVarBndr flag GhcRn -> RnM (b, FreeVars))
                 -> RnM (b, FreeVars)
bindLHsTyVarBndr :: forall a flag b.
HsDocContext
-> Maybe a
-> LHsTyVarBndr flag GhcPs
-> (LHsTyVarBndr flag GhcRn -> RnM (b, FreeVars))
-> RnM (b, FreeVars)
bindLHsTyVarBndr HsDocContext
_doc Maybe a
mb_assoc (L SrcSpanAnnA
loc
                                 (UserTyVar XUserTyVar GhcPs
x flag
fl
                                    lrdr :: LIdP GhcPs
lrdr@(L SrcSpanAnnN
lv RdrName
_))) LHsTyVarBndr flag GhcRn -> RnM (b, FreeVars)
thing_inside
  = do { Name
nm <- Maybe a
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
forall a.
Maybe a
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
newTyVarNameRn Maybe a
mb_assoc LIdP GhcPs
GenLocated SrcSpanAnnN RdrName
lrdr
       ; [Name] -> RnM (b, FreeVars) -> RnM (b, FreeVars)
forall a. [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
bindLocalNamesFV [Name
nm] (RnM (b, FreeVars) -> RnM (b, FreeVars))
-> RnM (b, FreeVars) -> RnM (b, FreeVars)
forall a b. (a -> b) -> a -> b
$
         LHsTyVarBndr flag GhcRn -> RnM (b, FreeVars)
thing_inside (SrcSpanAnnA
-> HsTyVarBndr flag GhcRn
-> GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc (XUserTyVar GhcRn -> flag -> LIdP GhcRn -> HsTyVarBndr flag GhcRn
forall flag pass.
XUserTyVar pass -> flag -> LIdP pass -> HsTyVarBndr flag pass
UserTyVar XUserTyVar GhcPs
XUserTyVar GhcRn
x flag
fl (SrcSpanAnnN -> Name -> GenLocated SrcSpanAnnN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
lv Name
nm))) }

bindLHsTyVarBndr HsDocContext
doc Maybe a
mb_assoc (L SrcSpanAnnA
loc (KindedTyVar XKindedTyVar GhcPs
x flag
fl lrdr :: LIdP GhcPs
lrdr@(L SrcSpanAnnN
lv RdrName
_) LHsType GhcPs
kind))
                 LHsTyVarBndr flag GhcRn -> RnM (b, FreeVars)
thing_inside
  = do { Bool
sig_ok <- Extension -> TcRnIf TcGblEnv TcLclEnv Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.KindSignatures
           ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
sig_ok (HsDocContext -> LHsType GhcPs -> TcRn ()
badKindSigErr HsDocContext
doc LHsType GhcPs
kind)
           ; (GenLocated SrcSpanAnnA (HsType GhcRn)
kind', FreeVars
fvs1) <- HsDocContext -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsKind HsDocContext
doc LHsType GhcPs
kind
           ; Name
tv_nm  <- Maybe a
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
forall a.
Maybe a
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
newTyVarNameRn Maybe a
mb_assoc LIdP GhcPs
GenLocated SrcSpanAnnN RdrName
lrdr
           ; (b
b, FreeVars
fvs2) <- [Name] -> RnM (b, FreeVars) -> RnM (b, FreeVars)
forall a. [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
bindLocalNamesFV [Name
tv_nm]
               (RnM (b, FreeVars) -> RnM (b, FreeVars))
-> RnM (b, FreeVars) -> RnM (b, FreeVars)
forall a b. (a -> b) -> a -> b
$ LHsTyVarBndr flag GhcRn -> RnM (b, FreeVars)
thing_inside (SrcSpanAnnA
-> HsTyVarBndr flag GhcRn
-> GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc (XKindedTyVar GhcRn
-> flag -> LIdP GhcRn -> LHsType GhcRn -> HsTyVarBndr flag GhcRn
forall flag pass.
XKindedTyVar pass
-> flag -> LIdP pass -> LHsKind pass -> HsTyVarBndr flag pass
KindedTyVar XKindedTyVar GhcPs
XKindedTyVar GhcRn
x flag
fl (SrcSpanAnnN -> Name -> GenLocated SrcSpanAnnN Name
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
lv Name
tv_nm) LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
kind'))
           ; (b, FreeVars) -> RnM (b, FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) }

-- Check for TypeAbstractions and update the type parameter of HsBndrVis.
-- The binder itself is already renamed and is returned unmodified.
rnLHsTyVarBndrVisFlag
  :: LHsTyVarBndr (HsBndrVis GhcPs) GhcRn
  -> RnM (LHsTyVarBndr (HsBndrVis GhcRn) GhcRn)
rnLHsTyVarBndrVisFlag :: LHsTyVarBndr (HsBndrVis GhcPs) GhcRn
-> RnM (LHsTyVarBndr (HsBndrVis GhcRn) GhcRn)
rnLHsTyVarBndrVisFlag (L SrcSpanAnnA
loc HsTyVarBndr (HsBndrVis GhcPs) GhcRn
bndr) = do
  let lbndr :: GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn)
lbndr = SrcSpanAnnA
-> HsTyVarBndr (HsBndrVis GhcRn) GhcRn
-> GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc ((HsBndrVis GhcPs -> HsBndrVis GhcRn)
-> HsTyVarBndr (HsBndrVis GhcPs) GhcRn
-> HsTyVarBndr (HsBndrVis GhcRn) GhcRn
forall flag flag' (pass :: Pass).
(flag -> flag')
-> HsTyVarBndr flag (GhcPass pass)
-> HsTyVarBndr flag' (GhcPass pass)
updateHsTyVarBndrFlag HsBndrVis GhcPs -> HsBndrVis GhcRn
rnHsBndrVis HsTyVarBndr (HsBndrVis GhcPs) GhcRn
bndr)
  Extension -> TcRn () -> TcRn ()
forall gbl lcl. Extension -> TcRnIf gbl lcl () -> TcRnIf gbl lcl ()
unlessXOptM Extension
LangExt.TypeAbstractions (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
    Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (HsBndrVis GhcPs -> Bool
forall pass. HsBndrVis pass -> Bool
isHsBndrInvisible (HsTyVarBndr (HsBndrVis GhcPs) GhcRn -> HsBndrVis GhcPs
forall flag (pass :: Pass). HsTyVarBndr flag (GhcPass pass) -> flag
hsTyVarBndrFlag HsTyVarBndr (HsBndrVis GhcPs) GhcRn
bndr)) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
      TcRnMessage -> TcRn ()
addErr (LHsTyVarBndr (HsBndrVis GhcRn) GhcRn -> TcRnMessage
TcRnIllegalInvisTyVarBndr LHsTyVarBndr (HsBndrVis GhcRn) GhcRn
GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn)
lbndr)
  GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcRn) GhcRn)
lbndr

-- rnHsBndrVis is a no-op. We could use 'coerce' in an ideal world,
-- but GHC can't crack this nut because type families are involved:
-- HsBndrInvisible stores (LHsToken "@" pass), which is defined via XRec.
rnHsBndrVis :: HsBndrVis GhcPs -> HsBndrVis GhcRn
rnHsBndrVis :: HsBndrVis GhcPs -> HsBndrVis GhcRn
rnHsBndrVis HsBndrVis GhcPs
HsBndrRequired = HsBndrVis GhcRn
forall pass. HsBndrVis pass
HsBndrRequired
rnHsBndrVis (HsBndrInvisible LHsToken "@" GhcPs
at) = LHsToken "@" GhcRn -> HsBndrVis GhcRn
forall pass. LHsToken "@" pass -> HsBndrVis pass
HsBndrInvisible LHsToken "@" GhcPs
LHsToken "@" GhcRn
at

newTyVarNameRn, newTyVarNameRnImplicit
  :: Maybe a -- associated class
  -> LocatedN RdrName -> RnM Name
newTyVarNameRn :: forall a.
Maybe a
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
newTyVarNameRn         Maybe a
mb_assoc = Maybe a
-> (GenLocated SrcSpanAnnN RdrName
    -> IOEnv (Env TcGblEnv TcLclEnv) Name)
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
forall a.
Maybe a
-> (GenLocated SrcSpanAnnN RdrName
    -> IOEnv (Env TcGblEnv TcLclEnv) Name)
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
new_tv_name_rn Maybe a
mb_assoc GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
newLocalBndrRn
newTyVarNameRnImplicit :: forall a.
Maybe a
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
newTyVarNameRnImplicit Maybe a
mb_assoc = Maybe a
-> (GenLocated SrcSpanAnnN RdrName
    -> IOEnv (Env TcGblEnv TcLclEnv) Name)
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
forall a.
Maybe a
-> (GenLocated SrcSpanAnnN RdrName
    -> IOEnv (Env TcGblEnv TcLclEnv) Name)
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
new_tv_name_rn Maybe a
mb_assoc ((GenLocated SrcSpanAnnN RdrName
  -> IOEnv (Env TcGblEnv TcLclEnv) Name)
 -> GenLocated SrcSpanAnnN RdrName
 -> IOEnv (Env TcGblEnv TcLclEnv) Name)
-> (GenLocated SrcSpanAnnN RdrName
    -> IOEnv (Env TcGblEnv TcLclEnv) Name)
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
forall a b. (a -> b) -> a -> b
$ \GenLocated SrcSpanAnnN RdrName
lrdr ->
  do { GenLocated SrcSpanAnnN RdrName -> TcRn ()
warn_term_var_capture GenLocated SrcSpanAnnN RdrName
lrdr
     ; GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
newLocalBndrRn GenLocated SrcSpanAnnN RdrName
lrdr }

new_tv_name_rn :: Maybe a -- associated class
               -> (LocatedN RdrName -> RnM Name) -- how to create a new name
               -> (LocatedN RdrName -> RnM Name)
new_tv_name_rn :: forall a.
Maybe a
-> (GenLocated SrcSpanAnnN RdrName
    -> IOEnv (Env TcGblEnv TcLclEnv) Name)
-> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
new_tv_name_rn Maybe a
Nothing  GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
cont GenLocated SrcSpanAnnN RdrName
lrdr = GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
cont GenLocated SrcSpanAnnN RdrName
lrdr
new_tv_name_rn (Just a
_) GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
cont lrdr :: GenLocated SrcSpanAnnN RdrName
lrdr@(L SrcSpanAnnN
_ RdrName
rdr)
  = do { LocalRdrEnv
rdr_env <- RnM LocalRdrEnv
getLocalRdrEnv
       ; case LocalRdrEnv -> RdrName -> Maybe Name
lookupLocalRdrEnv LocalRdrEnv
rdr_env RdrName
rdr of
           Just Name
n -> Name -> IOEnv (Env TcGblEnv TcLclEnv) Name
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Name
n -- Use the same Name as the parent class decl
           Maybe Name
_      -> GenLocated SrcSpanAnnN RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) Name
cont GenLocated SrcSpanAnnN RdrName
lrdr }

{- Note [Term variable capture and implicit quantification]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Wterm-variable-capture is a warning introduced in GHC Proposal #281 "Visible forall in types of terms",
Section 7.3: https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0281-visible-forall.rst#73implicit-quantification

Its purpose is to notify users when implicit quantification occurs that would
stop working under RequiredTypeArguments (a future GHC extension). Example:

   a = 42
   id :: a -> a

As it stands, the `a` in the signature `id :: a -> a` is considered free and
leads to implicit quantification, as if the user wrote `id :: forall a. a -> a`.
Under RequiredTypeArguments it will capture the term-level variable `a` (bound by `a = 42`),
leading to a type error.

`warn_term_var_capture` detects this by demoting the namespace of the
implicitly quantified type variable (`TvName` becomes `VarName`) and looking it up
in the environment. But when do we call `warn_term_var_capture`? It's tempting
to do so at the start of `rnImplicitTvOccs`, as soon as we know our implicit
variables:

  rnImplicitTvOccs mb_assoc implicit_vs_with_dups thing_inside
    = do { let implicit_vs = nubN implicit_vs_with_dups
         ; mapM_ warn_term_var_capture implicit_vs
         ... }

This approach generates false positives (#23434) because it misses a corner
case: class variables in associated types. Consider the following example:

  k = 12
  class C k a where
    type AT a :: k -> Type

If we look at the signature for `AT` in isolation, the `k` looks like a free
variable, so it's passed to `rnImplicitTvOccs`. And if we passed it to
`warn_term_var_capture`, we would find the `k` bound by `k = 12` and report a warning.
But we don't want that: `k` is actually bound in the declaration header of the
parent class.

The solution is to check if it's a class variable (this is done in `new_tv_name_rn`)
before we check for term variable capture.
-}

{-
*********************************************************
*                                                       *
        ConDeclField
*                                                       *
*********************************************************

When renaming a ConDeclField, we have to find the FieldLabel
associated with each field.  But we already have all the FieldLabels
available (since they were brought into scope by
GHC.Rename.Names.getLocalNonValBinders), so we just take the list as an
argument, build a map and look them up.
-}

rnConDeclFields :: HsDocContext -> [FieldLabel] -> [LConDeclField GhcPs]
                -> RnM ([LConDeclField GhcRn], FreeVars)
-- Also called from GHC.Rename.Module
-- No wildcards can appear in record fields
rnConDeclFields :: HsDocContext
-> [FieldLabel]
-> [LConDeclField GhcPs]
-> RnM ([LConDeclField GhcRn], FreeVars)
rnConDeclFields HsDocContext
ctxt [FieldLabel]
fls [LConDeclField GhcPs]
fields
   = (GenLocated SrcSpanAnnA (ConDeclField GhcPs)
 -> RnM (GenLocated SrcSpanAnnA (ConDeclField GhcRn), FreeVars))
-> [GenLocated SrcSpanAnnA (ConDeclField GhcPs)]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     ([GenLocated SrcSpanAnnA (ConDeclField GhcRn)], FreeVars)
forall (f :: * -> *) a b.
Traversable f =>
(a -> RnM (b, FreeVars)) -> f a -> RnM (f b, FreeVars)
mapFvRn (FastStringEnv FieldLabel
-> RnTyKiEnv
-> LConDeclField GhcPs
-> RnM (LConDeclField GhcRn, FreeVars)
rnField FastStringEnv FieldLabel
fl_env RnTyKiEnv
env) [LConDeclField GhcPs]
[GenLocated SrcSpanAnnA (ConDeclField GhcPs)]
fields
  where
    env :: RnTyKiEnv
env    = HsDocContext -> TypeOrKind -> RnTyKiWhat -> RnTyKiEnv
mkTyKiEnv HsDocContext
ctxt TypeOrKind
TypeLevel RnTyKiWhat
RnTypeBody
    fl_env :: FastStringEnv FieldLabel
fl_env = [(FastString, FieldLabel)] -> FastStringEnv FieldLabel
forall a. [(FastString, a)] -> FastStringEnv a
mkFsEnv [ (FieldLabelString -> FastString
field_label (FieldLabelString -> FastString) -> FieldLabelString -> FastString
forall a b. (a -> b) -> a -> b
$ FieldLabel -> FieldLabelString
flLabel FieldLabel
fl, FieldLabel
fl) | FieldLabel
fl <- [FieldLabel]
fls ]

rnField :: FastStringEnv FieldLabel -> RnTyKiEnv -> LConDeclField GhcPs
        -> RnM (LConDeclField GhcRn, FreeVars)
rnField :: FastStringEnv FieldLabel
-> RnTyKiEnv
-> LConDeclField GhcPs
-> RnM (LConDeclField GhcRn, FreeVars)
rnField FastStringEnv FieldLabel
fl_env RnTyKiEnv
env (L SrcSpanAnnA
l (ConDeclField XConDeclField GhcPs
_ [LFieldOcc GhcPs]
names LHsType GhcPs
ty Maybe (LHsDoc GhcPs)
haddock_doc))
  = do { (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcPs) -> TcRn ())
-> [GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcPs)] -> TcRn ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\(L SrcAnn NoEpAnns
_ (FieldOcc XCFieldOcc GhcPs
_ XRec GhcPs RdrName
rdr_name)) -> GenLocated SrcSpanAnnN RdrName -> TcRn ()
warnForallIdentifier XRec GhcPs RdrName
GenLocated SrcSpanAnnN RdrName
rdr_name) [LFieldOcc GhcPs]
[GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcPs)]
names
       ; let new_names :: [GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)]
new_names = (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcPs)
 -> GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
-> [GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcPs)]
-> [GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)]
forall a b. (a -> b) -> [a] -> [b]
map ((FieldOcc GhcPs -> FieldOcc GhcRn)
-> GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcPs)
-> GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)
forall a b.
(a -> b)
-> GenLocated (SrcAnn NoEpAnns) a -> GenLocated (SrcAnn NoEpAnns) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (FastStringEnv FieldLabel -> FieldOcc GhcPs -> FieldOcc GhcRn
lookupField FastStringEnv FieldLabel
fl_env)) [LFieldOcc GhcPs]
[GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcPs)]
names
       ; (GenLocated SrcSpanAnnA (HsType GhcRn)
new_ty, FreeVars
fvs) <- RnTyKiEnv -> LHsType GhcPs -> RnM (LHsType GhcRn, FreeVars)
rnLHsTyKi RnTyKiEnv
env LHsType GhcPs
ty
       ; Maybe (LHsDoc GhcRn)
haddock_doc' <- (LHsDoc GhcPs -> RnM (LHsDoc GhcRn))
-> Maybe (LHsDoc GhcPs)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe (LHsDoc GhcRn))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Maybe a -> f (Maybe b)
traverse LHsDoc GhcPs -> RnM (LHsDoc GhcRn)
rnLHsDoc Maybe (LHsDoc GhcPs)
haddock_doc
       ; (GenLocated SrcSpanAnnA (ConDeclField GhcRn), FreeVars)
-> RnM (GenLocated SrcSpanAnnA (ConDeclField GhcRn), FreeVars)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpanAnnA
-> ConDeclField GhcRn
-> GenLocated SrcSpanAnnA (ConDeclField GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (XConDeclField GhcRn
-> [LFieldOcc GhcRn]
-> LHsType GhcRn
-> Maybe (LHsDoc GhcRn)
-> ConDeclField GhcRn
forall pass.
XConDeclField pass
-> [LFieldOcc pass]
-> LBangType pass
-> Maybe (LHsDoc pass)
-> ConDeclField pass
ConDeclField XConDeclField GhcRn
EpAnn [AddEpAnn]
forall a. EpAnn a
noAnn [LFieldOcc GhcRn]
[GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)]
new_names LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
new_ty Maybe (LHsDoc GhcRn)
haddock_doc')
                , FreeVars
fvs) }

lookupField :: FastStringEnv FieldLabel -> FieldOcc GhcPs -> FieldOcc GhcRn
lookupField :: FastStringEnv FieldLabel -> FieldOcc GhcPs -> FieldOcc GhcRn
lookupField FastStringEnv FieldLabel
fl_env (FieldOcc XCFieldOcc GhcPs
_ (L SrcSpanAnnN
lr RdrName
rdr)) =
    XCFieldOcc GhcRn -> XRec GhcRn RdrName -> FieldOcc GhcRn
forall pass. XCFieldOcc pass -> XRec pass RdrName -> FieldOcc pass
FieldOcc XCFieldOcc GhcRn
Name
sel (SrcSpanAnnN -> RdrName -> GenLocated SrcSpanAnnN RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
lr (RdrName -> GenLocated SrcSpanAnnN RdrName)
-> RdrName -> GenLocated SrcSpanAnnN RdrName
forall a b. (a -> b) -> a -> b
$ OccName -> RdrName
mkRdrUnqual (OccName -> RdrName) -> OccName -> RdrName
forall a b. (a -> b) -> a -> b
$ Name -> OccName
forall name. HasOccName name => name -> OccName
occName Name
sel)
  where
    lbl :: FastString
lbl = OccName -> FastString
occNameFS (OccName -> FastString) -> OccName -> FastString
forall a b. (a -> b) -> a -> b
$ RdrName -> OccName
rdrNameOcc RdrName
rdr
    sel :: Name
sel = FieldLabel -> Name
flSelector
        (FieldLabel -> Name) -> FieldLabel -> Name
forall a b. (a -> b) -> a -> b
$ String -> Maybe FieldLabel -> FieldLabel
forall a. HasCallStack => String -> Maybe a -> a
expectJust String
"lookupField"
        (Maybe FieldLabel -> FieldLabel) -> Maybe FieldLabel -> FieldLabel
forall a b. (a -> b) -> a -> b
$ FastStringEnv FieldLabel -> FastString -> Maybe FieldLabel
forall a. FastStringEnv a -> FastString -> Maybe a
lookupFsEnv FastStringEnv FieldLabel
fl_env FastString
lbl

{-
************************************************************************
*                                                                      *
        Fixities and precedence parsing
*                                                                      *
************************************************************************

@mkOpAppRn@ deals with operator fixities.  The argument expressions
are assumed to be already correctly arranged.  It needs the fixities
recorded in the OpApp nodes, because fixity info applies to the things
the programmer actually wrote, so you can't find it out from the Name.

Furthermore, the second argument is guaranteed not to be another
operator application.  Why? Because the parser parses all
operator applications left-associatively, EXCEPT negation, which
we need to handle specially.
Infix types are read in a *right-associative* way, so that
        a `op` b `op` c
is always read in as
        a `op` (b `op` c)

mkHsOpTyRn rearranges where necessary.  The two arguments
have already been renamed and rearranged.

In the past, mkHsOpTyRn used to handle (->), but this was unnecessary. In the
syntax tree produced by the parser, the arrow already has the least possible
precedence and does not require rearrangement.
-}

---------------
-- Building (ty1 `op1` (ty2a `op2` ty2b))
mkHsOpTyRn :: PromotionFlag
           -> LocatedN Name -> Fixity -> LHsType GhcRn -> LHsType GhcRn
           -> RnM (HsType GhcRn)

mkHsOpTyRn :: PromotionFlag
-> GenLocated SrcSpanAnnN Name
-> Fixity
-> LHsType GhcRn
-> LHsType GhcRn
-> RnM (HsType GhcRn)
mkHsOpTyRn PromotionFlag
prom1 GenLocated SrcSpanAnnN Name
op1 Fixity
fix1 LHsType GhcRn
ty1 (L SrcSpanAnnA
loc2 (HsOpTy XOpTy GhcRn
_ PromotionFlag
prom2 LHsType GhcRn
ty2a LIdP GhcRn
op2 LHsType GhcRn
ty2b))
  = do  { Fixity
fix2 <- GenLocated SrcSpanAnnN Name -> RnM Fixity
lookupTyFixityRn LIdP GhcRn
GenLocated SrcSpanAnnN Name
op2
        ; PromotionFlag
-> GenLocated SrcSpanAnnN Name
-> Fixity
-> LHsType GhcRn
-> PromotionFlag
-> GenLocated SrcSpanAnnN Name
-> Fixity
-> LHsType GhcRn
-> LHsType GhcRn
-> SrcSpanAnnA
-> RnM (HsType GhcRn)
mk_hs_op_ty PromotionFlag
prom1 GenLocated SrcSpanAnnN Name
op1 Fixity
fix1 LHsType GhcRn
ty1 PromotionFlag
prom2 LIdP GhcRn
GenLocated SrcSpanAnnN Name
op2 Fixity
fix2 LHsType GhcRn
ty2a LHsType GhcRn
ty2b SrcSpanAnnA
loc2 }

mkHsOpTyRn PromotionFlag
prom1 GenLocated SrcSpanAnnN Name
op1 Fixity
_ LHsType GhcRn
ty1 LHsType GhcRn
ty2              -- Default case, no rearrangement
  = HsType GhcRn -> RnM (HsType GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XOpTy GhcRn
-> PromotionFlag
-> LHsType GhcRn
-> LIdP GhcRn
-> LHsType GhcRn
-> HsType GhcRn
forall pass.
XOpTy pass
-> PromotionFlag
-> LHsType pass
-> LIdP pass
-> LHsType pass
-> HsType pass
HsOpTy XOpTy GhcRn
EpAnn [AddEpAnn]
forall a. EpAnn a
noAnn PromotionFlag
prom1 LHsType GhcRn
ty1 LIdP GhcRn
GenLocated SrcSpanAnnN Name
op1 LHsType GhcRn
ty2)

---------------
mk_hs_op_ty :: PromotionFlag -> LocatedN Name -> Fixity -> LHsType GhcRn
            -> PromotionFlag -> LocatedN Name -> Fixity -> LHsType GhcRn
            -> LHsType GhcRn -> SrcSpanAnnA
            -> RnM (HsType GhcRn)
mk_hs_op_ty :: PromotionFlag
-> GenLocated SrcSpanAnnN Name
-> Fixity
-> LHsType GhcRn
-> PromotionFlag
-> GenLocated SrcSpanAnnN Name
-> Fixity
-> LHsType GhcRn
-> LHsType GhcRn
-> SrcSpanAnnA
-> RnM (HsType GhcRn)
mk_hs_op_ty PromotionFlag
prom1 GenLocated SrcSpanAnnN Name
op1 Fixity
fix1 LHsType GhcRn
ty1 PromotionFlag
prom2 GenLocated SrcSpanAnnN Name
op2 Fixity
fix2 LHsType GhcRn
ty2a LHsType GhcRn
ty2b SrcSpanAnnA
loc2
  | Bool
nofix_error     = do { (OpName, Fixity) -> (OpName, Fixity) -> TcRn ()
precParseErr (Name -> OpName
NormalOp (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnN Name
op1),Fixity
fix1)
                                        (Name -> OpName
NormalOp (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnN Name
op2),Fixity
fix2)
                         ; HsType GhcRn -> RnM (HsType GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty1 GenLocated SrcSpanAnnA (HsType GhcRn)
-> GenLocated SrcSpanAnnA (HsType GhcRn) -> HsType GhcRn
`op1ty` (SrcSpanAnnA
-> HsType GhcRn -> GenLocated SrcSpanAnnA (HsType GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc2 (LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty2a GenLocated SrcSpanAnnA (HsType GhcRn)
-> GenLocated SrcSpanAnnA (HsType GhcRn) -> HsType GhcRn
`op2ty` LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty2b))) }
  | Bool
associate_right = HsType GhcRn -> RnM (HsType GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty1 GenLocated SrcSpanAnnA (HsType GhcRn)
-> GenLocated SrcSpanAnnA (HsType GhcRn) -> HsType GhcRn
`op1ty` (SrcSpanAnnA
-> HsType GhcRn -> GenLocated SrcSpanAnnA (HsType GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc2 (LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty2a GenLocated SrcSpanAnnA (HsType GhcRn)
-> GenLocated SrcSpanAnnA (HsType GhcRn) -> HsType GhcRn
`op2ty` LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty2b)))
  | Bool
otherwise       = do { -- Rearrange to ((ty1 `op1` ty2a) `op2` ty2b)
                           HsType GhcRn
new_ty <- PromotionFlag
-> GenLocated SrcSpanAnnN Name
-> Fixity
-> LHsType GhcRn
-> LHsType GhcRn
-> RnM (HsType GhcRn)
mkHsOpTyRn PromotionFlag
prom1 GenLocated SrcSpanAnnN Name
op1 Fixity
fix1 LHsType GhcRn
ty1 LHsType GhcRn
ty2a
                         ; HsType GhcRn -> RnM (HsType GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (HsType GhcRn -> GenLocated SrcSpanAnnA (HsType GhcRn)
forall a an. a -> LocatedAn an a
noLocA HsType GhcRn
new_ty GenLocated SrcSpanAnnA (HsType GhcRn)
-> GenLocated SrcSpanAnnA (HsType GhcRn) -> HsType GhcRn
`op2ty` LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
ty2b) }
  where
    GenLocated SrcSpanAnnA (HsType GhcRn)
lhs op1ty :: GenLocated SrcSpanAnnA (HsType GhcRn)
-> GenLocated SrcSpanAnnA (HsType GhcRn) -> HsType GhcRn
`op1ty` GenLocated SrcSpanAnnA (HsType GhcRn)
rhs = XOpTy GhcRn
-> PromotionFlag
-> LHsType GhcRn
-> LIdP GhcRn
-> LHsType GhcRn
-> HsType GhcRn
forall pass.
XOpTy pass
-> PromotionFlag
-> LHsType pass
-> LIdP pass
-> LHsType pass
-> HsType pass
HsOpTy XOpTy GhcRn
EpAnn [AddEpAnn]
forall a. EpAnn a
noAnn PromotionFlag
prom1 LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
lhs LIdP GhcRn
GenLocated SrcSpanAnnN Name
op1 LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
rhs
    GenLocated SrcSpanAnnA (HsType GhcRn)
lhs op2ty :: GenLocated SrcSpanAnnA (HsType GhcRn)
-> GenLocated SrcSpanAnnA (HsType GhcRn) -> HsType GhcRn
`op2ty` GenLocated SrcSpanAnnA (HsType GhcRn)
rhs = XOpTy GhcRn
-> PromotionFlag
-> LHsType GhcRn
-> LIdP GhcRn
-> LHsType GhcRn
-> HsType GhcRn
forall pass.
XOpTy pass
-> PromotionFlag
-> LHsType pass
-> LIdP pass
-> LHsType pass
-> HsType pass
HsOpTy XOpTy GhcRn
EpAnn [AddEpAnn]
forall a. EpAnn a
noAnn PromotionFlag
prom2 LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
lhs LIdP GhcRn
GenLocated SrcSpanAnnN Name
op2 LHsType GhcRn
GenLocated SrcSpanAnnA (HsType GhcRn)
rhs
    (Bool
nofix_error, Bool
associate_right) = Fixity -> Fixity -> (Bool, Bool)
compareFixity Fixity
fix1 Fixity
fix2


---------------------------
mkOpAppRn :: NegationHandling
          -> LHsExpr GhcRn             -- Left operand; already rearranged
          -> LHsExpr GhcRn -> Fixity   -- Operator and fixity
          -> LHsExpr GhcRn             -- Right operand (not an OpApp, but might
                                       -- be a NegApp)
          -> RnM (HsExpr GhcRn)

-- (e1a `op1` e1b) `op2` e2
mkOpAppRn :: NegationHandling
-> LHsExpr GhcRn
-> LHsExpr GhcRn
-> Fixity
-> LHsExpr GhcRn
-> RnM (HsExpr GhcRn)
mkOpAppRn NegationHandling
negation_handling e1 :: LHsExpr GhcRn
e1@(L SrcSpanAnnA
_ (OpApp XOpApp GhcRn
fix1 LHsExpr GhcRn
e1a LHsExpr GhcRn
op1 LHsExpr GhcRn
e1b)) LHsExpr GhcRn
op2 Fixity
fix2 LHsExpr GhcRn
e2
  | Bool
nofix_error
  = do (OpName, Fixity) -> (OpName, Fixity) -> TcRn ()
precParseErr (LHsExpr GhcRn -> OpName
get_op LHsExpr GhcRn
op1,XOpApp GhcRn
Fixity
fix1) (LHsExpr GhcRn -> OpName
get_op LHsExpr GhcRn
op2,Fixity
fix2)
       HsExpr GhcRn -> RnM (HsExpr GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XOpApp GhcRn
-> LHsExpr GhcRn -> LHsExpr GhcRn -> LHsExpr GhcRn -> HsExpr GhcRn
forall p.
XOpApp p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
OpApp XOpApp GhcRn
Fixity
fix2 LHsExpr GhcRn
e1 LHsExpr GhcRn
op2 LHsExpr GhcRn
e2)

  | Bool
associate_right = do
    HsExpr GhcRn
new_e <- NegationHandling
-> LHsExpr GhcRn
-> LHsExpr GhcRn
-> Fixity
-> LHsExpr GhcRn
-> RnM (HsExpr GhcRn)
mkOpAppRn NegationHandling
negation_handling LHsExpr GhcRn
e1b LHsExpr GhcRn
op2 Fixity
fix2 LHsExpr GhcRn
e2
    HsExpr GhcRn -> RnM (HsExpr GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XOpApp GhcRn
-> LHsExpr GhcRn -> LHsExpr GhcRn -> LHsExpr GhcRn -> HsExpr GhcRn
forall p.
XOpApp p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
OpApp XOpApp GhcRn
fix1 LHsExpr GhcRn
e1a LHsExpr GhcRn
op1 (SrcSpanAnnA
-> HsExpr GhcRn -> GenLocated SrcSpanAnnA (HsExpr GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc' HsExpr GhcRn
new_e))
  where
    loc' :: SrcSpanAnnA
loc'= GenLocated SrcSpanAnnA (HsExpr GhcRn)
-> GenLocated SrcSpanAnnA (HsExpr GhcRn) -> SrcSpanAnnA
forall a e1 e2.
Semigroup a =>
GenLocated (SrcAnn a) e1 -> GenLocated (SrcAnn a) e2 -> SrcAnn a
combineLocsA LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
e1b LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
e2
    (Bool
nofix_error, Bool
associate_right) = Fixity -> Fixity -> (Bool, Bool)
compareFixity XOpApp GhcRn
Fixity
fix1 Fixity
fix2

---------------------------
--      (- neg_arg) `op` e2
mkOpAppRn NegationHandling
ReassociateNegation e1 :: LHsExpr GhcRn
e1@(L SrcSpanAnnA
_ (NegApp XNegApp GhcRn
_ LHsExpr GhcRn
neg_arg SyntaxExpr GhcRn
neg_name)) LHsExpr GhcRn
op2 Fixity
fix2 LHsExpr GhcRn
e2
  | Bool
nofix_error
  = do (OpName, Fixity) -> (OpName, Fixity) -> TcRn ()
precParseErr (OpName
NegateOp,Fixity
negateFixity) (LHsExpr GhcRn -> OpName
get_op LHsExpr GhcRn
op2,Fixity
fix2)
       HsExpr GhcRn -> RnM (HsExpr GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XOpApp GhcRn
-> LHsExpr GhcRn -> LHsExpr GhcRn -> LHsExpr GhcRn -> HsExpr GhcRn
forall p.
XOpApp p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
OpApp XOpApp GhcRn
Fixity
fix2 LHsExpr GhcRn
e1 LHsExpr GhcRn
op2 LHsExpr GhcRn
e2)

  | Bool
associate_right
  = do HsExpr GhcRn
new_e <- NegationHandling
-> LHsExpr GhcRn
-> LHsExpr GhcRn
-> Fixity
-> LHsExpr GhcRn
-> RnM (HsExpr GhcRn)
mkOpAppRn NegationHandling
ReassociateNegation LHsExpr GhcRn
neg_arg LHsExpr GhcRn
op2 Fixity
fix2 LHsExpr GhcRn
e2
       HsExpr GhcRn -> RnM (HsExpr GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XNegApp GhcRn -> LHsExpr GhcRn -> SyntaxExpr GhcRn -> HsExpr GhcRn
forall p. XNegApp p -> LHsExpr p -> SyntaxExpr p -> HsExpr p
NegApp XNegApp GhcRn
NoExtField
noExtField (SrcSpanAnnA
-> HsExpr GhcRn -> GenLocated SrcSpanAnnA (HsExpr GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc' HsExpr GhcRn
new_e) SyntaxExpr GhcRn
neg_name)
  where
    loc' :: SrcSpanAnnA
loc' = GenLocated SrcSpanAnnA (HsExpr GhcRn)
-> GenLocated SrcSpanAnnA (HsExpr GhcRn) -> SrcSpanAnnA
forall a e1 e2.
Semigroup a =>
GenLocated (SrcAnn a) e1 -> GenLocated (SrcAnn a) e2 -> SrcAnn a
combineLocsA LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
neg_arg LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
e2
    (Bool
nofix_error, Bool
associate_right) = Fixity -> Fixity -> (Bool, Bool)
compareFixity Fixity
negateFixity Fixity
fix2

---------------------------
--      e1 `op` - neg_arg
mkOpAppRn NegationHandling
ReassociateNegation LHsExpr GhcRn
e1 LHsExpr GhcRn
op1 Fixity
fix1 e2 :: LHsExpr GhcRn
e2@(L SrcSpanAnnA
_ (NegApp {})) -- NegApp can occur on the right
  | Bool -> Bool
not Bool
associate_right                        -- We *want* right association
  = do (OpName, Fixity) -> (OpName, Fixity) -> TcRn ()
precParseErr (LHsExpr GhcRn -> OpName
get_op LHsExpr GhcRn
op1, Fixity
fix1) (OpName
NegateOp, Fixity
negateFixity)
       HsExpr GhcRn -> RnM (HsExpr GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XOpApp GhcRn
-> LHsExpr GhcRn -> LHsExpr GhcRn -> LHsExpr GhcRn -> HsExpr GhcRn
forall p.
XOpApp p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
OpApp XOpApp GhcRn
Fixity
fix1 LHsExpr GhcRn
e1 LHsExpr GhcRn
op1 LHsExpr GhcRn
e2)
  where
    (Bool
_, Bool
associate_right) = Fixity -> Fixity -> (Bool, Bool)
compareFixity Fixity
fix1 Fixity
negateFixity

---------------------------
--      Default case
mkOpAppRn NegationHandling
_ LHsExpr GhcRn
e1 LHsExpr GhcRn
op Fixity
fix LHsExpr GhcRn
e2                  -- Default case, no rearrangement
  = Bool -> SDoc -> RnM (HsExpr GhcRn) -> RnM (HsExpr GhcRn)
forall a. HasCallStack => Bool -> SDoc -> a -> a
assertPpr (Fixity -> HsExpr GhcRn -> Bool
right_op_ok Fixity
fix (GenLocated SrcSpanAnnA (HsExpr GhcRn) -> HsExpr GhcRn
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
e2))
              (GenLocated SrcSpanAnnA (HsExpr GhcRn) -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
e1 SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"---" SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ GenLocated SrcSpanAnnA (HsExpr GhcRn) -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
op SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"---" SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ Fixity -> SDoc
forall a. Outputable a => a -> SDoc
ppr Fixity
fix SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"---" SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ GenLocated SrcSpanAnnA (HsExpr GhcRn) -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
e2) (RnM (HsExpr GhcRn) -> RnM (HsExpr GhcRn))
-> RnM (HsExpr GhcRn) -> RnM (HsExpr GhcRn)
forall a b. (a -> b) -> a -> b
$
    HsExpr GhcRn -> RnM (HsExpr GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XOpApp GhcRn
-> LHsExpr GhcRn -> LHsExpr GhcRn -> LHsExpr GhcRn -> HsExpr GhcRn
forall p.
XOpApp p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
OpApp XOpApp GhcRn
Fixity
fix LHsExpr GhcRn
e1 LHsExpr GhcRn
op LHsExpr GhcRn
e2)

data NegationHandling = ReassociateNegation | KeepNegationIntact

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

get_op :: LHsExpr GhcRn -> OpName
-- An unbound name could be either HsVar or HsUnboundVar
-- See GHC.Rename.Expr.rnUnboundVar
get_op :: LHsExpr GhcRn -> OpName
get_op (L SrcSpanAnnA
_ (HsVar XVar GhcRn
_ LIdP GhcRn
n))         = Name -> OpName
NormalOp (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc LIdP GhcRn
GenLocated SrcSpanAnnN Name
n)
get_op (L SrcSpanAnnA
_ (HsUnboundVar XUnboundVar GhcRn
_ RdrName
uv)) = RdrName -> OpName
UnboundOp RdrName
uv
get_op (L SrcSpanAnnA
_ (HsRecSel XRecSel GhcRn
_ FieldOcc GhcRn
fld))    = FieldOcc GhcRn -> OpName
RecFldOp FieldOcc GhcRn
fld
get_op LHsExpr GhcRn
other                     = String -> SDoc -> OpName
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"get_op" (GenLocated SrcSpanAnnA (HsExpr GhcRn) -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
other)

-- Parser left-associates everything, but
-- derived instances may have correctly-associated things to
-- in the right operand.  So we just check that the right operand is OK
right_op_ok :: Fixity -> HsExpr GhcRn -> Bool
right_op_ok :: Fixity -> HsExpr GhcRn -> Bool
right_op_ok Fixity
fix1 (OpApp XOpApp GhcRn
fix2 LHsExpr GhcRn
_ LHsExpr GhcRn
_ LHsExpr GhcRn
_)
  = Bool -> Bool
not Bool
error_please Bool -> Bool -> Bool
&& Bool
associate_right
  where
    (Bool
error_please, Bool
associate_right) = Fixity -> Fixity -> (Bool, Bool)
compareFixity Fixity
fix1 XOpApp GhcRn
Fixity
fix2
right_op_ok Fixity
_ HsExpr GhcRn
_
  = Bool
True

-- Parser initially makes negation bind more tightly than any other operator
-- And "deriving" code should respect this (use HsPar if not)
mkNegAppRn :: LHsExpr GhcRn -> SyntaxExpr GhcRn -> RnM (HsExpr GhcRn)
mkNegAppRn :: LHsExpr GhcRn -> SyntaxExpr GhcRn -> RnM (HsExpr GhcRn)
mkNegAppRn LHsExpr GhcRn
neg_arg SyntaxExpr GhcRn
neg_name
  = Bool -> RnM (HsExpr GhcRn) -> RnM (HsExpr GhcRn)
forall a. HasCallStack => Bool -> a -> a
assert (HsExpr GhcRn -> Bool
forall id. HsExpr id -> Bool
not_op_app (GenLocated SrcSpanAnnA (HsExpr GhcRn) -> HsExpr GhcRn
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
neg_arg)) (RnM (HsExpr GhcRn) -> RnM (HsExpr GhcRn))
-> RnM (HsExpr GhcRn) -> RnM (HsExpr GhcRn)
forall a b. (a -> b) -> a -> b
$
    HsExpr GhcRn -> RnM (HsExpr GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XNegApp GhcRn -> LHsExpr GhcRn -> SyntaxExpr GhcRn -> HsExpr GhcRn
forall p. XNegApp p -> LHsExpr p -> SyntaxExpr p -> HsExpr p
NegApp XNegApp GhcRn
NoExtField
noExtField LHsExpr GhcRn
neg_arg SyntaxExpr GhcRn
neg_name)

not_op_app :: HsExpr id -> Bool
not_op_app :: forall id. HsExpr id -> Bool
not_op_app (OpApp {}) = Bool
False
not_op_app HsExpr id
_          = Bool
True

---------------------------
mkOpFormRn :: LHsCmdTop GhcRn            -- Left operand; already rearranged
          -> LHsExpr GhcRn -> Fixity     -- Operator and fixity
          -> LHsCmdTop GhcRn             -- Right operand (not an infix)
          -> RnM (HsCmd GhcRn)

-- (e1a `op1` e1b) `op2` e2
mkOpFormRn :: LHsCmdTop GhcRn
-> LHsExpr GhcRn -> Fixity -> LHsCmdTop GhcRn -> RnM (HsCmd GhcRn)
mkOpFormRn e1 :: LHsCmdTop GhcRn
e1@(L SrcAnn NoEpAnns
loc
                    (HsCmdTop XCmdTop GhcRn
_
                     (L SrcSpanAnnA
_ (HsCmdArrForm XCmdArrForm GhcRn
x LHsExpr GhcRn
op1 LexicalFixity
f (Just Fixity
fix1)
                        [LHsCmdTop GhcRn
e1a,LHsCmdTop GhcRn
e1b]))))
        LHsExpr GhcRn
op2 Fixity
fix2 LHsCmdTop GhcRn
e2
  | Bool
nofix_error
  = do (OpName, Fixity) -> (OpName, Fixity) -> TcRn ()
precParseErr (LHsExpr GhcRn -> OpName
get_op LHsExpr GhcRn
op1,Fixity
fix1) (LHsExpr GhcRn -> OpName
get_op LHsExpr GhcRn
op2,Fixity
fix2)
       HsCmd GhcRn -> RnM (HsCmd GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCmdArrForm GhcRn
-> LHsExpr GhcRn
-> LexicalFixity
-> Maybe Fixity
-> [LHsCmdTop GhcRn]
-> HsCmd GhcRn
forall id.
XCmdArrForm id
-> LHsExpr id
-> LexicalFixity
-> Maybe Fixity
-> [LHsCmdTop id]
-> HsCmd id
HsCmdArrForm XCmdArrForm GhcRn
x LHsExpr GhcRn
op2 LexicalFixity
f (Fixity -> Maybe Fixity
forall a. a -> Maybe a
Just Fixity
fix2) [LHsCmdTop GhcRn
e1, LHsCmdTop GhcRn
e2])

  | Bool
associate_right
  = do HsCmd GhcRn
new_c <- LHsCmdTop GhcRn
-> LHsExpr GhcRn -> Fixity -> LHsCmdTop GhcRn -> RnM (HsCmd GhcRn)
mkOpFormRn LHsCmdTop GhcRn
e1a LHsExpr GhcRn
op2 Fixity
fix2 LHsCmdTop GhcRn
e2
       HsCmd GhcRn -> RnM (HsCmd GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCmdArrForm GhcRn
-> LHsExpr GhcRn
-> LexicalFixity
-> Maybe Fixity
-> [LHsCmdTop GhcRn]
-> HsCmd GhcRn
forall id.
XCmdArrForm id
-> LHsExpr id
-> LexicalFixity
-> Maybe Fixity
-> [LHsCmdTop id]
-> HsCmd id
HsCmdArrForm XCmdArrForm GhcRn
NoExtField
noExtField LHsExpr GhcRn
op1 LexicalFixity
f (Fixity -> Maybe Fixity
forall a. a -> Maybe a
Just Fixity
fix1)
               [LHsCmdTop GhcRn
e1b, SrcAnn NoEpAnns
-> HsCmdTop GhcRn -> GenLocated (SrcAnn NoEpAnns) (HsCmdTop GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
loc (XCmdTop GhcRn -> LHsCmd GhcRn -> HsCmdTop GhcRn
forall p. XCmdTop p -> LHsCmd p -> HsCmdTop p
HsCmdTop [] (SrcSpanAnnA -> HsCmd GhcRn -> GenLocated SrcSpanAnnA (HsCmd GhcRn)
forall l e. l -> e -> GenLocated l e
L (SrcAnn NoEpAnns -> SrcSpanAnnA
forall a ann. SrcSpanAnn' a -> SrcAnn ann
l2l SrcAnn NoEpAnns
loc) HsCmd GhcRn
new_c))])
        -- TODO: locs are wrong
  where
    (Bool
nofix_error, Bool
associate_right) = Fixity -> Fixity -> (Bool, Bool)
compareFixity Fixity
fix1 Fixity
fix2

--      Default case
mkOpFormRn LHsCmdTop GhcRn
arg1 LHsExpr GhcRn
op Fixity
fix LHsCmdTop GhcRn
arg2                     -- Default case, no rearrangement
  = HsCmd GhcRn -> RnM (HsCmd GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (XCmdArrForm GhcRn
-> LHsExpr GhcRn
-> LexicalFixity
-> Maybe Fixity
-> [LHsCmdTop GhcRn]
-> HsCmd GhcRn
forall id.
XCmdArrForm id
-> LHsExpr id
-> LexicalFixity
-> Maybe Fixity
-> [LHsCmdTop id]
-> HsCmd id
HsCmdArrForm XCmdArrForm GhcRn
NoExtField
noExtField LHsExpr GhcRn
op LexicalFixity
Infix (Fixity -> Maybe Fixity
forall a. a -> Maybe a
Just Fixity
fix) [LHsCmdTop GhcRn
arg1, LHsCmdTop GhcRn
arg2])


--------------------------------------
mkConOpPatRn :: LocatedN Name -> Fixity -> LPat GhcRn -> LPat GhcRn
             -> RnM (Pat GhcRn)

mkConOpPatRn :: GenLocated SrcSpanAnnN Name
-> Fixity -> LPat GhcRn -> LPat GhcRn -> RnM (Pat GhcRn)
mkConOpPatRn GenLocated SrcSpanAnnN Name
op2 Fixity
fix2 p1 :: LPat GhcRn
p1@(L SrcSpanAnnA
loc (ConPat XConPat GhcRn
NoExtField
NoExtField XRec GhcRn (ConLikeP GhcRn)
op1 (InfixCon LPat GhcRn
p1a LPat GhcRn
p1b))) LPat GhcRn
p2
  = do  { Fixity
fix1 <- Name -> RnM Fixity
lookupFixityRn (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc XRec GhcRn (ConLikeP GhcRn)
GenLocated SrcSpanAnnN Name
op1)
        ; let (Bool
nofix_error, Bool
associate_right) = Fixity -> Fixity -> (Bool, Bool)
compareFixity Fixity
fix1 Fixity
fix2

        ; if Bool
nofix_error then do
                { (OpName, Fixity) -> (OpName, Fixity) -> TcRn ()
precParseErr (Name -> OpName
NormalOp (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc XRec GhcRn (ConLikeP GhcRn)
GenLocated SrcSpanAnnN Name
op1),Fixity
fix1)
                               (Name -> OpName
NormalOp (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnN Name
op2),Fixity
fix2)
                ; Pat GhcRn -> RnM (Pat GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Pat GhcRn -> RnM (Pat GhcRn)) -> Pat GhcRn -> RnM (Pat GhcRn)
forall a b. (a -> b) -> a -> b
$ ConPat
                    { pat_con_ext :: XConPat GhcRn
pat_con_ext = XConPat GhcRn
NoExtField
noExtField
                    , pat_con :: XRec GhcRn (ConLikeP GhcRn)
pat_con = XRec GhcRn (ConLikeP GhcRn)
GenLocated SrcSpanAnnN Name
op2
                    , pat_args :: HsConDetails
  (HsConPatTyArg (NoGhcTc GhcRn))
  (LPat GhcRn)
  (HsRecFields GhcRn (LPat GhcRn))
pat_args = GenLocated SrcSpanAnnA (Pat GhcRn)
-> GenLocated SrcSpanAnnA (Pat GhcRn)
-> HsConDetails
     (HsConPatTyArg GhcRn)
     (GenLocated SrcSpanAnnA (Pat GhcRn))
     (HsRecFields GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn)))
forall tyarg arg rec. arg -> arg -> HsConDetails tyarg arg rec
InfixCon LPat GhcRn
GenLocated SrcSpanAnnA (Pat GhcRn)
p1 LPat GhcRn
GenLocated SrcSpanAnnA (Pat GhcRn)
p2
                    }
                }

          else if Bool
associate_right then do
                { Pat GhcRn
new_p <- GenLocated SrcSpanAnnN Name
-> Fixity -> LPat GhcRn -> LPat GhcRn -> RnM (Pat GhcRn)
mkConOpPatRn GenLocated SrcSpanAnnN Name
op2 Fixity
fix2 LPat GhcRn
p1b LPat GhcRn
p2
                ; Pat GhcRn -> RnM (Pat GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Pat GhcRn -> RnM (Pat GhcRn)) -> Pat GhcRn -> RnM (Pat GhcRn)
forall a b. (a -> b) -> a -> b
$ ConPat
                    { pat_con_ext :: XConPat GhcRn
pat_con_ext = XConPat GhcRn
NoExtField
noExtField
                    , pat_con :: XRec GhcRn (ConLikeP GhcRn)
pat_con = XRec GhcRn (ConLikeP GhcRn)
op1
                    , pat_args :: HsConDetails
  (HsConPatTyArg (NoGhcTc GhcRn))
  (LPat GhcRn)
  (HsRecFields GhcRn (LPat GhcRn))
pat_args = GenLocated SrcSpanAnnA (Pat GhcRn)
-> GenLocated SrcSpanAnnA (Pat GhcRn)
-> HsConDetails
     (HsConPatTyArg GhcRn)
     (GenLocated SrcSpanAnnA (Pat GhcRn))
     (HsRecFields GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn)))
forall tyarg arg rec. arg -> arg -> HsConDetails tyarg arg rec
InfixCon LPat GhcRn
GenLocated SrcSpanAnnA (Pat GhcRn)
p1a (SrcSpanAnnA -> Pat GhcRn -> GenLocated SrcSpanAnnA (Pat GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc Pat GhcRn
new_p)
                    }
                }
                -- XXX loc right?
          else Pat GhcRn -> RnM (Pat GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Pat GhcRn -> RnM (Pat GhcRn)) -> Pat GhcRn -> RnM (Pat GhcRn)
forall a b. (a -> b) -> a -> b
$ ConPat
                 { pat_con_ext :: XConPat GhcRn
pat_con_ext = XConPat GhcRn
NoExtField
noExtField
                 , pat_con :: XRec GhcRn (ConLikeP GhcRn)
pat_con = XRec GhcRn (ConLikeP GhcRn)
GenLocated SrcSpanAnnN Name
op2
                 , pat_args :: HsConDetails
  (HsConPatTyArg (NoGhcTc GhcRn))
  (LPat GhcRn)
  (HsRecFields GhcRn (LPat GhcRn))
pat_args = GenLocated SrcSpanAnnA (Pat GhcRn)
-> GenLocated SrcSpanAnnA (Pat GhcRn)
-> HsConDetails
     (HsConPatTyArg GhcRn)
     (GenLocated SrcSpanAnnA (Pat GhcRn))
     (HsRecFields GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn)))
forall tyarg arg rec. arg -> arg -> HsConDetails tyarg arg rec
InfixCon LPat GhcRn
GenLocated SrcSpanAnnA (Pat GhcRn)
p1 LPat GhcRn
GenLocated SrcSpanAnnA (Pat GhcRn)
p2
                 }
        }

mkConOpPatRn GenLocated SrcSpanAnnN Name
op Fixity
_ LPat GhcRn
p1 LPat GhcRn
p2                         -- Default case, no rearrangement
  = Bool -> RnM (Pat GhcRn) -> RnM (Pat GhcRn)
forall a. HasCallStack => Bool -> a -> a
assert (Pat GhcRn -> Bool
not_op_pat (GenLocated SrcSpanAnnA (Pat GhcRn) -> Pat GhcRn
forall l e. GenLocated l e -> e
unLoc LPat GhcRn
GenLocated SrcSpanAnnA (Pat GhcRn)
p2)) (RnM (Pat GhcRn) -> RnM (Pat GhcRn))
-> RnM (Pat GhcRn) -> RnM (Pat GhcRn)
forall a b. (a -> b) -> a -> b
$
    Pat GhcRn -> RnM (Pat GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Pat GhcRn -> RnM (Pat GhcRn)) -> Pat GhcRn -> RnM (Pat GhcRn)
forall a b. (a -> b) -> a -> b
$ ConPat
      { pat_con_ext :: XConPat GhcRn
pat_con_ext = XConPat GhcRn
NoExtField
noExtField
      , pat_con :: XRec GhcRn (ConLikeP GhcRn)
pat_con = XRec GhcRn (ConLikeP GhcRn)
GenLocated SrcSpanAnnN Name
op
      , pat_args :: HsConDetails
  (HsConPatTyArg (NoGhcTc GhcRn))
  (LPat GhcRn)
  (HsRecFields GhcRn (LPat GhcRn))
pat_args = GenLocated SrcSpanAnnA (Pat GhcRn)
-> GenLocated SrcSpanAnnA (Pat GhcRn)
-> HsConDetails
     (HsConPatTyArg GhcRn)
     (GenLocated SrcSpanAnnA (Pat GhcRn))
     (HsRecFields GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn)))
forall tyarg arg rec. arg -> arg -> HsConDetails tyarg arg rec
InfixCon LPat GhcRn
GenLocated SrcSpanAnnA (Pat GhcRn)
p1 LPat GhcRn
GenLocated SrcSpanAnnA (Pat GhcRn)
p2
      }

not_op_pat :: Pat GhcRn -> Bool
not_op_pat :: Pat GhcRn -> Bool
not_op_pat (ConPat XConPat GhcRn
NoExtField
NoExtField XRec GhcRn (ConLikeP GhcRn)
_ (InfixCon LPat GhcRn
_ LPat GhcRn
_)) = Bool
False
not_op_pat Pat GhcRn
_                                    = Bool
True

--------------------------------------
checkPrecMatch :: Name -> MatchGroup GhcRn body -> RnM ()
  -- Check precedence of a function binding written infix
  --   eg  a `op` b `C` c = ...
  -- See comments with rnExpr (OpApp ...) about "deriving"

checkPrecMatch :: forall body. Name -> MatchGroup GhcRn body -> TcRn ()
checkPrecMatch Name
op (MG { mg_alts :: forall p body. MatchGroup p body -> XRec p [LMatch p body]
mg_alts = (L Anno [GenLocated (Anno (Match GhcRn body)) (Match GhcRn body)]
_ [GenLocated (Anno (Match GhcRn body)) (Match GhcRn body)]
ms) })
  = (GenLocated (Anno (Match GhcRn body)) (Match GhcRn body)
 -> TcRn ())
-> [GenLocated (Anno (Match GhcRn body)) (Match GhcRn body)]
-> TcRn ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ GenLocated (Anno (Match GhcRn body)) (Match GhcRn body) -> TcRn ()
check [GenLocated (Anno (Match GhcRn body)) (Match GhcRn body)]
ms
  where
    check :: GenLocated (Anno (Match GhcRn body)) (Match GhcRn body) -> TcRn ()
check (L Anno (Match GhcRn body)
_ (Match { m_pats :: forall p body. Match p body -> [LPat p]
m_pats = (L SrcSpanAnnA
l1 Pat GhcRn
p1)
                               : (L SrcSpanAnnA
l2 Pat GhcRn
p2)
                               : [LPat GhcRn]
_ }))
      = SrcSpan -> TcRn () -> TcRn ()
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan (SrcSpanAnnA -> SrcSpan
forall a. SrcSpanAnn' a -> SrcSpan
locA (SrcSpanAnnA -> SrcSpan) -> SrcSpanAnnA -> SrcSpan
forall a b. (a -> b) -> a -> b
$ SrcSpanAnnA -> SrcSpanAnnA -> SrcSpanAnnA
forall a. Semigroup a => SrcAnn a -> SrcAnn a -> SrcAnn a
combineSrcSpansA SrcSpanAnnA
l1 SrcSpanAnnA
l2) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
        do Name -> Pat GhcRn -> Bool -> TcRn ()
checkPrec Name
op Pat GhcRn
p1 Bool
False
           Name -> Pat GhcRn -> Bool -> TcRn ()
checkPrec Name
op Pat GhcRn
p2 Bool
True

    check GenLocated (Anno (Match GhcRn body)) (Match GhcRn body)
_ = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        -- This can happen.  Consider
        --      a `op` True = ...
        --      op          = ...
        -- The infix flag comes from the first binding of the group
        -- but the second eqn has no args (an error, but not discovered
        -- until the type checker).  So we don't want to crash on the
        -- second eqn.

checkPrec :: Name -> Pat GhcRn -> Bool -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkPrec :: Name -> Pat GhcRn -> Bool -> TcRn ()
checkPrec Name
op (ConPat XConPat GhcRn
NoExtField
NoExtField XRec GhcRn (ConLikeP GhcRn)
op1 (InfixCon LPat GhcRn
_ LPat GhcRn
_)) Bool
right = do
    op_fix :: Fixity
op_fix@(Fixity SourceText
_ Int
op_prec  FixityDirection
op_dir) <- Name -> RnM Fixity
lookupFixityRn Name
op
    op1_fix :: Fixity
op1_fix@(Fixity SourceText
_ Int
op1_prec FixityDirection
op1_dir) <- Name -> RnM Fixity
lookupFixityRn (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc XRec GhcRn (ConLikeP GhcRn)
GenLocated SrcSpanAnnN Name
op1)
    let
        inf_ok :: Bool
inf_ok = Int
op1_prec Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
op_prec Bool -> Bool -> Bool
||
                 (Int
op1_prec Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
op_prec Bool -> Bool -> Bool
&&
                  (FixityDirection
op1_dir FixityDirection -> FixityDirection -> Bool
forall a. Eq a => a -> a -> Bool
== FixityDirection
InfixR Bool -> Bool -> Bool
&& FixityDirection
op_dir FixityDirection -> FixityDirection -> Bool
forall a. Eq a => a -> a -> Bool
== FixityDirection
InfixR Bool -> Bool -> Bool
&& Bool
right Bool -> Bool -> Bool
||
                   FixityDirection
op1_dir FixityDirection -> FixityDirection -> Bool
forall a. Eq a => a -> a -> Bool
== FixityDirection
InfixL Bool -> Bool -> Bool
&& FixityDirection
op_dir FixityDirection -> FixityDirection -> Bool
forall a. Eq a => a -> a -> Bool
== FixityDirection
InfixL Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
right))

        info :: (OpName, Fixity)
info  = (Name -> OpName
NormalOp Name
op,          Fixity
op_fix)
        info1 :: (OpName, Fixity)
info1 = (Name -> OpName
NormalOp (GenLocated SrcSpanAnnN Name -> Name
forall l e. GenLocated l e -> e
unLoc XRec GhcRn (ConLikeP GhcRn)
GenLocated SrcSpanAnnN Name
op1), Fixity
op1_fix)
        ((OpName, Fixity)
infol, (OpName, Fixity)
infor) = if Bool
right then ((OpName, Fixity)
info, (OpName, Fixity)
info1) else ((OpName, Fixity)
info1, (OpName, Fixity)
info)
    Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
inf_ok ((OpName, Fixity) -> (OpName, Fixity) -> TcRn ()
precParseErr (OpName, Fixity)
infol (OpName, Fixity)
infor)

checkPrec Name
_ Pat GhcRn
_ Bool
_
  = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- Check precedence of (arg op) or (op arg) respectively
-- If arg is itself an operator application, then either
--   (a) its precedence must be higher than that of op
--   (b) its precedency & associativity must be the same as that of op
checkSectionPrec :: FixityDirection -> HsExpr GhcPs
        -> LHsExpr GhcRn -> LHsExpr GhcRn -> RnM ()
checkSectionPrec :: FixityDirection
-> HsExpr GhcPs -> LHsExpr GhcRn -> LHsExpr GhcRn -> TcRn ()
checkSectionPrec FixityDirection
direction HsExpr GhcPs
section LHsExpr GhcRn
op LHsExpr GhcRn
arg
  = case GenLocated SrcSpanAnnA (HsExpr GhcRn) -> HsExpr GhcRn
forall l e. GenLocated l e -> e
unLoc LHsExpr GhcRn
GenLocated SrcSpanAnnA (HsExpr GhcRn)
arg of
        OpApp XOpApp GhcRn
fix LHsExpr GhcRn
_ LHsExpr GhcRn
op' LHsExpr GhcRn
_ -> OpName -> Fixity -> TcRn ()
go_for_it (LHsExpr GhcRn -> OpName
get_op LHsExpr GhcRn
op') XOpApp GhcRn
Fixity
fix
        NegApp XNegApp GhcRn
_ LHsExpr GhcRn
_ SyntaxExpr GhcRn
_      -> OpName -> Fixity -> TcRn ()
go_for_it OpName
NegateOp     Fixity
negateFixity
        HsExpr GhcRn
_                 -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
  where
    op_name :: OpName
op_name = LHsExpr GhcRn -> OpName
get_op LHsExpr GhcRn
op
    go_for_it :: OpName -> Fixity -> TcRn ()
go_for_it OpName
arg_op arg_fix :: Fixity
arg_fix@(Fixity SourceText
_ Int
arg_prec FixityDirection
assoc) = do
          op_fix :: Fixity
op_fix@(Fixity SourceText
_ Int
op_prec FixityDirection
_) <- OpName -> RnM Fixity
lookupFixityOp OpName
op_name
          Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int
op_prec Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
arg_prec
                  Bool -> Bool -> Bool
|| (Int
op_prec Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
arg_prec Bool -> Bool -> Bool
&& FixityDirection
direction FixityDirection -> FixityDirection -> Bool
forall a. Eq a => a -> a -> Bool
== FixityDirection
assoc))
                 ((OpName, Fixity) -> (OpName, Fixity) -> HsExpr GhcPs -> TcRn ()
sectionPrecErr (LHsExpr GhcRn -> OpName
get_op LHsExpr GhcRn
op, Fixity
op_fix)
                                 (OpName
arg_op, Fixity
arg_fix) HsExpr GhcPs
section)

-- | Look up the fixity for an operator name.
lookupFixityOp :: OpName -> RnM Fixity
lookupFixityOp :: OpName -> RnM Fixity
lookupFixityOp (NormalOp Name
n)  = Name -> RnM Fixity
lookupFixityRn Name
n
lookupFixityOp OpName
NegateOp      = Name -> RnM Fixity
lookupFixityRn Name
negateName
lookupFixityOp (UnboundOp RdrName
u) = Name -> RnM Fixity
lookupFixityRn (OccName -> Name
mkUnboundName (RdrName -> OccName
forall name. HasOccName name => name -> OccName
occName RdrName
u))
lookupFixityOp (RecFldOp FieldOcc GhcRn
f)  = FieldOcc GhcRn -> RnM Fixity
lookupFieldFixityRn FieldOcc GhcRn
f


-- Precedence-related error messages

precParseErr :: (OpName,Fixity) -> (OpName,Fixity) -> RnM ()
precParseErr :: (OpName, Fixity) -> (OpName, Fixity) -> TcRn ()
precParseErr op1 :: (OpName, Fixity)
op1@(OpName
n1,Fixity
_) op2 :: (OpName, Fixity)
op2@(OpName
n2,Fixity
_)
  | OpName -> Bool
is_unbound OpName
n1 Bool -> Bool -> Bool
|| OpName -> Bool
is_unbound OpName
n2
  = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()     -- Avoid error cascade
  | Bool
otherwise
  = TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ (OpName, Fixity) -> (OpName, Fixity) -> TcRnMessage
TcRnPrecedenceParsingError (OpName, Fixity)
op1 (OpName, Fixity)
op2

sectionPrecErr :: (OpName,Fixity) -> (OpName,Fixity) -> HsExpr GhcPs -> RnM ()
sectionPrecErr :: (OpName, Fixity) -> (OpName, Fixity) -> HsExpr GhcPs -> TcRn ()
sectionPrecErr op :: (OpName, Fixity)
op@(OpName
n1,Fixity
_) arg_op :: (OpName, Fixity)
arg_op@(OpName
n2,Fixity
_) HsExpr GhcPs
section
  | OpName -> Bool
is_unbound OpName
n1 Bool -> Bool -> Bool
|| OpName -> Bool
is_unbound OpName
n2
  = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()     -- Avoid error cascade
  | Bool
otherwise
  = TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$ (OpName, Fixity) -> (OpName, Fixity) -> HsExpr GhcPs -> TcRnMessage
TcRnSectionPrecedenceError (OpName, Fixity)
op (OpName, Fixity)
arg_op HsExpr GhcPs
section

is_unbound :: OpName -> Bool
is_unbound :: OpName -> Bool
is_unbound (NormalOp Name
n) = Name -> Bool
isUnboundName Name
n
is_unbound UnboundOp{}  = Bool
True
is_unbound OpName
_            = Bool
False


{- *****************************************************
*                                                      *
                 Errors
*                                                      *
***************************************************** -}

unexpectedPatSigTypeErr :: HsPatSigType GhcPs -> TcRnMessage
unexpectedPatSigTypeErr :: HsPatSigType GhcPs -> TcRnMessage
unexpectedPatSigTypeErr HsPatSigType GhcPs
ty
  = HsPatSigType GhcPs -> TcRnMessage
TcRnUnexpectedPatSigType HsPatSigType GhcPs
ty

badKindSigErr :: HsDocContext -> LHsType GhcPs -> TcM ()
badKindSigErr :: HsDocContext -> LHsType GhcPs -> TcRn ()
badKindSigErr HsDocContext
doc (L SrcSpanAnnA
loc HsType GhcPs
ty)
  = SrcSpanAnnA -> TcRn () -> TcRn ()
forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
addErr (TcRnMessage -> TcRn ()) -> TcRnMessage -> TcRn ()
forall a b. (a -> b) -> a -> b
$
    HsDocContext -> TcRnMessage -> TcRnMessage
TcRnWithHsDocContext HsDocContext
doc (TcRnMessage -> TcRnMessage) -> TcRnMessage -> TcRnMessage
forall a b. (a -> b) -> a -> b
$
    Either (HsType GhcPs) (Name, HsType GhcRn) -> TcRnMessage
TcRnKindSignaturesDisabled (HsType GhcPs -> Either (HsType GhcPs) (Name, HsType GhcRn)
forall a b. a -> Either a b
Left HsType GhcPs
ty)

dataKindsErr :: RnTyKiEnv -> HsType GhcPs -> TcRnMessage
dataKindsErr :: RnTyKiEnv -> HsType GhcPs -> TcRnMessage
dataKindsErr RnTyKiEnv
env HsType GhcPs
thing
  = TypeOrKind -> HsType GhcPs -> TcRnMessage
TcRnDataKindsError TypeOrKind
type_or_Kind HsType GhcPs
thing
  where
    type_or_Kind :: TypeOrKind
type_or_Kind | RnTyKiEnv -> Bool
isRnKindLevel RnTyKiEnv
env = TypeOrKind
KindLevel
                 | Bool
otherwise         = TypeOrKind
TypeLevel

warnUnusedForAll :: OutputableBndrFlag flag 'Renamed
                 => HsDocContext -> LHsTyVarBndr flag GhcRn -> FreeVars -> TcM ()
warnUnusedForAll :: forall flag.
OutputableBndrFlag flag 'Renamed =>
HsDocContext -> LHsTyVarBndr flag GhcRn -> FreeVars -> TcRn ()
warnUnusedForAll HsDocContext
doc (L SrcSpanAnnA
loc HsTyVarBndr flag GhcRn
tv) FreeVars
used_names
  = Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (HsTyVarBndr flag GhcRn -> IdP GhcRn
forall flag (p :: Pass).
HsTyVarBndr flag (GhcPass p) -> IdP (GhcPass p)
hsTyVarName HsTyVarBndr flag GhcRn
tv Name -> FreeVars -> Bool
`elemNameSet` FreeVars
used_names) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ do
      let msg :: TcRnMessage
msg = HsDocContext -> HsTyVarBndrExistentialFlag -> TcRnMessage
TcRnUnusedQuantifiedTypeVar HsDocContext
doc (HsTyVarBndr flag GhcRn -> HsTyVarBndrExistentialFlag
forall flag.
OutputableBndrFlag flag 'Renamed =>
HsTyVarBndr flag GhcRn -> HsTyVarBndrExistentialFlag
HsTyVarBndrExistentialFlag HsTyVarBndr flag GhcRn
tv)
      SrcSpan -> TcRnMessage -> TcRn ()
addDiagnosticAt (SrcSpanAnnA -> SrcSpan
forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
loc) TcRnMessage
msg

warnCapturedTerm :: LocatedN RdrName -> Either [GlobalRdrElt] Name -> TcM ()
warnCapturedTerm :: GenLocated SrcSpanAnnN RdrName
-> Either [GlobalRdrEltX GREInfo] Name -> TcRn ()
warnCapturedTerm (L SrcSpanAnnN
loc RdrName
tv) Either [GlobalRdrEltX GREInfo] Name
shadowed_term_names
  = let msg :: TcRnMessage
msg = RdrName -> Either [GlobalRdrEltX GREInfo] Name -> TcRnMessage
TcRnCapturedTermName RdrName
tv Either [GlobalRdrEltX GREInfo] Name
shadowed_term_names
    in SrcSpan -> TcRnMessage -> TcRn ()
addDiagnosticAt (SrcSpanAnnN -> SrcSpan
forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnN
loc) TcRnMessage
msg

{-
************************************************************************
*                                                                      *
      Finding the free type variables of a (HsType RdrName)
*                                                                      *
************************************************************************


Note [Kind and type-variable binders]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In a type signature we may implicitly bind type/kind variables. For example:
  *   f :: a -> a
      f = ...
    Here we need to find the free type variables of (a -> a),
    so that we know what to quantify

  *   class C (a :: k) where ...
    This binds 'k' in ..., as well as 'a'

  *   f (x :: a -> [a]) = ....
    Here we bind 'a' in ....

  *   f (x :: T a -> T (b :: k)) = ...
    Here we bind both 'a' and the kind variable 'k'

  *   type instance F (T (a :: Maybe k)) = ...a...k...
    Here we want to constrain the kind of 'a', and bind 'k'.

To do that, we need to walk over a type and find its free type/kind variables.
We preserve the left-to-right order of each variable occurrence.
See Note [Ordering of implicit variables].

It is common for lists of free type variables to contain duplicates. For
example, in `f :: a -> a`, the free type variable list is [a, a]. When these
implicitly bound variables are brought into scope (with rnImplicitTvOccs),
duplicates are removed with nubL.

Note [Ordering of implicit variables]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Since the advent of -XTypeApplications, GHC makes promises about the ordering
of implicit variable quantification. Specifically, we offer that implicitly
quantified variables (such as those in const :: a -> b -> a, without a `forall`)
will occur in left-to-right order of first occurrence. Here are a few examples:

  const :: a -> b -> a       -- forall a b. ...
  f :: Eq a => b -> a -> a   -- forall a b. ...  contexts are included

  type a <-< b = b -> a
  g :: a <-< b               -- forall a b. ...  type synonyms matter

  class Functor f where
    fmap :: (a -> b) -> f a -> f b   -- forall f a b. ...
    -- The f is quantified by the class, so only a and b are considered in fmap

This simple story is complicated by the possibility of dependency: all variables
must come after any variables mentioned in their kinds.

  typeRep :: Typeable a => TypeRep (a :: k)   -- forall k a. ...

The k comes first because a depends on k, even though the k appears later than
the a in the code. Thus, GHC does ScopedSort on the variables.
See Note [ScopedSort] in GHC.Core.Type.

Implicitly bound variables are collected by any function which returns a
FreeKiTyVars, which notably includes the `extract-` family of functions
(extractHsTysRdrTyVars, extractHsTyVarBndrsKVs, etc.).
These functions thus promise to keep left-to-right ordering.

Note [Implicit quantification in type synonyms]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We typically bind type/kind variables implicitly when they are in a kind
annotation on the LHS, for example:

  data Proxy (a :: k) = Proxy
  type KindOf (a :: k) = k

Here 'k' is in the kind annotation of a type variable binding, KindedTyVar, and
we want to implicitly quantify over it.  This is easy: just extract all free
variables from the kind signature. That's what we do in extract_hs_tv_bndrs_kvs

By contrast, on the RHS we can't simply collect *all* free variables. Which of
the following are allowed?

  type TySyn1 = a :: Type
  type TySyn2 = 'Nothing :: Maybe a
  type TySyn3 = 'Just ('Nothing :: Maybe a)
  type TySyn4 = 'Left a :: Either Type a

After some design deliberations (see non-taken alternatives below), the answer
is to reject TySyn1 and TySyn3, but allow TySyn2 and TySyn4, at least for now.
We implicitly quantify over free variables of the outermost kind signature, if
one exists:

  * In TySyn1, the outermost kind signature is (:: Type), and it does not have
    any free variables.
  * In TySyn2, the outermost kind signature is (:: Maybe a), it contains a
    free variable 'a', which we implicitly quantify over.
  * In TySyn3, there is no outermost kind signature. The (:: Maybe a) signature
    is hidden inside 'Just.
  * In TySyn4, the outermost kind signature is (:: Either Type a), it contains
    a free variable 'a', which we implicitly quantify over. That is why we can
    also use it to the left of the double colon: 'Left a

The logic resides in extractHsTyRdrTyVarsKindVars.

This was a stopgap solution until we could explicitly bind invisible
type/kind variables:

  type TySyn3 :: forall a. Maybe a
  type TySyn3 @a = 'Just ('Nothing :: Maybe a)

Now that the new syntax was proposed in #425 and implemented in 9.8, we issue a warning
-Wimplicit-rhs-quantification for TySyn2 and TySyn4 and will eventually disallow them.

Note [Implicit quantification in type synonyms: non-taken alternatives]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Alternative I: No quantification
--------------------------------
We could offer no implicit quantification on the RHS, accepting none of the
TySyn<N> examples. The user would have to bind the variables explicitly:

  type TySyn1 a = a :: Type
  type TySyn2 a = 'Nothing :: Maybe a
  type TySyn3 a = 'Just ('Nothing :: Maybe a)
  type TySyn4 a = 'Left a :: Either Type a

However, this would mean that one would have to specify 'a' at call sites every
time, which could be undesired.

Alternative II: Indiscriminate quantification
---------------------------------------------
We could implicitly quantify over all free variables on the RHS just like we do
on the LHS. Then we would infer the following kinds:

  TySyn1 :: forall {a}. Type
  TySyn2 :: forall {a}. Maybe a
  TySyn3 :: forall {a}. Maybe (Maybe a)
  TySyn4 :: forall {a}. Either Type a

This would work fine for TySyn<2,3,4>, but TySyn1 is clearly bogus: the variable
is free-floating, not fixed by anything.

Alternative III: reportFloatingKvs
----------------------------------
We could augment Alternative II by hunting down free-floating variables during
type checking. While viable, this would mean we'd end up accepting this:

  data Prox k (a :: k)
  type T = Prox k

-}

-- A list of free type/kind variables, which can contain duplicates.
-- See Note [Kind and type-variable binders]
-- These lists are guaranteed to preserve left-to-right ordering of
-- the types the variables were extracted from. See also
-- Note [Ordering of implicit variables].
type FreeKiTyVars = [LocatedN RdrName]

-- | Filter out any type and kind variables that are already in scope in the
-- the supplied LocalRdrEnv. Note that this includes named wildcards, which
-- look like perfectly ordinary type variables at this point.
filterInScope :: LocalRdrEnv -> FreeKiTyVars -> FreeKiTyVars
filterInScope :: LocalRdrEnv -> FreeKiTyVars -> FreeKiTyVars
filterInScope LocalRdrEnv
rdr_env = (GenLocated SrcSpanAnnN RdrName -> Bool)
-> FreeKiTyVars -> FreeKiTyVars
forall a. (a -> Bool) -> [a] -> [a]
filterOut (LocalRdrEnv -> RdrName -> Bool
inScope LocalRdrEnv
rdr_env (RdrName -> Bool)
-> (GenLocated SrcSpanAnnN RdrName -> RdrName)
-> GenLocated SrcSpanAnnN RdrName
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnN RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc)

-- | Filter out any type and kind variables that are already in scope in the
-- the environment's LocalRdrEnv. Note that this includes named wildcards,
-- which look like perfectly ordinary type variables at this point.
filterInScopeM :: FreeKiTyVars -> RnM FreeKiTyVars
filterInScopeM :: FreeKiTyVars -> RnM FreeKiTyVars
filterInScopeM FreeKiTyVars
vars
  = do { LocalRdrEnv
rdr_env <- RnM LocalRdrEnv
getLocalRdrEnv
       ; FreeKiTyVars -> RnM FreeKiTyVars
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (LocalRdrEnv -> FreeKiTyVars -> FreeKiTyVars
filterInScope LocalRdrEnv
rdr_env FreeKiTyVars
vars) }

inScope :: LocalRdrEnv -> RdrName -> Bool
inScope :: LocalRdrEnv -> RdrName -> Bool
inScope LocalRdrEnv
rdr_env RdrName
rdr = RdrName
rdr RdrName -> LocalRdrEnv -> Bool
`elemLocalRdrEnv` LocalRdrEnv
rdr_env

extract_tyarg :: LHsTypeArg GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_tyarg :: LHsTypeArg GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_tyarg (HsValArg LHsType GhcPs
ty) FreeKiTyVars
acc = LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty FreeKiTyVars
acc
extract_tyarg (HsTypeArg LHsToken "@" GhcPs
_ LHsType GhcPs
ki) FreeKiTyVars
acc = LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ki FreeKiTyVars
acc
extract_tyarg (HsArgPar SrcSpan
_) FreeKiTyVars
acc = FreeKiTyVars
acc

extract_tyargs :: [LHsTypeArg GhcPs] -> FreeKiTyVars -> FreeKiTyVars
extract_tyargs :: [LHsTypeArg GhcPs] -> FreeKiTyVars -> FreeKiTyVars
extract_tyargs [LHsTypeArg GhcPs]
args FreeKiTyVars
acc = (HsArg
   GhcPs
   (GenLocated SrcSpanAnnA (HsType GhcPs))
   (GenLocated SrcSpanAnnA (HsType GhcPs))
 -> FreeKiTyVars -> FreeKiTyVars)
-> FreeKiTyVars
-> [HsArg
      GhcPs
      (GenLocated SrcSpanAnnA (HsType GhcPs))
      (GenLocated SrcSpanAnnA (HsType GhcPs))]
-> FreeKiTyVars
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr LHsTypeArg GhcPs -> FreeKiTyVars -> FreeKiTyVars
HsArg
  GhcPs
  (GenLocated SrcSpanAnnA (HsType GhcPs))
  (GenLocated SrcSpanAnnA (HsType GhcPs))
-> FreeKiTyVars -> FreeKiTyVars
extract_tyarg FreeKiTyVars
acc [LHsTypeArg GhcPs]
[HsArg
   GhcPs
   (GenLocated SrcSpanAnnA (HsType GhcPs))
   (GenLocated SrcSpanAnnA (HsType GhcPs))]
args

extractHsTyArgRdrKiTyVars :: [LHsTypeArg GhcPs] -> FreeKiTyVars
extractHsTyArgRdrKiTyVars :: [LHsTypeArg GhcPs] -> FreeKiTyVars
extractHsTyArgRdrKiTyVars [LHsTypeArg GhcPs]
args
  = [LHsTypeArg GhcPs] -> FreeKiTyVars -> FreeKiTyVars
extract_tyargs [LHsTypeArg GhcPs]
args []

-- | 'extractHsTyRdrTyVars' finds the type/kind variables
--                          of a HsType/HsKind.
-- It's used when making the @forall@s explicit.
-- See Note [Kind and type-variable binders]
extractHsTyRdrTyVars :: LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVars :: LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVars LHsType GhcPs
ty = LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty []

-- | Extracts the free type/kind variables from the kind signature of a HsType.
--   This is used to implicitly quantify over @k@ in @type T = Nothing :: Maybe k@.
-- The left-to-right order of variables is preserved.
-- See Note [Kind and type-variable binders] and
--     Note [Ordering of implicit variables] and
--     Note [Implicit quantification in type synonyms].
extractHsTyRdrTyVarsKindVars :: LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVarsKindVars :: LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVarsKindVars (L SrcSpanAnnA
_ HsType GhcPs
ty) =
  case HsType GhcPs
ty of
    HsParTy XParTy GhcPs
_ LHsType GhcPs
ty -> LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVarsKindVars LHsType GhcPs
ty
    HsKindSig XKindSig GhcPs
_ LHsType GhcPs
_ LHsType GhcPs
ki -> LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVars LHsType GhcPs
ki
    HsType GhcPs
_ -> []

-- | Extracts free type and kind variables from types in a list.
-- When the same name occurs multiple times in the types, all occurrences
-- are returned.
extractHsTysRdrTyVars :: [LHsType GhcPs] -> FreeKiTyVars -> FreeKiTyVars
extractHsTysRdrTyVars :: HsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
extractHsTysRdrTyVars HsContext GhcPs
tys = HsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_ltys HsContext GhcPs
tys

-- Returns the free kind variables of any explicitly-kinded binders, returning
-- variable occurrences in left-to-right order.
-- See Note [Ordering of implicit variables].
-- NB: Does /not/ delete the binders themselves.
--     E.g. given  [k1, a:k1, b:k2]
--          the function returns [k1,k2], even though k1 is bound here
extractHsTyVarBndrsKVs :: [LHsTyVarBndr flag GhcPs] -> FreeKiTyVars
extractHsTyVarBndrsKVs :: forall flag. [LHsTyVarBndr flag GhcPs] -> FreeKiTyVars
extractHsTyVarBndrsKVs [LHsTyVarBndr flag GhcPs]
tv_bndrs = [LHsTyVarBndr flag GhcPs] -> FreeKiTyVars
forall flag. [LHsTyVarBndr flag GhcPs] -> FreeKiTyVars
extract_hs_tv_bndrs_kvs [LHsTyVarBndr flag GhcPs]
tv_bndrs

-- Returns the free kind variables in a type family result signature, returning
-- variable occurrences in left-to-right order.
-- See Note [Ordering of implicit variables].
extractRdrKindSigVars :: LFamilyResultSig GhcPs -> FreeKiTyVars
extractRdrKindSigVars :: LFamilyResultSig GhcPs -> FreeKiTyVars
extractRdrKindSigVars (L SrcAnn NoEpAnns
_ FamilyResultSig GhcPs
resultSig) = case FamilyResultSig GhcPs
resultSig of
  KindSig XCKindSig GhcPs
_ LHsType GhcPs
k                            -> LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVars LHsType GhcPs
k
  TyVarSig XTyVarSig GhcPs
_ (L SrcSpanAnnA
_ (KindedTyVar XKindedTyVar GhcPs
_ ()
_ LIdP GhcPs
_ LHsType GhcPs
k)) -> LHsType GhcPs -> FreeKiTyVars
extractHsTyRdrTyVars LHsType GhcPs
k
  FamilyResultSig GhcPs
_ -> []

-- | Extracts free type and kind variables from an argument in a GADT
-- constructor, returning variable occurrences in left-to-right order.
-- See @Note [Ordering of implicit variables]@.
extractConDeclGADTDetailsTyVars ::
  HsConDeclGADTDetails GhcPs -> FreeKiTyVars -> FreeKiTyVars
extractConDeclGADTDetailsTyVars :: HsConDeclGADTDetails GhcPs -> FreeKiTyVars -> FreeKiTyVars
extractConDeclGADTDetailsTyVars HsConDeclGADTDetails GhcPs
con_args = case HsConDeclGADTDetails GhcPs
con_args of
  PrefixConGADT [HsScaled GhcPs (LHsType GhcPs)]
args      -> [HsScaled GhcPs (LHsType GhcPs)] -> FreeKiTyVars -> FreeKiTyVars
extract_scaled_ltys [HsScaled GhcPs (LHsType GhcPs)]
args
  RecConGADT (L SrcSpanAnnL
_ [GenLocated SrcSpanAnnA (ConDeclField GhcPs)]
flds) LHsUniToken "->" "\8594" GhcPs
_ -> HsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_ltys (HsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars)
-> HsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$ (GenLocated SrcSpanAnnA (ConDeclField GhcPs) -> LHsType GhcPs)
-> [GenLocated SrcSpanAnnA (ConDeclField GhcPs)] -> HsContext GhcPs
forall a b. (a -> b) -> [a] -> [b]
map (ConDeclField GhcPs -> LHsType GhcPs
ConDeclField GhcPs -> GenLocated SrcSpanAnnA (HsType GhcPs)
forall pass. ConDeclField pass -> LBangType pass
cd_fld_type (ConDeclField GhcPs -> GenLocated SrcSpanAnnA (HsType GhcPs))
-> (GenLocated SrcSpanAnnA (ConDeclField GhcPs)
    -> ConDeclField GhcPs)
-> GenLocated SrcSpanAnnA (ConDeclField GhcPs)
-> GenLocated SrcSpanAnnA (HsType GhcPs)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnA (ConDeclField GhcPs) -> ConDeclField GhcPs
forall l e. GenLocated l e -> e
unLoc) ([GenLocated SrcSpanAnnA (ConDeclField GhcPs)] -> HsContext GhcPs)
-> [GenLocated SrcSpanAnnA (ConDeclField GhcPs)] -> HsContext GhcPs
forall a b. (a -> b) -> a -> b
$ [GenLocated SrcSpanAnnA (ConDeclField GhcPs)]
flds

-- | Get type/kind variables mentioned in the kind signature, preserving
-- left-to-right order:
--
--  * data T a (b :: k1) :: k2 -> k1 -> k2 -> Type   -- result: [k2,k1]
--  * data T a (b :: k1)                             -- result: []
--
-- See Note [Ordering of implicit variables].
extractDataDefnKindVars :: HsDataDefn GhcPs ->  FreeKiTyVars
extractDataDefnKindVars :: HsDataDefn GhcPs -> FreeKiTyVars
extractDataDefnKindVars (HsDataDefn { dd_kindSig :: forall pass. HsDataDefn pass -> Maybe (LHsKind pass)
dd_kindSig = Maybe (LHsType GhcPs)
ksig })
  = FreeKiTyVars
-> (GenLocated SrcSpanAnnA (HsType GhcPs) -> FreeKiTyVars)
-> Maybe (GenLocated SrcSpanAnnA (HsType GhcPs))
-> FreeKiTyVars
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] LHsType GhcPs -> FreeKiTyVars
GenLocated SrcSpanAnnA (HsType GhcPs) -> FreeKiTyVars
extractHsTyRdrTyVars Maybe (LHsType GhcPs)
Maybe (GenLocated SrcSpanAnnA (HsType GhcPs))
ksig

extract_lctxt :: LHsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lctxt :: LHsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lctxt LHsContext GhcPs
ctxt = HsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_ltys (GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcPs)]
-> [GenLocated SrcSpanAnnA (HsType GhcPs)]
forall l e. GenLocated l e -> e
unLoc LHsContext GhcPs
GenLocated SrcSpanAnnC [GenLocated SrcSpanAnnA (HsType GhcPs)]
ctxt)

extract_scaled_ltys :: [HsScaled GhcPs (LHsType GhcPs)]
                    -> FreeKiTyVars -> FreeKiTyVars
extract_scaled_ltys :: [HsScaled GhcPs (LHsType GhcPs)] -> FreeKiTyVars -> FreeKiTyVars
extract_scaled_ltys [HsScaled GhcPs (LHsType GhcPs)]
args FreeKiTyVars
acc = (HsScaled GhcPs (GenLocated SrcSpanAnnA (HsType GhcPs))
 -> FreeKiTyVars -> FreeKiTyVars)
-> FreeKiTyVars
-> [HsScaled GhcPs (GenLocated SrcSpanAnnA (HsType GhcPs))]
-> FreeKiTyVars
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr HsScaled GhcPs (LHsType GhcPs) -> FreeKiTyVars -> FreeKiTyVars
HsScaled GhcPs (GenLocated SrcSpanAnnA (HsType GhcPs))
-> FreeKiTyVars -> FreeKiTyVars
extract_scaled_lty FreeKiTyVars
acc [HsScaled GhcPs (LHsType GhcPs)]
[HsScaled GhcPs (GenLocated SrcSpanAnnA (HsType GhcPs))]
args

extract_scaled_lty :: HsScaled GhcPs (LHsType GhcPs)
                   -> FreeKiTyVars -> FreeKiTyVars
extract_scaled_lty :: HsScaled GhcPs (LHsType GhcPs) -> FreeKiTyVars -> FreeKiTyVars
extract_scaled_lty (HsScaled HsArrow GhcPs
m LHsType GhcPs
ty) FreeKiTyVars
acc = LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$ HsArrow GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_hs_arrow HsArrow GhcPs
m FreeKiTyVars
acc

extract_ltys :: [LHsType GhcPs] -> FreeKiTyVars -> FreeKiTyVars
extract_ltys :: HsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_ltys HsContext GhcPs
tys FreeKiTyVars
acc = (GenLocated SrcSpanAnnA (HsType GhcPs)
 -> FreeKiTyVars -> FreeKiTyVars)
-> FreeKiTyVars
-> [GenLocated SrcSpanAnnA (HsType GhcPs)]
-> FreeKiTyVars
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
GenLocated SrcSpanAnnA (HsType GhcPs)
-> FreeKiTyVars -> FreeKiTyVars
extract_lty FreeKiTyVars
acc HsContext GhcPs
[GenLocated SrcSpanAnnA (HsType GhcPs)]
tys

extract_lty :: LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty :: LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty (L SrcSpanAnnA
_ HsType GhcPs
ty) FreeKiTyVars
acc
  = case HsType GhcPs
ty of
      HsTyVar XTyVar GhcPs
_ PromotionFlag
_  LIdP GhcPs
ltv            -> GenLocated SrcSpanAnnN RdrName -> FreeKiTyVars -> FreeKiTyVars
extract_tv LIdP GhcPs
GenLocated SrcSpanAnnN RdrName
ltv FreeKiTyVars
acc
      HsBangTy XBangTy GhcPs
_ HsSrcBang
_ LHsType GhcPs
ty             -> LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty FreeKiTyVars
acc
      HsRecTy XRecTy GhcPs
_ [LConDeclField GhcPs]
flds              -> (GenLocated SrcSpanAnnA (ConDeclField GhcPs)
 -> FreeKiTyVars -> FreeKiTyVars)
-> FreeKiTyVars
-> [GenLocated SrcSpanAnnA (ConDeclField GhcPs)]
-> FreeKiTyVars
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
GenLocated SrcSpanAnnA (HsType GhcPs)
-> FreeKiTyVars -> FreeKiTyVars
extract_lty
                                            (GenLocated SrcSpanAnnA (HsType GhcPs)
 -> FreeKiTyVars -> FreeKiTyVars)
-> (GenLocated SrcSpanAnnA (ConDeclField GhcPs)
    -> GenLocated SrcSpanAnnA (HsType GhcPs))
-> GenLocated SrcSpanAnnA (ConDeclField GhcPs)
-> FreeKiTyVars
-> FreeKiTyVars
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConDeclField GhcPs -> LHsType GhcPs
ConDeclField GhcPs -> GenLocated SrcSpanAnnA (HsType GhcPs)
forall pass. ConDeclField pass -> LBangType pass
cd_fld_type (ConDeclField GhcPs -> GenLocated SrcSpanAnnA (HsType GhcPs))
-> (GenLocated SrcSpanAnnA (ConDeclField GhcPs)
    -> ConDeclField GhcPs)
-> GenLocated SrcSpanAnnA (ConDeclField GhcPs)
-> GenLocated SrcSpanAnnA (HsType GhcPs)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnA (ConDeclField GhcPs) -> ConDeclField GhcPs
forall l e. GenLocated l e -> e
unLoc) FreeKiTyVars
acc
                                           [LConDeclField GhcPs]
[GenLocated SrcSpanAnnA (ConDeclField GhcPs)]
flds
      HsAppTy XAppTy GhcPs
_ LHsType GhcPs
ty1 LHsType GhcPs
ty2           -> LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty1 (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
                                     LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty2 FreeKiTyVars
acc
      HsAppKindTy XAppKindTy GhcPs
_ LHsType GhcPs
ty LHsToken "@" GhcPs
_ LHsType GhcPs
k        -> LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
                                     LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
k FreeKiTyVars
acc
      HsListTy XListTy GhcPs
_ LHsType GhcPs
ty               -> LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty FreeKiTyVars
acc
      HsTupleTy XTupleTy GhcPs
_ HsTupleSort
_ HsContext GhcPs
tys           -> HsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_ltys HsContext GhcPs
tys FreeKiTyVars
acc
      HsSumTy XSumTy GhcPs
_ HsContext GhcPs
tys               -> HsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_ltys HsContext GhcPs
tys FreeKiTyVars
acc
      HsFunTy XFunTy GhcPs
_ HsArrow GhcPs
w LHsType GhcPs
ty1 LHsType GhcPs
ty2         -> LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty1 (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
                                     LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty2 (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
                                     HsArrow GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_hs_arrow HsArrow GhcPs
w FreeKiTyVars
acc
      HsIParamTy XIParamTy GhcPs
_ XRec GhcPs HsIPName
_ LHsType GhcPs
ty           -> LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty FreeKiTyVars
acc
      HsOpTy XOpTy GhcPs
_ PromotionFlag
_ LHsType GhcPs
ty1 LIdP GhcPs
tv LHsType GhcPs
ty2       -> GenLocated SrcSpanAnnN RdrName -> FreeKiTyVars -> FreeKiTyVars
extract_tv LIdP GhcPs
GenLocated SrcSpanAnnN RdrName
tv (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
                                     LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty1 (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
                                     LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty2 FreeKiTyVars
acc
      HsParTy XParTy GhcPs
_ LHsType GhcPs
ty                -> LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty FreeKiTyVars
acc
      HsSpliceTy {}               -> FreeKiTyVars
acc  -- Type splices mention no tvs
      HsDocTy XDocTy GhcPs
_ LHsType GhcPs
ty LHsDoc GhcPs
_              -> LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty FreeKiTyVars
acc
      HsExplicitListTy XExplicitListTy GhcPs
_ PromotionFlag
_ HsContext GhcPs
tys    -> HsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_ltys HsContext GhcPs
tys FreeKiTyVars
acc
      HsExplicitTupleTy XExplicitTupleTy GhcPs
_ HsContext GhcPs
tys     -> HsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_ltys HsContext GhcPs
tys FreeKiTyVars
acc
      HsTyLit XTyLit GhcPs
_ HsTyLit GhcPs
_                 -> FreeKiTyVars
acc
      HsStarTy XStarTy GhcPs
_ Bool
_                -> FreeKiTyVars
acc
      HsKindSig XKindSig GhcPs
_ LHsType GhcPs
ty LHsType GhcPs
ki           -> LHsType GhcPs -> LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_kind_sig LHsType GhcPs
ty LHsType GhcPs
ki FreeKiTyVars
acc
      HsForAllTy { hst_tele :: forall pass. HsType pass -> HsForAllTelescope pass
hst_tele = HsForAllTelescope GhcPs
tele, hst_body :: forall pass. HsType pass -> LHsType pass
hst_body = LHsType GhcPs
ty }
                                  -> HsForAllTelescope GhcPs
-> FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
extract_hs_for_all_telescope HsForAllTelescope GhcPs
tele FreeKiTyVars
acc (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
                                     LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty []
      HsQualTy { hst_ctxt :: forall pass. HsType pass -> LHsContext pass
hst_ctxt = LHsContext GhcPs
ctxt, hst_body :: forall pass. HsType pass -> LHsType pass
hst_body = LHsType GhcPs
ty }
                                  -> LHsContext GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lctxt LHsContext GhcPs
ctxt (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
                                     LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty FreeKiTyVars
acc
      XHsType {}                  -> FreeKiTyVars
acc
      -- We deal with these separately in rnLHsTypeWithWildCards
      HsWildCardTy {}             -> FreeKiTyVars
acc

extract_kind_sig :: LHsType GhcPs -- type
                 -> LHsType GhcPs -- kind
                 -> FreeKiTyVars -> FreeKiTyVars
extract_kind_sig :: LHsType GhcPs -> LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_kind_sig LHsType GhcPs
ty LHsType GhcPs
ki FreeKiTyVars
acc
  | (L SrcSpanAnnA
_ HsForAllTy { hst_tele :: forall pass. HsType pass -> HsForAllTelescope pass
hst_tele = HsForAllInvis { hsf_invis_bndrs :: forall pass.
HsForAllTelescope pass -> [LHsTyVarBndr Specificity pass]
hsf_invis_bndrs = [LHsTyVarBndr Specificity GhcPs]
bndrs }
                    , hst_body :: forall pass. HsType pass -> LHsType pass
hst_body = LHsType GhcPs
ki_body }) <- LHsType GhcPs
ki
  = [LHsTyVarBndr Specificity GhcPs]
-> FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
forall flag.
[LHsTyVarBndr flag GhcPs]
-> FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
extract_hs_tv_bndrs [LHsTyVarBndr Specificity GhcPs]
bndrs FreeKiTyVars
acc (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
    LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
    LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ki_body []
extract_kind_sig LHsType GhcPs
ty LHsType GhcPs
ki FreeKiTyVars
acc
  = LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ty (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$
    LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
ki FreeKiTyVars
acc

extract_lhs_sig_ty :: LHsSigType GhcPs -> FreeKiTyVars
extract_lhs_sig_ty :: LHsSigType GhcPs -> FreeKiTyVars
extract_lhs_sig_ty (L SrcSpanAnnA
_ (HsSig{sig_bndrs :: forall pass. HsSigType pass -> HsOuterSigTyVarBndrs pass
sig_bndrs = HsOuterSigTyVarBndrs GhcPs
outer_bndrs, sig_body :: forall pass. HsSigType pass -> LHsType pass
sig_body = LHsType GhcPs
body})) =
  HsOuterSigTyVarBndrs GhcPs -> FreeKiTyVars -> FreeKiTyVars
forall flag.
HsOuterTyVarBndrs flag GhcPs -> FreeKiTyVars -> FreeKiTyVars
extractHsOuterTvBndrs HsOuterSigTyVarBndrs GhcPs
outer_bndrs (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$ LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
body []

extract_hs_arrow :: HsArrow GhcPs -> FreeKiTyVars ->
                   FreeKiTyVars
extract_hs_arrow :: HsArrow GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_hs_arrow (HsExplicitMult LHsToken "%" GhcPs
_ LHsType GhcPs
p LHsUniToken "->" "\8594" GhcPs
_) FreeKiTyVars
acc = LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
extract_lty LHsType GhcPs
p FreeKiTyVars
acc
extract_hs_arrow HsArrow GhcPs
_ FreeKiTyVars
acc = FreeKiTyVars
acc

extract_hs_for_all_telescope :: HsForAllTelescope GhcPs
                             -> FreeKiTyVars -- Accumulator
                             -> FreeKiTyVars -- Free in body
                             -> FreeKiTyVars
extract_hs_for_all_telescope :: HsForAllTelescope GhcPs
-> FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
extract_hs_for_all_telescope HsForAllTelescope GhcPs
tele FreeKiTyVars
acc_vars FreeKiTyVars
body_fvs =
  case HsForAllTelescope GhcPs
tele of
    HsForAllVis { hsf_vis_bndrs :: forall pass. HsForAllTelescope pass -> [LHsTyVarBndr () pass]
hsf_vis_bndrs = [LHsTyVarBndr () GhcPs]
bndrs } ->
      [LHsTyVarBndr () GhcPs]
-> FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
forall flag.
[LHsTyVarBndr flag GhcPs]
-> FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
extract_hs_tv_bndrs [LHsTyVarBndr () GhcPs]
bndrs FreeKiTyVars
acc_vars FreeKiTyVars
body_fvs
    HsForAllInvis { hsf_invis_bndrs :: forall pass.
HsForAllTelescope pass -> [LHsTyVarBndr Specificity pass]
hsf_invis_bndrs = [LHsTyVarBndr Specificity GhcPs]
bndrs } ->
      [LHsTyVarBndr Specificity GhcPs]
-> FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
forall flag.
[LHsTyVarBndr flag GhcPs]
-> FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
extract_hs_tv_bndrs [LHsTyVarBndr Specificity GhcPs]
bndrs FreeKiTyVars
acc_vars FreeKiTyVars
body_fvs

extractHsOuterTvBndrs :: HsOuterTyVarBndrs flag GhcPs
                      -> FreeKiTyVars -- Free in body
                      -> FreeKiTyVars -- Free in result
extractHsOuterTvBndrs :: forall flag.
HsOuterTyVarBndrs flag GhcPs -> FreeKiTyVars -> FreeKiTyVars
extractHsOuterTvBndrs HsOuterTyVarBndrs flag GhcPs
outer_bndrs FreeKiTyVars
body_fvs =
  case HsOuterTyVarBndrs flag GhcPs
outer_bndrs of
    HsOuterImplicit{}                  -> FreeKiTyVars
body_fvs
    HsOuterExplicit{hso_bndrs :: forall flag pass.
HsOuterTyVarBndrs flag pass -> [LHsTyVarBndr flag (NoGhcTc pass)]
hso_bndrs = [LHsTyVarBndr flag (NoGhcTc GhcPs)]
bndrs} -> [LHsTyVarBndr flag GhcPs]
-> FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
forall flag.
[LHsTyVarBndr flag GhcPs]
-> FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
extract_hs_tv_bndrs [LHsTyVarBndr flag (NoGhcTc GhcPs)]
[LHsTyVarBndr flag GhcPs]
bndrs [] FreeKiTyVars
body_fvs

extract_hs_tv_bndrs :: [LHsTyVarBndr flag GhcPs]
                    -> FreeKiTyVars  -- Accumulator
                    -> FreeKiTyVars  -- Free in body
                    -> FreeKiTyVars
-- In (forall (a :: Maybe e). a -> b) we have
--     'a' is bound by the forall
--     'b' is a free type variable
--     'e' is a free kind variable
extract_hs_tv_bndrs :: forall flag.
[LHsTyVarBndr flag GhcPs]
-> FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
extract_hs_tv_bndrs [LHsTyVarBndr flag GhcPs]
tv_bndrs FreeKiTyVars
acc_vars FreeKiTyVars
body_vars = FreeKiTyVars
new_vars FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
forall a. [a] -> [a] -> [a]
++ FreeKiTyVars
acc_vars
  where
    new_vars :: FreeKiTyVars
new_vars
      | [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LHsTyVarBndr flag GhcPs]
[GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)]
tv_bndrs = FreeKiTyVars
body_vars
      | Bool
otherwise = FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
filterFreeVarsToBind FreeKiTyVars
tv_bndr_rdrs (FreeKiTyVars -> FreeKiTyVars) -> FreeKiTyVars -> FreeKiTyVars
forall a b. (a -> b) -> a -> b
$ FreeKiTyVars
bndr_vars FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
forall a. [a] -> [a] -> [a]
++ FreeKiTyVars
body_vars
    -- NB: delete all tv_bndr_rdrs from bndr_vars as well as body_vars.
    -- See Note [Kind variable scoping]
    bndr_vars :: FreeKiTyVars
bndr_vars = [LHsTyVarBndr flag GhcPs] -> FreeKiTyVars
forall flag. [LHsTyVarBndr flag GhcPs] -> FreeKiTyVars
extract_hs_tv_bndrs_kvs [LHsTyVarBndr flag GhcPs]
tv_bndrs
    tv_bndr_rdrs :: FreeKiTyVars
tv_bndr_rdrs = (GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)
 -> GenLocated SrcSpanAnnN RdrName)
-> [GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)]
-> FreeKiTyVars
forall a b. (a -> b) -> [a] -> [b]
map LHsTyVarBndr flag GhcPs -> LocatedN (IdP GhcPs)
GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)
-> GenLocated SrcSpanAnnN RdrName
forall (p :: Pass) flag.
(Anno (IdGhcP p) ~ SrcSpanAnnN) =>
LHsTyVarBndr flag (GhcPass p) -> LocatedN (IdP (GhcPass p))
hsLTyVarLocName [LHsTyVarBndr flag GhcPs]
[GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)]
tv_bndrs

extract_hs_tv_bndrs_kvs :: [LHsTyVarBndr flag GhcPs] -> FreeKiTyVars
-- Returns the free kind variables of any explicitly-kinded binders, returning
-- variable occurrences in left-to-right order.
-- See Note [Ordering of implicit variables].
-- NB: Does /not/ delete the binders themselves.
--     E.g. given  [k1, a:k1, b:k2]
--          the function returns [k1,k2], even though k1 is bound here
extract_hs_tv_bndrs_kvs :: forall flag. [LHsTyVarBndr flag GhcPs] -> FreeKiTyVars
extract_hs_tv_bndrs_kvs [LHsTyVarBndr flag GhcPs]
tv_bndrs =
    (GenLocated SrcSpanAnnA (HsType GhcPs)
 -> FreeKiTyVars -> FreeKiTyVars)
-> FreeKiTyVars
-> [GenLocated SrcSpanAnnA (HsType GhcPs)]
-> FreeKiTyVars
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr LHsType GhcPs -> FreeKiTyVars -> FreeKiTyVars
GenLocated SrcSpanAnnA (HsType GhcPs)
-> FreeKiTyVars -> FreeKiTyVars
extract_lty []
          [LHsType GhcPs
GenLocated SrcSpanAnnA (HsType GhcPs)
k | L SrcSpanAnnA
_ (KindedTyVar XKindedTyVar GhcPs
_ flag
_ LIdP GhcPs
_ LHsType GhcPs
k) <- [LHsTyVarBndr flag GhcPs]
[GenLocated SrcSpanAnnA (HsTyVarBndr flag GhcPs)]
tv_bndrs]

extract_tv :: LocatedN RdrName -> FreeKiTyVars -> FreeKiTyVars
extract_tv :: GenLocated SrcSpanAnnN RdrName -> FreeKiTyVars -> FreeKiTyVars
extract_tv GenLocated SrcSpanAnnN RdrName
tv FreeKiTyVars
acc =
  if RdrName -> Bool
isRdrTyVar (GenLocated SrcSpanAnnN RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnN RdrName
tv) Bool -> Bool -> Bool
&& (Bool -> Bool
not (Bool -> Bool) -> (RdrName -> Bool) -> RdrName -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RdrName -> Bool
isQual) (GenLocated SrcSpanAnnN RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnN RdrName
tv) then GenLocated SrcSpanAnnN RdrName
tvGenLocated SrcSpanAnnN RdrName -> FreeKiTyVars -> FreeKiTyVars
forall a. a -> [a] -> [a]
:FreeKiTyVars
acc else FreeKiTyVars
acc

-- Deletes duplicates in a list of Located things. This is used to:
--
-- * Delete duplicate occurrences of implicitly bound type/kind variables when
--   bringing them into scope (in rnImplicitTvOccs).
--
-- * Delete duplicate occurrences of named wildcards (in rn_hs_sig_wc_type and
--   rnHsWcType).
--
-- Importantly, this function is stable with respect to the original ordering
-- of things in the list. This is important, as it is a property that GHC
-- relies on to maintain the left-to-right ordering of implicitly quantified
-- type variables.
-- See Note [Ordering of implicit variables].
nubL :: Eq a => [GenLocated l a] -> [GenLocated l a]
nubL :: forall a l. Eq a => [GenLocated l a] -> [GenLocated l a]
nubL = (GenLocated l a -> GenLocated l a -> Bool)
-> [GenLocated l a] -> [GenLocated l a]
forall a. (a -> a -> Bool) -> [a] -> [a]
nubBy GenLocated l a -> GenLocated l a -> Bool
forall a l. Eq a => GenLocated l a -> GenLocated l a -> Bool
eqLocated

nubN :: Eq a => [LocatedN a] -> [LocatedN a]
nubN :: forall a. Eq a => [LocatedN a] -> [LocatedN a]
nubN = (LocatedN a -> LocatedN a -> Bool) -> [LocatedN a] -> [LocatedN a]
forall a. (a -> a -> Bool) -> [a] -> [a]
nubBy LocatedN a -> LocatedN a -> Bool
forall a l. Eq a => GenLocated l a -> GenLocated l a -> Bool
eqLocated

-- | Filter out any potential implicit binders that are either
-- already in scope, or are explicitly bound in the binder.
filterFreeVarsToBind :: FreeKiTyVars
                     -- ^ Explicitly bound here
                     -> FreeKiTyVars
                     -- ^ Potential implicit binders
                     -> FreeKiTyVars
                     -- ^ Final implicit binders
filterFreeVarsToBind :: FreeKiTyVars -> FreeKiTyVars -> FreeKiTyVars
filterFreeVarsToBind FreeKiTyVars
bndrs = (GenLocated SrcSpanAnnN RdrName -> Bool)
-> FreeKiTyVars -> FreeKiTyVars
forall a. (a -> Bool) -> [a] -> [a]
filterOut GenLocated SrcSpanAnnN RdrName -> Bool
is_in_scope
    -- Make sure to list the binder kvs before the body kvs, as mandated by
    -- Note [Ordering of implicit variables]
  where
    is_in_scope :: GenLocated SrcSpanAnnN RdrName -> Bool
is_in_scope GenLocated SrcSpanAnnN RdrName
locc = (GenLocated SrcSpanAnnN RdrName -> Bool) -> FreeKiTyVars -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (GenLocated SrcSpanAnnN RdrName
-> GenLocated SrcSpanAnnN RdrName -> Bool
forall a l. Eq a => GenLocated l a -> GenLocated l a -> Bool
eqLocated GenLocated SrcSpanAnnN RdrName
locc) FreeKiTyVars
bndrs