From 4917397e279b0aa755eb09e1ca62913237132895 Mon Sep 17 00:00:00 2001 From: Simon Marlow Date: Wed, 3 Mar 2010 11:22:42 +0000 Subject: [PATCH] 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. --- compiler/main/DriverMkDepend.hs | 4 ---- compiler/main/HeaderInfo.hs | 47 ++++++++++++++++++++++++++++++++++----- compiler/rename/RnNames.lhs | 33 ++------------------------- 3 files changed, 43 insertions(+), 41 deletions(-) diff --git a/compiler/main/DriverMkDepend.hs b/compiler/main/DriverMkDepend.hs index 656ced2..48617ec 100644 --- a/compiler/main/DriverMkDepend.hs +++ b/compiler/main/DriverMkDepend.hs @@ -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 } diff --git a/compiler/main/HeaderInfo.hs b/compiler/main/HeaderInfo.hs index 597253e..e16c2ce 100644 --- a/compiler/main/HeaderInfo.hs +++ b/compiler/main/HeaderInfo.hs @@ -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 diff --git a/compiler/rename/RnNames.lhs b/compiler/rename/RnNames.lhs index 7d367c7..d82bea9 100644 --- a/compiler/rename/RnNames.lhs +++ b/compiler/rename/RnNames.lhs @@ -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) -- 1.7.10.4