Skip to content

Commit f3dd1e8

Browse files
JakobBruenkerwz1000
authored andcommitted
Allow source plugins to change parser errors
In 9.4, the ability for parser source plugins to access and manipulate non-fatal parse errors was added: https://gitlab.haskell.org/ghc/ghc/-/wikis/migration/9.4#parser-plugins-have-a-different-type HLS always threw an error in this situation without running the plugins though. This commit fixes that.
1 parent c3b010d commit f3dd1e8

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

ghcide/src/Development/IDE/Core/Compile.hs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ import qualified GHC as G
132132
import GHC.Hs (LEpaComment)
133133
import qualified GHC.Types.Error as Error
134134
#endif
135+
#if MIN_VERSION_ghc(9,3,0)
136+
import GHC.Driver.Plugins (PsMessages (..))
137+
#endif
135138

136139
-- | Given a string buffer, return the string (after preprocessing) and the 'ParsedModule'.
137140
parseModule
@@ -1219,7 +1222,7 @@ parseHeader dflags filename contents = do
12191222
PFailedWithErrorMessages msgs ->
12201223
throwE $ diagFromErrMsgs "parser" dflags $ msgs dflags
12211224
POk pst rdr_module -> do
1222-
let (warns, errs) = getMessages' pst dflags
1225+
let (warns, errs) = renderMessages $ getMessages' pst dflags
12231226

