@@ -48,7 +48,7 @@ use session::config::nightly_options;
48
48
use util:: common:: FN_OUTPUT_NAME ;
49
49
use util:: nodemap:: { DefIdMap , NodeMap } ;
50
50
51
- use std:: collections:: BTreeMap ;
51
+ use std:: collections:: { BTreeSet , BTreeMap } ;
52
52
use std:: fmt:: Debug ;
53
53
use std:: mem;
54
54
use smallvec:: SmallVec ;
@@ -90,6 +90,8 @@ pub struct LoweringContext<'a> {
90
90
trait_impls : BTreeMap < DefId , Vec < NodeId > > ,
91
91
trait_auto_impl : BTreeMap < DefId , NodeId > ,
92
92
93
+ modules : BTreeMap < NodeId , hir:: ModuleItems > ,
94
+
93
95
is_generator : bool ,
94
96
95
97
catch_scopes : Vec < NodeId > ,
@@ -124,6 +126,8 @@ pub struct LoweringContext<'a> {
124
126
// needs to be created for it.
125
127
in_scope_lifetimes : Vec < Ident > ,
126
128
129
+ current_module : NodeId ,
130
+
127
131
type_def_lifetime_params : DefIdMap < usize > ,
128
132
129
133
current_hir_id_owner : Vec < ( DefIndex , u32 ) > ,
@@ -228,12 +232,14 @@ pub fn lower_crate(
228
232
bodies : BTreeMap :: new ( ) ,
229
233
trait_impls : BTreeMap :: new ( ) ,
230
234
trait_auto_impl : BTreeMap :: new ( ) ,
235
+ modules : BTreeMap :: new ( ) ,
231
236
exported_macros : Vec :: new ( ) ,
232
237
catch_scopes : Vec :: new ( ) ,
233
238
loop_scopes : Vec :: new ( ) ,
234
239
is_in_loop_condition : false ,
235
240
anonymous_lifetime_mode : AnonymousLifetimeMode :: PassThrough ,
236
241
type_def_lifetime_params : Default :: default ( ) ,
242
+ current_module : CRATE_NODE_ID ,
237
243
current_hir_id_owner : vec ! [ ( CRATE_DEF_INDEX , 0 ) ] ,
238
244
item_local_id_counters : Default :: default ( ) ,
239
245
node_id_to_hir_id : IndexVec :: new ( ) ,
@@ -356,6 +362,15 @@ impl<'a> LoweringContext<'a> {
356
362
}
357
363
358
364
impl < ' lcx , ' interner > Visitor < ' lcx > for MiscCollector < ' lcx , ' interner > {
365
+ fn visit_mod ( & mut self , m : & ' lcx Mod , _s : Span , _attrs : & [ Attribute ] , n : NodeId ) {
366
+ self . lctx . modules . insert ( n, hir:: ModuleItems {
367
+ items : BTreeSet :: new ( ) ,
368
+ trait_items : BTreeSet :: new ( ) ,
369
+ impl_items : BTreeSet :: new ( ) ,
370
+ } ) ;
371
+ visit:: walk_mod ( self , m) ;
372
+ }
373
+
359
374
fn visit_item ( & mut self , item : & ' lcx Item ) {
360
375
self . lctx . allocate_hir_id_counter ( item. id , item) ;
361
376
@@ -414,11 +429,18 @@ impl<'a> LoweringContext<'a> {
414
429
}
415
430
416
431
impl < ' lcx , ' interner > Visitor < ' lcx > for ItemLowerer < ' lcx , ' interner > {
432
+ fn visit_mod ( & mut self , m : & ' lcx Mod , _s : Span , _attrs : & [ Attribute ] , n : NodeId ) {
433
+ let old = self . lctx . current_module ;
434
+ self . lctx . current_module = n;
435
+ visit:: walk_mod ( self , m) ;
436
+ self . lctx . current_module = old;
437
+ }
438
+
417
439
fn visit_item ( & mut self , item : & ' lcx Item ) {
418
440
let mut item_lowered = true ;
419
441
self . lctx . with_hir_id_owner ( item. id , |lctx| {
420
442
if let Some ( hir_item) = lctx. lower_item ( item) {
421
- lctx. items . insert ( item. id , hir_item) ;
443
+ lctx. insert_item ( item. id , hir_item) ;
422
444
} else {
423
445
item_lowered = false ;
424
446
}
@@ -451,6 +473,7 @@ impl<'a> LoweringContext<'a> {
451
473
let id = hir:: TraitItemId { node_id : item. id } ;
452
474
let hir_item = lctx. lower_trait_item ( item) ;
453
475
lctx. trait_items . insert ( id, hir_item) ;
476
+ lctx. modules . get_mut ( & lctx. current_module ) . unwrap ( ) . trait_items . insert ( id) ;
454
477
} ) ;
455
478
456
479
visit:: walk_trait_item ( self , item) ;
@@ -461,6 +484,7 @@ impl<'a> LoweringContext<'a> {
461
484
let id = hir:: ImplItemId { node_id : item. id } ;
462
485
let hir_item = lctx. lower_impl_item ( item) ;
463
486
lctx. impl_items . insert ( id, hir_item) ;
487
+ lctx. modules . get_mut ( & lctx. current_module ) . unwrap ( ) . impl_items . insert ( id) ;
464
488
} ) ;
465
489
visit:: walk_impl_item ( self , item) ;
466
490
}
@@ -492,9 +516,15 @@ impl<'a> LoweringContext<'a> {
492
516
body_ids,
493
517
trait_impls : self . trait_impls ,
494
518
trait_auto_impl : self . trait_auto_impl ,
519
+ modules : self . modules ,
495
520
}
496
521
}
497
522
523
+ fn insert_item ( & mut self , id : NodeId , item : hir:: Item ) {
524
+ self . items . insert ( id, item) ;
525
+ self . modules . get_mut ( & self . current_module ) . unwrap ( ) . items . insert ( id) ;
526
+ }
527
+
498
528
fn allocate_hir_id_counter < T : Debug > ( & mut self , owner : NodeId , debug : & T ) -> LoweredNodeId {
499
529
if self . item_local_id_counters . insert ( owner, 0 ) . is_some ( ) {
500
530
bug ! (
@@ -1370,7 +1400,7 @@ impl<'a> LoweringContext<'a> {
1370
1400
// Insert the item into the global list. This usually happens
1371
1401
// automatically for all AST items. But this existential type item
1372
1402
// does not actually exist in the AST.
1373
- lctx. items . insert ( exist_ty_id. node_id , exist_ty_item) ;
1403
+ lctx. insert_item ( exist_ty_id. node_id , exist_ty_item) ;
1374
1404
1375
1405
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
1376
1406
hir:: TyKind :: Def ( hir:: ItemId { id : exist_ty_id. node_id } , lifetimes)
@@ -3026,7 +3056,7 @@ impl<'a> LoweringContext<'a> {
3026
3056
} ;
3027
3057
let vis = respan ( vis. span , vis_kind) ;
3028
3058
3029
- this. items . insert (
3059
+ this. insert_item (
3030
3060
new_id. node_id ,
3031
3061
hir:: Item {
3032
3062
id : new_id. node_id ,
@@ -3133,7 +3163,7 @@ impl<'a> LoweringContext<'a> {
3133
3163
} ;
3134
3164
let vis = respan ( vis. span , vis_kind) ;
3135
3165
3136
- this. items . insert (
3166
+ this. insert_item (
3137
3167
new_id,
3138
3168
hir:: Item {
3139
3169
id : new_id,
0 commit comments