]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_passes/src/diagnostic_items.rs
remove out-of-date fixme
[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) {
62             attr.value_str()
63         } else {
64             None
65         }
66     })
67 }
68
69 /// Traverse and collect the diagnostic items in the current
70 fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> DiagnosticItems {
71     assert_eq!(cnum, LOCAL_CRATE);
72
73     // Initialize the collector.
74     let mut diagnostic_items = DiagnosticItems::default();
75
76     // Collect diagnostic items in this crate.
77     let crate_items = tcx.hir_crate_items(());
78
79     for id in crate_items.items() {
80         observe_item(tcx, &mut diagnostic_items, id.def_id.def_id);
81     }
82
83     for id in crate_items.trait_items() {
84         observe_item(tcx, &mut diagnostic_items, id.def_id.def_id);
85     }
86
87     for id in crate_items.impl_items() {
88         observe_item(tcx, &mut diagnostic_items, id.def_id.def_id);
89     }
90
91     for id in crate_items.foreign_items() {
92         observe_item(tcx, &mut diagnostic_items, id.def_id.def_id);
93     }
94
95     diagnostic_items
96 }
97
98 /// Traverse and collect all the diagnostic items in all crates.
99 fn all_diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> DiagnosticItems {
100     // Initialize the collector.
101     let mut items = DiagnosticItems::default();
102
103     // Collect diagnostic items in other crates.
104     for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
105         for (&name, &def_id) in &tcx.diagnostic_items(cnum).name_to_id {
106             collect_item(tcx, &mut items, name, def_id);
107         }
108     }
109
110     items
111 }
112
113 pub fn provide(providers: &mut Providers) {
114     providers.diagnostic_items = diagnostic_items;
115     providers.all_diagnostic_items = all_diagnostic_items;
116 }