@@ -15,13 +15,12 @@ use crate::lift::Lift;
15
15
use crate :: visit:: { Flags , TypeSuperVisitable , TypeVisitable , TypeVisitableExt , TypeVisitor } ;
16
16
use crate :: { self as ty, Interner } ;
17
17
18
- /// Binder is a binder for higher-ranked lifetimes or types. It is part of the
18
+ /// ` Binder` is a binder for higher-ranked lifetimes or types. It is part of the
19
19
/// compiler's representation for things like `for<'a> Fn(&'a isize)`
20
- /// (which would be represented by the type `PolyTraitRef ==
21
- /// Binder<I, TraitRef>`). Note that when we instantiate,
22
- /// erase, or otherwise "discharge" these bound vars, we change the
23
- /// type from `Binder<I, T>` to just `T` (see
24
- /// e.g., `liberate_late_bound_regions`).
20
+ /// (which would be represented by the type `PolyTraitRef == Binder<I, TraitRef>`).
21
+ ///
22
+ /// See <https://rustc-dev-guide.rust-lang.org/ty_module/instantiating_binders.html>
23
+ /// for more details.
25
24
///
26
25
/// `Decodable` and `Encodable` are implemented for `Binder<T>` using the `impl_binder_encode_decode!` macro.
27
26
#[ derive_where( Clone ; I : Interner , T : Clone ) ]
@@ -154,22 +153,19 @@ impl<I: Interner, T: TypeVisitable<I>> TypeSuperVisitable<I> for Binder<I, T> {
154
153
}
155
154
156
155
impl < I : Interner , T > Binder < I , T > {
157
- /// Skips the binder and returns the "bound" value. This is a
158
- /// risky thing to do because it's easy to get confused about
159
- /// De Bruijn indices and the like. It is usually better to
160
- /// discharge the binder using `no_bound_vars` or
161
- /// `instantiate_bound_regions` or something like
162
- /// that. `skip_binder` is only valid when you are either
163
- /// extracting data that has nothing to do with bound vars, you
164
- /// are doing some sort of test that does not involve bound
165
- /// regions, or you are being very careful about your depth
166
- /// accounting.
156
+ /// Returns the value contained inside of this `for<'a>`. Accessing generic args
157
+ /// in the returned value is generally incorrect.
158
+ ///
159
+ /// Please read <https://rustc-dev-guide.rust-lang.org/ty_module/instantiating_binders.html>
160
+ /// before using this function. It is usually better to discharge the binder using
161
+ /// `no_bound_vars` or `instantiate_bound_regions` or something like that.
167
162
///
168
- /// Some examples where `skip_binder` is reasonable:
163
+ /// `skip_binder` is only valid when you are either extracting data that does not reference
164
+ /// any generic arguments, e.g. a `DefId`, or when you're making sure you only pass the
165
+ /// value to things which can handle escaping bound vars.
169
166
///
170
- /// - extracting the `DefId` from a PolyTraitRef;
171
- /// - comparing the self type of a PolyTraitRef to see if it is equal to
172
- /// a type parameter `X`, since the type `X` does not reference any regions
167
+ /// See existing uses of `.skip_binder()` inside of the `rustc_trait_selection::traits::select`
168
+ /// or `rustc_next_trait_solver` for examples.
173
169
pub fn skip_binder ( self ) -> T {
174
170
self . value
175
171
}
@@ -336,12 +332,11 @@ impl<I: Interner> TypeVisitor<I> for ValidateBoundVars<I> {
336
332
}
337
333
}
338
334
339
- /// Similar to [`super:: Binder`] except that it tracks early bound generics, i.e. `struct Foo<T>(T)`
335
+ /// Similar to [`Binder`] except that it tracks early bound generics, i.e. `struct Foo<T>(T)`
340
336
/// needs `T` instantiated immediately. This type primarily exists to avoid forgetting to call
341
337
/// `instantiate`.
342
338
///
343
- /// If you don't have anything to `instantiate`, you may be looking for
344
- /// [`instantiate_identity`](EarlyBinder::instantiate_identity) or [`skip_binder`](EarlyBinder::skip_binder).
339
+ /// See <https://rustc-dev-guide.rust-lang.org/ty_module/early_binder.html> for more details.
345
340
#[ derive_where( Clone ; I : Interner , T : Clone ) ]
346
341
#[ derive_where( Copy ; I : Interner , T : Copy ) ]
347
342
#[ derive_where( PartialEq ; I : Interner , T : PartialEq ) ]
@@ -404,17 +399,22 @@ impl<I: Interner, T> EarlyBinder<I, T> {
404
399
EarlyBinder { value, _tcx : PhantomData }
405
400
}
406
401
407
- /// Skips the binder and returns the "bound" value.
408
- /// This can be used to extract data that does not depend on generic parameters
409
- /// (e.g., getting the `DefId` of the inner value or getting the number of
410
- /// arguments of an `FnSig`). Otherwise, consider using
411
- /// [`instantiate_identity`](EarlyBinder::instantiate_identity).
402
+ /// Skips the binder and returns the "bound" value. Accessing generic args
403
+ /// in the returned value is generally incorrect.
404
+ ///
405
+ /// Please read <https://rustc-dev-guide.rust-lang.org/ty_module/early_binder.html>
406
+ /// before using this function.
407
+ ///
408
+ /// Only use this to extract data that does not depend on generic parameters, e.g.
409
+ /// to get the `DefId` of the inner value or the number of arguments ofan `FnSig`,
410
+ /// or while making sure to only pass the value to functions which are explicitly
411
+ /// set up to handle these uninstantiated generic parameters.
412
412
///
413
413
/// To skip the binder on `x: &EarlyBinder<I, T>` to obtain `&T`, leverage
414
414
/// [`EarlyBinder::as_ref`](EarlyBinder::as_ref): `x.as_ref().skip_binder()`.
415
415
///
416
- /// See also [`Binder::skip_binder`](super:: Binder::skip_binder), which is
417
- /// the analogous operation on [`super:: Binder`].
416
+ /// See also [`Binder::skip_binder`](Binder::skip_binder), which is
417
+ /// the analogous operation on [`Binder`].
418
418
pub fn skip_binder ( self ) -> T {
419
419
self . value
420
420
}
0 commit comments