2019-05-19 20:16:04 +02:00
|
|
|
//! Detecting diagnostic items.
|
|
|
|
//!
|
|
|
|
//! Diagnostic items are items that are not language-inherent, but can reasonably be expected to
|
|
|
|
//! exist for diagnostic purposes. This allows diagnostic authors to refer to specific items
|
|
|
|
//! directly, without having to guess module paths and crates.
|
|
|
|
//! Examples are:
|
|
|
|
//!
|
|
|
|
//! * Traits like `Debug`, that have no bearing on language semantics
|
|
|
|
//!
|
|
|
|
//! * Compiler internal types like `Ty` and `TyCtxt`
|
|
|
|
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast as ast;
|
2019-12-24 05:02:53 +01:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-07-30 11:27:50 +10:00
|
|
|
use rustc_session::Session;
|
2021-01-31 18:21:04 +01:00
|
|
|
use rustc_span::def_id::{DefId, LocalDefId, LOCAL_CRATE};
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2019-05-19 20:16:04 +02:00
|
|
|
|
|
|
|
struct DiagnosticItemCollector<'tcx> {
|
|
|
|
// items from this crate
|
|
|
|
items: FxHashMap<Symbol, DefId>,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> {
|
2019-11-28 19:28:50 +01:00
|
|
|
fn visit_item(&mut self, item: &hir::Item<'_>) {
|
2020-11-27 00:34:23 +01:00
|
|
|
self.observe_item(item.def_id);
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:47:10 +01:00
|
|
|
fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
|
2020-11-27 00:34:23 +01:00
|
|
|
self.observe_item(trait_item.def_id);
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 22:16:44 +01:00
|
|
|
fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
|
2020-11-27 00:34:23 +01:00
|
|
|
self.observe_item(impl_item.def_id);
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
2020-11-11 21:57:54 +01:00
|
|
|
|
2020-11-26 19:12:24 +01:00
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &hir::ForeignItem<'_>) {
|
2020-11-27 00:34:23 +01:00
|
|
|
self.observe_item(foreign_item.def_id);
|
2020-11-26 19:12:24 +01:00
|
|
|
}
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> DiagnosticItemCollector<'tcx> {
|
|
|
|
fn new(tcx: TyCtxt<'tcx>) -> DiagnosticItemCollector<'tcx> {
|
2019-12-22 17:42:04 -05:00
|
|
|
DiagnosticItemCollector { tcx, items: Default::default() }
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-27 00:34:23 +01:00
|
|
|
fn observe_item(&mut self, def_id: LocalDefId) {
|
|
|
|
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
|
|
|
|
let attrs = self.tcx.hir().attrs(hir_id);
|
2020-07-30 11:27:50 +10:00
|
|
|
if let Some(name) = extract(&self.tcx.sess, attrs) {
|
2019-05-19 20:16:04 +02:00
|
|
|
// insert into our table
|
2020-04-09 09:43:00 +01:00
|
|
|
collect_item(self.tcx, &mut self.items, name, def_id.to_def_id());
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collect_item(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
items: &mut FxHashMap<Symbol, DefId>,
|
|
|
|
name: Symbol,
|
|
|
|
item_def_id: DefId,
|
|
|
|
) {
|
|
|
|
// Check for duplicates.
|
|
|
|
if let Some(original_def_id) = items.insert(name, item_def_id) {
|
|
|
|
if original_def_id != item_def_id {
|
|
|
|
let mut err = match tcx.hir().span_if_local(item_def_id) {
|
|
|
|
Some(span) => tcx.sess.struct_span_err(
|
|
|
|
span,
|
2019-12-22 17:42:04 -05:00
|
|
|
&format!("duplicate diagnostic item found: `{}`.", name),
|
|
|
|
),
|
2019-05-19 20:16:04 +02:00
|
|
|
None => tcx.sess.struct_err(&format!(
|
2019-12-22 17:42:04 -05:00
|
|
|
"duplicate diagnostic item in crate `{}`: `{}`.",
|
|
|
|
tcx.crate_name(item_def_id.krate),
|
|
|
|
name
|
|
|
|
)),
|
2019-05-19 20:16:04 +02:00
|
|
|
};
|
|
|
|
if let Some(span) = tcx.hir().span_if_local(original_def_id) {
|
2020-01-22 23:57:38 +00:00
|
|
|
err.span_note(span, "the diagnostic item is first defined here");
|
2019-05-19 20:16:04 +02:00
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
err.note(&format!(
|
2020-01-22 23:57:38 +00:00
|
|
|
"the diagnostic item is first defined in crate `{}`.",
|
2019-12-22 17:42:04 -05:00
|
|
|
tcx.crate_name(original_def_id.krate)
|
|
|
|
));
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.
|
2020-07-30 11:27:50 +10:00
|
|
|
fn extract(sess: &Session, attrs: &[ast::Attribute]) -> Option<Symbol> {
|
2019-05-19 20:16:04 +02:00
|
|
|
attrs.iter().find_map(|attr| {
|
2020-07-30 11:27:50 +10:00
|
|
|
if sess.check_name(attr, sym::rustc_diagnostic_item) { attr.value_str() } else { None }
|
2019-05-19 20:16:04 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Traverse and collect the diagnostic items in the current
|
2020-03-27 20:26:20 +01:00
|
|
|
fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> FxHashMap<Symbol, DefId> {
|
2019-05-19 20:16:04 +02:00
|
|
|
// Initialize the collector.
|
|
|
|
let mut collector = DiagnosticItemCollector::new(tcx);
|
|
|
|
|
|
|
|
// Collect diagnostic items in this crate.
|
|
|
|
tcx.hir().krate().visit_all_item_likes(&mut collector);
|
|
|
|
|
2020-10-18 22:19:13 +02:00
|
|
|
for m in tcx.hir().krate().exported_macros {
|
2020-11-27 00:34:23 +01:00
|
|
|
collector.observe_item(m.def_id);
|
2020-10-18 22:19:13 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 20:26:20 +01:00
|
|
|
collector.items
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Traverse and collect all the diagnostic items in all crates.
|
2020-03-27 20:26:20 +01:00
|
|
|
fn collect_all<'tcx>(tcx: TyCtxt<'tcx>) -> FxHashMap<Symbol, DefId> {
|
2019-05-19 20:16:04 +02:00
|
|
|
// Initialize the collector.
|
|
|
|
let mut collector = FxHashMap::default();
|
|
|
|
|
|
|
|
// Collect diagnostic items in other crates.
|
|
|
|
for &cnum in tcx.crates().iter().chain(std::iter::once(&LOCAL_CRATE)) {
|
|
|
|
for (&name, &def_id) in tcx.diagnostic_items(cnum).iter() {
|
|
|
|
collect_item(tcx, &mut collector, name, def_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 20:26:20 +01:00
|
|
|
collector
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
2019-12-26 23:33:40 +01:00
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2019-12-26 23:33:40 +01:00
|
|
|
providers.diagnostic_items = |tcx, id| {
|
|
|
|
assert_eq!(id, LOCAL_CRATE);
|
|
|
|
collect(tcx)
|
|
|
|
};
|
|
|
|
providers.all_diagnostic_items = |tcx, id| {
|
|
|
|
assert_eq!(id, LOCAL_CRATE);
|
|
|
|
collect_all(tcx)
|
|
|
|
};
|
|
|
|
}
|