From 8ea3d18bea9021035f8cfb0693469f2db682638a Mon Sep 17 00:00:00 2001
From: Simon Peyton Jones <simonpj@microsoft.com>
Date: Sat, 22 Mar 2014 23:18:14 +0000
Subject: [PATCH] Simplify handling of the interactive package; fixes Trac
 #8831

This patch is really a fix to the big commint
   73c08ab10e4077e18e459a1325996bff110360c3
   Re-work the naming story for the GHCi prompt (Trac #8649)
which introduced the 'interactive' package
See Note [The interactive package] in HscTypes

The original commit set both
  (a) The tcg_mod field of TcGblEnv to 'interactive:Ghci4' (say)
  (b) The thisPackage field of DynFlags to 'interactive'

But the second step interacts badly with linking.  :loaded modules are
in the package set by 'thisPackage' (usually 'main'); if you change
that, then we try to link package 'main', but can't find it, and
that is what happened in #8831.

The fix was simple: do (a) but not (b).

I changed Note [The interactive package] in HscTypes to describe this.

(cherry picked from commit 28e8d878b63d06824001ac3a631254679e0f1960)
---
 compiler/main/HscMain.hs   | 11 ++---------
 compiler/main/HscTypes.lhs | 11 +++++++----
 2 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/compiler/main/HscMain.hs b/compiler/main/HscMain.hs
index 04b0823db41c..748f7480ec24 100644
--- a/compiler/main/HscMain.hs
+++ b/compiler/main/HscMain.hs
@@ -1357,11 +1357,7 @@ hscStmtWithLocation hsc_env0 stmt source linenumber =
         Just parsed_stmt -> do
             -- Rename and typecheck it
             hsc_env <- getHscEnv
-            let interactive_hsc_env = setInteractivePackage hsc_env
-                  -- Bindings created here belong to the interactive package
-                  -- See Note [The interactive package] in HscTypes
-                  -- (NB: maybe not necessary, since Stmts bind only Ids)
-            (ids, tc_expr, fix_env) <- ioMsgMaybe $ tcRnStmt interactive_hsc_env parsed_stmt
+            (ids, tc_expr, fix_env) <- ioMsgMaybe $ tcRnStmt hsc_env parsed_stmt
 
             -- Desugar it
             ds_expr <- ioMsgMaybe $ deSugarExpr hsc_env tc_expr
@@ -1397,10 +1393,7 @@ hscDeclsWithLocation hsc_env0 str source linenumber =
 
     {- Rename and typecheck it -}
     hsc_env <- getHscEnv
-    let interactive_hsc_env = setInteractivePackage hsc_env
-            -- Bindings created here belong to the interactive package
-            -- See Note [The interactive package] in HscTypes
-    tc_gblenv <- ioMsgMaybe $ tcRnDeclsi interactive_hsc_env decls
+    tc_gblenv <- ioMsgMaybe $ tcRnDeclsi hsc_env decls
 
     {- Grab the new instances -}
     -- We grab the whole environment because of the overlapping that may have
diff --git a/compiler/main/HscTypes.lhs b/compiler/main/HscTypes.lhs
index c4c5efd43c08..6fcf8e24a724 100644
--- a/compiler/main/HscTypes.lhs
+++ b/compiler/main/HscTypes.lhs
@@ -1140,10 +1140,13 @@ The details are a bit tricky though:
    package to which :load'ed modules are added to.
 
  * So how do we arrange that declarations at the command prompt get
-   to be in the 'interactive' package?  By setting 'thisPackage' just
-   before the typecheck/rename step for command-line processing;
-   see the calls to HscTypes.setInteractivePackage in
-   HscMain.hscDeclsWithLocation and hscStmtWithLocation.
+   to be in the 'interactive' package?  Simply by setting the tcg_mod
+   field of the TcGblEnv to "interactive:Ghci1".  This is done by the
+   call to initTc in initTcInteractive, initTcForLookup, which in 
+   turn get the module from it 'icInteractiveModule' field of the 
+   interactive context.
+
+   The 'thisPackage' field stays as 'main' (or whatever -package-name says.
 
  * The main trickiness is that the type environment (tcg_type_env and
    fixity envt (tcg_fix_env) now contains entities from all the
-- 
GitLab