]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/diagnostic_items.rs
Rollup merge of #68033 - ollie27:win_f32, r=dtolnay
[rust.git] / src / librustc_passes / diagnostic_items.rs
1 //! Detecting diagnostic items.
2 //!
3 //! Diagnostic items are items that are not language-inherent, but can reasonably be expected to
4 //! exist for diagnostic purposes. This allows diagnostic authors to refer to specific items
5 //! directly, without having to guess module paths and crates.
6 //! Examples are:
7 //!
8 //! * Traits like `Debug`, that have no bearing on language semantics
9 //!
10 //! * Compiler internal types like `Ty` and `TyCtxt`
11
12 use rustc::ty::query::Providers;
13 use rustc::ty::TyCtxt;
14 use rustc_data_structures::fx::FxHashMap;
15 use rustc_hir as hir;
16 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
17 use rustc_hir::itemlikevisit::ItemLikeVisitor;
18 use rustc_span::symbol::{sym, Symbol};
19 use syntax::ast;
20
21 struct DiagnosticItemCollector<'tcx> {
22     // items from this crate
23     items: FxHashMap<Symbol, DefId>,
24     tcx: TyCtxt<'tcx>,
25 }
26
27 impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> {
28     fn visit_item(&mut self, item: &hir::Item<'_>) {
29         self.observe_item(&item.attrs, item.hir_id);
30     }
31
32     fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
33         self.observe_item(&trait_item.attrs, trait_item.hir_id);
34     }
35
36     fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
37         self.observe_item(&impl_item.attrs, impl_item.hir_id);
38     }
39 }
40
41 impl<'tcx> DiagnosticItemCollector<'tcx> {
42     fn new(tcx: TyCtxt<'tcx>) -> DiagnosticItemCollector<'tcx> {
43         DiagnosticItemCollector { tcx, items: Default::default() }
44     }
45
46     fn observe_item(&mut self, attrs: &[ast::Attribute], hir_id: hir::HirId) {
47         if let Some(name) = extract(attrs) {
48             let def_id = self.tcx.hir().local_def_id(hir_id);
49             // insert into our table
50             collect_item(self.tcx, &mut self.items, name, def_id);
51         }
52     }
53 }
54
55 fn collect_item(
56     tcx: TyCtxt<'_>,
57     items: &mut FxHashMap<Symbol, DefId>,
58     name: Symbol,
59     item_def_id: DefId,
60 ) {
61     // Check for duplicates.
62     if let Some(original_def_id) = items.insert(name, item_def_id) {
63         if original_def_id != item_def_id {
64             let mut err = match tcx.hir().span_if_local(item_def_id) {
65                 Some(span) => tcx.sess.struct_span_err(
66                     span,
67                     &format!("duplicate diagnostic item found: `{}`.", name),
68                 ),
69                 None => tcx.sess.struct_err(&format!(
70                     "duplicate diagnostic item in crate `{}`: `{}`.",
71                     tcx.crate_name(item_def_id.krate),
72                     name
73                 )),
74             };
75             if let Some(span) = tcx.hir().span_if_local(original_def_id) {
76                 err.span_note(span, "first defined here");
77             } else {
78                 err.note(&format!(
79                     "first defined in crate `{}`.",
80                     tcx.crate_name(original_def_id.krate)
81                 ));
82             }
83             err.emit();
84         }
85     }
86 }
87
88 /// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.
89 fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
90     attrs.iter().find_map(|attr| {
91         if attr.check_name(sym::rustc_diagnostic_item) { attr.value_str() } else { None }
92     })
93 }
94
95 /// Traverse and collect the diagnostic items in the current
96 fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> &'tcx FxHashMap<Symbol, DefId> {
97     // Initialize the collector.
98     let mut collector = DiagnosticItemCollector::new(tcx);
99
100     // Collect diagnostic items in this crate.
101     tcx.hir().krate().visit_all_item_likes(&mut collector);
102
103     tcx.arena.alloc(collector.items)
104 }
105
106 /// Traverse and collect all the diagnostic items in all crates.
107 fn collect_all<'tcx>(tcx: TyCtxt<'tcx>) -> &'tcx FxHashMap<Symbol, DefId> {
108     // Initialize the collector.
109     let mut collector = FxHashMap::default();
110
111     // Collect diagnostic items in other crates.
112     for &cnum in tcx.crates().iter().chain(std::iter::once(&LOCAL_CRATE)) {
113         for (&name, &def_id) in tcx.diagnostic_items(cnum).iter() {
114             collect_item(tcx, &mut collector, name, def_id);
115         }
116     }
117
118     tcx.arena.alloc(collector)
119 }
120
121 pub fn provide(providers: &mut Providers<'_>) {
122     providers.diagnostic_items = |tcx, id| {
123         assert_eq!(id, LOCAL_CRATE);
124         collect(tcx)
125     };
126     providers.all_diagnostic_items = |tcx, id| {
127         assert_eq!(id, LOCAL_CRATE);
128         collect_all(tcx)
129     };
130 }