@@ -174,15 +174,6 @@ pub struct Map<'hir> {
174
174
/// The SVH of the local crate.
175
175
pub crate_hash : Svh ,
176
176
177
- /// `NodeId`s are sequential integers from 0, so we can be
178
- /// super-compact by storing them in a vector. Not everything with
179
- /// a `NodeId` is in the map, but empirically the occupancy is about
180
- /// 75-80%, so there's not too much overhead (certainly less than
181
- /// a hashmap, since they (at the time of writing) have a maximum
182
- /// of 75% occupancy).
183
- ///
184
- /// Also, indexing is pretty quick when you've got a vector and
185
- /// plain old integers.
186
177
map : FxHashMap < HirId , Entry < ' hir > > ,
187
178
188
179
definitions : & ' hir Definitions ,
@@ -199,13 +190,7 @@ impl<'hir> Map<'hir> {
199
190
/// otherwise have had access to those contents, and hence needs a
200
191
/// read recorded). If the function just returns a DefId or
201
192
/// NodeId, no actual content was returned, so no read is needed.
202
- pub fn read ( & self , id : NodeId ) {
203
- let hir_id = self . node_to_hir_id ( id) ;
204
- self . read_by_hir_id ( hir_id) ;
205
- }
206
-
207
- // FIXME(@ljedrz): replace the NodeId variant
208
- pub fn read_by_hir_id ( & self , hir_id : HirId ) {
193
+ pub fn read ( & self , hir_id : HirId ) {
209
194
if let Some ( entry) = self . map . get ( & hir_id) {
210
195
self . dep_graph . read_index ( entry. dep_node ) ;
211
196
} else {
@@ -223,17 +208,12 @@ impl<'hir> Map<'hir> {
223
208
self . definitions . def_key ( def_id. index )
224
209
}
225
210
226
- pub fn def_path_from_id ( & self , id : NodeId ) -> Option < DefPath > {
227
- self . opt_local_def_id ( id) . map ( |def_id| {
211
+ pub fn def_path_from_hir_id ( & self , id : HirId ) -> Option < DefPath > {
212
+ self . opt_local_def_id_from_hir_id ( id) . map ( |def_id| {
228
213
self . def_path ( def_id)
229
214
} )
230
215
}
231
216
232
- // FIXME(@ljedrz): replace the NodeId variant
233
- pub fn def_path_from_hir_id ( & self , id : HirId ) -> DefPath {
234
- self . def_path ( self . local_def_id_from_hir_id ( id) )
235
- }
236
-
237
217
pub fn def_path ( & self , def_id : DefId ) -> DefPath {
238
218
assert ! ( def_id. is_local( ) ) ;
239
219
self . definitions . def_path ( def_id. index )
@@ -411,23 +391,23 @@ impl<'hir> Map<'hir> {
411
391
}
412
392
413
393
pub fn trait_item ( & self , id : TraitItemId ) -> & ' hir TraitItem {
414
- self . read_by_hir_id ( id. hir_id ) ;
394
+ self . read ( id. hir_id ) ;
415
395
416
396
// N.B., intentionally bypass `self.forest.krate()` so that we
417
397
// do not trigger a read of the whole krate here
418
398
self . forest . krate . trait_item ( id)
419
399
}
420
400
421
401
pub fn impl_item ( & self , id : ImplItemId ) -> & ' hir ImplItem {
422
- self . read_by_hir_id ( id. hir_id ) ;
402
+ self . read ( id. hir_id ) ;
423
403
424
404
// N.B., intentionally bypass `self.forest.krate()` so that we
425
405
// do not trigger a read of the whole krate here
426
406
self . forest . krate . impl_item ( id)
427
407
}
428
408
429
409
pub fn body ( & self , id : BodyId ) -> & ' hir Body {
430
- self . read_by_hir_id ( id. hir_id ) ;
410
+ self . read ( id. hir_id ) ;
431
411
432
412
// N.B., intentionally bypass `self.forest.krate()` so that we
433
413
// do not trigger a read of the whole krate here
@@ -560,7 +540,7 @@ impl<'hir> Map<'hir> {
560
540
pub fn get_module ( & self , module : DefId ) -> ( & ' hir Mod , Span , HirId )
561
541
{
562
542
let hir_id = self . as_local_hir_id ( module) . unwrap ( ) ;
563
- self . read_by_hir_id ( hir_id) ;
543
+ self . read ( hir_id) ;
564
544
match self . find_entry ( hir_id) . unwrap ( ) . node {
565
545
Node :: Item ( & Item {
566
546
span,
@@ -575,13 +555,15 @@ impl<'hir> Map<'hir> {
575
555
pub fn visit_item_likes_in_module < V > ( & self , module : DefId , visitor : & mut V )
576
556
where V : ItemLikeVisitor < ' hir >
577
557
{
578
- let node_id = self . as_local_node_id ( module) . unwrap ( ) ;
558
+ let hir_id = self . as_local_hir_id ( module) . unwrap ( ) ;
579
559
580
560
// Read the module so we'll be re-executed if new items
581
561
// appear immediately under in the module. If some new item appears
582
562
// in some nested item in the module, we'll be re-executed due to reads
583
563
// in the expect_* calls the loops below
584
- self . read ( node_id) ;
564
+ self . read ( hir_id) ;
565
+
566
+ let node_id = self . hir_to_node_id [ & hir_id] ;
585
567
586
568
let module = & self . forest . krate . modules [ & node_id] ;
587
569
@@ -659,7 +641,7 @@ impl<'hir> Map<'hir> {
659
641
}
660
642
} ) ;
661
643
if result. is_some ( ) {
662
- self . read_by_hir_id ( hir_id) ;
644
+ self . read ( hir_id) ;
663
645
}
664
646
result
665
647
}
@@ -893,7 +875,7 @@ impl<'hir> Map<'hir> {
893
875
if let Entry {
894
876
node : Node :: Item ( Item { node : ItemKind :: ForeignMod ( ref nm) , .. } ) , .. } = entry
895
877
{
896
- self . read_by_hir_id ( hir_id) ; // reveals some of the content of a node
878
+ self . read ( hir_id) ; // reveals some of the content of a node
897
879
return nm. abi ;
898
880
}
899
881
}
@@ -1001,7 +983,7 @@ impl<'hir> Map<'hir> {
1001
983
1002
984
// FIXME(@ljedrz): replace the NodeId variant
1003
985
pub fn attrs_by_hir_id ( & self , id : HirId ) -> & ' hir [ ast:: Attribute ] {
1004
- self . read_by_hir_id ( id) ; // reveals attributes on the node
986
+ self . read ( id) ; // reveals attributes on the node
1005
987
let attrs = match self . find_entry ( id) . map ( |entry| entry. node ) {
1006
988
Some ( Node :: Local ( l) ) => Some ( & l. attrs [ ..] ) ,
1007
989
Some ( Node :: Item ( i) ) => Some ( & i. attrs [ ..] ) ,
@@ -1046,7 +1028,7 @@ impl<'hir> Map<'hir> {
1046
1028
1047
1029
// FIXME(@ljedrz): replace the NodeId variant
1048
1030
pub fn span_by_hir_id ( & self , hir_id : HirId ) -> Span {
1049
- self . read_by_hir_id ( hir_id) ; // reveals span from node
1031
+ self . read ( hir_id) ; // reveals span from node
1050
1032
match self . find_entry ( hir_id) . map ( |entry| entry. node ) {
1051
1033
Some ( Node :: Item ( item) ) => item. span ,
1052
1034
Some ( Node :: ForeignItem ( foreign_item) ) => foreign_item. span ,
@@ -1088,7 +1070,7 @@ impl<'hir> Map<'hir> {
1088
1070
}
1089
1071
1090
1072
pub fn node_to_string ( & self , id : NodeId ) -> String {
1091
- node_id_to_string ( self , id , true )
1073
+ hir_id_to_string ( self , self . node_to_hir_id ( id ) , true )
1092
1074
}
1093
1075
1094
1076
// FIXME(@ljedrz): replace the NodeId variant
@@ -1097,7 +1079,7 @@ impl<'hir> Map<'hir> {
1097
1079
}
1098
1080
1099
1081
pub fn node_to_user_string ( & self , id : NodeId ) -> String {
1100
- node_id_to_string ( self , id , false )
1082
+ hir_id_to_string ( self , self . node_to_hir_id ( id ) , false )
1101
1083
}
1102
1084
1103
1085
// FIXME(@ljedrz): replace the NodeId variant
@@ -1316,18 +1298,18 @@ impl<'a> print::State<'a> {
1316
1298
}
1317
1299
}
1318
1300
1319
- fn node_id_to_string ( map : & Map < ' _ > , id : NodeId , include_id : bool ) -> String {
1320
- let id_str = format ! ( " (id ={})" , id) ;
1301
+ fn hir_id_to_string ( map : & Map < ' _ > , id : HirId , include_id : bool ) -> String {
1302
+ let id_str = format ! ( " (hir_id ={})" , id) ;
1321
1303
let id_str = if include_id { & id_str[ ..] } else { "" } ;
1322
1304
1323
1305
let path_str = || {
1324
1306
// This functionality is used for debugging, try to use TyCtxt to get
1325
1307
// the user-friendly path, otherwise fall back to stringifying DefPath.
1326
1308
crate :: ty:: tls:: with_opt ( |tcx| {
1327
1309
if let Some ( tcx) = tcx {
1328
- let def_id = map. local_def_id ( id) ;
1310
+ let def_id = map. local_def_id_from_hir_id ( id) ;
1329
1311
tcx. def_path_str ( def_id)
1330
- } else if let Some ( path) = map. def_path_from_id ( id) {
1312
+ } else if let Some ( path) = map. def_path_from_hir_id ( id) {
1331
1313
path. data . into_iter ( ) . map ( |elem| {
1332
1314
elem. data . to_string ( )
1333
1315
} ) . collect :: < Vec < _ > > ( ) . join ( "::" )
@@ -1337,7 +1319,7 @@ fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
1337
1319
} )
1338
1320
} ;
1339
1321
1340
- match map. find ( id) {
1322
+ match map. find_by_hir_id ( id) {
1341
1323
Some ( Node :: Item ( item) ) => {
1342
1324
let item_str = match item. node {
1343
1325
ItemKind :: ExternCrate ( ..) => "extern crate" ,
@@ -1398,40 +1380,40 @@ fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
1398
1380
path_str( ) , id_str)
1399
1381
}
1400
1382
Some ( Node :: AnonConst ( _) ) => {
1401
- format ! ( "const {}{}" , map. node_to_pretty_string ( id) , id_str)
1383
+ format ! ( "const {}{}" , map. hir_to_pretty_string ( id) , id_str)
1402
1384
}
1403
1385
Some ( Node :: Expr ( _) ) => {
1404
- format ! ( "expr {}{}" , map. node_to_pretty_string ( id) , id_str)
1386
+ format ! ( "expr {}{}" , map. hir_to_pretty_string ( id) , id_str)
1405
1387
}
1406
1388
Some ( Node :: Stmt ( _) ) => {
1407
- format ! ( "stmt {}{}" , map. node_to_pretty_string ( id) , id_str)
1389
+ format ! ( "stmt {}{}" , map. hir_to_pretty_string ( id) , id_str)
1408
1390
}
1409
1391
Some ( Node :: PathSegment ( _) ) => {
1410
- format ! ( "path segment {}{}" , map. node_to_pretty_string ( id) , id_str)
1392
+ format ! ( "path segment {}{}" , map. hir_to_pretty_string ( id) , id_str)
1411
1393
}
1412
1394
Some ( Node :: Ty ( _) ) => {
1413
- format ! ( "type {}{}" , map. node_to_pretty_string ( id) , id_str)
1395
+ format ! ( "type {}{}" , map. hir_to_pretty_string ( id) , id_str)
1414
1396
}
1415
1397
Some ( Node :: TraitRef ( _) ) => {
1416
- format ! ( "trait_ref {}{}" , map. node_to_pretty_string ( id) , id_str)
1398
+ format ! ( "trait_ref {}{}" , map. hir_to_pretty_string ( id) , id_str)
1417
1399
}
1418
1400
Some ( Node :: Binding ( _) ) => {
1419
- format ! ( "local {}{}" , map. node_to_pretty_string ( id) , id_str)
1401
+ format ! ( "local {}{}" , map. hir_to_pretty_string ( id) , id_str)
1420
1402
}
1421
1403
Some ( Node :: Pat ( _) ) => {
1422
- format ! ( "pat {}{}" , map. node_to_pretty_string ( id) , id_str)
1404
+ format ! ( "pat {}{}" , map. hir_to_pretty_string ( id) , id_str)
1423
1405
}
1424
1406
Some ( Node :: Block ( _) ) => {
1425
- format ! ( "block {}{}" , map. node_to_pretty_string ( id) , id_str)
1407
+ format ! ( "block {}{}" , map. hir_to_pretty_string ( id) , id_str)
1426
1408
}
1427
1409
Some ( Node :: Local ( _) ) => {
1428
- format ! ( "local {}{}" , map. node_to_pretty_string ( id) , id_str)
1410
+ format ! ( "local {}{}" , map. hir_to_pretty_string ( id) , id_str)
1429
1411
}
1430
1412
Some ( Node :: Ctor ( ..) ) => {
1431
1413
format ! ( "ctor {}{}" , path_str( ) , id_str)
1432
1414
}
1433
1415
Some ( Node :: Lifetime ( _) ) => {
1434
- format ! ( "lifetime {}{}" , map. node_to_pretty_string ( id) , id_str)
1416
+ format ! ( "lifetime {}{}" , map. hir_to_pretty_string ( id) , id_str)
1435
1417
}
1436
1418
Some ( Node :: GenericParam ( ref param) ) => {
1437
1419
format ! ( "generic_param {:?}{}" , param, id_str)
@@ -1447,12 +1429,6 @@ fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
1447
1429
}
1448
1430
}
1449
1431
1450
- // FIXME(@ljedrz): replace the NodeId variant
1451
- fn hir_id_to_string ( map : & Map < ' _ > , id : HirId , include_id : bool ) -> String {
1452
- let node_id = map. hir_to_node_id ( id) ;
1453
- node_id_to_string ( map, node_id, include_id)
1454
- }
1455
-
1456
1432
pub fn def_kind ( tcx : TyCtxt < ' _ , ' _ , ' _ > , def_id : DefId ) -> Option < DefKind > {
1457
1433
if let Some ( node_id) = tcx. hir ( ) . as_local_node_id ( def_id) {
1458
1434
tcx. hir ( ) . def_kind ( node_id)
0 commit comments