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`
|
|
|
|
|
|
2021-10-04 15:57:39 -05:00
|
|
|
use rustc_hir::diagnostic_items::DiagnosticItems;
|
2024-10-17 01:14:01 +02:00
|
|
|
use rustc_hir::{Attribute, OwnerId};
|
2023-03-13 22:22:59 +00:00
|
|
|
use rustc_middle::query::{LocalCrate, Providers};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2023-03-13 18:54:05 +00:00
|
|
|
use rustc_span::def_id::{DefId, LOCAL_CRATE};
|
2024-12-13 10:29:23 +11:00
|
|
|
use rustc_span::{Symbol, sym};
|
2019-05-19 20:16:04 +02:00
|
|
|
|
2023-02-26 10:29:25 +00:00
|
|
|
use crate::errors::DuplicateDiagnosticItemInCrate;
|
2022-09-22 18:23:05 -06:00
|
|
|
|
2022-10-25 18:34:58 +00:00
|
|
|
fn observe_item<'tcx>(tcx: TyCtxt<'tcx>, diagnostic_items: &mut DiagnosticItems, owner: OwnerId) {
|
|
|
|
|
let attrs = tcx.hir().attrs(owner.into());
|
2022-05-03 17:05:19 -04:00
|
|
|
if let Some(name) = extract(attrs) {
|
|
|
|
|
// insert into our table
|
2022-10-25 18:34:58 +00:00
|
|
|
collect_item(tcx, diagnostic_items, name, owner.to_def_id());
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-04 15:57:39 -05:00
|
|
|
fn collect_item(tcx: TyCtxt<'_>, items: &mut DiagnosticItems, name: Symbol, item_def_id: DefId) {
|
|
|
|
|
items.id_to_name.insert(item_def_id, name);
|
|
|
|
|
if let Some(original_def_id) = items.name_to_id.insert(name, item_def_id) {
|
2019-05-19 20:16:04 +02:00
|
|
|
if original_def_id != item_def_id {
|
2022-10-25 18:34:58 +00:00
|
|
|
report_duplicate_item(tcx, name, original_def_id, item_def_id);
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 18:34:58 +00:00
|
|
|
fn report_duplicate_item(
|
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
|
name: Symbol,
|
|
|
|
|
original_def_id: DefId,
|
|
|
|
|
item_def_id: DefId,
|
|
|
|
|
) {
|
2023-02-26 10:29:25 +00:00
|
|
|
let orig_span = tcx.hir().span_if_local(original_def_id);
|
|
|
|
|
let duplicate_span = tcx.hir().span_if_local(item_def_id);
|
2023-12-18 22:21:37 +11:00
|
|
|
tcx.dcx().emit_err(DuplicateDiagnosticItemInCrate {
|
2023-02-26 10:29:25 +00:00
|
|
|
duplicate_span,
|
|
|
|
|
orig_span,
|
|
|
|
|
crate_name: tcx.crate_name(item_def_id.krate),
|
|
|
|
|
orig_crate_name: tcx.crate_name(original_def_id.krate),
|
2024-08-21 00:57:58 -04:00
|
|
|
different_crates: (item_def_id.krate != original_def_id.krate),
|
2023-02-26 10:29:25 +00:00
|
|
|
name,
|
|
|
|
|
});
|
2022-10-25 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-10 12:02:20 +02:00
|
|
|
/// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.
|
2024-10-17 01:14:01 +02:00
|
|
|
fn extract(attrs: &[Attribute]) -> Option<Symbol> {
|
2019-05-19 20:16:04 +02:00
|
|
|
attrs.iter().find_map(|attr| {
|
2022-10-10 10:06:44 -06:00
|
|
|
if attr.has_name(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
|
2023-03-13 22:22:59 +00:00
|
|
|
fn diagnostic_items(tcx: TyCtxt<'_>, _: LocalCrate) -> DiagnosticItems {
|
2019-05-19 20:16:04 +02:00
|
|
|
// Initialize the collector.
|
2022-05-03 17:05:19 -04:00
|
|
|
let mut diagnostic_items = DiagnosticItems::default();
|
2019-05-19 20:16:04 +02:00
|
|
|
|
|
|
|
|
// Collect diagnostic items in this crate.
|
2022-05-03 17:05:19 -04:00
|
|
|
let crate_items = tcx.hir_crate_items(());
|
2022-10-25 18:34:58 +00:00
|
|
|
for id in crate_items.owners() {
|
|
|
|
|
observe_item(tcx, &mut diagnostic_items, id);
|
2022-05-03 17:05:19 -04:00
|
|
|
}
|
2019-05-19 20:16:04 +02:00
|
|
|
|
2022-05-03 17:05:19 -04:00
|
|
|
diagnostic_items
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Traverse and collect all the diagnostic items in all crates.
|
2022-12-20 22:10:40 +01:00
|
|
|
fn all_diagnostic_items(tcx: TyCtxt<'_>, (): ()) -> DiagnosticItems {
|
2019-05-19 20:16:04 +02:00
|
|
|
// Initialize the collector.
|
2021-10-04 15:57:39 -05:00
|
|
|
let mut items = DiagnosticItems::default();
|
2019-05-19 20:16:04 +02:00
|
|
|
|
|
|
|
|
// Collect diagnostic items in other crates.
|
2024-06-06 09:45:50 +00:00
|
|
|
for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
|
2021-10-04 15:57:39 -05:00
|
|
|
for (&name, &def_id) in &tcx.diagnostic_items(cnum).name_to_id {
|
|
|
|
|
collect_item(tcx, &mut items, name, def_id);
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-04 15:57:39 -05:00
|
|
|
items
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
2019-12-26 23:33:40 +01:00
|
|
|
|
2024-08-29 15:17:14 +10:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
2021-05-11 13:50:41 +02:00
|
|
|
providers.diagnostic_items = diagnostic_items;
|
|
|
|
|
providers.all_diagnostic_items = all_diagnostic_items;
|
2019-12-26 23:33:40 +01:00
|
|
|
}
|