Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit c798cf4

Browse files
harpocratessjakobi
authored andcommitted
Delete dead code-paths (#910)
1 parent 29edbb9 commit c798cf4

File tree

2 files changed

+32
-142
lines changed

2 files changed

+32
-142
lines changed

haddock-api/src/Haddock/Interface/Create.hs

Lines changed: 32 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ import Data.Traversable
3737

3838
import Avail hiding (avail)
3939
import qualified Avail
40-
import qualified Module
41-
import qualified SrcLoc
4240
import ConLike (ConLike(..))
4341
import GHC
4442
import GhcMonad
@@ -51,7 +49,7 @@ import TcIface
5149
import TcRnMonad
5250
import FastString ( unpackFS )
5351
import HsDecls ( getConArgs )
54-
import BasicTypes ( SourceText(..), WarningTxt(..), WarningSort(..), warningTxtContents )
52+
import BasicTypes ( WarningTxt(..), WarningSort(..), warningTxtContents )
5553
import qualified Outputable as O
5654
import DynFlags ( getDynFlags )
5755

@@ -112,9 +110,7 @@ createInterface mod_iface flags modMap instIfaceMap = do
112110
++ map getName fam_instances
113111
instanceMap = M.fromList (map (getSrcSpan &&& id) localInsts)
114112

115-
-- TODO: Entirely remove DeclMap.
116-
let declMap = M.empty
117-
maps = (docMap, argMap, declMap, instanceMap)
113+
let maps = (docMap, argMap, instanceMap)
118114
allWarnings = M.unions (warningMap : map ifaceWarningMap (M.elems modMap))
119115

120116
-- Locations of all TH splices
@@ -161,7 +157,7 @@ createInterface mod_iface flags modMap instIfaceMap = do
161157
, ifaceRnExportItems = []
162158
, ifaceExports = exportedNames
163159
, ifaceVisibleExports = visibleNames
164-
, ifaceDeclMap = declMap
160+
, ifaceDeclMap = M.empty -- TODO: get rid of this
165161
, ifaceFixMap = fixMap
166162
, ifaceInstances = instances
167163
, ifaceFamInstances = fam_instances
@@ -274,7 +270,7 @@ parseOption "not-home" = return (Just OptNotHome)
274270
parseOption "show-extensions" = return (Just OptShowExtensions)
275271
parseOption other = tell ["Unrecognised option: " ++ other] >> return Nothing
276272

277-
type Maps = (DocMap Name, ArgMap Name, DeclMap, InstMap)
273+
type Maps = (DocMap Name, ArgMap Name, InstMap)
278274

279275
-- | Extract a map of fixity declarations only
280276
mkFixMap :: [Name] -> [(OccName, Fixity)] -> FixMap
@@ -349,88 +345,40 @@ availExportItem :: Bool -- is it a signature
349345
-> AvailInfo
350346
-> ErrMsgGhc [ExportItem GhcRn]
351347
availExportItem is_sig modMap thisMod semMod warnings exportedNames
352-
(docMap, argMap, declMap, _) fixMap splices instIfaceMap
348+
(docMap, argMap, _) fixMap splices instIfaceMap
353349
availInfo = declWith availInfo
354350
where
355351
declWith :: AvailInfo -> ErrMsgGhc [ ExportItem GhcRn ]
356352
declWith avail = do
357353
dflags <- getDynFlags
358354
let t = availName avail -- NB: 't' might not be in the scope of 'avail'.
359355
-- Example: @data C = D@, where C isn't exported.
360-
r <- findDecl avail
361-
case r of
362-
([L l (ValD _ _)], (doc, _)) -> do
363-
-- Top-level binding without type signature
364-
export <- hiValExportItem t l doc (l `elem` splices) $ M.lookup t fixMap
365-
return [export]
366-
(ds, docs_) | decl : _ <- filter (not . isValD . unLoc) ds ->
367-
let declNames = getMainDeclBinder (unL decl)
368-
in case () of
369-
_
370-
-- We should not show a subordinate by itself if any of its
371-
-- parents is also exported. See note [1].
372-
| t `notElem` declNames,
373-
Just p <- find isExported (parents t $ unL decl) ->
374-
do liftErrMsg $ tell [
375-
"Warning: " ++ moduleString thisMod ++ ": " ++
376-
pretty dflags (nameOccName t) ++ " is exported separately but " ++
377-
"will be documented under " ++ pretty dflags (nameOccName p) ++
378-
". Consider exporting it together with its parent(s)" ++
379-
" for code clarity." ]
380-
return []
381-
382-
-- normal case
383-
| otherwise -> case decl of
384-
-- A single signature might refer to many names, but we
385-
-- create an export item for a single name only. So we
386-
-- modify the signature to contain only that single name.
387-
L loc (SigD _ sig) ->
388-
case filterSigNames (== t) sig of
389-
Nothing -> do
390-
liftErrMsg $ tell [
391-
"Warning: " ++ moduleString thisMod ++ ": " ++
392-
pretty dflags sig ++ " doesn't contain " ++ pretty dflags t ++
393-
". Names in the signature: " ++ pretty dflags (sigNameNoLoc sig)]
394-
pure []
395-
Just sig' ->
396-
availExportDecl avail (L loc (SigD noExt sig')) docs_
397-
L loc (TyClD _ cl@ClassDecl{}) -> do
398-
mdef <- liftGhcToErrMsgGhc $ minimalDef t
399-
let sig = maybeToList $ fmap (noLoc . MinimalSig noExt NoSourceText . noLoc . fmap noLoc) mdef
400-
availExportDecl avail
401-
(L loc $ TyClD noExt cl { tcdSigs = sig ++ tcdSigs cl }) docs_
402-
403-
_ -> availExportDecl avail decl docs_
404-
405-
([], _) -> do
406-
mayDecl <- hiDecl t
407-
case mayDecl of
408-
Nothing -> return [ ExportNoDecl t [] ]
409-
Just decl -> do
410-
docs_ <- do
411-
let tmod = nameModule t
412-
if tmod == thisMod
413-
then pure (lookupDocs avail warnings docMap argMap)
414-
else case M.lookup tmod modMap of
415-
Just iface ->
416-
pure (lookupDocs avail warnings (ifaceDocMap iface) (ifaceArgMap iface))
417-
Nothing ->
418-
-- We try to get the subs and docs
419-
-- from the installed .haddock file for that package.
420-
-- TODO: This needs to be more sophisticated to deal
421-
-- with signature inheritance
422-
case M.lookup (nameModule t) instIfaceMap of
423-
Nothing -> do
424-
liftErrMsg $ tell
425-
["Warning: " ++ pretty dflags thisMod ++
426-
": Couldn't find .haddock for export " ++ pretty dflags t]
427-
let subs_ = availNoDocs avail
428-
pure (noDocForDecl, subs_)
429-
Just instIface ->
430-
pure (lookupDocs avail warnings (instDocMap instIface) (instArgMap instIface))
431-
availExportDecl avail decl docs_
432-
433-
_ -> return []
356+
mayDecl <- hiDecl t
357+
case mayDecl of
358+
Nothing -> return [ ExportNoDecl t [] ]
359+
Just decl -> do
360+
docs_ <- do
361+
let tmod = nameModule t
362+
if tmod == thisMod
363+
then pure (lookupDocs avail warnings docMap argMap)
364+
else case M.lookup tmod modMap of
365+
Just iface ->
366+
pure (lookupDocs avail warnings (ifaceDocMap iface) (ifaceArgMap iface))
367+
Nothing ->
368+
-- We try to get the subs and docs
369+
-- from the installed .haddock file for that package.
370+
-- TODO: This needs to be more sophisticated to deal
371+
-- with signature inheritance
372+
case M.lookup (nameModule t) instIfaceMap of
373+
Nothing -> do
374+
liftErrMsg $ tell
375+
["Warning: " ++ pretty dflags thisMod ++
376+
": Couldn't find .haddock for export " ++ pretty dflags t]
377+
let subs_ = availNoDocs avail
378+
pure (noDocForDecl, subs_)
379+
Just instIface ->
380+
pure (lookupDocs avail warnings (instDocMap instIface) (instArgMap instIface))
381+
availExportDecl avail decl docs_
434382

435383
availExportDecl :: AvailInfo -> LHsDecl GhcRn
436384
-> (DocForDecl Name, [(Name, DocForDecl Name)])
@@ -478,38 +426,6 @@ availExportItem is_sig modMap thisMod semMod warnings exportedNames
478426
})
479427
in traverse extractSub subs
480428

481-
exportedNameSet = mkNameSet exportedNames
482-
isExported n = elemNameSet n exportedNameSet
483-
484-
findDecl :: AvailInfo -> ErrMsgGhc ([LHsDecl GhcRn], (DocForDecl Name, [(Name, DocForDecl Name)]))
485-
findDecl avail
486-
| m == semMod =
487-
case M.lookup n declMap of
488-
Just ds -> return (ds, lookupDocs avail warnings docMap argMap)
489-
Nothing
490-
| is_sig -> do
491-
-- OK, so it wasn't in the local declaration map. It could
492-
-- have been inherited from a signature. Reconstitute it
493-
-- from the type.
494-
mb_r <- hiDecl n
495-
case mb_r of
496-
Nothing -> return ([], (noDocForDecl, availNoDocs avail))
497-
-- TODO: If we try harder, we might be able to find
498-
-- a Haddock! Look in the Haddocks for each thing in
499-
-- requirementContext (pkgState)
500-
Just decl -> return ([decl], (noDocForDecl, availNoDocs avail))
501-
| otherwise ->
502-
return ([], (noDocForDecl, availNoDocs avail))
503-
| Just iface <- M.lookup (semToIdMod (moduleUnitId thisMod) m) modMap
504-
, Just ds <- M.lookup n (ifaceDeclMap iface) =
505-
return (ds, lookupDocs avail warnings
506-
(ifaceDocMap iface)
507-
(ifaceArgMap iface))
508-
| otherwise = return ([], (noDocForDecl, availNoDocs avail))
509-
where
510-
n = availName avail
511-
m = nameModule n
512-
513429
findBundledPatterns :: AvailInfo -> ErrMsgGhc [(HsDecl GhcRn, DocForDecl Name)]
514430
findBundledPatterns avail = do
515431
patsyns <- for constructor_names $ \name -> do
@@ -544,13 +460,6 @@ availNoDocs :: AvailInfo -> [(Name, DocForDecl Name)]
544460
availNoDocs avail =
545461
zip (availSubordinates avail) (repeat noDocForDecl)
546462

547-
-- | Given a 'Module' from a 'Name', convert it into a 'Module' that
548-
-- we can actually find in the 'IfaceMap'.
549-
semToIdMod :: UnitId -> Module -> Module
550-
semToIdMod this_uid m
551-
| Module.isHoleModule m = mkModule this_uid (moduleName m)
552-
| otherwise = m
553-
554463
hiDecl :: Name -> ErrMsgGhc (Maybe (LHsDecl GhcRn))
555464
hiDecl t = do
556465
dflags <- getDynFlags
@@ -569,24 +478,6 @@ hiDecl t = do
569478
O.comma O.<+> O.quotes (O.ppr t) O.<+>
570479
O.text "-- Please report this on Haddock issue tracker!"
571480

572-
-- | This function is called for top-level bindings without type signatures.
573-
-- It gets the type signature from GHC and that means it's not going to
574-
-- have a meaningful 'SrcSpan'. So we pass down 'SrcSpan' for the
575-
-- declaration and use it instead - 'nLoc' here.
576-
hiValExportItem :: Name -> SrcSpan -> DocForDecl Name -> Bool
577-
-> Maybe Fixity -> ErrMsgGhc (ExportItem GhcRn)
578-
hiValExportItem name nLoc doc splice fixity = do
579-
mayDecl <- hiDecl name
580-
case mayDecl of
581-
Nothing -> return (ExportNoDecl name [])
582-
Just decl -> return (ExportDecl (fixSpan decl) [] doc [] [] fixities splice)
583-
where
584-
fixSpan (L l t) = L (SrcLoc.combineSrcSpans l nLoc) t
585-
fixities = case fixity of
586-
Just f -> [(name, f)]
587-
Nothing -> []
588-
589-
590481
-- | Lookup docs for a declaration from maps.
591482
lookupDocs :: AvailInfo -> WarningMap -> DocMap Name -> ArgMap Name
592483
-> (DocForDecl Name, [(Name, DocForDecl Name)])
@@ -761,7 +652,7 @@ pruneExportItems = filter hasDoc
761652

762653

763654
mkVisibleNames :: Maps -> [ExportItem GhcRn] -> [DocOption] -> [Name]
764-
mkVisibleNames (_, _, _, instMap) exports opts
655+
mkVisibleNames (_, _, instMap) exports opts
765656
| OptHide `elem` opts = []
766657
| otherwise = let ns = concatMap exportName exports
767658
in seqList ns `seq` ns

haddock-api/src/Haddock/Types.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ type InstIfaceMap = Map Module InstalledInterface -- TODO: rename
5858
type DocMap a = Map Name (MDoc a)
5959
type ArgMap a = Map Name (Map Int (MDoc a))
6060
type SubMap = Map Name [Name]
61-
type DeclMap = Map Name [LHsDecl GhcRn]
6261
type InstMap = Map SrcSpan Name
6362
type FixMap = Map Name Fixity
6463
type DocPaths = (FilePath, Maybe FilePath) -- paths to HTML and sources

0 commit comments

Comments
 (0)