Skip to main content

rustc_passes/
diagnostic_items.rs

1//! Detecting diagnostic items.
2//!
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.
6//! Examples are:
7//!
8//! * Traits like `Debug`, that have no bearing on language semantics
9//!
10//! * Compiler internal types like `Ty` and `TyCtxt`
11
12use rustc_hir::attrs::AttributeKind;
13use rustc_hir::diagnostic_items::DiagnosticItems;
14use rustc_hir::{CRATE_OWNER_ID, OwnerId, find_attr};
15use rustc_middle::query::{LocalCrate, Providers};
16use rustc_middle::ty::TyCtxt;
17use rustc_span::Symbol;
18use rustc_span::def_id::{DefId, LOCAL_CRATE};
19
20use crate::errors::DuplicateDiagnosticItemInCrate;
21
22fn observe_item<'tcx>(tcx: TyCtxt<'tcx>, diagnostic_items: &mut DiagnosticItems, owner: OwnerId) {
23    let attrs = tcx.hir_attrs(owner.into());
24    if let Some(name) = {
    'done:
        {
        for i in attrs {
            let i: &rustc_hir::Attribute = i;
            match i {
                rustc_hir::Attribute::Parsed(AttributeKind::RustcDiagnosticItem(name))
                    => {
                    break 'done Some(name);
                }
                _ => {}
            }
        }
        None
    }
}find_attr!(attrs, AttributeKind::RustcDiagnosticItem(name) => name) {
25        // insert into our table
26        collect_item(tcx, diagnostic_items, *name, owner.to_def_id());
27    }
28}
29
30fn collect_item(tcx: TyCtxt<'_>, items: &mut DiagnosticItems, name: Symbol, item_def_id: DefId) {
31    items.id_to_name.insert(item_def_id, name);
32    if let Some(original_def_id) = items.name_to_id.insert(name, item_def_id) {
33        if original_def_id != item_def_id {
34            report_duplicate_item(tcx, name, original_def_id, item_def_id);
35        }
36    }
37}
38
39fn report_duplicate_item(
40    tcx: TyCtxt<'_>,
41    name: Symbol,
42    original_def_id: DefId,
43    item_def_id: DefId,
44) {
45    let orig_span = tcx.hir_span_if_local(original_def_id);
46    let duplicate_span = tcx.hir_span_if_local(item_def_id);
47    tcx.dcx().emit_err(DuplicateDiagnosticItemInCrate {
48        duplicate_span,
49        orig_span,
50        crate_name: tcx.crate_name(item_def_id.krate),
51        orig_crate_name: tcx.crate_name(original_def_id.krate),
52        different_crates: (item_def_id.krate != original_def_id.krate),
53        name,
54    });
55}
56
57/// Traverse and collect the diagnostic items in the current
58fn diagnostic_items(tcx: TyCtxt<'_>, _: LocalCrate) -> DiagnosticItems {
59    // Initialize the collector.
60    let mut diagnostic_items = DiagnosticItems::default();
61
62    // Collect diagnostic items in this crate.
63    let crate_items = tcx.hir_crate_items(());
64    for id in crate_items.owners().chain(std::iter::once(CRATE_OWNER_ID)) {
65        observe_item(tcx, &mut diagnostic_items, id);
66    }
67
68    diagnostic_items
69}
70
71/// Traverse and collect all the diagnostic items in all crates.
72fn all_diagnostic_items(tcx: TyCtxt<'_>, (): ()) -> DiagnosticItems {
73    // Initialize the collector.
74    let mut items = DiagnosticItems::default();
75
76    // Collect diagnostic items in visible crates.
77    for cnum in tcx
78        .crates(())
79        .iter()
80        .copied()
81        .filter(|cnum| tcx.is_user_visible_dep(*cnum))
82        .chain(std::iter::once(LOCAL_CRATE))
83    {
84        for (&name, &def_id) in &tcx.diagnostic_items(cnum).name_to_id {
85            collect_item(tcx, &mut items, name, def_id);
86        }
87    }
88
89    items
90}
91
92pub(crate) fn provide(providers: &mut Providers) {
93    providers.diagnostic_items = diagnostic_items;
94    providers.all_diagnostic_items = all_diagnostic_items;
95}