Skip to content

Commit 3538cb3

Browse files
committed
Only hash the Hir owner (including its bodies)
1 parent d73268b commit 3538cb3

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/librustc/hir/map/collector.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,22 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
203203
let owner = HirOwner { parent: entry.parent, node: entry.node };
204204

205205
let arena = self.arena;
206+
let krate = self.krate;
206207

207208
let items = self.owner_items_map.entry(id.owner).or_insert_with(|| {
208-
arena.alloc(HirOwnerItems { items: IndexVec::new(), bodies: FxHashMap::default() })
209+
arena.alloc(HirOwnerItems {
210+
// Insert a dummy node which will be overwritten
211+
// when we call `insert_entry` on the HIR owner.
212+
owner: Node::Crate(&krate.item),
213+
items: IndexVec::new(),
214+
bodies: FxHashMap::default(),
215+
})
209216
});
210217

211218
if i == 0 {
219+
// Overwrite the dummy node with the real HIR owner.
220+
items.owner = entry.node;
221+
212222
self.owner_map.insert(id.owner, self.arena.alloc(owner));
213223
// FIXME: feature(impl_trait_in_bindings) broken and trigger this assert
214224
//assert!(self.owner_map.insert(id.owner, self.arena.alloc(owner)).is_none());

src/librustc/hir/mod.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
pub mod exports;
66
pub mod map;
77

8+
use crate::ich::StableHashingContext;
89
use crate::ty::query::Providers;
910
use crate::ty::TyCtxt;
1011
use rustc_data_structures::cold_path;
1112
use rustc_data_structures::fx::FxHashMap;
13+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1214
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1315
use rustc_hir::print;
1416
use rustc_hir::Body;
@@ -25,19 +27,39 @@ pub struct HirOwner<'tcx> {
2527
node: Node<'tcx>,
2628
}
2729

28-
#[derive(HashStable, Clone)]
30+
#[derive(Clone)]
2931
pub struct HirItem<'tcx> {
3032
parent: ItemLocalId,
3133
node: Node<'tcx>,
3234
}
3335

34-
#[derive(HashStable)]
36+
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for HirItem<'tcx> {
37+
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
38+
let HirItem { parent, node } = self;
39+
hcx.while_hashing_hir_bodies(false, |hcx| {
40+
parent.hash_stable(hcx, hasher);
41+
node.hash_stable(hcx, hasher);
42+
});
43+
}
44+
}
45+
3546
pub struct HirOwnerItems<'tcx> {
36-
//owner: &'tcx HirOwner<'tcx>,
47+
owner: Node<'tcx>,
3748
items: IndexVec<ItemLocalId, Option<HirItem<'tcx>>>,
3849
bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
3950
}
4051

52+
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for HirOwnerItems<'tcx> {
53+
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
54+
// We ignore the `items` and `bodies` fields since these refer to information reachable
55+
// when hashing `owner` with its bodies.
56+
let HirOwnerItems { owner, items: _, bodies: _ } = *self;
57+
hcx.while_hashing_hir_bodies(true, |hcx| {
58+
owner.hash_stable(hcx, hasher);
59+
});
60+
}
61+
}
62+
4163
/// A wrapper type which allows you to access HIR.
4264
#[derive(Clone)]
4365
pub struct Hir<'tcx> {

0 commit comments

Comments
 (0)