@@ -10,7 +10,7 @@ use rustc_data_structures::fx::FxHashSet;
10
10
use rustc_errors:: struct_span_err;
11
11
use rustc_hir as hir;
12
12
use rustc_hir:: def:: { DefKind , Res } ;
13
- use rustc_hir:: def_id:: LocalDefId ;
13
+ use rustc_hir:: def_id:: { LocalDefId , CRATE_DEF_ID } ;
14
14
use rustc_span:: source_map:: { respan, DesugaringKind } ;
15
15
use rustc_span:: symbol:: { kw, sym, Ident } ;
16
16
use rustc_span:: Span ;
@@ -26,44 +26,44 @@ pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
26
26
}
27
27
28
28
impl ItemLowerer < ' _ , ' _ , ' _ > {
29
- fn with_trait_impl_ref ( & mut self , impl_ref : & Option < TraitRef > , f : impl FnOnce ( & mut Self ) ) {
29
+ fn with_trait_impl_ref < T > (
30
+ & mut self ,
31
+ impl_ref : & Option < TraitRef > ,
32
+ f : impl FnOnce ( & mut Self ) -> T ,
33
+ ) -> T {
30
34
let old = self . lctx . is_in_trait_impl ;
31
35
self . lctx . is_in_trait_impl = impl_ref. is_some ( ) ;
32
- f ( self ) ;
36
+ let ret = f ( self ) ;
33
37
self . lctx . is_in_trait_impl = old;
38
+ ret
34
39
}
35
40
}
36
41
37
42
impl < ' a > Visitor < ' a > for ItemLowerer < ' a , ' _ , ' _ > {
38
43
fn visit_item ( & mut self , item : & ' a Item ) {
39
- let mut item_hir_id = None ;
40
- self . lctx . with_hir_id_owner ( item. id , |lctx| {
41
- lctx. without_in_scope_lifetime_defs ( |lctx| {
42
- if let Some ( hir_item) = lctx. lower_item ( item) {
43
- let id = lctx. insert_item ( hir_item) ;
44
- item_hir_id = Some ( id) ;
45
- }
46
- } )
44
+ let hir_item = self . lctx . with_hir_id_owner ( item. id , |lctx| {
45
+ lctx. without_in_scope_lifetime_defs ( |lctx| lctx. lower_item ( item) )
47
46
} ) ;
48
-
49
- if let Some ( hir_id) = item_hir_id {
50
- self . lctx . with_parent_item_lifetime_defs ( hir_id, |this| {
51
- let this = & mut ItemLowerer { lctx : this } ;
52
- match item. kind {
53
- ItemKind :: Mod ( ..) => {
54
- let def_id = this. lctx . lower_node_id ( item. id ) . expect_owner ( ) ;
55
- let old_current_module =
56
- mem:: replace ( & mut this. lctx . current_module , def_id) ;
57
- visit:: walk_item ( this, item) ;
58
- this. lctx . current_module = old_current_module;
59
- }
60
- ItemKind :: Impl ( box ImplKind { ref of_trait, .. } ) => {
61
- this. with_trait_impl_ref ( of_trait, |this| visit:: walk_item ( this, item) ) ;
62
- }
63
- _ => visit:: walk_item ( this, item) ,
47
+ let force_top_level = self . lctx . check_force_top_level ( item) ;
48
+ let hir_id = self . lctx . insert_item ( hir_item, force_top_level) ;
49
+
50
+ let was_at_top_level = mem:: replace ( & mut self . lctx . is_at_top_level , false ) ;
51
+ self . lctx . with_parent_item_lifetime_defs ( hir_id, |this| {
52
+ let this = & mut ItemLowerer { lctx : this } ;
53
+ match item. kind {
54
+ ItemKind :: Mod ( ..) => {
55
+ let def_id = this. lctx . lower_node_id ( item. id ) . expect_owner ( ) ;
56
+ let old_current_module = mem:: replace ( & mut this. lctx . current_module , def_id) ;
57
+ visit:: walk_item ( this, item) ;
58
+ this. lctx . current_module = old_current_module;
64
59
}
65
- } ) ;
66
- }
60
+ ItemKind :: Impl ( box ImplKind { ref of_trait, .. } ) => {
61
+ this. with_trait_impl_ref ( of_trait, |this| visit:: walk_item ( this, item) ) ;
62
+ }
63
+ _ => visit:: walk_item ( this, item) ,
64
+ }
65
+ } ) ;
66
+ self . lctx . is_at_top_level = was_at_top_level;
67
67
}
68
68
69
69
fn visit_fn ( & mut self , fk : FnKind < ' a > , sp : Span , _: NodeId ) {
@@ -113,7 +113,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
113
113
fn with_parent_item_lifetime_defs < T > (
114
114
& mut self ,
115
115
parent_hir_id : hir:: ItemId ,
116
- f : impl FnOnce ( & mut LoweringContext < ' _ , ' _ > ) -> T ,
116
+ f : impl FnOnce ( & mut Self ) -> T ,
117
117
) -> T {
118
118
let old_len = self . in_scope_lifetimes . len ( ) ;
119
119
@@ -137,10 +137,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
137
137
// Clears (and restores) the `in_scope_lifetimes` field. Used when
138
138
// visiting nested items, which never inherit in-scope lifetimes
139
139
// from their surrounding environment.
140
- fn without_in_scope_lifetime_defs < T > (
141
- & mut self ,
142
- f : impl FnOnce ( & mut LoweringContext < ' _ , ' _ > ) -> T ,
143
- ) -> T {
140
+ fn without_in_scope_lifetime_defs < T > ( & mut self , f : impl FnOnce ( & mut Self ) -> T ) -> T {
144
141
let old_in_scope_lifetimes = mem:: replace ( & mut self . in_scope_lifetimes , vec ! [ ] ) ;
145
142
146
143
// this vector is only used when walking over impl headers,
@@ -157,10 +154,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
157
154
}
158
155
159
156
pub ( super ) fn lower_mod ( & mut self , items : & [ P < Item > ] , inner : Span ) -> hir:: Mod < ' hir > {
160
- hir:: Mod {
161
- inner,
162
- item_ids : self . arena . alloc_from_iter ( items. iter ( ) . flat_map ( |x| self . lower_item_id ( x) ) ) ,
157
+ let mut items: Vec < _ > = items
158
+ . iter ( )
159
+ . filter_map ( |x| {
160
+ if self . check_force_top_level ( & x) { None } else { Some ( self . lower_item_id ( & x) ) }
161
+ } )
162
+ . flatten ( )
163
+ . collect ( ) ;
164
+
165
+ if self . current_hir_id_owner . 0 == CRATE_DEF_ID {
166
+ let top_level_items = self . top_level_items . clone ( ) ;
167
+ for item in top_level_items {
168
+ let id = self . lower_item_id ( & item) [ 0 ] ;
169
+ items. push ( id) ;
170
+ }
163
171
}
172
+
173
+ hir:: Mod { inner, item_ids : self . arena . alloc_from_iter ( items. into_iter ( ) ) }
164
174
}
165
175
166
176
pub ( super ) fn lower_item_id ( & mut self , i : & Item ) -> SmallVec < [ hir:: ItemId ; 1 ] > {
@@ -208,13 +218,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
208
218
}
209
219
}
210
220
211
- pub fn lower_item ( & mut self , i : & Item ) -> Option < hir:: Item < ' hir > > {
221
+ pub fn lower_item ( & mut self , i : & Item ) -> hir:: Item < ' hir > {
222
+ let was_at_top_level = mem:: replace ( & mut self . is_at_top_level , false ) ;
212
223
let mut ident = i. ident ;
213
224
let mut vis = self . lower_visibility ( & i. vis , None ) ;
214
225
let hir_id = self . lower_node_id ( i. id ) ;
215
226
let attrs = self . lower_attrs ( hir_id, & i. attrs ) ;
216
227
let kind = self . lower_item_kind ( i. span , i. id , hir_id, & mut ident, attrs, & mut vis, & i. kind ) ;
217
- Some ( hir:: Item { def_id : hir_id. expect_owner ( ) , ident, kind, vis, span : i. span } )
228
+ self . is_at_top_level = was_at_top_level;
229
+ hir:: Item { def_id : hir_id. expect_owner ( ) , ident, kind, vis, span : i. span }
218
230
}
219
231
220
232
fn lower_item_kind (
@@ -285,7 +297,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
285
297
}
286
298
ItemKind :: Mod ( _, ref mod_kind) => match mod_kind {
287
299
ModKind :: Loaded ( items, _, inner_span) => {
288
- hir:: ItemKind :: Mod ( self . lower_mod ( items, * inner_span) )
300
+ let module = self . lower_mod ( items, * inner_span) ;
301
+ hir:: ItemKind :: Mod ( module)
289
302
}
290
303
ModKind :: Unloaded => panic ! ( "`mod` items should have been loaded by now" ) ,
291
304
} ,
@@ -537,13 +550,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
537
550
this. attrs . insert ( new_id, attrs) ;
538
551
}
539
552
540
- this. insert_item ( hir:: Item {
541
- def_id : new_id. expect_owner ( ) ,
542
- ident,
543
- kind,
544
- vis,
545
- span,
546
- } ) ;
553
+ this. insert_item (
554
+ hir:: Item { def_id : new_id. expect_owner ( ) , ident, kind, vis, span } ,
555
+ false ,
556
+ ) ;
547
557
} ) ;
548
558
}
549
559
@@ -611,13 +621,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
611
621
this. attrs . insert ( new_hir_id, attrs) ;
612
622
}
613
623
614
- this. insert_item ( hir:: Item {
615
- def_id : new_hir_id. expect_owner ( ) ,
616
- ident,
617
- kind,
618
- vis,
619
- span : use_tree. span ,
620
- } ) ;
624
+ this. insert_item (
625
+ hir:: Item {
626
+ def_id : new_hir_id. expect_owner ( ) ,
627
+ ident,
628
+ kind,
629
+ vis,
630
+ span : use_tree. span ,
631
+ } ,
632
+ false ,
633
+ ) ;
621
634
} ) ;
622
635
}
623
636
0 commit comments