@@ -40,7 +40,8 @@ use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
4040use rustc_expand:: base:: { DeriveResolutions , SyntaxExtension , SyntaxExtensionKind } ;
4141use rustc_hir:: def:: Namespace :: * ;
4242use rustc_hir:: def:: { self , CtorOf , DefKind , NonMacroAttrKind , PartialRes } ;
43- use rustc_hir:: def_id:: { CrateNum , DefId , DefIdMap , DefPathHash , LocalDefId , CRATE_DEF_INDEX } ;
43+ use rustc_hir:: def_id:: { CrateNum , DefId , DefIdMap , DefPathHash , LocalDefId } ;
44+ use rustc_hir:: def_id:: { CRATE_DEF_INDEX , LOCAL_CRATE } ;
4445use rustc_hir:: definitions:: { DefKey , DefPathData , Definitions } ;
4546use rustc_hir:: TraitCandidate ;
4647use rustc_index:: vec:: IndexVec ;
@@ -505,10 +506,6 @@ pub struct ModuleData<'a> {
505506 /// What kind of module this is, because this may not be a `mod`.
506507 kind : ModuleKind ,
507508
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-
512509 /// Mapping between names and their (possibly in-progress) resolutions in this module.
513510 /// Resolutions in modules from other crates are not populated until accessed.
514511 lazy_resolutions : Resolutions < ' a > ,
@@ -536,19 +533,16 @@ pub struct ModuleData<'a> {
536533type Module < ' a > = & ' a ModuleData < ' a > ;
537534
538535impl < ' 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 {
536+ fn new ( parent : Option < Module < ' a > > , kind : ModuleKind , expansion : ExpnId , span : Span ) -> Self {
537+ let is_foreign = match kind {
538+ ModuleKind :: Def ( _, def_id, _) => !def_id. is_local ( ) ,
539+ ModuleKind :: Block ( _) => false ,
540+ } ;
546541 ModuleData {
547542 parent,
548543 kind,
549- nearest_parent_mod,
550544 lazy_resolutions : Default :: default ( ) ,
551- populate_on_access : Cell :: new ( !nearest_parent_mod . is_local ( ) ) ,
545+ populate_on_access : Cell :: new ( is_foreign ) ,
552546 unexpanded_invocations : Default :: default ( ) ,
553547 no_implicit_prelude : false ,
554548 glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
@@ -623,6 +617,15 @@ impl<'a> ModuleData<'a> {
623617 }
624618 }
625619
620+ /// The [`DefId`] of the nearest `mod` item ancestor (which may be this module).
621+ /// This may be the crate root.
622+ fn nearest_parent_mod ( & self ) -> DefId {
623+ match self . kind {
624+ ModuleKind :: Def ( DefKind :: Mod , def_id, _) => def_id,
625+ _ => self . parent . expect ( "non-root module without parent" ) . nearest_parent_mod ( ) ,
626+ }
627+ }
628+
626629 fn is_ancestor_of ( & self , mut other : & Self ) -> bool {
627630 while !ptr:: eq ( self , other) {
628631 if let Some ( parent) = other. parent {
@@ -1260,18 +1263,12 @@ impl<'a> Resolver<'a> {
12601263 let root_module_kind = ModuleKind :: Def ( DefKind :: Mod , root_def_id, kw:: Empty ) ;
12611264 let graph_root = arenas. alloc_module ( ModuleData {
12621265 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 )
1266+ ..ModuleData :: new ( None , root_module_kind, ExpnId :: root ( ) , krate. span )
12641267 } ) ;
12651268 let empty_module_kind = ModuleKind :: Def ( DefKind :: Mod , root_def_id, kw:: Empty ) ;
12661269 let empty_module = arenas. alloc_module ( ModuleData {
12671270 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- )
1271+ ..ModuleData :: new ( Some ( graph_root) , empty_module_kind, ExpnId :: root ( ) , DUMMY_SP )
12751272 } ) ;
12761273 let mut module_map = FxHashMap :: default ( ) ;
12771274 module_map. insert ( root_local_def_id, graph_root) ;
@@ -1636,11 +1633,10 @@ impl<'a> Resolver<'a> {
16361633 & self ,
16371634 parent : Module < ' a > ,
16381635 kind : ModuleKind ,
1639- nearest_parent_mod : DefId ,
16401636 expn_id : ExpnId ,
16411637 span : Span ,
16421638 ) -> Module < ' a > {
1643- let module = ModuleData :: new ( Some ( parent) , kind, nearest_parent_mod , expn_id, span) ;
1639+ let module = ModuleData :: new ( Some ( parent) , kind, expn_id, span) ;
16441640 self . arenas . alloc_module ( module)
16451641 }
16461642
@@ -2167,7 +2163,9 @@ impl<'a> Resolver<'a> {
21672163 return self . graph_root ;
21682164 }
21692165 } ;
2170- let module = self . get_module ( DefId { index : CRATE_DEF_INDEX , ..module. nearest_parent_mod } ) ;
2166+ let module =
2167+ self . get_module ( module. def_id ( ) . map_or ( LOCAL_CRATE , |def_id| def_id. krate ) . as_def_id ( ) ) ;
2168+
21712169 debug ! (
21722170 "resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})" ,
21732171 ident,
@@ -2179,10 +2177,10 @@ impl<'a> Resolver<'a> {
21792177 }
21802178
21812179 fn resolve_self ( & mut self , ctxt : & mut SyntaxContext , module : Module < ' a > ) -> Module < ' a > {
2182- let mut module = self . get_module ( module. nearest_parent_mod ) ;
2180+ let mut module = self . get_module ( module. nearest_parent_mod ( ) ) ;
21832181 while module. span . ctxt ( ) . normalize_to_macros_2_0 ( ) != * ctxt {
21842182 let parent = module. parent . unwrap_or_else ( || self . macro_def_scope ( ctxt. remove_mark ( ) ) ) ;
2185- module = self . get_module ( parent. nearest_parent_mod ) ;
2183+ module = self . get_module ( parent. nearest_parent_mod ( ) ) ;
21862184 }
21872185 module
21882186 }
@@ -2896,7 +2894,7 @@ impl<'a> Resolver<'a> {
28962894 }
28972895
28982896 fn is_accessible_from ( & self , vis : ty:: Visibility , module : Module < ' a > ) -> bool {
2899- vis. is_accessible_from ( module. nearest_parent_mod , self )
2897+ vis. is_accessible_from ( module. nearest_parent_mod ( ) , self )
29002898 }
29012899
29022900 fn set_binding_parent_module ( & mut self , binding : & ' a NameBinding < ' a > , module : Module < ' a > ) {
@@ -2920,7 +2918,7 @@ impl<'a> Resolver<'a> {
29202918 self . binding_parent_modules . get ( & PtrKey ( modularized) ) ,
29212919 ) {
29222920 ( Some ( macro_rules) , Some ( modularized) ) => {
2923- macro_rules. nearest_parent_mod == modularized. nearest_parent_mod
2921+ macro_rules. nearest_parent_mod ( ) == modularized. nearest_parent_mod ( )
29242922 && modularized. is_ancestor_of ( macro_rules)
29252923 }
29262924 _ => false ,
0 commit comments