Skip to content

Commit f63627f

Browse files
mpickeringwz1000
authored andcommitted
Simplify implementation of eval plugin and make it work with GHC 9.4
The plugin was implemented by calling "load" which circumvents all of HLSs caching mechanisms for interface files and linkables. Instead we should work like the other typechecking functions which get all the stuff we need using HLS rules and setup the HscEnv with all the state in the right places. The key part to this is setting up all the HPT modules with linkables if they are depenedencies of the module we are trying to run a function from. - ban load functions from GHC driver - Enable CI for hls-eval-plugin and fix a bug due to clearing of mi_globals
1 parent a73c07c commit f63627f

File tree

14 files changed

+180
-245
lines changed

14 files changed

+180
-245
lines changed

Diff for: .github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ jobs:
148148
name: Test hls-pragmas-plugin
149149
run: cabal test hls-pragmas-plugin --test-options="$TEST_OPTS" || LSP_TEST_LOG_COLOR=0 LSP_TEST_LOG_MESSAGES=true LSP_TEST_LOG_STDERR=true cabal test hls-pragmas-plugin --test-options="$TEST_OPTS"
150150

151-
- if: matrix.test && matrix.ghc != '9.4.4'
151+
- if: matrix.test
152152
name: Test hls-eval-plugin
153153
run: cabal test hls-eval-plugin --test-options="$TEST_OPTS" || LSP_TEST_LOG_COLOR=0 LSP_TEST_LOG_MESSAGES=true LSP_TEST_LOG_STDERR=true cabal test hls-eval-plugin --test-options="$TEST_OPTS"
154154

Diff for: .hlint.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@
208208
- name: "GHC.Arr.!"
209209
within: []
210210

211+
# We do not want to use functions from the
212+
# GHC driver. Instead use hls rules to construct
213+
# an appropriate GHC session
214+
- name: "load"
215+
within: []
216+
- name: "load'"
217+
within: []
218+
- name: "loadWithCache"
219+
within: []
220+
211221
# Tracing functions
212222
# We ban an explicit list rather than the
213223
# Debug.Trace, because that module also

Diff for: ghcide/src/Development/IDE/Core/Compile.hs

-22
Original file line numberDiff line numberDiff line change
@@ -1008,28 +1008,6 @@ handleGenerationErrors' dflags source action =
10081008
. (("Error during " ++ T.unpack source) ++) . show @SomeException
10091009
]
10101010

