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

Commit b93c635

Browse files
alexbiehl-gcalexbiehl
authored andcommitted
Abstract Monad for interface creation
I found that when running as a plugin the lookupName function (which runs in Ghc monad) does not work correctly from the typeCheckResultAction hook. Instead, we abstracted the monad used when creating interfaces, so that access to GHC session specific parts is explicit and so that the TcM can provide their (correct) implementation of lookupName.
1 parent f4672fa commit b93c635

21 files changed

+224
-313
lines changed

haddock-api/haddock-api.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ library
6060
, exceptions
6161
, filepath
6262
, ghc-boot
63+
, mtl
6364
, transformers
6465

6566
hs-source-dirs: src
@@ -192,6 +193,7 @@ test-suite spec
192193
, exceptions
193194
, filepath
194195
, ghc-boot
196+
, mtl
195197
, transformers
196198

197199
build-tool-depends:

haddock-api/src/Haddock/GhcUtils.hs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ import GHC.Utils.Outputable ( Outputable, panic, showPpr )
3030
import GHC.Types.Basic (PromotionFlag(..))
3131
import GHC.Types.Name
3232
import GHC.Unit.Module
33-
import GHC.Driver.Types
3433
import GHC
35-
import GHC.Core.Class
3634
import GHC.Driver.Session
3735
import GHC.Types.SrcLoc ( advanceSrcLoc )
3836
import GHC.Types.Var ( Specificity, VarBndr(..), TyVarBinder
@@ -526,14 +524,6 @@ modifySessionDynFlags f = do
526524
return ()
527525

528526

529-
-- Extract the minimal complete definition of a Name, if one exists
530-
minimalDef :: GhcMonad m => Name -> m (Maybe ClassMinimalDef)
531-
minimalDef n = do
532-
mty <- lookupGlobalName n
533-
case mty of
534-
Just (ATyCon (tyConClass_maybe -> Just c)) -> return . Just $ classMinimalDef c
535-
_ -> return Nothing
536-
537527
-------------------------------------------------------------------------------
538528
-- * DynFlags
539529
-------------------------------------------------------------------------------
@@ -766,4 +756,3 @@ defaultRuntimeRepVars = go emptyVarEnv
766756

767757
go _ ty@(LitTy {}) = ty
768758
go _ ty@(CoercionTy {}) = ty
769-

haddock-api/src/Haddock/Interface.hs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ import GHC hiding (verbosity)
5555
import GHC.Data.Graph.Directed
5656
import GHC.Driver.Session hiding (verbosity)
5757
import GHC.Driver.Types (isBootSummary)
58-
import GHC.Driver.Monad (Session(..), modifySession, reflectGhc)
58+
import GHC.Driver.Monad (modifySession)
5959
import GHC.Data.FastString (unpackFS)
60-
import GHC.Tc.Types (TcGblEnv(..))
61-
import GHC.Tc.Utils.Monad (getTopEnv)
60+
import GHC.Tc.Types (TcM, TcGblEnv(..))
61+
import GHC.Tc.Utils.Monad (setGblEnv)
62+
import GHC.Tc.Utils.Env (tcLookupGlobal)
6263
import GHC.Types.Name (nameIsFromExternalPackage, nameOccName)
6364
import GHC.Types.Name.Occurrence (isTcOcc)
6465
import GHC.Types.Name.Reader (unQualOK, gre_name, globalRdrEnvElts)
@@ -200,7 +201,7 @@ plugin verbosity flags instIfaceMap = liftIO $ do
200201
moduleSetRef <- newIORef emptyModuleSet
201202

202203
let
203-
processTypeCheckedResult :: ModSummary -> TcGblEnv -> Ghc ()
204+
processTypeCheckedResult :: ModSummary -> TcGblEnv -> TcM ()
204205
processTypeCheckedResult mod_summary tc_gbl_env
205206
-- Don't do anything for hs-boot modules
206207
| IsBoot <- isBootSummary mod_summary =
@@ -225,11 +226,8 @@ plugin verbosity flags instIfaceMap = liftIO $ do
225226
paPlugin = defaultPlugin
226227
{
227228
renamedResultAction = keepRenamedSource
228-
, typeCheckResultAction = \_ mod_summary tc_gbl_env -> do
229-
session <- getTopEnv >>= liftIO . newIORef
230-
liftIO $ reflectGhc
231-
(processTypeCheckedResult mod_summary tc_gbl_env)
232-
(Session session)
229+
, typeCheckResultAction = \_ mod_summary tc_gbl_env -> setGblEnv tc_gbl_env $ do
230+
processTypeCheckedResult mod_summary tc_gbl_env
233231
pure tc_gbl_env
234232

235233
}
@@ -244,31 +242,28 @@ plugin verbosity flags instIfaceMap = liftIO $ do
244242
)
245243

246244

247-
248245
processModule1
249246
:: Verbosity
250247
-> [Flag]
251248
-> IfaceMap
252249
-> InstIfaceMap
253250
-> ModSummary
254251
-> TcGblEnv
255-
-> Ghc (Interface, ModuleSet)
252+
-> TcM (Interface, ModuleSet)
256253
processModule1 verbosity flags ifaces inst_ifaces mod_summary tc_gbl_env = do
257254
out verbosity verbose "Creating interface..."
258255

259256
let
260257
TcGblEnv { tcg_rdr_env } = tc_gbl_env
261258

262259
(!interface, messages) <- {-# SCC createInterface #-}
263-
withTimingD "createInterface" (const ()) $
264-
runWriterGhc $ createInterface1 flags mod_summary
265-
tc_gbl_env ifaces inst_ifaces
260+
withTimingD "createInterface" (const ()) $ runIfM (fmap Just . tcLookupGlobal) $
261+
createInterface1 flags mod_summary tc_gbl_env ifaces inst_ifaces
266262

267263
-- We need to keep track of which modules were somehow in scope so that when
268264
-- Haddock later looks for instances, it also looks in these modules too.
269265
--
270266
-- See https://github.com/haskell/haddock/issues/469.
271-
272267
dflags <- getDynFlags
273268
let
274269
mods :: ModuleSet

0 commit comments

Comments
 (0)