Skip to content

Commit d33db6e

Browse files
committed
Rename HirMap to HirEntryMap and add some comments
1 parent df5a011 commit d33db6e

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/librustc/hir/map/collector.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22
use crate::dep_graph::{DepGraph, DepKind, DepNodeIndex};
33
use crate::hir;
4-
use crate::hir::map::HirMap;
4+
use crate::hir::map::HirEntryMap;
55
use crate::hir::def_id::{LOCAL_CRATE, CrateNum};
66
use crate::hir::intravisit::{Visitor, NestedVisitorMap};
77
use rustc_data_structures::svh::Svh;
@@ -28,7 +28,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
2828
source_map: &'a SourceMap,
2929

3030
/// The node map
31-
map: HirMap<'hir>,
31+
map: HirEntryMap<'hir>,
3232
/// The parent of this node
3333
parent_node: hir::HirId,
3434

@@ -178,7 +178,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
178178
crate_disambiguator: CrateDisambiguator,
179179
cstore: &dyn CrateStore,
180180
commandline_args_hash: u64)
181-
-> (HirMap<'hir>, Svh)
181+
-> (HirEntryMap<'hir>, Svh)
182182
{
183183
self.hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
184184

src/librustc/hir/map/mod.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,12 @@ impl Forest {
162162
}
163163
}
164164

165-
pub(super) type HirMap<'hir> = [Vec<Option<IndexVec<ItemLocalId, Option<Entry<'hir>>>>>; 2];
165+
/// This type is effectively a `HashMap<HirId, Entry<'hir>>`,
166+
/// but is implemented by 3 layers of arrays.
167+
/// - the outer layer is `[A; 2]` and correspond to the 2 address spaces `DefIndex`es can be in
168+
/// - then we have `A = Vec<Option<B>>` mapping a `DefIndex`'s index to a inner value
169+
/// - which is `B = IndexVec<ItemLocalId, Option<Entry<'hir>>` which finally gives you the `Entry`.
170+
pub(super) type HirEntryMap<'hir> = [Vec<Option<IndexVec<ItemLocalId, Option<Entry<'hir>>>>>; 2];
166171

167172
/// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s.
168173
#[derive(Clone)]
@@ -177,7 +182,7 @@ pub struct Map<'hir> {
177182
/// The SVH of the local crate.
178183
pub crate_hash: Svh,
179184

180-
map: HirMap<'hir>,
185+
map: HirEntryMap<'hir>,
181186

182187
definitions: &'hir Definitions,
183188

@@ -1011,15 +1016,25 @@ impl<'hir> Map<'hir> {
10111016

10121017
/// Returns an iterator that yields all the hir ids in the map.
10131018
fn all_ids<'a>(&'a self) -> impl Iterator<Item = HirId> + 'a {
1019+
// This code is a bit awkward because the map is implemented as 3 levels of arrays,
1020+
// see the comment on `HirEntryMap`.
10141021
let map = &self.map;
1022+
1023+
// Look at both the def index address spaces
10151024
let spaces = [DefIndexAddressSpace::Low, DefIndexAddressSpace::High].iter().cloned();
10161025
spaces.flat_map(move |space| {
1017-
map[space.index()].iter().enumerate().filter_map(|(i, local_map)| {
1026+
// Iterate over all the indices in the address space and return a reference to
1027+
// local maps and their index given that they exist.
1028+
let local_maps = map[space.index()].iter().enumerate().filter_map(|(i, local_map)| {
10181029
local_map.as_ref().map(|m| (i, m))
1019-
}).flat_map(move |(def_index, local_map)| {
1030+
});
1031+
1032+
local_maps.flat_map(move |(array_index, local_map)| {
1033+
// Iterate over each valid entry in the local map
10201034
local_map.iter_enumerated().filter_map(move |(i, entry)| entry.map(move |_| {
1035+
// Reconstruct the HirId based on the 3 indices we used to find it
10211036
HirId {
1022-
owner: DefIndex::from_array_index(def_index, space),
1037+
owner: DefIndex::from_array_index(array_index, space),
10231038
local_id: i,
10241039
}
10251040
}))

0 commit comments

Comments
 (0)