1
1
use crate :: creader:: CrateMetadataRef ;
2
2
use decoder:: Metadata ;
3
3
use def_path_hash_map:: DefPathHashMapRef ;
4
- use table:: { Table , TableBuilder } ;
4
+ use table:: TableBuilder ;
5
5
6
6
use rustc_ast as ast;
7
7
use rustc_attr as attr;
@@ -20,8 +20,8 @@ use rustc_middle::mir;
20
20
use rustc_middle:: thir;
21
21
use rustc_middle:: ty:: fast_reject:: SimplifiedType ;
22
22
use rustc_middle:: ty:: query:: Providers ;
23
- use rustc_middle:: ty:: GeneratorDiagnosticData ;
24
23
use rustc_middle:: ty:: { self , ReprOptions , Ty } ;
24
+ use rustc_middle:: ty:: { GeneratorDiagnosticData , TyCtxt } ;
25
25
use rustc_serialize:: opaque:: Encoder ;
26
26
use rustc_session:: config:: SymbolManglingVersion ;
27
27
use rustc_session:: cstore:: { CrateDepKind , ForeignModule , LinkagePreference , NativeLib } ;
@@ -62,20 +62,6 @@ const METADATA_VERSION: u8 = 6;
62
62
/// and further followed by the rustc version string.
63
63
pub const METADATA_HEADER : & [ u8 ] = & [ b'r' , b'u' , b's' , b't' , 0 , 0 , 0 , METADATA_VERSION ] ;
64
64
65
- /// Additional metadata for a `Lazy<T>` where `T` may not be `Sized`,
66
- /// e.g. for `Lazy<[T]>`, this is the length (count of `T` values).
67
- trait LazyMeta {
68
- type Meta : Copy + ' static ;
69
- }
70
-
71
- impl < T > LazyMeta for T {
72
- type Meta = ( ) ;
73
- }
74
-
75
- impl < T > LazyMeta for [ T ] {
76
- type Meta = usize ;
77
- }
78
-
79
65
/// A value of type T referred to by its absolute position
80
66
/// in the metadata, and which can be decoded lazily.
81
67
///
@@ -91,8 +77,19 @@ impl<T> LazyMeta for [T] {
91
77
/// Distances start at 1, as 0-byte nodes are invalid.
92
78
/// Also invalid are nodes being referred in a different
93
79
/// order than they were encoded in.
94
- ///
95
- /// # Sequences (`Lazy<[T]>`)
80
+ #[ must_use]
81
+ struct LazyValue < T > {
82
+ position : NonZeroUsize ,
83
+ _marker : PhantomData < fn ( ) -> T > ,
84
+ }
85
+
86
+ impl < T > LazyValue < T > {
87
+ fn from_position ( position : NonZeroUsize ) -> LazyValue < T > {
88
+ LazyValue { position, _marker : PhantomData }
89
+ }
90
+ }
91
+
92
+ /// A list of lazily-decoded values.
96
93
///
97
94
/// Unlike `Lazy<Vec<T>>`, the length is encoded next to the
98
95
/// position, not at the position, which means that the length
@@ -102,39 +99,58 @@ impl<T> LazyMeta for [T] {
102
99
/// the encoding is that of `Lazy`, with the distinction that
103
100
/// the minimal distance the length of the sequence, i.e.
104
101
/// it's assumed there's no 0-byte element in the sequence.
105
- #[ must_use]
106
- // FIXME(#59875) the `Meta` parameter only exists to dodge
107
- // invariance wrt `T` (coming from the `meta: T::Meta` field).
108
- struct Lazy < T , Meta = <T as LazyMeta >:: Meta >
109
- where
110
- T : ?Sized + LazyMeta < Meta = Meta > ,
111
- Meta : ' static + Copy ,
112
- {
102
+ struct LazyArray < T > {
103
+ position : NonZeroUsize ,
104
+ num_elems : usize ,
105
+ _marker : PhantomData < fn ( ) -> T > ,
106
+ }
107
+
108
+ impl < T > LazyArray < T > {
109
+ fn from_position_and_num_elems ( position : NonZeroUsize , num_elems : usize ) -> LazyArray < T > {
110
+ LazyArray { position, num_elems, _marker : PhantomData }
111
+ }
112
+
113
+ fn empty ( ) -> LazyArray < T > {
114
+ LazyArray :: from_position_and_num_elems ( NonZeroUsize :: new ( 1 ) . unwrap ( ) , 0 )
115
+ }
116
+ }
117
+
118
+ /// A list of lazily-decoded values, with the added capability of random access.
119
+ ///
120
+ /// Random-access table (i.e. offering constant-time `get`/`set`), similar to
121
+ /// `LazyArray<T>`, but without requiring encoding or decoding all the values
122
+ /// eagerly and in-order.
123
+ struct LazyTable < I , T > {
113
124
position : NonZeroUsize ,
114
- meta : Meta ,
115
- _marker : PhantomData < T > ,
125
+ encoded_size : usize ,
126
+ _marker : PhantomData < fn ( I ) -> T > ,
116
127
}
117
128
118
- impl < T : ?Sized + LazyMeta > Lazy < T > {
119
- fn from_position_and_meta ( position : NonZeroUsize , meta : T :: Meta ) -> Lazy < T > {
120
- Lazy { position, meta, _marker : PhantomData }
129
+ impl < I , T > LazyTable < I , T > {
130
+ fn from_position_and_encoded_size (
131
+ position : NonZeroUsize ,
132
+ encoded_size : usize ,
133
+ ) -> LazyTable < I , T > {
134
+ LazyTable { position, encoded_size, _marker : PhantomData }
121
135
}
122
136
}
123
137
124
- impl < T > Lazy < T > {
125
- fn from_position ( position : NonZeroUsize ) -> Lazy < T > {
126
- Lazy :: from_position_and_meta ( position, ( ) )
138
+ impl < T > Copy for LazyValue < T > { }
139
+ impl < T > Clone for LazyValue < T > {
140
+ fn clone ( & self ) -> Self {
141
+ * self
127
142
}
128
143
}
129
144
130
- impl < T > Lazy < [ T ] > {
131
- fn empty ( ) -> Lazy < [ T ] > {
132
- Lazy :: from_position_and_meta ( NonZeroUsize :: new ( 1 ) . unwrap ( ) , 0 )
145
+ impl < T > Copy for LazyArray < T > { }
146
+ impl < T > Clone for LazyArray < T > {
147
+ fn clone ( & self ) -> Self {
148
+ * self
133
149
}
134
150
}
135
151
136
- impl < T : ? Sized + LazyMeta > Copy for Lazy < T > { }
137
- impl < T : ? Sized + LazyMeta > Clone for Lazy < T > {
152
+ impl < I , T > Copy for LazyTable < I , T > { }
153
+ impl < I , T > Clone for LazyTable < I , T > {
138
154
fn clone ( & self ) -> Self {
139
155
* self
140
156
}
@@ -155,29 +171,20 @@ enum LazyState {
155
171
Previous ( NonZeroUsize ) ,
156
172
}
157
173
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
- ( Table <$I: ty, $T: ty>) => { Lazy <Table <$I, $T>, usize >} ;
163
- ( [ $T: ty] ) => { Lazy <[ $T] , usize >} ;
164
- ( $T: ty) => { Lazy <$T, ( ) >} ;
165
- }
166
-
167
- type SyntaxContextTable = Lazy < Table < u32 , Lazy < SyntaxContextData > > > ;
168
- type ExpnDataTable = Lazy < Table < ExpnIndex , Lazy < ExpnData > > > ;
169
- type ExpnHashTable = Lazy < Table < ExpnIndex , Lazy < ExpnHash > > > ;
174
+ type SyntaxContextTable = LazyTable < u32 , LazyValue < SyntaxContextData > > ;
175
+ type ExpnDataTable = LazyTable < ExpnIndex , LazyValue < ExpnData > > ;
176
+ type ExpnHashTable = LazyTable < ExpnIndex , LazyValue < ExpnHash > > ;
170
177
171
178
#[ derive( MetadataEncodable , MetadataDecodable ) ]
172
179
pub ( crate ) struct ProcMacroData {
173
180
proc_macro_decls_static : DefIndex ,
174
181
stability : Option < attr:: Stability > ,
175
- macros : Lazy < [ DefIndex ] > ,
182
+ macros : LazyArray < DefIndex > ,
176
183
}
177
184
178
185
/// Serialized metadata for a crate.
179
186
/// When compiling a proc-macro crate, we encode many of
180
- /// the `Lazy<[T] >` fields as `Lazy::empty()`. This serves two purposes:
187
+ /// the `LazyArray<T >` fields as `Lazy::empty()`. This serves two purposes:
181
188
///
182
189
/// 1. We avoid performing unnecessary work. Proc-macro crates can only
183
190
/// export proc-macros functions, which are compiled into a shared library.
@@ -205,32 +212,32 @@ pub(crate) struct CrateRoot<'tcx> {
205
212
has_panic_handler : bool ,
206
213
has_default_lib_allocator : bool ,
207
214
208
- crate_deps : Lazy < [ CrateDep ] > ,
209
- dylib_dependency_formats : Lazy < [ Option < LinkagePreference > ] > ,
210
- lib_features : Lazy < [ ( Symbol , Option < Symbol > ) ] > ,
211
- lang_items : Lazy < [ ( DefIndex , usize ) ] > ,
212
- lang_items_missing : Lazy < [ lang_items:: LangItem ] > ,
213
- diagnostic_items : Lazy < [ ( Symbol , DefIndex ) ] > ,
214
- native_libraries : Lazy < [ NativeLib ] > ,
215
- foreign_modules : Lazy < [ ForeignModule ] > ,
216
- traits : Lazy < [ DefIndex ] > ,
217
- impls : Lazy < [ TraitImpls ] > ,
218
- incoherent_impls : Lazy < [ IncoherentImpls ] > ,
219
- interpret_alloc_index : Lazy < [ u32 ] > ,
215
+ crate_deps : LazyArray < CrateDep > ,
216
+ dylib_dependency_formats : LazyArray < Option < LinkagePreference > > ,
217
+ lib_features : LazyArray < ( Symbol , Option < Symbol > ) > ,
218
+ lang_items : LazyArray < ( DefIndex , usize ) > ,
219
+ lang_items_missing : LazyArray < lang_items:: LangItem > ,
220
+ diagnostic_items : LazyArray < ( Symbol , DefIndex ) > ,
221
+ native_libraries : LazyArray < NativeLib > ,
222
+ foreign_modules : LazyArray < ForeignModule > ,
223
+ traits : LazyArray < DefIndex > ,
224
+ impls : LazyArray < TraitImpls > ,
225
+ incoherent_impls : LazyArray < IncoherentImpls > ,
226
+ interpret_alloc_index : LazyArray < u32 > ,
220
227
proc_macro_data : Option < ProcMacroData > ,
221
228
222
229
tables : LazyTables < ' tcx > ,
223
- debugger_visualizers : Lazy < [ rustc_span:: DebuggerVisualizerFile ] > ,
230
+ debugger_visualizers : LazyArray < rustc_span:: DebuggerVisualizerFile > ,
224
231
225
- exported_symbols : Lazy ! ( [ ( ExportedSymbol <' tcx>, SymbolExportInfo ) ] ) ,
232
+ exported_symbols : LazyArray < ( ExportedSymbol < ' tcx > , SymbolExportInfo ) > ,
226
233
227
234
syntax_contexts : SyntaxContextTable ,
228
235
expn_data : ExpnDataTable ,
229
236
expn_hashes : ExpnHashTable ,
230
237
231
- def_path_hash_map : Lazy < DefPathHashMapRef < ' tcx > > ,
238
+ def_path_hash_map : LazyValue < DefPathHashMapRef < ' tcx > > ,
232
239
233
- source_map : Lazy < [ rustc_span:: SourceFile ] > ,
240
+ source_map : LazyArray < rustc_span:: SourceFile > ,
234
241
235
242
compiler_builtins : bool ,
236
243
needs_allocator : bool ,
@@ -257,7 +264,12 @@ impl Into<RawDefId> for DefId {
257
264
}
258
265
259
266
impl RawDefId {
260
- fn decode ( self , cdata : CrateMetadataRef < ' _ > ) -> DefId {
267
+ /// This exists so that `provide_one!` is happy
268
+ fn decode ( self , meta : ( CrateMetadataRef < ' _ > , TyCtxt < ' _ > ) ) -> DefId {
269
+ self . decode_from_cdata ( meta. 0 )
270
+ }
271
+
272
+ fn decode_from_cdata ( self , cdata : CrateMetadataRef < ' _ > ) -> DefId {
261
273
let krate = CrateNum :: from_u32 ( self . krate ) ;
262
274
let krate = cdata. map_encoded_cnum_to_current ( krate) ;
263
275
DefId { krate, index : DefIndex :: from_u32 ( self . index ) }
@@ -276,21 +288,21 @@ pub(crate) struct CrateDep {
276
288
#[ derive( MetadataEncodable , MetadataDecodable ) ]
277
289
pub ( crate ) struct TraitImpls {
278
290
trait_id : ( u32 , DefIndex ) ,
279
- impls : Lazy < [ ( DefIndex , Option < SimplifiedType > ) ] > ,
291
+ impls : LazyArray < ( DefIndex , Option < SimplifiedType > ) > ,
280
292
}
281
293
282
294
#[ derive( MetadataEncodable , MetadataDecodable ) ]
283
295
pub ( crate ) struct IncoherentImpls {
284
296
self_ty : SimplifiedType ,
285
- impls : Lazy < [ DefIndex ] > ,
297
+ impls : LazyArray < DefIndex > ,
286
298
}
287
299
288
300
/// Define `LazyTables` and `TableBuilders` at the same time.
289
301
macro_rules! define_tables {
290
302
( $( $name: ident: Table <$IDX: ty, $T: ty>) ,+ $( , ) ?) => {
291
303
#[ derive( MetadataEncodable , MetadataDecodable ) ]
292
304
pub ( crate ) struct LazyTables <' tcx> {
293
- $( $name: Lazy ! ( Table <$IDX, $T>) ) ,+
305
+ $( $name: LazyTable <$IDX, $T>) ,+
294
306
}
295
307
296
308
#[ derive( Default ) ]
@@ -309,61 +321,62 @@ macro_rules! define_tables {
309
321
}
310
322
311
323
define_tables ! {
312
- kind: Table <DefIndex , Lazy <EntryKind >>,
313
- attributes: Table <DefIndex , Lazy < [ ast:: Attribute ] >>,
314
- children: Table <DefIndex , Lazy < [ DefIndex ] >>,
324
+ kind: Table <DefIndex , LazyValue <EntryKind >>,
325
+ attributes: Table <DefIndex , LazyArray < ast:: Attribute >>,
326
+ children: Table <DefIndex , LazyArray < DefIndex >>,
315
327
316
328
opt_def_kind: Table <DefIndex , DefKind >,
317
- visibility: Table <DefIndex , Lazy <ty:: Visibility >>,
318
- def_span: Table <DefIndex , Lazy <Span >>,
319
- def_ident_span: Table <DefIndex , Lazy <Span >>,
320
- lookup_stability: Table <DefIndex , Lazy <attr:: Stability >>,
321
- lookup_const_stability: Table <DefIndex , Lazy <attr:: ConstStability >>,
322
- lookup_deprecation_entry: Table <DefIndex , Lazy <attr:: Deprecation >>,
329
+ visibility: Table <DefIndex , LazyValue <ty:: Visibility >>,
330
+ def_span: Table <DefIndex , LazyValue <Span >>,
331
+ def_ident_span: Table <DefIndex , LazyValue <Span >>,
332
+ lookup_stability: Table <DefIndex , LazyValue <attr:: Stability >>,
333
+ lookup_const_stability: Table <DefIndex , LazyValue <attr:: ConstStability >>,
334
+ lookup_deprecation_entry: Table <DefIndex , LazyValue <attr:: Deprecation >>,
323
335
// As an optimization, a missing entry indicates an empty `&[]`.
324
- explicit_item_bounds: Table <DefIndex , Lazy ! ( [ ( ty:: Predicate <' tcx>, Span ) ] ) >,
325
- explicit_predicates_of: Table <DefIndex , Lazy ! ( ty:: GenericPredicates <' tcx>) >,
326
- generics_of: Table <DefIndex , Lazy <ty:: Generics >>,
336
+ explicit_item_bounds: Table <DefIndex , LazyArray < ( ty:: Predicate <' tcx>, Span ) > >,
337
+ explicit_predicates_of: Table <DefIndex , LazyValue < ty:: GenericPredicates <' tcx>> >,
338
+ generics_of: Table <DefIndex , LazyValue <ty:: Generics >>,
327
339
// As an optimization, a missing entry indicates an empty `&[]`.
328
- inferred_outlives_of: Table <DefIndex , Lazy !( [ ( ty:: Predicate <' tcx>, Span ) ] ) >,
329
- super_predicates_of: Table <DefIndex , Lazy !( ty:: GenericPredicates <' tcx>) >,
330
- type_of: Table <DefIndex , Lazy !( Ty <' tcx>) >,
331
- variances_of: Table <DefIndex , Lazy <[ ty:: Variance ] >>,
332
- fn_sig: Table <DefIndex , Lazy !( ty:: PolyFnSig <' tcx>) >,
333
- codegen_fn_attrs: Table <DefIndex , Lazy !( CodegenFnAttrs ) >,
334
- impl_trait_ref: Table <DefIndex , Lazy !( ty:: TraitRef <' tcx>) >,
335
- const_param_default: Table <DefIndex , Lazy <rustc_middle:: ty:: Const <' tcx>>>,
336
- optimized_mir: Table <DefIndex , Lazy !( mir:: Body <' tcx>) >,
337
- mir_for_ctfe: Table <DefIndex , Lazy !( mir:: Body <' tcx>) >,
338
- promoted_mir: Table <DefIndex , Lazy !( IndexVec <mir:: Promoted , mir:: Body <' tcx>>) >,
339
- thir_abstract_const: Table <DefIndex , Lazy !( & ' tcx [ thir:: abstract_const:: Node <' tcx>] ) >,
340
+ inferred_outlives_of: Table <DefIndex , LazyArray <( ty:: Predicate <' tcx>, Span ) >>,
341
+ super_predicates_of: Table <DefIndex , LazyValue <ty:: GenericPredicates <' tcx>>>,
342
+ type_of: Table <DefIndex , LazyValue <Ty <' tcx>>>,
343
+ variances_of: Table <DefIndex , LazyArray <ty:: Variance >>,
344
+ fn_sig: Table <DefIndex , LazyValue <ty:: PolyFnSig <' tcx>>>,
345
+ codegen_fn_attrs: Table <DefIndex , LazyValue <CodegenFnAttrs >>,
346
+ impl_trait_ref: Table <DefIndex , LazyValue <ty:: TraitRef <' tcx>>>,
347
+ const_param_default: Table <DefIndex , LazyValue <rustc_middle:: ty:: Const <' tcx>>>,
348
+ optimized_mir: Table <DefIndex , LazyValue <mir:: Body <' tcx>>>,
349
+ mir_for_ctfe: Table <DefIndex , LazyValue <mir:: Body <' tcx>>>,
350
+ promoted_mir: Table <DefIndex , LazyValue <IndexVec <mir:: Promoted , mir:: Body <' tcx>>>>,
351
+ // FIXME(compiler-errors): Why isn't this a LazyArray?
352
+ thir_abstract_const: Table <DefIndex , LazyValue <& ' tcx [ thir:: abstract_const:: Node <' tcx>] >>,
340
353
impl_parent: Table <DefIndex , RawDefId >,
341
354
impl_polarity: Table <DefIndex , ty:: ImplPolarity >,
342
355
impl_constness: Table <DefIndex , hir:: Constness >,
343
356
is_intrinsic: Table <DefIndex , ( ) >,
344
357
impl_defaultness: Table <DefIndex , hir:: Defaultness >,
345
358
// FIXME(eddyb) perhaps compute this on the fly if cheap enough?
346
- coerce_unsized_info: Table <DefIndex , Lazy ! ( ty:: adjustment:: CoerceUnsizedInfo ) >,
347
- mir_const_qualif: Table <DefIndex , Lazy ! ( mir:: ConstQualifs ) >,
348
- rendered_const: Table <DefIndex , Lazy ! ( String ) >,
359
+ coerce_unsized_info: Table <DefIndex , LazyValue < ty:: adjustment:: CoerceUnsizedInfo > >,
360
+ mir_const_qualif: Table <DefIndex , LazyValue < mir:: ConstQualifs > >,
361
+ rendered_const: Table <DefIndex , LazyValue < String > >,
349
362
asyncness: Table <DefIndex , hir:: IsAsync >,
350
- fn_arg_names: Table <DefIndex , Lazy ! ( [ Ident ] ) >,
351
- generator_kind: Table <DefIndex , Lazy ! ( hir:: GeneratorKind ) >,
352
- trait_def: Table <DefIndex , Lazy ! ( ty:: TraitDef ) >,
363
+ fn_arg_names: Table <DefIndex , LazyArray < Ident > >,
364
+ generator_kind: Table <DefIndex , LazyValue < hir:: GeneratorKind > >,
365
+ trait_def: Table <DefIndex , LazyValue < ty:: TraitDef > >,
353
366
354
367
trait_item_def_id: Table <DefIndex , RawDefId >,
355
- inherent_impls: Table <DefIndex , Lazy < [ DefIndex ] >>,
356
- expn_that_defined: Table <DefIndex , Lazy <ExpnId >>,
357
- unused_generic_params: Table <DefIndex , Lazy <FiniteBitSet <u32 >>>,
358
- repr_options: Table <DefIndex , Lazy <ReprOptions >>,
368
+ inherent_impls: Table <DefIndex , LazyArray < DefIndex >>,
369
+ expn_that_defined: Table <DefIndex , LazyValue <ExpnId >>,
370
+ unused_generic_params: Table <DefIndex , LazyValue <FiniteBitSet <u32 >>>,
371
+ repr_options: Table <DefIndex , LazyValue <ReprOptions >>,
359
372
// `def_keys` and `def_path_hashes` represent a lazy version of a
360
373
// `DefPathTable`. This allows us to avoid deserializing an entire
361
374
// `DefPathTable` up front, since we may only ever use a few
362
375
// definitions from any given crate.
363
- def_keys: Table <DefIndex , Lazy <DefKey >>,
376
+ def_keys: Table <DefIndex , LazyValue <DefKey >>,
364
377
def_path_hashes: Table <DefIndex , DefPathHash >,
365
- proc_macro_quoted_spans: Table <usize , Lazy <Span >>,
366
- generator_diagnostic_data: Table <DefIndex , Lazy <GeneratorDiagnosticData <' tcx>>>,
378
+ proc_macro_quoted_spans: Table <usize , LazyValue <Span >>,
379
+ generator_diagnostic_data: Table <DefIndex , LazyValue <GeneratorDiagnosticData <' tcx>>>,
367
380
may_have_doc_links: Table <DefIndex , ( ) >,
368
381
}
369
382
@@ -382,19 +395,19 @@ enum EntryKind {
382
395
OpaqueTy ,
383
396
Enum ,
384
397
Field ,
385
- Variant ( Lazy < VariantData > ) ,
386
- Struct ( Lazy < VariantData > ) ,
387
- Union ( Lazy < VariantData > ) ,
398
+ Variant ( LazyValue < VariantData > ) ,
399
+ Struct ( LazyValue < VariantData > ) ,
400
+ Union ( LazyValue < VariantData > ) ,
388
401
Fn ,
389
402
ForeignFn ,
390
- Mod ( Lazy < [ ModChild ] > ) ,
391
- MacroDef ( Lazy < ast:: MacArgs > , /*macro_rules*/ bool ) ,
403
+ Mod ( LazyArray < ModChild > ) ,
404
+ MacroDef ( LazyValue < ast:: MacArgs > , /*macro_rules*/ bool ) ,
392
405
ProcMacro ( MacroKind ) ,
393
406
Closure ,
394
407
Generator ,
395
408
Trait ,
396
409
Impl ,
397
- AssocFn ( Lazy < AssocFnData > ) ,
410
+ AssocFn ( LazyValue < AssocFnData > ) ,
398
411
AssocType ( AssocContainer ) ,
399
412
AssocConst ( AssocContainer ) ,
400
413
TraitAlias ,
0 commit comments