{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes        #-}
{-# LANGUAGE TypeFamilies      #-}
{-# LANGUAGE TupleSections     #-}

module GHC.Tc.Gen.Export (rnExports, exports_from_avail, classifyGREs) where

import GHC.Prelude

import GHC.Hs
import GHC.Builtin.Names
import GHC.Tc.Errors.Types
import GHC.Tc.Utils.Monad
import GHC.Tc.Utils.Env
    ( TyThing(AConLike, AnId), tcLookupGlobal, tcLookupTyCon )
import GHC.Tc.Utils.TcType
import GHC.Rename.Doc
import GHC.Rename.Module
import GHC.Rename.Names
import GHC.Rename.Env
import GHC.Rename.Unbound ( reportUnboundName )
import GHC.Utils.Error
import GHC.Unit.Module
import GHC.Unit.Module.Imported
import GHC.Unit.Module.Warnings
import GHC.Core.TyCon
import GHC.Utils.Misc (sndOf3, thdOf3)
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Core.ConLike
import GHC.Core.PatSyn
import GHC.Data.Maybe
import GHC.Data.FastString (fsLit)
import GHC.Driver.Env
import GHC.Driver.DynFlags
import GHC.Parser.PostProcess ( setRdrNameSpace )
import qualified GHC.LanguageExtensions as LangExt

import GHC.Types.Unique.Map
import GHC.Types.SrcLoc as SrcLoc
import GHC.Types.Name
import GHC.Types.Name.Env
import GHC.Types.Name.Set
import GHC.Types.DefaultEnv (ClassDefaults (cd_class), DefaultEnv,
                             emptyDefaultEnv, filterDefaultEnv, isEmptyDefaultEnv)
import GHC.Types.Avail
import GHC.Types.SourceFile
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Types.Name.Reader

import Control.Arrow ( first )
import Control.Monad ( when )
import qualified Data.List.NonEmpty as NE
import Data.Traversable   ( for )
import Data.List ( sortBy )
import qualified Data.Map as Map

{-
************************************************************************
*                                                                      *
\subsection{Export list processing}
*                                                                      *
************************************************************************

Processing the export list.

You might think that we should record things that appear in the export
list as ``occurrences'' (using @addOccurrenceName@), but you'd be
wrong.  We do check (here) that they are in scope, but there is no
need to slurp in their actual declaration (which is what
@addOccurrenceName@ forces).

Indeed, doing so would big trouble when compiling @PrelBase@, because
it re-exports @GHC@, which includes @takeMVar#@, whose type includes
@ConcBase.StateAndSynchVar#@, and so on...

Note [Exports of data families]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Suppose you see (#5306)
        module M where
          import X( F )
          data instance F Int = FInt
What does M export?  AvailTC F [FInt]
                  or AvailTC F [F,FInt]?
The former is strictly right because F isn't defined in this module.
But then you can never do an explicit import of M, thus
    import M( F( FInt ) )
because F isn't exported by M.  Nor can you import FInt alone from here
    import M( FInt )
because we don't have syntax to support that.  (It looks like an import of
the type FInt.)

At one point I implemented a compromise:
  * When constructing exports with no export list, or with module M(
    module M ), we add the parent to the exports as well.
  * But not when you see module M( f ), even if f is a
    class method with a parent.
  * Nor when you see module M( module N ), with N /= M.

But the compromise seemed too much of a hack, so we backed it out.
You just have to use an explicit export list:
    module M( F(..) ) where ...

Note [Avails of associated data families]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Suppose you have (#16077)

    {-# LANGUAGE TypeFamilies #-}
    module A (module A) where

    class    C a  where { data T a }
    instance C () where { data T () = D }

Because @A@ is exported explicitly, GHC tries to produce an export list
from the @GlobalRdrEnv@. In this case, it pulls out the following:

    [ C defined at A.hs:4:1
    , T parent:C defined at A.hs:4:23
    , D parent:T defined at A.hs:5:35 ]

If map these directly into avails, (via 'availFromGRE'), we get
@[C{C;}, C{T;}, T{D;}]@, which eventually gets merged into @[C{C, T;}, T{D;}]@.
That's not right, because @T{D;}@ violates the AvailTC invariant: @T@ is
exported, but it isn't the first entry in the avail!

We work around this issue by expanding GREs where the parent and child
are both type constructors into two GRES.

    T parent:C defined at A.hs:4:23

      =>

    [ T parent:C defined at A.hs:4:23
    , T defined at A.hs:4:23 ]

Then, we get  @[C{C;}, C{T;}, T{T;}, T{D;}]@, which eventually gets merged
into @[C{C, T;}, T{T, D;}]@ (which satisfies the AvailTC invariant).
-}

data ExportAccum        -- The type of the accumulating parameter of
                        -- the main worker function in rnExports
     = ExportAccum {
         ExportAccum -> ExportOccMap
expacc_exp_occs :: ExportOccMap,
           -- ^ Tracks exported occurrence names
         ExportAccum -> UniqMap ModuleName [Name]
expacc_mods :: UniqMap ModuleName [Name],
           -- ^ Tracks (re-)exported module names
           --   and the names they re-export
         ExportAccum -> ExportWarnSpanNames
expacc_warn_spans :: ExportWarnSpanNames,
           -- ^ Information about warnings for names
         ExportAccum -> DontWarnExportNames
expacc_dont_warn :: DontWarnExportNames
           -- ^ What names not to export warnings for
           --   (because they are exported without a warning)
     }


emptyExportAccum :: ExportAccum
emptyExportAccum :: ExportAccum
emptyExportAccum = ExportOccMap
-> UniqMap ModuleName [Name]
-> ExportWarnSpanNames
-> DontWarnExportNames
-> ExportAccum
ExportAccum ExportOccMap
forall a. OccEnv a
emptyOccEnv UniqMap ModuleName [Name]
forall k a. UniqMap k a
emptyUniqMap [] DontWarnExportNames
forall a. NameEnv a
emptyNameEnv

accumExports :: (ExportAccum -> x -> TcRn (ExportAccum, Maybe y))
             -> [x]
             -> TcRn ([y], ExportWarnSpanNames, DontWarnExportNames)
accumExports :: forall x y.
(ExportAccum -> x -> TcRn (ExportAccum, Maybe y))
-> [x] -> TcRn ([y], ExportWarnSpanNames, DontWarnExportNames)
accumExports ExportAccum -> x -> TcRn (ExportAccum, Maybe y)
f [x]
xs = do
  (ExportAccum _ _ export_warn_spans dont_warn_export, ys)
    <- (ExportAccum -> x -> TcRn (ExportAccum, Maybe y))
-> ExportAccum
-> [x]
-> IOEnv (Env TcGblEnv TcLclEnv) (ExportAccum, [Maybe y])
forall (m :: * -> *) (t :: * -> *) acc x y.
(Monad m, Traversable t) =>
(acc -> x -> m (acc, y)) -> acc -> t x -> m (acc, t y)
mapAccumLM ExportAccum -> x -> TcRn (ExportAccum, Maybe y)
f' ExportAccum
emptyExportAccum [x]
xs
  return ( catMaybes ys
         , export_warn_spans
         , dont_warn_export )
  where f' :: ExportAccum -> x -> TcRn (ExportAccum, Maybe y)
f' ExportAccum
acc x
x
          = (ExportAccum, Maybe y)
-> Maybe (ExportAccum, Maybe y) -> (ExportAccum, Maybe y)
forall a. a -> Maybe a -> a
fromMaybe (ExportAccum
acc, Maybe y
forall a. Maybe a
Nothing) (Maybe (ExportAccum, Maybe y) -> (ExportAccum, Maybe y))
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe (ExportAccum, Maybe y))
-> TcRn (ExportAccum, Maybe y)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TcRn (ExportAccum, Maybe y)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe (ExportAccum, Maybe y))
forall r. TcRn r -> TcRn (Maybe r)
attemptM (ExportAccum -> x -> TcRn (ExportAccum, Maybe y)
f ExportAccum
acc x
x)

type ExportOccMap = OccEnv (Name, IE GhcPs)
        -- Tracks what a particular exported OccName
        --   in an export list refers to, and which item
        --   it came from.  It's illegal to export two distinct things
        --   that have the same occurrence name

rnExports :: Bool       -- False => no 'module M(..) where' header at all
          -> Maybe (LocatedLI [LIE GhcPs]) -- Nothing => no explicit export list
          -> RnM TcGblEnv

        -- Complains if two distinct exports have same OccName
        -- Warns about identical exports.
        -- Complains about exports items not in scope

rnExports :: Bool -> Maybe (LocatedLI [LIE GhcPs]) -> RnM TcGblEnv
rnExports Bool
explicit_mod Maybe (LocatedLI [LIE GhcPs])
exports
 = RnM TcGblEnv -> RnM TcGblEnv
forall r. TcM r -> TcM r
checkNoErrs (RnM TcGblEnv -> RnM TcGblEnv) -> RnM TcGblEnv -> RnM TcGblEnv
forall a b. (a -> b) -> a -> b
$   -- Fail if anything in rnExports finds
                   -- an error fails, to avoid error cascade
   do   { hsc_env <- TcRnIf TcGblEnv TcLclEnv HscEnv
forall gbl lcl. TcRnIf gbl lcl HscEnv
getTopEnv
        ; tcg_env <- getGblEnv
        ; let dflags = HscEnv -> DynFlags
hsc_dflags HscEnv
hsc_env
              TcGblEnv { tcg_mod     = this_mod
                       , tcg_rdr_env = rdr_env
                       , tcg_imports = imports
                       , tcg_warns   = warns
                       , tcg_src     = hsc_src } = tcg_env
              default_main | HomeUnitEnv -> Module
mainModIs (HscEnv -> HomeUnitEnv
hsc_HUE HscEnv
hsc_env) Module -> Module -> Bool
forall a. Eq a => a -> a -> Bool
== Module
this_mod
                           , Just String
main_fun <- DynFlags -> Maybe String
mainFunIs DynFlags
dflags
                           = NameSpace -> FastString -> RdrName
mkUnqual NameSpace
varName (String -> FastString
fsLit String
main_fun)
                           | Bool
otherwise
                           = RdrName
main_RDR_Unqual
        ; has_main <- (not . null) <$> lookupInfoOccRn default_main -- #17832

        -- If a module has no explicit header, and it has one or more main
        -- functions in scope, then add a header like
        -- "module Main(main) where ..."                               #13839
        -- See Note [Modules without a module header]
        ; let real_exports
                 | Bool
explicit_mod = Maybe (LocatedLI [LIE GhcPs])
exports
                 | Bool
has_main
                          = GenLocated SrcSpanAnnLI [GenLocated SrcSpanAnnA (IE GhcPs)]
-> Maybe
     (GenLocated SrcSpanAnnLI [GenLocated SrcSpanAnnA (IE GhcPs)])
forall a. a -> Maybe a
Just ([GenLocated SrcSpanAnnA (IE GhcPs)]
-> GenLocated SrcSpanAnnLI [GenLocated SrcSpanAnnA (IE GhcPs)]
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA [IE GhcPs -> GenLocated SrcSpanAnnA (IE GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (XIEVar GhcPs
-> LIEWrappedName GhcPs -> Maybe (ExportDoc GhcPs) -> IE GhcPs
forall pass.
XIEVar pass
-> LIEWrappedName pass -> Maybe (ExportDoc pass) -> IE pass
IEVar Maybe (GenLocated SrcSpanAnnP (WarningTxt GhcPs))
XIEVar GhcPs
forall a. Maybe a
Nothing
                                     (IEWrappedName GhcPs -> GenLocated SrcSpanAnnA (IEWrappedName GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (XIEName GhcPs -> LIdP GhcPs -> IEWrappedName GhcPs
forall p. XIEName p -> LIdP p -> IEWrappedName p
IEName XIEName GhcPs
NoExtField
noExtField (LIdP GhcPs -> IEWrappedName GhcPs)
-> LIdP GhcPs -> IEWrappedName GhcPs
forall a b. (a -> b) -> a -> b
$ RdrName -> GenLocated SrcSpanAnnN RdrName
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA RdrName
default_main)) Maybe (ExportDoc GhcPs)
forall a. Maybe a
Nothing)])
                        -- ToDo: the 'noLoc' here is unhelpful if 'main'
                        --       turns out to be out of scope
                 | Bool
otherwise = Maybe (LocatedLI [LIE GhcPs])
Maybe (GenLocated SrcSpanAnnLI [GenLocated SrcSpanAnnA (IE GhcPs)])
forall a. Maybe a
Nothing

        -- Rename the export list
        ; let do_it = Maybe (LocatedLI [LIE GhcPs])
-> GlobalRdrEnv
-> ImportAvails
-> Module
-> RnM
     (Maybe [(LIE GhcRn, DefaultEnv, [AvailInfo])], [AvailInfo],
      ExportWarnNames GhcRn)
exports_from_avail Maybe (LocatedLI [LIE GhcPs])
real_exports GlobalRdrEnv
rdr_env ImportAvails
imports Module
this_mod
        ; (rn_exports, final_avails, new_export_warns)
            <- if hsc_src == HsigFile
                then do (mb_r, msgs) <- tryTc do_it
                        case mb_r of
                            Just (Maybe
   [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])],
 [AvailInfo], ExportWarnNames GhcRn)
r  -> (Maybe
   [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])],
 [AvailInfo], ExportWarnNames GhcRn)
-> TcRn
     (Maybe
        [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])],
      [AvailInfo], ExportWarnNames GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe
   [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])],
 [AvailInfo], ExportWarnNames GhcRn)
