Ticket #1074 (new bug)

Opened 2 years ago

Last modified 1 month ago

-fwarn-unused-imports complains about wrong import

Reported by: guest Assigned to:
Priority: high Milestone: 6.10.2
Component: Compiler Version: 6.6
Severity: minor Keywords:
Cc: jyasskin@gmail.com, neil.mitchell.2@credit-suisse.com Difficulty: Unknown
Test Case: Architecture: Unknown/Multiple
Operating System: Unknown/Multiple

Description

If a symbol is accessible through multiple imports, ghc seems to complain about the first one, even if that's the one it's actually accessed through.

$ cat Test.hs
module Test where

import qualified Control.Monad (ap)
import qualified Control.Monad.Reader

foo :: IO ()
foo = return id `Control.Monad.ap` return ()

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.6

$ ghc -c Test.hs -Werror -fwarn-unused-imports

Test.hs:3:0:
    Warning: Module `Control.Monad' is imported, but nothing from it is used,
               except perhaps instances visible in `Control.Monad'
             To suppress this warning, use: import Control.Monad()

$

Change History

12/29/06 23:56:49 changed by guest

  • cc set to jyasskin@gmail.com.

01/03/07 01:11:28 changed by simonpj

  • severity changed from normal to minor.
  • milestone set to _|_.

Good bug report. Alas, it's not easy to fix. Here's why.

To report unused imports, GHC gathers up all the entities that are referred to (Names, in GHC's terminology). But that doesn't say *how* they are referred to, so it does not distinguish 'ap' from 'Control.Monad.ap'.

In this case, the import of Control.Monad is indeed required, because the reference was a qualified one.

So really this exposes an architectural shortcoming in the way GHC reports unused imports. Of course it's fixable, but it'd be non-trivial, so don't hold your breath.

Simon

06/21/07 22:56:31 changed by ghc@henning-thielemann.de

I assume that this is the same bug, that I encountered, too. It is possibly the same like http://hackage.haskell.org/trac/ghc/ticket/1148 I remember that such wrong warnings occured when switching from GHC 6.2 to GHC 6.4. Here I have setup a self-contained example:

http://users.informatik.uni-halle.de/~thielema/Haskell/ImportWarning/

I insert the example here:

module A where

type T = Int
module B ( module A ) where

import A
module C where

{-
This import is necessary but is reported as unnecessary.
"necessary" means:
If you remove that import statement, then the module cannot be compiled.
Of course, we could use the identifiers from B,
since B just re-exports A.
But formally I consider it a bug to declare the import of A as unnecessary.
-}
import qualified A

{-
This import is unnecessary but no warning is emitted.
If this import is removed, everything compiles fine without warnings.
-}
import B


f :: String -> A.T
f = read

06/25/07 09:05:02 changed by Isaac Dupree

I think this is the same bug, that I ran into:

module Foo where
import qualified Prelude as A (take)
import qualified Prelude as B

Warning: Imported from `Prelude' but not used: `B.take'

The module it refers to is the last applicable one in the import list - even if there would never be a warning about importing a particular identifier when it is not explicitly named in a relevant import list, and even if B.somethingElse is used. Re-ordering the imports "fixes" the warning; also, adding ...as B hiding (take) "fixes" it. Maybe this bit is fixable, assuming ghc remembers the import lines...

Also I noticed that replacing take with Int results in no warning at all - is this intentional? (note that, apparently GHC *never* gives unused-*module*-import warnings for Prelude, if considering this)

P.S. if {{{"necessary" means: If you remove that import statement, then the module cannot be compiled.}}} then GHC is designed (whether or not I agree with that design) to warn about those, when the only uses of that import are by function definitions that are never used or exported.

06/27/07 02:04:29 changed by simonpj

Thanks for further examples. The more examples we have, the more likely we are to get it right when we eventually fix this one! (It's trickier than one would think.)

But as my comment above says, to fix it properly requires some real work, and since it's (by definition) not a show-stopper, it's not top of the priority list. (Unless you all say it is!)

Simon

06/29/07 14:25:37 changed by simonpj

When fixing this, be sure to fix #1384 at the same time.

Simon

11/13/07 03:07:23 changed by simonmar

  • os changed from MacOS X to Multiple.
  • architecture changed from powerpc to Multiple.

05/06/08 03:33:22 changed by igloo

Here's the behaviour I'd like:

For each use of a variable foo, mark imports

import M (..., foo, ...)

as "used" if any exist; otherwise mark any imports

import M

which export foo as "used". Mark all imports of the form

import M ()

as used.

Then warn about any imports not marked as "used".

05/06/08 03:34:04 changed by igloo

See also #2267.

09/30/08 08:45:08 changed by simonmar

  • architecture changed from Multiple to Unknown/Multiple.

09/30/08 08:54:45 changed by simonmar

  • os changed from Multiple to Unknown/Multiple.

10/10/08 08:02:27 changed by igloo

  • priority changed from normal to high.
  • milestone changed from _|_ to 6.10.2.

This is biting more and more people, as warnings are increasingly enabled by users. Also, it's a real pain to workaround. Let's just rewrite the warning code and fix it.

See also #2681, #1148, #2267.

(nb. my algorithm above needs extending to work with qualified imports too)

10/10/08 08:06:11 changed by NeilMitchell

  • cc changed from jyasskin@gmail.com to jyasskin@gmail.com, neil.mitchell.2@credit-suisse.com.