Skip to content

Commit 7d32533

Browse files
author
Ariel Ben-Yehuda
committed
add documentation
1 parent 0153001 commit 7d32533

File tree

1 file changed

+57
-30
lines changed

1 file changed

+57
-30
lines changed

src/librustc/middle/ty.rs

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,40 +3192,56 @@ pub type ADTDef<'tcx> = ADTDef_<'tcx, 'static>;
31923192
pub type VariantDef<'tcx> = VariantDef_<'tcx, 'static>;
31933193
pub type FieldDef<'tcx> = FieldDef_<'tcx, 'static>;
31943194

3195-
pub struct VariantDef_<'tcx, 'lt: 'tcx> {
3195+
pub struct VariantDef_<'tcx, 'container: 'tcx> {
31963196
pub did: DefId,
31973197
pub name: Name, // struct's name if this is a struct
31983198
pub disr_val: Disr,
3199-
pub fields: Vec<FieldDef_<'tcx, 'lt>>
3199+
pub fields: Vec<FieldDef_<'tcx, 'container>>
32003200
}
32013201

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.
32033205
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
32063208
pub name: Name,
32073209
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>
32103213
}
32113214

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> {
32153231
pub did: DefId,
3216-
pub variants: Vec<VariantDef_<'tcx, 'lt>>,
3232+
pub variants: Vec<VariantDef_<'tcx, 'container>>,
32173233
flags: Cell<ADTFlags>,
32183234
}
32193235

