Ticket #4983 (closed bug: wontfix)

Opened 2 years ago

Last modified 2 years ago

Warning about redundant import is wrong when hiding identifiers in order to avoid export ambiguities

Reported by: Lemming Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.0.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Incorrect warning at compile-time Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description

Although warnings about redundant imports are almost perfect since GHC-6.12, I have a corner case, where the warning about a redundant import is wrong. Consider the module

module Data.Monoid.HT (cycle, ) where

import Data.Monoid (Monoid, mappend, )
import Data.Function (fix, )

import Prelude hiding (cycle, )

{- |
Generalization of 'Data.List.cycle' to any monoid.
-}
cycle :: Monoid m => m -> m
cycle x =
   fix (mappend x)

GHC says:

src/Data/Monoid/HT.hs:6:1:
    Warning: The import of `Prelude' is redundant
               except perhaps to import instances from `Prelude'
             To import instances alone, use: import Prelude()

I have to hide 'cycle' from Prelude if I want to export it unqualified.

I have several work-arounds: Export 'cycle' with qualification, use no export list at all, import from Prelude by enumeration of needed functions. I think this problem has low priority. I report it just for the case that someone claims that Haskell's module system is simple and could be more complicated. ;-)

Change History

  Changed 2 years ago by igloo

  • status changed from new to closed
  • resolution set to wontfix

I think the warning is OK in this case: You've explicitly imported the Prelude and then used nothing from it. You need to do

import Prelude ()

if you want to explicitly import it (which you need to do to avoid the name collision), but import nothing from it.

follow-up: ↓ 3   Changed 2 years ago by simonpj

I think Lemming's complaint is that the error message isn't right. We don't want to import instances, but we still need import Prelude (). So the suggestion is right but the reason is wrong.

A good point. But as you say, a corner case.

Simon

in reply to: ↑ 2   Changed 2 years ago by Lemming

Replying to simonpj:

I think Lemming's complaint is that the error message isn't right. We don't want to import instances, but we still need import Prelude (). So the suggestion is right but the reason is wrong.

Maybe this confused me and let me think that GHC is broken. My problem was, that I was not aware, that I did not use something from Prelude. Usually I use variables from Prelude without noting it. Maybe you can just alter the warning to:

src/Data/Monoid/HT.hs:6:1:
    Warning: You imported `Prelude', but used nothing of it,
               except perhaps instances from `Prelude'
             To import instances alone, use: import Prelude()

I remember that I have already seen such warnings from GHC (I mean: "used nothing of it"), but I do not know in what cases GHC emits them.

  Changed 2 years ago by simonpj

Well that would apply to any other redundant import too. I suppose we could replace "The import of M is redundant" by "You imported M but used nothing of it". Maybe that'd be an improvement, I'm not sure.

Note: See TracTickets for help on using tickets.