Skip to content

Commit 5139b53

Browse files
authored
Rollup merge of rust-lang#66399 - eddyb:rmeta-table-cleanup, r=Mark-Simulacrum
rustc_metadata: simplify the interactions between Lazy and Table. These are small post-rust-lang#59953 cleanups (including undoing some contrivances from that PR). r? @michaelwoerister
2 parents 2f1a4b3 + a0556b3 commit 5139b53

File tree

4 files changed

+98
-160
lines changed

4 files changed

+98
-160
lines changed

src/librustc_metadata/rmeta/decoder.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Decoding metadata from a single crate's metadata
22

33
use crate::rmeta::*;
4-
use crate::rmeta::table::{FixedSizeEncoding, PerDefTable};
4+
use crate::rmeta::table::{FixedSizeEncoding, Table};
55

6-
use rustc_index::vec::IndexVec;
6+
use rustc_index::vec::{Idx, IndexVec};
77
use rustc_data_structures::sync::{Lrc, Lock, Once, AtomicCell};
88
use rustc::hir::map::{DefKey, DefPath, DefPathData, DefPathHash};
99
use rustc::hir::map::definitions::DefPathTable;
@@ -32,7 +32,7 @@ use std::mem;
3232
use std::num::NonZeroUsize;
3333
use std::u32;
3434

35-
use rustc_serialize::{Decodable, Decoder, Encodable, SpecializedDecoder, opaque};
35+
use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
3636
use syntax::attr;
3737
use syntax::ast::{self, Ident};
3838
use syntax::source_map::{self, respan, Spanned};
@@ -217,15 +217,15 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'tcx>) {
217217
}
218218
}
219219

