{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DeriveFunctor       #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE ViewPatterns        #-}
{-# LANGUAGE DisambiguateRecordFields #-}

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

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

Renaming of patterns

Basically dependency analysis.

Handles @Match@, @GRHSs@, @HsExpr@, and @Qualifier@ datatypes.  In
general, all of these functions return a renamed thing, and a set of
free variables.
-}
module GHC.Rename.Pat (-- main entry points
              rnPat, rnPats, rnBindPat, rnPatAndThen,

              NameMaker, applyNameMaker,     -- a utility for making names:
              localRecNameMaker, topRecNameMaker,  --   sometimes we want to make local names,
                                             --   sometimes we want to make top (qualified) names.
              isTopRecNameMaker,

              rnHsRecFields, HsRecFieldContext(..),
              rnHsRecUpdFields,

              -- CpsRn monad
              CpsRn, liftCps, liftCpsWithCont,

              -- Literals
              rnLit, rnOverLit,
             ) where

-- ENH: thin imports to only what is necessary for patterns

import GHC.Prelude

import {-# SOURCE #-} GHC.Rename.Expr ( rnLExpr )
import {-# SOURCE #-} GHC.Rename.Splice ( rnSplicePat )

import GHC.Hs
import GHC.Tc.Errors.Types
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.Zonk   ( hsOverLitName )
import GHC.Rename.Env
import GHC.Rename.Fixity
import GHC.Rename.Utils    ( newLocalBndrRn, bindLocalNames
                           , warnUnusedMatches, newLocalBndrRn
                           , checkUnusedRecordWildcard
                           , checkDupNames, checkDupAndShadowedNames
                           , wrapGenSpan, genHsApps, genLHsVar, genHsIntegralLit, warnForallIdentifier )
import GHC.Rename.HsType
import GHC.Builtin.Names
import GHC.Types.Avail ( greNameMangledName )
import GHC.Types.Error
import GHC.Types.Name
import GHC.Types.Name.Set
import GHC.Types.Name.Reader
import GHC.Types.Basic
import GHC.Types.SourceText
import GHC.Utils.Misc
import GHC.Data.List.SetOps( removeDups )
import GHC.Utils.Outputable
import GHC.Utils.Panic.Plain
import GHC.Types.SrcLoc
import GHC.Types.Literal   ( inCharRange )
import GHC.Builtin.Types   ( nilDataCon )
import GHC.Core.DataCon
import GHC.Driver.Session ( getDynFlags, xopt_DuplicateRecordFields )
import qualified GHC.LanguageExtensions as LangExt

import Control.Monad       ( when, ap, guard, forM, unless )
import qualified Data.List.NonEmpty as NE
import Data.Maybe
import Data.Ratio
import GHC.Types.FieldLabel (DuplicateRecordFields(..))

{-
*********************************************************
*                                                      *
        The CpsRn Monad
*                                                      *
*********************************************************

Note [CpsRn monad]
~~~~~~~~~~~~~~~~~~
The CpsRn monad uses continuation-passing style to support this
style of programming:

        do { ...
           ; ns <- bindNames rs
           ; ...blah... }

   where rs::[RdrName], ns::[Name]

The idea is that '...blah...'
  a) sees the bindings of ns
  b) returns the free variables it mentions
     so that bindNames can report unused ones

In particular,
    mapM rnPatAndThen [p1, p2, p3]
has a *left-to-right* scoping: it makes the binders in
p1 scope over p2,p3.
-}

newtype CpsRn b = CpsRn { forall b.
CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
unCpsRn :: forall r. (b -> RnM (r, FreeVars))
                                            -> RnM (r, FreeVars) }
        deriving (forall a b. a -> CpsRn b -> CpsRn a
forall a b. (a -> b) -> CpsRn a -> CpsRn b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> CpsRn b -> CpsRn a
$c<$ :: forall a b. a -> CpsRn b -> CpsRn a
fmap :: forall a b. (a -> b) -> CpsRn a -> CpsRn b
$cfmap :: forall a b. (a -> b) -> CpsRn a -> CpsRn b
Functor)
        -- See Note [CpsRn monad]

instance Applicative CpsRn where
    pure :: forall a. a -> CpsRn a
pure a
x = forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\a -> RnM (r, FreeVars)
k -> a -> RnM (r, FreeVars)
k a
x)
    <*> :: forall a b. CpsRn (a -> b) -> CpsRn a -> CpsRn b
(<*>) = forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Monad CpsRn where
  (CpsRn forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
m) >>= :: forall a b. CpsRn a -> (a -> CpsRn b) -> CpsRn b
>>= a -> CpsRn b
mk = forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\b -> RnM (r, FreeVars)
k -> forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
m (\a
v -> forall b.
CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
unCpsRn (a -> CpsRn b
mk a
v) b -> RnM (r, FreeVars)
k))

runCps :: CpsRn a -> RnM (a, FreeVars)
runCps :: forall a. CpsRn a -> RnM (a, FreeVars)
runCps (CpsRn forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
m) = forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
m (\a
r -> forall (m :: * -> *) a. Monad m => a -> m a
return (a
r, FreeVars
emptyFVs))

liftCps :: RnM a -> CpsRn a
liftCps :: forall a. RnM a -> CpsRn a
liftCps RnM a
rn_thing = forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\a -> RnM (r, FreeVars)
k -> RnM a
rn_thing forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> RnM (r, FreeVars)
k)

liftCpsFV :: RnM (a, FreeVars) -> CpsRn a
liftCpsFV :: forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV RnM (a, FreeVars)
rn_thing = forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\a -> RnM (r, FreeVars)
k -> do { (a
v,FreeVars
fvs1) <- RnM (a, FreeVars)
rn_thing
                                     ; (r
r,FreeVars
fvs2) <- a -> RnM (r, FreeVars)
k a
v
                                     ; forall (m :: * -> *) a. Monad m => a -> m a
return (r
r, FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) })

liftCpsWithCont :: (forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)) -> CpsRn b
liftCpsWithCont :: forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
liftCpsWithCont = forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn

wrapSrcSpanCps :: (a -> CpsRn b) -> LocatedA a -> CpsRn (LocatedA b)
-- Set the location, and also wrap it around the value returned
wrapSrcSpanCps :: forall a b. (a -> CpsRn b) -> LocatedA a -> CpsRn (LocatedA b)
wrapSrcSpanCps a -> CpsRn b
fn (L SrcSpanAnnA
loc a
a)
  = forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\LocatedA b -> RnM (r, FreeVars)
k -> forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcSpanAnnA
loc forall a b. (a -> b) -> a -> b
$
                 forall b.
CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
unCpsRn (a -> CpsRn b
fn a
a) forall a b. (a -> b) -> a -> b
$ \b
v ->
                 LocatedA b -> RnM (r, FreeVars)
k (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc b
v))

lookupConCps :: LocatedN RdrName -> CpsRn (LocatedN Name)
lookupConCps :: GenLocated (SrcAnn NameAnn) RdrName -> CpsRn (LocatedN Name)
lookupConCps GenLocated (SrcAnn NameAnn) RdrName
con_rdr
  = forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\LocatedN Name -> RnM (r, FreeVars)
k -> do { LocatedN Name
con_name <- forall ann.
GenLocated (SrcSpanAnn' ann) RdrName
-> TcRn (GenLocated (SrcSpanAnn' ann) Name)
lookupLocatedOccRnConstr GenLocated (SrcAnn NameAnn) RdrName
con_rdr
                    ; (r
r, FreeVars
fvs) <- LocatedN Name -> RnM (r, FreeVars)
k LocatedN Name
con_name
                    ; forall (m :: * -> *) a. Monad m => a -> m a
return (r
r, FreeVars -> Name -> FreeVars
addOneFV FreeVars
fvs (forall l e. GenLocated l e -> e
unLoc LocatedN Name
con_name)) })
    -- We add the constructor name to the free vars
    -- See Note [Patterns are uses]

{-
Note [Patterns are uses]
~~~~~~~~~~~~~~~~~~~~~~~~
Consider
  module Foo( f, g ) where
  data T = T1 | T2

  f T1 = True
  f T2 = False

  g _ = T1

Arguably we should report T2 as unused, even though it appears in a
pattern, because it never occurs in a constructed position.
See #7336.
However, implementing this in the face of pattern synonyms would be
less straightforward, since given two pattern synonyms

  pattern P1 <- P2
  pattern P2 <- ()

we need to observe the dependency between P1 and P2 so that type
checking can be done in the correct order (just like for value
bindings). Dependencies between bindings is analyzed in the renamer,
where we don't know yet whether P2 is a constructor or a pattern
synonym. So for now, we do report conid occurrences in patterns as
uses.

*********************************************************
*                                                      *
        Name makers
*                                                      *
*********************************************************

Externally abstract type of name makers,
which is how you go from a RdrName to a Name
-}

data NameMaker
  = LamMk       -- Lambdas
      Bool      -- True <=> report unused bindings
                --   (even if True, the warning only comes out
                --    if -Wunused-matches is on)

  | LetMk       -- Let bindings, incl top level
                -- Do *not* check for unused bindings
      TopLevelFlag
      MiniFixityEnv

topRecNameMaker :: MiniFixityEnv -> NameMaker
topRecNameMaker :: MiniFixityEnv -> NameMaker
topRecNameMaker MiniFixityEnv
fix_env = TopLevelFlag -> MiniFixityEnv -> NameMaker
LetMk TopLevelFlag
TopLevel MiniFixityEnv
fix_env

isTopRecNameMaker :: NameMaker -> Bool
isTopRecNameMaker :: NameMaker -> Bool
isTopRecNameMaker (LetMk TopLevelFlag
TopLevel MiniFixityEnv
_) = Bool
True
isTopRecNameMaker NameMaker
_ = Bool
False

localRecNameMaker :: MiniFixityEnv -> NameMaker
localRecNameMaker :: MiniFixityEnv -> NameMaker
localRecNameMaker MiniFixityEnv
fix_env = TopLevelFlag -> MiniFixityEnv -> NameMaker
LetMk TopLevelFlag
NotTopLevel MiniFixityEnv
fix_env

matchNameMaker :: HsMatchContext a -> NameMaker
matchNameMaker :: forall a. HsMatchContext a -> NameMaker
matchNameMaker HsMatchContext a
ctxt = Bool -> NameMaker
LamMk Bool
report_unused
  where
    -- Do not report unused names in interactive contexts
    -- i.e. when you type 'x <- e' at the GHCi prompt
    report_unused :: Bool
report_unused = case HsMatchContext a
ctxt of
                      StmtCtxt (HsDoStmt HsDoFlavour
GhciStmtCtxt) -> Bool
False
                      -- also, don't warn in pattern quotes, as there
                      -- is no RHS where the variables can be used!
                      HsMatchContext a
ThPatQuote            -> Bool
False
                      HsMatchContext a
_                     -> Bool
True

newPatLName :: NameMaker -> LocatedN RdrName -> CpsRn (LocatedN Name)
newPatLName :: NameMaker
-> GenLocated (SrcAnn NameAnn) RdrName -> CpsRn (LocatedN Name)
newPatLName NameMaker
name_maker rdr_name :: GenLocated (SrcAnn NameAnn) RdrName
rdr_name@(L SrcAnn NameAnn
loc RdrName
_)
  = do { Name
name <- NameMaker -> GenLocated (SrcAnn NameAnn) RdrName -> CpsRn Name
newPatName NameMaker
name_maker GenLocated (SrcAnn NameAnn) RdrName
rdr_name
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L SrcAnn NameAnn
loc Name
name) }

newPatName :: NameMaker -> LocatedN RdrName -> CpsRn Name
newPatName :: NameMaker -> GenLocated (SrcAnn NameAnn) RdrName -> CpsRn Name
newPatName (LamMk Bool
report_unused) GenLocated (SrcAnn NameAnn) RdrName
rdr_name
  = forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\ Name -> RnM (r, FreeVars)
thing_inside ->
        do { GenLocated (SrcAnn NameAnn) RdrName -> RnM ()
warnForallIdentifier GenLocated (SrcAnn NameAnn) RdrName
rdr_name
           ; Name
name <- GenLocated (SrcAnn NameAnn) RdrName -> RnM Name
newLocalBndrRn GenLocated (SrcAnn NameAnn) RdrName
rdr_name
           ; (r
res, FreeVars
fvs) <- forall a. [Name] -> RnM a -> RnM a
bindLocalNames [Name
name] (Name -> RnM (r, FreeVars)
thing_inside Name
name)
           ; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
report_unused forall a b. (a -> b) -> a -> b
$ [Name] -> FreeVars -> RnM ()
warnUnusedMatches [Name
name] FreeVars
fvs
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (r
res, Name
name Name -> FreeVars -> FreeVars
`delFV` FreeVars
fvs) })