r
                            Maybe
  (Maybe
     [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])],
   [AvailInfo], ExportWarnNames GhcRn)
Nothing -> Messages TcRnMessage -> TcRn ()
addMessages Messages TcRnMessage
msgs TcRn ()
-> TcRn
     (Maybe
        [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])],
      [AvailInfo], ExportWarnNames GhcRn)
-> TcRn
     (Maybe
        [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])],
      [AvailInfo], ExportWarnNames GhcRn)
forall a b.
IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> TcRn
  (Maybe
     [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])],
   [AvailInfo], ExportWarnNames GhcRn)
forall env a. IOEnv env a
failM
                else checkNoErrs do_it

        -- Final processing
        ; let final_ns = [AvailInfo] -> NameSet
availsToNameSet [AvailInfo]
final_avails
              drop_defaults (a
spans, b
_defaults, b
avails) = (a
spans, b
avails)

        ; traceRn "rnExports: Exports:" (ppr final_avails)

        ; return (tcg_env { tcg_exports    = final_avails
                          , tcg_rn_exports = case tcg_rn_exports tcg_env of
                                                Maybe [(LIE GhcRn, [AvailInfo])]
Nothing -> Maybe [(LIE GhcRn, [AvailInfo])]
Maybe [(GenLocated SrcSpanAnnA (IE GhcRn), [AvailInfo])]
forall a. Maybe a
Nothing
                                                Just [(LIE GhcRn, [AvailInfo])]
_  -> ((GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])
 -> (GenLocated SrcSpanAnnA (IE GhcRn), [AvailInfo]))
-> [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])]
-> [(GenLocated SrcSpanAnnA (IE GhcRn), [AvailInfo])]
forall a b. (a -> b) -> [a] -> [b]
map (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])
-> (GenLocated SrcSpanAnnA (IE GhcRn), [AvailInfo])
forall {a} {b} {b}. (a, b, b) -> (a, b)
drop_defaults ([(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])]
 -> [(GenLocated SrcSpanAnnA (IE GhcRn), [AvailInfo])])
-> Maybe
     [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])]
-> Maybe [(GenLocated SrcSpanAnnA (IE GhcRn), [AvailInfo])]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe
  [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])]
rn_exports
                          , tcg_default_exports = case exports of
                              Maybe (LocatedLI [LIE GhcPs])
Nothing -> DefaultEnv
emptyDefaultEnv
                              Maybe (LocatedLI [LIE GhcPs])
_ -> ([(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])]
 -> DefaultEnv)
-> Maybe
     [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])]
-> DefaultEnv
forall m a. Monoid m => (a -> m) -> Maybe a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (((GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])
 -> DefaultEnv)
-> [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])]
-> DefaultEnv
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])
-> DefaultEnv
forall a b c. (a, b, c) -> b
sndOf3) Maybe
  [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])]
rn_exports
                          , tcg_dus = tcg_dus tcg_env `plusDU`
                                      usesOnly final_ns
                          , tcg_warns = insertWarnExports
                                        warns new_export_warns}) }

-- | List of names and the information about their warnings
--   (warning, export list item span)
type ExportWarnSpanNames = [(Name, WarningTxt GhcRn, SrcSpan)]

-- | Map from names that should not have export warnings to
--   the spans of export list items that are missing those warnings
type DontWarnExportNames = NameEnv (NE.NonEmpty SrcSpan)


{- Note [Default exports]
~~~~~~~~~~~~~~~~~~~~~~~~~
Named default declarations (see Note [Named default declarations] in
GHC.Tc.Gen.Default) can be exported. A named default declaration is
exported only when it's specified in the export list, using the `default`
keyword and the class name.  For example:

    module TextWrap (Text, default IsString) where
      import Data.String (IsString)
      import Data.Text (Text)
      default IsString (Text, String)

A module with no explicit export list does not export any default
declarations, and neither does the re-export of a whole imported module.

The export item `default IsString` is parsed into the `IE` item

    IEThingAbs ext (L loc (IEDefault ext "IsString")) doc

If exported, a default is imported automatically much like a class instance. For
example,

    import TextWrap ()

would import the above `default IsString (Text, String)` declaration into the
importing module.

The `cd_module` field of `ClassDefaults` tracks the module whence the default was
imported from, for the purpose of warning reports. The said warning report may be
triggered by `-Wtype-defaults` or by a user-defined `WARNING` pragma attached to
the default export. In the latter case the warning text is stored in the
`cd_warn` field. See test `testsuite/tests/default/ExportWarn.hs` for an example
of a user-defined warning on default.
-}

exports_from_avail :: Maybe (LocatedLI [LIE GhcPs])
                         -- ^ 'Nothing' means no explicit export list
                   -> GlobalRdrEnv
                   -> ImportAvails
                         -- ^ Imported modules; this is used to test if a
                         -- @module Foo@ export is valid (it's not valid
                         -- if we didn't import @Foo@!)
                   -> Module
                   -> RnM (Maybe [(LIE GhcRn, DefaultEnv, Avails)], Avails, ExportWarnNames GhcRn)
                         -- (Nothing, _, _) <=> no explicit export list
                         -- if explicit export list is present it contains
                         -- each renamed export item together with its exported
                         -- names.

exports_from_avail :: Maybe (LocatedLI [LIE GhcPs])
-> GlobalRdrEnv
-> ImportAvails
-> Module
-> RnM
     (Maybe [(LIE GhcRn, DefaultEnv, [AvailInfo])], [AvailInfo],
      ExportWarnNames GhcRn)
exports_from_avail Maybe (LocatedLI [LIE GhcPs])
Nothing GlobalRdrEnv
rdr_env ImportAvails
_imports Module
_this_mod
   -- The same as (module M) where M is the current module name,
   -- so that's how we handle it, except we also export the data family
   -- when a data instance is exported.
  = do {
    ; TcRnMessage -> TcRn ()
addDiagnostic
        (ModuleName -> TcRnMessage
TcRnMissingExportList (ModuleName -> TcRnMessage) -> ModuleName -> TcRnMessage
forall a b. (a -> b) -> a -> b
$ Module -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName Module
_this_mod)
    ; let avails :: [AvailInfo]
avails =
            (AvailInfo -> AvailInfo) -> [AvailInfo] -> [AvailInfo]
forall a b. (a -> b) -> [a] -> [b]
map AvailInfo -> AvailInfo
fix_faminst ([AvailInfo] -> [AvailInfo])
-> (GlobalRdrEnv -> [AvailInfo]) -> GlobalRdrEnv -> [AvailInfo]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [GlobalRdrEltX GREInfo] -> [AvailInfo]
forall info. [GlobalRdrEltX info] -> [AvailInfo]
gresToAvailInfo
              ([GlobalRdrEltX GREInfo] -> [AvailInfo])
-> (GlobalRdrEnv -> [GlobalRdrEltX GREInfo])
-> GlobalRdrEnv
-> [AvailInfo]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GlobalRdrEltX GREInfo -> Bool)
-> [GlobalRdrEltX GREInfo] -> [GlobalRdrEltX GREInfo]
forall a. (a -> Bool) -> [a] -> [a]
filter GlobalRdrEltX GREInfo -> Bool
forall info. GlobalRdrEltX info -> Bool
isLocalGRE ([GlobalRdrEltX GREInfo] -> [GlobalRdrEltX GREInfo])
-> (GlobalRdrEnv -> [GlobalRdrEltX GREInfo])
-> GlobalRdrEnv
-> [GlobalRdrEltX GREInfo]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GlobalRdrEnv -> [GlobalRdrEltX GREInfo]
forall info. GlobalRdrEnvX info -> [GlobalRdrEltX info]
globalRdrEnvElts (GlobalRdrEnv -> [AvailInfo]) -> GlobalRdrEnv -> [AvailInfo]
forall a b. (a -> b) -> a -> b
$ GlobalRdrEnv
rdr_env
    ; (Maybe
   [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])],
 [AvailInfo], ExportWarnNames GhcRn)
-> TcRn
     (Maybe
        [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])],
      [AvailInfo], ExportWarnNames GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe
  [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])]
forall a. Maybe a
Nothing, [AvailInfo]
avails, []) }
  where
    -- #11164: when we define a data instance
    -- but not data family, re-export the family
    -- Even though we don't check whether this is actually a data family
    -- only data families can locally define subordinate things (`ns` here)
    -- without locally defining (and instead importing) the parent (`n`)
    fix_faminst :: AvailInfo -> AvailInfo
fix_faminst avail :: AvailInfo
avail@(AvailTC Name
n [Name]
ns)
      | AvailInfo -> Bool
availExportsDecl AvailInfo
avail
      = AvailInfo
avail
      | Bool
otherwise
      = Name -> [Name] -> AvailInfo
AvailTC Name
n (Name
nName -> [Name] -> [Name]
forall a. a -> [a] -> [a]
:[Name]
ns)
    fix_faminst AvailInfo
avail = AvailInfo
avail


exports_from_avail (Just (L SrcSpanAnnLI
_ [LIE GhcPs]
rdr_items)) GlobalRdrEnv
rdr_env ImportAvails
imports Module
this_mod
  = do (ie_avails, export_warn_spans, dont_warn_export)
         <- (ExportAccum
 -> GenLocated SrcSpanAnnA (IE GhcPs)
 -> TcRn
      (ExportAccum,
       Maybe
         (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])))
-> [GenLocated SrcSpanAnnA (IE GhcPs)]
-> TcRn
     ([(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])],
      ExportWarnSpanNames, DontWarnExportNames)
forall x y.
(ExportAccum -> x -> TcRn (ExportAccum, Maybe y))
-> [x] -> TcRn ([y], ExportWarnSpanNames, DontWarnExportNames)
accumExports ExportAccum
-> LIE GhcPs
-> RnM (ExportAccum, Maybe (LIE GhcRn, DefaultEnv, [AvailInfo]))
ExportAccum
-> GenLocated SrcSpanAnnA (IE GhcPs)
-> TcRn
     (ExportAccum,
      Maybe (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo]))
do_litem [LIE GhcPs]
[GenLocated SrcSpanAnnA (IE GhcPs)]
rdr_items
       let final_exports = [AvailInfo] -> [AvailInfo]
nubAvails (((GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])
 -> [AvailInfo])
-> [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])]
-> [AvailInfo]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])
-> [AvailInfo]
forall a b c. (a, b, c) -> c
thdOf3 [(GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])]
ie_avails) -- Combine families
       export_warn_names <- aggregate_warnings export_warn_spans dont_warn_export
       return (Just ie_avails, final_exports, export_warn_names)
  where
    do_litem :: ExportAccum -> LIE GhcPs
             -> RnM (ExportAccum, Maybe (LIE GhcRn, DefaultEnv, Avails))
    do_litem :: ExportAccum
-> LIE GhcPs
-> RnM (ExportAccum, Maybe (LIE GhcRn, DefaultEnv, [AvailInfo]))
do_litem ExportAccum
acc LIE GhcPs
lie = SrcSpan
-> TcRn
     (ExportAccum,
      Maybe (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo]))
-> TcRn
     (ExportAccum,
      Maybe (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo]))
forall a. SrcSpan -> TcRn a -> TcRn a
setSrcSpan (GenLocated SrcSpanAnnA (IE GhcPs) -> SrcSpan
forall a e. HasLoc a => GenLocated a e -> SrcSpan
getLocA LIE GhcPs
GenLocated SrcSpanAnnA (IE GhcPs)
lie) (ExportAccum
-> LIE GhcPs
-> RnM (ExportAccum, Maybe (LIE GhcRn, DefaultEnv, [AvailInfo]))
exports_from_item ExportAccum
acc LIE GhcPs
lie)

    -- Maps a parent to its in-scope children
    kids_env :: NameEnv [GlobalRdrElt]
    kids_env :: NameEnv [GlobalRdrEltX GREInfo]
kids_env = [GlobalRdrEltX GREInfo] -> NameEnv [GlobalRdrEltX GREInfo]
mkChildEnv (GlobalRdrEnv -> [GlobalRdrEltX GREInfo]
forall info. GlobalRdrEnvX info -> [GlobalRdrEltX info]
globalRdrEnvElts GlobalRdrEnv
rdr_env)

    -- See Note [Avails of associated data families]
    expand_tyty_gre :: GlobalRdrElt -> [GlobalRdrElt]
    expand_tyty_gre :: GlobalRdrEltX GREInfo -> [GlobalRdrEltX GREInfo]
expand_tyty_gre (gre :: GlobalRdrEltX GREInfo
gre@GRE { gre_par :: forall info. GlobalRdrEltX info -> Parent
gre_par = ParentIs Name
p })
      | Name -> Bool
isTyConName Name
p
      , Name -> Bool
isTyConName (GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName GlobalRdrEltX GREInfo
gre)
      = [GlobalRdrEltX GREInfo
gre, GlobalRdrEltX GREInfo
gre{ gre_par = NoParent }]
    expand_tyty_gre GlobalRdrEltX GREInfo
gre
      = [GlobalRdrEltX GREInfo
gre]

    imported_modules :: [ModuleName]
imported_modules = [ ImportedModsVal -> ModuleName
imv_name ImportedModsVal
imv
                       | [ImportedBy]
xs <- Map Module [ImportedBy] -> [[ImportedBy]]
forall k a. Map k a -> [a]
Map.elems (Map Module [ImportedBy] -> [[ImportedBy]])
-> Map Module [ImportedBy] -> [[ImportedBy]]
forall a b. (a -> b) -> a -> b
$ ImportAvails -> Map Module [ImportedBy]
imp_mods ImportAvails
imports
                       , ImportedModsVal
imv <- [ImportedBy] -> [ImportedModsVal]
importedByUser [ImportedBy]
xs ]

    exports_from_item :: ExportAccum -> LIE GhcPs
                      -> RnM (ExportAccum, Maybe (LIE GhcRn, DefaultEnv, Avails))
    exports_from_item :: ExportAccum
