summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Pickering <matthewtpickering@gmail.com>2021-08-18 12:54:51 +0100
committerZubin Duggal <zubin.duggal@gmail.com>2021-09-24 14:54:42 +0530
commite5008c70a950a4a2963d9176d30ec858acb9125f (patch)
tree540ab6382098fc0b7c1df190dc0cd4dc67b9fd22
parent6de0f80b4a79d3767640feee67f8b5747c0acd38 (diff)
downloadhaskell-e5008c70a950a4a2963d9176d30ec858acb9125f.tar.gz
driver: Only check for unused package warning in after succesful downsweep
Before we would check for the unused package warning even if the module graph was compromised due to an error in downsweep. This is easily fixed by pushing warmUnusedPackages into depanalE, and then returning the errors like the other downsweep errors. Fixes #20242 (cherry picked from commit c1acfd210153dc78748904467c6be023c702467a)
-rw-r--r--compiler/GHC/Driver/Make.hs31
-rw-r--r--testsuite/tests/cabal/t20242/BootNoHeader.cabal12
-rw-r--r--testsuite/tests/cabal/t20242/Foo.hs4
-rw-r--r--testsuite/tests/cabal/t20242/Foo.hs-boot1
-rw-r--r--testsuite/tests/cabal/t20242/Main.hs5
-rw-r--r--testsuite/tests/cabal/t20242/Makefile18
-rw-r--r--testsuite/tests/cabal/t20242/Setup.hs2
-rw-r--r--testsuite/tests/cabal/t20242/T20242.stderr5
-rw-r--r--testsuite/tests/cabal/t20242/all.T9
-rw-r--r--testsuite/tests/warnings/should_compile/UnusedPackages.stderr14
10 files changed, 80 insertions, 21 deletions
diff --git a/compiler/GHC/Driver/Make.hs b/compiler/GHC/Driver/Make.hs
index e1b6fca4c9..ecc9353c2a 100644
--- a/compiler/GHC/Driver/Make.hs
+++ b/compiler/GHC/Driver/Make.hs
@@ -143,7 +143,11 @@ depanalE excluded_mods allow_dup_roots = do
(errs, mod_graph) <- depanalPartial excluded_mods allow_dup_roots
if isEmptyBag errs
then do
- warnMissingHomeModules hsc_env mod_graph
+ let unused_home_mod_err = warnMissingHomeModules hsc_env mod_graph
+ unused_pkg_err = warnUnusedPackages hsc_env mod_graph
+ warns = unused_home_mod_err ++ unused_pkg_err
+ when (not $ null warns) $
+ logWarnings (listToBag warns)
setSession hsc_env { hsc_mod_graph = mod_graph }
pure (errs, mod_graph)
else do
@@ -207,10 +211,11 @@ depanalPartial excluded_mods allow_dup_roots = do
-- about module "C" not being listed in a command line.
--
-- The warning in enabled by `-Wmissing-home-modules`. See #13129
-warnMissingHomeModules :: GhcMonad m => HscEnv -> ModuleGraph -> m ()
+warnMissingHomeModules :: HscEnv -> ModuleGraph -> [ErrMsg]
warnMissingHomeModules hsc_env mod_graph =
- when (wopt Opt_WarnMissingHomeModules dflags && not (null missing)) $
- logWarnings (listToBag [warn])
+ if (wopt Opt_WarnMissingHomeModules dflags && not (null missing))
+ then [warn]
+ else []
where
dflags = hsc_dflags hsc_env
targets = map targetId (hsc_targets hsc_env)
@@ -290,7 +295,6 @@ load :: GhcMonad m => LoadHowMuch -> m SuccessFlag
load how_much = do
(errs, mod_graph) <- depanalE [] False -- #17459
success <- load' how_much (Just batchMsg) mod_graph
- warnUnusedPackages mod_graph
if isEmptyBag errs
then pure success
else throwErrors errs
@@ -302,25 +306,23 @@ load how_much = do
-- actually loaded packages. All the packages, specified on command line,
-- but never loaded, are probably unused dependencies.
-warnUnusedPackages :: GhcMonad m => ModuleGraph -> m ()
-warnUnusedPackages mod_graph = do
- hsc_env <- getSession
-
+warnUnusedPackages :: HscEnv -> ModuleGraph -> [ErrMsg]
+warnUnusedPackages hsc_env mod_graph =
let dflags = hsc_dflags hsc_env
state = unitState dflags
-- Only need non-source imports here because SOURCE imports are always HPT
- let loadedPackages = concat $
+ loadedPackages = concat $
mapMaybe (\(fs, mn) -> lookupModulePackage state (unLoc mn) fs)
$ concatMap ms_imps (mgModSummaries mod_graph)
- let requestedArgs = mapMaybe packageArg (packageFlags dflags)
+ requestedArgs = mapMaybe packageArg (packageFlags dflags)
unusedArgs
= filter (\arg -> not $ any (matching state arg) loadedPackages)
requestedArgs
- let warn = makeIntoWarning
+ warn = makeIntoWarning
(Reason Opt_WarnUnusedPackages)
(mkPlainErrMsg dflags noSrcSpan msg)
msg = vcat [ text "The following packages were specified" <+>
@@ -328,8 +330,9 @@ warnUnusedPackages mod_graph = do
, text "but were not needed for compilation:"
, nest 2 (vcat (map (withDash . pprUnusedArg) unusedArgs)) ]
- when (wopt Opt_WarnUnusedPackages dflags && not (null unusedArgs)) $
- logWarnings (listToBag [warn])
+ in if not (null unusedArgs) && wopt Opt_WarnUnusedPackages dflags
+ then [warn]
+ else []
where
packageArg (ExposePackage _ arg _) = Just arg
diff --git a/testsuite/tests/cabal/t20242/BootNoHeader.cabal b/testsuite/tests/cabal/t20242/BootNoHeader.cabal
new file mode 100644
index 0000000000..1a20184494
--- /dev/null
+++ b/testsuite/tests/cabal/t20242/BootNoHeader.cabal
@@ -0,0 +1,12 @@
+cabal-version: 2.4
+name: BootNoHeader
+version: 0.1.0.0
+
+executable BootNoHeader
+ main-is: Main.hs
+ other-modules: Foo
+ build-depends: base
+ hs-source-dirs: .
+ default-language: Haskell2010
+
+ ghc-options: -Werror=unused-packages
diff --git a/testsuite/tests/cabal/t20242/Foo.hs b/testsuite/tests/cabal/t20242/Foo.hs
new file mode 100644
index 0000000000..c3707c7a24
--- /dev/null
+++ b/testsuite/tests/cabal/t20242/Foo.hs
@@ -0,0 +1,4 @@
+module Foo where
+
+a :: Int
+a = 5
diff --git a/testsuite/tests/cabal/t20242/Foo.hs-boot b/testsuite/tests/cabal/t20242/Foo.hs-boot
new file mode 100644
index 0000000000..9aa298fba9
--- /dev/null
+++ b/testsuite/tests/cabal/t20242/Foo.hs-boot
@@ -0,0 +1 @@
+a :: Int
diff --git a/testsuite/tests/cabal/t20242/Main.hs b/testsuite/tests/cabal/t20242/Main.hs
new file mode 100644
index 0000000000..de3cdee837
--- /dev/null
+++ b/testsuite/tests/cabal/t20242/Main.hs
@@ -0,0 +1,5 @@
+module Main where
+
+import {-# SOURCE #-} Foo
+
+main = putStrLn "Hello, world!"
diff --git a/testsuite/tests/cabal/t20242/Makefile b/testsuite/tests/cabal/t20242/Makefile
new file mode 100644
index 0000000000..1eab3808e6
--- /dev/null
+++ b/testsuite/tests/cabal/t20242/Makefile
@@ -0,0 +1,18 @@
+TOP=../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
+
+SETUP = ./Setup -v0
+
+T20242: clean
+ $(MAKE) clean
+ '$(TEST_HC)' $(TEST_HC_OPTS) -v0 --make Setup
+ $(SETUP) clean
+ $(SETUP) configure $(CABAL_MINIMAL_BUILD) --with-ghc='$(TEST_HC)' --ghc-options='$(TEST_HC_OPTS)'
+ $(SETUP) build || true
+ifneq "$(CLEANUP)" ""
+ $(MAKE) clean
+endif
+
+clean :
+ $(RM) -r */dist Setup$(exeext) *.o *.hi
diff --git a/testsuite/tests/cabal/t20242/Setup.hs b/testsuite/tests/cabal/t20242/Setup.hs
new file mode 100644
index 0000000000..9a994af677
--- /dev/null
+++ b/testsuite/tests/cabal/t20242/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/testsuite/tests/cabal/t20242/T20242.stderr b/testsuite/tests/cabal/t20242/T20242.stderr
new file mode 100644
index 0000000000..162f5e515d
--- /dev/null
+++ b/testsuite/tests/cabal/t20242/T20242.stderr
@@ -0,0 +1,5 @@
+
+Foo.hs-boot:1:1: error:
+ File name does not match module name:
+ Saw: ‘Main’
+ Expected: ‘Foo’
diff --git a/testsuite/tests/cabal/t20242/all.T b/testsuite/tests/cabal/t20242/all.T
new file mode 100644
index 0000000000..4e1c0387ee
--- /dev/null
+++ b/testsuite/tests/cabal/t20242/all.T
@@ -0,0 +1,9 @@
+if config.cleanup:
+ cleanup = 'CLEANUP=1'
+else:
+ cleanup = 'CLEANUP=0'
+
+test('T20242',
+ [extra_files(['Setup.hs', 'BootNoHeader.cabal','Foo.hs', 'Foo.hs-boot', 'Main.hs'])],
+ run_command,
+ ['$MAKE -s --no-print-directory T20242 ' + cleanup])
diff --git a/testsuite/tests/warnings/should_compile/UnusedPackages.stderr b/testsuite/tests/warnings/should_compile/UnusedPackages.stderr
index 4eea3bfc8b..ba6a76207f 100644
--- a/testsuite/tests/warnings/should_compile/UnusedPackages.stderr
+++ b/testsuite/tests/warnings/should_compile/UnusedPackages.stderr
@@ -1,9 +1,9 @@
-[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:
- - ghc
- - process
- - bytestring
+ The following packages were specified via -package or -package-id flags,
+ but were not needed for compilation:
+ - ghc
+ - process
+ - bytestring
+[1 of 1] Compiling Main ( UnusedPackages.hs, UnusedPackages.o )
+Linking UnusedPackages ...