From c99090761c71b7c1c59ce889df5f1b4f78376538 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Thu, 11 Apr 2019 10:22:02 +0300 Subject: [PATCH] rustc_metadata: parametrize schema::CrateRoot by 'tcx. --- src/librustc_metadata/creader.rs | 6 +++--- src/librustc_metadata/cstore.rs | 6 +++++- src/librustc_metadata/cstore_impl.rs | 7 +------ src/librustc_metadata/decoder.rs | 11 ++++------- src/librustc_metadata/encoder.rs | 17 ++++++----------- src/librustc_metadata/index.rs | 17 ++++++++++------- src/librustc_metadata/index_builder.rs | 4 ++-- src/librustc_metadata/lib.rs | 1 + src/librustc_metadata/schema.rs | 13 ++++--------- 9 files changed, 36 insertions(+), 46 deletions(-) diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index 3e00ba3c620..991bebc647d 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -162,7 +162,7 @@ impl<'a> CrateLoader<'a> { fn verify_no_symbol_conflicts(&self, span: Span, - root: &CrateRoot) { + root: &CrateRoot<'_>) { // Check for (potential) conflicts with the local crate if self.local_crate_name == root.name && self.sess.local_crate_disambiguator() == root.disambiguator { @@ -476,7 +476,7 @@ impl<'a> CrateLoader<'a> { // Go through the crate metadata and load any crates that it references fn resolve_crate_deps(&mut self, root: &Option, - crate_root: &CrateRoot, + crate_root: &CrateRoot<'_>, metadata: &MetadataBlob, krate: CrateNum, span: Span, @@ -582,7 +582,7 @@ impl<'a> CrateLoader<'a> { /// implemented as dynamic libraries, but we have a possible future where /// custom derive (and other macro-1.1 style features) are implemented via /// executables and custom IPC. - fn load_derive_macros(&mut self, root: &CrateRoot, dylib: Option, span: Span) + fn load_derive_macros(&mut self, root: &CrateRoot<'_>, dylib: Option, span: Span) -> Vec<(ast::Name, Lrc)> { use std::{env, mem}; use crate::dynamic_lib::DynamicLibrary; diff --git a/src/librustc_metadata/cstore.rs b/src/librustc_metadata/cstore.rs index 22a13f37722..5d8fabc7e69 100644 --- a/src/librustc_metadata/cstore.rs +++ b/src/librustc_metadata/cstore.rs @@ -64,7 +64,11 @@ pub struct CrateMetadata { /// Used for decoding interpret::AllocIds in a cached & thread-safe manner. pub alloc_decoding_state: AllocDecodingState, - pub root: schema::CrateRoot, + // NOTE(eddyb) we pass `'static` to a `'tcx` parameter because this + // lifetime is only used behind `Lazy` / `LazySeq`, and therefore + // acts like an universal (`for<'tcx>`), that is paired up with + // whichever `TyCtxt` is being used to decode those values. + pub root: schema::CrateRoot<'static>, /// For each public item in this crate, we encode a key. When the /// crate is loaded, we read all the keys and put them in this diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index 2070a38b7b8..49f93c4014d 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -246,12 +246,7 @@ provide! { <'tcx> tcx, def_id, other, cdata, used_crate_source => { Lrc::new(cdata.source.clone()) } - exported_symbols => { - let cnum = cdata.cnum; - assert!(cnum != LOCAL_CRATE); - - Arc::new(cdata.exported_symbols(tcx)) - } + exported_symbols => { Arc::new(cdata.exported_symbols(tcx)) } } pub fn provide<'tcx>(providers: &mut Providers<'tcx>) { diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index d2ba82b5a1c..cf21e9bd920 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -365,7 +365,7 @@ for DecodeContext<'a, 'tcx> { implement_ty_decoder!( DecodeContext<'a, 'tcx> ); -impl<'a, 'tcx> MetadataBlob { +impl<'tcx> MetadataBlob { pub fn is_compatible(&self) -> bool { self.raw_bytes().starts_with(METADATA_HEADER) } @@ -374,7 +374,7 @@ impl<'a, 'tcx> MetadataBlob { Lazy::with_position(METADATA_HEADER.len() + 4).decode(self) } - pub fn get_root(&self) -> CrateRoot { + pub fn get_root(&self) -> CrateRoot<'tcx> { let slice = self.raw_bytes(); let offset = METADATA_HEADER.len(); let pos = (((slice[offset + 0] as u32) << 24) | ((slice[offset + 1] as u32) << 16) | @@ -444,7 +444,7 @@ impl<'tcx> EntryKind<'tcx> { /// |- proc macro #0 (DefIndex 1:N) /// |- proc macro #1 (DefIndex 1:N+1) /// \- ... -crate fn proc_macro_def_path_table(crate_root: &CrateRoot, +crate fn proc_macro_def_path_table(crate_root: &CrateRoot<'_>, proc_macros: &[(ast::Name, Lrc)]) -> DefPathTable { @@ -1126,10 +1126,7 @@ impl<'a, 'tcx> CrateMetadata { // link those in so we skip those crates. vec![] } else { - let lazy_seq: LazySeq<(ExportedSymbol<'tcx>, SymbolExportLevel)> = - LazySeq::with_position_and_length(self.root.exported_symbols.position, - self.root.exported_symbols.len); - lazy_seq.decode((self, tcx)).collect() + self.root.exported_symbols.decode((self, tcx)).collect() } } diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index 7f0993e799f..4fda3e769e0 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -309,7 +309,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { op(&mut IsolatedEncoder::new(self), data) } - fn encode_info_for_items(&mut self) -> Index { + fn encode_info_for_items(&mut self) -> Index<'tcx> { let krate = self.tcx.hir().krate(); let mut index = IndexBuilder::new(self); let vis = Spanned { span: syntax_pos::DUMMY_SP, node: hir::VisibilityKind::Public }; @@ -371,7 +371,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.lazy_seq_ref(adapted.iter().map(|rc| &**rc)) } - fn encode_crate_root(&mut self) -> Lazy { + fn encode_crate_root(&mut self) -> Lazy> { let mut i = self.position(); let crate_deps = self.tracked(IsolatedEncoder::encode_crate_deps, ()); @@ -1595,13 +1595,13 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> { // symbol associated with them (they weren't translated) or if they're an FFI // definition (as that's not defined in this crate). fn encode_exported_symbols(&mut self, - exported_symbols: &[(ExportedSymbol<'_>, SymbolExportLevel)]) - -> EncodedExportedSymbols { + exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportLevel)]) + -> LazySeq<(ExportedSymbol<'tcx>, SymbolExportLevel)> { // The metadata symbol name is special. It should not show up in // downstream crates. let metadata_symbol_name = SymbolName::new(&metadata_symbol_name(self.tcx)); - let lazy_seq = self.lazy_seq(exported_symbols + self.lazy_seq(exported_symbols .iter() .filter(|&&(ref exported_symbol, _)| { match *exported_symbol { @@ -1611,12 +1611,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> { _ => true, } }) - .cloned()); - - EncodedExportedSymbols { - len: lazy_seq.len, - position: lazy_seq.position, - } + .cloned()) } fn encode_dylib_dependency_formats(&mut self, _: ()) -> LazySeq> { diff --git a/src/librustc_metadata/index.rs b/src/librustc_metadata/index.rs index 934e871559c..dd2f59922ef 100644 --- a/src/librustc_metadata/index.rs +++ b/src/librustc_metadata/index.rs @@ -2,6 +2,7 @@ use crate::schema::*; use rustc::hir::def_id::{DefId, DefIndex}; use rustc_serialize::opaque::Encoder; +use std::marker::PhantomData; use std::u32; use log::debug; @@ -74,23 +75,25 @@ impl FixedSizeEncoding for u32 { /// `u32::MAX`. Whenever an index is visited, we fill in the /// appropriate spot by calling `record_position`. We should never /// visit the same index twice. -pub struct Index { +pub struct Index<'tcx> { positions: Vec, + _marker: PhantomData<&'tcx ()>, } -impl Index { - pub fn new(max_index: usize) -> Index { +impl Index<'tcx> { + pub fn new(max_index: usize) -> Self { Index { positions: vec![0xff; max_index * 4], + _marker: PhantomData, } } - pub fn record(&mut self, def_id: DefId, entry: Lazy>) { + pub fn record(&mut self, def_id: DefId, entry: Lazy>) { assert!(def_id.is_local()); self.record_index(def_id.index, entry); } - pub fn record_index(&mut self, item: DefIndex, entry: Lazy>) { + pub fn record_index(&mut self, item: DefIndex, entry: Lazy>) { assert!(entry.position < (u32::MAX as usize)); let position = entry.position as u32; let array_index = item.index(); @@ -105,7 +108,7 @@ impl Index { position.write_to_bytes_at(positions, array_index) } - pub fn write_index(&self, buf: &mut Encoder) -> LazySeq { + pub fn write_index(&self, buf: &mut Encoder) -> LazySeq { let pos = buf.position(); // First we write the length of the lower range ... @@ -116,7 +119,7 @@ impl Index { } } -impl<'tcx> LazySeq { +impl LazySeq> { /// Given the metadata, extract out the offset of a particular /// DefIndex (if any). #[inline(never)] diff --git a/src/librustc_metadata/index_builder.rs b/src/librustc_metadata/index_builder.rs index e780693a5a9..ee928968cb4 100644 --- a/src/librustc_metadata/index_builder.rs +++ b/src/librustc_metadata/index_builder.rs @@ -60,7 +60,7 @@ use std::ops::{Deref, DerefMut}; /// Builder that can encode new items, adding them into the index. /// Item encoding cannot be nested. pub struct IndexBuilder<'a, 'b: 'a, 'tcx: 'b> { - items: Index, + items: Index<'tcx>, pub ecx: &'a mut EncodeContext<'b, 'tcx>, } @@ -123,7 +123,7 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> { }) } - pub fn into_items(self) -> Index { + pub fn into_items(self) -> Index<'tcx> { self.items } } diff --git a/src/librustc_metadata/lib.rs b/src/librustc_metadata/lib.rs index 4f84ca69b7f..0faadd23f47 100644 --- a/src/librustc_metadata/lib.rs +++ b/src/librustc_metadata/lib.rs @@ -2,6 +2,7 @@ #![feature(box_patterns)] #![feature(drain_filter)] +#![feature(in_band_lifetimes)] #![feature(libc)] #![feature(nll)] #![feature(proc_macro_internals)] diff --git a/src/librustc_metadata/schema.rs b/src/librustc_metadata/schema.rs index 504c7da7d6b..b2f6fd65ed4 100644 --- a/src/librustc_metadata/schema.rs +++ b/src/librustc_metadata/schema.rs @@ -4,6 +4,7 @@ use rustc::hir; use rustc::hir::def::{self, CtorKind}; use rustc::hir::def_id::{DefIndex, DefId, CrateNum}; use rustc::ich::StableHashingContext; +use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel}; use rustc::middle::cstore::{DepKind, LinkagePreference, NativeLibrary, ForeignModule}; use rustc::middle::lang_items; use rustc::mir; @@ -174,7 +175,7 @@ pub enum LazyState { } #[derive(RustcEncodable, RustcDecodable)] -pub struct CrateRoot { +pub struct CrateRoot<'tcx> { pub name: Symbol, pub triple: TargetTriple, pub extra_filename: String, @@ -199,10 +200,10 @@ pub struct CrateRoot { pub source_map: LazySeq, pub def_path_table: Lazy, pub impls: LazySeq, - pub exported_symbols: EncodedExportedSymbols, + pub exported_symbols: LazySeq<(ExportedSymbol<'tcx>, SymbolExportLevel)>, pub interpret_alloc_index: LazySeq, - pub index: LazySeq, + pub index: LazySeq>, pub compiler_builtins: bool, pub needs_allocator: bool, @@ -577,9 +578,3 @@ impl_stable_hash_for!(struct GeneratorData<'tcx> { layout }); // Tags used for encoding Spans: pub const TAG_VALID_SPAN: u8 = 0; pub const TAG_INVALID_SPAN: u8 = 1; - -#[derive(RustcEncodable, RustcDecodable)] -pub struct EncodedExportedSymbols { - pub position: usize, - pub len: usize, -}