-> LIE GhcPs
-> RnM (ExportAccum, Maybe (LIE GhcRn, DefaultEnv, [AvailInfo]))
exports_from_item expacc :: ExportAccum
expacc@ExportAccum{
                        expacc_exp_occs :: ExportAccum -> ExportOccMap
expacc_exp_occs   = ExportOccMap
occs,
                        expacc_mods :: ExportAccum -> UniqMap ModuleName [Name]
expacc_mods       = UniqMap ModuleName [Name]
earlier_mods,
                        expacc_warn_spans :: ExportAccum -> ExportWarnSpanNames
expacc_warn_spans = ExportWarnSpanNames
export_warn_spans,
                        expacc_dont_warn :: ExportAccum -> DontWarnExportNames
expacc_dont_warn  = DontWarnExportNames
dont_warn_export
                      } (L SrcSpanAnnA
loc ie :: IE GhcPs
ie@(IEModuleContents (Maybe (GenLocated SrcSpanAnnP (WarningTxt GhcPs))
warn_txt_ps, EpToken "module"
_) lmod :: XRec GhcPs ModuleName
lmod@(L SrcSpanAnnA
_ ModuleName
mod)))
      | Just [Name]
exported_names <- UniqMap ModuleName [Name] -> ModuleName -> Maybe [Name]
forall k a. Uniquable k => UniqMap k a -> k -> Maybe a
lookupUniqMap UniqMap ModuleName [Name]
earlier_mods ModuleName
mod  -- Duplicate export of M
      = do { TcRnMessage -> TcRn ()
addDiagnostic (ModuleName -> TcRnMessage
TcRnDupeModuleExport ModuleName
mod)
           ; (export_warn_spans', dont_warn_export', _) <-
                ExportWarnSpanNames
-> DontWarnExportNames
-> [Name]
-> Maybe (LWarningTxt GhcPs)
-> SrcSpan
-> RnM
     (ExportWarnSpanNames, DontWarnExportNames,
      Maybe (LWarningTxt GhcRn))
process_warning ExportWarnSpanNames
export_warn_spans
                                DontWarnExportNames
dont_warn_export
                                [Name]
exported_names
                                Maybe (LWarningTxt GhcPs)
Maybe (GenLocated SrcSpanAnnP (WarningTxt GhcPs))
warn_txt_ps
                                (SrcSpanAnnA -> SrcSpan
forall a. HasLoc a => a -> SrcSpan
locA SrcSpanAnnA
loc)
                   -- Checks if all the names are exported with the same warning message
                   -- or if they should not be warned about
           ; return ( expacc{ expacc_warn_spans = export_warn_spans'
                            , expacc_dont_warn  = dont_warn_export' }
                    , Nothing ) }

      | Bool
otherwise
      = do { let { exportValid :: Bool
exportValid    = (ModuleName
mod ModuleName -> [ModuleName] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [ModuleName]
imported_modules)
                                  Bool -> Bool -> Bool
|| (Module -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName Module
this_mod ModuleName -> ModuleName -> Bool
forall a. Eq a => a -> a -> Bool
== ModuleName
mod)
                 ; gre_prs :: [(GlobalRdrEltX GREInfo, GlobalRdrEltX GREInfo)]
gre_prs        = ModuleName
-> [GlobalRdrEltX GREInfo]
-> [(GlobalRdrEltX GREInfo, GlobalRdrEltX GREInfo)]
forall info.
ModuleName
-> [GlobalRdrEltX info]
-> [(GlobalRdrEltX info, GlobalRdrEltX info)]
pickGREsModExp ModuleName
mod (GlobalRdrEnv -> [GlobalRdrEltX GREInfo]
forall info. GlobalRdrEnvX info -> [GlobalRdrEltX info]
globalRdrEnvElts GlobalRdrEnv
rdr_env)
                 ; new_gres :: [GlobalRdrEltX GREInfo]
new_gres       = [ GlobalRdrEltX GREInfo
gre'
                                    | (GlobalRdrEltX GREInfo
gre, GlobalRdrEltX GREInfo
_) <- [(GlobalRdrEltX GREInfo, GlobalRdrEltX GREInfo)]
gre_prs
                                    , GlobalRdrEltX GREInfo
gre' <- GlobalRdrEltX GREInfo -> [GlobalRdrEltX GREInfo]
expand_tyty_gre GlobalRdrEltX GREInfo
gre ]
                 ; new_exports :: [AvailInfo]
new_exports    = (GlobalRdrEltX GREInfo -> AvailInfo)
-> [GlobalRdrEltX GREInfo] -> [AvailInfo]
forall a b. (a -> b) -> [a] -> [b]
map GlobalRdrEltX GREInfo -> AvailInfo
forall info. GlobalRdrEltX info -> AvailInfo
availFromGRE [GlobalRdrEltX GREInfo]
new_gres
                 ; all_gres :: [GlobalRdrEltX GREInfo]
all_gres       = ((GlobalRdrEltX GREInfo, GlobalRdrEltX GREInfo)
 -> [GlobalRdrEltX GREInfo] -> [GlobalRdrEltX GREInfo])
-> [GlobalRdrEltX GREInfo]
-> [(GlobalRdrEltX GREInfo, GlobalRdrEltX GREInfo)]
-> [GlobalRdrEltX GREInfo]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\(GlobalRdrEltX GREInfo
gre1,GlobalRdrEltX GREInfo
gre2) [GlobalRdrEltX GREInfo]
gres -> GlobalRdrEltX GREInfo
gre1 GlobalRdrEltX GREInfo
-> [GlobalRdrEltX GREInfo] -> [GlobalRdrEltX GREInfo]
forall a. a -> [a] -> [a]
: GlobalRdrEltX GREInfo
gre2 GlobalRdrEltX GREInfo
-> [GlobalRdrEltX GREInfo] -> [GlobalRdrEltX GREInfo]
forall a. a -> [a] -> [a]
: [GlobalRdrEltX GREInfo]
gres) [] [(GlobalRdrEltX GREInfo, GlobalRdrEltX GREInfo)]
gre_prs
                 ; exported_names :: [Name]
exported_names = (GlobalRdrEltX GREInfo -> Name)
-> [GlobalRdrEltX GREInfo] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName [GlobalRdrEltX GREInfo]
new_gres
                 ; mods :: UniqMap ModuleName [Name]
mods           = UniqMap ModuleName [Name]
-> ModuleName -> [Name] -> UniqMap ModuleName [Name]
forall k a. Uniquable k => UniqMap k a -> k -> a -> UniqMap k a
addToUniqMap UniqMap ModuleName [Name]
earlier_mods ModuleName
mod [Name]
exported_names
                 }

            ; Bool -> TcRnMessage -> TcRn ()
checkErr Bool
exportValid (ModuleName -> TcRnMessage
TcRnExportedModNotImported ModuleName
mod)
            ; Bool -> TcRnMessage -> TcRn ()
warnIf (Bool
exportValid Bool -> Bool -> Bool
&& [(GlobalRdrEltX GREInfo, GlobalRdrEltX GREInfo)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(GlobalRdrEltX GREInfo, GlobalRdrEltX GREInfo)]
gre_prs) (ModuleName -> TcRnMessage
TcRnNullExportedModule ModuleName
mod)

            ; String -> SDoc -> TcRn ()
traceRn String
"efa" (ModuleName -> SDoc
forall a. Outputable a => a -> SDoc
ppr ModuleName
mod SDoc -> SDoc -> SDoc
forall doc. IsDoc doc => doc -> doc -> doc
$$ [GlobalRdrEltX GREInfo] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [GlobalRdrEltX GREInfo]
all_gres)
            ; DeprecationWarnings -> [GlobalRdrEltX GREInfo] -> TcRn ()
addUsedGREs DeprecationWarnings
ExportDeprecationWarnings [GlobalRdrEltX GREInfo]
all_gres

            ; occs' <- ExportOccMap
-> IE GhcPs -> [GlobalRdrEltX GREInfo] -> RnM ExportOccMap
check_occs ExportOccMap
occs IE GhcPs
ie [GlobalRdrEltX GREInfo]
new_gres
                          -- This check_occs not only finds conflicts
                          -- between this item and others, but also
                          -- internally within this item.  That is, if
                          -- 'M.x' is in scope in several ways, we'll have
                          -- several members of mod_avails with the same
                          -- OccName.
            ; (export_warn_spans', dont_warn_export', warn_txt_rn) <-
                process_warning export_warn_spans
                                dont_warn_export
                                exported_names
                                warn_txt_ps
                                (locA loc)

            ; traceRn "export_mod"
                      (vcat [ ppr mod
                            , ppr new_exports ])
            ; return ( ExportAccum { expacc_exp_occs   = occs'
                                   , expacc_mods       = mods
                                   , expacc_warn_spans = export_warn_spans'
                                   , expacc_dont_warn  = dont_warn_export' }
                     , Just (L loc (IEModuleContents warn_txt_rn lmod), emptyDefaultEnv, new_exports) ) }

    exports_from_item ExportAccum
acc LIE GhcPs
lie = do
        m_doc_ie <- LIE GhcPs -> RnM (Maybe (LIE GhcRn))
lookup_doc_ie LIE GhcPs
lie
        case m_doc_ie of
          Just GenLocated SrcSpanAnnA (IE GhcRn)
new_ie -> (ExportAccum,
 Maybe (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo]))
-> TcRn
     (ExportAccum,
      Maybe (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo]))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ExportAccum
acc, (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])
-> Maybe
     (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])
forall a. a -> Maybe a
Just (GenLocated SrcSpanAnnA (IE GhcRn)
new_ie, DefaultEnv
emptyDefaultEnv, []))
          Maybe (GenLocated SrcSpanAnnA (IE GhcRn))
Nothing -> do
            m_ie <- ExportAccum
-> LIE GhcPs
-> RnM (Maybe (ExportAccum, LIE GhcRn, Either OccName AvailInfo))
lookup_ie ExportAccum
acc LIE GhcPs
lie
            case m_ie of
              Maybe
  (ExportAccum, GenLocated SrcSpanAnnA (IE GhcRn),
   Either OccName AvailInfo)
Nothing -> (ExportAccum,
 Maybe (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo]))
-> TcRn
     (ExportAccum,
      Maybe (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo]))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ExportAccum
acc, Maybe (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])
forall a. Maybe a
Nothing)
              Just (ExportAccum
acc', GenLocated SrcSpanAnnA (IE GhcRn)
new_ie, Left OccName
cls) -> do
                defaults <- TcGblEnv -> DefaultEnv
tcg_default (TcGblEnv -> DefaultEnv)
-> RnM TcGblEnv -> IOEnv (Env TcGblEnv TcLclEnv) DefaultEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RnM TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
                let exported_default = (ClassDefaults -> Bool) -> DefaultEnv -> DefaultEnv
filterDefaultEnv ((OccName
cls OccName -> OccName -> Bool
forall a. Eq a => a -> a -> Bool
==) (OccName -> Bool)
-> (ClassDefaults -> OccName) -> ClassDefaults -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> OccName
nameOccName (Name -> OccName)
-> (ClassDefaults -> Name) -> ClassDefaults -> OccName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyCon -> Name
tyConName (TyCon -> Name)
-> (ClassDefaults -> TyCon) -> ClassDefaults -> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ClassDefaults -> TyCon
cd_class) DefaultEnv
defaults
                return (acc', Just (new_ie, exported_default, []))
              Just (ExportAccum
acc', GenLocated SrcSpanAnnA (IE GhcRn)
new_ie, Right AvailInfo
avail)
                -> (ExportAccum,
 Maybe (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo]))
-> TcRn
     (ExportAccum,
      Maybe (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo]))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ExportAccum
acc', (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])
-> Maybe
     (GenLocated SrcSpanAnnA (IE GhcRn), DefaultEnv, [AvailInfo])
forall a. a -> Maybe a
Just (GenLocated SrcSpanAnnA (IE GhcRn)
new_ie, DefaultEnv
emptyDefaultEnv, [AvailInfo
avail]))

    -------------
    lookup_ie :: ExportAccum -> LIE GhcPs -> RnM (Maybe (ExportAccum, LIE GhcRn, Either OccName AvailInfo))
    lookup_ie :: ExportAccum
-> LIE GhcPs
-> RnM (Maybe (ExportAccum, LIE GhcRn, Either OccName AvailInfo))
lookup_ie expacc :: ExportAccum
expacc@ExportAccum{
            expacc_exp_occs :: ExportAccum -> ExportOccMap
expacc_exp_occs   = ExportOccMap
occs,
            expacc_warn_spans :: ExportAccum -> ExportWarnSpanNames
expacc_warn_spans = ExportWarnSpanNames
export_warn_spans,
            expacc_dont_warn :: ExportAccum -> DontWarnExportNames
expacc_dont_warn  = DontWarnExportNames
dont_warn_export
          } (L SrcSpanAnnA
loc ie :: IE GhcPs
ie@(IEVar XIEVar GhcPs
warn_txt_ps LIEWrappedName GhcPs
l Maybe (ExportDoc GhcPs)
doc))
        = do mb_gre <- RdrName -> RnM (Maybe (GlobalRdrEltX GREInfo))