12241227
-- Just because we got a `POk`, it doesn't mean there
12251228
-- weren't errors! To clarify, the GHC parser
@@ -1254,9 +1257,18 @@ parseFileContents env customPreprocessor filename ms = do
12541257
POk pst rdr_module ->
12551258
let
12561259
hpm_annotations = mkApiAnns pst
1257-
(warns, errs) = getMessages' pst dflags
1260+
psMessages = getMessages' pst dflags
12581261
in
12591262
do
1263+
let IdePreprocessedSource preproc_warns errs parsed = customPreprocessor rdr_module
1264+
1265+
unless (null errs) $
1266+
throwE $ diagFromStrings "parser" DsError errs
1267+
1268+
let preproc_warnings = diagFromStrings "parser" DsWarning preproc_warns
1269+
(parsed', msgs) <- liftIO $ applyPluginsParsedResultAction env dflags ms hpm_annotations parsed psMessages
1270+
let (warns, errs) = renderMessages msgs
1271+
12601272
-- Just because we got a `POk`, it doesn't mean there
12611273
-- weren't errors! To clarify, the GHC parser
12621274
-- distinguishes between fatal and non-fatal
@@ -1269,14 +1281,6 @@ parseFileContents env customPreprocessor filename ms = do
12691281
unless (null errs) $
12701282
throwE $ diagFromErrMsgs "parser" dflags errs
12711283

1272-
-- Ok, we got here. It's safe to continue.
1273-
let IdePreprocessedSource preproc_warns errs parsed = customPreprocessor rdr_module
1274-
1275-
unless (null errs) $
1276-
throwE $ diagFromStrings "parser" DsError errs
1277-
1278-
let preproc_warnings = diagFromStrings "parser" DsWarning preproc_warns
1279-
parsed' <- liftIO $ applyPluginsParsedResultAction env dflags ms hpm_annotations parsed
12801284

12811285
-- To get the list of extra source files, we take the list
12821286
-- that the parser gave us,

ghcide/src/Development/IDE/GHC/Compat.hs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Development.IDE.GHC.Compat(
2727
reLoc,
2828
reLocA,
2929
getMessages',
30+
renderMessages,
3031
pattern PFailedWithErrorMessages,
3132
isObjectLinkable,
3233

@@ -268,6 +269,7 @@ import GHC.Types.IPE
268269
#if MIN_VERSION_ghc(9,3,0)
269270
import GHC.Types.Error
270271
import GHC.Driver.Config.Stg.Pipeline
272+
import GHC.Driver.Plugins (PsMessages (..))
271273
#endif
272274

273275
#if !MIN_VERSION_ghc(9,3,0)
@@ -390,10 +392,14 @@ type ErrMsg = MsgEnvelope DecoratedSDoc
390392
type WarnMsg = MsgEnvelope DecoratedSDoc
391393
#endif
392394

393-
getMessages' :: PState -> DynFlags -> (Bag WarnMsg, Bag ErrMsg)
395+
#if !MIN_VERSION_ghc(9,3,0)
396+
type PsMessages = (Bag WarnMsg, Bag ErrMsg)
397+
#endif
398+
399+
getMessages' :: PState -> DynFlags -> PsMessages
394400
getMessages' pst dflags =
395401
#if MIN_VERSION_ghc(9,3,0)
396-
bimap (fmap (fmap renderDiagnosticMessageWithHints) . getMessages) (fmap (fmap renderDiagnosticMessageWithHints) . getMessages) $ getPsMessages pst
402+
uncurry PsMessages $ getPsMessages pst
397403
#else
398404
#if MIN_VERSION_ghc(9,2,0)
399405
bimap (fmap pprWarning) (fmap pprError) $
@@ -404,6 +410,15 @@ getMessages' pst dflags =
404410
#endif
405411
#endif
406412

413+
renderMessages :: PsMessages -> (Bag WarnMsg, Bag ErrMsg)
414+
renderMessages msgs =
415+
#if MIN_VERSION_ghc(9,3,0)
416+
let renderMsgs extractor = (fmap . fmap) renderDiagnosticMessageWithHints . getMessages $ extractor msgs
417+
in (renderMsgs psWarnings, renderMsgs psErrors)
418+
#else
419+
msgs
420+
#endif
421+
407422
#if MIN_VERSION_ghc(9,2,0)
408423
pattern PFailedWithErrorMessages :: forall a b. (b -> Bag (MsgEnvelope DecoratedSDoc)) -> ParseResult a
409424
pattern PFailedWithErrorMessages msgs

ghcide/src/Development/IDE/GHC/Compat/Plugins.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@ import Plugins
3535
import Development.IDE.GHC.Compat.Core
3636
import Development.IDE.GHC.Compat.Env (hscSetFlags, hsc_dflags)
3737
import Development.IDE.GHC.Compat.Parser as Parser
38+
import Debug.Trace
39+
import GHC.Driver.Env (hsc_plugins)
40+
import GHC.Driver.Plugins
3841

39-
applyPluginsParsedResultAction :: HscEnv -> DynFlags -> ModSummary -> Parser.ApiAnns -> ParsedSource -> IO ParsedSource
40-
applyPluginsParsedResultAction env dflags ms hpm_annotations parsed = do
42+
applyPluginsParsedResultAction :: HscEnv -> DynFlags -> ModSummary -> Parser.ApiAnns -> ParsedSource -> PsMessages -> IO (ParsedSource, PsMessages)
43+
applyPluginsParsedResultAction env dflags ms hpm_annotations parsed msgs = do
4144
-- Apply parsedResultAction of plugins
4245
let applyPluginAction p opts = parsedResultAction p opts ms
4346
#if MIN_VERSION_ghc(9,3,0)
44-
fmap (hpm_module . parsedResultModule) $ runHsc env $ withPlugins
47+
fmap (\result -> (hpm_module (parsedResultModule result), (parsedResultMessages result))) $ runHsc env $ withPlugins
4548
#else
46-
fmap hpm_module $ runHsc env $ withPlugins
49+
fmap ((, msgs), hpm_module) $ runHsc env $ withPlugins
4750
#endif
4851
#if MIN_VERSION_ghc(9,3,0)
4952
(Env.hsc_plugins env)
@@ -54,7 +57,7 @@ applyPluginsParsedResultAction env dflags ms hpm_annotations parsed = do
5457
#endif
5558
applyPluginAction
5659
#if MIN_VERSION_ghc(9,3,0)
57-
(ParsedResult (HsParsedModule parsed [] hpm_annotations) (PsMessages mempty mempty))
60+
(ParsedResult (HsParsedModule parsed [] hpm_annotations) msgs)
5861
#else
5962
(HsParsedModule parsed [] hpm_annotations)
6063
#endif

0 commit comments

Comments
 (0)