Skip to content

Commit 391a7eb

Browse files
committed
add comment to <List<ExistentialPredicates>>::principal
1 parent 64857de commit 391a7eb

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/librustc/ty/sty.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,14 +569,38 @@ impl<'a, 'gcx, 'tcx> Binder<ExistentialPredicate<'tcx>> {
569569
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx List<ExistentialPredicate<'tcx>> {}
570570

571571
impl<'tcx> List<ExistentialPredicate<'tcx>> {
572+
/// Returns the "principal def id" of this set of existential predicates.
573+
///
574+
/// A Rust trait object type consists (in addition to a lifetime bound)
575+
/// of a set of trait bounds, which are separated into any number
576+
/// of auto-trait bounds, and at most 1 non-auto-trait bound. The
577+
/// non-auto-trait bound is called the "principal" of the trait
578+
/// object.
579+
///
580+
/// Only the principal can have methods or type parameters (because
581+
/// auto traits can have neither of them). This is important, because
582+
/// it means the auto traits can be treated as an unordered set (methods
583+
/// would force an order for the vtable, while relating traits with
584+
/// type parameters without knowing the order to relate them in is
585+
/// a rather non-trivial task).
586+
///
587+
/// For example, in the trait object `dyn fmt::Debug + Sync`, the
588+
/// principal bound is `Some(fmt::Debug)`, while the auto-trait bounds
589+
/// are the set `{Sync}`.
590+
///
591+
/// It is also possible to have a "trivial" trait object that
592+
/// consists only of auto traits, with no principal - for example,
593+
/// `dyn Send + Sync`. In that case, the set of auto-trait bounds
594+
/// is `{Send, Sync}`, while there is no principal. These trait objects
595+
/// have a "trivial" vtable consisting of just the size, alignment,
596+
/// and destructor.
572597
pub fn principal(&self) -> Option<ExistentialTraitRef<'tcx>> {
573598
match self[0] {
574599
ExistentialPredicate::Trait(tr) => Some(tr),
575600
_ => None
576601
}
577602
}
578603

579-
580604
pub fn principal_def_id(&self) -> Option<DefId> {
581605
self.principal().map(|d| d.def_id)
582606
}

0 commit comments

Comments
 (0)