Skip to content

Commit

Permalink
Move parenting info to index_hir.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Apr 29, 2021
1 parent 99d3798 commit 18bffdb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 42 deletions.
36 changes: 29 additions & 7 deletions compiler/rustc_middle/src/hir/map/collector.rs
@@ -1,6 +1,6 @@
use crate::arena::Arena;
use crate::hir::map::{HirOwnerData, Map};
use crate::hir::{Owner, OwnerNodes, ParentedNode};
use crate::hir::{IndexedHir, Owner, OwnerNodes, ParentedNode};
use crate::ich::StableHashingContext;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
Expand Down Expand Up @@ -29,6 +29,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
source_map: &'a SourceMap,

map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
parenting: FxHashMap<LocalDefId, HirId>,

/// The parent of this node
parent_node: hir::HirId,
Expand Down Expand Up @@ -109,6 +110,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
map: (0..definitions.def_index_count())
.map(|_| HirOwnerData { signature: None, with_bodies: None })
.collect(),
parenting: FxHashMap::default(),
};
collector.insert_entry(
hir::CRATE_HIR_ID,
Expand All @@ -119,15 +121,13 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
collector
}

pub(super) fn finalize_and_compute_crate_hash(
mut self,
) -> IndexVec<LocalDefId, HirOwnerData<'hir>> {
pub(super) fn finalize_and_compute_crate_hash(mut self) -> IndexedHir<'hir> {
// Insert bodies into the map
for (id, body) in self.krate.bodies.iter() {
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());
}
self.map
IndexedHir { map: self.map, parenting: self.parenting }
}

fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>, hash: Fingerprint) {
Expand All @@ -152,8 +152,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
nodes.hash = hash;

debug_assert!(data.signature.is_none());
data.signature =
Some(self.arena.alloc(Owner { parent: entry.parent, node: entry.node }));
data.signature = Some(self.arena.alloc(Owner { node: entry.node }));

let dk_parent = self.definitions.def_key(id.owner).parent;
if let Some(dk_parent) = dk_parent {
Expand All @@ -165,6 +164,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
id.owner, dk_parent, entry.parent,
)
}

debug_assert_eq!(self.parenting.get(&id.owner), Some(&entry.parent));
}
} else {
assert_eq!(entry.parent.owner, id.owner);
Expand Down Expand Up @@ -234,6 +235,22 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
f(self, hash);
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> {
Expand All @@ -249,18 +266,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {

fn visit_nested_item(&mut self, item: ItemId) {
debug!("visit_nested_item: {:?}", item);
self.insert_nested(item.def_id);
self.visit_item(self.krate.item(item));
}

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));
}

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));
}

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));
}

Expand Down Expand Up @@ -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.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.insert_with_hash(
macro_def.span,
Expand Down
22 changes: 9 additions & 13 deletions compiler/rustc_middle/src/hir/map/mod.rs
Expand Up @@ -285,8 +285,8 @@ impl<'hir> Map<'hir> {

pub fn find_parent_node(&self, id: HirId) -> Option<HirId> {
if id.local_id == ItemLocalId::from_u32(0) {
let owner = self.tcx.hir_owner(id.owner)?;
Some(owner.parent)
let index = self.tcx.index_hir(LOCAL_CRATE);
index.parenting.get(&id.owner).copied()
} else {
let owner = self.tcx.hir_owner_nodes(id.owner)?;
let node = owner.nodes[id.local_id].as_ref()?;
Expand All @@ -296,7 +296,7 @@ impl<'hir> Map<'hir> {
}

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.
Expand Down Expand Up @@ -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 map = {
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);

collector.finalize_and_compute_crate_hash()
};
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);

tcx.arena.alloc(IndexedHir { map })
let map = collector.finalize_and_compute_crate_hash();
tcx.arena.alloc(map)
}

pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_middle/src/hir/mod.rs
Expand Up @@ -28,21 +28,18 @@ struct HirOwnerData<'hir> {
#[derive(Debug)]
pub struct IndexedHir<'hir> {
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
parenting: FxHashMap<LocalDefId, HirId>,
}

#[derive(Debug)]
pub struct Owner<'tcx> {
parent: HirId,
node: Node<'tcx>,
}

impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
let Owner { node, parent } = self;
hcx.while_hashing_hir_bodies(false, |hcx| {
parent.hash_stable(hcx, hasher);
node.hash_stable(hcx, hasher)
});
let Owner { node } = self;
hcx.while_hashing_hir_bodies(false, |hcx| node.hash_stable(hcx, hasher));
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/test/incremental/hashes/inherent_impls.rs
Expand Up @@ -23,7 +23,7 @@ impl Foo {
}

#[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")]
impl Foo {
#[rustc_clean(cfg="cfail3")]
Expand Down Expand Up @@ -85,7 +85,7 @@ impl Foo {
}

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")]
impl Foo {
#[rustc_clean(cfg="cfail2", except="associated_item,hir_owner,hir_owner_nodes")]
Expand All @@ -100,7 +100,7 @@ impl Foo {
}

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")]
impl Foo {
#[rustc_dirty(cfg="cfail2", except="type_of,predicates_of,promoted_mir")]
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Foo {
}

#[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")]
impl Foo {
#[rustc_clean(cfg="cfail2")]
Expand Down Expand Up @@ -468,7 +468,7 @@ impl Bar<u32> {
}

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")]
impl Bar<u64> {
#[rustc_clean(cfg="cfail2", except="fn_sig,optimized_mir,typeck")]
Expand Down
14 changes: 7 additions & 7 deletions src/test/incremental/hashes/type_defs.rs
Expand Up @@ -24,7 +24,7 @@
type ChangePrimitiveType = i32;

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")]
type ChangePrimitiveType = i64;

Expand All @@ -35,7 +35,7 @@ type ChangePrimitiveType = i64;
type ChangeMutability = &'static i32;

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")]
type ChangeMutability = &'static mut i32;

Expand All @@ -60,7 +60,7 @@ struct Struct2;
type ChangeTypeStruct = Struct1;

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")]
type ChangeTypeStruct = Struct2;

Expand All @@ -71,7 +71,7 @@ type ChangeTypeStruct = Struct2;
type ChangeTypeTuple = (u32, u64);

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")]
type ChangeTypeTuple = (u32, i64);

Expand All @@ -91,7 +91,7 @@ enum Enum2 {
type ChangeTypeEnum = Enum1;

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")]
type ChangeTypeEnum = Enum2;

Expand All @@ -102,7 +102,7 @@ type ChangeTypeEnum = Enum2;
type AddTupleField = (i32, i64);

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")]
type AddTupleField = (i32, i64, i16);

Expand All @@ -113,7 +113,7 @@ type AddTupleField = (i32, i64, i16);
type ChangeNestedTupleField = (i32, (i64, i16));

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
#[rustc_clean(cfg="cfail2", except="hir_owner")]
#[rustc_clean(cfg="cfail3")]
type ChangeNestedTupleField = (i32, (i64, i8));

Expand Down
5 changes: 1 addition & 4 deletions src/test/incremental/ich_nested_items.rs
Expand Up @@ -14,10 +14,7 @@ pub fn foo() {
#[cfg(cfail1)]
pub fn baz() {} // order is different...

// FIXME: Make "hir_owner" use `rustc_clean` here. Currently "hir_owner" includes a reference to
// 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", cfg = "cfail2")]
#[rustc_clean(label = "hir_owner_nodes", cfg = "cfail2")]
pub fn bar() {} // but that doesn't matter.

Expand Down

0 comments on commit 18bffdb

Please sign in to comment.