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_span::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
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.def_id);
32 fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
33 self.observe_item(trait_item.def_id);
36 fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
37 self.observe_item(impl_item.def_id);
40 fn visit_foreign_item(&mut self, foreign_item: &hir::ForeignItem<'_>) {
41 self.observe_item(foreign_item.def_id);
45 impl<'tcx> DiagnosticItemCollector<'tcx> {
46 fn new(tcx: TyCtxt<'tcx>) -> DiagnosticItemCollector<'tcx> {
47 DiagnosticItemCollector { tcx, items: Default::default() }
50 fn observe_item(&mut self, def_id: LocalDefId) {
51 let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
52 let attrs = self.tcx.hir().attrs(hir_id);
53 if let Some(name) = extract(attrs) {
54 // insert into our table
55 collect_item(self.tcx, &mut self.items, name, def_id.to_def_id());
62 items: &mut FxHashMap<Symbol, DefId>,
66 // Check for duplicates.
67 if let Some(original_def_id) = items.insert(name, item_def_id) {
68 if original_def_id != item_def_id {
69 let mut err = match tcx.hir().span_if_local(item_def_id) {
70 Some(span) => tcx.sess.struct_span_err(
72 &format!("duplicate diagnostic item found: `{}`.", name),
74 None => tcx.sess.struct_err(&format!(
75 "duplicate diagnostic item in crate `{}`: `{}`.",
76 tcx.crate_name(item_def_id.krate),
80 if let Some(span) = tcx.hir().span_if_local(original_def_id) {
81 err.span_note(span, "the diagnostic item is first defined here");
84 "the diagnostic item is first defined in crate `{}`.",
85 tcx.crate_name(original_def_id.krate)
93 /// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.p
94 fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
95 attrs.iter().find_map(|attr| {
96 if attr.has_name(sym::rustc_diagnostic_item) { attr.value_str() } else { None }
100 /// Traverse and collect the diagnostic items in the current
101 fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> FxHashMap<Symbol, DefId> {
102 assert_eq!(cnum, LOCAL_CRATE);
104 // Initialize the collector.
105 let mut collector = DiagnosticItemCollector::new(tcx);
107 // Collect diagnostic items in this crate.
108 tcx.hir().visit_all_item_likes(&mut collector);
113 /// Traverse and collect all the diagnostic items in all crates.
114 fn all_diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> FxHashMap<Symbol, DefId> {
115 // Initialize the collector.
116 let mut collector = FxHashMap::default();
118 // Collect diagnostic items in other crates.
119 for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
120 for (&name, &def_id) in tcx.diagnostic_items(cnum).iter() {
121 collect_item(tcx, &mut collector, name, def_id);
128 pub fn provide(providers: &mut Providers) {
129 providers.diagnostic_items = diagnostic_items;
130 providers.all_diagnostic_items = all_diagnostic_items;