{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ScopedTypeVariables, BangPatterns #-}
{-# LANGUAGE TypeFamilies #-}

{-# OPTIONS_GHC -Wno-incomplete-uni-patterns   #-}
{-# OPTIONS_GHC -Wno-incomplete-record-updates #-}

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

Renaming and dependency analysis of bindings

This module does renaming and dependency analysis on value bindings in
the abstract syntax.  It does {\em not} do cycle-checks on class or
type-synonym declarations; those cannot be done at this stage because
they may be affected by renaming (which isn't fully worked out yet).
-}

module GHC.Rename.Bind (
   -- Renaming top-level bindings
   rnTopBindsLHS, rnTopBindsLHSBoot, rnTopBindsBoot, rnValBindsRHS,

   -- Renaming local bindings
   rnLocalBindsAndThen, rnLocalValBindsLHS, rnLocalValBindsRHS,

   -- Other bindings
   rnMethodBinds, renameSigs,
   rnMatchGroup, rnGRHSs, rnGRHS, rnSrcFixityDecl,
   makeMiniFixityEnv, MiniFixityEnv,
   HsSigCtxt(..)
   ) where

import GHC.Prelude

import {-# SOURCE #-} GHC.Rename.Expr( rnExpr, rnLExpr, rnStmts )

import GHC.Hs
import GHC.Tc.Errors.Types
import GHC.Tc.Utils.Monad
import GHC.Rename.HsType
import GHC.Rename.Pat
import GHC.Rename.Names
import GHC.Rename.Env
import GHC.Rename.Fixity
import GHC.Rename.Utils ( mapFvRn
                        , checkDupRdrNames, checkDupRdrNamesN
                        , warnUnusedLocalBinds
                        , warnForallIdentifier
                        , checkUnusedRecordWildcard
                        , checkDupAndShadowedNames, bindLocalNamesFV
                        , addNoNestedForallsContextsErr, checkInferredVars )
import GHC.Driver.Session
import GHC.Unit.Module
import GHC.Types.Error
import GHC.Types.FieldLabel
import GHC.Types.Name
import GHC.Types.Name.Env
import GHC.Types.Name.Set
import GHC.Types.Name.Reader ( RdrName, rdrNameOcc )
import GHC.Types.SrcLoc as SrcLoc
import GHC.Data.List.SetOps    ( findDupsEq )
import GHC.Types.Basic         ( RecFlag(..), TypeOrKind(..) )
import GHC.Data.Graph.Directed ( SCC(..) )
import GHC.Data.Bag
import GHC.Utils.Misc
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Types.Unique.Set
import GHC.Data.Maybe          ( orElse )
import GHC.Data.OrdList
import qualified GHC.LanguageExtensions as LangExt

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

import Control.Monad
import Data.Foldable      ( toList )
import Data.List          ( partition, sortBy )
import Data.List.NonEmpty ( NonEmpty(..) )

{-
-- ToDo: Put the annotations into the monad, so that they arrive in the proper
-- place and can be used when complaining.

The code tree received by the function @rnBinds@ contains definitions
in where-clauses which are all apparently mutually recursive, but which may
not really depend upon each other. For example, in the top level program
\begin{verbatim}
f x = y where a = x
              y = x
\end{verbatim}
the definitions of @a@ and @y@ do not depend on each other at all.
Unfortunately, the typechecker cannot always check such definitions.
\footnote{Mycroft, A. 1984. Polymorphic type schemes and recursive
definitions. In Proceedings of the International Symposium on Programming,
Toulouse, pp. 217-39. LNCS 167. Springer Verlag.}
However, the typechecker usually can check definitions in which only the
strongly connected components have been collected into recursive bindings.
This is precisely what the function @rnBinds@ does.

ToDo: deal with case where a single monobinds binds the same variable
twice.

The vertag tag is a unique @Int@; the tags only need to be unique
within one @MonoBinds@, so that unique-Int plumbing is done explicitly
(heavy monad machinery not needed).


************************************************************************
*                                                                      *
* naming conventions                                                   *
*                                                                      *
************************************************************************

\subsection[name-conventions]{Name conventions}

The basic algorithm involves walking over the tree and returning a tuple
containing the new tree plus its free variables. Some functions, such
as those walking polymorphic bindings (HsBinds) and qualifier lists in
list comprehensions (@Quals@), return the variables bound in local
environments. These are then used to calculate the free variables of the
expression evaluated in these environments.

Conventions for variable names are as follows:
\begin{itemize}
\item
new code is given a prime to distinguish it from the old.

\item
a set of variables defined in @Exp@ is written @dvExp@

\item
a set of variables free in @Exp@ is written @fvExp@
\end{itemize}

************************************************************************
*                                                                      *
* analysing polymorphic bindings (HsBindGroup, HsBind)
*                                                                      *
************************************************************************

\subsubsection[dep-HsBinds]{Polymorphic bindings}

Non-recursive expressions are reconstructed without any changes at top
level, although their component expressions may have to be altered.
However, non-recursive expressions are currently not expected as
\Haskell{} programs, and this code should not be executed.

Monomorphic bindings contain information that is returned in a tuple
(a @FlatMonoBinds@) containing:

\begin{enumerate}
\item
a unique @Int@ that serves as the ``vertex tag'' for this binding.

\item
the name of a function or the names in a pattern. These are a set
referred to as @dvLhs@, the defined variables of the left hand side.

\item
the free variables of the body. These are referred to as @fvBody@.

\item
the definition's actual code. This is referred to as just @code@.
\end{enumerate}

The function @nonRecDvFv@ returns two sets of variables. The first is
the set of variables defined in the set of monomorphic bindings, while the
second is the set of free variables in those bindings.

The set of variables defined in a non-recursive binding is just the
union of all of them, as @union@ removes duplicates. However, the
free variables in each successive set of cumulative bindings is the
union of those in the previous set plus those of the newest binding after
the defined variables of the previous set have been removed.

@rnMethodBinds@ deals only with the declarations in class and
instance declarations.  It expects only to see @FunMonoBind@s, and
it expects the global environment to contain bindings for the binders
(which are all class operations).

************************************************************************
*                                                                      *
\subsubsection{ Top-level bindings}
*                                                                      *
************************************************************************
-}

-- for top-level bindings, we need to make top-level names,
-- so we have a different entry point than for local bindings
rnTopBindsLHS :: MiniFixityEnv
              -> HsValBinds GhcPs
              -> RnM (HsValBindsLR GhcRn GhcPs)
rnTopBindsLHS :: MiniFixityEnv
-> HsValBinds GhcPs -> RnM (HsValBindsLR (GhcPass 'Renamed) GhcPs)
rnTopBindsLHS MiniFixityEnv
fix_env HsValBinds GhcPs
binds
  = NameMaker
-> HsValBinds GhcPs -> RnM (HsValBindsLR (GhcPass 'Renamed) GhcPs)
rnValBindsLHS (MiniFixityEnv -> NameMaker
topRecNameMaker MiniFixityEnv
fix_env) HsValBinds GhcPs
binds

-- Ensure that a hs-boot file has no top-level bindings.
rnTopBindsLHSBoot :: MiniFixityEnv
                  -> HsValBinds GhcPs
                  -> RnM (HsValBindsLR GhcRn GhcPs)
rnTopBindsLHSBoot :: MiniFixityEnv
-> HsValBinds GhcPs -> RnM (HsValBindsLR (GhcPass 'Renamed) GhcPs)
rnTopBindsLHSBoot MiniFixityEnv
fix_env HsValBinds GhcPs
binds
  = do  { HsValBindsLR (GhcPass 'Renamed) GhcPs
topBinds <- MiniFixityEnv
-> HsValBinds GhcPs -> RnM (HsValBindsLR (GhcPass 'Renamed) GhcPs)
rnTopBindsLHS MiniFixityEnv
fix_env HsValBinds GhcPs
binds
        ; case HsValBindsLR (GhcPass 'Renamed) GhcPs
topBinds of
            ValBinds XValBinds (GhcPass 'Renamed) GhcPs
x LHsBindsLR (GhcPass 'Renamed) GhcPs
mbinds [LSig GhcPs]
sigs ->
              do  { forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ LHsBindLR (GhcPass 'Renamed) GhcPs -> RnM ()
bindInHsBootFileErr LHsBindsLR (GhcPass 'Renamed) GhcPs
mbinds
                  ; forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall idL idR.
XValBinds idL idR
-> LHsBindsLR idL idR -> [LSig idR] -> HsValBindsLR idL idR
ValBinds XValBinds (GhcPass 'Renamed) GhcPs
x forall a. Bag a
emptyBag [LSig GhcPs]
sigs) }
            HsValBindsLR (GhcPass 'Renamed) GhcPs
_ -> forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"rnTopBindsLHSBoot" (forall a. Outputable a => a -> SDoc
ppr HsValBindsLR (GhcPass 'Renamed) GhcPs
topBinds) }

rnTopBindsBoot :: NameSet -> HsValBindsLR GhcRn GhcPs
               -> RnM (HsValBinds GhcRn, DefUses)
-- A hs-boot file has no bindings.
-- Return a single HsBindGroup with empty binds and renamed signatures
rnTopBindsBoot :: NameSet
-> HsValBindsLR (GhcPass 'Renamed) GhcPs
-> RnM (HsValBinds (GhcPass 'Renamed), DefUses)
rnTopBindsBoot NameSet
bound_names (ValBinds XValBinds (GhcPass 'Renamed) GhcPs
_ LHsBindsLR (GhcPass 'Renamed) GhcPs
_ [LSig GhcPs]
sigs)
  = do  { ([GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
sigs', NameSet
fvs) <- HsSigCtxt
-> [LSig GhcPs] -> RnM ([LSig (GhcPass 'Renamed)], NameSet)
renameSigs (NameSet -> HsSigCtxt
HsBootCtxt NameSet
bound_names) [LSig GhcPs]
sigs
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall idL idR. XXValBindsLR idL idR -> HsValBindsLR idL idR
XValBindsLR (forall idL.
[(RecFlag, LHsBinds idL)]
-> [LSig (GhcPass 'Renamed)] -> NHsValBindsLR idL
NValBinds [] [GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
sigs'), NameSet -> DefUses
usesOnly NameSet
fvs) }
rnTopBindsBoot NameSet
_ HsValBindsLR (GhcPass 'Renamed) GhcPs
b = forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"rnTopBindsBoot" (forall a. Outputable a => a -> SDoc
ppr HsValBindsLR (GhcPass 'Renamed) GhcPs
b)

{-
*********************************************************
*                                                      *
                HsLocalBinds
*                                                      *
*********************************************************
-}

rnLocalBindsAndThen :: HsLocalBinds GhcPs
                   -> (HsLocalBinds GhcRn -> FreeVars -> RnM (result, FreeVars))
                   -> RnM (result, FreeVars)
-- This version (a) assumes that the binding vars are *not* already in scope
--               (b) removes the binders from the free vars of the thing inside
-- The parser doesn't produce ThenBinds
rnLocalBindsAndThen :: forall result.
HsLocalBinds GhcPs
-> (HsLocalBinds (GhcPass 'Renamed)
    -> NameSet -> RnM (result, NameSet))
-> RnM (result, NameSet)
rnLocalBindsAndThen (EmptyLocalBinds XEmptyLocalBinds GhcPs GhcPs
x) HsLocalBinds (GhcPass 'Renamed) -> NameSet -> RnM (result, NameSet)
thing_inside =
  HsLocalBinds (GhcPass 'Renamed) -> NameSet -> RnM (result, NameSet)
thing_inside (forall idL idR. XEmptyLocalBinds idL idR -> HsLocalBindsLR idL idR
EmptyLocalBinds XEmptyLocalBinds GhcPs GhcPs
x) NameSet
emptyNameSet

rnLocalBindsAndThen (HsValBinds XHsValBinds GhcPs GhcPs
x HsValBinds GhcPs
val_binds) HsLocalBinds (GhcPass 'Renamed) -> NameSet -> RnM (result, NameSet)
thing_inside
  = forall result.
HsValBinds GhcPs
-> (HsValBinds (GhcPass 'Renamed)
    -> NameSet -> RnM (result, NameSet))
-> RnM (result, NameSet)
rnLocalValBindsAndThen HsValBinds GhcPs
val_binds forall a b. (a -> b) -> a -> b
$ \ HsValBinds (GhcPass 'Renamed)
val_binds' ->
      HsLocalBinds (GhcPass 'Renamed) -> NameSet -> RnM (result, NameSet)
thing_inside (forall idL idR.
XHsValBinds idL idR
-> HsValBindsLR idL idR -> HsLocalBindsLR idL idR
HsValBinds XHsValBinds GhcPs GhcPs
x HsValBinds (GhcPass 'Renamed)
val_binds')

rnLocalBindsAndThen (HsIPBinds XHsIPBinds GhcPs GhcPs
x HsIPBinds GhcPs
binds) HsLocalBinds (GhcPass 'Renamed) -> NameSet -> RnM (result, NameSet)
thing_inside = do
    (HsIPBinds (GhcPass 'Renamed)
binds',NameSet
fv_binds) <- HsIPBinds GhcPs -> RnM (HsIPBinds (GhcPass 'Renamed), NameSet)
rnIPBinds HsIPBinds GhcPs
binds
    (result
thing, NameSet
fvs_thing) <- HsLocalBinds (GhcPass 'Renamed) -> NameSet -> RnM (result, NameSet)
thing_inside (forall idL idR.
XHsIPBinds idL idR -> HsIPBinds idR -> HsLocalBindsLR idL idR
HsIPBinds XHsIPBinds GhcPs GhcPs
x HsIPBinds (GhcPass 'Renamed)
binds') NameSet
fv_binds
    forall (m :: * -> *) a. Monad m => a -> m a
return (result
thing, NameSet
fvs_thing NameSet -> NameSet -> NameSet
`plusFV` NameSet
fv_binds)

rnIPBinds :: HsIPBinds GhcPs -> RnM (HsIPBinds GhcRn, FreeVars)
rnIPBinds :: HsIPBinds GhcPs -> RnM (HsIPBinds (GhcPass 'Renamed), NameSet)
rnIPBinds (IPBinds XIPBinds GhcPs
_ [LIPBind GhcPs]
ip_binds ) = do
    ([GenLocated SrcSpanAnnA (IPBind (GhcPass 'Renamed))]
ip_binds', [NameSet]
fvs_s) <- forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM (forall a b c ann.
(a -> TcM (b, c))
-> GenLocated (SrcSpanAnn' ann) a
-> TcM (GenLocated (SrcSpanAnn' ann) b, c)
wrapLocFstMA IPBind GhcPs -> RnM (IPBind (GhcPass 'Renamed), NameSet)
rnIPBind) [LIPBind GhcPs]
ip_binds
    forall (m :: * -> *) a. Monad m => a -> m a
return (forall id. XIPBinds id -> [LIPBind id] -> HsIPBinds id
IPBinds NoExtField
noExtField [GenLocated SrcSpanAnnA (IPBind (GhcPass 'Renamed))]
ip_binds', [NameSet] -> NameSet
plusFVs [NameSet]
fvs_s)

rnIPBind :: IPBind GhcPs -> RnM (IPBind GhcRn, FreeVars)
rnIPBind :: IPBind GhcPs -> RnM (IPBind (GhcPass 'Renamed), NameSet)
rnIPBind (IPBind XCIPBind GhcPs
_ XRec GhcPs HsIPName
n LHsExpr GhcPs
expr) = do
    (GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed))
expr',NameSet
fvExpr) <- LHsExpr GhcPs -> RnM (LHsExpr (GhcPass 'Renamed), NameSet)
rnLExpr LHsExpr GhcPs
expr
    forall (m :: * -> *) a. Monad m => a -> m a
return (forall id.
XCIPBind id -> XRec id HsIPName -> LHsExpr id -> IPBind id
IPBind NoExtField
noExtField XRec GhcPs HsIPName
n GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed))
expr', NameSet
fvExpr)

{-
************************************************************************
*                                                                      *
                ValBinds
*                                                                      *
************************************************************************
-}

-- Renaming local binding groups
-- Does duplicate/shadow check
rnLocalValBindsLHS :: MiniFixityEnv
                   -> HsValBinds GhcPs
                   -> RnM ([Name], HsValBindsLR GhcRn GhcPs)
rnLocalValBindsLHS :: MiniFixityEnv
-> HsValBinds GhcPs
-> RnM ([Name], HsValBindsLR (GhcPass 'Renamed) GhcPs)
rnLocalValBindsLHS MiniFixityEnv
fix_env HsValBinds GhcPs
binds
  = do { HsValBindsLR (GhcPass 'Renamed) GhcPs
binds' <- NameMaker
-> HsValBinds GhcPs -> RnM (HsValBindsLR (GhcPass 'Renamed) GhcPs)
rnValBindsLHS (MiniFixityEnv -> NameMaker
localRecNameMaker MiniFixityEnv
fix_env) HsValBinds GhcPs
binds

         -- Check for duplicates and shadowing
         -- Must do this *after* renaming the patterns
         -- See Note [Collect binders only after renaming] in GHC.Hs.Utils

         -- We need to check for dups here because we
         -- don't don't bind all of the variables from the ValBinds at once
         -- with bindLocatedLocals any more.
         --
         -- Note that we don't want to do this at the top level, since
         -- sorting out duplicates and shadowing there happens elsewhere.
         -- The behavior is even different. For example,
         --   import A(f)
         --   f = ...
         -- should not produce a shadowing warning (but it will produce
         -- an ambiguity warning if you use f), but
         --   import A(f)
         --   g = let f = ... in f
         -- should.
       ; let bound_names :: [IdP (GhcPass 'Renamed)]
bound_names = forall (idL :: Pass) idR.
CollectPass (GhcPass idL) =>
CollectFlag (GhcPass idL)
-> HsValBindsLR (GhcPass idL) idR -> [IdP (GhcPass idL)]
collectHsValBinders forall p. CollectFlag p
CollNoDictBinders HsValBindsLR (GhcPass 'Renamed) GhcPs
binds'
             -- There should be only Ids, but if there are any bogus
             -- pattern synonyms, we'll collect them anyway, so that
             -- we don't generate subsequent out-of-scope messages
       ; (GlobalRdrEnv, LocalRdrEnv)
envs <- TcRn (GlobalRdrEnv, LocalRdrEnv)
getRdrEnvs
       ; (GlobalRdrEnv, LocalRdrEnv) -> [Name] -> RnM ()
checkDupAndShadowedNames (GlobalRdrEnv, LocalRdrEnv)
envs [IdP (GhcPass 'Renamed)]
bound_names

       ; forall (m :: * -> *) a. Monad m => a -> m a
return ([IdP (GhcPass 'Renamed)]
bound_names, HsValBindsLR (GhcPass 'Renamed) GhcPs
binds') }

-- renames the left-hand sides
-- generic version used both at the top level and for local binds
-- does some error checking, but not what gets done elsewhere at the top level
rnValBindsLHS :: NameMaker
              -> HsValBinds GhcPs
              -> RnM (HsValBindsLR GhcRn GhcPs)
rnValBindsLHS :: NameMaker
-> HsValBinds GhcPs -> RnM (HsValBindsLR (GhcPass 'Renamed) GhcPs)
rnValBindsLHS NameMaker
topP (ValBinds XValBinds GhcPs GhcPs
x LHsBindsLR GhcPs GhcPs
mbinds [LSig GhcPs]
sigs)
  = do { Bag (GenLocated SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) GhcPs))
mbinds' <- forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Bag a -> m (Bag b)
mapBagM (forall a b ann.
(a -> TcM b)
-> GenLocated (SrcSpanAnn' ann) a
-> TcRn (GenLocated (SrcSpanAnn' ann) b)
wrapLocMA (NameMaker
-> SDoc
-> HsBindLR GhcPs GhcPs
-> RnM (HsBindLR (GhcPass 'Renamed) GhcPs)
rnBindLHS NameMaker
topP SDoc
doc)) LHsBindsLR GhcPs GhcPs
mbinds
       ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall idL idR.
XValBinds idL idR
-> LHsBindsLR idL idR -> [LSig idR] -> HsValBindsLR idL idR
ValBinds XValBinds GhcPs GhcPs
x Bag (GenLocated SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) GhcPs))
mbinds' [LSig GhcPs]
sigs }
  where
    bndrs :: [IdP GhcPs]
bndrs = forall p idR.
CollectPass p =>
CollectFlag p -> LHsBindsLR p idR -> [IdP p]
collectHsBindsBinders forall p. CollectFlag p
CollNoDictBinders LHsBindsLR GhcPs GhcPs
mbinds
    doc :: SDoc
doc   = String -> SDoc
text String
"In the binding group for:" SDoc -> SDoc -> SDoc
<+> forall a. (a -> SDoc) -> [a] -> SDoc
pprWithCommas forall a. Outputable a => a -> SDoc
ppr [IdP GhcPs]
bndrs

rnValBindsLHS NameMaker
_ HsValBinds GhcPs
b = forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"rnValBindsLHSFromDoc" (forall a. Outputable a => a -> SDoc
ppr HsValBinds GhcPs
b)

-- General version used both from the top-level and for local things
-- Assumes the LHS vars are in scope
--
-- Does not bind the local fixity declarations
rnValBindsRHS :: HsSigCtxt
              -> HsValBindsLR GhcRn GhcPs
              -> RnM (HsValBinds GhcRn, DefUses)

rnValBindsRHS :: HsSigCtxt
-> HsValBindsLR (GhcPass 'Renamed) GhcPs
-> RnM (HsValBinds (GhcPass 'Renamed), DefUses)
rnValBindsRHS HsSigCtxt
ctxt (ValBinds XValBinds (GhcPass 'Renamed) GhcPs
_ LHsBindsLR (GhcPass 'Renamed) GhcPs
mbinds [LSig GhcPs]
sigs)
  = do { ([GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
sigs', NameSet
sig_fvs) <- HsSigCtxt
-> [LSig GhcPs] -> RnM ([LSig (GhcPass 'Renamed)], NameSet)
renameSigs HsSigCtxt
ctxt [LSig GhcPs]
sigs
       ; Bag
  (GenLocated
     SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)),
   [Name], NameSet)
binds_w_dus <- forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Bag a -> m (Bag b)
mapBagM ((Name -> [Name])
-> LHsBindLR (GhcPass 'Renamed) GhcPs
-> RnM (LHsBind (GhcPass 'Renamed), [Name], NameSet)
rnLBind ([LSig (GhcPass 'Renamed)] -> Name -> [Name]
mkScopedTvFn [GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
sigs')) LHsBindsLR (GhcPass 'Renamed) GhcPs
mbinds
       ; let !([(RecFlag, LHsBinds (GhcPass 'Renamed))]
anal_binds, DefUses
anal_dus) = Bag (LHsBind (GhcPass 'Renamed), [Name], NameSet)
-> ([(RecFlag, LHsBinds (GhcPass 'Renamed))], DefUses)
depAnalBinds Bag
  (GenLocated
     SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)),
   [Name], NameSet)
binds_w_dus

       ; let patsyn_fvs :: NameSet
patsyn_fvs = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (NameSet -> NameSet -> NameSet
unionNameSet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall idL idR. PatSynBind idL idR -> XPSB idL idR
psb_ext) NameSet
emptyNameSet forall a b. (a -> b) -> a -> b
$
                          forall id.
UnXRec id =>
[(RecFlag, LHsBinds id)] -> [PatSynBind id id]
getPatSynBinds [(RecFlag, LHsBinds (GhcPass 'Renamed))]
anal_binds
                -- The uses in binds_w_dus for PatSynBinds do not include
                -- variables used in the patsyn builders; see
                -- Note [Pattern synonym builders don't yield dependencies]
                -- But psb_fvs /does/ include those builder fvs.  So we
                -- add them back in here to avoid bogus warnings about
                -- unused variables (#12548)

             valbind'_dus :: DefUses
valbind'_dus = DefUses
anal_dus DefUses -> DefUses -> DefUses
`plusDU` NameSet -> DefUses
usesOnly NameSet
sig_fvs
                                     DefUses -> DefUses -> DefUses
`plusDU` NameSet -> DefUses
usesOnly NameSet
patsyn_fvs
                            -- Put the sig uses *after* the bindings
                            -- so that the binders are removed from
                            -- the uses in the sigs

        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall idL idR. XXValBindsLR idL idR -> HsValBindsLR idL idR
XValBindsLR (forall idL.
[(RecFlag, LHsBinds idL)]
-> [LSig (GhcPass 'Renamed)] -> NHsValBindsLR idL
NValBinds [(RecFlag, LHsBinds (GhcPass 'Renamed))]
anal_binds [GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
sigs'), DefUses
valbind'_dus) }

rnValBindsRHS HsSigCtxt
_ HsValBindsLR (GhcPass 'Renamed) GhcPs
b = forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"rnValBindsRHS" (forall a. Outputable a => a -> SDoc
ppr HsValBindsLR (GhcPass 'Renamed) GhcPs
b)

-- Wrapper for local binds
--
-- The *client* of this function is responsible for checking for unused binders;
-- it doesn't (and can't: we don't have the thing inside the binds) happen here
--
-- The client is also responsible for bringing the fixities into scope
rnLocalValBindsRHS :: NameSet  -- names bound by the LHSes
                   -> HsValBindsLR GhcRn GhcPs
                   -> RnM (HsValBinds GhcRn, DefUses)
rnLocalValBindsRHS :: NameSet
-> HsValBindsLR (GhcPass 'Renamed) GhcPs
-> RnM (HsValBinds (GhcPass 'Renamed), DefUses)
rnLocalValBindsRHS NameSet
bound_names HsValBindsLR (GhcPass 'Renamed) GhcPs
binds
  = HsSigCtxt
-> HsValBindsLR (GhcPass 'Renamed) GhcPs
-> RnM (HsValBinds (GhcPass 'Renamed), DefUses)
rnValBindsRHS (NameSet -> HsSigCtxt
LocalBindCtxt NameSet
bound_names) HsValBindsLR (GhcPass 'Renamed) GhcPs
binds

-- for local binds
-- wrapper that does both the left- and right-hand sides
--
-- here there are no local fixity decls passed in;
-- the local fixity decls come from the ValBinds sigs
rnLocalValBindsAndThen
  :: HsValBinds GhcPs
  -> (HsValBinds GhcRn -> FreeVars -> RnM (result, FreeVars))
  -> RnM (result, FreeVars)
rnLocalValBindsAndThen :: forall result.
HsValBinds GhcPs
-> (HsValBinds (GhcPass 'Renamed)
    -> NameSet -> RnM (result, NameSet))
-> RnM (result, NameSet)
rnLocalValBindsAndThen binds :: HsValBinds GhcPs
binds@(ValBinds XValBinds GhcPs GhcPs
_ LHsBindsLR GhcPs GhcPs
_ [LSig GhcPs]
sigs) HsValBinds (GhcPass 'Renamed) -> NameSet -> RnM (result, NameSet)
thing_inside
 = do   {     -- (A) Create the local fixity environment
          MiniFixityEnv
new_fixities <- [LFixitySig GhcPs] -> RnM MiniFixityEnv
makeMiniFixityEnv [ forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc FixitySig GhcPs
sig
                                            | L SrcSpanAnnA
loc (FixSig XFixSig GhcPs
_ FixitySig GhcPs
sig) <- [LSig GhcPs]
sigs]

              -- (B) Rename the LHSes
        ; ([Name]
bound_names, HsValBindsLR (GhcPass 'Renamed) GhcPs
new_lhs) <- MiniFixityEnv
-> HsValBinds GhcPs
-> RnM ([Name], HsValBindsLR (GhcPass 'Renamed) GhcPs)
rnLocalValBindsLHS MiniFixityEnv
new_fixities HsValBinds GhcPs
binds

              --     ...and bring them (and their fixities) into scope
        ; forall a. [Name] -> RnM (a, NameSet) -> RnM (a, NameSet)
bindLocalNamesFV [Name]
bound_names              forall a b. (a -> b) -> a -> b
$
          forall a. MiniFixityEnv -> [Name] -> RnM a -> RnM a
addLocalFixities MiniFixityEnv
new_fixities [Name]
bound_names forall a b. (a -> b) -> a -> b
$ do

        {      -- (C) Do the RHS and thing inside
          (HsValBinds (GhcPass 'Renamed)
binds', DefUses
dus) <- NameSet
-> HsValBindsLR (GhcPass 'Renamed) GhcPs
-> RnM (HsValBinds (GhcPass 'Renamed), DefUses)
rnLocalValBindsRHS ([Name] -> NameSet
mkNameSet [Name]
bound_names) HsValBindsLR (GhcPass 'Renamed) GhcPs
new_lhs
        ; (result
result, NameSet
result_fvs) <- HsValBinds (GhcPass 'Renamed) -> NameSet -> RnM (result, NameSet)
thing_inside HsValBinds (GhcPass 'Renamed)
binds' (DefUses -> NameSet
allUses DefUses
dus)

                -- Report unused bindings based on the (accurate)
                -- findUses.  E.g.
                --      let x = x in 3
                -- should report 'x' unused
        ; let real_uses :: NameSet
real_uses = DefUses -> NameSet -> NameSet
findUses DefUses
dus NameSet
result_fvs
              -- Insert fake uses for variables introduced implicitly by
              -- wildcards (#4404)
              rec_uses :: [(SrcSpan, [Name])]
rec_uses = forall (idR :: Pass).
HsValBindsLR (GhcPass 'Renamed) (GhcPass idR)
-> [(SrcSpan, [Name])]
hsValBindsImplicits HsValBinds (GhcPass 'Renamed)
binds'
              implicit_uses :: NameSet
implicit_uses = [Name] -> NameSet
mkNameSet forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a b. (a, b) -> b
snd
                                        forall a b. (a -> b) -> a -> b
$ [(SrcSpan, [Name])]
rec_uses
        ; forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\(SrcSpan
loc, [Name]
ns) ->
                    SrcSpan -> NameSet -> Maybe [Name] -> RnM ()
checkUnusedRecordWildcard SrcSpan
loc NameSet
real_uses (forall a. a -> Maybe a
Just [Name]
ns))
                [(SrcSpan, [Name])]
rec_uses
        ; [Name] -> NameSet -> RnM ()
warnUnusedLocalBinds [Name]
bound_names
                                      (NameSet
real_uses NameSet -> NameSet -> NameSet
`unionNameSet` NameSet
implicit_uses)

        ; let
            -- The variables "used" in the val binds are:
            --   (1) the uses of the binds (allUses)
            --   (2) the FVs of the thing-inside
            all_uses :: NameSet
all_uses = DefUses -> NameSet
allUses DefUses
dus NameSet -> NameSet -> NameSet
`plusFV` NameSet
result_fvs
                -- Note [Unused binding hack]
                -- ~~~~~~~~~~~~~~~~~~~~~~~~~~
                -- Note that *in contrast* to the above reporting of
                -- unused bindings, (1) above uses duUses to return *all*
                -- the uses, even if the binding is unused.  Otherwise consider:
                --      x = 3
                --      y = let p = x in 'x'    -- NB: p not used
                -- If we don't "see" the dependency of 'y' on 'x', we may put the
                -- bindings in the wrong order, and the type checker will complain
                -- that x isn't in scope
                --
                -- But note that this means we won't report 'x' as unused,
                -- whereas we would if we had { x = 3; p = x; y = 'x' }

        ; forall (m :: * -> *) a. Monad m => a -> m a
return (result
result, NameSet
all_uses) }}
                -- The bound names are pruned out of all_uses
                -- by the bindLocalNamesFV call above

rnLocalValBindsAndThen HsValBinds GhcPs
bs HsValBinds (GhcPass 'Renamed) -> NameSet -> RnM (result, NameSet)
_ = forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"rnLocalValBindsAndThen" (forall a. Outputable a => a -> SDoc
ppr HsValBinds GhcPs
bs)


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

-- renaming a single bind

rnBindLHS :: NameMaker
          -> SDoc
          -> HsBind GhcPs
          -- returns the renamed left-hand side,
          -- and the FreeVars *of the LHS*
          -- (i.e., any free variables of the pattern)
          -> RnM (HsBindLR GhcRn GhcPs)

rnBindLHS :: NameMaker
-> SDoc
-> HsBindLR GhcPs GhcPs
-> RnM (HsBindLR (GhcPass 'Renamed) GhcPs)
rnBindLHS NameMaker
name_maker SDoc
_ bind :: HsBindLR GhcPs GhcPs
bind@(PatBind { pat_lhs :: forall idL idR. HsBindLR idL idR -> LPat idL
pat_lhs = LPat GhcPs
pat })
  = do
      -- we don't actually use the FV processing of rnPatsAndThen here
      (GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
pat',NameSet
pat'_fvs) <- NameMaker -> LPat GhcPs -> RnM (LPat (GhcPass 'Renamed), NameSet)
rnBindPat NameMaker
name_maker LPat GhcPs
pat
      forall (m :: * -> *) a. Monad m => a -> m a
return (HsBindLR GhcPs GhcPs
bind { pat_lhs :: LPat (GhcPass 'Renamed)
pat_lhs = GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
pat', pat_ext :: XPatBind (GhcPass 'Renamed) GhcPs
pat_ext = NameSet
pat'_fvs })
                -- We temporarily store the pat's FVs in bind_fvs;
                -- gets updated to the FVs of the whole bind
                -- when doing the RHS below

rnBindLHS NameMaker
name_maker SDoc
_ bind :: HsBindLR GhcPs GhcPs
bind@(FunBind { fun_id :: forall idL idR. HsBindLR idL idR -> LIdP idL
fun_id = LIdP GhcPs
rdr_name })
  = do { GenLocated SrcSpanAnnN Name
name <- NameMaker -> LocatedN RdrName -> RnM (GenLocated SrcSpanAnnN Name)
applyNameMaker NameMaker
name_maker LIdP GhcPs
rdr_name
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (HsBindLR GhcPs GhcPs
bind { fun_id :: LIdP (GhcPass 'Renamed)
fun_id = GenLocated SrcSpanAnnN Name
name
                      , fun_ext :: XFunBind (GhcPass 'Renamed) GhcPs
fun_ext = NoExtField
noExtField }) }

rnBindLHS NameMaker
name_maker SDoc
_ (PatSynBind XPatSynBind GhcPs GhcPs
x psb :: PatSynBind GhcPs GhcPs
psb@PSB{ psb_id :: forall idL idR. PatSynBind idL idR -> LIdP idL
psb_id = LIdP GhcPs
rdrname })
  | NameMaker -> Bool
isTopRecNameMaker NameMaker
name_maker
  = do { forall a b ann.
(a -> TcM b) -> GenLocated (SrcSpanAnn' ann) a -> TcM b
addLocMA RdrName -> RnM ()
checkConName LIdP GhcPs
rdrname
       ; GenLocated SrcSpanAnnN Name
name <-
           LocatedN RdrName -> RnM (GenLocated SrcSpanAnnN Name)
lookupLocatedTopConstructorRnN LIdP GhcPs
rdrname -- Should be in scope already
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall idL idR.
XPatSynBind idL idR -> PatSynBind idL idR -> HsBindLR idL idR
PatSynBind XPatSynBind GhcPs GhcPs
x PatSynBind GhcPs GhcPs
psb{ psb_ext :: XPSB (GhcPass 'Renamed) GhcPs
psb_ext = forall a. EpAnn a
noAnn, psb_id :: LIdP (GhcPass 'Renamed)
psb_id = GenLocated SrcSpanAnnN Name
name }) }

  | Bool
otherwise  -- Pattern synonym, not at top level
  = do { TcRnMessage -> RnM ()
addErr TcRnMessage
localPatternSynonymErr  -- Complain, but make up a fake
                                        -- name so that we can carry on
       ; GenLocated SrcSpanAnnN Name
name <- NameMaker -> LocatedN RdrName -> RnM (GenLocated SrcSpanAnnN Name)
applyNameMaker NameMaker
name_maker LIdP GhcPs
rdrname
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall idL idR.
XPatSynBind idL idR -> PatSynBind idL idR -> HsBindLR idL idR
PatSynBind XPatSynBind GhcPs GhcPs
x PatSynBind GhcPs GhcPs
psb{ psb_ext :: XPSB (GhcPass 'Renamed) GhcPs
psb_ext = forall a. EpAnn a
noAnn, psb_id :: LIdP (GhcPass 'Renamed)
psb_id = GenLocated SrcSpanAnnN Name
name }) }
  where
    localPatternSynonymErr :: TcRnMessage
    localPatternSynonymErr :: TcRnMessage
localPatternSynonymErr = LIdP GhcPs -> TcRnMessage
TcRnIllegalPatSynDecl LIdP GhcPs
rdrname

rnBindLHS NameMaker
_ SDoc
_ HsBindLR GhcPs GhcPs
b = forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"rnBindHS" (forall a. Outputable a => a -> SDoc
ppr HsBindLR GhcPs GhcPs
b)

rnLBind :: (Name -> [Name])      -- Signature tyvar function
        -> LHsBindLR GhcRn GhcPs
        -> RnM (LHsBind GhcRn, [Name], Uses)
rnLBind :: (Name -> [Name])
-> LHsBindLR (GhcPass 'Renamed) GhcPs
-> RnM (LHsBind (GhcPass 'Renamed), [Name], NameSet)
rnLBind Name -> [Name]
sig_fn (L SrcSpanAnnA
loc HsBindLR (GhcPass 'Renamed) GhcPs
bind)
  = forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc forall a b. (a -> b) -> a -> b
$
    do { (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)
bind', [Name]
bndrs, NameSet
dus) <- (Name -> [Name])
-> HsBindLR (GhcPass 'Renamed) GhcPs
-> RnM
     (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed), [Name], NameSet)
rnBind Name -> [Name]
sig_fn HsBindLR (GhcPass 'Renamed) GhcPs
bind
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)
bind', [Name]
bndrs, NameSet
dus) }

-- assumes the left-hands-side vars are in scope
rnBind :: (Name -> [Name])        -- Signature tyvar function
       -> HsBindLR GhcRn GhcPs
       -> RnM (HsBind GhcRn, [Name], Uses)
rnBind :: (Name -> [Name])
-> HsBindLR (GhcPass 'Renamed) GhcPs
-> RnM
     (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed), [Name], NameSet)
rnBind Name -> [Name]
_ bind :: HsBindLR (GhcPass 'Renamed) GhcPs
bind@(PatBind { pat_lhs :: forall idL idR. HsBindLR idL idR -> LPat idL
pat_lhs = LPat (GhcPass 'Renamed)
pat
                       , pat_rhs :: forall idL idR. HsBindLR idL idR -> GRHSs idR (LHsExpr idR)
pat_rhs = GRHSs GhcPs (LHsExpr GhcPs)
grhss
                                   -- pat fvs were stored in bind_fvs
                                   -- after processing the LHS
                       , pat_ext :: forall idL idR. HsBindLR idL idR -> XPatBind idL idR
pat_ext = XPatBind (GhcPass 'Renamed) GhcPs
pat_fvs })
  = do  { Module
mod <- forall (m :: * -> *). HasModule m => m Module
getModule
        ; (GRHSs
  (GhcPass 'Renamed)
  (GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed)))
grhss', NameSet
rhs_fvs) <- forall (body :: * -> *).
AnnoBody body =>
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> GRHSs GhcPs (LocatedA (body GhcPs))
-> RnM
     (GRHSs (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnGRHSs forall p. HsMatchContext p
PatBindRhs LHsExpr GhcPs -> RnM (LHsExpr (GhcPass 'Renamed), NameSet)
rnLExpr GRHSs GhcPs (LHsExpr GhcPs)
grhss

                -- No scoped type variables for pattern bindings
        ; let all_fvs :: NameSet
all_fvs = XPatBind (GhcPass 'Renamed) GhcPs
pat_fvs NameSet -> NameSet -> NameSet
`plusFV` NameSet
rhs_fvs
              fvs' :: NameSet
fvs'    = (Name -> Bool) -> NameSet -> NameSet
filterNameSet (Module -> Name -> Bool
nameIsLocalOrFrom Module
mod) NameSet
all_fvs
                -- Keep locally-defined Names
                -- As well as dependency analysis, we need these for the
                -- MonoLocalBinds test in GHC.Tc.Gen.Bind.decideGeneralisationPlan
              bndrs :: [IdP (GhcPass 'Renamed)]
bndrs = forall p. CollectPass p => CollectFlag p -> LPat p -> [IdP p]
collectPatBinders forall p. CollectFlag p
CollNoDictBinders LPat (GhcPass 'Renamed)
pat
              bind' :: HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)
bind' = HsBindLR (GhcPass 'Renamed) GhcPs
bind { pat_rhs :: GRHSs (GhcPass 'Renamed) (LHsExpr (GhcPass 'Renamed))
pat_rhs  = GRHSs
  (GhcPass 'Renamed)
  (GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed)))
grhss'
                           , pat_ext :: XPatBind (GhcPass 'Renamed) (GhcPass 'Renamed)
pat_ext = NameSet
fvs' }

              ok_nobind_pat :: Bool
ok_nobind_pat
                  = -- See Note [Pattern bindings that bind no variables]
                    case forall l e. GenLocated l e -> e
unLoc LPat (GhcPass 'Renamed)
pat of
                       WildPat {}   -> Bool
True
                       BangPat {}   -> Bool
True -- #9127, #13646
                       SplicePat {} -> Bool
True
                       Pat (GhcPass 'Renamed)
_            -> Bool
False

        -- Warn if the pattern binds no variables
        -- See Note [Pattern bindings that bind no variables]
        ; forall gbl lcl.
WarningFlag -> TcRnIf gbl lcl () -> TcRnIf gbl lcl ()
whenWOptM WarningFlag
Opt_WarnUnusedPatternBinds forall a b. (a -> b) -> a -> b
$
          forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [IdP (GhcPass 'Renamed)]
bndrs Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
ok_nobind_pat) forall a b. (a -> b) -> a -> b
$
          TcRnMessage -> RnM ()
addTcRnDiagnostic (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed) -> TcRnMessage
TcRnUnusedPatternBinds HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)
bind')

        ; NameSet
fvs' seq :: forall a b. a -> b -> b
`seq` -- See Note [Free-variable space leak]
          forall (m :: * -> *) a. Monad m => a -> m a
return (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)
bind', [IdP (GhcPass 'Renamed)]
bndrs, NameSet
all_fvs) }

rnBind Name -> [Name]
sig_fn bind :: HsBindLR (GhcPass 'Renamed) GhcPs
bind@(FunBind { fun_id :: forall idL idR. HsBindLR idL idR -> LIdP idL
fun_id = LIdP (GhcPass 'Renamed)
name
                            , fun_matches :: forall idL idR. HsBindLR idL idR -> MatchGroup idR (LHsExpr idR)
fun_matches = MatchGroup GhcPs (LHsExpr GhcPs)
matches })
       -- invariant: no free vars here when it's a FunBind
  = do  { let plain_name :: Name
plain_name = forall l e. GenLocated l e -> e
unLoc LIdP (GhcPass 'Renamed)
name

        ; (MatchGroup
  (GhcPass 'Renamed)
  (GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed)))
matches', NameSet
rhs_fvs) <- forall a. [Name] -> RnM (a, NameSet) -> RnM (a, NameSet)
bindSigTyVarsFV (Name -> [Name]
sig_fn Name
plain_name) forall a b. (a -> b) -> a -> b
$
                                -- bindSigTyVars tests for LangExt.ScopedTyVars
                                 forall (body :: * -> *).
(Outputable (body GhcPs), AnnoBody body) =>
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> MatchGroup GhcPs (LocatedA (body GhcPs))
-> RnM
     (MatchGroup
        (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnMatchGroup (forall p. LIdP p -> HsMatchContext p
mkPrefixFunRhs LIdP (GhcPass 'Renamed)
name)
                                              LHsExpr GhcPs -> RnM (LHsExpr (GhcPass 'Renamed), NameSet)
rnLExpr MatchGroup GhcPs (LHsExpr GhcPs)
matches
        ; let is_infix :: Bool
is_infix = forall id1 id2. UnXRec id2 => HsBindLR id1 id2 -> Bool
isInfixFunBind HsBindLR (GhcPass 'Renamed) GhcPs
bind
        ; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
is_infix forall a b. (a -> b) -> a -> b
$ forall body. Name -> MatchGroup (GhcPass 'Renamed) body -> RnM ()
checkPrecMatch Name
plain_name MatchGroup
  (GhcPass 'Renamed)
  (GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed)))
matches'

        ; Module
mod <- forall (m :: * -> *). HasModule m => m Module
getModule
        ; let fvs' :: NameSet
fvs' = (Name -> Bool) -> NameSet -> NameSet
filterNameSet (Module -> Name -> Bool
nameIsLocalOrFrom Module
mod) NameSet
rhs_fvs
                -- Keep locally-defined Names
                -- As well as dependency analysis, we need these for the
                -- MonoLocalBinds test in GHC.Tc.Gen.Bind.decideGeneralisationPlan

        ; NameSet
fvs' seq :: forall a b. a -> b -> b
`seq` -- See Note [Free-variable space leak]
          forall (m :: * -> *) a. Monad m => a -> m a
return (HsBindLR (GhcPass 'Renamed) GhcPs
bind { fun_matches :: MatchGroup (GhcPass 'Renamed) (LHsExpr (GhcPass 'Renamed))
fun_matches = MatchGroup
  (GhcPass 'Renamed)
  (GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed)))
matches'
                       , fun_ext :: XFunBind (GhcPass 'Renamed) (GhcPass 'Renamed)
fun_ext     = NameSet
fvs' },
                  [Name
plain_name], NameSet
rhs_fvs)
      }

rnBind Name -> [Name]
sig_fn (PatSynBind XPatSynBind (GhcPass 'Renamed) GhcPs
x PatSynBind (GhcPass 'Renamed) GhcPs
bind)
  = do  { (PatSynBind (GhcPass 'Renamed) (GhcPass 'Renamed)
bind', [Name]
name, NameSet
fvs) <- (Name -> [Name])
-> PatSynBind (GhcPass 'Renamed) GhcPs
-> RnM
     (PatSynBind (GhcPass 'Renamed) (GhcPass 'Renamed), [Name], NameSet)
rnPatSynBind Name -> [Name]
sig_fn PatSynBind (GhcPass 'Renamed) GhcPs
bind
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall idL idR.
XPatSynBind idL idR -> PatSynBind idL idR -> HsBindLR idL idR
PatSynBind XPatSynBind (GhcPass 'Renamed) GhcPs
x PatSynBind (GhcPass 'Renamed) (GhcPass 'Renamed)
bind', [Name]
name, NameSet
fvs) }

rnBind Name -> [Name]
_ HsBindLR (GhcPass 'Renamed) GhcPs
b = forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"rnBind" (forall a. Outputable a => a -> SDoc
ppr HsBindLR (GhcPass 'Renamed) GhcPs
b)

{- Note [Pattern bindings that bind no variables]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Generally, we want to warn about pattern bindings like
  Just _ = e
because they don't do anything!  But we have three exceptions:

* A wildcard pattern
       _ = rhs
  which (a) is not that different from  _v = rhs
        (b) is sometimes used to give a type sig for,
            or an occurrence of, a variable on the RHS

* A strict pattern binding; that is, one with an outermost bang
     !Just _ = e
  This can fail, so unlike the lazy variant, it is not a no-op.
  Moreover, #13646 argues that even for single constructor
  types, you might want to write the constructor.  See also #9127.

* A splice pattern
      $(th-lhs) = rhs
   It is impossible to determine whether or not th-lhs really
   binds any variable. We should disable the warning for any pattern
   which contain splices, but that is a more expensive check.

Note [Free-variable space leak]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We have
    fvs' = trim fvs
and we seq fvs' before turning it as part of a record.

The reason is that trim is sometimes something like
    \xs -> intersectNameSet (mkNameSet bound_names) xs
and we don't want to retain the list bound_names. This showed up in
trac ticket #1136.
-}

{- *********************************************************************
*                                                                      *
          Dependency analysis and other support functions
*                                                                      *
********************************************************************* -}

depAnalBinds :: Bag (LHsBind GhcRn, [Name], Uses)
             -> ([(RecFlag, LHsBinds GhcRn)], DefUses)
-- Dependency analysis; this is important so that
-- unused-binding reporting is accurate
depAnalBinds :: Bag (LHsBind (GhcPass 'Renamed), [Name], NameSet)
-> ([(RecFlag, LHsBinds (GhcPass 'Renamed))], DefUses)
depAnalBinds Bag (LHsBind (GhcPass 'Renamed), [Name], NameSet)
binds_w_dus
  = (forall a b. (a -> b) -> [a] -> [b]
map forall {a} {b} {c}. SCC (a, b, c) -> (RecFlag, Bag a)
get_binds [SCC
   (GenLocated
      SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)),
    [Name], NameSet)]
sccs, forall a. [a] -> OrdList a
toOL forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall {a}. SCC (a, [Name], NameSet) -> (Maybe NameSet, NameSet)
get_du [SCC
   (GenLocated
      SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)),
    [Name], NameSet)]
sccs)
  where
    sccs :: [SCC
   (GenLocated
      SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)),
    [Name], NameSet)]
sccs = forall node.
(node -> [Name]) -> (node -> [Name]) -> [node] -> [SCC node]
depAnal (\(GenLocated
  SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed))
_, [Name]
defs, NameSet
_) -> [Name]
defs)
                   (\(GenLocated
  SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed))
_, [Name]
_, NameSet
uses) -> forall elt. UniqSet elt -> [elt]
nonDetEltsUniqSet NameSet
uses)
                   -- It's OK to use nonDetEltsUniqSet here as explained in
                   -- Note [depAnal determinism] in GHC.Types.Name.Env.
                   (forall a. Bag a -> [a]
bagToList Bag (LHsBind (GhcPass 'Renamed), [Name], NameSet)
binds_w_dus)

    get_binds :: SCC (a, b, c) -> (RecFlag, Bag a)
get_binds (AcyclicSCC (a
bind, b
_, c
_)) = (RecFlag
NonRecursive, forall a. a -> Bag a
unitBag a
bind)
    get_binds (CyclicSCC  [(a, b, c)]
binds_w_dus)  = (RecFlag
Recursive, forall a. [a] -> Bag a
listToBag [a
b | (a
b,b
_,c
_) <- [(a, b, c)]
binds_w_dus])

    get_du :: SCC (a, [Name], NameSet) -> (Maybe NameSet, NameSet)
get_du (AcyclicSCC (a
_, [Name]
bndrs, NameSet
uses)) = (forall a. a -> Maybe a
Just ([Name] -> NameSet
mkNameSet [Name]
bndrs), NameSet
uses)
    get_du (CyclicSCC  [(a, [Name], NameSet)]
binds_w_dus)      = (forall a. a -> Maybe a
Just NameSet
defs, NameSet
uses)
        where
          defs :: NameSet
defs = [Name] -> NameSet
mkNameSet [Name
b | (a
_,[Name]
bs,NameSet
_) <- [(a, [Name], NameSet)]
binds_w_dus, Name
b <- [Name]
bs]
          uses :: NameSet
uses = [NameSet] -> NameSet
unionNameSets [NameSet
u | (a
_,[Name]
_,NameSet
u) <- [(a, [Name], NameSet)]
binds_w_dus]

---------------------
-- Bind the top-level forall'd type variables in the sigs.
-- E.g  f :: forall a. a -> a
--      f = rhs
--      The 'a' scopes over the rhs
--
-- NB: there'll usually be just one (for a function binding)
--     but if there are many, one may shadow the rest; too bad!
--      e.g  x :: forall a. [a] -> [a]
--           y :: forall a. [(a,a)] -> a
--           (x,y) = e
--      In e, 'a' will be in scope, and it'll be the one from 'y'!

mkScopedTvFn :: [LSig GhcRn] -> (Name -> [Name])
-- Return a lookup function that maps an Id Name to the names
-- of the type variables that should scope over its body.
mkScopedTvFn :: [LSig (GhcPass 'Renamed)] -> Name -> [Name]
mkScopedTvFn [LSig (GhcPass 'Renamed)]
sigs = \Name
n -> forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv NameEnv [Name]
env Name
n forall a. Maybe a -> a -> a
`orElse` []
  where
    env :: NameEnv [Name]
env = forall a.
(LSig (GhcPass 'Renamed)
 -> Maybe ([GenLocated SrcSpanAnnN Name], a))
-> [LSig (GhcPass 'Renamed)] -> NameEnv a
mkHsSigEnv LSig (GhcPass 'Renamed)
-> Maybe ([GenLocated SrcSpanAnnN Name], [Name])
get_scoped_tvs [LSig (GhcPass 'Renamed)]
sigs

    get_scoped_tvs :: LSig GhcRn -> Maybe ([LocatedN Name], [Name])
    -- Returns (binders, scoped tvs for those binders)
    get_scoped_tvs :: LSig (GhcPass 'Renamed)
-> Maybe ([GenLocated SrcSpanAnnN Name], [Name])
get_scoped_tvs (L SrcSpanAnnA
_ (ClassOpSig XClassOpSig (GhcPass 'Renamed)
_ Bool
_ [LIdP (GhcPass 'Renamed)]
names LHsSigType (GhcPass 'Renamed)
sig_ty))
      = forall a. a -> Maybe a
Just ([LIdP (GhcPass 'Renamed)]
names, LHsSigType (GhcPass 'Renamed) -> [Name]
hsScopedTvs LHsSigType (GhcPass 'Renamed)
sig_ty)
    get_scoped_tvs (L SrcSpanAnnA
_ (TypeSig XTypeSig (GhcPass 'Renamed)
_ [LIdP (GhcPass 'Renamed)]
names LHsSigWcType (GhcPass 'Renamed)
sig_ty))
      = forall a. a -> Maybe a
Just ([LIdP (GhcPass 'Renamed)]
names, LHsSigWcType (GhcPass 'Renamed) -> [Name]
hsWcScopedTvs LHsSigWcType (GhcPass 'Renamed)
sig_ty)
    get_scoped_tvs (L SrcSpanAnnA
_ (PatSynSig XPatSynSig (GhcPass 'Renamed)
_ [LIdP (GhcPass 'Renamed)]
names LHsSigType (GhcPass 'Renamed)
sig_ty))
      = forall a. a -> Maybe a
Just ([LIdP (GhcPass 'Renamed)]
names, LHsSigType (GhcPass 'Renamed) -> [Name]
hsScopedTvs LHsSigType (GhcPass 'Renamed)
sig_ty)
    get_scoped_tvs LSig (GhcPass 'Renamed)
_ = forall a. Maybe a
Nothing

-- Process the fixity declarations, making a FastString -> (Located Fixity) map
-- (We keep the location around for reporting duplicate fixity declarations.)
--
-- Checks for duplicates, but not that only locally defined things are fixed.
-- Note: for local fixity declarations, duplicates would also be checked in
--       check_sigs below.  But we also use this function at the top level.

makeMiniFixityEnv :: [LFixitySig GhcPs] -> RnM MiniFixityEnv

makeMiniFixityEnv :: [LFixitySig GhcPs] -> RnM MiniFixityEnv
makeMiniFixityEnv [LFixitySig GhcPs]
decls = forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM MiniFixityEnv -> LFixitySig GhcPs -> RnM MiniFixityEnv
add_one_sig forall a. FastStringEnv a
emptyFsEnv [LFixitySig GhcPs]
decls
 where
   add_one_sig :: MiniFixityEnv -> LFixitySig GhcPs -> RnM MiniFixityEnv
   add_one_sig :: MiniFixityEnv -> LFixitySig GhcPs -> RnM MiniFixityEnv
add_one_sig MiniFixityEnv
env (L SrcSpanAnnA
loc (FixitySig XFixitySig GhcPs
_ [LIdP GhcPs]
names Fixity
fixity)) =
     forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldlM forall {e}.
FastStringEnv (GenLocated SrcSpan e)
-> (SrcSpan, SrcSpan, RdrName, e)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (FastStringEnv (GenLocated SrcSpan e))
add_one MiniFixityEnv
env [ (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
loc,forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnN
name_loc,RdrName
name,Fixity
fixity)
                        | L SrcSpanAnnN
name_loc RdrName
name <- [LIdP GhcPs]
names ]

   add_one :: FastStringEnv (GenLocated SrcSpan e)
-> (SrcSpan, SrcSpan, RdrName, e)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (FastStringEnv (GenLocated SrcSpan e))
add_one FastStringEnv (GenLocated SrcSpan e)
env (SrcSpan
loc, SrcSpan
name_loc, RdrName
name,e
fixity) = do
     { -- this fixity decl is a duplicate iff
       -- the ReaderName's OccName's FastString is already in the env
       -- (we only need to check the local fix_env because
       --  definitions of non-local will be caught elsewhere)
       let { fs :: FastString
fs = OccName -> FastString
occNameFS (RdrName -> OccName
rdrNameOcc RdrName
name)
           ; fix_item :: GenLocated SrcSpan e
fix_item = forall l e. l -> e -> GenLocated l e
L SrcSpan
loc e
fixity };

       case forall a. FastStringEnv a -> FastString -> Maybe a
lookupFsEnv FastStringEnv (GenLocated SrcSpan e)
env FastString
fs of
         Maybe (GenLocated SrcSpan e)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. FastStringEnv a -> FastString -> a -> FastStringEnv a
extendFsEnv FastStringEnv (GenLocated SrcSpan e)
env FastString
fs GenLocated SrcSpan e
fix_item
         Just (L SrcSpan
loc' e
_) -> do
           { forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
loc forall a b. (a -> b) -> a -> b
$
             SrcSpan -> TcRnMessage -> RnM ()
addErrAt SrcSpan
name_loc (SrcSpan -> RdrName -> TcRnMessage
dupFixityDecl SrcSpan
loc' RdrName
name)
           ; forall (m :: * -> *) a. Monad m => a -> m a
return FastStringEnv (GenLocated SrcSpan e)
env}
     }

dupFixityDecl :: SrcSpan -> RdrName -> TcRnMessage
dupFixityDecl :: SrcSpan -> RdrName -> TcRnMessage
dupFixityDecl SrcSpan
loc RdrName
rdr_name
  = forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints forall a b. (a -> b) -> a -> b
$
    [SDoc] -> SDoc
vcat [String -> SDoc
text String
"Multiple fixity declarations for" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr RdrName
rdr_name),
          String -> SDoc
text String
"also at " SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr SrcSpan
loc]


{- *********************************************************************
*                                                                      *
                Pattern synonym bindings
*                                                                      *
********************************************************************* -}

rnPatSynBind :: (Name -> [Name])           -- Signature tyvar function
             -> PatSynBind GhcRn GhcPs
             -> RnM (PatSynBind GhcRn GhcRn, [Name], Uses)
rnPatSynBind :: (Name -> [Name])
-> PatSynBind (GhcPass 'Renamed) GhcPs
-> RnM
     (PatSynBind (GhcPass 'Renamed) (GhcPass 'Renamed), [Name], NameSet)
rnPatSynBind Name -> [Name]
sig_fn bind :: PatSynBind (GhcPass 'Renamed) GhcPs
bind@(PSB { psb_id :: forall idL idR. PatSynBind idL idR -> LIdP idL
psb_id = L SrcSpanAnnN
l Name
name
                              , psb_args :: forall idL idR. PatSynBind idL idR -> HsPatSynDetails idR
psb_args = HsPatSynDetails GhcPs
details
                              , psb_def :: forall idL idR. PatSynBind idL idR -> LPat idR
psb_def = LPat GhcPs
pat
                              , psb_dir :: forall idL idR. PatSynBind idL idR -> HsPatSynDir idR
psb_dir = HsPatSynDir GhcPs
dir })
       -- invariant: no free vars here when it's a FunBind
  = do  { Bool
pattern_synonym_ok <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.PatternSynonyms
        ; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
pattern_synonym_ok (TcRnMessage -> RnM ()
addErr TcRnMessage
patternSynonymErr)
        ; let scoped_tvs :: [Name]
scoped_tvs = Name -> [Name]
sig_fn Name
name

        ; ((GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
pat', HsConDetails
  Void
  (GenLocated SrcSpanAnnN Name)
  [RecordPatSynField (GhcPass 'Renamed)]
details'), NameSet
fvs1) <- forall a. [Name] -> RnM (a, NameSet) -> RnM (a, NameSet)
bindSigTyVarsFV [Name]
scoped_tvs forall a b. (a -> b) -> a -> b
$
                                      forall a.
HsMatchContext (GhcPass 'Renamed)
-> LPat GhcPs
-> (LPat (GhcPass 'Renamed) -> RnM (a, NameSet))
-> RnM (a, NameSet)
rnPat forall p. HsMatchContext p
PatSyn LPat GhcPs
pat forall a b. (a -> b) -> a -> b
$ \LPat (GhcPass 'Renamed)
pat' ->
         -- We check the 'RdrName's instead of the 'Name's
         -- so that the binding locations are reported
         -- from the left-hand side
            case HsPatSynDetails GhcPs
details of
               PrefixCon [Void]
_ [LIdP GhcPs]
vars ->
                   do { [LocatedN RdrName] -> RnM ()
checkDupRdrNamesN [LIdP GhcPs]
vars
                      ; [GenLocated SrcSpanAnnN Name]
names <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall {ann}.
GenLocated (SrcSpanAnn' ann) RdrName
-> TcRn (GenLocated (SrcSpanAnn' ann) Name)
lookupPatSynBndr [LIdP GhcPs]
vars
                      ; forall (m :: * -> *) a. Monad m => a -> m a
return ( (LPat (GhcPass 'Renamed)
pat', forall tyarg arg rec.
[tyarg] -> [arg] -> HsConDetails tyarg arg rec
PrefixCon [Void]
noTypeArgs [GenLocated SrcSpanAnnN Name]
names)
                               , [Name] -> NameSet
mkFVs (forall a b. (a -> b) -> [a] -> [b]
map forall l e. GenLocated l e -> e
unLoc [GenLocated SrcSpanAnnN Name]
names)) }
               InfixCon LIdP GhcPs
var1 LIdP GhcPs
var2 ->
                   do { [LocatedN RdrName] -> RnM ()
checkDupRdrNames [LIdP GhcPs
var1, LIdP GhcPs
var2]
                      ; GenLocated SrcSpanAnnN Name
name1 <- forall {ann}.
GenLocated (SrcSpanAnn' ann) RdrName
-> TcRn (GenLocated (SrcSpanAnn' ann) Name)
lookupPatSynBndr LIdP GhcPs
var1
                      ; GenLocated SrcSpanAnnN Name
name2 <- forall {ann}.
GenLocated (SrcSpanAnn' ann) RdrName
-> TcRn (GenLocated (SrcSpanAnn' ann) Name)
lookupPatSynBndr LIdP GhcPs
var2
                      -- ; checkPrecMatch -- TODO
                      ; forall (m :: * -> *) a. Monad m => a -> m a
return ( (LPat (GhcPass 'Renamed)
pat', forall tyarg arg rec. arg -> arg -> HsConDetails tyarg arg rec
InfixCon GenLocated SrcSpanAnnN Name
name1 GenLocated SrcSpanAnnN Name
name2)
                               , [Name] -> NameSet
mkFVs (forall a b. (a -> b) -> [a] -> [b]
map forall l e. GenLocated l e -> e
unLoc [GenLocated SrcSpanAnnN Name
name1, GenLocated SrcSpanAnnN Name
name2])) }
               RecCon [RecordPatSynField GhcPs]
vars ->
                   do { [LocatedN RdrName] -> RnM ()
checkDupRdrNames (forall a b. (a -> b) -> [a] -> [b]
map (forall pass. FieldOcc pass -> XRec pass RdrName
foLabel forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall pass. RecordPatSynField pass -> FieldOcc pass
recordPatSynField) [RecordPatSynField GhcPs]
vars)
                      ; [FieldLabel]
fls <- Name -> RnM [FieldLabel]
lookupConstructorFields Name
name
                      ; let fld_env :: FastStringEnv FieldLabel
fld_env = forall a. [(FastString, a)] -> FastStringEnv a
mkFsEnv [ (FieldLabelString -> FastString
field_label forall a b. (a -> b) -> a -> b
$ FieldLabel -> FieldLabelString
flLabel FieldLabel
fl, FieldLabel
fl) | FieldLabel
fl <- [FieldLabel]
fls ]
                      ; let rnRecordPatSynField :: RecordPatSynField GhcPs
-> IOEnv
     (Env TcGblEnv TcLclEnv) (RecordPatSynField (GhcPass 'Renamed))
rnRecordPatSynField
                              (RecordPatSynField { recordPatSynField :: forall pass. RecordPatSynField pass -> FieldOcc pass
recordPatSynField  = FieldOcc GhcPs
visible
                                                 , recordPatSynPatVar :: forall pass. RecordPatSynField pass -> LIdP pass
recordPatSynPatVar = LIdP GhcPs
hidden })
                              = do { let visible' :: FieldOcc (GhcPass 'Renamed)
visible' = FastStringEnv FieldLabel
-> FieldOcc GhcPs -> FieldOcc (GhcPass 'Renamed)
lookupField FastStringEnv FieldLabel
fld_env FieldOcc GhcPs
visible
                                   ; GenLocated SrcSpanAnnN Name
hidden'  <- forall {ann}.
GenLocated (SrcSpanAnn' ann) RdrName
-> TcRn (GenLocated (SrcSpanAnn' ann) Name)
lookupPatSynBndr LIdP GhcPs
hidden
                                   ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ RecordPatSynField { recordPatSynField :: FieldOcc (GhcPass 'Renamed)
recordPatSynField  = FieldOcc (GhcPass 'Renamed)
visible'
                                                                , recordPatSynPatVar :: LIdP (GhcPass 'Renamed)
recordPatSynPatVar = GenLocated SrcSpanAnnN Name
hidden' } }
                      ; [RecordPatSynField (GhcPass 'Renamed)]
names <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM RecordPatSynField GhcPs
-> IOEnv
     (Env TcGblEnv TcLclEnv) (RecordPatSynField (GhcPass 'Renamed))
rnRecordPatSynField  [RecordPatSynField GhcPs]
vars
                      ; forall (m :: * -> *) a. Monad m => a -> m a
return ( (LPat (GhcPass 'Renamed)
pat', forall tyarg arg rec. rec -> HsConDetails tyarg arg rec
RecCon [RecordPatSynField (GhcPass 'Renamed)]
names)
                               , [Name] -> NameSet
mkFVs (forall a b. (a -> b) -> [a] -> [b]
map (forall l e. GenLocated l e -> e
unLoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall pass. RecordPatSynField pass -> LIdP pass
recordPatSynPatVar) [RecordPatSynField (GhcPass 'Renamed)]
names)) }

        ; (HsPatSynDir (GhcPass 'Renamed)
dir', NameSet
fvs2) <- case HsPatSynDir GhcPs
dir of
            HsPatSynDir GhcPs
Unidirectional -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall id. HsPatSynDir id
Unidirectional, NameSet
emptyFVs)
            HsPatSynDir GhcPs
ImplicitBidirectional -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall id. HsPatSynDir id
ImplicitBidirectional, NameSet
emptyFVs)
            ExplicitBidirectional MatchGroup GhcPs (LHsExpr GhcPs)
mg ->
                do { (MatchGroup
  (GhcPass 'Renamed)
  (GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed)))
mg', NameSet
fvs) <- forall a. [Name] -> RnM (a, NameSet) -> RnM (a, NameSet)
bindSigTyVarsFV [Name]
scoped_tvs forall a b. (a -> b) -> a -> b
$
                                   forall (body :: * -> *).
(Outputable (body GhcPs), AnnoBody body) =>
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> MatchGroup GhcPs (LocatedA (body GhcPs))
-> RnM
     (MatchGroup
        (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnMatchGroup (forall p. LIdP p -> HsMatchContext p
mkPrefixFunRhs (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
l Name
name))
                                                LHsExpr GhcPs -> RnM (LHsExpr (GhcPass 'Renamed), NameSet)
rnLExpr MatchGroup GhcPs (LHsExpr GhcPs)
mg
                   ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall id. MatchGroup id (LHsExpr id) -> HsPatSynDir id
ExplicitBidirectional MatchGroup
  (GhcPass 'Renamed)
  (GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed)))
mg', NameSet
fvs) }

        ; Module
mod <- forall (m :: * -> *). HasModule m => m Module
getModule
        ; let fvs :: NameSet
fvs = NameSet
fvs1 NameSet -> NameSet -> NameSet
`plusFV` NameSet
fvs2
              fvs' :: NameSet
fvs' = (Name -> Bool) -> NameSet -> NameSet
filterNameSet (Module -> Name -> Bool
nameIsLocalOrFrom Module
mod) NameSet
fvs
                -- Keep locally-defined Names
                -- As well as dependency analysis, we need these for the
                -- MonoLocalBinds test in GHC.Tc.Gen.Bind.decideGeneralisationPlan

              bind' :: PatSynBind (GhcPass 'Renamed) (GhcPass 'Renamed)
bind' = PatSynBind (GhcPass 'Renamed) GhcPs
bind{ psb_args :: HsPatSynDetails (GhcPass 'Renamed)
psb_args = HsConDetails
  Void
  (GenLocated SrcSpanAnnN Name)
  [RecordPatSynField (GhcPass 'Renamed)]
details'
                          , psb_def :: LPat (GhcPass 'Renamed)
psb_def = GenLocated SrcSpanAnnA (Pat (GhcPass 'Renamed))
pat'
                          , psb_dir :: HsPatSynDir (GhcPass 'Renamed)
psb_dir = HsPatSynDir (GhcPass 'Renamed)
dir'
                          , psb_ext :: XPSB (GhcPass 'Renamed) (GhcPass 'Renamed)
psb_ext = NameSet
fvs' }
              selector_names :: [Name]
selector_names = case HsConDetails
  Void
  (GenLocated SrcSpanAnnN Name)
  [RecordPatSynField (GhcPass 'Renamed)]
details' of
                                 RecCon [RecordPatSynField (GhcPass 'Renamed)]
names ->
                                  forall a b. (a -> b) -> [a] -> [b]
map (forall pass. FieldOcc pass -> XCFieldOcc pass
foExt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall pass. RecordPatSynField pass -> FieldOcc pass
recordPatSynField) [RecordPatSynField (GhcPass 'Renamed)]
names
                                 HsConDetails
  Void
  (GenLocated SrcSpanAnnN Name)
  [RecordPatSynField (GhcPass 'Renamed)]
_ -> []

        ; NameSet
fvs' seq :: forall a b. a -> b -> b
`seq` -- See Note [Free-variable space leak]
          forall (m :: * -> *) a. Monad m => a -> m a
return (PatSynBind (GhcPass 'Renamed) (GhcPass 'Renamed)
bind', Name
name forall a. a -> [a] -> [a]
: [Name]
selector_names , NameSet
fvs1)
          -- Why fvs1?  See Note [Pattern synonym builders don't yield dependencies]
      }
  where
    -- See Note [Renaming pattern synonym variables]
    lookupPatSynBndr :: GenLocated (SrcSpanAnn' ann) RdrName
-> TcRn (GenLocated (SrcSpanAnn' ann) Name)
lookupPatSynBndr = forall a b ann.
(a -> TcM b)
-> GenLocated (SrcSpanAnn' ann) a
-> TcRn (GenLocated (SrcSpanAnn' ann) b)
wrapLocMA RdrName -> RnM Name
lookupLocalOccRn

    patternSynonymErr :: TcRnMessage
    patternSynonymErr :: TcRnMessage
patternSynonymErr
      = forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints forall a b. (a -> b) -> a -> b
$
        SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"Illegal pattern synonym declaration")
           Int
2 (String -> SDoc
text String
"Use -XPatternSynonyms to enable this extension")

{-
Note [Renaming pattern synonym variables]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We rename pattern synonym declaractions backwards to normal to reuse
the logic already implemented for renaming patterns.

We first rename the RHS of a declaration which brings into
scope the variables bound by the pattern (as they would be
in normal function definitions). We then lookup the variables
which we want to bind in this local environment.

It is crucial that we then only lookup in the *local* environment which
only contains the variables brought into scope by the pattern and nothing
else. Amazingly no-one encountered this bug for 3 GHC versions but
it was possible to define a pattern synonym which referenced global
identifiers and worked correctly.

```
x = 5

pattern P :: Int -> ()
pattern P x <- _

f (P x) = x

> f () = 5
```

See #13470 for the original report.

Note [Pattern synonym builders don't yield dependencies]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When renaming a pattern synonym that has an explicit builder,
references in the builder definition should not be used when
calculating dependencies. For example, consider the following pattern
synonym definition:

pattern P x <- C1 x where
  P x = f (C1 x)

f (P x) = C2 x

In this case, 'P' needs to be typechecked in two passes:

1. Typecheck the pattern definition of 'P', which fully determines the
   type of 'P'. This step doesn't require knowing anything about 'f',
   since the builder definition is not looked at.

2. Typecheck the builder definition, which needs the typechecked
   definition of 'f' to be in scope; done by calls oo tcPatSynBuilderBind
   in GHC.Tc.Gen.Bind.tcValBinds.

This behaviour is implemented in 'tcValBinds', but it crucially
depends on 'P' not being put in a recursive group with 'f' (which
would make it look like a recursive pattern synonym a la 'pattern P =
P' which is unsound and rejected).

So:
 * We do not include builder fvs in the Uses returned by rnPatSynBind
   (which is then used for dependency analysis)
 * But we /do/ include them in the psb_fvs for the PatSynBind
 * In rnValBinds we record these builder uses, to avoid bogus
   unused-variable warnings (#12548)
-}

{- *********************************************************************
*                                                                      *
                Class/instance method bindings
*                                                                      *
********************************************************************* -}

{- @rnMethodBinds@ is used for the method bindings of a class and an instance
declaration.   Like @rnBinds@ but without dependency analysis.

NOTA BENE: we record each {\em binder} of a method-bind group as a free variable.
That's crucial when dealing with an instance decl:
\begin{verbatim}
        instance Foo (T a) where
           op x = ...
\end{verbatim}
This might be the {\em sole} occurrence of @op@ for an imported class @Foo@,
and unless @op@ occurs we won't treat the type signature of @op@ in the class
decl for @Foo@ as a source of instance-decl gates.  But we should!  Indeed,
in many ways the @op@ in an instance decl is just like an occurrence, not
a binder.
-}

rnMethodBinds :: Bool                   -- True <=> is a class declaration
              -> Name                   -- Class name
              -> [Name]                 -- Type variables from the class/instance header
              -> LHsBinds GhcPs         -- Binds
              -> [LSig GhcPs]           -- and signatures/pragmas
              -> RnM (LHsBinds GhcRn, [LSig GhcRn], FreeVars)
-- Used for
--   * the default method bindings in a class decl
--   * the method bindings in an instance decl
rnMethodBinds :: Bool
-> Name
-> [Name]
-> LHsBindsLR GhcPs GhcPs
-> [LSig GhcPs]
-> RnM
     (LHsBinds (GhcPass 'Renamed), [LSig (GhcPass 'Renamed)], NameSet)
rnMethodBinds Bool
is_cls_decl Name
cls [Name]
ktv_names LHsBindsLR GhcPs GhcPs
binds [LSig GhcPs]
sigs
  = do { [LocatedN RdrName] -> RnM ()
checkDupRdrNamesN (forall idL idR. UnXRec idL => LHsBindsLR idL idR -> [LIdP idL]
collectMethodBinders LHsBindsLR GhcPs GhcPs
binds)
             -- Check that the same method is not given twice in the
             -- same instance decl      instance C T where
             --                       f x = ...
             --                       g y = ...
             --                       f x = ...
             -- We must use checkDupRdrNames because the Name of the
             -- method is the Name of the class selector, whose SrcSpan
             -- points to the class declaration; and we use rnMethodBinds
             -- for instance decls too

       -- Rename the bindings LHSs
       ; Bag (GenLocated SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) GhcPs))
binds' <- forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> b -> m b) -> b -> t a -> m b
foldrM (Bool
-> Name
-> LHsBindLR GhcPs GhcPs
-> LHsBindsLR (GhcPass 'Renamed) GhcPs
-> RnM (LHsBindsLR (GhcPass 'Renamed) GhcPs)
rnMethodBindLHS Bool
is_cls_decl Name
cls) forall a. Bag a
emptyBag LHsBindsLR GhcPs GhcPs
binds

       -- Rename the pragmas and signatures
       -- Annoyingly the type variables /are/ in scope for signatures, but
       -- /are not/ in scope in the SPECIALISE instance pramas; e.g.
       --    instance Eq a => Eq (T a) where
       --       (==) :: a -> a -> a
       --       {-# SPECIALISE instance Eq a => Eq (T [a]) #-}
       ; let ([LSig GhcPs]
spec_inst_prags, [LSig GhcPs]
other_sigs) = forall a. (a -> Bool) -> [a] -> ([a], [a])
partition forall p. UnXRec p => LSig p -> Bool
isSpecInstLSig [LSig GhcPs]
sigs
             bound_nms :: NameSet
bound_nms = [Name] -> NameSet
mkNameSet (forall p idR.
CollectPass p =>
CollectFlag p -> LHsBindsLR p idR -> [IdP p]
collectHsBindsBinders forall p. CollectFlag p
CollNoDictBinders Bag (GenLocated SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) GhcPs))
binds')
             sig_ctxt :: HsSigCtxt
sig_ctxt | Bool
is_cls_decl = Name -> HsSigCtxt
ClsDeclCtxt Name
cls
                      | Bool
otherwise   = NameSet -> HsSigCtxt
InstDeclCtxt NameSet
bound_nms
       ; ([GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
spec_inst_prags', NameSet
sip_fvs) <- HsSigCtxt
-> [LSig GhcPs] -> RnM ([LSig (GhcPass 'Renamed)], NameSet)
renameSigs HsSigCtxt
sig_ctxt [LSig GhcPs]
spec_inst_prags
       ; ([GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
other_sigs',      NameSet
sig_fvs) <- forall a. [Name] -> RnM (a, NameSet) -> RnM (a, NameSet)
bindLocalNamesFV [Name]
ktv_names forall a b. (a -> b) -> a -> b
$
                                        HsSigCtxt
-> [LSig GhcPs] -> RnM ([LSig (GhcPass 'Renamed)], NameSet)
renameSigs HsSigCtxt
sig_ctxt [LSig GhcPs]
other_sigs

       -- Rename the bindings RHSs.  Again there's an issue about whether the
       -- type variables from the class/instance head are in scope.
       -- Answer no in Haskell 2010, but yes if you have -XScopedTypeVariables
       ; (Bag
  (GenLocated
     SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)))
binds'', NameSet
bind_fvs) <- forall a. [Name] -> RnM (a, NameSet) -> RnM (a, NameSet)
bindSigTyVarsFV [Name]
ktv_names forall a b. (a -> b) -> a -> b
$
              do { Bag
  (GenLocated
     SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)),
   [Name], NameSet)
binds_w_dus <- forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Bag a -> m (Bag b)
mapBagM ((Name -> [Name])
-> LHsBindLR (GhcPass 'Renamed) GhcPs
-> RnM (LHsBind (GhcPass 'Renamed), [Name], NameSet)
rnLBind ([LSig (GhcPass 'Renamed)] -> Name -> [Name]
mkScopedTvFn [GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
other_sigs')) Bag (GenLocated SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) GhcPs))
binds'
                 ; let bind_fvs :: NameSet
bind_fvs = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\(GenLocated
  SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed))
_,[Name]
_,NameSet
fv1) NameSet
fv2 -> NameSet
fv1 NameSet -> NameSet -> NameSet
`plusFV` NameSet
fv2)
                                           NameSet
emptyFVs Bag
  (GenLocated
     SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)),
   [Name], NameSet)
binds_w_dus
                 ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (a -> b) -> Bag a -> Bag b
mapBag forall a b c. (a, b, c) -> a
fstOf3 Bag
  (GenLocated
     SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)),
   [Name], NameSet)
binds_w_dus, NameSet
bind_fvs) }

       ; forall (m :: * -> *) a. Monad m => a -> m a
return ( Bag
  (GenLocated
     SrcSpanAnnA (HsBindLR (GhcPass 'Renamed) (GhcPass 'Renamed)))
binds'', [GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
spec_inst_prags' forall a. [a] -> [a] -> [a]
++ [GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
other_sigs'
                , NameSet
sig_fvs NameSet -> NameSet -> NameSet
`plusFV` NameSet
sip_fvs NameSet -> NameSet -> NameSet
`plusFV` NameSet
bind_fvs) }

rnMethodBindLHS :: Bool -> Name
                -> LHsBindLR GhcPs GhcPs
                -> LHsBindsLR GhcRn GhcPs
                -> RnM (LHsBindsLR GhcRn GhcPs)
rnMethodBindLHS :: Bool
-> Name
-> LHsBindLR GhcPs GhcPs
-> LHsBindsLR (GhcPass 'Renamed) GhcPs
-> RnM (LHsBindsLR (GhcPass 'Renamed) GhcPs)
rnMethodBindLHS Bool
_ Name
cls (L SrcSpanAnnA
loc bind :: HsBindLR GhcPs GhcPs
bind@(FunBind { fun_id :: forall idL idR. HsBindLR idL idR -> LIdP idL
fun_id = LIdP GhcPs
name })) LHsBindsLR (GhcPass 'Renamed) GhcPs
rest
  = forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc forall a b. (a -> b) -> a -> b
$ do
    do { GenLocated SrcSpanAnnN Name
sel_name <- forall a b ann.
(a -> TcM b)
-> GenLocated (SrcSpanAnn' ann) a
-> TcRn (GenLocated (SrcSpanAnn' ann) b)
wrapLocMA (Name -> SDoc -> RdrName -> RnM Name
lookupInstDeclBndr Name
cls (String -> SDoc
text String
"method")) LIdP GhcPs
name
                     -- We use the selector name as the binder
       ; let bind' :: HsBindLR (GhcPass 'Renamed) GhcPs
bind' = HsBindLR GhcPs GhcPs
bind { fun_id :: LIdP (GhcPass 'Renamed)
fun_id = GenLocated SrcSpanAnnN Name
sel_name, fun_ext :: XFunBind (GhcPass 'Renamed) GhcPs
fun_ext = NoExtField
noExtField }
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc HsBindLR (GhcPass 'Renamed) GhcPs
bind' forall a. a -> Bag a -> Bag a
`consBag` LHsBindsLR (GhcPass 'Renamed) GhcPs
rest ) }

-- Report error for all other forms of bindings
-- This is why we use a fold rather than map
rnMethodBindLHS Bool
is_cls_decl Name
_ (L SrcSpanAnnA
loc HsBindLR GhcPs GhcPs
bind) LHsBindsLR (GhcPass 'Renamed) GhcPs
rest
  = do { SrcSpan -> TcRnMessage -> RnM ()
addErrAt (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
loc) forall a b. (a -> b) -> a -> b
$ forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints forall a b. (a -> b) -> a -> b
$
         [SDoc] -> SDoc
vcat [ SDoc
what SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"not allowed in" SDoc -> SDoc -> SDoc
<+> SDoc
decl_sort
              , Int -> SDoc -> SDoc
nest Int
2 (forall a. Outputable a => a -> SDoc
ppr HsBindLR GhcPs GhcPs
bind) ]
       ; forall (m :: * -> *) a. Monad m => a -> m a
return LHsBindsLR (GhcPass 'Renamed) GhcPs
rest }
  where
    decl_sort :: SDoc
decl_sort | Bool
is_cls_decl = String -> SDoc
text String
"class declaration:"
              | Bool
otherwise   = String -> SDoc
text String
"instance declaration:"
    what :: SDoc
what = case HsBindLR GhcPs GhcPs
bind of
              PatBind {}    -> String -> SDoc
text String
"Pattern bindings (except simple variables)"
              PatSynBind {} -> String -> SDoc
text String
"Pattern synonyms"
                               -- Associated pattern synonyms are not implemented yet
              HsBindLR GhcPs GhcPs
_ -> forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"rnMethodBind" (forall a. Outputable a => a -> SDoc
ppr HsBindLR GhcPs GhcPs
bind)

{-
************************************************************************
*                                                                      *
\subsubsection[dep-Sigs]{Signatures (and user-pragmas for values)}
*                                                                      *
************************************************************************

@renameSigs@ checks for:
\begin{enumerate}
\item more than one sig for one thing;
\item signatures given for things not bound here;
\end{enumerate}

At the moment we don't gather free-var info from the types in
signatures.  We'd only need this if we wanted to report unused tyvars.
-}

renameSigs :: HsSigCtxt
           -> [LSig GhcPs]
           -> RnM ([LSig GhcRn], FreeVars)
-- Renames the signatures and performs error checks
renameSigs :: HsSigCtxt
-> [LSig GhcPs] -> RnM ([LSig (GhcPass 'Renamed)], NameSet)
renameSigs HsSigCtxt
ctxt [LSig GhcPs]
sigs
  = do  { forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ NonEmpty (LocatedN RdrName, Sig GhcPs) -> RnM ()
dupSigDeclErr ([LSig GhcPs] -> [NonEmpty (LocatedN RdrName, Sig GhcPs)]
findDupSigs [LSig GhcPs]
sigs)

        ; [LSig GhcPs] -> RnM ()
checkDupMinimalSigs [LSig GhcPs]
sigs

        ; ([GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
sigs', NameSet
sig_fvs) <- forall a b. (a -> RnM (b, NameSet)) -> [a] -> RnM ([b], NameSet)
mapFvRn (forall a b c ann.
(a -> TcM (b, c))
-> GenLocated (SrcSpanAnn' ann) a
-> TcM (GenLocated (SrcSpanAnn' ann) b, c)
wrapLocFstMA (HsSigCtxt -> Sig GhcPs -> RnM (Sig (GhcPass 'Renamed), NameSet)
renameSig HsSigCtxt
ctxt)) [LSig GhcPs]
sigs

        ; let ([GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
good_sigs, [GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
bad_sigs) = forall a. (a -> Bool) -> [a] -> ([a], [a])
partition (forall (a :: Pass). HsSigCtxt -> LSig (GhcPass a) -> Bool
okHsSig HsSigCtxt
ctxt) [GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
sigs'
        ; forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ LSig (GhcPass 'Renamed) -> RnM ()
misplacedSigErr [GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
bad_sigs                 -- Misplaced

        ; forall (m :: * -> *) a. Monad m => a -> m a
return ([GenLocated SrcSpanAnnA (Sig (GhcPass 'Renamed))]
good_sigs, NameSet
sig_fvs) }

----------------------
-- We use lookupSigOccRn in the signatures, which is a little bit unsatisfactory
-- because this won't work for:
--      instance Foo T where
--        {-# INLINE op #-}
--        Baz.op = ...
-- We'll just rename the INLINE prag to refer to whatever other 'op'
-- is in scope.  (I'm assuming that Baz.op isn't in scope unqualified.)
-- Doesn't seem worth much trouble to sort this.

renameSig :: HsSigCtxt -> Sig GhcPs -> RnM (Sig GhcRn, FreeVars)
renameSig :: HsSigCtxt -> Sig GhcPs -> RnM (Sig (GhcPass 'Renamed), NameSet)
renameSig HsSigCtxt
ctxt sig :: Sig GhcPs
sig@(TypeSig XTypeSig GhcPs
_ [LIdP GhcPs]
vs LHsSigWcType GhcPs
ty)
  = do  { [GenLocated SrcSpanAnnN Name]
new_vs <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (HsSigCtxt
-> Sig GhcPs
-> LocatedN RdrName
-> RnM (GenLocated SrcSpanAnnN Name)
lookupSigOccRnN HsSigCtxt
ctxt Sig GhcPs
sig) [LIdP GhcPs]
vs
        ; let doc :: HsDocContext
doc = SDoc -> HsDocContext
TypeSigCtx ([LocatedN RdrName] -> SDoc
ppr_sig_bndrs [LIdP GhcPs]
vs)
        ; (HsWildCardBndrs
  (GhcPass 'Renamed)
  (GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed)))
new_ty, NameSet
fvs) <- HsDocContext
-> LHsSigWcType GhcPs
-> RnM (LHsSigWcType (GhcPass 'Renamed), NameSet)
rnHsSigWcType HsDocContext
doc LHsSigWcType GhcPs
ty
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall pass.
XTypeSig pass -> [LIdP pass] -> LHsSigWcType pass -> Sig pass
TypeSig forall a. EpAnn a
noAnn [GenLocated SrcSpanAnnN Name]
new_vs HsWildCardBndrs
  (GhcPass 'Renamed)
  (GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed)))
new_ty, NameSet
fvs) }

renameSig HsSigCtxt
ctxt sig :: Sig GhcPs
sig@(ClassOpSig XClassOpSig GhcPs
_ Bool
is_deflt [LIdP GhcPs]
vs LHsSigType GhcPs
ty)
  = do  { Bool
defaultSigs_on <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DefaultSignatures
        ; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
is_deflt Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
defaultSigs_on) forall a b. (a -> b) -> a -> b
$
          TcRnMessage -> RnM ()
addErr (Sig GhcPs -> TcRnMessage
defaultSigErr Sig GhcPs
sig)
        ; forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ LocatedN RdrName -> RnM ()
warnForallIdentifier [LIdP GhcPs]
vs
        ; [GenLocated SrcSpanAnnN Name]
new_v <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (HsSigCtxt
-> Sig GhcPs
-> LocatedN RdrName
-> RnM (GenLocated SrcSpanAnnN Name)
lookupSigOccRnN HsSigCtxt
ctxt Sig GhcPs
sig) [LIdP GhcPs]
vs
        ; (GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))
new_ty, NameSet
fvs) <- HsDocContext
-> TypeOrKind
-> LHsSigType GhcPs
-> RnM (LHsSigType (GhcPass 'Renamed), NameSet)
rnHsSigType HsDocContext
ty_ctxt TypeOrKind
TypeLevel LHsSigType GhcPs
ty
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall pass.
XClassOpSig pass
-> Bool -> [LIdP pass] -> LHsSigType pass -> Sig pass
ClassOpSig forall a. EpAnn a
noAnn Bool
is_deflt [GenLocated SrcSpanAnnN Name]
new_v GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))
new_ty, NameSet
fvs) }
  where
    (LIdP GhcPs
v1:[LIdP GhcPs]
_) = [LIdP GhcPs]
vs
    ty_ctxt :: HsDocContext
ty_ctxt = SDoc -> HsDocContext
GenericCtx (String -> SDoc
text String
"a class method signature for"
                          SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr LIdP GhcPs
v1))

renameSig HsSigCtxt
_ (SpecInstSig (EpAnn [AddEpAnn]
_, SourceText
src) LHsSigType GhcPs
ty)
  = do  { HsDocContext -> Maybe SDoc -> LHsSigType GhcPs -> RnM ()
checkInferredVars HsDocContext
doc Maybe SDoc
inf_msg LHsSigType GhcPs
ty
        ; (GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))
new_ty, NameSet
fvs) <- HsDocContext
-> TypeOrKind
-> LHsSigType GhcPs
-> RnM (LHsSigType (GhcPass 'Renamed), NameSet)
rnHsSigType HsDocContext
doc TypeOrKind
TypeLevel LHsSigType GhcPs
ty
          -- Check if there are any nested `forall`s or contexts, which are
          -- illegal in the type of an instance declaration (see
          -- Note [No nested foralls or contexts in instance types] in
          -- GHC.Hs.Type).
        ; HsDocContext -> SDoc -> LHsType (GhcPass 'Renamed) -> RnM ()
addNoNestedForallsContextsErr HsDocContext
doc (String -> SDoc
text String
"SPECIALISE instance type")
            (forall (p :: Pass). LHsSigType (GhcPass p) -> LHsType (GhcPass p)
getLHsInstDeclHead GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))
new_ty)
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall pass. XSpecInstSig pass -> LHsSigType pass -> Sig pass
SpecInstSig (forall a. EpAnn a
noAnn, SourceText
src) GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))
new_ty,NameSet
fvs) }
  where
    doc :: HsDocContext
doc = HsDocContext
SpecInstSigCtx
    inf_msg :: Maybe SDoc
inf_msg = forall a. a -> Maybe a
Just (String -> SDoc
text String
"Inferred type variables are not allowed")

-- {-# SPECIALISE #-} pragmas can refer to imported Ids
-- so, in the top-level case (when mb_names is Nothing)
-- we use lookupOccRn.  If there's both an imported and a local 'f'
-- then the SPECIALISE pragma is ambiguous, unlike all other signatures
renameSig HsSigCtxt
ctxt sig :: Sig GhcPs
sig@(SpecSig XSpecSig GhcPs
_ LIdP GhcPs
v [LHsSigType GhcPs]
tys InlinePragma
inl)
  = do  { GenLocated SrcSpanAnnN Name
new_v <- case HsSigCtxt
ctxt of
                     TopSigCtxt {} -> forall {ann}.
GenLocated (SrcSpanAnn' ann) RdrName
-> TcRn (GenLocated (SrcSpanAnn' ann) Name)
lookupLocatedOccRn LIdP GhcPs
v
                     HsSigCtxt
_             -> HsSigCtxt
-> Sig GhcPs
-> LocatedN RdrName
-> RnM (GenLocated SrcSpanAnnN Name)
lookupSigOccRnN HsSigCtxt
ctxt Sig GhcPs
sig LIdP GhcPs
v
        ; ([GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))]
new_ty, NameSet
fvs) <- forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM ([GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))], NameSet)
-> GenLocated SrcSpanAnnA (HsSigType GhcPs)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     ([GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))], NameSet)
do_one ([],NameSet
emptyFVs) [LHsSigType GhcPs]
tys
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall pass.
XSpecSig pass
-> LIdP pass -> [LHsSigType pass] -> InlinePragma -> Sig pass
SpecSig forall a. EpAnn a
noAnn GenLocated SrcSpanAnnN Name
new_v [GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))]
new_ty InlinePragma
inl, NameSet
fvs) }
  where
    ty_ctxt :: HsDocContext
ty_ctxt = SDoc -> HsDocContext
GenericCtx (String -> SDoc
text String
"a SPECIALISE signature for"
                          SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr LIdP GhcPs
v))
    do_one :: ([GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))], NameSet)
-> GenLocated SrcSpanAnnA (HsSigType GhcPs)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     ([GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))], NameSet)
do_one ([GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))]
tys,NameSet
fvs) GenLocated SrcSpanAnnA (HsSigType GhcPs)
ty
      = do { (GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))
new_ty, NameSet
fvs_ty) <- HsDocContext
-> TypeOrKind
-> LHsSigType GhcPs
-> RnM (LHsSigType (GhcPass 'Renamed), NameSet)
rnHsSigType HsDocContext
ty_ctxt TypeOrKind
TypeLevel GenLocated SrcSpanAnnA (HsSigType GhcPs)
ty
           ; forall (m :: * -> *) a. Monad m => a -> m a
return ( GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))
new_tyforall a. a -> [a] -> [a]
:[GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))]
tys, NameSet
fvs_ty NameSet -> NameSet -> NameSet
`plusFV` NameSet
fvs) }

renameSig HsSigCtxt
ctxt sig :: Sig GhcPs
sig@(InlineSig XInlineSig GhcPs
_ LIdP GhcPs
v InlinePragma
s)
  = do  { GenLocated SrcSpanAnnN Name
new_v <- HsSigCtxt
-> Sig GhcPs
-> LocatedN RdrName
-> RnM (GenLocated SrcSpanAnnN Name)
lookupSigOccRnN HsSigCtxt
ctxt Sig GhcPs
sig LIdP GhcPs
v
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall pass.
XInlineSig pass -> LIdP pass -> InlinePragma -> Sig pass
InlineSig forall a. EpAnn a
noAnn GenLocated SrcSpanAnnN Name
new_v InlinePragma
s, NameSet
emptyFVs) }

renameSig HsSigCtxt
ctxt (FixSig XFixSig GhcPs
_ FixitySig GhcPs
fsig)
  = do  { FixitySig (GhcPass 'Renamed)
new_fsig <- HsSigCtxt -> FixitySig GhcPs -> RnM (FixitySig (GhcPass 'Renamed))
rnSrcFixityDecl HsSigCtxt
ctxt FixitySig GhcPs
fsig
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall pass. XFixSig pass -> FixitySig pass -> Sig pass
FixSig forall a. EpAnn a
noAnn FixitySig (GhcPass 'Renamed)
new_fsig, NameSet
emptyFVs) }

renameSig HsSigCtxt
ctxt sig :: Sig GhcPs
sig@(MinimalSig (EpAnn [AddEpAnn]
_, SourceText
s) (L SrcSpanAnnL
l BooleanFormula (LIdP GhcPs)
bf))
  = do BooleanFormula (GenLocated SrcSpanAnnN Name)
new_bf <- forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (HsSigCtxt
-> Sig GhcPs
-> LocatedN RdrName
-> RnM (GenLocated SrcSpanAnnN Name)
lookupSigOccRnN HsSigCtxt
ctxt Sig GhcPs
sig) BooleanFormula (LIdP GhcPs)
bf
       forall (m :: * -> *) a. Monad m => a -> m a
return (forall pass.
XMinimalSig pass -> LBooleanFormula (LIdP pass) -> Sig pass
MinimalSig (forall a. EpAnn a
noAnn, SourceText
s) (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnL
l BooleanFormula (GenLocated SrcSpanAnnN Name)
new_bf), NameSet
emptyFVs)

renameSig HsSigCtxt
ctxt sig :: Sig GhcPs
sig@(PatSynSig XPatSynSig GhcPs
_ [LIdP GhcPs]
vs LHsSigType GhcPs
ty)
  = do  { [GenLocated SrcSpanAnnN Name]
new_vs <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (HsSigCtxt
-> Sig GhcPs
-> LocatedN RdrName
-> RnM (GenLocated SrcSpanAnnN Name)
lookupSigOccRnN HsSigCtxt
ctxt Sig GhcPs
sig) [LIdP GhcPs]
vs
        ; (GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))
ty', NameSet
fvs) <- HsDocContext
-> TypeOrKind
-> LHsSigType GhcPs
-> RnM (LHsSigType (GhcPass 'Renamed), NameSet)
rnHsSigType HsDocContext
ty_ctxt TypeOrKind
TypeLevel LHsSigType GhcPs
ty
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall pass.
XPatSynSig pass -> [LIdP pass] -> LHsSigType pass -> Sig pass
PatSynSig forall a. EpAnn a
noAnn [GenLocated SrcSpanAnnN Name]
new_vs GenLocated SrcSpanAnnA (HsSigType (GhcPass 'Renamed))
ty', NameSet
fvs) }
  where
    ty_ctxt :: HsDocContext
ty_ctxt = SDoc -> HsDocContext
GenericCtx (String -> SDoc
text String
"a pattern synonym signature for"
                          SDoc -> SDoc -> SDoc
<+> [LocatedN RdrName] -> SDoc
ppr_sig_bndrs [LIdP GhcPs]
vs)

renameSig HsSigCtxt
ctxt sig :: Sig GhcPs
sig@(SCCFunSig (EpAnn [AddEpAnn]
_, SourceText
st) LIdP GhcPs
v Maybe (XRec GhcPs StringLiteral)
s)
  = do  { GenLocated SrcSpanAnnN Name
new_v <- HsSigCtxt
-> Sig GhcPs
-> LocatedN RdrName
-> RnM (GenLocated SrcSpanAnnN Name)
lookupSigOccRnN HsSigCtxt
ctxt Sig GhcPs
sig LIdP GhcPs
v
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall pass.
XSCCFunSig pass
-> LIdP pass -> Maybe (XRec pass StringLiteral) -> Sig pass
SCCFunSig (forall a. EpAnn a
noAnn, SourceText
st) GenLocated SrcSpanAnnN Name
new_v Maybe (XRec GhcPs StringLiteral)
s, NameSet
emptyFVs) }

-- COMPLETE Sigs can refer to imported IDs which is why we use
-- lookupLocatedOccRn rather than lookupSigOccRn
renameSig HsSigCtxt
_ctxt sig :: Sig GhcPs
sig@(CompleteMatchSig (EpAnn [AddEpAnn]
_, SourceText
s) (L SrcSpan
l [LocatedN RdrName]
bf) Maybe (LIdP GhcPs)
mty)
  = do [GenLocated SrcSpanAnnN Name]
new_bf <- forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall {ann}.
GenLocated (SrcSpanAnn' ann) RdrName
-> TcRn (GenLocated (SrcSpanAnn' ann) Name)
lookupLocatedOccRn [LocatedN RdrName]
bf
       Maybe (GenLocated SrcSpanAnnN Name)
new_mty  <- forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse forall {ann}.
GenLocated (SrcSpanAnn' ann) RdrName
-> TcRn (GenLocated (SrcSpanAnn' ann) Name)
lookupLocatedOccRn Maybe (LIdP GhcPs)
mty

       Module
this_mod <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TcGblEnv -> Module
tcg_mod forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
       forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Module -> Name -> Bool
nameIsLocalOrFrom Module
this_mod forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc) [GenLocated SrcSpanAnnN Name]
new_bf) forall a b. (a -> b) -> a -> b
$
         -- Why 'any'? See Note [Orphan COMPLETE pragmas]
         forall a. SDoc -> TcM a -> TcM a
addErrCtxt (String -> SDoc
text String
"In" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr Sig GhcPs
sig) forall a b. (a -> b) -> a -> b
$ forall a. TcRnMessage -> TcM a
failWithTc TcRnMessage
orphanError

       forall (m :: * -> *) a. Monad m => a -> m a
return (forall pass.
XCompleteMatchSig pass
-> XRec pass [LIdP pass] -> Maybe (LIdP pass) -> Sig pass
CompleteMatchSig (forall a. EpAnn a
noAnn, SourceText
s) (forall l e. l -> e -> GenLocated l e
L SrcSpan
l [GenLocated SrcSpanAnnN Name]
new_bf) Maybe (GenLocated SrcSpanAnnN Name)
new_mty, NameSet
emptyFVs)
  where
    orphanError :: TcRnMessage
    orphanError :: TcRnMessage
orphanError = forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints forall a b. (a -> b) -> a -> b
$
      String -> SDoc
text String
"Orphan COMPLETE pragmas not supported" SDoc -> SDoc -> SDoc
$$
      String -> SDoc
text String
"A COMPLETE pragma must mention at least one data constructor" SDoc -> SDoc -> SDoc
$$
      String -> SDoc
text String
"or pattern synonym defined in the same module."

{-
Note [Orphan COMPLETE pragmas]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We define a COMPLETE pragma to be a non-orphan if it includes at least
one conlike defined in the current module. Why is this sufficient?
Well if you have a pattern match

  case expr of
    P1 -> ...
    P2 -> ...
    P3 -> ...

any COMPLETE pragma which mentions a conlike other than P1, P2 or P3
will not be of any use in verifying that the pattern match is
exhaustive. So as we have certainly read the interface files that
define P1, P2 and P3, we will have loaded all non-orphan COMPLETE
pragmas that could be relevant to this pattern match.

For now we simply disallow orphan COMPLETE pragmas, as the added
complexity of supporting them properly doesn't seem worthwhile.
-}

ppr_sig_bndrs :: [LocatedN RdrName] -> SDoc
ppr_sig_bndrs :: [LocatedN RdrName] -> SDoc
ppr_sig_bndrs [LocatedN RdrName]
bs = SDoc -> SDoc
quotes (forall a. (a -> SDoc) -> [a] -> SDoc
pprWithCommas forall a. Outputable a => a -> SDoc
ppr [LocatedN RdrName]
bs)

okHsSig :: HsSigCtxt -> LSig (GhcPass a) -> Bool
okHsSig :: forall (a :: Pass). HsSigCtxt -> LSig (GhcPass a) -> Bool
okHsSig HsSigCtxt
ctxt (L SrcSpanAnnA
_ Sig (GhcPass a)
sig)
  = case (Sig (GhcPass a)
sig, HsSigCtxt
ctxt) of
     (ClassOpSig {}, ClsDeclCtxt {})  -> Bool
True
     (ClassOpSig {}, InstDeclCtxt {}) -> Bool
True
     (ClassOpSig {}, HsSigCtxt
_)               -> Bool
False

     (TypeSig {}, ClsDeclCtxt {})  -> Bool
False
     (TypeSig {}, InstDeclCtxt {}) -> Bool
False
     (TypeSig {}, HsSigCtxt
_)               -> Bool
True

     (PatSynSig {}, TopSigCtxt{}) -> Bool
True
     (PatSynSig {}, HsSigCtxt
_)            -> Bool
False

     (FixSig {}, InstDeclCtxt {}) -> Bool
False
     (FixSig {}, HsSigCtxt
_)               -> Bool
True

     (InlineSig {}, HsBootCtxt {}) -> Bool
False
     (InlineSig {}, HsSigCtxt
_)             -> Bool
True

     (SpecSig {}, TopSigCtxt {})    -> Bool
True
     (SpecSig {}, LocalBindCtxt {}) -> Bool
True
     (SpecSig {}, InstDeclCtxt {})  -> Bool
True
     (SpecSig {}, HsSigCtxt
_)                -> Bool
False

     (SpecInstSig {}, InstDeclCtxt {}) -> Bool
True
     (SpecInstSig {}, HsSigCtxt
_)               -> Bool
False

     (MinimalSig {}, ClsDeclCtxt {}) -> Bool
True
     (MinimalSig {}, HsSigCtxt
_)              -> Bool
False

     (SCCFunSig {}, HsBootCtxt {}) -> Bool
False
     (SCCFunSig {}, HsSigCtxt
_)             -> Bool
True

     (CompleteMatchSig {}, TopSigCtxt {} ) -> Bool
True
     (CompleteMatchSig {}, HsSigCtxt
_)              -> Bool
False

     (XSig {}, TopSigCtxt {})   -> Bool
True
     (XSig {}, InstDeclCtxt {}) -> Bool
True
     (XSig {}, HsSigCtxt
_)               -> Bool
False


-------------------
findDupSigs :: [LSig GhcPs] -> [NonEmpty (LocatedN RdrName, Sig GhcPs)]
-- Check for duplicates on RdrName version,
-- because renamed version has unboundName for
-- not-in-scope binders, which gives bogus dup-sig errors
-- NB: in a class decl, a 'generic' sig is not considered
--     equal to an ordinary sig, so we allow, say
--           class C a where
--             op :: a -> a
--             default op :: Eq a => a -> a
findDupSigs :: [LSig GhcPs] -> [NonEmpty (LocatedN RdrName, Sig GhcPs)]
findDupSigs [LSig GhcPs]
sigs
  = forall a. (a -> a -> Bool) -> [a] -> [NonEmpty a]
findDupsEq (LocatedN RdrName, Sig GhcPs)
-> (LocatedN RdrName, Sig GhcPs) -> Bool
matching_sig (forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Sig GhcPs -> [(LocatedN RdrName, Sig GhcPs)]
expand_sig forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc) [LSig GhcPs]
sigs)
  where
    expand_sig :: Sig GhcPs -> [(LocatedN RdrName, Sig GhcPs)] -- AZ
    expand_sig :: Sig GhcPs -> [(LocatedN RdrName, Sig GhcPs)]
expand_sig sig :: Sig GhcPs
sig@(FixSig XFixSig GhcPs
_ (FixitySig XFixitySig GhcPs
_ [LIdP GhcPs]
ns Fixity
_)) = forall a b. [a] -> [b] -> [(a, b)]
zip [LIdP GhcPs]
ns (forall a. a -> [a]
repeat Sig GhcPs
sig)
    expand_sig sig :: Sig GhcPs
sig@(InlineSig XInlineSig GhcPs
_ LIdP GhcPs
n InlinePragma
_)             = [(LIdP GhcPs
n,Sig GhcPs
sig)]
    expand_sig sig :: Sig GhcPs
sig@(TypeSig XTypeSig GhcPs
_ [LIdP GhcPs]
ns LHsSigWcType GhcPs
_)              = [(LocatedN RdrName
n,Sig GhcPs
sig) | LocatedN RdrName
n <- [LIdP GhcPs]
ns]
    expand_sig sig :: Sig GhcPs
sig@(ClassOpSig XClassOpSig GhcPs
_ Bool
_ [LIdP GhcPs]
ns LHsSigType GhcPs
_)         = [(LocatedN RdrName
n,Sig GhcPs
sig) | LocatedN RdrName
n <- [LIdP GhcPs]
ns]
    expand_sig sig :: Sig GhcPs
sig@(PatSynSig XPatSynSig GhcPs
_ [LIdP GhcPs]
ns  LHsSigType GhcPs
_ )          = [(LocatedN RdrName
n,Sig GhcPs
sig) | LocatedN RdrName
n <- [LIdP GhcPs]
ns]
    expand_sig sig :: Sig GhcPs
sig@(SCCFunSig (EpAnn [AddEpAnn]
_, SourceText
_) LIdP GhcPs
n Maybe (XRec GhcPs StringLiteral)
_)           = [(LIdP GhcPs
n,Sig GhcPs
sig)]
    expand_sig Sig GhcPs
_ = []

    matching_sig :: (LocatedN RdrName, Sig GhcPs) -> (LocatedN RdrName, Sig GhcPs) -> Bool --AZ
    matching_sig :: (LocatedN RdrName, Sig GhcPs)
-> (LocatedN RdrName, Sig GhcPs) -> Bool
matching_sig (L SrcSpanAnnN
_ RdrName
n1,Sig GhcPs
sig1) (L SrcSpanAnnN
_ RdrName
n2,Sig GhcPs
sig2)       = RdrName
n1 forall a. Eq a => a -> a -> Bool
== RdrName
n2 Bool -> Bool -> Bool
&& forall {pass} {pass}. Sig pass -> Sig pass -> Bool
mtch Sig GhcPs
sig1 Sig GhcPs
sig2
    mtch :: Sig pass -> Sig pass -> Bool
mtch (FixSig {})           (FixSig {})         = Bool
True
    mtch (InlineSig {})        (InlineSig {})      = Bool
True
    mtch (TypeSig {})          (TypeSig {})        = Bool
True
    mtch (ClassOpSig XClassOpSig pass
_ Bool
d1 [LIdP pass]
_ LHsSigType pass
_) (ClassOpSig XClassOpSig pass
_ Bool
d2 [LIdP pass]
_ LHsSigType pass
_) = Bool
d1 forall a. Eq a => a -> a -> Bool
== Bool
d2
    mtch (PatSynSig XPatSynSig pass
_ [LIdP pass]
_ LHsSigType pass
_)     (PatSynSig XPatSynSig pass
_ [LIdP pass]
_ LHsSigType pass
_)   = Bool
True
    mtch (SCCFunSig{})         (SCCFunSig{})       = Bool
True
    mtch Sig pass
_ Sig pass
_ = Bool
False

-- Warn about multiple MINIMAL signatures
checkDupMinimalSigs :: [LSig GhcPs] -> RnM ()
checkDupMinimalSigs :: [LSig GhcPs] -> RnM ()
checkDupMinimalSigs [LSig GhcPs]
sigs
  = case forall a. (a -> Bool) -> [a] -> [a]
filter forall p. UnXRec p => LSig p -> Bool
isMinimalLSig [LSig GhcPs]
sigs of
      minSigs :: [LSig GhcPs]
minSigs@(LSig GhcPs
_:LSig GhcPs
_:[LSig GhcPs]
_) -> [LSig GhcPs] -> RnM ()
dupMinimalSigErr [LSig GhcPs]
minSigs
      [LSig GhcPs]
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()

{-
************************************************************************
*                                                                      *
\subsection{Match}
*                                                                      *
************************************************************************
-}

type AnnoBody body
  = ( Anno [LocatedA (Match GhcRn (LocatedA (body GhcRn)))] ~ SrcSpanAnnL
    , Anno [LocatedA (Match GhcPs (LocatedA (body GhcPs)))] ~ SrcSpanAnnL
    , Anno (Match GhcRn (LocatedA (body GhcRn))) ~ SrcSpanAnnA
    , Anno (Match GhcPs (LocatedA (body GhcPs))) ~ SrcSpanAnnA
    , Anno (GRHS GhcRn (LocatedA (body GhcRn))) ~ SrcAnn NoEpAnns
    , Anno (GRHS GhcPs (LocatedA (body GhcPs))) ~ SrcAnn NoEpAnns
    , Outputable (body GhcPs)
    )

-- Note [Empty MatchGroups]
-- ~~~~~~~~~~~~~~~~~~~~~~~~
-- In some cases, MatchGroups are allowed to be empty. Firstly, the
-- prerequisite is that -XEmptyCase is enabled. Then you have an empty
-- MatchGroup resulting either from a case-expression:
--
--     case e of {}
--
-- or from a \case-expression:
--
--     \case {}
--
-- NB: \cases {} is not allowed, since it's not clear how many patterns this
-- should match on.
--
-- The same applies in arrow notation commands: With -XEmptyCases, it is
-- allowed in case- and \case-commands, but not \cases.
--
-- Since the lambda expressions and empty function definitions are already
-- disallowed elsewhere, here, we only need to make sure we don't accept empty
-- \cases expressions or commands. In that case, or if we encounter an empty
-- MatchGroup but -XEmptyCases is disabled, we add an error.

rnMatchGroup :: (Outputable (body GhcPs), AnnoBody body) => HsMatchContext GhcRn
             -> (LocatedA (body GhcPs) -> RnM (LocatedA (body GhcRn), FreeVars))
             -> MatchGroup GhcPs (LocatedA (body GhcPs))
             -> RnM (MatchGroup GhcRn (LocatedA (body GhcRn)), FreeVars)
rnMatchGroup :: forall (body :: * -> *).
(Outputable (body GhcPs), AnnoBody body) =>
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> MatchGroup GhcPs (LocatedA (body GhcPs))
-> RnM
     (MatchGroup
        (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnMatchGroup HsMatchContext (GhcPass 'Renamed)
ctxt LocatedA (body GhcPs)
-> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet)
rnBody (MG { mg_alts :: forall p body. MatchGroup p body -> XRec p [LMatch p body]
mg_alts = L SrcSpanAnnL
lm [LocatedA (Match GhcPs (LocatedA (body GhcPs)))]
ms, mg_ext :: forall p body. MatchGroup p body -> XMG p body
mg_ext = XMG GhcPs (LocatedA (body GhcPs))
origin })
         -- see Note [Empty MatchGroups]
  = do { forall (m :: * -> *). Monad m => m Bool -> m () -> m ()
whenM ((forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LocatedA (Match GhcPs (LocatedA (body GhcPs)))]
ms Bool -> Bool -> Bool
&&) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) Bool
mustn't_be_empty) (TcRnMessage -> RnM ()
addErr (HsMatchContext (GhcPass 'Renamed) -> TcRnMessage
emptyCaseErr HsMatchContext (GhcPass 'Renamed)
ctxt))
       ; ([LocatedA
   (Match (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))))]
new_ms, NameSet
ms_fvs) <- forall a b. (a -> RnM (b, NameSet)) -> [a] -> RnM ([b], NameSet)
mapFvRn (forall (body :: * -> *).
AnnoBody body =>
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> LMatch GhcPs (LocatedA (body GhcPs))
-> RnM
     (LMatch (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnMatch HsMatchContext (GhcPass 'Renamed)
ctxt LocatedA (body GhcPs)
-> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet)
rnBody) [LocatedA (Match GhcPs (LocatedA (body GhcPs)))]
ms
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall (p :: Pass) (body :: * -> *).
AnnoBody p body =>
Origin
-> LocatedL
     [LocatedA (Match (GhcPass p) (LocatedA (body (GhcPass p))))]
-> MatchGroup (GhcPass p) (LocatedA (body (GhcPass p)))
mkMatchGroup XMG GhcPs (LocatedA (body GhcPs))
origin (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnL
lm [LocatedA
   (Match (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))))]
new_ms), NameSet
ms_fvs) }
  where
    mustn't_be_empty :: IOEnv (Env TcGblEnv TcLclEnv) Bool
mustn't_be_empty = case HsMatchContext (GhcPass 'Renamed)
ctxt of
      LamCaseAlt LamCaseVariant
LamCases -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
      ArrowMatchCtxt (ArrowLamCaseAlt LamCaseVariant
LamCases) -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
      HsMatchContext (GhcPass 'Renamed)
_ -> Bool -> Bool
not forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.EmptyCase

rnMatch :: AnnoBody body
        => HsMatchContext GhcRn
        -> (LocatedA (body GhcPs) -> RnM (LocatedA (body GhcRn), FreeVars))
        -> LMatch GhcPs (LocatedA (body GhcPs))
        -> RnM (LMatch GhcRn (LocatedA (body GhcRn)), FreeVars)
rnMatch :: forall (body :: * -> *).
AnnoBody body =>
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> LMatch GhcPs (LocatedA (body GhcPs))
-> RnM
     (LMatch (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnMatch HsMatchContext (GhcPass 'Renamed)
ctxt LocatedA (body GhcPs)
-> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet)
rnBody = forall a b c ann.
(a -> TcM (b, c))
-> GenLocated (SrcSpanAnn' ann) a
-> TcM (GenLocated (SrcSpanAnn' ann) b, c)
wrapLocFstMA (forall (body :: * -> *).
AnnoBody body =>
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> Match GhcPs (LocatedA (body GhcPs))
-> RnM
     (Match (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnMatch' HsMatchContext (GhcPass 'Renamed)
ctxt LocatedA (body GhcPs)
-> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet)
rnBody)

rnMatch' :: (AnnoBody body)
         => HsMatchContext GhcRn
         -> (LocatedA (body GhcPs) -> RnM (LocatedA (body GhcRn), FreeVars))
         -> Match GhcPs (LocatedA (body GhcPs))
         -> RnM (Match GhcRn (LocatedA (body GhcRn)), FreeVars)
rnMatch' :: forall (body :: * -> *).
AnnoBody body =>
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> Match GhcPs (LocatedA (body GhcPs))
-> RnM
     (Match (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnMatch' HsMatchContext (GhcPass 'Renamed)
ctxt LocatedA (body GhcPs)
-> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet)
rnBody (Match { m_ctxt :: forall p body. Match p body -> HsMatchContext p
m_ctxt = HsMatchContext GhcPs
mf, m_pats :: forall p body. Match p body -> [LPat p]
m_pats = [LPat GhcPs]
pats, m_grhss :: forall p body. Match p body -> GRHSs p body
m_grhss = GRHSs GhcPs (LocatedA (body GhcPs))
grhss })
  = forall a.
HsMatchContext (GhcPass 'Renamed)
-> [LPat GhcPs]
-> ([LPat (GhcPass 'Renamed)] -> RnM (a, NameSet))
-> RnM (a, NameSet)
rnPats HsMatchContext (GhcPass 'Renamed)
ctxt [LPat GhcPs]
pats forall a b. (a -> b) -> a -> b
$ \ [LPat (GhcPass 'Renamed)]
pats' -> do
        { (GRHSs (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed)))
grhss', NameSet
grhss_fvs) <- forall (body :: * -> *).
AnnoBody body =>
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> GRHSs GhcPs (LocatedA (body GhcPs))
-> RnM
     (GRHSs (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnGRHSs HsMatchContext (GhcPass 'Renamed)
ctxt LocatedA (body GhcPs)
-> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet)
rnBody GRHSs GhcPs (LocatedA (body GhcPs))
grhss
        ; let mf' :: HsMatchContext (GhcPass 'Renamed)
mf' = case (HsMatchContext (GhcPass 'Renamed)
ctxt, HsMatchContext GhcPs
mf) of
                      (FunRhs { mc_fun :: forall p. HsMatchContext p -> LIdP p
mc_fun = L SrcSpanAnnN
_ Name
funid }, FunRhs { mc_fun :: forall p. HsMatchContext p -> LIdP p
mc_fun = L SrcSpanAnnN
lf RdrName
_ })
                                            -> HsMatchContext GhcPs
mf { mc_fun :: LIdP (GhcPass 'Renamed)
mc_fun = forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
lf Name
funid }
                      (HsMatchContext (GhcPass 'Renamed), HsMatchContext GhcPs)
_                     -> HsMatchContext (GhcPass 'Renamed)
ctxt
        ; forall (m :: * -> *) a. Monad m => a -> m a
return (Match { m_ext :: XCMatch (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed)))
m_ext = forall a. EpAnn a
noAnn, m_ctxt :: HsMatchContext (GhcPass 'Renamed)
m_ctxt = HsMatchContext (GhcPass 'Renamed)
mf', m_pats :: [LPat (GhcPass 'Renamed)]
m_pats = [LPat (GhcPass 'Renamed)]
pats'
                        , m_grhss :: GRHSs (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed)))
m_grhss = GRHSs (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed)))
grhss'}, NameSet
grhss_fvs ) }

emptyCaseErr :: HsMatchContext GhcRn -> TcRnMessage
emptyCaseErr :: HsMatchContext (GhcPass 'Renamed) -> TcRnMessage
emptyCaseErr HsMatchContext (GhcPass 'Renamed)
ctxt = forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints forall a b. (a -> b) -> a -> b
$ HsMatchContext (GhcPass 'Renamed) -> SDoc
message HsMatchContext (GhcPass 'Renamed)
ctxt
  where
    pp_ctxt :: HsMatchContext GhcRn -> SDoc
    pp_ctxt :: HsMatchContext (GhcPass 'Renamed) -> SDoc
pp_ctxt HsMatchContext (GhcPass 'Renamed)
c = case HsMatchContext (GhcPass 'Renamed)
c of
      HsMatchContext (GhcPass 'Renamed)
CaseAlt                                  -> String -> SDoc
text String
"case expression"
      LamCaseAlt LamCaseVariant
LamCase                       -> String -> SDoc
text String
"\\case expression"
      ArrowMatchCtxt (ArrowLamCaseAlt LamCaseVariant
LamCase) -> String -> SDoc
text String
"\\case command"
      ArrowMatchCtxt HsArrowMatchContext
ArrowCaseAlt              -> String -> SDoc
text String
"case command"
      ArrowMatchCtxt HsArrowMatchContext
KappaExpr                 -> String -> SDoc
text String
"kappa abstraction"
      HsMatchContext (GhcPass 'Renamed)
_                                        -> String -> SDoc
text String
"(unexpected)"
                                                  SDoc -> SDoc -> SDoc
<+> forall p.
(Outputable (IdP p), UnXRec p) =>
HsMatchContext p -> SDoc
pprMatchContextNoun HsMatchContext (GhcPass 'Renamed)
c

    message :: HsMatchContext GhcRn -> SDoc
    message :: HsMatchContext (GhcPass 'Renamed) -> SDoc
message (LamCaseAlt LamCaseVariant
LamCases) = SDoc
lcases_msg SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"expression"
    message (ArrowMatchCtxt (ArrowLamCaseAlt LamCaseVariant
LamCases)) =
      SDoc
lcases_msg SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"command"
    message HsMatchContext (GhcPass 'Renamed)
ctxt =
      SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"Empty list of alternatives in" SDoc -> SDoc -> SDoc
<+> HsMatchContext (GhcPass 'Renamed) -> SDoc
pp_ctxt HsMatchContext (GhcPass 'Renamed)
ctxt)
           Int
2 (String -> SDoc
text String
"Use EmptyCase to allow this")

    lcases_msg :: SDoc
lcases_msg =
      String -> SDoc
text String
"Empty list of alternatives is not allowed in \\cases"

{-
************************************************************************
*                                                                      *
\subsubsection{Guarded right-hand sides (GRHSs)}
*                                                                      *
************************************************************************
-}

rnGRHSs :: AnnoBody body
        => HsMatchContext GhcRn
        -> (LocatedA (body GhcPs) -> RnM (LocatedA (body GhcRn), FreeVars))
        -> GRHSs GhcPs (LocatedA (body GhcPs))
        -> RnM (GRHSs GhcRn (LocatedA (body GhcRn)), FreeVars)
rnGRHSs :: forall (body :: * -> *).
AnnoBody body =>
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> GRHSs GhcPs (LocatedA (body GhcPs))
-> RnM
     (GRHSs (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnGRHSs HsMatchContext (GhcPass 'Renamed)
ctxt LocatedA (body GhcPs)
-> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet)
rnBody (GRHSs XCGRHSs GhcPs (LocatedA (body GhcPs))
_ [LGRHS GhcPs (LocatedA (body GhcPs))]
grhss HsLocalBinds GhcPs
binds)
  = forall result.
HsLocalBinds GhcPs
-> (HsLocalBinds (GhcPass 'Renamed)
    -> NameSet -> RnM (result, NameSet))
-> RnM (result, NameSet)
rnLocalBindsAndThen HsLocalBinds GhcPs
binds   forall a b. (a -> b) -> a -> b
$ \ HsLocalBinds (GhcPass 'Renamed)
binds' NameSet
_ -> do
    ([GenLocated
   (SrcAnn NoEpAnns)
   (GRHS (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))))]
grhss', NameSet
fvGRHSs) <- forall a b. (a -> RnM (b, NameSet)) -> [a] -> RnM ([b], NameSet)
mapFvRn (forall (body :: * -> *).
AnnoBody body =>
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> LGRHS GhcPs (LocatedA (body GhcPs))
-> RnM
     (LGRHS (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnGRHS HsMatchContext (GhcPass 'Renamed)
ctxt LocatedA (body GhcPs)
-> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet)
rnBody) [LGRHS GhcPs (LocatedA (body GhcPs))]
grhss
    forall (m :: * -> *) a. Monad m => a -> m a
return (forall p body.
XCGRHSs p body -> [LGRHS p body] -> HsLocalBinds p -> GRHSs p body
GRHSs EpAnnComments
emptyComments [GenLocated
   (SrcAnn NoEpAnns)
   (GRHS (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))))]
grhss' HsLocalBinds (GhcPass 'Renamed)
binds', NameSet
fvGRHSs)

rnGRHS :: AnnoBody body
       => HsMatchContext GhcRn
       -> (LocatedA (body GhcPs) -> RnM (LocatedA (body GhcRn), FreeVars))
       -> LGRHS GhcPs (LocatedA (body GhcPs))
       -> RnM (LGRHS GhcRn (LocatedA (body GhcRn)), FreeVars)
rnGRHS :: forall (body :: * -> *).
AnnoBody body =>
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> LGRHS GhcPs (LocatedA (body GhcPs))
-> RnM
     (LGRHS (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnGRHS HsMatchContext (GhcPass 'Renamed)
ctxt LocatedA (body GhcPs)
-> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet)
rnBody = forall a b c ann.
(a -> TcM (b, c))
-> GenLocated (SrcSpanAnn' ann) a
-> TcM (GenLocated (SrcSpanAnn' ann) b, c)
wrapLocFstMA (forall (body :: * -> *).
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> GRHS GhcPs (LocatedA (body GhcPs))
-> RnM
     (GRHS (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnGRHS' HsMatchContext (GhcPass 'Renamed)
ctxt LocatedA (body GhcPs)
-> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet)
rnBody)

rnGRHS' :: HsMatchContext GhcRn
        -> (LocatedA (body GhcPs) -> RnM (LocatedA (body GhcRn), FreeVars))
        -> GRHS GhcPs (LocatedA (body GhcPs))
        -> RnM (GRHS GhcRn (LocatedA (body GhcRn)), FreeVars)
rnGRHS' :: forall (body :: * -> *).
HsMatchContext (GhcPass 'Renamed)
-> (LocatedA (body GhcPs)
    -> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet))
-> GRHS GhcPs (LocatedA (body GhcPs))
-> RnM
     (GRHS (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed))),
      NameSet)
rnGRHS' HsMatchContext (GhcPass 'Renamed)
ctxt LocatedA (body GhcPs)
-> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet)
rnBody (GRHS XCGRHS GhcPs (LocatedA (body GhcPs))
_ [GuardLStmt GhcPs]
guards LocatedA (body GhcPs)
rhs)
  = do  { Bool
pattern_guards_allowed <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.PatternGuards
        ; (([GenLocated
   SrcSpanAnnA
   (Stmt
      (GhcPass 'Renamed)
      (GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed))))]
guards', LocatedA (body (GhcPass 'Renamed))
rhs'), NameSet
fvs) <- forall (body :: * -> *) thing.
AnnoBody body =>
HsStmtContext (GhcPass 'Renamed)
-> (body GhcPs -> RnM (body (GhcPass 'Renamed), NameSet))
-> [LStmt GhcPs (LocatedA (body GhcPs))]
-> ([Name] -> RnM (thing, NameSet))
-> RnM
     (([LStmt (GhcPass 'Renamed) (LocatedA (body (GhcPass 'Renamed)))],
       thing),
      NameSet)
rnStmts (forall p. HsMatchContext p -> HsStmtContext p
PatGuard HsMatchContext (GhcPass 'Renamed)
ctxt) HsExpr GhcPs -> RnM (HsExpr (GhcPass 'Renamed), NameSet)
rnExpr [GuardLStmt GhcPs]
guards forall a b. (a -> b) -> a -> b
$ \ [Name]
_ ->
                                    LocatedA (body GhcPs)
-> RnM (LocatedA (body (GhcPass 'Renamed)), NameSet)
rnBody LocatedA (body GhcPs)
rhs

        ; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool
pattern_guards_allowed Bool -> Bool -> Bool
|| forall {l} {idL} {idR} {body}.
[GenLocated l (StmtLR idL idR body)] -> Bool
is_standard_guard [GenLocated
   SrcSpanAnnA
   (Stmt
      (GhcPass 'Renamed)
      (GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed))))]
guards') forall a b. (a -> b) -> a -> b
$
            let diag :: TcRnMessage
diag = forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage forall a b. (a -> b) -> a -> b
$
                  DiagnosticReason -> [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainDiagnostic DiagnosticReason
WarningWithoutFlag [GhcHint]
noHints (forall body.
(Outputable body,
 Anno (Stmt (GhcPass 'Renamed) body) ~ SrcSpanAnnA) =>
[LStmtLR (GhcPass 'Renamed) (GhcPass 'Renamed) body] -> SDoc
nonStdGuardErr [GenLocated
   SrcSpanAnnA
   (Stmt
      (GhcPass 'Renamed)
      (GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed))))]
guards')
            in TcRnMessage -> RnM ()
addDiagnostic TcRnMessage
diag

        ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p body.
XCGRHS p body -> [GuardLStmt p] -> body -> GRHS p body
GRHS forall a. EpAnn a
noAnn [GenLocated
   SrcSpanAnnA
   (Stmt
      (GhcPass 'Renamed)
      (GenLocated SrcSpanAnnA (HsExpr (GhcPass 'Renamed))))]
guards' LocatedA (body (GhcPass 'Renamed))
rhs', NameSet
fvs) }
  where
        -- Standard Haskell 1.4 guards are just a single boolean
        -- expression, rather than a list of qualifiers as in the
        -- Glasgow extension
    is_standard_guard :: [GenLocated l (StmtLR idL idR body)] -> Bool
is_standard_guard []                  = Bool
True
    is_standard_guard [L l
_ (BodyStmt {})] = Bool
True
    is_standard_guard [GenLocated l (StmtLR idL idR body)]
_                   = Bool
False

{-
*********************************************************
*                                                       *
        Source-code fixity declarations
*                                                       *
*********************************************************
-}

rnSrcFixityDecl :: HsSigCtxt -> FixitySig GhcPs -> RnM (FixitySig GhcRn)
-- Rename a fixity decl, so we can put
-- the renamed decl in the renamed syntax tree
-- Errors if the thing being fixed is not defined locally.
rnSrcFixityDecl :: HsSigCtxt -> FixitySig GhcPs -> RnM (FixitySig (GhcPass 'Renamed))
rnSrcFixityDecl HsSigCtxt
sig_ctxt = FixitySig GhcPs -> RnM (FixitySig (GhcPass 'Renamed))
rn_decl
  where
    rn_decl :: FixitySig GhcPs -> RnM (FixitySig GhcRn)
        -- GHC extension: look up both the tycon and data con
        -- for con-like things; hence returning a list
        -- If neither are in scope, report an error; otherwise
        -- return a fixity sig for each (slightly odd)
    rn_decl :: FixitySig GhcPs -> RnM (FixitySig (GhcPass 'Renamed))
rn_decl (FixitySig XFixitySig GhcPs
_ [LIdP GhcPs]
fnames Fixity
fixity)
      = do [GenLocated SrcSpanAnnN Name]
names <- forall (m :: * -> *) a b. Monad m => (a -> m [b]) -> [a] -> m [b]
concatMapM LocatedN RdrName -> RnM [GenLocated SrcSpanAnnN Name]
lookup_one [LIdP GhcPs]
fnames
           forall (m :: * -> *) a. Monad m => a -> m a
return (forall pass.
XFixitySig pass -> [LIdP pass] -> Fixity -> FixitySig pass
FixitySig NoExtField
noExtField [GenLocated SrcSpanAnnN Name]
names Fixity
fixity)

    lookup_one :: LocatedN RdrName -> RnM [LocatedN Name]
    lookup_one :: LocatedN RdrName -> RnM [GenLocated SrcSpanAnnN Name]
lookup_one (L SrcSpanAnnN
name_loc RdrName
rdr_name)
      = forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnN
name_loc forall a b. (a -> b) -> a -> b
$
                    -- This lookup will fail if the name is not defined in the
                    -- same binding group as this fixity declaration.
        do [(RdrName, Name)]
names <- HsSigCtxt -> SDoc -> RdrName -> RnM [(RdrName, Name)]
lookupLocalTcNames HsSigCtxt
sig_ctxt SDoc
what RdrName
rdr_name
           forall (m :: * -> *) a. Monad m => a -> m a
return [ forall l e. l -> e -> GenLocated l e
L SrcSpanAnnN
name_loc Name
name | (RdrName
_, Name
name) <- [(RdrName, Name)]
names ]
    what :: SDoc
what = String -> SDoc
text String
"fixity signature"

{-
************************************************************************
*                                                                      *
\subsection{Error messages}
*                                                                      *
************************************************************************
-}

dupSigDeclErr :: NonEmpty (LocatedN RdrName, Sig GhcPs) -> RnM ()
dupSigDeclErr :: NonEmpty (LocatedN RdrName, Sig GhcPs) -> RnM ()
dupSigDeclErr pairs :: NonEmpty (LocatedN RdrName, Sig GhcPs)
pairs@((L SrcSpanAnnN
loc RdrName
name, Sig GhcPs
sig) :| [(LocatedN RdrName, Sig GhcPs)]
_)
  = SrcSpan -> TcRnMessage -> RnM ()
addErrAt (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnN
loc) forall a b. (a -> b) -> a -> b
$ forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints forall a b. (a -> b) -> a -> b
$
    [SDoc] -> SDoc
vcat [ String -> SDoc
text String
"Duplicate" SDoc -> SDoc -> SDoc
<+> SDoc
what_it_is
           SDoc -> SDoc -> SDoc
<> String -> SDoc
text String
"s for" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (forall a. Outputable a => a -> SDoc
ppr RdrName
name)
         , String -> SDoc
text String
"at" SDoc -> SDoc -> SDoc
<+> [SDoc] -> SDoc
vcat (forall a b. (a -> b) -> [a] -> [b]
map forall a. Outputable a => a -> SDoc
ppr forall a b. (a -> b) -> a -> b
$ forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy SrcSpan -> SrcSpan -> Ordering
SrcLoc.leftmost_smallest
                                       forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> a
fst)
                                       forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> [a]
toList NonEmpty (LocatedN RdrName, Sig GhcPs)
pairs)
         ]
  where
    what_it_is :: SDoc
what_it_is = forall (p :: Pass). IsPass p => Sig (GhcPass p) -> SDoc
hsSigDoc Sig GhcPs
sig

misplacedSigErr :: LSig GhcRn -> RnM ()
misplacedSigErr :: LSig (GhcPass 'Renamed) -> RnM ()
misplacedSigErr (L SrcSpanAnnA
loc Sig (GhcPass 'Renamed)
sig)
  = SrcSpan -> TcRnMessage -> RnM ()
addErrAt (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
loc) forall a b. (a -> b) -> a -> b
$ forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints forall a b. (a -> b) -> a -> b
$
    [SDoc] -> SDoc
sep [String -> SDoc
text String
"Misplaced" SDoc -> SDoc -> SDoc
<+> forall (p :: Pass). IsPass p => Sig (GhcPass p) -> SDoc
hsSigDoc Sig (GhcPass 'Renamed)
sig SDoc -> SDoc -> SDoc
<> SDoc
colon, forall a. Outputable a => a -> SDoc
ppr Sig (GhcPass 'Renamed)
sig]

defaultSigErr :: Sig GhcPs -> TcRnMessage
defaultSigErr :: Sig GhcPs -> TcRnMessage
defaultSigErr Sig GhcPs
sig = forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints forall a b. (a -> b) -> a -> b
$
  [SDoc] -> SDoc
vcat [ SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"Unexpected default signature:")
         Int
2 (forall a. Outputable a => a -> SDoc
ppr Sig GhcPs
sig)
       , String -> SDoc
text String
"Use DefaultSignatures to enable default signatures" ]

bindInHsBootFileErr :: LHsBindLR GhcRn GhcPs -> RnM ()
bindInHsBootFileErr :: LHsBindLR (GhcPass 'Renamed) GhcPs -> RnM ()
bindInHsBootFileErr (L SrcSpanAnnA
loc HsBindLR (GhcPass 'Renamed) GhcPs
_)
  = SrcSpan -> TcRnMessage -> RnM ()
addErrAt (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
loc) forall a b. (a -> b) -> a -> b
$ forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints forall a b. (a -> b) -> a -> b
$
      [SDoc] -> SDoc
vcat [ String -> SDoc
text String
"Bindings in hs-boot files are not allowed" ]

nonStdGuardErr :: (Outputable body,
                   Anno (Stmt GhcRn body) ~ SrcSpanAnnA)
               => [LStmtLR GhcRn GhcRn body] -> SDoc
nonStdGuardErr :: forall body.
(Outputable body,
 Anno (Stmt (GhcPass 'Renamed) body) ~ SrcSpanAnnA) =>
[LStmtLR (GhcPass 'Renamed) (GhcPass 'Renamed) body] -> SDoc
nonStdGuardErr [LStmtLR (GhcPass 'Renamed) (GhcPass 'Renamed) body]
guards
  = SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"accepting non-standard pattern guards (use PatternGuards to suppress this message)")
       Int
4 (forall a. Outputable a => [a] -> SDoc
interpp'SP [LStmtLR (GhcPass 'Renamed) (GhcPass 'Renamed) body]
guards)

dupMinimalSigErr :: [LSig GhcPs] -> RnM ()
dupMinimalSigErr :: [LSig GhcPs] -> RnM ()
dupMinimalSigErr sigs :: [LSig GhcPs]
sigs@(L SrcSpanAnnA
loc Sig GhcPs
_ : [LSig GhcPs]
_)
  = SrcSpan -> TcRnMessage -> RnM ()
addErrAt (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
loc) forall a b. (a -> b) -> a -> b
$ forall a. (Diagnostic a, Typeable a) => a -> TcRnMessage
TcRnUnknownMessage forall a b. (a -> b) -> a -> b
$ [GhcHint] -> SDoc -> DiagnosticMessage
mkPlainError [GhcHint]
noHints forall a b. (a -> b) -> a -> b
$
    [SDoc] -> SDoc
vcat [ String -> SDoc
text String
"Multiple minimal complete definitions"
         , String -> SDoc
text String
"at" SDoc -> SDoc -> SDoc
<+> [SDoc] -> SDoc
vcat (forall a b. (a -> b) -> [a] -> [b]
map forall a. Outputable a => a -> SDoc
ppr forall a b. (a -> b) -> a -> b
$ forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy SrcSpan -> SrcSpan -> Ordering
SrcLoc.leftmost_smallest forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA [LSig GhcPs]
sigs)
         , String -> SDoc
text String
"Combine alternative minimal complete definitions with `|'" ]
dupMinimalSigErr [] = forall a. String -> a
panic String
"dupMinimalSigErr"