summaryrefslogtreecommitdiff
path: root/compiler/rename
diff options
context:
space:
mode:
authorSimon Peyton Jones <simonpj@microsoft.com>2014-01-03 15:39:35 +0000
committerSimon Peyton Jones <simonpj@microsoft.com>2014-01-03 15:39:35 +0000
commite60a841b07d000703e7aceb0faec908ce8f3257f (patch)
tree51e73782ad431069cedf87561d68577f48a95c9c /compiler/rename
parent5c4265f25f04f191fafc4a7f6bfde088385da53e (diff)
downloadhaskell-e60a841b07d000703e7aceb0faec908ce8f3257f.tar.gz
Improve error message when using qualified names in GHCi
When you say ghci> :i Foo.x GHCi tries to find module Foo and get 'x' from it. But if Foo doesn't exist we don't want to say: Attempting to use module ‛Foo’ which is not loaded This is a bit confusing. Rather we just want to say that Foo.x is not in scope.
Diffstat (limited to 'compiler/rename')
-rw-r--r--compiler/rename/RnEnv.lhs75
1 files changed, 41 insertions, 34 deletions
diff --git a/compiler/rename/RnEnv.lhs b/compiler/rename/RnEnv.lhs
index c49652b442..c11cca0f1e 100644
--- a/compiler/rename/RnEnv.lhs
+++ b/compiler/rename/RnEnv.lhs
@@ -46,7 +46,7 @@ module RnEnv (
#include "HsVersions.h"
-import LoadIface ( loadInterfaceForName, loadSrcInterface )
+import LoadIface ( loadInterfaceForName, loadSrcInterface_maybe )
import IfaceEnv
import HsSyn
import RdrName
@@ -645,23 +645,10 @@ lookupOccRn_maybe rdr_name
; case mb_name of {
Just name -> return (Just name) ;
Nothing -> do
- { -- We allow qualified names on the command line to refer to
- -- *any* name exported by any module in scope, just as if there
- -- was an "import qualified M" declaration for every module.
- -- But we DONT allow it under Safe Haskell as we need to check
- -- imports. We can and should instead check the qualified import
- -- but at the moment this requires some refactoring so leave as a TODO
- ; dflags <- getDynFlags
- ; let allow_qual = gopt Opt_ImplicitImportQualified dflags &&
- not (safeDirectImpsReq dflags)
- ; is_ghci <- getIsGHCi
- -- This test is not expensive,
- -- and only happens for failed lookups
- ; if isQual rdr_name && allow_qual && is_ghci
- then lookupQualifiedName rdr_name
- else do { traceRn (text "lookupOccRn failed" <+> ppr rdr_name)
- ; return Nothing } } } } } }
-
+ { dflags <- getDynFlags
+ ; is_ghci <- getIsGHCi -- This test is not expensive,
+ -- and only happens for failed lookups
+ ; lookupQualifiedNameGHCi dflags is_ghci rdr_name } } } } }
lookupGlobalOccRn :: RdrName -> RnM Name
-- lookupGlobalOccRn is like lookupOccRn, except that it looks in the global
@@ -836,26 +823,46 @@ this is, after all, wired-in stuff.
%* *
%*********************************************************
+A qualified name on the command line can refer to any module at
+all: we try to load the interface if we don't already have it, just
+as if there was an "import qualified M" declaration for every
+module.
+
+If we fail we just return Nothing, rather than bleating
+about "attempting to use module ‛D’ (./D.hs) which is not loaded"
+which is what loadSrcInterface does.
+
+Note [Safe Haskell and GHCi]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+We DONT do this Safe Haskell as we need to check imports. We can
+and should instead check the qualified import but at the moment
+this requires some refactoring so leave as a TODO
+
\begin{code}
--- A qualified name on the command line can refer to any module at all: we
--- try to load the interface if we don't already have it.
-lookupQualifiedName :: RdrName -> RnM (Maybe Name)
-lookupQualifiedName rdr_name
+lookupQualifiedNameGHCi :: DynFlags -> Bool -> RdrName -> RnM (Maybe Name)
+lookupQualifiedNameGHCi dflags is_ghci rdr_name
| Just (mod,occ) <- isQual_maybe rdr_name
- -- Note: we want to behave as we would for a source file import here,
- -- and respect hiddenness of modules/packages, hence loadSrcInterface.
- = do iface <- loadSrcInterface doc mod False Nothing
-
- case [ name
- | avail <- mi_exports iface,
- name <- availNames avail,
- nameOccName name == occ ] of
- (n:ns) -> ASSERT(null ns) return (Just n)
- _ -> do { traceRn (text "lookupQualified" <+> ppr rdr_name)
- ; return Nothing }
+ , is_ghci
+ , gopt Opt_ImplicitImportQualified dflags -- Enables this GHCi behaviour
+ , not (safeDirectImpsReq dflags) -- See Note [Safe Haskell and GHCi]
+ = -- We want to behave as we would for a source file import here,
+ -- and respect hiddenness of modules/packages, hence loadSrcInterface.
+ do { res <- loadSrcInterface_maybe doc mod False Nothing
+ ; case res of
+ Succeeded iface
+ | (n:ns) <- [ name
+ | avail <- mi_exports iface
+ , name <- availNames avail
+ , nameOccName name == occ ]
+ -> ASSERT(null ns) return (Just n)
+
+ _ -> -- Either we couldn't load the interface, or
+ -- we could but we didn't find the name in it
+ do { traceRn (text "lookupQualifiedNameGHCi" <+> ppr rdr_name)
+ ; return Nothing } }
| otherwise
- = pprPanic "RnEnv.lookupQualifiedName" (ppr rdr_name)
+ = return Nothing
where
doc = ptext (sLit "Need to find") <+> ppr rdr_name
\end{code}