Load plugins in interactive session
authorChristiaan Baaij <christiaan.baaij@gmail.com>
Thu, 22 Nov 2018 16:50:51 +0000 (11:50 -0500)
committerBen Gamari <ben@smart-cactus.org>
Thu, 22 Nov 2018 19:01:35 +0000 (14:01 -0500)
Reviewers: bgamari, tdammers

Reviewed By: tdammers

Subscribers: monoidal, rwbarton, carter

GHC Trac Issues: #15633

Differential Revision: https://phabricator.haskell.org/D5348

(cherry picked from commit 599eaada382d04722219bfc319bde94591be3fb1)

14 files changed:
ghc/GHCi/UI.hs
ghc/Main.hs
testsuite/tests/ghci/should_run/T15633a.script [new file with mode: 0644]
testsuite/tests/ghci/should_run/T15633a.stderr [new file with mode: 0644]
testsuite/tests/ghci/should_run/T15633a.stdout [new file with mode: 0644]
testsuite/tests/ghci/should_run/T15633b.script [new file with mode: 0644]
testsuite/tests/ghci/should_run/T15633b.stderr [new file with mode: 0644]
testsuite/tests/ghci/should_run/T15633b.stdout [new file with mode: 0644]
testsuite/tests/ghci/should_run/all.T
testsuite/tests/ghci/should_run/tc-plugin-ghci/LICENSE [new file with mode: 0644]
testsuite/tests/ghci/should_run/tc-plugin-ghci/Makefile [new file with mode: 0644]
testsuite/tests/ghci/should_run/tc-plugin-ghci/Setup.hs [new file with mode: 0644]
testsuite/tests/ghci/should_run/tc-plugin-ghci/TcPluginGHCi.hs [new file with mode: 0644]
testsuite/tests/ghci/should_run/tc-plugin-ghci/tc-plugin-ghci.cabal [new file with mode: 0644]

index f36a42d..f9ee7ce 100644 (file)
@@ -68,6 +68,8 @@ import qualified Lexer
 import StringBuffer
 import Outputable hiding ( printForUser, printForUserPartWay )
 
+import DynamicLoading ( initializePlugins )
+
 -- Other random utilities
 import BasicTypes hiding ( isTopLevel )
 import Config
@@ -2720,10 +2722,14 @@ newDynFlags interactive_only minus_opts = do
 
       when (interactive_only && packageFlagsChanged idflags1 idflags0) $ do
           liftIO $ hPutStrLn stderr "cannot set package flags with :seti; use :set"
-      GHC.setInteractiveDynFlags idflags1
+      -- Load any new plugins
+      hsc_env0 <- GHC.getSession
+      idflags2 <- liftIO (initializePlugins hsc_env0 idflags1)
+      GHC.setInteractiveDynFlags idflags2
       installInteractivePrint (interactivePrint idflags1) False
 
       dflags0 <- getDynFlags
+
       when (not interactive_only) $ do
         (dflags1, _, _) <- liftIO $ GHC.parseDynamicFlags dflags0 lopts
         new_pkgs <- GHC.setProgramDynFlags dflags1
index 276546b..eda6a3d 100644 (file)
@@ -56,6 +56,7 @@ import Util
 import Panic
 import UniqSupply
 import MonadUtils       ( liftIO )
+import DynamicLoading   ( initializePlugins )
 
 -- Imports for --abi-hash
 import LoadIface           ( loadUserInterface )
@@ -244,8 +245,9 @@ main' postLoadMode dflags0 args flagWarnings = do
        DoMake                 -> doMake srcs
        DoMkDependHS           -> doMkDependHS (map fst srcs)
        StopBefore p           -> liftIO (oneShot hsc_env p srcs)
-       DoInteractive          -> ghciUI srcs Nothing
-       DoEval exprs           -> ghciUI srcs $ Just $ reverse exprs
+       DoInteractive          -> ghciUI hsc_env dflags6 srcs Nothing
+       DoEval exprs           -> ghciUI hsc_env dflags6 srcs $ Just $
+                                   reverse exprs
        DoAbiHash              -> abiHash (map fst srcs)
        ShowPackages           -> liftIO $ showPackages dflags6
        DoFrontend f           -> doFrontend f srcs
