10
10
//! * Compiler internal types like `Ty` and `TyCtxt`
11
11
12
12
use rustc_ast as ast;
13
- use rustc_data_structures:: fx:: FxHashMap ;
14
13
use rustc_hir as hir;
14
+ use rustc_hir:: diagnostic_items:: DiagnosticItems ;
15
15
use rustc_hir:: itemlikevisit:: ItemLikeVisitor ;
16
16
use rustc_middle:: ty:: query:: Providers ;
17
17
use rustc_middle:: ty:: TyCtxt ;
18
18
use rustc_span:: def_id:: { CrateNum , DefId , LocalDefId , LOCAL_CRATE } ;
19
19
use rustc_span:: symbol:: { sym, Symbol } ;
20
20
21
21
struct DiagnosticItemCollector < ' tcx > {
22
- // items from this crate
23
- items : FxHashMap < Symbol , DefId > ,
24
22
tcx : TyCtxt < ' tcx > ,
23
+ diagnostic_items : DiagnosticItems ,
25
24
}
26
25
27
26
impl < ' v , ' tcx > ItemLikeVisitor < ' v > for DiagnosticItemCollector < ' tcx > {
@@ -44,27 +43,22 @@ impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> {
44
43
45
44
impl < ' tcx > DiagnosticItemCollector < ' tcx > {
46
45
fn new ( tcx : TyCtxt < ' tcx > ) -> DiagnosticItemCollector < ' tcx > {
47
- DiagnosticItemCollector { tcx, items : Default :: default ( ) }
46
+ DiagnosticItemCollector { tcx, diagnostic_items : DiagnosticItems :: default ( ) }
48
47
}
49
48
50
49
fn observe_item ( & mut self , def_id : LocalDefId ) {
51
50
let hir_id = self . tcx . hir ( ) . local_def_id_to_hir_id ( def_id) ;
52
51
let attrs = self . tcx . hir ( ) . attrs ( hir_id) ;
53
52
if let Some ( name) = extract ( attrs) {
54
53
// 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 ( ) ) ;
56
55
}
57
56
}
58
57
}
59
58
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) {
68
62
if original_def_id != item_def_id {
69
63
let mut err = match tcx. hir ( ) . span_if_local ( item_def_id) {
70
64
Some ( span) => tcx. sess . struct_span_err (
@@ -98,7 +92,7 @@ fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
98
92
}
99
93
100
94
/// 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 {
102
96
assert_eq ! ( cnum, LOCAL_CRATE ) ;
103
97
104
98
// Initialize the collector.
@@ -107,22 +101,22 @@ fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> FxHashMap<Symbol
107
101
// Collect diagnostic items in this crate.
108
102
tcx. hir ( ) . visit_all_item_likes ( & mut collector) ;
109
103
110
- collector. items
104
+ collector. diagnostic_items
111
105
}
112
106
113
107
/// 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 {
115
109
// Initialize the collector.
116
- let mut collector = FxHashMap :: default ( ) ;
110
+ let mut items = DiagnosticItems :: default ( ) ;
117
111
118
112
// Collect diagnostic items in other crates.
119
113
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) ;
122
116
}
123
117
}
124
118
125
- collector
119
+ items
126
120
}
127
121
128
122
pub fn provide ( providers : & mut Providers ) {
0 commit comments