newPatName (LetMk TopLevelFlag
is_top MiniFixityEnv
fix_env) GenLocated (SrcAnn NameAnn) RdrName
rdr_name
  = forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\ Name -> RnM (r, FreeVars)
thing_inside ->
        do { GenLocated (SrcAnn NameAnn) RdrName -> RnM ()
warnForallIdentifier GenLocated (SrcAnn NameAnn) RdrName
rdr_name
           ; Name
name <- case TopLevelFlag
is_top of
                       TopLevelFlag
NotTopLevel -> GenLocated (SrcAnn NameAnn) RdrName -> RnM Name
newLocalBndrRn GenLocated (SrcAnn NameAnn) RdrName
rdr_name
                       TopLevelFlag
TopLevel    -> GenLocated (SrcAnn NameAnn) RdrName -> RnM Name
newTopSrcBinder GenLocated (SrcAnn NameAnn) RdrName
rdr_name
           ; forall a. [Name] -> RnM a -> RnM a
bindLocalNames [Name
name] forall a b. (a -> b) -> a -> b
$
                 -- Do *not* use bindLocalNameFV here;
                 --   see Note [View pattern usage]
                 -- For the TopLevel case
                 --   see Note [bindLocalNames for an External name]
             forall a. MiniFixityEnv -> [Name] -> RnM a -> RnM a
addLocalFixities MiniFixityEnv
fix_env [Name
name] forall a b. (a -> b) -> a -> b
$
             Name -> RnM (r, FreeVars)
thing_inside Name
name })

