@@ -15,11 +15,11 @@ use rustc_span::source_map::{respan, DesugaringKind};
1515use rustc_span:: symbol:: { kw, sym, Ident } ;
1616use rustc_span:: Span ;
1717use rustc_target:: spec:: abi;
18-
1918use smallvec:: { smallvec, SmallVec } ;
20- use std:: collections:: BTreeSet ;
2119use tracing:: debug;
2220
21+ use std:: mem;
22+
2323pub ( super ) struct ItemLowerer < ' a , ' lowering , ' hir > {
2424 pub ( super ) lctx : & ' a mut LoweringContext < ' lowering , ' hir > ,
2525}
@@ -34,25 +34,6 @@ impl ItemLowerer<'_, '_, '_> {
3434}
3535
3636impl < ' 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-
5637 fn visit_item ( & mut self , item : & ' a Item ) {
5738 let mut item_hir_id = None ;
5839 self . lctx . with_hir_id_owner ( item. id , |lctx| {
@@ -67,10 +48,18 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
6748 if let Some ( hir_id) = item_hir_id {
6849 self . lctx . with_parent_item_lifetime_defs ( hir_id, |this| {
6950 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) ,
7463 }
7564 } ) ;
7665 }
@@ -94,13 +83,13 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
9483 let hir_item = lctx. lower_trait_item ( item) ;
9584 let id = hir_item. trait_item_id ( ) ;
9685 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) ;
9887 }
9988 AssocCtxt :: Impl => {
10089 let hir_item = lctx. lower_impl_item ( item) ;
10190 let id = hir_item. impl_item_id ( ) ;
10291 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) ;
10493 }
10594 } ) ;
10695
@@ -113,7 +102,7 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
113102 let hir_item = lctx. lower_foreign_item ( item) ;
114103 let id = hir_item. foreign_item_id ( ) ;
115104 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) ;
117106 } ) ;
118107
119108 visit:: walk_foreign_item ( self , item) ;
@@ -157,7 +146,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
157146 & mut self ,
158147 f : impl FnOnce ( & mut LoweringContext < ' _ , ' _ > ) -> T ,
159148 ) -> 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 ! [ ] ) ;
161150
162151 // this vector is only used when walking over impl headers,
163152 // input types, and the like, and should not be non-empty in
@@ -172,12 +161,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
172161 res
173162 }
174163
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 > {
176165 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) ) ) ,
181168 }
182169 }
183170
@@ -327,7 +314,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
327314 hir:: ItemKind :: Fn ( sig, generics, body_id)
328315 } )
329316 }
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+ } ,
331323 ItemKind :: ForeignMod ( ref fm) => {
332324 if fm. abi . is_none ( ) {
333325 self . maybe_lint_missing_abi ( span, id, abi:: Abi :: C ) ;
0 commit comments