1011-
-- | Load modules, quickly. Input doesn't need to be desugared.
1012-
-- A module must be loaded before dependent modules can be typechecked.
1013-
-- This variant of loadModuleHome will *never* cause recompilation, it just
1014-
-- modifies the session.
1015-
-- The order modules are loaded is important when there are hs-boot files.
1016-
-- In particular you should make sure to load the .hs version of a file after the
1017-
-- .hs-boot version.
1018-
loadModulesHome
1019-
:: [HomeModInfo]
1020-
-> HscEnv
1021-
-> HscEnv
1022-
loadModulesHome mod_infos e =
1023-
#if MIN_VERSION_ghc(9,3,0)
1024-
hscUpdateHUG (\hug -> foldl' (flip addHomeModInfoToHug) hug mod_infos) (e { hsc_type_env_vars = emptyKnotVars })
1025-
#else
1026-
let !new_modules = addListToHpt (hsc_HPT e) [(mod_name x, x) | x <- mod_infos]
1027-
in e { hsc_HPT = new_modules
1028-
, hsc_type_env_var = Nothing
1029-
}
1030-
where
1031-
mod_name = moduleName . mi_module . hm_iface
1032-
#endif
10331011

10341012
-- Merge the HPTs, module graphs and FinderCaches
10351013
-- See Note [GhcSessionDeps] in Development.IDE.Core.Rules

Diff for: ghcide/src/Development/IDE/Core/Rules.hs

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module Development.IDE.Core.Rules(
5757
typeCheckRuleDefinition,
5858
getRebuildCount,
5959
getSourceFileSource,
60+
currentLinkables,
6061
GhcSessionDepsConfig(..),
6162
Log(..),
6263
DisplayTHWarning(..),

Diff for: ghcide/src/Development/IDE/GHC/Compat.hs

+24
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ module Development.IDE.GHC.Compat(
104104
icInteractiveModule,
105105
HomePackageTable,
106106
lookupHpt,
107+
loadModulesHome,
107108
#if MIN_VERSION_ghc(9,3,0)
108109
Dependencies(dep_direct_mods),
109110
#else
@@ -653,3 +654,26 @@ combineRealSrcSpans span1 span2
653654
(srcSpanEndLine span2, srcSpanEndCol span2)
654655
file = srcSpanFile span1
655656
#endif
657+
658+
-- | Load modules, quickly. Input doesn't need to be desugared.
659+
-- A module must be loaded before dependent modules can be typechecked.
660+
-- This variant of loadModuleHome will *never* cause recompilation, it just
661+
-- modifies the session.
662+
-- The order modules are loaded is important when there are hs-boot files.
663+
-- In particular you should make sure to load the .hs version of a file after the
664+
-- .hs-boot version.
665+
loadModulesHome
666+
:: [HomeModInfo]
667+
-> HscEnv
668+
-> HscEnv
669+
loadModulesHome mod_infos e =
670+
#if MIN_VERSION_ghc(9,3,0)
671+
hscUpdateHUG (\hug -> foldl' (flip addHomeModInfoToHug) hug mod_infos) (e { hsc_type_env_vars = emptyKnotVars })
672+
#else
673+
let !new_modules = addListToHpt (hsc_HPT e) [(mod_name x, x) | x <- mod_infos]
674+
in e { hsc_HPT = new_modules
675+
, hsc_type_env_var = Nothing
676+
}
677+
where
678+
mod_name = moduleName . mi_module . hm_iface
679+
#endif

Diff for: ghcide/src/Development/IDE/GHC/Compat/Core.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ pattern ExposePackage s a mr = DynFlags.ExposePackage s a mr
874874
pattern FunTy :: Type -> Type -> Type
875875
pattern FunTy arg res <- TyCoRep.FunTy {ft_arg = arg, ft_res = res}
876876

877-
#if MIN_VERSION_ghc(9,0,0)
877+
#if MIN_VERSION_ghc(8,10,0)
878878
-- type HasSrcSpan x a = (GenLocated SrcSpan a ~ x)
879879
-- type HasSrcSpan x = () :: Constraint
880880

Diff for: ghcide/src/Development/IDE/GHC/Compat/Outputable.hs

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ module Development.IDE.GHC.Compat.Outputable (
2020
#if MIN_VERSION_ghc(9,3,0)
2121
DiagnosticReason(..),
2222
renderDiagnosticMessageWithHints,
23+
pprMsgEnvelopeBagWithLoc,
24+
Error.getMessages,
25+
renderWithContext,
26+
defaultSDocContext,
27+
errMsgDiagnostic,
28+
unDecorated,
29+
diagnosticMessage,
2330
#else
2431
pprWarning,
2532
pprError,
@@ -29,6 +36,7 @@ module Development.IDE.GHC.Compat.Outputable (
2936
MsgEnvelope,
3037
ErrMsg,
3138
WarnMsg,
39+
SourceError(..),
3240
errMsgSpan,
3341
errMsgSeverity,
3442
formatErrorWithQual,

Diff for: ghcide/src/Development/IDE/GHC/Compat/Util.hs

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ module Development.IDE.GHC.Compat.Util (
2424
LBooleanFormula,
2525
BooleanFormula(..),
2626
-- * OverridingBool
27-
#if !MIN_VERSION_ghc(9,3,0)
2827
OverridingBool(..),
29-
#endif
3028
-- * Maybes
3129
MaybeErr(..),
3230
orElse,
@@ -104,6 +102,11 @@ import Unique
104102
import Util
105103
#endif
106104

105+
#if MIN_VERSION_ghc(9,3,0)
106+
import GHC.Data.Bool
107+
#endif
108+
109+
107110
#if !MIN_VERSION_ghc(9,0,0)
108111
type MonadCatch = Exception.ExceptionMonad
109112

Diff for: haskell-language-server.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ common haddockComments
237237
cpp-options: -Dhls_haddockComments
238238

239239
common eval
240-
if flag(eval) && (impl(ghc < 9.4.1) || flag(ignore-plugins-ghc-bounds))
240+
if flag(eval)
241241
build-depends: hls-eval-plugin ^>= 1.4
242242
cpp-options: -Dhls_eval
243243

Diff for: plugins/hls-eval-plugin/hls-eval-plugin.cabal

-8
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ source-repository head
3737
location: https://github.com/haskell/haskell-language-server
3838

3939
library
40-
if impl(ghc >= 9.3)
41-
buildable: False
42-
else
43-
buildable: True
4440
exposed-modules:
4541
Ide.Plugin.Eval
4642
Ide.Plugin.Eval.Types
@@ -101,10 +97,6 @@ library
10197
TypeOperators
10298

10399
test-suite tests
104-
if impl(ghc >= 9.3)
105-
buildable: False
106-
else
107-
buildable: True
108100
type: exitcode-stdio-1.0
109101
default-language: Haskell2010
110102
hs-source-dirs: test

Diff for: plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{-# OPTIONS_GHC -Wwarn -fno-warn-orphans #-}
55

66
-- | Expression execution
7-
module Ide.Plugin.Eval.Code (Statement, testRanges, resultRange, evalSetup, propSetup, testCheck, asStatements,myExecStmt) where
7+
module Ide.Plugin.Eval.Code (Statement, testRanges, resultRange, propSetup, testCheck, asStatements,myExecStmt) where
88

99
import Control.Lens ((^.))
1010
import Control.Monad.IO.Class
@@ -80,12 +80,6 @@ asStmts (Property t _ _) =
8080
["prop11 = " ++ t, "(propEvaluation prop11 :: IO String)"]
8181

8282

83-
-- |GHC declarations required for expression evaluation
84-
evalSetup :: Ghc ()
85-
evalSetup = do
86-
preludeAsP <- parseImportDecl "import qualified Prelude as P"
87-
context <- getContext
88-
setContext (IIDecl preludeAsP : context)
8983

9084
-- | A wrapper of 'InteractiveEval.execStmt', capturing the execution result
9185
myExecStmt :: String -> ExecOptions -> Ghc (Either String (Maybe String))

0 commit comments

Comments
 (0)