Skip to content

Commit 3f2ea7c

Browse files
authored
Ensure eval plugin Print class doesn't rely on Prelude being in scope (#1587)
1 parent a34d927 commit 3f2ea7c

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import qualified Data.List.NonEmpty as NE
1010
import Data.String (IsString)
1111
import qualified Data.Text as T
1212
import Development.IDE.Types.Location (Position (..), Range (..))
13-
import GHC (compileExpr)
13+
import GHC (InteractiveImport (IIDecl), compileExpr)
1414
import GHC.LanguageExtensions.Type (Extension (..))
1515
import GhcMonad (Ghc, GhcMonad, liftIO)
1616
import Ide.Plugin.Eval.Types (Language (Plain), Loc,
1717
Located (..),
1818
Section (sectionLanguage),
1919
Test (..), Txt, locate,
2020
locate0)
21-
import InteractiveEval (runDecls)
21+
import InteractiveEval (getContext, parseImportDecl, runDecls, setContext)
2222
import Language.LSP.Types.Lens (line, start)
2323
import Unsafe.Coerce (unsafeCoerce)
2424

@@ -95,12 +95,15 @@ evalExtensions =
9595

9696
-- |GHC declarations required for expression evaluation
9797
evalSetup :: Ghc ()
98-
evalSetup =
98+
evalSetup = do
99+
preludeAsP <- parseImportDecl "import qualified Prelude as P"
100+
context <- getContext
101+
setContext (IIDecl preludeAsP : context)
99102
mapM_
100103
runDecls
101-
[ "class Print f where asPrint :: f -> IO String"
102-
, "instance Show a => Print (IO a) where asPrint io = io >>= return . show"
103-
, "instance Show a => Print a where asPrint a = return (show a)"
104+
[ "class Print f where asPrint :: f -> P.IO P.String"
105+
, "instance P.Show a => Print (P.IO a) where asPrint io = io P.>>= P.return P.. P.show"
106+
, "instance P.Show a => Print a where asPrint a = P.return (P.show a)"
104107
]
105108

106109
{- |GHC declarations required to execute test properties

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ tests =
155155
$ testCase "Literate Haskell Bird Style" $ goldenTest "TLHS.lhs"
156156
-- , testCase "Literate Haskell LaTeX Style" $ goldenTest "TLHSLateX.lhs"
157157
]
158+
, testCase "Works with NoImplicitPrelude"
159+
$ goldenTest "TNoImplicitPrelude.hs"
158160
]
159161

160162
goldenTest :: FilePath -> IO ()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
3+
module TNoImplicitPrelude where
4+
5+
import Data.List (unwords)
6+
import Data.String (String)
7+
8+
-- >>> unwords example
9+
example :: [String]
10+
example = ["This","is","an","example","of","evaluation"]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE NoImplicitPrelude #-}
2+
3+
module TNoImplicitPrelude where
4+
5+
import Data.List (unwords)
6+
import Data.String (String)
7+
8+
-- >>> unwords example
9+
-- "This is an example of evaluation"
10+
example :: [String]
11+
example = ["This","is","an","example","of","evaluation"]

0 commit comments

Comments
 (0)