summaryrefslogtreecommitdiff
path: root/testsuite/tests/plugins/simple-plugin
diff options
context:
space:
mode:
authorSylvain Henry <sylvain@haskus.fr>2021-09-10 10:52:37 +0200
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-10-08 13:08:16 -0400
commit3d31f11e83a3d60d17459457e036ae387fc95323 (patch)
tree86def0d3861e72c25688fd28f761bdaa7dc19b67 /testsuite/tests/plugins/simple-plugin
parent01f5324f8eaa2ce28d617922bc4d3b680ad4fc38 (diff)
downloadhaskell-3d31f11e83a3d60d17459457e036ae387fc95323.tar.gz
Don't link plugins' units with target code (#20218)
Before this patch, plugin units were linked with the target code even when the unit was passed via `-plugin-package`. This is an issue to support plugins in cross-compilers (plugins are definitely not ABI compatible with target code). We now clearly separate unit dependencies for plugins and unit dependencies for target code and only link the latter ones. We've also added a test to ensure that plugin units passed via `-package` are linked with target code so that `thNameToGhcName` can still be used in plugins that need it (see T20218b).
Diffstat (limited to 'testsuite/tests/plugins/simple-plugin')
-rw-r--r--testsuite/tests/plugins/simple-plugin/Simple/ReplacePlugin.hs55
-rw-r--r--testsuite/tests/plugins/simple-plugin/simple-plugin.cabal1
2 files changed, 56 insertions, 0 deletions
diff --git a/testsuite/tests/plugins/simple-plugin/Simple/ReplacePlugin.hs b/testsuite/tests/plugins/simple-plugin/Simple/ReplacePlugin.hs
new file mode 100644
index 0000000000..b20f3fe80a
--- /dev/null
+++ b/testsuite/tests/plugins/simple-plugin/Simple/ReplacePlugin.hs
@@ -0,0 +1,55 @@
+{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE LambdaCase #-}
+
+module Simple.ReplacePlugin(plugin) where
+
+import GHC.Types.Unique.FM
+import GHC.Plugins
+import qualified GHC.Utils.Error
+import GHC.Types.TyThing
+
+import Debug.Trace
+import Data.Bifunctor (second)
+import Control.Monad
+import qualified Language.Haskell.TH as TH
+import Data.List (isSuffixOf)
+
+woz :: Int -> Int
+woz x = trace ("Got " ++ show x) x
+
+plugin :: Plugin
+plugin = defaultPlugin {
+ installCoreToDos = install,
+ pluginRecompile = purePlugin
+ }
+
+install :: [CommandLineOption] -> [CoreToDo] -> CoreM [CoreToDo]
+install options todos = do
+ mb <- thNameToGhcName 'woz
+ case mb of
+ Nothing -> error "Failed to locate woz"
+ Just m -> do
+ rep <- lookupId m
+ return $ CoreDoPluginPass "Replace wiz with woz" (fixGuts rep) : todos
+
+fixGuts :: Id -> ModGuts -> CoreM ModGuts
+fixGuts rep guts = pure $ guts { mg_binds = fmap fix_bind (mg_binds guts) }
+ where
+ fix_bind (NonRec b e) = NonRec b (fix_expr e)
+ fix_bind (Rec bes) = Rec (fmap (second fix_expr) bes)
+
+ fix_expr :: CoreExpr -> CoreExpr
+ fix_expr = \case
+ Var i -> if "$wiz" `isSuffixOf` nameStableString (idName i)
+ then Var rep
+ else Var i
+ Lit l -> Lit l
+ App e1 e2 -> App (fix_expr e1) (fix_expr e2)
+ Lam b e -> Lam b (fix_expr e)
+ Case e b t as -> Case (fix_expr e) b t (map fix_alt as)
+ Cast e c -> Cast (fix_expr e) c
+ Tick t e -> Tick t (fix_expr e)
+ Type t -> Type t
+ Coercion c -> Coercion c
+
+ fix_alt (Alt c bs e) = Alt c bs (fix_expr e)
diff --git a/testsuite/tests/plugins/simple-plugin/simple-plugin.cabal b/testsuite/tests/plugins/simple-plugin/simple-plugin.cabal
index af68c5ca3b..c5297440c9 100644
--- a/testsuite/tests/plugins/simple-plugin/simple-plugin.cabal
+++ b/testsuite/tests/plugins/simple-plugin/simple-plugin.cabal
@@ -22,3 +22,4 @@ Library
Simple.RemovePlugin
Simple.TrustworthyPlugin
Simple.DefaultPlugin
+ Simple.ReplacePlugin