@@ -48,7 +48,7 @@ use session::config::nightly_options;
4848use  util:: common:: FN_OUTPUT_NAME ; 
4949use  util:: nodemap:: { DefIdMap ,  NodeMap } ; 
5050
51- use  std:: collections:: BTreeMap ; 
51+ use  std:: collections:: { BTreeSet ,   BTreeMap } ; 
5252use  std:: fmt:: Debug ; 
5353use  std:: mem; 
5454use  smallvec:: SmallVec ; 
@@ -90,6 +90,8 @@ pub struct LoweringContext<'a> {
9090    trait_impls :  BTreeMap < DefId ,  Vec < NodeId > > , 
9191    trait_auto_impl :  BTreeMap < DefId ,  NodeId > , 
9292
93+     modules :  BTreeMap < NodeId ,  hir:: ModuleItems > , 
94+ 
9395    is_generator :  bool , 
9496
9597    catch_scopes :  Vec < NodeId > , 
@@ -124,6 +126,8 @@ pub struct LoweringContext<'a> {
124126    // needs to be created for it. 
125127    in_scope_lifetimes :  Vec < Ident > , 
126128
129+     current_module :  NodeId , 
130+ 
127131    type_def_lifetime_params :  DefIdMap < usize > , 
128132
129133    current_hir_id_owner :  Vec < ( DefIndex ,  u32 ) > , 
@@ -228,12 +232,14 @@ pub fn lower_crate(
228232        bodies :  BTreeMap :: new ( ) , 
229233        trait_impls :  BTreeMap :: new ( ) , 
230234        trait_auto_impl :  BTreeMap :: new ( ) , 
235+         modules :  BTreeMap :: new ( ) , 
231236        exported_macros :  Vec :: new ( ) , 
232237        catch_scopes :  Vec :: new ( ) , 
233238        loop_scopes :  Vec :: new ( ) , 
234239        is_in_loop_condition :  false , 
235240        anonymous_lifetime_mode :  AnonymousLifetimeMode :: PassThrough , 
236241        type_def_lifetime_params :  Default :: default ( ) , 
242+         current_module :  CRATE_NODE_ID , 
237243        current_hir_id_owner :  vec ! [ ( CRATE_DEF_INDEX ,  0 ) ] , 
238244        item_local_id_counters :  Default :: default ( ) , 
239245        node_id_to_hir_id :  IndexVec :: new ( ) , 
@@ -414,11 +420,24 @@ impl<'a> LoweringContext<'a> {
414420        } 
415421
416422        impl < ' lcx ,  ' interner >  Visitor < ' lcx >  for  ItemLowerer < ' lcx ,  ' interner >  { 
423+             fn  visit_mod ( & mut  self ,  m :  & ' lcx  Mod ,  _s :  Span ,  _attrs :  & [ Attribute ] ,  n :  NodeId )  { 
424+                 self . lctx . modules . insert ( n,  hir:: ModuleItems  { 
425+                     items :  BTreeSet :: new ( ) , 
426+                     trait_items :  BTreeSet :: new ( ) , 
427+                     impl_items :  BTreeSet :: new ( ) , 
428+                 } ) ; 
429+ 
430+                 let  old = self . lctx . current_module ; 
431+                 self . lctx . current_module  = n; 
432+                 visit:: walk_mod ( self ,  m) ; 
433+                 self . lctx . current_module  = old; 
434+             } 
435+ 
417436            fn  visit_item ( & mut  self ,  item :  & ' lcx  Item )  { 
418437                let  mut  item_lowered = true ; 
419438                self . lctx . with_hir_id_owner ( item. id ,  |lctx| { 
420439                    if  let  Some ( hir_item)  = lctx. lower_item ( item)  { 
421-                         lctx. items . insert ( item. id ,  hir_item) ; 
440+                         lctx. insert_item ( item. id ,  hir_item) ; 
422441                    }  else  { 
423442                        item_lowered = false ; 
424443                    } 
@@ -451,6 +470,7 @@ impl<'a> LoweringContext<'a> {
451470                    let  id = hir:: TraitItemId  {  node_id :  item. id  } ; 
452471                    let  hir_item = lctx. lower_trait_item ( item) ; 
453472                    lctx. trait_items . insert ( id,  hir_item) ; 
473+                     lctx. modules . get_mut ( & lctx. current_module ) . unwrap ( ) . trait_items . insert ( id) ; 
454474                } ) ; 
455475
456476                visit:: walk_trait_item ( self ,  item) ; 
@@ -461,6 +481,7 @@ impl<'a> LoweringContext<'a> {
461481                    let  id = hir:: ImplItemId  {  node_id :  item. id  } ; 
462482                    let  hir_item = lctx. lower_impl_item ( item) ; 
463483                    lctx. impl_items . insert ( id,  hir_item) ; 
484+                     lctx. modules . get_mut ( & lctx. current_module ) . unwrap ( ) . impl_items . insert ( id) ; 
464485                } ) ; 
465486                visit:: walk_impl_item ( self ,  item) ; 
466487            } 
@@ -492,9 +513,15 @@ impl<'a> LoweringContext<'a> {
492513            body_ids, 
493514            trait_impls :  self . trait_impls , 
494515            trait_auto_impl :  self . trait_auto_impl , 
516+             modules :  self . modules , 
495517        } 
496518    } 
497519
520+     fn  insert_item ( & mut  self ,  id :  NodeId ,  item :  hir:: Item )  { 
521+         self . items . insert ( id,  item) ; 
522+         self . modules . get_mut ( & self . current_module ) . unwrap ( ) . items . insert ( id) ; 
523+     } 
524+ 
498525    fn  allocate_hir_id_counter < T :  Debug > ( & mut  self ,  owner :  NodeId ,  debug :  & T )  -> LoweredNodeId  { 
499526        if  self . item_local_id_counters . insert ( owner,  0 ) . is_some ( )  { 
500527            bug ! ( 
@@ -1370,7 +1397,7 @@ impl<'a> LoweringContext<'a> {
13701397            // Insert the item into the global list. This usually happens 
13711398            // automatically for all AST items. But this existential type item 
13721399            // does not actually exist in the AST. 
1373-             lctx. items . insert ( exist_ty_id. node_id ,  exist_ty_item) ; 
1400+             lctx. insert_item ( exist_ty_id. node_id ,  exist_ty_item) ; 
13741401
13751402            // `impl Trait` now just becomes `Foo<'a, 'b, ..>`. 
13761403            hir:: TyKind :: Def ( hir:: ItemId  {  id :  exist_ty_id. node_id  } ,  lifetimes) 
@@ -3026,7 +3053,7 @@ impl<'a> LoweringContext<'a> {
30263053                        } ; 
30273054                        let  vis = respan ( vis. span ,  vis_kind) ; 
30283055
3029-                         this. items . insert ( 
3056+                         this. insert_item ( 
30303057                            new_id. node_id , 
30313058                            hir:: Item  { 
30323059                                id :  new_id. node_id , 
@@ -3133,7 +3160,7 @@ impl<'a> LoweringContext<'a> {
31333160                        } ; 
31343161                        let  vis = respan ( vis. span ,  vis_kind) ; 
31353162
3136-                         this. items . insert ( 
3163+                         this. insert_item ( 
31373164                            new_id, 
31383165                            hir:: Item  { 
31393166                                id :  new_id, 
0 commit comments