Skip to content

Commit fd86800

Browse files
committed
Serialize attributes into the CrateRoot
1 parent 2d4c203 commit fd86800

File tree

8 files changed

+80
-64
lines changed

8 files changed

+80
-64
lines changed

src/librustc_incremental/persist/dirty_clean.rs

Lines changed: 13 additions & 9 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 9 additions & 28 deletions
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};
@@ -221,17 +220,6 @@ impl<'a> CrateLoader<'a> {
221220
crate_root.def_path_table.decode((&metadata, self.sess))
222221
});
223222

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-
235223
let trait_impls = crate_root
236224
.impls
237225
.decode((&metadata, self.sess))
@@ -257,14 +245,7 @@ impl<'a> CrateLoader<'a> {
257245
dylib,
258246
rlib,
259247
rmeta,
260-
},
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"),
248+
}
268249
};
269250

270251
let cmeta = Lrc::new(cmeta);
@@ -654,12 +635,12 @@ impl<'a> CrateLoader<'a> {
654635

655636
self.cstore.iter_crate_data(|cnum, data| {
656637
needs_panic_runtime = needs_panic_runtime ||
657-
data.needs_panic_runtime;
658-
if data.panic_runtime {
638+
data.needs_panic_runtime();
639+
if data.is_panic_runtime() {
659640
// Inject a dependency from all #![needs_panic_runtime] to this
660641
// #![panic_runtime] crate.
661642
self.inject_dependency_if(cnum, "a panic runtime",
662-
&|data| data.needs_panic_runtime);
643+
&|data| data.needs_panic_runtime());
663644
runtime_found = runtime_found || *data.dep_kind.lock() == DepKind::Explicit;
664645
}
665646
});
@@ -696,7 +677,7 @@ impl<'a> CrateLoader<'a> {
696677

697678
// Sanity check the loaded crate to ensure it is indeed a panic runtime
698679
// and the panic strategy is indeed what we thought it was.
699-
if !data.panic_runtime {
680+
if !data.is_panic_runtime() {
700681
self.sess.err(&format!("the crate `{}` is not a panic runtime",
701682
name));
702683
}
@@ -708,7 +689,7 @@ impl<'a> CrateLoader<'a> {
708689

709690
self.sess.injected_panic_runtime.set(Some(cnum));
710691
self.inject_dependency_if(cnum, "a panic runtime",
711-
&|data| data.needs_panic_runtime);
692+
&|data| data.needs_panic_runtime());
712693
}
713694

714695
fn inject_sanitizer_runtime(&mut self) {
@@ -803,7 +784,7 @@ impl<'a> CrateLoader<'a> {
803784
PathKind::Crate, dep_kind);
804785

805786
// Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
806-
if !data.sanitizer_runtime {
787+
if !data.is_sanitizer_runtime() {
807788
self.sess.err(&format!("the crate `{}` is not a sanitizer runtime",
808789
name));
809790
}
@@ -826,7 +807,7 @@ impl<'a> CrateLoader<'a> {
826807
PathKind::Crate, dep_kind);
827808

828809
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
829-
if !data.profiler_runtime {
810+
if !data.is_profiler_runtime() {
830811
self.sess.err(&format!("the crate `profiler_builtins` is not \
831812
a profiler runtime"));
832813
}
@@ -843,7 +824,7 @@ impl<'a> CrateLoader<'a> {
843824
let mut needs_allocator = attr::contains_name(&krate.attrs,
844825
"needs_allocator");
845826
self.cstore.iter_crate_data(|_, data| {
846-
needs_allocator = needs_allocator || data.needs_allocator;
827+
needs_allocator = needs_allocator || data.needs_allocator();
847828
});
848829
if !needs_allocator {
849830
self.sess.injected_allocator.set(None);

src/librustc_metadata/cstore.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,6 @@ 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: 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,
9687
}
9788

9889
pub struct CStore {
@@ -198,6 +189,10 @@ impl CrateMetadata {
198189
self.root.disambiguator
199190
}
200191

192+
pub fn needs_allocator(&self) -> bool {
193+
self.root.needs_allocator
194+
}
195+
201196
pub fn has_global_allocator(&self) -> bool {
202197
self.root.has_global_allocator
203198
}
@@ -206,6 +201,30 @@ impl CrateMetadata {
206201
self.root.has_default_lib_allocator
207202
}
208203

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

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.panic_runtime }
173-
is_compiler_builtins => { cdata.compiler_builtins }
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.sanitizer_runtime }
176-
is_profiler_runtime => { cdata.profiler_runtime }
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.no_builtins }
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

src/librustc_metadata/decoder.rs

Lines changed: 6 additions & 10 deletions
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

Lines changed: 10 additions & 2 deletions
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 {
@@ -509,6 +509,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
509509
None
510510
},
511511

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

src/librustc_metadata/schema.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ pub struct CrateRoot {
208208
pub interpret_alloc_index: LazySeq<u32>,
209209

210210
pub index: LazySeq<index::Index>,
211+
212+
pub compiler_builtins: bool,
213+
pub needs_allocator: bool,
214+
pub needs_panic_runtime: bool,
215+
pub no_builtins: bool,
216+
pub panic_runtime: bool,
217+
pub profiler_runtime: bool,
218+
pub sanitizer_runtime: bool,
211219
}
212220

213221
#[derive(RustcEncodable, RustcDecodable)]

0 commit comments

Comments
 (0)