import Name
import NameEnv
import NameSet
-import OccName ( srcDataName, pprNonVarNameSpace,
- occNameSpace,
- OccEnv, mkOccEnv, mkOccEnv_C, lookupOccEnv,
- emptyOccEnv, extendOccEnv )
-import HscTypes ( GenAvailInfo(..), AvailInfo, availNames, availName,
- HomePackageTable, PackageIfaceTable,
- mkPrintUnqualified, availsToNameSet,
- Deprecs(..), ModIface(..), Dependencies(..),
- lookupIfaceByModule, ExternalPackageState(..)
- )
-import RdrName ( RdrName, rdrNameOcc, setRdrNameSpace, Parent(..),
- GlobalRdrEnv, mkGlobalRdrEnv, GlobalRdrElt(..),
- emptyGlobalRdrEnv, plusGlobalRdrEnv, globalRdrEnvElts,
- extendGlobalRdrEnv, lookupGlobalRdrEnv,
- lookupGRE_RdrName, lookupGRE_Name,
- Provenance(..), ImportSpec(..), ImpDeclSpec(..), ImpItemSpec(..),
- importSpecLoc, importSpecModule, isLocalGRE, pprNameProvenance,
- unQualSpecOK, qualSpecOK )
+import OccName
+import HscTypes
+import RdrName
import Outputable
import Maybes
-import SrcLoc ( Located(..), mkGeneralSrcSpan, getLoc,
- unLoc, noLoc, srcLocSpan, SrcSpan )
+import SrcLoc
import FiniteMap
import ErrUtils
import BasicTypes ( DeprecTxt )
return (IEVar (gre_name gre), greAvail gre)
lookup_ie (IEThingAbs rdr)
- = do name <- lookupGlobalOccRn rdr
- case lookupGRE_RdrName rdr rdr_env of
- [] -> panic "RnNames.lookup_ie"
- elt:_ -> case gre_par elt of
- NoParent -> return (IEThingAbs name,
- AvailTC name [name])
- ParentIs p -> return (IEThingAbs name,
- AvailTC p [name])
+ = do gre <- lookupGreRn rdr
+ let name = gre_name gre
+ case gre_par gre of
+ NoParent -> return (IEThingAbs name,
+ AvailTC name [name])
+ ParentIs p -> return (IEThingAbs name,
+ AvailTC p [name])
lookup_ie ie@(IEThingAll rdr)
= do name <- lookupGlobalOccRn rdr
check hpt pit gre@(GRE {gre_name = name, gre_prov = Imported (imp_spec:_)})
| name `elemNameSet` used_names
- , Just deprec_txt <- lookupDeprec dflags hpt pit gre
+ , Just deprec_txt <- lookupImpDeprec dflags hpt pit gre
= addWarnAt (importSpecLoc imp_spec)
(sep [ptext SLIT("Deprecated use of") <+>
pprNonVarNameSpace (occNameSpace (nameOccName name)) <+>
-- the defn of a non-deprecated thing, when changing a module's
-- interface
-lookupDeprec :: DynFlags -> HomePackageTable -> PackageIfaceTable
- -> GlobalRdrElt -> Maybe DeprecTxt
-lookupDeprec dflags hpt pit gre
+lookupImpDeprec :: DynFlags -> HomePackageTable -> PackageIfaceTable
+ -> GlobalRdrElt -> Maybe DeprecTxt
+-- The name is definitely imported, so look in HPT, PIT
+lookupImpDeprec dflags hpt pit gre
= case lookupIfaceByModule dflags hpt pit (nameModule name) of
Just iface -> mi_dep_fn iface name `seqMaybe` -- Bleat if the thing, *or
case gre_par gre of
is_unused_local :: GlobalRdrElt -> Bool
is_unused_local gre = isLocalGRE gre && isExternalName (gre_name gre)
- unused_imports :: [GlobalRdrElt]
- unused_imports = filter unused_imp defined_but_not_used
- unused_imp (GRE {gre_prov = Imported imp_specs})
- = not (all (module_unused . importSpecModule) imp_specs)
- && or [exp | ImpSpec { is_item = ImpSome { is_explicit = exp } } <- imp_specs]
- -- Don't complain about unused imports if we've already said the
- -- entire import is unused
- unused_imp other = False
+ unused_imports :: [GlobalRdrElt]
+ unused_imports = mapCatMaybes unused_imp defined_but_not_used
+ unused_imp :: GlobalRdrElt -> Maybe GlobalRdrElt -- Result has trimmed Imported provenances
+ unused_imp gre@(GRE {gre_prov = LocalDef}) = Nothing
+ unused_imp gre@(GRE {gre_prov = Imported imp_specs})
+ | null trimmed_specs = Nothing
+ | otherwise = Just (gre {gre_prov = Imported trimmed_specs})
+ where
+ trimmed_specs = filter report_if_unused imp_specs
-- To figure out the minimal set of imports, start with the things
-- that are in scope (i.e. in gbl_env). Then just combine them
--
-- BUG WARNING: does not deal correctly with multiple imports of the same module
-- becuase direct_import_mods has only one entry per module
+ unused_imp_mods :: [(ModuleName, SrcSpan)]
unused_imp_mods = [(mod_name,loc) | (mod,no_imp,loc) <- direct_import_mods,
let mod_name = moduleName mod,
not (mod_name `elemFM` minimal_imports1),
module_unused :: ModuleName -> Bool
module_unused mod = any (((==) mod) . fst) unused_imp_mods
+ report_if_unused :: ImportSpec -> Bool
+ -- Do we want to report this as an unused import?
+ report_if_unused (ImpSpec {is_decl = d, is_item = i})
+ = not (module_unused (is_mod d)) -- Not if we've already said entire import is unused
+ && isExplicitItem i -- Only if the import was explicit
+
---------------------
warnDuplicateImports :: [GlobalRdrElt] -> RnM ()
-- Given the GREs for names that are used, figure out which imports
warnDuplicateImports gres
= ifOptM Opt_WarnUnusedImports $
sequenceM_ [ warn name pr
- -- The 'head' picks the first offending group
- -- for this particular name
| GRE { gre_name = name, gre_prov = Imported imps } <- gres
, pr <- redundants imps ]
where
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
= 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
= 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
- cov_selective = selectiveImpItem cov_item
- red_selective = selectiveImpItem red_item
-
-selectiveImpItem :: ImpItemSpec -> Bool
-selectiveImpItem ImpAll = False
-selectiveImpItem (ImpSome {}) = True
-- ToDo: deal with original imports with 'qualified' and 'as M' clauses
printMinimalImports :: FiniteMap ModuleName AvailEnv -- Minimal imports