1010//! * Compiler internal types like `Ty` and `TyCtxt`
1111
1212use rustc_ast as ast;
13- use rustc_data_structures:: fx:: FxHashMap ;
1413use rustc_hir as hir;
14+ use rustc_hir:: diagnostic_items:: DiagnosticItems ;
1515use rustc_hir:: itemlikevisit:: ItemLikeVisitor ;
1616use rustc_middle:: ty:: query:: Providers ;
1717use rustc_middle:: ty:: TyCtxt ;
1818use rustc_span:: def_id:: { CrateNum , DefId , LocalDefId , LOCAL_CRATE } ;
1919use rustc_span:: symbol:: { sym, Symbol } ;
2020
2121struct DiagnosticItemCollector < ' tcx > {
22- // items from this crate
23- items : FxHashMap < Symbol , DefId > ,
2422 tcx : TyCtxt < ' tcx > ,
23+ diagnostic_items : DiagnosticItems ,
2524}
2625
2726impl < ' v , ' tcx > ItemLikeVisitor < ' v > for DiagnosticItemCollector < ' tcx > {
@@ -44,27 +43,22 @@ impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> {
4443
4544impl < ' tcx > DiagnosticItemCollector < ' tcx > {
4645 fn new ( tcx : TyCtxt < ' tcx > ) -> DiagnosticItemCollector < ' tcx > {
47- DiagnosticItemCollector { tcx, items : Default :: default ( ) }
46+ DiagnosticItemCollector { tcx, diagnostic_items : DiagnosticItems :: default ( ) }
4847 }
4948
5049 fn observe_item ( & mut self , def_id : LocalDefId ) {
5150 let hir_id = self . tcx . hir ( ) . local_def_id_to_hir_id ( def_id) ;
5251 let attrs = self . tcx . hir ( ) . attrs ( hir_id) ;
5352 if let Some ( name) = extract ( attrs) {
5453 // insert into our table
55- collect_item ( self . tcx , & mut self . items , name, def_id. to_def_id ( ) ) ;
54+ collect_item ( self . tcx , & mut self . diagnostic_items , name, def_id. to_def_id ( ) ) ;
5655 }
5756 }
5857}
5958
60- fn collect_item (
61- tcx : TyCtxt < ' _ > ,
62- items : & mut FxHashMap < Symbol , DefId > ,
63- name : Symbol ,
64- item_def_id : DefId ,
65- ) {
66- // Check for duplicates.
67- if let Some ( original_def_id) = items. insert ( name, item_def_id) {
59+ fn collect_item ( tcx : TyCtxt < ' _ > , items : & mut DiagnosticItems , name : Symbol , item_def_id : DefId ) {
60+ items. id_to_name . insert ( item_def_id, name) ;
61+ if let Some ( original_def_id) = items. name_to_id . insert ( name, item_def_id) {
6862 if original_def_id != item_def_id {
6963 let mut err = match tcx. hir ( ) . span_if_local ( item_def_id) {
7064 Some ( span) => tcx. sess . struct_span_err (
@@ -98,7 +92,7 @@ fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
9892}
9993
10094/// Traverse and collect the diagnostic items in the current
101- fn diagnostic_items < ' tcx > ( tcx : TyCtxt < ' tcx > , cnum : CrateNum ) -> FxHashMap < Symbol , DefId > {
95+ fn diagnostic_items < ' tcx > ( tcx : TyCtxt < ' tcx > , cnum : CrateNum ) -> DiagnosticItems {
10296 assert_eq ! ( cnum, LOCAL_CRATE ) ;
10397
10498 // Initialize the collector.
@@ -107,22 +101,22 @@ fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> FxHashMap<Symbol
107101 // Collect diagnostic items in this crate.
108102 tcx. hir ( ) . visit_all_item_likes ( & mut collector) ;
109103
110- collector. items
104+ collector. diagnostic_items
111105}
112106
113107/// Traverse and collect all the diagnostic items in all crates.
114- fn all_diagnostic_items < ' tcx > ( tcx : TyCtxt < ' tcx > , ( ) : ( ) ) -> FxHashMap < Symbol , DefId > {
108+ fn all_diagnostic_items < ' tcx > ( tcx : TyCtxt < ' tcx > , ( ) : ( ) ) -> DiagnosticItems {
115109 // Initialize the collector.
116- let mut collector = FxHashMap :: default ( ) ;
110+ let mut items = DiagnosticItems :: default ( ) ;
117111
118112 // Collect diagnostic items in other crates.
119113 for & cnum in tcx. crates ( ( ) ) . iter ( ) . chain ( std:: iter:: once ( & LOCAL_CRATE ) ) {
120- for ( & name, & def_id) in tcx. diagnostic_items ( cnum) . iter ( ) {
121- collect_item ( tcx, & mut collector , name, def_id) ;
114+ for ( & name, & def_id) in & tcx. diagnostic_items ( cnum) . name_to_id {
115+ collect_item ( tcx, & mut items , name, def_id) ;
122116 }
123117 }
124118
125- collector
119+ items
126120}
127121
128122pub fn provide ( providers : & mut Providers ) {
0 commit comments