Skip to content

Commit 18bffdb

Browse files
committed
Move parenting info to index_hir.
1 parent 99d3798 commit 18bffdb

File tree

6 files changed

+54
-42
lines changed

6 files changed

+54
-42
lines changed

compiler/rustc_middle/src/hir/map/collector.rs

+29-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::arena::Arena;
22
use crate::hir::map::{HirOwnerData, Map};
3-
use crate::hir::{Owner, OwnerNodes, ParentedNode};
3+
use crate::hir::{IndexedHir, Owner, OwnerNodes, ParentedNode};
44
use crate::ich::StableHashingContext;
55
use rustc_data_structures::fingerprint::Fingerprint;
66
use rustc_data_structures::fx::FxHashMap;
@@ -29,6 +29,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
2929
source_map: &'a SourceMap,
3030

3131
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
32+
parenting: FxHashMap<LocalDefId, HirId>,
3233

3334
/// The parent of this node
3435
parent_node: hir::HirId,
@@ -109,6 +110,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
109110
map: (0..definitions.def_index_count())
110111
.map(|_| HirOwnerData { signature: None, with_bodies: None })
111112
.collect(),
113+
parenting: FxHashMap::default(),
112114
};
113115
collector.insert_entry(
114116
hir::CRATE_HIR_ID,
@@ -119,15 +121,13 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
119121
collector
120122
}
121123

122-
pub(super) fn finalize_and_compute_crate_hash(
123-
mut self,
124-
) -> IndexVec<LocalDefId, HirOwnerData<'hir>> {
124+
pub(super) fn finalize_and_compute_crate_hash(mut self) -> IndexedHir<'hir> {
125125
// Insert bodies into the map
126126
for (id, body) in self.krate.bodies.iter() {
127127
let bodies = &mut self.map[id.hir_id.owner].with_bodies.as_mut().unwrap().bodies;
128128
assert!(bodies.insert(id.hir_id.local_id, body).is_none());
129129
}
130-
self.map
130+
IndexedHir { map: self.map, parenting: self.parenting }
131131
}
132132

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

154154
debug_assert!(data.signature.is_none());
155-
data.signature =
156-
Some(self.arena.alloc(Owner { parent: entry.parent, node: entry.node }));
155+
data.signature = Some(self.arena.alloc(Owner { node: entry.node }));
157156

158157
let dk_parent = self.definitions.def_key(id.owner).parent;
159158
if let Some(dk_parent) = dk_parent {
@@ -165,6 +164,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
165164
id.owner, dk_parent, entry.parent,
166165
)
167166
}
167+
168+
debug_assert_eq!(self.parenting.get(&id.owner), Some(&entry.parent));
168169
}
169170
} else {
170171
assert_eq!(entry.parent.owner, id.owner);
@@ -234,6 +235,22 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
234235
f(self, hash);
235236
self.current_dep_node_owner = prev_owner;
236237
}
238+
239+
fn insert_nested(&mut self, item: LocalDefId) {
240+
#[cfg(debug_assertions)]
241+
{
242+
let dk_parent = self.definitions.def_key(item).parent.unwrap();
243+
let dk_parent = LocalDefId { local_def_index: dk_parent };
244+
let dk_parent = self.definitions.local_def_id_to_hir_id(dk_parent);
245+
debug_assert_eq!(
246+
dk_parent.owner, self.parent_node.owner,
247+
"Different parents for {:?}",
248+
item
249+
)
250+
}
251+
252+
assert_eq!(self.parenting.insert(item, self.parent_node), None);
253+
}
237254
}
238255

239256
impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
@@ -249,18 +266,22 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
249266

250267
fn visit_nested_item(&mut self, item: ItemId) {
251268
debug!("visit_nested_item: {:?}", item);
269+
self.insert_nested(item.def_id);
252270
self.visit_item(self.krate.item(item));
253271
}
254272

