1 //! Detecting diagnostic items.
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.
8 //! * Traits like `Debug`, that have no bearing on language semantics
10 //! * Compiler internal types like `Ty` and `TyCtxt`
12 use rustc::ty::query::Providers;
13 use rustc::ty::TyCtxt;
15 use rustc_data_structures::fx::FxHashMap;
17 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
18 use rustc_hir::itemlikevisit::ItemLikeVisitor;
19 use rustc_span::symbol::{sym, Symbol};
21 struct DiagnosticItemCollector<'tcx> {
22 // items from this crate
23 items: FxHashMap<Symbol, DefId>,
27 impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> {
28 fn visit_item(&mut self, item: &hir::Item<'_>) {
29 self.observe_item(&item.attrs, item.hir_id);
32 fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
33 self.observe_item(&trait_item.attrs, trait_item.hir_id);
36 fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
37 self.observe_item(&impl_item.attrs, impl_item.hir_id);
41 impl<'tcx> DiagnosticItemCollector<'tcx> {
42 fn new(tcx: TyCtxt<'tcx>) -> DiagnosticItemCollector<'tcx> {
43 DiagnosticItemCollector { tcx, items: Default::default() }
46 fn observe_item(&mut self, attrs: &[ast::Attribute], hir_id: hir::HirId) {
47 if let Some(name) = extract(attrs) {
48 let def_id = self.tcx.hir().local_def_id(hir_id);
49 // insert into our table
50 collect_item(self.tcx, &mut self.items, name, def_id);
57 items: &mut FxHashMap<Symbol, DefId>,
61 // Check for duplicates.
62 if let Some(original_def_id) = items.insert(name, item_def_id) {
63 if original_def_id != item_def_id {
64 let mut err = match tcx.hir().span_if_local(item_def_id) {
65 Some(span) => tcx.sess.struct_span_err(
67 &format!("duplicate diagnostic item found: `{}`.", name),
69 None => tcx.sess.struct_err(&format!(
70 "duplicate diagnostic item in crate `{}`: `{}`.",
71 tcx.crate_name(item_def_id.krate),
75 if let Some(span) = tcx.hir().span_if_local(original_def_id) {
76 err.span_note(span, "the diagnostic item is first defined here");
79 "the diagnostic item is first defined in crate `{}`.",
80 tcx.crate_name(original_def_id.krate)
88 /// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.
89 fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
90 attrs.iter().find_map(|attr| {
91 if attr.check_name(sym::rustc_diagnostic_item) { attr.value_str() } else { None }
95 /// Traverse and collect the diagnostic items in the current
96 fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> &'tcx FxHashMap<Symbol, DefId> {
97 // Initialize the collector.
98 let mut collector = DiagnosticItemCollector::new(tcx);
100 // Collect diagnostic items in this crate.
101 tcx.hir().krate().visit_all_item_likes(&mut collector);
103 tcx.arena.alloc(collector.items)
106 /// Traverse and collect all the diagnostic items in all crates.
107 fn collect_all<'tcx>(tcx: TyCtxt<'tcx>) -> &'tcx FxHashMap<Symbol, DefId> {
108 // Initialize the collector.
109 let mut collector = FxHashMap::default();
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).iter() {
114 collect_item(tcx, &mut collector, name, def_id);
118 tcx.arena.alloc(collector)
121 pub fn provide(providers: &mut Providers<'_>) {
122 providers.diagnostic_items = |tcx, id| {
123 assert_eq!(id, LOCAL_CRATE);
126 providers.all_diagnostic_items = |tcx, id| {
127 assert_eq!(id, LOCAL_CRATE);