{- Note [bindLocalNames for an External name]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the TopLevel case, the use of bindLocalNames here is somewhat
suspicious because it binds a top-level External name in the
LocalRdrEnv.  c.f. Note [LocalRdrEnv] in GHC.Types.Name.Reader.

However, this only happens when renaming the LHS (only) of a top-level
pattern binding.  Even though this only the LHS, we need to bring the
binder into scope in the pattern itself in case the binder is used in
subsequent view patterns.  A bit bizarre, something like
  (x, Just y <- f x) = e

Anyway, bindLocalNames does work, and the binding only exists for the
duration of the pattern; then the top-level name is added to the
global env before going on to the RHSes (see GHC.Rename.Module).

Note [View pattern usage]
~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
  let (r, (r -> x)) = x in ...
Here the pattern binds 'r', and then uses it *only* in the view pattern.
We want to "see" this use, and in let-bindings we collect all uses and
report unused variables at the binding level. So we must use bindLocalNames
here, *not* bindLocalNameFV.  #3943.


Note [Don't report shadowing for pattern synonyms]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is one special context where a pattern doesn't introduce any new binders -
pattern synonym declarations. Therefore we don't check to see if pattern
variables shadow existing identifiers as they are never bound to anything
and have no scope.

Without this check, there would be quite a cryptic warning that the `x`
in the RHS of the pattern synonym declaration shadowed the top level `x`.

```
x :: ()
x = ()

pattern P x = Just x
```

See #12615 for some more examples.

Note [Handling overloaded and rebindable patterns]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Overloaded paterns and rebindable patterns are desugared in the renamer
using the HsPatExpansion mechanism detailed in:
Note [Rebindable syntax and HsExpansion]
The approach is similar to that of expressions, which is further detailed
in Note [Handling overloaded and rebindable constructs] in GHC.Rename.Expr.

Here are the patterns that are currently desugared in this way:

* ListPat (list patterns [p1,p2,p3])
  When (and only when) OverloadedLists is on, desugar to a view pattern:
    [p1, p2, p3]
  ==>
    toList -> [p1, p2, p3]
              ^^^^^^^^^^^^ built-in (non-overloaded) list pattern
  NB: the type checker and desugarer still see ListPat,
      but to them it always means the built-in list pattern.
  See Note [Desugaring overloaded list patterns] below for more details.

We expect to add to this list as we deal with more patterns via the expansion
mechanism.

Note [Desugaring overloaded list patterns]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If OverloadedLists is enabled, we desugar a list pattern to a view pattern:

  [p1, p2, p3]
==>
  toList -> [p1, p2, p3]

This happens directly in the renamer, using the HsPatExpansion mechanism
detailed in Note [Rebindable syntax and HsExpansion].

Note that we emit a special view pattern: we additionally keep track of an
inverse to the pattern.
See Note [Invertible view patterns] in GHC.Tc.TyCl.PatSyn for details.

== Wrinkle ==

This is all fine, except in one very specific case:
  - when RebindableSyntax is off,
  - and the type being matched on is already a list type.

In this case, it is undesirable to desugar an overloaded list pattern into
a view pattern. To illustrate, consider the following program:

> {-# LANGUAGE OverloadedLists #-}
>
> f []    = True
> f (_:_) = False

Without any special logic, the pattern `[]` is desugared to `(toList -> [])`,
whereas `(_:_)` remains a constructor pattern. This implies that the argument
of `f` is necessarily a list (even though `OverloadedLists` is enabled).
After desugaring the overloaded list pattern `[]`, and type-checking, we obtain:

> f :: [a] -> Bool
> f (toList -> []) = True
> f (_:_)          = False

The pattern match checker then warns that the pattern `[]` is not covered,
as it isn't able to look through view patterns.
We can see that this is silly: as we are matching on a list, `toList` doesn't
actually do anything. So we ignore it, and desugar the pattern to an explicit
list pattern, instead of a view pattern.

Note however that this is not necessarily sound, because it is possible to have
a list `l` such that `toList l` is not the same as `l`.
This can happen with an overlapping instance, such as the following:

instance {-# OVERLAPPING #-} IsList [Int] where
  type Item [Int] = Int
  toList = reverse
  fromList = reverse

We make the assumption that no such instance exists, in order to avoid worsening
pattern-match warnings (see #14547).

*********************************************************
*                                                      *
        External entry points
*                                                      *
*********************************************************

There are various entry points to renaming patterns, depending on
 (1) whether the names created should be top-level names or local names
 (2) whether the scope of the names is entirely given in a continuation
     (e.g., in a case or lambda, but not in a let or at the top-level,
      because of the way mutually recursive bindings are handled)
 (3) whether the a type signature in the pattern can bind
        lexically-scoped type variables (for unpacking existential
        type vars in data constructors)
 (4) whether we do duplicate and unused variable checking
 (5) whether there are fixity declarations associated with the names
     bound by the patterns that need to be brought into scope with them.

 Rather than burdening the clients of this module with all of these choices,
 we export the three points in this design space that we actually need:
-}

-- ----------- Entry point 1: rnPats -------------------
-- Binds local names; the scope of the bindings is entirely in the thing_inside
--   * allows type sigs to bind type vars
--   * local namemaker
--   * unused and duplicate checking
--   * no fixities
rnPats :: HsMatchContext GhcRn -- for error messages
       -> [LPat GhcPs]
       -> ([LPat GhcRn] -> RnM (a, FreeVars))
       -> RnM (a, FreeVars)
rnPats :: forall a.
HsMatchContext GhcRn
-> [LPat GhcPs]
-> ([LPat GhcRn] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnPats HsMatchContext GhcRn
ctxt [LPat GhcPs]
pats [LPat GhcRn] -> RnM (a, FreeVars)
thing_inside
  = do  { (GlobalRdrEnv, LocalRdrEnv)
envs_before <- TcRn (GlobalRdrEnv, LocalRdrEnv)
getRdrEnvs

          -- (1) rename the patterns, bringing into scope all of the term variables
          -- (2) then do the thing inside.
        ; forall b.
CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
unCpsRn (NameMaker -> [LPat GhcPs] -> CpsRn [LPat GhcRn]
rnLPatsAndThen (forall a. HsMatchContext a -> NameMaker
matchNameMaker HsMatchContext GhcRn
ctxt) [LPat GhcPs]
pats) forall a b. (a -> b) -> a -> b
$ \ [GenLocated SrcSpanAnnA (Pat GhcRn)]
pats' -> do
        { -- Check for duplicated and shadowed names
          -- Must do this *after* renaming the patterns
          -- See Note [Collect binders only after renaming] in GHC.Hs.Utils
          -- Because we don't bind the vars all at once, we can't
          --    check incrementally for duplicates;
          -- Nor can we check incrementally for shadowing, else we'll
          --    complain *twice* about duplicates e.g. f (x,x) = ...
          --
          -- See Note [Don't report shadowing for pattern synonyms]
        ; let bndrs :: [IdP GhcRn]
bndrs = forall p. CollectPass p => CollectFlag p -> [LPat p] -> [IdP p]
collectPatsBinders forall p. CollectFlag p
CollNoDictBinders [GenLocated SrcSpanAnnA (Pat GhcRn)]
pats'
        ; forall a. SDoc -> TcM a -> TcM a
addErrCtxt SDoc
doc_pat forall a b. (a -> b) -> a -> b
$
          if forall p. HsMatchContext p -> Bool
isPatSynCtxt HsMatchContext GhcRn
ctxt
             then [Name] -> RnM ()
checkDupNames [IdP GhcRn]
bndrs
             else (GlobalRdrEnv, LocalRdrEnv) -> [Name] -> RnM ()
checkDupAndShadowedNames (GlobalRdrEnv, LocalRdrEnv)
envs_before [IdP GhcRn]
bndrs
        ; [LPat GhcRn] -> RnM (a, FreeVars)
thing_inside [GenLocated SrcSpanAnnA (Pat GhcRn)]
pats' } }
  where
    doc_pat :: SDoc
doc_pat = String -> SDoc
text String
"In" SDoc -> SDoc -> SDoc
<+> forall p.
(Outputable (IdP p), UnXRec p) =>
HsMatchContext p -> SDoc
pprMatchContext HsMatchContext GhcRn
ctxt

rnPat :: HsMatchContext GhcRn -- for error messages
      -> LPat GhcPs
      -> (LPat GhcRn -> RnM (a, FreeVars))
      -> RnM (a, FreeVars)     -- Variables bound by pattern do not
                               -- appear in the result FreeVars
rnPat :: forall a.
HsMatchContext GhcRn
-> LPat GhcPs
-> (LPat GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnPat HsMatchContext GhcRn
ctxt LPat GhcPs
pat LPat GhcRn -> RnM (a, FreeVars)
thing_inside
  = forall a.
HsMatchContext GhcRn
-> [LPat GhcPs]
-> ([LPat GhcRn] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnPats HsMatchContext GhcRn
ctxt [LPat GhcPs
pat] (\[LPat GhcRn]
pats' -> let [LPat GhcRn
pat'] = [LPat GhcRn]
pats' in LPat GhcRn -> RnM (a, FreeVars)
thing_inside LPat GhcRn
pat')

applyNameMaker :: NameMaker -> LocatedN RdrName -> RnM (LocatedN Name)
applyNameMaker :: NameMaker
-> GenLocated (SrcAnn NameAnn) RdrName -> RnM (LocatedN Name)
applyNameMaker NameMaker
mk GenLocated (SrcAnn NameAnn) RdrName
rdr = do { (LocatedN Name
n, FreeVars
_fvs) <- forall a. CpsRn a -> RnM (a, FreeVars)
runCps (NameMaker
-> GenLocated (SrcAnn NameAnn) RdrName -> CpsRn (LocatedN Name)
newPatLName NameMaker
mk GenLocated (SrcAnn NameAnn) RdrName
rdr)
                           ; forall (m :: * -> *) a. Monad m => a -> m a
return LocatedN Name
n }

-- ----------- Entry point 2: rnBindPat -------------------
-- Binds local names; in a recursive scope that involves other bound vars
--      e.g let { (x, Just y) = e1; ... } in ...
--   * does NOT allows type sig to bind type vars
--   * local namemaker
--   * no unused and duplicate checking
--   * fixities might be coming in
rnBindPat :: NameMaker
          -> LPat GhcPs
          -> RnM (LPat GhcRn, FreeVars)
   -- Returned FreeVars are the free variables of the pattern,
   -- of course excluding variables bound by this pattern

rnBindPat :: NameMaker -> LPat GhcPs -> RnM (LPat GhcRn, FreeVars)
rnBindPat NameMaker
name_maker LPat GhcPs
pat = forall a. CpsRn a -> RnM (a, FreeVars)
runCps (NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
name_maker LPat GhcPs
pat)

{-
*********************************************************
*                                                      *
        The main event
*                                                      *
*********************************************************
-}

-- ----------- Entry point 3: rnLPatAndThen -------------------
-- General version: parametrized by how you make new names

rnLPatsAndThen :: NameMaker -> [LPat GhcPs] -> CpsRn [LPat GhcRn]
rnLPatsAndThen :: NameMaker -> [LPat GhcPs] -> CpsRn [LPat GhcRn]
rnLPatsAndThen NameMaker
mk = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk)
  -- Despite the map, the monad ensures that each pattern binds
  -- variables that may be mentioned in subsequent patterns in the list

--------------------
-- The workhorse
rnLPatAndThen :: NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen :: NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
nm LPat GhcPs
lpat = forall a b. (a -> CpsRn b) -> LocatedA a -> CpsRn (LocatedA b)
wrapSrcSpanCps (NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen NameMaker
nm) LPat GhcPs
lpat

rnPatAndThen :: NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen :: NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen NameMaker
_  (WildPat XWildPat GhcPs
_)   = forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XWildPat p -> Pat p
WildPat NoExtField
noExtField)
rnPatAndThen NameMaker
mk (ParPat XParPat GhcPs
x LHsToken "(" GhcPs
lpar LPat GhcPs
pat LHsToken ")" GhcPs
rpar) =
  do { GenLocated SrcSpanAnnA (Pat GhcRn)
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
     ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p.
XParPat p -> LHsToken "(" p -> LPat p -> LHsToken ")" p -> Pat p
ParPat XParPat GhcPs
x LHsToken "(" GhcPs
lpar GenLocated SrcSpanAnnA (Pat GhcRn)
pat' LHsToken ")" GhcPs
rpar) }
rnPatAndThen NameMaker
mk (LazyPat XLazyPat GhcPs
_ LPat GhcPs
pat) = do { GenLocated SrcSpanAnnA (Pat GhcRn)
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
                                     ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XLazyPat p -> LPat p -> Pat p
LazyPat NoExtField
noExtField GenLocated SrcSpanAnnA (Pat GhcRn)
pat') }
rnPatAndThen NameMaker
mk (BangPat XBangPat GhcPs
_ LPat GhcPs
pat) = do { GenLocated SrcSpanAnnA (Pat GhcRn)
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
                                     ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XBangPat p -> LPat p -> Pat p
BangPat NoExtField
noExtField GenLocated SrcSpanAnnA (Pat GhcRn)
pat') }
rnPatAndThen NameMaker
mk (VarPat XVarPat GhcPs
x (L SrcAnn NameAnn
l RdrName
rdr))
    = do { SrcSpan
loc <- forall a. RnM a -> CpsRn a
liftCps TcRn SrcSpan
getSrcSpanM
         ; Name
name <- NameMaker -> GenLocated (SrcAnn NameAnn) RdrName -> CpsRn Name
newPatName NameMaker
mk (forall l e. l -> e -> GenLocated l e
L (forall ann. SrcSpan -> SrcAnn ann
noAnnSrcSpan SrcSpan
loc) RdrName
rdr)
         ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XVarPat p -> LIdP p -> Pat p
VarPat XVarPat GhcPs
x (forall l e. l -> e -> GenLocated l e
L SrcAnn NameAnn
l Name
name)) }
     -- we need to bind pattern variables for view pattern expressions
     -- (e.g. in the pattern (x, x -> y) x needs to be bound in the rhs of the tuple)

rnPatAndThen NameMaker
mk (SigPat XSigPat GhcPs
_ LPat GhcPs
pat HsPatSigType (NoGhcTc GhcPs)
sig)
  -- When renaming a pattern type signature (e.g. f (a :: T) = ...), it is
  -- important to rename its type signature _before_ renaming the rest of the
  -- pattern, so that type variables are first bound by the _outermost_ pattern
  -- type signature they occur in. This keeps the type checker happy when
  -- pattern type signatures happen to be nested (#7827)
  --
  -- f ((Just (x :: a) :: Maybe a)
  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~^       `a' is first bound here
  -- ~~~~~~~~~~~~~~~^                   the same `a' then used here
  = do { HsPatSigType GhcRn
sig' <- HsPatSigType GhcPs -> CpsRn (HsPatSigType GhcRn)
rnHsPatSigTypeAndThen HsPatSigType (NoGhcTc GhcPs)
sig
       ; GenLocated SrcSpanAnnA (Pat GhcRn)
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XSigPat p -> LPat p -> HsPatSigType (NoGhcTc p) -> Pat p
SigPat NoExtField
noExtField GenLocated SrcSpanAnnA (Pat GhcRn)
pat' HsPatSigType GhcRn
sig' ) }
  where
    rnHsPatSigTypeAndThen :: HsPatSigType GhcPs -> CpsRn (HsPatSigType GhcRn)
    rnHsPatSigTypeAndThen :: HsPatSigType GhcPs -> CpsRn (HsPatSigType GhcRn)
rnHsPatSigTypeAndThen HsPatSigType GhcPs
sig = forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
liftCpsWithCont (forall a.
HsPatSigTypeScoping
-> HsDocContext
-> HsPatSigType GhcPs
-> (HsPatSigType GhcRn -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnHsPatSigType HsPatSigTypeScoping
AlwaysBind HsDocContext
PatCtx HsPatSigType GhcPs
sig)

rnPatAndThen NameMaker
mk (LitPat XLitPat GhcPs
x HsLit GhcPs
lit)
  | HsString XHsString GhcPs
src FastString
s <- HsLit GhcPs
lit
  = do { Bool
ovlStr <- forall a. RnM a -> CpsRn a
liftCps (forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.OverloadedStrings)
       ; if Bool
ovlStr
         then NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen NameMaker
mk
                           (LocatedAn NoEpAnns (HsOverLit GhcPs)
-> Maybe (SyntaxExpr GhcPs) -> EpAnn [AddEpAnn] -> Pat GhcPs
mkNPat (forall a an. a -> LocatedAn an a
noLocA (SourceText -> FastString -> HsOverLit GhcPs
mkHsIsString XHsString GhcPs
src FastString
s))
                                      forall a. Maybe a
Nothing forall a. EpAnn a
noAnn)
         else CpsRn (Pat GhcRn)
normal_lit }
  | Bool
otherwise = CpsRn (Pat GhcRn)
normal_lit
  where
    normal_lit :: CpsRn (Pat GhcRn)
normal_lit = do { forall a. RnM a -> CpsRn a
liftCps (forall p. HsLit p -> RnM ()
rnLit HsLit GhcPs
lit); forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XLitPat p -> HsLit p -> Pat p
LitPat XLitPat GhcPs
x (forall (p1 :: Pass) (p2 :: Pass).
HsLit (GhcPass p1) -> HsLit (GhcPass p2)
convertLit HsLit GhcPs
lit)) }

rnPatAndThen NameMaker
_ (NPat XNPat GhcPs
x (L SrcAnn NoEpAnns
l HsOverLit GhcPs
lit) Maybe (SyntaxExpr GhcPs)
mb_neg SyntaxExpr GhcPs
_eq)
  = do { (HsOverLit GhcRn
lit', Maybe (HsExpr GhcRn)
mb_neg') <- forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV forall a b. (a -> b) -> a -> b
$ forall t.
HsOverLit t
-> RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
rnOverLit HsOverLit GhcPs
lit
       ; Maybe SyntaxExprRn
mb_neg' -- See Note [Negative zero]
           <- let negative :: IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
negative = do { (SyntaxExprRn
neg, FreeVars
fvs) <- Name -> RnM (SyntaxExpr GhcRn, FreeVars)
lookupSyntax Name
negateName
                                ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just SyntaxExprRn
neg, FreeVars
fvs) }
                  positive :: IOEnv (Env TcGblEnv TcLclEnv) (Maybe a, FreeVars)
positive = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Maybe a
Nothing, FreeVars
emptyFVs)
              in forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV forall a b. (a -> b) -> a -> b
$ case (Maybe (SyntaxExpr GhcPs)
mb_neg , Maybe (HsExpr GhcRn)
mb_neg') of
                                  (Maybe NoExtField
Nothing, Just HsExpr GhcRn
_ ) -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
negative
                                  (Just NoExtField
_ , Maybe (HsExpr GhcRn)
Nothing) -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
negative
                                  (Maybe NoExtField
Nothing, Maybe (HsExpr GhcRn)
Nothing) -> forall {a}. IOEnv (Env TcGblEnv TcLclEnv) (Maybe a, FreeVars)
positive
                                  (Just NoExtField
_ , Just HsExpr GhcRn
_ ) -> forall {a}. IOEnv (Env TcGblEnv TcLclEnv) (Maybe a, FreeVars)
positive
       ; SyntaxExprRn
eq' <- forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV forall a b. (a -> b) -> a -> b
$ Name -> RnM (SyntaxExpr GhcRn, FreeVars)
lookupSyntax Name
eqName
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p.
XNPat p
-> XRec p (HsOverLit p)
-> Maybe (SyntaxExpr p)
-> SyntaxExpr p
-> Pat p
NPat XNPat GhcPs
x (forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
l HsOverLit GhcRn
lit') Maybe SyntaxExprRn
mb_neg' SyntaxExprRn
eq') }

rnPatAndThen NameMaker
mk (NPlusKPat XNPlusKPat GhcPs
_ XRec GhcPs (IdP GhcPs)
rdr (L SrcAnn NoEpAnns
l HsOverLit GhcPs
lit) HsOverLit GhcPs
_ SyntaxExpr GhcPs
_ SyntaxExpr GhcPs
_ )
  = do { Name
new_name <- NameMaker -> GenLocated (SrcAnn NameAnn) RdrName -> CpsRn Name
newPatName NameMaker
mk (forall a1 a2. LocatedAn a1 a2 -> LocatedN a2
l2n XRec GhcPs (IdP GhcPs)
rdr)
       ; (HsOverLit GhcRn
lit', Maybe (HsExpr GhcRn)
_) <- forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV forall a b. (a -> b) -> a -> b
$ forall t.
HsOverLit t
-> RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
rnOverLit HsOverLit GhcPs
lit -- See Note [Negative zero]
                                                -- We skip negateName as
                                                -- negative zero doesn't make
                                                -- sense in n + k patterns
       ; SyntaxExprRn
minus <- forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV forall a b. (a -> b) -> a -> b
$ Name -> RnM (SyntaxExpr GhcRn, FreeVars)
lookupSyntax Name
minusName
       ; SyntaxExprRn
ge    <- forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV forall a b. (a -> b) -> a -> b
$ Name -> RnM (SyntaxExpr GhcRn, FreeVars)
lookupSyntax Name
geName
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p.
XNPlusKPat p
-> LIdP p
-> XRec p (HsOverLit p)
-> HsOverLit p
-> SyntaxExpr p
-> SyntaxExpr p
-> Pat p
NPlusKPat NoExtField
noExtField (forall l e. l -> e -> GenLocated l e
L (forall ann. SrcSpan -> SrcAnn ann
noAnnSrcSpan forall a b. (a -> b) -> a -> b
$ Name -> SrcSpan
nameSrcSpan Name
new_name) Name
new_name)
                                      (forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
l HsOverLit GhcRn
lit') HsOverLit GhcRn
lit' SyntaxExprRn
ge SyntaxExprRn
minus) }
                -- The Report says that n+k patterns must be in Integral

rnPatAndThen NameMaker
mk (AsPat XAsPat GhcPs
_ XRec GhcPs (IdP GhcPs)
rdr LPat GhcPs
pat)
  = do { LocatedN Name
new_name <- NameMaker
-> GenLocated (SrcAnn NameAnn) RdrName -> CpsRn (LocatedN Name)
newPatLName NameMaker
mk XRec GhcPs (IdP GhcPs)
rdr
       ; GenLocated SrcSpanAnnA (Pat GhcRn)
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XAsPat p -> LIdP p -> LPat p -> Pat p
AsPat NoExtField
noExtField LocatedN Name
new_name GenLocated SrcSpanAnnA (Pat GhcRn)
pat') }

rnPatAndThen NameMaker
mk p :: Pat GhcPs
p@(ViewPat XViewPat GhcPs
_ LHsExpr GhcPs
expr LPat GhcPs
pat)
  = do { forall a. RnM a -> CpsRn a
liftCps forall a b. (a -> b) -> a -> b
$ do { Bool
vp_flag <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.ViewPatterns
                      ; Bool -> TcRnMessage -> RnM ()
checkErr Bool
vp_flag (Pat GhcPs -> TcRnMessage
TcRnIllegalViewPattern Pat GhcPs
p) }
         -- Because of the way we're arranging the recursive calls,
         -- this will be in the right context
       ; GenLocated SrcSpanAnnA (HsExpr GhcRn)
expr' <- forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV forall a b. (a -> b) -> a -> b
$ LHsExpr GhcPs -> RnM (LHsExpr GhcRn, FreeVars)
rnLExpr LHsExpr GhcPs
expr
       ; GenLocated SrcSpanAnnA (Pat GhcRn)
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
       -- Note: at this point the PreTcType in ty can only be a placeHolder
       -- ; return (ViewPat expr' pat' ty) }

       -- Note: we can't cook up an inverse for an arbitrary view pattern,
       -- so we pass 'Nothing'.
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat forall a. Maybe a
Nothing GenLocated SrcSpanAnnA (HsExpr GhcRn)
expr' GenLocated SrcSpanAnnA (Pat GhcRn)
pat') }

rnPatAndThen NameMaker
mk (ConPat XConPat GhcPs
_ XRec GhcPs (ConLikeP GhcPs)
con HsConPatDetails GhcPs
args)
   -- rnConPatAndThen takes care of reconstructing the pattern
   -- The pattern for the empty list needs to be replaced by an empty explicit list pattern when overloaded lists is turned on.
  = case forall l e. GenLocated l e -> e
unLoc XRec GhcPs (ConLikeP GhcPs)
con forall a. Eq a => a -> a -> Bool
== Name -> RdrName
nameRdrName (DataCon -> Name
dataConName DataCon
nilDataCon) of
      Bool
True    -> do { Bool
ol_flag <- forall a. RnM a -> CpsRn a
liftCps forall a b. (a -> b) -> a -> b
$ forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.OverloadedLists
                    ; if Bool
ol_flag then NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen NameMaker
mk (forall p. XListPat p -> [LPat p] -> Pat p
ListPat forall a. EpAnn a
noAnn [])
                                 else NameMaker
-> GenLocated (SrcAnn NameAnn) RdrName
-> HsConPatDetails GhcPs
-> CpsRn (Pat GhcRn)
rnConPatAndThen NameMaker
mk XRec GhcPs (ConLikeP GhcPs)
con HsConPatDetails GhcPs
args}
      Bool
False   -> NameMaker
-> GenLocated (SrcAnn NameAnn) RdrName
-> HsConPatDetails GhcPs
-> CpsRn (Pat GhcRn)
rnConPatAndThen NameMaker
mk XRec GhcPs (ConLikeP GhcPs)
con HsConPatDetails GhcPs
args

rnPatAndThen NameMaker
mk (ListPat XListPat GhcPs
_ [LPat GhcPs]
pats)
  = do { Bool
opt_OverloadedLists  <- forall a. RnM a -> CpsRn a
liftCps forall a b. (a -> b) -> a -> b
$ forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.OverloadedLists
       ; [GenLocated SrcSpanAnnA (Pat GhcRn)]
pats' <- NameMaker -> [LPat GhcPs] -> CpsRn [LPat GhcRn]
rnLPatsAndThen NameMaker
mk [LPat GhcPs]
pats
       ; if Bool -> Bool
not Bool
opt_OverloadedLists
         then forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XListPat p -> [LPat p] -> Pat p
ListPat NoExtField
noExtField [GenLocated SrcSpanAnnA (Pat GhcRn)]
pats')
         else
    -- If OverloadedLists is enabled, desugar to a view pattern.
    -- See Note [Desugaring overloaded list patterns]
    do { (Name
to_list_name,FreeVars
_)     <- forall a. RnM a -> CpsRn a
liftCps forall a b. (a -> b) -> a -> b
$ Name -> RnM (Name, FreeVars)
lookupSyntaxName Name
toListName
       -- Use 'fromList' as proof of invertibility of the view pattern.
       -- See Note [Invertible view patterns] in GHC.Tc.TyCl.PatSyn
       ; (Name
from_list_n_name,FreeVars
_) <- forall a. RnM a -> CpsRn a
liftCps forall a b. (a -> b) -> a -> b
$ Name -> RnM (Name, FreeVars)
lookupSyntaxName Name
fromListNName
       ; let
           lit_n :: IntegralLit
lit_n   = forall a. Integral a => a -> IntegralLit
mkIntegralLit (forall (t :: * -> *) a. Foldable t => t a -> Int
length [LPat GhcPs]
pats)
           hs_lit :: GenLocated SrcSpanAnnA (HsExpr GhcRn)
hs_lit  = forall an. IntegralLit -> LocatedAn an (HsExpr GhcRn)
genHsIntegralLit IntegralLit
lit_n
           inverse :: HsExpr GhcRn
inverse = Name -> [LHsExpr GhcRn] -> HsExpr GhcRn
genHsApps Name
from_list_n_name [GenLocated SrcSpanAnnA (HsExpr GhcRn)
hs_lit]
           rn_list_pat :: Pat GhcRn
rn_list_pat  = forall p. XListPat p -> [LPat p] -> Pat p
ListPat NoExtField
noExtField [GenLocated SrcSpanAnnA (Pat GhcRn)]
pats'
           exp_expr :: LHsExpr GhcRn
exp_expr     = Name -> LHsExpr GhcRn
genLHsVar Name
to_list_name
           exp_list_pat :: Pat GhcRn
exp_list_pat = forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat (forall a. a -> Maybe a
Just HsExpr GhcRn
inverse) LHsExpr GhcRn
exp_expr (forall a an. a -> LocatedAn an a
wrapGenSpan Pat GhcRn
rn_list_pat)
       ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Pat GhcRn -> Pat GhcRn -> Pat GhcRn
mkExpandedPat Pat GhcRn
rn_list_pat Pat GhcRn
exp_list_pat }}

rnPatAndThen NameMaker
mk (TuplePat XTuplePat GhcPs
_ [LPat GhcPs]
pats Boxity
boxed)
  = do { [GenLocated SrcSpanAnnA (Pat GhcRn)]
pats' <- NameMaker -> [LPat GhcPs] -> CpsRn [LPat GhcRn]
rnLPatsAndThen NameMaker
mk [LPat GhcPs]
pats
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XTuplePat p -> [LPat p] -> Boxity -> Pat p
TuplePat NoExtField
noExtField [GenLocated SrcSpanAnnA (Pat GhcRn)]
pats' Boxity
boxed) }

rnPatAndThen NameMaker
mk (SumPat XSumPat GhcPs
_ LPat GhcPs
pat Int
alt Int
arity)
  = do { GenLocated SrcSpanAnnA (Pat GhcRn)
pat <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall p. XSumPat p -> LPat p -> Int -> Int -> Pat p
SumPat NoExtField
noExtField GenLocated SrcSpanAnnA (Pat GhcRn)
pat Int
alt Int
arity)
       }

-- If a splice has been run already, just rename the result.
rnPatAndThen NameMaker
mk (SplicePat XSplicePat GhcPs
x (HsSpliced XSpliced GhcPs
x2 ThModFinalizers
mfs (HsSplicedPat Pat GhcPs
pat)))
  = forall p. XSplicePat p -> HsSplice p -> Pat p
SplicePat XSplicePat GhcPs
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall id.
XSpliced id -> ThModFinalizers -> HsSplicedThing id -> HsSplice id
HsSpliced XSpliced GhcPs
x2 ThModFinalizers
mfs forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall id. Pat id -> HsSplicedThing id
HsSplicedPat forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen NameMaker
mk Pat GhcPs
pat

rnPatAndThen NameMaker
mk (SplicePat XSplicePat GhcPs
_ HsSplice GhcPs
splice)
  = do { Either (Pat GhcPs) (Pat GhcRn)
eith <- forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV forall a b. (a -> b) -> a -> b
$ HsSplice GhcPs -> RnM (Either (Pat GhcPs) (Pat GhcRn), FreeVars)
rnSplicePat HsSplice GhcPs
splice
       ; case Either (Pat GhcPs) (Pat GhcRn)
eith of   -- See Note [rnSplicePat] in GHC.Rename.Splice
           Left  Pat GhcPs
not_yet_renamed -> NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen NameMaker
mk Pat GhcPs
not_yet_renamed
           Right Pat GhcRn
already_renamed -> forall (m :: * -> *) a. Monad m => a -> m a
return Pat GhcRn
already_renamed }

--------------------
rnConPatAndThen :: NameMaker
                -> LocatedN RdrName    -- the constructor
                -> HsConPatDetails GhcPs
                -> CpsRn (Pat GhcRn)

rnConPatAndThen :: NameMaker
-> GenLocated (SrcAnn NameAnn) RdrName
-> HsConPatDetails GhcPs
-> CpsRn (Pat GhcRn)
rnConPatAndThen NameMaker
mk GenLocated (SrcAnn NameAnn) RdrName
con (PrefixCon [HsPatSigType (NoGhcTc GhcPs)]
tyargs [LPat GhcPs]
pats)
  = do  { LocatedN Name
con' <- GenLocated (SrcAnn NameAnn) RdrName -> CpsRn (LocatedN Name)
lookupConCps GenLocated (SrcAnn NameAnn) RdrName
con
        ; forall a. RnM a -> CpsRn a
liftCps RnM ()
check_lang_exts
        ; [HsPatSigType GhcRn]
tyargs' <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [HsPatSigType (NoGhcTc GhcPs)]
tyargs forall a b. (a -> b) -> a -> b
$ \HsPatSigType GhcPs
t ->
            forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
liftCpsWithCont forall a b. (a -> b) -> a -> b
$ forall r.
HsDocContext
-> HsPatSigType GhcPs
-> (HsPatSigType GhcRn -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
rnHsPatSigTypeBindingVars HsDocContext
HsTypeCtx HsPatSigType GhcPs
t
        ; [GenLocated SrcSpanAnnA (Pat GhcRn)]
pats' <- NameMaker -> [LPat GhcPs] -> CpsRn [LPat GhcRn]
rnLPatsAndThen NameMaker
mk [LPat GhcPs]
pats
        ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ConPat
            { pat_con_ext :: XConPat GhcRn
pat_con_ext = NoExtField
noExtField
            , pat_con :: XRec GhcRn (ConLikeP GhcRn)
pat_con = LocatedN Name
con'
            , pat_args :: HsConPatDetails GhcRn
pat_args = forall tyarg arg rec.
[tyarg] -> [arg] -> HsConDetails tyarg arg rec
PrefixCon [HsPatSigType GhcRn]
tyargs' [GenLocated SrcSpanAnnA (Pat GhcRn)]
pats'
            }
        }
  where
    check_lang_exts :: RnM ()
    check_lang_exts :: RnM ()
check_lang_exts = do
      Bool
scoped_tyvars <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.ScopedTypeVariables
      Bool
type_app      <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.TypeApplications
      forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool
scoped_tyvars Bool -> Bool -> Bool
&& Bool
type_app) forall a b. (a -> b) -> a -> b
$
        case forall a. [a] -> Maybe a
listToMaybe [HsPatSigType (NoGhcTc GhcPs)]
tyargs of
          Maybe (HsPatSigType GhcPs)
Nothing    -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
          Just HsPatSigType GhcPs
tyarg -> TcRnMessage -> RnM ()
addErr 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 -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"Illegal visible type application in a pattern:"
                    SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (Char -> SDoc
char Char
'@' SDoc -> SDoc -> SDoc
<> forall a. Outputable a => a -> SDoc
ppr HsPatSigType GhcPs
tyarg))
               Int
2 (String -> SDoc
text String
"Both ScopedTypeVariables and TypeApplications are"
                    SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"required to use this feature")

rnConPatAndThen NameMaker
mk GenLocated (SrcAnn NameAnn) RdrName
con (InfixCon LPat GhcPs
pat1 LPat GhcPs
pat2)
  = do  { LocatedN Name
con' <- GenLocated (SrcAnn NameAnn) RdrName -> CpsRn (LocatedN Name)
lookupConCps GenLocated (SrcAnn NameAnn) RdrName
con
        ; GenLocated SrcSpanAnnA (Pat GhcRn)
pat1' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat1
        ; GenLocated SrcSpanAnnA (Pat GhcRn)
pat2' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen NameMaker
mk LPat GhcPs
pat2
        ; Fixity
fixity <- forall a. RnM a -> CpsRn a
liftCps forall a b. (a -> b) -> a -> b
$ Name -> RnM Fixity
lookupFixityRn (forall l e. GenLocated l e -> e
unLoc LocatedN Name
con')
        ; forall a. RnM a -> CpsRn a
liftCps forall a b. (a -> b) -> a -> b
$ LocatedN Name
-> Fixity -> LPat GhcRn -> LPat GhcRn -> RnM (Pat GhcRn)
mkConOpPatRn LocatedN Name
con' Fixity
fixity GenLocated SrcSpanAnnA (Pat GhcRn)
pat1' GenLocated SrcSpanAnnA (Pat GhcRn)
pat2' }

rnConPatAndThen NameMaker
mk GenLocated (SrcAnn NameAnn) RdrName
con (RecCon HsRecFields GhcPs (LPat GhcPs)
rpats)
  = do  { LocatedN Name
con' <- GenLocated (SrcAnn NameAnn) RdrName -> CpsRn (LocatedN Name)
lookupConCps GenLocated (SrcAnn NameAnn) RdrName
con
        ; HsRecFields GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn))
rpats' <- NameMaker
-> LocatedN Name
-> HsRecFields GhcPs (LPat GhcPs)
-> CpsRn (HsRecFields GhcRn (LPat GhcRn))
rnHsRecPatsAndThen NameMaker
mk LocatedN Name
con' HsRecFields GhcPs (LPat GhcPs)
rpats
        ; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ConPat
            { pat_con_ext :: XConPat GhcRn
pat_con_ext = NoExtField
noExtField
            , pat_con :: XRec GhcRn (ConLikeP GhcRn)
pat_con = LocatedN Name
con'
            , pat_args :: HsConPatDetails GhcRn
pat_args = forall tyarg arg rec. rec -> HsConDetails tyarg arg rec
RecCon HsRecFields GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn))
rpats'
            }
        }

checkUnusedRecordWildcardCps :: SrcSpan -> Maybe [Name] -> CpsRn ()
checkUnusedRecordWildcardCps :: SrcSpan -> Maybe [Name] -> CpsRn ()
checkUnusedRecordWildcardCps SrcSpan
loc Maybe [Name]
dotdot_names =
  forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\() -> RnM (r, FreeVars)
thing -> do
                    (r
r, FreeVars
fvs) <- () -> RnM (r, FreeVars)
thing ()
                    SrcSpan -> FreeVars -> Maybe [Name] -> RnM ()
checkUnusedRecordWildcard SrcSpan
loc FreeVars
fvs Maybe [Name]
dotdot_names
                    forall (m :: * -> *) a. Monad m => a -> m a
return (r
r, FreeVars
fvs) )
--------------------
rnHsRecPatsAndThen :: NameMaker
                   -> LocatedN Name      -- Constructor
                   -> HsRecFields GhcPs (LPat GhcPs)
                   -> CpsRn (HsRecFields GhcRn (LPat GhcRn))
rnHsRecPatsAndThen :: NameMaker
-> LocatedN Name
-> HsRecFields GhcPs (LPat GhcPs)
-> CpsRn (HsRecFields GhcRn (LPat GhcRn))
rnHsRecPatsAndThen NameMaker
mk (L SrcAnn NameAnn
_ Name
con)
     hs_rec_fields :: HsRecFields GhcPs (LPat GhcPs)
hs_rec_fields@(HsRecFields { rec_dotdot :: forall p arg. HsRecFields p arg -> Maybe (Located Int)
rec_dotdot = Maybe (Located Int)
dd })
  = do { [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
      (GenLocated SrcSpanAnnA (Pat GhcPs)))]
flds <- forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV forall a b. (a -> b) -> a -> b
$ forall arg.
HsRecFieldContext
-> (SrcSpan -> RdrName -> arg)
-> HsRecFields GhcPs (LocatedA arg)
-> RnM ([LHsRecField GhcRn (LocatedA arg)], FreeVars)
rnHsRecFields (Name -> HsRecFieldContext
HsRecFieldPat Name
con) forall {p} {ann}.
(XVarPat p ~ NoExtField,
 XRec p (IdP p) ~ GenLocated (SrcAnn ann) (IdP p)) =>
SrcSpan -> IdP p -> Pat p
mkVarPat
                                            HsRecFields GhcPs (LPat GhcPs)
hs_rec_fields
       ; [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
      (GenLocated SrcSpanAnnA (Pat GhcRn)))]
flds' <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
      (GenLocated SrcSpanAnnA (Pat GhcPs))),
 Int)
-> CpsRn
     (GenLocated
        SrcSpanAnnA
        (HsFieldBind
           (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
           (GenLocated SrcSpanAnnA (Pat GhcRn))))
rn_field ([GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
      (GenLocated SrcSpanAnnA (Pat GhcPs)))]
flds forall a b. [a] -> [b] -> [(a, b)]
`zip` [Int
1..])
       ; Maybe [Name] -> CpsRn ()
check_unused_wildcard (forall {p} {l} {lhs} {l}.
CollectPass p =>
[GenLocated l (HsFieldBind lhs (XRec p (Pat p)))]
-> GenLocated l Int -> [IdP p]
implicit_binders [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
      (GenLocated SrcSpanAnnA (Pat GhcRn)))]
flds' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Located Int)
dd)
       ; forall (m :: * -> *) a. Monad m => a -> m a
return (HsRecFields { rec_flds :: [LHsRecField GhcRn (GenLocated SrcSpanAnnA (Pat GhcRn))]
rec_flds = [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
      (GenLocated SrcSpanAnnA (Pat GhcRn)))]
flds', rec_dotdot :: Maybe (Located Int)
rec_dotdot = Maybe (Located Int)
dd }) }
  where
    mkVarPat :: SrcSpan -> IdP p -> Pat p
mkVarPat SrcSpan
l IdP p
n = forall p. XVarPat p -> LIdP p -> Pat p
VarPat NoExtField
noExtField (forall l e. l -> e -> GenLocated l e
L (forall ann. SrcSpan -> SrcAnn ann
noAnnSrcSpan SrcSpan
l) IdP p
n)
    rn_field :: (GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
      (GenLocated SrcSpanAnnA (Pat GhcPs))),
 Int)
-> CpsRn
     (GenLocated
        SrcSpanAnnA
        (HsFieldBind
           (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
           (GenLocated SrcSpanAnnA (Pat GhcRn))))
rn_field (L SrcSpanAnnA
l HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (Pat GhcPs))
fld, Int
n') =
      do { GenLocated SrcSpanAnnA (Pat GhcRn)
arg' <- NameMaker -> LPat GhcPs -> CpsRn (LPat GhcRn)
rnLPatAndThen (forall {a} {l}.
Ord a =>
Maybe (GenLocated l a) -> NameMaker -> a -> NameMaker
nested_mk Maybe (Located Int)
dd NameMaker
mk Int
n') (forall lhs rhs. HsFieldBind lhs rhs -> rhs
hfbRHS HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (Pat GhcPs))
fld)
         ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (HsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
  (GenLocated SrcSpanAnnA (Pat GhcPs))
fld { hfbRHS :: GenLocated SrcSpanAnnA (Pat GhcRn)
hfbRHS = GenLocated SrcSpanAnnA (Pat GhcRn)
arg' })) }

    loc :: SrcSpan
loc = forall b a. b -> (a -> b) -> Maybe a -> b
maybe SrcSpan
noSrcSpan forall l e. GenLocated l e -> l
getLoc Maybe (Located Int)
dd

    -- Get the arguments of the implicit binders
    implicit_binders :: [GenLocated l (HsFieldBind lhs (XRec p (Pat p)))]
-> GenLocated l Int -> [IdP p]
implicit_binders [GenLocated l (HsFieldBind lhs (XRec p (Pat p)))]
fs (forall l e. GenLocated l e -> e
unLoc -> Int
n) = forall p. CollectPass p => CollectFlag p -> [LPat p] -> [IdP p]
collectPatsBinders forall p. CollectFlag p
CollNoDictBinders [XRec p (Pat p)]
implicit_pats
      where
        implicit_pats :: [XRec p (Pat p)]
implicit_pats = forall a b. (a -> b) -> [a] -> [b]
map (forall lhs rhs. HsFieldBind lhs rhs -> rhs
hfbRHS forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc) (forall a. Int -> [a] -> [a]
drop Int
n [GenLocated l (HsFieldBind lhs (XRec p (Pat p)))]
fs)

    -- Don't warn for let P{..} = ... in ...
    check_unused_wildcard :: Maybe [Name] -> CpsRn ()
check_unused_wildcard = case NameMaker
mk of
                              LetMk{} -> forall a b. a -> b -> a
const (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                              LamMk{} -> SrcSpan -> Maybe [Name] -> CpsRn ()
checkUnusedRecordWildcardCps SrcSpan
loc

        -- Suppress unused-match reporting for fields introduced by ".."
    nested_mk :: Maybe (GenLocated l a) -> NameMaker -> a -> NameMaker
nested_mk Maybe (GenLocated l a)
Nothing  NameMaker
mk                    a
_  = NameMaker
mk
    nested_mk (Just GenLocated l a
_) mk :: NameMaker
mk@(LetMk {})         a
_  = NameMaker
mk
    nested_mk (Just (forall l e. GenLocated l e -> e
unLoc -> a
n)) (LamMk Bool
report_unused) a
n'
      = Bool -> NameMaker
LamMk (Bool
report_unused Bool -> Bool -> Bool
&& (a
n' forall a. Ord a => a -> a -> Bool
<= a
n))


{- *********************************************************************
*                                                                      *
              Generating code for HsPatExpanded
      See Note [Handling overloaded and rebindable constructs]
*                                                                      *
********************************************************************* -}

-- | Build a 'HsPatExpansion' out of an extension constructor,
--   and the two components of the expansion: original and
--   desugared patterns
mkExpandedPat
  :: Pat GhcRn -- ^ source pattern
  -> Pat GhcRn -- ^ expanded pattern
  -> Pat GhcRn -- ^ suitably wrapped 'HsPatExpansion'
mkExpandedPat :: Pat GhcRn -> Pat GhcRn -> Pat GhcRn
mkExpandedPat Pat GhcRn
a Pat GhcRn
b = forall p. XXPat p -> Pat p
XPat (forall a b. a -> b -> HsPatExpansion a b
HsPatExpanded Pat GhcRn
a Pat GhcRn
b)

{-
************************************************************************
*                                                                      *
        Record fields
*                                                                      *
************************************************************************
-}

data HsRecFieldContext
  = HsRecFieldCon Name
  | HsRecFieldPat Name
  | HsRecFieldUpd

rnHsRecFields
    :: forall arg.
       HsRecFieldContext
    -> (SrcSpan -> RdrName -> arg)
         -- When punning, use this to build a new field
    -> HsRecFields GhcPs (LocatedA arg)
    -> RnM ([LHsRecField GhcRn (LocatedA arg)], FreeVars)

-- This surprisingly complicated pass
--   a) looks up the field name (possibly using disambiguation)
--   b) fills in puns and dot-dot stuff
-- When we've finished, we've renamed the LHS, but not the RHS,
-- of each x=e binding
--
-- This is used for record construction and pattern-matching, but not updates.

rnHsRecFields :: forall arg.
HsRecFieldContext
-> (SrcSpan -> RdrName -> arg)
-> HsRecFields GhcPs (LocatedA arg)
-> RnM ([LHsRecField GhcRn (LocatedA arg)], FreeVars)
rnHsRecFields HsRecFieldContext
ctxt SrcSpan -> RdrName -> arg
mk_arg (HsRecFields { rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField GhcPs (LocatedA arg)]
flds, rec_dotdot :: forall p arg. HsRecFields p arg -> Maybe (Located Int)
rec_dotdot = Maybe (Located Int)
dotdot })
  = do { Bool
pun_ok      <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.NamedFieldPuns
       ; Bool
disambig_ok <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DisambiguateRecordFields
       ; let parent :: Maybe Name
parent = forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
disambig_ok forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe Name
mb_con
       ; [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)) (LocatedA arg))]
flds1  <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Bool
-> Maybe Name
-> LHsRecField GhcPs (LocatedA arg)
-> RnM (LHsRecField GhcRn (LocatedA arg))
rn_fld Bool
pun_ok Maybe Name
parent) [LHsRecField GhcPs (LocatedA arg)]
flds
       ; forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (TcRnMessage -> RnM ()
addErr forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecFieldContext -> NonEmpty RdrName -> TcRnMessage
dupFieldErr HsRecFieldContext
ctxt) [NonEmpty RdrName]
dup_flds
       ; [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)) (LocatedA arg))]
