@@ -30,6 +30,7 @@ import Haddock.Backends.Hyperlinker.Types
30
30
import Haddock.Backends.Hyperlinker.Ast as Hyperlinker
31
31
import Haddock.Backends.Hyperlinker.Parser as Hyperlinker
32
32
33
+ import Data.Bitraversable
33
34
import qualified Data.ByteString as BS
34
35
import qualified Data.Map as M
35
36
import Data.Map (Map )
@@ -38,8 +39,6 @@ import Data.Maybe
38
39
import Data.Monoid
39
40
import Data.Ord
40
41
import Control.Applicative
41
- import Control.Arrow (second )
42
- import Control.DeepSeq (force )
43
42
import Control.Exception (evaluate )
44
43
import Control.Monad
45
44
import Data.Traversable
@@ -109,7 +108,6 @@ createInterface tm flags modMap instIfaceMap = do
109
108
exports
110
109
| OptIgnoreExports `elem` opts = Nothing
111
110
| otherwise = exports0
112
- warningMap = mkWarningMap dflags warnings gre exportedNames
113
111
114
112
localBundledPatSyns :: Map Name [Name ]
115
113
localBundledPatSyns =
@@ -134,8 +132,10 @@ createInterface tm flags modMap instIfaceMap = do
134
132
-- Locations of all TH splices
135
133
splices = [ l | L l (SpliceD _) <- hsmodDecls hsm ]
136
134
137
- maps@ (! docMap, ! argMap, ! subMap, ! declMap, _) =
138
- mkMaps dflags gre localInsts declsWithDocs
135
+ warningMap <- liftErrMsg (mkWarningMap dflags warnings gre exportedNames)
136
+
137
+ maps@ (! docMap, ! argMap, ! subMap, ! declMap, _) <-
138
+ liftErrMsg (mkMaps dflags gre localInsts declsWithDocs)
139
139
140
140
let allWarnings = M. unions (warningMap : map ifaceWarningMap (M. elems modMap))
141
141
@@ -161,7 +161,8 @@ createInterface tm flags modMap instIfaceMap = do
161
161
162
162
let ! aliases =
163
163
mkAliasMap dflags $ tm_renamed_source tm
164
- modWarn = moduleWarning dflags gre warnings
164
+
165
+ modWarn <- liftErrMsg (moduleWarning dflags gre warnings)
165
166
166
167
tokenizedSrc <- mkMaybeTokenizedSrc flags tm
167
168
@@ -245,27 +246,29 @@ lookupModuleDyn dflags Nothing mdlName =
245
246
-- Warnings
246
247
-------------------------------------------------------------------------------
247
248
248
- mkWarningMap :: DynFlags -> Warnings -> GlobalRdrEnv -> [Name ] -> WarningMap
249
+ mkWarningMap :: DynFlags -> Warnings -> GlobalRdrEnv -> [Name ] -> ErrMsgM WarningMap
249
250
mkWarningMap dflags warnings gre exps = case warnings of
250
- NoWarnings -> M. empty
251
- WarnAll _ -> M. empty
251
+ NoWarnings -> pure M. empty
252
+ WarnAll _ -> pure M. empty
252
253
WarnSome ws ->
253
- let ws' = [ (n, w) | (occ, w) <- ws, elt <- lookupGlobalRdrEnv gre occ
254
+ let ws' = [ (n, w)
255
+ | (occ, w) <- ws
256
+ , elt <- lookupGlobalRdrEnv gre occ
254
257
, let n = gre_name elt, n `elem` exps ]
255
- in M. fromList $ map (second $ parseWarning dflags gre) ws'
258
+ in M. fromList <$> traverse (bitraverse pure ( parseWarning dflags gre) ) ws'
256
259
257
- moduleWarning :: DynFlags -> GlobalRdrEnv -> Warnings -> Maybe (Doc Name )
258
- moduleWarning _ _ NoWarnings = Nothing
259
- moduleWarning _ _ (WarnSome _) = Nothing
260
- moduleWarning dflags gre (WarnAll w) = Just $ parseWarning dflags gre w
260
+ moduleWarning :: DynFlags -> GlobalRdrEnv -> Warnings -> ErrMsgM ( Maybe (Doc Name ) )
261
+ moduleWarning _ _ NoWarnings = pure Nothing
262
+ moduleWarning _ _ (WarnSome _) = pure Nothing
263
+ moduleWarning dflags gre (WarnAll w) = Just <$> parseWarning dflags gre w
261
264
262
- parseWarning :: DynFlags -> GlobalRdrEnv -> WarningTxt -> Doc Name
263
- parseWarning dflags gre w = force $ case w of
265
+ parseWarning :: DynFlags -> GlobalRdrEnv -> WarningTxt -> ErrMsgM ( Doc Name )
266
+ parseWarning dflags gre w = case w of
264
267
DeprecatedTxt _ msg -> format " Deprecated: " (concatFS $ map (sl_fs . unLoc) msg)
265
268
WarningTxt _ msg -> format " Warning: " (concatFS $ map (sl_fs . unLoc) msg)
266
269
where
267
270
format x xs = DocWarning . DocParagraph . DocAppend (DocString x)
268
- . processDocString dflags gre $ HsDocString xs
271
+ <$> processDocString dflags gre ( HsDocString xs)
269
272
270
273
271
274
-------------------------------------------------------------------------------
@@ -313,16 +316,15 @@ mkMaps :: DynFlags
313
316
-> GlobalRdrEnv
314
317
-> [Name ]
315
318
-> [(LHsDecl Name , [HsDocString ])]
316
- -> Maps
317
- mkMaps dflags gre instances decls =
318
- let
319
- (a, b, c, d) = unzip4 $ map mappings decls
320
- in ( f' (map (nubByName fst ) a)
321
- , f (filterMapping (not . M. null ) b)
322
- , f (filterMapping (not . null ) c)
323
- , f (filterMapping (not . null ) d)
324
- , instanceMap
325
- )
319
+ -> ErrMsgM Maps
320
+ mkMaps dflags gre instances decls = do
321
+ (a, b, c, d) <- unzip4 <$> traverse mappings decls
322
+ pure ( f' (map (nubByName fst ) a)
323
+ , f (filterMapping (not . M. null ) b)
324
+ , f (filterMapping (not . null ) c)
325
+ , f (filterMapping (not . null ) d)
326
+ , instanceMap
327
+ )
326
328
where
327
329
f :: (Ord a , Monoid b ) => [[(a , b )]] -> Map a b
328
330
f = M. fromListWith (<>) . concat
@@ -334,35 +336,42 @@ mkMaps dflags gre instances decls =
334
336
filterMapping p = map (filter (p . snd ))
335
337
336
338
mappings :: (LHsDecl Name , [HsDocString ])
337
- -> ( [(Name , MDoc Name )]
338
- , [(Name , Map Int (MDoc Name ))]
339
- , [(Name , [Name ])]
340
- , [(Name , [LHsDecl Name ])]
341
- )
342
- mappings (ldecl, docStrs) =
339
+ -> ErrMsgM ( [(Name , MDoc Name )]
340
+ , [(Name , Map Int (MDoc Name ))]
341
+ , [(Name , [Name ])]
342
+ , [(Name , [LHsDecl Name ])]
343
+ )
344
+ mappings (ldecl, docStrs) = do
343
345
let L l decl = ldecl
344
346
declDoc :: [HsDocString ] -> Map Int HsDocString
345
- -> (Maybe (MDoc Name ), Map Int (MDoc Name ))
346
- declDoc strs m =
347
- let doc' = processDocStrings dflags gre strs
348
- m' = M. map (processDocStringParas dflags gre) m
349
- in (doc', m')
350
- (doc, args) = declDoc docStrs (typeDocs decl)
347
+ -> ErrMsgM (Maybe (MDoc Name ), Map Int (MDoc Name ))
348
+ declDoc strs m = do
349
+ doc' <- processDocStrings dflags gre strs
350
+ m' <- traverse (processDocStringParas dflags gre) m
351
+ pure (doc', m')
352
+
353
+ (doc, args) <- declDoc docStrs (typeDocs decl)
354
+
355
+ let
351
356
subs :: [(Name , [HsDocString ], Map Int HsDocString )]
352
357
subs = subordinates instanceMap decl
353
- (subDocs, subArgs) = unzip $ map (\ (_, strs, m) -> declDoc strs m) subs
358
+
359
+ (subDocs, subArgs) <- unzip <$> traverse (\ (_, strs, m) -> declDoc strs m) subs
360
+
361
+ let
354
362
ns = names l decl
355
363
subNs = [ n | (n, _, _) <- subs ]
356
364
dm = [ (n, d) | (n, Just d) <- zip ns (repeat doc) ++ zip subNs subDocs ]
357
365
am = [ (n, args) | n <- ns ] ++ zip subNs subArgs
358
366
sm = [ (n, subNs) | n <- ns ]
359
367
cm = [ (n, [ldecl]) | n <- ns ++ subNs ]
360
- in seqList ns `seq`
361
- seqList subNs `seq`
362
- doc `seq`
363
- seqList subDocs `seq`
364
- seqList subArgs `seq`
365
- (dm, am, sm, cm)
368
+
369
+ seqList ns `seq`
370
+ seqList subNs `seq`
371
+ doc `seq`
372
+ seqList subDocs `seq`
373
+ seqList subArgs `seq`
374
+ pure (dm, am, sm, cm)
366
375
367
376
instanceMap :: Map SrcSpan Name
368
377
instanceMap = M. fromList [ (getSrcSpan n, n) | n <- instances ]
@@ -602,16 +611,20 @@ mkExportItems
602
611
-- do so.
603
612
-- NB: Pass in identity module, so we can look it up in index correctly
604
613
moduleExports thisMod m dflags warnings gre exportedNames decls modMap instIfaceMap maps fixMap splices
605
- lookupExport (IEGroup lev docStr) = return $
606
- return . ExportGroup lev " " $ processDocString dflags gre docStr
614
+ lookupExport (IEGroup lev docStr) = liftErrMsg $ do
615
+ doc <- processDocString dflags gre docStr
616
+ return [ExportGroup lev " " doc]
607
617
608
- lookupExport (IEDoc docStr) = return $
609
- return . ExportDoc $ processDocStringParas dflags gre docStr
618
+ lookupExport (IEDoc docStr) = liftErrMsg $ do
619
+ doc <- processDocStringParas dflags gre docStr
620
+ return [ExportDoc doc]
610
621
611
622
lookupExport (IEDocNamed str) = liftErrMsg $
612
- findNamedDoc str [ unL d | d <- decls ] >>= return . \ case
613
- Nothing -> []
614
- Just doc -> return . ExportDoc $ processDocStringParas dflags gre doc
623
+ findNamedDoc str [ unL d | d <- decls ] >>= \ case
624
+ Nothing -> return []
625
+ Just docStr -> do
626
+ doc <- processDocStringParas dflags gre docStr
627
+ return [ExportDoc doc]
615
628
616
629
declWith :: [(HsDecl Name , DocForDecl Name )] -> Name -> ErrMsgGhc [ ExportItem Name ]
617
630
declWith pats t = do
@@ -924,9 +937,11 @@ fullModuleContents dflags warnings gre (docMap, argMap, subMap, declMap, instMap
924
937
925
938
mkExportItem :: LHsDecl Name -> ErrMsgGhc (Maybe (ExportItem Name ))
926
939
mkExportItem (L _ (DocD (DocGroup lev docStr))) = do
927
- return . Just . ExportGroup lev " " $ processDocString dflags gre docStr
940
+ doc <- liftErrMsg (processDocString dflags gre docStr)
941
+ return . Just . ExportGroup lev " " $ doc
928
942
mkExportItem (L _ (DocD (DocCommentNamed _ docStr))) = do
929
- return . Just . ExportDoc $ processDocStringParas dflags gre docStr
943
+ doc <- liftErrMsg (processDocStringParas dflags gre docStr)
944
+ return . Just . ExportDoc $ doc
930
945
mkExportItem (L l (ValD d))
931
946
| name: _ <- collectHsBindBinders d, Just [L _ (ValD _)] <- M. lookup name declMap =
932
947
-- Top-level binding without type signature.
0 commit comments