diff options
author | Simon Peyton Jones <simonpj@microsoft.com> | 2013-11-26 10:47:16 +0000 |
---|---|---|
committer | Simon Peyton Jones <simonpj@microsoft.com> | 2013-11-28 15:59:47 +0000 |
commit | f3a841612e754536d635e8c970fb67453fb57cab (patch) | |
tree | f9cbe69322cb6603643793a82b362fc765094cdc | |
parent | a8ac471d435214dbdc1fa70f938c63128993a1db (diff) | |
download | haskell-f3a841612e754536d635e8c970fb67453fb57cab.tar.gz |
More faff to get GHCi's top-level environment right
This fixes #8540 (again), and simplifies matters a bit more. In
particular, I got rid of ic_sys_vars altogether. Mostly they can just
go in ic_tythings, apart from dfuns, which are readily gettable from
the instances anyway.
See documentation in Note [Initialising the type environment for GHCi]
in TcEnv.
-rw-r--r-- | compiler/deSugar/Desugar.lhs | 34 | ||||
-rw-r--r-- | compiler/main/HscMain.hs | 55 | ||||
-rw-r--r-- | compiler/main/HscTypes.lhs | 46 | ||||
-rw-r--r-- | compiler/simplCore/CoreMonad.lhs | 16 | ||||
-rw-r--r-- | compiler/typecheck/TcEnv.lhs | 113 | ||||
-rw-r--r-- | compiler/typecheck/TcRnDriver.lhs | 84 |
6 files changed, 190 insertions, 158 deletions
diff --git a/compiler/deSugar/Desugar.lhs b/compiler/deSugar/Desugar.lhs index f0d28d0ba5..709f2fed0c 100644 --- a/compiler/deSugar/Desugar.lhs +++ b/compiler/deSugar/Desugar.lhs @@ -17,6 +17,7 @@ import MkIface import Id import Name import Type +import FamInstEnv import InstEnv import Class import Avail @@ -28,13 +29,12 @@ import DsExpr import DsBinds import DsForeign import Module -import RdrName import NameSet import NameEnv -import FamInstEnv ( FamInstEnv ) import Rules import BasicTypes ( Activation(.. ) ) import CoreMonad ( endPass, CoreToDo(..) ) +import PrelNames ( iNTERACTIVE ) import FastString import ErrUtils import Outputable @@ -218,29 +218,29 @@ and Rec the rest. \begin{code} -deSugarExpr :: HscEnv - -> Module -> GlobalRdrEnv -> TypeEnv -> FamInstEnv - -> LHsExpr Id - -> IO (Messages, Maybe CoreExpr) --- Prints its own errors; returns Nothing if error occurred +deSugarExpr :: HscEnv -> LHsExpr Id -> IO (Messages, Maybe CoreExpr) + +deSugarExpr hsc_env tc_expr + = do { let dflags = hsc_dflags hsc_env + icntxt = hsc_IC hsc_env + rdr_env = ic_rn_gbl_env icntxt + type_env = mkTypeEnvWithImplicits (ic_tythings icntxt) + fam_insts = snd (ic_instances icntxt) + fam_inst_env = extendFamInstEnvList emptyFamInstEnv fam_insts + -- This stuff is a half baked version of TcRnDriver.setInteractiveContext -deSugarExpr hsc_env this_mod rdr_env type_env fam_inst_env tc_expr - = do { let dflags = hsc_dflags hsc_env ; showPass dflags "Desugar" -- Do desugaring - ; (msgs, mb_core_expr) <- initDs hsc_env this_mod rdr_env + ; (msgs, mb_core_expr) <- initDs hsc_env iNTERACTIVE rdr_env type_env fam_inst_env $ dsLExpr tc_expr - ; case mb_core_expr of { - Nothing -> return (msgs, Nothing) ; - Just expr -> - - -- Dump output - do { dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr expr) + ; case mb_core_expr of + Nothing -> return () + Just expr -> dumpIfSet_dyn dflags Opt_D_dump_ds "Desugared" (pprCoreExpr expr) - ; return (msgs, Just expr) } } } + ; return (msgs, mb_core_expr) } \end{code} %************************************************************************ diff --git a/compiler/main/HscMain.hs b/compiler/main/HscMain.hs index 417fee4ce4..a2d87a5e2d 100644 --- a/compiler/main/HscMain.hs +++ b/compiler/main/HscMain.hs @@ -286,9 +286,8 @@ hscTcRnGetInfo hsc_env0 name = runInteractiveHsc hsc_env0 $ do #ifdef GHCI hscIsGHCiMonad :: HscEnv -> String -> IO Name -hscIsGHCiMonad hsc_env name = - let icntxt = hsc_IC hsc_env - in runHsc hsc_env $ ioMsgMaybe $ isGHCiMonad hsc_env icntxt name +hscIsGHCiMonad hsc_env name + = runHsc hsc_env $ ioMsgMaybe $ isGHCiMonad hsc_env name hscGetModuleInterface :: HscEnv -> Module -> IO ModIface hscGetModuleInterface hsc_env0 mod = runInteractiveHsc hsc_env0 $ do @@ -1353,25 +1352,18 @@ hscStmtWithLocation hsc_env0 stmt source linenumber = Nothing -> return Nothing Just parsed_stmt -> do - let icntxt = hsc_IC hsc_env - rdr_env = ic_rn_gbl_env icntxt - type_env = mkTypeEnvWithImplicits (ic_tythings icntxt) - fam_insts = snd (ic_instances icntxt) - fam_inst_env = extendFamInstEnvList emptyFamInstEnv fam_insts - src_span = srcLocSpan interactiveSrcLoc - -- Rename and typecheck it -- Here we lift the stmt into the IO monad, see Note -- [Interactively-bound Ids in GHCi] in TcRnDriver - (ids, tc_expr, fix_env) <- ioMsgMaybe $ tcRnStmt hsc_env icntxt parsed_stmt + (ids, tc_expr, fix_env) <- ioMsgMaybe $ tcRnStmt hsc_env parsed_stmt -- Desugar it - ds_expr <- ioMsgMaybe $ - deSugarExpr hsc_env iNTERACTIVE rdr_env type_env fam_inst_env tc_expr + ds_expr <- ioMsgMaybe $ deSugarExpr hsc_env tc_expr liftIO (lintInteractiveExpr "desugar expression" hsc_env ds_expr) handleWarnings -- Then code-gen, and link it + let src_span = srcLocSpan interactiveSrcLoc hval <- liftIO $ hscCompileCoreExpr hsc_env src_span ds_expr let hval_io = unsafeCoerce# hval :: IO [HValue] @@ -1396,8 +1388,7 @@ hscDeclsWithLocation hsc_env0 str source linenumber = hscParseThingWithLocation source linenumber parseModule str {- Rename and typecheck it -} - let icontext = hsc_IC hsc_env - tc_gblenv <- ioMsgMaybe $ tcRnDeclsi hsc_env icontext decls + tc_gblenv <- ioMsgMaybe $ tcRnDeclsi hsc_env decls {- Grab the new instances -} -- We grab the whole environment because of the overlapping that may have @@ -1441,26 +1432,22 @@ hscDeclsWithLocation hsc_env0 str source linenumber = hsc_env <- getHscEnv liftIO $ linkDecls hsc_env src_span cbc - let tcs = filter (not . isImplicitTyCon) $ (mg_tcs simpl_mg) + let tcs = filterOut isImplicitTyCon (mg_tcs simpl_mg) - ext_vars = filter (isExternalName . idName) $ - bindersOfBinds core_binds + ext_ids = [ id | id <- bindersOfBinds core_binds + , isExternalName (idName id) + , not (isDFunId id) ] + -- We only need to keep around the external bindings + -- (as decided by TidyPgm), since those are the only ones + -- that might be referenced elsewhere. + -- The DFunIds are in 'insts' (see Note [ic_tythings] in HscTypes - (sys_vars, user_vars) = partition is_sys_var ext_vars - is_sys_var id = isDFunId id - || isRecordSelector id - || isJust (isClassOpId_maybe id) - -- we only need to keep around the external bindings - -- (as decided by TidyPgm), since those are the only ones - -- that might be referenced elsewhere. + tythings = map AnId ext_ids ++ map ATyCon tcs - tythings = map AnId user_vars - ++ map ATyCon tcs - - let ictxt1 = extendInteractiveContext icontext tythings - ictxt = ictxt1 { ic_sys_vars = sys_vars ++ ic_sys_vars ictxt1, - ic_instances = (insts, finsts), - ic_default = defaults } + let icontext = hsc_IC hsc_env + ictxt1 = extendInteractiveContext icontext tythings + ictxt = ictxt1 { ic_instances = (insts, finsts) + , ic_default = defaults } return (tythings, ictxt) @@ -1484,7 +1471,7 @@ hscTcExpr hsc_env0 expr = runInteractiveHsc hsc_env0 $ do maybe_stmt <- hscParseStmt expr case maybe_stmt of Just (L _ (BodyStmt expr _ _ _)) -> - ioMsgMaybe $ tcRnExpr hsc_env (hsc_IC hsc_env) expr + ioMsgMaybe $ tcRnExpr hsc_env expr _ -> throwErrors $ unitBag $ mkPlainErrMsg (hsc_dflags hsc_env) noSrcSpan (text "not an expression:" <+> quotes (text expr)) @@ -1499,7 +1486,7 @@ hscKcType hscKcType hsc_env0 normalise str = runInteractiveHsc hsc_env0 $ do hsc_env <- getHscEnv ty <- hscParseType str - ioMsgMaybe $ tcRnType hsc_env (hsc_IC hsc_env) normalise ty + ioMsgMaybe $ tcRnType hsc_env normalise ty hscParseStmt :: String -> Hsc (Maybe (GhciLStmt RdrName)) hscParseStmt = hscParseThing parseStmt diff --git a/compiler/main/HscTypes.lhs b/compiler/main/HscTypes.lhs index 5e634c6dc0..dcb6f1d903 100644 --- a/compiler/main/HscTypes.lhs +++ b/compiler/main/HscTypes.lhs @@ -1104,17 +1104,12 @@ data InteractiveContext -- ^ The cached 'GlobalRdrEnv', built by -- 'InteractiveEval.setContext' and updated regularly -- It contains everything in scope at the command line, - -- including everything in ic_tythings and ic_sys_vars + -- including everything in ic_tythings ic_tythings :: [TyThing], -- ^ TyThings defined by the user, in reverse order of - -- definition. At a breakpoint, this list includes the - -- local variables in scope at that point - - ic_sys_vars :: [Id], - -- ^ Variables defined automatically from - -- ic_ty_things (e.g. record field selectors). - -- See Notes [ic_sys_vars] + -- definition (ie most recent at the front) + -- See Note [ic_tythings] ic_instances :: ([ClsInst], [FamInst]), -- ^ All instances and family instances created during @@ -1144,24 +1139,24 @@ data InteractiveContext } {- -Note [ic_sys_vars] +Note [ic_tythings] ~~~~~~~~~~~~~~~~~~ -This list constains any Ids that arise from TyCons, Classes or -instances defined interactively, but that are *not* given by -'implicitTyThings'. This includes record selectors, default methods, -and dfuns. - -We *could* get rid of this list and generate these Ids from -ic_tythings: - - - dfuns come from Instances - - record selectors from TyCons - - default methods from Classes - -For record selectors the TyCon gives the Name, but in order to make an -Id we would have to construct the type ourselves. Similarly for -default methods. So for now we collect the Ids after tidying (see -hscDeclsWithLocation) and save them in ic_sys_vars. +The ic_tythings field contains + * The TyThings declared by the user at the command prompt + (eg Ids, TyCons, Classes) + + * The user-visible Ids that arise from such things, which + *don't* come from 'implicitTyThings', notably: + - record selectors + - class ops + The implicitTyThings are readily obtained from the TyThings + but record selectors etc are not + +It does *not* contain + * DFunIds (they can be gotten from ic_instances) + * CoAxioms (ditto) + +See also Note [Interactively-bound Ids in GHCi] in TcRnDriver -} -- | Constructs an empty InteractiveContext. @@ -1173,7 +1168,6 @@ emptyInteractiveContext dflags ic_imports = [], ic_rn_gbl_env = emptyGlobalRdrEnv, ic_tythings = [], - ic_sys_vars = [], ic_instances = ([],[]), ic_fix_env = emptyNameEnv, -- System.IO.print by default diff --git a/compiler/simplCore/CoreMonad.lhs b/compiler/simplCore/CoreMonad.lhs index 6bcdbb09a2..62e45e02ec 100644 --- a/compiler/simplCore/CoreMonad.lhs +++ b/compiler/simplCore/CoreMonad.lhs @@ -84,6 +84,9 @@ import IOEnv hiding ( liftIO, failM, failWithM ) import qualified IOEnv ( liftIO ) import TcEnv ( tcLookupGlobal ) import TcRnMonad ( initTcForLookup ) +import InstEnv ( instanceDFunId ) +import Type ( tyVarsOfType ) +import Id ( idType ) import Var import VarSet @@ -264,12 +267,15 @@ interactiveInScope :: HscEnv -> [Var] -- -- See Trac #8215 for an example interactiveInScope hsc_env - = tyvars ++ vars + = varSetElems tyvars ++ ids where - ictxt = hsc_IC hsc_env - te = mkTypeEnvWithImplicits (ic_tythings ictxt ++ map AnId (ic_sys_vars ictxt)) - vars = typeEnvIds te - tyvars = varSetElems $ tyThingsTyVars $ typeEnvElts $ te + -- C.f. TcRnDriver.setInteractiveContext, Desugar.deSugarExpr + ictxt = hsc_IC hsc_env + (cls_insts, _fam_insts) = ic_instances ictxt + te1 = mkTypeEnvWithImplicits (ic_tythings ictxt) + te = extendTypeEnvWithIds te1 (map instanceDFunId cls_insts) + ids = typeEnvIds te + tyvars = foldr (unionVarSet . tyVarsOfType . idType) emptyVarSet ids -- Why the type variables? How can the top level envt have free tyvars? -- I think it's because of the GHCi debugger, which can bind variables -- f :: [t] -> [t] diff --git a/compiler/typecheck/TcEnv.lhs b/compiler/typecheck/TcEnv.lhs index 0b5e452718..6be4772ab8 100644 --- a/compiler/typecheck/TcEnv.lhs +++ b/compiler/typecheck/TcEnv.lhs @@ -25,7 +25,7 @@ module TcEnv( tcExtendTyVarEnv, tcExtendTyVarEnv2, tcExtendLetEnv, tcExtendIdEnv, tcExtendIdEnv1, tcExtendIdEnv2, - tcExtendIdBndrs, tcExtendGlobalTyVars, + tcExtendIdBndrs, tcExtendGhciIdEnv, tcLookup, tcLookupLocated, tcLookupLocalIds, tcLookupId, tcLookupTyVar, @@ -380,13 +380,66 @@ getScopedTyVarBinds ; return [(name, tv) | ATyVar name tv <- nameEnvElts (tcl_env lcl_env)] } \end{code} +Note [Initialising the type environment for GHCi] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +tcExtendGhciIdEnv extends the local type environemnt with GHCi +identifiers (from ic_tythings), bound earlier in the interaction. +They may have free type variables (RuntimeUnk things), and if we don't +register these free TyVars as global TyVars then the typechecker will +try to quantify over them and fall over in zonkQuantifiedTyVar. +So we must add any free TyVars to the typechecker's global +TyVar set. That is most conveniently done here, using the local function +tcExtendLocalTypeEnv. + +Note especially that + + * tcExtendGhciIdEnv extends the local type env, tcl_env + That's important because some are not closed (ie have free tyvars) + and the compiler assumes that the global type env (tcg_type_env) has + no free tyvars. Actually, only ones with Internal names can be non-closed + so we jsut add those + + * The tct_closed flag depends on whether the thing has free (RuntimeUnk) + type variables + + * It will also does tcExtendGlobalTyVars; this is important + because of those RuntimeUnk variables + + * It does not extend the local RdrEnv (tcl_rdr), because the things are + already in the GlobalRdrEnv. Extending the local RdrEnv isn't terrible, + but it means there is an entry for the same Name in both global and local + RdrEnvs, and that lead to duplicate "perhpas you meant..." suggestions + (e.g. T5564). + + We don't bother with the tcl_th_bndrs environment either. + + * NB: all these TcTyThings will be in the global type envt (tcg_type_env) as + well. We are just shadowing them here to deal with the global tyvar + stuff. That's why we can simply drop the External-Name ones; they + will be found in the global envt \begin{code} +tcExtendGhciIdEnv :: [TyThing] -> TcM a -> TcM a +-- Used to bind Ids for GHCi identifiers bound earlier in the user interaction +-- See Note [Initialising the type environment for GHCi] +tcExtendGhciIdEnv ids thing_inside + = do { lcl_env <- tcExtendLocalTypeEnv tc_ty_things + ; setLclEnv lcl_env thing_inside } + where + tc_ty_things = [ (name, ATcId { tct_id = id + , tct_closed = is_top id }) + | AnId id <- ids + , let name = idName id + , isInternalName name ] + is_top id | isEmptyVarSet (tyVarsOfType (idType id)) = TopLevel + | otherwise = NotTopLevel + tcExtendLetEnv :: TopLevelFlag -> TopLevelFlag -> [TcId] -> TcM a -> TcM a -tcExtendLetEnv top_lvl closed ids thing_inside +-- Used for both top-level value bindings and and nested let/where-bindings +tcExtendLetEnv top_lvl closed ids thing_inside = do { stage <- getStage ; tc_extend_local_env (top_lvl, thLevel stage) - [ (idName id, ATcId { tct_id = id + [ (idName id, ATcId { tct_id = id , tct_closed = closed }) | id <- ids] $ tcExtendIdBndrs [TcIdBndr id top_lvl | id <- ids] thing_inside } @@ -431,50 +484,50 @@ tc_extend_local_env :: (TopLevelFlag, ThLevel) -> [(Name, TcTyThing)] -> TcM a - tc_extend_local_env thlvl extra_env thing_inside = do { traceTc "env2" (ppr extra_env) - ; env1 <- tcExtendGlobalTyVars (map snd extra_env) + ; env1 <- tcExtendLocalTypeEnv extra_env ; let env2 = extend_local_env thlvl extra_env env1 ; setLclEnv env2 thing_inside } - -extend_local_env :: (TopLevelFlag, ThLevel) -> [(Name, TcTyThing)] -> TcLclEnv -> TcLclEnv --- Extend the local TcTypeEnv *and* the local LocalRdrEnv simultaneously --- Reason for extending LocalRdrEnv: after running a TH splice we need --- to do renaming. -extend_local_env thlvl pairs env@(TcLclEnv { tcl_rdr = rdr_env - , tcl_env = type_env - , tcl_th_bndrs = th_bndrs }) - = env { tcl_rdr = extendLocalRdrEnvList rdr_env - [ n | (n, _) <- pairs, isInternalName n ] - -- The LocalRdrEnv contains only non-top-level names - -- (GlobalRdrEnv handles the top level) - , tcl_th_bndrs = extendNameEnvList th_bndrs -- We only track Ids in tcl_th_bndrs - [(n, thlvl) | (n, ATcId {}) <- pairs] - , tcl_env = extendNameEnvList type_env pairs } - -tcExtendGlobalTyVars :: [TcTyThing] -> TcM TcLclEnv -tcExtendGlobalTyVars tc_ty_things + where + extend_local_env :: (TopLevelFlag, ThLevel) -> [(Name, TcTyThing)] -> TcLclEnv -> TcLclEnv + -- Extend the local LocalRdrEnv and Template Haskell staging env simultaneously + -- Reason for extending LocalRdrEnv: after running a TH splice we need + -- to do renaming. + extend_local_env thlvl pairs env@(TcLclEnv { tcl_rdr = rdr_env + , tcl_th_bndrs = th_bndrs }) + = env { tcl_rdr = extendLocalRdrEnvList rdr_env + [ n | (n, _) <- pairs, isInternalName n ] + -- The LocalRdrEnv contains only non-top-level names + -- (GlobalRdrEnv handles the top level) + , tcl_th_bndrs = extendNameEnvList th_bndrs -- We only track Ids in tcl_th_bndrs + [(n, thlvl) | (n, ATcId {}) <- pairs] } + +tcExtendLocalTypeEnv :: [(Name, TcTyThing)] -> TcM TcLclEnv +tcExtendLocalTypeEnv tc_ty_things | isEmptyVarSet extra_tvs - = getLclEnv + = do { lcl_env@(TcLclEnv { tcl_env = lcl_type_env }) <- getLclEnv + ; return (lcl_env { tcl_env = extendNameEnvList lcl_type_env tc_ty_things } ) } | otherwise - = do { lcl_env <- getLclEnv + = do { lcl_env@(TcLclEnv { tcl_env = lcl_type_env }) <- getLclEnv ; global_tvs <- readMutVar (tcl_tyvars lcl_env) ; new_g_var <- newMutVar (global_tvs `unionVarSet` extra_tvs) - ; return (lcl_env { tcl_tyvars = new_g_var }) } + ; return (lcl_env { tcl_tyvars = new_g_var + , tcl_env = extendNameEnvList lcl_type_env tc_ty_things } ) } where extra_tvs = foldr get_tvs emptyVarSet tc_ty_things - get_tvs (ATcId { tct_id = id, tct_closed = closed }) tvs + get_tvs (_, ATcId { tct_id = id, tct_closed = closed }) tvs = case closed of TopLevel -> ASSERT2( isEmptyVarSet (tyVarsOfType (idType id)), ppr id $$ ppr (idType id) ) tvs NotTopLevel -> tvs `unionVarSet` tyVarsOfType (idType id) - get_tvs (ATyVar _ tv) tvs -- See Note [Global TyVars] + get_tvs (_, ATyVar _ tv) tvs -- See Note [Global TyVars] = tvs `unionVarSet` tyVarsOfType (tyVarKind tv) `extendVarSet` tv - get_tvs (AThing k) tvs = tvs `unionVarSet` tyVarsOfType k + get_tvs (_, AThing k) tvs = tvs `unionVarSet` tyVarsOfType k - get_tvs (AGlobal {}) tvs = tvs - get_tvs (APromotionErr {}) tvs = tvs + get_tvs (_, AGlobal {}) tvs = tvs + get_tvs (_, APromotionErr {}) tvs = tvs -- Note [Global TyVars] -- It's important to add the in-scope tyvars to the global tyvar set diff --git a/compiler/typecheck/TcRnDriver.lhs b/compiler/typecheck/TcRnDriver.lhs index 0926b49259..e1ea4d32c5 100644 --- a/compiler/typecheck/TcRnDriver.lhs +++ b/compiler/typecheck/TcRnDriver.lhs @@ -62,7 +62,6 @@ import CoreSyn import ErrUtils import Id import VarEnv -import VarSet import Module import UniqFM import Name @@ -83,9 +82,9 @@ import Annotations import Data.List ( sortBy ) import Data.IORef ( readIORef ) import Data.Ord -import BasicTypes hiding( SuccessFlag(..) ) #ifdef GHCI +import BasicTypes hiding( SuccessFlag(..) ) import TcType ( isUnitTy, isTauTy ) import TcHsType import TcMatches @@ -1440,22 +1439,32 @@ get two defns for 'main' in the interface file! %********************************************************* \begin{code} -setInteractiveContext :: HscEnv -> InteractiveContext -> TcRn a -> TcRn a -setInteractiveContext hsc_env icxt thing_inside +setInteractiveContext :: HscEnv -> TcRn a -> TcRn a +setInteractiveContext hsc_env thing_inside = let -- Initialise the tcg_inst_env with instances from all home modules. -- This mimics the more selective call to hptInstances in tcRnImports + icxt = hsc_IC hsc_env (home_insts, home_fam_insts) = hptInstances hsc_env (\_ -> True) (ic_insts, ic_finsts) = ic_instances icxt ty_things = ic_tythings icxt - type_env = mkTypeEnvWithImplicits - (map AnId (ic_sys_vars icxt) ++ ty_things) + type_env1 = mkTypeEnvWithImplicits ty_things + type_env = extendTypeEnvWithIds type_env1 (map instanceDFunId ic_insts) + -- Putting the dfuns in the type_env is just + -- to keep Core Lint happy con_fields = [ (dataConName c, dataConFieldLabels c) - | ATyCon t <- ty_things + | ATyCon t <- ic_tythings icxt , c <- tyConDataCons t ] in - do { gbl_env <- getGblEnv + do { traceTc "setInteractiveContext" $ + vcat [ text "ic_tythings:" <+> vcat (map ppr (ic_tythings icxt)) + , text "ic_insts:" <+> vcat (map (pprBndr LetBind . instanceDFunId) ic_insts) + , text "ic_rn_gbl_env (LocalDef)" <+> + vcat (map ppr [ local_gres | gres <- occEnvElts (ic_rn_gbl_env icxt) + , let local_gres = filter isLocalGRE gres + , not (null local_gres) ]) ] + ; gbl_env <- getGblEnv ; let gbl_env' = gbl_env { tcg_rdr_env = ic_rn_gbl_env icxt , tcg_type_env = type_env @@ -1470,17 +1479,14 @@ setInteractiveContext hsc_env icxt thing_inside home_fam_insts , tcg_field_env = RecFields (mkNameEnv con_fields) (mkNameSet (concatMap snd con_fields)) - -- setting tcg_field_env is necessary + -- setting tcg_field_env is necessary -- to make RecordWildCards work (test: ghci049) , tcg_fix_env = ic_fix_env icxt , tcg_default = ic_default icxt } - ; lcl_env' <- tcExtendGlobalTyVars [ ATcId { tct_id = id, tct_closed = NotTopLevel } - | AnId id <- ty_things - , not (isEmptyVarSet (tyVarsOfType (idType id))) ] - -- See Note [Global tyvars] - - ; setEnvs (gbl_env', lcl_env') thing_inside } + ; setGblEnv gbl_env' $ + tcExtendGhciIdEnv ty_things $ -- See Note [Initialising the type environment for GHCi] + thing_inside } -- in TcEnv #ifdef GHCI -- | The returned [Id] is the list of new Ids bound by this statement. It can @@ -1488,11 +1494,11 @@ setInteractiveContext hsc_env icxt thing_inside -- -- The returned TypecheckedHsExpr is of type IO [ () ], a list of the bound -- values, coerced to (). -tcRnStmt :: HscEnv -> InteractiveContext -> GhciLStmt RdrName +tcRnStmt :: HscEnv -> GhciLStmt RdrName -> IO (Messages, Maybe ([Id], LHsExpr Id, FixityEnv)) -tcRnStmt hsc_env ictxt rdr_stmt +tcRnStmt hsc_env rdr_stmt = initTcPrintErrors hsc_env iNTERACTIVE $ - setInteractiveContext hsc_env ictxt $ do { + setInteractiveContext hsc_env $ do { -- The real work is done here ((bound_ids, tc_expr), fix_env) <- tcUserStmt rdr_stmt ; @@ -1537,16 +1543,6 @@ tcRnStmt hsc_env ictxt rdr_stmt nest 2 (ppr id <+> dcolon <+> ppr (idType id))]) \end{code} -Note [Global tyvars] -~~~~~~~~~~~~~~~~~~~~ -Ids bound interactively (in ic_tythings) might have some free type -variables (RuntimeUnk things), and if we don't register these free -TyVars as global TyVars then the typechecker will try to quantify over -them and fall over in zonkQuantifiedTyVar. - -So we must add any free TyVars to the typechecker's global -TyVar set. - Note [Interactively-bound Ids in GHCi] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The Ids bound by previous Stmts in GHCi are currently @@ -1778,10 +1774,10 @@ getGhciStepIO = do step = noLoc $ ExprWithTySig (nlHsVar ghciStepIoMName) stepTy return step -isGHCiMonad :: HscEnv -> InteractiveContext -> String -> IO (Messages, Maybe Name) -isGHCiMonad hsc_env ictxt ty +isGHCiMonad :: HscEnv -> String -> IO (Messages, Maybe Name) +isGHCiMonad hsc_env ty = initTcPrintErrors hsc_env iNTERACTIVE $ - setInteractiveContext hsc_env ictxt $ do + setInteractiveContext hsc_env $ do rdrEnv <- getGlobalRdrEnv let occIO = lookupOccEnv rdrEnv (mkOccName tcName ty) case occIO of @@ -1802,13 +1798,12 @@ tcRnExpr just finds the type of an expression \begin{code} tcRnExpr :: HscEnv - -> InteractiveContext -> LHsExpr RdrName -> IO (Messages, Maybe Type) -- Type checks the expression and returns its most general type -tcRnExpr hsc_env ictxt rdr_expr +tcRnExpr hsc_env rdr_expr = initTcPrintErrors hsc_env iNTERACTIVE $ - setInteractiveContext hsc_env ictxt $ do { + setInteractiveContext hsc_env $ do { (rn_expr, _fvs) <- rnLExpr rdr_expr ; failIfErrsM ; @@ -1845,13 +1840,12 @@ tcRnType just finds the kind of a type \begin{code} tcRnType :: HscEnv - -> InteractiveContext -> Bool -- Normalise the returned type -> LHsType RdrName -> IO (Messages, Maybe (Type, Kind)) -tcRnType hsc_env ictxt normalise rdr_type +tcRnType hsc_env normalise rdr_type = initTcPrintErrors hsc_env iNTERACTIVE $ - setInteractiveContext hsc_env ictxt $ + setInteractiveContext hsc_env $ setXOptM Opt_PolyKinds $ -- See Note [Kind-generalise in tcRnType] do { (rn_type, _fvs) <- rnLHsType GHCiCtx rdr_type ; failIfErrsM @@ -1891,13 +1885,12 @@ tcRnDeclsi exists to allow class, data, and other declarations in GHCi. \begin{code} tcRnDeclsi :: HscEnv - -> InteractiveContext -> [LHsDecl RdrName] -> IO (Messages, Maybe TcGblEnv) -tcRnDeclsi hsc_env ictxt local_decls = +tcRnDeclsi hsc_env local_decls = initTcPrintErrors hsc_env iNTERACTIVE $ - setInteractiveContext hsc_env ictxt $ do + setInteractiveContext hsc_env $ do ((tcg_env, tclcl_env), lie) <- captureConstraints $ tc_rn_src_decls emptyModDetails local_decls @@ -1953,7 +1946,7 @@ getModuleInterface hsc_env mod tcRnLookupRdrName :: HscEnv -> RdrName -> IO (Messages, Maybe [Name]) tcRnLookupRdrName hsc_env rdr_name = initTcPrintErrors hsc_env iNTERACTIVE $ - setInteractiveContext hsc_env (hsc_IC hsc_env) $ + setInteractiveContext hsc_env $ lookup_rdr_name rdr_name lookup_rdr_name :: RdrName -> TcM [Name] @@ -1989,7 +1982,7 @@ lookup_rdr_name rdr_name = do tcRnLookupName :: HscEnv -> Name -> IO (Messages, Maybe TyThing) tcRnLookupName hsc_env name = initTcPrintErrors hsc_env iNTERACTIVE $ - setInteractiveContext hsc_env (hsc_IC hsc_env) $ + setInteractiveContext hsc_env $ tcRnLookupName' name -- To look up a name we have to look in the local environment (tcl_lcl) @@ -2016,15 +2009,14 @@ tcRnGetInfo :: HscEnv -- *and* as a type or class constructor; -- hence the call to dataTcOccs, and we return up to two results tcRnGetInfo hsc_env name - = let ictxt = hsc_IC hsc_env in - initTcPrintErrors hsc_env iNTERACTIVE $ - setInteractiveContext hsc_env ictxt $ do + = initTcPrintErrors hsc_env iNTERACTIVE $ + setInteractiveContext hsc_env $ do -- Load the interface for all unqualified types and classes -- That way we will find all the instance declarations -- (Packages have not orphan modules, and we assume that -- in the home package all relevant modules are loaded.) - loadUnqualIfaces hsc_env ictxt + loadUnqualIfaces hsc_env (hsc_IC hsc_env) thing <- tcRnLookupName' name fixity <- lookupFixityRn name |