Fix handling of non-in-scope exports (fixes test mod7)
[ghc-hetmet.git] / compiler / rename / RnNames.lhs
index d61133b..3671676 100644 (file)
@@ -30,28 +30,12 @@ import Module
 import Name
 import NameEnv
 import NameSet
-import OccName         ( srcDataName, pprNonVarNameSpace,
-                         occNameSpace,
-                         OccEnv, mkOccEnv, mkOccEnv_C, lookupOccEnv,
-                         emptyOccEnv, extendOccEnv )
-import HscTypes                ( GenAvailInfo(..), AvailInfo, availNames, availName,
-                         HomePackageTable, PackageIfaceTable, 
-                         mkPrintUnqualified, availsToNameSet,
-                         Deprecs(..), ModIface(..), Dependencies(..), 
-                         lookupIfaceByModule, ExternalPackageState(..)
-                       )
-import RdrName         ( RdrName, rdrNameOcc, setRdrNameSpace, Parent(..),
-                         GlobalRdrEnv, mkGlobalRdrEnv, GlobalRdrElt(..), 
-                         emptyGlobalRdrEnv, plusGlobalRdrEnv, globalRdrEnvElts,
-                         extendGlobalRdrEnv, lookupGlobalRdrEnv,
-                         lookupGRE_RdrName, lookupGRE_Name, 
-                         Provenance(..), ImportSpec(..), ImpDeclSpec(..), ImpItemSpec(..), 
-                         importSpecLoc, importSpecModule, isLocalGRE, pprNameProvenance,
-                         unQualSpecOK, qualSpecOK )
+import OccName
+import HscTypes
+import RdrName
 import Outputable
 import Maybes
-import SrcLoc          ( Located(..), mkGeneralSrcSpan, getLoc,
-                         unLoc, noLoc, srcLocSpan, SrcSpan )
+import SrcLoc
 import FiniteMap
 import ErrUtils
 import BasicTypes      ( DeprecTxt )
@@ -81,29 +65,16 @@ rnImports imports
          -- warning for {- SOURCE -} ones that are unnecessary
     = do this_mod <- getModule
          implicit_prelude <- doptM Opt_ImplicitPrelude
-         let all_imports              = mk_prel_imports this_mod implicit_prelude ++ imports
-             (source, ordinary) = partition is_source_import all_imports
+         let prel_imports      = mkPrelImports this_mod implicit_prelude imports
+             (source, ordinary) = partition is_source_import imports
              is_source_import (L _ (ImportDecl _ is_boot _ _ _)) = is_boot
 
-         stuff1 <- mapM (rnImportDecl this_mod) ordinary
+         stuff1 <- mapM (rnImportDecl this_mod) (prel_imports ++ ordinary)
          stuff2 <- mapM (rnImportDecl this_mod) source
          let (decls, rdr_env, imp_avails) = combine (stuff1 ++ stuff2)
          return (decls, rdr_env, imp_avails) 
 
     where
--- NB: opt_NoImplicitPrelude is slightly different to import Prelude ();
--- because the former doesn't even look at Prelude.hi for instance 
--- declarations, whereas the latter does.
-   mk_prel_imports this_mod implicit_prelude
-       |  this_mod == pRELUDE
-          || explicit_prelude_import
-          || not implicit_prelude
-           = []
-       | otherwise = [preludeImportDecl]
-   explicit_prelude_import
-       = notNull [ () | L _ (ImportDecl mod _ _ _ _) <- imports, 
-                  unLoc mod == pRELUDE_NAME ]
-
    combine :: [(LImportDecl Name,  GlobalRdrEnv, ImportAvails)]
            -> ([LImportDecl Name], GlobalRdrEnv, ImportAvails)
    combine = foldr plus ([], emptyGlobalRdrEnv, emptyImportAvails)
@@ -113,18 +84,34 @@ rnImports imports
                    gbl_env1 `plusGlobalRdrEnv` gbl_env2,
                    imp_avails1 `plusImportAvails` imp_avails2)
 
-preludeImportDecl :: LImportDecl RdrName
-preludeImportDecl
-  = L loc $
-       ImportDecl (L loc pRELUDE_NAME)
+mkPrelImports :: Module -> Bool -> [LImportDecl RdrName] -> [LImportDecl RdrName]
+-- Consruct the implicit declaration "import Prelude" (or not)
+--
+-- NB: opt_NoImplicitPrelude is slightly different to import Prelude ();
+-- because the former doesn't even look at Prelude.hi for instance 
+-- declarations, whereas the latter does.
+mkPrelImports this_mod implicit_prelude import_decls
+  | this_mod == pRELUDE
+   || explicit_prelude_import
+   || not implicit_prelude
+  = []
+  | otherwise = [preludeImportDecl]
+  where
+      explicit_prelude_import
+       = notNull [ () | L _ (ImportDecl mod _ _ _ _) <- import_decls, 
+                  unLoc mod == pRELUDE_NAME ]
+
+      preludeImportDecl :: LImportDecl RdrName
+      preludeImportDecl
+        = L loc $
+         ImportDecl (L loc pRELUDE_NAME)
               False {- Not a boot interface -}
               False    {- Not qualified -}
               Nothing  {- No "as" -}
               Nothing  {- No import list -}
-  where
-    loc = mkGeneralSrcSpan FSLIT("Implicit import declaration")         
 
-       
+      loc = mkGeneralSrcSpan FSLIT("Implicit import declaration")         
+
 
 rnImportDecl  :: Module
              -> LImportDecl RdrName
@@ -812,14 +799,13 @@ exports_from_avail (Just rdr_items) rdr_env imports this_mod
              return (IEVar (gre_name gre), greAvail gre)
 
     lookup_ie (IEThingAbs rdr) 
-        = do name <- lookupGlobalOccRn rdr
-            case lookupGRE_RdrName rdr rdr_env of
-              []    -> panic "RnNames.lookup_ie"
-              elt:_ -> case gre_par elt of
-                         NoParent   -> return (IEThingAbs name, 
-                                               AvailTC name [name])
-                         ParentIs p -> return (IEThingAbs name, 
-                                               AvailTC p [name])
+        = do gre <- lookupGreRn rdr
+            let name = gre_name gre
+            case gre_par gre of
+               NoParent   -> return (IEThingAbs name, 
+                                     AvailTC name [name])
+               ParentIs p -> return (IEThingAbs name, 
+                                     AvailTC p [name])
 
     lookup_ie ie@(IEThingAll rdr) 
         = do name <- lookupGlobalOccRn rdr