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