dotdot_flds <- Maybe (Located Int)
-> Maybe Name
-> [LHsRecField GhcRn (LocatedA arg)]
-> RnM [LHsRecField GhcRn (LocatedA arg)]
rn_dotdot Maybe (Located Int)
dotdot Maybe Name
mb_con [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)) (LocatedA arg))]
flds1
       ; let all_flds :: [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)) (LocatedA arg))]
all_flds | forall (t :: * -> *) a. Foldable t => t a -> Bool
null [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)) (LocatedA arg))]
dotdot_flds = [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)) (LocatedA arg))]
flds1
                      | Bool
otherwise        = [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)) (LocatedA arg))]
flds1 forall a. [a] -> [a] -> [a]
++ [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)) (LocatedA arg))]
dotdot_flds
       ; forall (m :: * -> *) a. Monad m => a -> m a
return ([GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)) (LocatedA arg))]
all_flds, [Name] -> FreeVars
mkFVs (forall arg. [LHsRecField GhcRn arg] -> [Name]
getFieldIds [GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)) (LocatedA arg))]
all_flds)) }
  where
    mb_con :: Maybe Name
mb_con = case HsRecFieldContext
ctxt of
                HsRecFieldCon Name
con  -> forall a. a -> Maybe a
Just Name
con
                HsRecFieldPat Name