220-
impl<'a, 'tcx, T: Encodable + Decodable> Lazy<T> {
220+
impl<'a, 'tcx, T: Decodable> Lazy<T> {
221221
fn decode<M: Metadata<'a, 'tcx>>(self, metadata: M) -> T {
222222
let mut dcx = metadata.decoder(self.position.get());
223223
dcx.lazy_state = LazyState::NodeStart(self.position);
224224
T::decode(&mut dcx).unwrap()
225225
}
226226
}
227227

228-
impl<'a: 'x, 'tcx: 'x, 'x, T: Encodable + Decodable> Lazy<[T]> {
228+
impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable> Lazy<[T]> {
229229
fn decode<M: Metadata<'a, 'tcx>>(
230230
self,
231231
metadata: M,
@@ -324,13 +324,13 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> {
324324
}
325325
}
326326

327-
impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> {
327+
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<T>> for DecodeContext<'a, 'tcx> {
328328
fn specialized_decode(&mut self) -> Result<Lazy<T>, Self::Error> {
329329
self.read_lazy_with_meta(())
330330
}
331331
}
332332

333-
impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> {
333+
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a, 'tcx> {
334334
fn specialized_decode(&mut self) -> Result<Lazy<[T]>, Self::Error> {
335335
let len = self.read_usize()?;
336336
if len == 0 {
@@ -341,10 +341,10 @@ impl<'a, 'tcx, T: Encodable> SpecializedDecoder<Lazy<[T]>> for DecodeContext<'a,
341341
}
342342
}
343343

344-
impl<'a, 'tcx, T> SpecializedDecoder<Lazy<PerDefTable<T>>> for DecodeContext<'a, 'tcx>
344+
impl<'a, 'tcx, I: Idx, T> SpecializedDecoder<Lazy<Table<I, T>>> for DecodeContext<'a, 'tcx>
345345
where Option<T>: FixedSizeEncoding,
346346
{
347-
fn specialized_decode(&mut self) -> Result<Lazy<PerDefTable<T>>, Self::Error> {
347+
fn specialized_decode(&mut self) -> Result<Lazy<Table<I, T>>, Self::Error> {
348348
let len = self.read_usize()?;
349349
self.read_lazy_with_meta(len)
350350
}

src/librustc_metadata/rmeta/encoder.rs

+11-56
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::rmeta::*;
2-
use crate::rmeta::table::{FixedSizeEncoding, PerDefTable};
2+
use crate::rmeta::table::FixedSizeEncoding;
33

44
use rustc::middle::cstore::{LinkagePreference, NativeLibrary,
55
EncodedMetadata, ForeignModule};
@@ -8,7 +8,7 @@ use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LocalDefId,
88
use rustc::hir::{GenericParamKind, AnonConst};
99
use rustc::hir::map::definitions::DefPathTable;
1010
use rustc_data_structures::fingerprint::Fingerprint;
11-
use rustc_index::vec::IndexVec;
11+
use rustc_index::vec::Idx;
1212
use rustc::middle::dependency_format::Linkage;
1313
use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel,
1414
metadata_symbol_name};
@@ -47,7 +47,7 @@ struct EncodeContext<'tcx> {
4747
opaque: opaque::Encoder,
4848
tcx: TyCtxt<'tcx>,
4949

50-
per_def: PerDefTables<'tcx>,
50+
per_def: PerDefTableBuilders<'tcx>,
5151

5252
lazy_state: LazyState,
5353
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
@@ -60,30 +60,6 @@ struct EncodeContext<'tcx> {
6060
source_file_cache: Lrc<SourceFile>,
6161
}
6262

63-
#[derive(Default)]
64-
struct PerDefTables<'tcx> {
65-
kind: PerDefTable<Lazy<EntryKind<'tcx>>>,
66-
visibility: PerDefTable<Lazy<ty::Visibility>>,
67-
span: PerDefTable<Lazy<Span>>,
68-
attributes: PerDefTable<Lazy<[ast::Attribute]>>,
69-
children: PerDefTable<Lazy<[DefIndex]>>,
70-
stability: PerDefTable<Lazy<attr::Stability>>,
71-
deprecation: PerDefTable<Lazy<attr::Deprecation>>,
72-
73-
ty: PerDefTable<Lazy<Ty<'tcx>>>,
74-
fn_sig: PerDefTable<Lazy<ty::PolyFnSig<'tcx>>>,
75-
impl_trait_ref: PerDefTable<Lazy<ty::TraitRef<'tcx>>>,
76-
inherent_impls: PerDefTable<Lazy<[DefIndex]>>,
77-
variances: PerDefTable<Lazy<[ty::Variance]>>,
78-
generics: PerDefTable<Lazy<ty::Generics>>,
79-
explicit_predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,
80-
inferred_outlives: PerDefTable<Lazy<&'tcx [(ty::Predicate<'tcx>, Span)]>>,
81-
super_predicates: PerDefTable<Lazy<ty::GenericPredicates<'tcx>>>,
82-
83-
mir: PerDefTable<Lazy<mir::Body<'tcx>>>,
84-
promoted_mir: PerDefTable<Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,
85-
}
86-
8763
macro_rules! encoder_methods {
8864
($($name:ident($ty:ty);)*) => {
8965
$(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> {
@@ -122,13 +98,13 @@ impl<'tcx> Encoder for EncodeContext<'tcx> {
12298
}
12399
}
124100

125-
impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<T>> for EncodeContext<'tcx> {
101+
impl<'tcx, T> SpecializedEncoder<Lazy<T>> for EncodeContext<'tcx> {
126102
fn specialized_encode(&mut self, lazy: &Lazy<T>) -> Result<(), Self::Error> {
127103
self.emit_lazy_distance(*lazy)
128104
}
129105
}
130106

131-
impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
107+
impl<'tcx, T> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
132108
fn specialized_encode(&mut self, lazy: &Lazy<[T]>) -> Result<(), Self::Error> {
133109
self.emit_usize(lazy.meta)?;
134110
if lazy.meta == 0 {
@@ -138,10 +114,10 @@ impl<'tcx, T: Encodable> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
138114
}
139115
}
140116

141-
impl<'tcx, T> SpecializedEncoder<Lazy<PerDefTable<T>>> for EncodeContext<'tcx>
117+
impl<'tcx, I: Idx, T> SpecializedEncoder<Lazy<Table<I, T>>> for EncodeContext<'tcx>
142118
where Option<T>: FixedSizeEncoding,
143119
{
144-
fn specialized_encode(&mut self, lazy: &Lazy<PerDefTable<T>>) -> Result<(), Self::Error> {
120+
fn specialized_encode(&mut self, lazy: &Lazy<Table<I, T>>) -> Result<(), Self::Error> {
145121
self.emit_usize(lazy.meta)?;
146122
self.emit_lazy_distance(*lazy)
147123
}
@@ -307,14 +283,14 @@ impl<I, T: Encodable> EncodeContentsForLazy<[T]> for I
307283
}
308284
}
309285

310-
// Shorthand for `$self.$tables.$table.set($key, $self.lazy($value))`, which would
286+
// Shorthand for `$self.$tables.$table.set($def_id.index, $self.lazy($value))`, which would
311287
// normally need extra variables to avoid errors about multiple mutable borrows.
312288
macro_rules! record {
313-
($self:ident.$tables:ident.$table:ident[$key:expr] <- $value:expr) => {{
289+
($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
314290
{
315291
let value = $value;
316292
let lazy = $self.lazy(value);
317-
$self.$tables.$table.set($key, lazy);
293+
$self.$tables.$table.set($def_id.index, lazy);
318294
}
319295
}}
320296
}
@@ -509,28 +485,7 @@ impl<'tcx> EncodeContext<'tcx> {
509485

510486

511487
i = self.position();
512-
let per_def = LazyPerDefTables {
513-
kind: self.per_def.kind.encode(&mut self.opaque),
514-
visibility: self.per_def.visibility.encode(&mut self.opaque),
515-
span: self.per_def.span.encode(&mut self.opaque),
516-
attributes: self.per_def.attributes.encode(&mut self.opaque),
517-
children: self.per_def.children.encode(&mut self.opaque),
518-
stability: self.per_def.stability.encode(&mut self.opaque),
519-
deprecation: self.per_def.deprecation.encode(&mut self.opaque),
520-
521-
ty: self.per_def.ty.encode(&mut self.opaque),
522-
fn_sig: self.per_def.fn_sig.encode(&mut self.opaque),
523-
impl_trait_ref: self.per_def.impl_trait_ref.encode(&mut self.opaque),
524-
inherent_impls: self.per_def.inherent_impls.encode(&mut self.opaque),
525-
variances: self.per_def.variances.encode(&mut self.opaque),
526-
generics: self.per_def.generics.encode(&mut self.opaque),
527-
explicit_predicates: self.per_def.explicit_predicates.encode(&mut self.opaque),
528-
inferred_outlives: self.per_def.inferred_outlives.encode(&mut self.opaque),
529-
super_predicates: self.per_def.super_predicates.encode(&mut self.opaque),
530-
531-
mir: self.per_def.mir.encode(&mut self.opaque),
532-
promoted_mir: self.per_def.promoted_mir.encode(&mut self.opaque),
533-
};
488+
let per_def = self.per_def.encode(&mut self.opaque);
534489
let per_def_bytes = self.position() - i;
535490

536491
// Encode the proc macro data

src/librustc_metadata/rmeta/mod.rs

+49-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use decoder::Metadata;
2-
use table::PerDefTable;
2+
use table::{Table, TableBuilder};
33

44
use rustc::hir;
55
use rustc::hir::def::{self, CtorKind};
@@ -15,7 +15,7 @@ use rustc_target::spec::{PanicStrategy, TargetTriple};
1515
use rustc_index::vec::IndexVec;
1616
use rustc_data_structures::svh::Svh;
1717
use rustc_data_structures::sync::MetadataRef;
18-
use rustc_serialize::Encodable;
18+
use rustc_serialize::opaque::Encoder;
1919
use syntax::{ast, attr};
2020
use syntax::edition::Edition;
2121
use syntax::symbol::Symbol;
@@ -59,7 +59,7 @@ trait LazyMeta {
5959
fn min_size(meta: Self::Meta) -> usize;
6060
}
6161

62-
impl<T: Encodable> LazyMeta for T {
62+
impl<T> LazyMeta for T {
6363
type Meta = ();
6464

6565
fn min_size(_: ()) -> usize {
@@ -68,7 +68,7 @@ impl<T: Encodable> LazyMeta for T {
6868
}
6969
}
7070

71-
impl<T: Encodable> LazyMeta for [T] {
71+
impl<T> LazyMeta for [T] {
7272
type Meta = usize;
7373

7474
fn min_size(len: usize) -> usize {
@@ -124,13 +124,13 @@ impl<T: ?Sized + LazyMeta> Lazy<T> {
124124
}
125125
}
126126

127-
impl<T: Encodable> Lazy<T> {
127+
impl<T> Lazy<T> {
128128
fn from_position(position: NonZeroUsize) -> Lazy<T> {
129129
Lazy::from_position_and_meta(position, ())
130130
}
131131
}
132132

133-
impl<T: Encodable> Lazy<[T]> {
133+
impl<T> Lazy<[T]> {
134134
fn empty() -> Lazy<[T]> {
135135
Lazy::from_position_and_meta(NonZeroUsize::new(1).unwrap(), 0)
136136
}
@@ -166,8 +166,7 @@ enum LazyState {
166166
// manually, instead of relying on the default, to get the correct variance.
167167
// Only needed when `T` itself contains a parameter (e.g. `'tcx`).
168168
macro_rules! Lazy {
169-
(Table<$T:ty>) => {Lazy<Table<$T>, usize>};
170-
(PerDefTable<$T:ty>) => {Lazy<PerDefTable<$T>, usize>};
169+
(Table<$I:ty, $T:ty>) => {Lazy<Table<$I, $T>, usize>};
171170
([$T:ty]) => {Lazy<[$T], usize>};
172171
($T:ty) => {Lazy<$T, ()>};
173172
}
@@ -232,31 +231,53 @@ crate struct TraitImpls {
232231
impls: Lazy<[DefIndex]>,
233232
}
234233

235-
#[derive(RustcEncodable, RustcDecodable)]
236-
crate struct LazyPerDefTables<'tcx> {
237-
kind: Lazy!(PerDefTable<Lazy!(EntryKind<'tcx>)>),
238-
visibility: Lazy!(PerDefTable<Lazy<ty::Visibility>>),
239-
span: Lazy!(PerDefTable<Lazy<Span>>),
240-
attributes: Lazy!(PerDefTable<Lazy<[ast::Attribute]>>),
241-
children: Lazy!(PerDefTable<Lazy<[DefIndex]>>),
242-
stability: Lazy!(PerDefTable<Lazy<attr::Stability>>),
243-
deprecation: Lazy!(PerDefTable<Lazy<attr::Deprecation>>),
244-
ty: Lazy!(PerDefTable<Lazy!(Ty<'tcx>)>),
245-
fn_sig: Lazy!(PerDefTable<Lazy!(ty::PolyFnSig<'tcx>)>),
246-
impl_trait_ref: Lazy!(PerDefTable<Lazy!(ty::TraitRef<'tcx>)>),
247-
inherent_impls: Lazy!(PerDefTable<Lazy<[DefIndex]>>),
248-
variances: Lazy!(PerDefTable<Lazy<[ty::Variance]>>),
249-
generics: Lazy!(PerDefTable<Lazy<ty::Generics>>),
250-
explicit_predicates: Lazy!(PerDefTable<Lazy!(ty::GenericPredicates<'tcx>)>),
234+
/// Define `LazyPerDefTables` and `PerDefTableBuilders` at the same time.
235+
macro_rules! define_per_def_tables {
236+
($($name:ident: Table<DefIndex, $T:ty>),+ $(,)?) => {
237+
#[derive(RustcEncodable, RustcDecodable)]
238+
crate struct LazyPerDefTables<'tcx> {
239+
$($name: Lazy!(Table<DefIndex, $T>)),+
240+
}
241+
242+
#[derive(Default)]
243+
struct PerDefTableBuilders<'tcx> {
244+
$($name: TableBuilder<DefIndex, $T>),+
245+
}
246+
247+
impl PerDefTableBuilders<'tcx> {
248+
fn encode(&self, buf: &mut Encoder) -> LazyPerDefTables<'tcx> {
249+
LazyPerDefTables {
250+
$($name: self.$name.encode(buf)),+
251+
}
252+
}
253+
}
254+
}
255+
}
256+
257+
define_per_def_tables! {
258+
kind: Table<DefIndex, Lazy!(EntryKind<'tcx>)>,
259+
visibility: Table<DefIndex, Lazy<ty::Visibility>>,
260+
span: Table<DefIndex, Lazy<Span>>,
261+
attributes: Table<DefIndex, Lazy<[ast::Attribute]>>,
262+
children: Table<DefIndex, Lazy<[DefIndex]>>,
263+
stability: Table<DefIndex, Lazy<attr::Stability>>,
264+
deprecation: Table<DefIndex, Lazy<attr::Deprecation>>,
265+
ty: Table<DefIndex, Lazy!(Ty<'tcx>)>,
266+
fn_sig: Table<DefIndex, Lazy!(ty::PolyFnSig<'tcx>)>,
267+
impl_trait_ref: Table<DefIndex, Lazy!(ty::TraitRef<'tcx>)>,
268+
inherent_impls: Table<DefIndex, Lazy<[DefIndex]>>,
269+
variances: Table<DefIndex, Lazy<[ty::Variance]>>,
270+
generics: Table<DefIndex, Lazy<ty::Generics>>,
271+
explicit_predicates: Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>,
251272
// FIXME(eddyb) this would ideally be `Lazy<[...]>` but `ty::Predicate`
252273
// doesn't handle shorthands in its own (de)serialization impls,
253274
// as it's an `enum` for which we want to derive (de)serialization,
254275
// so the `ty::codec` APIs handle the whole `&'tcx [...]` at once.
255276
// Also, as an optimization, a missing entry indicates an empty `&[]`.
256-
inferred_outlives: Lazy!(PerDefTable<Lazy!(&'tcx [(ty::Predicate<'tcx>, Span)])>),
257-
super_predicates: Lazy!(PerDefTable<Lazy!(ty::GenericPredicates<'tcx>)>),
258-
mir: Lazy!(PerDefTable<Lazy!(mir::Body<'tcx>)>),
259-
promoted_mir: Lazy!(PerDefTable<Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>),
277+
inferred_outlives: Table<DefIndex, Lazy!(&'tcx [(ty::Predicate<'tcx>, Span)])>,
278+
super_predicates: Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>,
279+
mir: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
280+
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
260281
}
261282

262283
#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]

0 commit comments

Comments
 (0)