Skip to content

Commit d76fbf9

Browse files
authored
simplify things unnecessarily running in GhcM (#875)
* simplify things unnecessarily running in GhcM * untick catchSrcErrors * set useUnicode
1 parent e2ee58f commit d76fbf9

File tree

12 files changed

+107
-163
lines changed

12 files changed

+107
-163
lines changed

ghcide.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ library
175175
Development.IDE.GHC.CPP
176176
Development.IDE.GHC.Orphans
177177
Development.IDE.GHC.Warnings
178-
Development.IDE.GHC.WithDynFlags
179178
Development.IDE.Import.FindImports
180179
Development.IDE.LSP.Notifications
181180
Development.IDE.Spans.Documentation

session-loader/Development/IDE/Session.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ emptyHscEnv :: IORef NameCache -> FilePath -> IO HscEnv
371371
emptyHscEnv nc libDir = do
372372
env <- runGhc (Just libDir) getSession
373373
initDynLinker env
374-
pure $ setNameCache nc env
374+
pure $ setNameCache nc env{ hsc_dflags = (hsc_dflags env){useUnicode = True } }
375375

376376
data TargetDetails = TargetDetails
377377
{

src/Development/IDE/Core/Compile.hs

Lines changed: 61 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ import System.FilePath
8989
import System.Directory
9090
import System.IO.Extra
9191
import Control.Exception (evaluate)
92-
import Exception (ExceptionMonad)
9392
import TcEnv (tcLookup)
9493
import Data.Time (UTCTime, getCurrentTime)
9594
import Linker (unload)
@@ -105,7 +104,7 @@ parseModule
105104
-> IO (IdeResult (StringBuffer, ParsedModule))
106105
parseModule IdeOptions{..} env comp_pkgs filename modTime mbContents =
107106
fmap (either (, Nothing) id) $
108-
evalGhcEnv env $ runExceptT $ do
107+
runExceptT $ do
109108
(contents, dflags) <- preprocessor env filename mbContents
110109
(diag, modu) <- parseFileContents env optPreprocessor dflags comp_pkgs filename modTime contents
111110
return (diag, Just (contents, modu))
@@ -127,39 +126,37 @@ typecheckModule :: IdeDefer
127126
-> HscEnv
128127
-> Maybe [Linkable] -- ^ linkables not to unload, if Nothing don't unload anything
129128
-> ParsedModule
130-
-> IO (IdeResult (HscEnv, TcModuleResult))
129+
-> IO (IdeResult TcModuleResult)
131130
typecheckModule (IdeDefer defer) hsc keep_lbls pm = do
132-
fmap (\(hsc, res) -> case res of Left d -> (d,Nothing); Right (d,res) -> (d,fmap (hsc,) res)) $
133-
runGhcEnv hsc $
134-
catchSrcErrors "typecheck" $ do
131+
fmap (either (,Nothing) id) $
132+
catchSrcErrors (hsc_dflags hsc) "typecheck" $ do
135133

136134
let modSummary = pm_mod_summary pm
137135
dflags = ms_hspp_opts modSummary
138136

139-
modSummary' <- initPlugins modSummary
137+
modSummary' <- initPlugins hsc modSummary
140138
(warnings, tcm) <- withWarnings "typecheck" $ \tweak ->
141-
tcRnModule keep_lbls $ enableTopLevelWarnings
142-
$ enableUnnecessaryAndDeprecationWarnings
143-
$ demoteIfDefer pm{pm_mod_summary = tweak modSummary'}
139+
tcRnModule hsc keep_lbls $ enableTopLevelWarnings
140+
$ enableUnnecessaryAndDeprecationWarnings
141+
$ demoteIfDefer pm{pm_mod_summary = tweak modSummary'}
144142
let errorPipeline = unDefer . hideDiag dflags . tagDiag
145143
diags = map errorPipeline warnings
146144
deferedError = any fst diags
147145
return (map snd diags, Just $ tcm{tmrDeferedError = deferedError})
148146
where
149147
demoteIfDefer = if defer then demoteTypeErrorsToWarnings else id
150148

151-
tcRnModule :: GhcMonad m => Maybe [Linkable] -> ParsedModule -> m TcModuleResult
152-
tcRnModule keep_lbls pmod = do
149+
tcRnModule :: HscEnv -> Maybe [Linkable] -> ParsedModule -> IO TcModuleResult
150+
tcRnModule hsc_env keep_lbls pmod = do
153151
let ms = pm_mod_summary pmod
154-
hsc_env <- getSession
155-
let hsc_env_tmp = hsc_env { hsc_dflags = ms_hspp_opts ms }
156-
(tc_gbl_env, mrn_info)
157-
<- liftIO $ do
158-
whenJust keep_lbls $ unload hsc_env_tmp
159-
hscTypecheckRename hsc_env_tmp ms $
160-
HsParsedModule { hpm_module = parsedSource pmod,
161-
hpm_src_files = pm_extra_src_files pmod,
162-
hpm_annotations = pm_annotations pmod }
152+
hsc_env_tmp = hsc_env { hsc_dflags = ms_hspp_opts ms }
153+
154+
whenJust keep_lbls $ unload hsc_env_tmp
155+
(tc_gbl_env, mrn_info) <-
156+
hscTypecheckRename hsc_env_tmp ms $
157+
HsParsedModule { hpm_module = parsedSource pmod,
158+
hpm_src_files = pm_extra_src_files pmod,
159+
hpm_annotations = pm_annotations pmod }
163160
let rn_info = case mrn_info of
164161
Just x -> x
165162
Nothing -> error "no renamed info tcRnModule"
@@ -215,9 +212,8 @@ mkHiFileResultCompile session' tcm simplified_guts ltype = catchErrs $ do
215212
. (("Error during " ++ T.unpack source) ++) . show @SomeException
216213
]
217214

218-
initPlugins :: GhcMonad m => ModSummary -> m ModSummary
219-
initPlugins modSummary = do
220-
session <- getSession
215+
initPlugins :: HscEnv -> ModSummary -> IO ModSummary
216+
initPlugins session modSummary = do
221217
dflags <- liftIO $ initializePlugins session $ ms_hspp_opts modSummary
222218
return modSummary{ms_hspp_opts = dflags}
223219

@@ -235,40 +231,37 @@ compileModule
235231
-> ModSummary
236232
-> TcGblEnv
237233
-> IO (IdeResult ModGuts)
238-
compileModule (RunSimplifier simplify) packageState ms tcg =
234+
compileModule (RunSimplifier simplify) session ms tcg =
239235
fmap (either (, Nothing) (second Just)) $
240-
evalGhcEnv packageState $
241-
catchSrcErrors "compile" $ do
242-
session <- getSession
243-
(warnings,desugar) <- withWarnings "compile" $ \tweak -> do
236+
catchSrcErrors (hsc_dflags session) "compile" $ do
237+
(warnings,desugared_guts) <- withWarnings "compile" $ \tweak -> do
244238
let ms' = tweak ms
245-
liftIO $ hscDesugar session{ hsc_dflags = ms_hspp_opts ms'} ms' tcg
246-
desugared_guts <-
247-
if simplify
248-
then do
249-
plugins <- liftIO $ readIORef (tcg_th_coreplugins tcg)
250-
liftIO $ hscSimplify session plugins desugar
251-
else pure desugar
239+
session' = session{ hsc_dflags = ms_hspp_opts ms'}
240+
desugar <- hscDesugar session' ms' tcg
241+
if simplify
242+
then do
243+
plugins <- readIORef (tcg_th_coreplugins tcg)
244+
hscSimplify session' plugins desugar
245+
else pure desugar
252246
return (map snd warnings, desugared_guts)
253247

254248
generateObjectCode :: HscEnv -> ModSummary -> CgGuts -> IO (IdeResult Linkable)
255-
generateObjectCode hscEnv summary guts = do
249+
generateObjectCode session summary guts = do
256250
fmap (either (, Nothing) (second Just)) $
257-
evalGhcEnv hscEnv $
258-
catchSrcErrors "object" $ do
259-
session <- getSession
251+
catchSrcErrors (hsc_dflags session) "object" $ do
260252
let dot_o = ml_obj_file (ms_location summary)
261253
mod = ms_mod summary
262-
session' = session { hsc_dflags = (hsc_dflags session) { outputFile = Just dot_o }}
263254
fp = replaceExtension dot_o "s"
264-
liftIO $ createDirectoryIfMissing True (takeDirectory fp)
255+
createDirectoryIfMissing True (takeDirectory fp)
265256
(warnings, dot_o_fp) <-
266-
withWarnings "object" $ \_tweak -> liftIO $ do
257+
withWarnings "object" $ \_tweak -> do
258+
let summary' = _tweak summary
259+
session' = session { hsc_dflags = (ms_hspp_opts summary') { outputFile = Just dot_o }}
267260
(outputFilename, _mStub, _foreign_files) <- hscGenHardCode session' guts
268261
#if MIN_GHC_API_VERSION(8,10,0)
269-
(ms_location summary)
262+
(ms_location summary')
270263
#else
271-
(_tweak summary)
264+
summary'
272265
#endif
273266
fp
274267
compileFile session' StopLn (outputFilename, Just (As False))
@@ -282,16 +275,16 @@ generateObjectCode hscEnv summary guts = do
282275
generateByteCode :: HscEnv -> ModSummary -> CgGuts -> IO (IdeResult Linkable)
283276
generateByteCode hscEnv summary guts = do
284277
fmap (either (, Nothing) (second Just)) $
285-
evalGhcEnv hscEnv $
286-
catchSrcErrors "bytecode" $ do
287-
session <- getSession
278+
catchSrcErrors (hsc_dflags hscEnv) "bytecode" $ do
288279
(warnings, (_, bytecode, sptEntries)) <-
289-
withWarnings "bytecode" $ \_tweak -> liftIO $
280+
withWarnings "bytecode" $ \_tweak -> do
281+
let summary' = _tweak summary
282+
session = hscEnv { hsc_dflags = ms_hspp_opts summary' }
290283
hscInteractive session guts
291284
#if MIN_GHC_API_VERSION(8,10,0)
292-
(ms_location summary)
285+
(ms_location summary')
293286
#else
294-
(_tweak summary)
287+
summary'
295288
#endif
296289
let unlinked = BCOs bytecode sptEntries
297290
time <- liftIO getCurrentTime
@@ -510,13 +503,12 @@ withBootSuffix _ = id
510503

511504
-- | Produce a module summary from a StringBuffer.
512505
getModSummaryFromBuffer
513-
:: GhcMonad m
514-
=> FilePath
506+
:: FilePath
515507
-> UTCTime
516508
-> DynFlags
517509
-> GHC.ParsedSource
518510
-> StringBuffer
519-
-> ExceptT [FileDiagnostic] m ModSummary
511+
-> ExceptT [FileDiagnostic] IO ModSummary
520512
getModSummaryFromBuffer fp modTime dflags parsed contents = do
521513
(modName, imports) <- liftEither $ getImportsParsed dflags parsed
522514

@@ -553,12 +545,11 @@ getModSummaryFromBuffer fp modTime dflags parsed contents = do
553545
-- | Given a buffer, env and filepath, produce a module summary by parsing only the imports.
554546
-- Runs preprocessors as needed.
555547
getModSummaryFromImports
556-
:: (HasDynFlags m, ExceptionMonad m, MonadIO m)
557-
=> HscEnv
548+
:: HscEnv
558549
-> FilePath
559550
-> UTCTime
560551
-> Maybe SB.StringBuffer
561-
-> ExceptT [FileDiagnostic] m ModSummary
552+
-> ExceptT [FileDiagnostic] IO ModSummary
562553
getModSummaryFromImports env fp modTime contents = do
563554
(contents, dflags) <- preprocessor env fp contents
564555
(srcImports, textualImports, L _ moduleName) <-
@@ -595,7 +586,7 @@ getModSummaryFromImports env fp modTime contents = do
595586

596587
-- | Parse only the module header
597588
parseHeader
598-
:: GhcMonad m
589+
:: Monad m
599590
=> DynFlags -- ^ flags to use
600591
-> FilePath -- ^ the filename (for source locations)
601592
-> SB.StringBuffer -- ^ Haskell module source text (full Unicode is supported)
@@ -630,15 +621,14 @@ parseHeader dflags filename contents = do
630621
-- | Given a buffer, flags, and file path, produce a
631622
-- parsed module (or errors) and any parse warnings. Does not run any preprocessors
632623
parseFileContents
633-
:: GhcMonad m
634-
=> HscEnv
624+
:: HscEnv
635625
-> (GHC.ParsedSource -> IdePreprocessedSource)
636626
-> DynFlags -- ^ flags to use
637627
-> [PackageName] -- ^ The package imports to ignore
638628
-> FilePath -- ^ the filename (for source locations)
639629
-> UTCTime -- ^ the modification timestamp
640630
-> SB.StringBuffer -- ^ Haskell module source text (full Unicode is supported)
641-
-> ExceptT [FileDiagnostic] m ([FileDiagnostic], ParsedModule)
631+
-> ExceptT [FileDiagnostic] IO ([FileDiagnostic], ParsedModule)
642632
parseFileContents env customPreprocessor dflags comp_pkgs filename modTime contents = do
643633
let loc = mkRealSrcLoc (mkFastString filename) 1 1
644634
case unP Parser.parseModule (mkPState dflags contents loc) of
@@ -756,12 +746,12 @@ mkDetailsFromIface session iface linkable = do
756746
-- | Non-interactive, batch version of 'InteractiveEval.getDocs'.
757747
-- The interactive paths create problems in ghc-lib builds
758748
--- and leads to fun errors like "Cannot continue after interface file error".
759-
getDocsBatch :: GhcMonad m
760-
=> Module -- ^ a moudle where the names are in scope
761-
-> [Name]
762-
-> m [Either String (Maybe HsDocString, Map.Map Int HsDocString)]
763-
getDocsBatch _mod _names =
764-
withSession $ \hsc_env -> liftIO $ do
749+
getDocsBatch
750+
:: HscEnv
751+
-> Module -- ^ a moudle where the names are in scope
752+
-> [Name]
753+
-> IO [Either String (Maybe HsDocString, Map.Map Int HsDocString)]
754+
getDocsBatch hsc_env _mod _names = do
765755
((_warns,errs), res) <- initTc hsc_env HsSrcFile False _mod fakeSpan $ forM _names $ \name ->
766756
case nameModule_maybe name of
767757
Nothing -> return (Left $ NameHasNoModule name)
@@ -791,11 +781,11 @@ fakeSpan = realSrcLocSpan $ mkRealSrcLoc (fsLit "<ghcide>") 1 1
791781
-- | Non-interactive, batch version of 'InteractiveEval.lookupNames'.
792782
-- The interactive paths create problems in ghc-lib builds
793783
--- and leads to fun errors like "Cannot continue after interface file error".
794-
lookupName :: GhcMonad m
795-
=> Module -- ^ A module where the Names are in scope
784+
lookupName :: HscEnv
785+
-> Module -- ^ A module where the Names are in scope
796786
-> Name
797-
-> m (Maybe TyThing)
798-
lookupName mod name = withSession $ \hsc_env -> liftIO $ do
787+
-> IO (Maybe TyThing)
788+
lookupName hsc_env mod name = do
799789
(_messages, res) <- initTc hsc_env HsSrcFile False mod fakeSpan $ do
800790
tcthing <- tcLookup name
801791
case tcthing of

src/Development/IDE/Core/Preprocessor.hs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,17 @@ import qualified Data.Text as T
3131
import Outputable (showSDoc)
3232
import Control.DeepSeq (NFData(rnf))
3333
import Control.Exception (evaluate)
34-
import Control.Monad.IO.Class (MonadIO)
35-
import Exception (ExceptionMonad)
34+
import HscTypes (HscEnv(hsc_dflags))
3635

3736

3837
-- | Given a file and some contents, apply any necessary preprocessors,
3938
-- e.g. unlit/cpp. Return the resulting buffer and the DynFlags it implies.
40-
preprocessor :: (ExceptionMonad m, HasDynFlags m, MonadIO m) => HscEnv -> FilePath -> Maybe StringBuffer -> ExceptT [FileDiagnostic] m (StringBuffer, DynFlags)
39+
preprocessor :: HscEnv -> FilePath -> Maybe StringBuffer -> ExceptT [FileDiagnostic] IO (StringBuffer, DynFlags)
4140
preprocessor env filename mbContents = do
4241
-- Perform unlit
4342
(isOnDisk, contents) <-
4443
if isLiterate filename then do
45-
dflags <- getDynFlags
44+
let dflags = hsc_dflags env
4645
newcontent <- liftIO $ runLhs dflags filename mbContents
4746
return (False, newcontent)
4847
else do
@@ -58,7 +57,6 @@ preprocessor env filename mbContents = do
5857
else do
5958
cppLogs <- liftIO $ newIORef []
6059
contents <- ExceptT
61-
$ liftIO
6260
$ (Right <$> (runCpp dflags {log_action = logAction cppLogs} filename
6361
$ if isOnDisk then Nothing else Just contents))
6462
`catch`
@@ -133,21 +131,20 @@ isLiterate x = takeExtension x `elem` [".lhs",".lhs-boot"]
133131

134132
-- | This reads the pragma information directly from the provided buffer.
135133
parsePragmasIntoDynFlags
136-
:: (ExceptionMonad m, HasDynFlags m, MonadIO m)
137-
=> HscEnv
134+
:: HscEnv
138135
-> FilePath
139136
-> SB.StringBuffer
140-
-> m (Either [FileDiagnostic] DynFlags)
141-
parsePragmasIntoDynFlags env fp contents = catchSrcErrors "pragmas" $ do
142-
dflags0 <- getDynFlags
137+
-> IO (Either [FileDiagnostic] DynFlags)
138+
parsePragmasIntoDynFlags env fp contents = catchSrcErrors dflags0 "pragmas" $ do
143139
let opts = Hdr.getOptions dflags0 contents fp
144140

145141
-- Force bits that might keep the dflags and stringBuffer alive unnecessarily
146-
liftIO $ evaluate $ rnf opts
142+
evaluate $ rnf opts
147143

148144
(dflags, _, _) <- parseDynamicFilePragma dflags0 opts
149-
dflags' <- liftIO $ initializePlugins env dflags
145+
dflags' <- initializePlugins env dflags
150146
return $ disableWarningsAsErrors dflags'
147+
where dflags0 = hsc_dflags env
151148

152149
-- | Run (unlit) literate haskell preprocessor on a file, or buffer if set
153150
runLhs :: DynFlags -> FilePath -> Maybe SB.StringBuffer -> IO SB.StringBuffer

src/Development/IDE/Core/Rules.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ import Development.IDE.Types.Diagnostics as Diag
4949
import Development.IDE.Types.Location
5050
import Development.IDE.GHC.Compat hiding (parseModule, typecheckModule, writeHieFile, TargetModule, TargetFile)
5151
import Development.IDE.GHC.Util
52-
import Development.IDE.GHC.WithDynFlags
5352
import Data.Either.Extra
5453
import qualified Development.IDE.Types.Logger as L
5554
import Data.Maybe
@@ -575,7 +574,7 @@ getDocMapRule =
575574
parsedDeps <- uses_ GetParsedModule tdeps
576575
#endif
577576

578-
dkMap <- liftIO $ evalGhcEnv hsc $ mkDocMap parsedDeps rf tc
577+
dkMap <- liftIO $ mkDocMap hsc parsedDeps rf tc
579578
return ([],Just dkMap)
580579

581580
-- Typechecks a module.
@@ -611,7 +610,7 @@ typeCheckRuleDefinition hsc pm = do
611610

612611
linkables_to_keep <- currentLinkables
613612

614-
addUsageDependencies $ fmap (second (fmap snd)) $ liftIO $
613+
addUsageDependencies $ liftIO $
615614
typecheckModule defer hsc (Just linkables_to_keep) pm
616615
where
617616
addUsageDependencies :: Action (a, Maybe TcModuleResult) -> Action (a, Maybe TcModuleResult)
@@ -746,7 +745,7 @@ getModSummaryRule = do
746745
let dflags = hsc_dflags session
747746
(modTime, mFileContent) <- getFileContents f
748747
let fp = fromNormalizedFilePath f
749-
modS <- liftIO $ evalWithDynFlags dflags $ runExceptT $
748+
modS <- liftIO $ runExceptT $
750749
getModSummaryFromImports session fp modTime (textToStringBuffer <$> mFileContent)
751750
case modS of
752751
Right ms -> do

0 commit comments

Comments
 (0)