Skip to content

Commit e45b9c8

Browse files
committed
Attempt to pass CrateMetadata flags on creation
1 parent d0079d1 commit e45b9c8

File tree

3 files changed

+40
-73
lines changed

3 files changed

+40
-73
lines changed

src/librustc_metadata/creader.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//! Validates all used crates and extern libraries and loads their metadata
1212
1313
use cstore::{self, CStore, CrateSource, MetadataBlob};
14+
use decoder::Metadata;
1415
use locator::{self, CratePaths};
1516
use schema::CrateRoot;
1617
use rustc_data_structures::sync::{Lrc, RwLock, Lock};
@@ -220,13 +221,24 @@ impl<'a> CrateLoader<'a> {
220221
crate_root.def_path_table.decode((&metadata, self.sess))
221222
});
222223

224+
let crate_entry = crate_root
225+
.index
226+
.lookup(metadata.raw_bytes(), CRATE_DEF_INDEX)
227+
.unwrap()
228+
.decode(&metadata);
229+
230+
let crate_attrs: Vec<ast::Attribute> = crate_entry
231+
.attributes
232+
.decode((&metadata, self.sess))
233+
.collect();
234+
223235
let trait_impls = crate_root
224236
.impls
225237
.decode((&metadata, self.sess))
226238
.map(|trait_impls| (trait_impls.trait_id, trait_impls.impls))
227239
.collect();
228240