@@ -253,11 +255,16 @@ main' postLoadMode dflags0 args flagWarnings = do
 
   liftIO $ dumpFinalStats dflags6
 
-ghciUI :: [(FilePath, Maybe Phase)] -> Maybe [String] -> Ghc ()
+ghciUI :: HscEnv -> DynFlags -> [(FilePath, Maybe Phase)] -> Maybe [String]
+       -> Ghc ()
 #if !defined(GHCI)
-ghciUI _ _ = throwGhcException (CmdLineError "not built for interactive use")
+ghciUI _ _ _ _ =
+  throwGhcException (CmdLineError "not built for interactive use")
 #else
-ghciUI     = interactiveUI defaultGhciSettings
+ghciUI hsc_env dflags0 srcs maybe_expr = do
+  dflags1 <- liftIO (initializePlugins hsc_env dflags0)
+  _ <- GHC.setSessionDynFlags dflags1
+  interactiveUI defaultGhciSettings srcs maybe_expr
 #endif
 
 -- -----------------------------------------------------------------------------
diff --git a/testsuite/tests/ghci/should_run/T15633a.script b/testsuite/tests/ghci/should_run/T15633a.script
new file mode 100644 (file)
index 0000000..dabd06c
--- /dev/null
@@ -0,0 +1 @@
+:t True
diff --git a/testsuite/tests/ghci/should_run/T15633a.stderr b/testsuite/tests/ghci/should_run/T15633a.stderr
new file mode 100644 (file)
index 0000000..986efb1
--- /dev/null
@@ -0,0 +1 @@
+TcPluginGHCi
diff --git a/testsuite/tests/ghci/should_run/T15633a.stdout b/testsuite/tests/ghci/should_run/T15633a.stdout
new file mode 100644 (file)
index 0000000..7ca26c4
--- /dev/null
@@ -0,0 +1 @@
+True :: Bool
diff --git a/testsuite/tests/ghci/should_run/T15633b.script b/testsuite/tests/ghci/should_run/T15633b.script
new file mode 100644 (file)
index 0000000..361fac8
--- /dev/null
@@ -0,0 +1,2 @@
+:set -fplugin TcPluginGHCi
+:t True
diff --git a/testsuite/tests/ghci/should_run/T15633b.stderr b/testsuite/tests/ghci/should_run/T15633b.stderr
new file mode 100644 (file)
index 0000000..986efb1
--- /dev/null
@@ -0,0 +1 @@
+TcPluginGHCi
diff --git a/testsuite/tests/ghci/should_run/T15633b.stdout b/testsuite/tests/ghci/should_run/T15633b.stdout
new file mode 100644 (file)
index 0000000..7ca26c4
--- /dev/null
@@ -0,0 +1 @@
+True :: Bool
index 02ef665..d9ccd27 100644 (file)
@@ -34,3 +34,21 @@ test('T14963a', just_ghci, ghci_script, ['T14963a.script'])
 test('T14963b', just_ghci, ghci_script, ['T14963b.script'])
 test('T14963c', [extra_hc_opts("-fdefer-type-errors")], ghci_script, ['T14963c.script'])
 test('T15806',     just_ghci, ghci_script, ['T15806.script'])
