From b59f10427e219bb03d823a2c46487565e7455539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Ackerman?= <6054733+akrmn@users.noreply.github.com> Date: Wed, 17 Mar 2021 19:17:19 +0100 Subject: [PATCH] Ensure eval plugin Print class doesn't rely on Prelude being in scope --- .../hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs | 15 +++++++++------ plugins/hls-eval-plugin/test/Eval.hs | 2 ++ .../test/testdata/TNoImplicitPrelude.hs | 10 ++++++++++ .../test/testdata/TNoImplicitPrelude.hs.expected | 11 +++++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 plugins/hls-eval-plugin/test/testdata/TNoImplicitPrelude.hs create mode 100644 plugins/hls-eval-plugin/test/testdata/TNoImplicitPrelude.hs.expected 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 b058a6a278..5ad4537338 100644 --- a/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs +++ b/plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Code.hs @@ -10,7 +10,7 @@ import qualified Data.List.NonEmpty as NE import Data.String (IsString) import qualified Data.Text as T import Development.IDE.Types.Location (Position (..), Range (..)) -import GHC (compileExpr) +import GHC (InteractiveImport (IIDecl), compileExpr) import GHC.LanguageExtensions.Type (Extension (..)) import GhcMonad (Ghc, GhcMonad, liftIO) import Ide.Plugin.Eval.Types (Language (Plain), Loc, @@ -18,7 +18,7 @@ import Ide.Plugin.Eval.Types (Language (Plain), Loc, Section (sectionLanguage), Test (..), Txt, locate, locate0) -import InteractiveEval (runDecls) +import InteractiveEval (getContext, parseImportDecl, runDecls, setContext) import Language.LSP.Types.Lens (line, start) import Unsafe.Coerce (unsafeCoerce) @@ -95,12 +95,15 @@ evalExtensions = -- |GHC declarations required for expression evaluation evalSetup :: Ghc () -evalSetup = +evalSetup = do + preludeAsP <- parseImportDecl "import qualified Prelude as P" + context <- getContext + setContext (IIDecl preludeAsP : context) mapM_ runDecls - [ "class Print f where asPrint :: f -> IO String" - , "instance Show a => Print (IO a) where asPrint io = io >>= return . show" - , "instance Show a => Print a where asPrint a = return (show a)" + [ "class Print f where asPrint :: f -> P.IO P.String" + , "instance P.Show a => Print (P.IO a) where asPrint io = io P.>>= P.return P.. P.show" + , "instance P.Show a => Print a where asPrint a = P.return (P.show a)" ] {- |GHC declarations required to execute test properties diff --git a/plugins/hls-eval-plugin/test/Eval.hs b/plugins/hls-eval-plugin/test/Eval.hs index 9e05a942e6..06b50af087 100644 --- a/plugins/hls-eval-plugin/test/Eval.hs +++ b/plugins/hls-eval-plugin/test/Eval.hs @@ -155,6 +155,8 @@ tests = $ testCase "Literate Haskell Bird Style" $ goldenTest "TLHS.lhs" -- , testCase "Literate Haskell LaTeX Style" $ goldenTest "TLHSLateX.lhs" ] + , testCase "Works with NoImplicitPrelude" + $ goldenTest "TNoImplicitPrelude.hs" ] goldenTest :: FilePath -> IO () diff --git a/plugins/hls-eval-plugin/test/testdata/TNoImplicitPrelude.hs b/plugins/hls-eval-plugin/test/testdata/TNoImplicitPrelude.hs new file mode 100644 index 0000000000..521fb6a87a --- /dev/null +++ b/plugins/hls-eval-plugin/test/testdata/TNoImplicitPrelude.hs @@ -0,0 +1,10 @@ +{-# LANGUAGE NoImplicitPrelude #-} + +module TNoImplicitPrelude where + +import Data.List (unwords) +import Data.String (String) + +-- >>> unwords example +example :: [String] +example = ["This","is","an","example","of","evaluation"] diff --git a/plugins/hls-eval-plugin/test/testdata/TNoImplicitPrelude.hs.expected b/plugins/hls-eval-plugin/test/testdata/TNoImplicitPrelude.hs.expected new file mode 100644 index 0000000000..c0dfae5983 --- /dev/null +++ b/plugins/hls-eval-plugin/test/testdata/TNoImplicitPrelude.hs.expected @@ -0,0 +1,11 @@ +{-# LANGUAGE NoImplicitPrelude #-} + +module TNoImplicitPrelude where + +import Data.List (unwords) +import Data.String (String) + +-- >>> unwords example +-- "This is an example of evaluation" +example :: [String] +example = ["This","is","an","example","of","evaluation"]