Skip to content

Commit 9f4220f

Browse files
fmehtajneiraberbermanmergify[bot]
authored
Make the eval plugin use the same default language extensions as ghci. (#1596)
* Enable ExtendedDefaultRules for expressions in eval plugin (#1591) * Add tests for fix#1591 * Add fix for #1954 * Remove dependency on xopt_set_unlessExplSpec. Reason: It was breaking GHC 8.6.2 tests. * Remove unnecessary comment. Co-authored-by: Potato Hatsue <berberman@yandex.com> Co-authored-by: Javier Neira <atreyu.bbb@gmail.com> Co-authored-by: Potato Hatsue <1793913507@qq.com> Co-authored-by: Potato Hatsue <berberman@yandex.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent f75f381 commit 9f4220f

File tree

6 files changed

+64
-15
lines changed

6 files changed

+64
-15
lines changed

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

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

55
-- | Expression execution
6-
module Ide.Plugin.Eval.Code (Statement, testRanges, resultRange, evalExtensions, evalSetup, propSetup, testCheck, asStatements,myExecStmt) where
6+
module Ide.Plugin.Eval.Code (Statement, testRanges, resultRange, evalSetup, propSetup, testCheck, asStatements,myExecStmt) where
77

88
import Control.Lens ((^.))
99
import Data.Algorithm.Diff (Diff, PolyDiff (..), getDiff)
@@ -13,7 +13,6 @@ import qualified Data.Text as T
1313
import Development.IDE.Types.Location (Position (..), Range (..))
1414
import GHC (ExecOptions, ExecResult (..),
1515
execStmt)
16-
import GHC.LanguageExtensions.Type (Extension (..))
1716
import GhcMonad (Ghc, liftIO, modifySession)
1817
import HscTypes
1918
import Ide.Plugin.Eval.Types (Language (Plain), Loc,
@@ -81,15 +80,6 @@ asStmts (Example e _ _) = NE.toList e
8180
asStmts (Property t _ _) =
8281
["prop11 = " ++ t, "(propEvaluation prop11 :: IO String)"]
8382

84-
-- |GHC extensions required for expression evaluation
85-
evalExtensions :: [Extension]
86-
evalExtensions =
87-
[ OverlappingInstances
88-
, UndecidableInstances
89-
, FlexibleInstances
90-
, IncoherentInstances
91-
, TupleSections
92-
]
9383

9484
-- |GHC declarations required for expression evaluation
9585
evalSetup :: Ghc ()

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ import GHC (ClsInst,
108108
setContext, setLogAction,
109109
setSessionDynFlags,
110110
setTargets, typeKind)
111+
import qualified GHC.LanguageExtensions.Type as LangExt (Extension (..))
111112
import GhcPlugins (DynFlags (..),
112113
defaultLogActionHPutStrDoc,
113114
elemNameSet, gopt_set,
@@ -118,13 +119,13 @@ import GhcPlugins (DynFlags (..),
118119
pprInfixName,
119120
targetPlatform,
120121
tyThingParent_maybe,
121-
xopt_set)
122+
xopt_set, xopt_unset)
123+
122124
import HscTypes (InteractiveImport (IIModule),
123125
ModSummary (ms_mod),
124126
Target (Target),
125127
TargetId (TargetFile))
126128
import Ide.Plugin.Eval.Code (Statement, asStatements,
127-
evalExtensions,
128129
evalSetup, myExecStmt,
129130
propSetup, resultRange,
130131
testCheck, testRanges)
@@ -316,8 +317,13 @@ runEvalCmd st EvalParams{..} =
316317
-- copy the package state to the interactive DynFlags
317318
idflags <- getInteractiveDynFlags
318319
df <- getSessionDynFlags
319-
setInteractiveDynFlags $
320-
(foldl xopt_set idflags evalExtensions)
320+
-- set the identical DynFlags as GHCi
321+
-- Source: https://github.com/ghc/ghc/blob/5abf59976c7335df760e5d8609d9488489478173/ghc/GHCi/UI.hs#L473-L483
322+
-- This needs to be done manually since the default flags are not visible externally.
323+
let df' = flip xopt_set LangExt.ExtendedDefaultRules
324+
. flip xopt_unset LangExt.MonomorphismRestriction
325+
$ idflags
326+
setInteractiveDynFlags $ df'
321327
#if MIN_VERSION_ghc(9,0,0)
322328
{ unitState =
323329
unitState

plugins/hls-eval-plugin/test/Main.hs

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ tests =
9191
-- , goldenWithEval "Local Modules can be imported in a test" "TLocalImportInTest" "hs"
9292
, goldenWithEval "Setting language option TupleSections" "TLanguageOptionsTupleSections" "hs"
9393
, goldenWithEval ":set accepts ghci flags" "TFlags" "hs"
94+
, goldenWithEval "The default language extensions for the eval plugin are the same as those for ghci" "TSameDefaultLanguageExtensionsAsGhci" "hs"
9495
, goldenWithEval "IO expressions are supported, stdout/stderr output is ignored" "TIO" "hs"
9596
, goldenWithEval "Property checking" "TProperty" "hs"
9697
, goldenWithEval "Prelude has no special treatment, it is imported as stated in the module" "TPrelude" "hs"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-- The default language extensions for the eval plugin are the same as those for ghci
2+
3+
module TSameDefaultLanguageExtensionsAsGhci where
4+
5+
{-
6+
Running `:showi language` within ghci currently lists NoDatatypeContexts, ExtendedDefaultRules, NoMonomorphismRestriction and NondecreasingIndentation.
7+
8+
The flags NoDatatypeContexts and NondecreasingIndentation are globally set in Haskell2021, whereas ExtendedDefaultRules and NoMonomorphismRestriction are set manually within ghci.
9+
(see https://github.com/ghc/ghc/blob/5abf59976c7335df760e5d8609d9488489478173/ghc/GHCi/UI.hs#L473-L483)
10+
11+
It therefore suffices to test for ExtendedDefaultRules and NoMonomorphismRestriction only.
12+
-}
13+
14+
15+
-- ExtendedDefaultRules
16+
17+
-- >>> []
18+
-- []
19+
20+
-- >>> reverse []
21+
-- []
22+
23+
-- NoMonomorphismRestriction
24+
25+
-- >>> plus = (+)
26+
-- >>> :t plus
27+
-- plus :: forall a. Num a => a -> a -> a
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- The default language extensions for the eval plugin are the same as those for ghci
2+
3+
module TSameDefaultLanguageExtensionsAsGhci where
4+
5+
{-
6+
Running `:showi language` within ghci currently lists NoDatatypeContexts, ExtendedDefaultRules, NoMonomorphismRestriction and NondecreasingIndentation.
7+
8+
The flags NoDatatypeContexts and NondecreasingIndentation are globally set in Haskell2021, whereas ExtendedDefaultRules and NoMonomorphismRestriction are set manually within ghci.
9+
(see https://github.com/ghc/ghc/blob/5abf59976c7335df760e5d8609d9488489478173/ghc/GHCi/UI.hs#L473-L483)
10+
11+
It therefore suffices to test for ExtendedDefaultRules and NoMonomorphismRestriction only.
12+
-}
13+
14+
15+
-- ExtendedDefaultRules
16+
17+
-- >>> []
18+
19+
-- >>> reverse []
20+
21+
-- NoMonomorphismRestriction
22+
23+
-- >>> plus = (+)
24+
-- >>> :t plus

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

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ library
5050
TLanguageOptionsTupleSections
5151
TIO
5252
TProperty
53+
TSameDefaultLanguageExtensionsAsGhci
5354
TPrelude
5455
TCPP
5556
TLHS

0 commit comments

Comments
 (0)