Commit 15b9a85e authored by Yuras's avatar Yuras Committed by Ben Gamari
Browse files

Warn on missing home modules

Introduce a warning, -Wmissing-home-modules, to warn about home modules,
not listed in command line.

It is usefull for cabal when user fails to list a module in
`exposed-modules` and `other-modules`.

Test Plan: make TEST=MissingMod

Reviewers: mpickering, austin, bgamari

Reviewed By: bgamari

Subscribers: simonpj, mpickering, thomie

Differential Revision:

GHC Trac Issues: #13129
parent c43011da
......@@ -636,6 +636,7 @@ data WarningFlag =
| Opt_WarnSimplifiableClassConstraints -- Since 8.2
| Opt_WarnCPPUndef -- Since 8.2
| Opt_WarnUnbangedStrictPatterns -- Since 8.2
| Opt_WarnMissingHomeModules -- Since 8.2
deriving (Eq, Show, Enum)
data Language = Haskell98 | Haskell2010
......@@ -3443,6 +3444,7 @@ wWarningFlagsDeps = [
flagSpec "missing-pattern-synonym-signatures"
flagSpec "simplifiable-class-constraints" Opt_WarnSimplifiableClassConstraints,
flagSpec "missing-home-modules" Opt_WarnMissingHomeModules,
flagSpec "unrecognised-warning-flags" Opt_WarnUnrecognisedWarningFlags ]
-- | These @-\<blah\>@ flags can all be reversed with @-no-\<blah\>@
......@@ -132,9 +132,48 @@ depanal excluded_mods allow_dup_roots = do
mod_graphE <- liftIO $ downsweep hsc_env old_graph
excluded_mods allow_dup_roots
mod_graph <- reportImportErrors mod_graphE
warnMissingHomeModules hsc_env mod_graph
setSession hsc_env { hsc_mod_graph = mod_graph }
return mod_graph
-- Note [Missing home modules]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- Sometimes user doesn't want GHC to pick up modules, not explicitly listed
-- in a command line. For example, cabal may want to enable this warning
-- when building a library, so that GHC warns user about modules, not listed
-- neither in `exposed-modules`, nor in `other-modules`.
-- Here "home module" means a module, that doesn't come from an other package.
-- For example, if GHC is invoked with modules "A" and "B" as targets,
-- but "A" imports some other module "C", then GHC will issue a warning
-- about module "C" not being listed in a command line.
-- The warning in enabled by `-Wmissing-home-modules`. See Trac #13129
warnMissingHomeModules :: GhcMonad m => HscEnv -> ModuleGraph -> m ()
warnMissingHomeModules hsc_env mod_graph =
when (wopt Opt_WarnMissingHomeModules dflags && not (null missing)) $
logWarnings (listToBag [warn])
dflags = hsc_dflags hsc_env
missing = filter (`notElem` targets) imports
imports = map (moduleName . ms_mod) mod_graph
targets = map (targetid_to_name . targetId) (hsc_targets hsc_env)
msg = text "Modules are not listed in command line: "
<> sep (map ppr missing)
warn = makeIntoWarning
(Reason Opt_WarnMissingHomeModules)
(mkPlainErrMsg dflags noSrcSpan msg)
targetid_to_name (TargetModule name) = name
targetid_to_name (TargetFile file _) =
-- We can get a file even if module name in specified in command line
-- because it can be converted in guessTarget. So lets convert it back.
mkModuleName (fst $ splitExtension file)
-- | Describes which modules of the module graph need to be loaded.
data LoadHowMuch
= LoadAllTargets
......@@ -66,6 +66,7 @@ The following flags are simple ways to select standard "packages" of warnings:
* :ghc-flag:`-Wmissing-local-signatures`
* :ghc-flag:`-Wmissing-exported-signatures`
* :ghc-flag:`-Wmissing-import-lists`
* :ghc-flag:`-Wmissing-home-modules`
* :ghc-flag:`-Widentities`
.. ghc-flag:: -Wcompat
......@@ -1035,8 +1036,18 @@ of ``-W(no-)*``.
This flag warns whenever you write a pattern that binds a variable whose
type is unlifted, and yet the pattern is not a bang pattern nor a bare variable.
See :ref:`glasgow-unboxed` for informatino about unlifted types.
See :ref:`glasgow-unboxed` for information about unlifted types.
.. ghc-flag:: -Wmissing-home-modules
:since: 8.2
When a module provided by the package currently being compiled
(i.e. the "home" package) is imported, but not explicitly listed in
command line as a target. Useful for Cabal to ensure GHC won't
pick up modules, not listed neither in ``exposed-modules``, nor in
If you're feeling really paranoid, the :ghc-flag:`-dcore-lint` option is a good choice.
It turns on heavyweight intra-pass sanity-checking within GHC. (It checks GHC's
sanity, not yours.)
module MissingMod
import MissingMod1
<no location info>: warning: [-Wmissing-home-modules]
Modules are not listed in command line: MissingMod1
[1 of 2] Compiling MissingMod1 ( MissingMod1.hs, MissingMod1.o )
[2 of 2] Compiling MissingMod ( MissingMod.hs, MissingMod.o )
......@@ -24,3 +24,5 @@ test('DeprU',
test('Werror01', normal, compile, [''])
test('Werror02', normal, compile, [''])
test('MissingMod', normal, multimod_compile, ['MissingMod', '-Wmissing-home-modules'])
......@@ -445,4 +445,13 @@ warningsOptions =
, flagType = DynamicFlag
, flagReverse = "-Wno-deriving-typeable"
, flag { flagName = "-Wmissing-home-modules"
, flagDescription =
"warn when encountering a home module imported, but not listed "++
"on the command line. Useful for cabal to ensure GHC won't pick "++
"up modules, not listed neither in ``exposed-modules``, nor in "++
, flagType = DynamicFlag
, flagReverse = "-Wno-missing-home-modules"
Supports Markdown
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