]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_passes/src/diagnostic_items.rs
Auto merge of #89323 - estebank:derive-binop, r=petrochenkov
[rust.git] / compiler / rustc_passes / src / 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_ast as ast;
13 use rustc_data_structures::fx::FxHashMap;
14 use rustc_hir as hir;
15 use rustc_hir::itemlikevisit::ItemLikeVisitor;
16 use rustc_middle::ty::query::Providers;
17 use rustc_middle::ty::TyCtxt;
18 use rustc_span::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
19 use rustc_span::symbol::{sym, Symbol};
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.def_id);
30     }
31
32     fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
33         self.observe_item(trait_item.def_id);
34     }
35
36     fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
37         self.observe_item(impl_item.def_id);
38     }
39
40     fn visit_foreign_item(&mut self, foreign_item: &hir::ForeignItem<'_>) {
41         self.observe_item(foreign_item.def_id);
42     }
43 }
44
45 impl<'tcx> DiagnosticItemCollector<'tcx> {
46     fn new(tcx: TyCtxt<'tcx>) -> DiagnosticItemCollector<'tcx> {
47         DiagnosticItemCollector { tcx, items: Default::default() }
48     }
49
50     fn observe_item(&mut self, def_id: LocalDefId) {
51         let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
52         let attrs = self.tcx.hir().attrs(hir_id);
53         if let Some(name) = extract(attrs) {
54             // insert into our table
55             collect_item(self.tcx, &mut self.items, name, def_id.to_def_id());
56         }
57     }
58 }
59
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) {
68         if original_def_id != item_def_id {
69             let mut err = match tcx.hir().span_if_local(item_def_id) {
70                 Some(span) => tcx.sess.struct_span_err(
71                     span,
72                     &format!("duplicate diagnostic item found: `{}`.", name),
73                 ),
74                 None => tcx.sess.struct_err(&format!(
75                     "duplicate diagnostic item in crate `{}`: `{}`.",
76                     tcx.crate_name(item_def_id.krate),
77                     name
78                 )),
79             };
80             if let Some(span) = tcx.hir().span_if_local(original_def_id) {
81                 err.span_note(span, "the diagnostic item is first defined here");
82             } else {
83                 err.note(&format!(
84                     "the diagnostic item is first defined in crate `{}`.",
85                     tcx.crate_name(original_def_id.krate)
86                 ));
87             }
88             err.emit();
89         }
90     }
91 }
92
93 /// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.p
94 fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
95     attrs.iter().find_map(|attr| {
96         if attr.has_name(sym::rustc_diagnostic_item) { attr.value_str() } else { None }
97     })
98 }
99
100 /// Traverse and collect the diagnostic items in the current
101 fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> FxHashMap<Symbol, DefId> {
102     assert_eq!(cnum, LOCAL_CRATE);
103
104     // Initialize the collector.
105     let mut collector = DiagnosticItemCollector::new(tcx);
106
107     // Collect diagnostic items in this crate.
108     tcx.hir().visit_all_item_likes(&mut collector);
109
110     collector.items
111 }
112
113 /// Traverse and collect all the diagnostic items in all crates.
114 fn all_diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> FxHashMap<Symbol, DefId> {
115     // Initialize the collector.
116     let mut collector = FxHashMap::default();
117
118     // Collect diagnostic items in other crates.
119     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);
122         }
123     }
124
125     collector
126 }
127
128 pub fn provide(providers: &mut Providers) {
129     providers.diagnostic_items = diagnostic_items;
130     providers.all_diagnostic_items = all_diagnostic_items;
131 }