@@ -40,7 +40,8 @@ use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
40
40
use rustc_expand:: base:: { DeriveResolutions , SyntaxExtension , SyntaxExtensionKind } ;
41
41
use rustc_hir:: def:: Namespace :: * ;
42
42
use 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 } ;
44
45
use rustc_hir:: definitions:: { DefKey , DefPathData , Definitions } ;
45
46
use rustc_hir:: TraitCandidate ;
46
47
use rustc_index:: vec:: IndexVec ;
@@ -505,10 +506,6 @@ pub struct ModuleData<'a> {
505
506
/// What kind of module this is, because this may not be a `mod`.
506
507
kind : ModuleKind ,
507
508
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
509
/// Mapping between names and their (possibly in-progress) resolutions in this module.
513
510
/// Resolutions in modules from other crates are not populated until accessed.
514
511
lazy_resolutions : Resolutions < ' a > ,
@@ -536,19 +533,16 @@ pub struct ModuleData<'a> {
536
533
type Module < ' a > = & ' a ModuleData < ' a > ;
537
534
538
535
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 {
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
+ } ;
546
541
ModuleData {
547
542
parent,
548
543
kind,
549
- nearest_parent_mod,
550
544
lazy_resolutions : Default :: default ( ) ,
551
- populate_on_access : Cell :: new ( !nearest_parent_mod . is_local ( ) ) ,
545
+ populate_on_access : Cell :: new ( is_foreign ) ,
552
546
unexpanded_invocations : Default :: default ( ) ,
553
547
no_implicit_prelude : false ,
554
548
glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
@@ -623,6 +617,15 @@ impl<'a> ModuleData<'a> {
623
617
}
624
618
}
625
619
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
+
626
629
fn is_ancestor_of ( & self , mut other : & Self ) -> bool {
627
630
while !ptr:: eq ( self , other) {
628
631
if let Some ( parent) = other. parent {
@@ -1260,18 +1263,12 @@ impl<'a> Resolver<'a> {
1260
1263
let root_module_kind = ModuleKind :: Def ( DefKind :: Mod , root_def_id, kw:: Empty ) ;
1261
1264
let graph_root = arenas. alloc_module ( ModuleData {
1262
1265
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 )
1264
1267
} ) ;
1265
1268
let empty_module_kind = ModuleKind :: Def ( DefKind :: Mod , root_def_id, kw:: Empty ) ;
1266
1269
let empty_module = arenas. alloc_module ( ModuleData {
1267
1270
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 )
1275
1272
} ) ;
1276
1273
let mut module_map = FxHashMap :: default ( ) ;
1277
1274
module_map. insert ( root_local_def_id, graph_root) ;
@@ -1636,11 +1633,10 @@ impl<'a> Resolver<'a> {
1636
1633
& self ,
1637
1634
parent : Module < ' a > ,
1638
1635
kind : ModuleKind ,
1639
- nearest_parent_mod : DefId ,
1640
1636
expn_id : ExpnId ,
1641
1637
span : Span ,
1642
1638
) -> 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) ;
1644
1640
self . arenas . alloc_module ( module)
1645
1641
}
1646
1642
@@ -2167,7 +2163,9 @@ impl<'a> Resolver<'a> {
2167
2163
return self . graph_root ;
2168
2164
}
2169
2165
} ;
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
+
2171
2169
debug ! (
2172
2170
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})" ,
2173
2171
ident,
@@ -2179,10 +2177,10 @@ impl<'a> Resolver<'a> {
2179
2177
}
2180
2178
2181
2179
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 ( ) ) ;
2183
2181
while module. span . ctxt ( ) . normalize_to_macros_2_0 ( ) != * ctxt {
2184
2182
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 ( ) ) ;
2186
2184
}
2187
2185
module
2188
2186
}
@@ -2896,7 +2894,7 @@ impl<'a> Resolver<'a> {
2896
2894
}
2897
2895
2898
2896
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 )
2900
2898
}
2901
2899
2902
2900
fn set_binding_parent_module ( & mut self , binding : & ' a NameBinding < ' a > , module : Module < ' a > ) {
@@ -2920,7 +2918,7 @@ impl<'a> Resolver<'a> {
2920
2918
self . binding_parent_modules . get ( & PtrKey ( modularized) ) ,
2921
2919
) {
2922
2920
( 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 ( )
2924
2922
&& modularized. is_ancestor_of ( macro_rules)
2925
2923
}
2926
2924
_ => false ,
0 commit comments