Commit 4917397e authored by Simon Marlow's avatar Simon Marlow
Browse files

Add the implicit 'import Prelude' in getImports

This makes things more consistent, and avoids a strange "Prelude is
not loaded" error when there is a Prelude.hs on the search path.
parent 8693219d
......@@ -18,7 +18,6 @@ module DriverMkDepend (
import qualified GHC
-- import GHC ( ModSummary(..), GhcMonad )
import HsSyn ( ImportDecl(..) )
import PrelNames
import DynFlags
import Util
import HscTypes
......@@ -218,9 +217,6 @@ processDeps dflags hsc_env excl_mods root hdl (AcyclicSCC node)
; do_imps True (ms_srcimps node)
; do_imps False (ms_imps node)
; when (dopt Opt_ImplicitPrelude (ms_hspp_opts node)) $
do_imp noSrcSpan False Nothing pRELUDE_NAME
}
......
......@@ -9,6 +9,7 @@
-----------------------------------------------------------------------------
module HeaderInfo ( getImports
, mkPrelImports -- used by the renamer too
, getOptionsFromFile, getOptions
, optionsErrorMsgs,
checkProcessArgsResult ) where
......@@ -20,9 +21,9 @@ import HscTypes
import Parser ( parseHeader )
import Lexer
import FastString
import HsSyn ( ImportDecl(..), HsModule(..) )
import Module ( ModuleName, moduleName )
import PrelNames ( gHC_PRIM, mAIN_NAME )
import HsSyn
import Module
import PrelNames
import StringBuffer
import SrcLoc
import DynFlags
......@@ -73,12 +74,46 @@ getImports dflags buf filename source_filename = do
main_loc = mkSrcLoc (mkFastString source_filename) 1 1
mod = mb_mod `orElse` L (srcLocSpan main_loc) mAIN_NAME
(src_idecls, ord_idecls) = partition (ideclSource.unLoc) imps
-- GHC.Prim doesn't exist physically, so don't go looking for it.
ordinary_imps = filter ((/= moduleName gHC_PRIM) . unLoc . ideclName . unLoc)
ord_idecls
-- GHC.Prim doesn't exist physically, so don't go looking for it.
implicit_prelude = dopt Opt_ImplicitPrelude dflags
implicit_imports = mkPrelImports (unLoc mod) implicit_prelude imps
in
return (src_idecls, ordinary_imps, mod)
return (src_idecls, implicit_imports ++ ordinary_imps, mod)
mkPrelImports :: ModuleName -> 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_NAME
|| explicit_prelude_import
|| not implicit_prelude
= []
| otherwise = [preludeImportDecl]
where
explicit_prelude_import
= notNull [ () | L _ (ImportDecl mod Nothing _ _ _ _) <- import_decls,
unLoc mod == pRELUDE_NAME ]
preludeImportDecl :: LImportDecl RdrName
preludeImportDecl
= L loc $
ImportDecl (L loc pRELUDE_NAME)
Nothing {- no specific package -}
False {- Not a boot interface -}
False {- Not qualified -}
Nothing {- No "as" -}
Nothing {- No import list -}
loc = mkGeneralSrcSpan (fsLit "Implicit import declaration")
parseError :: GhcMonad m => SrcSpan -> Message -> m a
parseError span err = throwOneError $ mkPlainErrMsg span err
......
......@@ -21,6 +21,7 @@ import IfaceEnv ( ifaceExportNames )
import LoadIface ( loadSrcInterface, loadSysInterface )
import TcRnMonad hiding (LIE)
import HeaderInfo ( mkPrelImports )
import PrelNames
import Module
import Name
......@@ -60,7 +61,7 @@ rnImports imports
-- warning for {- SOURCE -} ones that are unnecessary
= do this_mod <- getModule
implicit_prelude <- doptM Opt_ImplicitPrelude
let prel_imports = mkPrelImports this_mod implicit_prelude imports
let prel_imports = mkPrelImports (moduleName this_mod) implicit_prelude imports
(source, ordinary) = partition is_source_import imports
is_source_import (L _ (ImportDecl _ _ is_boot _ _ _)) = is_boot
......@@ -84,36 +85,6 @@ rnImports imports
imp_avails1 `plusImportAvails` imp_avails2,
hpc_usage1 || hpc_usage2)
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 Nothing _ _ _ _) <- import_decls,
unLoc mod == pRELUDE_NAME ]
preludeImportDecl :: LImportDecl RdrName
preludeImportDecl
= L loc $
ImportDecl (L loc pRELUDE_NAME)
Nothing {- no specific package -}
False {- Not a boot interface -}
False {- Not qualified -}
Nothing {- No "as" -}
Nothing {- No import list -}
loc = mkGeneralSrcSpan (fsLit "Implicit import declaration")
rnImportDecl :: Module
-> LImportDecl RdrName
-> RnM (LImportDecl Name, GlobalRdrEnv, ImportAvails,AnyHpcUsage)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment