Skip to content

Commit 1ae1c49

Browse files
committed
document EarlyBinder::subst_identity and skip_binder
1 parent 4817259 commit 1ae1c49

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

compiler/rustc_middle/src/ty/subst.rs

+19
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,9 @@ impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for &'tcx ty::List<T> {
538538
/// Similar to [`super::Binder`] except that it tracks early bound generics, i.e. `struct Foo<T>(T)`
539539
/// needs `T` substituted immediately. This type primarily exists to avoid forgetting to call
540540
/// `subst`.
541+
///
542+
/// If you don't have anything to `subst`, you may be looking for
543+
/// [`subst_identity`](EarlyBinder::subst_identity) or [`skip_binder`](EarlyBinder::skip_binder).
541544
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
542545
#[derive(Encodable, Decodable, HashStable)]
543546
pub struct EarlyBinder<T>(pub T);
@@ -578,6 +581,14 @@ impl<T> EarlyBinder<T> {
578581
EarlyBinder(value)
579582
}
580583

584+
/// Skips the binder and returns the "bound" value.
585+
/// This can be used to extract data that does not depend on generic parameters
586+
/// (e.g., getting the `DefId` of the inner value or getting the number of
587+
/// arguments of an `FnSig`). Otherwise, consider using
588+
/// [`subst_identity`](EarlyBinder::subst_identity).
589+
///
590+
/// See also [`Binder::skip_binder`](super::Binder::skip_binder), which is
591+
/// the analogous operation on [`super::Binder`].
581592
pub fn skip_binder(self) -> T {
582593
self.0
583594
}
@@ -729,6 +740,14 @@ impl<'tcx, T: TypeFoldable<'tcx>> ty::EarlyBinder<T> {
729740
self.0.fold_with(&mut folder)
730741
}
731742

743+
/// Makes the identity substitution `T0 => T0, ..., TN => TN`.
744+
/// Conceptually, this converts universally bound variables into placeholders
745+
/// when inside of a given item.
746+
///
747+
/// For example, consider `for<T> fn foo<T>(){ .. }`:
748+
/// - Outside of `foo`, `T` is bound (represented by the presence of `EarlyBinder`).
749+
/// - Inside of the body of `foo`, we treat `T` as a placeholder by calling
750+
/// `subst_identity` to discharge the `EarlyBinder`.
732751
pub fn subst_identity(self) -> T {
733752
self.0
734753
}

0 commit comments

Comments
 (0)