con  -> forall a. a -> Maybe a
Just Name
con
                HsRecFieldContext
_ {- update -}     -> forall a. Maybe a
Nothing

    rn_fld :: Bool -> Maybe Name -> LHsRecField GhcPs (LocatedA arg)
           -> RnM (LHsRecField GhcRn (LocatedA arg))
    rn_fld :: Bool
-> Maybe Name
-> LHsRecField GhcPs (LocatedA arg)
-> RnM (LHsRecField GhcRn (LocatedA arg))
rn_fld Bool
pun_ok Maybe Name
parent (L SrcSpanAnnA
l
                           (HsFieldBind
                              { hfbLHS :: forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS =
                                  (L SrcAnn NoEpAnns
loc (FieldOcc XCFieldOcc GhcPs
_ (L SrcAnn NameAnn
ll RdrName
lbl)))
                              , hfbRHS :: forall lhs rhs. HsFieldBind lhs rhs -> rhs
hfbRHS = LocatedA arg
arg
                              , hfbPun :: forall lhs rhs. HsFieldBind lhs rhs -> Bool
hfbPun      = Bool
pun }))
      = do { Name
sel <- forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcAnn NoEpAnns
loc forall a b. (a -> b) -> a -> b
$ Maybe Name -> RdrName -> RnM Name
lookupRecFieldOcc Maybe Name
parent RdrName
lbl
           ; LocatedA arg
arg' <- if Bool
pun
                     then do { Bool -> TcRnMessage -> RnM ()
checkErr Bool
pun_ok (Located RdrName -> TcRnMessage
TcRnIllegalFieldPunning (forall l e. l -> e -> GenLocated l e
L (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcAnn NoEpAnns
loc) RdrName
lbl))
                               -- Discard any module qualifier (#11662)
                             ; let arg_rdr :: RdrName
arg_rdr = OccName -> RdrName
mkRdrUnqual (RdrName -> OccName
rdrNameOcc RdrName
lbl)
                             ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L (forall a ann. SrcSpanAnn' a -> SrcAnn ann
l2l SrcAnn NoEpAnns
loc) (SrcSpan -> RdrName -> arg
mk_arg (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcAnn NoEpAnns
loc) RdrName
arg_rdr)) }
                     else forall (m :: * -> *) a. Monad m => a -> m a
return LocatedA arg
arg
           ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (HsFieldBind
                             { hfbAnn :: XHsFieldBind (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
hfbAnn = forall a. EpAnn a
noAnn
                             , hfbLHS :: GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)
hfbLHS = (forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
loc (forall pass. XCFieldOcc pass -> XRec pass RdrName -> FieldOcc pass
FieldOcc Name
sel (forall l e. l -> e -> GenLocated l e
L SrcAnn NameAnn
ll RdrName
lbl)))
                             , hfbRHS :: LocatedA arg
hfbRHS = LocatedA arg
arg'
                             , hfbPun :: Bool
hfbPun      = Bool
pun })) }


    rn_dotdot :: Maybe (Located Int)      -- See Note [DotDot fields] in GHC.Hs.Pat
              -> Maybe Name -- The constructor (Nothing for an
                                --    out of scope constructor)
              -> [LHsRecField GhcRn (LocatedA arg)] -- Explicit fields
              -> RnM ([LHsRecField GhcRn (LocatedA arg)])   -- Field Labels we need to fill in
    rn_dotdot :: Maybe (Located Int)
