Skip to content

Commit 21386e1

Browse files
committed
Collect the new maps
1 parent cfa1d4e commit 21386e1

File tree

5 files changed

+67
-9
lines changed

5 files changed

+67
-9
lines changed

src/librustc/arena.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ macro_rules! arena_types {
161161
[] type_binding: rustc_hir::TypeBinding<$tcx>,
162162
[] variant: rustc_hir::Variant<$tcx>,
163163
[] where_predicate: rustc_hir::WherePredicate<$tcx>,
164+
165+
// HIR query types
166+
[] hir_owner: rustc::hir::HirOwner<$tcx>,
167+
[] hir_owner_items: rustc::hir::HirOwnerItems<$tcx>,
164168
], $tcx);
165169
)
166170
}

src/librustc/hir/map/collector.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use crate::arena::Arena;
12
use crate::dep_graph::{DepGraph, DepKind, DepNode, DepNodeIndex};
23
use crate::hir::map::definitions::{self, DefPathHash};
34
use crate::hir::map::{Entry, HirEntryMap, Map};
5+
use crate::hir::{HirItem, HirOwner, HirOwnerItems};
46
use crate::ich::StableHashingContext;
57
use crate::middle::cstore::CrateStore;
68
use rustc_ast::ast::NodeId;
@@ -22,12 +24,17 @@ use std::iter::repeat;
2224

