Skip to content

Commit 32a6ae3

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.
1 parent af2f579 commit 32a6ae3

File tree

8 files changed

+111
-214
lines changed

8 files changed

+111
-214
lines changed

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -978,28 +978,6 @@ handleGenerationErrors' dflags source action =
978978
. (("Error during " ++ T.unpack source) ++) . show @SomeException
979979
]
980980

981-
-- | Load modules, quickly. Input doesn't need to be desugared.
982-
-- A module must be loaded before dependent modules can be typechecked.
983-
-- This variant of loadModuleHome will *never* cause recompilation, it just
984-
-- modifies the session.
985-
-- The order modules are loaded is important when there are hs-boot files.
986-
-- In particular you should make sure to load the .hs version of a file after the
987-
-- .hs-boot version.
988-
loadModulesHome
989-
:: [HomeModInfo]
990-
-> HscEnv
991-
-> HscEnv
992-
loadModulesHome mod_infos e =
993-
#if MIN_VERSION_ghc(9,3,0)
994-
hscUpdateHUG (\hug -> foldr addHomeModInfoToHug hug mod_infos) (e { hsc_type_env_vars = emptyKnotVars })
995-
#else
996-
let !new_modules = addListToHpt (hsc_HPT e) [(mod_name x, x) | x <- mod_infos]
997-
in e { hsc_HPT = new_modules
998-
, hsc_type_env_var = Nothing
999-
}
1000-
where
1001-
mod_name = moduleName . mi_module . hm_iface
1002-
#endif
1003981

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

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

Lines changed: 1 addition & 0 deletions
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(..),

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ module Development.IDE.GHC.Compat(
9696
icInteractiveModule,
9797
HomePackageTable,
9898
lookupHpt,
99+
loadModulesHome,
99100
#if MIN_VERSION_ghc(9,3,0)
100101
Dependencies(dep_direct_mods),
101102
#else
@@ -630,3 +631,26 @@ combineRealSrcSpans span1 span2
630631
(srcSpanEndLine span2, srcSpanEndCol span2)
631632
file = srcSpanFile span1
632633
#endif
634+
635+
-- | Load modules, quickly. Input doesn't need to be desugared.
636+
-- A module must be loaded before dependent modules can be typechecked.
637+
-- This variant of loadModuleHome will *never* cause recompilation, it just
638+
-- modifies the session.
639+
-- The order modules are loaded is important when there are hs-boot files.
640+
-- In particular you should make sure to load the .hs version of a file after the
641+
-- .hs-boot version.
642+
loadModulesHome
643+
:: [HomeModInfo]
644+
-> HscEnv
645+
-> HscEnv
646+
loadModulesHome mod_infos e =
647+
#if MIN_VERSION_ghc(9,3,0)
648+
hscUpdateHUG (\hug -> foldr addHomeModInfoToHug hug mod_infos) (e { hsc_type_env_vars = emptyKnotVars })
649+
#else
650+
let !new_modules = addListToHpt (hsc_HPT e) [(mod_name x, x) | x <- mod_infos]
651+
in e { hsc_HPT = new_modules
652+
, hsc_type_env_var = Nothing
653+
}
654+
where
655+
mod_name = moduleName . mi_module . hm_iface
656+
#endif

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

Lines changed: 5 additions & 2 deletions
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

haskell-language-server.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ common haddockComments
242242
cpp-options: -Dhls_haddockComments
243243

244244
common eval
245-
if flag(eval) && (impl(ghc < 9.4.1) || flag(ignore-plugins-ghc-bounds))
245+
if flag(eval)
246246
build-depends: hls-eval-plugin ^>= 1.3
247247
cpp-options: -Dhls_eval
248248

plugins/hls-eval-plugin/hls-eval-plugin.cabal

Lines changed: 0 additions & 8 deletions
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

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

Lines changed: 1 addition & 7 deletions
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)