Skip to content

Commit d0079d1

Browse files
committed
Avoid generating attributes more than once for CrateMetadata
1 parent e4b5b89 commit d0079d1

File tree

3 files changed

+62
-42
lines changed

3 files changed

+62
-42
lines changed

src/librustc_metadata/creader.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ use std::ops::Deref;
3333
use std::path::PathBuf;
3434
use std::{cmp, fs};
3535

36-
use syntax::ast;
37-
use syntax::attr;
36+
use syntax::{ast, attr};
3837
use syntax::ext::base::SyntaxExtension;
3938
use syntax::symbol::Symbol;
4039
use syntax::visit;
@@ -213,7 +212,6 @@ impl<'a> CrateLoader<'a> {
213212
let root = if root.is_some() { root } else { &crate_paths };
214213

215214
let Library { dylib, rlib, rmeta, metadata } = lib;
216-
217215
let cnum_map = self.resolve_crate_deps(root, &crate_root, &metadata, cnum, span, dep_kind);
218216

219217
let dependencies: Vec<CrateNum> = cnum_map.iter().cloned().collect();
@@ -228,7 +226,7 @@ impl<'a> CrateLoader<'a> {
228226
.map(|trait_impls| (trait_impls.trait_id, trait_impls.impls))
229227
.collect();
230228

231-
let cmeta = cstore::CrateMetadata {
229+
let mut cmeta = cstore::CrateMetadata {
232230
name,
233231
extern_crate: Lock::new(None),
234232
def_path_table: Lrc::new(def_path_table),
@@ -248,8 +246,17 @@ impl<'a> CrateLoader<'a> {
248246
rlib,
249247
rmeta,
250248
},
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,
251256
};
252257

258+
cmeta.derive_attributes(self.sess);
259+
253260
let cmeta = Lrc::new(cmeta);
254261
self.cstore.set_crate_data(cnum, cmeta.clone());
255262
(cnum, cmeta)
@@ -635,15 +642,14 @@ impl<'a> CrateLoader<'a> {
635642
let mut needs_panic_runtime = attr::contains_name(&krate.attrs,
636643
"needs_panic_runtime");
637644

638-
let sess = self.sess;
639645
self.cstore.iter_crate_data(|cnum, data| {
640646
needs_panic_runtime = needs_panic_runtime ||
641-
data.needs_panic_runtime(sess);
642-
if data.is_panic_runtime(sess) {
647+
data.needs_panic_runtime();
648+
if data.is_panic_runtime() {
643649
// Inject a dependency from all #![needs_panic_runtime] to this
644650
// #![panic_runtime] crate.
645651
self.inject_dependency_if(cnum, "a panic runtime",
646-
&|data| data.needs_panic_runtime(sess));
652+
&|data| data.needs_panic_runtime());
647653
runtime_found = runtime_found || *data.dep_kind.lock() == DepKind::Explicit;
648654
}
649655
});
@@ -680,7 +686,7 @@ impl<'a> CrateLoader<'a> {
680686

681687
// Sanity check the loaded crate to ensure it is indeed a panic runtime
682688
// and the panic strategy is indeed what we thought it was.
683-
if !data.is_panic_runtime(self.sess) {
689+
if !data.is_panic_runtime() {
684690
self.sess.err(&format!("the crate `{}` is not a panic runtime",
685691
name));
686692
}
@@ -692,7 +698,7 @@ impl<'a> CrateLoader<'a> {
692698

693699
self.sess.injected_panic_runtime.set(Some(cnum));
694700
self.inject_dependency_if(cnum, "a panic runtime",
695-
&|data| data.needs_panic_runtime(self.sess));
701+
&|data| data.needs_panic_runtime());
696702
}
697703

698704
fn inject_sanitizer_runtime(&mut self) {
@@ -787,7 +793,7 @@ impl<'a> CrateLoader<'a> {
787793
PathKind::Crate, dep_kind);
788794

789795
// Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
790-
if !data.is_sanitizer_runtime(self.sess) {
796+
if !data.is_sanitizer_runtime() {
791797
self.sess.err(&format!("the crate `{}` is not a sanitizer runtime",
792798
name));
793799
}
@@ -810,7 +816,7 @@ impl<'a> CrateLoader<'a> {
810816
PathKind::Crate, dep_kind);
811817

812818
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
813-
if !data.is_profiler_runtime(self.sess) {
819+
if !data.is_profiler_runtime() {
814820
self.sess.err(&format!("the crate `profiler_builtins` is not \
815821
a profiler runtime"));
816822
}
@@ -827,7 +833,7 @@ impl<'a> CrateLoader<'a> {
827833
let mut needs_allocator = attr::contains_name(&krate.attrs,
828834
"needs_allocator");
829835
self.cstore.iter_crate_data(|_, data| {
830-
needs_allocator = needs_allocator || data.needs_allocator(self.sess);
836+
needs_allocator = needs_allocator || data.needs_allocator();
831837
});
832838
if !needs_allocator {
833839
self.sess.injected_allocator.set(None);

src/librustc_metadata/cstore.rs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc::hir::def_id::{CRATE_DEF_INDEX, 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::{Session, CrateDisambiguator};
20+
use rustc::session::{CrateDisambiguator, Session};
2121
use rustc_target::spec::PanicStrategy;
2222
use rustc_data_structures::indexed_vec::IndexVec;
2323
use rustc::util::nodemap::{FxHashMap, NodeMap};
@@ -84,6 +84,15 @@ pub struct CrateMetadata {
8484
pub source: CrateSource,
8585

8686
pub proc_macros: Option<Vec<(ast::Name, Lrc<SyntaxExtension>)>>,
87+
88+
// 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>,
8796
}
8897

8998
pub struct CStore {
@@ -187,50 +196,55 @@ impl CrateMetadata {
187196
self.root.disambiguator
188197
}
189198

190-
pub fn needs_allocator(&self, sess: &Session) -> bool {
191-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
192-
attr::contains_name(&attrs, "needs_allocator")
199+
pub fn needs_allocator(&self) -> bool {
200+
self.needs_allocator.unwrap_or(false)
193201
}
194202

195203
pub fn has_global_allocator(&self) -> bool {
196-
self.root.has_global_allocator.clone()
204+
self.root.has_global_allocator
197205
}
198206

199207
pub fn has_default_lib_allocator(&self) -> bool {
200-
self.root.has_default_lib_allocator.clone()
208+
self.root.has_default_lib_allocator
201209
}
202210

203-
pub fn is_panic_runtime(&self, sess: &Session) -> bool {
204-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
205-
attr::contains_name(&attrs, "panic_runtime")
211+
pub fn is_panic_runtime(&self) -> bool {
212+
self.panic_runtime.unwrap_or(false)
206213
}
207214

208-
pub fn needs_panic_runtime(&self, sess: &Session) -> bool {
209-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
210-
attr::contains_name(&attrs, "needs_panic_runtime")
215+
pub fn needs_panic_runtime(&self) -> bool {
216+
self.needs_panic_runtime.unwrap_or(false)
211217
}
212218

213-
pub fn is_compiler_builtins(&self, sess: &Session) -> bool {
214-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
215-
attr::contains_name(&attrs, "compiler_builtins")
219+
pub fn is_compiler_builtins(&self) -> bool {
220+
self.compiler_builtins.unwrap_or(false)
216221
}
217222

218-
pub fn is_sanitizer_runtime(&self, sess: &Session) -> bool {
219-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
220-
attr::contains_name(&attrs, "sanitizer_runtime")
223+
pub fn is_sanitizer_runtime(&self) -> bool {
224+
self.sanitizer_runtime.unwrap_or(false)
221225
}
222226

223-
pub fn is_profiler_runtime(&self, sess: &Session) -> bool {
224-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
225-
attr::contains_name(&attrs, "profiler_runtime")
227+
pub fn is_profiler_runtime(&self) -> bool {
228+
self.profiler_runtime.unwrap_or(false)
226229
}
227230

228-
pub fn is_no_builtins(&self, sess: &Session) -> bool {
229-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
230-
attr::contains_name(&attrs, "no_builtins")
231+
pub fn is_no_builtins(&self) -> bool {
232+
self.no_builtins.unwrap_or(false)
231233
}
232234

233235
pub fn panic_strategy(&self) -> PanicStrategy {
234236
self.root.panic_strategy.clone()
235237
}
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+
}
236250
}

src/librustc_metadata/cstore_impl.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,17 @@ 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(tcx.sess) }
173-
is_compiler_builtins => { cdata.is_compiler_builtins(tcx.sess) }
172+
is_panic_runtime => { cdata.is_panic_runtime() }
173+
is_compiler_builtins => { cdata.is_compiler_builtins() }
174174
has_global_allocator => { cdata.has_global_allocator() }
175-
is_sanitizer_runtime => { cdata.is_sanitizer_runtime(tcx.sess) }
176-
is_profiler_runtime => { cdata.is_profiler_runtime(tcx.sess) }
175+
is_sanitizer_runtime => { cdata.is_sanitizer_runtime() }
176+
is_profiler_runtime => { cdata.is_profiler_runtime() }
177177
panic_strategy => { cdata.panic_strategy() }
178178
extern_crate => {
179179
let r = Lrc::new(*cdata.extern_crate.lock());
180180
r
181181
}
182-
is_no_builtins => { cdata.is_no_builtins(tcx.sess) }
182+
is_no_builtins => { cdata.is_no_builtins() }
183183
impl_defaultness => { cdata.get_impl_defaultness(def_id.index) }
184184
reachable_non_generics => {
185185
let reachable_non_generics = tcx

0 commit comments

Comments
 (0)