@@ -40,12 +40,9 @@ 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 ) ;
44
43
let hir_id = self . lctx . with_hir_id_owner ( item. id , |lctx| {
45
- lctx. without_in_scope_lifetime_defs ( |lctx| {
46
- let hir_item = lctx. lower_item ( item) ;
47
- lctx. insert_item ( hir_item)
48
- } )
44
+ let node = lctx. without_in_scope_lifetime_defs ( |lctx| lctx. lower_item ( item) ) ;
45
+ hir:: OwnerNode :: Item ( node)
49
46
} ) ;
50
47
51
48
self . lctx . with_parent_item_lifetime_defs ( hir_id, |this| {
@@ -72,26 +69,17 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
72
69
}
73
70
74
71
fn visit_assoc_item ( & mut self , item : & ' a AssocItem , ctxt : AssocCtxt ) {
75
- self . lctx . allocate_hir_id_counter ( item. id ) ;
76
72
self . lctx . with_hir_id_owner ( item. id , |lctx| match ctxt {
77
- AssocCtxt :: Trait => {
78
- let hir_item = lctx. lower_trait_item ( item) ;
79
- lctx. insert_trait_item ( hir_item) ;
80
- }
81
- AssocCtxt :: Impl => {
82
- let hir_item = lctx. lower_impl_item ( item) ;
83
- lctx. insert_impl_item ( hir_item) ;
84
- }
73
+ AssocCtxt :: Trait => hir:: OwnerNode :: TraitItem ( lctx. lower_trait_item ( item) ) ,
74
+ AssocCtxt :: Impl => hir:: OwnerNode :: ImplItem ( lctx. lower_impl_item ( item) ) ,
85
75
} ) ;
86
76
87
77
visit:: walk_assoc_item ( self , item, ctxt) ;
88
78
}
89
79
90
80
fn visit_foreign_item ( & mut self , item : & ' a ForeignItem ) {
91
- self . lctx . allocate_hir_id_counter ( item. id ) ;
92
81
self . lctx . with_hir_id_owner ( item. id , |lctx| {
93
- let hir_item = lctx. lower_foreign_item ( item) ;
94
- lctx. insert_foreign_item ( hir_item) ;
82
+ hir:: OwnerNode :: ForeignItem ( lctx. lower_foreign_item ( item) )
95
83
} ) ;
96
84
97
85
visit:: walk_foreign_item ( self , item) ;
@@ -106,12 +94,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
106
94
// only used when lowering a child item of a trait or impl.
107
95
fn with_parent_item_lifetime_defs < T > (
108
96
& mut self ,
109
- parent_hir_id : hir :: ItemId ,
97
+ parent_hir_id : LocalDefId ,
110
98
f : impl FnOnce ( & mut Self ) -> T ,
111
99
) -> T {
112
100
let old_len = self . in_scope_lifetimes . len ( ) ;
113
101
114
- let parent_generics = match self . owners [ parent_hir_id. def_id ] . unwrap ( ) . expect_item ( ) . kind {
102
+ let parent_generics = match self . owners [ parent_hir_id] . unwrap ( ) . expect_item ( ) . kind {
115
103
hir:: ItemKind :: Impl ( hir:: Impl { ref generics, .. } )
116
104
| hir:: ItemKind :: Trait ( _, _, ref generics, ..) => generics. params ,
117
105
_ => & [ ] ,
@@ -186,19 +174,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
186
174
}
187
175
}
188
176
189
- pub fn lower_item ( & mut self , i : & Item ) -> hir:: Item < ' hir > {
177
+ fn lower_item ( & mut self , i : & Item ) -> & ' hir hir:: Item < ' hir > {
190
178
let mut ident = i. ident ;
191
179
let mut vis = self . lower_visibility ( & i. vis ) ;
192
180
let hir_id = self . lower_node_id ( i. id ) ;
193
181
let attrs = self . lower_attrs ( hir_id, & i. attrs ) ;
194
182
let kind = self . lower_item_kind ( i. span , i. id , hir_id, & mut ident, attrs, & mut vis, & i. kind ) ;
195
- hir:: Item {
183
+ let item = hir:: Item {
196
184
def_id : hir_id. expect_owner ( ) ,
197
185
ident : self . lower_ident ( ident) ,
198
186
kind,
199
187
vis,
200
188
span : self . lower_span ( i. span ) ,
201
- }
189
+ } ;
190
+ self . arena . alloc ( item)
202
191
}
203
192
204
193
fn lower_item_kind (
@@ -480,10 +469,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
480
469
// Essentially a single `use` which imports two names is desugared into
481
470
// two imports.
482
471
for new_node_id in [ id1, id2] {
483
- // Associate an HirId to both ids even if there is no resolution.
484
- let new_id = self . allocate_hir_id_counter ( new_node_id) ;
485
-
486
- let res = if let Some ( res) = resolutions. next ( ) { res } else { continue } ;
472
+ let new_id = self . resolver . local_def_id ( new_node_id) ;
473
+ let res = if let Some ( res) = resolutions. next ( ) {
474
+ res
475
+ } else {
476
+ // Associate an HirId to both ids even if there is no resolution.
477
+ self . node_id_to_hir_id . ensure_contains_elem ( new_node_id, || None ) ;
478
+ debug_assert ! ( self . node_id_to_hir_id[ new_node_id] . is_none( ) ) ;
479
+ self . node_id_to_hir_id [ new_node_id] = Some ( hir:: HirId :: make_owner ( new_id) ) ;
480
+ continue ;
481
+ } ;
487
482
let ident = * ident;
488
483
let mut path = path. clone ( ) ;
489
484
for seg in & mut path. segments {
@@ -500,13 +495,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
500
495
this. attrs . insert ( hir:: HirId :: make_owner ( new_id) , attrs) ;
501
496
}
502
497
503
- this . insert_item ( hir:: Item {
498
+ let item = hir:: Item {
504
499
def_id : new_id,
505
500
ident : this. lower_ident ( ident) ,
506
501
kind,
507
502
vis,
508
503
span : this. lower_span ( span) ,
509
- } ) ;
504
+ } ;
505
+ hir:: OwnerNode :: Item ( this. arena . alloc ( item) )
510
506
} ) ;
511
507
}
512
508
@@ -550,7 +546,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
550
546
551
547
// Add all the nested `PathListItem`s to the HIR.
552
548
for & ( ref use_tree, id) in trees {
553
- let new_hir_id = self . allocate_hir_id_counter ( id) ;
549
+ let new_hir_id = self . resolver . local_def_id ( id) ;
554
550
555
551
let mut prefix = prefix. clone ( ) ;
556
552
@@ -574,13 +570,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
574
570
this. attrs . insert ( hir:: HirId :: make_owner ( new_hir_id) , attrs) ;
575
571
}
576
572
577
- this . insert_item ( hir:: Item {
573
+ let item = hir:: Item {
578
574
def_id : new_hir_id,
579
575
ident : this. lower_ident ( ident) ,
580
576
kind,
581
577
vis,
582
578
span : this. lower_span ( use_tree. span ) ,
583
- } ) ;
579
+ } ;
580
+ hir:: OwnerNode :: Item ( this. arena . alloc ( item) )
584
581
} ) ;
585
582
}
586
583
@@ -647,11 +644,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
647
644
respan ( self . lower_span ( vis. span ) , vis_kind)
648
645
}
649
646
650
- fn lower_foreign_item ( & mut self , i : & ForeignItem ) -> hir:: ForeignItem < ' hir > {
647
+ fn lower_foreign_item ( & mut self , i : & ForeignItem ) -> & ' hir hir:: ForeignItem < ' hir > {
651
648
let hir_id = self . lower_node_id ( i. id ) ;
652
649
let def_id = hir_id. expect_owner ( ) ;
653
650
self . lower_attrs ( hir_id, & i. attrs ) ;
654
- hir:: ForeignItem {
651
+ let item = hir:: ForeignItem {
655
652
def_id,
656
653
ident : self . lower_ident ( i. ident ) ,
657
654
kind : match i. kind {
@@ -681,12 +678,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
681
678
} ,
682
679
vis : self . lower_visibility ( & i. vis ) ,
683
680
span : self . lower_span ( i. span ) ,
684
- }
681
+ } ;
682
+ self . arena . alloc ( item)
685
683
}
686
684
687
685
fn lower_foreign_item_ref ( & mut self , i : & ForeignItem ) -> hir:: ForeignItemRef {
688
686
hir:: ForeignItemRef {
689
- id : hir:: ForeignItemId { def_id : self . allocate_hir_id_counter ( i. id ) } ,
687
+ id : hir:: ForeignItemId { def_id : self . resolver . local_def_id ( i. id ) } ,
690
688
ident : self . lower_ident ( i. ident ) ,
691
689
span : self . lower_span ( i. span ) ,
692
690
}
@@ -761,7 +759,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
761
759
}
762
760
}
763
761
764
- fn lower_trait_item ( & mut self , i : & AssocItem ) -> hir:: TraitItem < ' hir > {
762
+ fn lower_trait_item ( & mut self , i : & AssocItem ) -> & ' hir hir:: TraitItem < ' hir > {
765
763
let hir_id = self . lower_node_id ( i. id ) ;
766
764
let trait_item_def_id = hir_id. expect_owner ( ) ;
767
765
@@ -804,13 +802,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
804
802
} ;
805
803
806
804
self . lower_attrs ( hir_id, & i. attrs ) ;
807
- hir:: TraitItem {
805
+ let item = hir:: TraitItem {
808
806
def_id : trait_item_def_id,
809
807
ident : self . lower_ident ( i. ident ) ,
810
808
generics,
811
809
kind,
812
810
span : self . lower_span ( i. span ) ,
813
- }
811
+ } ;
812
+ self . arena . alloc ( item)
814
813
}
815
814
816
815
fn lower_trait_item_ref ( & mut self , i : & AssocItem ) -> hir:: TraitItemRef {
@@ -840,7 +839,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
840
839
self . expr ( span, hir:: ExprKind :: Err , AttrVec :: new ( ) )
841
840
}
842
841
843
- fn lower_impl_item ( & mut self , i : & AssocItem ) -> hir:: ImplItem < ' hir > {
842
+ fn lower_impl_item ( & mut self , i : & AssocItem ) -> & ' hir hir:: ImplItem < ' hir > {
844
843
let impl_item_def_id = self . resolver . local_def_id ( i. id ) ;
845
844
846
845
let ( generics, kind) = match & i. kind {
@@ -894,23 +893,24 @@ impl<'hir> LoweringContext<'_, 'hir> {
894
893
let ( defaultness, _) = self . lower_defaultness ( i. kind . defaultness ( ) , has_value) ;
895
894
let hir_id = self . lower_node_id ( i. id ) ;
896
895
self . lower_attrs ( hir_id, & i. attrs ) ;
897
- hir:: ImplItem {
896
+ let item = hir:: ImplItem {
898
897
def_id : hir_id. expect_owner ( ) ,
899
898
ident : self . lower_ident ( i. ident ) ,
900
899
generics,
901
900
vis : self . lower_visibility ( & i. vis ) ,
902
901
defaultness,
903
902
kind,
904
903
span : self . lower_span ( i. span ) ,
905
- }
904
+ } ;
905
+ self . arena . alloc ( item)
906
906
}
907
907
908
908
fn lower_impl_item_ref ( & mut self , i : & AssocItem ) -> hir:: ImplItemRef {
909
909
// Since `default impl` is not yet implemented, this is always true in impls.
910
910
let has_value = true ;
911
911
let ( defaultness, _) = self . lower_defaultness ( i. kind . defaultness ( ) , has_value) ;
912
912
hir:: ImplItemRef {
913
- id : hir:: ImplItemId { def_id : self . allocate_hir_id_counter ( i. id ) } ,
913
+ id : hir:: ImplItemId { def_id : self . resolver . local_def_id ( i. id ) } ,
914
914
ident : self . lower_ident ( i. ident ) ,
915
915
span : self . lower_span ( i. span ) ,
916
916
defaultness,
0 commit comments