From 48356987c18d77605342d3bd5f5dc98d88f1d245 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Mon, 12 Jun 2017 00:59:22 -0700 Subject: [PATCH] On-demandify extern_crate --- src/librustc/middle/cstore.rs | 2 -- src/librustc/ty/item_path.rs | 6 +++--- src/librustc/ty/maps.rs | 10 +++++++++- src/librustc_metadata/cstore_impl.rs | 8 ++------ src/librustc_save_analysis/lib.rs | 2 +- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/librustc/middle/cstore.rs b/src/librustc/middle/cstore.rs index 6631e919e42..0a4cc937739 100644 --- a/src/librustc/middle/cstore.rs +++ b/src/librustc/middle/cstore.rs @@ -254,7 +254,6 @@ pub trait CrateStore { fn is_compiler_builtins(&self, cnum: CrateNum) -> bool; fn is_sanitizer_runtime(&self, cnum: CrateNum) -> bool; fn panic_strategy(&self, cnum: CrateNum) -> PanicStrategy; - fn extern_crate(&self, cnum: CrateNum) -> Option; /// The name of the crate as it is referred to in source code of the current /// crate. fn crate_name(&self, cnum: CrateNum) -> Symbol; @@ -374,7 +373,6 @@ impl CrateStore for DummyCrateStore { fn panic_strategy(&self, cnum: CrateNum) -> PanicStrategy { bug!("panic_strategy") } - fn extern_crate(&self, cnum: CrateNum) -> Option { bug!("extern_crate") } fn crate_name(&self, cnum: CrateNum) -> Symbol { bug!("crate_name") } fn original_crate_name(&self, cnum: CrateNum) -> Symbol { bug!("original_crate_name") diff --git a/src/librustc/ty/item_path.rs b/src/librustc/ty/item_path.rs index 30dcc3fc810..e63fe42877f 100644 --- a/src/librustc/ty/item_path.rs +++ b/src/librustc/ty/item_path.rs @@ -100,7 +100,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { // // Returns `None` for the local crate. if cnum != LOCAL_CRATE { - let opt_extern_crate = self.sess.cstore.extern_crate(cnum); + let opt_extern_crate = self.extern_crate(cnum); let opt_extern_crate = opt_extern_crate.and_then(|extern_crate| { if extern_crate.direct { Some(extern_crate.def_id) @@ -136,8 +136,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { // If `cur_def` is a direct or injected extern crate, push the path to the crate // followed by the path to the item within the crate and return. if cur_def.index == CRATE_DEF_INDEX { - match self.sess.cstore.extern_crate(cur_def.krate) { - Some(extern_crate) if extern_crate.direct => { + match *self.extern_crate(cur_def.krate) { + Some(ref extern_crate) if extern_crate.direct => { self.push_item_path(buffer, extern_crate.def_id); cur_path.iter().rev().map(|segment| buffer.push(&segment.as_str())).count(); return true; diff --git a/src/librustc/ty/maps.rs b/src/librustc/ty/maps.rs index abfd021e4f1..27713f728ac 100644 --- a/src/librustc/ty/maps.rs +++ b/src/librustc/ty/maps.rs @@ -13,7 +13,7 @@ use hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId, LOCAL_CRATE}; use hir::def::Def; use hir; use middle::const_val; -use middle::cstore::LinkagePreference; +use middle::cstore::{ExternCrate, LinkagePreference}; use middle::privacy::AccessLevels; use middle::region::RegionMaps; use mir; @@ -501,6 +501,12 @@ impl<'tcx> QueryDescription for queries::is_panic_runtime<'tcx> { } } +impl<'tcx> QueryDescription for queries::extern_crate<'tcx> { + fn describe(_: TyCtxt, _: CrateNum) -> String { + "getting crate's ExternCrateData".to_string() + } +} + macro_rules! define_maps { (<$tcx:tt> $($(#[$attr:meta])* @@ -963,6 +969,8 @@ define_maps! { <'tcx> [] is_allocator: MetaDataByCrateNum(CrateNum) -> bool, [] is_panic_runtime: MetaDataByCrateNum(CrateNum) -> bool, + + [] extern_crate: MetaDataByCrateNum(CrateNum) -> Rc>, } fn type_param_predicates((item_id, param_id): (DefId, DefId)) -> DepConstructor { diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index ca45696360e..9522d9625c9 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -14,7 +14,7 @@ use schema; use rustc::dep_graph::DepTrackingMapConfig; use rustc::middle::cstore::{CrateStore, CrateSource, LibSource, DepKind, - ExternCrate, NativeLibrary, MetadataLoader, LinkMeta, + NativeLibrary, MetadataLoader, LinkMeta, LinkagePreference, LoadedMacro, EncodedMetadata}; use rustc::hir::def; use rustc::middle::lang_items; @@ -156,6 +156,7 @@ provide! { <'tcx> tcx, def_id, cdata, cnum, dylib_dependency_formats => { Rc::new(cdata.get_dylib_dependency_formats(&tcx.dep_graph)) } is_allocator => { cdata.is_allocator(&tcx.dep_graph) } is_panic_runtime => { cdata.is_panic_runtime(&tcx.dep_graph) } + extern_crate => { Rc::new(cdata.extern_crate.get()) } } } @@ -283,11 +284,6 @@ impl CrateStore for cstore::CStore { self.get_crate_data(cnum).name() } - fn extern_crate(&self, cnum: CrateNum) -> Option - { - self.get_crate_data(cnum).extern_crate.get() - } - fn crate_hash(&self, cnum: CrateNum) -> Svh { self.get_crate_hash(cnum) diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index 4802494f08e..814a45ef636 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -107,7 +107,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { let mut result = Vec::new(); for n in self.tcx.sess.cstore.crates() { - let span = match self.tcx.sess.cstore.extern_crate(n) { + let span = match *self.tcx.extern_crate(n) { Some(ref c) => c.span, None => { debug!("Skipping crate {}, no data", n);