255273
fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {
274+
self.insert_nested(item_id.def_id);
256275
self.visit_trait_item(self.krate.trait_item(item_id));
257276
}
258277

259278
fn visit_nested_impl_item(&mut self, item_id: ImplItemId) {
279+
self.insert_nested(item_id.def_id);
260280
self.visit_impl_item(self.krate.impl_item(item_id));
261281
}
262282

263283
fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) {
284+
self.insert_nested(foreign_id.def_id);
264285
self.visit_foreign_item(self.krate.foreign_item(foreign_id));
265286
}
266287

@@ -448,6 +469,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
448469
self.definitions.local_def_id_to_hir_id(LocalDefId { local_def_index })
449470
});
450471
self.with_parent(parent, |this| {
472+
this.insert_nested(macro_def.def_id);
451473
this.with_dep_node_owner(macro_def.def_id, macro_def, |this, hash| {
452474
this.insert_with_hash(
453475
macro_def.span,

compiler/rustc_middle/src/hir/map/mod.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ impl<'hir> Map<'hir> {
285285

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

298298
pub fn get_parent_node(&self, hir_id: HirId) -> HirId {
299-
self.find_parent_node(hir_id).unwrap()
299+
self.find_parent_node(hir_id).unwrap_or(CRATE_HIR_ID)
300300
}
301301

302302
/// 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
934934

935935
let _prof_timer = tcx.sess.prof.generic_activity("build_hir_map");
936936

937-
let map = {
938-
let hcx = tcx.create_stable_hashing_context();
939-
940-
let mut collector =
941-
NodeCollector::root(tcx.sess, &**tcx.arena, tcx.untracked_crate, &tcx.definitions, hcx);
942-
intravisit::walk_crate(&mut collector, tcx.untracked_crate);
943-
944-
collector.finalize_and_compute_crate_hash()
945-
};
937+
let hcx = tcx.create_stable_hashing_context();
938+
let mut collector =
939+
NodeCollector::root(tcx.sess, &**tcx.arena, tcx.untracked_crate, &tcx.definitions, hcx);
940+
intravisit::walk_crate(&mut collector, tcx.untracked_crate);
946941

947-
tcx.arena.alloc(IndexedHir { map })
942+
let map = collector.finalize_and_compute_crate_hash();
943+
tcx.arena.alloc(map)
948944
}
949945

950946
pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {

compiler/rustc_middle/src/hir/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,18 @@ struct HirOwnerData<'hir> {
2828
#[derive(Debug)]
2929
pub struct IndexedHir<'hir> {
3030
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
31+
parenting: FxHashMap<LocalDefId, HirId>,
3132
}
3233

3334
#[derive(Debug)]
3435
pub struct Owner<'tcx> {
35-
parent: HirId,
3636
node: Node<'tcx>,
3737
}
3838

3939
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for Owner<'tcx> {
4040
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
41-
let Owner { node, parent } = self;
42-
hcx.while_hashing_hir_bodies(false, |hcx| {
43-
parent.hash_stable(hcx, hasher);
44-
node.hash_stable(hcx, hasher)
45-
});
41+
let Owner { node } = self;
42+
hcx.while_hashing_hir_bodies(false, |hcx| node.hash_stable(hcx, hasher));
4643
}
4744
}
4845

src/test/incremental/hashes/inherent_impls.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl Foo {
2323
}
2424

2525
#[cfg(not(cfail1))]
26-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes,associated_item_def_ids")]
26+
#[rustc_clean(cfg="cfail2", except="hir_owner,associated_item_def_ids")]
2727
#[rustc_clean(cfg="cfail3")]
2828
impl Foo {
2929
#[rustc_clean(cfg="cfail3")]
@@ -85,7 +85,7 @@ impl Foo {
8585
}
8686

8787
#[cfg(not(cfail1))]
88-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
88+
#[rustc_clean(cfg="cfail2", except="hir_owner")]
8989
#[rustc_clean(cfg="cfail3")]
9090
impl Foo {
9191
#[rustc_clean(cfg="cfail2", except="associated_item,hir_owner,hir_owner_nodes")]
@@ -100,7 +100,7 @@ impl Foo {
100100
}
101101

102102
#[cfg(not(cfail1))]
103-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
103+
#[rustc_clean(cfg="cfail2", except="hir_owner")]
104104
#[rustc_clean(cfg="cfail3")]
105105
impl Foo {
106106
#[rustc_dirty(cfg="cfail2", except="type_of,predicates_of,promoted_mir")]
@@ -135,7 +135,7 @@ impl Foo {
135135
}
136136

137137
#[cfg(not(cfail1))]
138-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes,associated_item_def_ids")]
138+
#[rustc_clean(cfg="cfail2", except="hir_owner,associated_item_def_ids")]
139139
#[rustc_clean(cfg="cfail3")]
140140
impl Foo {
141141
#[rustc_clean(cfg="cfail2")]
@@ -468,7 +468,7 @@ impl Bar<u32> {
468468
}
469469

470470
#[cfg(not(cfail1))]
471-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
471+
#[rustc_clean(cfg="cfail2", except="hir_owner")]
472472
#[rustc_clean(cfg="cfail3")]
473473
impl Bar<u64> {
474474
#[rustc_clean(cfg="cfail2", except="fn_sig,optimized_mir,typeck")]

src/test/incremental/hashes/type_defs.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
type ChangePrimitiveType = i32;
2525

2626
#[cfg(not(cfail1))]
27-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
27+
#[rustc_clean(cfg="cfail2", except="hir_owner")]
2828
#[rustc_clean(cfg="cfail3")]
2929
type ChangePrimitiveType = i64;
3030

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

3737
#[cfg(not(cfail1))]
38-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
38+
#[rustc_clean(cfg="cfail2", except="hir_owner")]
3939
#[rustc_clean(cfg="cfail3")]
4040
type ChangeMutability = &'static mut i32;
4141

@@ -60,7 +60,7 @@ struct Struct2;
6060
type ChangeTypeStruct = Struct1;
6161

6262
#[cfg(not(cfail1))]
63-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
63+
#[rustc_clean(cfg="cfail2", except="hir_owner")]
6464
#[rustc_clean(cfg="cfail3")]
6565
type ChangeTypeStruct = Struct2;
6666

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

7373
#[cfg(not(cfail1))]
74-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
74+
#[rustc_clean(cfg="cfail2", except="hir_owner")]
7575
#[rustc_clean(cfg="cfail3")]
7676
type ChangeTypeTuple = (u32, i64);
7777

@@ -91,7 +91,7 @@ enum Enum2 {
9191
type ChangeTypeEnum = Enum1;
9292

9393
#[cfg(not(cfail1))]
94-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
94+
#[rustc_clean(cfg="cfail2", except="hir_owner")]
9595
#[rustc_clean(cfg="cfail3")]
9696
type ChangeTypeEnum = Enum2;
9797

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

104104
#[cfg(not(cfail1))]
105-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
105+
#[rustc_clean(cfg="cfail2", except="hir_owner")]
106106
#[rustc_clean(cfg="cfail3")]
107107
type AddTupleField = (i32, i64, i16);
108108

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

115115
#[cfg(not(cfail1))]
116-
#[rustc_clean(cfg="cfail2", except="hir_owner,hir_owner_nodes")]
116+
#[rustc_clean(cfg="cfail2", except="hir_owner")]
117117
#[rustc_clean(cfg="cfail3")]
118118
type ChangeNestedTupleField = (i32, (i64, i8));
119119

src/test/incremental/ich_nested_items.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ pub fn foo() {
1414
#[cfg(cfail1)]
1515
pub fn baz() {} // order is different...
1616

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

0 commit comments

Comments
 (0)