@@ -3192,40 +3192,56 @@ pub type ADTDef<'tcx> = ADTDef_<'tcx, 'static>;
3192
3192
pub type VariantDef < ' tcx > = VariantDef_ < ' tcx , ' static > ;
3193
3193
pub type FieldDef < ' tcx > = FieldDef_ < ' tcx , ' static > ;
3194
3194
3195
- pub struct VariantDef_ < ' tcx , ' lt : ' tcx > {
3195
+ pub struct VariantDef_ < ' tcx , ' container : ' tcx > {
3196
3196
pub did : DefId ,
3197
3197
pub name : Name , // struct's name if this is a struct
3198
3198
pub disr_val : Disr ,
3199
- pub fields : Vec < FieldDef_ < ' tcx , ' lt > >
3199
+ pub fields : Vec < FieldDef_ < ' tcx , ' container > >
3200
3200
}
3201
3201
3202
- pub struct FieldDef_ < ' tcx , ' lt : ' tcx > {
3202
+ pub struct FieldDef_ < ' tcx , ' container : ' tcx > {
3203
+ /// The field's DefId. NOTE: the fields of tuple-like enum variants
3204
+ /// are not real items, and don't have entries in tcache etc.
3203
3205
pub did : DefId ,
3204
- // special_idents::unnamed_field.name
3205
- // if this is a tuple-like field
3206
+ /// special_idents::unnamed_field.name
3207
+ /// if this is a tuple-like field
3206
3208
pub name : Name ,
3207
3209
pub vis : ast:: Visibility ,
3208
- // TyIVar is used here to allow for
3209
- ty : TyIVar < ' tcx , ' lt >
3210
+ /// TyIVar is used here to allow for variance (see the doc at
3211
+ /// ADTDef_).
3212
+ ty : TyIVar < ' tcx , ' container >
3210
3213
}
3211
3214
3212
- /// The definition of an abstract data type - a struct or enum. 'lt
3213
- /// is here so 'tcx can be variant.
3214
- pub struct ADTDef_ < ' tcx , ' lt : ' tcx > {
3215
+ /// The definition of an abstract data type - a struct or enum.
3216
+ ///
3217
+ /// These are all interned (by intern_adt_def) into the adt_defs
3218
+ /// table.
3219
+ ///
3220
+ /// Because of the possibility of nested tcx-s, this type
3221
+ /// needs 2 lifetimes: the traditional variant lifetime ('tcx)
3222
+ /// bounding the lifetime of the inner types is of course necessary.
3223
+ /// However, it is not sufficient - types from a child tcx must
3224
+ /// not be leaked into the master tcx by being stored in an ADTDef_.
3225
+ ///
3226
+ /// The 'container lifetime ensures that by outliving the container
3227
+ /// tcx and preventing shorter-lived types from being inserted. When
3228
+ /// write access is not needed, the 'container lifetime can be
3229
+ /// erased to 'static, which can be done by the ADTDef wrapper.
3230
+ pub struct ADTDef_ < ' tcx , ' container : ' tcx > {
3215
3231
pub did : DefId ,
3216
- pub variants : Vec < VariantDef_ < ' tcx , ' lt > > ,
3232
+ pub variants : Vec < VariantDef_ < ' tcx , ' container > > ,
3217
3233
flags : Cell < ADTFlags > ,
3218
3234
}
3219
3235
3220
- impl < ' tcx , ' lt > PartialEq for ADTDef_ < ' tcx , ' lt > {
3236
+ impl < ' tcx , ' container > PartialEq for ADTDef_ < ' tcx , ' container > {
3221
3237
// ADTDef are always interned and this is part of TyS equality
3222
3238
#[ inline]
3223
3239
fn eq ( & self , other : & Self ) -> bool { self as * const _ == other as * const _ }
3224
3240
}
3225
3241
3226
- impl < ' tcx , ' lt > Eq for ADTDef_ < ' tcx , ' lt > { }
3242
+ impl < ' tcx , ' container > Eq for ADTDef_ < ' tcx , ' container > { }
3227
3243
3228
- impl < ' tcx , ' lt > Hash for ADTDef_ < ' tcx , ' lt > {
3244
+ impl < ' tcx , ' container > Hash for ADTDef_ < ' tcx , ' container > {
3229
3245
#[ inline]
3230
3246
fn hash < H : Hasher > ( & self , s : & mut H ) {
3231
3247
( self as * const ADTDef ) . hash ( s)
@@ -3239,11 +3255,11 @@ pub enum ADTKind { Struct, Enum }
3239
3255
#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
3240
3256
pub enum VariantKind { Dict , Tuple , Unit }
3241
3257
3242
- impl < ' tcx , ' lt > ADTDef_ < ' tcx , ' lt > {
3258
+ impl < ' tcx , ' container > ADTDef_ < ' tcx , ' container > {
3243
3259
fn new ( tcx : & ctxt < ' tcx > ,
3244
3260
did : DefId ,
3245
3261
kind : ADTKind ,
3246
- variants : Vec < VariantDef_ < ' tcx , ' lt > > ) -> Self {
3262
+ variants : Vec < VariantDef_ < ' tcx , ' container > > ) -> Self {
3247
3263
let mut flags = ADTFlags :: NO_ADT_FLAGS ;
3248
3264
let attrs = tcx. get_attrs ( did) ;
3249
3265
if attrs. iter ( ) . any ( |item| item. check_name ( "fundamental" ) ) {
@@ -3272,6 +3288,7 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
3272
3288
self . flags . set ( self . flags . get ( ) | ADTFlags :: IS_DTORCK_VALID )
3273
3289
}
3274
3290
3291
+ /// Returns the kind of the ADT - Struct or Enum.
3275
3292
#[ inline]
3276
3293
pub fn adt_kind ( & self ) -> ADTKind {
3277
3294
if self . flags . get ( ) . intersects ( ADTFlags :: IS_ENUM ) {
@@ -3281,6 +3298,9 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
3281
3298
}
3282
3299
}
3283
3300
3301
+ /// Returns whether this is a dtorck type. If this returns
3302
+ /// true, this type being safe for destruction requires it to be
3303
+ /// alive; Otherwise, only the contents are required to be.
3284
3304
#[ inline]
3285
3305
pub fn is_dtorck ( & ' tcx self , tcx : & ctxt < ' tcx > ) -> bool {
3286
3306
if !self . flags . get ( ) . intersects ( ADTFlags :: IS_DTORCK_VALID ) {
@@ -3289,6 +3309,8 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
3289
3309
self . flags . get ( ) . intersects ( ADTFlags :: IS_DTORCK )
3290
3310
}
3291
3311
3312
+ /// Returns whether this type is #[fundamental] for the purposes
3313
+ /// of coherence checking.
3292
3314
#[ inline]
3293
3315
pub fn is_fundamental ( & self ) -> bool {
3294
3316
self . flags . get ( ) . intersects ( ADTFlags :: IS_FUNDAMENTAL )
@@ -3299,17 +3321,20 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
3299
3321
self . flags . get ( ) . intersects ( ADTFlags :: IS_SIMD )
3300
3322
}
3301
3323
3324
+ /// Returns true if this is PhantomData<T>.
3302
3325
#[ inline]
3303
3326
pub fn is_phantom_data ( & self ) -> bool {
3304
3327
self . flags . get ( ) . intersects ( ADTFlags :: IS_PHANTOM_DATA )
3305
3328
}
3306
3329
3307
- # [ inline ( never ) ]
3330
+ /// Returns whether this type has a destructor.
3308
3331
pub fn has_dtor ( & self , tcx : & ctxt < ' tcx > ) -> bool {
3309
3332
tcx. destructor_for_type . borrow ( ) . contains_key ( & self . did )
3310
3333
}
3311
3334
3312
- pub fn struct_variant ( & self ) -> & ty:: VariantDef_ < ' tcx , ' lt > {
3335
+ /// Asserts this is a struct and returns the struct's unique
3336
+ /// variant.
3337
+ pub fn struct_variant ( & self ) -> & ty:: VariantDef_ < ' tcx , ' container > {
3313
3338
assert ! ( self . adt_kind( ) == ADTKind :: Struct ) ;
3314
3339
& self . variants [ 0 ]
3315
3340
}
@@ -3324,13 +3349,15 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
3324
3349
tcx. lookup_predicates ( self . did )
3325
3350
}
3326
3351
3352
+ /// Returns an iterator over all fields contained
3353
+ /// by this ADT.
3327
3354
#[ inline]
3328
3355
pub fn all_fields ( & self ) ->
3329
3356
iter:: FlatMap <
3330
- slice:: Iter < VariantDef_ < ' tcx , ' lt > > ,
3331
- slice:: Iter < FieldDef_ < ' tcx , ' lt > > ,
3332
- for <' s > fn ( & ' s VariantDef_ < ' tcx , ' lt > )
3333
- -> slice:: Iter < ' s , FieldDef_ < ' tcx , ' lt > >
3357
+ slice:: Iter < VariantDef_ < ' tcx , ' container > > ,
3358
+ slice:: Iter < FieldDef_ < ' tcx , ' container > > ,
3359
+ for <' s > fn ( & ' s VariantDef_ < ' tcx , ' container > )
3360
+ -> slice:: Iter < ' s , FieldDef_ < ' tcx , ' container > >
3334
3361
> {
3335
3362
self . variants . iter ( ) . flat_map ( VariantDef_ :: fields_iter)
3336
3363
}
@@ -3350,14 +3377,14 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
3350
3377
self . variants . iter ( ) . all ( |v| v. fields . is_empty ( ) )
3351
3378
}
3352
3379
3353
- pub fn variant_with_id ( & self , vid : DefId ) -> & VariantDef_ < ' tcx , ' lt > {
3380
+ pub fn variant_with_id ( & self , vid : DefId ) -> & VariantDef_ < ' tcx , ' container > {
3354
3381
self . variants
3355
3382
. iter ( )
3356
3383
. find ( |v| v. did == vid)
3357
3384
. expect ( "variant_with_id: unknown variant" )
3358
3385
}
3359
3386
3360
- pub fn variant_of_def ( & self , def : def:: Def ) -> & VariantDef_ < ' tcx , ' lt > {
3387
+ pub fn variant_of_def ( & self , def : def:: Def ) -> & VariantDef_ < ' tcx , ' container > {
3361
3388
match def {
3362
3389
def:: DefVariant ( _, vid, _) => self . variant_with_id ( vid) ,
3363
3390
def:: DefStruct ( ..) | def:: DefTy ( ..) => self . struct_variant ( ) ,
@@ -3366,9 +3393,9 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
3366
3393
}
3367
3394
}
3368
3395
3369
- impl < ' tcx , ' lt > VariantDef_ < ' tcx , ' lt > {
3396
+ impl < ' tcx , ' container > VariantDef_ < ' tcx , ' container > {
3370
3397
#[ inline]
3371
- fn fields_iter ( & self ) -> slice:: Iter < FieldDef_ < ' tcx , ' lt > > {
3398
+ fn fields_iter ( & self ) -> slice:: Iter < FieldDef_ < ' tcx , ' container > > {
3372
3399
self . fields . iter ( )
3373
3400
}
3374
3401
@@ -3387,17 +3414,17 @@ impl<'tcx, 'lt> VariantDef_<'tcx, 'lt> {
3387
3414
}
3388
3415
3389
3416
#[ inline]
3390
- pub fn find_field_named ( & self , name : ast:: Name ) -> Option < & FieldDef_ < ' tcx , ' lt > > {
3417
+ pub fn find_field_named ( & self , name : ast:: Name ) -> Option < & FieldDef_ < ' tcx , ' container > > {
3391
3418
self . fields . iter ( ) . find ( |f| f. name == name)
3392
3419
}
3393
3420
3394
3421
#[ inline]
3395
- pub fn field_named ( & self , name : ast:: Name ) -> & FieldDef_ < ' tcx , ' lt > {
3422
+ pub fn field_named ( & self , name : ast:: Name ) -> & FieldDef_ < ' tcx , ' container > {
3396
3423
self . find_field_named ( name) . unwrap ( )
3397
3424
}
3398
3425
}
3399
3426
3400
- impl < ' tcx , ' lt > FieldDef_ < ' tcx , ' lt > {
3427
+ impl < ' tcx , ' container > FieldDef_ < ' tcx , ' container > {
3401
3428
pub fn new ( did : DefId ,
3402
3429
name : Name ,
3403
3430
vis : ast:: Visibility ) -> Self {
@@ -3417,7 +3444,7 @@ impl<'tcx, 'lt> FieldDef_<'tcx, 'lt> {
3417
3444
self . ty . unwrap ( )
3418
3445
}
3419
3446
3420
- pub fn fulfill_ty ( & self , ty : Ty < ' lt > ) {
3447
+ pub fn fulfill_ty ( & self , ty : Ty < ' container > ) {
3421
3448
self . ty . fulfill ( ty) ;
3422
3449
}
3423
3450
}
0 commit comments