Skip to content

Commit 680f3b2

Browse files
committed
Serialize attributes into the CrateRoot
1 parent ca60c40 commit 680f3b2

File tree

8 files changed

+80
-64
lines changed

8 files changed

+80
-64
lines changed

src/librustc_incremental/persist/dirty_clean.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -453,16 +453,20 @@ impl<'a, 'tcx> DirtyCleanVisitor<'a, 'tcx> {
453453
out
454454
}
455455

456-
fn dep_nodes(&self, labels: &Labels, def_id: DefId) -> Vec<DepNode> {
457-
let mut out = Vec::with_capacity(labels.len());
456+
fn dep_nodes<'l>(
457+
&self,
458+
labels: &'l Labels,
459+
def_id: DefId
460+
) -> impl Iterator<Item = DepNode> + 'l {
458461
let def_path_hash = self.tcx.def_path_hash(def_id);
459-
for label in labels.iter() {
460-
match DepNode::from_label_string(label, def_path_hash) {
461-
Ok(dep_node) => out.push(dep_node),
462-
Err(()) => unreachable!(),
463-
}
464-
}
465-
out
462+
labels
463+
.iter()
464+
.map(move |label| {
465+
match DepNode::from_label_string(label, def_path_hash) {
466+
Ok(dep_node) => dep_node,
467+
Err(()) => unreachable!(),
468+
}
469+
})
466470
}
467471

468472
fn dep_node_str(&self, dep_node: &DepNode) -> String {

src/librustc_incremental/persist/work_product.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pub fn copy_cgu_workproducts_to_incr_comp_cache_dir(
2828
if sess.opts.incremental.is_none() {
2929
return None
3030
}
31-
let work_product_id = WorkProductId::from_cgu_name(cgu_name);
3231

3332
let saved_files: Option<Vec<_>> =
3433
files.iter()
@@ -63,6 +62,7 @@ pub fn copy_cgu_workproducts_to_incr_comp_cache_dir(
6362
saved_files,
6463
};
6564

65+
let work_product_id = WorkProductId::from_cgu_name(cgu_name);
6666
Some((work_product_id, work_product))
6767
}
6868

src/librustc_metadata/creader.rs

+9-28
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//! Validates all used crates and extern libraries and loads their metadata
1212
1313
use cstore::{self, CStore, CrateSource, MetadataBlob};
14-
use decoder::Metadata;
1514
use locator::{self, CratePaths};
1615
use schema::CrateRoot;
1716
use rustc_data_structures::sync::{Lrc, RwLock, Lock};
@@ -223,17 +222,6 @@ impl<'a> CrateLoader<'a> {
223222
crate_root.def_path_table.decode((&metadata, self.sess))
224223
});
225224

226-
let crate_entry = crate_root
227-
.index
228-
.lookup(metadata.raw_bytes(), CRATE_DEF_INDEX)
229-
.unwrap()
230-
.decode(&metadata);
231-
232-
let crate_attrs: Vec<ast::Attribute> = crate_entry
233-
.attributes
234-
.decode((&metadata, self.sess))
235-
.collect();
236-
237225
let trait_impls = crate_root
238226
.impls
239227
.decode((&metadata, self.sess))
@@ -259,14 +247,7 @@ impl<'a> CrateLoader<'a> {
259247
dylib,
260248
rlib,
261249
rmeta,
262-
},
263-
compiler_builtins: attr::contains_name(&crate_attrs, "compiler_builtins"),
264-
needs_allocator: attr::contains_name(&crate_attrs, "needs_allocator"),
265-
needs_panic_runtime: attr::contains_name(&crate_attrs, "needs_panic_runtime"),
266-
no_builtins: attr::contains_name(&crate_attrs, "no_builtins"),
267-
panic_runtime: attr::contains_name(&crate_attrs, "panic_runtime"),
268-
profiler_runtime: attr::contains_name(&crate_attrs, "profiler_runtime"),
269-
sanitizer_runtime: attr::contains_name(&crate_attrs, "sanitizer_runtime"),
250+
}
270251
};
271252