+
+test('T15633a',
+     [extra_files(['tc-plugin-ghci/']),
+      when(opsys('mingw32'), multi_cpu_race),
+      only_ways(['ghci']),
+      pre_cmd('$MAKE -s --no-print-directory -C tc-plugin-ghci package.plugins01 TOP={top}'),
+      extra_hc_opts("-package-db tc-plugin-ghci/pkg.plugins01/local.package.conf -fplugin TcPluginGHCi")
+      ],
+     ghci_script, ['T15633a.script'])
+
+test('T15633b',
+     [extra_files(['tc-plugin-ghci/']),
+      when(opsys('mingw32'), multi_cpu_race),
+      only_ways(['ghci']),
+      pre_cmd('$MAKE -s --no-print-directory -C tc-plugin-ghci package.plugins01 TOP={top}'),
+      extra_hc_opts("-package-db tc-plugin-ghci/pkg.plugins01/local.package.conf")
+      ],
+     ghci_script, ['T15633b.script'])
diff --git a/testsuite/tests/ghci/should_run/tc-plugin-ghci/LICENSE b/testsuite/tests/ghci/should_run/tc-plugin-ghci/LICENSE
new file mode 100644 (file)
index 0000000..6297f71
--- /dev/null
@@ -0,0 +1,10 @@
+Copyright (c) 2008, Max Bolingbroke
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+    * Neither the name of Max Bolingbroke nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/testsuite/tests/ghci/should_run/tc-plugin-ghci/Makefile b/testsuite/tests/ghci/should_run/tc-plugin-ghci/Makefile
new file mode 100644 (file)
index 0000000..9ee7737
--- /dev/null
@@ -0,0 +1,23 @@
+TOP=../../..
+RUN=-DRUN1
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
+
+clean.%:
+       rm -rf pkg.$*
+
+HERE := $(abspath .)
+$(eval $(call canonicalise,HERE))
+
+package.%:
+       $(MAKE) -s --no-print-directory clean.$*
+       mkdir pkg.$*
+       "$(TEST_HC)" -outputdir pkg.$* --make -v0 -o pkg.$*/setup Setup.hs
+
+       "$(GHC_PKG)" init pkg.$*/local.package.conf
+
+  # The bogus extra-lib-dirs ensures the package is registered with multiple
+  # dynamic-library-directories which tests that the fix for #15475 works
+       pkg.$*/setup configure --distdir pkg.$*/dist -v0 $(CABAL_PLUGIN_BUILD) --ghc-option="$(RUN)" --prefix="$(HERE)/pkg.$*/install" --with-compiler="$(TEST_HC)" --with-hc-pkg="$(GHC_PKG)" --extra-lib-dirs="$(HERE)" --package-db=pkg.$*/local.package.conf $(if $(findstring YES,$(HAVE_PROFILING)), --enable-library-profiling)
+       pkg.$*/setup build     --distdir pkg.$*/dist -v0
+       pkg.$*/setup install   --distdir pkg.$*/dist -v0
diff --git a/testsuite/tests/ghci/should_run/tc-plugin-ghci/Setup.hs b/testsuite/tests/ghci/should_run/tc-plugin-ghci/Setup.hs
new file mode 100644 (file)
index 0000000..e8ef27d
--- /dev/null
@@ -0,0 +1,3 @@
+import Distribution.Simple
+
+main = defaultMain
diff --git a/testsuite/tests/ghci/should_run/tc-plugin-ghci/TcPluginGHCi.hs b/testsuite/tests/ghci/should_run/tc-plugin-ghci/TcPluginGHCi.hs
new file mode 100644 (file)
index 0000000..c8a3bb6
--- /dev/null
@@ -0,0 +1,15 @@
+module TcPluginGHCi where
+
+import TcRnMonad ( TcPlugin(..), TcPluginResult(..) )
+import Plugins ( defaultPlugin, Plugin(..), CommandLineOption )
+import Debug.Trace
+
+plugin :: Plugin
+plugin = defaultPlugin { tcPlugin = Just . thePlugin }
+
+thePlugin :: [CommandLineOption] -> TcPlugin
+thePlugin opts = TcPlugin
+  { tcPluginInit  = trace "TcPluginGHCi" (return ())
+  , tcPluginSolve = \_ _ _ _ -> return $ TcPluginOk [] []
+  , tcPluginStop  = \_ -> return ()
+  }
diff --git a/testsuite/tests/ghci/should_run/tc-plugin-ghci/tc-plugin-ghci.cabal b/testsuite/tests/ghci/should_run/tc-plugin-ghci/tc-plugin-ghci.cabal
new file mode 100644 (file)
index 0000000..fdcbef9
--- /dev/null
@@ -0,0 +1,17 @@
+Name:           tc-plugin-ghci
+Version:        0.1
+Synopsis:       Testing type-checker plugins in ghci
+Cabal-Version:  >= 1.2
+Build-Type:     Simple
+License:        BSD3
+License-File:   LICENSE
+Author:         Matthew Pickering
+Homepage:       http://blog.omega-prime.co.uk
+
+Library
+    Extensions:     CPP
+    Build-Depends:
+        base,
+        ghc >= 6.11
+    Exposed-Modules:
+        TcPluginGHCi