-> Maybe Name
-> [LHsRecField GhcRn (LocatedA arg)]
-> RnM [LHsRecField GhcRn (LocatedA arg)]
rn_dotdot (Just (L SrcSpan
loc Int
n)) (Just Name
con) [LHsRecField GhcRn (LocatedA arg)]
flds -- ".." on record construction / pat match
      | Bool -> Bool
not (Name -> Bool
isUnboundName Name
con) -- This test is because if the constructor
                                -- isn't in scope the constructor lookup will add
                                -- an error but still return an unbound name. We
                                -- don't want that to screw up the dot-dot fill-in stuff.
      = forall a. HasCallStack => Bool -> a -> a
assert ([LHsRecField GhcRn (LocatedA arg)]
flds forall a. [a] -> Int -> Bool
`lengthIs` Int
n) forall a b. (a -> b) -> a -> b
$
        do { Bool
dd_flag <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.RecordWildCards
           ; Bool -> TcRnMessage -> RnM ()
checkErr Bool
dd_flag (HsRecFieldContext -> TcRnMessage
needFlagDotDot HsRecFieldContext
ctxt)
           ; (GlobalRdrEnv
rdr_env, LocalRdrEnv
lcl_env) <- TcRn (GlobalRdrEnv, LocalRdrEnv)
getRdrEnvs
           ; [FieldLabel]
con_fields <- Name -> RnM [FieldLabel]
lookupConstructorFields Name
con
           ; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FieldLabel]
