diff options
author | Matthew Pickering <matthewtpickering@gmail.com> | 2021-08-18 12:54:51 +0100 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2021-08-23 13:35:26 -0400 |
commit | c1acfd210153dc78748904467c6be023c702467a (patch) | |
tree | 35b9d956bf90d337c5cb05e7dd548510f0d07d78 | |
parent | 06aa8da540b971bb5756636ea5945c3382662b2f (diff) | |
download | haskell-c1acfd210153dc78748904467c6be023c702467a.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
-rw-r--r-- | compiler/GHC/Driver/Make.hs | 31 | ||||
-rw-r--r-- | testsuite/tests/cabal/t20242/BootNoHeader.cabal | 12 | ||||
-rw-r--r-- | testsuite/tests/cabal/t20242/Foo.hs | 4 | ||||
-rw-r--r-- | testsuite/tests/cabal/t20242/Foo.hs-boot | 1 | ||||
-rw-r--r-- | testsuite/tests/cabal/t20242/Main.hs | 5 | ||||
-rw-r--r-- | testsuite/tests/cabal/t20242/Makefile | 18 | ||||
-rw-r--r-- | testsuite/tests/cabal/t20242/Setup.hs | 2 | ||||
-rw-r--r-- | testsuite/tests/cabal/t20242/T20242.stderr | 5 | ||||
-rw-r--r-- | testsuite/tests/cabal/t20242/all.T | 9 | ||||
-rw-r--r-- | testsuite/tests/warnings/should_compile/UnusedPackages.stderr | 14 |
10 files changed, 79 insertions, 22 deletions
diff --git a/compiler/GHC/Driver/Make.hs b/compiler/GHC/Driver/Make.hs index 736d5771f5..33cfbfe4f4 100644 --- a/compiler/GHC/Driver/Make.hs +++ b/compiler/GHC/Driver/Make.hs @@ -187,9 +187,11 @@ depanalE excluded_mods allow_dup_roots = do (errs, mod_graph) <- depanalPartial excluded_mods allow_dup_roots if isEmptyMessages 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 + logDiagnostics (GhcDriverMessage <$> (unused_home_mod_err `unionMessages` unused_pkg_err)) setSession hsc_env { hsc_mod_graph = mod_graph } - pure (errs, mod_graph) + pure (emptyMessages, mod_graph) else do -- We don't have a complete module dependency graph, -- The graph may be disconnected and is unusable. @@ -276,10 +278,11 @@ instantiationNodes unit_state = InstantiationNode <$> iuids_to_check -- 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 -> DriverMessages warnMissingHomeModules hsc_env mod_graph = - when (not (null missing)) $ - logDiagnostics (GhcDriverMessage <$> warn) + if null missing + then emptyMessages + else warn where dflags = hsc_dflags hsc_env targets = map targetId (hsc_targets hsc_env) @@ -347,7 +350,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 isEmptyMessages errs then pure success else throwErrors (fmap GhcDriverMessage errs) @@ -359,30 +361,29 @@ 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 -> DriverMessages +warnUnusedPackages hsc_env mod_graph = let dflags = hsc_dflags hsc_env state = hsc_units hsc_env diag_opts = initDiagOpts dflags us = hsc_units hsc_env -- Only need non-source imports here because SOURCE imports are always HPT - let loadedPackages = concat $ + loadedPackages = concat $ mapMaybe (\(fs, mn) -> lookupModulePackage us (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 = singleMessage $ mkPlainMsgEnvelope diag_opts noSrcSpan (DriverUnusedPackages unusedArgs) + warn = singleMessage $ mkPlainMsgEnvelope diag_opts noSrcSpan (DriverUnusedPackages unusedArgs) - when (not (null unusedArgs)) $ - logDiagnostics (GhcDriverMessage <$> warn) + in if null unusedArgs + then emptyMessages + else warn 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..73c50e8c51 --- /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 ... |