- warn name (red_imp, cov_imp)
- = addWarnAt (importSpecLoc red_imp)
- (vcat [ptext (sLit "Redundant import of:") <+> quotes pp_name,
- ptext (sLit "It is also") <+> ppr cov_imp])
- where
- pp_name | is_qual red_decl = ppr (is_as red_decl) <> dot <> ppr occ
- | otherwise = ppr occ
- occ = nameOccName name
- red_decl = is_decl red_imp
-
- redundants :: [ImportSpec] -> [(ImportSpec,ImportSpec)]
- -- The returned pair is (redundant-import, covering-import)
- redundants imps
- = [ (red_imp, cov_imp)
- | red_imp <- imps
- , isExplicitItem (is_item red_imp)
- -- Complain only about redundant imports
- -- mentioned explicitly by the user
- , cov_imp <- take 1 (filter (covers red_imp) imps) ]
- -- The 'take 1' picks the first offending group
- -- for this particular name
-
- -- "red_imp" is a putative redundant import
- -- "cov_imp" potentially covers it
- -- This test decides whether red_imp could be dropped
- --
- -- NOTE: currently the test does not warn about
- -- import M( x )
- -- import N( x )
- -- even if the same underlying 'x' is involved, because dropping
- -- either import would change the qualified names in scope (M.x, N.x)
- -- But if the qualified names aren't used, the import is indeed redundant
- -- Sadly we don't know that. Oh well.
- covers red_imp@(ImpSpec { is_decl = red_decl })
- cov_imp@(ImpSpec { is_decl = cov_decl, is_item = cov_item })
- | red_loc == cov_loc
- = False -- Ignore diagonal elements
- | not (is_as red_decl == is_as cov_decl)
- = False -- They bring into scope different qualified names
- | not (is_qual red_decl) && is_qual cov_decl
- = False -- Covering one doesn't bring unqualified name into scope
- | otherwise
- = not (isExplicitItem cov_item) -- Redundant one is selective and covering one isn't
- || red_later -- or both are explicit; tie-break using red_later
-{-
- | red_selective
- = not cov_selective -- Redundant one is selective and covering one isn't
- || red_later -- Both are explicit; tie-break using red_later
- | otherwise
- = not cov_selective -- Neither import is selective
- && (is_mod red_decl == is_mod cov_decl) -- They import the same module
- && red_later -- Tie-break
--}
- where
- red_loc = importSpecLoc red_imp
- cov_loc = importSpecLoc cov_imp
- red_later = red_loc > cov_loc
-
--- ToDo: deal with original imports with 'qualified' and 'as M' clauses
-printMinimalImports :: FiniteMap ModuleName AvailEnv -- Minimal imports
- -> RnM ()
-printMinimalImports imps
- = ifOptM Opt_D_dump_minimal_imports $ do {
-
- mod_ies <- initIfaceTcRn $ mapM to_ies (fmToList imps) ;
- this_mod <- getModule ;
- rdr_env <- getGlobalRdrEnv ;
- dflags <- getDOpts ;
- liftIO $ do h <- openFile (mkFilename this_mod) WriteMode
- printForUser h (mkPrintUnqualified dflags rdr_env)
- (vcat (map ppr_mod_ie mod_ies))
- }
+ msg1 = vcat [pp_herald <+> quotes pp_mod <+> pp_not_used,
+ nest 2 (ptext (sLit "except perhaps to import instances from")
+ <+> quotes pp_mod),
+ ptext (sLit "To import instances alone, use:")
+ <+> ptext (sLit "import") <+> pp_mod <> parens empty ]
+ msg2 = sep [pp_herald <+> quotes (pprWithCommas ppr unused),
+ text "from module" <+> quotes pp_mod <+> pp_not_used]
+ pp_herald = text "The import of"
+ pp_mod = ppr (unLoc (ideclName decl))
+ pp_not_used = text "is redundant"
+\end{code}
+
+To print the minimal imports we walk over the user-supplied import
+decls, and simply trim their import lists. NB that
+
+ * We do *not* change the 'qualified' or 'as' parts!
+
+ * We do not disard a decl altogether; we might need instances
+ from it. Instead we just trim to an empty import list
+
+\begin{code}
+printMinimalImports :: [ImportDeclUsage] -> RnM ()
+printMinimalImports imports_w_usage
+ = do { imports' <- mapM mk_minimal imports_w_usage
+ ; this_mod <- getModule
+ ; liftIO $
+ do { h <- openFile (mkFilename this_mod) WriteMode
+ ; printForUser h neverQualify (vcat (map ppr imports')) }
+ -- The neverQualify is important. We are printing Names
+ -- but they are in the context of an 'import' decl, and
+ -- we never qualify things inside there
+ -- E.g. import Blag( f, b )
+ -- not import Blag( Blag.f, Blag.g )!
+ }