con_fields) (TcRnMessage -> RnM ()
addErr (Name -> TcRnMessage
TcRnIllegalWildcardsInConstructor Name
con))
           ; let present_flds :: OccSet
present_flds = [OccName] -> OccSet
mkOccSet forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map RdrName -> OccName
rdrNameOcc (forall p arg. UnXRec p => [LHsRecField p arg] -> [RdrName]
getFieldLbls [LHsRecField GhcRn (LocatedA arg)]
flds)

                   -- For constructor uses (but not patterns)
                   -- the arg should be in scope locally;
                   -- i.e. not top level or imported
                   -- Eg.  data R = R { x,y :: Int }
                   --      f x = R { .. }   -- Should expand to R {x=x}, not R{x=x,y=y}
                 arg_in_scope :: OccName -> Bool
arg_in_scope OccName
lbl = OccName -> RdrName
mkRdrUnqual OccName
lbl RdrName -> LocalRdrEnv -> Bool
`elemLocalRdrEnv` LocalRdrEnv
lcl_env

                 ([FieldLabel]
dot_dot_fields, [GlobalRdrElt]
dot_dot_gres)
                        = forall a b. [(a, b)] -> ([a], [b])
unzip [ (FieldLabel
fl, GlobalRdrElt
gre)
                                | FieldLabel
fl <- [FieldLabel]
con_fields
                                , let lbl :: OccName
lbl = FastString -> OccName
mkVarOccFS (FieldLabel -> FastString
flLabel FieldLabel
fl)
                                , Bool -> Bool
not (OccName
lbl OccName -> OccSet -> Bool
`elemOccSet` OccSet
present_flds)
                                , Just GlobalRdrElt
gre <- [GlobalRdrEnv -> FieldLabel -> Maybe GlobalRdrElt
lookupGRE_FieldLabel GlobalRdrEnv
rdr_env FieldLabel
fl]
                                              -- Check selector is in scope
                                , case HsRecFieldContext
ctxt of
                                    HsRecFieldCon {} -> OccName -> Bool
arg_in_scope OccName
lbl
                                    HsRecFieldContext
_other           -> Bool
True ]

           ; [GlobalRdrElt] -> RnM ()
addUsedGREs [GlobalRdrElt]
dot_dot_gres
           ; let locn :: SrcSpanAnnA
locn = forall ann. SrcSpan -> SrcAnn ann
noAnnSrcSpan SrcSpan
loc
           ; forall (m :: * -> *) a. Monad m => a -> m a
return [ forall l e. l -> e -> GenLocated l e
L (forall ann. SrcSpan -> SrcAnn ann
noAnnSrcSpan SrcSpan
loc) (HsFieldBind
                        { hfbAnn :: XHsFieldBind (GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn))
hfbAnn = forall a. EpAnn a
noAnn
                        , hfbLHS :: GenLocated (SrcAnn NoEpAnns) (FieldOcc GhcRn)
hfbLHS
                           = forall l e. l -> e -> GenLocated l e
L (forall ann. SrcSpan -> SrcAnn ann
noAnnSrcSpan SrcSpan
loc) (forall pass. XCFieldOcc pass -> XRec pass RdrName -> FieldOcc pass
FieldOcc Name
sel (forall l e. l -> e -> GenLocated l e
L (forall ann. SrcSpan -> SrcAnn ann
noAnnSrcSpan SrcSpan
loc) RdrName
arg_rdr))
                        , hfbRHS :: LocatedA arg
hfbRHS = forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
locn (SrcSpan -> RdrName -> arg
mk_arg SrcSpan
loc RdrName
arg_rdr)
                        , hfbPun :: Bool
hfbPun      = Bool
False })
                    | FieldLabel
fl <- [FieldLabel]
dot_dot_fields
                    , let sel :: Name
sel     = FieldLabel -> Name
flSelector FieldLabel
fl
                    , let arg_rdr :: RdrName
arg_rdr = FastString -> RdrName
mkVarUnqual (FieldLabel -> FastString
flLabel FieldLabel
fl) ] }

    rn_dotdot Maybe (Located Int)
_dotdot Maybe Name
_mb_con [LHsRecField GhcRn (LocatedA arg)]
_flds
      = forall (m :: * -> *) a. Monad m => a -> m a
return []
      -- _dotdot = Nothing => No ".." at all
      -- _mb_con = Nothing => Record update
      -- _mb_con = Just unbound => Out of scope data constructor

    dup_flds :: [NE.NonEmpty RdrName]
        -- Each list represents a RdrName that occurred more than once
        -- (the list contains all occurrences)
        -- Each list in dup_fields is non-empty
    ([RdrName]
_, [NonEmpty RdrName]
dup_flds) = forall a. (a -> a -> Ordering) -> [a] -> ([a], [NonEmpty a])
removeDups forall a. Ord a => a -> a -> Ordering
compare (forall p arg. UnXRec p => [LHsRecField p arg] -> [RdrName]
getFieldLbls [LHsRecField GhcPs (LocatedA arg)]
flds)


-- NB: Consider this:
--      module Foo where { data R = R { fld :: Int } }
--      module Odd where { import Foo; fld x = x { fld = 3 } }
-- Arguably this should work, because the reference to 'fld' is
-- unambiguous because there is only one field id 'fld' in scope.
-- But currently it's rejected.

rnHsRecUpdFields
    :: [LHsRecUpdField GhcPs]
    -> RnM ([LHsRecUpdField GhcRn], FreeVars)
