{-# OPTIONS_GHC -Wwarn #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ViewPatterns #-}
  -----------------------------------------------------------------------------
-- |
-- Module      :  Haddock.Interface.LexParseRn
-- Copyright   :  (c) Isaac Dupree 2009,
--                    Mateusz Kowalczyk 2013
-- License     :  BSD-like
--
-- Maintainer  :  haddock@projects.haskell.org
-- Stability   :  experimental
-- Portability :  portable
-----------------------------------------------------------------------------
module Haddock.Interface.LexParseRn
  ( processDocString
  , processDocStringParas
  , processDocStrings
  , processModuleHeader
  ) where

import Control.Arrow
import Control.Monad
import Data.Functor (($>))
import Data.List (maximumBy, (\\))
import Data.Ord
import Documentation.Haddock.Doc (metaDocConcat)
import DynFlags (languageExtensions)
import qualified GHC.LanguageExtensions as LangExt
import GHC
import Haddock.Interface.ParseModuleHeader
import Haddock.Parser
import Haddock.Types
import Name
import Outputable ( showPpr, showSDoc )
import RdrName
import RdrHsSyn (setRdrNameSpace)
import EnumSet

processDocStrings :: DynFlags -> Maybe Package -> GlobalRdrEnv -> [HsDocString]
                  -> ErrMsgM (Maybe (MDoc Name))
processDocStrings :: DynFlags
-> Maybe Package
-> GlobalRdrEnv
-> [HsDocString]
-> ErrMsgM (Maybe (MDoc Name))
processDocStrings DynFlags
dflags Maybe Package
pkg GlobalRdrEnv
gre [HsDocString]
strs = do
  MDoc Name
mdoc <- [MDoc Name] -> MDoc Name
forall mod id. [MetaDoc mod id] -> MetaDoc mod id
metaDocConcat ([MDoc Name] -> MDoc Name)
-> ErrMsgM [MDoc Name] -> ErrMsgM (MDoc Name)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (HsDocString -> ErrMsgM (MDoc Name))
-> [HsDocString] -> ErrMsgM [MDoc Name]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (DynFlags
-> Maybe Package
-> GlobalRdrEnv
-> HsDocString
-> ErrMsgM (MDoc Name)
processDocStringParas DynFlags
dflags Maybe Package
pkg GlobalRdrEnv
gre) [HsDocString]
strs
  case MDoc Name
mdoc of
    -- We check that we don't have any version info to render instead
    -- of just checking if there is no comment: there may not be a
    -- comment but we still want to pass through any meta data.
    MetaDoc { _meta :: forall mod id. MetaDoc mod id -> Meta
_meta = Meta Maybe Version
Nothing Maybe Package
Nothing, _doc :: forall mod id. MetaDoc mod id -> DocH mod id
_doc = DocH (Wrap (ModuleName, OccName)) (Wrap Name)
DocEmpty } -> Maybe (MDoc Name) -> ErrMsgM (Maybe (MDoc Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (MDoc Name)
forall a. Maybe a
Nothing
    MDoc Name
x -> Maybe (MDoc Name) -> ErrMsgM (Maybe (MDoc Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MDoc Name -> Maybe (MDoc Name)
forall a. a -> Maybe a
Just MDoc Name
x)

processDocStringParas :: DynFlags -> Maybe Package -> GlobalRdrEnv -> HsDocString -> ErrMsgM (MDoc Name)
processDocStringParas :: DynFlags
-> Maybe Package
-> GlobalRdrEnv
-> HsDocString
-> ErrMsgM (MDoc Name)
processDocStringParas DynFlags
dflags Maybe Package
pkg GlobalRdrEnv
gre HsDocString
hds =
  (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
 -> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> MetaDoc (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (MDoc Name)
forall (f :: * -> *) a b c d.
Functor f =>
(DocH a b -> f (DocH c d)) -> MetaDoc a b -> f (MetaDoc c d)
overDocF (DynFlags
-> GlobalRdrEnv
-> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rename DynFlags
dflags GlobalRdrEnv
gre) (MetaDoc (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
 -> ErrMsgM (MDoc Name))
-> MetaDoc (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (MDoc Name)
forall a b. (a -> b) -> a -> b
$ DynFlags
-> Maybe Package
-> Package
-> MetaDoc (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
forall mod.
DynFlags
-> Maybe Package -> Package -> MetaDoc mod (Wrap NsRdrName)
parseParas DynFlags
dflags Maybe Package
pkg (HsDocString -> Package
unpackHDS HsDocString
hds)

processDocString :: DynFlags -> GlobalRdrEnv -> HsDocString -> ErrMsgM (Doc Name)
processDocString :: DynFlags
-> GlobalRdrEnv
-> HsDocString
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
processDocString DynFlags
dflags GlobalRdrEnv
gre HsDocString
hds =
  DynFlags
-> GlobalRdrEnv
-> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rename DynFlags
dflags GlobalRdrEnv
gre (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
 -> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall a b. (a -> b) -> a -> b
$ DynFlags
-> Package -> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
forall mod. DynFlags -> Package -> DocH mod (Wrap NsRdrName)
parseString DynFlags
dflags (HsDocString -> Package
unpackHDS HsDocString
hds)

processModuleHeader :: DynFlags -> Maybe Package -> GlobalRdrEnv -> SafeHaskellMode -> Maybe LHsDocString
                    -> ErrMsgM (HaddockModInfo Name, Maybe (MDoc Name))
processModuleHeader :: DynFlags
-> Maybe Package
-> GlobalRdrEnv
-> SafeHaskellMode
-> Maybe LHsDocString
-> ErrMsgM (HaddockModInfo Name, Maybe (MDoc Name))
processModuleHeader DynFlags
dflags Maybe Package
pkgName GlobalRdrEnv
gre SafeHaskellMode
safety Maybe LHsDocString
mayStr = do
  (HaddockModInfo Name
hmi, Maybe (MDoc Name)
doc) <-
    case Maybe LHsDocString
mayStr of
      Maybe LHsDocString
Nothing -> (HaddockModInfo Name, Maybe (MDoc Name))
-> ErrMsgM (HaddockModInfo Name, Maybe (MDoc Name))
forall (m :: * -> *) a. Monad m => a -> m a
return (HaddockModInfo Name, Maybe (MDoc Name))
forall a a. (HaddockModInfo a, Maybe a)
failure
      Just (L SrcSpan
_ HsDocString
hds) -> do
        let str :: Package
str = HsDocString -> Package
unpackHDS HsDocString
hds
            (HaddockModInfo NsRdrName
hmi, MetaDoc (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc) = DynFlags
-> Maybe Package
-> Package
-> (HaddockModInfo NsRdrName,
    MetaDoc (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
parseModuleHeader DynFlags
dflags Maybe Package
pkgName Package
str
        !Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
descr <- case HaddockModInfo NsRdrName
-> Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
forall name. HaddockModInfo name -> Maybe (Doc name)
hmi_description HaddockModInfo NsRdrName
hmi of
                    Just DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
hmi_descr -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall a. a -> Maybe a
Just (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
 -> Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DynFlags
-> GlobalRdrEnv
-> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rename DynFlags
dflags GlobalRdrEnv
gre DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
hmi_descr
                    Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
Nothing        -> Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall a. Maybe a
Nothing
        let hmi' :: HaddockModInfo Name
hmi' = HaddockModInfo NsRdrName
hmi { hmi_description :: Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
hmi_description = Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
descr }
        MDoc Name
doc'  <- (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
 -> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> MetaDoc (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (MDoc Name)
forall (f :: * -> *) a b c d.
Functor f =>
(DocH a b -> f (DocH c d)) -> MetaDoc a b -> f (MetaDoc c d)
overDocF (DynFlags
-> GlobalRdrEnv
-> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rename DynFlags
dflags GlobalRdrEnv
gre) MetaDoc (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc
        (HaddockModInfo Name, Maybe (MDoc Name))
-> ErrMsgM (HaddockModInfo Name, Maybe (MDoc Name))
forall (m :: * -> *) a. Monad m => a -> m a
return (HaddockModInfo Name
hmi', MDoc Name -> Maybe (MDoc Name)
forall a. a -> Maybe a
Just MDoc Name
doc')

  let flags :: [LangExt.Extension]
      -- We remove the flags implied by the language setting and we display the language instead
      flags :: [Extension]
flags = EnumSet Extension -> [Extension]
forall a. Enum a => EnumSet a -> [a]
EnumSet.toList (DynFlags -> EnumSet Extension
extensionFlags DynFlags
dflags) [Extension] -> [Extension] -> [Extension]
forall a. Eq a => [a] -> [a] -> [a]
\\ Maybe Language -> [Extension]
languageExtensions (DynFlags -> Maybe Language
language DynFlags
dflags)
  (HaddockModInfo Name, Maybe (MDoc Name))
-> ErrMsgM (HaddockModInfo Name, Maybe (MDoc Name))
forall (m :: * -> *) a. Monad m => a -> m a
return (HaddockModInfo Name
hmi { hmi_safety :: Maybe Package
hmi_safety = Package -> Maybe Package
forall a. a -> Maybe a
Just (Package -> Maybe Package) -> Package -> Maybe Package
forall a b. (a -> b) -> a -> b
$ DynFlags -> SafeHaskellMode -> Package
forall a. Outputable a => DynFlags -> a -> Package
showPpr DynFlags
dflags SafeHaskellMode
safety
              , hmi_language :: Maybe Language
hmi_language = DynFlags -> Maybe Language
language DynFlags
dflags
              , hmi_extensions :: [Extension]
hmi_extensions = [Extension]
flags
              } , Maybe (MDoc Name)
doc)
  where
    failure :: (HaddockModInfo a, Maybe a)
failure = (HaddockModInfo a
forall a. HaddockModInfo a
emptyHaddockModInfo, Maybe a
forall a. Maybe a
Nothing)

-- | Takes a 'GlobalRdrEnv' which (hopefully) contains all the
-- definitions and a parsed comment and we attempt to make sense of
-- where the identifiers in the comment point to. We're in effect
-- trying to convert 'RdrName's to 'Name's, with some guesswork and
-- fallbacks in case we can't locate the identifiers.
--
-- See the comments in the source for implementation commentary.
rename :: DynFlags -> GlobalRdrEnv -> Doc NsRdrName -> ErrMsgM (Doc Name)
rename :: DynFlags
-> GlobalRdrEnv
-> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rename DynFlags
dflags GlobalRdrEnv
gre = DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn
  where
    rn :: DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
d = case DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
d of
      DocAppend DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
a DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
b -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. DocH mod id -> DocH mod id -> DocH mod id
DocAppend (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM
     (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
      -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
a ErrMsgM
  (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
   -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
b
      DocParagraph DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. DocH mod id -> DocH mod id
DocParagraph (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc
      DocIdentifier Wrap NsRdrName
i -> do
        let NsRdrName Namespace
ns RdrName
x = Wrap NsRdrName -> NsRdrName
forall n. Wrap n -> n
unwrap Wrap NsRdrName
i
            occ :: OccName
occ = RdrName -> OccName
rdrNameOcc RdrName
x
            isValueName :: Bool
isValueName = OccName -> Bool
isDataOcc OccName
occ Bool -> Bool -> Bool
|| OccName -> Bool
isVarOcc OccName
occ

        let valueNsChoices :: [RdrName]
valueNsChoices | Bool
isValueName = [RdrName
x]
                           | Bool
otherwise   = [] -- is this ever possible?
            typeNsChoices :: [RdrName]
typeNsChoices  | Bool
isValueName = [RdrName -> NameSpace -> RdrName
setRdrNameSpace RdrName
x NameSpace
tcName]
                           | Bool
otherwise   = [RdrName
x]

        -- Generate the choices for the possible kind of thing this
        -- is. We narrow down the possibilities with the namespace (if
        -- there is one).
        let choices :: [RdrName]
choices = case Namespace
ns of
                        Namespace
Value -> [RdrName]
valueNsChoices
                        Namespace
Type  -> [RdrName]
typeNsChoices
                        Namespace
None  -> [RdrName]
valueNsChoices [RdrName] -> [RdrName] -> [RdrName]
forall a. [a] -> [a] -> [a]
++ [RdrName]
typeNsChoices

        -- Lookup any GlobalRdrElts that match the choices.
        case (RdrName -> [GlobalRdrElt]) -> [RdrName] -> [GlobalRdrElt]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\RdrName
c -> RdrName -> GlobalRdrEnv -> [GlobalRdrElt]
lookupGRE_RdrName RdrName
c GlobalRdrEnv
gre) [RdrName]
choices of
          -- We found no names in the env so we start guessing.
          [] ->
            case [RdrName]
choices of
              -- The only way this can happen is if a value namespace was
              -- specified on something that cannot be a value.
              [] -> DynFlags
-> Wrap NsRdrName
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall a. DynFlags -> Wrap NsRdrName -> ErrMsgM (Doc a)
invalidValue DynFlags
dflags Wrap NsRdrName
i

              -- There was nothing in the environment so we need to
              -- pick some default from what's available to us. We
              -- diverge here from the old way where we would default
              -- to type constructors as we're much more likely to
              -- actually want anchors to regular definitions than
              -- type constructor names (such as in #253). So now we
              -- only get type constructor links if they are actually
              -- in scope.
              RdrName
a:[RdrName]
_ -> DynFlags
-> Namespace
-> Wrap RdrName
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall a. DynFlags -> Namespace -> Wrap RdrName -> ErrMsgM (Doc a)
outOfScope DynFlags
dflags Namespace
ns (Wrap NsRdrName
i Wrap NsRdrName -> RdrName -> Wrap RdrName
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> RdrName
a)

          -- There is only one name in the environment that matches so
          -- use it.
          [GlobalRdrElt
a] -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Wrap Name -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. id -> DocH mod id
DocIdentifier (Wrap NsRdrName
i Wrap NsRdrName -> Name -> Wrap Name
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> GlobalRdrElt -> Name
gre_name GlobalRdrElt
a))

          -- There are multiple names available.
          [GlobalRdrElt]
gres -> DynFlags
-> Wrap NsRdrName
-> [GlobalRdrElt]
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
ambiguous DynFlags
dflags Wrap NsRdrName
i [GlobalRdrElt]
gres

      DocWarning DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. DocH mod id -> DocH mod id
DocWarning (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc
      DocEmphasis DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. DocH mod id -> DocH mod id
DocEmphasis (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc
      DocBold DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. DocH mod id -> DocH mod id
DocBold (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc
      DocMonospaced DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. DocH mod id -> DocH mod id
DocMonospaced (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc
      DocUnorderedList [DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)]
docs -> [DocH (Wrap (ModuleName, OccName)) (Wrap Name)]
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. [DocH mod id] -> DocH mod id
DocUnorderedList ([DocH (Wrap (ModuleName, OccName)) (Wrap Name)]
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM [DocH (Wrap (ModuleName, OccName)) (Wrap Name)]
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
 -> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> [DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)]
-> ErrMsgM [DocH (Wrap (ModuleName, OccName)) (Wrap Name)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn [DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)]
docs
      DocOrderedList [DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)]
docs -> [DocH (Wrap (ModuleName, OccName)) (Wrap Name)]
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. [DocH mod id] -> DocH mod id
DocOrderedList ([DocH (Wrap (ModuleName, OccName)) (Wrap Name)]
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM [DocH (Wrap (ModuleName, OccName)) (Wrap Name)]
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
 -> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> [DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)]
-> ErrMsgM [DocH (Wrap (ModuleName, OccName)) (Wrap Name)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn [DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)]
docs
      DocDefList [(DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName),
  DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))]
list -> [(DocH (Wrap (ModuleName, OccName)) (Wrap Name),
  DocH (Wrap (ModuleName, OccName)) (Wrap Name))]
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. [(DocH mod id, DocH mod id)] -> DocH mod id
DocDefList ([(DocH (Wrap (ModuleName, OccName)) (Wrap Name),
   DocH (Wrap (ModuleName, OccName)) (Wrap Name))]
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM
     [(DocH (Wrap (ModuleName, OccName)) (Wrap Name),
       DocH (Wrap (ModuleName, OccName)) (Wrap Name))]
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName),
  DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
 -> ErrMsgM
      (DocH (Wrap (ModuleName, OccName)) (Wrap Name),
       DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> [(DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName),
     DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))]
-> ErrMsgM
     [(DocH (Wrap (ModuleName, OccName)) (Wrap Name),
       DocH (Wrap (ModuleName, OccName)) (Wrap Name))]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (\(DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
a, DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
b) -> (,) (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
 -> (DocH (Wrap (ModuleName, OccName)) (Wrap Name),
     DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM
     (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
      -> (DocH (Wrap (ModuleName, OccName)) (Wrap Name),
          DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
a ErrMsgM
  (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
   -> (DocH (Wrap (ModuleName, OccName)) (Wrap Name),
       DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM
     (DocH (Wrap (ModuleName, OccName)) (Wrap Name),
      DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
b) [(DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName),
  DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))]
list
      DocCodeBlock DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. DocH mod id -> DocH mod id
DocCodeBlock (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
doc
      DocIdentifierUnchecked Wrap (ModuleName, OccName)
x -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Wrap (ModuleName, OccName)
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. mod -> DocH mod id
DocIdentifierUnchecked Wrap (ModuleName, OccName)
x)
      DocModule (ModLink Package
m Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
l) -> ModLink (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. ModLink (DocH mod id) -> DocH mod id
DocModule (ModLink (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> (Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
    -> ModLink (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Package
-> Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ModLink (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall id. Package -> Maybe id -> ModLink id
ModLink Package
m (Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
 -> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
-> ErrMsgM (Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
l
      DocHyperlink (Hyperlink Package
u Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
l) -> Hyperlink (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. Hyperlink (DocH mod id) -> DocH mod id
DocHyperlink (Hyperlink (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> (Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
    -> Hyperlink (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Package
-> Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> Hyperlink (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall id. Package -> Maybe id -> Hyperlink id
Hyperlink Package
u (Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
 -> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
-> ErrMsgM (Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn Maybe (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
l
      DocPic Picture
str -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Picture -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. Picture -> DocH mod id
DocPic Picture
str)
      DocMathInline Package
str -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Package -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. Package -> DocH mod id
DocMathInline Package
str)
      DocMathDisplay Package
str -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Package -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. Package -> DocH mod id
DocMathDisplay Package
str)
      DocAName Package
str -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Package -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. Package -> DocH mod id
DocAName Package
str)
      DocProperty Package
p -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Package -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. Package -> DocH mod id
DocProperty Package
p)
      DocExamples [Example]
e -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Example] -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. [Example] -> DocH mod id
DocExamples [Example]
e)
      DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
DocEmpty -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. DocH mod id
DocEmpty)
      DocString Package
str -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Package -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. Package -> DocH mod id
DocString Package
str)
      DocHeader (Header Int
l DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
t) -> Header (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. Header (DocH mod id) -> DocH mod id
DocHeader (Header (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
    -> Header (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> Header (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall id. Int -> id -> Header id
Header Int
l (DocH (Wrap (ModuleName, OccName)) (Wrap Name)
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
t
      DocTable Table (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
t -> Table (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. Table (DocH mod id) -> DocH mod id
DocTable (Table (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
 -> DocH (Wrap (ModuleName, OccName)) (Wrap Name))
-> ErrMsgM (Table (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
 -> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
-> Table (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
-> ErrMsgM (Table (DocH (Wrap (ModuleName, OccName)) (Wrap Name)))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
rn Table (DocH (Wrap (ModuleName, OccName)) (Wrap NsRdrName))
t

-- | Wrap an identifier that's out of scope (i.e. wasn't found in
-- 'GlobalReaderEnv' during 'rename') in an appropriate doc. Currently
-- we simply monospace the identifier in most cases except when the
-- identifier is qualified: if the identifier is qualified then we can
-- still try to guess and generate anchors accross modules but the
-- users shouldn't rely on this doing the right thing. See tickets
-- #253 and #375 on the confusion this causes depending on which
-- default we pick in 'rename'.
outOfScope :: DynFlags -> Namespace -> Wrap RdrName -> ErrMsgM (Doc a)
outOfScope :: DynFlags -> Namespace -> Wrap RdrName -> ErrMsgM (Doc a)
outOfScope DynFlags
dflags Namespace
ns Wrap RdrName
x =
  case Wrap RdrName -> RdrName
forall n. Wrap n -> n
unwrap Wrap RdrName
x of
    Unqual OccName
occ -> Wrap OccName -> ErrMsgM (Doc a)
forall a mod id. Outputable a => Wrap a -> ErrMsgM (DocH mod id)
warnAndMonospace (Wrap RdrName
x Wrap RdrName -> OccName -> Wrap OccName
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> OccName
occ)
    Qual ModuleName
mdl OccName
occ -> Doc a -> ErrMsgM (Doc a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Wrap (ModuleName, OccName) -> Doc a
forall mod id. mod -> DocH mod id
DocIdentifierUnchecked (Wrap RdrName
x Wrap RdrName -> (ModuleName, OccName) -> Wrap (ModuleName, OccName)
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> (ModuleName
mdl, OccName
occ)))
    Orig Module
_ OccName
occ -> Wrap OccName -> ErrMsgM (Doc a)
forall a mod id. Outputable a => Wrap a -> ErrMsgM (DocH mod id)
warnAndMonospace (Wrap RdrName
x Wrap RdrName -> OccName -> Wrap OccName
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> OccName
occ)
    Exact Name
name -> Wrap Name -> ErrMsgM (Doc a)
forall a mod id. Outputable a => Wrap a -> ErrMsgM (DocH mod id)
warnAndMonospace (Wrap RdrName
x Wrap RdrName -> Name -> Wrap Name
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Name
name)  -- Shouldn't happen since x is out of scope
  where
    prefix :: Package
prefix = case Namespace
ns of
               Namespace
Value -> Package
"the value "
               Namespace
Type -> Package
"the type "
               Namespace
None -> Package
""

    warnAndMonospace :: Wrap a -> ErrMsgM (DocH mod id)
warnAndMonospace Wrap a
a = do
      let a' :: Package
a' = (a -> Package) -> Wrap a -> Package
forall a. (a -> Package) -> Wrap a -> Package
showWrapped (DynFlags -> a -> Package
forall a. Outputable a => DynFlags -> a -> Package
showPpr DynFlags
dflags) Wrap a
a
      [Package] -> ErrMsgM ()
tell [Package
"Warning: " Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ Package
prefix Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ Package
"'" Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ Package
a' Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ Package
"' is out of scope.\n" Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++
            Package
"    If you qualify the identifier, haddock can try to link it anyway."]
      DocH mod id -> ErrMsgM (DocH mod id)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Package -> DocH mod id
forall mod id. Package -> DocH mod id
monospaced Package
a')
    monospaced :: Package -> DocH mod id
monospaced = DocH mod id -> DocH mod id
forall mod id. DocH mod id -> DocH mod id
DocMonospaced (DocH mod id -> DocH mod id)
-> (Package -> DocH mod id) -> Package -> DocH mod id
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Package -> DocH mod id
forall mod id. Package -> DocH mod id
DocString

-- | Handle ambiguous identifiers.
--
-- Prefers local names primarily and type constructors or class names secondarily.
--
-- Emits a warning if the 'GlobalRdrElts's don't belong to the same type or class.
ambiguous :: DynFlags
          -> Wrap NsRdrName
          -> [GlobalRdrElt] -- ^ More than one @gre@s sharing the same `RdrName` above.
          -> ErrMsgM (Doc Name)
ambiguous :: DynFlags
-> Wrap NsRdrName
-> [GlobalRdrElt]
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
ambiguous DynFlags
dflags Wrap NsRdrName
x [GlobalRdrElt]
gres = do
  let dflt :: GlobalRdrElt
dflt = (GlobalRdrElt -> GlobalRdrElt -> Ordering)
-> [GlobalRdrElt] -> GlobalRdrElt
forall (t :: * -> *) a.
Foldable t =>
(a -> a -> Ordering) -> t a -> a
maximumBy ((GlobalRdrElt -> (Bool, Bool))
-> GlobalRdrElt -> GlobalRdrElt -> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (GlobalRdrElt -> Bool
gre_lcl (GlobalRdrElt -> Bool)
-> (GlobalRdrElt -> Bool) -> GlobalRdrElt -> (Bool, Bool)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& Name -> Bool
isTyConName (Name -> Bool) -> (GlobalRdrElt -> Name) -> GlobalRdrElt -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GlobalRdrElt -> Name
gre_name)) [GlobalRdrElt]
gres
      msg :: Package
msg = Package
"Warning: " Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ DynFlags -> Wrap NsRdrName -> Package
showNsRdrName DynFlags
dflags Wrap NsRdrName
x Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ Package
" is ambiguous. It is defined\n" Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++
            (GlobalRdrElt -> Package) -> [GlobalRdrElt] -> Package
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\GlobalRdrElt
n -> Package
"    * " Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ GlobalRdrElt -> Package
defnLoc GlobalRdrElt
n Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ Package
"\n") [GlobalRdrElt]
gres Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++
            Package
"    You may be able to disambiguate the identifier by qualifying it or\n" Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++
            Package
"    by specifying the type/value namespace explicitly.\n" Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++
            Package
"    Defaulting to the one defined " Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ GlobalRdrElt -> Package
defnLoc GlobalRdrElt
dflt
  -- TODO: Once we have a syntax for namespace qualification (#667) we may also
  -- want to emit a warning when an identifier is a data constructor for a type
  -- of the same name, but not the only constructor.
  -- For example, for @data D = C | D@, someone may want to reference the @D@
  -- constructor.
  Bool -> ErrMsgM () -> ErrMsgM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([AvailInfo] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([GlobalRdrElt] -> [AvailInfo]
gresToAvailInfo [GlobalRdrElt]
gres) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1) (ErrMsgM () -> ErrMsgM ()) -> ErrMsgM () -> ErrMsgM ()
forall a b. (a -> b) -> a -> b
$ [Package] -> ErrMsgM ()
tell [Package
msg]
  DocH (Wrap (ModuleName, OccName)) (Wrap Name)
-> ErrMsgM (DocH (Wrap (ModuleName, OccName)) (Wrap Name))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Wrap Name -> DocH (Wrap (ModuleName, OccName)) (Wrap Name)
forall mod id. id -> DocH mod id
DocIdentifier (Wrap NsRdrName
x Wrap NsRdrName -> Name -> Wrap Name
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> GlobalRdrElt -> Name
gre_name GlobalRdrElt
dflt))
  where
    defnLoc :: GlobalRdrElt -> Package
defnLoc = DynFlags -> SDoc -> Package
showSDoc DynFlags
dflags (SDoc -> Package)
-> (GlobalRdrElt -> SDoc) -> GlobalRdrElt -> Package
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> SDoc
pprNameDefnLoc (Name -> SDoc) -> (GlobalRdrElt -> Name) -> GlobalRdrElt -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GlobalRdrElt -> Name
gre_name

-- | Handle value-namespaced names that cannot be for values.
--
-- Emits a warning that the value-namespace is invalid on a non-value identifier.
invalidValue :: DynFlags -> Wrap NsRdrName -> ErrMsgM (Doc a)
invalidValue :: DynFlags -> Wrap NsRdrName -> ErrMsgM (Doc a)
invalidValue DynFlags
dflags Wrap NsRdrName
x = do
  [Package] -> ErrMsgM ()
tell [Package
"Warning: " Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ DynFlags -> Wrap NsRdrName -> Package
showNsRdrName DynFlags
dflags Wrap NsRdrName
x Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ Package
" cannot be value, yet it is\n" Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++
            Package
"    namespaced as such. Did you mean to specify a type namespace\n" Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++
            Package
"    instead?"]
  Doc a -> ErrMsgM (Doc a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Doc a -> Doc a
forall mod id. DocH mod id -> DocH mod id
DocMonospaced (Package -> Doc a
forall mod id. Package -> DocH mod id
DocString (DynFlags -> Wrap NsRdrName -> Package
showNsRdrName DynFlags
dflags Wrap NsRdrName
x)))

-- | Printable representation of a wrapped and namespaced name
showNsRdrName :: DynFlags -> Wrap NsRdrName -> String
showNsRdrName :: DynFlags -> Wrap NsRdrName -> Package
showNsRdrName DynFlags
dflags = (\Package
p Package
i -> Package
p Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ Package
"'" Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ Package
i Package -> Package -> Package
forall a. [a] -> [a] -> [a]
++ Package
"'") (Package -> Package -> Package)
-> (Wrap NsRdrName -> Package)
-> Wrap NsRdrName
-> Package
-> Package
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Wrap NsRdrName -> Package
prefix (Wrap NsRdrName -> Package -> Package)
-> (Wrap NsRdrName -> Package) -> Wrap NsRdrName -> Package
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Wrap NsRdrName -> Package
ident
  where
    ident :: Wrap NsRdrName -> Package
ident = (NsRdrName -> Package) -> Wrap NsRdrName -> Package
forall a. (a -> Package) -> Wrap a -> Package
showWrapped (DynFlags -> RdrName -> Package
forall a. Outputable a => DynFlags -> a -> Package
showPpr DynFlags
dflags (RdrName -> Package)
-> (NsRdrName -> RdrName) -> NsRdrName -> Package
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NsRdrName -> RdrName
rdrName)
    prefix :: Wrap NsRdrName -> Package
prefix = Namespace -> Package
renderNs (Namespace -> Package)
-> (Wrap NsRdrName -> Namespace) -> Wrap NsRdrName -> Package
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NsRdrName -> Namespace
namespace (NsRdrName -> Namespace)
-> (Wrap NsRdrName -> NsRdrName) -> Wrap NsRdrName -> Namespace
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Wrap NsRdrName -> NsRdrName
forall n. Wrap n -> n
unwrap