diff options
author | Thomas Miedema <thomasmiedema@gmail.com> | 2015-02-23 03:38:26 -0600 |
---|---|---|
committer | Austin Seipp <austin@well-typed.com> | 2015-02-23 03:38:27 -0600 |
commit | 0fa20726b0587530712677e50a56c2b03ba43095 (patch) | |
tree | b82eaaf4828a17dfc72845ac11a918feed801bc3 /compiler | |
parent | b2be772a97f6e7fe9f1d1c28108949f81a13158b (diff) | |
download | haskell-0fa20726b0587530712677e50a56c2b03ba43095.tar.gz |
Error out on `Main` without `main` in GHCi (#7765)
Summary:
GHC does 2 validation checks for module `Main`:
* does `main` exist
* is `main` exported (#414)
The second check is done in ghc as well as in ghci (and runghc and ghc -e).
The first check however is currently not done in ghci, to prevent "'main' is
not in scope" errors when loading simple scripts. See commit d28ba8c8009 for
more information.
This commit tightens the special case for ghci. When the file does not contain
a main function, but does contain an explicit module header (i.e. "module Main
where"), then /do/ raise an error in ghci (and runghc and ghc -e) as well
Test Plan:
module/T7765: a module Main with an explicit module header but without a
main function should be an error for all Ways.
Additionaly: delete test module/mod174. It was added in commit 5a54c38, but it
is a duplicate of module/T414.
Reviewers: austin
Reviewed By: austin
Subscribers: thomie
Differential Revision: https://phabricator.haskell.org/D649
GHC Trac Issues: #7765
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/typecheck/TcRnDriver.hs | 37 | ||||
-rw-r--r-- | compiler/typecheck/TcRnMonad.hs | 5 | ||||
-rw-r--r-- | compiler/typecheck/TcRnTypes.hs | 4 | ||||
-rw-r--r-- | compiler/utils/IOEnv.hs | 9 |
4 files changed, 21 insertions, 34 deletions
diff --git a/compiler/typecheck/TcRnDriver.hs b/compiler/typecheck/TcRnDriver.hs index c3b16ca367..85d5a2ade1 100644 --- a/compiler/typecheck/TcRnDriver.hs +++ b/compiler/typecheck/TcRnDriver.hs @@ -3,6 +3,8 @@ (c) The GRASP/AQUA Project, Glasgow University, 1992-1998 \section[TcMovectle]{Typechecking a whole module} + +https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/TypeChecker -} {-# LANGUAGE CPP, NondecreasingIndentation #-} @@ -268,7 +270,7 @@ tcRnModuleTcRnM :: HscEnv -> HsParsedModule -> (Module, SrcSpan) -> TcRn TcGblEnv --- Factored out separately so that a Core plugin can +-- Factored out separately from tcRnModule so that a Core plugin can -- call the type checker directly tcRnModuleTcRnM hsc_env hsc_src (HsParsedModule { @@ -283,7 +285,7 @@ tcRnModuleTcRnM hsc_env hsc_src do { let { dflags = hsc_dflags hsc_env } ; tcg_env <- tcRnSignature dflags hsc_src ; - setGblEnv tcg_env $ do { + setGblEnv tcg_env { tcg_mod_name=maybe_mod } $ do { -- Deal with imports; first add implicit prelude implicit_prelude <- xoptM Opt_ImplicitPrelude; @@ -1070,23 +1072,11 @@ instMisMatch is_boot inst {- ************************************************************************ * * - Type-checking the top level of a module + Type-checking the top level of a module (continued) * * ************************************************************************ - -tcRnGroup takes a bunch of top-level source-code declarations, and - * renames them - * gets supporting declarations from interface files - * typechecks them - * zonks them - * and augments the TcGblEnv with the results - -In Template Haskell it may be called repeatedly for each group of -declarations. It expects there to be an incoming TcGblEnv in the -monad; it augments it and returns the new TcGblEnv. -} ------------------------------------------------- rnTopSrcDecls :: [Name] -> HsGroup RdrName -> TcM (TcGblEnv, HsGroup Name) -- Fails if there are any errors rnTopSrcDecls extra_deps group @@ -1108,14 +1098,6 @@ rnTopSrcDecls extra_deps group return (tcg_env', rn_decls) } -{- -************************************************************************ -* * - tcTopSrcDecls -* * -************************************************************************ --} - tcTopSrcDecls :: ModDetails -> HsGroup Name -> TcM (TcGblEnv, TcLclEnv) tcTopSrcDecls boot_details (HsGroup { hs_tyclds = tycl_decls, @@ -1339,10 +1321,12 @@ check_main dflags tcg_env mod = tcg_mod tcg_env main_mod = mainModIs dflags main_fn = getMainFun dflags + interactive = ghcLink dflags == LinkInMemory + implicit_mod = isNothing (tcg_mod_name tcg_env) - complain_no_main | ghcLink dflags == LinkInMemory = return () - | otherwise = failWithTc noMainMsg - -- In interactive mode, don't worry about the absence of 'main' + complain_no_main = checkTc (interactive && implicit_mod) noMainMsg + -- In interactive mode, without an explicit module header, don't + -- worry about the absence of 'main'. -- In other modes, fail altogether, so that we don't go on -- and complain a second time when processing the export list. @@ -1358,6 +1342,7 @@ getMainFun dflags = case mainFunIs dflags of Just fn -> mkRdrUnqual (mkVarOccFS (mkFastString fn)) Nothing -> main_RDR_Unqual +-- If we are in module Main, check that 'main' is exported. checkMainExported :: TcGblEnv -> TcM () checkMainExported tcg_env = case tcg_main tcg_env of diff --git a/compiler/typecheck/TcRnMonad.hs b/compiler/typecheck/TcRnMonad.hs index 84ae0b97de..f3c16cb555 100644 --- a/compiler/typecheck/TcRnMonad.hs +++ b/compiler/typecheck/TcRnMonad.hs @@ -120,6 +120,7 @@ initTc hsc_env hsc_src keep_rn_syntax mod loc do_this tcg_mod = mod, tcg_src = hsc_src, tcg_sig_of = getSigOf dflags (moduleName mod), + tcg_mod_name = Nothing, tcg_impl_rdr_env = Nothing, tcg_rdr_env = emptyGlobalRdrEnv, tcg_fix_env = emptyNameEnv, @@ -463,7 +464,7 @@ instance MonadUnique (IOEnv (Env gbl lcl)) where {- ************************************************************************ * * - Debugging + Accessing input/output * * ************************************************************************ -} @@ -762,7 +763,7 @@ reportWarning err ; writeTcRef errs_var (warns `snocBag` warn, errs) } try_m :: TcRn r -> TcRn (Either IOEnvFailure r) --- Does try_m, with a debug-trace on failure +-- Does tryM, with a debug-trace on failure try_m thing = do { mb_r <- tryM thing ; case mb_r of diff --git a/compiler/typecheck/TcRnTypes.hs b/compiler/typecheck/TcRnTypes.hs index b460faee12..23d0635868 100644 --- a/compiler/typecheck/TcRnTypes.hs +++ b/compiler/typecheck/TcRnTypes.hs @@ -27,7 +27,7 @@ module TcRnTypes( TcGblEnv(..), TcLclEnv(..), IfGblEnv(..), IfLclEnv(..), - -- Ranamer types + -- Renamer types ErrCtxt, RecFieldEnv(..), ImportAvails(..), emptyImportAvails, plusImportAvails, WhereFrom(..), mkModDeps, @@ -334,6 +334,8 @@ data TcGblEnv -- ^ What kind of module (regular Haskell, hs-boot, ext-core) tcg_sig_of :: Maybe Module, -- ^ Are we being compiled as a signature of an implementation? + tcg_mod_name :: Maybe (Located ModuleName), + -- ^ @Nothing@: \"module X where\" is omitted tcg_impl_rdr_env :: Maybe GlobalRdrEnv, -- ^ Environment used only during -sig-of for resolving top level -- bindings. See Note [Signature parameters in TcGblEnv and DynFlags] diff --git a/compiler/utils/IOEnv.hs b/compiler/utils/IOEnv.hs index fd98bad213..1ddf170cf0 100644 --- a/compiler/utils/IOEnv.hs +++ b/compiler/utils/IOEnv.hs @@ -102,7 +102,7 @@ instance ContainsModule env => HasModule (IOEnv env) where return $ extractModule env ---------------------------------------------------------------------- --- Fundmantal combinators specific to the monad +-- Fundamental combinators specific to the monad ---------------------------------------------------------------------- @@ -113,9 +113,9 @@ runIOEnv env (IOEnv m) = m env --------------------------- {-# NOINLINE fixM #-} - -- Aargh! Not inlining fixTc alleviates a space leak problem. - -- Normally fixTc is used with a lazy tuple match: if the optimiser is - -- shown the definition of fixTc, it occasionally transforms the code + -- Aargh! Not inlining fixM alleviates a space leak problem. + -- Normally fixM is used with a lazy tuple match: if the optimiser is + -- shown the definition of fixM, it occasionally transforms the code -- in such a way that the code generator doesn't spot the selector -- thunks. Sigh. @@ -213,4 +213,3 @@ setEnv new_env (IOEnv m) = IOEnv (\ _ -> m new_env) updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a {-# INLINE updEnv #-} updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env)) - |