{-
(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.
-}

{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE DeriveFunctor #-}

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

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,

              -- Literals
              rnLit, rnOverLit,

             -- Pattern Error messages that are also used elsewhere
             checkTupSize, patSigErr
             ) 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 )

#include "HsVersions.h"

import GHC.Hs
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.Zonk   ( hsOverLitName )
import GHC.Rename.Env
import GHC.Rename.Fixity
import GHC.Rename.Utils    ( HsDocContext(..), newLocalBndrRn, bindLocalNames
                           , warnUnusedMatches, newLocalBndrRn
                           , checkUnusedRecordWildcard
                           , checkDupNames, checkDupAndShadowedNames
                           , checkTupSize , unknownSubordinateErr )
import GHC.Rename.HsType
import GHC.Builtin.Names
import GHC.Types.Name
import GHC.Types.Name.Set
import GHC.Types.Name.Reader
import GHC.Types.Basic
import GHC.Utils.Misc
import GHC.Data.List.SetOps( removeDups )
import GHC.Utils.Outputable
import GHC.Types.SrcLoc
import GHC.Types.Literal   ( inCharRange )
import GHC.Builtin.Types   ( nilDataCon )
import GHC.Core.DataCon
import qualified GHC.LanguageExtensions as LangExt

import Control.Monad       ( when, ap, guard )
import qualified Data.List.NonEmpty as NE
import Data.Ratio

{-
*********************************************************
*                                                      *
        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 -> b) -> CpsRn a -> CpsRn b)
