@@ -15,11 +15,11 @@ use rustc_span::source_map::{respan, DesugaringKind};
15
15
use rustc_span:: symbol:: { kw, sym, Ident } ;
16
16
use rustc_span:: Span ;
17
17
use rustc_target:: spec:: abi;
18
-
19
18
use smallvec:: { smallvec, SmallVec } ;
20
- use std:: collections:: BTreeSet ;
21
19
use tracing:: debug;
22
20
21
+ use std:: mem;
22
+
23
23
pub ( super ) struct ItemLowerer < ' a , ' lowering , ' hir > {
24
24
pub ( super ) lctx : & ' a mut LoweringContext < ' lowering , ' hir > ,
25
25
}
@@ -34,25 +34,6 @@ impl ItemLowerer<'_, '_, '_> {
34
34
}
35
35
36
36
impl < ' a > Visitor < ' a > for ItemLowerer < ' a , ' _ , ' _ > {
37
- fn visit_mod ( & mut self , m : & ' a Mod , _s : Span , _attrs : & [ Attribute ] , n : NodeId ) {
38
- let def_id = self . lctx . lower_node_id ( n) . expect_owner ( ) ;
39
-
40
- self . lctx . modules . insert (
41
- def_id,
42
- hir:: ModuleItems {
43
- items : BTreeSet :: new ( ) ,
44
- trait_items : BTreeSet :: new ( ) ,
45
- impl_items : BTreeSet :: new ( ) ,
46
- foreign_items : BTreeSet :: new ( ) ,
47
- } ,
48
- ) ;
49
-
50
- let old = self . lctx . current_module ;
51
- self . lctx . current_module = def_id;
52
- visit:: walk_mod ( self , m) ;
53
- self . lctx . current_module = old;
54
- }
55
-
56
37
fn visit_item ( & mut self , item : & ' a Item ) {
57
38
let mut item_hir_id = None ;
58
39
self . lctx . with_hir_id_owner ( item. id , |lctx| {
@@ -67,10 +48,18 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
67
48
if let Some ( hir_id) = item_hir_id {
68
49
self . lctx . with_parent_item_lifetime_defs ( hir_id, |this| {
69
50
let this = & mut ItemLowerer { lctx : this } ;
70
- if let ItemKind :: Impl ( box ImplKind { ref of_trait, .. } ) = item. kind {
71
- this. with_trait_impl_ref ( of_trait, |this| visit:: walk_item ( this, item) ) ;
72
- } else {
73
- visit:: walk_item ( this, item) ;
51
+ match item. kind {
52
+ ItemKind :: Mod ( ..) => {
53
+ let def_id = this. lctx . lower_node_id ( item. id ) . expect_owner ( ) ;
54
+ let old_current_module =
55
+ mem:: replace ( & mut this. lctx . current_module , def_id) ;
56
+ visit:: walk_item ( this, item) ;
57
+ this. lctx . current_module = old_current_module;
58
+ }
59
+ ItemKind :: Impl ( box ImplKind { ref of_trait, .. } ) => {
60
+ this. with_trait_impl_ref ( of_trait, |this| visit:: walk_item ( this, item) ) ;
61
+ }
62
+ _ => visit:: walk_item ( this, item) ,
74
63
}
75
64
} ) ;
76
65
}
@@ -94,13 +83,13 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
94
83
let hir_item = lctx. lower_trait_item ( item) ;
95
84
let id = hir_item. trait_item_id ( ) ;
96
85
lctx. trait_items . insert ( id, hir_item) ;
97
- lctx. modules . get_mut ( & lctx. current_module ) . unwrap ( ) . trait_items . insert ( id) ;
86
+ lctx. modules . entry ( lctx. current_module ) . or_default ( ) . trait_items . insert ( id) ;
98
87
}
99
88
AssocCtxt :: Impl => {
100
89
let hir_item = lctx. lower_impl_item ( item) ;
101
90
let id = hir_item. impl_item_id ( ) ;
102
91
lctx. impl_items . insert ( id, hir_item) ;
103
- lctx. modules . get_mut ( & lctx. current_module ) . unwrap ( ) . impl_items . insert ( id) ;
92
+ lctx. modules . entry ( lctx. current_module ) . or_default ( ) . impl_items . insert ( id) ;
104
93
}
105
94
} ) ;
106
95
@@ -113,7 +102,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
113
102
let hir_item = lctx. lower_foreign_item ( item) ;
114
103
let id = hir_item. foreign_item_id ( ) ;
115
104
lctx. foreign_items . insert ( id, hir_item) ;
116
- lctx. modules . get_mut ( & lctx. current_module ) . unwrap ( ) . foreign_items . insert ( id) ;
105
+ lctx. modules . entry ( lctx. current_module ) . or_default ( ) . foreign_items . insert ( id) ;
117
106
} ) ;
118
107
119
108
visit:: walk_foreign_item ( self , item) ;
@@ -157,7 +146,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
157
146
& mut self ,
158
147
f : impl FnOnce ( & mut LoweringContext < ' _ , ' _ > ) -> T ,
159
148
) -> T {
160
- let old_in_scope_lifetimes = std :: mem:: replace ( & mut self . in_scope_lifetimes , vec ! [ ] ) ;
149
+ let old_in_scope_lifetimes = mem:: replace ( & mut self . in_scope_lifetimes , vec ! [ ] ) ;
161
150
162
151
// this vector is only used when walking over impl headers,
163
152
// input types, and the like, and should not be non-empty in
@@ -172,12 +161,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
172
161
res
173
162
}
174
163
175
- pub ( super ) fn lower_mod ( & mut self , m : & Mod ) -> hir:: Mod < ' hir > {
164
+ pub ( super ) fn lower_mod ( & mut self , items : & [ P < Item > ] , inner : Span ) -> hir:: Mod < ' hir > {
176
165
hir:: Mod {
177
- inner : m. inner ,
178
- item_ids : self
179
- . arena
180
- . alloc_from_iter ( m. items . iter ( ) . flat_map ( |x| self . lower_item_id ( x) ) ) ,
166
+ inner,
167
+ item_ids : self . arena . alloc_from_iter ( items. iter ( ) . flat_map ( |x| self . lower_item_id ( x) ) ) ,
181
168
}
182
169
}
183
170
@@ -327,7 +314,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
327
314
hir:: ItemKind :: Fn ( sig, generics, body_id)
328
315
} )
329
316
}
330
- ItemKind :: Mod ( ref m) => hir:: ItemKind :: Mod ( self . lower_mod ( m) ) ,
317
+ ItemKind :: Mod ( _, ref mod_kind) => match mod_kind {
318
+ ModKind :: Loaded ( items, _, inner_span) => {
319
+ hir:: ItemKind :: Mod ( self . lower_mod ( items, * inner_span) )
320
+ }
321
+ ModKind :: Unloaded => panic ! ( "`mod` items should have been loaded by now" ) ,
322
+ } ,
331
323
ItemKind :: ForeignMod ( ref fm) => {
332
324
if fm. abi . is_none ( ) {
333
325
self . maybe_lint_missing_abi ( span, id, abi:: Abi :: C ) ;
0 commit comments