]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_passes/src/diagnostic_items.rs
Rollup merge of #105708 - tomerze:enable-atomic-cas-bpf, r=nagisa
[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::diagnostic_items::DiagnosticItems;
14 use rustc_middle::ty::query::Providers;
15 use rustc_middle::ty::TyCtxt;
16 use rustc_span::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
17 use rustc_span::symbol::{kw::Empty, sym, Symbol};
18
19 use crate::errors::{DuplicateDiagnosticItem, DuplicateDiagnosticItemInCrate};
20
21 fn observe_item(tcx: TyCtxt<'_>, diagnostic_items: &mut DiagnosticItems, def_id: LocalDefId) {
22     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
23     let attrs = tcx.hir().attrs(hir_id);
24     if let Some(name) = extract(attrs) {
25         // insert into our table
26         collect_item(tcx, diagnostic_items, name, def_id.to_def_id());
27     }
28 }
29
30 fn collect_item(tcx: TyCtxt<'_>, items: &mut DiagnosticItems, name: Symbol, item_def_id: DefId) {
31     items.id_to_name.insert(item_def_id, name);
32     if let Some(original_def_id) = items.name_to_id.insert(name, item_def_id) {
33         if original_def_id != item_def_id {
34             let orig_span = tcx.hir().span_if_local(original_def_id);
35             let orig_crate_name = if orig_span.is_some() {
36                 None
37             } else {
38                 Some(tcx.crate_name(original_def_id.krate))
39             };
40             match tcx.hir().span_if_local(item_def_id) {
41                 Some(span) => tcx.sess.emit_err(DuplicateDiagnosticItem { span, name }),
42                 None => tcx.sess.emit_err(DuplicateDiagnosticItemInCrate {
43                     span: orig_span,
44                     orig_crate_name: orig_crate_name.unwrap_or(Empty),
45                     have_orig_crate_name: orig_crate_name.map(|_| ()),
46                     crate_name: tcx.crate_name(item_def_id.krate),
47                     name,
48                 }),
49             };
50         }
51     }
52 }
53
54 /// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.
55 fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
56     attrs.iter().find_map(|attr| {
57         if attr.has_name(sym::rustc_diagnostic_item) { attr.value_str() } else { None }
58     })
59 }
60
61 /// Traverse and collect the diagnostic items in the current
62 fn diagnostic_items(tcx: TyCtxt<'_>, cnum: CrateNum) -> DiagnosticItems {
63     assert_eq!(cnum, LOCAL_CRATE);
64
65     // Initialize the collector.
66     let mut diagnostic_items = DiagnosticItems::default();
67
68     // Collect diagnostic items in this crate.
69     let crate_items = tcx.hir_crate_items(());
70
71     for id in crate_items.items() {
72         observe_item(tcx, &mut diagnostic_items, id.owner_id.def_id);
73     }
74
75     for id in crate_items.trait_items() {
76         observe_item(tcx, &mut diagnostic_items, id.owner_id.def_id);
77     }
78
79     for id in crate_items.impl_items() {
80         observe_item(tcx, &mut diagnostic_items, id.owner_id.def_id);
81     }
82
83     for id in crate_items.foreign_items() {
84         observe_item(tcx, &mut diagnostic_items, id.owner_id.def_id);
85     }
86
87     diagnostic_items
88 }
89
90 /// Traverse and collect all the diagnostic items in all crates.
91 fn all_diagnostic_items(tcx: TyCtxt<'_>, (): ()) -> DiagnosticItems {
92     // Initialize the collector.
93     let mut items = DiagnosticItems::default();
94
95     // Collect diagnostic items in other crates.
96     for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
97         for (&name, &def_id) in &tcx.diagnostic_items(cnum).name_to_id {
98             collect_item(tcx, &mut items, name, def_id);
99         }
100     }
101
102     items
103 }
104
105 pub fn provide(providers: &mut Providers) {
106     providers.diagnostic_items = diagnostic_items;
107     providers.all_diagnostic_items = all_diagnostic_items;
108 }