Skip to content

Commit 466fc68

Browse files
committed
Avoid generating attributes more than once for CrateMetadata
1 parent f418f1d commit 466fc68

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

src/librustc_metadata/creader.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ impl<'a> CrateLoader<'a> {
214214
let root = if root.is_some() { root } else { &crate_paths };
215215

216216
let Library { dylib, rlib, rmeta, metadata } = lib;
217-
218217
let cnum_map = self.resolve_crate_deps(root, &crate_root, &metadata, cnum, span, dep_kind);
219218

220219
let dependencies: Vec<CrateNum> = cnum_map.iter().cloned().collect();
@@ -229,7 +228,7 @@ impl<'a> CrateLoader<'a> {
229228
.map(|trait_impls| (trait_impls.trait_id, trait_impls.impls))
230229
.collect();
231230

232-
let cmeta = cstore::CrateMetadata {
231+
let mut cmeta = cstore::CrateMetadata {
233232
name,
234233
extern_crate: Lock::new(None),
235234
def_path_table: Lrc::new(def_path_table),
@@ -249,8 +248,17 @@ impl<'a> CrateLoader<'a> {
249248
rlib,
250249
rmeta,
251250
},
251+
compiler_builtins: None,
252+
needs_allocator: None,
253+
needs_panic_runtime: None,
254+
no_builtins: None,
255+
panic_runtime: None,
256+
profiler_runtime: None,
257+
sanitizer_runtime: None,
252258
};
253259

260+
cmeta.derive_attributes(self.sess);
261+
254262
let cmeta = Lrc::new(cmeta);
255263
self.cstore.set_crate_data(cnum, cmeta.clone());
256264
(cnum, cmeta)
@@ -641,15 +649,14 @@ impl<'a> CrateLoader<'a> {
641649
let mut needs_panic_runtime = attr::contains_name(&krate.attrs,
642650
"needs_panic_runtime");
643651

644-
let sess = self.sess;
645652
self.cstore.iter_crate_data(|cnum, data| {
646653
needs_panic_runtime = needs_panic_runtime ||
647-
data.needs_panic_runtime(sess);
648-
if data.is_panic_runtime(sess) {
654+
data.needs_panic_runtime();
655+
if data.is_panic_runtime() {
649656
// Inject a dependency from all #![needs_panic_runtime] to this
650657
// #![panic_runtime] crate.
651658
self.inject_dependency_if(cnum, "a panic runtime",
652-
&|data| data.needs_panic_runtime(sess));
659+
&|data| data.needs_panic_runtime());
653660
runtime_found = runtime_found || *data.dep_kind.lock() == DepKind::Explicit;
654661
}
655662
});
@@ -686,7 +693,7 @@ impl<'a> CrateLoader<'a> {
686693

687694
// Sanity check the loaded crate to ensure it is indeed a panic runtime
688695
// and the panic strategy is indeed what we thought it was.
689-
if !data.is_panic_runtime(self.sess) {
696+
if !data.is_panic_runtime() {
690697
self.sess.err(&format!("the crate `{}` is not a panic runtime",
691698
name));
692699
}
@@ -698,7 +705,7 @@ impl<'a> CrateLoader<'a> {
698705

699706
self.sess.injected_panic_runtime.set(Some(cnum));
700707
self.inject_dependency_if(cnum, "a panic runtime",
701-
&|data| data.needs_panic_runtime(self.sess));
708+
&|data| data.needs_panic_runtime());
702709
}
703710

704711
fn inject_sanitizer_runtime(&mut self) {
@@ -793,7 +800,7 @@ impl<'a> CrateLoader<'a> {
793800
PathKind::Crate, dep_kind);
794801

795802
// Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
796-
if !data.is_sanitizer_runtime(self.sess) {
803+
if !data.is_sanitizer_runtime() {
797804
self.sess.err(&format!("the crate `{}` is not a sanitizer runtime",
798805
name));
799806
}
@@ -816,7 +823,7 @@ impl<'a> CrateLoader<'a> {
816823
PathKind::Crate, dep_kind);
817824

818825
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
819-
if !data.is_profiler_runtime(self.sess) {
826+
if !data.is_profiler_runtime() {
820827
self.sess.err(&format!("the crate `profiler_builtins` is not \
821828
a profiler runtime"));
822829
}
@@ -833,7 +840,7 @@ impl<'a> CrateLoader<'a> {
833840
let mut needs_allocator = attr::contains_name(&krate.attrs,
834841
"needs_allocator");
835842
self.cstore.iter_crate_data(|_, data| {
836-
needs_allocator = needs_allocator || data.needs_allocator(self.sess);
843+
needs_allocator = needs_allocator || data.needs_allocator();
837844
});
838845
if !needs_allocator {
839846
self.sess.injected_allocator.set(None);

src/librustc_metadata/cstore.rs

Lines changed: 26 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};
@@ -85,6 +85,15 @@ pub struct CrateMetadata {
8585
pub source: CrateSource,
8686

8787
pub proc_macros: Option<Vec<(ast::Name, Lrc<SyntaxExtension>)>>,
88+
89+
// Booleans derived from attributes
90+
pub compiler_builtins: Option<bool>,
91+
pub needs_allocator: Option<bool>,
92+
pub needs_panic_runtime: Option<bool>,
93+
pub no_builtins: Option<bool>,
94+
pub panic_runtime: Option<bool>,
95+
pub profiler_runtime: Option<bool>,
96+
pub sanitizer_runtime: Option<bool>,
8897
}
8998

9099
pub struct CStore {
@@ -188,47 +197,40 @@ impl CrateMetadata {
188197
self.root.disambiguator
189198
}
190199

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

196204
pub fn has_global_allocator(&self) -> bool {
197-
self.root.has_global_allocator.clone()
205+
self.root.has_global_allocator
198206
}
199207

200208
pub fn has_default_lib_allocator(&self) -> bool {
201-
self.root.has_default_lib_allocator.clone()
209+
self.root.has_default_lib_allocator
202210
}
203211

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

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

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

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

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

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

234236
pub fn panic_strategy(&self) -> PanicStrategy {

src/librustc_metadata/cstore_impl.rs

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

172172
dylib_dependency_formats => { Lrc::new(cdata.get_dylib_dependency_formats()) }
173-
is_panic_runtime => { cdata.is_panic_runtime(tcx.sess) }
174-
is_compiler_builtins => { cdata.is_compiler_builtins(tcx.sess) }
173+
is_panic_runtime => { cdata.is_panic_runtime() }
174+
is_compiler_builtins => { cdata.is_compiler_builtins() }
175175
has_global_allocator => { cdata.has_global_allocator() }
176-
is_sanitizer_runtime => { cdata.is_sanitizer_runtime(tcx.sess) }
177-
is_profiler_runtime => { cdata.is_profiler_runtime(tcx.sess) }
176+
is_sanitizer_runtime => { cdata.is_sanitizer_runtime() }
177+
is_profiler_runtime => { cdata.is_profiler_runtime() }
178178
panic_strategy => { cdata.panic_strategy() }
179179
extern_crate => {
180180
let r = Lrc::new(*cdata.extern_crate.lock());
181181
r
182182
}
183-
is_no_builtins => { cdata.is_no_builtins(tcx.sess) }
183+
is_no_builtins => { cdata.is_no_builtins() }
184184
impl_defaultness => { cdata.get_impl_defaultness(def_id.index) }
185185
reachable_non_generics => {
186186
let reachable_non_generics = tcx

0 commit comments

Comments
 (0)