lookupGreAvailRn (RdrName -> RnM (Maybe (GlobalRdrEltX GREInfo)))
-> RdrName -> RnM (Maybe (GlobalRdrEltX GREInfo))
forall a b. (a -> b) -> a -> b
$ LIEWrappedName GhcPs -> IdP GhcPs
forall (p :: Pass). LIEWrappedName (GhcPass p) -> IdP (GhcPass p)
lieWrappedName LIEWrappedName GhcPs
l
             for mb_gre $ \ GlobalRdrEltX GREInfo
gre -> do
               let avail :: AvailInfo
avail = GlobalRdrEltX GREInfo -> AvailInfo
forall info. GlobalRdrEltX info -> AvailInfo
availFromGRE GlobalRdrEltX GREInfo
gre
                   name :: Name
name = GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName GlobalRdrEltX GREInfo
gre

               occs' <- ExportOccMap
-> IE GhcPs -> [GlobalRdrEltX GREInfo] -> RnM ExportOccMap
check_occs ExportOccMap
occs IE GhcPs
ie [GlobalRdrEltX GREInfo
gre]
               (export_warn_spans', dont_warn_export', warn_txt_rn)
                 <- process_warning export_warn_spans
                                    dont_warn_export
                                    [name]
                                    warn_txt_ps
                                    (locA loc)

               doc' <- traverse rnLHsDoc doc
               return ( expacc{ expacc_exp_occs   = occs'
                              , expacc_warn_spans = export_warn_spans'
                              , expacc_dont_warn  = dont_warn_export' }
                      , L loc (IEVar warn_txt_rn (replaceLWrappedName l name) doc')
                      , Right avail )

    lookup_ie expacc :: ExportAccum
expacc@ExportAccum{
            expacc_exp_occs :: ExportAccum -> ExportOccMap
expacc_exp_occs   = ExportOccMap
occs,
            expacc_warn_spans :: ExportAccum -> ExportWarnSpanNames
expacc_warn_spans = ExportWarnSpanNames
export_warn_spans,
            expacc_dont_warn :: ExportAccum -> DontWarnExportNames
expacc_dont_warn  = DontWarnExportNames
dont_warn_export
          } (L SrcSpanAnnA
loc ie :: IE GhcPs
ie@(IEThingAbs XIEThingAbs GhcPs
warn_txt_ps LIEWrappedName GhcPs
l Maybe (ExportDoc GhcPs)
doc))
        = do mb_gre <- RdrName -> RnM (Maybe (GlobalRdrEltX GREInfo))
lookupGreAvailRn (RdrName -> RnM (Maybe (GlobalRdrEltX GREInfo)))
-> RdrName -> RnM (Maybe (GlobalRdrEltX GREInfo))
forall a b. (a -> b) -> a -> b
$ LIEWrappedName GhcPs -> IdP GhcPs
forall (p :: Pass). LIEWrappedName (GhcPass p) -> IdP (GhcPass p)
lieWrappedName LIEWrappedName GhcPs
l
             for mb_gre $ \ GlobalRdrEltX GREInfo
gre -> do
               let avail :: AvailInfo
avail = GlobalRdrEltX GREInfo -> AvailInfo
forall info. GlobalRdrEltX info -> AvailInfo
availFromGRE GlobalRdrEltX GREInfo
gre
                   name :: Name
name = GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName GlobalRdrEltX GREInfo
gre

               occs' <- ExportOccMap
-> IE GhcPs -> [GlobalRdrEltX GREInfo] -> RnM ExportOccMap
check_occs ExportOccMap
occs IE GhcPs
ie [GlobalRdrEltX GREInfo
gre]
               (export_warn_spans', dont_warn_export', warn_txt_rn)
                 <- process_warning export_warn_spans
                                    dont_warn_export
                                    [name]
                                    warn_txt_ps
                                    (locA loc)

               doc' <- traverse rnLHsDoc doc
               avail' <- case unLoc l of
                 -- see Note [Default exports]
                 IEDefault XIEDefault GhcPs
_ LIdP GhcPs
cls -> do
                   let defaultOccName :: ClassDefaults -> OccName
defaultOccName = Name -> OccName
nameOccName (Name -> OccName)
-> (ClassDefaults -> Name) -> ClassDefaults -> OccName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyCon -> Name
tyConName (TyCon -> Name)
-> (ClassDefaults -> TyCon) -> ClassDefaults -> Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ClassDefaults -> TyCon
cd_class
                       occName :: OccName
occName = RdrName -> OccName
rdrNameOcc (GenLocated SrcSpanAnnN RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc LIdP GhcPs
GenLocated SrcSpanAnnN RdrName
cls)
                   defaults <- TcGblEnv -> DefaultEnv
tcg_default (TcGblEnv -> DefaultEnv)
-> RnM TcGblEnv -> IOEnv (Env TcGblEnv TcLclEnv) DefaultEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RnM TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
                   when (isEmptyDefaultEnv $ filterDefaultEnv ((occName ==) . defaultOccName) defaults)
                        (addErr $ TcRnExportHiddenDefault ie)
                   pure (Left occName)
                 IEWrappedName GhcPs
_ -> Either OccName AvailInfo
-> IOEnv (Env TcGblEnv TcLclEnv) (Either OccName AvailInfo)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AvailInfo -> Either OccName AvailInfo
forall a b. b -> Either a b
Right AvailInfo
avail)
               return ( expacc{ expacc_exp_occs   = occs'
                              , expacc_warn_spans = export_warn_spans'
                              , expacc_dont_warn  = dont_warn_export' }
                      , L loc (IEThingAbs warn_txt_rn (replaceLWrappedName l name) doc')
                      , avail' )

    lookup_ie expacc :: ExportAccum
expacc@ExportAccum{
            expacc_exp_occs :: ExportAccum -> ExportOccMap
expacc_exp_occs   = ExportOccMap
occs,
            expacc_warn_spans :: ExportAccum -> ExportWarnSpanNames
expacc_warn_spans = ExportWarnSpanNames
export_warn_spans,
            expacc_dont_warn :: ExportAccum -> DontWarnExportNames
expacc_dont_warn  = DontWarnExportNames
dont_warn_export
          } (L SrcSpanAnnA
loc ie :: IE GhcPs
ie@(IEThingAll (Maybe (GenLocated SrcSpanAnnP (WarningTxt GhcPs))
warn_txt_ps, (EpToken "(", EpToken "..", EpToken ")")
ann) LIEWrappedName GhcPs
l Maybe (ExportDoc GhcPs)
doc))
        = do mb_gre <- RdrName -> RnM (Maybe (GlobalRdrEltX GREInfo))
lookupGreAvailRn (RdrName -> RnM (Maybe (GlobalRdrEltX GREInfo)))
-> RdrName -> RnM (Maybe (GlobalRdrEltX GREInfo))
forall a b. (a -> b) -> a -> b
$ LIEWrappedName GhcPs -> IdP GhcPs
forall (p :: Pass). LIEWrappedName (GhcPass p) -> IdP (GhcPass p)
lieWrappedName LIEWrappedName GhcPs
l
             for mb_gre $ \ GlobalRdrEltX GREInfo
par -> do
               all_kids <- IE GhcPs
-> LIEWrappedName GhcPs
-> GlobalRdrEltX GREInfo
-> RnM [GlobalRdrEltX GREInfo]
lookup_ie_kids_all IE GhcPs
ie LIEWrappedName GhcPs
l GlobalRdrEltX GREInfo
par
               let name = GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName GlobalRdrEltX GREInfo
par
                   all_gres = GlobalRdrEltX GREInfo
par GlobalRdrEltX GREInfo
-> [GlobalRdrEltX GREInfo] -> [GlobalRdrEltX GREInfo]
forall a. a -> [a] -> [a]
: [GlobalRdrEltX GREInfo]
all_kids
                   all_names = (GlobalRdrEltX GREInfo -> Name)
-> [GlobalRdrEltX GREInfo] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName [GlobalRdrEltX GREInfo]
all_gres

               occs' <- check_occs occs ie all_gres
               (export_warn_spans', dont_warn_export', warn_txt_rn)
                 <- process_warning export_warn_spans
                                    dont_warn_export
                                    all_names
                                    warn_txt_ps
                                    (locA loc)

               doc' <- traverse rnLHsDoc doc
               return ( expacc{ expacc_exp_occs   = occs'
                              , expacc_warn_spans = export_warn_spans'
                              , expacc_dont_warn  = dont_warn_export' }
                      , L loc (IEThingAll (warn_txt_rn, ann) (replaceLWrappedName l name) doc')
                      , Right (AvailTC name all_names) )

    lookup_ie expacc :: ExportAccum
expacc@ExportAccum{
            expacc_exp_occs :: ExportAccum -> ExportOccMap
expacc_exp_occs   = ExportOccMap
occs,
            expacc_warn_spans :: ExportAccum -> ExportWarnSpanNames
expacc_warn_spans = ExportWarnSpanNames
export_warn_spans,
            expacc_dont_warn :: ExportAccum -> DontWarnExportNames
expacc_dont_warn  = DontWarnExportNames
dont_warn_export
          } (L SrcSpanAnnA
loc ie :: IE GhcPs
ie@(IEThingWith (Maybe (GenLocated SrcSpanAnnP (WarningTxt GhcPs))
warn_txt_ps, IEThingWithAnns
ann) LIEWrappedName GhcPs
l IEWildcard
wc [LIEWrappedName GhcPs]
sub_rdrs Maybe (ExportDoc GhcPs)
doc))
        = do mb_gre <- IE GhcPs
-> RnM (Maybe (GlobalRdrEltX GREInfo))
-> RnM (Maybe (GlobalRdrEltX GREInfo))
forall (p :: Pass) a.
OutputableBndrId p =>
IE (GhcPass p) -> TcM a -> TcM a
addExportErrCtxt IE GhcPs
ie
                     (RnM (Maybe (GlobalRdrEltX GREInfo))
 -> RnM (Maybe (GlobalRdrEltX GREInfo)))
-> RnM (Maybe (GlobalRdrEltX GREInfo))
-> RnM (Maybe (GlobalRdrEltX GREInfo))
forall a b. (a -> b) -> a -> b
$ RdrName -> RnM (Maybe (GlobalRdrEltX GREInfo))
lookupGreAvailRn (RdrName -> RnM (Maybe (GlobalRdrEltX GREInfo)))
-> RdrName -> RnM (Maybe (GlobalRdrEltX GREInfo))
forall a b. (a -> b) -> a -> b
$ LIEWrappedName GhcPs -> IdP GhcPs
forall (p :: Pass). LIEWrappedName (GhcPass p) -> IdP (GhcPass p)
lieWrappedName LIEWrappedName GhcPs
l
             for mb_gre $ \ GlobalRdrEltX GREInfo
par -> do
               (subs, with_kids)
                 <- IE GhcPs
-> TcM ([LIEWrappedName GhcRn], [GlobalRdrEltX GREInfo])
-> TcM ([LIEWrappedName GhcRn], [GlobalRdrEltX GREInfo])
forall (p :: Pass) a.
OutputableBndrId p =>
IE (GhcPass p) -> TcM a -> TcM a
addExportErrCtxt IE GhcPs
ie
                  (TcM ([LIEWrappedName GhcRn], [GlobalRdrEltX GREInfo])
 -> TcM ([LIEWrappedName GhcRn], [GlobalRdrEltX GREInfo]))
-> TcM ([LIEWrappedName GhcRn], [GlobalRdrEltX GREInfo])
-> TcM ([LIEWrappedName GhcRn], [GlobalRdrEltX GREInfo])
forall a b. (a -> b) -> a -> b
$ GlobalRdrEltX GREInfo
-> [LIEWrappedName GhcPs]
-> TcM ([LIEWrappedName GhcRn], [GlobalRdrEltX GREInfo])
lookup_ie_kids_with GlobalRdrEltX GREInfo
par [LIEWrappedName GhcPs]
sub_rdrs

               wc_kids <-
                 case wc of
                   IEWildcard
NoIEWildcard -> [GlobalRdrEltX GREInfo] -> RnM [GlobalRdrEltX GREInfo]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return []
                   IEWildcard Int
_ -> IE GhcPs
-> LIEWrappedName GhcPs
-> GlobalRdrEltX GREInfo
-> RnM [GlobalRdrEltX GREInfo]
lookup_ie_kids_all IE GhcPs
ie LIEWrappedName GhcPs
l GlobalRdrEltX GREInfo
par

               let name = GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName GlobalRdrEltX GREInfo
par
                   all_kids = [GlobalRdrEltX GREInfo]
with_kids [GlobalRdrEltX GREInfo]
-> [GlobalRdrEltX GREInfo] -> [GlobalRdrEltX GREInfo]
forall a. [a] -> [a] -> [a]
++ [GlobalRdrEltX GREInfo]
wc_kids
                   all_gres = GlobalRdrEltX GREInfo
par GlobalRdrEltX GREInfo
-> [GlobalRdrEltX GREInfo] -> [GlobalRdrEltX GREInfo]
forall a. a -> [a] -> [a]
: [GlobalRdrEltX GREInfo]
all_kids
                   all_names = (GlobalRdrEltX GREInfo -> Name)
-> [GlobalRdrEltX GREInfo] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName [GlobalRdrEltX GREInfo]
all_gres

               occs' <- check_occs occs ie all_gres
               (export_warn_spans', dont_warn_export', warn_txt_rn)
                 <- process_warning export_warn_spans
                                    dont_warn_export
                                    all_names
                                    warn_txt_ps
                                    (locA loc)

               doc' <- traverse rnLHsDoc doc
               return ( expacc{ expacc_exp_occs   = occs'
                              , expacc_warn_spans = export_warn_spans'
                              , expacc_dont_warn  = dont_warn_export' }
                      , L loc (IEThingWith (warn_txt_rn, ann) (replaceLWrappedName l name) wc subs doc')
                      , Right (AvailTC name all_names) )

    lookup_ie ExportAccum
_ LIE GhcPs
_ = String
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (Maybe
        (ExportAccum, GenLocated SrcSpanAnnA (IE GhcRn),
         Either OccName AvailInfo))
forall a. HasCallStack => String -> a
panic String
"lookup_ie"    -- Other cases covered earlier


    lookup_ie_kids_with :: GlobalRdrElt -> [LIEWrappedName GhcPs]
                   -> RnM ([LIEWrappedName GhcRn], [GlobalRdrElt])
    lookup_ie_kids_with :: GlobalRdrEltX GREInfo
-> [LIEWrappedName GhcPs]
-> TcM ([LIEWrappedName GhcRn], [GlobalRdrEltX GREInfo])
lookup_ie_kids_with GlobalRdrEltX GREInfo
gre [LIEWrappedName GhcPs]
sub_rdrs =
      do { let name :: Name
name = GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName GlobalRdrEltX GREInfo
gre
         ; kids <- Name
-> [LIEWrappedName GhcPs]
-> RnM [(LIEWrappedName GhcRn, GlobalRdrEltX GREInfo)]
lookupChildrenExport Name
name [LIEWrappedName GhcPs]
sub_rdrs
         ; return (map fst kids, map snd kids) }

    lookup_ie_kids_all :: IE GhcPs -> LIEWrappedName GhcPs -> GlobalRdrElt
                  -> RnM [GlobalRdrElt]
    lookup_ie_kids_all :: IE GhcPs
-> LIEWrappedName GhcPs
-> GlobalRdrEltX GREInfo
-> RnM [GlobalRdrEltX GREInfo]
lookup_ie_kids_all IE GhcPs
ie (L SrcSpanAnnA
_ IEWrappedName GhcPs
rdr) GlobalRdrEltX GREInfo
gre =
      do { let name :: Name
name = GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName GlobalRdrEltX GREInfo
gre
               gres :: [GlobalRdrEltX GREInfo]
gres = NameEnv [GlobalRdrEltX GREInfo] -> Name -> [GlobalRdrEltX GREInfo]
forall a. NameEnv [a] -> Name -> [a]
findChildren NameEnv [GlobalRdrEltX GREInfo]
kids_env Name
name
         ; RdrName -> [GlobalRdrEltX GREInfo] -> TcRn ()
addUsedKids (IEWrappedName GhcPs -> IdP GhcPs
forall (p :: Pass). IEWrappedName (GhcPass p) -> IdP (GhcPass p)
ieWrappedName IEWrappedName GhcPs
rdr) [GlobalRdrEltX GREInfo]
gres
         ; Bool -> TcRn () -> TcRn ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([GlobalRdrEltX GREInfo] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [GlobalRdrEltX GREInfo]
gres) (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$
            if Name -> Bool
isTyConName Name
name
            then TcRnMessage -> TcRn ()
addTcRnDiagnostic (GlobalRdrEltX GREInfo -> TcRnMessage
TcRnDodgyExports GlobalRdrEltX GREInfo
gre)
            else -- This occurs when you export T(..), but
                 -- only import T abstractly, or T is a synonym.
                 TcRnMessage -> TcRn ()
addErr (IE GhcPs -> TcRnMessage
TcRnExportHiddenComponents IE GhcPs
ie)
         ; [GlobalRdrEltX GREInfo] -> RnM [GlobalRdrEltX GREInfo]
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return [GlobalRdrEltX GREInfo]
gres }

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

    -- Runs for every Name
    -- - If there is no new warning, flags that the old warning should not be
    --     included (since a warning should only be emitted if all
    --     of the export statements have a warning)
    -- - If the Name already has a warning, adds it
    process_warning :: ExportWarnSpanNames       -- Old aggregate data about warnins
                    -> DontWarnExportNames       -- Old names not to warn about
                    -> [Name]                              -- Names to warn about
                    -> Maybe (LWarningTxt GhcPs) -- Warning
                    -> SrcSpan                             -- Span of the export list item
                    -> RnM (ExportWarnSpanNames, -- Aggregate data about the warnings
                            DontWarnExportNames, -- Names not to warn about in the end
                                                 -- (when there was a non-warned export)
                            Maybe (LWarningTxt GhcRn)) -- Renamed warning
    process_warning :: ExportWarnSpanNames
-> DontWarnExportNames
-> [Name]
-> Maybe (LWarningTxt GhcPs)
-> SrcSpan
-> RnM
     (ExportWarnSpanNames, DontWarnExportNames,
      Maybe (LWarningTxt GhcRn))
process_warning ExportWarnSpanNames
export_warn_spans
                    DontWarnExportNames
dont_warn_export
                    [Name]
names Maybe (LWarningTxt GhcPs)
Nothing SrcSpan
loc
      = (ExportWarnSpanNames, DontWarnExportNames,
 Maybe (GenLocated SrcSpanAnnP (WarningTxt GhcRn)))
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     (ExportWarnSpanNames, DontWarnExportNames,
      Maybe (GenLocated SrcSpanAnnP (WarningTxt GhcRn)))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ( ExportWarnSpanNames
export_warn_spans
               , (Name -> DontWarnExportNames -> DontWarnExportNames)
-> DontWarnExportNames -> [Name] -> DontWarnExportNames
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Name -> DontWarnExportNames -> DontWarnExportNames
update_dont_warn_export
                       DontWarnExportNames
dont_warn_export [Name]
names
               , Maybe (GenLocated SrcSpanAnnP (WarningTxt GhcRn))
forall a. Maybe a
Nothing )
      where
        update_dont_warn_export :: Name -> DontWarnExportNames -> DontWarnExportNames
        update_dont_warn_export :: Name -> DontWarnExportNames -> DontWarnExportNames
update_dont_warn_export Name
name DontWarnExportNames
dont_warn_export'
          = (SrcSpan -> NonEmpty SrcSpan -> NonEmpty SrcSpan)
-> (SrcSpan -> NonEmpty SrcSpan)
-> DontWarnExportNames
-> Name
-> SrcSpan
-> DontWarnExportNames
forall a b.
(a -> b -> b) -> (a -> b) -> NameEnv b -> Name -> a -> NameEnv b
extendNameEnv_Acc SrcSpan -> NonEmpty SrcSpan -> NonEmpty SrcSpan
forall a. a -> NonEmpty a -> NonEmpty a
(NE.<|)
                              SrcSpan -> NonEmpty SrcSpan
forall a. a -> NonEmpty a
NE.singleton
                              DontWarnExportNames
dont_warn_export'
                              Name
name
                              SrcSpan
loc

    process_warning ExportWarnSpanNames
export_warn_spans
                    DontWarnExportNames
dont_warn_export
                    [Name]
names (Just LWarningTxt GhcPs
warn_txt_ps) SrcSpan
loc
      = do
          warn_txt_rn <- LWarningTxt GhcPs -> RnM (LWarningTxt GhcRn)
rnLWarningTxt LWarningTxt GhcPs
warn_txt_ps
          let new_export_warn_spans = (Name -> (Name, WarningTxt GhcRn, SrcSpan))
-> [Name] -> ExportWarnSpanNames
forall a b. (a -> b) -> [a] -> [b]
map (, GenLocated SrcSpanAnnP (WarningTxt GhcRn) -> WarningTxt GhcRn
forall l e. GenLocated l e -> e
unLoc GenLocated SrcSpanAnnP (WarningTxt GhcRn)
warn_txt_rn, SrcSpan
loc) [Name]
names
          return ( new_export_warn_spans ++ export_warn_spans
                 , dont_warn_export
                 , Just warn_txt_rn )

    -- For each name exported with any warnings throws an error
    --   if there are any exports of that name with a different warning
    aggregate_warnings :: ExportWarnSpanNames
                       -> DontWarnExportNames
                       -> RnM (ExportWarnNames GhcRn)
    aggregate_warnings :: ExportWarnSpanNames
-> DontWarnExportNames -> RnM (ExportWarnNames GhcRn)
aggregate_warnings ExportWarnSpanNames
export_warn_spans DontWarnExportNames
dont_warn_export
      = ([Maybe (Name, WarningTxt GhcRn)] -> ExportWarnNames GhcRn)
-> IOEnv (Env TcGblEnv TcLclEnv) [Maybe (Name, WarningTxt GhcRn)]
-> RnM (ExportWarnNames GhcRn)
forall a b.
(a -> b)
-> IOEnv (Env TcGblEnv TcLclEnv) a
-> IOEnv (Env TcGblEnv TcLclEnv) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Maybe (Name, WarningTxt GhcRn)] -> ExportWarnNames GhcRn
forall a. [Maybe a] -> [a]
catMaybes
      (IOEnv (Env TcGblEnv TcLclEnv) [Maybe (Name, WarningTxt GhcRn)]
 -> RnM (ExportWarnNames GhcRn))
-> IOEnv (Env TcGblEnv TcLclEnv) [Maybe (Name, WarningTxt GhcRn)]
-> RnM (ExportWarnNames GhcRn)
forall a b. (a -> b) -> a -> b
$ (NonEmpty (Name, WarningTxt GhcRn, SrcSpan)
 -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe (Name, WarningTxt GhcRn)))
-> [NonEmpty (Name, WarningTxt GhcRn, SrcSpan)]
-> IOEnv (Env TcGblEnv TcLclEnv) [Maybe (Name, WarningTxt GhcRn)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((Name, NonEmpty (WarningTxt GhcRn, SrcSpan))
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe (Name, WarningTxt GhcRn))
aggregate_single ((Name, NonEmpty (WarningTxt GhcRn, SrcSpan))
 -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe (Name, WarningTxt GhcRn)))
-> (NonEmpty (Name, WarningTxt GhcRn, SrcSpan)
    -> (Name, NonEmpty (WarningTxt GhcRn, SrcSpan)))
-> NonEmpty (Name, WarningTxt GhcRn, SrcSpan)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe (Name, WarningTxt GhcRn))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (Name, WarningTxt GhcRn, SrcSpan)
-> (Name, NonEmpty (WarningTxt GhcRn, SrcSpan))
extract_name)
      ([NonEmpty (Name, WarningTxt GhcRn, SrcSpan)]
 -> IOEnv (Env TcGblEnv TcLclEnv) [Maybe (Name, WarningTxt GhcRn)])
-> [NonEmpty (Name, WarningTxt GhcRn, SrcSpan)]
-> IOEnv (Env TcGblEnv TcLclEnv) [Maybe (Name, WarningTxt GhcRn)]
forall a b. (a -> b) -> a -> b
$ ((Name, WarningTxt GhcRn, SrcSpan)
 -> (Name, WarningTxt GhcRn, SrcSpan) -> Bool)
-> ExportWarnSpanNames
-> [NonEmpty (Name, WarningTxt GhcRn, SrcSpan)]
forall (f :: * -> *) a.
Foldable f =>
(a -> a -> Bool) -> f a -> [NonEmpty a]
NE.groupBy (\(Name
n1, WarningTxt GhcRn
_, SrcSpan
_) (Name
n2, WarningTxt GhcRn
_, SrcSpan
_) -> Name
n1 Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
n2)
      (ExportWarnSpanNames
 -> [NonEmpty (Name, WarningTxt GhcRn, SrcSpan)])
-> ExportWarnSpanNames
-> [NonEmpty (Name, WarningTxt GhcRn, SrcSpan)]
forall a b. (a -> b) -> a -> b
$ ((Name, WarningTxt GhcRn, SrcSpan)
 -> (Name, WarningTxt GhcRn, SrcSpan) -> Ordering)
-> ExportWarnSpanNames -> ExportWarnSpanNames
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (\(Name
n1, WarningTxt GhcRn
_, SrcSpan
_) (Name
n2, WarningTxt GhcRn
_, SrcSpan
_) -> Name
n1 Name -> Name -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Name
n2) ExportWarnSpanNames
export_warn_spans
      where
        extract_name :: NE.NonEmpty (Name, WarningTxt GhcRn, SrcSpan)
                     -> (Name, NE.NonEmpty (WarningTxt GhcRn, SrcSpan))
        extract_name :: NonEmpty (Name, WarningTxt GhcRn, SrcSpan)
-> (Name, NonEmpty (WarningTxt GhcRn, SrcSpan))
extract_name l :: NonEmpty (Name, WarningTxt GhcRn, SrcSpan)
l@((Name
name, WarningTxt GhcRn
_, SrcSpan
_) NE.:| ExportWarnSpanNames
_)
          = (Name
name, ((Name, WarningTxt GhcRn, SrcSpan) -> (WarningTxt GhcRn, SrcSpan))
-> NonEmpty (Name, WarningTxt GhcRn, SrcSpan)
-> NonEmpty (WarningTxt GhcRn, SrcSpan)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
NE.map (\(Name
_, WarningTxt GhcRn
warn_txt, SrcSpan
span) -> (WarningTxt GhcRn
warn_txt, SrcSpan
span)) NonEmpty (Name, WarningTxt GhcRn, SrcSpan)
l)

        aggregate_single :: (Name, NE.NonEmpty (WarningTxt GhcRn, SrcSpan))
                         -> RnM (Maybe (Name, WarningTxt GhcRn))
        aggregate_single :: (Name, NonEmpty (WarningTxt GhcRn, SrcSpan))
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe (Name, WarningTxt GhcRn))
aggregate_single (Name
name, (WarningTxt GhcRn
warn_txt_rn, SrcSpan
loc) NE.:| [(WarningTxt GhcRn, SrcSpan)]
warn_spans)
          = do
              -- Emit an error if the warnings differ
              case [SrcSpan] -> Maybe (NonEmpty SrcSpan)
forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty [SrcSpan]
spans_different of
                Maybe (NonEmpty SrcSpan)
Nothing -> () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                Just NonEmpty SrcSpan
spans_different
                  -> SrcSpan -> TcRnMessage -> TcRn ()
addErrAt SrcSpan
loc (Name -> NonEmpty SrcSpan -> TcRnMessage
TcRnDifferentExportWarnings Name
name NonEmpty SrcSpan
spans_different)
              -- Emit a warning if some export list items do not have a warning
              case DontWarnExportNames -> Name -> Maybe (NonEmpty SrcSpan)
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv DontWarnExportNames
dont_warn_export Name
name of
                Maybe (NonEmpty SrcSpan)
Nothing -> Maybe (Name, WarningTxt GhcRn)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe (Name, WarningTxt GhcRn))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Name, WarningTxt GhcRn)
 -> IOEnv (Env TcGblEnv TcLclEnv) (Maybe (Name, WarningTxt GhcRn)))
-> Maybe (Name, WarningTxt GhcRn)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe (Name, WarningTxt GhcRn))
forall a b. (a -> b) -> a -> b
$ (Name, WarningTxt GhcRn) -> Maybe (Name, WarningTxt GhcRn)
forall a. a -> Maybe a
Just (Name
name, WarningTxt GhcRn
warn_txt_rn)
                Just NonEmpty SrcSpan