-> (forall a b. a -> CpsRn b -> CpsRn a) -> Functor CpsRn
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 r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn a
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
(<*>) = 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 r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\b -> RnM (r, FreeVars)
k -> (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
m (\a
v -> CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
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) = (a -> RnM (a, FreeVars)) -> RnM (a, FreeVars)
forall r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
m (\a
r -> (a, FreeVars) -> RnM (a, FreeVars)
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 r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn a
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\a -> RnM (r, FreeVars)
k -> RnM a
rn_thing RnM a -> (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
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 r. (a -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn a
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
                                     ; (r, FreeVars) -> RnM (r, FreeVars)
forall (m :: * -> *) a. Monad m => a -> m a
return (r
r, FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) })

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

lookupConCps :: Located RdrName -> CpsRn (Located Name)
lookupConCps :: Located RdrName -> CpsRn (Located Name)
lookupConCps Located RdrName
con_rdr
  = (forall r.
 (Located Name -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn (Located Name)
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\Located Name -> RnM (r, FreeVars)
k -> do { Located Name
con_name <- Located RdrName -> RnM (Located Name)
lookupLocatedOccRn Located RdrName
con_rdr
                    ; (r
r, FreeVars
fvs) <- Located Name -> RnM (r, FreeVars)
k Located Name
con_name
                    ; (r, FreeVars) -> RnM (r, FreeVars)
forall (m :: * -> *) a. Monad m => a -> m a
return (r
r, FreeVars -> Name -> FreeVars
addOneFV FreeVars
fvs (Located Name -> Name
forall l e. GenLocated l e -> e
unLoc Located 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 HsStmtContext a
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 -> Located RdrName -> CpsRn (Located Name)
newPatLName :: NameMaker -> Located RdrName -> CpsRn (Located Name)
newPatLName NameMaker
name_maker rdr_name :: Located RdrName
rdr_name@(L SrcSpan
loc RdrName
_)
  = do { Name
name <- NameMaker -> Located RdrName -> CpsRn Name
newPatName NameMaker
name_maker Located RdrName
rdr_name
       ; Located Name -> CpsRn (Located Name)
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpan -> Name -> Located Name
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc Name
name) }

newPatName :: NameMaker -> Located RdrName -> CpsRn Name
newPatName :: NameMaker -> Located RdrName -> CpsRn Name
newPatName (LamMk Bool
report_unused) Located RdrName
rdr_name
  = (forall r. (Name -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn Name
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\ Name -> RnM (r, FreeVars)
thing_inside ->
        do { Name
name <- Located RdrName -> RnM Name
newLocalBndrRn Located RdrName
rdr_name
           ; (r
res, FreeVars
fvs) <- [Name] -> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a. [Name] -> RnM a -> RnM a
bindLocalNames [Name
name] (Name -> RnM (r, FreeVars)
thing_inside Name
name)
           ; Bool
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
report_unused (IOEnv (Env TcGblEnv TcLclEnv) ()
 -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$ [Name] -> FreeVars -> IOEnv (Env TcGblEnv TcLclEnv) ()
warnUnusedMatches [Name
name] FreeVars
fvs
           ; (r, FreeVars) -> RnM (r, FreeVars)
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) Located RdrName
rdr_name
  = (forall r. (Name -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn Name
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (\ Name -> RnM (r, FreeVars)
thing_inside ->
        do { Name
name <- case TopLevelFlag
is_top of
                       TopLevelFlag
NotTopLevel -> Located RdrName -> RnM Name
newLocalBndrRn Located RdrName
rdr_name
                       TopLevelFlag
TopLevel    -> Located RdrName -> RnM Name
newTopSrcBinder Located RdrName
rdr_name
           ; [Name] -> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a. [Name] -> RnM a -> RnM a
bindLocalNames [Name
name] (RnM (r, FreeVars) -> RnM (r, FreeVars))
-> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a b. (a -> b) -> a -> b
$       -- Do *not* use bindLocalNameFV here
                                        -- See Note [View pattern usage]
             MiniFixityEnv -> [Name] -> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a. MiniFixityEnv -> [Name] -> RnM a -> RnM a
addLocalFixities MiniFixityEnv
fix_env [Name
name] (RnM (r, FreeVars) -> RnM (r, FreeVars))
-> RnM (r, FreeVars) -> RnM (r, FreeVars)
forall a b. (a -> b) -> a -> b
$
             Name -> RnM (r, FreeVars)
thing_inside Name
name })

    -- Note: the bindLocalNames is somewhat suspicious
    --       because it binds a top-level name as a local name.
    --       however, this binding seems to work, and it only exists for
    --       the duration of the patterns and the continuation;
    --       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.

*********************************************************
*                                                      *
        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 (GhcPass 'Renamed)
-> [LPat GhcPs]
-> ([LPat (GhcPass 'Renamed)] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnPats HsMatchContext (GhcPass 'Renamed)
ctxt [LPat GhcPs]
pats [LPat (GhcPass 'Renamed)] -> 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.
        ; CpsRn [Located (Pat (GhcPass 'Renamed))]
-> forall r.
   ([Located (Pat (GhcPass 'Renamed))] -> RnM (r, FreeVars))
   -> RnM (r, FreeVars)
forall b.
CpsRn b -> forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars)
unCpsRn (NameMaker -> [LPat GhcPs] -> CpsRn [LPat (GhcPass 'Renamed)]
rnLPatsAndThen (HsMatchContext (GhcPass 'Renamed) -> NameMaker
forall a. HsMatchContext a -> NameMaker
matchNameMaker HsMatchContext (GhcPass 'Renamed)
ctxt) [LPat GhcPs]
pats) (([Located (Pat (GhcPass 'Renamed))] -> RnM (a, FreeVars))
 -> RnM (a, FreeVars))
-> ([Located (Pat (GhcPass 'Renamed))] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
forall a b. (a -> b) -> a -> b
$ \ [Located (Pat (GhcPass 'Renamed))]
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 (GhcPass 'Renamed)]
bndrs = [LPat (GhcPass 'Renamed)] -> [IdP (GhcPass 'Renamed)]
forall p. CollectPass p => [LPat p] -> [IdP p]
collectPatsBinders [Located (Pat (GhcPass 'Renamed))]
[LPat (GhcPass 'Renamed)]
pats'
        ; MsgDoc
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a. MsgDoc -> TcM a -> TcM a
addErrCtxt MsgDoc
doc_pat (IOEnv (Env TcGblEnv TcLclEnv) ()
 -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$
          if HsMatchContext (GhcPass 'Renamed) -> Bool
forall p. HsMatchContext p -> Bool
isPatSynCtxt HsMatchContext (GhcPass 'Renamed)
ctxt
             then [Name] -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkDupNames [Name]
[IdP (GhcPass 'Renamed)]
bndrs
             else (GlobalRdrEnv, LocalRdrEnv)
-> [Name] -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkDupAndShadowedNames (GlobalRdrEnv, LocalRdrEnv)
envs_before [Name]
[IdP (GhcPass 'Renamed)]
bndrs
        ; [LPat (GhcPass 'Renamed)] -> RnM (a, FreeVars)
thing_inside [Located (Pat (GhcPass 'Renamed))]
[LPat (GhcPass 'Renamed)]
pats' } }
  where
    doc_pat :: MsgDoc
doc_pat = String -> MsgDoc
text String
"In" MsgDoc -> MsgDoc -> MsgDoc
<+> HsMatchContext (GhcPass 'Renamed) -> MsgDoc
forall p. Outputable (IdP p) => HsMatchContext p -> MsgDoc
pprMatchContext HsMatchContext (GhcPass 'Renamed)
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 (GhcPass 'Renamed)
-> LPat GhcPs
-> (LPat (GhcPass 'Renamed) -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnPat HsMatchContext (GhcPass 'Renamed)
ctxt LPat GhcPs
pat LPat (GhcPass 'Renamed) -> RnM (a, FreeVars)
thing_inside
  = HsMatchContext (GhcPass 'Renamed)
-> [LPat GhcPs]
-> ([LPat (GhcPass 'Renamed)] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
forall a.
HsMatchContext (GhcPass 'Renamed)
-> [LPat GhcPs]
-> ([LPat (GhcPass 'Renamed)] -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnPats HsMatchContext (GhcPass 'Renamed)
ctxt [LPat GhcPs
pat] (\[LPat (GhcPass 'Renamed)]
pats' -> let [Located (Pat (GhcPass 'Renamed))
pat'] = [Located (Pat (GhcPass 'Renamed))]
[LPat (GhcPass 'Renamed)]
pats' in LPat (GhcPass 'Renamed) -> RnM (a, FreeVars)
thing_inside Located (Pat (GhcPass 'Renamed))
LPat (GhcPass 'Renamed)
pat')

applyNameMaker :: NameMaker -> Located RdrName -> RnM (Located Name)
applyNameMaker :: NameMaker -> Located RdrName -> RnM (Located Name)
applyNameMaker NameMaker
mk Located RdrName
rdr = do { (Located Name
n, FreeVars
_fvs) <- CpsRn (Located Name) -> RnM (Located Name, FreeVars)
forall a. CpsRn a -> RnM (a, FreeVars)
runCps (NameMaker -> Located RdrName -> CpsRn (Located Name)
newPatLName NameMaker
mk Located RdrName
rdr)
                           ; Located Name -> RnM (Located Name)
forall (m :: * -> *) a. Monad m => a -> m a
return Located 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 (GhcPass 'Renamed), FreeVars)
rnBindPat NameMaker
name_maker LPat GhcPs
pat = CpsRn (Located (Pat (GhcPass 'Renamed)))
-> RnM (Located (Pat (GhcPass 'Renamed)), FreeVars)
forall a. CpsRn a -> RnM (a, FreeVars)
runCps (NameMaker -> LPat GhcPs -> CpsRn (LPat (GhcPass 'Renamed))
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 (GhcPass 'Renamed)]
rnLPatsAndThen NameMaker
mk = (Located (Pat GhcPs) -> CpsRn (Located (Pat (GhcPass 'Renamed))))
-> [Located (Pat GhcPs)]
-> CpsRn [Located (Pat (GhcPass 'Renamed))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (NameMaker -> LPat GhcPs -> CpsRn (LPat (GhcPass 'Renamed))
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 (GhcPass 'Renamed))
rnLPatAndThen NameMaker
nm LPat GhcPs
lpat = (Pat GhcPs -> CpsRn (Pat (GhcPass 'Renamed)))
-> Located (Pat GhcPs) -> CpsRn (Located (Pat (GhcPass 'Renamed)))
forall a b. (a -> CpsRn b) -> Located a -> CpsRn (Located b)
wrapSrcSpanCps (NameMaker -> Pat GhcPs -> CpsRn (Pat (GhcPass 'Renamed))
rnPatAndThen NameMaker
nm) Located (Pat GhcPs)
LPat GhcPs
lpat

rnPatAndThen :: NameMaker -> Pat GhcPs -> CpsRn (Pat GhcRn)
rnPatAndThen :: NameMaker -> Pat GhcPs -> CpsRn (Pat (GhcPass 'Renamed))
rnPatAndThen NameMaker
_  (WildPat XWildPat GhcPs
_)   = Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XWildPat (GhcPass 'Renamed) -> Pat (GhcPass 'Renamed)
forall p. XWildPat p -> Pat p
WildPat NoExtField
XWildPat (GhcPass 'Renamed)
noExtField)
rnPatAndThen NameMaker
mk (ParPat XParPat GhcPs
x LPat GhcPs
pat)  = do { Located (Pat (GhcPass 'Renamed))
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat (GhcPass 'Renamed))
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
                                     ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XParPat (GhcPass 'Renamed)
-> LPat (GhcPass 'Renamed) -> Pat (GhcPass 'Renamed)
forall p. XParPat p -> LPat p -> Pat p
ParPat XParPat GhcPs
XParPat (GhcPass 'Renamed)
x Located (Pat (GhcPass 'Renamed))
LPat (GhcPass 'Renamed)
pat') }
rnPatAndThen NameMaker
mk (LazyPat XLazyPat GhcPs
x LPat GhcPs
pat) = do { Located (Pat (GhcPass 'Renamed))
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat (GhcPass 'Renamed))
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
                                     ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XLazyPat (GhcPass 'Renamed)
-> LPat (GhcPass 'Renamed) -> Pat (GhcPass 'Renamed)
forall p. XLazyPat p -> LPat p -> Pat p
LazyPat XLazyPat GhcPs
XLazyPat (GhcPass 'Renamed)
x Located (Pat (GhcPass 'Renamed))
LPat (GhcPass 'Renamed)
pat') }
rnPatAndThen NameMaker
mk (BangPat XBangPat GhcPs
x LPat GhcPs
pat) = do { Located (Pat (GhcPass 'Renamed))
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat (GhcPass 'Renamed))
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
                                     ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XBangPat (GhcPass 'Renamed)
-> LPat (GhcPass 'Renamed) -> Pat (GhcPass 'Renamed)
forall p. XBangPat p -> LPat p -> Pat p
BangPat XBangPat GhcPs
XBangPat (GhcPass 'Renamed)
x Located (Pat (GhcPass 'Renamed))
LPat (GhcPass 'Renamed)
pat') }
rnPatAndThen NameMaker
mk (VarPat XVarPat GhcPs
x (L SrcSpan
l IdP GhcPs
rdr))
    = do { SrcSpan
loc <- RnM SrcSpan -> CpsRn SrcSpan
forall a. RnM a -> CpsRn a
liftCps RnM SrcSpan
getSrcSpanM
         ; Name
name <- NameMaker -> Located RdrName -> CpsRn Name
newPatName NameMaker
mk (SrcSpan -> RdrName -> Located RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc RdrName
IdP GhcPs
rdr)
         ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XVarPat (GhcPass 'Renamed)
-> Located (IdP (GhcPass 'Renamed)) -> Pat (GhcPass 'Renamed)
forall p. XVarPat p -> Located (IdP p) -> Pat p
VarPat XVarPat GhcPs
XVarPat (GhcPass 'Renamed)
x (SrcSpan -> Name -> Located Name
forall l e. l -> e -> GenLocated l e
L SrcSpan
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
x 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 (GhcPass 'Renamed)
sig' <- HsPatSigType GhcPs -> CpsRn (HsPatSigType (GhcPass 'Renamed))
rnHsPatSigTypeAndThen HsPatSigType GhcPs
HsPatSigType (NoGhcTc GhcPs)
sig
       ; Located (Pat (GhcPass 'Renamed))
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat (GhcPass 'Renamed))
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
       ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XSigPat (GhcPass 'Renamed)
-> LPat (GhcPass 'Renamed)
-> HsPatSigType (NoGhcTc (GhcPass 'Renamed))
-> Pat (GhcPass 'Renamed)
forall p. XSigPat p -> LPat p -> HsPatSigType (NoGhcTc p) -> Pat p
SigPat XSigPat GhcPs
XSigPat (GhcPass 'Renamed)
x Located (Pat (GhcPass 'Renamed))
LPat (GhcPass 'Renamed)
pat' HsPatSigType (GhcPass 'Renamed)
HsPatSigType (NoGhcTc (GhcPass 'Renamed))
sig' ) }
  where
    rnHsPatSigTypeAndThen :: HsPatSigType GhcPs -> CpsRn (HsPatSigType GhcRn)
    rnHsPatSigTypeAndThen :: HsPatSigType GhcPs -> CpsRn (HsPatSigType (GhcPass 'Renamed))
rnHsPatSigTypeAndThen HsPatSigType GhcPs
sig = (forall r.
 (HsPatSigType (GhcPass 'Renamed) -> RnM (r, FreeVars))
 -> RnM (r, FreeVars))
-> CpsRn (HsPatSigType (GhcPass 'Renamed))
forall b.
(forall r. (b -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn b
CpsRn (HsSigWcTypeScoping
-> HsDocContext
-> HsPatSigType GhcPs
-> (HsPatSigType (GhcPass 'Renamed) -> RnM (r, FreeVars))
-> RnM (r, FreeVars)
forall a.
HsSigWcTypeScoping
-> HsDocContext
-> HsPatSigType GhcPs
-> (HsPatSigType (GhcPass 'Renamed) -> RnM (a, FreeVars))
-> RnM (a, FreeVars)
rnHsPatSigType HsSigWcTypeScoping
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 <- RnM Bool -> CpsRn Bool
forall a. RnM a -> CpsRn a
liftCps (Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.OverloadedStrings)
       ; if Bool
ovlStr
         then NameMaker -> Pat GhcPs -> CpsRn (Pat (GhcPass 'Renamed))
rnPatAndThen NameMaker
mk
                           (Located (HsOverLit GhcPs) -> Maybe (SyntaxExpr GhcPs) -> Pat GhcPs
mkNPat (HsOverLit GhcPs -> Located (HsOverLit GhcPs)
forall e. e -> Located e
noLoc (SourceText -> FastString -> HsOverLit GhcPs
mkHsIsString SourceText
XHsString GhcPs
src FastString
s))
                                      Maybe (SyntaxExpr GhcPs)
forall a. Maybe a
Nothing)
         else CpsRn (Pat (GhcPass 'Renamed))
normal_lit }
  | Bool
otherwise = CpsRn (Pat (GhcPass 'Renamed))
normal_lit
  where
    normal_lit :: CpsRn (Pat (GhcPass 'Renamed))
normal_lit = do { IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a. RnM a -> CpsRn a
liftCps (HsLit GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall p. HsLit p -> IOEnv (Env TcGblEnv TcLclEnv) ()
rnLit HsLit GhcPs
lit); Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XLitPat (GhcPass 'Renamed)
-> HsLit (GhcPass 'Renamed) -> Pat (GhcPass 'Renamed)
forall p. XLitPat p -> HsLit p -> Pat p
LitPat XLitPat GhcPs
XLitPat (GhcPass 'Renamed)
x (HsLit GhcPs -> HsLit (GhcPass 'Renamed)
forall (p1 :: Pass) (p2 :: Pass).
HsLit (GhcPass p1) -> HsLit (GhcPass p2)
convertLit HsLit GhcPs
lit)) }

rnPatAndThen NameMaker
_ (NPat XNPat GhcPs
x (L SrcSpan
l HsOverLit GhcPs
lit) Maybe (SyntaxExpr GhcPs)
mb_neg SyntaxExpr GhcPs
_eq)
  = do { (HsOverLit (GhcPass 'Renamed)
lit', Maybe (HsExpr (GhcPass 'Renamed))
mb_neg') <- RnM
  ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
   FreeVars)
-> CpsRn
     (HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed)))
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (RnM
   ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
    FreeVars)
 -> CpsRn
      (HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))))
-> RnM
     ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
      FreeVars)
-> CpsRn
     (HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed)))
forall a b. (a -> b) -> a -> b
$ HsOverLit GhcPs
-> RnM
     ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
      FreeVars)
forall t.
HsOverLit t
-> RnM
     ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
      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 (GhcPass 'Renamed), FreeVars)
lookupSyntax Name
negateName
                                ; (Maybe SyntaxExprRn, FreeVars)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
forall (m :: * -> *) a. Monad m => a -> m a
return (SyntaxExprRn -> Maybe SyntaxExprRn
forall a. a -> Maybe a
Just SyntaxExprRn
neg, FreeVars
fvs) }
                  positive :: IOEnv (Env TcGblEnv TcLclEnv) (Maybe a, FreeVars)
positive = (Maybe a, FreeVars)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe a, FreeVars)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a
forall a. Maybe a
Nothing, FreeVars
emptyFVs)
              in IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
-> CpsRn (Maybe SyntaxExprRn)
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
 -> CpsRn (Maybe SyntaxExprRn))
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
-> CpsRn (Maybe SyntaxExprRn)
forall a b. (a -> b) -> a -> b
$ case (Maybe NoExtField
Maybe (SyntaxExpr GhcPs)
mb_neg , Maybe (HsExpr (GhcPass 'Renamed))
mb_neg') of
                                  (Maybe NoExtField
Nothing, Just HsExpr (GhcPass 'Renamed)
_ ) -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
negative
                                  (Just NoExtField
_ , Maybe (HsExpr (GhcPass 'Renamed))
Nothing) -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
negative
                                  (Maybe NoExtField
Nothing, Maybe (HsExpr (GhcPass 'Renamed))
Nothing) -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
forall {a}. IOEnv (Env TcGblEnv TcLclEnv) (Maybe a, FreeVars)
positive
                                  (Just NoExtField
_ , Just HsExpr (GhcPass 'Renamed)
_ ) -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe SyntaxExprRn, FreeVars)
forall {a}. IOEnv (Env TcGblEnv TcLclEnv) (Maybe a, FreeVars)
positive
       ; SyntaxExprRn
eq' <- IOEnv (Env TcGblEnv TcLclEnv) (SyntaxExprRn, FreeVars)
-> CpsRn SyntaxExprRn
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (IOEnv (Env TcGblEnv TcLclEnv) (SyntaxExprRn, FreeVars)
 -> CpsRn SyntaxExprRn)
-> IOEnv (Env TcGblEnv TcLclEnv) (SyntaxExprRn, FreeVars)
-> CpsRn SyntaxExprRn
forall a b. (a -> b) -> a -> b
$ Name -> RnM (SyntaxExpr (GhcPass 'Renamed), FreeVars)
lookupSyntax Name
eqName
       ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XNPat (GhcPass 'Renamed)
-> Located (HsOverLit (GhcPass 'Renamed))
-> Maybe (SyntaxExpr (GhcPass 'Renamed))
-> SyntaxExpr (GhcPass 'Renamed)
-> Pat (GhcPass 'Renamed)
forall p.
XNPat p
-> Located (HsOverLit p)
-> Maybe (SyntaxExpr p)
-> SyntaxExpr p
-> Pat p
NPat XNPat GhcPs
XNPat (GhcPass 'Renamed)
x (SrcSpan
-> HsOverLit (GhcPass 'Renamed)
-> Located (HsOverLit (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L SrcSpan
l HsOverLit (GhcPass 'Renamed)
lit') Maybe (SyntaxExpr (GhcPass 'Renamed))
Maybe SyntaxExprRn
mb_neg' SyntaxExpr (GhcPass 'Renamed)
SyntaxExprRn
eq') }

rnPatAndThen NameMaker
mk (NPlusKPat XNPlusKPat GhcPs
x GenLocated SrcSpan (IdP GhcPs)
rdr (L SrcSpan
l HsOverLit GhcPs
lit) HsOverLit GhcPs
_ SyntaxExpr GhcPs
_ SyntaxExpr GhcPs
_ )
  = do { Name
new_name <- NameMaker -> Located RdrName -> CpsRn Name
newPatName NameMaker
mk Located RdrName
GenLocated SrcSpan (IdP GhcPs)
rdr
       ; (HsOverLit (GhcPass 'Renamed)
lit', Maybe (HsExpr (GhcPass 'Renamed))
_) <- RnM
  ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
   FreeVars)
-> CpsRn
     (HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed)))
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (RnM
   ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
    FreeVars)
 -> CpsRn
      (HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))))
-> RnM
     ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
      FreeVars)
-> CpsRn
     (HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed)))
forall a b. (a -> b) -> a -> b
$ HsOverLit GhcPs
-> RnM
     ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
      FreeVars)
forall t.
HsOverLit t
-> RnM
     ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
      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 <- IOEnv (Env TcGblEnv TcLclEnv) (SyntaxExprRn, FreeVars)
-> CpsRn SyntaxExprRn
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (IOEnv (Env TcGblEnv TcLclEnv) (SyntaxExprRn, FreeVars)
 -> CpsRn SyntaxExprRn)
-> IOEnv (Env TcGblEnv TcLclEnv) (SyntaxExprRn, FreeVars)
-> CpsRn SyntaxExprRn
forall a b. (a -> b) -> a -> b
$ Name -> RnM (SyntaxExpr (GhcPass 'Renamed), FreeVars)
lookupSyntax Name
minusName
       ; SyntaxExprRn
ge    <- IOEnv (Env TcGblEnv TcLclEnv) (SyntaxExprRn, FreeVars)
-> CpsRn SyntaxExprRn
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (IOEnv (Env TcGblEnv TcLclEnv) (SyntaxExprRn, FreeVars)
 -> CpsRn SyntaxExprRn)
-> IOEnv (Env TcGblEnv TcLclEnv) (SyntaxExprRn, FreeVars)
-> CpsRn SyntaxExprRn
forall a b. (a -> b) -> a -> b
$ Name -> RnM (SyntaxExpr (GhcPass 'Renamed), FreeVars)
lookupSyntax Name
geName
       ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XNPlusKPat (GhcPass 'Renamed)
-> Located (IdP (GhcPass 'Renamed))
-> Located (HsOverLit (GhcPass 'Renamed))
-> HsOverLit (GhcPass 'Renamed)
-> SyntaxExpr (GhcPass 'Renamed)
-> SyntaxExpr (GhcPass 'Renamed)
-> Pat (GhcPass 'Renamed)
forall p.
XNPlusKPat p
-> Located (IdP p)
-> Located (HsOverLit p)
-> HsOverLit p
-> SyntaxExpr p
-> SyntaxExpr p
-> Pat p
NPlusKPat XNPlusKPat GhcPs
XNPlusKPat (GhcPass 'Renamed)
x (SrcSpan -> Name -> Located Name
forall l e. l -> e -> GenLocated l e
L (Name -> SrcSpan
nameSrcSpan Name
new_name) Name
new_name)
                             (SrcSpan
-> HsOverLit (GhcPass 'Renamed)
-> Located (HsOverLit (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L SrcSpan
l HsOverLit (GhcPass 'Renamed)
lit') HsOverLit (GhcPass 'Renamed)
lit' SyntaxExpr (GhcPass 'Renamed)
SyntaxExprRn
ge SyntaxExpr (GhcPass 'Renamed)
SyntaxExprRn
minus) }
                -- The Report says that n+k patterns must be in Integral

rnPatAndThen NameMaker
mk (AsPat XAsPat GhcPs
x GenLocated SrcSpan (IdP GhcPs)
rdr LPat GhcPs
pat)
  = do { Located Name
new_name <- NameMaker -> Located RdrName -> CpsRn (Located Name)
newPatLName NameMaker
mk Located RdrName
GenLocated SrcSpan (IdP GhcPs)
rdr
       ; Located (Pat (GhcPass 'Renamed))
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat (GhcPass 'Renamed))
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
       ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XAsPat (GhcPass 'Renamed)
-> Located (IdP (GhcPass 'Renamed))
-> LPat (GhcPass 'Renamed)
-> Pat (GhcPass 'Renamed)
forall p. XAsPat p -> Located (IdP p) -> LPat p -> Pat p
AsPat XAsPat GhcPs
XAsPat (GhcPass 'Renamed)
x Located Name
Located (IdP (GhcPass 'Renamed))
new_name Located (Pat (GhcPass 'Renamed))
LPat (GhcPass 'Renamed)
pat') }

rnPatAndThen NameMaker
mk p :: Pat GhcPs
p@(ViewPat XViewPat GhcPs
x LHsExpr GhcPs
expr LPat GhcPs
pat)
  = do { IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a. RnM a -> CpsRn a
liftCps (IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ())
-> IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a b. (a -> b) -> a -> b
$ do { Bool
vp_flag <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.ViewPatterns
                      ; Bool -> MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkErr Bool
vp_flag (Pat GhcPs -> MsgDoc
badViewPat Pat GhcPs
p) }
         -- Because of the way we're arranging the recursive calls,
         -- this will be in the right context
       ; LHsExpr (GhcPass 'Renamed)
expr' <- RnM (LHsExpr (GhcPass 'Renamed), FreeVars)
-> CpsRn (LHsExpr (GhcPass 'Renamed))
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (RnM (LHsExpr (GhcPass 'Renamed), FreeVars)
 -> CpsRn (LHsExpr (GhcPass 'Renamed)))
-> RnM (LHsExpr (GhcPass 'Renamed), FreeVars)
-> CpsRn (LHsExpr (GhcPass 'Renamed))
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcPs -> RnM (LHsExpr (GhcPass 'Renamed), FreeVars)
rnLExpr LHsExpr GhcPs
expr
       ; Located (Pat (GhcPass 'Renamed))
pat' <- NameMaker -> LPat GhcPs -> CpsRn (LPat (GhcPass 'Renamed))
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
       -- Note: at this point the PreTcType in ty can only be a placeHolder
       -- ; return (ViewPat expr' pat' ty) }
       ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XViewPat (GhcPass 'Renamed)
-> LHsExpr (GhcPass 'Renamed)
-> LPat (GhcPass 'Renamed)
-> Pat (GhcPass 'Renamed)
forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat XViewPat GhcPs
XViewPat (GhcPass 'Renamed)
x LHsExpr (GhcPass 'Renamed)
expr' Located (Pat (GhcPass 'Renamed))
LPat (GhcPass 'Renamed)
pat') }

rnPatAndThen NameMaker
mk (ConPat NoExtField
XConPat GhcPs
NoExtField Located (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 Located RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc Located RdrName
Located (ConLikeP GhcPs)
con RdrName -> RdrName -> Bool
forall a. Eq a => a -> a -> Bool
== Name -> RdrName
nameRdrName (DataCon -> Name
dataConName DataCon
nilDataCon) of
      Bool
True    -> do { Bool
ol_flag <- RnM Bool -> CpsRn Bool
forall a. RnM a -> CpsRn a
liftCps (RnM Bool -> CpsRn Bool) -> RnM Bool -> CpsRn Bool
forall a b. (a -> b) -> a -> b
$ Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.OverloadedLists
                    ; if Bool
ol_flag then NameMaker -> Pat GhcPs -> CpsRn (Pat (GhcPass 'Renamed))
rnPatAndThen NameMaker
mk (XListPat GhcPs -> [LPat GhcPs] -> Pat GhcPs
forall p. XListPat p -> [LPat p] -> Pat p
ListPat NoExtField
XListPat GhcPs
noExtField [])
                                 else NameMaker
-> Located RdrName
-> HsConPatDetails GhcPs
-> CpsRn (Pat (GhcPass 'Renamed))
rnConPatAndThen NameMaker
mk Located RdrName
Located (ConLikeP GhcPs)
con HsConPatDetails GhcPs
args}
      Bool
False   -> NameMaker
-> Located RdrName
-> HsConPatDetails GhcPs
-> CpsRn (Pat (GhcPass 'Renamed))
rnConPatAndThen NameMaker
mk Located RdrName
Located (ConLikeP GhcPs)
con HsConPatDetails GhcPs
args

rnPatAndThen NameMaker
mk (ListPat XListPat GhcPs
_ [LPat GhcPs]
pats)
  = do { Bool
opt_OverloadedLists <- RnM Bool -> CpsRn Bool
forall a. RnM a -> CpsRn a
liftCps (RnM Bool -> CpsRn Bool) -> RnM Bool -> CpsRn Bool
forall a b. (a -> b) -> a -> b
$ Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.OverloadedLists
       ; [Located (Pat (GhcPass 'Renamed))]
pats' <- NameMaker -> [LPat GhcPs] -> CpsRn [LPat (GhcPass 'Renamed)]
rnLPatsAndThen NameMaker
mk [LPat GhcPs]
pats
       ; case Bool
opt_OverloadedLists of
          Bool
True -> do { (SyntaxExprRn
to_list_name,FreeVars
_) <- IOEnv (Env TcGblEnv TcLclEnv) (SyntaxExprRn, FreeVars)
-> CpsRn (SyntaxExprRn, FreeVars)
forall a. RnM a -> CpsRn a
liftCps (IOEnv (Env TcGblEnv TcLclEnv) (SyntaxExprRn, FreeVars)
 -> CpsRn (SyntaxExprRn, FreeVars))
-> IOEnv (Env TcGblEnv TcLclEnv) (SyntaxExprRn, FreeVars)
-> CpsRn (SyntaxExprRn, FreeVars)
forall a b. (a -> b) -> a -> b
$ Name -> RnM (SyntaxExpr (GhcPass 'Renamed), FreeVars)
lookupSyntax Name
toListName
                     ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XListPat (GhcPass 'Renamed)
-> [LPat (GhcPass 'Renamed)] -> Pat (GhcPass 'Renamed)
forall p. XListPat p -> [LPat p] -> Pat p
ListPat (SyntaxExprRn -> Maybe SyntaxExprRn
forall a. a -> Maybe a
Just SyntaxExprRn
to_list_name) [Located (Pat (GhcPass 'Renamed))]
[LPat (GhcPass 'Renamed)]
pats')}
          Bool
False -> Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XListPat (GhcPass 'Renamed)
-> [LPat (GhcPass 'Renamed)] -> Pat (GhcPass 'Renamed)
forall p. XListPat p -> [LPat p] -> Pat p
ListPat XListPat (GhcPass 'Renamed)
forall a. Maybe a
Nothing [Located (Pat (GhcPass 'Renamed))]
[LPat (GhcPass 'Renamed)]
pats') }

rnPatAndThen NameMaker
mk (TuplePat XTuplePat GhcPs
x [LPat GhcPs]
pats Boxity
boxed)
  = do { IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a. RnM a -> CpsRn a
liftCps (IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ())
-> IOEnv (Env TcGblEnv TcLclEnv) () -> CpsRn ()
forall a b. (a -> b) -> a -> b
$ Int -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkTupSize ([Located (Pat GhcPs)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Located (Pat GhcPs)]
[LPat GhcPs]
pats)
       ; [Located (Pat (GhcPass 'Renamed))]
pats' <- NameMaker -> [LPat GhcPs] -> CpsRn [LPat (GhcPass 'Renamed)]
rnLPatsAndThen NameMaker
mk [LPat GhcPs]
pats
       ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XTuplePat (GhcPass 'Renamed)
-> [LPat (GhcPass 'Renamed)] -> Boxity -> Pat (GhcPass 'Renamed)
forall p. XTuplePat p -> [LPat p] -> Boxity -> Pat p
TuplePat XTuplePat GhcPs
XTuplePat (GhcPass 'Renamed)
x [Located (Pat (GhcPass 'Renamed))]
[LPat (GhcPass 'Renamed)]
pats' Boxity
boxed) }

rnPatAndThen NameMaker
mk (SumPat XSumPat GhcPs
x LPat GhcPs
pat Int
alt Int
arity)
  = do { Located (Pat (GhcPass 'Renamed))
pat <- NameMaker -> LPat GhcPs -> CpsRn (LPat (GhcPass 'Renamed))
rnLPatAndThen NameMaker
mk LPat GhcPs
pat
       ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (XSumPat (GhcPass 'Renamed)
-> LPat (GhcPass 'Renamed) -> Int -> Int -> Pat (GhcPass 'Renamed)
forall p. XSumPat p -> LPat p -> Int -> Int -> Pat p
SumPat XSumPat GhcPs
XSumPat (GhcPass 'Renamed)
x Located (Pat (GhcPass 'Renamed))
LPat (GhcPass 'Renamed)
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)))
  = XSplicePat (GhcPass 'Renamed)
-> HsSplice (GhcPass 'Renamed) -> Pat (GhcPass 'Renamed)
forall p. XSplicePat p -> HsSplice p -> Pat p
SplicePat XSplicePat GhcPs
XSplicePat (GhcPass 'Renamed)
x (HsSplice (GhcPass 'Renamed) -> Pat (GhcPass 'Renamed))
-> (Pat (GhcPass 'Renamed) -> HsSplice (GhcPass 'Renamed))
-> Pat (GhcPass 'Renamed)
-> Pat (GhcPass 'Renamed)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XSpliced (GhcPass 'Renamed)
-> ThModFinalizers
-> HsSplicedThing (GhcPass 'Renamed)
-> HsSplice (GhcPass 'Renamed)
forall id.
XSpliced id -> ThModFinalizers -> HsSplicedThing id -> HsSplice id
HsSpliced XSpliced GhcPs
XSpliced (GhcPass 'Renamed)
x2 ThModFinalizers
mfs (HsSplicedThing (GhcPass 'Renamed) -> HsSplice (GhcPass 'Renamed))
-> (Pat (GhcPass 'Renamed) -> HsSplicedThing (GhcPass 'Renamed))
-> Pat (GhcPass 'Renamed)
-> HsSplice (GhcPass 'Renamed)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pat (GhcPass 'Renamed) -> HsSplicedThing (GhcPass 'Renamed)
forall id. Pat id -> HsSplicedThing id
HsSplicedPat (Pat (GhcPass 'Renamed) -> Pat (GhcPass 'Renamed))
-> CpsRn (Pat (GhcPass 'Renamed)) -> CpsRn (Pat (GhcPass 'Renamed))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NameMaker -> Pat GhcPs -> CpsRn (Pat (GhcPass 'Renamed))
rnPatAndThen NameMaker
mk Pat GhcPs
pat

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

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

rnConPatAndThen :: NameMaker
-> Located RdrName
-> HsConPatDetails GhcPs
-> CpsRn (Pat (GhcPass 'Renamed))
rnConPatAndThen NameMaker
mk Located RdrName
con (PrefixCon [LPat GhcPs]
pats)
  = do  { Located Name
con' <- Located RdrName -> CpsRn (Located Name)
lookupConCps Located RdrName
con
        ; [Located (Pat (GhcPass 'Renamed))]
pats' <- NameMaker -> [LPat GhcPs] -> CpsRn [LPat (GhcPass 'Renamed)]
rnLPatsAndThen NameMaker
mk [LPat GhcPs]
pats
        ; Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall (m :: * -> *) a. Monad m => a -> m a
return (Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed)))
-> Pat (GhcPass 'Renamed) -> CpsRn (Pat (GhcPass 'Renamed))
forall a b. (a -> b) -> a -> b
$ ConPat :: forall p.
XConPat p -> Located (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat
            { pat_con_ext :: XConPat (GhcPass 'Renamed)
pat_con_ext = NoExtField
XConPat (GhcPass 'Renamed)
noExtField
            , pat_con :: Located (ConLikeP (GhcPass 'Renamed))
pat_con = Located Name
Located (ConLikeP (GhcPass 'Renamed))
con'
            , pat_args :: HsConPatDetails (GhcPass 'Renamed)
pat_args = [Located (Pat (GhcPass 'Renamed))]
-> HsConDetails
     (Located (Pat (GhcPass 'Renamed)))
     (HsRecFields (GhcPass 'Renamed) (Located (Pat (GhcPass 'Renamed))))
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [Located (Pat (GhcPass 'Renamed))]
pats'
            }
        }

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

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

checkUnusedRecordWildcardCps :: SrcSpan -> Maybe [Name] -> CpsRn ()
checkUnusedRecordWildcardCps :: SrcSpan -> Maybe [Name] -> CpsRn ()
checkUnusedRecordWildcardCps SrcSpan
loc Maybe [Name]
dotdot_names =
  (forall r. (() -> RnM (r, FreeVars)) -> RnM (r, FreeVars))
-> CpsRn ()
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] -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkUnusedRecordWildcard SrcSpan
loc FreeVars
fvs Maybe [Name]
dotdot_names
                    (r, FreeVars) -> RnM (r, FreeVars)
forall (m :: * -> *) a. Monad m => a -> m a
return (r
r, FreeVars
fvs) )
--------------------
rnHsRecPatsAndThen :: NameMaker
                   -> Located Name      -- Constructor
                   -> HsRecFields GhcPs (LPat GhcPs)
                   -> CpsRn (HsRecFields GhcRn (LPat GhcRn))
rnHsRecPatsAndThen :: NameMaker
-> Located Name
-> HsRecFields GhcPs (LPat GhcPs)
-> CpsRn (HsRecFields (GhcPass 'Renamed) (LPat (GhcPass 'Renamed)))
rnHsRecPatsAndThen NameMaker
mk (L SrcSpan
_ 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 { [LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs))]
flds <- RnM
  ([LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs))], FreeVars)
-> CpsRn [LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs))]
forall a. RnM (a, FreeVars) -> CpsRn a
liftCpsFV (RnM
   ([LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs))], FreeVars)
 -> CpsRn [LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs))])
-> RnM
     ([LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs))], FreeVars)
-> CpsRn [LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs))]
forall a b. (a -> b) -> a -> b
$ HsRecFieldContext
-> (SrcSpan -> RdrName -> Pat GhcPs)
-> HsRecFields GhcPs (Located (Pat GhcPs))
-> RnM
     ([LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs))], FreeVars)
forall arg.
HsRecFieldContext
-> (SrcSpan -> RdrName -> arg)
-> HsRecFields GhcPs (Located arg)
-> RnM ([LHsRecField (GhcPass 'Renamed) (Located arg)], FreeVars)
rnHsRecFields (Name -> HsRecFieldContext
HsRecFieldPat Name
con) SrcSpan -> RdrName -> Pat GhcPs
forall {p}. (XVarPat p ~ NoExtField) => SrcSpan -> IdP p -> Pat p
mkVarPat
                                            HsRecFields GhcPs (Located (Pat GhcPs))
HsRecFields GhcPs (LPat GhcPs)
hs_rec_fields
       ; [GenLocated
   SrcSpan
   (HsRecField'
      (FieldOcc (GhcPass 'Renamed)) (Located (Pat (GhcPass 'Renamed))))]
flds' <- ((LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs)), Int)
 -> CpsRn
      (GenLocated
         SrcSpan
         (HsRecField'
            (FieldOcc (GhcPass 'Renamed)) (Located (Pat (GhcPass 'Renamed))))))
-> [(LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs)), Int)]
-> CpsRn
     [GenLocated
        SrcSpan
        (HsRecField'
           (FieldOcc (GhcPass 'Renamed)) (Located (Pat (GhcPass 'Renamed))))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs)), Int)
-> CpsRn
     (GenLocated
        SrcSpan
        (HsRecField'
           (FieldOcc (GhcPass 'Renamed)) (Located (Pat (GhcPass 'Renamed)))))
rn_field ([LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs))]
flds [LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs))]
-> [Int]
-> [(LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs)), Int)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Int
1..])
       ; Maybe [Name] -> CpsRn ()
check_unused_wildcard ([GenLocated
   SrcSpan
   (HsRecField'
      (FieldOcc (GhcPass 'Renamed)) (LPat (GhcPass 'Renamed)))]
-> Located Int -> [IdP (GhcPass 'Renamed)]
forall {p} {l} {id} {l}.
CollectPass p =>
[GenLocated l (HsRecField' id (XRec p Pat))]
-> GenLocated l Int -> [IdP p]
implicit_binders [GenLocated
   SrcSpan
   (HsRecField'
      (FieldOcc (GhcPass 'Renamed)) (Located (Pat (GhcPass 'Renamed))))]
[GenLocated
   SrcSpan
   (HsRecField'
      (FieldOcc (GhcPass 'Renamed)) (LPat (GhcPass 'Renamed)))]
flds' (Located Int -> [Name]) -> Maybe (Located Int) -> Maybe [Name]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Located Int)
dd)
       ; HsRecFields (GhcPass 'Renamed) (Located (Pat (GhcPass 'Renamed)))
-> CpsRn
     (HsRecFields (GhcPass 'Renamed) (Located (Pat (GhcPass 'Renamed))))
forall (m :: * -> *) a. Monad m => a -> m a
return (HsRecFields :: forall p arg.
[LHsRecField p arg] -> Maybe (Located Int) -> HsRecFields p arg
HsRecFields { rec_flds :: [GenLocated
   SrcSpan
   (HsRecField'
      (FieldOcc (GhcPass 'Renamed)) (Located (Pat (GhcPass 'Renamed))))]
rec_flds = [GenLocated
   SrcSpan
   (HsRecField'
      (FieldOcc (GhcPass 'Renamed)) (Located (Pat (GhcPass 'Renamed))))]
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 = XVarPat p -> Located (IdP p) -> Pat p
forall p. XVarPat p -> Located (IdP p) -> Pat p
VarPat NoExtField
XVarPat p
noExtField (SrcSpan -> IdP p -> Located (IdP p)
forall l e. l -> e -> GenLocated l e
L SrcSpan
l IdP p
n)
    rn_field :: (LHsRecField (GhcPass 'Renamed) (Located (Pat GhcPs)), Int)
-> CpsRn
     (GenLocated
        SrcSpan
        (HsRecField'
           (FieldOcc (GhcPass 'Renamed)) (Located (Pat (GhcPass 'Renamed)))))
rn_field (L SrcSpan
l HsRecField' (FieldOcc (GhcPass 'Renamed)) (Located (Pat GhcPs))
fld, Int
n') =
      do { Located (Pat (GhcPass 'Renamed))
arg' <- NameMaker -> LPat GhcPs -> CpsRn (LPat (GhcPass 'Renamed))
rnLPatAndThen (Maybe (Located Int) -> NameMaker -> Int -> NameMaker
forall {a} {l}.
Ord a =>
Maybe (GenLocated l a) -> NameMaker -> a -> NameMaker
nested_mk Maybe (Located Int)
dd NameMaker
mk Int
n') (HsRecField' (FieldOcc (GhcPass 'Renamed)) (Located (Pat GhcPs))
-> Located (Pat GhcPs)
forall id arg. HsRecField' id arg -> arg
hsRecFieldArg HsRecField' (FieldOcc (GhcPass 'Renamed)) (Located (Pat GhcPs))
fld)
         ; GenLocated
  SrcSpan
  (HsRecField'
     (FieldOcc (GhcPass 'Renamed)) (Located (Pat (GhcPass 'Renamed))))
-> CpsRn
     (GenLocated
        SrcSpan
        (HsRecField'
           (FieldOcc (GhcPass 'Renamed)) (Located (Pat (GhcPass 'Renamed)))))
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpan
-> HsRecField'
     (FieldOcc (GhcPass 'Renamed)) (Located (Pat (GhcPass 'Renamed)))
-> GenLocated
     SrcSpan
     (HsRecField'
        (FieldOcc (GhcPass 'Renamed)) (Located (Pat (GhcPass 'Renamed))))
forall l e. l -> e -> GenLocated l e
L SrcSpan
l (HsRecField' (FieldOcc (GhcPass 'Renamed)) (Located (Pat GhcPs))
fld { hsRecFieldArg :: Located (Pat (GhcPass 'Renamed))
hsRecFieldArg = Located (Pat (GhcPass 'Renamed))
arg' })) }

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

    -- Get the arguments of the implicit binders
    implicit_binders :: [GenLocated l (HsRecField' id (XRec p Pat))]
-> GenLocated l Int -> [IdP p]
implicit_binders [GenLocated l (HsRecField' id (XRec p Pat))]
fs (GenLocated l Int -> Int
forall l e. GenLocated l e -> e
unLoc -> Int
n) = [XRec p Pat] -> [IdP p]
forall p. CollectPass p => [LPat p] -> [IdP p]
collectPatsBinders [XRec p Pat]
implicit_pats
      where
        implicit_pats :: [XRec p Pat]
implicit_pats = (GenLocated l (HsRecField' id (XRec p Pat)) -> XRec p Pat)
-> [GenLocated l (HsRecField' id (XRec p Pat))] -> [XRec p Pat]
forall a b. (a -> b) -> [a] -> [b]
map (HsRecField' id (XRec p Pat) -> XRec p Pat
forall id arg. HsRecField' id arg -> arg
hsRecFieldArg (HsRecField' id (XRec p Pat) -> XRec p Pat)
-> (GenLocated l (HsRecField' id (XRec p Pat))
    -> HsRecField' id (XRec p Pat))
-> GenLocated l (HsRecField' id (XRec p Pat))
-> XRec p Pat
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated l (HsRecField' id (XRec p Pat))
-> HsRecField' id (XRec p Pat)
forall l e. GenLocated l e -> e
unLoc) (Int
-> [GenLocated l (HsRecField' id (XRec p Pat))]
-> [GenLocated l (HsRecField' id (XRec p Pat))]
forall a. Int -> [a] -> [a]
drop Int
n [GenLocated l (HsRecField' id (XRec p Pat))]
fs)

    -- Don't warn for let P{..} = ... in ...
    check_unused_wildcard :: Maybe [Name] -> CpsRn ()
check_unused_wildcard = case NameMaker
mk of
                              LetMk{} -> CpsRn () -> Maybe [Name] -> CpsRn ()
forall a b. a -> b -> a
const (() -> CpsRn ()
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 (GenLocated l a -> a
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' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
n))

{-
************************************************************************
*                                                                      *
        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 (Located arg)
    -> RnM ([LHsRecField GhcRn (Located 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 (Located arg)
-> RnM ([LHsRecField (GhcPass 'Renamed) (Located 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 (Located arg)]
flds, rec_dotdot :: forall p arg. HsRecFields p arg -> Maybe (Located Int)
rec_dotdot = Maybe (Located Int)
dotdot })
  = do { Bool
pun_ok      <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.RecordPuns
       ; Bool
disambig_ok <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DisambiguateRecordFields
       ; let parent :: Maybe Name
parent = Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
disambig_ok Maybe () -> Maybe Name -> Maybe Name
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe Name
mb_con
       ; [LHsRecField (GhcPass 'Renamed) (Located arg)]
flds1  <- (LHsRecField GhcPs (Located arg)
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (LHsRecField (GhcPass 'Renamed) (Located arg)))
-> [LHsRecField GhcPs (Located arg)]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     [LHsRecField (GhcPass 'Renamed) (Located arg)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Bool
-> Maybe Name
-> LHsRecField GhcPs (Located arg)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (LHsRecField (GhcPass 'Renamed) (Located arg))
rn_fld Bool
pun_ok Maybe Name
parent) [LHsRecField GhcPs (Located arg)]
flds
       ; (NonEmpty RdrName -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> [NonEmpty RdrName] -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr (MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> (NonEmpty RdrName -> MsgDoc)
-> NonEmpty RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecFieldContext -> NonEmpty RdrName -> MsgDoc
dupFieldErr HsRecFieldContext
ctxt) [NonEmpty RdrName]
dup_flds
       ; [LHsRecField (GhcPass 'Renamed) (Located arg)]
dotdot_flds <- Maybe (Located Int)
-> Maybe Name
-> [LHsRecField (GhcPass 'Renamed) (Located arg)]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     [LHsRecField (GhcPass 'Renamed) (Located arg)]
rn_dotdot Maybe (Located Int)
dotdot Maybe Name
mb_con [LHsRecField (GhcPass 'Renamed) (Located arg)]
flds1
       ; let all_flds :: [LHsRecField (GhcPass 'Renamed) (Located arg)]
all_flds | [LHsRecField (GhcPass 'Renamed) (Located arg)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LHsRecField (GhcPass 'Renamed) (Located arg)]
dotdot_flds = [LHsRecField (GhcPass 'Renamed) (Located arg)]
flds1
                      | Bool
otherwise        = [LHsRecField (GhcPass 'Renamed) (Located arg)]
flds1 [LHsRecField (GhcPass 'Renamed) (Located arg)]
-> [LHsRecField (GhcPass 'Renamed) (Located arg)]
-> [LHsRecField (GhcPass 'Renamed) (Located arg)]
forall a. [a] -> [a] -> [a]
++ [LHsRecField (GhcPass 'Renamed) (Located arg)]
dotdot_flds
       ; ([LHsRecField (GhcPass 'Renamed) (Located arg)], FreeVars)
-> RnM ([LHsRecField (GhcPass 'Renamed) (Located arg)], FreeVars)
forall (m :: * -> *) a. Monad m => a -> m a
return ([LHsRecField (GhcPass 'Renamed) (Located arg)]
all_flds, [Name] -> FreeVars
mkFVs ([LHsRecField (GhcPass 'Renamed) (Located arg)] -> [Name]
forall arg. [LHsRecField (GhcPass 'Renamed) arg] -> [Name]
getFieldIds [LHsRecField (GhcPass 'Renamed) (Located arg)]
all_flds)) }
  where
    mb_con :: Maybe Name
mb_con = case HsRecFieldContext
ctxt of
                HsRecFieldCon Name
con  -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
con
                HsRecFieldPat Name
con  -> Name -> Maybe Name
forall a. a -> Maybe a
Just Name
con
                HsRecFieldContext
_ {- update -}     -> Maybe Name
forall a. Maybe a
Nothing

    rn_fld :: Bool -> Maybe Name -> LHsRecField GhcPs (Located arg)
           -> RnM (LHsRecField GhcRn (Located arg))
    rn_fld :: Bool
-> Maybe Name
-> LHsRecField GhcPs (Located arg)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (LHsRecField (GhcPass 'Renamed) (Located arg))
rn_fld Bool
pun_ok Maybe Name
parent (L SrcSpan
l
                           (HsRecField
                              { hsRecFieldLbl :: forall id arg. HsRecField' id arg -> Located id
hsRecFieldLbl =
                                  (L SrcSpan
loc (FieldOcc XCFieldOcc GhcPs
_ (L SrcSpan
ll RdrName
lbl)))
                              , hsRecFieldArg :: forall id arg. HsRecField' id arg -> arg
hsRecFieldArg = Located arg
arg
                              , hsRecPun :: forall id arg. HsRecField' id arg -> Bool
hsRecPun      = Bool
pun }))
      = do { Name
sel <- SrcSpan -> RnM Name -> RnM Name
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
loc (RnM Name -> RnM Name) -> RnM Name -> RnM Name
forall a b. (a -> b) -> a -> b
$ Maybe Name -> RdrName -> RnM Name
lookupRecFieldOcc Maybe Name
parent RdrName
lbl
           ; Located arg
arg' <- if Bool
pun
                     then do { Bool -> MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkErr Bool
pun_ok (Located RdrName -> MsgDoc
badPun (SrcSpan -> RdrName -> Located RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc RdrName
lbl))
                               -- Discard any module qualifier (#11662)
                             ; let arg_rdr :: RdrName
arg_rdr = OccName -> RdrName
mkRdrUnqual (RdrName -> OccName
rdrNameOcc RdrName
lbl)
                             ; Located arg -> IOEnv (Env TcGblEnv TcLclEnv) (Located arg)
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpan -> arg -> Located arg
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (SrcSpan -> RdrName -> arg
mk_arg SrcSpan
loc RdrName
arg_rdr)) }
                     else Located arg -> IOEnv (Env TcGblEnv TcLclEnv) (Located arg)
forall (m :: * -> *) a. Monad m => a -> m a
return Located arg
arg
           ; LHsRecField (GhcPass 'Renamed) (Located arg)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (LHsRecField (GhcPass 'Renamed) (Located arg))
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpan
-> HsRecField' (FieldOcc (GhcPass 'Renamed)) (Located arg)
-> LHsRecField (GhcPass 'Renamed) (Located arg)
forall l e. l -> e -> GenLocated l e
L SrcSpan
l (HsRecField :: forall id arg. Located id -> arg -> Bool -> HsRecField' id arg
HsRecField
                             { hsRecFieldLbl :: Located (FieldOcc (GhcPass 'Renamed))
hsRecFieldLbl = (SrcSpan
-> FieldOcc (GhcPass 'Renamed)
-> Located (FieldOcc (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (XCFieldOcc (GhcPass 'Renamed)
-> Located RdrName -> FieldOcc (GhcPass 'Renamed)
forall pass. XCFieldOcc pass -> Located RdrName -> FieldOcc pass
FieldOcc
                                                          Name
XCFieldOcc (GhcPass 'Renamed)
sel (SrcSpan -> RdrName -> Located RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpan
ll RdrName
lbl)))
                             , hsRecFieldArg :: Located arg
hsRecFieldArg = Located arg
arg'
                             , hsRecPun :: Bool
hsRecPun      = 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 (Located arg)] -- Explicit fields
              -> RnM ([LHsRecField GhcRn (Located arg)])   -- Field Labels we need to fill in
    rn_dotdot :: Maybe (Located Int)
-> Maybe Name
-> [LHsRecField (GhcPass 'Renamed) (Located arg)]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     [LHsRecField (GhcPass 'Renamed) (Located arg)]
rn_dotdot (Just (L SrcSpan
loc Int
n)) (Just Name
con) [LHsRecField (GhcPass 'Renamed) (Located 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.
      = ASSERT( flds `lengthIs` n )
        do { Bool
dd_flag <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.RecordWildCards
           ; Bool -> MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkErr Bool
dd_flag (HsRecFieldContext -> MsgDoc
needFlagDotDot HsRecFieldContext
ctxt)
           ; (GlobalRdrEnv
rdr_env, LocalRdrEnv
lcl_env) <- TcRn (GlobalRdrEnv, LocalRdrEnv)
getRdrEnvs
           ; [FieldLabel]
con_fields <- Name -> RnM [FieldLabel]
lookupConstructorFields Name
con
           ; Bool
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([FieldLabel] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FieldLabel]
con_fields) (MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr (Name -> MsgDoc
badDotDotCon Name
con))
           ; let present_flds :: OccSet
present_flds = [OccName] -> OccSet
mkOccSet ([OccName] -> OccSet) -> [OccName] -> OccSet
forall a b. (a -> b) -> a -> b
$ (RdrName -> OccName) -> [RdrName] -> [OccName]
forall a b. (a -> b) -> [a] -> [b]
map RdrName -> OccName
rdrNameOcc ([LHsRecField (GhcPass 'Renamed) (Located arg)] -> [RdrName]
forall id arg. [LHsRecField id arg] -> [RdrName]
getFieldLbls [LHsRecField (GhcPass 'Renamed) (Located 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)
                        = [(FieldLabel, GlobalRdrElt)] -> ([FieldLabel], [GlobalRdrElt])
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
forall a. FieldLbl a -> 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] -> IOEnv (Env TcGblEnv TcLclEnv) ()
addUsedGREs [GlobalRdrElt]
dot_dot_gres
           ; [LHsRecField (GhcPass 'Renamed) (Located arg)]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     [LHsRecField (GhcPass 'Renamed) (Located arg)]
forall (m :: * -> *) a. Monad m => a -> m a
return [ SrcSpan
-> HsRecField' (FieldOcc (GhcPass 'Renamed)) (Located arg)
-> LHsRecField (GhcPass 'Renamed) (Located arg)
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (HsRecField :: forall id arg. Located id -> arg -> Bool -> HsRecField' id arg
HsRecField
                        { hsRecFieldLbl :: Located (FieldOcc (GhcPass 'Renamed))
hsRecFieldLbl = SrcSpan
-> FieldOcc (GhcPass 'Renamed)
-> Located (FieldOcc (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (XCFieldOcc (GhcPass 'Renamed)
-> Located RdrName -> FieldOcc (GhcPass 'Renamed)
forall pass. XCFieldOcc pass -> Located RdrName -> FieldOcc pass
FieldOcc Name
XCFieldOcc (GhcPass 'Renamed)
sel (SrcSpan -> RdrName -> Located RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc RdrName
arg_rdr))
                        , hsRecFieldArg :: Located arg
hsRecFieldArg = SrcSpan -> arg -> Located arg
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (SrcSpan -> RdrName -> arg
mk_arg SrcSpan
loc RdrName
arg_rdr)
                        , hsRecPun :: Bool
hsRecPun      = Bool
False })
                    | FieldLabel
fl <- [FieldLabel]
dot_dot_fields
                    , let sel :: Name
sel     = FieldLabel -> Name
forall a. FieldLbl a -> a
flSelector FieldLabel
fl
                    , let arg_rdr :: RdrName
arg_rdr = FastString -> RdrName
mkVarUnqual (FieldLabel -> FastString
forall a. FieldLbl a -> FastString
flLabel FieldLabel
fl) ] }

    rn_dotdot Maybe (Located Int)
_dotdot Maybe Name
_mb_con [LHsRecField (GhcPass 'Renamed) (Located arg)]
_flds
      = [LHsRecField (GhcPass 'Renamed) (Located arg)]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     [LHsRecField (GhcPass 'Renamed) (Located arg)]
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) = (RdrName -> RdrName -> Ordering)
-> [RdrName] -> ([RdrName], [NonEmpty RdrName])
forall a. (a -> a -> Ordering) -> [a] -> ([a], [NonEmpty a])
removeDups RdrName -> RdrName -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ([LHsRecField GhcPs (Located arg)] -> [RdrName]
forall id arg. [LHsRecField id arg] -> [RdrName]
getFieldLbls [LHsRecField GhcPs (Located 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 (GhcPass 'Renamed)], FreeVars)
rnHsRecUpdFields [LHsRecUpdField GhcPs]
flds
  = do { Bool
pun_ok        <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.RecordPuns
       ; Bool
overload_ok   <- Extension -> RnM Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DuplicateRecordFields
       ; ([LHsRecUpdField (GhcPass 'Renamed)]
flds1, [FreeVars]
fvss) <- (LHsRecUpdField GhcPs
 -> IOEnv
      (Env TcGblEnv TcLclEnv)
      (LHsRecUpdField (GhcPass 'Renamed), FreeVars))
-> [LHsRecUpdField GhcPs]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     ([LHsRecUpdField (GhcPass 'Renamed)], [FreeVars])
forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM (Bool
-> Bool
-> LHsRecUpdField GhcPs
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (LHsRecUpdField (GhcPass 'Renamed), FreeVars)
rn_fld Bool
pun_ok Bool
overload_ok) [LHsRecUpdField GhcPs]
flds
       ; (NonEmpty RdrName -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> [NonEmpty RdrName] -> IOEnv (Env TcGblEnv TcLclEnv) ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr (MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> (NonEmpty RdrName -> MsgDoc)
-> NonEmpty RdrName
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecFieldContext -> NonEmpty RdrName -> MsgDoc
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
       ; Bool
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([LHsRecUpdField GhcPs] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LHsRecUpdField GhcPs]
flds) (IOEnv (Env TcGblEnv TcLclEnv) ()
 -> IOEnv (Env TcGblEnv TcLclEnv) ())
-> IOEnv (Env TcGblEnv TcLclEnv) ()
-> IOEnv (Env TcGblEnv TcLclEnv) ()
forall a b. (a -> b) -> a -> b
$ MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr MsgDoc
emptyUpdateErr

       ; ([LHsRecUpdField (GhcPass 'Renamed)], FreeVars)
-> RnM ([LHsRecUpdField (GhcPass 'Renamed)], FreeVars)
forall (m :: * -> *) a. Monad m => a -> m a
return ([LHsRecUpdField (GhcPass 'Renamed)]
flds1, [FreeVars] -> FreeVars
plusFVs [FreeVars]
fvss) }
  where
    doc :: MsgDoc
doc = String -> MsgDoc
text String
"constructor field name"

    rn_fld :: Bool -> Bool -> LHsRecUpdField GhcPs
           -> RnM (LHsRecUpdField GhcRn, FreeVars)
    rn_fld :: Bool
-> Bool
-> LHsRecUpdField GhcPs
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (LHsRecUpdField (GhcPass 'Renamed), FreeVars)
rn_fld Bool
pun_ok Bool
overload_ok (L SrcSpan
l (HsRecField { hsRecFieldLbl :: forall id arg. HsRecField' id arg -> Located id
hsRecFieldLbl = L SrcSpan
loc AmbiguousFieldOcc GhcPs
f
                                               , hsRecFieldArg :: forall id arg. HsRecField' id arg -> arg
hsRecFieldArg = LHsExpr GhcPs
arg
                                               , hsRecPun :: forall id arg. HsRecField' id arg -> Bool
hsRecPun      = Bool
pun }))
      = do { let lbl :: RdrName
lbl = AmbiguousFieldOcc GhcPs -> RdrName
forall (p :: Pass). AmbiguousFieldOcc (GhcPass p) -> RdrName
rdrNameAmbiguousFieldOcc AmbiguousFieldOcc GhcPs
f
           ; Either Name [Name]
sel <- SrcSpan -> TcRn (Either Name [Name]) -> TcRn (Either Name [Name])
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan SrcSpan
loc (TcRn (Either Name [Name]) -> TcRn (Either Name [Name]))
-> TcRn (Either Name [Name]) -> TcRn (Either Name [Name])
forall a b. (a -> b) -> a -> b
$
                      -- Defer renaming of overloaded fields to the typechecker
                      -- See Note [Disambiguating record fields] in GHC.Tc.Gen.Expr
                      if Bool
overload_ok
                          then do { Maybe (Either Name [Name])
mb <- Bool -> RdrName -> RnM (Maybe (Either Name [Name]))
lookupGlobalOccRn_overloaded
                                            Bool
overload_ok RdrName
lbl
                                  ; case Maybe (Either Name [Name])
mb of
                                      Maybe (Either Name [Name])
Nothing ->
                                        do { MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) ()
addErr
                                               (MsgDoc -> RdrName -> MsgDoc
unknownSubordinateErr MsgDoc
doc RdrName
lbl)
                                           ; Either Name [Name] -> TcRn (Either Name [Name])
forall (m :: * -> *) a. Monad m => a -> m a
return ([Name] -> Either Name [Name]
forall a b. b -> Either a b
Right []) }
                                      Just Either Name [Name]
r  -> Either Name [Name] -> TcRn (Either Name [Name])
forall (m :: * -> *) a. Monad m => a -> m a
return Either Name [Name]
r }
                          else (Name -> Either Name [Name])
-> RnM Name -> TcRn (Either Name [Name])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Name -> Either Name [Name]
forall a b. a -> Either a b
Left (RnM Name -> TcRn (Either Name [Name]))
-> RnM Name -> TcRn (Either Name [Name])
forall a b. (a -> b) -> a -> b
$ RdrName -> RnM Name
lookupGlobalOccRn RdrName
lbl
           ; LHsExpr GhcPs
arg' <- if Bool
pun
                     then do { Bool -> MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkErr Bool
pun_ok (Located RdrName -> MsgDoc
badPun (SrcSpan -> RdrName -> Located RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc RdrName
lbl))
                               -- Discard any module qualifier (#11662)
                             ; let arg_rdr :: RdrName
arg_rdr = OccName -> RdrName
mkRdrUnqual (RdrName -> OccName
rdrNameOcc RdrName
lbl)
                             ; LHsExpr GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) (LHsExpr GhcPs)
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpan -> HsExpr GhcPs -> LHsExpr GhcPs
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (XVar GhcPs -> GenLocated SrcSpan (IdP GhcPs) -> HsExpr GhcPs
forall p. XVar p -> Located (IdP p) -> HsExpr p
HsVar NoExtField
XVar GhcPs
noExtField (SrcSpan -> RdrName -> Located RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc RdrName
arg_rdr))) }
                     else LHsExpr GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) (LHsExpr GhcPs)
forall (m :: * -> *) a. Monad m => a -> m a
return LHsExpr GhcPs
arg
           ; (LHsExpr (GhcPass 'Renamed)
arg'', FreeVars
fvs) <- LHsExpr GhcPs -> RnM (LHsExpr (GhcPass 'Renamed), FreeVars)
rnLExpr LHsExpr GhcPs
arg'

           ; let fvs' :: FreeVars
fvs' = case Either Name [Name]
sel of
                          Left Name
sel_name -> FreeVars
fvs FreeVars -> Name -> FreeVars
`addOneFV` Name
sel_name
                          Right [Name
sel_name] -> FreeVars
fvs FreeVars -> Name -> FreeVars
`addOneFV` Name
sel_name
                          Right [Name]
_       -> FreeVars
fvs
                 lbl' :: GenLocated SrcSpan (AmbiguousFieldOcc (GhcPass 'Renamed))
lbl' = case Either Name [Name]
sel of
                          Left Name
sel_name ->
                                     SrcSpan
-> AmbiguousFieldOcc (GhcPass 'Renamed)
-> GenLocated SrcSpan (AmbiguousFieldOcc (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (XUnambiguous (GhcPass 'Renamed)
-> Located RdrName -> AmbiguousFieldOcc (GhcPass 'Renamed)
forall pass.
XUnambiguous pass -> Located RdrName -> AmbiguousFieldOcc pass
Unambiguous Name
XUnambiguous (GhcPass 'Renamed)
sel_name   (SrcSpan -> RdrName -> Located RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc RdrName
lbl))
                          Right [Name
sel_name] ->
                                     SrcSpan
-> AmbiguousFieldOcc (GhcPass 'Renamed)
-> GenLocated SrcSpan (AmbiguousFieldOcc (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (XUnambiguous (GhcPass 'Renamed)
-> Located RdrName -> AmbiguousFieldOcc (GhcPass 'Renamed)
forall pass.
XUnambiguous pass -> Located RdrName -> AmbiguousFieldOcc pass
Unambiguous Name
XUnambiguous (GhcPass 'Renamed)
sel_name   (SrcSpan -> RdrName -> Located RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc RdrName
lbl))
                          Right [Name]
_ -> SrcSpan
-> AmbiguousFieldOcc (GhcPass 'Renamed)
-> GenLocated SrcSpan (AmbiguousFieldOcc (GhcPass 'Renamed))
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc (XAmbiguous (GhcPass 'Renamed)
-> Located RdrName -> AmbiguousFieldOcc (GhcPass 'Renamed)
forall pass.
XAmbiguous pass -> Located RdrName -> AmbiguousFieldOcc pass
Ambiguous   NoExtField
XAmbiguous (GhcPass 'Renamed)
noExtField (SrcSpan -> RdrName -> Located RdrName
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc RdrName
lbl))

           ; (LHsRecUpdField (GhcPass 'Renamed), FreeVars)
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (LHsRecUpdField (GhcPass 'Renamed), FreeVars)
forall (m :: * -> *) a. Monad m => a -> m a
return (SrcSpan
-> HsRecField'
     (AmbiguousFieldOcc (GhcPass 'Renamed)) (LHsExpr (GhcPass 'Renamed))
-> LHsRecUpdField (GhcPass 'Renamed)
forall l e. l -> e -> GenLocated l e
L SrcSpan
l (HsRecField :: forall id arg. Located id -> arg -> Bool -> HsRecField' id arg
HsRecField { hsRecFieldLbl :: GenLocated SrcSpan (AmbiguousFieldOcc (GhcPass 'Renamed))
hsRecFieldLbl = GenLocated SrcSpan (AmbiguousFieldOcc (GhcPass 'Renamed))
lbl'
                                     , hsRecFieldArg :: LHsExpr (GhcPass 'Renamed)
hsRecFieldArg = LHsExpr (GhcPass 'Renamed)
arg''
                                     , hsRecPun :: Bool
hsRecPun      = 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) = (RdrName -> RdrName -> Ordering)
-> [RdrName] -> ([RdrName], [NonEmpty RdrName])
forall a. (a -> a -> Ordering) -> [a] -> ([a], [NonEmpty a])
removeDups RdrName -> RdrName -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ([LHsRecUpdField GhcPs] -> [RdrName]
getFieldUpdLbls [LHsRecUpdField GhcPs]
flds)



getFieldIds :: [LHsRecField GhcRn arg] -> [Name]
getFieldIds :: forall arg. [LHsRecField (GhcPass 'Renamed) arg] -> [Name]
getFieldIds [LHsRecField (GhcPass 'Renamed) arg]
flds = (LHsRecField (GhcPass 'Renamed) arg -> Name)
-> [LHsRecField (GhcPass 'Renamed) arg] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map (Located Name -> Name
forall l e. GenLocated l e -> e
unLoc (Located Name -> Name)
-> (LHsRecField (GhcPass 'Renamed) arg -> Located Name)
-> LHsRecField (GhcPass 'Renamed) arg
-> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecField (GhcPass 'Renamed) arg -> Located Name
forall pass arg. HsRecField pass arg -> Located (XCFieldOcc pass)
hsRecFieldSel (HsRecField (GhcPass 'Renamed) arg -> Located Name)
-> (LHsRecField (GhcPass 'Renamed) arg
    -> HsRecField (GhcPass 'Renamed) arg)
-> LHsRecField (GhcPass 'Renamed) arg
-> Located Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsRecField (GhcPass 'Renamed) arg
-> HsRecField (GhcPass 'Renamed) arg
forall l e. GenLocated l e -> e
unLoc) [LHsRecField (GhcPass 'Renamed) arg]
flds

getFieldLbls :: [LHsRecField id arg] -> [RdrName]
getFieldLbls :: forall id arg. [LHsRecField id arg] -> [RdrName]
getFieldLbls [LHsRecField id arg]
flds
  = (LHsRecField id arg -> RdrName)
-> [LHsRecField id arg] -> [RdrName]
forall a b. (a -> b) -> [a] -> [b]
map (Located RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc (Located RdrName -> RdrName)
-> (LHsRecField id arg -> Located RdrName)
-> LHsRecField id arg
-> RdrName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldOcc id -> Located RdrName
forall pass. FieldOcc pass -> Located RdrName
rdrNameFieldOcc (FieldOcc id -> Located RdrName)
-> (LHsRecField id arg -> FieldOcc id)
-> LHsRecField id arg
-> Located RdrName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpan (FieldOcc id) -> FieldOcc id
forall l e. GenLocated l e -> e
unLoc (GenLocated SrcSpan (FieldOcc id) -> FieldOcc id)
-> (LHsRecField id arg -> GenLocated SrcSpan (FieldOcc id))
-> LHsRecField id arg
-> FieldOcc id
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecField' (FieldOcc id) arg -> GenLocated SrcSpan (FieldOcc id)
forall id arg. HsRecField' id arg -> Located id
hsRecFieldLbl (HsRecField' (FieldOcc id) arg -> GenLocated SrcSpan (FieldOcc id))
-> (LHsRecField id arg -> HsRecField' (FieldOcc id) arg)
-> LHsRecField id arg
-> GenLocated SrcSpan (FieldOcc id)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsRecField id arg -> HsRecField' (FieldOcc id) arg
forall l e. GenLocated l e -> e
unLoc) [LHsRecField id arg]
flds

getFieldUpdLbls :: [LHsRecUpdField GhcPs] -> [RdrName]
getFieldUpdLbls :: [LHsRecUpdField GhcPs] -> [RdrName]
getFieldUpdLbls [LHsRecUpdField GhcPs]
flds = (LHsRecUpdField GhcPs -> RdrName)
-> [LHsRecUpdField GhcPs] -> [RdrName]
forall a b. (a -> b) -> [a] -> [b]
map (AmbiguousFieldOcc GhcPs -> RdrName
forall (p :: Pass). AmbiguousFieldOcc (GhcPass p) -> RdrName
rdrNameAmbiguousFieldOcc (AmbiguousFieldOcc GhcPs -> RdrName)
-> (LHsRecUpdField GhcPs -> AmbiguousFieldOcc GhcPs)
-> LHsRecUpdField GhcPs
-> RdrName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpan (AmbiguousFieldOcc GhcPs)
-> AmbiguousFieldOcc GhcPs
forall l e. GenLocated l e -> e
unLoc (GenLocated SrcSpan (AmbiguousFieldOcc GhcPs)
 -> AmbiguousFieldOcc GhcPs)
-> (LHsRecUpdField GhcPs
    -> GenLocated SrcSpan (AmbiguousFieldOcc GhcPs))
-> LHsRecUpdField GhcPs
-> AmbiguousFieldOcc GhcPs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsRecField' (AmbiguousFieldOcc GhcPs) (LHsExpr GhcPs)
-> GenLocated SrcSpan (AmbiguousFieldOcc GhcPs)
forall id arg. HsRecField' id arg -> Located id
hsRecFieldLbl (HsRecField' (AmbiguousFieldOcc GhcPs) (LHsExpr GhcPs)
 -> GenLocated SrcSpan (AmbiguousFieldOcc GhcPs))
-> (LHsRecUpdField GhcPs
    -> HsRecField' (AmbiguousFieldOcc GhcPs) (LHsExpr GhcPs))
-> LHsRecUpdField GhcPs
-> GenLocated SrcSpan (AmbiguousFieldOcc GhcPs)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsRecUpdField GhcPs
-> HsRecField' (AmbiguousFieldOcc GhcPs) (LHsExpr GhcPs)
forall l e. GenLocated l e -> e
unLoc) [LHsRecUpdField GhcPs]
flds

needFlagDotDot :: HsRecFieldContext -> SDoc
needFlagDotDot :: HsRecFieldContext -> MsgDoc
needFlagDotDot HsRecFieldContext
ctxt = [MsgDoc] -> MsgDoc
vcat [String -> MsgDoc
text String
"Illegal `..' in record" MsgDoc -> MsgDoc -> MsgDoc
<+> HsRecFieldContext -> MsgDoc
pprRFC HsRecFieldContext
ctxt,
                            String -> MsgDoc
text String
"Use RecordWildCards to permit this"]

badDotDotCon :: Name -> SDoc
badDotDotCon :: Name -> MsgDoc
badDotDotCon Name
con
  = [MsgDoc] -> MsgDoc
vcat [ String -> MsgDoc
text String
"Illegal `..' notation for constructor" MsgDoc -> MsgDoc -> MsgDoc
<+> MsgDoc -> MsgDoc
quotes (Name -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Name
con)
         , Int -> MsgDoc -> MsgDoc
nest Int
2 (String -> MsgDoc
text String
"The constructor has no labelled fields") ]

emptyUpdateErr :: SDoc
emptyUpdateErr :: MsgDoc
emptyUpdateErr = String -> MsgDoc
text String
"Empty record update"

badPun :: Located RdrName -> SDoc
badPun :: Located RdrName -> MsgDoc
badPun Located RdrName
fld = [MsgDoc] -> MsgDoc
vcat [String -> MsgDoc
text String
"Illegal use of punning for field" MsgDoc -> MsgDoc -> MsgDoc
<+> MsgDoc -> MsgDoc
quotes (Located RdrName -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Located RdrName
fld),
                   String -> MsgDoc
text String
"Use NamedFieldPuns to permit this"]

dupFieldErr :: HsRecFieldContext -> NE.NonEmpty RdrName -> SDoc
dupFieldErr :: HsRecFieldContext -> NonEmpty RdrName -> MsgDoc
dupFieldErr HsRecFieldContext
ctxt NonEmpty RdrName
dups
  = [MsgDoc] -> MsgDoc
hsep [String -> MsgDoc
text String
"duplicate field name",
          MsgDoc -> MsgDoc
quotes (RdrName -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr (NonEmpty RdrName -> RdrName
forall a. NonEmpty a -> a
NE.head NonEmpty RdrName
dups)),
          String -> MsgDoc
text String
"in record", HsRecFieldContext -> MsgDoc
pprRFC HsRecFieldContext
ctxt]

pprRFC :: HsRecFieldContext -> SDoc
pprRFC :: HsRecFieldContext -> MsgDoc
pprRFC (HsRecFieldCon {}) = String -> MsgDoc
text String
"construction"
pprRFC (HsRecFieldPat {}) = String -> MsgDoc
text String
"pattern"
pprRFC (HsRecFieldUpd {}) = String -> MsgDoc
text String
"update"

{-
************************************************************************
*                                                                      *
\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 -> IOEnv (Env TcGblEnv TcLclEnv) ()
rnLit (HsChar XHsChar p
_ Char
c) = Bool -> MsgDoc -> IOEnv (Env TcGblEnv TcLclEnv) ()
checkErr (Char -> Bool
inCharRange Char
c) (Char -> MsgDoc
bogusCharError Char
c)
rnLit HsLit p
_ = () -> IOEnv (Env TcGblEnv TcLclEnv) ()
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.
generalizeOverLitVal :: OverLitVal -> OverLitVal
generalizeOverLitVal :: OverLitVal -> OverLitVal
generalizeOverLitVal (HsFractional (FL {fl_text :: FractionalLit -> SourceText
fl_text=SourceText
src,fl_neg :: FractionalLit -> Bool
fl_neg=Bool
neg,fl_value :: FractionalLit -> Rational
fl_value=Rational
val}))
    | Rational -> Integer
forall a. Ratio a -> a
denominator Rational
val Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1 = IntegralLit -> OverLitVal
HsIntegral (IL :: SourceText -> Bool -> Integer -> IntegralLit
IL { il_text :: SourceText
il_text=SourceText
src
                                            , il_neg :: Bool
il_neg=Bool
neg
                                            , il_value :: Integer
il_value=Rational -> Integer
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 HsOverLit t -> OverLitVal
forall p. HsOverLit p -> OverLitVal
ol_val HsOverLit t
lit of
        HsIntegral IntegralLit
i   -> Integer
0 Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== IntegralLit -> Integer
il_value IntegralLit
i Bool -> Bool -> Bool
&& IntegralLit -> Bool
il_neg IntegralLit
i
        HsFractional FractionalLit
f -> Rational
0 Rational -> Rational -> Bool
forall a. Eq a => a -> a -> Bool
== FractionalLit -> Rational
fl_value FractionalLit
f Bool -> Bool -> Bool
&& FractionalLit -> Bool
fl_neg FractionalLit
f
        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 (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
      FreeVars)
rnOverLit HsOverLit t
origLit
  = do  { Bool
opt_NumDecimals <- Extension -> RnM Bool
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 (HsOverLit t -> OverLitVal
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 Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
/= Name
std_name
              lit' :: HsOverLit (GhcPass 'Renamed)
lit' = HsOverLit t
lit { ol_witness :: HsExpr (GhcPass 'Renamed)
ol_witness = IdP (GhcPass 'Renamed) -> HsExpr (GhcPass 'Renamed)
forall (id :: Pass). IdP (GhcPass id) -> HsExpr (GhcPass id)
nl_HsVar Name
IdP (GhcPass 'Renamed)
from_thing_name
                         , ol_ext :: XOverLit (GhcPass 'Renamed)
ol_ext = Bool
XOverLit (GhcPass 'Renamed)
rebindable }
        ; if HsOverLit (GhcPass 'Renamed) -> Bool
forall t. HsOverLit t -> Bool
isNegativeZeroOverLit HsOverLit (GhcPass 'Renamed)
lit'
          then do { (HsExpr (GhcPass 'Renamed)
negate_name, FreeVars
fvs2) <- Name -> RnM (HsExpr (GhcPass 'Renamed), FreeVars)
lookupSyntaxExpr Name
negateName
                  ; ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
 FreeVars)
-> RnM
     ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
      FreeVars)
forall (m :: * -> *) a. Monad m => a -> m a
return ((HsOverLit (GhcPass 'Renamed)
lit' { ol_val :: OverLitVal
ol_val = OverLitVal -> OverLitVal
negateOverLitVal OverLitVal
val }, HsExpr (GhcPass 'Renamed) -> Maybe (HsExpr (GhcPass 'Renamed))
forall a. a -> Maybe a
Just HsExpr (GhcPass 'Renamed)
negate_name)
                                  , FreeVars
fvs1 FreeVars -> FreeVars -> FreeVars
`plusFV` FreeVars
fvs2) }
          else ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
 FreeVars)
-> RnM
     ((HsOverLit (GhcPass 'Renamed), Maybe (HsExpr (GhcPass 'Renamed))),
      FreeVars)
forall (m :: * -> *) a. Monad m => a -> m a
return ((HsOverLit (GhcPass 'Renamed)
lit', Maybe (HsExpr (GhcPass 'Renamed))
forall a. Maybe a
Nothing), FreeVars
fvs1) }

{-
************************************************************************
*                                                                      *
\subsubsection{Errors}
*                                                                      *
************************************************************************
-}

patSigErr :: Outputable a => a -> SDoc
patSigErr :: forall a. Outputable a => a -> MsgDoc
patSigErr a
ty
  =  (String -> MsgDoc
text String
"Illegal signature in pattern:" MsgDoc -> MsgDoc -> MsgDoc
<+> a -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr a
ty)
        MsgDoc -> MsgDoc -> MsgDoc
$$ Int -> MsgDoc -> MsgDoc
nest Int
4 (String -> MsgDoc
text String
"Use ScopedTypeVariables to permit it")

bogusCharError :: Char -> SDoc
bogusCharError :: Char -> MsgDoc
bogusCharError Char
c
  = String -> MsgDoc
text String
"character literal out of range: '\\" MsgDoc -> MsgDoc -> MsgDoc
<> Char -> MsgDoc
char Char
c  MsgDoc -> MsgDoc -> MsgDoc
<> Char -> MsgDoc
char Char
'\''

badViewPat :: Pat GhcPs -> SDoc
badViewPat :: Pat GhcPs -> MsgDoc
badViewPat Pat GhcPs
pat = [MsgDoc] -> MsgDoc
vcat [String -> MsgDoc
text String
"Illegal view pattern: " MsgDoc -> MsgDoc -> MsgDoc
<+> Pat GhcPs -> MsgDoc
forall a. Outputable a => a -> MsgDoc
ppr Pat GhcPs
pat,
                       String -> MsgDoc
text String
"Use ViewPatterns to enable view patterns"]