1
Fork 0

Move parenting info to index_hir.

This commit is contained in:
Camille GILLOT 2021-03-05 20:09:33 +01:00
parent 99d3798b6c
commit 18bffdb10e
6 changed files with 54 additions and 42 deletions

View file

@ -1,6 +1,6 @@
use crate::arena::Arena; use crate::arena::Arena;
use crate::hir::map::{HirOwnerData, Map}; use crate::hir::map::{HirOwnerData, Map};
use crate::hir::{Owner, OwnerNodes, ParentedNode}; use crate::hir::{IndexedHir, Owner, OwnerNodes, ParentedNode};
use crate::ich::StableHashingContext; use crate::ich::StableHashingContext;
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
@ -29,6 +29,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
source_map: &'a SourceMap, source_map: &'a SourceMap,
map: IndexVec<LocalDefId, HirOwnerData<'hir>>, map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
parenting: FxHashMap<LocalDefId, HirId>,
/// The parent of this node /// The parent of this node
parent_node: hir::HirId, parent_node: hir::HirId,
@ -109,6 +110,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
map: (0..definitions.def_index_count()) map: (0..definitions.def_index_count())
.map(|_| HirOwnerData { signature: None, with_bodies: None }) .map(|_| HirOwnerData { signature: None, with_bodies: None })
.collect(), .collect(),
parenting: FxHashMap::default(),
}; };
collector.insert_entry( collector.insert_entry(
hir::CRATE_HIR_ID, hir::CRATE_HIR_ID,
@ -119,15 +121,13 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
collector collector
} }
pub(super) fn finalize_and_compute_crate_hash( pub(super) fn finalize_and_compute_crate_hash(mut self) -> IndexedHir<'hir> {
mut self,
) -> IndexVec<LocalDefId, HirOwnerData<'hir>> {
// Insert bodies into the map // Insert bodies into the map
for (id, body) in self.krate.bodies.iter() { for (id, body) in self.krate.bodies.iter() {
let bodies = &mut self.map[id.hir_id.owner].with_bodies.as_mut().unwrap().bodies; let bodies = &mut self.map[id.hir_id.owner].with_bodies.as_mut().unwrap().bodies;
assert!(bodies.insert(id.hir_id.local_id, body).is_none()); assert!(bodies.insert(id.hir_id.local_id, body).is_none());
} }
self.map IndexedHir { map: self.map, parenting: self.parenting }
} }
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>, hash: Fingerprint) { fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>, hash: Fingerprint) {
@ -152,8 +152,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
nodes.hash = hash; nodes.hash = hash;
debug_assert!(data.signature.is_none()); debug_assert!(data.signature.is_none());
data.signature = data.signature = Some(self.arena.alloc(Owner { node: entry.node }));
Some(self.arena.alloc(Owner { parent: entry.parent, node: entry.node }));
let dk_parent = self.definitions.def_key(id.owner).parent; let dk_parent = self.definitions.def_key(id.owner).parent;
if let Some(dk_parent) = dk_parent { if let Some(dk_parent) = dk_parent {
@ -165,6 +164,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
id.owner, dk_parent, entry.parent, id.owner, dk_parent, entry.parent,
) )
} }
debug_assert_eq!(self.parenting.get(&id.owner), Some(&entry.parent));
} }
} else { } else {
assert_eq!(entry.parent.owner, id.owner); assert_eq!(entry.parent.owner, id.owner);
@ -234,6 +235,22 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
f(self, hash); f(self, hash);
self.current_dep_node_owner = prev_owner; self.current_dep_node_owner = prev_owner;
} }
fn insert_nested(&mut self, item: LocalDefId) {
#[cfg(debug_assertions)]
{
let dk_parent = self.definitions.def_key(item).parent.unwrap();
let dk_parent = LocalDefId { local_def_index: dk_parent };
let dk_parent = self.definitions.local_def_id_to_hir_id(dk_parent);
debug_assert_eq!(
dk_parent.owner, self.parent_node.owner,
"Different parents for {:?}",
item
)
}
assert_eq!(self.parenting.insert(item, self.parent_node), None);
}
} }
impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
@ -249,18 +266,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
fn visit_nested_item(&mut self, item: ItemId) { fn visit_nested_item(&mut self, item: ItemId) {
debug!("visit_nested_item: {:?}", item); debug!("visit_nested_item: {:?}", item);
self.insert_nested(item.def_id);
self.visit_item(self.krate.item(item)); self.visit_item(self.krate.item(item));
} }
fn visit_nested_trait_item(&mut self, item_id: TraitItemId) { fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {
self.insert_nested(item_id.def_id);
self.visit_trait_item(self.krate.trait_item(item_id)); self.visit_trait_item(self.krate.trait_item(item_id));
} }
fn visit_nested_impl_item(&mut self, item_id: ImplItemId) { fn visit_nested_impl_item(&mut self, item_id: ImplItemId) {
self.insert_nested(item_id.def_id);
self.visit_impl_item(self.krate.impl_item(item_id)); self.visit_impl_item(self.krate.impl_item(item_id));
} }
fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) { fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) {
self.insert_nested(foreign_id.def_id);
self.visit_foreign_item(self.krate.foreign_item(foreign_id)); self.visit_foreign_item(self.krate.foreign_item(foreign_id));
} }
@ -448,6 +469,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
self.definitions.local_def_id_to_hir_id(LocalDefId { local_def_index }) self.definitions.local_def_id_to_hir_id(LocalDefId { local_def_index })
}); });
self.with_parent(parent, |this| { self.with_parent(parent, |this| {
this.insert_nested(macro_def.def_id);
this.with_dep_node_owner(macro_def.def_id, macro_def, |this, hash| { this.with_dep_node_owner(macro_def.def_id, macro_def, |this, hash| {
this.insert_with_hash( this.insert_with_hash(
macro_def.span, macro_def.span,

View file

@ -285,8 +285,8 @@ impl<'hir> Map<'hir> {
pub fn find_parent_node(&self, id: HirId) -> Option<HirId> { pub fn find_parent_node(&self, id: HirId) -> Option<HirId> {
if id.local_id == ItemLocalId::from_u32(0) { if id.local_id == ItemLocalId::from_u32(0) {
let owner = self.tcx.hir_owner(id.owner)?; let index = self.tcx.index_hir(LOCAL_CRATE);
Some(owner.parent) index.parenting.get(&id.owner).copied()
} else { } else {
let owner = self.tcx.hir_owner_nodes(id.owner)?; let owner = self.tcx.hir_owner_nodes(id.owner)?;
let node = owner.nodes[id.local_id].as_ref()?; let node = owner.nodes[id.local_id].as_ref()?;
@ -296,7 +296,7 @@ impl<'hir> Map<'hir> {
} }
pub fn get_parent_node(&self, hir_id: HirId) -> HirId { pub fn get_parent_node(&self, hir_id: HirId) -> HirId {
self.find_parent_node(hir_id).unwrap() self.find_parent_node(hir_id).unwrap_or(CRATE_HIR_ID)
} }
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found. /// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
@ -934,17 +934,13 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx Indexe
let _prof_timer = tcx.sess.prof.generic_activity("build_hir_map"); let _prof_timer = tcx.sess.prof.generic_activity("build_hir_map");
let map = { let hcx = tcx.create_stable_hashing_context();
let hcx = tcx.create_stable_hashing_context(); let mut collector =
NodeCollector::root(tcx.sess, &**tcx.arena, tcx.untracked_crate, &tcx.definitions, hcx);
intravisit::walk_crate(&mut collector, tcx.untracked_crate);
let mut collector = let map = collector.finalize_and_compute_crate_hash();
NodeCollector::root(tcx.sess, &**tcx.arena, tcx.untracked_crate, &tcx.definitions, hcx); tcx.arena.alloc(map)
intravisit::walk_crate(&mut collector, tcx.untracked_crate);
collector.finalize_and_compute_crate_hash()
};
tcx.arena.alloc(IndexedHir { map })
} }
pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh { pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {

View file

@ -28,21 +28,18 @@ struct HirOwnerData<'hir> {
#[derive(Debug)] #[derive(Debug)]
pub struct IndexedHir<'hir> { pub struct IndexedHir<'hir> {
map: IndexVec<LocalDefId, HirOwnerData<'hir>>, map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
parenting: FxHashMap<LocalDefId, HirId>,
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Owner<'tcx> { pub struct Owner<'tcx> {
parent: HirId,
node: Node<'tcx>, node: Node<'tcx>,
} }
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> { impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
let Owner { node, parent } = self; let Owner { node } = self;
hcx.while_hashing_hir_bodies(false, |hcx| { hcx.while_hashing_hir_bodies(false, |hcx| node.hash_stable(hcx, hasher));
parent.hash_stable(hcx, hasher);
node.hash_stable(hcx, hasher)
});
} }
} }

View file

@ -23,7 +23,7 @@ impl Foo {
} }
#[cfg(not(cfail1))] #[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes,associated_item_def_ids")] #[rustc_clean(cfg="cfail2", except="hir_owner,associated_item_def_ids")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
impl Foo { impl Foo {
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
@ -85,7 +85,7 @@ impl Foo {
} }
#[cfg(not(cfail1))] #[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")] #[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
impl Foo { impl Foo {
#[rustc_clean(cfg="cfail2", except="associated_item,hir_owner,hir_owner_nodes")] #[rustc_clean(cfg="cfail2", except="associated_item,hir_owner,hir_owner_nodes")]
@ -100,7 +100,7 @@ impl Foo {
} }
#[cfg(not(cfail1))] #[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")] #[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
impl Foo { impl Foo {
#[rustc_dirty(cfg="cfail2", except="type_of,predicates_of,promoted_mir")] #[rustc_dirty(cfg="cfail2", except="type_of,predicates_of,promoted_mir")]
@ -135,7 +135,7 @@ impl Foo {
} }
#[cfg(not(cfail1))] #[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes,associated_item_def_ids")] #[rustc_clean(cfg="cfail2", except="hir_owner,associated_item_def_ids")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
impl Foo { impl Foo {
#[rustc_clean(cfg="cfail2")] #[rustc_clean(cfg="cfail2")]
@ -468,7 +468,7 @@ impl Bar<u32> {
} }
#[cfg(not(cfail1))] #[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")] #[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
impl Bar<u64> { impl Bar<u64> {
#[rustc_clean(cfg="cfail2", except="fn_sig,optimized_mir,typeck")] #[rustc_clean(cfg="cfail2", except="fn_sig,optimized_mir,typeck")]

View file

@ -24,7 +24,7 @@
type ChangePrimitiveType = i32; type ChangePrimitiveType = i32;
#[cfg(not(cfail1))] #[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")] #[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
type ChangePrimitiveType = i64; type ChangePrimitiveType = i64;
@ -35,7 +35,7 @@ type ChangePrimitiveType = i64;
type ChangeMutability = &'static i32; type ChangeMutability = &'static i32;
#[cfg(not(cfail1))] #[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")] #[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
type ChangeMutability = &'static mut i32; type ChangeMutability = &'static mut i32;
@ -60,7 +60,7 @@ struct Struct2;
type ChangeTypeStruct = Struct1; type ChangeTypeStruct = Struct1;
#[cfg(not(cfail1))] #[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")] #[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
type ChangeTypeStruct = Struct2; type ChangeTypeStruct = Struct2;
@ -71,7 +71,7 @@ type ChangeTypeStruct = Struct2;
type ChangeTypeTuple = (u32, u64); type ChangeTypeTuple = (u32, u64);
#[cfg(not(cfail1))] #[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")] #[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
type ChangeTypeTuple = (u32, i64); type ChangeTypeTuple = (u32, i64);
@ -91,7 +91,7 @@ enum Enum2 {
type ChangeTypeEnum = Enum1; type ChangeTypeEnum = Enum1;
#[cfg(not(cfail1))] #[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")] #[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
type ChangeTypeEnum = Enum2; type ChangeTypeEnum = Enum2;
@ -102,7 +102,7 @@ type ChangeTypeEnum = Enum2;
type AddTupleField = (i32, i64); type AddTupleField = (i32, i64);
#[cfg(not(cfail1))] #[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")] #[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
type AddTupleField = (i32, i64, i16); type AddTupleField = (i32, i64, i16);
@ -113,7 +113,7 @@ type AddTupleField = (i32, i64, i16);
type ChangeNestedTupleField = (i32, (i64, i16)); type ChangeNestedTupleField = (i32, (i64, i16));
#[cfg(not(cfail1))] #[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")] #[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")] #[rustc_clean(cfg="cfail3")]
type ChangeNestedTupleField = (i32, (i64, i8)); type ChangeNestedTupleField = (i32, (i64, i8));

View file

@ -14,10 +14,7 @@ pub fn foo() {
#[cfg(cfail1)] #[cfg(cfail1)]
pub fn baz() {} // order is different... pub fn baz() {} // order is different...
// FIXME: Make "hir_owner" use `rustc_clean` here. Currently "hir_owner" includes a reference to #[rustc_clean(label = "hir_owner", cfg = "cfail2")]
// the parent node, which is the statement holding this item. Changing the position of
// `bar` in `foo` will update that reference and make `hir_owner(bar)` dirty.
#[rustc_dirty(label = "hir_owner", cfg = "cfail2")]
#[rustc_clean(label = "hir_owner_nodes", cfg = "cfail2")] #[rustc_clean(label = "hir_owner_nodes", cfg = "cfail2")]
pub fn bar() {} // but that doesn't matter. pub fn bar() {} // but that doesn't matter.