@@ -5,7 +5,7 @@ use crate::{Arena, FnDeclKind};
5
5
use rustc_ast:: ptr:: P ;
6
6
use rustc_ast:: visit:: AssocCtxt ;
7
7
use rustc_ast:: * ;
8
- use rustc_data_structures:: fx:: FxHashSet ;
8
+ use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
9
9
use rustc_data_structures:: sorted_map:: SortedMap ;
10
10
use rustc_errors:: struct_span_err;
11
11
use rustc_hir as hir;
@@ -53,18 +53,22 @@ fn add_ty_alias_where_clause(
53
53
}
54
54
55
55
impl < ' a , ' hir > ItemLowerer < ' a , ' hir > {
56
- fn make_lctx ( & mut self ) -> LoweringContext < ' _ , ' hir > {
57
- LoweringContext {
56
+ fn with_lctx (
57
+ & mut self ,
58
+ owner : NodeId ,
59
+ f : impl FnOnce ( & mut LoweringContext < ' _ , ' hir > ) -> hir:: OwnerNode < ' hir > ,
60
+ ) {
61
+ let mut lctx = LoweringContext {
58
62
// Pseudo-globals.
59
63
sess : & self . sess ,
60
64
resolver : self . resolver ,
61
65
nt_to_tokenstream : self . nt_to_tokenstream ,
62
66
arena : self . arena ,
63
- owners : self . owners ,
64
67
65
68
// HirId handling.
66
69
bodies : Vec :: new ( ) ,
67
70
attrs : SortedMap :: default ( ) ,
71
+ children : FxHashMap :: default ( ) ,
68
72
current_hir_id_owner : CRATE_DEF_ID ,
69
73
item_local_id_counter : hir:: ItemLocalId :: new ( 0 ) ,
70
74
node_id_to_local_id : Default :: default ( ) ,
@@ -87,6 +91,13 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
87
91
allow_try_trait : Some ( [ sym:: try_trait_v2] [ ..] . into ( ) ) ,
88
92
allow_gen_future : Some ( [ sym:: gen_future] [ ..] . into ( ) ) ,
89
93
allow_into_future : Some ( [ sym:: into_future] [ ..] . into ( ) ) ,
94
+ } ;
95
+ lctx. with_hir_id_owner ( owner, |lctx| f ( lctx) ) ;
96
+
97
+ for ( def_id, info) in lctx. children {
98
+ self . owners . ensure_contains_elem ( def_id, || hir:: MaybeOwner :: Phantom ) ;
99
+ debug_assert ! ( matches!( self . owners[ def_id] , hir:: MaybeOwner :: Phantom ) ) ;
100
+ self . owners [ def_id] = info;
90
101
}
91
102
}
92
103
@@ -109,23 +120,21 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
109
120
self . owners [ def_id]
110
121
}
111
122
112
- fn lower_crate ( & mut self , c : & ' a Crate ) {
123
+ fn lower_crate ( & mut self , c : & Crate ) {
113
124
debug_assert_eq ! ( self . resolver. local_def_id( CRATE_NODE_ID ) , CRATE_DEF_ID ) ;
114
125
115
- let mut lctx = self . make_lctx ( ) ;
116
- lctx. with_hir_id_owner ( CRATE_NODE_ID , |lctx| {
126
+ self . with_lctx ( CRATE_NODE_ID , |lctx| {
117
127
let module = lctx. lower_mod ( & c. items , c. spans . inner_span ) ;
118
128
lctx. lower_attrs ( hir:: CRATE_HIR_ID , & c. attrs ) ;
119
129
hir:: OwnerNode :: Crate ( lctx. arena . alloc ( module) )
120
130
} )
121
131
}
122
132
123
- fn lower_item ( & mut self , item : & ' a Item ) {
124
- let mut lctx = self . make_lctx ( ) ;
125
- lctx. with_hir_id_owner ( item. id , |lctx| hir:: OwnerNode :: Item ( lctx. lower_item ( item) ) )
133
+ fn lower_item ( & mut self , item : & Item ) {
134
+ self . with_lctx ( item. id , |lctx| hir:: OwnerNode :: Item ( lctx. lower_item ( item) ) )
126
135
}
127
136
128
- fn lower_assoc_item ( & mut self , item : & ' a AssocItem , ctxt : AssocCtxt ) {
137
+ fn lower_assoc_item ( & mut self , item : & AssocItem , ctxt : AssocCtxt ) {
129
138
let def_id = self . resolver . local_def_id ( item. id ) ;
130
139
131
140
let parent_id = {
@@ -135,43 +144,44 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
135
144
} ;
136
145
137
146
let parent_hir = self . lower_node ( parent_id) . unwrap ( ) . node ( ) . expect_item ( ) ;
138
- let mut lctx = self . make_lctx ( ) ;
139
-
140
- // Evaluate with the lifetimes in `params` in-scope.
141
- // This is used to track which lifetimes have already been defined,
142
- // and which need to be replicated when lowering an async fn.
143
- match parent_hir. kind {
144
- hir:: ItemKind :: Impl ( hir:: Impl { ref of_trait, ref generics, .. } ) => {
145
- lctx. is_in_trait_impl = of_trait. is_some ( ) ;
146
- lctx. in_scope_lifetimes = generics
147
- . params
148
- . iter ( )
149
- . filter ( |param| matches ! ( param. kind, hir:: GenericParamKind :: Lifetime { .. } ) )
150
- . map ( |param| param. name )
151
- . collect ( ) ;
152
- }
153
- hir:: ItemKind :: Trait ( _, _, ref generics, ..) => {
154
- lctx. in_scope_lifetimes = generics
155
- . params
156
- . iter ( )
157
- . filter ( |param| matches ! ( param. kind, hir:: GenericParamKind :: Lifetime { .. } ) )
158
- . map ( |param| param. name )
159
- . collect ( ) ;
160
- }
161
- _ => { }
162
- } ;
147
+ self . with_lctx ( item. id , |lctx| {
148
+ // Evaluate with the lifetimes in `params` in-scope.
149
+ // This is used to track which lifetimes have already been defined,
150
+ // and which need to be replicated when lowering an async fn.
151
+ match parent_hir. kind {
152
+ hir:: ItemKind :: Impl ( hir:: Impl { ref of_trait, ref generics, .. } ) => {
153
+ lctx. is_in_trait_impl = of_trait. is_some ( ) ;
154
+ lctx. in_scope_lifetimes = generics
155
+ . params
156
+ . iter ( )
157
+ . filter ( |param| {
158
+ matches ! ( param. kind, hir:: GenericParamKind :: Lifetime { .. } )
159
+ } )
160
+ . map ( |param| param. name )
161
+ . collect ( ) ;
162
+ }
163
+ hir:: ItemKind :: Trait ( _, _, ref generics, ..) => {
164
+ lctx. in_scope_lifetimes = generics
165
+ . params
166
+ . iter ( )
167
+ . filter ( |param| {
168
+ matches ! ( param. kind, hir:: GenericParamKind :: Lifetime { .. } )
169
+ } )
170
+ . map ( |param| param. name )
171
+ . collect ( ) ;
172
+ }
173
+ _ => { }
174
+ } ;
163
175
164
- lctx. with_hir_id_owner ( item. id , |lctx| match ctxt {
165
- AssocCtxt :: Trait => hir:: OwnerNode :: TraitItem ( lctx. lower_trait_item ( item) ) ,
166
- AssocCtxt :: Impl => hir:: OwnerNode :: ImplItem ( lctx. lower_impl_item ( item) ) ,
176
+ match ctxt {
177
+ AssocCtxt :: Trait => hir:: OwnerNode :: TraitItem ( lctx. lower_trait_item ( item) ) ,
178
+ AssocCtxt :: Impl => hir:: OwnerNode :: ImplItem ( lctx. lower_impl_item ( item) ) ,
179
+ }
167
180
} )
168
181
}
169
182
170
- fn lower_foreign_item ( & mut self , item : & ' a ForeignItem ) {
171
- let mut lctx = self . make_lctx ( ) ;
172
- lctx. with_hir_id_owner ( item. id , |lctx| {
173
- hir:: OwnerNode :: ForeignItem ( lctx. lower_foreign_item ( item) )
174
- } )
183
+ fn lower_foreign_item ( & mut self , item : & ForeignItem ) {
184
+ self . with_lctx ( item. id , |lctx| hir:: OwnerNode :: ForeignItem ( lctx. lower_foreign_item ( item) ) )
175
185
}
176
186
}
177
187
@@ -555,12 +565,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
555
565
let new_id = self . resolver . local_def_id ( new_node_id) ;
556
566
let Some ( res) = resolutions. next ( ) else {
557
567
// Associate an HirId to both ids even if there is no resolution.
558
- self . owners . ensure_contains_elem ( new_id, || hir:: MaybeOwner :: Phantom ) ;
559
- let _old = std:: mem:: replace (
560
- & mut self . owners [ new_id] ,
568
+ let _old = self . children . insert (
569
+ new_id,
561
570
hir:: MaybeOwner :: NonOwner ( hir:: HirId :: make_owner ( new_id) ) ,
562
571
) ;
563
- debug_assert ! ( matches! ( _old, hir :: MaybeOwner :: Phantom ) ) ;
572
+ debug_assert ! ( _old. is_none ( ) ) ;
564
573
continue ;
565
574
} ;
566
575
let ident = * ident;
0 commit comments