@@ -14,7 +14,9 @@ use crate::bounds::Bounds;
14
14
use crate :: errors;
15
15
16
16
impl < ' tcx > dyn HirTyLowerer < ' tcx > + ' _ {
17
- /// Sets `implicitly_sized` to true on `Bounds` if necessary
17
+ /// Add a `Sized` bound to the `bounds` if appropriate.
18
+ ///
19
+ /// Doesn't add the bound if the HIR bounds contain any of `Sized`, `?Sized` or `!Sized`.
18
20
pub ( crate ) fn add_sized_bound (
19
21
& self ,
20
22
bounds : & mut Bounds < ' tcx > ,
@@ -98,21 +100,21 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
98
100
}
99
101
}
100
102
101
- /// This helper takes a *converted* parameter type (`param_ty`)
102
- /// and an *unconverted* list of bounds:
103
+ /// Lower HIR bounds into `bounds` given the self type `param_ty`.
104
+ ///
105
+ /// ### Example
103
106
///
104
- /// ```text
105
- /// fn foo<T: Debug>
106
- /// ^ ^^^^^ `ast_bounds` parameter , in HIR form
107
- /// |
108
- /// `param_ty`, in ty form
107
+ /// ```ignore (illustrative)
108
+ /// fn foo<T: Debug>() { }
109
+ /// // ^ ^^^^^ `ast_bounds`, in HIR form
110
+ /// // |
111
+ /// // `param_ty`, in ty form
109
112
/// ```
110
113
///
111
- /// It adds these `ast_bounds` into the `bounds` structure.
114
+ /// ### A Note on Binders
112
115
///
113
- /// **A note on binders:** there is an implied binder around
114
- /// `param_ty` and `ast_bounds`. See `instantiate_poly_trait_ref`
115
- /// for more details.
116
+ /// There is an implied binder around `param_ty` and `ast_bounds`.
117
+ /// See `lower_poly_trait_ref` for more details.
116
118
#[ instrument( level = "debug" , skip( self , ast_bounds, bounds) ) ]
117
119
pub ( crate ) fn lower_poly_bounds < ' hir , I : Iterator < Item = & ' hir hir:: GenericBound < ' tcx > > > (
118
120
& self ,
@@ -168,22 +170,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
168
170
}
169
171
}
170
172
171
- /// Translates a list of bounds from the HIR into the `Bounds` data structure.
172
- /// The self-type for the bounds is given by `param_ty`.
173
+ /// Lower HIR bounds into `bounds` given the self type `param_ty`.
173
174
///
174
- /// Example:
175
+ /// ### Example
175
176
///
176
177
/// ```ignore (illustrative)
177
178
/// fn foo<T: Bar + Baz>() { }
178
179
/// // ^ ^^^^^^^^^ ast_bounds
179
180
/// // param_ty
180
181
/// ```
181
- ///
182
- /// The `sized_by_default` parameter indicates if, in this context, the `param_ty` should be
183
- /// considered `Sized` unless there is an explicit `?Sized` bound. This would be true in the
184
- /// example above, but is not true in supertrait listings like `trait Foo: Bar + Baz`.
185
- ///
186
- /// `span` should be the declaration size of the parameter.
187
182
pub ( crate ) fn lower_mono_bounds (
188
183
& self ,
189
184
param_ty : Ty < ' tcx > ,
@@ -225,12 +220,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
225
220
bounds
226
221
}
227
222
228
- /// Given an HIR binding like `Item = Foo` or `Item: Foo`, pushes the corresponding predicates
229
- /// onto `bounds`.
223
+ /// Lower an associated item binding from HIR into `bounds`.
224
+ ///
225
+ /// ### A Note on Binders
230
226
///
231
- /// **A note on binders:** given something like `T: for<'a> Iterator<Item = &'a u32>`, the
232
- /// `trait_ref` here will be `for<'a> T: Iterator`. The `binding` data however is from *inside*
233
- /// the binder (e.g., `&'a u32`) and hence may reference bound regions.
227
+ /// Given something like `T: for<'a> Iterator<Item = &'a u32>`,
228
+ /// the `trait_ref` here will be `for<'a> T: Iterator`.
229
+ /// The `binding` data however is from *inside* the binder
230
+ /// (e.g., `&'a u32`) and hence may reference bound regions.
234
231
#[ instrument( level = "debug" , skip( self , bounds, speculative, dup_bindings, path_span) ) ]
235
232
pub ( super ) fn lower_assoc_item_binding (
236
233
& self ,
@@ -243,22 +240,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
243
240
path_span : Span ,
244
241
only_self_bounds : OnlySelfBounds ,
245
242
) -> Result < ( ) , ErrorGuaranteed > {
246
- // Given something like `U: SomeTrait<T = X>`, we want to produce a
247
- // predicate like `<U as SomeTrait>::T = X`. This is somewhat
248
- // subtle in the event that `T` is defined in a supertrait of
249
- // `SomeTrait`, because in that case we need to upcast.
250
- //
251
- // That is, consider this case:
252
- //
253
- // ```
254
- // trait SubTrait: SuperTrait<i32> { }
255
- // trait SuperTrait<A> { type T; }
256
- //
257
- // ... B: SubTrait<T = foo> ...
258
- // ```
259
- //
260
- // We want to produce `<B as SuperTrait<i32>>::T == foo`.
261
-
262
243
let tcx = self . tcx ( ) ;
263
244
264
245
let assoc_kind = if binding. gen_args . parenthesized
@@ -271,6 +252,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
271
252
ty:: AssocKind :: Type
272
253
} ;
273
254
255
+ // Given something like `U: Trait<T = X>`, we want to produce a predicate like
256
+ // `<U as Trait>::T = X`.
257
+ // This is somewhat subtle in the event that `T` is defined in a supertrait of `Trait`,
258
+ // because in that case we need to upcast. I.e., we want to produce
259
+ // `<B as SuperTrait<i32>>::T == X` for `B: SubTrait<T = X>` where
260
+ //
261
+ // trait SubTrait: SuperTrait<i32> {}
262
+ // trait SuperTrait<A> { type T; }
274
263
let candidate = if self . probe_trait_that_defines_assoc_item (
275
264
trait_ref. def_id ( ) ,
276
265
assoc_kind,
@@ -457,6 +446,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
457
446
span : binding. span ,
458
447
} ) ) ;
459
448
}
449
+ // Lower an equality constraint like `Item = u32` as found in HIR bound `T: Iterator<Item = u32>`
450
+ // to a projection predicate: `<T as Iterator>::Item = u32`.
460
451
hir:: TypeBindingKind :: Equality { term } => {
461
452
let term = match term {
462
453
hir:: Term :: Ty ( ty) => self . lower_ty ( ty) . into ( ) ,
@@ -500,29 +491,21 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
500
491
) ;
501
492
}
502
493
503
- // "Desugar" a constraint like `T: Iterator<Item = u32>` this to
504
- // the "projection predicate" for:
505
- //
506
- // `<T as Iterator>::Item = u32`
507
494
bounds. push_projection_bound (
508
495
tcx,
509
496
projection_ty
510
497
. map_bound ( |projection_ty| ty:: ProjectionPredicate { projection_ty, term } ) ,
511
498
binding. span ,
512
499
) ;
513
500
}
501
+ // Lower a constraint like `Item: Debug` as found in HIR bound `T: Iterator<Item: Debug>`
502
+ // to a bound involving a projection: `<T as Iterator>::Item: Debug`.
514
503
hir:: TypeBindingKind :: Constraint { bounds : ast_bounds } => {
515
- // "Desugar" a constraint like `T: Iterator<Item: Debug>` to
516
- //
517
- // `<T as Iterator>::Item: Debug`
518
- //
519
- // Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
520
- // parameter to have a skipped binder.
521
- //
522
- // NOTE: If `only_self_bounds` is true, do NOT expand this associated
523
- // type bound into a trait predicate, since we only want to add predicates
524
- // for the `Self` type.
504
+ // NOTE: If `only_self_bounds` is true, do NOT expand this associated type bound into
505
+ // a trait predicate, since we only want to add predicates for the `Self` type.
525
506
if !only_self_bounds. 0 {
507
+ // Calling `skip_binder` is okay, because `lower_bounds` expects the `param_ty`
508
+ // parameter to have a skipped binder.
526
509
let param_ty = Ty :: new_alias ( tcx, ty:: Projection , projection_ty. skip_binder ( ) ) ;
527
510
self . lower_poly_bounds (
528
511
param_ty,
0 commit comments