]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Rollup merge of #101162 - rajputrajat:master, r=davidtwco
[rust.git] / compiler / rustc_hir_analysis / src / collect / predicates_of.rs
1 use crate::astconv::AstConv;
2 use crate::bounds::Bounds;
3 use crate::collect::ItemCtxt;
4 use crate::constrained_generic_params as cgp;
5 use hir::{HirId, Node};
6 use rustc_data_structures::fx::FxIndexSet;
7 use rustc_hir as hir;
8 use rustc_hir::def::DefKind;
9 use rustc_hir::def_id::{DefId, LocalDefId};
10 use rustc_hir::intravisit::{self, Visitor};
11 use rustc_middle::ty::subst::InternalSubsts;
12 use rustc_middle::ty::ToPredicate;
13 use rustc_middle::ty::{self, Ty, TyCtxt};
14 use rustc_span::symbol::{sym, Ident};
15 use rustc_span::{Span, DUMMY_SP};
16
17 #[derive(Debug)]
18 struct OnlySelfBounds(bool);
19
20 /// Returns a list of all type predicates (explicit and implicit) for the definition with
21 /// ID `def_id`. This includes all predicates returned by `predicates_defined_on`, plus
22 /// `Self: Trait` predicates for traits.
23 pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
24     let mut result = tcx.predicates_defined_on(def_id);
25
26     if tcx.is_trait(def_id) {
27         // For traits, add `Self: Trait` predicate. This is
28         // not part of the predicates that a user writes, but it
29         // is something that one must prove in order to invoke a
30         // method or project an associated type.
31         //
32         // In the chalk setup, this predicate is not part of the
33         // "predicates" for a trait item. But it is useful in
34         // rustc because if you directly (e.g.) invoke a trait
35         // method like `Trait::method(...)`, you must naturally
36         // prove that the trait applies to the types that were
37         // used, and adding the predicate into this list ensures
38         // that this is done.
39         //
40         // We use a DUMMY_SP here as a way to signal trait bounds that come
41         // from the trait itself that *shouldn't* be shown as the source of
42         // an obligation and instead be skipped. Otherwise we'd use
43         // `tcx.def_span(def_id);`
44
45         let constness = if tcx.has_attr(def_id, sym::const_trait) {
46             ty::BoundConstness::ConstIfConst
47         } else {
48             ty::BoundConstness::NotConst
49         };
50
51         let span = rustc_span::DUMMY_SP;
52         result.predicates =
53             tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(std::iter::once((
54                 ty::TraitRef::identity(tcx, def_id).with_constness(constness).to_predicate(tcx),
55                 span,
56             ))));
57     }
58     debug!("predicates_of(def_id={:?}) = {:?}", def_id, result);
59     result
60 }
61
62 /// Returns a list of user-specified type predicates for the definition with ID `def_id`.
63 /// N.B., this does not include any implied/inferred constraints.
64 #[instrument(level = "trace", skip(tcx), ret)]
65 fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
66     use rustc_hir::*;
67
68     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
69     let node = tcx.hir().get(hir_id);
70
71     let mut is_trait = None;
72     let mut is_default_impl_trait = None;
73
74     let icx = ItemCtxt::new(tcx, def_id);
75
76     const NO_GENERICS: &hir::Generics<'_> = hir::Generics::empty();
77
78     // We use an `IndexSet` to preserves order of insertion.
79     // Preserving the order of insertion is important here so as not to break UI tests.
80     let mut predicates: FxIndexSet<(ty::Predicate<'_>, Span)> = FxIndexSet::default();
81
82     let ast_generics = match node {
83         Node::TraitItem(item) => item.generics,
84
85         Node::ImplItem(item) => item.generics,
86
87         Node::Item(item) => {
88             match item.kind {
89                 ItemKind::Impl(ref impl_) => {
90                     if impl_.defaultness.is_default() {
91                         is_default_impl_trait = tcx.impl_trait_ref(def_id).map(ty::Binder::dummy);
92                     }
93                     &impl_.generics
94                 }
95                 ItemKind::Fn(.., ref generics, _)
96                 | ItemKind::TyAlias(_, ref generics)
97                 | ItemKind::Enum(_, ref generics)
98                 | ItemKind::Struct(_, ref generics)
99                 | ItemKind::Union(_, ref generics) => *generics,
100
101                 ItemKind::Trait(_, _, ref generics, ..) => {
102                     is_trait = Some(ty::TraitRef::identity(tcx, def_id));
103                     *generics
104                 }
105                 ItemKind::TraitAlias(ref generics, _) => {
106                     is_trait = Some(ty::TraitRef::identity(tcx, def_id));
107                     *generics
108                 }
109                 ItemKind::OpaqueTy(OpaqueTy {
110                     origin: hir::OpaqueTyOrigin::AsyncFn(..) | hir::OpaqueTyOrigin::FnReturn(..),
111                     ..
112                 }) => {
113                     // return-position impl trait
114                     //
115                     // We don't inherit predicates from the parent here:
116                     // If we have, say `fn f<'a, T: 'a>() -> impl Sized {}`
117                     // then the return type is `f::<'static, T>::{{opaque}}`.
118                     //
119                     // If we inherited the predicates of `f` then we would
120                     // require that `T: 'static` to show that the return
121                     // type is well-formed.
122                     //
123                     // The only way to have something with this opaque type
124                     // is from the return type of the containing function,
125                     // which will ensure that the function's predicates
126                     // hold.
127                     return ty::GenericPredicates { parent: None, predicates: &[] };
128                 }
129                 ItemKind::OpaqueTy(OpaqueTy {
130                     ref generics,
131                     origin: hir::OpaqueTyOrigin::TyAlias,
132                     ..
133                 }) => {
134                     // type-alias impl trait
135                     generics
136                 }
137
138                 _ => NO_GENERICS,
139             }
140         }
141
142         Node::ForeignItem(item) => match item.kind {
143             ForeignItemKind::Static(..) => NO_GENERICS,
144             ForeignItemKind::Fn(_, _, ref generics) => *generics,
145             ForeignItemKind::Type => NO_GENERICS,
146         },
147
148         _ => NO_GENERICS,
149     };
150
151     let generics = tcx.generics_of(def_id);
152     let parent_count = generics.parent_count as u32;
153     let has_own_self = generics.has_self && parent_count == 0;
154
155     // Below we'll consider the bounds on the type parameters (including `Self`)
156     // and the explicit where-clauses, but to get the full set of predicates
157     // on a trait we need to add in the supertrait bounds and bounds found on
158     // associated types.
159     if let Some(_trait_ref) = is_trait {
160         predicates.extend(tcx.super_predicates_of(def_id).predicates.iter().cloned());
161     }
162
163     // In default impls, we can assume that the self type implements
164     // the trait. So in:
165     //
166     //     default impl Foo for Bar { .. }
167     //
168     // we add a default where clause `Foo: Bar`. We do a similar thing for traits
169     // (see below). Recall that a default impl is not itself an impl, but rather a
170     // set of defaults that can be incorporated into another impl.
171     if let Some(trait_ref) = is_default_impl_trait {
172         predicates.insert((trait_ref.without_const().to_predicate(tcx), tcx.def_span(def_id)));
173     }
174
175     // Collect the region predicates that were declared inline as
176     // well. In the case of parameters declared on a fn or method, we
177     // have to be careful to only iterate over early-bound regions.
178     let mut index = parent_count
179         + has_own_self as u32
180         + super::early_bound_lifetimes_from_generics(tcx, ast_generics).count() as u32;
181
182     trace!(?predicates);
183     trace!(?ast_generics);
184
185     // Collect the predicates that were written inline by the user on each
186     // type parameter (e.g., `<T: Foo>`).
187     for param in ast_generics.params {
188         match param.kind {
189             // We already dealt with early bound lifetimes above.
190             GenericParamKind::Lifetime { .. } => (),
191             GenericParamKind::Type { .. } => {
192                 let name = param.name.ident().name;
193                 let param_ty = ty::ParamTy::new(index, name).to_ty(tcx);
194                 index += 1;
195
196                 let mut bounds = Bounds::default();
197                 // Params are implicitly sized unless a `?Sized` bound is found
198                 <dyn AstConv<'_>>::add_implicitly_sized(
199                     &icx,
200                     &mut bounds,
201                     &[],
202                     Some((param.def_id, ast_generics.predicates)),
203                     param.span,
204                 );
205                 trace!(?bounds);
206                 predicates.extend(bounds.predicates(tcx, param_ty));
207                 trace!(?predicates);
208             }
209             GenericParamKind::Const { .. } => {
210                 // Bounds on const parameters are currently not possible.
211                 index += 1;
212             }
213         }
214     }
215
216     trace!(?predicates);
217     // Add in the bounds that appear in the where-clause.
218     for predicate in ast_generics.predicates {
219         match predicate {
220             hir::WherePredicate::BoundPredicate(bound_pred) => {
221                 let ty = icx.to_ty(bound_pred.bounded_ty);
222                 let bound_vars = icx.tcx.late_bound_vars(bound_pred.hir_id);
223
224                 // Keep the type around in a dummy predicate, in case of no bounds.
225                 // That way, `where Ty:` is not a complete noop (see #53696) and `Ty`
226                 // is still checked for WF.
227                 if bound_pred.bounds.is_empty() {
228                     if let ty::Param(_) = ty.kind() {
229                         // This is a `where T:`, which can be in the HIR from the
230                         // transformation that moves `?Sized` to `T`'s declaration.
231                         // We can skip the predicate because type parameters are
232                         // trivially WF, but also we *should*, to avoid exposing
233                         // users who never wrote `where Type:,` themselves, to
234                         // compiler/tooling bugs from not handling WF predicates.
235                     } else {
236                         let span = bound_pred.bounded_ty.span;
237                         let predicate = ty::Binder::bind_with_vars(
238                             ty::PredicateKind::WellFormed(ty.into()),
239                             bound_vars,
240                         );
241                         predicates.insert((predicate.to_predicate(tcx), span));
242                     }
243                 }
244
245                 let mut bounds = Bounds::default();
246                 <dyn AstConv<'_>>::add_bounds(
247                     &icx,
248                     ty,
249                     bound_pred.bounds.iter(),
250                     &mut bounds,
251                     bound_vars,
252                 );
253                 predicates.extend(bounds.predicates(tcx, ty));
254             }
255
256             hir::WherePredicate::RegionPredicate(region_pred) => {
257                 let r1 = <dyn AstConv<'_>>::ast_region_to_region(&icx, &region_pred.lifetime, None);
258                 predicates.extend(region_pred.bounds.iter().map(|bound| {
259                     let (r2, span) = match bound {
260                         hir::GenericBound::Outlives(lt) => {
261                             (<dyn AstConv<'_>>::ast_region_to_region(&icx, lt, None), lt.span)
262                         }
263                         _ => bug!(),
264                     };
265                     let pred = ty::Binder::dummy(ty::PredicateKind::RegionOutlives(
266                         ty::OutlivesPredicate(r1, r2),
267                     ))
268                     .to_predicate(icx.tcx);
269
270                     (pred, span)
271                 }))
272             }
273
274             hir::WherePredicate::EqPredicate(..) => {
275                 // FIXME(#20041)
276             }
277         }
278     }
279
280     if tcx.features().generic_const_exprs {
281         predicates.extend(const_evaluatable_predicates_of(tcx, def_id.expect_local()));
282     }
283
284     let mut predicates: Vec<_> = predicates.into_iter().collect();
285
286     // Subtle: before we store the predicates into the tcx, we
287     // sort them so that predicates like `T: Foo<Item=U>` come
288     // before uses of `U`.  This avoids false ambiguity errors
289     // in trait checking. See `setup_constraining_predicates`
290     // for details.
291     if let Node::Item(&Item { kind: ItemKind::Impl { .. }, .. }) = node {
292         let self_ty = tcx.type_of(def_id);
293         let trait_ref = tcx.impl_trait_ref(def_id);
294         cgp::setup_constraining_predicates(
295             tcx,
296             &mut predicates,
297             trait_ref,
298             &mut cgp::parameters_for_impl(self_ty, trait_ref),
299         );
300     }
301
302     ty::GenericPredicates {
303         parent: generics.parent,
304         predicates: tcx.arena.alloc_from_iter(predicates),
305     }
306 }
307
308 fn const_evaluatable_predicates_of<'tcx>(
309     tcx: TyCtxt<'tcx>,
310     def_id: LocalDefId,
311 ) -> FxIndexSet<(ty::Predicate<'tcx>, Span)> {
312     struct ConstCollector<'tcx> {
313         tcx: TyCtxt<'tcx>,
314         preds: FxIndexSet<(ty::Predicate<'tcx>, Span)>,
315     }
316
317     impl<'tcx> intravisit::Visitor<'tcx> for ConstCollector<'tcx> {
318         fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
319             let ct = ty::Const::from_anon_const(self.tcx, c.def_id);
320             if let ty::ConstKind::Unevaluated(_) = ct.kind() {
321                 let span = self.tcx.def_span(c.def_id);
322                 self.preds.insert((
323                     ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(ct))
324                         .to_predicate(self.tcx),
325                     span,
326                 ));
327             }
328         }
329
330         fn visit_const_param_default(&mut self, _param: HirId, _ct: &'tcx hir::AnonConst) {
331             // Do not look into const param defaults,
332             // these get checked when they are actually instantiated.
333             //
334             // We do not want the following to error:
335             //
336             //     struct Foo<const N: usize, const M: usize = { N + 1 }>;
337             //     struct Bar<const N: usize>(Foo<N, 3>);
338         }
339     }
340
341     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
342     let node = tcx.hir().get(hir_id);
343
344     let mut collector = ConstCollector { tcx, preds: FxIndexSet::default() };
345     if let hir::Node::Item(item) = node && let hir::ItemKind::Impl(ref impl_) = item.kind {
346         if let Some(of_trait) = &impl_.of_trait {
347             debug!("const_evaluatable_predicates_of({:?}): visit impl trait_ref", def_id);
348             collector.visit_trait_ref(of_trait);
349         }
350
351         debug!("const_evaluatable_predicates_of({:?}): visit_self_ty", def_id);
352         collector.visit_ty(impl_.self_ty);
353     }
354
355     if let Some(generics) = node.generics() {
356         debug!("const_evaluatable_predicates_of({:?}): visit_generics", def_id);
357         collector.visit_generics(generics);
358     }
359
360     if let Some(fn_sig) = tcx.hir().fn_sig_by_hir_id(hir_id) {
361         debug!("const_evaluatable_predicates_of({:?}): visit_fn_decl", def_id);
362         collector.visit_fn_decl(fn_sig.decl);
363     }
364     debug!("const_evaluatable_predicates_of({:?}) = {:?}", def_id, collector.preds);
365
366     collector.preds
367 }
368
369 pub(super) fn trait_explicit_predicates_and_bounds(
370     tcx: TyCtxt<'_>,
371     def_id: LocalDefId,
372 ) -> ty::GenericPredicates<'_> {
373     assert_eq!(tcx.def_kind(def_id), DefKind::Trait);
374     gather_explicit_predicates_of(tcx, def_id.to_def_id())
375 }
376
377 pub(super) fn explicit_predicates_of<'tcx>(
378     tcx: TyCtxt<'tcx>,
379     def_id: DefId,
380 ) -> ty::GenericPredicates<'tcx> {
381     let def_kind = tcx.def_kind(def_id);
382     if let DefKind::Trait = def_kind {
383         // Remove bounds on associated types from the predicates, they will be
384         // returned by `explicit_item_bounds`.
385         let predicates_and_bounds = tcx.trait_explicit_predicates_and_bounds(def_id.expect_local());
386         let trait_identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
387
388         let is_assoc_item_ty = |ty: Ty<'tcx>| {
389             // For a predicate from a where clause to become a bound on an
390             // associated type:
391             // * It must use the identity substs of the item.
392             //     * Since any generic parameters on the item are not in scope,
393             //       this means that the item is not a GAT, and its identity
394             //       substs are the same as the trait's.
395             // * It must be an associated type for this trait (*not* a
396             //   supertrait).
397             if let ty::Projection(projection) = ty.kind() {
398                 projection.substs == trait_identity_substs
399                     && tcx.associated_item(projection.item_def_id).container_id(tcx) == def_id
400             } else {
401                 false
402             }
403         };
404
405         let predicates: Vec<_> = predicates_and_bounds
406             .predicates
407             .iter()
408             .copied()
409             .filter(|(pred, _)| match pred.kind().skip_binder() {
410                 ty::PredicateKind::Trait(tr) => !is_assoc_item_ty(tr.self_ty()),
411                 ty::PredicateKind::Projection(proj) => {
412                     !is_assoc_item_ty(proj.projection_ty.self_ty())
413                 }
414                 ty::PredicateKind::TypeOutlives(outlives) => !is_assoc_item_ty(outlives.0),
415                 _ => true,
416             })
417             .collect();
418         if predicates.len() == predicates_and_bounds.predicates.len() {
419             predicates_and_bounds
420         } else {
421             ty::GenericPredicates {
422                 parent: predicates_and_bounds.parent,
423                 predicates: tcx.arena.alloc_slice(&predicates),
424             }
425         }
426     } else {
427         if matches!(def_kind, DefKind::AnonConst) && tcx.lazy_normalization() {
428             let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
429             let parent_def_id = tcx.hir().get_parent_item(hir_id);
430
431             if tcx.hir().opt_const_param_default_param_def_id(hir_id).is_some() {
432                 // In `generics_of` we set the generics' parent to be our parent's parent which means that
433                 // we lose out on the predicates of our actual parent if we dont return those predicates here.
434                 // (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
435                 //
436                 // struct Foo<T, const N: usize = { <T as Trait>::ASSOC }>(T) where T: Trait;
437                 //        ^^^                     ^^^^^^^^^^^^^^^^^^^^^^^ the def id we are calling
438                 //        ^^^                                             explicit_predicates_of on
439                 //        parent item we dont have set as the
440                 //        parent of generics returned by `generics_of`
441                 //
442                 // In the above code we want the anon const to have predicates in its param env for `T: Trait`
443                 // and we would be calling `explicit_predicates_of(Foo)` here
444                 return tcx.explicit_predicates_of(parent_def_id);
445             }
446
447             let parent_def_kind = tcx.def_kind(parent_def_id);
448             if matches!(parent_def_kind, DefKind::OpaqueTy) {
449                 // In `instantiate_identity` we inherit the predicates of our parent.
450                 // However, opaque types do not have a parent (see `gather_explicit_predicates_of`), which means
451                 // that we lose out on the predicates of our actual parent if we dont return those predicates here.
452                 //
453                 //
454                 // fn foo<T: Trait>() -> impl Iterator<Output = Another<{ <T as Trait>::ASSOC }> > { todo!() }
455                 //                                                        ^^^^^^^^^^^^^^^^^^^ the def id we are calling
456                 //                                                                            explicit_predicates_of on
457                 //
458                 // In the above code we want the anon const to have predicates in its param env for `T: Trait`.
459                 // However, the anon const cannot inherit predicates from its parent since it's opaque.
460                 //
461                 // To fix this, we call `explicit_predicates_of` directly on `foo`, the parent's parent.
462
463                 // In the above example this is `foo::{opaque#0}` or `impl Iterator`
464                 let parent_hir_id = tcx.hir().local_def_id_to_hir_id(parent_def_id.def_id);
465
466                 // In the above example this is the function `foo`
467                 let item_def_id = tcx.hir().get_parent_item(parent_hir_id);
468
469                 // In the above code example we would be calling `explicit_predicates_of(foo)` here
470                 return tcx.explicit_predicates_of(item_def_id);
471             }
472         }
473         gather_explicit_predicates_of(tcx, def_id)
474     }
475 }
476
477 /// Ensures that the super-predicates of the trait with a `DefId`
478 /// of `trait_def_id` are converted and stored. This also ensures that
479 /// the transitive super-predicates are converted.
480 pub(super) fn super_predicates_of(
481     tcx: TyCtxt<'_>,
482     trait_def_id: DefId,
483 ) -> ty::GenericPredicates<'_> {
484     tcx.super_predicates_that_define_assoc_type((trait_def_id, None))
485 }
486
487 /// Ensures that the super-predicates of the trait with a `DefId`
488 /// of `trait_def_id` are converted and stored. This also ensures that
489 /// the transitive super-predicates are converted.
490 pub(super) fn super_predicates_that_define_assoc_type(
491     tcx: TyCtxt<'_>,
492     (trait_def_id, assoc_name): (DefId, Option<Ident>),
493 ) -> ty::GenericPredicates<'_> {
494     if trait_def_id.is_local() {
495         debug!("local trait");
496         let trait_hir_id = tcx.hir().local_def_id_to_hir_id(trait_def_id.expect_local());
497
498         let Node::Item(item) = tcx.hir().get(trait_hir_id) else {
499             bug!("trait_node_id {} is not an item", trait_hir_id);
500         };
501
502         let (generics, bounds) = match item.kind {
503             hir::ItemKind::Trait(.., ref generics, ref supertraits, _) => (generics, supertraits),
504             hir::ItemKind::TraitAlias(ref generics, ref supertraits) => (generics, supertraits),
505             _ => span_bug!(item.span, "super_predicates invoked on non-trait"),
506         };
507
508         let icx = ItemCtxt::new(tcx, trait_def_id);
509
510         // Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
511         let self_param_ty = tcx.types.self_param;
512         let superbounds1 = if let Some(assoc_name) = assoc_name {
513             <dyn AstConv<'_>>::compute_bounds_that_match_assoc_type(
514                 &icx,
515                 self_param_ty,
516                 bounds,
517                 assoc_name,
518             )
519         } else {
520             <dyn AstConv<'_>>::compute_bounds(&icx, self_param_ty, bounds)
521         };
522
523         let superbounds1 = superbounds1.predicates(tcx, self_param_ty);
524
525         // Convert any explicit superbounds in the where-clause,
526         // e.g., `trait Foo where Self: Bar`.
527         // In the case of trait aliases, however, we include all bounds in the where-clause,
528         // so e.g., `trait Foo = where u32: PartialEq<Self>` would include `u32: PartialEq<Self>`
529         // as one of its "superpredicates".
530         let is_trait_alias = tcx.is_trait_alias(trait_def_id);
531         let superbounds2 = icx.type_parameter_bounds_in_generics(
532             generics,
533             item.owner_id.def_id,
534             self_param_ty,
535             OnlySelfBounds(!is_trait_alias),
536             assoc_name,
537         );
538
539         // Combine the two lists to form the complete set of superbounds:
540         let superbounds = &*tcx.arena.alloc_from_iter(superbounds1.into_iter().chain(superbounds2));
541         debug!(?superbounds);
542
543         // Now require that immediate supertraits are converted,
544         // which will, in turn, reach indirect supertraits.
545         if assoc_name.is_none() {
546             // Now require that immediate supertraits are converted,
547             // which will, in turn, reach indirect supertraits.
548             for &(pred, span) in superbounds {
549                 debug!("superbound: {:?}", pred);
550                 if let ty::PredicateKind::Trait(bound) = pred.kind().skip_binder() {
551                     tcx.at(span).super_predicates_of(bound.def_id());
552                 }
553             }
554         }
555
556         ty::GenericPredicates { parent: None, predicates: superbounds }
557     } else {
558         // if `assoc_name` is None, then the query should've been redirected to an
559         // external provider
560         assert!(assoc_name.is_some());
561         tcx.super_predicates_of(trait_def_id)
562     }
563 }
564
565 /// Returns the predicates defined on `item_def_id` of the form
566 /// `X: Foo` where `X` is the type parameter `def_id`.
567 #[instrument(level = "trace", skip(tcx))]
568 pub(super) fn type_param_predicates(
569     tcx: TyCtxt<'_>,
570     (item_def_id, def_id, assoc_name): (DefId, LocalDefId, Ident),
571 ) -> ty::GenericPredicates<'_> {
572     use rustc_hir::*;
573
574     // In the AST, bounds can derive from two places. Either
575     // written inline like `<T: Foo>` or in a where-clause like
576     // `where T: Foo`.
577
578     let param_id = tcx.hir().local_def_id_to_hir_id(def_id);
579     let param_owner = tcx.hir().ty_param_owner(def_id);
580     let generics = tcx.generics_of(param_owner);
581     let index = generics.param_def_id_to_index[&def_id.to_def_id()];
582     let ty = tcx.mk_ty_param(index, tcx.hir().ty_param_name(def_id));
583
584     // Don't look for bounds where the type parameter isn't in scope.
585     let parent = if item_def_id == param_owner.to_def_id() {
586         None
587     } else {
588         tcx.generics_of(item_def_id).parent
589     };
590
591     let mut result = parent
592         .map(|parent| {
593             let icx = ItemCtxt::new(tcx, parent);
594             icx.get_type_parameter_bounds(DUMMY_SP, def_id.to_def_id(), assoc_name)
595         })
596         .unwrap_or_default();
597     let mut extend = None;
598
599     let item_hir_id = tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local());
600     let ast_generics = match tcx.hir().get(item_hir_id) {
601         Node::TraitItem(item) => &item.generics,
602
603         Node::ImplItem(item) => &item.generics,
604
605         Node::Item(item) => {
606             match item.kind {
607                 ItemKind::Fn(.., ref generics, _)
608                 | ItemKind::Impl(hir::Impl { ref generics, .. })
609                 | ItemKind::TyAlias(_, ref generics)
610                 | ItemKind::OpaqueTy(OpaqueTy {
611                     ref generics,
612                     origin: hir::OpaqueTyOrigin::TyAlias,
613                     ..
614                 })
615                 | ItemKind::Enum(_, ref generics)
616                 | ItemKind::Struct(_, ref generics)
617                 | ItemKind::Union(_, ref generics) => generics,
618                 ItemKind::Trait(_, _, ref generics, ..) => {
619                     // Implied `Self: Trait` and supertrait bounds.
620                     if param_id == item_hir_id {
621                         let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id);
622                         extend =
623                             Some((identity_trait_ref.without_const().to_predicate(tcx), item.span));
624                     }
625                     generics
626                 }
627                 _ => return result,
628             }
629         }
630
631         Node::ForeignItem(item) => match item.kind {
632             ForeignItemKind::Fn(_, _, ref generics) => generics,
633             _ => return result,
634         },
635
636         _ => return result,
637     };
638
639     let icx = ItemCtxt::new(tcx, item_def_id);
640     let extra_predicates = extend.into_iter().chain(
641         icx.type_parameter_bounds_in_generics(
642             ast_generics,
643             def_id,
644             ty,
645             OnlySelfBounds(true),
646             Some(assoc_name),
647         )
648         .into_iter()
649         .filter(|(predicate, _)| match predicate.kind().skip_binder() {
650             ty::PredicateKind::Trait(data) => data.self_ty().is_param(index),
651             _ => false,
652         }),
653     );
654     result.predicates =
655         tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(extra_predicates));
656     result
657 }
658
659 impl<'tcx> ItemCtxt<'tcx> {
660     /// Finds bounds from `hir::Generics`. This requires scanning through the
661     /// AST. We do this to avoid having to convert *all* the bounds, which
662     /// would create artificial cycles. Instead, we can only convert the
663     /// bounds for a type parameter `X` if `X::Foo` is used.
664     #[instrument(level = "trace", skip(self, ast_generics))]
665     fn type_parameter_bounds_in_generics(
666         &self,
667         ast_generics: &'tcx hir::Generics<'tcx>,
668         param_def_id: LocalDefId,
669         ty: Ty<'tcx>,
670         only_self_bounds: OnlySelfBounds,
671         assoc_name: Option<Ident>,
672     ) -> Vec<(ty::Predicate<'tcx>, Span)> {
673         ast_generics
674             .predicates
675             .iter()
676             .filter_map(|wp| match *wp {
677                 hir::WherePredicate::BoundPredicate(ref bp) => Some(bp),
678                 _ => None,
679             })
680             .flat_map(|bp| {
681                 let bt = if bp.is_param_bound(param_def_id.to_def_id()) {
682                     Some(ty)
683                 } else if !only_self_bounds.0 {
684                     Some(self.to_ty(bp.bounded_ty))
685                 } else {
686                     None
687                 };
688                 let bvars = self.tcx.late_bound_vars(bp.hir_id);
689
690                 bp.bounds.iter().filter_map(move |b| bt.map(|bt| (bt, b, bvars))).filter(
691                     |(_, b, _)| match assoc_name {
692                         Some(assoc_name) => self.bound_defines_assoc_item(b, assoc_name),
693                         None => true,
694                     },
695                 )
696             })
697             .flat_map(|(bt, b, bvars)| predicates_from_bound(self, bt, b, bvars))
698             .collect()
699     }
700
701     #[instrument(level = "trace", skip(self))]
702     fn bound_defines_assoc_item(&self, b: &hir::GenericBound<'_>, assoc_name: Ident) -> bool {
703         match b {
704             hir::GenericBound::Trait(poly_trait_ref, _) => {
705                 let trait_ref = &poly_trait_ref.trait_ref;
706                 if let Some(trait_did) = trait_ref.trait_def_id() {
707                     self.tcx.trait_may_define_assoc_type(trait_did, assoc_name)
708                 } else {
709                     false
710                 }
711             }
712             _ => false,
713         }
714     }
715 }
716
717 /// Converts a specific `GenericBound` from the AST into a set of
718 /// predicates that apply to the self type. A vector is returned
719 /// because this can be anywhere from zero predicates (`T: ?Sized` adds no
720 /// predicates) to one (`T: Foo`) to many (`T: Bar<X = i32>` adds `T: Bar`
721 /// and `<T as Bar>::X == i32`).
722 fn predicates_from_bound<'tcx>(
723     astconv: &dyn AstConv<'tcx>,
724     param_ty: Ty<'tcx>,
725     bound: &'tcx hir::GenericBound<'tcx>,
726     bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
727 ) -> Vec<(ty::Predicate<'tcx>, Span)> {
728     let mut bounds = Bounds::default();
729     astconv.add_bounds(param_ty, [bound].into_iter(), &mut bounds, bound_vars);
730     bounds.predicates(astconv.tcx(), param_ty).collect()
731 }