Skip to content

Commit 042639a

Browse files
committed
rustc_metadata: parametrize Table by element type.
1 parent 462e00b commit 042639a

File tree

3 files changed

+43
-38
lines changed

3 files changed

+43
-38
lines changed

src/librustc_metadata/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub struct EncodeContext<'a, 'tcx: 'a> {
4545
opaque: opaque::Encoder,
4646
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
4747

48-
entries_table: Table<'tcx>,
48+
entries_table: Table<Entry<'tcx>>,
4949

5050
lazy_state: LazyState,
5151
type_shorthands: FxHashMap<Ty<'tcx>, usize>,

src/librustc_metadata/schema.rs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ pub enum LazyState {
155155
Previous(NonZeroUsize),
156156
}
157157

158+
// FIXME(#59875) `Lazy!(T)` replaces `Lazy<T>`, passing the `Meta` parameter
159+
// manually, instead of relying on the default, to get the correct variance.
160+
// Only needed when `T` itself contains a parameter (e.g. `'tcx`).
161+
macro_rules! Lazy {
162+
([$T:ty]) => {Lazy<[$T], usize>};
163+
($T:ty) => {Lazy<$T, ()>};
164+
}
165+
158166
#[derive(RustcEncodable, RustcDecodable)]
159167
pub struct CrateRoot<'tcx> {
160168
pub name: Symbol,
@@ -181,10 +189,10 @@ pub struct CrateRoot<'tcx> {
181189
pub source_map: Lazy<[syntax_pos::SourceFile]>,
182190
pub def_path_table: Lazy<hir::map::definitions::DefPathTable>,
183191
pub impls: Lazy<[TraitImpls]>,
184-
pub exported_symbols: Lazy<[(ExportedSymbol<'tcx>, SymbolExportLevel)]>,
192+
pub exported_symbols: Lazy!([(ExportedSymbol<'tcx>, SymbolExportLevel)]),
185193
pub interpret_alloc_index: Lazy<[u32]>,
186194

187-
pub entries_table: Lazy<[Table<'tcx>]>,
195+
pub entries_table: Lazy!([Table<Entry<'tcx>>]),
188196

189197
pub compiler_builtins: bool,
190198
pub needs_allocator: bool,
@@ -219,14 +227,14 @@ pub struct Entry<'tcx> {
219227
pub stability: Option<Lazy<attr::Stability>>,
220228
pub deprecation: Option<Lazy<attr::Deprecation>>,
221229

222-
pub ty: Option<Lazy<Ty<'tcx>>>,
230+
pub ty: Option<Lazy!(Ty<'tcx>)>,
223231
pub inherent_impls: Lazy<[DefIndex]>,
224232
pub variances: Lazy<[ty::Variance]>,
225233
pub generics: Option<Lazy<ty::Generics>>,
226-
pub predicates: Option<Lazy<ty::GenericPredicates<'tcx>>>,
227-
pub predicates_defined_on: Option<Lazy<ty::GenericPredicates<'tcx>>>,
234+
pub predicates: Option<Lazy!(ty::GenericPredicates<'tcx>)>,
235+
pub predicates_defined_on: Option<Lazy!(ty::GenericPredicates<'tcx>)>,
228236

229-
pub mir: Option<Lazy<mir::Mir<'tcx>>>,
237+
pub mir: Option<Lazy!(mir::Mir<'tcx>)>,
230238
}
231239

232240
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
@@ -245,22 +253,22 @@ pub enum EntryKind<'tcx> {
245253
Existential,
246254
Enum(ReprOptions),
247255
Field,
248-
Variant(Lazy<VariantData<'tcx>>),
249-
Struct(Lazy<VariantData<'tcx>>, ReprOptions),
250-
Union(Lazy<VariantData<'tcx>>, ReprOptions),
251-
Fn(Lazy<FnData<'tcx>>),
252-
ForeignFn(Lazy<FnData<'tcx>>),
256+
Variant(Lazy!(VariantData<'tcx>)),
257+
Struct(Lazy!(VariantData<'tcx>), ReprOptions),
258+
Union(Lazy!(VariantData<'tcx>), ReprOptions),
259+
Fn(Lazy!(FnData<'tcx>)),
260+
ForeignFn(Lazy!(FnData<'tcx>)),
253261
Mod(Lazy<ModData>),
254262
MacroDef(Lazy<MacroDef>),
255-
Closure(Lazy<ClosureData<'tcx>>),
256-
Generator(Lazy<GeneratorData<'tcx>>),
257-
Trait(Lazy<TraitData<'tcx>>),
258-
Impl(Lazy<ImplData<'tcx>>),
259-
Method(Lazy<MethodData<'tcx>>),
263+
Closure(Lazy!(ClosureData<'tcx>)),
264+
Generator(Lazy!(GeneratorData<'tcx>)),
265+
Trait(Lazy!(TraitData<'tcx>)),
266+
Impl(Lazy!(ImplData<'tcx>)),
267+
Method(Lazy!(MethodData<'tcx>)),
260268
AssociatedType(AssociatedContainer),
261269
AssociatedExistential(AssociatedContainer),
262270
AssociatedConst(AssociatedContainer, ConstQualif, Lazy<RenderedConst>),
263-
TraitAlias(Lazy<TraitAliasData<'tcx>>),
271+
TraitAlias(Lazy!(TraitAliasData<'tcx>)),
264272
}
265273

266274
/// Additional data for EntryKind::Const and EntryKind::AssociatedConst
@@ -290,7 +298,7 @@ pub struct MacroDef {
290298
pub struct FnData<'tcx> {
291299
pub constness: hir::Constness,
292300
pub arg_names: Lazy<[ast::Name]>,
293-
pub sig: Lazy<ty::PolyFnSig<'tcx>>,
301+
pub sig: Lazy!(ty::PolyFnSig<'tcx>),
294302
}
295303

296304
#[derive(RustcEncodable, RustcDecodable)]
@@ -301,7 +309,7 @@ pub struct VariantData<'tcx> {
301309
pub ctor: Option<DefIndex>,
302310
/// If this is a tuple struct or variant
303311
/// ctor, this is its "function" signature.
304-
pub ctor_sig: Option<Lazy<ty::PolyFnSig<'tcx>>>,
312+
pub ctor_sig: Option<Lazy!(ty::PolyFnSig<'tcx>)>,
305313
}
306314

307315
#[derive(RustcEncodable, RustcDecodable)]
@@ -310,12 +318,12 @@ pub struct TraitData<'tcx> {
310318
pub paren_sugar: bool,
311319
pub has_auto_impl: bool,
312320
pub is_marker: bool,
313-
pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
321+
pub super_predicates: Lazy!(ty::GenericPredicates<'tcx>),
314322
}
315323

316324
#[derive(RustcEncodable, RustcDecodable)]
317325
pub struct TraitAliasData<'tcx> {
318-
pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
326+
pub super_predicates: Lazy!(ty::GenericPredicates<'tcx>),
319327
}
320328

321329
#[derive(RustcEncodable, RustcDecodable)]
@@ -326,7 +334,7 @@ pub struct ImplData<'tcx> {
326334

327335
/// This is `Some` only for impls of `CoerceUnsized`.
328336
pub coerce_unsized_info: Option<ty::adjustment::CoerceUnsizedInfo>,
329-
pub trait_ref: Option<Lazy<ty::TraitRef<'tcx>>>,
337+
pub trait_ref: Option<Lazy!(ty::TraitRef<'tcx>)>,
330338
}
331339

332340

@@ -377,7 +385,7 @@ pub struct MethodData<'tcx> {
377385

378386
#[derive(RustcEncodable, RustcDecodable)]
379387
pub struct ClosureData<'tcx> {
380-
pub sig: Lazy<ty::PolyFnSig<'tcx>>,
388+
pub sig: Lazy!(ty::PolyFnSig<'tcx>),
381389
}
382390

383391
#[derive(RustcEncodable, RustcDecodable)]

src/librustc_metadata/table.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@ use std::marker::PhantomData;
66
use std::num::NonZeroUsize;
77
use log::debug;
88

9-
/// While we are generating the metadata, we also track the position
10-
/// of each DefIndex. It is not required that all definitions appear
11-
/// in the metadata, nor that they are serialized in order, and
12-
/// therefore we first allocate the vector here and fill it with
13-
/// `0`. Whenever an index is visited, we fill in the
14-
/// appropriate spot by calling `record_position`. We should never
15-
/// visit the same index twice.
16-
pub struct Table<'tcx> {
9+
/// Random-access position table, allowing encoding in an arbitrary order
10+
/// (e.g. while visiting the definitions of a crate), and on-demand decoding
11+
/// of specific indices (e.g. queries for per-definition data).
12+
/// Similar to `Vec<Lazy<T>>`, but with zero-copy decoding.
13+
pub struct Table<T> {
1714
positions: [Vec<u8>; 2],
18-
_marker: PhantomData<&'tcx ()>,
15+
_marker: PhantomData<T>,
1916
}
2017

21-
impl Table<'tcx> {
18+
impl<T> Table<T> {
2219
pub fn new((max_index_lo, max_index_hi): (usize, usize)) -> Self {
2320
Table {
2421
positions: [vec![0; max_index_lo * 4],
@@ -27,12 +24,12 @@ impl Table<'tcx> {
2724
}
2825
}
2926

30-
pub fn record(&mut self, def_id: DefId, entry: Lazy<Entry<'tcx>>) {
27+
pub fn record(&mut self, def_id: DefId, entry: Lazy<T>) {
3128
assert!(def_id.is_local());
3229
self.record_index(def_id.index, entry);
3330
}
3431

35-
pub fn record_index(&mut self, item: DefIndex, entry: Lazy<Entry<'tcx>>) {
32+
pub fn record_index(&mut self, item: DefIndex, entry: Lazy<T>) {
3633
let position = entry.position.get() as u32;
3734
assert_eq!(position as usize, entry.position.get());
3835
let space_index = item.address_space().index();
@@ -64,11 +61,11 @@ impl Table<'tcx> {
6461
}
6562
}
6663

67-
impl Lazy<[Table<'tcx>]> {
64+
impl<T> Lazy<[Table<T>]> {
6865
/// Given the metadata, extract out the offset of a particular
6966
/// DefIndex (if any).
7067
#[inline(never)]
71-
pub fn lookup(&self, bytes: &[u8], def_index: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
68+
pub fn lookup(&self, bytes: &[u8], def_index: DefIndex) -> Option<Lazy<T>> {
7269
debug!("Table::lookup: index={:?} len={:?}",
7370
def_index,
7471
self.meta);

0 commit comments

Comments
 (0)