@@ -52,8 +52,8 @@ use rustc::hir::def_id::DefId;
5252use rustc:: hir:: pat_util:: pat_bindings;
5353use rustc:: ty;
5454use rustc:: ty:: subst:: { ParamSpace , FnSpace , TypeSpace } ;
55- use rustc:: hir:: { Freevar , FreevarMap , TraitMap , GlobMap } ;
56- use rustc:: util:: nodemap:: { NodeMap , FnvHashMap , FnvHashSet } ;
55+ use rustc:: hir:: { Freevar , FreevarMap , TraitCandidate , TraitMap , GlobMap } ;
56+ use rustc:: util:: nodemap:: { NodeMap , NodeSet , FnvHashMap , FnvHashSet } ;
5757
5858use syntax:: ast:: { self , FloatTy } ;
5959use syntax:: ast:: { CRATE_NODE_ID , Name , NodeId , CrateNum , IntTy , UintTy } ;
@@ -1042,6 +1042,7 @@ pub struct Resolver<'a, 'tcx: 'a> {
10421042
10431043 used_imports : HashSet < ( NodeId , Namespace ) > ,
10441044 used_crates : HashSet < CrateNum > ,
1045+ maybe_unused_trait_imports : NodeSet ,
10451046
10461047 privacy_errors : Vec < PrivacyError < ' a > > ,
10471048
@@ -1137,13 +1138,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11371138 export_map : NodeMap ( ) ,
11381139 trait_map : NodeMap ( ) ,
11391140 module_map : module_map,
1140- used_imports : HashSet :: new ( ) ,
1141- used_crates : HashSet :: new ( ) ,
11421141
11431142 emit_errors : true ,
11441143 make_glob_map : make_glob_map == MakeGlobMap :: Yes ,
11451144 glob_map : NodeMap ( ) ,
11461145
1146+ used_imports : HashSet :: new ( ) ,
1147+ used_crates : HashSet :: new ( ) ,
1148+ maybe_unused_trait_imports : NodeSet ( ) ,
1149+
11471150 privacy_errors : Vec :: new ( ) ,
11481151
11491152 arenas : arenas,
@@ -1177,7 +1180,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11771180 }
11781181
11791182 #[ inline]
1180- fn record_use ( & mut self , name : Name , ns : Namespace , binding : & ' a NameBinding < ' a > ) {
1183+ fn record_use ( & mut self , name : Name , binding : & ' a NameBinding < ' a > ) {
11811184 // track extern crates for unused_extern_crate lint
11821185 if let Some ( DefId { krate, .. } ) = binding. module ( ) . and_then ( ModuleS :: def_id) {
11831186 self . used_crates . insert ( krate) ;
@@ -1189,7 +1192,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11891192 _ => return ,
11901193 } ;
11911194
1192- self . used_imports . insert ( ( directive. id , ns) ) ;
11931195 if let Some ( error) = privacy_error. as_ref ( ) {
11941196 self . privacy_errors . push ( ( * * error) . clone ( ) ) ;
11951197 }
@@ -1492,7 +1494,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14921494 false => module. resolve_name ( name, namespace, false ) ,
14931495 } . and_then ( |binding| {
14941496 if record_used {
1495- self . record_use ( name, namespace, binding) ;
1497+ if let NameBindingKind :: Import { directive, .. } = binding. kind {
1498+ self . used_imports . insert ( ( directive. id , namespace) ) ;
1499+ }
1500+ self . record_use ( name, binding) ;
14961501 }
14971502 Success ( binding)
14981503 } )
@@ -3094,21 +3099,27 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
30943099 }
30953100 }
30963101
3097- fn get_traits_containing_item ( & mut self , name : Name ) -> Vec < DefId > {
3102+ fn get_traits_containing_item ( & mut self , name : Name ) -> Vec < TraitCandidate > {
30983103 debug ! ( "(getting traits containing item) looking for '{}'" , name) ;
30993104
3100- fn add_trait_info ( found_traits : & mut Vec < DefId > , trait_def_id : DefId , name : Name ) {
3105+ fn add_trait_info ( found_traits : & mut Vec < TraitCandidate > ,
3106+ trait_def_id : DefId ,
3107+ import_id : Option < NodeId > ,
3108+ name : Name ) {
31013109 debug ! ( "(adding trait info) found trait {:?} for method '{}'" ,
31023110 trait_def_id,
31033111 name) ;
3104- found_traits. push ( trait_def_id) ;
3112+ found_traits. push ( TraitCandidate {
3113+ def_id : trait_def_id,
3114+ import_id : import_id,
3115+ } ) ;
31053116 }
31063117
31073118 let mut found_traits = Vec :: new ( ) ;
31083119 // Look for the current trait.
31093120 if let Some ( ( trait_def_id, _) ) = self . current_trait_ref {
31103121 if self . trait_item_map . contains_key ( & ( name, trait_def_id) ) {
3111- add_trait_info ( & mut found_traits, trait_def_id, name) ;
3122+ add_trait_info ( & mut found_traits, trait_def_id, None , name) ;
31123123 }
31133124 }
31143125
@@ -3131,8 +3142,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
31313142 for & ( trait_name, binding) in traits. as_ref ( ) . unwrap ( ) . iter ( ) {
31323143 let trait_def_id = binding. def ( ) . unwrap ( ) . def_id ( ) ;
31333144 if self . trait_item_map . contains_key ( & ( name, trait_def_id) ) {
3134- add_trait_info ( & mut found_traits, trait_def_id, name) ;
3135- self . record_use ( trait_name, TypeNS , binding) ;
3145+ let mut import_id = None ;
3146+ if let NameBindingKind :: Import { directive, .. } = binding. kind {
3147+ let id = directive. id ;
3148+ self . maybe_unused_trait_imports . insert ( id) ;
3149+ import_id = Some ( id) ;
3150+ }
3151+ add_trait_info ( & mut found_traits, trait_def_id, import_id, name) ;
3152+ self . record_use ( trait_name, binding) ;
31363153 }
31373154 }
31383155 } ;
@@ -3506,6 +3523,7 @@ fn err_path_resolution() -> PathResolution {
35063523pub struct CrateMap {
35073524 pub def_map : RefCell < DefMap > ,
35083525 pub freevars : FreevarMap ,
3526+ pub maybe_unused_trait_imports : NodeSet ,
35093527 pub export_map : ExportMap ,
35103528 pub trait_map : TraitMap ,
35113529 pub glob_map : Option < GlobMap > ,
@@ -3543,6 +3561,7 @@ pub fn resolve_crate<'a, 'tcx>(session: &'a Session,
35433561 CrateMap {
35443562 def_map : resolver. def_map ,
35453563 freevars : resolver. freevars ,
3564+ maybe_unused_trait_imports : resolver. maybe_unused_trait_imports ,
35463565 export_map : resolver. export_map ,
35473566 trait_map : resolver. trait_map ,
35483567 glob_map : if resolver. make_glob_map {
0 commit comments