]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_passes/src/diagnostic_items.rs
Auto merge of #82043 - tmiasko:may-have-side-effect, r=kennytm
[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_session::Session;
19 use rustc_span::def_id::{DefId, LocalDefId, LOCAL_CRATE};
20 use rustc_span::symbol::{sym, Symbol};
21
22 struct DiagnosticItemCollector<'tcx> {
23     // items from this crate
24     items: FxHashMap<Symbol, DefId>,
25     tcx: TyCtxt<'tcx>,
26 }
27
28 impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> {
29     fn visit_item(&mut self, item: &hir::Item<'_>) {
30         self.observe_item(&item.attrs, item.def_id);
31     }
32
33     fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
34         self.observe_item(&trait_item.attrs, trait_item.def_id);
35     }
36
37     fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
38         self.observe_item(&impl_item.attrs, impl_item.def_id);
39     }
40
41     fn visit_foreign_item(&mut self, foreign_item: &hir::ForeignItem<'_>) {
42         self.observe_item(foreign_item.attrs, foreign_item.def_id);
43     }
44 }
45
46 impl<'tcx> DiagnosticItemCollector<'tcx> {
47     fn new(tcx: TyCtxt<'tcx>) -> DiagnosticItemCollector<'tcx> {
48         DiagnosticItemCollector { tcx, items: Default::default() }
49     }
50
51     fn observe_item(&mut self, attrs: &[ast::Attribute], def_id: LocalDefId) {
52         if let Some(name) = extract(&self.tcx.sess, attrs) {
53             // insert into our table
54             collect_item(self.tcx, &mut self.items, name, def_id.to_def_id());
55         }
56     }
57 }
58
59 fn collect_item(
60     tcx: TyCtxt<'_>,
61     items: &mut FxHashMap<Symbol, DefId>,
62     name: Symbol,
63     item_def_id: DefId,
64 ) {
65     // Check for duplicates.
66     if let Some(original_def_id) = items.insert(name, item_def_id) {
67         if original_def_id != item_def_id {
68             let mut err = match tcx.hir().span_if_local(item_def_id) {
69                 Some(span) => tcx.sess.struct_span_err(
70                     span,
71                     &format!("duplicate diagnostic item found: `{}`.", name),
72                 ),
73                 None => tcx.sess.struct_err(&format!(
74                     "duplicate diagnostic item in crate `{}`: `{}`.",
75                     tcx.crate_name(item_def_id.krate),
76                     name
77                 )),
78             };
79             if let Some(span) = tcx.hir().span_if_local(original_def_id) {
80                 err.span_note(span, "the diagnostic item is first defined here");
81             } else {
82                 err.note(&format!(
83                     "the diagnostic item is first defined in crate `{}`.",
84                     tcx.crate_name(original_def_id.krate)
85                 ));
86             }
87             err.emit();
88         }
89     }
90 }
91
92 /// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.
93 fn extract(sess: &Session, attrs: &[ast::Attribute]) -> Option<Symbol> {
94     attrs.iter().find_map(|attr| {
95         if sess.check_name(attr, sym::rustc_diagnostic_item) { attr.value_str() } else { None }
96     })
97 }
98
99 /// Traverse and collect the diagnostic items in the current
100 fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> FxHashMap<Symbol, DefId> {
101     // Initialize the collector.
102     let mut collector = DiagnosticItemCollector::new(tcx);
103
104     // Collect diagnostic items in this crate.
105     tcx.hir().krate().visit_all_item_likes(&mut collector);
106
107     for m in tcx.hir().krate().exported_macros {
108         collector.observe_item(m.attrs, m.def_id);
109     }
110
111     collector.items
112 }
113
114 /// Traverse and collect all the diagnostic items in all crates.
115 fn collect_all<'tcx>(tcx: TyCtxt<'tcx>) -> FxHashMap<Symbol, DefId> {
116     // Initialize the collector.
117     let mut collector = FxHashMap::default();
118
119     // Collect diagnostic items in other crates.
120     for &cnum in tcx.crates().iter().chain(std::iter::once(&LOCAL_CRATE)) {
121         for (&name, &def_id) in tcx.diagnostic_items(cnum).iter() {
122             collect_item(tcx, &mut collector, name, def_id);
123         }
124     }
125
126     collector
127 }
128
129 pub fn provide(providers: &mut Providers) {
130     providers.diagnostic_items = |tcx, id| {
131         assert_eq!(id, LOCAL_CRATE);
132         collect(tcx)
133     };
134     providers.all_diagnostic_items = |tcx, id| {
135         assert_eq!(id, LOCAL_CRATE);
136         collect_all(tcx)
137     };
138 }