@@ -51,7 +51,6 @@ use rustc_data_structures::thin_vec::ThinVec;
5151use rustc_data_structures:: sync:: Lrc ;
5252
5353use std:: collections:: { BTreeSet , BTreeMap } ;
54- use std:: fmt:: Debug ;
5554use std:: mem;
5655use smallvec:: SmallVec ;
5756use syntax:: attr;
@@ -82,7 +81,7 @@ pub struct LoweringContext<'a> {
8281 resolver : & ' a mut dyn Resolver ,
8382
8483 /// The items being lowered are collected here.
85- items : BTreeMap < NodeId , hir:: Item > ,
84+ items : BTreeMap < hir :: HirId , hir:: Item > ,
8685
8786 trait_items : BTreeMap < hir:: TraitItemId , hir:: TraitItem > ,
8887 impl_items : BTreeMap < hir:: ImplItemId , hir:: ImplItem > ,
@@ -321,7 +320,7 @@ enum AnonymousLifetimeMode {
321320 PassThrough ,
322321}
323322
324- struct ImplTraitTypeIdVisitor < ' a > { ids : & ' a mut SmallVec < [ hir :: ItemId ; 1 ] > }
323+ struct ImplTraitTypeIdVisitor < ' a > { ids : & ' a mut SmallVec < [ NodeId ; 1 ] > }
325324
326325impl < ' a , ' b > Visitor < ' a > for ImplTraitTypeIdVisitor < ' b > {
327326 fn visit_ty ( & mut self , ty : & ' a Ty ) {
@@ -330,7 +329,7 @@ impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
330329 | TyKind :: BareFn ( _)
331330 => return ,
332331
333- TyKind :: ImplTrait ( id, _) => self . ids . push ( hir :: ItemId { id } ) ,
332+ TyKind :: ImplTrait ( id, _) => self . ids . push ( id ) ,
334333 _ => { } ,
335334 }
336335 visit:: walk_ty ( self , ty) ;
@@ -361,9 +360,40 @@ impl<'a> LoweringContext<'a> {
361360 lctx : & ' lcx mut LoweringContext < ' interner > ,
362361 }
363362
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+
364394 impl < ' lcx , ' interner > Visitor < ' lcx > for MiscCollector < ' lcx , ' interner > {
365395 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 ;
367397
368398 match item. node {
369399 ItemKind :: Struct ( _, ref generics)
@@ -383,18 +413,21 @@ impl<'a> LoweringContext<'a> {
383413 . count ( ) ;
384414 self . lctx . type_def_lifetime_params . insert ( def_id, count) ;
385415 }
416+ ItemKind :: Use ( ref use_tree) => {
417+ self . allocate_use_tree_hir_id_counters ( use_tree, hir_id. owner ) ;
418+ }
386419 _ => { }
387420 }
388421 visit:: walk_item ( self , item) ;
389422 }
390423
391424 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 ) ;
393426 visit:: walk_trait_item ( self , item) ;
394427 }
395428
396429 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 ) ;
398431 visit:: walk_impl_item ( self , item) ;
399432 }
400433 }
@@ -434,17 +467,16 @@ impl<'a> LoweringContext<'a> {
434467 }
435468
436469 fn visit_item ( & mut self , item : & ' lcx Item ) {
437- let mut item_lowered = true ;
470+ let mut item_hir_id = None ;
438471 self . lctx . with_hir_id_owner ( item. id , |lctx| {
439472 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) ;
443475 }
444476 } ) ;
445477
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 {
448480 hir:: ItemKind :: Impl ( _, _, _, ref generics, ..)
449481 | hir:: ItemKind :: Trait ( _, _, ref generics, ..) => {
450482 generics. params . clone ( )
@@ -516,20 +548,21 @@ impl<'a> LoweringContext<'a> {
516548 }
517549 }
518550
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 ) ) ;
520555 self . items . insert ( id, item) ;
521556 self . modules . get_mut ( & self . current_module ) . unwrap ( ) . items . insert ( id) ;
522557 }
523558
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 ) ;
531562 // 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
533566 }
534567
535568 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> {
13811414 . opt_def_index ( exist_ty_node_id)
13821415 . unwrap ( ) ;
13831416
1384- self . allocate_hir_id_counter ( exist_ty_node_id, & "existential impl trait" ) ;
1417+ self . allocate_hir_id_counter ( exist_ty_node_id) ;
13851418
13861419 let hir_bounds = self . with_hir_id_owner ( exist_ty_node_id, lower_bounds) ;
13871420
@@ -1422,10 +1455,10 @@ impl<'a> LoweringContext<'a> {
14221455 // Insert the item into the global list. This usually happens
14231456 // automatically for all AST items. But this existential type item
14241457 // 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) ;
14261459
14271460 // `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)
14291462 } )
14301463 }
14311464
@@ -2002,9 +2035,9 @@ impl<'a> LoweringContext<'a> {
20022035 )
20032036 }
20042037
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 ] > ) {
20062039 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 ( ) ;
20082041 if self . sess . features_untracked ( ) . impl_trait_in_bindings {
20092042 if let Some ( ref ty) = l. ty {
20102043 let mut visitor = ImplTraitTypeIdVisitor { ids : & mut ids } ;
@@ -3065,7 +3098,6 @@ impl<'a> LoweringContext<'a> {
30653098 }
30663099 }
30673100
3068- let parent_def_index = self . current_hir_id_owner . last ( ) . unwrap ( ) . 0 ;
30693101 let mut defs = self . expect_full_def_from_use ( id) ;
30703102 // We want to return *something* from this function, so hold onto the first item
30713103 // for later.
@@ -3084,14 +3116,6 @@ impl<'a> LoweringContext<'a> {
30843116 seg. id = self . sess . next_node_id ( ) ;
30853117 }
30863118 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) ;
30953119
30963120 self . with_hir_id_owner ( new_node_id, |this| {
30973121 let new_id = this. lower_node_id ( new_node_id) ;
@@ -3114,7 +3138,6 @@ impl<'a> LoweringContext<'a> {
31143138 let vis = respan ( vis. span , vis_kind) ;
31153139
31163140 this. insert_item (
3117- new_id. node_id ,
31183141 hir:: Item {
31193142 hir_id : new_id. hir_id ,
31203143 ident,
@@ -3174,8 +3197,6 @@ impl<'a> LoweringContext<'a> {
31743197
31753198 // Add all the nested `PathListItem`s to the HIR.
31763199 for & ( ref use_tree, id) in trees {
3177- self . allocate_hir_id_counter ( id, & use_tree) ;
3178-
31793200 let LoweredNodeId {
31803201 node_id : new_id,
31813202 hir_id : new_hir_id,
@@ -3219,7 +3240,6 @@ impl<'a> LoweringContext<'a> {
32193240 let vis = respan ( vis. span , vis_kind) ;
32203241
32213242 this. insert_item (
3222- new_id,
32233243 hir:: Item {
32243244 hir_id : new_hir_id,
32253245 ident,
@@ -3443,43 +3463,47 @@ impl<'a> LoweringContext<'a> {
34433463 }
34443464
34453465 fn lower_item_id ( & mut self , i : & Item ) -> SmallVec < [ hir:: ItemId ; 1 ] > {
3446- match i. node {
3466+ let node_ids = match i. node {
34473467 ItemKind :: Use ( ref use_tree) => {
3448- let mut vec = smallvec ! [ hir :: ItemId { id : i. id } ] ;
3468+ let mut vec = smallvec ! [ i. id] ;
34493469 self . lower_item_id_use_tree ( use_tree, i. id , & mut vec) ;
34503470 vec
34513471 }
34523472 ItemKind :: MacroDef ( ..) => SmallVec :: new ( ) ,
34533473 ItemKind :: Fn ( ..) |
3454- ItemKind :: Impl ( .., None , _, _) => smallvec ! [ hir :: ItemId { id : i. id } ] ,
3474+ ItemKind :: Impl ( .., None , _, _) => smallvec ! [ i. id] ,
34553475 ItemKind :: Static ( ref ty, ..) => {
3456- let mut ids = smallvec ! [ hir :: ItemId { id : i. id } ] ;
3476+ let mut ids = smallvec ! [ i. id] ;
34573477 if self . sess . features_untracked ( ) . impl_trait_in_bindings {
34583478 let mut visitor = ImplTraitTypeIdVisitor { ids : & mut ids } ;
34593479 visitor. visit_ty ( ty) ;
34603480 }
34613481 ids
34623482 } ,
34633483 ItemKind :: Const ( ref ty, ..) => {
3464- let mut ids = smallvec ! [ hir :: ItemId { id : i. id } ] ;
3484+ let mut ids = smallvec ! [ i. id] ;
34653485 if self . sess . features_untracked ( ) . impl_trait_in_bindings {
34663486 let mut visitor = ImplTraitTypeIdVisitor { ids : & mut ids } ;
34673487 visitor. visit_ty ( ty) ;
34683488 }
34693489 ids
34703490 } ,
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 ( )
34733497 }
34743498
34753499 fn lower_item_id_use_tree ( & mut self ,
34763500 tree : & UseTree ,
34773501 base_id : NodeId ,
3478- vec : & mut SmallVec < [ hir :: ItemId ; 1 ] > )
3502+ vec : & mut SmallVec < [ NodeId ; 1 ] > )
34793503 {
34803504 match tree. kind {
34813505 UseTreeKind :: Nested ( ref nested_vec) => for & ( ref nested, id) in nested_vec {
3482- vec. push ( hir :: ItemId { id } ) ;
3506+ vec. push ( id ) ;
34833507 self . lower_item_id_use_tree ( nested, id, vec) ;
34843508 } ,
34853509 UseTreeKind :: Glob => { }
@@ -3488,7 +3512,7 @@ impl<'a> LoweringContext<'a> {
34883512 . skip ( 1 )
34893513 . zip ( [ id1, id2] . iter ( ) )
34903514 {
3491- vec. push ( hir :: ItemId { id } ) ;
3515+ vec. push ( id ) ;
34923516 }
34933517 } ,
34943518 }
@@ -4604,6 +4628,7 @@ impl<'a> LoweringContext<'a> {
46044628 let mut ids: SmallVec <[ hir:: Stmt ; 1 ] > = item_ids
46054629 . into_iter( )
46064630 . map( |item_id| {
4631+ let item_id = hir:: ItemId { id: self . lower_node_id( item_id) . hir_id } ;
46074632 let LoweredNodeId { node_id: _, hir_id } = self . next_id( ) ;
46084633
46094634 hir:: Stmt {
0 commit comments