From 4c7a84627556cda9491300ba61f3060e357bc3ac Mon Sep 17 00:00:00 2001
From: Xavier Denis <xldenis@gmail.com>
Date: Mon, 8 Jul 2019 14:58:46 +0200
Subject: [PATCH] Make sure to load interfaces when running :instances

---
 compiler/main/InteractiveEval.hs            |  4 +++-
 testsuite/tests/ghci/T16793/T16793.script   |  1 +
 testsuite/tests/ghci/T16793/T16793.stdout   |  9 +++++++++
 testsuite/tests/ghci/T16793/all.T           |  1 +
 testsuite/tests/ghci/scripts/ghci064.stdout | 18 ++++++++++++++++++
 5 files changed, 32 insertions(+), 1 deletion(-)
 create mode 100644 testsuite/tests/ghci/T16793/T16793.script
 create mode 100644 testsuite/tests/ghci/T16793/T16793.stdout
 create mode 100644 testsuite/tests/ghci/T16793/all.T

diff --git a/compiler/main/InteractiveEval.hs b/compiler/main/InteractiveEval.hs
index ee43aaf6751c..88c8ecc7df52 100644
--- a/compiler/main/InteractiveEval.hs
+++ b/compiler/main/InteractiveEval.hs
@@ -104,7 +104,7 @@ import GHC.Exts
 import Data.Array
 import Exception
 
-import TcRnDriver ( runTcInteractive, tcRnType )
+import TcRnDriver ( runTcInteractive, tcRnType, loadUnqualIfaces )
 import TcHsSyn          ( ZonkFlexi (SkolemiseFlexi) )
 
 import TcEnv (tcGetInstEnvs)
@@ -1048,6 +1048,8 @@ getInstancesForType :: GhcMonad m => Type -> m [ClsInst]
 getInstancesForType ty = withSession $ \hsc_env -> do
   liftIO $ runInteractiveHsc hsc_env $ do
     ioMsgMaybe $ runTcInteractive hsc_env $ do
+      -- Bring class and instances from unqualified modules into scope, this fixes #16793.
+      loadUnqualIfaces hsc_env (hsc_IC hsc_env)
       matches <- findMatchingInstances ty
       fmap catMaybes . forM matches $ uncurry checkForExistence
 
diff --git a/testsuite/tests/ghci/T16793/T16793.script b/testsuite/tests/ghci/T16793/T16793.script
new file mode 100644
index 000000000000..9344a014efa6
--- /dev/null
+++ b/testsuite/tests/ghci/T16793/T16793.script
@@ -0,0 +1 @@
+:instances Int
diff --git a/testsuite/tests/ghci/T16793/T16793.stdout b/testsuite/tests/ghci/T16793/T16793.stdout
new file mode 100644
index 000000000000..e20747293e43
--- /dev/null
+++ b/testsuite/tests/ghci/T16793/T16793.stdout
@@ -0,0 +1,9 @@
+instance Eq Int -- Defined in ‘GHC.Classes’
+instance Ord Int -- Defined in ‘GHC.Classes’
+instance Enum Int -- Defined in ‘GHC.Enum’
+instance Num Int -- Defined in ‘GHC.Num’
+instance Real Int -- Defined in ‘GHC.Real’
+instance Show Int -- Defined in ‘GHC.Show’
+instance Read Int -- Defined in ‘GHC.Read’
+instance Bounded Int -- Defined in ‘GHC.Enum’
+instance Integral Int -- Defined in ‘GHC.Real’
diff --git a/testsuite/tests/ghci/T16793/all.T b/testsuite/tests/ghci/T16793/all.T
new file mode 100644
index 000000000000..4efc926719c9
--- /dev/null
+++ b/testsuite/tests/ghci/T16793/all.T
@@ -0,0 +1 @@
+test('T16793', normal, ghci_script, ['T16793.script'])
diff --git a/testsuite/tests/ghci/scripts/ghci064.stdout b/testsuite/tests/ghci/scripts/ghci064.stdout
index 2b841138ce28..91298153fc06 100644
--- a/testsuite/tests/ghci/scripts/ghci064.stdout
+++ b/testsuite/tests/ghci/scripts/ghci064.stdout
@@ -3,27 +3,45 @@ instance Applicative Maybe -- Defined in ‘GHC.Base’
 instance Functor Maybe -- Defined in ‘GHC.Base’
 instance Monad Maybe -- Defined in ‘GHC.Base’
 instance GHC.Base.MonadPlus Maybe -- Defined in ‘GHC.Base’
+instance MonadFail Maybe -- Defined in ‘Control.Monad.Fail’
+instance Foldable Maybe -- Defined in ‘Data.Foldable’
+instance Traversable Maybe -- Defined in ‘Data.Traversable’
 instance Eq _ => Eq (Maybe _) -- Defined in ‘GHC.Maybe’
 instance Semigroup _ => Monoid (Maybe _) -- Defined in ‘GHC.Base’
 instance Ord _ => Ord (Maybe _) -- Defined in ‘GHC.Maybe’
 instance Semigroup _ => Semigroup (Maybe _)
   -- Defined in ‘GHC.Base’
 instance Show _ => Show (Maybe _) -- Defined in ‘GHC.Show’
+instance Read _ => Read (Maybe _) -- Defined in ‘GHC.Read’
+instance GHC.Generics.Generic (Maybe _)
+  -- Defined in ‘GHC.Generics’
+instance GHC.Generics.SingKind _ => GHC.Generics.SingKind (Maybe _)
+  -- Defined in ‘GHC.Generics’
 instance Eq _ => Eq [_] -- Defined in ‘GHC.Classes’
 instance Monoid [_] -- Defined in ‘GHC.Base’
 instance Ord _ => Ord [_] -- Defined in ‘GHC.Classes’
 instance Semigroup [_] -- Defined in ‘GHC.Base’
 instance Show _ => Show [_] -- Defined in ‘GHC.Show’
+instance Read _ => Read [_] -- Defined in ‘GHC.Read’
+instance GHC.Generics.Generic [_] -- Defined in ‘GHC.Generics’
 instance [safe] MyShow _ => MyShow [_]
   -- Defined at ghci064.hs:7:10
 instance Monoid [T] -- Defined in ‘GHC.Base’
 instance Semigroup [T] -- Defined in ‘GHC.Base’
+instance GHC.Generics.Generic [T] -- Defined in ‘GHC.Generics’
 instance [safe] MyShow [T] -- Defined at ghci064.hs:7:10
 instance [safe] MyShow [T] -- Defined at ghci064.hs:15:10
 instance Eq Bool -- Defined in ‘GHC.Classes’
 instance Ord Bool -- Defined in ‘GHC.Classes’
 instance Enum Bool -- Defined in ‘GHC.Enum’
 instance Show Bool -- Defined in ‘GHC.Show’
+instance Read Bool -- Defined in ‘GHC.Read’
 instance Bounded Bool -- Defined in ‘GHC.Enum’
+instance GHC.Generics.Generic Bool -- Defined in ‘GHC.Generics’
+instance GHC.Arr.Ix Bool -- Defined in ‘GHC.Arr’
+instance GHC.Generics.SingKind Bool -- Defined in ‘GHC.Generics’
+instance Data.Bits.Bits Bool -- Defined in ‘Data.Bits’
+instance Data.Bits.FiniteBits Bool -- Defined in ‘Data.Bits’
 instance Functor ((,) Int) -- Defined in ‘GHC.Base’
 instance Foldable ((,) Int) -- Defined in ‘Data.Foldable’
+instance Traversable ((,) Int) -- Defined in ‘Data.Traversable’
-- 
GitLab