]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_passes/src/diagnostic_items.rs
Auto merge of #95159 - nnethercote:TtParser, 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_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
65                     .sess
66                     .struct_span_err(span, &format!("duplicate diagnostic item found: `{name}`.")),
67                 None => tcx.sess.struct_err(&format!(
68                     "duplicate diagnostic item in crate `{}`: `{}`.",
69                     tcx.crate_name(item_def_id.krate),
70                     name
71                 )),
72             };
73             if let Some(span) = tcx.hir().span_if_local(original_def_id) {
74                 err.span_note(span, "the diagnostic item is first defined here");
75             } else {
76                 err.note(&format!(
77                     "the diagnostic item is first defined in crate `{}`.",
78                     tcx.crate_name(original_def_id.krate)
79                 ));
80             }
81             err.emit();
82         }
83     }
84 }
85
86 /// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.p
87 fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
88     attrs.iter().find_map(|attr| {
89         if attr.has_name(sym::rustc_diagnostic_item) { attr.value_str() } else { None }
90     })
91 }
92
93 /// Traverse and collect the diagnostic items in the current
94 fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> DiagnosticItems {
95     assert_eq!(cnum, LOCAL_CRATE);
96
97     // Initialize the collector.
98     let mut collector = DiagnosticItemCollector::new(tcx);
99
100     // Collect diagnostic items in this crate.
101     tcx.hir().visit_all_item_likes(&mut collector);
102
103     collector.diagnostic_items
104 }
105
106 /// Traverse and collect all the diagnostic items in all crates.
107 fn all_diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> DiagnosticItems {
108     // Initialize the collector.
109     let mut items = DiagnosticItems::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).name_to_id {
114             collect_item(tcx, &mut items, name, def_id);
115         }
116     }
117
118     items
119 }
120
121 pub fn provide(providers: &mut Providers) {
122     providers.diagnostic_items = diagnostic_items;
123     providers.all_diagnostic_items = all_diagnostic_items;
124 }