1- use crate :: hir:: map:: Map ;
2- use crate :: hir:: { IndexedHir , OwnerNodes , ParentedNode } ;
31use rustc_data_structures:: fx:: FxHashMap ;
42use rustc_hir as hir;
53use rustc_hir:: def_id:: LocalDefId ;
@@ -12,16 +10,16 @@ use rustc_span::source_map::SourceMap;
1210use rustc_span:: { Span , DUMMY_SP } ;
1311
1412use std:: iter:: repeat;
13+ use tracing:: debug;
1514
1615/// A visitor that walks over the HIR and collects `Node`s into a HIR map.
1716pub ( super ) struct NodeCollector < ' a , ' hir > {
18- /// The crate
19- krate : & ' hir Crate < ' hir > ,
20-
2117 /// Source map
2218 source_map : & ' a SourceMap ,
19+ bodies : & ' a IndexVec < ItemLocalId , Option < & ' hir Body < ' hir > > > ,
2320
24- nodes : OwnerNodes < ' hir > ,
21+ /// Outputs
22+ nodes : IndexVec < ItemLocalId , Option < ParentedNode < ' hir > > > ,
2523 parenting : FxHashMap < LocalDefId , ItemLocalId > ,
2624
2725 /// The parent of this node
@@ -42,28 +40,21 @@ fn insert_vec_map<K: Idx, V: Clone>(map: &mut IndexVec<K, Option<V>>, k: K, v: V
4240 map[ k] = Some ( v) ;
4341}
4442
45- pub ( super ) fn collect < ' a , ' hir : ' a > (
46- sess : & ' a Session ,
47- krate : & ' hir Crate < ' hir > ,
48- definitions : & ' a definitions:: Definitions ,
49- owner : LocalDefId ,
50- ) -> Option < IndexedHir < ' hir > > {
51- let info = krate. owners . get ( owner) ?. as_ref ( ) ?;
52- let item = info. node ;
43+ pub ( super ) fn index_hir < ' hir > (
44+ sess : & Session ,
45+ definitions : & definitions:: Definitions ,
46+ item : hir:: OwnerNode < ' hir > ,
47+ bodies : & IndexVec < ItemLocalId , Option < & ' hir Body < ' hir > > > ,
48+ ) -> ( IndexVec < ItemLocalId , Option < ParentedNode < ' hir > > > , FxHashMap < LocalDefId , ItemLocalId > ) {
5349 let mut nodes = IndexVec :: new ( ) ;
5450 nodes. push ( Some ( ParentedNode { parent : ItemLocalId :: new ( 0 ) , node : item. into ( ) } ) ) ;
5551 let mut collector = NodeCollector {
56- krate,
5752 source_map : sess. source_map ( ) ,
58- owner,
59- parent_node : ItemLocalId :: new ( 0 ) ,
6053 definitions,
61- nodes : OwnerNodes {
62- hash : info. hash ,
63- node_hash : info. node_hash ,
64- nodes,
65- bodies : & info. bodies ,
66- } ,
54+ owner : item. def_id ( ) ,
55+ parent_node : ItemLocalId :: new ( 0 ) ,
56+ nodes,
57+ bodies,
6758 parenting : FxHashMap :: default ( ) ,
6859 } ;
6960
@@ -75,7 +66,7 @@ pub(super) fn collect<'a, 'hir: 'a>(
7566 OwnerNode :: ForeignItem ( item) => collector. visit_foreign_item ( item) ,
7667 } ;
7768
78- Some ( IndexedHir { nodes : collector. nodes , parenting : collector. parenting } )
69+ ( collector. nodes , collector. parenting )
7970}
8071
8172impl < ' a , ' hir > NodeCollector < ' a , ' hir > {
@@ -87,17 +78,11 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
8778 // owner of that node.
8879 if cfg ! ( debug_assertions) {
8980 if hir_id. owner != self . owner {
90- let node_str = match self . definitions . opt_hir_id_to_local_def_id ( hir_id) {
91- Some ( def_id) => self . definitions . def_path ( def_id) . to_string_no_crate_verbose ( ) ,
92- None => format ! ( "{:?}" , node) ,
93- } ;
94-
95- span_bug ! (
96- span,
97- "inconsistent DepNode at `{:?}` for `{}`: \
81+ panic ! (
82+ "inconsistent DepNode at `{:?}` for `{:?}`: \
9883 current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
9984 self . source_map. span_to_diagnostic_string( span) ,
100- node_str ,
85+ node ,
10186 self . definitions. def_path( self . owner) . to_string_no_crate_verbose( ) ,
10287 self . owner,
10388 self . definitions. def_path( hir_id. owner) . to_string_no_crate_verbose( ) ,
@@ -107,7 +92,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
10792 }
10893
10994 insert_vec_map (
110- & mut self . nodes . nodes ,
95+ & mut self . nodes ,
11196 hir_id. local_id ,
11297 ParentedNode { parent : self . parent_node , node : node } ,
11398 ) ;
@@ -122,18 +107,12 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
122107 }
123108
124109 fn insert_nested ( & mut self , item : LocalDefId ) {
125- let dk_parent = self . definitions . def_key ( item) . parent . unwrap ( ) ;
126- let dk_parent = LocalDefId { local_def_index : dk_parent } ;
127- let dk_parent = self . definitions . local_def_id_to_hir_id ( dk_parent) ;
128- debug_assert_eq ! ( dk_parent. owner, self . owner, "Different parents for {:?}" , item) ;
129- if dk_parent. local_id != self . parent_node {
130- self . parenting . insert ( item, self . parent_node ) ;
131- }
110+ self . parenting . insert ( item, self . parent_node ) ;
132111 }
133112}
134113
135114impl < ' a , ' hir > Visitor < ' hir > for NodeCollector < ' a , ' hir > {
136- type Map = Map < ' hir > ;
115+ type Map = ! ;
137116
138117 /// Because we want to track parent items and so forth, enable
139118 /// deep walking so that we walk nested items in the context of
@@ -161,8 +140,8 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
161140 }
162141
163142 fn visit_nested_body ( & mut self , id : BodyId ) {
164- let body = self . krate . body ( id) ;
165143 debug_assert_eq ! ( id. hir_id. owner, self . owner) ;
144+ let body = self . bodies [ id. hir_id . local_id ] . unwrap ( ) ;
166145 self . visit_body ( body) ;
167146 }
168147
0 commit comments