3220-
impl<'tcx, 'lt> PartialEq for ADTDef_<'tcx, 'lt> {
3236+
impl<'tcx, 'container> PartialEq for ADTDef_<'tcx, 'container> {
32213237
// ADTDef are always interned and this is part of TyS equality
32223238
#[inline]
32233239
fn eq(&self, other: &Self) -> bool { self as *const _ == other as *const _ }
32243240
}
32253241

3226-
impl<'tcx, 'lt> Eq for ADTDef_<'tcx, 'lt> {}
3242+
impl<'tcx, 'container> Eq for ADTDef_<'tcx, 'container> {}
32273243

3228-
impl<'tcx, 'lt> Hash for ADTDef_<'tcx, 'lt> {
3244+
impl<'tcx, 'container> Hash for ADTDef_<'tcx, 'container> {
32293245
#[inline]
32303246
fn hash<H: Hasher>(&self, s: &mut H) {
32313247
(self as *const ADTDef).hash(s)
@@ -3239,11 +3255,11 @@ pub enum ADTKind { Struct, Enum }
32393255
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
32403256
pub enum VariantKind { Dict, Tuple, Unit }
32413257

3242-
impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
3258+
impl<'tcx, 'container> ADTDef_<'tcx, 'container> {
32433259
fn new(tcx: &ctxt<'tcx>,
32443260
did: DefId,
32453261
kind: ADTKind,
3246-
variants: Vec<VariantDef_<'tcx, 'lt>>) -> Self {
3262+
variants: Vec<VariantDef_<'tcx, 'container>>) -> Self {
32473263
let mut flags = ADTFlags::NO_ADT_FLAGS;
32483264
let attrs = tcx.get_attrs(did);
32493265
if attrs.iter().any(|item| item.check_name("fundamental")) {
@@ -3272,6 +3288,7 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
32723288
self.flags.set(self.flags.get() | ADTFlags::IS_DTORCK_VALID)
32733289
}
32743290

3291+
/// Returns the kind of the ADT - Struct or Enum.
32753292
#[inline]
32763293
pub fn adt_kind(&self) -> ADTKind {
32773294
if self.flags.get().intersects(ADTFlags::IS_ENUM) {
@@ -3281,6 +3298,9 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
32813298
}
32823299
}
32833300

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.
32843304
#[inline]
32853305
pub fn is_dtorck(&'tcx self, tcx: &ctxt<'tcx>) -> bool {
32863306
if !self.flags.get().intersects(ADTFlags::IS_DTORCK_VALID) {
@@ -3289,6 +3309,8 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
32893309
self.flags.get().intersects(ADTFlags::IS_DTORCK)
32903310
}
32913311

3312+
/// Returns whether this type is #[fundamental] for the purposes
3313+
/// of coherence checking.
32923314
#[inline]
32933315
pub fn is_fundamental(&self) -> bool {
32943316
self.flags.get().intersects(ADTFlags::IS_FUNDAMENTAL)
@@ -3299,17 +3321,20 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
32993321
self.flags.get().intersects(ADTFlags::IS_SIMD)
33003322
}
33013323

3324+
/// Returns true if this is PhantomData<T>.
33023325
#[inline]
33033326
pub fn is_phantom_data(&self) -> bool {
33043327
self.flags.get().intersects(ADTFlags::IS_PHANTOM_DATA)
33053328
}
33063329

3307-
#[inline(never)]
3330+
/// Returns whether this type has a destructor.
33083331
pub fn has_dtor(&self, tcx: &ctxt<'tcx>) -> bool {
33093332
tcx.destructor_for_type.borrow().contains_key(&self.did)
33103333
}
33113334

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> {
33133338
assert!(self.adt_kind() == ADTKind::Struct);
33143339
&self.variants[0]
33153340
}
@@ -3324,13 +3349,15 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
33243349
tcx.lookup_predicates(self.did)
33253350
}
33263351

3352+
/// Returns an iterator over all fields contained
3353+
/// by this ADT.
33273354
#[inline]
33283355
pub fn all_fields(&self) ->
33293356
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>>
33343361
> {
33353362
self.variants.iter().flat_map(VariantDef_::fields_iter)
33363363
}
@@ -3350,14 +3377,14 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
33503377
self.variants.iter().all(|v| v.fields.is_empty())
33513378
}
33523379

3353-
pub fn variant_with_id(&self, vid: DefId) -> &VariantDef_<'tcx, 'lt> {
3380+
pub fn variant_with_id(&self, vid: DefId) -> &VariantDef_<'tcx, 'container> {
33543381
self.variants
33553382
.iter()
33563383
.find(|v| v.did == vid)
33573384
.expect("variant_with_id: unknown variant")
33583385
}
33593386

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> {
33613388
match def {
33623389
def::DefVariant(_, vid, _) => self.variant_with_id(vid),
33633390
def::DefStruct(..) | def::DefTy(..) => self.struct_variant(),
@@ -3366,9 +3393,9 @@ impl<'tcx, 'lt> ADTDef_<'tcx, 'lt> {
33663393
}
33673394
}
33683395

3369-
impl<'tcx, 'lt> VariantDef_<'tcx, 'lt> {
3396+
impl<'tcx, 'container> VariantDef_<'tcx, 'container> {
33703397
#[inline]
3371-
fn fields_iter(&self) -> slice::Iter<FieldDef_<'tcx, 'lt>> {
3398+
fn fields_iter(&self) -> slice::Iter<FieldDef_<'tcx, 'container>> {
33723399
self.fields.iter()
33733400
}
33743401

@@ -3387,17 +3414,17 @@ impl<'tcx, 'lt> VariantDef_<'tcx, 'lt> {
33873414
}
33883415

33893416
#[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>> {
33913418
self.fields.iter().find(|f| f.name == name)
33923419
}
33933420

33943421
#[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> {
33963423
self.find_field_named(name).unwrap()
33973424
}
33983425
}
33993426

3400-
impl<'tcx, 'lt> FieldDef_<'tcx, 'lt> {
3427+
impl<'tcx, 'container> FieldDef_<'tcx, 'container> {
34013428
pub fn new(did: DefId,
34023429
name: Name,
34033430
vis: ast::Visibility) -> Self {
@@ -3417,7 +3444,7 @@ impl<'tcx, 'lt> FieldDef_<'tcx, 'lt> {
34173444
self.ty.unwrap()
34183445
}
34193446

3420-
pub fn fulfill_ty(&self, ty: Ty<'lt>) {
3447+
pub fn fulfill_ty(&self, ty: Ty<'container>) {
34213448
self.ty.fulfill(ty);
34223449
}
34233450
}

0 commit comments

Comments
 (0)