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