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