@@ -89,7 +89,6 @@ import System.FilePath
89
89
import System.Directory
90
90
import System.IO.Extra
91
91
import Control.Exception (evaluate )
92
- import Exception (ExceptionMonad )
93
92
import TcEnv (tcLookup )
94
93
import Data.Time (UTCTime , getCurrentTime )
95
94
import Linker (unload )
@@ -105,7 +104,7 @@ parseModule
105
104
-> IO (IdeResult (StringBuffer , ParsedModule ))
106
105
parseModule IdeOptions {.. } env comp_pkgs filename modTime mbContents =
107
106
fmap (either (, Nothing ) id ) $
108
- evalGhcEnv env $ runExceptT $ do
107
+ runExceptT $ do
109
108
(contents, dflags) <- preprocessor env filename mbContents
110
109
(diag, modu) <- parseFileContents env optPreprocessor dflags comp_pkgs filename modTime contents
111
110
return (diag, Just (contents, modu))
@@ -127,39 +126,37 @@ typecheckModule :: IdeDefer
127
126
-> HscEnv
128
127
-> Maybe [Linkable ] -- ^ linkables not to unload, if Nothing don't unload anything
129
128
-> ParsedModule
130
- -> IO (IdeResult ( HscEnv , TcModuleResult ) )
129
+ -> IO (IdeResult TcModuleResult )
131
130
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
135
133
136
134
let modSummary = pm_mod_summary pm
137
135
dflags = ms_hspp_opts modSummary
138
136
139
- modSummary' <- initPlugins modSummary
137
+ modSummary' <- initPlugins hsc modSummary
140
138
(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'}
144
142
let errorPipeline = unDefer . hideDiag dflags . tagDiag
145
143
diags = map errorPipeline warnings
146
144
deferedError = any fst diags
147
145
return (map snd diags, Just $ tcm{tmrDeferedError = deferedError})
148
146
where
149
147
demoteIfDefer = if defer then demoteTypeErrorsToWarnings else id
150
148
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
153
151
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 }
163
160
let rn_info = case mrn_info of
164
161
Just x -> x
165
162
Nothing -> error " no renamed info tcRnModule"
@@ -215,9 +212,8 @@ mkHiFileResultCompile session' tcm simplified_guts ltype = catchErrs $ do
215
212
. ((" Error during " ++ T. unpack source) ++ ) . show @ SomeException
216
213
]
217
214
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
221
217
dflags <- liftIO $ initializePlugins session $ ms_hspp_opts modSummary
222
218
return modSummary{ms_hspp_opts = dflags}
223
219
@@ -235,40 +231,37 @@ compileModule
235
231
-> ModSummary
236
232
-> TcGblEnv
237
233
-> IO (IdeResult ModGuts )
238
- compileModule (RunSimplifier simplify) packageState ms tcg =
234
+ compileModule (RunSimplifier simplify) session ms tcg =
239
235
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
244
238
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
252
246
return (map snd warnings, desugared_guts)
253
247
254
248
generateObjectCode :: HscEnv -> ModSummary -> CgGuts -> IO (IdeResult Linkable )
255
- generateObjectCode hscEnv summary guts = do
249
+ generateObjectCode session summary guts = do
256
250
fmap (either (, Nothing ) (second Just )) $
257
- evalGhcEnv hscEnv $
258
- catchSrcErrors " object" $ do
259
- session <- getSession
251
+ catchSrcErrors (hsc_dflags session) " object" $ do
260
252
let dot_o = ml_obj_file (ms_location summary)
261
253
mod = ms_mod summary
262
- session' = session { hsc_dflags = (hsc_dflags session) { outputFile = Just dot_o }}
263
254
fp = replaceExtension dot_o " s"
264
- liftIO $ createDirectoryIfMissing True (takeDirectory fp)
255
+ createDirectoryIfMissing True (takeDirectory fp)
265
256
(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 }}
267
260
(outputFilename, _mStub, _foreign_files) <- hscGenHardCode session' guts
268
261
#if MIN_GHC_API_VERSION(8,10,0)
269
- (ms_location summary)
262
+ (ms_location summary' )
270
263
#else
271
- (_tweak summary)
264
+ summary'
272
265
#endif
273
266
fp
274
267
compileFile session' StopLn (outputFilename, Just (As False ))
@@ -282,16 +275,16 @@ generateObjectCode hscEnv summary guts = do
282
275
generateByteCode :: HscEnv -> ModSummary -> CgGuts -> IO (IdeResult Linkable )
283
276
generateByteCode hscEnv summary guts = do
284
277
fmap (either (, Nothing ) (second Just )) $
285
- evalGhcEnv hscEnv $
286
- catchSrcErrors " bytecode" $ do
287
- session <- getSession
278
+ catchSrcErrors (hsc_dflags hscEnv) " bytecode" $ do
288
279
(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' }
290
283
hscInteractive session guts
291
284
#if MIN_GHC_API_VERSION(8,10,0)
292
- (ms_location summary)
285
+ (ms_location summary' )
293
286
#else
294
- (_tweak summary)
287
+ summary'
295
288
#endif
296
289
let unlinked = BCOs bytecode sptEntries
297
290
time <- liftIO getCurrentTime
@@ -510,13 +503,12 @@ withBootSuffix _ = id
510
503
511
504
-- | Produce a module summary from a StringBuffer.
512
505
getModSummaryFromBuffer
513
- :: GhcMonad m
514
- => FilePath
506
+ :: FilePath
515
507
-> UTCTime
516
508
-> DynFlags
517
509
-> GHC. ParsedSource
518
510
-> StringBuffer
519
- -> ExceptT [FileDiagnostic ] m ModSummary
511
+ -> ExceptT [FileDiagnostic ] IO ModSummary
520
512
getModSummaryFromBuffer fp modTime dflags parsed contents = do
521
513
(modName, imports) <- liftEither $ getImportsParsed dflags parsed
522
514
@@ -553,12 +545,11 @@ getModSummaryFromBuffer fp modTime dflags parsed contents = do
553
545
-- | Given a buffer, env and filepath, produce a module summary by parsing only the imports.
554
546
-- Runs preprocessors as needed.
555
547
getModSummaryFromImports
556
- :: (HasDynFlags m , ExceptionMonad m , MonadIO m )
557
- => HscEnv
548
+ :: HscEnv
558
549
-> FilePath
559
550
-> UTCTime
560
551
-> Maybe SB. StringBuffer
561
- -> ExceptT [FileDiagnostic ] m ModSummary
552
+ -> ExceptT [FileDiagnostic ] IO ModSummary
562
553
getModSummaryFromImports env fp modTime contents = do
563
554
(contents, dflags) <- preprocessor env fp contents
564
555
(srcImports, textualImports, L _ moduleName) <-
@@ -595,7 +586,7 @@ getModSummaryFromImports env fp modTime contents = do
595
586
596
587
-- | Parse only the module header
597
588
parseHeader
598
- :: GhcMonad m
589
+ :: Monad m
599
590
=> DynFlags -- ^ flags to use
600
591
-> FilePath -- ^ the filename (for source locations)
601
592
-> SB. StringBuffer -- ^ Haskell module source text (full Unicode is supported)
@@ -630,15 +621,14 @@ parseHeader dflags filename contents = do
630
621
-- | Given a buffer, flags, and file path, produce a
631
622
-- parsed module (or errors) and any parse warnings. Does not run any preprocessors
632
623
parseFileContents
633
- :: GhcMonad m
634
- => HscEnv
624
+ :: HscEnv
635
625
-> (GHC. ParsedSource -> IdePreprocessedSource )
636
626
-> DynFlags -- ^ flags to use
637
627
-> [PackageName ] -- ^ The package imports to ignore
638
628
-> FilePath -- ^ the filename (for source locations)
639
629
-> UTCTime -- ^ the modification timestamp
640
630
-> SB. StringBuffer -- ^ Haskell module source text (full Unicode is supported)
641
- -> ExceptT [FileDiagnostic ] m ([FileDiagnostic ], ParsedModule )
631
+ -> ExceptT [FileDiagnostic ] IO ([FileDiagnostic ], ParsedModule )
642
632
parseFileContents env customPreprocessor dflags comp_pkgs filename modTime contents = do
643
633
let loc = mkRealSrcLoc (mkFastString filename) 1 1
644
634
case unP Parser. parseModule (mkPState dflags contents loc) of
@@ -756,12 +746,12 @@ mkDetailsFromIface session iface linkable = do
756
746
-- | Non-interactive, batch version of 'InteractiveEval.getDocs'.
757
747
-- The interactive paths create problems in ghc-lib builds
758
748
--- 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
765
755
((_warns,errs), res) <- initTc hsc_env HsSrcFile False _mod fakeSpan $ forM _names $ \ name ->
766
756
case nameModule_maybe name of
767
757
Nothing -> return (Left $ NameHasNoModule name)
@@ -791,11 +781,11 @@ fakeSpan = realSrcLocSpan $ mkRealSrcLoc (fsLit "<ghcide>") 1 1
791
781
-- | Non-interactive, batch version of 'InteractiveEval.lookupNames'.
792
782
-- The interactive paths create problems in ghc-lib builds
793
783
--- 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
796
786
-> 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
799
789
(_messages, res) <- initTc hsc_env HsSrcFile False mod fakeSpan $ do
800
790
tcthing <- tcLookup name
801
791
case tcthing of
0 commit comments