[commit: ghc] wip/D5285: Warn about unused packages (076584c)
git at git.haskell.org
git at git.haskell.org
Sun Mar 31 15:09:07 UTC 2019
Repository : ssh://git@git.haskell.org/ghc
On branch : wip/D5285
Link : http://ghc.haskell.org/trac/ghc/changeset/076584c11bf536dcc15f56b01dafe2957bcb02d4/ghc
>---------------------------------------------------------------
commit 076584c11bf536dcc15f56b01dafe2957bcb02d4
Author: Yuras Shumovich <shumovichy at gmail.com>
Date: Sun Jan 20 19:49:56 2019 -0500
Warn about unused packages
Reviewers: bgamari, simonpj
Reviewed By: simonpj
Subscribers: hvr, simonpj, mpickering, rwbarton, carter
GHC Trac Issues: #15838
Differential Revision: https://phabricator.haskell.org/D5285
>---------------------------------------------------------------
076584c11bf536dcc15f56b01dafe2957bcb02d4
compiler/main/DynFlags.hs | 4 +-
compiler/main/GhcMake.hs | 70 +++++++++++++++++++++-
docs/users_guide/8.8.1-notes.rst | 2 +
docs/users_guide/using-warnings.rst | 15 +++++
.../should_compile/UnusedPackages.hs} | 3 +-
.../warnings/should_compile/UnusedPackages.stderr | 6 ++
testsuite/tests/warnings/should_compile/all.T | 2 +
7 files changed, 99 insertions(+), 3 deletions(-)
diff --git a/compiler/main/DynFlags.hs b/compiler/main/DynFlags.hs
index 78be688..74c7dbd 100644
--- a/compiler/main/DynFlags.hs
+++ b/compiler/main/DynFlags.hs
@@ -836,6 +836,7 @@ data WarningFlag =
| Opt_WarnImplicitKindVars -- Since 8.6
| Opt_WarnSpaceAfterBang
| Opt_WarnMissingDerivingStrategies -- Since 8.8
+ | Opt_WarnUnusedPackages -- Since 8.10
deriving (Eq, Show, Enum)
data Language = Haskell98 | Haskell2010
@@ -4047,7 +4048,8 @@ wWarningFlagsDeps = [
flagSpec "star-binder" Opt_WarnStarBinder,
flagSpec "star-is-type" Opt_WarnStarIsType,
flagSpec "missing-space-after-bang" Opt_WarnSpaceAfterBang,
- flagSpec "partial-fields" Opt_WarnPartialFields ]
+ flagSpec "partial-fields" Opt_WarnPartialFields,
+ flagSpec "unused-packages" Opt_WarnUnusedPackages ]
-- | These @-\<blah\>@ flags can all be reversed with @-no-\<blah\>@
negatableFlagsDeps :: [(Deprecation, FlagSpec GeneralFlag)]
diff --git a/compiler/main/GhcMake.hs b/compiler/main/GhcMake.hs
index ae27d4e..3574993 100644
--- a/compiler/main/GhcMake.hs
+++ b/compiler/main/GhcMake.hs
@@ -238,7 +238,75 @@ data LoadHowMuch
load :: GhcMonad m => LoadHowMuch -> m SuccessFlag
load how_much = do
mod_graph <- depanal [] False
- load' how_much (Just batchMsg) mod_graph
+ success <- load' how_much (Just batchMsg) mod_graph
+ warnUnusedPackages
+ pure success
+
+-- Note [Unused packages]
+--
+-- Cabal passes `--package-id` flag for each direct dependency. But GHC
+-- loads them lazily, so when compilation is done, we have a list of all
+-- actually loaded packages. All the packages, specified on command line,
+-- but never loaded, are probably unused dependencies.
+
+warnUnusedPackages :: GhcMonad m => m ()
+warnUnusedPackages = do
+ hsc_env <- getSession
+ eps <- liftIO $ hscEPS hsc_env
+
+ let dflags = hsc_dflags hsc_env
+ pit = eps_PIT eps
+
+ let loadedPackages
+ = map (getPackageDetails dflags)
+ . nub . sort
+ . map moduleUnitId
+ . moduleEnvKeys
+ $ pit
+
+ requestedArgs = mapMaybe packageArg (packageFlags dflags)
+
+ unusedArgs
+ = filter (\arg -> not $ any (matching dflags arg) loadedPackages)
+ requestedArgs
+
+ let warn = makeIntoWarning
+ (Reason Opt_WarnUnusedPackages)
+ (mkPlainErrMsg dflags noSrcSpan msg)
+ msg = hang
+ ( text "The following packages were specified "
+ <> text "via -package or -package-id flags, "
+ <> text "but were not needed for compilation: ")
+ 4
+ (sep (map pprUnusedArg unusedArgs))
+
+ when (wopt Opt_WarnUnusedPackages dflags && not (null unusedArgs)) $
+ logWarnings (listToBag [warn])
+
+ where
+ packageArg (ExposePackage _ arg _) = Just arg
+ packageArg _ = Nothing
+
+ pprUnusedArg (PackageArg str) = text str
+ pprUnusedArg (UnitIdArg uid) = ppr uid
+
+ matchingStr :: String -> PackageConfig -> Bool
+ matchingStr str p
+ = str == sourcePackageIdString p
+ || str == packageNameString p
+
+ matching :: DynFlags -> PackageArg -> PackageConfig -> Bool
+ matching _ (PackageArg str) p = matchingStr str p
+ matching dflags (UnitIdArg uid) p = uid == realUnitId dflags p
+
+ -- For wired-in packages, we have to unwire their id,
+ -- otherwise they won't match package flags
+ realUnitId :: DynFlags -> PackageConfig -> UnitId
+ realUnitId dflags
+ = unwireUnitId dflags
+ . DefiniteUnitId
+ . DefUnitId
+ . installedPackageConfigId
-- | Generalized version of 'load' which also supports a custom
-- 'Messager' (for reporting progress) and 'ModuleGraph' (generally
diff --git a/docs/users_guide/8.8.1-notes.rst b/docs/users_guide/8.8.1-notes.rst
index cd4c00d..b022b6c 100644
--- a/docs/users_guide/8.8.1-notes.rst
+++ b/docs/users_guide/8.8.1-notes.rst
@@ -76,6 +76,8 @@ Compiler
- The :ghc-flag:`-Wcompat` warning group now includes :ghc-flag:`-Wstar-is-type`.
+- New :ghc-flag:`-Wunused-packages` warning reports unused packages.
+
- The :ghc-flag:`-fllvm-pass-vectors-in-regs` flag is now deprecated as vector
arguments are now passed in registers by default.
diff --git a/docs/users_guide/using-warnings.rst b/docs/users_guide/using-warnings.rst
index 03ca184..8bf2348 100644
--- a/docs/users_guide/using-warnings.rst
+++ b/docs/users_guide/using-warnings.rst
@@ -1690,6 +1690,21 @@ of ``-W(no-)*``.
data Foo = Foo { f :: Int } | Bar
+.. ghc-flag:: -Wunused-packages
+ :shortdesc: warn when package is requested on command line, but was never loaded.
+ :type: dynamic
+ :reverse: -Wno-unused-packages
+ :category:
+
+ :since: 8.8
+
+ The option :ghc-flag:`-Wunused-packages` warns about packages, specified on
+ command line via :ghc-flag:`-package` or :ghc-flag:`-package-id`, but were not
+ loaded during compication. Usually it means that you have an unused dependency.
+
+ You may want to enable this warning on a clean build or enable :ghc-flag:`-fforce-recomp`
+ in order to get reliable results.
+
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.)
diff --git a/testsuite/tests/dynlibs/T5373B.hs b/testsuite/tests/warnings/should_compile/UnusedPackages.hs
similarity index 64%
copy from testsuite/tests/dynlibs/T5373B.hs
copy to testsuite/tests/warnings/should_compile/UnusedPackages.hs
index 0570fb1..ef70dbb 100644
--- a/testsuite/tests/dynlibs/T5373B.hs
+++ b/testsuite/tests/warnings/should_compile/UnusedPackages.hs
@@ -1,4 +1,5 @@
+module Main
+where
main :: IO ()
main = return ()
-
diff --git a/testsuite/tests/warnings/should_compile/UnusedPackages.stderr b/testsuite/tests/warnings/should_compile/UnusedPackages.stderr
new file mode 100644
index 0000000..7660287
--- /dev/null
+++ b/testsuite/tests/warnings/should_compile/UnusedPackages.stderr
@@ -0,0 +1,6 @@
+[1 of 1] Compiling Main ( UnusedPackages.hs, UnusedPackages.o )
+Linking UnusedPackages ...
+
+<no location info>: warning: [-Wunused-packages]
+ The following packages were specified via -package or -package-id flags, but were not needed for compilation:
+ bytestring
diff --git a/testsuite/tests/warnings/should_compile/all.T b/testsuite/tests/warnings/should_compile/all.T
index 10a3ecf..686eee9 100644
--- a/testsuite/tests/warnings/should_compile/all.T
+++ b/testsuite/tests/warnings/should_compile/all.T
@@ -28,3 +28,5 @@ test('MissingMod', normal, multimod_compile, ['MissingMod', '-Wmissing-home-modu
test('StarBinder', normal, compile, [''])
test('Overflow', normal, compile, [''])
+
+test('UnusedPackages', normal, multimod_compile, ['UnusedPackages.hs', '-package=bytestring -package=base -Wunused-packages'])
More information about the ghc-commits
mailing list