From b40e6baec7504ebc7c681259d268c81b1e0241b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Fri, 7 Feb 2020 18:25:36 +0100 Subject: [PATCH] Update `visit_item_likes_in_module` --- src/librustc/dep_graph/dep_node.rs | 12 ++----- src/librustc/hir/map/collector.rs | 4 +-- src/librustc/hir/map/definitions.rs | 26 ++------------- src/librustc/hir/map/mod.rs | 12 ++----- src/librustc/hir/mod.rs | 6 ++++ src/librustc/ich/hcx.rs | 15 +-------- src/librustc/ich/impls_hir.rs | 7 +++- src/librustc/query/mod.rs | 4 +++ src/librustc_hir/hir.rs | 2 +- src/librustc_hir/stable_hash_impls.rs | 34 ++++++++++++++++++-- src/librustc_incremental/assert_dep_graph.rs | 2 +- src/librustc_metadata/rmeta/decoder.rs | 5 +-- src/librustc_span/def_id.rs | 25 ++++++++++++++ 13 files changed, 88 insertions(+), 66 deletions(-) diff --git a/src/librustc/dep_graph/dep_node.rs b/src/librustc/dep_graph/dep_node.rs index 03218920ef1..755bc15ba93 100644 --- a/src/librustc/dep_graph/dep_node.rs +++ b/src/librustc/dep_graph/dep_node.rs @@ -223,8 +223,8 @@ macro_rules! define_dep_nodes { /// Construct a DepNode from the given DepKind and DefPathHash. This /// method will assert that the given DepKind actually requires a /// single DefId/DefPathHash parameter. - pub fn from_def_path_hash(kind: DepKind, - def_path_hash: DefPathHash) + pub fn from_def_path_hash(def_path_hash: DefPathHash, + kind: DepKind) -> DepNode { debug_assert!(kind.can_reconstruct_query_key() && kind.has_params()); DepNode { @@ -280,7 +280,7 @@ macro_rules! define_dep_nodes { } if kind.has_params() { - Ok(def_path_hash.to_dep_node(kind)) + Ok(DepNode::from_def_path_hash(def_path_hash, kind)) } else { Ok(DepNode::new_no_params(kind)) } @@ -337,12 +337,6 @@ impl fmt::Debug for DepNode { } } -impl DefPathHash { - pub fn to_dep_node(self, kind: DepKind) -> DepNode { - DepNode::from_def_path_hash(kind, self) - } -} - rustc_dep_node_append!([define_dep_nodes!][ <'tcx> // We use this for most things when incr. comp. is turned off. [] Null, diff --git a/src/librustc/hir/map/collector.rs b/src/librustc/hir/map/collector.rs index 7e626f79c0c..231d0ee4821 100644 --- a/src/librustc/hir/map/collector.rs +++ b/src/librustc/hir/map/collector.rs @@ -86,7 +86,7 @@ fn alloc_hir_dep_nodes( ) -> (DepNodeIndex, DepNodeIndex) { let sig = dep_graph .input_task( - def_path_hash.to_dep_node(DepKind::Hir), + DepNode::from_def_path_hash(def_path_hash, DepKind::Hir), &mut *hcx, HirItemLike { item_like: &item_like, hash_bodies: false }, ) @@ -94,7 +94,7 @@ fn alloc_hir_dep_nodes( let (full, hash) = input_dep_node_and_hash( dep_graph, hcx, - def_path_hash.to_dep_node(DepKind::HirBody), + DepNode::from_def_path_hash(def_path_hash, DepKind::HirBody), HirItemLike { item_like: &item_like, hash_bodies: true }, ); hir_body_nodes.push((def_path_hash, hash)); diff --git a/src/librustc/hir/map/definitions.rs b/src/librustc/hir/map/definitions.rs index e1b5ec041db..50117f73d48 100644 --- a/src/librustc/hir/map/definitions.rs +++ b/src/librustc/hir/map/definitions.rs @@ -6,7 +6,6 @@ use rustc_ast::ast; use rustc_ast::node_id::NodeMap; -use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::StableHasher; use rustc_hir as hir; @@ -17,10 +16,11 @@ use rustc_span::hygiene::ExpnId; use rustc_span::symbol::{sym, Symbol}; use rustc_span::Span; -use std::borrow::Borrow; use std::fmt::Write; use std::hash::Hash; +pub use rustc_hir::def_id::DefPathHash; + /// The `DefPathTable` maps `DefIndex`es to `DefKey`s and vice versa. /// Internally the `DefPathTable` holds a tree of `DefKey`s, where each `DefKey` /// stores the `DefIndex` of its parent. @@ -282,28 +282,6 @@ pub enum DefPathData { ImplTrait, } -#[derive( - Copy, - Clone, - Hash, - PartialEq, - Eq, - PartialOrd, - Ord, - Debug, - RustcEncodable, - RustcDecodable, - HashStable -)] -pub struct DefPathHash(pub Fingerprint); - -impl Borrow for DefPathHash { - #[inline] - fn borrow(&self) -> &Fingerprint { - &self.0 - } -} - impl Definitions { pub fn def_path_table(&self) -> &DefPathTable { &self.table diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 106475e5e13..f7990ca0149 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -562,15 +562,7 @@ impl<'hir> Map<'hir> { where V: ItemLikeVisitor<'hir>, { - let hir_id = self.as_local_hir_id(module).unwrap(); - - // Read the module so we'll be re-executed if new items - // appear immediately under in the module. If some new item appears - // in some nested item in the module, we'll be re-executed due to reads - // in the expect_* calls the loops below - self.read(hir_id); - - let module = &self.krate.modules[&hir_id]; + let module = self.tcx.hir_module_items(module); for id in &module.items { visitor.visit_item(self.expect_item(*id)); @@ -639,7 +631,7 @@ impl<'hir> Map<'hir> { if self.dep_graph.is_fully_enabled() { let hir_id_owner = hir_id.owner; let def_path_hash = self.definitions.def_path_hash(hir_id_owner); - self.dep_graph.read(def_path_hash.to_dep_node(DepKind::HirBody)); + self.dep_graph.read(DepNode::from_def_path_hash(def_path_hash, DepKind::HirBody)); } self.find_entry(hir_id).and_then(|x| x.parent_node()).unwrap_or(hir_id) diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index d7999ee5f51..f113efa6300 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -114,6 +114,12 @@ pub fn provide(providers: &mut Providers<'_>) { hir_to_node_id: early.hir_to_node_id, }) }; + providers.hir_module_items = |tcx, id| { + assert_eq!(id.krate, LOCAL_CRATE); + let hir = tcx.hir(); + let module = hir.as_local_hir_id(id).unwrap(); + &hir.untracked_krate().modules[&module] + }; providers.hir_owner = |tcx, id| { assert_eq!(id.krate, LOCAL_CRATE); *tcx.hir().map.owner_map.get(&id.index).unwrap() diff --git a/src/librustc/ich/hcx.rs b/src/librustc/ich/hcx.rs index 5a49d9a1f1a..c15d54745a1 100644 --- a/src/librustc/ich/hcx.rs +++ b/src/librustc/ich/hcx.rs @@ -7,7 +7,7 @@ use crate::ty::{fast_reject, TyCtxt}; use rustc_ast::ast; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; -use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::Lrc; use rustc_hir as hir; use rustc_hir::def_id::{DefId, DefIndex}; @@ -197,19 +197,6 @@ impl<'a> StableHashingContextProvider<'a> for StableHashingContext<'a> { impl<'a> crate::dep_graph::DepGraphSafe for StableHashingContext<'a> {} -impl<'a> ToStableHashKey> for hir::HirId { - type KeyType = (DefPathHash, hir::ItemLocalId); - - #[inline] - fn to_stable_hash_key( - &self, - hcx: &StableHashingContext<'a>, - ) -> (DefPathHash, hir::ItemLocalId) { - let def_path_hash = hcx.local_def_path_hash(self.owner); - (def_path_hash, self.local_id) - } -} - impl<'a> HashStable> for ast::NodeId { fn hash_stable(&self, _: &mut StableHashingContext<'a>, _: &mut StableHasher) { panic!("Node IDs should not appear in incremental state"); diff --git a/src/librustc/ich/impls_hir.rs b/src/librustc/ich/impls_hir.rs index 1015ffdbf28..06bfd782b59 100644 --- a/src/librustc/ich/impls_hir.rs +++ b/src/librustc/ich/impls_hir.rs @@ -6,7 +6,7 @@ use crate::ich::{Fingerprint, NodeIdHashingMode, StableHashingContext}; use rustc_attr as attr; use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; use rustc_hir as hir; -use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX}; +use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX}; use smallvec::SmallVec; use std::mem; @@ -114,6 +114,11 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> { self.node_id_hashing_mode = prev_hash_node_ids; } + + #[inline] + fn local_def_path_hash(&self, def_index: DefIndex) -> DefPathHash { + self.local_def_path_hash(def_index) + } } impl<'a> ToStableHashKey> for DefId { diff --git a/src/librustc/query/mod.rs b/src/librustc/query/mod.rs index b843bc17d2c..2a8e34ddc25 100644 --- a/src/librustc/query/mod.rs +++ b/src/librustc/query/mod.rs @@ -61,6 +61,10 @@ rustc_queries! { desc { "index HIR" } } + query hir_module_items(key: DefId) -> &'tcx hir::ModuleItems { + eval_always + } + query hir_owner(key: DefId) -> &'tcx HirOwner<'tcx> { eval_always } diff --git a/src/librustc_hir/hir.rs b/src/librustc_hir/hir.rs index 65bb4b9a6a1..88cc8639331 100644 --- a/src/librustc_hir/hir.rs +++ b/src/librustc_hir/hir.rs @@ -597,7 +597,7 @@ pub struct WhereEqPredicate<'hir> { pub rhs_ty: &'hir Ty<'hir>, } -#[derive(RustcEncodable, RustcDecodable, Debug)] +#[derive(RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] pub struct ModuleItems { // Use BTreeSets here so items are in the same order as in the // list of all items in Crate diff --git a/src/librustc_hir/stable_hash_impls.rs b/src/librustc_hir/stable_hash_impls.rs index 9756edc3f4d..7ca2bfded3c 100644 --- a/src/librustc_hir/stable_hash_impls.rs +++ b/src/librustc_hir/stable_hash_impls.rs @@ -1,10 +1,11 @@ -use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; use crate::hir::{ BodyId, Expr, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem, TraitItemId, Ty, VisibilityKind, }; -use crate::hir_id::HirId; +use crate::hir_id::{HirId, ItemLocalId}; +use rustc_span::def_id::{DefIndex, DefPathHash}; /// Requirements for a `StableHashingContext` to be used in this crate. /// This is a hack to allow using the `HashStable_Generic` derive macro @@ -20,6 +21,35 @@ pub trait HashStableContext: fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher); fn hash_hir_visibility_kind(&mut self, _: &VisibilityKind<'_>, hasher: &mut StableHasher); fn hash_hir_item_like(&mut self, f: F); + fn local_def_path_hash(&self, def_index: DefIndex) -> DefPathHash; +} + +impl ToStableHashKey for HirId { + type KeyType = (DefPathHash, ItemLocalId); + + #[inline] + fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) { + let def_path_hash = hcx.local_def_path_hash(self.owner); + (def_path_hash, self.local_id) + } +} + +impl ToStableHashKey for TraitItemId { + type KeyType = (DefPathHash, ItemLocalId); + + #[inline] + fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) { + self.hir_id.to_stable_hash_key(hcx) + } +} + +impl ToStableHashKey for ImplItemId { + type KeyType = (DefPathHash, ItemLocalId); + + #[inline] + fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) { + self.hir_id.to_stable_hash_key(hcx) + } } impl HashStable for HirId { diff --git a/src/librustc_incremental/assert_dep_graph.rs b/src/librustc_incremental/assert_dep_graph.rs index 5f186f0e1b2..7686a507ff5 100644 --- a/src/librustc_incremental/assert_dep_graph.rs +++ b/src/librustc_incremental/assert_dep_graph.rs @@ -120,7 +120,7 @@ impl IfThisChanged<'tcx> { if attr.check_name(sym::rustc_if_this_changed) { let dep_node_interned = self.argument(attr); let dep_node = match dep_node_interned { - None => def_path_hash.to_dep_node(DepKind::Hir), + None => DepNode::from_def_path_hash(def_path_hash, DepKind::Hir), Some(n) => match DepNode::from_label_string(&n.as_str(), def_path_hash) { Ok(n) => n, Err(()) => { diff --git a/src/librustc_metadata/rmeta/decoder.rs b/src/librustc_metadata/rmeta/decoder.rs index 1d8eb0cde46..7126f86c326 100644 --- a/src/librustc_metadata/rmeta/decoder.rs +++ b/src/librustc_metadata/rmeta/decoder.rs @@ -4,7 +4,7 @@ use crate::creader::CrateMetadataRef; use crate::rmeta::table::{FixedSizeEncoding, Table}; use crate::rmeta::*; -use rustc::dep_graph::{self, DepNodeIndex}; +use rustc::dep_graph::{self, DepNode, DepNodeIndex}; use rustc::hir::exports::Export; use rustc::hir::map::definitions::DefPathTable; use rustc::hir::map::{DefKey, DefPath, DefPathData, DefPathHash}; @@ -1607,7 +1607,8 @@ impl CrateMetadata { // would always write the same value. let def_path_hash = self.def_path_hash(CRATE_DEF_INDEX); - let dep_node = def_path_hash.to_dep_node(dep_graph::DepKind::CrateMetadata); + let dep_node = + DepNode::from_def_path_hash(def_path_hash, dep_graph::DepKind::CrateMetadata); dep_node_index = tcx.dep_graph.dep_node_index_of(&dep_node); assert!(dep_node_index != DepNodeIndex::INVALID); diff --git a/src/librustc_span/def_id.rs b/src/librustc_span/def_id.rs index 66cdf46bd4e..a2944782e91 100644 --- a/src/librustc_span/def_id.rs +++ b/src/librustc_span/def_id.rs @@ -1,8 +1,11 @@ use crate::HashStableContext; +use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::AtomicRef; use rustc_index::vec::Idx; +use rustc_macros::HashStable_Generic; use rustc_serialize::{Decoder, Encoder}; +use std::borrow::Borrow; use std::fmt; use std::{u32, u64}; @@ -102,6 +105,28 @@ impl ::std::fmt::Debug for CrateNum { } } +#[derive( + Copy, + Clone, + Hash, + PartialEq, + Eq, + PartialOrd, + Ord, + Debug, + RustcEncodable, + RustcDecodable, + HashStable_Generic +)] +pub struct DefPathHash(pub Fingerprint); + +impl Borrow for DefPathHash { + #[inline] + fn borrow(&self) -> &Fingerprint { + &self.0 + } +} + rustc_index::newtype_index! { /// A DefIndex is an index into the hir-map for a crate, identifying a /// particular definition. It should really be considered an interned