]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_analysis/src/collect/predicates_of.rs
change `ConstEvaluatable` to use `ty::Const`
[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.hir_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 def_id = self.tcx.hir().local_def_id(c.hir_id);
320             let ct = ty::Const::from_anon_const(self.tcx, def_id);
321             if let ty::ConstKind::Unevaluated(_) = ct.kind() {
322                 let span = self.tcx.hir().span(c.hir_id);
323                 self.preds.insert((
324                     ty::Binder::dummy(ty::PredicateKind::ConstEvaluatable(ct))
325                         .to_predicate(self.tcx),
326                     span,
327                 ));
328             }
329         }
330
331         fn visit_const_param_default(&mut self, _param: HirId, _ct: &'tcx hir::AnonConst) {
332             // Do not look into const param defaults,
333             // these get checked when they are actually instantiated.
334             //
335             // We do not want the following to error:
336             //
337             //     struct Foo<const N: usize, const M: usize = { N + 1 }>;
338             //     struct Bar<const N: usize>(Foo<N, 3>);
339         }
340     }
341
342     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
343     let node = tcx.hir().get(hir_id);
344
345     let mut collector = ConstCollector { tcx, preds: FxIndexSet::default() };
346     if let hir::Node::Item(item) = node && let hir::ItemKind::Impl(ref impl_) = item.kind {
347         if let Some(of_trait) = &impl_.of_trait {
348             debug!("const_evaluatable_predicates_of({:?}): visit impl trait_ref", def_id);
349             collector.visit_trait_ref(of_trait);
350         }
351
352         debug!("const_evaluatable_predicates_of({:?}): visit_self_ty", def_id);
353         collector.visit_ty(impl_.self_ty);
354     }
355
356     if let Some(generics) = node.generics() {
357         debug!("const_evaluatable_predicates_of({:?}): visit_generics", def_id);
358         collector.visit_generics(generics);
359     }
360
361     if let Some(fn_sig) = tcx.hir().fn_sig_by_hir_id(hir_id) {
362         debug!("const_evaluatable_predicates_of({:?}): visit_fn_decl", def_id);
363         collector.visit_fn_decl(fn_sig.decl);
364     }
365     debug!("const_evaluatable_predicates_of({:?}) = {:?}", def_id, collector.preds);
366
367     collector.preds
368 }
369
370 pub(super) fn trait_explicit_predicates_and_bounds(
371     tcx: TyCtxt<'_>,
372     def_id: LocalDefId,
373 ) -> ty::GenericPredicates<'_> {
374     assert_eq!(tcx.def_kind(def_id), DefKind::Trait);
375     gather_explicit_predicates_of(tcx, def_id.to_def_id())
376 }
377
378 pub(super) fn explicit_predicates_of<'tcx>(
379     tcx: TyCtxt<'tcx>,
380     def_id: DefId,
381 ) -> ty::GenericPredicates<'tcx> {
382     let def_kind = tcx.def_kind(def_id);
383     if let DefKind::Trait = def_kind {
384         // Remove bounds on associated types from the predicates, they will be
385         // returned by `explicit_item_bounds`.
386         let predicates_and_bounds = tcx.trait_explicit_predicates_and_bounds(def_id.expect_local());
387         let trait_identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
388
389         let is_assoc_item_ty = |ty: Ty<'tcx>| {
390             // For a predicate from a where clause to become a bound on an
391             // associated type:
392             // * It must use the identity substs of the item.
393             //     * Since any generic parameters on the item are not in scope,
394             //       this means that the item is not a GAT, and its identity
395             //       substs are the same as the trait's.
396             // * It must be an associated type for this trait (*not* a
397             //   supertrait).
398             if let ty::Projection(projection) = ty.kind() {
399                 projection.substs == trait_identity_substs
400                     && tcx.associated_item(projection.item_def_id).container_id(tcx) == def_id
401             } else {
402                 false
403             }
404         };
405
406         let predicates: Vec<_> = predicates_and_bounds
407             .predicates
408             .iter()
409             .copied()
410             .filter(|(pred, _)| match pred.kind().skip_binder() {
411                 ty::PredicateKind::Trait(tr) => !is_assoc_item_ty(tr.self_ty()),
412                 ty::PredicateKind::Projection(proj) => {
413                     !is_assoc_item_ty(proj.projection_ty.self_ty())
414                 }
415                 ty::PredicateKind::TypeOutlives(outlives) => !is_assoc_item_ty(outlives.0),
416                 _ => true,
417             })
418             .collect();
419         if predicates.len() == predicates_and_bounds.predicates.len() {
420             predicates_and_bounds
421         } else {
422             ty::GenericPredicates {
423                 parent: predicates_and_bounds.parent,
424                 predicates: tcx.arena.alloc_slice(&predicates),
425             }
426         }
427     } else {
428         if matches!(def_kind, DefKind::AnonConst) && tcx.lazy_normalization() {
429             let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
430             if tcx.hir().opt_const_param_default_param_hir_id(hir_id).is_some() {
431                 // In `generics_of` we set the generics' parent to be our parent's parent which means that
432                 // we lose out on the predicates of our actual parent if we dont return those predicates here.
433                 // (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
434                 //
435                 // struct Foo<T, const N: usize = { <T as Trait>::ASSOC }>(T) where T: Trait;
436                 //        ^^^                     ^^^^^^^^^^^^^^^^^^^^^^^ the def id we are calling
437                 //        ^^^                                             explicit_predicates_of on
438                 //        parent item we dont have set as the
439                 //        parent of generics returned by `generics_of`
440                 //
441                 // In the above code we want the anon const to have predicates in its param env for `T: Trait`
442                 let item_def_id = tcx.hir().get_parent_item(hir_id);
443                 // In the above code example we would be calling `explicit_predicates_of(Foo)` here
444                 return tcx.explicit_predicates_of(item_def_id);
445             }
446         }
447         gather_explicit_predicates_of(tcx, def_id)
448     }
449 }
450
451 /// Ensures that the super-predicates of the trait with a `DefId`
452 /// of `trait_def_id` are converted and stored. This also ensures that
453 /// the transitive super-predicates are converted.
454 pub(super) fn super_predicates_of(
455     tcx: TyCtxt<'_>,
456     trait_def_id: DefId,
457 ) -> ty::GenericPredicates<'_> {
458     tcx.super_predicates_that_define_assoc_type((trait_def_id, None))
459 }
460
461 /// Ensures that the super-predicates of the trait with a `DefId`
462 /// of `trait_def_id` are converted and stored. This also ensures that
463 /// the transitive super-predicates are converted.
464 pub(super) fn super_predicates_that_define_assoc_type(
465     tcx: TyCtxt<'_>,
466     (trait_def_id, assoc_name): (DefId, Option<Ident>),
467 ) -> ty::GenericPredicates<'_> {
468     if trait_def_id.is_local() {
469         debug!("local trait");
470         let trait_hir_id = tcx.hir().local_def_id_to_hir_id(trait_def_id.expect_local());
471
472         let Node::Item(item) = tcx.hir().get(trait_hir_id) else {
473             bug!("trait_node_id {} is not an item", trait_hir_id);
474         };
475
476         let (generics, bounds) = match item.kind {
477             hir::ItemKind::Trait(.., ref generics, ref supertraits, _) => (generics, supertraits),
478             hir::ItemKind::TraitAlias(ref generics, ref supertraits) => (generics, supertraits),
479             _ => span_bug!(item.span, "super_predicates invoked on non-trait"),
480         };
481
482         let icx = ItemCtxt::new(tcx, trait_def_id);
483
484         // Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
485         let self_param_ty = tcx.types.self_param;
486         let superbounds1 = if let Some(assoc_name) = assoc_name {
487             <dyn AstConv<'_>>::compute_bounds_that_match_assoc_type(
488                 &icx,
489                 self_param_ty,
490                 bounds,
491                 assoc_name,
492             )
493         } else {
494             <dyn AstConv<'_>>::compute_bounds(&icx, self_param_ty, bounds)
495         };
496
497         let superbounds1 = superbounds1.predicates(tcx, self_param_ty);
498
499         // Convert any explicit superbounds in the where-clause,
500         // e.g., `trait Foo where Self: Bar`.
501         // In the case of trait aliases, however, we include all bounds in the where-clause,
502         // so e.g., `trait Foo = where u32: PartialEq<Self>` would include `u32: PartialEq<Self>`
503         // as one of its "superpredicates".
504         let is_trait_alias = tcx.is_trait_alias(trait_def_id);
505         let superbounds2 = icx.type_parameter_bounds_in_generics(
506             generics,
507             item.hir_id(),
508             self_param_ty,
509             OnlySelfBounds(!is_trait_alias),
510             assoc_name,
511         );
512
513         // Combine the two lists to form the complete set of superbounds:
514         let superbounds = &*tcx.arena.alloc_from_iter(superbounds1.into_iter().chain(superbounds2));
515         debug!(?superbounds);
516
517         // Now require that immediate supertraits are converted,
518         // which will, in turn, reach indirect supertraits.
519         if assoc_name.is_none() {
520             // Now require that immediate supertraits are converted,
521             // which will, in turn, reach indirect supertraits.
522             for &(pred, span) in superbounds {
523                 debug!("superbound: {:?}", pred);
524                 if let ty::PredicateKind::Trait(bound) = pred.kind().skip_binder() {
525                     tcx.at(span).super_predicates_of(bound.def_id());
526                 }
527             }
528         }
529
530         ty::GenericPredicates { parent: None, predicates: superbounds }
531     } else {
532         // if `assoc_name` is None, then the query should've been redirected to an
533         // external provider
534         assert!(assoc_name.is_some());
535         tcx.super_predicates_of(trait_def_id)
536     }
537 }
538
539 /// Returns the predicates defined on `item_def_id` of the form
540 /// `X: Foo` where `X` is the type parameter `def_id`.
541 #[instrument(level = "trace", skip(tcx))]
542 pub(super) fn type_param_predicates(
543     tcx: TyCtxt<'_>,
544     (item_def_id, def_id, assoc_name): (DefId, LocalDefId, Ident),
545 ) -> ty::GenericPredicates<'_> {
546     use rustc_hir::*;
547
548     // In the AST, bounds can derive from two places. Either
549     // written inline like `<T: Foo>` or in a where-clause like
550     // `where T: Foo`.
551
552     let param_id = tcx.hir().local_def_id_to_hir_id(def_id);
553     let param_owner = tcx.hir().ty_param_owner(def_id);
554     let generics = tcx.generics_of(param_owner);
555     let index = generics.param_def_id_to_index[&def_id.to_def_id()];
556     let ty = tcx.mk_ty_param(index, tcx.hir().ty_param_name(def_id));
557
558     // Don't look for bounds where the type parameter isn't in scope.
559     let parent = if item_def_id == param_owner.to_def_id() {
560         None
561     } else {
562         tcx.generics_of(item_def_id).parent
563     };
564
565     let mut result = parent
566         .map(|parent| {
567             let icx = ItemCtxt::new(tcx, parent);
568             icx.get_type_parameter_bounds(DUMMY_SP, def_id.to_def_id(), assoc_name)
569         })
570         .unwrap_or_default();
571     let mut extend = None;
572
573     let item_hir_id = tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local());
574     let ast_generics = match tcx.hir().get(item_hir_id) {
575         Node::TraitItem(item) => &item.generics,
576
577         Node::ImplItem(item) => &item.generics,
578
579         Node::Item(item) => {
580             match item.kind {
581                 ItemKind::Fn(.., ref generics, _)
582                 | ItemKind::Impl(hir::Impl { ref generics, .. })
583                 | ItemKind::TyAlias(_, ref generics)
584                 | ItemKind::OpaqueTy(OpaqueTy {
585                     ref generics,
586                     origin: hir::OpaqueTyOrigin::TyAlias,
587                     ..
588                 })
589                 | ItemKind::Enum(_, ref generics)
590                 | ItemKind::Struct(_, ref generics)
591                 | ItemKind::Union(_, ref generics) => generics,
592                 ItemKind::Trait(_, _, ref generics, ..) => {
593                     // Implied `Self: Trait` and supertrait bounds.
594                     if param_id == item_hir_id {
595                         let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id);
596                         extend =
597                             Some((identity_trait_ref.without_const().to_predicate(tcx), item.span));
598                     }
599                     generics
600                 }
601                 _ => return result,
602             }
603         }
604
605         Node::ForeignItem(item) => match item.kind {
606             ForeignItemKind::Fn(_, _, ref generics) => generics,
607             _ => return result,
608         },
609
610         _ => return result,
611     };
612
613     let icx = ItemCtxt::new(tcx, item_def_id);
614     let extra_predicates = extend.into_iter().chain(
615         icx.type_parameter_bounds_in_generics(
616             ast_generics,
617             param_id,
618             ty,
619             OnlySelfBounds(true),
620             Some(assoc_name),
621         )
622         .into_iter()
623         .filter(|(predicate, _)| match predicate.kind().skip_binder() {
624             ty::PredicateKind::Trait(data) => data.self_ty().is_param(index),
625             _ => false,
626         }),
627     );
628     result.predicates =
629         tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(extra_predicates));
630     result
631 }
632
633 impl<'tcx> ItemCtxt<'tcx> {
634     /// Finds bounds from `hir::Generics`. This requires scanning through the
635     /// AST. We do this to avoid having to convert *all* the bounds, which
636     /// would create artificial cycles. Instead, we can only convert the
637     /// bounds for a type parameter `X` if `X::Foo` is used.
638     #[instrument(level = "trace", skip(self, ast_generics))]
639     fn type_parameter_bounds_in_generics(
640         &self,
641         ast_generics: &'tcx hir::Generics<'tcx>,
642         param_id: hir::HirId,
643         ty: Ty<'tcx>,
644         only_self_bounds: OnlySelfBounds,
645         assoc_name: Option<Ident>,
646     ) -> Vec<(ty::Predicate<'tcx>, Span)> {
647         let param_def_id = self.tcx.hir().local_def_id(param_id).to_def_id();
648         trace!(?param_def_id);
649         ast_generics
650             .predicates
651             .iter()
652             .filter_map(|wp| match *wp {
653                 hir::WherePredicate::BoundPredicate(ref bp) => Some(bp),
654                 _ => None,
655             })
656             .flat_map(|bp| {
657                 let bt = if bp.is_param_bound(param_def_id) {
658                     Some(ty)
659                 } else if !only_self_bounds.0 {
660                     Some(self.to_ty(bp.bounded_ty))
661                 } else {
662                     None
663                 };
664                 let bvars = self.tcx.late_bound_vars(bp.hir_id);
665
666                 bp.bounds.iter().filter_map(move |b| bt.map(|bt| (bt, b, bvars))).filter(
667                     |(_, b, _)| match assoc_name {
668                         Some(assoc_name) => self.bound_defines_assoc_item(b, assoc_name),
669                         None => true,
670                     },
671                 )
672             })
673             .flat_map(|(bt, b, bvars)| predicates_from_bound(self, bt, b, bvars))
674             .collect()
675     }
676
677     #[instrument(level = "trace", skip(self))]
678     fn bound_defines_assoc_item(&self, b: &hir::GenericBound<'_>, assoc_name: Ident) -> bool {
679         match b {
680             hir::GenericBound::Trait(poly_trait_ref, _) => {
681                 let trait_ref = &poly_trait_ref.trait_ref;
682                 if let Some(trait_did) = trait_ref.trait_def_id() {
683                     self.tcx.trait_may_define_assoc_type(trait_did, assoc_name)
684                 } else {
685                     false
686                 }
687             }
688             _ => false,
689         }
690     }
691 }
692
693 /// Converts a specific `GenericBound` from the AST into a set of
694 /// predicates that apply to the self type. A vector is returned
695 /// because this can be anywhere from zero predicates (`T: ?Sized` adds no
696 /// predicates) to one (`T: Foo`) to many (`T: Bar<X = i32>` adds `T: Bar`
697 /// and `<T as Bar>::X == i32`).
698 fn predicates_from_bound<'tcx>(
699     astconv: &dyn AstConv<'tcx>,
700     param_ty: Ty<'tcx>,
701     bound: &'tcx hir::GenericBound<'tcx>,
702     bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
703 ) -> Vec<(ty::Predicate<'tcx>, Span)> {
704     let mut bounds = Bounds::default();
705     astconv.add_bounds(param_ty, [bound].into_iter(), &mut bounds, bound_vars);
706     bounds.predicates(astconv.tcx(), param_ty).collect()
707 }