@@ -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
- let mut vis = self . lower_visibility ( & i. vis , None ) ;
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 {
@@ -493,24 +488,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
493
488
494
489
self . with_hir_id_owner ( new_node_id, |this| {
495
490
let res = this. lower_res ( res) ;
496
- let path = this. lower_path_extra ( res, & path, ParamMode :: Explicit , None ) ;
491
+ let path = this. lower_path_extra ( res, & path, ParamMode :: Explicit ) ;
497
492
let kind = hir:: ItemKind :: Use ( path, hir:: UseKind :: Single ) ;
498
493
let vis = this. rebuild_vis ( & vis) ;
499
494
if let Some ( attrs) = attrs {
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
513
- let path = self . lower_path_extra ( ret_res, & path, ParamMode :: Explicit , None ) ;
509
+ let path = self . lower_path_extra ( ret_res, & path, ParamMode :: Explicit ) ;
514
510
hir:: ItemKind :: Use ( path, hir:: UseKind :: Single )
515
511
}
516
512
UseTreeKind :: Glob => {
@@ -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
@@ -610,7 +607,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
610
607
611
608
let res = self . expect_full_res_from_use ( id) . next ( ) . unwrap_or ( Res :: Err ) ;
612
609
let res = self . lower_res ( res) ;
613
- let path = self . lower_path_extra ( res, & prefix, ParamMode :: Explicit , None ) ;
610
+ let path = self . lower_path_extra ( res, & prefix, ParamMode :: Explicit ) ;
614
611
hir:: ItemKind :: Use ( path, hir:: UseKind :: ListStem )
615
612
}
616
613
}
@@ -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 {
@@ -679,17 +676,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
679
676
ForeignItemKind :: TyAlias ( ..) => hir:: ForeignItemKind :: Type ,
680
677
ForeignItemKind :: MacCall ( _) => panic ! ( "macro shouldn't exist here" ) ,
681
678
} ,
682
- vis : self . lower_visibility ( & i. vis , None ) ,
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
- fn lower_foreign_item_ref ( & mut self , i : & ForeignItem ) -> hir:: ForeignItemRef < ' hir > {
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
- vis : self . lower_visibility ( & i. vis , Some ( i. id ) ) ,
693
690
}
694
691
}
695
692
@@ -757,12 +754,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
757
754
// FIXME(jseyfried): positional field hygiene.
758
755
None => Ident :: new ( sym:: integer ( index) , self . lower_span ( f. span ) ) ,
759
756
} ,
760
- vis : self . lower_visibility ( & f. vis , None ) ,
757
+ vis : self . lower_visibility ( & f. vis ) ,
761
758
ty,
762
759
}
763
760
}
764
761
765
- fn lower_trait_item ( & mut self , i : & AssocItem ) -> hir:: TraitItem < ' hir > {
762
+ fn lower_trait_item ( & mut self , i : & AssocItem ) -> & ' hir hir:: TraitItem < ' hir > {
766
763
let hir_id = self . lower_node_id ( i. id ) ;
767
764
let trait_item_def_id = hir_id. expect_owner ( ) ;
768
765
@@ -805,13 +802,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
805
802
} ;
806
803
807
804
self . lower_attrs ( hir_id, & i. attrs ) ;
808
- hir:: TraitItem {
805
+ let item = hir:: TraitItem {
809
806
def_id : trait_item_def_id,
810
807
ident : self . lower_ident ( i. ident ) ,
811
808
generics,
812
809
kind,
813
810
span : self . lower_span ( i. span ) ,
814
- }
811
+ } ;
812
+ self . arena . alloc ( item)
815
813
}
816
814
817
815
fn lower_trait_item_ref ( & mut self , i : & AssocItem ) -> hir:: TraitItemRef {
@@ -841,7 +839,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
841
839
self . expr ( span, hir:: ExprKind :: Err , AttrVec :: new ( ) )
842
840
}
843
841
844
- fn lower_impl_item ( & mut self , i : & AssocItem ) -> hir:: ImplItem < ' hir > {
842
+ fn lower_impl_item ( & mut self , i : & AssocItem ) -> & ' hir hir:: ImplItem < ' hir > {
845
843
let impl_item_def_id = self . resolver . local_def_id ( i. id ) ;
846
844
847
845
let ( generics, kind) = match & i. kind {
@@ -895,26 +893,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
895
893
let ( defaultness, _) = self . lower_defaultness ( i. kind . defaultness ( ) , has_value) ;
896
894
let hir_id = self . lower_node_id ( i. id ) ;
897
895
self . lower_attrs ( hir_id, & i. attrs ) ;
898
- hir:: ImplItem {
896
+ let item = hir:: ImplItem {
899
897
def_id : hir_id. expect_owner ( ) ,
900
898
ident : self . lower_ident ( i. ident ) ,
901
899
generics,
902
- vis : self . lower_visibility ( & i. vis , None ) ,
900
+ vis : self . lower_visibility ( & i. vis ) ,
903
901
defaultness,
904
902
kind,
905
903
span : self . lower_span ( i. span ) ,
906
- }
904
+ } ;
905
+ self . arena . alloc ( item)
907
906
}
908
907
909
- fn lower_impl_item_ref ( & mut self , i : & AssocItem ) -> hir:: ImplItemRef < ' hir > {
908
+ fn lower_impl_item_ref ( & mut self , i : & AssocItem ) -> hir:: ImplItemRef {
910
909
// Since `default impl` is not yet implemented, this is always true in impls.
911
910
let has_value = true ;
912
911
let ( defaultness, _) = self . lower_defaultness ( i. kind . defaultness ( ) , has_value) ;
913
912
hir:: ImplItemRef {
914
- 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 ) } ,
915
914
ident : self . lower_ident ( i. ident ) ,
916
915
span : self . lower_span ( i. span ) ,
917
- vis : self . lower_visibility ( & i. vis , Some ( i. id ) ) ,
918
916
defaultness,
919
917
kind : match & i. kind {
920
918
AssocItemKind :: Const ( ..) => hir:: AssocItemKind :: Const ,
@@ -932,25 +930,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
932
930
/// lowered. This can happen during `lower_impl_item_ref()` where we need to
933
931
/// lower a `Visibility` value although we haven't lowered the owning
934
932
/// `ImplItem` in question yet.
935
- fn lower_visibility (
936
- & mut self ,
937
- v : & Visibility ,
938
- explicit_owner : Option < NodeId > ,
939
- ) -> hir:: Visibility < ' hir > {
933
+ fn lower_visibility ( & mut self , v : & Visibility ) -> hir:: Visibility < ' hir > {
940
934
let node = match v. kind {
941
935
VisibilityKind :: Public => hir:: VisibilityKind :: Public ,
942
936
VisibilityKind :: Crate ( sugar) => hir:: VisibilityKind :: Crate ( sugar) ,
943
937
VisibilityKind :: Restricted { ref path, id } => {
944
938
debug ! ( "lower_visibility: restricted path id = {:?}" , id) ;
945
- let lowered_id = if let Some ( owner) = explicit_owner {
946
- self . lower_node_id_with_owner ( id, owner)
947
- } else {
948
- self . lower_node_id ( id)
949
- } ;
950
- let res = self . expect_full_res ( id) ;
951
- let res = self . lower_res ( res) ;
939
+ let lowered_id = self . lower_node_id ( id) ;
952
940
hir:: VisibilityKind :: Restricted {
953
- path : self . lower_path_extra ( res , path, ParamMode :: Explicit , explicit_owner ) ,
941
+ path : self . lower_path ( id , path, ParamMode :: Explicit ) ,
954
942
hir_id : lowered_id,
955
943
}
956
944
}
0 commit comments