@@ -505,10 +505,6 @@ pub struct ModuleData<'a> {
505505 /// What kind of module this is, because this may not be a `mod`.
506506 kind : ModuleKind ,
507507
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-
512508 /// Mapping between names and their (possibly in-progress) resolutions in this module.
513509 /// Resolutions in modules from other crates are not populated until accessed.
514510 lazy_resolutions : Resolutions < ' a > ,
@@ -536,19 +532,16 @@ pub struct ModuleData<'a> {
536532type Module < ' a > = & ' a ModuleData < ' a > ;
537533
538534impl < ' 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+ } ;
546540 ModuleData {
547541 parent,
548542 kind,
549- nearest_parent_mod,
550543 lazy_resolutions : Default :: default ( ) ,
551- populate_on_access : Cell :: new ( !nearest_parent_mod . is_local ( ) ) ,
544+ populate_on_access : Cell :: new ( is_foreign ) ,
552545 unexpanded_invocations : Default :: default ( ) ,
553546 no_implicit_prelude : false ,
554547 glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
@@ -559,6 +552,13 @@ impl<'a> ModuleData<'a> {
559552 }
560553 }
561554
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+
562562 fn for_each_child < R , F > ( & ' a self , resolver : & mut R , mut f : F )
563563 where
564564 R : AsMut < Resolver < ' a > > ,
@@ -1260,18 +1260,12 @@ impl<'a> Resolver<'a> {
12601260 let root_module_kind = ModuleKind :: Def ( DefKind :: Mod , root_def_id, kw:: Empty ) ;
12611261 let graph_root = arenas. alloc_module ( ModuleData {
12621262 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 )
12641264 } ) ;
12651265 let empty_module_kind = ModuleKind :: Def ( DefKind :: Mod , root_def_id, kw:: Empty ) ;
12661266 let empty_module = arenas. alloc_module ( ModuleData {
12671267 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 )
12751269 } ) ;
12761270 let mut module_map = FxHashMap :: default ( ) ;
12771271 module_map. insert ( root_local_def_id, graph_root) ;
@@ -1636,11 +1630,10 @@ impl<'a> Resolver<'a> {
16361630 & self ,
16371631 parent : Module < ' a > ,
16381632 kind : ModuleKind ,
1639- nearest_parent_mod : DefId ,
16401633 expn_id : ExpnId ,
16411634 span : Span ,
16421635 ) -> 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) ;
16441637 self . arenas . alloc_module ( module)
16451638 }
16461639
@@ -2167,7 +2160,8 @@ impl<'a> Resolver<'a> {
21672160 return self . graph_root ;
21682161 }
21692162 } ;
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 ( ) } ) ;
21712165 debug ! (
21722166 "resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})" ,
21732167 ident,
@@ -2179,10 +2173,10 @@ impl<'a> Resolver<'a> {
21792173 }
21802174
21812175 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 ( ) ) ;
21832177 while module. span . ctxt ( ) . normalize_to_macros_2_0 ( ) != * ctxt {
21842178 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 ( ) ) ;
21862180 }
21872181 module
21882182 }
@@ -2896,7 +2890,7 @@ impl<'a> Resolver<'a> {
28962890 }
28972891
28982892 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 )
29002894 }
29012895
29022896 fn set_binding_parent_module ( & mut self , binding : & ' a NameBinding < ' a > , module : Module < ' a > ) {
@@ -2920,7 +2914,7 @@ impl<'a> Resolver<'a> {
29202914 self . binding_parent_modules . get ( & PtrKey ( modularized) ) ,
29212915 ) {
29222916 ( 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 ( )
29242918 && modularized. is_ancestor_of ( macro_rules)
29252919 }
29262920 _ => false ,
0 commit comments