• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::astconv::{AstConv, OnlySelfBounds, PredicateFilter};
2 use crate::bounds::Bounds;
3 use crate::collect::ItemCtxt;
4 use crate::constrained_generic_params as cgp;
5 use hir::{HirId, Lifetime, 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::{self, Ty, TyCtxt};
13 use rustc_middle::ty::{GenericPredicates, Generics, ImplTraitInTraitData, ToPredicate};
14 use rustc_span::symbol::{sym, Ident};
15 use rustc_span::{Span, Symbol, DUMMY_SP};
16 
17 /// Returns a list of all type predicates (explicit and implicit) for the definition with
18 /// ID `def_id`. This includes all predicates returned by `predicates_defined_on`, plus
19 /// `Self: Trait` predicates for traits.
predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_>20 pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
21     let mut result = tcx.predicates_defined_on(def_id);
22 
23     if tcx.is_trait(def_id) {
24         // For traits, add `Self: Trait` predicate. This is
25         // not part of the predicates that a user writes, but it
26         // is something that one must prove in order to invoke a
27         // method or project an associated type.
28         //
29         // In the chalk setup, this predicate is not part of the
30         // "predicates" for a trait item. But it is useful in
31         // rustc because if you directly (e.g.) invoke a trait
32         // method like `Trait::method(...)`, you must naturally
33         // prove that the trait applies to the types that were
34         // used, and adding the predicate into this list ensures
35         // that this is done.
36         //
37         // We use a DUMMY_SP here as a way to signal trait bounds that come
38         // from the trait itself that *shouldn't* be shown as the source of
39         // an obligation and instead be skipped. Otherwise we'd use
40         // `tcx.def_span(def_id);`
41 
42         let constness = if tcx.has_attr(def_id, sym::const_trait) {
43             ty::BoundConstness::ConstIfConst
44         } else {
45             ty::BoundConstness::NotConst
46         };
47 
48         let span = rustc_span::DUMMY_SP;
49         result.predicates =
50             tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(std::iter::once((
51                 ty::TraitRef::identity(tcx, def_id).with_constness(constness).to_predicate(tcx),
52                 span,
53             ))));
54     }
55     debug!("predicates_of(def_id={:?}) = {:?}", def_id, result);
56     result
57 }
58 
59 /// Returns a list of user-specified type predicates for the definition with ID `def_id`.
60 /// N.B., this does not include any implied/inferred constraints.
61 #[instrument(level = "trace", skip(tcx), ret)]
gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::GenericPredicates<'_>62 fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::GenericPredicates<'_> {
63     use rustc_hir::*;
64 
65     match tcx.opt_rpitit_info(def_id.to_def_id()) {
66         Some(ImplTraitInTraitData::Trait { opaque_def_id, fn_def_id }) => {
67             let opaque_ty_id = tcx.hir().local_def_id_to_hir_id(opaque_def_id.expect_local());
68             let opaque_ty_node = tcx.hir().get(opaque_ty_id);
69             let Node::Item(&Item { kind: ItemKind::OpaqueTy(OpaqueTy { lifetime_mapping, .. }), .. }) = opaque_ty_node else {
70                 bug!("unexpected {opaque_ty_node:?}")
71             };
72 
73             let mut predicates = Vec::new();
74 
75             // RPITITs should inherit the predicates of their parent. This is
76             // both to ensure that the RPITITs are only instantiated when the
77             // parent predicates would hold, and also so that the param-env
78             // inherits these predicates as assumptions.
79             let identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
80             predicates.extend(
81                 tcx.explicit_predicates_of(fn_def_id).instantiate_own(tcx, identity_substs),
82             );
83 
84             // We also install bidirectional outlives predicates for the RPITIT
85             // to keep the duplicates lifetimes from opaque lowering in sync.
86             compute_bidirectional_outlives_predicates(
87                 tcx,
88                 def_id,
89                 lifetime_mapping.iter().map(|(lifetime, def_id)| {
90                     (*lifetime, (*def_id, lifetime.ident.name, lifetime.ident.span))
91                 }),
92                 tcx.generics_of(def_id.to_def_id()),
93                 &mut predicates,
94             );
95 
96             return ty::GenericPredicates {
97                 parent: Some(tcx.parent(def_id.to_def_id())),
98                 predicates: tcx.arena.alloc_from_iter(predicates),
99             };
100         }
101 
102         Some(ImplTraitInTraitData::Impl { fn_def_id }) => {
103             let assoc_item = tcx.associated_item(def_id);
104             let trait_assoc_predicates =
105                 tcx.explicit_predicates_of(assoc_item.trait_item_def_id.unwrap());
106 
107             let impl_assoc_identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
108             let impl_def_id = tcx.parent(fn_def_id);
109             let impl_trait_ref_substs =
110                 tcx.impl_trait_ref(impl_def_id).unwrap().subst_identity().substs;
111 
112             let impl_assoc_substs =
113                 impl_assoc_identity_substs.rebase_onto(tcx, impl_def_id, impl_trait_ref_substs);
114 
115             let impl_predicates = trait_assoc_predicates.instantiate_own(tcx, impl_assoc_substs);
116 
117             return ty::GenericPredicates {
118                 parent: Some(impl_def_id),
119                 predicates: tcx.arena.alloc_from_iter(impl_predicates),
120             };
121         }
122 
123         None => {}
124     }
125 
126     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
127     let node = tcx.hir().get(hir_id);
128 
129     let mut is_trait = None;
130     let mut is_default_impl_trait = None;
131 
132     // FIXME: Should ItemCtxt take a LocalDefId?
133     let icx = ItemCtxt::new(tcx, def_id);
134 
135     const NO_GENERICS: &hir::Generics<'_> = hir::Generics::empty();
136 
137     // We use an `IndexSet` to preserve order of insertion.
138     // Preserving the order of insertion is important here so as not to break UI tests.
139     let mut predicates: FxIndexSet<(ty::Clause<'_>, Span)> = FxIndexSet::default();
140 
141     let ast_generics = match node {
142         Node::TraitItem(item) => item.generics,
143 
144         Node::ImplItem(item) => item.generics,
145 
146         Node::Item(item) => match item.kind {
147             ItemKind::Impl(impl_) => {
148                 if impl_.defaultness.is_default() {
149                     is_default_impl_trait =
150                         tcx.impl_trait_ref(def_id).map(|t| ty::Binder::dummy(t.subst_identity()));
151                 }
152                 impl_.generics
153             }
154             ItemKind::Fn(.., generics, _)
155             | ItemKind::TyAlias(_, generics)
156             | ItemKind::Enum(_, generics)
157             | ItemKind::Struct(_, generics)
158             | ItemKind::Union(_, generics) => generics,
159 
160             ItemKind::Trait(_, _, generics, self_bounds, ..)
161             | ItemKind::TraitAlias(generics, self_bounds) => {
162                 is_trait = Some(self_bounds);
163                 generics
164             }
165             ItemKind::OpaqueTy(OpaqueTy { generics, .. }) => generics,
166             _ => NO_GENERICS,
167         },
168 
169         Node::ForeignItem(item) => match item.kind {
170             ForeignItemKind::Static(..) => NO_GENERICS,
171             ForeignItemKind::Fn(_, _, generics) => generics,
172             ForeignItemKind::Type => NO_GENERICS,
173         },
174 
175         _ => NO_GENERICS,
176     };
177 
178     let generics = tcx.generics_of(def_id);
179     let parent_count = generics.parent_count as u32;
180     let has_own_self = generics.has_self && parent_count == 0;
181 
182     // Below we'll consider the bounds on the type parameters (including `Self`)
183     // and the explicit where-clauses, but to get the full set of predicates
184     // on a trait we must also consider the bounds that follow the trait's name,
185     // like `trait Foo: A + B + C`.
186     if let Some(self_bounds) = is_trait {
187         predicates.extend(
188             icx.astconv()
189                 .compute_bounds(tcx.types.self_param, self_bounds, PredicateFilter::All)
190                 .clauses(),
191         );
192     }
193 
194     // In default impls, we can assume that the self type implements
195     // the trait. So in:
196     //
197     //     default impl Foo for Bar { .. }
198     //
199     // we add a default where clause `Foo: Bar`. We do a similar thing for traits
200     // (see below). Recall that a default impl is not itself an impl, but rather a
201     // set of defaults that can be incorporated into another impl.
202     if let Some(trait_ref) = is_default_impl_trait {
203         predicates.insert((trait_ref.without_const().to_predicate(tcx), tcx.def_span(def_id)));
204     }
205 
206     // Collect the region predicates that were declared inline as
207     // well. In the case of parameters declared on a fn or method, we
208     // have to be careful to only iterate over early-bound regions.
209     let mut index = parent_count
210         + has_own_self as u32
211         + super::early_bound_lifetimes_from_generics(tcx, ast_generics).count() as u32;
212 
213     trace!(?predicates);
214     trace!(?ast_generics);
215     trace!(?generics);
216 
217     // Collect the predicates that were written inline by the user on each
218     // type parameter (e.g., `<T: Foo>`). Also add `ConstArgHasType` predicates
219     // for each const parameter.
220     for param in ast_generics.params {
221         match param.kind {
222             // We already dealt with early bound lifetimes above.
223             GenericParamKind::Lifetime { .. } => (),
224             GenericParamKind::Type { .. } => {
225                 let name = param.name.ident().name;
226                 let param_ty = ty::ParamTy::new(index, name).to_ty(tcx);
227                 index += 1;
228 
229                 let mut bounds = Bounds::default();
230                 // Params are implicitly sized unless a `?Sized` bound is found
231                 icx.astconv().add_implicitly_sized(
232                     &mut bounds,
233                     param_ty,
234                     &[],
235                     Some((param.def_id, ast_generics.predicates)),
236                     param.span,
237                 );
238                 trace!(?bounds);
239                 predicates.extend(bounds.clauses());
240                 trace!(?predicates);
241             }
242             GenericParamKind::Const { .. } => {
243                 let name = param.name.ident().name;
244                 let param_const = ty::ParamConst::new(index, name);
245 
246                 let ct_ty = tcx
247                     .type_of(param.def_id.to_def_id())
248                     .no_bound_vars()
249                     .expect("const parameters cannot be generic");
250 
251                 let ct = ty::Const::new_param(tcx, param_const, ct_ty);
252 
253                 predicates.insert((
254                     ty::ClauseKind::ConstArgHasType(ct, ct_ty).to_predicate(tcx),
255                     param.span,
256                 ));
257 
258                 index += 1;
259             }
260         }
261     }
262 
263     trace!(?predicates);
264     // Add in the bounds that appear in the where-clause.
265     for predicate in ast_generics.predicates {
266         match predicate {
267             hir::WherePredicate::BoundPredicate(bound_pred) => {
268                 let ty = icx.to_ty(bound_pred.bounded_ty);
269                 let bound_vars = icx.tcx.late_bound_vars(bound_pred.hir_id);
270 
271                 // Keep the type around in a dummy predicate, in case of no bounds.
272                 // That way, `where Ty:` is not a complete noop (see #53696) and `Ty`
273                 // is still checked for WF.
274                 if bound_pred.bounds.is_empty() {
275                     if let ty::Param(_) = ty.kind() {
276                         // This is a `where T:`, which can be in the HIR from the
277                         // transformation that moves `?Sized` to `T`'s declaration.
278                         // We can skip the predicate because type parameters are
279                         // trivially WF, but also we *should*, to avoid exposing
280                         // users who never wrote `where Type:,` themselves, to
281                         // compiler/tooling bugs from not handling WF predicates.
282                     } else {
283                         let span = bound_pred.bounded_ty.span;
284                         let predicate = ty::Binder::bind_with_vars(
285                             ty::ClauseKind::WellFormed(ty.into()),
286                             bound_vars,
287                         );
288                         predicates.insert((predicate.to_predicate(tcx), span));
289                     }
290                 }
291 
292                 let mut bounds = Bounds::default();
293                 icx.astconv().add_bounds(
294                     ty,
295                     bound_pred.bounds.iter(),
296                     &mut bounds,
297                     bound_vars,
298                     OnlySelfBounds(false),
299                 );
300                 predicates.extend(bounds.clauses());
301             }
302 
303             hir::WherePredicate::RegionPredicate(region_pred) => {
304                 let r1 = icx.astconv().ast_region_to_region(&region_pred.lifetime, None);
305                 predicates.extend(region_pred.bounds.iter().map(|bound| {
306                     let (r2, span) = match bound {
307                         hir::GenericBound::Outlives(lt) => {
308                             (icx.astconv().ast_region_to_region(lt, None), lt.ident.span)
309                         }
310                         _ => bug!(),
311                     };
312                     let pred = ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(r1, r2))
313                         .to_predicate(icx.tcx);
314                     (pred, span)
315                 }))
316             }
317 
318             hir::WherePredicate::EqPredicate(..) => {
319                 // FIXME(#20041)
320             }
321         }
322     }
323 
324     if tcx.features().generic_const_exprs {
325         predicates.extend(const_evaluatable_predicates_of(tcx, def_id));
326     }
327 
328     let mut predicates: Vec<_> = predicates.into_iter().collect();
329 
330     // Subtle: before we store the predicates into the tcx, we
331     // sort them so that predicates like `T: Foo<Item=U>` come
332     // before uses of `U`. This avoids false ambiguity errors
333     // in trait checking. See `setup_constraining_predicates`
334     // for details.
335     if let Node::Item(&Item { kind: ItemKind::Impl { .. }, .. }) = node {
336         let self_ty = tcx.type_of(def_id).subst_identity();
337         let trait_ref = tcx.impl_trait_ref(def_id).map(ty::EarlyBinder::subst_identity);
338         cgp::setup_constraining_predicates(
339             tcx,
340             &mut predicates,
341             trait_ref,
342             &mut cgp::parameters_for_impl(self_ty, trait_ref),
343         );
344     }
345 
346     // Opaque types duplicate some of their generic parameters.
347     // We create bi-directional Outlives predicates between the original
348     // and the duplicated parameter, to ensure that they do not get out of sync.
349     if let Node::Item(&Item { kind: ItemKind::OpaqueTy(..), .. }) = node {
350         let opaque_ty_id = tcx.hir().parent_id(hir_id);
351         let opaque_ty_node = tcx.hir().get(opaque_ty_id);
352         let Node::Ty(&Ty { kind: TyKind::OpaqueDef(_, lifetimes, _), .. }) = opaque_ty_node else {
353             bug!("unexpected {opaque_ty_node:?}")
354         };
355         debug!(?lifetimes);
356 
357         let lifetime_mapping = std::iter::zip(lifetimes, ast_generics.params)
358             .map(|(arg, dup)| {
359                 let hir::GenericArg::Lifetime(arg) = arg else { bug!() };
360                 (**arg, dup)
361             })
362             .filter(|(_, dup)| matches!(dup.kind, hir::GenericParamKind::Lifetime { .. }))
363             .map(|(lifetime, dup)| (lifetime, (dup.def_id, dup.name.ident().name, dup.span)));
364 
365         compute_bidirectional_outlives_predicates(
366             tcx,
367             def_id,
368             lifetime_mapping,
369             generics,
370             &mut predicates,
371         );
372         debug!(?predicates);
373     }
374 
375     ty::GenericPredicates {
376         parent: generics.parent,
377         predicates: tcx.arena.alloc_from_iter(predicates),
378     }
379 }
380 
381 /// Opaques have duplicated lifetimes and we need to compute bidirectional outlives predicates to
382 /// enforce that these lifetimes stay in sync.
compute_bidirectional_outlives_predicates<'tcx>( tcx: TyCtxt<'tcx>, item_def_id: LocalDefId, lifetime_mapping: impl Iterator<Item = (Lifetime, (LocalDefId, Symbol, Span))>, generics: &Generics, predicates: &mut Vec<(ty::Clause<'tcx>, Span)>, )383 fn compute_bidirectional_outlives_predicates<'tcx>(
384     tcx: TyCtxt<'tcx>,
385     item_def_id: LocalDefId,
386     lifetime_mapping: impl Iterator<Item = (Lifetime, (LocalDefId, Symbol, Span))>,
387     generics: &Generics,
388     predicates: &mut Vec<(ty::Clause<'tcx>, Span)>,
389 ) {
390     let icx = ItemCtxt::new(tcx, item_def_id);
391 
392     for (arg, (dup_def, name, span)) in lifetime_mapping {
393         let orig_region = icx.astconv().ast_region_to_region(&arg, None);
394         if !matches!(orig_region.kind(), ty::ReEarlyBound(..)) {
395             // There is no late-bound lifetime to actually match up here, since the lifetime doesn't
396             // show up in the opaque's parent's substs.
397             continue;
398         }
399 
400         let Some(dup_index) = generics.param_def_id_to_index(icx.tcx, dup_def.to_def_id()) else { bug!() };
401 
402         let dup_region = ty::Region::new_early_bound(
403             tcx,
404             ty::EarlyBoundRegion { def_id: dup_def.to_def_id(), index: dup_index, name },
405         );
406 
407         predicates.push((
408             ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(orig_region, dup_region))
409                 .to_predicate(tcx),
410             span,
411         ));
412 
413         predicates.push((
414             ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(dup_region, orig_region))
415                 .to_predicate(tcx),
416             span,
417         ));
418     }
419 }
420 
const_evaluatable_predicates_of( tcx: TyCtxt<'_>, def_id: LocalDefId, ) -> FxIndexSet<(ty::Clause<'_>, Span)>421 fn const_evaluatable_predicates_of(
422     tcx: TyCtxt<'_>,
423     def_id: LocalDefId,
424 ) -> FxIndexSet<(ty::Clause<'_>, Span)> {
425     struct ConstCollector<'tcx> {
426         tcx: TyCtxt<'tcx>,
427         preds: FxIndexSet<(ty::Clause<'tcx>, Span)>,
428     }
429 
430     impl<'tcx> intravisit::Visitor<'tcx> for ConstCollector<'tcx> {
431         fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
432             let ct = ty::Const::from_anon_const(self.tcx, c.def_id);
433             if let ty::ConstKind::Unevaluated(_) = ct.kind() {
434                 let span = self.tcx.def_span(c.def_id);
435                 self.preds
436                     .insert((ty::ClauseKind::ConstEvaluatable(ct).to_predicate(self.tcx), span));
437             }
438         }
439 
440         fn visit_const_param_default(&mut self, _param: HirId, _ct: &'tcx hir::AnonConst) {
441             // Do not look into const param defaults,
442             // these get checked when they are actually instantiated.
443             //
444             // We do not want the following to error:
445             //
446             //     struct Foo<const N: usize, const M: usize = { N + 1 }>;
447             //     struct Bar<const N: usize>(Foo<N, 3>);
448         }
449     }
450 
451     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
452     let node = tcx.hir().get(hir_id);
453 
454     let mut collector = ConstCollector { tcx, preds: FxIndexSet::default() };
455     if let hir::Node::Item(item) = node && let hir::ItemKind::Impl(impl_) = item.kind {
456         if let Some(of_trait) = &impl_.of_trait {
457             debug!("const_evaluatable_predicates_of({:?}): visit impl trait_ref", def_id);
458             collector.visit_trait_ref(of_trait);
459         }
460 
461         debug!("const_evaluatable_predicates_of({:?}): visit_self_ty", def_id);
462         collector.visit_ty(impl_.self_ty);
463     }
464 
465     if let Some(generics) = node.generics() {
466         debug!("const_evaluatable_predicates_of({:?}): visit_generics", def_id);
467         collector.visit_generics(generics);
468     }
469 
470     if let Some(fn_sig) = tcx.hir().fn_sig_by_hir_id(hir_id) {
471         debug!("const_evaluatable_predicates_of({:?}): visit_fn_decl", def_id);
472         collector.visit_fn_decl(fn_sig.decl);
473     }
474     debug!("const_evaluatable_predicates_of({:?}) = {:?}", def_id, collector.preds);
475 
476     collector.preds
477 }
478 
trait_explicit_predicates_and_bounds( tcx: TyCtxt<'_>, def_id: LocalDefId, ) -> ty::GenericPredicates<'_>479 pub(super) fn trait_explicit_predicates_and_bounds(
480     tcx: TyCtxt<'_>,
481     def_id: LocalDefId,
482 ) -> ty::GenericPredicates<'_> {
483     assert_eq!(tcx.def_kind(def_id), DefKind::Trait);
484     gather_explicit_predicates_of(tcx, def_id)
485 }
486 
explicit_predicates_of<'tcx>( tcx: TyCtxt<'tcx>, def_id: LocalDefId, ) -> ty::GenericPredicates<'tcx>487 pub(super) fn explicit_predicates_of<'tcx>(
488     tcx: TyCtxt<'tcx>,
489     def_id: LocalDefId,
490 ) -> ty::GenericPredicates<'tcx> {
491     let def_kind = tcx.def_kind(def_id);
492     if let DefKind::Trait = def_kind {
493         // Remove bounds on associated types from the predicates, they will be
494         // returned by `explicit_item_bounds`.
495         let predicates_and_bounds = tcx.trait_explicit_predicates_and_bounds(def_id);
496         let trait_identity_substs = InternalSubsts::identity_for_item(tcx, def_id);
497 
498         let is_assoc_item_ty = |ty: Ty<'tcx>| {
499             // For a predicate from a where clause to become a bound on an
500             // associated type:
501             // * It must use the identity substs of the item.
502             //   * We're in the scope of the trait, so we can't name any
503             //     parameters of the GAT. That means that all we need to
504             //     check are that the substs of the projection are the
505             //     identity substs of the trait.
506             // * It must be an associated type for this trait (*not* a
507             //   supertrait).
508             if let ty::Alias(ty::Projection, projection) = ty.kind() {
509                 projection.substs == trait_identity_substs
510                     // FIXME(return_type_notation): This check should be more robust
511                     && !tcx.is_impl_trait_in_trait(projection.def_id)
512                     && tcx.associated_item(projection.def_id).container_id(tcx)
513                         == def_id.to_def_id()
514             } else {
515                 false
516             }
517         };
518 
519         let predicates: Vec<_> = predicates_and_bounds
520             .predicates
521             .iter()
522             .copied()
523             .filter(|(pred, _)| match pred.kind().skip_binder() {
524                 ty::ClauseKind::Trait(tr) => !is_assoc_item_ty(tr.self_ty()),
525                 ty::ClauseKind::Projection(proj) => !is_assoc_item_ty(proj.projection_ty.self_ty()),
526                 ty::ClauseKind::TypeOutlives(outlives) => !is_assoc_item_ty(outlives.0),
527                 _ => true,
528             })
529             .collect();
530         if predicates.len() == predicates_and_bounds.predicates.len() {
531             predicates_and_bounds
532         } else {
533             ty::GenericPredicates {
534                 parent: predicates_and_bounds.parent,
535                 predicates: tcx.arena.alloc_slice(&predicates),
536             }
537         }
538     } else {
539         if matches!(def_kind, DefKind::AnonConst) && tcx.features().generic_const_exprs {
540             let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
541             let parent_def_id = tcx.hir().get_parent_item(hir_id);
542 
543             if let Some(defaulted_param_def_id) =
544                 tcx.hir().opt_const_param_default_param_def_id(hir_id)
545             {
546                 // In `generics_of` we set the generics' parent to be our parent's parent which means that
547                 // we lose out on the predicates of our actual parent if we dont return those predicates here.
548                 // (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
549                 //
550                 // struct Foo<T, const N: usize = { <T as Trait>::ASSOC }>(T) where T: Trait;
551                 //        ^^^                     ^^^^^^^^^^^^^^^^^^^^^^^ the def id we are calling
552                 //        ^^^                                             explicit_predicates_of on
553                 //        parent item we dont have set as the
554                 //        parent of generics returned by `generics_of`
555                 //
556                 // In the above code we want the anon const to have predicates in its param env for `T: Trait`
557                 // and we would be calling `explicit_predicates_of(Foo)` here
558                 let parent_preds = tcx.explicit_predicates_of(parent_def_id);
559 
560                 // If we dont filter out `ConstArgHasType` predicates then every single defaulted const parameter
561                 // will ICE because of #106994. FIXME(generic_const_exprs): remove this when a more general solution
562                 // to #106994 is implemented.
563                 let filtered_predicates = parent_preds
564                     .predicates
565                     .into_iter()
566                     .filter(|(pred, _)| {
567                         if let ty::ClauseKind::ConstArgHasType(ct, _) = pred.kind().skip_binder() {
568                             match ct.kind() {
569                                 ty::ConstKind::Param(param_const) => {
570                                     let defaulted_param_idx = tcx
571                                         .generics_of(parent_def_id)
572                                         .param_def_id_to_index[&defaulted_param_def_id.to_def_id()];
573                                     param_const.index < defaulted_param_idx
574                                 }
575                                 _ => bug!(
576                                     "`ConstArgHasType` in `predicates_of`\
577                                  that isn't a `Param` const"
578                                 ),
579                             }
580                         } else {
581                             true
582                         }
583                     })
584                     .cloned();
585                 return GenericPredicates {
586                     parent: parent_preds.parent,
587                     predicates: { tcx.arena.alloc_from_iter(filtered_predicates) },
588                 };
589             }
590 
591             let parent_def_kind = tcx.def_kind(parent_def_id);
592             if matches!(parent_def_kind, DefKind::OpaqueTy) {
593                 // In `instantiate_identity` we inherit the predicates of our parent.
594                 // However, opaque types do not have a parent (see `gather_explicit_predicates_of`), which means
595                 // that we lose out on the predicates of our actual parent if we dont return those predicates here.
596                 //
597                 //
598                 // fn foo<T: Trait>() -> impl Iterator<Output = Another<{ <T as Trait>::ASSOC }> > { todo!() }
599                 //                                                        ^^^^^^^^^^^^^^^^^^^ the def id we are calling
600                 //                                                                            explicit_predicates_of on
601                 //
602                 // In the above code we want the anon const to have predicates in its param env for `T: Trait`.
603                 // However, the anon const cannot inherit predicates from its parent since it's opaque.
604                 //
605                 // To fix this, we call `explicit_predicates_of` directly on `foo`, the parent's parent.
606 
607                 // In the above example this is `foo::{opaque#0}` or `impl Iterator`
608                 let parent_hir_id = tcx.hir().local_def_id_to_hir_id(parent_def_id.def_id);
609 
610                 // In the above example this is the function `foo`
611                 let item_def_id = tcx.hir().get_parent_item(parent_hir_id);
612 
613                 // In the above code example we would be calling `explicit_predicates_of(foo)` here
614                 return tcx.explicit_predicates_of(item_def_id);
615             }
616         }
617         gather_explicit_predicates_of(tcx, def_id)
618     }
619 }
620 
621 /// Ensures that the super-predicates of the trait with a `DefId`
622 /// of `trait_def_id` are converted and stored. This also ensures that
623 /// the transitive super-predicates are converted.
super_predicates_of( tcx: TyCtxt<'_>, trait_def_id: LocalDefId, ) -> ty::GenericPredicates<'_>624 pub(super) fn super_predicates_of(
625     tcx: TyCtxt<'_>,
626     trait_def_id: LocalDefId,
627 ) -> ty::GenericPredicates<'_> {
628     implied_predicates_with_filter(tcx, trait_def_id.to_def_id(), PredicateFilter::SelfOnly)
629 }
630 
super_predicates_that_define_assoc_item( tcx: TyCtxt<'_>, (trait_def_id, assoc_name): (DefId, Ident), ) -> ty::GenericPredicates<'_>631 pub(super) fn super_predicates_that_define_assoc_item(
632     tcx: TyCtxt<'_>,
633     (trait_def_id, assoc_name): (DefId, Ident),
634 ) -> ty::GenericPredicates<'_> {
635     implied_predicates_with_filter(tcx, trait_def_id, PredicateFilter::SelfThatDefines(assoc_name))
636 }
637 
implied_predicates_of( tcx: TyCtxt<'_>, trait_def_id: LocalDefId, ) -> ty::GenericPredicates<'_>638 pub(super) fn implied_predicates_of(
639     tcx: TyCtxt<'_>,
640     trait_def_id: LocalDefId,
641 ) -> ty::GenericPredicates<'_> {
642     implied_predicates_with_filter(
643         tcx,
644         trait_def_id.to_def_id(),
645         if tcx.is_trait_alias(trait_def_id.to_def_id()) {
646             PredicateFilter::All
647         } else {
648             PredicateFilter::SelfAndAssociatedTypeBounds
649         },
650     )
651 }
652 
653 /// Ensures that the super-predicates of the trait with a `DefId`
654 /// of `trait_def_id` are converted and stored. This also ensures that
655 /// the transitive super-predicates are converted.
implied_predicates_with_filter( tcx: TyCtxt<'_>, trait_def_id: DefId, filter: PredicateFilter, ) -> ty::GenericPredicates<'_>656 pub(super) fn implied_predicates_with_filter(
657     tcx: TyCtxt<'_>,
658     trait_def_id: DefId,
659     filter: PredicateFilter,
660 ) -> ty::GenericPredicates<'_> {
661     let Some(trait_def_id) = trait_def_id.as_local() else {
662         // if `assoc_name` is None, then the query should've been redirected to an
663         // external provider
664         assert!(matches!(filter, PredicateFilter::SelfThatDefines(_)));
665         return tcx.super_predicates_of(trait_def_id);
666     };
667 
668     let trait_hir_id = tcx.hir().local_def_id_to_hir_id(trait_def_id);
669 
670     let Node::Item(item) = tcx.hir().get(trait_hir_id) else {
671         bug!("trait_node_id {} is not an item", trait_hir_id);
672     };
673 
674     let (generics, bounds) = match item.kind {
675         hir::ItemKind::Trait(.., generics, supertraits, _) => (generics, supertraits),
676         hir::ItemKind::TraitAlias(generics, supertraits) => (generics, supertraits),
677         _ => span_bug!(item.span, "super_predicates invoked on non-trait"),
678     };
679 
680     let icx = ItemCtxt::new(tcx, trait_def_id);
681 
682     let self_param_ty = tcx.types.self_param;
683     let superbounds = icx.astconv().compute_bounds(self_param_ty, bounds, filter);
684 
685     let where_bounds_that_match = icx.type_parameter_bounds_in_generics(
686         generics,
687         item.owner_id.def_id,
688         self_param_ty,
689         filter,
690     );
691 
692     // Combine the two lists to form the complete set of superbounds:
693     let implied_bounds =
694         &*tcx.arena.alloc_from_iter(superbounds.clauses().chain(where_bounds_that_match));
695     debug!(?implied_bounds);
696 
697     // Now require that immediate supertraits are converted, which will, in
698     // turn, reach indirect supertraits, so we detect cycles now instead of
699     // overflowing during elaboration.
700     if matches!(filter, PredicateFilter::SelfOnly) {
701         for &(pred, span) in implied_bounds {
702             debug!("superbound: {:?}", pred);
703             if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
704                 && bound.polarity == ty::ImplPolarity::Positive
705             {
706                 tcx.at(span).super_predicates_of(bound.def_id());
707             }
708         }
709     }
710 
711     ty::GenericPredicates { parent: None, predicates: implied_bounds }
712 }
713 
714 /// Returns the predicates defined on `item_def_id` of the form
715 /// `X: Foo` where `X` is the type parameter `def_id`.
716 #[instrument(level = "trace", skip(tcx))]
type_param_predicates( tcx: TyCtxt<'_>, (item_def_id, def_id, assoc_name): (LocalDefId, LocalDefId, Ident), ) -> ty::GenericPredicates<'_>717 pub(super) fn type_param_predicates(
718     tcx: TyCtxt<'_>,
719     (item_def_id, def_id, assoc_name): (LocalDefId, LocalDefId, Ident),
720 ) -> ty::GenericPredicates<'_> {
721     use rustc_hir::*;
722     use rustc_middle::ty::Ty;
723 
724     // In the AST, bounds can derive from two places. Either
725     // written inline like `<T: Foo>` or in a where-clause like
726     // `where T: Foo`.
727 
728     let param_id = tcx.hir().local_def_id_to_hir_id(def_id);
729     let param_owner = tcx.hir().ty_param_owner(def_id);
730     let generics = tcx.generics_of(param_owner);
731     let index = generics.param_def_id_to_index[&def_id.to_def_id()];
732     let ty = Ty::new_param(tcx, index, tcx.hir().ty_param_name(def_id));
733 
734     // Don't look for bounds where the type parameter isn't in scope.
735     let parent = if item_def_id == param_owner {
736         None
737     } else {
738         tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local())
739     };
740 
741     let mut result = parent
742         .map(|parent| {
743             let icx = ItemCtxt::new(tcx, parent);
744             icx.get_type_parameter_bounds(DUMMY_SP, def_id, assoc_name)
745         })
746         .unwrap_or_default();
747     let mut extend = None;
748 
749     let item_hir_id = tcx.hir().local_def_id_to_hir_id(item_def_id);
750     let ast_generics = match tcx.hir().get(item_hir_id) {
751         Node::TraitItem(item) => &item.generics,
752 
753         Node::ImplItem(item) => &item.generics,
754 
755         Node::Item(item) => {
756             match item.kind {
757                 ItemKind::Fn(.., generics, _)
758                 | ItemKind::Impl(&hir::Impl { generics, .. })
759                 | ItemKind::TyAlias(_, generics)
760                 | ItemKind::OpaqueTy(&OpaqueTy {
761                     generics,
762                     origin: hir::OpaqueTyOrigin::TyAlias { .. },
763                     ..
764                 })
765                 | ItemKind::Enum(_, generics)
766                 | ItemKind::Struct(_, generics)
767                 | ItemKind::Union(_, generics) => generics,
768                 ItemKind::Trait(_, _, generics, ..) => {
769                     // Implied `Self: Trait` and supertrait bounds.
770                     if param_id == item_hir_id {
771                         let identity_trait_ref =
772                             ty::TraitRef::identity(tcx, item_def_id.to_def_id());
773                         extend =
774                             Some((identity_trait_ref.without_const().to_predicate(tcx), item.span));
775                     }
776                     generics
777                 }
778                 _ => return result,
779             }
780         }
781 
782         Node::ForeignItem(item) => match item.kind {
783             ForeignItemKind::Fn(_, _, generics) => generics,
784             _ => return result,
785         },
786 
787         _ => return result,
788     };
789 
790     let icx = ItemCtxt::new(tcx, item_def_id);
791     let extra_predicates = extend.into_iter().chain(
792         icx.type_parameter_bounds_in_generics(
793             ast_generics,
794             def_id,
795             ty,
796             PredicateFilter::SelfThatDefines(assoc_name),
797         )
798         .into_iter()
799         .filter(|(predicate, _)| match predicate.kind().skip_binder() {
800             ty::ClauseKind::Trait(data) => data.self_ty().is_param(index),
801             _ => false,
802         }),
803     );
804     result.predicates =
805         tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(extra_predicates));
806     result
807 }
808 
809 impl<'tcx> ItemCtxt<'tcx> {
810     /// Finds bounds from `hir::Generics`. This requires scanning through the
811     /// AST. We do this to avoid having to convert *all* the bounds, which
812     /// would create artificial cycles. Instead, we can only convert the
813     /// bounds for a type parameter `X` if `X::Foo` is used.
814     #[instrument(level = "trace", skip(self, ast_generics))]
type_parameter_bounds_in_generics( &self, ast_generics: &'tcx hir::Generics<'tcx>, param_def_id: LocalDefId, ty: Ty<'tcx>, filter: PredicateFilter, ) -> Vec<(ty::Clause<'tcx>, Span)>815     fn type_parameter_bounds_in_generics(
816         &self,
817         ast_generics: &'tcx hir::Generics<'tcx>,
818         param_def_id: LocalDefId,
819         ty: Ty<'tcx>,
820         filter: PredicateFilter,
821     ) -> Vec<(ty::Clause<'tcx>, Span)> {
822         let mut bounds = Bounds::default();
823 
824         for predicate in ast_generics.predicates {
825             let hir::WherePredicate::BoundPredicate(predicate) = predicate else {
826                 continue;
827             };
828 
829             let (only_self_bounds, assoc_name) = match filter {
830                 PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
831                     (OnlySelfBounds(false), None)
832                 }
833                 PredicateFilter::SelfOnly => (OnlySelfBounds(true), None),
834                 PredicateFilter::SelfThatDefines(assoc_name) => {
835                     (OnlySelfBounds(true), Some(assoc_name))
836                 }
837             };
838 
839             // Subtle: If we're collecting `SelfAndAssociatedTypeBounds`, then we
840             // want to only consider predicates with `Self: ...`, but we don't want
841             // `OnlySelfBounds(true)` since we want to collect the nested associated
842             // type bound as well.
843             let bound_ty = if predicate.is_param_bound(param_def_id.to_def_id()) {
844                 ty
845             } else if matches!(filter, PredicateFilter::All) {
846                 self.to_ty(predicate.bounded_ty)
847             } else {
848                 continue;
849             };
850 
851             let bound_vars = self.tcx.late_bound_vars(predicate.hir_id);
852             self.astconv().add_bounds(
853                 bound_ty,
854                 predicate.bounds.iter().filter(|bound| {
855                     assoc_name
856                         .map_or(true, |assoc_name| self.bound_defines_assoc_item(bound, assoc_name))
857                 }),
858                 &mut bounds,
859                 bound_vars,
860                 only_self_bounds,
861             );
862         }
863 
864         bounds.clauses().collect()
865     }
866 
867     #[instrument(level = "trace", skip(self))]
bound_defines_assoc_item(&self, b: &hir::GenericBound<'_>, assoc_name: Ident) -> bool868     fn bound_defines_assoc_item(&self, b: &hir::GenericBound<'_>, assoc_name: Ident) -> bool {
869         match b {
870             hir::GenericBound::Trait(poly_trait_ref, _) => {
871                 let trait_ref = &poly_trait_ref.trait_ref;
872                 if let Some(trait_did) = trait_ref.trait_def_id() {
873                     self.tcx.trait_may_define_assoc_item(trait_did, assoc_name)
874                 } else {
875                     false
876                 }
877             }
878             _ => false,
879         }
880     }
881 }
882