Skip to content

Commit 624a9b7

Browse files
committed
Avoid building multiple reduced graphs for a crate
that is referenced by multiple `extern crate` items.
1 parent b0e13dc commit 624a9b7

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use {resolve_error, resolve_struct_error, ResolutionError};
2424

2525
use rustc::middle::cstore::LoadedMacros;
2626
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};
2828
use rustc::ty;
2929
use 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>) {

src/librustc_resolve/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,7 @@ pub struct Resolver<'a> {
10831083
// There will be an anonymous module created around `g` with the ID of the
10841084
// entry block for `f`.
10851085
module_map: NodeMap<Module<'a>>,
1086+
extern_crate_roots: FxHashMap<CrateNum, Module<'a>>,
10861087

10871088
// Whether or not to print error messages. Can be set to true
10881089
// when getting additional info for error message suggestions,
@@ -1276,6 +1277,7 @@ impl<'a> Resolver<'a> {
12761277
export_map: NodeMap(),
12771278
trait_map: NodeMap(),
12781279
module_map: module_map,
1280+
extern_crate_roots: FxHashMap(),
12791281

12801282
emit_errors: true,
12811283
make_glob_map: make_glob_map == MakeGlobMap::Yes,

0 commit comments

Comments
 (0)