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