272253
let cmeta = Lrc::new(cmeta);
@@ -661,12 +642,12 @@ impl<'a> CrateLoader<'a> {
661642

662643
self.cstore.iter_crate_data(|cnum, data| {
663644
needs_panic_runtime = needs_panic_runtime ||
664-
data.needs_panic_runtime;
665-
if data.panic_runtime {
645+
data.needs_panic_runtime();
646+
if data.is_panic_runtime() {
666647
// Inject a dependency from all #![needs_panic_runtime] to this
667648
// #![panic_runtime] crate.
668649
self.inject_dependency_if(cnum, "a panic runtime",
669-
&|data| data.needs_panic_runtime);
650+
&|data| data.needs_panic_runtime());
670651
runtime_found = runtime_found || *data.dep_kind.lock() == DepKind::Explicit;
671652
}
672653
});
@@ -703,7 +684,7 @@ impl<'a> CrateLoader<'a> {
703684

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

716697
self.sess.injected_panic_runtime.set(Some(cnum));
717698
self.inject_dependency_if(cnum, "a panic runtime",
718-
&|data| data.needs_panic_runtime);
699+
&|data| data.needs_panic_runtime());
719700
}
720701

721702
fn inject_sanitizer_runtime(&mut self) {
@@ -810,7 +791,7 @@ impl<'a> CrateLoader<'a> {
810791
PathKind::Crate, dep_kind);
811792

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

835816
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
836-
if !data.profiler_runtime {
817+
if !data.is_profiler_runtime() {
837818
self.sess.err(&format!("the crate `profiler_builtins` is not \
838819
a profiler runtime"));
839820
}
@@ -850,7 +831,7 @@ impl<'a> CrateLoader<'a> {
850831
let mut needs_allocator = attr::contains_name(&krate.attrs,
851832
"needs_allocator");
852833
self.cstore.iter_crate_data(|_, data| {
853-
needs_allocator = needs_allocator || data.needs_allocator;
834+
needs_allocator = needs_allocator || data.needs_allocator();
854835
});
855836
if !needs_allocator {
856837
self.sess.injected_allocator.set(None);

src/librustc_metadata/cstore.rs

+28-9
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,6 @@ 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: bool,
91-
pub needs_allocator: bool,
92-
pub needs_panic_runtime: bool,
93-
pub no_builtins: bool,
94-
pub panic_runtime: bool,
95-
pub profiler_runtime: bool,
96-
pub sanitizer_runtime: bool,
9788
}
9889

9990
pub struct CStore {
@@ -199,6 +190,10 @@ impl CrateMetadata {
199190
self.root.disambiguator
200191
}
201192

193+
pub fn needs_allocator(&self) -> bool {
194+
self.root.needs_allocator
195+
}
196+
202197
pub fn has_global_allocator(&self) -> bool {
203198
self.root.has_global_allocator
204199
}
@@ -207,6 +202,30 @@ impl CrateMetadata {
207202
self.root.has_default_lib_allocator
208203
}
209204

205+
pub fn is_panic_runtime(&self) -> bool {
206+
self.root.panic_runtime
207+
}
208+
209+
pub fn needs_panic_runtime(&self) -> bool {
210+
self.root.needs_panic_runtime
211+
}
212+
213+
pub fn is_compiler_builtins(&self) -> bool {
214+
self.root.compiler_builtins
215+
}
216+
217+
pub fn is_sanitizer_runtime(&self) -> bool {
218+
self.root.sanitizer_runtime
219+
}
220+
221+
pub fn is_profiler_runtime(&self) -> bool {
222+
self.root.profiler_runtime
223+
}
224+
225+
pub fn is_no_builtins(&self) -> bool {
226+
self.root.no_builtins
227+
}
228+
210229
pub fn panic_strategy(&self) -> PanicStrategy {
211230
self.root.panic_strategy
212231
}

src/librustc_metadata/cstore_impl.rs

+5-5
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.panic_runtime }
174-
is_compiler_builtins => { cdata.compiler_builtins }
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.sanitizer_runtime }
177-
is_profiler_runtime => { cdata.profiler_runtime }
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.no_builtins }
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

src/librustc_metadata/decoder.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,14 @@ impl<'a, 'tcx> CrateMetadata {
557557
-> &'tcx ty::AdtDef {
558558
let item = self.entry(item_id);
559559
let did = self.local_def_id(item_id);
560-
let kind = match item.kind {
561-
EntryKind::Enum(_) => ty::AdtKind::Enum,
562-
EntryKind::Struct(_, _) => ty::AdtKind::Struct,
563-
EntryKind::Union(_, _) => ty::AdtKind::Union,
560+
561+
let (kind, repr) = match item.kind {
562+
EntryKind::Enum(repr) => (ty::AdtKind::Enum, repr),
563+
EntryKind::Struct(_, repr) => (ty::AdtKind::Struct, repr),
564+
EntryKind::Union(_, repr) => (ty::AdtKind::Union, repr),
564565
_ => bug!("get_adt_def called on a non-ADT {:?}", did),
565566
};
567+
566568
let variants = if let ty::AdtKind::Enum = kind {
567569
item.children
568570
.decode(self)
@@ -573,12 +575,6 @@ impl<'a, 'tcx> CrateMetadata {
573575
} else {
574576
vec![self.get_variant(&item, item_id)]
575577
};
576-
let (kind, repr) = match item.kind {
577-
EntryKind::Enum(repr) => (ty::AdtKind::Enum, repr),
578-
EntryKind::Struct(_, repr) => (ty::AdtKind::Struct, repr),
579-
EntryKind::Union(_, repr) => (ty::AdtKind::Union, repr),
580-
_ => bug!("get_adt_def called on a non-ADT {:?}", did),
581-
};
582578

583579
tcx.alloc_adt_def(did, kind, variants, repr)
584580
}

src/librustc_metadata/encoder.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
483483
let index = items.write_index(&mut self.opaque.cursor);
484484
let index_bytes = self.position() - i;
485485

486+
let attrs = tcx.hir.krate_attrs();
486487
let link_meta = self.link_meta;
487488
let is_proc_macro = tcx.sess.crate_types.borrow().contains(&CrateTypeProcMacro);
488-
let has_default_lib_allocator =
489-
attr::contains_name(tcx.hir.krate_attrs(), "default_lib_allocator");
489+
let has_default_lib_allocator = attr::contains_name(&attrs, "default_lib_allocator");
490490
let has_global_allocator = *tcx.sess.has_global_allocator.get();
491491

492492
let root = self.lazy(&CrateRoot {
@@ -510,6 +510,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
510510
None
511511
},
512512

513+
compiler_builtins: attr::contains_name(&attrs, "compiler_builtins"),
514+
needs_allocator: attr::contains_name(&attrs, "needs_allocator"),
515+
needs_panic_runtime: attr::contains_name(&attrs, "needs_panic_runtime"),
516+
no_builtins: attr::contains_name(&attrs, "no_builtins"),
517+
panic_runtime: attr::contains_name(&attrs, "panic_runtime"),
518+
profiler_runtime: attr::contains_name(&attrs, "profiler_runtime"),
519+
sanitizer_runtime: attr::contains_name(&attrs, "sanitizer_runtime"),
520+
513521
crate_deps,
514522
dylib_dependency_formats,
515523
lang_items,

src/librustc_metadata/schema.rs

+8
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ pub struct CrateRoot {
210210
pub interpret_alloc_index: LazySeq<u32>,
211211

212212
pub index: LazySeq<index::Index>,
213+
214+
pub compiler_builtins: bool,
215+
pub needs_allocator: bool,
216+
pub needs_panic_runtime: bool,
217+
pub no_builtins: bool,
218+
pub panic_runtime: bool,
219+
pub profiler_runtime: bool,
220+
pub sanitizer_runtime: bool,
213221
}
214222

215223
#[derive(RustcEncodable, RustcDecodable)]

0 commit comments

Comments
 (0)