Skip to content

Commit

Permalink
Attempt to pass CrateMetadata flags on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
whitfin committed May 18, 2018
1 parent 466fc68 commit ece778e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 60 deletions.
46 changes: 28 additions & 18 deletions src/librustc_metadata/creader.rs
Expand Up @@ -11,6 +11,7 @@
//! Validates all used crates and extern libraries and loads their metadata

use cstore::{self, CStore, CrateSource, MetadataBlob};
use decoder::Metadata;
use locator::{self, CratePaths};
use schema::CrateRoot;
use rustc_data_structures::sync::{Lrc, RwLock, Lock};
Expand Down Expand Up @@ -222,13 +223,24 @@ impl<'a> CrateLoader<'a> {
crate_root.def_path_table.decode((&metadata, self.sess))
});

let crate_entry = crate_root
.index
.lookup(metadata.raw_bytes(), CRATE_DEF_INDEX)
.unwrap()
.decode(&metadata);

let crate_attrs: Vec<ast::Attribute> = crate_entry
.attributes
.decode((&metadata, self.sess))
.collect();

let trait_impls = crate_root
.impls
.decode((&metadata, self.sess))
.map(|trait_impls| (trait_impls.trait_id, trait_impls.impls))
.collect();

let mut cmeta = cstore::CrateMetadata {
let cmeta = cstore::CrateMetadata {
name,
extern_crate: Lock::new(None),
def_path_table: Lrc::new(def_path_table),
Expand All @@ -248,17 +260,15 @@ impl<'a> CrateLoader<'a> {
rlib,
rmeta,
},
compiler_builtins: None,
needs_allocator: None,
needs_panic_runtime: None,
no_builtins: None,
panic_runtime: None,
profiler_runtime: None,
sanitizer_runtime: None,
compiler_builtins: attr::contains_name(&crate_attrs, "compiler_builtins"),
needs_allocator: attr::contains_name(&crate_attrs, "needs_allocator"),
needs_panic_runtime: attr::contains_name(&crate_attrs, "needs_panic_runtime"),
no_builtins: attr::contains_name(&crate_attrs, "no_builtins"),
panic_runtime: attr::contains_name(&crate_attrs, "panic_runtime"),
profiler_runtime: attr::contains_name(&crate_attrs, "profiler_runtime"),
sanitizer_runtime: attr::contains_name(&crate_attrs, "sanitizer_runtime"),
};

cmeta.derive_attributes(self.sess);

let cmeta = Lrc::new(cmeta);
self.cstore.set_crate_data(cnum, cmeta.clone());
(cnum, cmeta)
Expand Down Expand Up @@ -651,12 +661,12 @@ impl<'a> CrateLoader<'a> {

self.cstore.iter_crate_data(|cnum, data| {
needs_panic_runtime = needs_panic_runtime ||
data.needs_panic_runtime();
if data.is_panic_runtime() {
data.needs_panic_runtime;
if data.panic_runtime {
// Inject a dependency from all #![needs_panic_runtime] to this
// #![panic_runtime] crate.
self.inject_dependency_if(cnum, "a panic runtime",
&|data| data.needs_panic_runtime());
&|data| data.needs_panic_runtime);
runtime_found = runtime_found || *data.dep_kind.lock() == DepKind::Explicit;
}
});
Expand Down Expand Up @@ -693,7 +703,7 @@ impl<'a> CrateLoader<'a> {

// Sanity check the loaded crate to ensure it is indeed a panic runtime
// and the panic strategy is indeed what we thought it was.
if !data.is_panic_runtime() {
if !data.panic_runtime {
self.sess.err(&format!("the crate `{}` is not a panic runtime",
name));
}
Expand All @@ -705,7 +715,7 @@ impl<'a> CrateLoader<'a> {

self.sess.injected_panic_runtime.set(Some(cnum));
self.inject_dependency_if(cnum, "a panic runtime",
&|data| data.needs_panic_runtime());
&|data| data.needs_panic_runtime);
}

fn inject_sanitizer_runtime(&mut self) {
Expand Down Expand Up @@ -800,7 +810,7 @@ impl<'a> CrateLoader<'a> {
PathKind::Crate, dep_kind);

// Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
if !data.is_sanitizer_runtime() {
if !data.sanitizer_runtime {
self.sess.err(&format!("the crate `{}` is not a sanitizer runtime",
name));
}
Expand All @@ -823,7 +833,7 @@ impl<'a> CrateLoader<'a> {
PathKind::Crate, dep_kind);

// Sanity check the loaded crate to ensure it is indeed a profiler runtime
if !data.is_profiler_runtime() {
if !data.profiler_runtime {
self.sess.err(&format!("the crate `profiler_builtins` is not \
a profiler runtime"));
}
Expand All @@ -840,7 +850,7 @@ impl<'a> CrateLoader<'a> {
let mut needs_allocator = attr::contains_name(&krate.attrs,
"needs_allocator");
self.cstore.iter_crate_data(|_, data| {
needs_allocator = needs_allocator || data.needs_allocator();
needs_allocator = needs_allocator || data.needs_allocator;
});
if !needs_allocator {
self.sess.injected_allocator.set(None);
Expand Down
48 changes: 11 additions & 37 deletions src/librustc_metadata/cstore.rs
Expand Up @@ -13,11 +13,11 @@

use schema;

use rustc::hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefIndex};
use rustc::hir::def_id::{CrateNum, DefIndex};
use rustc::hir::map::definitions::DefPathTable;
use rustc::hir::svh::Svh;
use rustc::middle::cstore::{DepKind, ExternCrate, MetadataLoader};
use rustc::session::{CrateDisambiguator, Session};
use rustc::session::CrateDisambiguator;
use rustc_target::spec::PanicStrategy;
use rustc_data_structures::indexed_vec::IndexVec;
use rustc::util::nodemap::{FxHashMap, NodeMap};
Expand Down Expand Up @@ -87,13 +87,13 @@ pub struct CrateMetadata {
pub proc_macros: Option<Vec<(ast::Name, Lrc<SyntaxExtension>)>>,

// Booleans derived from attributes
pub compiler_builtins: Option<bool>,
pub needs_allocator: Option<bool>,
pub needs_panic_runtime: Option<bool>,
pub no_builtins: Option<bool>,
pub panic_runtime: Option<bool>,
pub profiler_runtime: Option<bool>,
pub sanitizer_runtime: Option<bool>,
pub compiler_builtins: bool,
pub needs_allocator: bool,
pub needs_panic_runtime: bool,
pub no_builtins: bool,
pub panic_runtime: bool,
pub profiler_runtime: bool,
pub sanitizer_runtime: bool,
}

pub struct CStore {
Expand Down Expand Up @@ -190,17 +190,15 @@ impl CrateMetadata {
pub fn name(&self) -> Symbol {
self.root.name
}

pub fn hash(&self) -> Svh {
self.root.hash
}

pub fn disambiguator(&self) -> CrateDisambiguator {
self.root.disambiguator
}

pub fn needs_allocator(&self) -> bool {
self.needs_allocator.unwrap_or(false)
}

pub fn has_global_allocator(&self) -> bool {
self.root.has_global_allocator
}
Expand All @@ -209,30 +207,6 @@ impl CrateMetadata {
self.root.has_default_lib_allocator
}

pub fn is_panic_runtime(&self) -> bool {
self.panic_runtime.unwrap_or(false)
}

pub fn needs_panic_runtime(&self) -> bool {
self.needs_panic_runtime.unwrap_or(false)
}

pub fn is_compiler_builtins(&self) -> bool {
self.compiler_builtins.unwrap_or(false)
}

pub fn is_sanitizer_runtime(&self) -> bool {
self.sanitizer_runtime.unwrap_or(false)
}

pub fn is_profiler_runtime(&self) -> bool {
self.profiler_runtime.unwrap_or(false)
}

pub fn is_no_builtins(&self) -> bool {
self.no_builtins.unwrap_or(false)
}

pub fn panic_strategy(&self) -> PanicStrategy {
self.root.panic_strategy.clone()
}
Expand Down
5 changes: 0 additions & 5 deletions src/librustc_metadata/cstore_impl.rs
Expand Up @@ -170,17 +170,12 @@ provide! { <'tcx> tcx, def_id, other, cdata,
is_mir_available => { cdata.is_item_mir_available(def_id.index) }

dylib_dependency_formats => { Lrc::new(cdata.get_dylib_dependency_formats()) }
is_panic_runtime => { cdata.is_panic_runtime() }
is_compiler_builtins => { cdata.is_compiler_builtins() }
has_global_allocator => { cdata.has_global_allocator() }
is_sanitizer_runtime => { cdata.is_sanitizer_runtime() }
is_profiler_runtime => { cdata.is_profiler_runtime() }
panic_strategy => { cdata.panic_strategy() }
extern_crate => {
let r = Lrc::new(*cdata.extern_crate.lock());
r
}
is_no_builtins => { cdata.is_no_builtins() }
impl_defaultness => { cdata.get_impl_defaultness(def_id.index) }
reachable_non_generics => {
let reachable_non_generics = tcx
Expand Down

0 comments on commit ece778e

Please sign in to comment.