@@ -40,6 +40,7 @@ impl ItemLowerer<'_, '_, '_> {
40
40
41
41
impl < ' a > Visitor < ' a > for ItemLowerer < ' a , ' _ , ' _ > {
42
42
fn visit_item ( & mut self , item : & ' a Item ) {
43
+ self . lctx . allocate_hir_id_counter ( item. id ) ;
43
44
let hir_id = self . lctx . with_hir_id_owner ( item. id , |lctx| {
44
45
lctx. without_in_scope_lifetime_defs ( |lctx| {
45
46
let hir_item = lctx. lower_item ( item) ;
@@ -77,6 +78,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
77
78
}
78
79
79
80
fn visit_assoc_item ( & mut self , item : & ' a AssocItem , ctxt : AssocCtxt ) {
81
+ self . lctx . allocate_hir_id_counter ( item. id ) ;
80
82
self . lctx . with_hir_id_owner ( item. id , |lctx| match ctxt {
81
83
AssocCtxt :: Trait => {
82
84
let hir_item = lctx. lower_trait_item ( item) ;
@@ -154,41 +156,28 @@ impl<'hir> LoweringContext<'_, 'hir> {
154
156
pub ( super ) fn lower_mod ( & mut self , items : & [ P < Item > ] , inner : Span ) -> hir:: Mod < ' hir > {
155
157
hir:: Mod {
156
158
inner : self . lower_span ( inner) ,
157
- item_ids : self . arena . alloc_from_iter ( items. iter ( ) . flat_map ( |x| self . lower_item_id ( x) ) ) ,
159
+ item_ids : self . arena . alloc_from_iter ( items. iter ( ) . flat_map ( |x| self . lower_item_ref ( x) ) ) ,
158
160
}
159
161
}
160
162
161
- pub ( super ) fn lower_item_id ( & mut self , i : & Item ) -> SmallVec < [ hir:: ItemId ; 1 ] > {
162
- let node_ids = match i. kind {
163
- ItemKind :: Use ( ref use_tree) => {
164
- let mut vec = smallvec ! [ i. id] ;
165
- self . lower_item_id_use_tree ( use_tree, i. id , & mut vec) ;
166
- vec
167
- }
168
- ItemKind :: Fn ( ..) | ItemKind :: Impl ( box ImplKind { of_trait : None , .. } ) => {
169
- smallvec ! [ i. id]
170
- }
171
- _ => smallvec ! [ i. id] ,
172
- } ;
173
-
163
+ pub ( super ) fn lower_item_ref ( & mut self , i : & Item ) -> SmallVec < [ hir:: ItemId ; 1 ] > {
164
+ let mut node_ids = smallvec ! [ hir:: ItemId { def_id: self . resolver. local_def_id( i. id) } ] ;
165
+ if let ItemKind :: Use ( ref use_tree) = & i. kind {
166
+ self . lower_item_id_use_tree ( use_tree, i. id , & mut node_ids) ;
167
+ }
174
168
node_ids
175
- . into_iter ( )
176
- . map ( |node_id| hir:: ItemId {
177
- def_id : self . allocate_hir_id_counter ( node_id) . expect_owner ( ) ,
178
- } )
179
- . collect ( )
180
169
}
181
170
182
171
fn lower_item_id_use_tree (
183
172
& mut self ,
184
173
tree : & UseTree ,
185
174
base_id : NodeId ,
186
- vec : & mut SmallVec < [ NodeId ; 1 ] > ,
175
+ vec : & mut SmallVec < [ hir :: ItemId ; 1 ] > ,
187
176
) {
188
177
match tree. kind {
189
178
UseTreeKind :: Nested ( ref nested_vec) => {
190
179
for & ( ref nested, id) in nested_vec {
191
- vec. push ( id ) ;
180
+ vec. push ( hir :: ItemId { def_id : self . resolver . local_def_id ( id ) } ) ;
192
181
self . lower_item_id_use_tree ( nested, id, vec) ;
193
182
}
194
183
}
@@ -197,7 +186,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
197
186
for ( _, & id) in
198
187
iter:: zip ( self . expect_full_res_from_use ( base_id) . skip ( 1 ) , & [ id1, id2] )
199
188
{
200
- vec. push ( id ) ;
189
+ vec. push ( hir :: ItemId { def_id : self . resolver . local_def_id ( id ) } ) ;
201
190
}
202
191
}
203
192
}
@@ -486,7 +475,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
486
475
}
487
476
}
488
477
489
- let mut resolutions = self . expect_full_res_from_use ( id) ;
478
+ let mut resolutions = self . expect_full_res_from_use ( id) . fuse ( ) ;
490
479
// We want to return *something* from this function, so hold onto the first item
491
480
// for later.
492
481
let ret_res = self . lower_res ( resolutions. next ( ) . unwrap_or ( Res :: Err ) ) ;
@@ -496,7 +485,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
496
485
// won't be dealing with macros in the rest of the compiler.
497
486
// Essentially a single `use` which imports two names is desugared into
498
487
// two imports.
499
- for ( res, & new_node_id) in iter:: zip ( resolutions, & [ id1, id2] ) {
488
+ for new_node_id in [ id1, id2] {
489
+ // Associate an HirId to both ids even if there is no resolution.
490
+ let new_id = self . allocate_hir_id_counter ( new_node_id) ;
491
+
492
+ let res = if let Some ( res) = resolutions. next ( ) { res } else { continue } ;
500
493
let ident = * ident;
501
494
let mut path = path. clone ( ) ;
502
495
for seg in & mut path. segments {
@@ -505,17 +498,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
505
498
let span = path. span ;
506
499
507
500
self . with_hir_id_owner ( new_node_id, |this| {
508
- let new_id = this. lower_node_id ( new_node_id) ;
509
501
let res = this. lower_res ( res) ;
510
502
let path = this. lower_path_extra ( res, & path, ParamMode :: Explicit , None ) ;
511
503
let kind = hir:: ItemKind :: Use ( path, hir:: UseKind :: Single ) ;
512
504
let vis = this. rebuild_vis ( & vis) ;
513
505
if let Some ( attrs) = attrs {
514
- this. attrs . insert ( new_id, attrs) ;
506
+ this. attrs . insert ( hir :: HirId :: make_owner ( new_id) , attrs) ;
515
507
}
516
508
517
509
this. insert_item ( hir:: Item {
518
- def_id : new_id. expect_owner ( ) ,
510
+ def_id : new_id,
519
511
ident : this. lower_ident ( ident) ,
520
512
kind,
521
513
vis,
@@ -564,7 +556,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
564
556
565
557
// Add all the nested `PathListItem`s to the HIR.
566
558
for & ( ref use_tree, id) in trees {
567
- let new_hir_id = self . lower_node_id ( id) ;
559
+ let new_hir_id = self . allocate_hir_id_counter ( id) ;
568
560
569
561
let mut prefix = prefix. clone ( ) ;
570
562
@@ -585,11 +577,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
585
577
let kind =
586
578
this. lower_use_tree ( use_tree, & prefix, id, & mut vis, & mut ident, attrs) ;
587
579
if let Some ( attrs) = attrs {
588
- this. attrs . insert ( new_hir_id, attrs) ;
580
+ this. attrs . insert ( hir :: HirId :: make_owner ( new_hir_id) , attrs) ;
589
581
}
590
582
591
583
this. insert_item ( hir:: Item {
592
- def_id : new_hir_id. expect_owner ( ) ,
584
+ def_id : new_hir_id,
593
585
ident : this. lower_ident ( ident) ,
594
586
kind,
595
587
vis,
@@ -700,7 +692,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
700
692
701
693
fn lower_foreign_item_ref ( & mut self , i : & ForeignItem ) -> hir:: ForeignItemRef < ' hir > {
702
694
hir:: ForeignItemRef {
703
- id : hir:: ForeignItemId { def_id : self . lower_node_id ( i. id ) . expect_owner ( ) } ,
695
+ id : hir:: ForeignItemId { def_id : self . allocate_hir_id_counter ( i. id ) } ,
704
696
ident : self . lower_ident ( i. ident ) ,
705
697
span : self . lower_span ( i. span ) ,
706
698
vis : self . lower_visibility ( & i. vis , Some ( i. id ) ) ,
@@ -839,7 +831,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
839
831
}
840
832
AssocItemKind :: MacCall ( ..) => unimplemented ! ( ) ,
841
833
} ;
842
- let id = hir:: TraitItemId { def_id : self . lower_node_id ( i. id ) . expect_owner ( ) } ;
834
+ let id = hir:: TraitItemId { def_id : self . resolver . local_def_id ( i. id ) } ;
843
835
let defaultness = hir:: Defaultness :: Default { has_value : has_default } ;
844
836
hir:: TraitItemRef {
845
837
id,
@@ -925,7 +917,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
925
917
let has_value = true ;
926
918
let ( defaultness, _) = self . lower_defaultness ( i. kind . defaultness ( ) , has_value) ;
927
919
hir:: ImplItemRef {
928
- id : hir:: ImplItemId { def_id : self . lower_node_id ( i. id ) . expect_owner ( ) } ,
920
+ id : hir:: ImplItemId { def_id : self . allocate_hir_id_counter ( i. id ) } ,
929
921
ident : self . lower_ident ( i. ident ) ,
930
922
span : self . lower_span ( i. span ) ,
931
923
vis : self . lower_visibility ( & i. vis , Some ( i. id ) ) ,
0 commit comments