@@ -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