@@ -162,7 +162,12 @@ impl Forest {
162
162
}
163
163
}
164
164
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 ] ;
166
171
167
172
/// Represents a mapping from `NodeId`s to AST elements and their parent `NodeId`s.
168
173
#[ derive( Clone ) ]
@@ -177,7 +182,7 @@ pub struct Map<'hir> {
177
182
/// The SVH of the local crate.
178
183
pub crate_hash : Svh ,
179
184
180
- map : HirMap < ' hir > ,
185
+ map : HirEntryMap < ' hir > ,
181
186
182
187
definitions : & ' hir Definitions ,
183
188
@@ -1011,15 +1016,25 @@ impl<'hir> Map<'hir> {
1011
1016
1012
1017
/// Returns an iterator that yields all the hir ids in the map.
1013
1018
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`.
1014
1021
let map = & self . map ;
1022
+
1023
+ // Look at both the def index address spaces
1015
1024
let spaces = [ DefIndexAddressSpace :: Low , DefIndexAddressSpace :: High ] . iter ( ) . cloned ( ) ;
1016
1025
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) | {
1018
1029
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
1020
1034
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
1021
1036
HirId {
1022
- owner : DefIndex :: from_array_index ( def_index , space) ,
1037
+ owner : DefIndex :: from_array_index ( array_index , space) ,
1023
1038
local_id : i,
1024
1039
}
1025
1040
} ) )
0 commit comments