Skip to content

Commit 5162e39

Browse files
authored
Rollup merge of rust-lang#106953 - kylematsuda:early-binder-docs, r=jackh726
Document `EarlyBinder::subst_identity` and `skip_binder` Finishing implementing rust-lang#105779 will change several commonly used queries to return `EarlyBinder` by default. This PR adds documentation for two of the methods used to access data inside the `EarlyBinder`. I tried to summarize some of the [discussion from the issue](rust-lang#105779 (comment)) in writing this. r? `@lcnr`
2 parents 9bcc46e + 1ae1c49 commit 5162e39

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
@@ -545,6 +545,9 @@ impl<'tcx, T: TypeVisitable<'tcx>> TypeVisitable<'tcx> for &'tcx ty::List<T> {
545545
/// Similar to [`super::Binder`] except that it tracks early bound generics, i.e. `struct Foo<T>(T)`
546546
/// needs `T` substituted immediately. This type primarily exists to avoid forgetting to call
547547
/// `subst`.
548+
///
549+
/// If you don't have anything to `subst`, you may be looking for
550+
/// [`subst_identity`](EarlyBinder::subst_identity) or [`skip_binder`](EarlyBinder::skip_binder).
548551
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
549552
#[derive(Encodable, Decodable, HashStable)]
550553
pub struct EarlyBinder<T>(pub T);
@@ -585,6 +588,14 @@ impl<T> EarlyBinder<T> {
585588
EarlyBinder(value)
586589
}
587590

591+
/// Skips the binder and returns the "bound" value.
592+
/// This can be used to extract data that does not depend on generic parameters
593+
/// (e.g., getting the `DefId` of the inner value or getting the number of
594+
/// arguments of an `FnSig`). Otherwise, consider using
595+
/// [`subst_identity`](EarlyBinder::subst_identity).
596+
///
597+
/// See also [`Binder::skip_binder`](super::Binder::skip_binder), which is
598+
/// the analogous operation on [`super::Binder`].
588599
pub fn skip_binder(self) -> T {
589600
self.0
590601
}
@@ -736,6 +747,14 @@ impl<'tcx, T: TypeFoldable<'tcx>> ty::EarlyBinder<T> {
736747
self.0.fold_with(&mut folder)
737748
}
738749

750+
/// Makes the identity substitution `T0 => T0, ..., TN => TN`.
751+
/// Conceptually, this converts universally bound variables into placeholders
752+
/// when inside of a given item.
753+
///
754+
/// For example, consider `for<T> fn foo<T>(){ .. }`:
755+
/// - Outside of `foo`, `T` is bound (represented by the presence of `EarlyBinder`).
756+
/// - Inside of the body of `foo`, we treat `T` as a placeholder by calling
757+
/// `subst_identity` to discharge the `EarlyBinder`.
739758
pub fn subst_identity(self) -> T {
740759
self.0
741760
}

0 commit comments

Comments
 (0)