229-
let mut cmeta = cstore::CrateMetadata {
241+
let cmeta = cstore::CrateMetadata {
230242
name,
231243
extern_crate: Lock::new(None),
232244
def_path_table: Lrc::new(def_path_table),
@@ -246,17 +258,15 @@ impl<'a> CrateLoader<'a> {
246258
rlib,
247259
rmeta,
248260
},
249-
compiler_builtins: None,
250-
needs_allocator: None,
251-
needs_panic_runtime: None,
252-
no_builtins: None,
253-
panic_runtime: None,
254-
profiler_runtime: None,
255-
sanitizer_runtime: None,
261+
compiler_builtins: attr::contains_name(&crate_attrs, "compiler_builtins"),
262+
needs_allocator: attr::contains_name(&crate_attrs, "needs_allocator"),
263+
needs_panic_runtime: attr::contains_name(&crate_attrs, "needs_panic_runtime"),
264+
no_builtins: attr::contains_name(&crate_attrs, "no_builtins"),
265+
panic_runtime: attr::contains_name(&crate_attrs, "panic_runtime"),
266+
profiler_runtime: attr::contains_name(&crate_attrs, "profiler_runtime"),
267+
sanitizer_runtime: attr::contains_name(&crate_attrs, "sanitizer_runtime"),
256268
};
257269

258-
cmeta.derive_attributes(self.sess);
259-
260270
let cmeta = Lrc::new(cmeta);
261271
self.cstore.set_crate_data(cnum, cmeta.clone());
262272
(cnum, cmeta)
@@ -644,12 +654,12 @@ impl<'a> CrateLoader<'a> {
644654

645655
self.cstore.iter_crate_data(|cnum, data| {
646656
needs_panic_runtime = needs_panic_runtime ||
647-
data.needs_panic_runtime();
648-
if data.is_panic_runtime() {
657+
data.needs_panic_runtime;
658+
if data.panic_runtime {
649659
// Inject a dependency from all #![needs_panic_runtime] to this
650660
// #![panic_runtime] crate.
651661
self.inject_dependency_if(cnum, "a panic runtime",
652-
&|data| data.needs_panic_runtime());
662+
&|data| data.needs_panic_runtime);
653663
runtime_found = runtime_found || *data.dep_kind.lock() == DepKind::Explicit;
654664
}
655665
});
@@ -686,7 +696,7 @@ impl<'a> CrateLoader<'a> {
686696

687697
// Sanity check the loaded crate to ensure it is indeed a panic runtime
688698
// and the panic strategy is indeed what we thought it was.
689-
if !data.is_panic_runtime() {
699+
if !data.panic_runtime {
690700
self.sess.err(&format!("the crate `{}` is not a panic runtime",
691701
name));
692702
}
@@ -698,7 +708,7 @@ impl<'a> CrateLoader<'a> {
698708

699709
self.sess.injected_panic_runtime.set(Some(cnum));
700710
self.inject_dependency_if(cnum, "a panic runtime",
701-
&|data| data.needs_panic_runtime());
711+
&|data| data.needs_panic_runtime);
702712
}
703713

704714
fn inject_sanitizer_runtime(&mut self) {
@@ -793,7 +803,7 @@ impl<'a> CrateLoader<'a> {
793803
PathKind::Crate, dep_kind);
794804

795805
// Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
796-
if !data.is_sanitizer_runtime() {
806+
if !data.sanitizer_runtime {
797807
self.sess.err(&format!("the crate `{}` is not a sanitizer runtime",
798808
name));
799809
}
@@ -816,7 +826,7 @@ impl<'a> CrateLoader<'a> {
816826
PathKind::Crate, dep_kind);
817827

818828
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
819-
if !data.is_profiler_runtime() {
829+
if !data.profiler_runtime {
820830
self.sess.err(&format!("the crate `profiler_builtins` is not \
821831
a profiler runtime"));
822832
}
@@ -833,7 +843,7 @@ impl<'a> CrateLoader<'a> {
833843
let mut needs_allocator = attr::contains_name(&krate.attrs,
834844
"needs_allocator");
835845
self.cstore.iter_crate_data(|_, data| {
836-
needs_allocator = needs_allocator || data.needs_allocator();
846+
needs_allocator = needs_allocator || data.needs_allocator;
837847
});
838848
if !needs_allocator {
839849
self.sess.injected_allocator.set(None);

src/librustc_metadata/cstore.rs

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313

1414
use schema;
1515

16-
use rustc::hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefIndex};
16+
use rustc::hir::def_id::{CrateNum, DefIndex};
1717
use rustc::hir::map::definitions::DefPathTable;
1818
use rustc::hir::svh::Svh;
1919
use rustc::middle::cstore::{DepKind, ExternCrate, MetadataLoader};
20-
use rustc::session::{CrateDisambiguator, Session};
20+
use rustc::session::CrateDisambiguator;
2121
use rustc_target::spec::PanicStrategy;
2222
use rustc_data_structures::indexed_vec::IndexVec;
2323
use rustc::util::nodemap::{FxHashMap, NodeMap};
2424

2525
use rustc_data_structures::sync::{Lrc, RwLock, Lock};
26-
use syntax::{ast, attr};
26+
use syntax::ast;
2727
use syntax::ext::base::SyntaxExtension;
2828
use syntax::symbol::Symbol;
2929
use syntax_pos;
@@ -86,13 +86,13 @@ pub struct CrateMetadata {
8686
pub proc_macros: Option<Vec<(ast::Name, Lrc<SyntaxExtension>)>>,
8787

8888
// Booleans derived from attributes
89-
pub compiler_builtins: Option<bool>,
90-
pub needs_allocator: Option<bool>,
91-
pub needs_panic_runtime: Option<bool>,
92-
pub no_builtins: Option<bool>,
93-
pub panic_runtime: Option<bool>,
94-
pub profiler_runtime: Option<bool>,
95-
pub sanitizer_runtime: Option<bool>,
89+
pub compiler_builtins: bool,
90+
pub needs_allocator: bool,
91+
pub needs_panic_runtime: bool,
92+
pub no_builtins: bool,
93+
pub panic_runtime: bool,
94+
pub profiler_runtime: bool,
95+
pub sanitizer_runtime: bool,
9696
}
9797

9898
pub struct CStore {
@@ -189,17 +189,15 @@ impl CrateMetadata {
189189
pub fn name(&self) -> Symbol {
190190
self.root.name
191191
}
192+
192193
pub fn hash(&self) -> Svh {
193194
self.root.hash
194195
}
196+
195197
pub fn disambiguator(&self) -> CrateDisambiguator {
196198
self.root.disambiguator
197199
}
198200

199-
pub fn needs_allocator(&self) -> bool {
200-
self.needs_allocator.unwrap_or(false)
201-
}
202-
203201
pub fn has_global_allocator(&self) -> bool {
204202
self.root.has_global_allocator
205203
}
@@ -208,43 +206,7 @@ impl CrateMetadata {
208206
self.root.has_default_lib_allocator
209207
}
210208

211-
pub fn is_panic_runtime(&self) -> bool {
212-
self.panic_runtime.unwrap_or(false)
213-
}
214-
215-
pub fn needs_panic_runtime(&self) -> bool {
216-
self.needs_panic_runtime.unwrap_or(false)
217-
}
218-
219-
pub fn is_compiler_builtins(&self) -> bool {
220-
self.compiler_builtins.unwrap_or(false)
221-
}
222-
223-
pub fn is_sanitizer_runtime(&self) -> bool {
224-
self.sanitizer_runtime.unwrap_or(false)
225-
}
226-
227-
pub fn is_profiler_runtime(&self) -> bool {
228-
self.profiler_runtime.unwrap_or(false)
229-
}
230-
231-
pub fn is_no_builtins(&self) -> bool {
232-
self.no_builtins.unwrap_or(false)
233-
}
234-
235209
pub fn panic_strategy(&self) -> PanicStrategy {
236210
self.root.panic_strategy.clone()
237211
}
238-
239-
pub fn derive_attributes(&mut self, sess: &Session) {
240-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
241-
242-
self.compiler_builtins = Some(attr::contains_name(&attrs, "compiler_builtins"));
243-
self.needs_allocator = Some(attr::contains_name(&attrs, "needs_allocator"));
244-
self.needs_panic_runtime = Some(attr::contains_name(&attrs, "needs_panic_runtime"));
245-
self.no_builtins = Some(attr::contains_name(&attrs, "no_builtins"));
246-
self.panic_runtime = Some(attr::contains_name(&attrs, "panic_runtime"));
247-
self.profiler_runtime = Some(attr::contains_name(&attrs, "profiler_runtime"));
248-
self.sanitizer_runtime = Some(attr::contains_name(&attrs, "sanitizer_runtime"));
249-
}
250212
}

src/librustc_metadata/cstore_impl.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,12 @@ provide! { <'tcx> tcx, def_id, other, cdata,
169169
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
170170

171171
dylib_dependency_formats => { Lrc::new(cdata.get_dylib_dependency_formats()) }
172-
is_panic_runtime => { cdata.is_panic_runtime() }
173-
is_compiler_builtins => { cdata.is_compiler_builtins() }
174172
has_global_allocator => { cdata.has_global_allocator() }
175-
is_sanitizer_runtime => { cdata.is_sanitizer_runtime() }
176-
is_profiler_runtime => { cdata.is_profiler_runtime() }
177173
panic_strategy => { cdata.panic_strategy() }
178174
extern_crate => {
179175
let r = Lrc::new(*cdata.extern_crate.lock());
180176
r
181177
}
182-
is_no_builtins => { cdata.is_no_builtins() }
183178
impl_defaultness => { cdata.get_impl_defaultness(def_id.index) }
184179
reachable_non_generics => {
185180
let reachable_non_generics = tcx

0 commit comments

Comments
 (0)