From ffd18fc22c7dcfd59bb09567217046f6e0b71e7f Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Thu, 11 Apr 2019 22:52:12 +0300 Subject: [PATCH] rustc_metadata: parametrize Table by element type. --- src/librustc_metadata/encoder.rs | 2 +- src/librustc_metadata/schema.rs | 54 ++++++++++++++++++-------------- src/librustc_metadata/table.rs | 25 +++++++-------- 3 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index abfee8eaa3cd..ef11ebdae43b 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -47,7 +47,7 @@ struct EncodeContext<'tcx> { opaque: opaque::Encoder, tcx: TyCtxt<'tcx>, - entries_table: Table<'tcx>, + entries_table: Table>, lazy_state: LazyState, type_shorthands: FxHashMap, usize>, diff --git a/src/librustc_metadata/schema.rs b/src/librustc_metadata/schema.rs index 52bbebb17f26..a1b097225b7d 100644 --- a/src/librustc_metadata/schema.rs +++ b/src/librustc_metadata/schema.rs @@ -156,6 +156,14 @@ crate enum LazyState { Previous(NonZeroUsize), } +// FIXME(#59875) `Lazy!(T)` replaces `Lazy`, passing the `Meta` parameter +// manually, instead of relying on the default, to get the correct variance. +// Only needed when `T` itself contains a parameter (e.g. `'tcx`). +macro_rules! Lazy { + ([$T:ty]) => {Lazy<[$T], usize>}; + ($T:ty) => {Lazy<$T, ()>}; +} + #[derive(RustcEncodable, RustcDecodable)] crate struct CrateRoot<'tcx> { pub name: Symbol, @@ -183,10 +191,10 @@ crate struct CrateRoot<'tcx> { pub source_map: Lazy<[syntax_pos::SourceFile]>, pub def_path_table: Lazy, pub impls: Lazy<[TraitImpls]>, - pub exported_symbols: Lazy<[(ExportedSymbol<'tcx>, SymbolExportLevel)]>, + pub exported_symbols: Lazy!([(ExportedSymbol<'tcx>, SymbolExportLevel)]), pub interpret_alloc_index: Lazy<[u32]>, - pub entries_table: Lazy<[Table<'tcx>]>, + pub entries_table: Lazy!([Table>]), /// The DefIndex's of any proc macros delcared by /// this crate @@ -226,14 +234,14 @@ crate struct Entry<'tcx> { pub stability: Option>, pub deprecation: Option>, - pub ty: Option>>, + pub ty: Option)>, pub inherent_impls: Lazy<[DefIndex]>, pub variances: Lazy<[ty::Variance]>, pub generics: Option>, - pub predicates: Option>>, - pub predicates_defined_on: Option>>, + pub predicates: Option)>, + pub predicates_defined_on: Option)>, - pub mir: Option>>, + pub mir: Option)>, pub promoted_mir: Option>>>, } @@ -253,22 +261,22 @@ crate enum EntryKind<'tcx> { OpaqueTy, Enum(ReprOptions), Field, - Variant(Lazy>), - Struct(Lazy>, ReprOptions), - Union(Lazy>, ReprOptions), - Fn(Lazy>), - ForeignFn(Lazy>), + Variant(Lazy!(VariantData<'tcx>)), + Struct(Lazy!(VariantData<'tcx>), ReprOptions), + Union(Lazy!(VariantData<'tcx>), ReprOptions), + Fn(Lazy!(FnData<'tcx>)), + ForeignFn(Lazy!(FnData<'tcx>)), Mod(Lazy), MacroDef(Lazy), - Closure(Lazy>), - Generator(Lazy>), - Trait(Lazy>), - Impl(Lazy>), - Method(Lazy>), + Closure(Lazy!(ClosureData<'tcx>)), + Generator(Lazy!(GeneratorData<'tcx>)), + Trait(Lazy!(TraitData<'tcx>)), + Impl(Lazy!(ImplData<'tcx>)), + Method(Lazy!(MethodData<'tcx>)), AssocType(AssocContainer), AssocOpaqueTy(AssocContainer), AssocConst(AssocContainer, ConstQualif, Lazy), - TraitAlias(Lazy>), + TraitAlias(Lazy!(TraitAliasData<'tcx>)), } /// Additional data for EntryKind::Const and EntryKind::AssocConst @@ -298,7 +306,7 @@ crate struct FnData<'tcx> { pub asyncness: hir::IsAsync, pub constness: hir::Constness, pub param_names: Lazy<[ast::Name]>, - pub sig: Lazy>, + pub sig: Lazy!(ty::PolyFnSig<'tcx>), } #[derive(RustcEncodable, RustcDecodable)] @@ -309,7 +317,7 @@ crate struct VariantData<'tcx> { pub ctor: Option, /// If this is a tuple struct or variant /// ctor, this is its "function" signature. - pub ctor_sig: Option>>, + pub ctor_sig: Option)>, } #[derive(RustcEncodable, RustcDecodable)] @@ -318,12 +326,12 @@ crate struct TraitData<'tcx> { pub paren_sugar: bool, pub has_auto_impl: bool, pub is_marker: bool, - pub super_predicates: Lazy>, + pub super_predicates: Lazy!(ty::GenericPredicates<'tcx>), } #[derive(RustcEncodable, RustcDecodable)] crate struct TraitAliasData<'tcx> { - pub super_predicates: Lazy>, + pub super_predicates: Lazy!(ty::GenericPredicates<'tcx>), } #[derive(RustcEncodable, RustcDecodable)] @@ -334,7 +342,7 @@ crate struct ImplData<'tcx> { /// This is `Some` only for impls of `CoerceUnsized`. pub coerce_unsized_info: Option, - pub trait_ref: Option>>, + pub trait_ref: Option)>, } @@ -385,7 +393,7 @@ crate struct MethodData<'tcx> { #[derive(RustcEncodable, RustcDecodable)] crate struct ClosureData<'tcx> { - pub sig: Lazy>, + pub sig: Lazy!(ty::PolyFnSig<'tcx>), } #[derive(RustcEncodable, RustcDecodable)] diff --git a/src/librustc_metadata/table.rs b/src/librustc_metadata/table.rs index c53d69907044..94966a44edd2 100644 --- a/src/librustc_metadata/table.rs +++ b/src/librustc_metadata/table.rs @@ -69,19 +69,16 @@ impl FixedSizeEncoding for u32 { } } -/// While we are generating the metadata, we also track the position -/// of each DefIndex. It is not required that all definitions appear -/// in the metadata, nor that they are serialized in order, and -/// therefore we first allocate the vector here and fill it with -/// `0`. Whenever an index is visited, we fill in the -/// appropriate spot by calling `record_position`. We should never -/// visit the same index twice. -crate struct Table<'tcx> { +/// Random-access position table, allowing encoding in an arbitrary order +/// (e.g. while visiting the definitions of a crate), and on-demand decoding +/// of specific indices (e.g. queries for per-definition data). +/// Similar to `Vec>`, but with zero-copy decoding. +crate struct Table { positions: Vec, - _marker: PhantomData<&'tcx ()>, + _marker: PhantomData, } -impl Table<'tcx> { +impl Table { crate fn new(max_index: usize) -> Self { Table { positions: vec![0; max_index * 4], @@ -89,12 +86,12 @@ impl Table<'tcx> { } } - crate fn record(&mut self, def_id: DefId, entry: Lazy>) { + crate fn record(&mut self, def_id: DefId, entry: Lazy) { assert!(def_id.is_local()); self.record_index(def_id.index, entry); } - fn record_index(&mut self, item: DefIndex, entry: Lazy>) { + crate fn record_index(&mut self, item: DefIndex, entry: Lazy) { let position: u32 = entry.position.get().try_into().unwrap(); let array_index = item.index(); @@ -118,11 +115,11 @@ impl Table<'tcx> { } } -impl Lazy<[Table<'tcx>]> { +impl Lazy<[Table]> { /// Given the metadata, extract out the offset of a particular /// DefIndex (if any). #[inline(never)] - crate fn lookup(&self, bytes: &[u8], def_index: DefIndex) -> Option>> { + crate fn lookup(&self, bytes: &[u8], def_index: DefIndex) -> Option> { debug!("Table::lookup: index={:?} len={:?}", def_index, self.meta);