Skip to content

Commit 81640b6

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 ddc67b2 commit 81640b6

File tree

9 files changed

+112
-215
lines changed

9 files changed

+112
-215
lines changed

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

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

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

1010988
-- Merge the HPTs, module graphs and FinderCaches
1011989
-- 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
@@ -102,6 +102,7 @@ module Development.IDE.GHC.Compat(
102102
icInteractiveModule,
103103
HomePackageTable,
104104
lookupHpt,
105+
loadModulesHome,
105106
#if MIN_VERSION_ghc(9,3,0)
106107
Dependencies(dep_direct_mods),
107108
#else
@@ -651,3 +652,26 @@ combineRealSrcSpans span1 span2
651652
(srcSpanEndLine span2, srcSpanEndCol span2)
652653
file = srcSpanFile span1
653654
#endif
655+
656+
-- | Load modules, quickly. Input doesn't need to be desugared.
657+
-- A module must be loaded before dependent modules can be typechecked.
658+
-- This variant of loadModuleHome will *never* cause recompilation, it just
659+
-- modifies the session.
660+
-- The order modules are loaded is important when there are hs-boot files.
661+
-- In particular you should make sure to load the .hs version of a file after the
662+
-- .hs-boot version.
663+
loadModulesHome
664+
:: [HomeModInfo]
665+
-> HscEnv
666+
-> HscEnv
667+
loadModulesHome mod_infos e =
668+
#if MIN_VERSION_ghc(9,3,0)
669+
hscUpdateHUG (\hug -> foldr addHomeModInfoToHug hug mod_infos) (e { hsc_type_env_vars = emptyKnotVars })
670+
#else
671+
let !new_modules = addListToHpt (hsc_HPT e) [(mod_name x, x) | x <- mod_infos]
672+
in e { hsc_HPT = new_modules
673+
, hsc_type_env_var = Nothing
674+
}
675+
where
676+
mod_name = moduleName . mi_module . hm_iface
677+
#endif

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ pattern ExposePackage s a mr = DynFlags.ExposePackage s a mr
872872
pattern FunTy :: Type -> Type -> Type
873873
pattern FunTy arg res <- TyCoRep.FunTy {ft_arg = arg, ft_res = res}
874874

875-
#if MIN_VERSION_ghc(9,0,0)
875+
#if MIN_VERSION_ghc(8,10,0)
876876
-- type HasSrcSpan x a = (GenLocated SrcSpan a ~ x)
877877
-- type HasSrcSpan x = () :: Constraint
878878

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
@@ -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

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)