From f395f2ea120cad774a2d7396e0e2ab5667be3116 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 31 Jan 2021 18:21:04 +0100 Subject: [PATCH] Use less HirId when referring to items. --- .../src/persist/dirty_clean.rs | 28 +++++++++---------- compiler/rustc_passes/src/diagnostic_items.rs | 15 +++++----- compiler/rustc_symbol_mangling/src/test.rs | 12 ++++---- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_incremental/src/persist/dirty_clean.rs b/compiler/rustc_incremental/src/persist/dirty_clean.rs index 752431247f0..8a83149d732 100644 --- a/compiler/rustc_incremental/src/persist/dirty_clean.rs +++ b/compiler/rustc_incremental/src/persist/dirty_clean.rs @@ -17,7 +17,7 @@ use rustc_ast::{self as ast, Attribute, NestedMetaItem}; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::FxHashSet; use rustc_hir as hir; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit; use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::Node as HirNode; @@ -179,7 +179,7 @@ pub struct DirtyCleanVisitor<'tcx> { impl DirtyCleanVisitor<'tcx> { /// Possibly "deserialize" the attribute into a clean/dirty assertion - fn assertion_maybe(&mut self, item_id: hir::HirId, attr: &Attribute) -> Option { + fn assertion_maybe(&mut self, item_id: LocalDefId, attr: &Attribute) -> Option { let is_clean = if self.tcx.sess.check_name(attr, sym::rustc_dirty) { false } else if self.tcx.sess.check_name(attr, sym::rustc_clean) { @@ -207,7 +207,7 @@ impl DirtyCleanVisitor<'tcx> { /// Gets the "auto" assertion on pre-validated attr, along with the `except` labels. fn assertion_auto( &mut self, - item_id: hir::HirId, + item_id: LocalDefId, attr: &Attribute, is_clean: bool, ) -> Assertion { @@ -253,8 +253,9 @@ impl DirtyCleanVisitor<'tcx> { /// Return all DepNode labels that should be asserted for this item. /// index=0 is the "name" used for error messages - fn auto_labels(&mut self, item_id: hir::HirId, attr: &Attribute) -> (&'static str, Labels) { - let node = self.tcx.hir().get(item_id); + fn auto_labels(&mut self, item_id: LocalDefId, attr: &Attribute) -> (&'static str, Labels) { + let hir_id = self.tcx.hir().local_def_id_to_hir_id(item_id); + let node = self.tcx.hir().get(hir_id); let (name, labels) = match node { HirNode::Item(item) => { match item.kind { @@ -430,18 +431,17 @@ impl DirtyCleanVisitor<'tcx> { } } - fn check_item(&mut self, item_id: hir::HirId, item_span: Span) { - let def_id = self.tcx.hir().local_def_id(item_id); - for attr in self.tcx.get_attrs(def_id.to_def_id()).iter() { + fn check_item(&mut self, item_id: LocalDefId, item_span: Span) { + for attr in self.tcx.get_attrs(item_id.to_def_id()).iter() { let assertion = match self.assertion_maybe(item_id, attr) { Some(a) => a, None => continue, }; self.checked_attrs.insert(attr.id); - for dep_node in self.dep_nodes(&assertion.clean, def_id.to_def_id()) { + for dep_node in self.dep_nodes(&assertion.clean, item_id.to_def_id()) { self.assert_clean(item_span, dep_node); } - for dep_node in self.dep_nodes(&assertion.dirty, def_id.to_def_id()) { + for dep_node in self.dep_nodes(&assertion.dirty, item_id.to_def_id()) { self.assert_dirty(item_span, dep_node); } } @@ -450,19 +450,19 @@ impl DirtyCleanVisitor<'tcx> { impl ItemLikeVisitor<'tcx> for DirtyCleanVisitor<'tcx> { fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { - self.check_item(item.hir_id(), item.span); + self.check_item(item.def_id, item.span); } fn visit_trait_item(&mut self, item: &hir::TraitItem<'_>) { - self.check_item(item.hir_id(), item.span); + self.check_item(item.def_id, item.span); } fn visit_impl_item(&mut self, item: &hir::ImplItem<'_>) { - self.check_item(item.hir_id(), item.span); + self.check_item(item.def_id, item.span); } fn visit_foreign_item(&mut self, item: &hir::ForeignItem<'_>) { - self.check_item(item.hir_id(), item.span); + self.check_item(item.def_id, item.span); } } diff --git a/compiler/rustc_passes/src/diagnostic_items.rs b/compiler/rustc_passes/src/diagnostic_items.rs index 29e99203fdd..3ec7ea39248 100644 --- a/compiler/rustc_passes/src/diagnostic_items.rs +++ b/compiler/rustc_passes/src/diagnostic_items.rs @@ -16,7 +16,7 @@ use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_middle::ty::query::Providers; use rustc_middle::ty::TyCtxt; use rustc_session::Session; -use rustc_span::def_id::{DefId, LOCAL_CRATE}; +use rustc_span::def_id::{DefId, LocalDefId, LOCAL_CRATE}; use rustc_span::symbol::{sym, Symbol}; struct DiagnosticItemCollector<'tcx> { @@ -27,19 +27,19 @@ struct DiagnosticItemCollector<'tcx> { impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> { fn visit_item(&mut self, item: &hir::Item<'_>) { - self.observe_item(&item.attrs, item.hir_id()); + self.observe_item(&item.attrs, item.def_id); } fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) { - self.observe_item(&trait_item.attrs, trait_item.hir_id()); + self.observe_item(&trait_item.attrs, trait_item.def_id); } fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) { - self.observe_item(&impl_item.attrs, impl_item.hir_id()); + self.observe_item(&impl_item.attrs, impl_item.def_id); } fn visit_foreign_item(&mut self, foreign_item: &hir::ForeignItem<'_>) { - self.observe_item(foreign_item.attrs, foreign_item.hir_id()); + self.observe_item(foreign_item.attrs, foreign_item.def_id); } } @@ -48,9 +48,8 @@ impl<'tcx> DiagnosticItemCollector<'tcx> { DiagnosticItemCollector { tcx, items: Default::default() } } - fn observe_item(&mut self, attrs: &[ast::Attribute], hir_id: hir::HirId) { + fn observe_item(&mut self, attrs: &[ast::Attribute], def_id: LocalDefId) { if let Some(name) = extract(&self.tcx.sess, attrs) { - let def_id = self.tcx.hir().local_def_id(hir_id); // insert into our table collect_item(self.tcx, &mut self.items, name, def_id.to_def_id()); } @@ -106,7 +105,7 @@ fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> FxHashMap { tcx.hir().krate().visit_all_item_likes(&mut collector); for m in tcx.hir().krate().exported_macros { - collector.observe_item(m.attrs, m.hir_id()); + collector.observe_item(m.attrs, m.def_id); } collector.items diff --git a/compiler/rustc_symbol_mangling/src/test.rs b/compiler/rustc_symbol_mangling/src/test.rs index b1af95a3ab9..bfe9c4d6de3 100644 --- a/compiler/rustc_symbol_mangling/src/test.rs +++ b/compiler/rustc_symbol_mangling/src/test.rs @@ -5,6 +5,7 @@ //! paths etc in all kinds of annoying scenarios. use rustc_hir as hir; +use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{subst::InternalSubsts, Instance, TyCtxt}; use rustc_span::symbol::{sym, Symbol}; @@ -31,9 +32,8 @@ struct SymbolNamesTest<'tcx> { } impl SymbolNamesTest<'tcx> { - fn process_attrs(&mut self, hir_id: hir::HirId) { + fn process_attrs(&mut self, def_id: LocalDefId) { let tcx = self.tcx; - let def_id = tcx.hir().local_def_id(hir_id); for attr in tcx.get_attrs(def_id.to_def_id()).iter() { if tcx.sess.check_name(attr, SYMBOL_NAME) { let def_id = def_id.to_def_id(); @@ -61,18 +61,18 @@ impl SymbolNamesTest<'tcx> { impl hir::itemlikevisit::ItemLikeVisitor<'tcx> for SymbolNamesTest<'tcx> { fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { - self.process_attrs(item.hir_id()); + self.process_attrs(item.def_id); } fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) { - self.process_attrs(trait_item.hir_id()); + self.process_attrs(trait_item.def_id); } fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) { - self.process_attrs(impl_item.hir_id()); + self.process_attrs(impl_item.def_id); } fn visit_foreign_item(&mut self, foreign_item: &'tcx hir::ForeignItem<'tcx>) { - self.process_attrs(foreign_item.hir_id()); + self.process_attrs(foreign_item.def_id); } }