@@ -505,10 +505,6 @@ pub struct ModuleData<'a> {
505
505
/// What kind of module this is, because this may not be a `mod`.
506
506
kind : ModuleKind ,
507
507
508
- /// The [`DefId`] of the nearest `mod` item ancestor (which may be this module).
509
- /// This may be the crate root.
510
- nearest_parent_mod : DefId ,
511
-
512
508
/// Mapping between names and their (possibly in-progress) resolutions in this module.
513
509
/// Resolutions in modules from other crates are not populated until accessed.
514
510
lazy_resolutions : Resolutions < ' a > ,
@@ -536,19 +532,16 @@ pub struct ModuleData<'a> {
536
532
type Module < ' a > = & ' a ModuleData < ' a > ;
537
533
538
534
impl < ' a > ModuleData < ' a > {
539
- fn new (
540
- parent : Option < Module < ' a > > ,
541
- kind : ModuleKind ,
542
- nearest_parent_mod : DefId ,
543
- expansion : ExpnId ,
544
- span : Span ,
545
- ) -> Self {
535
+ fn new ( parent : Option < Module < ' a > > , kind : ModuleKind , expansion : ExpnId , span : Span ) -> Self {
536
+ let is_foreign = match kind {
537
+ ModuleKind :: Def ( _, def_id, _) => !def_id. is_local ( ) ,
538
+ ModuleKind :: Block ( _) => false ,
539
+ } ;
546
540
ModuleData {
547
541
parent,
548
542
kind,
549
- nearest_parent_mod,
550
543
lazy_resolutions : Default :: default ( ) ,
551
- populate_on_access : Cell :: new ( !nearest_parent_mod . is_local ( ) ) ,
544
+ populate_on_access : Cell :: new ( is_foreign ) ,
552
545
unexpanded_invocations : Default :: default ( ) ,
553
546
no_implicit_prelude : false ,
554
547
glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
@@ -559,6 +552,13 @@ impl<'a> ModuleData<'a> {
559
552
}
560
553
}
561
554
555
+ fn nearest_parent_mod ( & self ) -> DefId {
556
+ match self . kind {
557
+ ModuleKind :: Def ( DefKind :: Mod , def_id, _) => def_id,
558
+ _ => self . parent . expect ( "non-root module without parent" ) . nearest_parent_mod ( ) ,
559
+ }
560
+ }
561
+
562
562
fn for_each_child < R , F > ( & ' a self , resolver : & mut R , mut f : F )
563
563
where
564
564
R : AsMut < Resolver < ' a > > ,
@@ -1260,18 +1260,12 @@ impl<'a> Resolver<'a> {
1260
1260
let root_module_kind = ModuleKind :: Def ( DefKind :: Mod , root_def_id, kw:: Empty ) ;
1261
1261
let graph_root = arenas. alloc_module ( ModuleData {
1262
1262
no_implicit_prelude : session. contains_name ( & krate. attrs , sym:: no_implicit_prelude) ,
1263
- ..ModuleData :: new ( None , root_module_kind, root_def_id , ExpnId :: root ( ) , krate. span )
1263
+ ..ModuleData :: new ( None , root_module_kind, ExpnId :: root ( ) , krate. span )
1264
1264
} ) ;
1265
1265
let empty_module_kind = ModuleKind :: Def ( DefKind :: Mod , root_def_id, kw:: Empty ) ;
1266
1266
let empty_module = arenas. alloc_module ( ModuleData {
1267
1267
no_implicit_prelude : true ,
1268
- ..ModuleData :: new (
1269
- Some ( graph_root) ,
1270
- empty_module_kind,
1271
- root_def_id,
1272
- ExpnId :: root ( ) ,
1273
- DUMMY_SP ,
1274
- )
1268
+ ..ModuleData :: new ( Some ( graph_root) , empty_module_kind, ExpnId :: root ( ) , DUMMY_SP )
1275
1269
} ) ;
1276
1270
let mut module_map = FxHashMap :: default ( ) ;
1277
1271
module_map. insert ( root_local_def_id, graph_root) ;
@@ -1636,11 +1630,10 @@ impl<'a> Resolver<'a> {
1636
1630
& self ,
1637
1631
parent : Module < ' a > ,
1638
1632
kind : ModuleKind ,
1639
- nearest_parent_mod : DefId ,
1640
1633
expn_id : ExpnId ,
1641
1634
span : Span ,
1642
1635
) -> Module < ' a > {
1643
- let module = ModuleData :: new ( Some ( parent) , kind, nearest_parent_mod , expn_id, span) ;
1636
+ let module = ModuleData :: new ( Some ( parent) , kind, expn_id, span) ;
1644
1637
self . arenas . alloc_module ( module)
1645
1638
}
1646
1639
@@ -2167,7 +2160,8 @@ impl<'a> Resolver<'a> {
2167
2160
return self . graph_root ;
2168
2161
}
2169
2162
} ;
2170
- let module = self . get_module ( DefId { index : CRATE_DEF_INDEX , ..module. nearest_parent_mod } ) ;
2163
+ let module =
2164
+ self . get_module ( DefId { index : CRATE_DEF_INDEX , ..module. nearest_parent_mod ( ) } ) ;
2171
2165
debug ! (
2172
2166
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})" ,
2173
2167
ident,
@@ -2179,10 +2173,10 @@ impl<'a> Resolver<'a> {
2179
2173
}
2180
2174
2181
2175
fn resolve_self ( & mut self , ctxt : & mut SyntaxContext , module : Module < ' a > ) -> Module < ' a > {
2182
- let mut module = self . get_module ( module. nearest_parent_mod ) ;
2176
+ let mut module = self . get_module ( module. nearest_parent_mod ( ) ) ;
2183
2177
while module. span . ctxt ( ) . normalize_to_macros_2_0 ( ) != * ctxt {
2184
2178
let parent = module. parent . unwrap_or_else ( || self . macro_def_scope ( ctxt. remove_mark ( ) ) ) ;
2185
- module = self . get_module ( parent. nearest_parent_mod ) ;
2179
+ module = self . get_module ( parent. nearest_parent_mod ( ) ) ;
2186
2180
}
2187
2181
module
2188
2182
}
@@ -2896,7 +2890,7 @@ impl<'a> Resolver<'a> {
2896
2890
}
2897
2891
2898
2892
fn is_accessible_from ( & self , vis : ty:: Visibility , module : Module < ' a > ) -> bool {
2899
- vis. is_accessible_from ( module. nearest_parent_mod , self )
2893
+ vis. is_accessible_from ( module. nearest_parent_mod ( ) , self )
2900
2894
}
2901
2895
2902
2896
fn set_binding_parent_module ( & mut self , binding : & ' a NameBinding < ' a > , module : Module < ' a > ) {
@@ -2920,7 +2914,7 @@ impl<'a> Resolver<'a> {
2920
2914
self . binding_parent_modules . get ( & PtrKey ( modularized) ) ,
2921
2915
) {
2922
2916
( Some ( macro_rules) , Some ( modularized) ) => {
2923
- macro_rules. nearest_parent_mod == modularized. nearest_parent_mod
2917
+ macro_rules. nearest_parent_mod ( ) == modularized. nearest_parent_mod ( )
2924
2918
&& modularized. is_ancestor_of ( macro_rules)
2925
2919
}
2926
2920
_ => false ,
0 commit comments