Skip to content

Commit 0e35e52

Browse files
committed
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 41c2b77 commit 0e35e52

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
@@ -1225,7 +1228,7 @@ parseHeader dflags filename contents = do
12251228
PFailedWithErrorMessages msgs ->
12261229
throwE $ diagFromErrMsgs "parser" dflags $ msgs dflags
12271230
POk pst rdr_module -> do
1228-
let (warns, errs) = getMessages' pst dflags
1231+
let (warns, errs) = renderMessages $ getMessages' pst dflags
12291232

12301233
-- Just because we got a `POk`, it doesn't mean there
12311234
-- weren't errors! To clarify, the GHC parser
@@ -1260,9 +1263,18 @@ parseFileContents env customPreprocessor filename ms = do
12601263
POk pst rdr_module ->
12611264
let
12621265
hpm_annotations = mkApiAnns pst
1263-
(warns, errs) = getMessages' pst dflags
1266+
psMessages = getMessages' pst dflags
12641267
in
12651268
do
1269+
let IdePreprocessedSource preproc_warns errs parsed = customPreprocessor rdr_module
1270+
1271+
unless (null errs) $
1272+
throwE $ diagFromStrings "parser" DsError errs
1273+
1274+
let preproc_warnings = diagFromStrings "parser" DsWarning preproc_warns
1275+
(parsed', msgs) <- liftIO $ applyPluginsParsedResultAction env dflags ms hpm_annotations parsed psMessages
1276+
let (warns, errs) = renderMessages msgs
1277+
12661278
-- Just because we got a `POk`, it doesn't mean there
12671279
-- weren't errors! To clarify, the GHC parser
12681280
-- distinguishes between fatal and non-fatal
@@ -1275,14 +1287,6 @@ parseFileContents env customPreprocessor filename ms = do
12751287
unless (null errs) $
12761288
throwE $ diagFromErrMsgs "parser" dflags errs
12771289

1278-
-- Ok, we got here. It's safe to continue.
1279-
let IdePreprocessedSource preproc_warns errs parsed = customPreprocessor rdr_module
1280-
1281-
unless (null errs) $
1282-
throwE $ diagFromStrings "parser" DsError errs
1283-
1284-
let preproc_warnings = diagFromStrings "parser" DsWarning preproc_warns
1285-
parsed' <- liftIO $ applyPluginsParsedResultAction env dflags ms hpm_annotations parsed
12861290

12871291
-- To get the list of extra source files, we take the list
12881292
-- 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
@@ -26,6 +26,7 @@ module Development.IDE.GHC.Compat(
2626
reLoc,
2727
reLocA,
2828
getMessages',
29+
renderMessages,
2930
pattern PFailedWithErrorMessages,
3031
isObjectLinkable,
3132

@@ -261,6 +262,7 @@ import GHC.Types.IPE
261262
#if MIN_VERSION_ghc(9,3,0)
262263
import GHC.Types.Error
263264
import GHC.Driver.Config.Stg.Pipeline
265+
import GHC.Driver.Plugins (PsMessages (..))
264266
#endif
265267

266268
type ModIfaceAnnotation = Annotation
@@ -378,10 +380,14 @@ type ErrMsg = MsgEnvelope DecoratedSDoc
378380
type WarnMsg = MsgEnvelope DecoratedSDoc
379381
#endif
380382

381-
getMessages' :: PState -> DynFlags -> (Bag WarnMsg, Bag ErrMsg)
383+
#if !MIN_VERSION_ghc(9,3,0)
384+
type PsMessages = (Bag WarnMsg, Bag ErrMsg)
385+
#endif
386+
387+
getMessages' :: PState -> DynFlags -> PsMessages
382388
getMessages' pst dflags =
383389
#if MIN_VERSION_ghc(9,3,0)
384-
bimap (fmap (fmap renderDiagnosticMessageWithHints) . getMessages) (fmap (fmap renderDiagnosticMessageWithHints) . getMessages) $ getPsMessages pst
390+
uncurry PsMessages $ getPsMessages pst
385391
#else
386392
#if MIN_VERSION_ghc(9,2,0)
387393
bimap (fmap pprWarning) (fmap pprError) $
@@ -392,6 +398,15 @@ getMessages' pst dflags =
392398
#endif
393399
#endif
394400

401+
renderMessages :: PsMessages -> (Bag WarnMsg, Bag ErrMsg)
402+
renderMessages msgs =
403+
#if MIN_VERSION_ghc(9,3,0)
404+
let renderMsgs extractor = (fmap . fmap) renderDiagnosticMessageWithHints . getMessages $ extractor msgs
405+
in (renderMsgs psWarnings, renderMsgs psErrors)
406+
#else
407+
msgs
408+
#endif
409+
395410
#if MIN_VERSION_ghc(9,2,0)
396411
pattern PFailedWithErrorMessages :: forall a b. (b -> Bag (MsgEnvelope DecoratedSDoc)) -> ParseResult a
397412
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)