@@ -24,7 +24,7 @@ use {resolve_error, resolve_struct_error, ResolutionError};
2424
2525use rustc:: middle:: cstore:: LoadedMacros ;
2626use rustc:: hir:: def:: * ;
27- use rustc:: hir:: def_id:: { CRATE_DEF_INDEX , DefId } ;
27+ use rustc:: hir:: def_id:: { CrateNum , CRATE_DEF_INDEX , DefId } ;
2828use rustc:: ty;
2929use rustc:: util:: nodemap:: FxHashMap ;
3030
@@ -233,14 +233,7 @@ impl<'b> Resolver<'b> {
233233 // n.b. we don't need to look at the path option here, because cstore already did
234234 let crate_id = self . session . cstore . extern_mod_stmt_cnum ( item. id ) ;
235235 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) ;
244237 let binding = ( module, sp, ty:: Visibility :: Public ) . to_name_binding ( ) ;
245238 let binding = self . arenas . alloc_name_binding ( binding) ;
246239 let directive = self . arenas . alloc_import_directive ( ImportDirective {
@@ -504,6 +497,17 @@ impl<'b> Resolver<'b> {
504497 }
505498 }
506499
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+
507511 /// Ensures that the reduced graph rooted at the given external module
508512 /// is built, building it if it is not.
509513 pub fn populate_module_if_necessary ( & mut self , module : Module < ' b > ) {
0 commit comments