diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 2e2ca6a2788..ad119c4e073 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -64,13 +64,17 @@ impl ModuleItems { self.foreign_items.iter().copied() } - pub fn definitions(&self) -> impl Iterator + '_ { + pub fn owners(&self) -> impl Iterator + '_ { self.items .iter() - .map(|id| id.owner_id.def_id) - .chain(self.trait_items.iter().map(|id| id.owner_id.def_id)) - .chain(self.impl_items.iter().map(|id| id.owner_id.def_id)) - .chain(self.foreign_items.iter().map(|id| id.owner_id.def_id)) + .map(|id| id.owner_id) + .chain(self.trait_items.iter().map(|id| id.owner_id)) + .chain(self.impl_items.iter().map(|id| id.owner_id)) + .chain(self.foreign_items.iter().map(|id| id.owner_id)) + } + + pub fn definitions(&self) -> impl Iterator + '_ { + self.owners().map(|id| id.def_id) } pub fn par_items(&self, f: impl Fn(ItemId) + Send + Sync) { diff --git a/compiler/rustc_passes/src/diagnostic_items.rs b/compiler/rustc_passes/src/diagnostic_items.rs index 0ae7096642c..ae3d40b0ec5 100644 --- a/compiler/rustc_passes/src/diagnostic_items.rs +++ b/compiler/rustc_passes/src/diagnostic_items.rs @@ -11,19 +11,19 @@ use rustc_ast as ast; use rustc_hir::diagnostic_items::DiagnosticItems; +use rustc_hir::OwnerId; use rustc_middle::ty::query::Providers; use rustc_middle::ty::TyCtxt; -use rustc_span::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; +use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE}; use rustc_span::symbol::{kw::Empty, sym, Symbol}; use crate::errors::{DuplicateDiagnosticItem, DuplicateDiagnosticItemInCrate}; -fn observe_item(tcx: TyCtxt<'_>, diagnostic_items: &mut DiagnosticItems, def_id: LocalDefId) { - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); - let attrs = tcx.hir().attrs(hir_id); +fn observe_item<'tcx>(tcx: TyCtxt<'tcx>, diagnostic_items: &mut DiagnosticItems, owner: OwnerId) { + let attrs = tcx.hir().attrs(owner.into()); if let Some(name) = extract(attrs) { // insert into our table - collect_item(tcx, diagnostic_items, name, def_id.to_def_id()); + collect_item(tcx, diagnostic_items, name, owner.to_def_id()); } } @@ -31,23 +31,33 @@ fn collect_item(tcx: TyCtxt<'_>, items: &mut DiagnosticItems, name: Symbol, item items.id_to_name.insert(item_def_id, name); if let Some(original_def_id) = items.name_to_id.insert(name, item_def_id) { if original_def_id != item_def_id { - let orig_span = tcx.hir().span_if_local(original_def_id); - let orig_crate_name = - orig_span.is_none().then(|| tcx.crate_name(original_def_id.krate)); - match tcx.hir().span_if_local(item_def_id) { - Some(span) => tcx.sess.emit_err(DuplicateDiagnosticItem { span, name }), - None => tcx.sess.emit_err(DuplicateDiagnosticItemInCrate { - span: orig_span, - orig_crate_name: orig_crate_name.unwrap_or(Empty), - have_orig_crate_name: orig_crate_name.map(|_| ()), - crate_name: tcx.crate_name(item_def_id.krate), - name, - }), - }; + report_duplicate_item(tcx, name, original_def_id, item_def_id); } } } +fn report_duplicate_item( + tcx: TyCtxt<'_>, + name: Symbol, + original_def_id: DefId, + item_def_id: DefId, +) { + let (orig_span, orig_crate_name, have_orig_crate_name) = match original_def_id.as_local() { + Some(local_original) => (Some(tcx.def_span(local_original)), Empty, None), + None => (None, tcx.crate_name(original_def_id.krate), Some(())), + }; + match tcx.hir().span_if_local(item_def_id) { + Some(span) => tcx.sess.emit_err(DuplicateDiagnosticItem { span, name }), + None => tcx.sess.emit_err(DuplicateDiagnosticItemInCrate { + span: orig_span, + orig_crate_name, + have_orig_crate_name, + crate_name: tcx.crate_name(item_def_id.krate), + name, + }), + }; +} + /// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes. fn extract(attrs: &[ast::Attribute]) -> Option { attrs.iter().find_map(|attr| { @@ -64,21 +74,8 @@ fn diagnostic_items(tcx: TyCtxt<'_>, cnum: CrateNum) -> DiagnosticItems { // Collect diagnostic items in this crate. let crate_items = tcx.hir_crate_items(()); - - for id in crate_items.items() { - observe_item(tcx, &mut diagnostic_items, id.owner_id.def_id); - } - - for id in crate_items.trait_items() { - observe_item(tcx, &mut diagnostic_items, id.owner_id.def_id); - } - - for id in crate_items.impl_items() { - observe_item(tcx, &mut diagnostic_items, id.owner_id.def_id); - } - - for id in crate_items.foreign_items() { - observe_item(tcx, &mut diagnostic_items, id.owner_id.def_id); + for id in crate_items.owners() { + observe_item(tcx, &mut diagnostic_items, id); } diagnostic_items