rnHsRecUpdFields :: [LHsRecUpdField GhcPs] -> RnM ([LHsRecUpdField GhcRn], FreeVars)
rnHsRecUpdFields [LHsRecUpdField GhcPs]
flds
  = do { Bool
pun_ok        <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.NamedFieldPuns
       ; DuplicateRecordFields
dup_fields_ok <- DynFlags -> DuplicateRecordFields
xopt_DuplicateRecordFields forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
       ; ([GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
      (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
flds1, [FreeVars]
fvss) <- forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM (Bool
-> DuplicateRecordFields
-> LHsRecUpdField GhcPs
-> RnM (LHsRecUpdField GhcRn, FreeVars)
rn_fld Bool
pun_ok DuplicateRecordFields
dup_fields_ok) [LHsRecUpdField GhcPs]
flds
       ; forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (TcRnMessage -> RnM ()
addErr forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecFieldContext -> NonEmpty RdrName -> TcRnMessage
dupFieldErr HsRecFieldContext
HsRecFieldUpd) [NonEmpty RdrName]
dup_flds

       -- Check for an empty record update  e {}
       -- NB: don't complain about e { .. }, because rn_dotdot has done that already
       ; forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LHsRecUpdField GhcPs]
flds) forall a b. (a -> b) -> a -> b
$ TcRnMessage -> RnM ()
addErr TcRnMessage
TcRnEmptyRecordUpdate

       ; forall (m :: * -> *) a. Monad m => a -> m a
return ([GenLocated
   SrcSpanAnnA
   (HsFieldBind
      (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
      (GenLocated SrcSpanAnnA (HsExpr GhcRn)))]
flds1, [FreeVars] -> FreeVars
plusFVs [FreeVars]
fvss) }
  where
    rn_fld :: Bool -> DuplicateRecordFields -> LHsRecUpdField GhcPs
           -> RnM (LHsRecUpdField GhcRn, FreeVars)
    rn_fld :: Bool
-> DuplicateRecordFields
-> LHsRecUpdField GhcPs
-> RnM (LHsRecUpdField GhcRn, FreeVars)
rn_fld Bool
pun_ok DuplicateRecordFields
dup_fields_ok (L SrcSpanAnnA
l (HsFieldBind { hfbLHS :: forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS = L SrcAnn NoEpAnns
loc AmbiguousFieldOcc GhcPs
f
                                                  , hfbRHS :: forall lhs rhs. HsFieldBind lhs rhs -> rhs
hfbRHS = GenLocated SrcSpanAnnA (HsExpr GhcPs)
arg
                                                  , hfbPun :: forall lhs rhs. HsFieldBind lhs rhs -> Bool
hfbPun      = Bool
pun }))
      = do { let lbl :: RdrName
lbl = forall (p :: Pass). AmbiguousFieldOcc (GhcPass p) -> RdrName
rdrNameAmbiguousFieldOcc AmbiguousFieldOcc GhcPs
f
           ; AmbiguousResult
mb_sel <- forall ann a. SrcSpanAnn' ann -> TcRn a -> TcRn a
setSrcSpanA SrcAnn NoEpAnns
loc forall a b. (a -> b) -> a -> b
$
                      -- Defer renaming of overloaded fields to the typechecker
                      -- See Note [Disambiguating record fields] in GHC.Tc.Gen.Head
                      DuplicateRecordFields -> RdrName -> TcRn AmbiguousResult
lookupRecFieldOcc_update DuplicateRecordFields
dup_fields_ok RdrName
lbl
           ; GenLocated SrcSpanAnnA (HsExpr GhcPs)
arg' <- if Bool
pun
                     then do { Bool -> TcRnMessage -> RnM ()
checkErr Bool
pun_ok (Located RdrName -> TcRnMessage
TcRnIllegalFieldPunning (forall l e. l -> e -> GenLocated l e
L (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcAnn NoEpAnns
loc) RdrName
lbl))
                               -- Discard any module qualifier (#11662)
                             ; let arg_rdr :: RdrName
arg_rdr = OccName -> RdrName
mkRdrUnqual (RdrName -> OccName
rdrNameOcc RdrName
lbl)
                             ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L (forall a ann. SrcSpanAnn' a -> SrcAnn ann
l2l SrcAnn NoEpAnns
loc) (forall p. XVar p -> LIdP p -> HsExpr p
HsVar NoExtField
noExtField
                                              (forall l e. l -> e -> GenLocated l e
L (forall a ann. SrcSpanAnn' a -> SrcAnn ann
l2l SrcAnn NoEpAnns
loc) RdrName
arg_rdr))) }
                     else forall (m :: * -> *) a. Monad m => a -> m a
return GenLocated SrcSpanAnnA (HsExpr GhcPs)
arg
           ; (GenLocated SrcSpanAnnA (HsExpr GhcRn)
arg'', FreeVars
fvs) <- LHsExpr GhcPs -> RnM (LHsExpr GhcRn, FreeVars)
rnLExpr GenLocated SrcSpanAnnA (HsExpr GhcPs)
arg'

           ; let (AmbiguousFieldOcc GhcRn
lbl', FreeVars
fvs') = case AmbiguousResult
mb_sel of
                   UnambiguousGre GreName
gname -> let sel_name :: Name
sel_name = GreName -> Name
greNameMangledName GreName
gname
                                           in (forall pass.
XUnambiguous pass
-> GenLocated (SrcAnn NameAnn) RdrName -> AmbiguousFieldOcc pass
Unambiguous Name
sel_name (forall l e. l -> e -> GenLocated l e
L (forall a ann. SrcSpanAnn' a -> SrcAnn ann
l2l SrcAnn NoEpAnns
loc) RdrName
lbl), FreeVars
fvs FreeVars -> Name -> FreeVars
`addOneFV` Name
sel_name)
                   AmbiguousResult
AmbiguousFields       -> (forall pass.
XAmbiguous pass
-> GenLocated (SrcAnn NameAnn) RdrName -> AmbiguousFieldOcc pass
Ambiguous   NoExtField
noExtField (forall l e. l -> e -> GenLocated l e
L (forall a ann. SrcSpanAnn' a -> SrcAnn ann
l2l SrcAnn NoEpAnns
loc) RdrName
lbl), FreeVars
fvs)

           ; forall (m :: * -> *) a. Monad m => a -> m a
return (forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
l (HsFieldBind { hfbAnn :: XHsFieldBind
  (GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn))
hfbAnn = forall a. EpAnn a
noAnn
                                      , hfbLHS :: GenLocated (SrcAnn NoEpAnns) (AmbiguousFieldOcc GhcRn)
hfbLHS = forall l e. l -> e -> GenLocated l e
L SrcAnn NoEpAnns
loc AmbiguousFieldOcc GhcRn
lbl'
                                      , hfbRHS :: GenLocated SrcSpanAnnA (HsExpr GhcRn)
hfbRHS = GenLocated SrcSpanAnnA (HsExpr GhcRn)
arg''
                                      , hfbPun :: Bool
hfbPun = Bool
pun }), FreeVars
fvs') }

    dup_flds :: [NE.NonEmpty RdrName]
        -- Each list represents a RdrName that occurred more than once
        -- (the list contains all occurrences)
        -- Each list in dup_fields is non-empty
    ([RdrName]
_, [NonEmpty RdrName]
dup_flds) = forall a. (a -> a -> Ordering) -> [a] -> ([a], [NonEmpty a])
removeDups forall a. Ord a => a -> a -> Ordering
compare ([LHsRecUpdField GhcPs] -> [RdrName]
getFieldUpdLbls [LHsRecUpdField GhcPs]
flds)



getFieldIds :: [LHsRecField GhcRn arg] -> [Name]
getFieldIds :: forall arg. [LHsRecField GhcRn arg] -> [Name]
getFieldIds [LHsRecField GhcRn arg]
flds = forall a b. (a -> b) -> [a] -> [b]
map (forall p arg. UnXRec p => HsRecField p arg -> XCFieldOcc p
hsRecFieldSel forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc) [LHsRecField GhcRn arg]
flds

getFieldLbls :: forall p arg . UnXRec p => [LHsRecField p arg] -> [RdrName]
getFieldLbls :: forall p arg. UnXRec p => [LHsRecField p arg] -> [RdrName]
getFieldLbls [LHsRecField p arg]
flds
  = forall a b. (a -> b) -> [a] -> [b]
map (forall p a. UnXRec p => XRec p a -> a
unXRec @p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall pass. FieldOcc pass -> XRec pass RdrName
foLabel forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall p a. UnXRec p => XRec p a -> a
unXRec @p forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall p a. UnXRec p => XRec p a -> a
unXRec @p) [LHsRecField p arg]
flds

getFieldUpdLbls :: [LHsRecUpdField GhcPs] -> [RdrName]
getFieldUpdLbls :: [LHsRecUpdField GhcPs] -> [RdrName]
getFieldUpdLbls [LHsRecUpdField GhcPs]
flds = forall a b. (a -> b) -> [a] -> [b]
map (forall (p :: Pass). AmbiguousFieldOcc (GhcPass p) -> RdrName
rdrNameAmbiguousFieldOcc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall lhs rhs. HsFieldBind lhs rhs -> lhs
hfbLHS forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc) [LHsRecUpdField GhcPs]
flds

needFlagDotDot :: HsRecFieldContext -> TcRnMessage
needFlagDotDot :: HsRecFieldContext -> TcRnMessage
needFlagDotDot = RecordFieldPart -> TcRnMessage
TcRnIllegalWildcardsInRecord forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecFieldContext -> RecordFieldPart
toRecordFieldPart

dupFieldErr :: HsRecFieldContext -> NE.NonEmpty RdrName -> TcRnMessage
dupFieldErr :: HsRecFieldContext -> NonEmpty RdrName -> TcRnMessage
dupFieldErr HsRecFieldContext
ctxt = RecordFieldPart -> NonEmpty RdrName -> TcRnMessage
TcRnDuplicateFieldName (HsRecFieldContext -> RecordFieldPart
toRecordFieldPart HsRecFieldContext
ctxt)

toRecordFieldPart :: HsRecFieldContext -> RecordFieldPart
toRecordFieldPart :: HsRecFieldContext -> RecordFieldPart
toRecordFieldPart (HsRecFieldCon Name
n)  = Name -> RecordFieldPart
RecordFieldConstructor Name
n
toRecordFieldPart (HsRecFieldPat Name
n)  = Name -> RecordFieldPart
RecordFieldPattern     Name
n
toRecordFieldPart (HsRecFieldUpd {}) = RecordFieldPart
RecordFieldUpdate

{-
************************************************************************
*                                                                      *
\subsubsection{Literals}
*                                                                      *
************************************************************************

When literals occur we have to make sure
that the types and classes they involve
are made available.
-}

rnLit :: HsLit p -> RnM ()
rnLit :: forall p. HsLit p -> RnM ()
rnLit (HsChar XHsChar p
_ Char
c) = Bool -> TcRnMessage -> RnM ()
checkErr (Char -> Bool
inCharRange Char
c) (Char -> TcRnMessage
TcRnCharLiteralOutOfRange Char
c)
rnLit HsLit p
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Turn a Fractional-looking literal which happens to be an integer into an
-- Integer-looking literal.
-- We only convert numbers where the exponent is between 0 and 100 to avoid
-- converting huge numbers and incurring long compilation times. See #15646.
generalizeOverLitVal :: OverLitVal -> OverLitVal
generalizeOverLitVal :: OverLitVal -> OverLitVal
generalizeOverLitVal (HsFractional fl :: FractionalLit
fl@(FL {fl_text :: FractionalLit -> SourceText
fl_text=SourceText
src,fl_neg :: FractionalLit -> Bool
fl_neg=Bool
neg,fl_exp :: FractionalLit -> Integer
fl_exp=Integer
e}))
    | Integer
e forall a. Ord a => a -> a -> Bool
>= -Integer
100 Bool -> Bool -> Bool
&& Integer
e forall a. Ord a => a -> a -> Bool
<= Integer
100
    , let val :: Rational
val = FractionalLit -> Rational
rationalFromFractionalLit FractionalLit
fl
    , forall a. Ratio a -> a
denominator Rational
val forall a. Eq a => a -> a -> Bool
== Integer
1 = IntegralLit -> OverLitVal
HsIntegral (IL {il_text :: SourceText
il_text=SourceText
src,il_neg :: Bool
il_neg=Bool
neg,il_value :: Integer
il_value=forall a. Ratio a -> a
numerator Rational
val})
generalizeOverLitVal OverLitVal
lit = OverLitVal
lit

isNegativeZeroOverLit :: HsOverLit t -> Bool
isNegativeZeroOverLit :: forall t. HsOverLit t -> Bool
isNegativeZeroOverLit HsOverLit t
lit
 = case forall p. HsOverLit p -> OverLitVal
ol_val HsOverLit t
lit of
        HsIntegral IntegralLit
i    -> Integer
0 forall a. Eq a => a -> a -> Bool
== IntegralLit -> Integer
il_value IntegralLit
i Bool -> Bool -> Bool
&& IntegralLit -> Bool
il_neg IntegralLit
i
        -- For HsFractional, the value of fl is n * (b ^^ e) so it is sufficient
        -- to check if n = 0. b is equal to either 2 or 10. We don't call
        -- rationalFromFractionalLit here as it is expensive when e is big.
        HsFractional FractionalLit
fl -> Rational
0 forall a. Eq a => a -> a -> Bool
== FractionalLit -> Rational
fl_signi FractionalLit
fl Bool -> Bool -> Bool
&& FractionalLit -> Bool
fl_neg FractionalLit
fl
        OverLitVal
_               -> Bool
False

{-
Note [Negative zero]
~~~~~~~~~~~~~~~~~~~~~~~~~
There were problems with negative zero in conjunction with Negative Literals
extension. Numeric literal value is contained in Integer and Rational types
inside IntegralLit and FractionalLit. These types cannot represent negative
zero value. So we had to add explicit field 'neg' which would hold information
about literal sign. Here in rnOverLit we use it to detect negative zeroes and
in this case return not only literal itself but also negateName so that users
can apply it explicitly. In this case it stays negative zero.  #13211
-}

rnOverLit :: HsOverLit t ->
             RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
rnOverLit :: forall t.
HsOverLit t
-> RnM ((HsOverLit GhcRn, Maybe (HsExpr GhcRn)), FreeVars)
rnOverLit HsOverLit t
origLit
  = do  { Bool
opt_NumDecimals <- forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.NumDecimals
        ; let { lit :: HsOverLit t
lit@(OverLit {ol_val :: forall p. HsOverLit p -> OverLitVal
ol_val=OverLitVal
val})
            | Bool
opt_NumDecimals = HsOverLit t
origLit {ol_val :: OverLitVal
ol_val = OverLitVal -> OverLitVal
generalizeOverLitVal (forall p. HsOverLit p -> OverLitVal
ol_val HsOverLit t
origLit)}
            | Bool
otherwise       = HsOverLit t
origLit
          }
        ; let std_name :: Name
std_name = OverLitVal -> Name
hsOverLitName OverLitVal
val
        ; (Name
from_thing_name, FreeVars
fvs1) <- Name -> RnM (Name, FreeVars)
lookupSyntaxName Name
std_name
        ; let rebindable :: Bool
rebindable = Name
from_thing_name forall a. Eq a => a -> a -> Bool
/= Name
std_name
              lit' :: HsOverLit GhcRn
lit' = HsOverLit t
lit { ol_ext :: XOverLit GhcRn
ol_ext = OverLitRn { $sel:ol_rebindable:OverLitRn :: Bool
ol_rebindable = Bool
rebindable
                                              , $sel:ol_from_fun:OverLitRn :: LIdP GhcRn
ol_from_fun = forall a an. a -> LocatedAn an a
noLocA Name
from_thing_name } }
        ; if forall t. HsOverLit t -> Bool
isNegativeZeroOverLit HsOverLit GhcRn
lit'
          then do { (HsExpr GhcRn
negate_name, FreeVars
fvs2) <- Name -> RnM (HsExpr GhcRn, FreeVars)
lookupSyntaxExpr Name
negateName
                  ; forall (m :: * -> *) a. Monad m => a -> m a
return ((HsOverLit GhcRn
lit' { ol_val :: OverLitVal
ol_val = OverLitVal -> OverLitVal
negateOverLitVal OverLitVal
val }, forall a. a -> Maybe a
Just HsExpr GhcRn
negate_name)
                                  , FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) }
          else forall (m :: * -> *) a. Monad m => a -> m a
return ((HsOverLit GhcRn
lit', forall a. Maybe a
Nothing), FreeVars
fvs1) }