not_warned_spans -> do
                  SrcSpan -> TcRnMessage -> TcRn ()
addDiagnosticAt SrcSpan
loc (Name -> NonEmpty SrcSpan -> TcRnMessage
TcRnIncompleteExportWarnings Name
name NonEmpty SrcSpan
not_warned_spans)
                  Maybe (Name, WarningTxt GhcRn)
-> IOEnv (Env TcGblEnv TcLclEnv) (Maybe (Name, WarningTxt GhcRn))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Name, WarningTxt GhcRn)
forall a. Maybe a
Nothing
          where
            spans_different :: [SrcSpan]
spans_different = ((WarningTxt GhcRn, SrcSpan) -> SrcSpan)
-> [(WarningTxt GhcRn, SrcSpan)] -> [SrcSpan]
forall a b. (a -> b) -> [a] -> [b]
map (WarningTxt GhcRn, SrcSpan) -> SrcSpan
forall a b. (a, b) -> b
snd ([(WarningTxt GhcRn, SrcSpan)] -> [SrcSpan])
-> [(WarningTxt GhcRn, SrcSpan)] -> [SrcSpan]
forall a b. (a -> b) -> a -> b
$ ((WarningTxt GhcRn, SrcSpan) -> Bool)
-> [(WarningTxt GhcRn, SrcSpan)] -> [(WarningTxt GhcRn, SrcSpan)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> ((WarningTxt GhcRn, SrcSpan) -> Bool)
-> (WarningTxt GhcRn, SrcSpan)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WarningTxt GhcRn -> WarningTxt GhcRn -> Bool
forall p1 p2. WarningTxt p1 -> WarningTxt p2 -> Bool
warningTxtSame WarningTxt GhcRn
warn_txt_rn (WarningTxt GhcRn -> Bool)
-> ((WarningTxt GhcRn, SrcSpan) -> WarningTxt GhcRn)
-> (WarningTxt GhcRn, SrcSpan)
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (WarningTxt GhcRn, SrcSpan) -> WarningTxt GhcRn
forall a b. (a, b) -> a
fst) [(WarningTxt GhcRn, SrcSpan)]
warn_spans

    -------------
    lookup_doc_ie :: LIE GhcPs -> RnM (Maybe (LIE GhcRn))
    lookup_doc_ie :: LIE GhcPs -> RnM (Maybe (LIE GhcRn))
