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`
13 use rustc_data_structures::fx::FxHashMap;
15 use rustc_hir::itemlikevisit::ItemLikeVisitor;
16 use rustc_middle::ty::query::Providers;
17 use rustc_middle::ty::TyCtxt;
18 use rustc_session::Session;
19 use rustc_span::def_id::{DefId, LocalDefId, LOCAL_CRATE};
20 use rustc_span::symbol::{sym, Symbol};
22 struct DiagnosticItemCollector<'tcx> {
23 // items from this crate
24 items: FxHashMap<Symbol, DefId>,
28 impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> {
29 fn visit_item(&mut self, item: &hir::Item<'_>) {
30 self.observe_item(item.def_id);
33 fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
34 self.observe_item(trait_item.def_id);
37 fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
38 self.observe_item(impl_item.def_id);
41 fn visit_foreign_item(&mut self, foreign_item: &hir::ForeignItem<'_>) {
42 self.observe_item(foreign_item.def_id);
46 impl<'tcx> DiagnosticItemCollector<'tcx> {
47 fn new(tcx: TyCtxt<'tcx>) -> DiagnosticItemCollector<'tcx> {
48 DiagnosticItemCollector { tcx, items: Default::default() }
51 fn observe_item(&mut self, def_id: LocalDefId) {
52 let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
53 let attrs = self.tcx.hir().attrs(hir_id);
54 if let Some(name) = extract(&self.tcx.sess, attrs) {
55 // insert into our table
56 collect_item(self.tcx, &mut self.items, name, def_id.to_def_id());
63 items: &mut FxHashMap<Symbol, DefId>,
67 // Check for duplicates.
68 if let Some(original_def_id) = items.insert(name, item_def_id) {
69 if original_def_id != item_def_id {
70 let mut err = match tcx.hir().span_if_local(item_def_id) {
71 Some(span) => tcx.sess.struct_span_err(
73 &format!("duplicate diagnostic item found: `{}`.", name),
75 None => tcx.sess.struct_err(&format!(
76 "duplicate diagnostic item in crate `{}`: `{}`.",
77 tcx.crate_name(item_def_id.krate),
81 if let Some(span) = tcx.hir().span_if_local(original_def_id) {
82 err.span_note(span, "the diagnostic item is first defined here");
85 "the diagnostic item is first defined in crate `{}`.",
86 tcx.crate_name(original_def_id.krate)
94 /// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.
95 fn extract(sess: &Session, attrs: &[ast::Attribute]) -> Option<Symbol> {
96 attrs.iter().find_map(|attr| {
97 if sess.check_name(attr, sym::rustc_diagnostic_item) { attr.value_str() } else { None }
101 /// Traverse and collect the diagnostic items in the current
102 fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> FxHashMap<Symbol, DefId> {
103 // Initialize the collector.
104 let mut collector = DiagnosticItemCollector::new(tcx);
106 // Collect diagnostic items in this crate.
107 tcx.hir().krate().visit_all_item_likes(&mut collector);
109 for m in tcx.hir().krate().exported_macros {
110 collector.observe_item(m.def_id);
116 /// Traverse and collect all the diagnostic items in all crates.
117 fn collect_all<'tcx>(tcx: TyCtxt<'tcx>) -> FxHashMap<Symbol, DefId> {
118 // Initialize the collector.
119 let mut collector = FxHashMap::default();
121 // Collect diagnostic items in other crates.
122 for &cnum in tcx.crates().iter().chain(std::iter::once(&LOCAL_CRATE)) {
123 for (&name, &def_id) in tcx.diagnostic_items(cnum).iter() {
124 collect_item(tcx, &mut collector, name, def_id);
131 pub fn provide(providers: &mut Providers) {
132 providers.diagnostic_items = |tcx, id| {
133 assert_eq!(id, LOCAL_CRATE);
136 providers.all_diagnostic_items = |tcx, id| {
137 assert_eq!(id, LOCAL_CRATE);