@@ -24,7 +24,7 @@ use {resolve_error, resolve_struct_error, ResolutionError};
24
24
25
25
use rustc:: middle:: cstore:: LoadedMacros ;
26
26
use rustc:: hir:: def:: * ;
27
- use rustc:: hir:: def_id:: { CRATE_DEF_INDEX , DefId } ;
27
+ use rustc:: hir:: def_id:: { CrateNum , CRATE_DEF_INDEX , DefId } ;
28
28
use rustc:: ty;
29
29
use rustc:: util:: nodemap:: FxHashMap ;
30
30
@@ -233,14 +233,7 @@ impl<'b> Resolver<'b> {
233
233
// n.b. we don't need to look at the path option here, because cstore already did
234
234
let crate_id = self . session . cstore . extern_mod_stmt_cnum ( item. id ) ;
235
235
let module = if let Some ( crate_id) = crate_id {
236
- let def_id = DefId {
237
- krate : crate_id,
238
- index : CRATE_DEF_INDEX ,
239
- } ;
240
- let module = self . arenas . alloc_module ( ModuleS {
241
- populated : Cell :: new ( false ) ,
242
- ..ModuleS :: new ( Some ( parent) , ModuleKind :: Def ( Def :: Mod ( def_id) , name) )
243
- } ) ;
236
+ let module = self . get_extern_crate_root ( crate_id) ;
244
237
let binding = ( module, sp, ty:: Visibility :: Public ) . to_name_binding ( ) ;
245
238
let binding = self . arenas . alloc_name_binding ( binding) ;
246
239
let directive = self . arenas . alloc_import_directive ( ImportDirective {
@@ -504,6 +497,17 @@ impl<'b> Resolver<'b> {
504
497
}
505
498
}
506
499
500
+ fn get_extern_crate_root ( & mut self , cnum : CrateNum ) -> Module < ' b > {
501
+ let def_id = DefId { krate : cnum, index : CRATE_DEF_INDEX } ;
502
+ let arenas = self . arenas ;
503
+ * self . extern_crate_roots . entry ( cnum) . or_insert_with ( || {
504
+ arenas. alloc_module ( ModuleS {
505
+ populated : Cell :: new ( false ) ,
506
+ ..ModuleS :: new ( None , ModuleKind :: Def ( Def :: Mod ( def_id) , keywords:: Invalid . name ( ) ) )
507
+ } )
508
+ } )
509
+ }
510
+
507
511
/// Ensures that the reduced graph rooted at the given external module
508
512
/// is built, building it if it is not.
509
513
pub fn populate_module_if_necessary ( & mut self , module : Module < ' b > ) {
0 commit comments