From 3d4bfa59aa7174952fc652676ac73cad6ef1958d Mon Sep 17 00:00:00 2001 From: Farhad Mehta Date: Fri, 19 Mar 2021 16:01:11 +0100 Subject: [PATCH 1/5] Enable ExtendedDefaultRules for expressions in eval plugin (#1591) --- plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs b/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs index 5ad4537338..500075601c 100644 --- a/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs +++ b/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs @@ -91,6 +91,7 @@ evalExtensions = , FlexibleInstances , IncoherentInstances , TupleSections + , ExtendedDefaultRules ] -- |GHC declarations required for expression evaluation From 4246ce8110064c32b81eb289bcbbb9b2b4aa3c48 Mon Sep 17 00:00:00 2001 From: Farhad Mehta Date: Mon, 21 Jun 2021 11:32:40 +0200 Subject: [PATCH 2/5] Add tests for fix#1591 --- plugins/hls-eval-plugin/test/Main.hs | 1 + .../TSameDefaultLanguageExtensionsAsGhci.expected.hs | 11 +++++++++++ .../testdata/TSameDefaultLanguageExtensionsAsGhci.hs | 9 +++++++++ plugins/hls-eval-plugin/test/testdata/test.cabal | 1 + 4 files changed, 22 insertions(+) create mode 100644 plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.expected.hs create mode 100644 plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.hs diff --git a/plugins/hls-eval-plugin/test/Main.hs b/plugins/hls-eval-plugin/test/Main.hs index eb7912e694..1ae87953d2 100644 --- a/plugins/hls-eval-plugin/test/Main.hs +++ b/plugins/hls-eval-plugin/test/Main.hs @@ -90,6 +90,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" diff --git a/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.expected.hs b/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.expected.hs new file mode 100644 index 0000000000..9634dac667 --- /dev/null +++ b/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.expected.hs @@ -0,0 +1,11 @@ +-- The default language extensions for the eval plugin are the same as those for ghci + +module TSameDefaultLanguageExtensionsAsGhci where + +-- ExtendedDefaultRules + +-- >>> [] +-- [] + +-- >>> reverse [] +-- [] diff --git a/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.hs b/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.hs new file mode 100644 index 0000000000..d68b5910be --- /dev/null +++ b/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.hs @@ -0,0 +1,9 @@ +-- The default language extensions for the eval plugin are the same as those for ghci + +module TSameDefaultLanguageExtensionsAsGhci where + +-- ExtendedDefaultRules + +-- >>> [] + +-- >>> reverse [] diff --git a/plugins/hls-eval-plugin/test/testdata/test.cabal b/plugins/hls-eval-plugin/test/testdata/test.cabal index 16054eca22..bf0c950352 100644 --- a/plugins/hls-eval-plugin/test/testdata/test.cabal +++ b/plugins/hls-eval-plugin/test/testdata/test.cabal @@ -50,6 +50,7 @@ library TLanguageOptionsTupleSections TIO TProperty + TSameDefaultLanguageExtensionsAsGhci TPrelude TCPP TLHS From d837b1fe24a02e2158603224996a8dd0919d3b5e Mon Sep 17 00:00:00 2001 From: Farhad Mehta Date: Mon, 21 Jun 2021 15:15:14 +0200 Subject: [PATCH 3/5] Add fix for #1954 --- .../src/Ide/Plugin/Eval/Code.hs | 24 +++++++++---------- .../src/Ide/Plugin/Eval/CodeLens.hs | 15 ++++++++---- ...efaultLanguageExtensionsAsGhci.expected.hs | 16 +++++++++++++ .../TSameDefaultLanguageExtensionsAsGhci.hs | 15 ++++++++++++ 4 files changed, 54 insertions(+), 16 deletions(-) diff --git a/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs b/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs index f3184c128c..88ef2b4246 100644 --- a/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs +++ b/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs @@ -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) @@ -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, @@ -81,16 +80,17 @@ 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 - , ExtendedDefaultRules - ] +-- -- |GHC extensions required for expression evaluation +-- evalExtensions :: [Extension] +-- evalExtensions = +-- [ NondecreasingIndentation +-- -- , OverlappingInstances +-- -- , UndecidableInstances +-- -- , FlexibleInstances +-- -- , IncoherentInstances +-- -- , TupleSections +-- , ExtendedDefaultRules +-- ] -- |GHC declarations required for expression evaluation evalSetup :: Ghc () diff --git a/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs b/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs index b0602a3583..bfa7e3b6ff 100644 --- a/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs +++ b/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs @@ -102,18 +102,18 @@ import GHC (ExecOptions (execLineNumb setLogAction, setSessionDynFlags, setTargets, typeKind) +import qualified GHC.LanguageExtensions.Type as LangExt (Extension (..)) import GhcPlugins (DynFlags (..), defaultLogActionHPutStrDoc, gopt_set, gopt_unset, hsc_dflags, parseDynamicFlagsCmdLine, - targetPlatform, xopt_set) + targetPlatform, xopt_set, xopt_unset, xopt_set_unlessExplSpec) 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) @@ -302,8 +302,15 @@ 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' = (xopt_set_unlessExplSpec + LangExt.ExtendedDefaultRules xopt_set) + . (xopt_set_unlessExplSpec + LangExt.MonomorphismRestriction xopt_unset) + $ idflags + setInteractiveDynFlags $ df' #if MIN_VERSION_ghc(9,0,0) { unitState = unitState diff --git a/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.expected.hs b/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.expected.hs index 9634dac667..dad95db872 100644 --- a/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.expected.hs +++ b/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.expected.hs @@ -2,6 +2,16 @@ 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 -- >>> [] @@ -9,3 +19,9 @@ module TSameDefaultLanguageExtensionsAsGhci where -- >>> reverse [] -- [] + +-- NoMonomorphismRestriction + +-- >>> plus = (+) +-- >>> :t plus +-- plus :: forall a. Num a => a -> a -> a diff --git a/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.hs b/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.hs index d68b5910be..148f0f86a8 100644 --- a/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.hs +++ b/plugins/hls-eval-plugin/test/testdata/TSameDefaultLanguageExtensionsAsGhci.hs @@ -2,8 +2,23 @@ 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 From f34e6d4b2c3bbb010caa2b906b47ea715fb76e40 Mon Sep 17 00:00:00 2001 From: Farhad Mehta Date: Tue, 22 Jun 2021 09:16:42 +0200 Subject: [PATCH 4/5] Remove dependency on xopt_set_unlessExplSpec. Reason: It was breaking GHC 8.6.2 tests. --- .../hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs b/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs index fa24eec7cd..c6b418372a 100644 --- a/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs +++ b/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs @@ -114,12 +114,12 @@ import GhcPlugins (DynFlags (..), elemNameSet, gopt_set, gopt_unset, hsc_dflags, isSymOcc, mkNameSet, - parseDynamicFlagsCmdLine, pprDefinedAt, + parseDynamicFlagsCmdLine, + pprDefinedAt, pprInfixName, targetPlatform, tyThingParent_maybe, - xopt_set, xopt_unset, - xopt_set_unlessExplSpec) + xopt_set, xopt_unset) import HscTypes (InteractiveImport (IIModule), ModSummary (ms_mod), @@ -320,10 +320,8 @@ runEvalCmd st EvalParams{..} = -- 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' = (xopt_set_unlessExplSpec - LangExt.ExtendedDefaultRules xopt_set) - . (xopt_set_unlessExplSpec - LangExt.MonomorphismRestriction xopt_unset) + let df' = flip xopt_set LangExt.ExtendedDefaultRules + . flip xopt_unset LangExt.MonomorphismRestriction $ idflags setInteractiveDynFlags $ df' #if MIN_VERSION_ghc(9,0,0) From 12d3f03a507717cc836c6ba3f5a3b3ba9b5adc1c Mon Sep 17 00:00:00 2001 From: Farhad Mehta Date: Wed, 23 Jun 2021 08:47:05 +0200 Subject: [PATCH 5/5] Remove unnecessary comment. Co-authored-by: Potato Hatsue --- plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs b/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs index 88ef2b4246..7497ae783a 100644 --- a/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs +++ b/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs @@ -80,17 +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 = --- [ NondecreasingIndentation --- -- , OverlappingInstances --- -- , UndecidableInstances --- -- , FlexibleInstances --- -- , IncoherentInstances --- -- , TupleSections --- , ExtendedDefaultRules --- ] -- |GHC declarations required for expression evaluation evalSetup :: Ghc ()