Skip to content

Commit ffd18fc

Browse files
committed
rustc_metadata: parametrize Table by element type.
1 parent f492740 commit ffd18fc

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
@@ -47,7 +47,7 @@ struct EncodeContext<'tcx> {
4747
opaque: opaque::Encoder,
4848
tcx: TyCtxt<'tcx>,
4949

50-
entries_table: Table<'tcx>,
50+
entries_table: Table<Entry<'tcx>>,
5151

5252
lazy_state: LazyState,
5353
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
@@ -156,6 +156,14 @@ crate enum LazyState {
156156
Previous(NonZeroUsize),
157157
}
158158

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

189-
pub entries_table: Lazy<[Table<'tcx>]>,
197+
pub entries_table: Lazy!([Table<Entry<'tcx>>]),
190198

191199
/// The DefIndex's of any proc macros delcared by
192200
/// this crate
@@ -226,14 +234,14 @@ crate struct Entry<'tcx> {
226234
pub stability: Option<Lazy<attr::Stability>>,
227235
pub deprecation: Option<Lazy<attr::Deprecation>>,
228236

229-
pub ty: Option<Lazy<Ty<'tcx>>>,
237+
pub ty: Option<Lazy!(Ty<'tcx>)>,
230238
pub inherent_impls: Lazy<[DefIndex]>,
231239
pub variances: Lazy<[ty::Variance]>,
232240
pub generics: Option<Lazy<ty::Generics>>,
233-
pub predicates: Option<Lazy<ty::GenericPredicates<'tcx>>>,
234-
pub predicates_defined_on: Option<Lazy<ty::GenericPredicates<'tcx>>>,
241+
pub predicates: Option<Lazy!(ty::GenericPredicates<'tcx>)>,
242+
pub predicates_defined_on: Option<Lazy!(ty::GenericPredicates<'tcx>)>,
235243

236-
pub mir: Option<Lazy<mir::Body<'tcx>>>,
244+
pub mir: Option<Lazy!(mir::Body<'tcx>)>,
237245
pub promoted_mir: Option<Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,
238246
}
239247

@@ -253,22 +261,22 @@ crate enum EntryKind<'tcx> {
253261
OpaqueTy,
254262
Enum(ReprOptions),
255263
Field,
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>>),
264+
Variant(Lazy!(VariantData<'tcx>)),
265+
Struct(Lazy!(VariantData<'tcx>), ReprOptions),
266+
Union(Lazy!(VariantData<'tcx>), ReprOptions),
267+
Fn(Lazy!(FnData<'tcx>)),
268+
ForeignFn(Lazy!(FnData<'tcx>)),
261269
Mod(Lazy<ModData>),
262270
MacroDef(Lazy<MacroDef>),
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>>),
271+
Closure(Lazy!(ClosureData<'tcx>)),
272+
Generator(Lazy!(GeneratorData<'tcx>)),
273+
Trait(Lazy!(TraitData<'tcx>)),
274+
Impl(Lazy!(ImplData<'tcx>)),
275+
Method(Lazy!(MethodData<'tcx>)),
268276
AssocType(AssocContainer),
269277
AssocOpaqueTy(AssocContainer),
270278
AssocConst(AssocContainer, ConstQualif, Lazy<RenderedConst>),
271-
TraitAlias(Lazy<TraitAliasData<'tcx>>),
279+
TraitAlias(Lazy!(TraitAliasData<'tcx>)),
272280
}
273281

274282
/// Additional data for EntryKind::Const and EntryKind::AssocConst
@@ -298,7 +306,7 @@ crate struct FnData<'tcx> {
298306
pub asyncness: hir::IsAsync,
299307
pub constness: hir::Constness,
300308
pub param_names: Lazy<[ast::Name]>,
301-
pub sig: Lazy<ty::PolyFnSig<'tcx>>,
309+
pub sig: Lazy!(ty::PolyFnSig<'tcx>),
302310
}
303311

304312
#[derive(RustcEncodable, RustcDecodable)]
@@ -309,7 +317,7 @@ crate struct VariantData<'tcx> {
309317
pub ctor: Option<DefIndex>,
310318
/// If this is a tuple struct or variant
311319
/// ctor, this is its "function" signature.
312-
pub ctor_sig: Option<Lazy<ty::PolyFnSig<'tcx>>>,
320+
pub ctor_sig: Option<Lazy!(ty::PolyFnSig<'tcx>)>,
313321
}
314322

315323
#[derive(RustcEncodable, RustcDecodable)]
@@ -318,12 +326,12 @@ crate struct TraitData<'tcx> {
318326
pub paren_sugar: bool,
319327
pub has_auto_impl: bool,
320328
pub is_marker: bool,
321-
pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
329+
pub super_predicates: Lazy!(ty::GenericPredicates<'tcx>),
322330
}
323331

324332
#[derive(RustcEncodable, RustcDecodable)]
325333
crate struct TraitAliasData<'tcx> {
326-
pub super_predicates: Lazy<ty::GenericPredicates<'tcx>>,
334+
pub super_predicates: Lazy!(ty::GenericPredicates<'tcx>),
327335
}
328336

329337
#[derive(RustcEncodable, RustcDecodable)]
@@ -334,7 +342,7 @@ crate struct ImplData<'tcx> {
334342

335343
/// This is `Some` only for impls of `CoerceUnsized`.
336344
pub coerce_unsized_info: Option<ty::adjustment::CoerceUnsizedInfo>,
337-
pub trait_ref: Option<Lazy<ty::TraitRef<'tcx>>>,
345+
pub trait_ref: Option<Lazy!(ty::TraitRef<'tcx>)>,
338346
}
339347

340348

@@ -385,7 +393,7 @@ crate struct MethodData<'tcx> {
385393

386394
#[derive(RustcEncodable, RustcDecodable)]
387395
crate struct ClosureData<'tcx> {
388-
pub sig: Lazy<ty::PolyFnSig<'tcx>>,
396+
pub sig: Lazy!(ty::PolyFnSig<'tcx>),
389397
}
390398

391399
#[derive(RustcEncodable, RustcDecodable)]

src/librustc_metadata/table.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,29 @@ impl FixedSizeEncoding for u32 {
6969
}
7070
}
7171

72-
/// While we are generating the metadata, we also track the position
73-
/// of each DefIndex. It is not required that all definitions appear
74-
/// in the metadata, nor that they are serialized in order, and
75-
/// therefore we first allocate the vector here and fill it with
76-
/// `0`. Whenever an index is visited, we fill in the
77-
/// appropriate spot by calling `record_position`. We should never
78-
/// visit the same index twice.
79-
crate struct Table<'tcx> {
72+
/// Random-access position table, allowing encoding in an arbitrary order
73+
/// (e.g. while visiting the definitions of a crate), and on-demand decoding
74+
/// of specific indices (e.g. queries for per-definition data).
75+
/// Similar to `Vec<Lazy<T>>`, but with zero-copy decoding.
76+
crate struct Table<T> {
8077
positions: Vec<u8>,
81-
_marker: PhantomData<&'tcx ()>,
78+
_marker: PhantomData<T>,
8279
}
8380

84-
impl Table<'tcx> {
81+
impl<T> Table<T> {
8582
crate fn new(max_index: usize) -> Self {
8683
Table {
8784
positions: vec![0; max_index * 4],
8885
_marker: PhantomData,
8986
}
9087
}
9188

92-
crate fn record(&mut self, def_id: DefId, entry: Lazy<Entry<'tcx>>) {
89+
crate fn record(&mut self, def_id: DefId, entry: Lazy<T>) {
9390
assert!(def_id.is_local());
9491
self.record_index(def_id.index, entry);
9592
}
9693

97-
fn record_index(&mut self, item: DefIndex, entry: Lazy<Entry<'tcx>>) {
94+
crate fn record_index(&mut self, item: DefIndex, entry: Lazy<T>) {
9895
let position: u32 = entry.position.get().try_into().unwrap();
9996
let array_index = item.index();
10097

@@ -118,11 +115,11 @@ impl Table<'tcx> {
118115
}
119116
}
120117

121-
impl Lazy<[Table<'tcx>]> {
118+
impl<T> Lazy<[Table<T>]> {
122119
/// Given the metadata, extract out the offset of a particular
123120
/// DefIndex (if any).
124121
#[inline(never)]
125-
crate fn lookup(&self, bytes: &[u8], def_index: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
122+
crate fn lookup(&self, bytes: &[u8], def_index: DefIndex) -> Option<Lazy<T>> {
126123
debug!("Table::lookup: index={:?} len={:?}",
127124
def_index,
128125
self.meta);

0 commit comments

Comments
 (0)