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