HsSyn clean up for indexed types
[ghc-hetmet.git] / compiler / rename / RnNames.lhs
index 3671676..ccdf18b 100644 (file)
@@ -17,7 +17,7 @@ import DynFlags               ( DynFlag(..), GhcMode(..), DynFlags(..) )
 import HsSyn           ( IE(..), ieName, ImportDecl(..), LImportDecl,
                          ForeignDecl(..), HsGroup(..), HsValBinds(..),
                          Sig(..), collectHsBindLocatedBinders, tyClDeclNames,
-                         instDeclATs, isIdxTyDecl,
+                         instDeclATs, isFamInstDecl,
                          LIE )
 import RnEnv
 import RnHsDoc          ( rnHsDoc )
@@ -336,7 +336,7 @@ getLocalDeclBinders gbl_env (HsGroup {hs_valds = ValBindsIn val_decls val_sigs,
     for_hs_bndrs = [nm | L _ (ForeignImport nm _ _) <- foreign_decls]
 
     new_tc tc_decl 
-      | isIdxTyDecl (unLoc tc_decl)
+      | isFamInstDecl (unLoc tc_decl)
        = do { main_name <- lookupFamInstDeclBndr mod main_rdr
             ; sub_names <- mappM (newTopSrcBinder mod) sub_rdrs
             ; return (AvailTC main_name sub_names) }
@@ -918,7 +918,7 @@ reportDeprecations dflags tcg_env
 
     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)) <+> 
@@ -940,9 +940,10 @@ reportDeprecations dflags tcg_env
            -- 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       
@@ -1012,14 +1013,15 @@ reportUnusedNames export_decls gbl_env
     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
@@ -1095,6 +1097,7 @@ reportUnusedNames export_decls gbl_env
     --
     -- 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),
@@ -1107,6 +1110,12 @@ reportUnusedNames export_decls gbl_env
     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 
@@ -1125,8 +1134,6 @@ warnDuplicateImports :: [GlobalRdrElt] -> RnM ()
 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
@@ -1145,7 +1152,12 @@ warnDuplicateImports gres
     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
@@ -1166,6 +1178,10 @@ warnDuplicateImports gres
        = 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
@@ -1173,16 +1189,11 @@ warnDuplicateImports gres
        = 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