Skip to content
Snippets Groups Projects
Commit c1acfd21 authored by Matthew Pickering's avatar Matthew Pickering Committed by Marge Bot
Browse files

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
parent 06aa8da5
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
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
module Foo where
a :: Int
a = 5
a :: Int
module Main where
import {-# SOURCE #-} Foo
main = putStrLn "Hello, world!"
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
import Distribution.Simple
main = defaultMain
Foo.hs-boot:1:1: error:
File name does not match module name:
Saw : ‘Main’
Expected: ‘Foo’
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])
[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 ...
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment