@@ -51,7 +51,6 @@ use rustc_data_structures::thin_vec::ThinVec;
51
51
use rustc_data_structures:: sync:: Lrc ;
52
52
53
53
use std:: collections:: { BTreeSet , BTreeMap } ;
54
- use std:: fmt:: Debug ;
55
54
use std:: mem;
56
55
use smallvec:: SmallVec ;
57
56
use syntax:: attr;
@@ -82,7 +81,7 @@ pub struct LoweringContext<'a> {
82
81
resolver : & ' a mut dyn Resolver ,
83
82
84
83
/// The items being lowered are collected here.
85
- items : BTreeMap < NodeId , hir:: Item > ,
84
+ items : BTreeMap < hir :: HirId , hir:: Item > ,
86
85
87
86
trait_items : BTreeMap < hir:: TraitItemId , hir:: TraitItem > ,
88
87
impl_items : BTreeMap < hir:: ImplItemId , hir:: ImplItem > ,
@@ -321,7 +320,7 @@ enum AnonymousLifetimeMode {
321
320
PassThrough ,
322
321
}
323
322
324
- struct ImplTraitTypeIdVisitor < ' a > { ids : & ' a mut SmallVec < [ hir :: ItemId ; 1 ] > }
323
+ struct ImplTraitTypeIdVisitor < ' a > { ids : & ' a mut SmallVec < [ NodeId ; 1 ] > }
325
324
326
325
impl < ' a , ' b > Visitor < ' a > for ImplTraitTypeIdVisitor < ' b > {
327
326
fn visit_ty ( & mut self , ty : & ' a Ty ) {
@@ -330,7 +329,7 @@ impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
330
329
| TyKind :: BareFn ( _)
331
330
=> return ,
332
331
333
- TyKind :: ImplTrait ( id, _) => self . ids . push ( hir :: ItemId { id } ) ,
332
+ TyKind :: ImplTrait ( id, _) => self . ids . push ( id ) ,
334
333
_ => { } ,
335
334
}
336
335
visit:: walk_ty ( self , ty) ;
@@ -361,9 +360,40 @@ impl<'a> LoweringContext<'a> {
361
360
lctx : & ' lcx mut LoweringContext < ' interner > ,
362
361
}
363
362
363
+ impl MiscCollector < ' _ , ' _ > {
364
+ fn allocate_use_tree_hir_id_counters (
365
+ & mut self ,
366
+ tree : & UseTree ,
367
+ owner : DefIndex ,
368
+ ) {
369
+ match tree. kind {
370
+ UseTreeKind :: Simple ( _, id1, id2) => {
371
+ for & id in & [ id1, id2] {
372
+ self . lctx . resolver . definitions ( ) . create_def_with_parent (
373
+ owner,
374
+ id,
375
+ DefPathData :: Misc ,
376
+ DefIndexAddressSpace :: High ,
377
+ Mark :: root ( ) ,
378
+ tree. prefix . span ,
379
+ ) ;
380
+ self . lctx . allocate_hir_id_counter ( id) ;
381
+ }
382
+ }
383
+ UseTreeKind :: Glob => ( ) ,
384
+ UseTreeKind :: Nested ( ref trees) => {
385
+ for & ( ref use_tree, id) in trees {
386
+ let hir_id = self . lctx . allocate_hir_id_counter ( id) . hir_id ;
387
+ self . allocate_use_tree_hir_id_counters ( use_tree, hir_id. owner ) ;
388
+ }
389
+ }
390
+ }
391
+ }
392
+ }
393
+
364
394
impl < ' lcx , ' interner > Visitor < ' lcx > for MiscCollector < ' lcx , ' interner > {
365
395
fn visit_item ( & mut self , item : & ' lcx Item ) {
366
- self . lctx . allocate_hir_id_counter ( item. id , item ) ;
396
+ let hir_id = self . lctx . allocate_hir_id_counter ( item. id ) . hir_id ;
367
397
368
398
match item. node {
369
399
ItemKind :: Struct ( _, ref generics)
@@ -383,18 +413,21 @@ impl<'a> LoweringContext<'a> {
383
413
. count ( ) ;
384
414
self . lctx . type_def_lifetime_params . insert ( def_id, count) ;
385
415
}
416
+ ItemKind :: Use ( ref use_tree) => {
417
+ self . allocate_use_tree_hir_id_counters ( use_tree, hir_id. owner ) ;
418
+ }
386
419
_ => { }
387
420
}
388
421
visit:: walk_item ( self , item) ;
389
422
}
390
423
391
424
fn visit_trait_item ( & mut self , item : & ' lcx TraitItem ) {
392
- self . lctx . allocate_hir_id_counter ( item. id , item ) ;
425
+ self . lctx . allocate_hir_id_counter ( item. id ) ;
393
426
visit:: walk_trait_item ( self , item) ;
394
427
}
395
428
396
429
fn visit_impl_item ( & mut self , item : & ' lcx ImplItem ) {
397
- self . lctx . allocate_hir_id_counter ( item. id , item ) ;
430
+ self . lctx . allocate_hir_id_counter ( item. id ) ;
398
431
visit:: walk_impl_item ( self , item) ;
399
432
}
400
433
}
@@ -434,17 +467,16 @@ impl<'a> LoweringContext<'a> {
434
467
}
435
468
436
469
fn visit_item ( & mut self , item : & ' lcx Item ) {
437
- let mut item_lowered = true ;
470
+ let mut item_hir_id = None ;
438
471
self . lctx . with_hir_id_owner ( item. id , |lctx| {
439
472
if let Some ( hir_item) = lctx. lower_item ( item) {
440
- lctx. insert_item ( item. id , hir_item) ;
441
- } else {
442
- item_lowered = false ;
473
+ item_hir_id = Some ( hir_item. hir_id ) ;
474
+ lctx. insert_item ( hir_item) ;
443
475
}
444
476
} ) ;
445
477
446
- if item_lowered {
447
- let item_generics = match self . lctx . items . get ( & item . id ) . unwrap ( ) . node {
478
+ if let Some ( hir_id ) = item_hir_id {
479
+ let item_generics = match self . lctx . items . get ( & hir_id ) . unwrap ( ) . node {
448
480
hir:: ItemKind :: Impl ( _, _, _, ref generics, ..)
449
481
| hir:: ItemKind :: Trait ( _, _, ref generics, ..) => {
450
482
generics. params . clone ( )
@@ -516,20 +548,21 @@ impl<'a> LoweringContext<'a> {
516
548
}
517
549
}
518
550
519
- fn insert_item ( & mut self , id : NodeId , item : hir:: Item ) {
551
+ fn insert_item ( & mut self , item : hir:: Item ) {
552
+ let id = item. hir_id ;
553
+ // FIXME: Use debug_asset-rt
554
+ assert_eq ! ( id. local_id, hir:: ItemLocalId :: from_u32( 0 ) ) ;
520
555
self . items . insert ( id, item) ;
521
556
self . modules . get_mut ( & self . current_module ) . unwrap ( ) . items . insert ( id) ;
522
557
}
523
558
524
- fn allocate_hir_id_counter < T : Debug > ( & mut self , owner : NodeId , debug : & T ) -> LoweredNodeId {
525
- if self . item_local_id_counters . insert ( owner, 0 ) . is_some ( ) {
526
- bug ! (
527
- "Tried to allocate item_local_id_counter for {:?} twice" ,
528
- debug
529
- ) ;
530
- }
559
+ fn allocate_hir_id_counter ( & mut self , owner : NodeId ) -> LoweredNodeId {
560
+ // Setup the counter if needed
561
+ self . item_local_id_counters . entry ( owner) . or_insert ( 0 ) ;
531
562
// Always allocate the first `HirId` for the owner itself.
532
- self . lower_node_id_with_owner ( owner, owner)
563
+ let lowered = self . lower_node_id_with_owner ( owner, owner) ;
564
+ debug_assert_eq ! ( lowered. hir_id. local_id. as_u32( ) , 0 ) ;
565
+ lowered
533
566
}
534
567
535
568
fn lower_node_id_generic < F > ( & mut self , ast_node_id : NodeId , alloc_hir_id : F ) -> LoweredNodeId
@@ -1381,7 +1414,7 @@ impl<'a> LoweringContext<'a> {
1381
1414
. opt_def_index ( exist_ty_node_id)
1382
1415
. unwrap ( ) ;
1383
1416
1384
- self . allocate_hir_id_counter ( exist_ty_node_id, & "existential impl trait" ) ;
1417
+ self . allocate_hir_id_counter ( exist_ty_node_id) ;
1385
1418
1386
1419
let hir_bounds = self . with_hir_id_owner ( exist_ty_node_id, lower_bounds) ;
1387
1420
@@ -1422,10 +1455,10 @@ impl<'a> LoweringContext<'a> {
1422
1455
// Insert the item into the global list. This usually happens
1423
1456
// automatically for all AST items. But this existential type item
1424
1457
// does not actually exist in the AST.
1425
- lctx. insert_item ( exist_ty_id . node_id , exist_ty_item) ;
1458
+ lctx. insert_item ( exist_ty_item) ;
1426
1459
1427
1460
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
1428
- hir:: TyKind :: Def ( hir:: ItemId { id : exist_ty_id. node_id } , lifetimes)
1461
+ hir:: TyKind :: Def ( hir:: ItemId { id : exist_ty_id. hir_id } , lifetimes)
1429
1462
} )
1430
1463
}
1431
1464
@@ -2002,9 +2035,9 @@ impl<'a> LoweringContext<'a> {
2002
2035
)
2003
2036
}
2004
2037
2005
- fn lower_local ( & mut self , l : & Local ) -> ( hir:: Local , SmallVec < [ hir :: ItemId ; 1 ] > ) {
2038
+ fn lower_local ( & mut self , l : & Local ) -> ( hir:: Local , SmallVec < [ NodeId ; 1 ] > ) {
2006
2039
let LoweredNodeId { node_id : _, hir_id } = self . lower_node_id ( l. id ) ;
2007
- let mut ids = SmallVec :: < [ hir :: ItemId ; 1 ] > :: new ( ) ;
2040
+ let mut ids = SmallVec :: < [ NodeId ; 1 ] > :: new ( ) ;
2008
2041
if self . sess . features_untracked ( ) . impl_trait_in_bindings {
2009
2042
if let Some ( ref ty) = l. ty {
2010
2043
let mut visitor = ImplTraitTypeIdVisitor { ids : & mut ids } ;
@@ -3065,7 +3098,6 @@ impl<'a> LoweringContext<'a> {
3065
3098
}
3066
3099
}
3067
3100
3068
- let parent_def_index = self . current_hir_id_owner . last ( ) . unwrap ( ) . 0 ;
3069
3101
let mut defs = self . expect_full_def_from_use ( id) ;
3070
3102
// We want to return *something* from this function, so hold onto the first item
3071
3103
// for later.
@@ -3084,14 +3116,6 @@ impl<'a> LoweringContext<'a> {
3084
3116
seg. id = self . sess . next_node_id ( ) ;
3085
3117
}
3086
3118
let span = path. span ;
3087
- self . resolver . definitions ( ) . create_def_with_parent (
3088
- parent_def_index,
3089
- new_node_id,
3090
- DefPathData :: Misc ,
3091
- DefIndexAddressSpace :: High ,
3092
- Mark :: root ( ) ,
3093
- span) ;
3094
- self . allocate_hir_id_counter ( new_node_id, & path) ;
3095
3119
3096
3120
self . with_hir_id_owner ( new_node_id, |this| {
3097
3121
let new_id = this. lower_node_id ( new_node_id) ;
@@ -3114,7 +3138,6 @@ impl<'a> LoweringContext<'a> {
3114
3138
let vis = respan ( vis. span , vis_kind) ;
3115
3139
3116
3140
this. insert_item (
3117
- new_id. node_id ,
3118
3141
hir:: Item {
3119
3142
hir_id : new_id. hir_id ,
3120
3143
ident,
@@ -3174,8 +3197,6 @@ impl<'a> LoweringContext<'a> {
3174
3197
3175
3198
// Add all the nested `PathListItem`s to the HIR.
3176
3199
for & ( ref use_tree, id) in trees {
3177
- self . allocate_hir_id_counter ( id, & use_tree) ;
3178
-
3179
3200
let LoweredNodeId {
3180
3201
node_id : new_id,
3181
3202
hir_id : new_hir_id,
@@ -3219,7 +3240,6 @@ impl<'a> LoweringContext<'a> {
3219
3240
let vis = respan ( vis. span , vis_kind) ;
3220
3241
3221
3242
this. insert_item (
3222
- new_id,
3223
3243
hir:: Item {
3224
3244
hir_id : new_hir_id,
3225
3245
ident,
@@ -3443,43 +3463,47 @@ impl<'a> LoweringContext<'a> {
3443
3463
}
3444
3464
3445
3465
fn lower_item_id ( & mut self , i : & Item ) -> SmallVec < [ hir:: ItemId ; 1 ] > {
3446
- match i. node {
3466
+ let node_ids = match i. node {
3447
3467
ItemKind :: Use ( ref use_tree) => {
3448
- let mut vec = smallvec ! [ hir :: ItemId { id : i. id } ] ;
3468
+ let mut vec = smallvec ! [ i. id] ;
3449
3469
self . lower_item_id_use_tree ( use_tree, i. id , & mut vec) ;
3450
3470
vec
3451
3471
}
3452
3472
ItemKind :: MacroDef ( ..) => SmallVec :: new ( ) ,
3453
3473
ItemKind :: Fn ( ..) |
3454
- ItemKind :: Impl ( .., None , _, _) => smallvec ! [ hir :: ItemId { id : i. id } ] ,
3474
+ ItemKind :: Impl ( .., None , _, _) => smallvec ! [ i. id] ,
3455
3475
ItemKind :: Static ( ref ty, ..) => {
3456
- let mut ids = smallvec ! [ hir :: ItemId { id : i. id } ] ;
3476
+ let mut ids = smallvec ! [ i. id] ;
3457
3477
if self . sess . features_untracked ( ) . impl_trait_in_bindings {
3458
3478
let mut visitor = ImplTraitTypeIdVisitor { ids : & mut ids } ;
3459
3479
visitor. visit_ty ( ty) ;
3460
3480
}
3461
3481
ids
3462
3482
} ,
3463
3483
ItemKind :: Const ( ref ty, ..) => {
3464
- let mut ids = smallvec ! [ hir :: ItemId { id : i. id } ] ;
3484
+ let mut ids = smallvec ! [ i. id] ;
3465
3485
if self . sess . features_untracked ( ) . impl_trait_in_bindings {
3466
3486
let mut visitor = ImplTraitTypeIdVisitor { ids : & mut ids } ;
3467
3487
visitor. visit_ty ( ty) ;
3468
3488
}
3469
3489
ids
3470
3490
} ,
3471
- _ => smallvec ! [ hir:: ItemId { id: i. id } ] ,
3472
- }
3491
+ _ => smallvec ! [ i. id] ,
3492
+ } ;
3493
+
3494
+ node_ids. into_iter ( ) . map ( |node_id| hir:: ItemId {
3495
+ id : self . allocate_hir_id_counter ( node_id) . hir_id
3496
+ } ) . collect ( )
3473
3497
}
3474
3498
3475
3499
fn lower_item_id_use_tree ( & mut self ,
3476
3500
tree : & UseTree ,
3477
3501
base_id : NodeId ,
3478
- vec : & mut SmallVec < [ hir :: ItemId ; 1 ] > )
3502
+ vec : & mut SmallVec < [ NodeId ; 1 ] > )
3479
3503
{
3480
3504
match tree. kind {
3481
3505
UseTreeKind :: Nested ( ref nested_vec) => for & ( ref nested, id) in nested_vec {
3482
- vec. push ( hir :: ItemId { id } ) ;
3506
+ vec. push ( id ) ;
3483
3507
self . lower_item_id_use_tree ( nested, id, vec) ;
3484
3508
} ,
3485
3509
UseTreeKind :: Glob => { }
@@ -3488,7 +3512,7 @@ impl<'a> LoweringContext<'a> {
3488
3512
. skip ( 1 )
3489
3513
. zip ( [ id1, id2] . iter ( ) )
3490
3514
{
3491
- vec. push ( hir :: ItemId { id } ) ;
3515
+ vec. push ( id ) ;
3492
3516
}
3493
3517
} ,
3494
3518
}
@@ -4604,6 +4628,7 @@ impl<'a> LoweringContext<'a> {
4604
4628
let mut ids: SmallVec <[ hir:: Stmt ; 1 ] > = item_ids
4605
4629
. into_iter( )
4606
4630
. map( |item_id| {
4631
+ let item_id = hir:: ItemId { id: self . lower_node_id( item_id) . hir_id } ;
4607
4632
let LoweredNodeId { node_id: _, hir_id } = self . next_id( ) ;
4608
4633
4609
4634
hir:: Stmt {
0 commit comments