Auto merge of #97376 - compiler-errors:lazy-polymorphic, r=jackh726

Make `Lazy*<T>` types in `rustc_metadata` not care about lifetimes until decode

This allows us to remove the `'tcx` lifetime from `CrateRoot`. This is necessary because of #97287, which makes the `'tcx` lifetime on `Ty` invariant instead of covariant, so [this hack](0a437b2ca0/compiler/rustc_metadata/src/rmeta/decoder.rs (L89-92)) no longer holds under that PR.

Introduces a trait called `ParameterizedOverTcx` which has a generic associated type that allows a type to be parameterized over that lifetime. This means we can decode, for example, `Lazy<Ty<'static>>` into any `Ty<'tcx>` depending on the `TyCtxt<'tcx>` we pass into the decode function.
This commit is contained in:
bors 2022-05-25 03:53:39 +00:00
commit 9fadabc879
10 changed files with 217 additions and 54 deletions

View File

@ -323,7 +323,7 @@ impl<'a> CrateLoader<'a> {
None None
} }
fn verify_no_symbol_conflicts(&self, root: &CrateRoot<'_>) -> Result<(), CrateError> { fn verify_no_symbol_conflicts(&self, root: &CrateRoot) -> Result<(), CrateError> {
// Check for (potential) conflicts with the local crate // Check for (potential) conflicts with the local crate
if self.sess.local_stable_crate_id() == root.stable_crate_id() { if self.sess.local_stable_crate_id() == root.stable_crate_id() {
return Err(CrateError::SymbolConflictsCurrent(root.name())); return Err(CrateError::SymbolConflictsCurrent(root.name()));
@ -342,7 +342,7 @@ impl<'a> CrateLoader<'a> {
fn verify_no_stable_crate_id_hash_conflicts( fn verify_no_stable_crate_id_hash_conflicts(
&mut self, &mut self,
root: &CrateRoot<'_>, root: &CrateRoot,
cnum: CrateNum, cnum: CrateNum,
) -> Result<(), CrateError> { ) -> Result<(), CrateError> {
if let Some(existing) = self.cstore.stable_crate_ids.insert(root.stable_crate_id(), cnum) { if let Some(existing) = self.cstore.stable_crate_ids.insert(root.stable_crate_id(), cnum) {
@ -623,7 +623,7 @@ impl<'a> CrateLoader<'a> {
fn resolve_crate_deps( fn resolve_crate_deps(
&mut self, &mut self,
root: &CratePaths, root: &CratePaths,
crate_root: &CrateRoot<'_>, crate_root: &CrateRoot,
metadata: &MetadataBlob, metadata: &MetadataBlob,
krate: CrateNum, krate: CrateNum,
dep_kind: CrateDepKind, dep_kind: CrateDepKind,

View File

@ -2,6 +2,7 @@
#![feature(decl_macro)] #![feature(decl_macro)]
#![feature(drain_filter)] #![feature(drain_filter)]
#![feature(generators)] #![feature(generators)]
#![feature(generic_associated_types)]
#![feature(let_chains)] #![feature(let_chains)]
#![feature(let_else)] #![feature(let_else)]
#![feature(nll)] #![feature(nll)]

View File

@ -25,7 +25,7 @@ use rustc_middle::thir;
use rustc_middle::ty::codec::TyDecoder; use rustc_middle::ty::codec::TyDecoder;
use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::fast_reject::SimplifiedType;
use rustc_middle::ty::GeneratorDiagnosticData; use rustc_middle::ty::GeneratorDiagnosticData;
use rustc_middle::ty::{self, Ty, TyCtxt, Visibility}; use rustc_middle::ty::{self, ParameterizedOverTcx, Ty, TyCtxt, Visibility};
use rustc_serialize::{opaque, Decodable, Decoder}; use rustc_serialize::{opaque, Decodable, Decoder};
use rustc_session::cstore::{ use rustc_session::cstore::{
CrateSource, ExternCrate, ForeignModule, LinkagePreference, NativeLib, CrateSource, ExternCrate, ForeignModule, LinkagePreference, NativeLib,
@ -86,7 +86,7 @@ pub(crate) struct CrateMetadata {
/// lifetime is only used behind `Lazy`, and therefore acts like a /// lifetime is only used behind `Lazy`, and therefore acts like a
/// universal (`for<'tcx>`), that is paired up with whichever `TyCtxt` /// universal (`for<'tcx>`), that is paired up with whichever `TyCtxt`
/// is being used to decode those values. /// is being used to decode those values.
root: CrateRoot<'static>, root: CrateRoot,
/// Trait impl data. /// Trait impl data.
/// FIXME: Used only from queries and can use query cache, /// FIXME: Used only from queries and can use query cache,
/// so pre-decoding can probably be avoided. /// so pre-decoding can probably be avoided.
@ -261,11 +261,14 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (CrateMetadataRef<'a>, TyCtxt<'tcx>) {
} }
} }
impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> LazyValue<T> { impl<T: ParameterizedOverTcx> LazyValue<T> {
fn decode<M: Metadata<'a, 'tcx>>(self, metadata: M) -> T { fn decode<'a, 'tcx, M: Metadata<'a, 'tcx>>(self, metadata: M) -> T::Value<'tcx>
where
T::Value<'tcx>: Decodable<DecodeContext<'a, 'tcx>>,
{
let mut dcx = metadata.decoder(self.position.get()); let mut dcx = metadata.decoder(self.position.get());
dcx.lazy_state = LazyState::NodeStart(self.position); dcx.lazy_state = LazyState::NodeStart(self.position);
T::decode(&mut dcx) T::Value::decode(&mut dcx)
} }
} }
@ -292,6 +295,9 @@ impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> Iterator for DecodeIterato
impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> ExactSizeIterator impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> ExactSizeIterator
for DecodeIterator<'a, 'tcx, T> for DecodeIterator<'a, 'tcx, T>
{ {
fn len(&self) -> usize {
self.elem_counter.len()
}
} }
unsafe impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> TrustedLen unsafe impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> TrustedLen
@ -299,8 +305,14 @@ unsafe impl<'a, 'tcx, T: Decodable<DecodeContext<'a, 'tcx>>> TrustedLen
{ {
} }
impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable<DecodeContext<'a, 'tcx>>> LazyArray<T> { impl<T: ParameterizedOverTcx> LazyArray<T> {
fn decode<M: Metadata<'a, 'tcx>>(self, metadata: M) -> DecodeIterator<'a, 'tcx, T> { fn decode<'a, 'tcx, M: Metadata<'a, 'tcx>>(
self,
metadata: M,
) -> DecodeIterator<'a, 'tcx, T::Value<'tcx>>
where
T::Value<'tcx>: Decodable<DecodeContext<'a, 'tcx>>,
{
let mut dcx = metadata.decoder(self.position.get()); let mut dcx = metadata.decoder(self.position.get());
dcx.lazy_state = LazyState::NodeStart(self.position); dcx.lazy_state = LazyState::NodeStart(self.position);
DecodeIterator { elem_counter: (0..self.num_elems), dcx, _phantom: PhantomData } DecodeIterator { elem_counter: (0..self.num_elems), dcx, _phantom: PhantomData }
@ -671,14 +683,14 @@ impl MetadataBlob {
.decode(self) .decode(self)
} }
pub(crate) fn get_root<'tcx>(&self) -> CrateRoot<'tcx> { pub(crate) fn get_root(&self) -> CrateRoot {
let slice = &self.blob()[..]; let slice = &self.blob()[..];
let offset = METADATA_HEADER.len(); let offset = METADATA_HEADER.len();
let pos = (((slice[offset + 0] as u32) << 24) let pos = (((slice[offset + 0] as u32) << 24)
| ((slice[offset + 1] as u32) << 16) | ((slice[offset + 1] as u32) << 16)
| ((slice[offset + 2] as u32) << 8) | ((slice[offset + 2] as u32) << 8)
| ((slice[offset + 3] as u32) << 0)) as usize; | ((slice[offset + 3] as u32) << 0)) as usize;
LazyValue::<CrateRoot<'tcx>>::from_position(NonZeroUsize::new(pos).unwrap()).decode(self) LazyValue::<CrateRoot>::from_position(NonZeroUsize::new(pos).unwrap()).decode(self)
} }
pub(crate) fn list_crate_metadata(&self, out: &mut dyn io::Write) -> io::Result<()> { pub(crate) fn list_crate_metadata(&self, out: &mut dyn io::Write) -> io::Result<()> {
@ -705,7 +717,7 @@ impl MetadataBlob {
} }
} }
impl CrateRoot<'_> { impl CrateRoot {
pub(crate) fn is_proc_macro_crate(&self) -> bool { pub(crate) fn is_proc_macro_crate(&self) -> bool {
self.proc_macro_data.is_some() self.proc_macro_data.is_some()
} }
@ -1677,7 +1689,7 @@ impl CrateMetadata {
sess: &Session, sess: &Session,
cstore: &CStore, cstore: &CStore,
blob: MetadataBlob, blob: MetadataBlob,
root: CrateRoot<'static>, root: CrateRoot,
raw_proc_macros: Option<&'static [ProcMacro]>, raw_proc_macros: Option<&'static [ProcMacro]>,
cnum: CrateNum, cnum: CrateNum,
cnum_map: CrateNumMap, cnum_map: CrateNumMap,

View File

@ -3,6 +3,7 @@ use crate::rmeta::EncodeContext;
use crate::rmeta::MetadataBlob; use crate::rmeta::MetadataBlob;
use rustc_data_structures::owning_ref::OwningRef; use rustc_data_structures::owning_ref::OwningRef;
use rustc_hir::def_path_hash_map::{Config as HashMapConfig, DefPathHashMap}; use rustc_hir::def_path_hash_map::{Config as HashMapConfig, DefPathHashMap};
use rustc_middle::parameterized_over_tcx;
use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder}; use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
use rustc_span::def_id::{DefIndex, DefPathHash}; use rustc_span::def_id::{DefIndex, DefPathHash};
@ -11,6 +12,10 @@ pub(crate) enum DefPathHashMapRef<'tcx> {
BorrowedFromTcx(&'tcx DefPathHashMap), BorrowedFromTcx(&'tcx DefPathHashMap),
} }
parameterized_over_tcx! {
DefPathHashMapRef,
}
impl DefPathHashMapRef<'_> { impl DefPathHashMapRef<'_> {
#[inline] #[inline]
pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex { pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {

View File

@ -47,7 +47,7 @@ pub(super) struct EncodeContext<'a, 'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
feat: &'tcx rustc_feature::Features, feat: &'tcx rustc_feature::Features,
tables: TableBuilders<'tcx>, tables: TableBuilders,
lazy_state: LazyState, lazy_state: LazyState,
type_shorthands: FxHashMap<Ty<'tcx>, usize>, type_shorthands: FxHashMap<Ty<'tcx>, usize>,
@ -388,10 +388,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
self.emit_usize(distance) self.emit_usize(distance)
} }
fn lazy<T: Encodable<EncodeContext<'a, 'tcx>>, B: Borrow<T>>( fn lazy<T: ParameterizedOverTcx, B: Borrow<T::Value<'tcx>>>(&mut self, value: B) -> LazyValue<T>
&mut self, where
value: B, T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>,
) -> LazyValue<T> { {
let pos = NonZeroUsize::new(self.position()).unwrap(); let pos = NonZeroUsize::new(self.position()).unwrap();
assert_eq!(self.lazy_state, LazyState::NoNode); assert_eq!(self.lazy_state, LazyState::NoNode);
@ -404,14 +404,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
LazyValue::from_position(pos) LazyValue::from_position(pos)
} }
fn lazy_array< fn lazy_array<T: ParameterizedOverTcx, I: IntoIterator<Item = B>, B: Borrow<T::Value<'tcx>>>(
T: Encodable<EncodeContext<'a, 'tcx>>,
I: IntoIterator<Item = B>,
B: Borrow<T>,
>(
&mut self, &mut self,
values: I, values: I,
) -> LazyArray<T> { ) -> LazyArray<T>
where
T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>,
{
let pos = NonZeroUsize::new(self.position()).unwrap(); let pos = NonZeroUsize::new(self.position()).unwrap();
assert_eq!(self.lazy_state, LazyState::NoNode); assert_eq!(self.lazy_state, LazyState::NoNode);
@ -456,7 +455,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
} }
} }
fn encode_def_path_hash_map(&mut self) -> LazyValue<DefPathHashMapRef<'tcx>> { fn encode_def_path_hash_map(&mut self) -> LazyValue<DefPathHashMapRef<'static>> {
self.lazy(DefPathHashMapRef::BorrowedFromTcx( self.lazy(DefPathHashMapRef::BorrowedFromTcx(
self.tcx.resolutions(()).definitions.def_path_hash_to_def_index_map(), self.tcx.resolutions(()).definitions.def_path_hash_to_def_index_map(),
)) ))
@ -535,7 +534,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
self.lazy_array(adapted.iter().map(|rc| &**rc)) self.lazy_array(adapted.iter().map(|rc| &**rc))
} }
fn encode_crate_root(&mut self) -> LazyValue<CrateRoot<'tcx>> { fn encode_crate_root(&mut self) -> LazyValue<CrateRoot> {
let tcx = self.tcx; let tcx = self.tcx;
let mut i = self.position(); let mut i = self.position();
@ -1859,7 +1858,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
fn encode_exported_symbols( fn encode_exported_symbols(
&mut self, &mut self,
exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportInfo)], exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportInfo)],
) -> LazyArray<(ExportedSymbol<'tcx>, SymbolExportInfo)> { ) -> LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)> {
empty_proc_macro!(self); empty_proc_macro!(self);
// The metadata symbol name is special. It should not show up in // The metadata symbol name is special. It should not show up in
// downstream crates. // downstream crates.

View File

@ -21,7 +21,7 @@ use rustc_middle::thir;
use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::fast_reject::SimplifiedType;
use rustc_middle::ty::query::Providers; use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, ReprOptions, Ty}; use rustc_middle::ty::{self, ReprOptions, Ty};
use rustc_middle::ty::{GeneratorDiagnosticData, TyCtxt}; use rustc_middle::ty::{GeneratorDiagnosticData, ParameterizedOverTcx, TyCtxt};
use rustc_serialize::opaque::Encoder; use rustc_serialize::opaque::Encoder;
use rustc_session::config::SymbolManglingVersion; use rustc_session::config::SymbolManglingVersion;
use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib}; use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib};
@ -83,6 +83,10 @@ struct LazyValue<T> {
_marker: PhantomData<fn() -> T>, _marker: PhantomData<fn() -> T>,
} }
impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyValue<T> {
type Value<'tcx> = LazyValue<T::Value<'tcx>>;
}
impl<T> LazyValue<T> { impl<T> LazyValue<T> {
fn from_position(position: NonZeroUsize) -> LazyValue<T> { fn from_position(position: NonZeroUsize) -> LazyValue<T> {
LazyValue { position, _marker: PhantomData } LazyValue { position, _marker: PhantomData }
@ -105,6 +109,10 @@ struct LazyArray<T> {
_marker: PhantomData<fn() -> T>, _marker: PhantomData<fn() -> T>,
} }
impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyArray<T> {
type Value<'tcx> = LazyArray<T::Value<'tcx>>;
}
impl<T> LazyArray<T> { impl<T> LazyArray<T> {
fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray<T> { fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray<T> {
LazyArray { position, num_elems, _marker: PhantomData } LazyArray { position, num_elems, _marker: PhantomData }
@ -126,6 +134,10 @@ struct LazyTable<I, T> {
_marker: PhantomData<fn(I) -> T>, _marker: PhantomData<fn(I) -> T>,
} }
impl<I: 'static, T: ParameterizedOverTcx> ParameterizedOverTcx for LazyTable<I, T> {
type Value<'tcx> = LazyTable<I, T::Value<'tcx>>;
}
impl<I, T> LazyTable<I, T> { impl<I, T> LazyTable<I, T> {
fn from_position_and_encoded_size( fn from_position_and_encoded_size(
position: NonZeroUsize, position: NonZeroUsize,
@ -199,7 +211,7 @@ pub(crate) struct ProcMacroData {
/// a normal crate, much of what we serialized would be unusable in addition /// a normal crate, much of what we serialized would be unusable in addition
/// to being unused. /// to being unused.
#[derive(MetadataEncodable, MetadataDecodable)] #[derive(MetadataEncodable, MetadataDecodable)]
pub(crate) struct CrateRoot<'tcx> { pub(crate) struct CrateRoot {
name: Symbol, name: Symbol,
triple: TargetTriple, triple: TargetTriple,
extra_filename: String, extra_filename: String,
@ -226,16 +238,16 @@ pub(crate) struct CrateRoot<'tcx> {
interpret_alloc_index: LazyArray<u32>, interpret_alloc_index: LazyArray<u32>,
proc_macro_data: Option<ProcMacroData>, proc_macro_data: Option<ProcMacroData>,
tables: LazyTables<'tcx>, tables: LazyTables,
debugger_visualizers: LazyArray<rustc_span::DebuggerVisualizerFile>, debugger_visualizers: LazyArray<rustc_span::DebuggerVisualizerFile>,
exported_symbols: LazyArray<(ExportedSymbol<'tcx>, SymbolExportInfo)>, exported_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
syntax_contexts: SyntaxContextTable, syntax_contexts: SyntaxContextTable,
expn_data: ExpnDataTable, expn_data: ExpnDataTable,
expn_hashes: ExpnHashTable, expn_hashes: ExpnHashTable,
def_path_hash_map: LazyValue<DefPathHashMapRef<'tcx>>, def_path_hash_map: LazyValue<DefPathHashMapRef<'static>>,
source_map: LazyArray<rustc_span::SourceFile>, source_map: LazyArray<rustc_span::SourceFile>,
@ -301,17 +313,17 @@ pub(crate) struct IncoherentImpls {
macro_rules! define_tables { macro_rules! define_tables {
($($name:ident: Table<$IDX:ty, $T:ty>),+ $(,)?) => { ($($name:ident: Table<$IDX:ty, $T:ty>),+ $(,)?) => {
#[derive(MetadataEncodable, MetadataDecodable)] #[derive(MetadataEncodable, MetadataDecodable)]
pub(crate) struct LazyTables<'tcx> { pub(crate) struct LazyTables {
$($name: LazyTable<$IDX, $T>),+ $($name: LazyTable<$IDX, $T>),+
} }
#[derive(Default)] #[derive(Default)]
struct TableBuilders<'tcx> { struct TableBuilders {
$($name: TableBuilder<$IDX, $T>),+ $($name: TableBuilder<$IDX, $T>),+
} }
impl<'tcx> TableBuilders<'tcx> { impl TableBuilders {
fn encode(&self, buf: &mut Encoder) -> LazyTables<'tcx> { fn encode(&self, buf: &mut Encoder) -> LazyTables {
LazyTables { LazyTables {
$($name: self.$name.encode(buf)),+ $($name: self.$name.encode(buf)),+
} }
@ -333,23 +345,23 @@ define_tables! {
lookup_const_stability: Table<DefIndex, LazyValue<attr::ConstStability>>, lookup_const_stability: Table<DefIndex, LazyValue<attr::ConstStability>>,
lookup_deprecation_entry: Table<DefIndex, LazyValue<attr::Deprecation>>, lookup_deprecation_entry: Table<DefIndex, LazyValue<attr::Deprecation>>,
// As an optimization, a missing entry indicates an empty `&[]`. // As an optimization, a missing entry indicates an empty `&[]`.
explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Predicate<'tcx>, Span)>>, explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Predicate<'static>, Span)>>,
explicit_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'tcx>>>, explicit_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
generics_of: Table<DefIndex, LazyValue<ty::Generics>>, generics_of: Table<DefIndex, LazyValue<ty::Generics>>,
// As an optimization, a missing entry indicates an empty `&[]`. // As an optimization, a missing entry indicates an empty `&[]`.
inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Predicate<'tcx>, Span)>>, inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Predicate<'static>, Span)>>,
super_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'tcx>>>, super_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
type_of: Table<DefIndex, LazyValue<Ty<'tcx>>>, type_of: Table<DefIndex, LazyValue<Ty<'static>>>,
variances_of: Table<DefIndex, LazyArray<ty::Variance>>, variances_of: Table<DefIndex, LazyArray<ty::Variance>>,
fn_sig: Table<DefIndex, LazyValue<ty::PolyFnSig<'tcx>>>, fn_sig: Table<DefIndex, LazyValue<ty::PolyFnSig<'static>>>,
codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>, codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>,
impl_trait_ref: Table<DefIndex, LazyValue<ty::TraitRef<'tcx>>>, impl_trait_ref: Table<DefIndex, LazyValue<ty::TraitRef<'static>>>,
const_param_default: Table<DefIndex, LazyValue<rustc_middle::ty::Const<'tcx>>>, const_param_default: Table<DefIndex, LazyValue<rustc_middle::ty::Const<'static>>>,
optimized_mir: Table<DefIndex, LazyValue<mir::Body<'tcx>>>, optimized_mir: Table<DefIndex, LazyValue<mir::Body<'static>>>,
mir_for_ctfe: Table<DefIndex, LazyValue<mir::Body<'tcx>>>, mir_for_ctfe: Table<DefIndex, LazyValue<mir::Body<'static>>>,
promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'tcx>>>>, promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'static>>>>,
// FIXME(compiler-errors): Why isn't this a LazyArray? // FIXME(compiler-errors): Why isn't this a LazyArray?
thir_abstract_const: Table<DefIndex, LazyValue<&'tcx [thir::abstract_const::Node<'tcx>]>>, thir_abstract_const: Table<DefIndex, LazyValue<&'static [thir::abstract_const::Node<'static>]>>,
impl_parent: Table<DefIndex, RawDefId>, impl_parent: Table<DefIndex, RawDefId>,
impl_polarity: Table<DefIndex, ty::ImplPolarity>, impl_polarity: Table<DefIndex, ty::ImplPolarity>,
impl_constness: Table<DefIndex, hir::Constness>, impl_constness: Table<DefIndex, hir::Constness>,
@ -376,7 +388,7 @@ define_tables! {
def_keys: Table<DefIndex, LazyValue<DefKey>>, def_keys: Table<DefIndex, LazyValue<DefKey>>,
def_path_hashes: Table<DefIndex, DefPathHash>, def_path_hashes: Table<DefIndex, DefPathHash>,
proc_macro_quoted_spans: Table<usize, LazyValue<Span>>, proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
generator_diagnostic_data: Table<DefIndex, LazyValue<GeneratorDiagnosticData<'tcx>>>, generator_diagnostic_data: Table<DefIndex, LazyValue<GeneratorDiagnosticData<'static>>>,
may_have_doc_links: Table<DefIndex, ()>, may_have_doc_links: Table<DefIndex, ()>,
} }
@ -477,3 +489,14 @@ pub fn provide(providers: &mut Providers) {
encoder::provide(providers); encoder::provide(providers);
decoder::provide(providers); decoder::provide(providers);
} }
trivially_parameterized_over_tcx! {
VariantData,
AssocFnData,
EntryKind,
RawDefId,
TraitImpls,
IncoherentImpls,
CrateRoot,
CrateDep,
}

View File

@ -3,6 +3,7 @@ use crate::rmeta::*;
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_hir::def::{CtorKind, CtorOf}; use rustc_hir::def::{CtorKind, CtorOf};
use rustc_index::vec::Idx; use rustc_index::vec::Idx;
use rustc_middle::ty::ParameterizedOverTcx;
use rustc_serialize::opaque::Encoder; use rustc_serialize::opaque::Encoder;
use rustc_serialize::Encoder as _; use rustc_serialize::Encoder as _;
use rustc_span::hygiene::MacroKind; use rustc_span::hygiene::MacroKind;
@ -296,7 +297,7 @@ where
} }
} }
impl<I: Idx, T> LazyTable<I, T> impl<I: Idx, T: ParameterizedOverTcx> LazyTable<I, T>
where where
Option<T>: FixedSizeEncoding, Option<T>: FixedSizeEncoding,
{ {
@ -306,9 +307,9 @@ where
&self, &self,
metadata: M, metadata: M,
i: I, i: I,
) -> Option<T> ) -> Option<T::Value<'tcx>>
where where
Option<T>: FixedSizeEncoding<ByteArray = [u8; N]>, Option<T::Value<'tcx>>: FixedSizeEncoding<ByteArray = [u8; N]>,
{ {
debug!("LazyTable::lookup: index={:?} len={:?}", i, self.encoded_size); debug!("LazyTable::lookup: index={:?} len={:?}", i, self.encoded_size);
@ -322,7 +323,7 @@ where
/// Size of the table in entries, including possible gaps. /// Size of the table in entries, including possible gaps.
pub(super) fn size<const N: usize>(&self) -> usize pub(super) fn size<const N: usize>(&self) -> usize
where where
Option<T>: FixedSizeEncoding<ByteArray = [u8; N]>, for<'tcx> Option<T::Value<'tcx>>: FixedSizeEncoding<ByteArray = [u8; N]>,
{ {
self.encoded_size / N self.encoded_size / N
} }

View File

@ -33,6 +33,7 @@
#![feature(discriminant_kind)] #![feature(discriminant_kind)]
#![feature(exhaustive_patterns)] #![feature(exhaustive_patterns)]
#![feature(get_mut_unchecked)] #![feature(get_mut_unchecked)]
#![feature(generic_associated_types)]
#![feature(if_let_guard)] #![feature(if_let_guard)]
#![feature(map_first_last)] #![feature(map_first_last)]
#![feature(negative_impls)] #![feature(negative_impls)]

View File

@ -72,6 +72,7 @@ pub use self::context::{
}; };
pub use self::instance::{Instance, InstanceDef}; pub use self::instance::{Instance, InstanceDef};
pub use self::list::List; pub use self::list::List;
pub use self::parameterized::ParameterizedOverTcx;
pub use self::rvalue_scopes::RvalueScopes; pub use self::rvalue_scopes::RvalueScopes;
pub use self::sty::BoundRegionKind::*; pub use self::sty::BoundRegionKind::*;
pub use self::sty::RegionKind::*; pub use self::sty::RegionKind::*;
@ -119,6 +120,7 @@ mod generics;
mod impls_ty; mod impls_ty;
mod instance; mod instance;
mod list; mod list;
mod parameterized;
mod rvalue_scopes; mod rvalue_scopes;
mod structural_impls; mod structural_impls;
mod sty; mod sty;

View File

@ -0,0 +1,119 @@
use rustc_hir::def_id::DefId;
use rustc_index::vec::{Idx, IndexVec};
use crate::middle::exported_symbols::ExportedSymbol;
use crate::mir::Body;
use crate::thir::abstract_const::Node;
use crate::ty::{
self, Const, FnSig, GeneratorDiagnosticData, GenericPredicates, Predicate, TraitRef, Ty,
};
pub trait ParameterizedOverTcx: 'static {
#[allow(unused_lifetimes)]
type Value<'tcx>;
}
impl<T: ParameterizedOverTcx> ParameterizedOverTcx for &'static [T] {
type Value<'tcx> = &'tcx [T::Value<'tcx>];
}
impl<T: ParameterizedOverTcx> ParameterizedOverTcx for Option<T> {
type Value<'tcx> = Option<T::Value<'tcx>>;
}
impl<A: ParameterizedOverTcx, B: ParameterizedOverTcx> ParameterizedOverTcx for (A, B) {
type Value<'tcx> = (A::Value<'tcx>, B::Value<'tcx>);
}
impl<I: Idx + 'static, T: ParameterizedOverTcx> ParameterizedOverTcx for IndexVec<I, T> {
type Value<'tcx> = IndexVec<I, T::Value<'tcx>>;
}
impl<T: ParameterizedOverTcx> ParameterizedOverTcx for ty::Binder<'static, T> {
type Value<'tcx> = ty::Binder<'tcx, T::Value<'tcx>>;
}
#[macro_export]
macro_rules! trivially_parameterized_over_tcx {
($($ty:ty),+ $(,)?) => {
$(
impl $crate::ty::ParameterizedOverTcx for $ty {
#[allow(unused_lifetimes)]
type Value<'tcx> = $ty;
}
)*
}
}
trivially_parameterized_over_tcx! {
usize,
(),
u32,
std::string::String,
crate::metadata::ModChild,
crate::middle::codegen_fn_attrs::CodegenFnAttrs,
crate::middle::exported_symbols::SymbolExportInfo,
crate::mir::ConstQualifs,
ty::Generics,
ty::ImplPolarity,
ty::ReprOptions,
ty::TraitDef,
ty::Visibility,
ty::adjustment::CoerceUnsizedInfo,
ty::fast_reject::SimplifiedTypeGen<DefId>,
rustc_ast::Attribute,
rustc_ast::MacArgs,
rustc_attr::ConstStability,
rustc_attr::Deprecation,
rustc_attr::Stability,
rustc_hir::Constness,
rustc_hir::Defaultness,
rustc_hir::GeneratorKind,
rustc_hir::IsAsync,
rustc_hir::LangItem,
rustc_hir::def::DefKind,
rustc_hir::def_id::DefIndex,
rustc_hir::definitions::DefKey,
rustc_index::bit_set::FiniteBitSet<u32>,
rustc_session::cstore::ForeignModule,
rustc_session::cstore::LinkagePreference,
rustc_session::cstore::NativeLib,
rustc_span::DebuggerVisualizerFile,
rustc_span::ExpnData,
rustc_span::ExpnHash,
rustc_span::ExpnId,
rustc_span::SourceFile,
rustc_span::Span,
rustc_span::Symbol,
rustc_span::def_id::DefPathHash,
rustc_span::hygiene::SyntaxContextData,
rustc_span::symbol::Ident,
rustc_type_ir::Variance,
}
// HACK(compiler-errors): This macro rule can only take an ident,
// not a path, due to parsing ambiguity reasons. That means we gotta
// import all of these types above.
#[macro_export]
macro_rules! parameterized_over_tcx {
($($ident:ident),+ $(,)?) => {
$(
impl $crate::ty::ParameterizedOverTcx for $ident<'static> {
type Value<'tcx> = $ident<'tcx>;
}
)*
}
}
parameterized_over_tcx! {
Ty,
FnSig,
GenericPredicates,
TraitRef,
Const,
Predicate,
GeneratorDiagnosticData,
Body,
Node,
ExportedSymbol,
}