2325
/// A visitor that walks over the HIR and collects `Node`s into a HIR map.
2426
pub(super) struct NodeCollector<'a, 'hir> {
27+
arena: &'hir Arena<'hir>,
28+
2529
/// The crate
2630
krate: &'hir Crate<'hir>,
2731

2832
/// Source map
2933
source_map: &'a SourceMap,
3034

35+
owner_map: FxHashMap<DefIndex, &'hir HirOwner<'hir>>,
36+
owner_items_map: FxHashMap<DefIndex, &'hir mut HirOwnerItems<'hir>>,
37+
3138
/// The node map
3239
map: HirEntryMap<'hir>,
3340
/// The parent of this node
@@ -112,6 +119,7 @@ fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(Symbol, Fingerprint, Svh)> {
112119
impl<'a, 'hir> NodeCollector<'a, 'hir> {
113120
pub(super) fn root(
114121
sess: &'a Session,
122+
arena: &'hir Arena<'hir>,
115123
krate: &'hir Crate<'hir>,
116124
dep_graph: &'a DepGraph,
117125
definitions: &'a definitions::Definitions,
@@ -161,6 +169,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
161169
}
162170

163171
let mut collector = NodeCollector {
172+
arena,
164173
krate,
165174
source_map: sess.source_map(),
166175
map: IndexVec::from_elem_n(IndexVec::new(), definitions.def_index_count()),
@@ -174,6 +183,8 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
174183
hir_to_node_id,
175184
hcx,
176185
hir_body_nodes,
186+
owner_map: FxHashMap::default(),
187+
owner_items_map: FxHashMap::default(),
177188
};
178189
collector.insert_entry(
179190
hir::CRATE_HIR_ID,
@@ -192,7 +203,12 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
192203
crate_disambiguator: CrateDisambiguator,
193204
cstore: &dyn CrateStore,
194205
commandline_args_hash: u64,
195-
) -> (HirEntryMap<'hir>, Svh) {
206+
) -> (
207+
HirEntryMap<'hir>,
208+
FxHashMap<DefIndex, &'hir HirOwner<'hir>>,
209+
FxHashMap<DefIndex, &'hir mut HirOwnerItems<'hir>>,
210+
Svh,
211+
) {
196212
self.hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
197213

198214
let node_hashes = self.hir_body_nodes.iter().fold(
@@ -229,13 +245,36 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
229245
let crate_hash: Fingerprint = stable_hasher.finish();
230246

231247
let svh = Svh::new(crate_hash.to_smaller_hash());
232-
(self.map, svh)
248+
(self.map, self.owner_map, self.owner_items_map, svh)
233249
}
234250

235251
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>) {
252+
let i = id.local_id.as_u32() as usize;
253+
254+
let owner = HirOwner { parent: entry.parent, node: entry.node };
255+
256+
let arena = self.arena;
257+
258+
let items = self.owner_items_map.entry(id.owner).or_insert_with(|| {
259+
arena.alloc(HirOwnerItems { items: IndexVec::new(), bodies: FxHashMap::default() })
260+
});
261+
262+
if i == 0 {
263+
self.owner_map.insert(id.owner, self.arena.alloc(owner));
264+
// FIXME: feature(impl_trait_in_bindings) broken and trigger this assert
265+
//assert!(self.owner_map.insert(id.owner, self.arena.alloc(owner)).is_none());
266+
} else {
267+
let len = items.items.len();
268+
if i >= len {
269+
items.items.extend(repeat(None).take(i - len + 1));
270+
}
271+
assert_eq!(entry.parent.owner, id.owner);
272+
items.items[id.local_id] =
273+
Some(HirItem { parent: entry.parent.local_id, node: entry.node });
274+
}
275+
236276
debug!("hir_map: {:?} => {:?}", id, entry);
237277
let local_map = &mut self.map[id.owner];
238-
let i = id.local_id.as_u32() as usize;
239278
let len = local_map.len();
240279
if i >= len {
241280
local_map.extend(repeat(None).take(i - len + 1));

src/librustc/hir/map/mod.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ pub use self::definitions::{
33
DefKey, DefPath, DefPathData, DefPathHash, Definitions, DisambiguatedDefPathData,
44
};
55

6+
use crate::arena::Arena;
67
use crate::dep_graph::{DepGraph, DepKind, DepNode, DepNodeIndex};
8+
use crate::hir::{HirOwner, HirOwnerItems};
79
use crate::middle::cstore::CrateStoreDyn;
810
use crate::ty::query::Providers;
911
use rustc_ast::ast::{self, Name, NodeId};
@@ -145,6 +147,9 @@ pub struct Map<'hir> {
145147
/// The SVH of the local crate.
146148
pub crate_hash: Svh,
147149

150+
pub(super) owner_map: FxHashMap<DefIndex, &'hir HirOwner<'hir>>,
151+
pub(super) owner_items_map: FxHashMap<DefIndex, &'hir HirOwnerItems<'hir>>,
152+
148153
map: HirEntryMap<'hir>,
149154

150155
definitions: Definitions,
@@ -1201,6 +1206,7 @@ impl Named for ImplItem<'_> {
12011206

12021207
pub fn map_crate<'hir>(
12031208
sess: &rustc_session::Session,
1209+
arena: &'hir Arena<'hir>,
12041210
cstore: &CrateStoreDyn,
12051211
krate: &'hir Crate<'hir>,
12061212
dep_graph: DepGraph,
@@ -1215,19 +1221,28 @@ pub fn map_crate<'hir>(
12151221
.map(|(node_id, &hir_id)| (hir_id, node_id))
12161222
.collect();
12171223

1218-
let (map, crate_hash) = {
1224+
let (map, owner_map, owner_items_map, crate_hash) = {
12191225
let hcx = crate::ich::StableHashingContext::new(sess, krate, &definitions, cstore);
12201226

12211227
let mut collector =
1222-
NodeCollector::root(sess, krate, &dep_graph, &definitions, &hir_to_node_id, hcx);
1228+
NodeCollector::root(sess, arena, krate, &dep_graph, &definitions, &hir_to_node_id, hcx);
12231229
intravisit::walk_crate(&mut collector, krate);
12241230

12251231
let crate_disambiguator = sess.local_crate_disambiguator();
12261232
let cmdline_args = sess.opts.dep_tracking_hash();
12271233
collector.finalize_and_compute_crate_hash(crate_disambiguator, cstore, cmdline_args)
12281234
};
12291235

1230-
let map = Map { krate, dep_graph, crate_hash, map, hir_to_node_id, definitions };
1236+
let map = Map {
1237+
krate,
1238+
dep_graph,
1239+
crate_hash,
1240+
map,
1241+
owner_map,
1242+
owner_items_map: owner_items_map.into_iter().map(|(k, v)| (k, &*v)).collect(),
1243+
hir_to_node_id,
1244+
definitions,
1245+
};
12311246

12321247
sess.time("validate_HIR_map", || {
12331248
hir_id_validator::check_crate(&map, sess);

src/librustc/hir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ pub struct HirOwner<'tcx> {
2424
node: Node<'tcx>,
2525
}
2626

27-
#[derive(HashStable)]
27+
#[derive(HashStable, Clone)]
2828
pub struct HirItem<'tcx> {
2929
parent: ItemLocalId,
3030
node: Node<'tcx>,
3131
}
3232

3333
#[derive(HashStable)]
3434
pub struct HirOwnerItems<'tcx> {
35-
owner: &'tcx HirOwner<'tcx>,
35+
//owner: &'tcx HirOwner<'tcx>,
3636
items: IndexVec<ItemLocalId, Option<HirItem<'tcx>>>,
3737
bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
3838
}

src/librustc_interface/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ pub fn create_global_ctxt<'tcx>(
716716
let defs = mem::take(&mut resolver_outputs.definitions);
717717

718718
// Construct the HIR map.
719-
let hir_map = map::map_crate(sess, &*resolver_outputs.cstore, krate, dep_graph, defs);
719+
let hir_map = map::map_crate(sess, &**arena, &*resolver_outputs.cstore, krate, dep_graph, defs);
720720

721721
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
722722

0 commit comments

Comments
 (0)