lookup_doc_ie (L SrcSpanAnnA
loc (IEGroup XIEGroup GhcPs
_ Int
lev ExportDoc GhcPs
doc)) = do
      doc' <- ExportDoc GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) (LHsDoc GhcRn)
rnLHsDoc ExportDoc GhcPs
doc
      pure $ Just (L loc (IEGroup noExtField lev doc'))
    lookup_doc_ie (L SrcSpanAnnA
loc (IEDoc XIEDoc GhcPs
_ ExportDoc GhcPs
doc))       = do
      doc' <- ExportDoc GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) (LHsDoc GhcRn)
rnLHsDoc ExportDoc GhcPs
doc
      pure $ Just (L loc (IEDoc noExtField doc'))
    lookup_doc_ie (L SrcSpanAnnA
loc (IEDocNamed XIEDocNamed GhcPs
_ String
str))
      = Maybe (LIE GhcRn) -> RnM (Maybe (LIE GhcRn))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (LIE GhcRn) -> RnM (Maybe (LIE GhcRn)))
-> Maybe (LIE GhcRn) -> RnM (Maybe (LIE GhcRn))
forall a b. (a -> b) -> a -> b
$ LIE GhcRn -> Maybe (LIE GhcRn)
forall a. a -> Maybe a
Just (SrcSpanAnnA -> IE GhcRn -> GenLocated SrcSpanAnnA (IE GhcRn)
forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc (XIEDocNamed GhcRn -> String -> IE GhcRn
forall pass. XIEDocNamed pass -> String -> IE pass
IEDocNamed XIEDocNamed GhcRn
NoExtField
noExtField String
str))
    lookup_doc_ie LIE GhcPs
_ = Maybe (GenLocated SrcSpanAnnA (IE GhcRn))
-> IOEnv
     (Env TcGblEnv TcLclEnv) (Maybe (GenLocated SrcSpanAnnA (IE GhcRn)))
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (GenLocated SrcSpanAnnA (IE GhcRn))
forall a. Maybe a
Nothing

    -- In an export item M.T(A,B,C), we want to treat the uses of
    -- A,B,C as if they were M.A, M.B, M.C
    -- Happily pickGREs does just the right thing
    addUsedKids :: RdrName -> [GlobalRdrElt] -> RnM ()
    addUsedKids :: RdrName -> [GlobalRdrEltX GREInfo] -> TcRn ()
addUsedKids RdrName
parent_rdr [GlobalRdrEltX GREInfo]
kid_gres
      = DeprecationWarnings -> [GlobalRdrEltX GREInfo] -> TcRn ()
addUsedGREs DeprecationWarnings
ExportDeprecationWarnings (RdrName -> [GlobalRdrEltX GREInfo] -> [GlobalRdrEltX GREInfo]
forall info.
RdrName -> [GlobalRdrEltX info] -> [GlobalRdrEltX info]
pickGREs RdrName
parent_rdr [GlobalRdrEltX GREInfo]
kid_gres)

-- Renaming and typechecking of exports happens after everything else has
-- been typechecked.

{-
Note [Modules without a module header]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The Haskell 2010 report says in section 5.1:

>> An abbreviated form of module, consisting only of the module body, is
>> permitted. If this is used, the header is assumed to be
>> ‘module Main(main) where’.

For modules without a module header, this is implemented the
following way:

If the module has a main function in scope:
   Then create a module header and export the main function,
   as if a module header like ‘module Main(main) where...’ would exist.
   This has the effect to mark the main function and all top level
   functions called directly or indirectly via main as 'used',
   and later on, unused top-level functions can be reported correctly.
   There is no distinction between GHC and GHCi.
If the module has several main functions in scope:
   Then generate a header as above. The ambiguity is reported later in
   module  `GHC.Tc.Module` function `check_main`.
If the module has NO main function:
   Then export all top-level functions. This marks all top level
   functions as 'used'.
   In GHCi this has the effect, that we don't get any 'non-used' warnings.
   In GHC, however, the 'has-main-module' check in GHC.Tc.Module.checkMain
   fires, and we get the error:
      The IO action ‘main’ is not defined in module ‘Main’
-}


-- Renaming exports lists is a minefield. Five different things can appear in
-- children export lists ( T(A, B, C) ).
-- 1. Record selectors
-- 2. Type constructors
-- 3. Data constructors
-- 4. Pattern Synonyms
-- 5. Pattern Synonym Selectors
--
-- However, things get put into weird name spaces.
-- 1. Some type constructors are parsed as variables (-.->) for example.
-- 2. All data constructors are parsed as type constructors
-- 3. When there is ambiguity, we default type constructors to data
-- constructors and require the explicit `type` keyword for type
-- constructors.
--
-- This function first establishes the possible namespaces that an
-- identifier might be in (`choosePossibleNameSpaces`).
--
-- Then for each namespace in turn, tries to find the correct identifier
-- there returning the first positive result or the first terminating
-- error.
--



lookupChildrenExport :: Name -> [LIEWrappedName GhcPs]
                     -> RnM ([(LIEWrappedName GhcRn, GlobalRdrElt)])
lookupChildrenExport :: Name
-> [LIEWrappedName GhcPs]
-> RnM [(LIEWrappedName GhcRn, GlobalRdrEltX GREInfo)]
lookupChildrenExport Name
spec_parent [LIEWrappedName GhcPs]
rdr_items = (GenLocated SrcSpanAnnA (IEWrappedName GhcPs)
 -> TcRn
      (GenLocated SrcSpanAnnA (IEWrappedName GhcRn),
       GlobalRdrEltX GREInfo))
-> [GenLocated SrcSpanAnnA (IEWrappedName GhcPs)]
-> IOEnv
     (Env TcGblEnv TcLclEnv)
     [(GenLocated SrcSpanAnnA (IEWrappedName GhcRn),
       GlobalRdrEltX GREInfo)]
forall a b. (a -> TcRn b) -> [a] -> TcRn [b]
mapAndReportM LIEWrappedName GhcPs
-> RnM (LIEWrappedName GhcRn, GlobalRdrEltX GREInfo)
GenLocated SrcSpanAnnA (IEWrappedName GhcPs)
-> TcRn
     (GenLocated SrcSpanAnnA (IEWrappedName GhcRn),
      GlobalRdrEltX GREInfo)
doOne [LIEWrappedName GhcPs]
[GenLocated SrcSpanAnnA (IEWrappedName GhcPs)]
rdr_items
    where
        -- Process an individual child
        doOne :: LIEWrappedName GhcPs
              -> RnM (LIEWrappedName GhcRn, GlobalRdrElt)
        doOne :: LIEWrappedName GhcPs
-> RnM (LIEWrappedName GhcRn, GlobalRdrEltX GREInfo)
doOne LIEWrappedName GhcPs
n = do

          let bareName :: RdrName
bareName = (IEWrappedName GhcPs -> IdP GhcPs
IEWrappedName GhcPs -> RdrName
forall (p :: Pass). IEWrappedName (GhcPass p) -> IdP (GhcPass p)
ieWrappedName (IEWrappedName GhcPs -> RdrName)
-> (GenLocated SrcSpanAnnA (IEWrappedName GhcPs)
    -> IEWrappedName GhcPs)
-> GenLocated SrcSpanAnnA (IEWrappedName GhcPs)
-> RdrName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenLocated SrcSpanAnnA (IEWrappedName GhcPs) -> IEWrappedName GhcPs
forall l e. GenLocated l e -> e
unLoc) LIEWrappedName GhcPs
GenLocated SrcSpanAnnA (IEWrappedName GhcPs)
n
              what_lkup :: LookupChild
              what_lkup :: LookupChild
what_lkup =
                LookupChild
                  { wantedParent :: Name
wantedParent       = Name
spec_parent
                  , lookupDataConFirst :: Bool
lookupDataConFirst = Bool
True
                  , prioritiseParent :: Bool
prioritiseParent   = Bool
False -- See T11970.
                  }

                -- Do not report export list declaration deprecations
          name <-  Bool
-> DeprecationWarnings
-> Name
-> RdrName
-> LookupChild
-> RnM ChildLookupResult
lookupSubBndrOcc_helper Bool
False DeprecationWarnings
ExportDeprecationWarnings
                        Name
spec_parent RdrName
bareName LookupChild
what_lkup
          traceRn "lookupChildrenExport" (ppr name)
          -- Default to data constructors for slightly better error
          -- messages
          let unboundName :: RdrName
              unboundName = if RdrName -> NameSpace
rdrNameSpace RdrName
bareName NameSpace -> NameSpace -> Bool
forall a. Eq a => a -> a -> Bool
== NameSpace
varName
                            then RdrName
bareName
                            else RdrName -> NameSpace -> RdrName
setRdrNameSpace RdrName
bareName NameSpace
dataName

          case name of
            ChildLookupResult
NameNotFound ->
              do { ub <- RdrName -> RnM Name
reportUnboundName RdrName
unboundName
                 ; let l = GenLocated SrcSpanAnnA (IEWrappedName GhcPs) -> SrcSpanAnnA
forall l e. GenLocated l e -> l
getLoc LIEWrappedName GhcPs
GenLocated SrcSpanAnnA (IEWrappedName GhcPs)
n
                       gre = GREInfo -> Parent -> Name -> GlobalRdrEltX GREInfo
mkLocalGRE GREInfo
UnboundGRE Parent
NoParent Name
ub
                 ; return (L l (IEName noExtField (L (l2l l) ub)), gre)}
            FoundChild child :: GlobalRdrEltX GREInfo
child@(GRE { gre_name :: forall info. GlobalRdrEltX info -> Name
gre_name = Name
child_nm, gre_par :: forall info. GlobalRdrEltX info -> Parent
gre_par = Parent
par }) ->
              do { Name -> Parent -> Name -> TcRn ()
checkPatSynParent Name
spec_parent Parent
par Name
child_nm
                 ; (GenLocated SrcSpanAnnA (IEWrappedName GhcRn),
 GlobalRdrEltX GREInfo)
-> TcRn
     (GenLocated SrcSpanAnnA (IEWrappedName GhcRn),
      GlobalRdrEltX GREInfo)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return (LIEWrappedName GhcPs -> IdP GhcRn -> LIEWrappedName GhcRn
replaceLWrappedName LIEWrappedName GhcPs
n IdP GhcRn
Name
child_nm, GlobalRdrEltX GREInfo
child)
                 }
            IncorrectParent Name
p GlobalRdrEltX GREInfo
c [Name]
gs -> Name
-> Name
-> [Name]
-> TcRn
     (GenLocated SrcSpanAnnA (IEWrappedName GhcRn),
      GlobalRdrEltX GREInfo)
forall a. Name -> Name -> [Name] -> TcM a
failWithDcErr Name
p (GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName GlobalRdrEltX GREInfo
c) [Name]
gs


-- Note [Typing Pattern Synonym Exports]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- It proved quite a challenge to precisely specify which pattern synonyms
-- should be allowed to be bundled with which type constructors.
-- In the end it was decided to be quite liberal in what we allow. Below is
-- how Simon described the implementation.
--
-- "Personally I think we should Keep It Simple.  All this talk of
--  satisfiability makes me shiver.  I suggest this: allow T( P ) in all
--   situations except where `P`'s type is ''visibly incompatible'' with
--   `T`.
--
--    What does "visibly incompatible" mean?  `P` is visibly incompatible
--    with
--     `T` if
--       * `P`'s type is of form `... -> S t1 t2`
--       * `S` is a data/newtype constructor distinct from `T`
--
--  Nothing harmful happens if we allow `P` to be exported with
--  a type it can't possibly be useful for, but specifying a tighter
--  relationship is very awkward as you have discovered."
--
-- Note that this allows *any* pattern synonym to be bundled with any
-- datatype type constructor. For example, the following pattern `P` can be
-- bundled with any type.
--
-- ```
-- pattern P :: (A ~ f) => f
-- ```
--
-- So we provide basic type checking in order to help the user out, most
-- pattern synonyms are defined with definite type constructors, but don't
-- actually prevent a library author completely confusing their users if
-- they want to.
--
-- So, we check for exactly four things
-- 1. The name arises from a pattern synonym definition. (Either a pattern
--    synonym constructor or a pattern synonym selector)
-- 2. The pattern synonym is only bundled with a datatype or newtype.
-- 3. Check that the head of the result type constructor is an actual type
--    constructor and not a type variable. (See above example)
-- 4. Is so, check that this type constructor is the same as the parent
--    type constructor.
--
--
-- Note [Types of TyCon]
-- ~~~~~~~~~~~~~~~~~~~~~
-- This check appears to be overly complicated, Richard asked why it
-- is not simply just `isAlgTyCon`. The answer for this is that
-- a classTyCon is also an `AlgTyCon` which we explicitly want to disallow.
-- (It is either a newtype or data depending on the number of methods)
--

-- | Given a resolved name in the children export list and a parent. Decide
-- whether we are allowed to export the child with the parent.
-- Invariant: gre_par == NoParent
-- See Note [Typing Pattern Synonym Exports]
checkPatSynParent :: Name    -- ^ Alleged parent type constructor
                             -- User wrote T( P, Q )
                  -> Parent  -- The parent of P we discovered
                  -> Name
                       -- ^ Either a
                       --   a) Pattern Synonym Constructor
                       --   b) A pattern synonym selector
                  -> TcM ()  -- Fails if wrong parent
checkPatSynParent :: Name -> Parent -> Name -> TcRn ()
checkPatSynParent Name
_ (ParentIs {}) Name
_
  = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

checkPatSynParent Name
parent Parent
NoParent Name
nm
  | Name -> Bool
isUnboundName Name
parent -- Avoid an error cascade
  = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

  | Bool
otherwise
  = do { parent_ty_con  <- Name -> TcM TyCon
tcLookupTyCon  Name
parent
       ; mpat_syn_thing <- tcLookupGlobal nm

        -- 1. Check that the Id was actually from a thing associated with patsyns
       ; case mpat_syn_thing of
            AnId Id
i | Id -> Bool
isId Id
i
                   , RecSelId { sel_tycon :: IdDetails -> RecSelParent
sel_tycon = RecSelPatSyn PatSyn
p } <- Id -> IdDetails
idDetails Id
i
                   -> SDoc -> TyCon -> PatSyn -> TcRn ()
handle_pat_syn (Name -> SDoc
selErr Name
nm) TyCon
parent_ty_con PatSyn
p

            AConLike (PatSynCon PatSyn
p) -> SDoc -> TyCon -> PatSyn -> TcRn ()
handle_pat_syn (PatSyn -> SDoc
psErr PatSyn
p) TyCon
parent_ty_con PatSyn
p

            TyThing
_ -> Name -> Name -> [Name] -> TcRn ()
forall a. Name -> Name -> [Name] -> TcM a
failWithDcErr Name
parent Name
nm [] }
  where
    psErr :: PatSyn -> SDoc
psErr  = String -> PatSyn -> SDoc
forall o. Outputable o => String -> o -> SDoc
exportErrCtxt String
"pattern synonym"
    selErr :: Name -> SDoc
selErr = String -> Name -> SDoc
forall o. Outputable o => String -> o -> SDoc
exportErrCtxt String
"pattern synonym record selector"

    handle_pat_syn :: SDoc
                   -> TyCon      -- Parent TyCon
                   -> PatSyn     -- Corresponding bundled PatSyn
                                 -- and pretty printed origin
                   -> TcM ()
    handle_pat_syn :: SDoc -> TyCon -> PatSyn -> TcRn ()
handle_pat_syn SDoc
doc TyCon
ty_con PatSyn
pat_syn

      -- 2. See Note [Types of TyCon]
      | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ TyCon -> Bool
isTyConWithSrcDataCons TyCon
ty_con
      = SDoc -> TcRn () -> TcRn ()
forall a. SDoc -> TcM a -> TcM a
addErrCtxt SDoc
doc (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
forall a. TcRnMessage -> TcM a
failWithTc TcRnMessage
TcRnPatSynBundledWithNonDataCon

      -- 3. Is the head a type variable?
      | Maybe TyCon
Nothing <- Maybe TyCon
mtycon
      = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      -- 4. Ok. Check they are actually the same type constructor.

      | Just TyCon
p_ty_con <- Maybe TyCon
mtycon, TyCon
p_ty_con TyCon -> TyCon -> Bool
forall a. Eq a => a -> a -> Bool
/= TyCon
ty_con
      = SDoc -> TcRn () -> TcRn ()
forall a. SDoc -> TcM a -> TcM a
addErrCtxt SDoc
doc (TcRn () -> TcRn ()) -> TcRn () -> TcRn ()
forall a b. (a -> b) -> a -> b
$ TcRnMessage -> TcRn ()
forall a. TcRnMessage -> TcM a
failWithTc
          (Type -> Type -> TcRnMessage
TcRnPatSynBundledWithWrongType Type
expected_res_ty Type
res_ty)

      -- 5. We passed!
      | Bool
otherwise
      = () -> TcRn ()
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

      where
        expected_res_ty :: Type
expected_res_ty = TyCon -> [Type] -> Type
mkTyConApp TyCon
ty_con ([Id] -> [Type]
mkTyVarTys (TyCon -> [Id]
tyConTyVars TyCon
ty_con))
        ([Id]
_, [Type]
_, [Id]
_, [Type]
_, [Scaled Type]
_, Type
res_ty) = PatSyn -> ([Id], [Type], [Id], [Type], [Scaled Type], Type)
patSynSig PatSyn
pat_syn
        mtycon :: Maybe TyCon
mtycon = (TyCon, [Type]) -> TyCon
forall a b. (a, b) -> a
fst ((TyCon, [Type]) -> TyCon) -> Maybe (TyCon, [Type]) -> Maybe TyCon
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HasDebugCallStack => Type -> Maybe (TyCon, [Type])
Type -> Maybe (TyCon, [Type])
tcSplitTyConApp_maybe Type
res_ty


{-===========================================================================-}

-- | Insert the given 'GlobalRdrElt's into the 'ExportOccMap', checking that
-- each of the given 'GlobalRdrElt's does not appear multiple times in
-- the 'ExportOccMap', as per Note [Exporting duplicate declarations].
check_occs :: ExportOccMap -> IE GhcPs -> [GlobalRdrElt] -> RnM ExportOccMap
check_occs :: ExportOccMap
-> IE GhcPs -> [GlobalRdrEltX GREInfo] -> RnM ExportOccMap
check_occs ExportOccMap
occs IE GhcPs
ie [GlobalRdrEltX GREInfo]
gres
  -- 'gres' are the entities specified by 'ie'
  = do { drf <- Extension -> IOEnv (Env TcGblEnv TcLclEnv) Bool
forall gbl lcl. Extension -> TcRnIf gbl lcl Bool
xoptM Extension
LangExt.DuplicateRecordFields
       ; foldlM (check drf) occs gres }
  where

    -- Check for distinct children exported with the same OccName (an error) or
    -- for duplicate exports of the same child (a warning).
    --
    -- See Note [Exporting duplicate declarations].
    check :: Bool -> ExportOccMap -> GlobalRdrElt -> RnM ExportOccMap
    check :: Bool -> ExportOccMap -> GlobalRdrEltX GREInfo -> RnM ExportOccMap
check Bool
drf_enabled ExportOccMap
occs GlobalRdrEltX GREInfo
gre
      = case ExportOccMap
-> GlobalRdrEltX GREInfo -> Either (Name, IE GhcPs) ExportOccMap
try_insert ExportOccMap
occs GlobalRdrEltX GREInfo
gre of
          Right ExportOccMap
occs'
            -- If DuplicateRecordFields is not enabled, also make sure
            -- that we are not exporting two fields with the same occNameFS
            -- under different namespaces.
            --
            -- See Note [Exporting duplicate record fields].
            | Bool
drf_enabled Bool -> Bool -> Bool
|| Bool -> Bool
not (OccName -> Bool
isFieldOcc OccName
child_occ)
            -> ExportOccMap -> RnM ExportOccMap
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ExportOccMap
occs'
            | Bool
otherwise
            -> do { let flds :: [(Name, IE GhcPs)]
flds = ((Name, IE GhcPs) -> Bool)
-> [(Name, IE GhcPs)] -> [(Name, IE GhcPs)]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(Name
_,IE GhcPs
ie') -> Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ IE GhcPs -> IE GhcPs -> Bool
dupFieldExport_ok IE GhcPs
ie IE GhcPs
ie')
                             ([(Name, IE GhcPs)] -> [(Name, IE GhcPs)])
-> [(Name, IE GhcPs)] -> [(Name, IE GhcPs)]
forall a b. (a -> b) -> a -> b
$ ExportOccMap -> FastString -> [(Name, IE GhcPs)]
forall a. OccEnv a -> FastString -> [a]
lookupFieldsOccEnv ExportOccMap
occs (OccName -> FastString
occNameFS OccName
child_occ)
                  ; case [(Name, IE GhcPs)]
flds of { [] -> ExportOccMap -> RnM ExportOccMap
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ExportOccMap
occs'; (Name, IE GhcPs)
clash1:[(Name, IE GhcPs)]
clashes ->
               do { (GlobalRdrEltX GREInfo, IE GhcPs)
-> NonEmpty (Name, IE GhcPs) -> TcRn ()
addDuplicateFieldExportErr (GlobalRdrEltX GREInfo
gre,IE GhcPs
ie) ((Name, IE GhcPs)
clash1 (Name, IE GhcPs) -> [(Name, IE GhcPs)] -> NonEmpty (Name, IE GhcPs)
forall a. a -> [a] -> NonEmpty a
NE.:| [(Name, IE GhcPs)]
clashes)
                  ; ExportOccMap -> RnM ExportOccMap
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ExportOccMap
occs } } }

          Left (Name
child', IE GhcPs
ie')
            | Name
child Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
child' -- Duplicate export of a single Name: a warning.
            -> do { Bool -> TcRnMessage -> TcRn ()
warnIf (Bool -> Bool
not (Name -> IE GhcPs -> IE GhcPs -> Bool
dupExport_ok Name
child IE GhcPs
ie IE GhcPs
ie')) (GlobalRdrEltX GREInfo -> IE GhcPs -> IE GhcPs -> TcRnMessage
TcRnDuplicateExport GlobalRdrEltX GREInfo
gre IE GhcPs
ie IE GhcPs
ie')
                  ; ExportOccMap -> RnM ExportOccMap
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (m :: * -> *) a. Monad m => a -> m a
return ExportOccMap
occs }

            | Bool
otherwise       -- Same OccName but different Name: an error.
            ->  do { global_env <- TcRn GlobalRdrEnv
getGlobalRdrEnv
                   ; addErr (exportClashErr global_env child' child ie' ie)
                   ; return occs }
      where
        child :: Name
child = GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName GlobalRdrEltX GREInfo
gre
        child_occ :: OccName
child_occ = Name -> OccName
forall name. HasOccName name => name -> OccName
occName Name
child

    -- Try to insert a child into the map, returning Left if there is something
    -- already exported with the same OccName.
    try_insert :: ExportOccMap -> GlobalRdrElt -> Either (Name, IE GhcPs) ExportOccMap
    try_insert :: ExportOccMap
-> GlobalRdrEltX GREInfo -> Either (Name, IE GhcPs) ExportOccMap
try_insert ExportOccMap
occs GlobalRdrEltX GREInfo
child
      = case ExportOccMap -> OccName -> Maybe (Name, IE GhcPs)
forall a. OccEnv a -> OccName -> Maybe a
lookupOccEnv ExportOccMap
occs OccName
occ of
          Maybe (Name, IE GhcPs)
Nothing -> ExportOccMap -> Either (Name, IE GhcPs) ExportOccMap
forall a b. b -> Either a b
Right (ExportOccMap -> OccName -> (Name, IE GhcPs) -> ExportOccMap
forall a. OccEnv a -> OccName -> a -> OccEnv a
extendOccEnv ExportOccMap
occs OccName
occ (GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName GlobalRdrEltX GREInfo
child, IE GhcPs
ie))
          Just (Name, IE GhcPs)
x  -> (Name, IE GhcPs) -> Either (Name, IE GhcPs) ExportOccMap
forall a b. a -> Either a b
Left (Name, IE GhcPs)
x
      where
        occ :: OccName
occ = GlobalRdrEltX GREInfo -> OccName
forall info. GlobalRdrEltX info -> OccName
greOccName GlobalRdrEltX GREInfo
child

-- | Is it OK for the given name to be exported by both export items?
--
-- See Note [Exporting duplicate declarations].
dupExport_ok :: Name -> IE GhcPs -> IE GhcPs -> Bool
dupExport_ok :: Name -> IE GhcPs -> IE GhcPs -> Bool
dupExport_ok Name
child IE GhcPs
ie1 IE GhcPs
ie2
  = Bool -> Bool
not (  IE GhcPs -> Bool
forall {pass}. IE pass -> Bool
single IE GhcPs
ie1 Bool -> Bool -> Bool
|| IE GhcPs -> Bool
forall {pass}. IE pass -> Bool
single IE GhcPs
ie2
        Bool -> Bool -> Bool
|| (IE GhcPs -> Bool
explicit_in IE GhcPs
ie1 Bool -> Bool -> Bool
&& IE GhcPs -> Bool
explicit_in IE GhcPs
ie2) )
  where
    explicit_in :: IE GhcPs -> Bool
explicit_in (IEModuleContents {}) = Bool
False                   -- module M
    explicit_in (IEThingAll XIEThingAll GhcPs
_ LIEWrappedName GhcPs
r Maybe (ExportDoc GhcPs)
_)
      = Name -> OccName
forall name. HasOccName name => name -> OccName
occName Name
child OccName -> OccName -> Bool
forall a. Eq a => a -> a -> Bool
== RdrName -> OccName
rdrNameOcc (IEWrappedName GhcPs -> IdP GhcPs
forall (p :: Pass). IEWrappedName (GhcPass p) -> IdP (GhcPass p)
ieWrappedName (IEWrappedName GhcPs -> IdP GhcPs)
-> IEWrappedName GhcPs -> IdP GhcPs
forall a b. (a -> b) -> a -> b
$ GenLocated SrcSpanAnnA (IEWrappedName GhcPs) -> IEWrappedName GhcPs
forall l e. GenLocated l e -> e
unLoc LIEWrappedName GhcPs
GenLocated SrcSpanAnnA (IEWrappedName GhcPs)
r)  -- T(..)
    explicit_in IE GhcPs
_              = Bool
True

    single :: IE pass -> Bool
single IEVar {}      = Bool
True
    single IEThingAbs {} = Bool
True
    single IE pass
_             = Bool
False

exportErrCtxt :: Outputable o => String -> o -> SDoc
exportErrCtxt :: forall o. Outputable o => String -> o -> SDoc
exportErrCtxt String
herald o
exp =
  String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"In the" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> SDoc
forall doc. IsLine doc => String -> doc
text (String
herald String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
":") SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> o -> SDoc
forall a. Outputable a => a -> SDoc
ppr o
exp


addExportErrCtxt :: (OutputableBndrId p)
                 => IE (GhcPass p) -> TcM a -> TcM a
addExportErrCtxt :: forall (p :: Pass) a.
OutputableBndrId p =>
IE (GhcPass p) -> TcM a -> TcM a
addExportErrCtxt IE (GhcPass p)
ie = SDoc -> TcM a -> TcM a
forall a. SDoc -> TcM a -> TcM a
addErrCtxt SDoc
exportCtxt
  where
    exportCtxt :: SDoc
exportCtxt = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"In the export:" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> IE (GhcPass p) -> SDoc
forall a. Outputable a => a -> SDoc
ppr IE (GhcPass p)
ie


failWithDcErr :: Name -> Name -> [Name] -> TcM a
failWithDcErr :: forall a. Name -> Name -> [Name] -> TcM a
failWithDcErr Name
parent Name
child [Name]
parents = do
  ty_thing <- Name -> TcM TyThing
tcLookupGlobal Name
child
  failWithTc $ TcRnExportedParentChildMismatch parent ty_thing child parents


exportClashErr :: GlobalRdrEnv
               -> Name -> Name
               -> IE GhcPs -> IE GhcPs
               -> TcRnMessage
exportClashErr :: GlobalRdrEnv -> Name -> Name -> IE GhcPs -> IE GhcPs -> TcRnMessage
exportClashErr GlobalRdrEnv
global_env Name
child1 Name
child2 IE GhcPs
ie1 IE GhcPs
ie2
  = OccName
-> GlobalRdrEltX GREInfo
-> IE GhcPs
-> GlobalRdrEltX GREInfo
-> IE GhcPs
-> TcRnMessage
TcRnConflictingExports OccName
occ GlobalRdrEltX GREInfo
gre1' IE GhcPs
ie1' GlobalRdrEltX GREInfo
gre2' IE GhcPs
ie2'
  where
    occ :: OccName
occ = Name -> OccName
forall name. HasOccName name => name -> OccName
occName Name
child1
    -- get_gre finds a GRE for the Name, so that we can show its provenance
    gre1 :: GlobalRdrEltX GREInfo
gre1 = Name -> GlobalRdrEltX GREInfo
get_gre Name
child1
    gre2 :: GlobalRdrEltX GREInfo
gre2 = Name -> GlobalRdrEltX GREInfo
get_gre Name
child2
    get_gre :: Name -> GlobalRdrEltX GREInfo
get_gre Name
child
        = GlobalRdrEltX GREInfo
-> Maybe (GlobalRdrEltX GREInfo) -> GlobalRdrEltX GREInfo
forall a. a -> Maybe a -> a
fromMaybe (String -> SDoc -> GlobalRdrEltX GREInfo
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"exportClashErr" (Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
child))
                    (GlobalRdrEnv -> Name -> Maybe (GlobalRdrEltX GREInfo)
forall info.
Outputable info =>
GlobalRdrEnvX info -> Name -> Maybe (GlobalRdrEltX info)
lookupGRE_Name GlobalRdrEnv
global_env Name
child)
    (GlobalRdrEltX GREInfo
gre1', IE GhcPs
ie1', GlobalRdrEltX GREInfo
gre2', IE GhcPs
ie2') =
      case SrcSpan -> SrcSpan -> Ordering
SrcLoc.leftmost_smallest (GlobalRdrEltX GREInfo -> SrcSpan
forall info. Outputable info => GlobalRdrEltX info -> SrcSpan
greSrcSpan GlobalRdrEltX GREInfo
gre1) (GlobalRdrEltX GREInfo -> SrcSpan
forall info. Outputable info => GlobalRdrEltX info -> SrcSpan
greSrcSpan GlobalRdrEltX GREInfo
gre2) of
        Ordering
LT -> (GlobalRdrEltX GREInfo
gre1, IE GhcPs
ie1, GlobalRdrEltX GREInfo
gre2, IE GhcPs
ie2)
        Ordering
GT -> (GlobalRdrEltX GREInfo
gre2, IE GhcPs
ie2, GlobalRdrEltX GREInfo
gre1, IE GhcPs
ie1)
        Ordering
EQ -> String
-> (GlobalRdrEltX GREInfo, IE GhcPs, GlobalRdrEltX GREInfo,
    IE GhcPs)
forall a. HasCallStack => String -> a
panic String
"exportClashErr: clashing exports have identical location"

addDuplicateFieldExportErr :: (GlobalRdrElt, IE GhcPs)
                           -> NE.NonEmpty (Name, IE GhcPs)
                           -> RnM ()
addDuplicateFieldExportErr :: (GlobalRdrEltX GREInfo, IE GhcPs)
-> NonEmpty (Name, IE GhcPs) -> TcRn ()
addDuplicateFieldExportErr (GlobalRdrEltX GREInfo, IE GhcPs)
gre NonEmpty (Name, IE GhcPs)
others
  = do { rdr_env <- TcRn GlobalRdrEnv
getGlobalRdrEnv
       ; let lkup = String -> Maybe (GlobalRdrEltX GREInfo) -> GlobalRdrEltX GREInfo
forall a. HasDebugCallStack => String -> Maybe a -> a
expectJust String
"addDuplicateFieldExportErr" (Maybe (GlobalRdrEltX GREInfo) -> GlobalRdrEltX GREInfo)
-> (Name -> Maybe (GlobalRdrEltX GREInfo))
-> Name
-> GlobalRdrEltX GREInfo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GlobalRdrEnv -> Name -> Maybe (GlobalRdrEltX GREInfo)
forall info.
Outputable info =>
GlobalRdrEnvX info -> Name -> Maybe (GlobalRdrEltX info)
lookupGRE_Name GlobalRdrEnv
rdr_env
             other_gres = ((Name, IE GhcPs) -> (GlobalRdrEltX GREInfo, IE GhcPs))
-> NonEmpty (Name, IE GhcPs)
-> NonEmpty (GlobalRdrEltX GREInfo, IE GhcPs)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Name -> GlobalRdrEltX GREInfo)
-> (Name, IE GhcPs) -> (GlobalRdrEltX GREInfo, IE GhcPs)
forall b c d. (b -> c) -> (b, d) -> (c, d)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Name -> GlobalRdrEltX GREInfo
lkup) NonEmpty (Name, IE GhcPs)
others
       ; addErr (TcRnDuplicateFieldExport gre other_gres) }

-- | Is it OK to export two clashing duplicate record fields coming from the
-- given export items, with @-XDisambiguateRecordFields@ disabled?
--
-- See Note [Exporting duplicate record fields].
dupFieldExport_ok :: IE GhcPs -> IE GhcPs -> Bool
dupFieldExport_ok :: IE GhcPs -> IE GhcPs -> Bool
dupFieldExport_ok IE GhcPs
ie1 IE GhcPs
ie2
  | IEModuleContents {} <- IE GhcPs
ie1
  , IE GhcPs
ie2 IE GhcPs -> IE GhcPs -> Bool
forall a. Eq a => a -> a -> Bool
== IE GhcPs
ie1
  = Bool
True
  | Bool
otherwise
  = Bool
False

{- Note [Exporting duplicate declarations]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We want to check that two different export items don't have both attempt to export
the same thing. What do we mean precisely? There are three main situations to consider:

  1. We export two distinct Names with identical OccNames. This is an error.
  2. We export the same Name in two different export items. This is usually
     a warning, but see below.
  3. We export a duplicate record field, and DuplicateRecordFields is not enabled.
     See Note [Exporting duplicate record fields].

Concerning (2), we sometimes want to allow a duplicate export of a given Name,
as #4478 points out. The logic, as implemented in dupExport_ok, is that we
do not allow a given Name to be exported by two IEs iff either:

  - the Name is mentioned explicitly in both IEs, or
  - one of the IEs mentions the name *alone*.

Examples:

  NOT OK: module M( f, f )

    f is mentioned explicitly in both

  NOT OK: module M( fmap, Functor(..) )
  NOT OK: module M( module Data.Functor, fmap )

    One of the import items mentions fmap alone, which is also
    exported by the other export item.

  OK:
    module M( module A, module B ) where
      import A( f )
      import B( f )

  OK: (#2436)
    module M( C(..), T(..) ) where
      class C a where { data T a }
      instance C Int where { data T Int = TInt }

  OK: (#2436)
    module Foo ( T ) where
      data family T a
    module Bar ( T(..), module Foo ) where
      import Foo
      data instance T Int = TInt

Note [Exporting duplicate record fields]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Record fields belonging to different datatypes belong to different namespaces,
as explained in Note [Record field namespacing] in GHC.Types.Name.Occurrence.
However, when the DuplicateRecordFields extension is NOT enabled, we want to
prevent users from exporting record fields that share the same underlying occNameFS.

To enforce this, in check_occs, when inserting a new record field into the ExportOccMap
and DuplicateRecordFields is not enabled, we also look up any clashing record fields,
and report an error.

Note however that the clash check has an extra wrinkle, similar to dupExport_ok,
as we want to allow the following:

  {-# LANGUAGE DuplicateRecordFields #-}
  module M1 where
    data D1 = MkD1 { foo :: Int }
    data D2 = MkD2 { foo :: Bool }

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

   module M2 ( module M1 ) where
     import M1

That is, we should be allowed to re-export the whole module M1, without reporting
any nameclashes, even though M1 exports duplicate record fields and we have not
enabled -XDuplicateRecordFields in M2. This logic is implemented in
dupFieldExport_ok. See test case NoDRFModuleExport.

Note that this logic only applies to whole-module imports, as we don't want
to allow the following:

  module N0 where
    data family D a
  module N1 where
    import N0
    data instance D Int = MkDInt { foo :: Int }
  module N2 where
    import N0
    data instance D Bool = MkDBool { foo :: Int }

  module N (D(..)) where
    import N1
    import N2

Here, the single export item D(..) of N exports both record fields,
`$fld:MkDInt:foo` and `$fld:MkDBool:foo`, so we have to reject the program.
See test overloadedrecfldsfail10.
-}