Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the eval plugin use the same default language extensions as ghci. #1596

Merged
merged 15 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 1 addition & 11 deletions plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{-# OPTIONS_GHC -Wwarn -fno-warn-orphans #-}

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

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

-- |GHC extensions required for expression evaluation
evalExtensions :: [Extension]
evalExtensions =
[ OverlappingInstances
, UndecidableInstances
, FlexibleInstances
, IncoherentInstances
, TupleSections
]

-- |GHC declarations required for expression evaluation
evalSetup :: Ghc ()
Expand Down
14 changes: 10 additions & 4 deletions plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ import GHC (ClsInst,
setContext, setLogAction,
setSessionDynFlags,
setTargets, typeKind)
import qualified GHC.LanguageExtensions.Type as LangExt (Extension (..))
import GhcPlugins (DynFlags (..),
defaultLogActionHPutStrDoc,
elemNameSet, gopt_set,
Expand All @@ -118,13 +119,13 @@ import GhcPlugins (DynFlags (..),
pprInfixName,
targetPlatform,
tyThingParent_maybe,
xopt_set)
xopt_set, xopt_unset)

import HscTypes (InteractiveImport (IIModule),
ModSummary (ms_mod),
Target (Target),
TargetId (TargetFile))
import Ide.Plugin.Eval.Code (Statement, asStatements,
evalExtensions,
evalSetup, myExecStmt,
propSetup, resultRange,
testCheck, testRanges)
Expand Down Expand Up @@ -316,8 +317,13 @@ runEvalCmd st EvalParams{..} =
-- copy the package state to the interactive DynFlags
idflags <- getInteractiveDynFlags
df <- getSessionDynFlags
setInteractiveDynFlags $
(foldl xopt_set idflags evalExtensions)
-- set the identical DynFlags as GHCi
-- Source: https://github.com/ghc/ghc/blob/5abf59976c7335df760e5d8609d9488489478173/ghc/GHCi/UI.hs#L473-L483
-- This needs to be done manually since the default flags are not visible externally.
let df' = flip xopt_set LangExt.ExtendedDefaultRules
. flip xopt_unset LangExt.MonomorphismRestriction
$ idflags
setInteractiveDynFlags $ df'
#if MIN_VERSION_ghc(9,0,0)
{ unitState =
unitState
Expand Down
1 change: 1 addition & 0 deletions plugins/hls-eval-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ tests =
-- , goldenWithEval "Local Modules can be imported in a test" "TLocalImportInTest" "hs"
, goldenWithEval "Setting language option TupleSections" "TLanguageOptionsTupleSections" "hs"
, goldenWithEval ":set accepts ghci flags" "TFlags" "hs"
, goldenWithEval "The default language extensions for the eval plugin are the same as those for ghci" "TSameDefaultLanguageExtensionsAsGhci" "hs"
, goldenWithEval "IO expressions are supported, stdout/stderr output is ignored" "TIO" "hs"
, goldenWithEval "Property checking" "TProperty" "hs"
, goldenWithEval "Prelude has no special treatment, it is imported as stated in the module" "TPrelude" "hs"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- The default language extensions for the eval plugin are the same as those for ghci

module TSameDefaultLanguageExtensionsAsGhci where

{-
Running `:showi language` within ghci currently lists NoDatatypeContexts, ExtendedDefaultRules, NoMonomorphismRestriction and NondecreasingIndentation.

The flags NoDatatypeContexts and NondecreasingIndentation are globally set in Haskell2021, whereas ExtendedDefaultRules and NoMonomorphismRestriction are set manually within ghci.
(see https://github.com/ghc/ghc/blob/5abf59976c7335df760e5d8609d9488489478173/ghc/GHCi/UI.hs#L473-L483)

It therefore suffices to test for ExtendedDefaultRules and NoMonomorphismRestriction only.
-}


-- ExtendedDefaultRules

-- >>> []
-- []

-- >>> reverse []
-- []

-- NoMonomorphismRestriction

-- >>> plus = (+)
-- >>> :t plus
-- plus :: forall a. Num a => a -> a -> a
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- The default language extensions for the eval plugin are the same as those for ghci

module TSameDefaultLanguageExtensionsAsGhci where

{-
Running `:showi language` within ghci currently lists NoDatatypeContexts, ExtendedDefaultRules, NoMonomorphismRestriction and NondecreasingIndentation.

The flags NoDatatypeContexts and NondecreasingIndentation are globally set in Haskell2021, whereas ExtendedDefaultRules and NoMonomorphismRestriction are set manually within ghci.
(see https://github.com/ghc/ghc/blob/5abf59976c7335df760e5d8609d9488489478173/ghc/GHCi/UI.hs#L473-L483)

It therefore suffices to test for ExtendedDefaultRules and NoMonomorphismRestriction only.
-}


-- ExtendedDefaultRules

-- >>> []

-- >>> reverse []

-- NoMonomorphismRestriction

-- >>> plus = (+)
-- >>> :t plus
1 change: 1 addition & 0 deletions plugins/hls-eval-plugin/test/testdata/test.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ library
TLanguageOptionsTupleSections
TIO
TProperty
TSameDefaultLanguageExtensionsAsGhci
TPrelude
TCPP
TLHS
Expand Down