rustc_hir_analysis/collect/
predicates_of.rs

1use std::assert_matches::assert_matches;
2
3use hir::Node;
4use rustc_data_structures::fx::FxIndexSet;
5use rustc_hir as hir;
6use rustc_hir::def::DefKind;
7use rustc_hir::def_id::{DefId, LocalDefId};
8use rustc_middle::ty::{
9    self, GenericPredicates, ImplTraitInTraitData, Ty, TyCtxt, TypeVisitable, TypeVisitor, Upcast,
10};
11use rustc_middle::{bug, span_bug};
12use rustc_span::{DUMMY_SP, Ident, Span};
13use tracing::{debug, instrument, trace};
14
15use super::item_bounds::explicit_item_bounds_with_filter;
16use crate::collect::ItemCtxt;
17use crate::constrained_generic_params as cgp;
18use crate::delegation::inherit_predicates_for_delegation_item;
19use crate::hir_ty_lowering::{HirTyLowerer, PredicateFilter, RegionInferReason};
20
21/// Returns a list of all type predicates (explicit and implicit) for the definition with
22/// ID `def_id`. This includes all predicates returned by `explicit_predicates_of`, plus
23/// inferred constraints concerning which regions outlive other regions.
24#[instrument(level = "debug", skip(tcx))]
25pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
26    let mut result = tcx.explicit_predicates_of(def_id);
27    debug!("predicates_of: explicit_predicates_of({:?}) = {:?}", def_id, result);
28
29    let inferred_outlives = tcx.inferred_outlives_of(def_id);
30    if !inferred_outlives.is_empty() {
31        debug!("predicates_of: inferred_outlives_of({:?}) = {:?}", def_id, inferred_outlives,);
32        let inferred_outlives_iter =
33            inferred_outlives.iter().map(|(clause, span)| ((*clause).upcast(tcx), *span));
34        if result.predicates.is_empty() {
35            result.predicates = tcx.arena.alloc_from_iter(inferred_outlives_iter);
36        } else {
37            result.predicates = tcx.arena.alloc_from_iter(
38                result.predicates.into_iter().copied().chain(inferred_outlives_iter),
39            );
40        }
41    }
42
43    if tcx.is_trait(def_id) {
44        // For traits, add `Self: Trait` predicate. This is
45        // not part of the predicates that a user writes, but it
46        // is something that one must prove in order to invoke a
47        // method or project an associated type.
48        //
49        // In the chalk setup, this predicate is not part of the
50        // "predicates" for a trait item. But it is useful in
51        // rustc because if you directly (e.g.) invoke a trait
52        // method like `Trait::method(...)`, you must naturally
53        // prove that the trait applies to the types that were
54        // used, and adding the predicate into this list ensures
55        // that this is done.
56        //
57        // We use a DUMMY_SP here as a way to signal trait bounds that come
58        // from the trait itself that *shouldn't* be shown as the source of
59        // an obligation and instead be skipped. Otherwise we'd use
60        // `tcx.def_span(def_id);`
61        let span = DUMMY_SP;
62
63        result.predicates = tcx.arena.alloc_from_iter(
64            result
65                .predicates
66                .iter()
67                .copied()
68                .chain(std::iter::once((ty::TraitRef::identity(tcx, def_id).upcast(tcx), span))),
69        );
70    }
71
72    debug!("predicates_of({:?}) = {:?}", def_id, result);
73    result
74}
75
76/// Returns a list of user-specified type predicates for the definition with ID `def_id`.
77/// N.B., this does not include any implied/inferred constraints.
78#[instrument(level = "trace", skip(tcx), ret)]
79fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::GenericPredicates<'_> {
80    use rustc_hir::*;
81
82    match tcx.opt_rpitit_info(def_id.to_def_id()) {
83        Some(ImplTraitInTraitData::Trait { fn_def_id, .. }) => {
84            let mut predicates = Vec::new();
85
86            // RPITITs should inherit the predicates of their parent. This is
87            // both to ensure that the RPITITs are only instantiated when the
88            // parent predicates would hold, and also so that the param-env
89            // inherits these predicates as assumptions.
90            let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
91            predicates
92                .extend(tcx.explicit_predicates_of(fn_def_id).instantiate_own(tcx, identity_args));
93
94            // We also install bidirectional outlives predicates for the RPITIT
95            // to keep the duplicates lifetimes from opaque lowering in sync.
96            // We only need to compute bidirectional outlives for the duplicated
97            // opaque lifetimes, which explains the slicing below.
98            compute_bidirectional_outlives_predicates(
99                tcx,
100                &tcx.generics_of(def_id.to_def_id()).own_params
101                    [tcx.generics_of(fn_def_id).own_params.len()..],
102                &mut predicates,
103            );
104
105            return ty::GenericPredicates {
106                parent: Some(tcx.parent(def_id.to_def_id())),
107                predicates: tcx.arena.alloc_from_iter(predicates),
108            };
109        }
110
111        Some(ImplTraitInTraitData::Impl { fn_def_id }) => {
112            let assoc_item = tcx.associated_item(def_id);
113            let trait_assoc_predicates =
114                tcx.explicit_predicates_of(assoc_item.trait_item_def_id.unwrap());
115
116            let impl_assoc_identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
117            let impl_def_id = tcx.parent(fn_def_id);
118            let impl_trait_ref_args =
119                tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity().args;
120
121            let impl_assoc_args =
122                impl_assoc_identity_args.rebase_onto(tcx, impl_def_id, impl_trait_ref_args);
123
124            let impl_predicates = trait_assoc_predicates.instantiate_own(tcx, impl_assoc_args);
125
126            return ty::GenericPredicates {
127                parent: Some(impl_def_id),
128                predicates: tcx.arena.alloc_from_iter(impl_predicates),
129            };
130        }
131
132        None => {}
133    }
134
135    let hir_id = tcx.local_def_id_to_hir_id(def_id);
136    let node = tcx.hir_node(hir_id);
137
138    if let Some(sig) = node.fn_sig()
139        && let Some(sig_id) = sig.decl.opt_delegation_sig_id()
140    {
141        return inherit_predicates_for_delegation_item(tcx, def_id, sig_id);
142    }
143
144    let mut is_trait = None;
145    let mut is_default_impl_trait = None;
146
147    let icx = ItemCtxt::new(tcx, def_id);
148
149    const NO_GENERICS: &hir::Generics<'_> = hir::Generics::empty();
150
151    // We use an `IndexSet` to preserve order of insertion.
152    // Preserving the order of insertion is important here so as not to break UI tests.
153    let mut predicates: FxIndexSet<(ty::Clause<'_>, Span)> = FxIndexSet::default();
154
155    let hir_generics = node.generics().unwrap_or(NO_GENERICS);
156    if let Node::Item(item) = node {
157        match item.kind {
158            ItemKind::Impl(impl_) => {
159                if impl_.defaultness.is_default() {
160                    is_default_impl_trait = tcx
161                        .impl_trait_ref(def_id)
162                        .map(|t| ty::Binder::dummy(t.instantiate_identity()));
163                }
164            }
165
166            ItemKind::Trait(_, _, _, _, self_bounds, ..)
167            | ItemKind::TraitAlias(_, _, self_bounds) => {
168                is_trait = Some(self_bounds);
169            }
170            _ => {}
171        }
172    };
173
174    let generics = tcx.generics_of(def_id);
175
176    // Below we'll consider the bounds on the type parameters (including `Self`)
177    // and the explicit where-clauses, but to get the full set of predicates
178    // on a trait we must also consider the bounds that follow the trait's name,
179    // like `trait Foo: A + B + C`.
180    if let Some(self_bounds) = is_trait {
181        let mut bounds = Vec::new();
182        icx.lowerer().lower_bounds(
183            tcx.types.self_param,
184            self_bounds,
185            &mut bounds,
186            ty::List::empty(),
187            PredicateFilter::All,
188        );
189        predicates.extend(bounds);
190    }
191
192    // In default impls, we can assume that the self type implements
193    // the trait. So in:
194    //
195    //     default impl Foo for Bar { .. }
196    //
197    // we add a default where clause `Bar: Foo`. We do a similar thing for traits
198    // (see below). Recall that a default impl is not itself an impl, but rather a
199    // set of defaults that can be incorporated into another impl.
200    if let Some(trait_ref) = is_default_impl_trait {
201        predicates.insert((trait_ref.upcast(tcx), tcx.def_span(def_id)));
202    }
203
204    // Add implicit predicates that should be treated as if the user has written them,
205    // including the implicit `T: Sized` for all generic parameters, and `ConstArgHasType`
206    // for const params.
207    for param in hir_generics.params {
208        match param.kind {
209            GenericParamKind::Lifetime { .. } => (),
210            GenericParamKind::Type { .. } => {
211                let param_ty = icx.lowerer().lower_ty_param(param.hir_id);
212                let mut bounds = Vec::new();
213                // Params are implicitly sized unless a `?Sized` bound is found
214                icx.lowerer().add_sized_bound(
215                    &mut bounds,
216                    param_ty,
217                    &[],
218                    Some((param.def_id, hir_generics.predicates)),
219                    param.span,
220                );
221                trace!(?bounds);
222                predicates.extend(bounds);
223                trace!(?predicates);
224            }
225            hir::GenericParamKind::Const { .. } => {
226                let param_def_id = param.def_id.to_def_id();
227                let ct_ty = tcx.type_of(param_def_id).instantiate_identity();
228                let ct = icx.lowerer().lower_const_param(param_def_id, param.hir_id);
229                predicates
230                    .insert((ty::ClauseKind::ConstArgHasType(ct, ct_ty).upcast(tcx), param.span));
231            }
232        }
233    }
234
235    trace!(?predicates);
236    // Add inline `<T: Foo>` bounds and bounds in the where clause.
237    for predicate in hir_generics.predicates {
238        match predicate.kind {
239            hir::WherePredicateKind::BoundPredicate(bound_pred) => {
240                let ty = icx.lowerer().lower_ty_maybe_return_type_notation(bound_pred.bounded_ty);
241
242                let bound_vars = tcx.late_bound_vars(predicate.hir_id);
243                // Keep the type around in a dummy predicate, in case of no bounds.
244                // That way, `where Ty:` is not a complete noop (see #53696) and `Ty`
245                // is still checked for WF.
246                if bound_pred.bounds.is_empty() {
247                    if let ty::Param(_) = ty.kind() {
248                        // This is a `where T:`, which can be in the HIR from the
249                        // transformation that moves `?Sized` to `T`'s declaration.
250                        // We can skip the predicate because type parameters are
251                        // trivially WF, but also we *should*, to avoid exposing
252                        // users who never wrote `where Type:,` themselves, to
253                        // compiler/tooling bugs from not handling WF predicates.
254                    } else {
255                        let span = bound_pred.bounded_ty.span;
256                        let predicate = ty::Binder::bind_with_vars(
257                            ty::ClauseKind::WellFormed(ty.into()),
258                            bound_vars,
259                        );
260                        predicates.insert((predicate.upcast(tcx), span));
261                    }
262                }
263
264                let mut bounds = Vec::new();
265                icx.lowerer().lower_bounds(
266                    ty,
267                    bound_pred.bounds,
268                    &mut bounds,
269                    bound_vars,
270                    PredicateFilter::All,
271                );
272                predicates.extend(bounds);
273            }
274
275            hir::WherePredicateKind::RegionPredicate(region_pred) => {
276                let r1 = icx
277                    .lowerer()
278                    .lower_lifetime(region_pred.lifetime, RegionInferReason::RegionPredicate);
279                predicates.extend(region_pred.bounds.iter().map(|bound| {
280                    let (r2, span) = match bound {
281                        hir::GenericBound::Outlives(lt) => (
282                            icx.lowerer().lower_lifetime(lt, RegionInferReason::RegionPredicate),
283                            lt.ident.span,
284                        ),
285                        bound => {
286                            span_bug!(
287                                bound.span(),
288                                "lifetime param bounds must be outlives, but found {bound:?}"
289                            )
290                        }
291                    };
292                    let pred =
293                        ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(r1, r2)).upcast(tcx);
294                    (pred, span)
295                }))
296            }
297
298            hir::WherePredicateKind::EqPredicate(..) => {
299                // FIXME(#20041)
300            }
301        }
302    }
303
304    if tcx.features().generic_const_exprs() {
305        predicates.extend(const_evaluatable_predicates_of(tcx, def_id, &predicates));
306    }
307
308    let mut predicates: Vec<_> = predicates.into_iter().collect();
309
310    // Subtle: before we store the predicates into the tcx, we
311    // sort them so that predicates like `T: Foo<Item=U>` come
312    // before uses of `U`. This avoids false ambiguity errors
313    // in trait checking. See `setup_constraining_predicates`
314    // for details.
315    if let Node::Item(&Item { kind: ItemKind::Impl { .. }, .. }) = node {
316        let self_ty = tcx.type_of(def_id).instantiate_identity();
317        let trait_ref = tcx.impl_trait_ref(def_id).map(ty::EarlyBinder::instantiate_identity);
318        cgp::setup_constraining_predicates(
319            tcx,
320            &mut predicates,
321            trait_ref,
322            &mut cgp::parameters_for_impl(tcx, self_ty, trait_ref),
323        );
324    }
325
326    // Opaque types duplicate some of their generic parameters.
327    // We create bi-directional Outlives predicates between the original
328    // and the duplicated parameter, to ensure that they do not get out of sync.
329    if let Node::OpaqueTy(..) = node {
330        compute_bidirectional_outlives_predicates(tcx, &generics.own_params, &mut predicates);
331        debug!(?predicates);
332    }
333
334    ty::GenericPredicates {
335        parent: generics.parent,
336        predicates: tcx.arena.alloc_from_iter(predicates),
337    }
338}
339
340/// Opaques have duplicated lifetimes and we need to compute bidirectional outlives predicates to
341/// enforce that these lifetimes stay in sync.
342fn compute_bidirectional_outlives_predicates<'tcx>(
343    tcx: TyCtxt<'tcx>,
344    opaque_own_params: &[ty::GenericParamDef],
345    predicates: &mut Vec<(ty::Clause<'tcx>, Span)>,
346) {
347    for param in opaque_own_params {
348        let orig_lifetime = tcx.map_opaque_lifetime_to_parent_lifetime(param.def_id.expect_local());
349        if let ty::ReEarlyParam(..) = *orig_lifetime {
350            let dup_lifetime = ty::Region::new_early_param(
351                tcx,
352                ty::EarlyParamRegion { index: param.index, name: param.name },
353            );
354            let span = tcx.def_span(param.def_id);
355            predicates.push((
356                ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(orig_lifetime, dup_lifetime))
357                    .upcast(tcx),
358                span,
359            ));
360            predicates.push((
361                ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(dup_lifetime, orig_lifetime))
362                    .upcast(tcx),
363                span,
364            ));
365        }
366    }
367}
368
369#[instrument(level = "debug", skip(tcx, predicates), ret)]
370fn const_evaluatable_predicates_of<'tcx>(
371    tcx: TyCtxt<'tcx>,
372    def_id: LocalDefId,
373    predicates: &FxIndexSet<(ty::Clause<'tcx>, Span)>,
374) -> FxIndexSet<(ty::Clause<'tcx>, Span)> {
375    struct ConstCollector<'tcx> {
376        tcx: TyCtxt<'tcx>,
377        preds: FxIndexSet<(ty::Clause<'tcx>, Span)>,
378    }
379
380    fn is_const_param_default(tcx: TyCtxt<'_>, def: LocalDefId) -> bool {
381        let hir_id = tcx.local_def_id_to_hir_id(def);
382        let (_, parent_node) = tcx
383            .hir_parent_iter(hir_id)
384            .skip_while(|(_, n)| matches!(n, Node::ConstArg(..)))
385            .next()
386            .unwrap();
387        matches!(
388            parent_node,
389            Node::GenericParam(hir::GenericParam { kind: hir::GenericParamKind::Const { .. }, .. })
390        )
391    }
392
393    impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ConstCollector<'tcx> {
394        fn visit_const(&mut self, c: ty::Const<'tcx>) {
395            if let ty::ConstKind::Unevaluated(uv) = c.kind() {
396                if is_const_param_default(self.tcx, uv.def.expect_local()) {
397                    // Do not look into const param defaults,
398                    // these get checked when they are actually instantiated.
399                    //
400                    // We do not want the following to error:
401                    //
402                    //     struct Foo<const N: usize, const M: usize = { N + 1 }>;
403                    //     struct Bar<const N: usize>(Foo<N, 3>);
404                    return;
405                }
406
407                let span = self.tcx.def_span(uv.def);
408                self.preds.insert((ty::ClauseKind::ConstEvaluatable(c).upcast(self.tcx), span));
409            }
410        }
411    }
412
413    let hir_id = tcx.local_def_id_to_hir_id(def_id);
414    let node = tcx.hir_node(hir_id);
415
416    let mut collector = ConstCollector { tcx, preds: FxIndexSet::default() };
417
418    for (clause, _sp) in predicates {
419        clause.visit_with(&mut collector);
420    }
421
422    if let hir::Node::Item(item) = node
423        && let hir::ItemKind::Impl(_) = item.kind
424    {
425        if let Some(of_trait) = tcx.impl_trait_ref(def_id) {
426            debug!("visit impl trait_ref");
427            of_trait.instantiate_identity().visit_with(&mut collector);
428        }
429
430        debug!("visit self_ty");
431        let self_ty = tcx.type_of(def_id);
432        self_ty.instantiate_identity().visit_with(&mut collector);
433    }
434
435    if let Some(_) = tcx.hir_fn_sig_by_hir_id(hir_id) {
436        debug!("visit fn sig");
437        let fn_sig = tcx.fn_sig(def_id);
438        let fn_sig = fn_sig.instantiate_identity();
439        debug!(?fn_sig);
440        fn_sig.visit_with(&mut collector);
441    }
442
443    collector.preds
444}
445
446pub(super) fn trait_explicit_predicates_and_bounds(
447    tcx: TyCtxt<'_>,
448    def_id: LocalDefId,
449) -> ty::GenericPredicates<'_> {
450    assert_eq!(tcx.def_kind(def_id), DefKind::Trait);
451    gather_explicit_predicates_of(tcx, def_id)
452}
453
454pub(super) fn explicit_predicates_of<'tcx>(
455    tcx: TyCtxt<'tcx>,
456    def_id: LocalDefId,
457) -> ty::GenericPredicates<'tcx> {
458    let def_kind = tcx.def_kind(def_id);
459    if let DefKind::Trait = def_kind {
460        // Remove bounds on associated types from the predicates, they will be
461        // returned by `explicit_item_bounds`.
462        let predicates_and_bounds = tcx.trait_explicit_predicates_and_bounds(def_id);
463        let trait_identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
464
465        let is_assoc_item_ty = |ty: Ty<'tcx>| {
466            // For a predicate from a where clause to become a bound on an
467            // associated type:
468            // * It must use the identity args of the item.
469            //   * We're in the scope of the trait, so we can't name any
470            //     parameters of the GAT. That means that all we need to
471            //     check are that the args of the projection are the
472            //     identity args of the trait.
473            // * It must be an associated type for this trait (*not* a
474            //   supertrait).
475            if let ty::Alias(ty::Projection, projection) = ty.kind() {
476                projection.args == trait_identity_args
477                    // FIXME(return_type_notation): This check should be more robust
478                    && !tcx.is_impl_trait_in_trait(projection.def_id)
479                    && tcx.associated_item(projection.def_id).container_id(tcx)
480                        == def_id.to_def_id()
481            } else {
482                false
483            }
484        };
485
486        let predicates: Vec<_> = predicates_and_bounds
487            .predicates
488            .iter()
489            .copied()
490            .filter(|(pred, _)| match pred.kind().skip_binder() {
491                ty::ClauseKind::Trait(tr) => !is_assoc_item_ty(tr.self_ty()),
492                ty::ClauseKind::Projection(proj) => {
493                    !is_assoc_item_ty(proj.projection_term.self_ty())
494                }
495                ty::ClauseKind::TypeOutlives(outlives) => !is_assoc_item_ty(outlives.0),
496                _ => true,
497            })
498            .collect();
499        if predicates.len() == predicates_and_bounds.predicates.len() {
500            predicates_and_bounds
501        } else {
502            ty::GenericPredicates {
503                parent: predicates_and_bounds.parent,
504                predicates: tcx.arena.alloc_slice(&predicates),
505            }
506        }
507    } else {
508        if matches!(def_kind, DefKind::AnonConst)
509            && tcx.features().generic_const_exprs()
510            && let Some(defaulted_param_def_id) =
511                tcx.hir().opt_const_param_default_param_def_id(tcx.local_def_id_to_hir_id(def_id))
512        {
513            // In `generics_of` we set the generics' parent to be our parent's parent which means that
514            // we lose out on the predicates of our actual parent if we dont return those predicates here.
515            // (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
516            //
517            // struct Foo<T, const N: usize = { <T as Trait>::ASSOC }>(T) where T: Trait;
518            //        ^^^                     ^^^^^^^^^^^^^^^^^^^^^^^ the def id we are calling
519            //        ^^^                                             explicit_predicates_of on
520            //        parent item we dont have set as the
521            //        parent of generics returned by `generics_of`
522            //
523            // In the above code we want the anon const to have predicates in its param env for `T: Trait`
524            // and we would be calling `explicit_predicates_of(Foo)` here
525            let parent_def_id = tcx.local_parent(def_id);
526            let parent_preds = tcx.explicit_predicates_of(parent_def_id);
527
528            // If we dont filter out `ConstArgHasType` predicates then every single defaulted const parameter
529            // will ICE because of #106994. FIXME(generic_const_exprs): remove this when a more general solution
530            // to #106994 is implemented.
531            let filtered_predicates = parent_preds
532                .predicates
533                .into_iter()
534                .filter(|(pred, _)| {
535                    if let ty::ClauseKind::ConstArgHasType(ct, _) = pred.kind().skip_binder() {
536                        match ct.kind() {
537                            ty::ConstKind::Param(param_const) => {
538                                let defaulted_param_idx = tcx
539                                    .generics_of(parent_def_id)
540                                    .param_def_id_to_index[&defaulted_param_def_id.to_def_id()];
541                                param_const.index < defaulted_param_idx
542                            }
543                            _ => bug!(
544                                "`ConstArgHasType` in `predicates_of`\
545                                 that isn't a `Param` const"
546                            ),
547                        }
548                    } else {
549                        true
550                    }
551                })
552                .cloned();
553            return GenericPredicates {
554                parent: parent_preds.parent,
555                predicates: { tcx.arena.alloc_from_iter(filtered_predicates) },
556            };
557        }
558        gather_explicit_predicates_of(tcx, def_id)
559    }
560}
561
562/// Ensures that the super-predicates of the trait with a `DefId`
563/// of `trait_def_id` are lowered and stored. This also ensures that
564/// the transitive super-predicates are lowered.
565pub(super) fn explicit_super_predicates_of<'tcx>(
566    tcx: TyCtxt<'tcx>,
567    trait_def_id: LocalDefId,
568) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
569    implied_predicates_with_filter(tcx, trait_def_id.to_def_id(), PredicateFilter::SelfOnly)
570}
571
572pub(super) fn explicit_supertraits_containing_assoc_item<'tcx>(
573    tcx: TyCtxt<'tcx>,
574    (trait_def_id, assoc_name): (DefId, Ident),
575) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
576    implied_predicates_with_filter(
577        tcx,
578        trait_def_id,
579        PredicateFilter::SelfTraitThatDefines(assoc_name),
580    )
581}
582
583pub(super) fn explicit_implied_predicates_of<'tcx>(
584    tcx: TyCtxt<'tcx>,
585    trait_def_id: LocalDefId,
586) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
587    implied_predicates_with_filter(
588        tcx,
589        trait_def_id.to_def_id(),
590        if tcx.is_trait_alias(trait_def_id.to_def_id()) {
591            PredicateFilter::All
592        } else {
593            PredicateFilter::SelfAndAssociatedTypeBounds
594        },
595    )
596}
597
598/// Ensures that the super-predicates of the trait with a `DefId`
599/// of `trait_def_id` are lowered and stored. This also ensures that
600/// the transitive super-predicates are lowered.
601pub(super) fn implied_predicates_with_filter<'tcx>(
602    tcx: TyCtxt<'tcx>,
603    trait_def_id: DefId,
604    filter: PredicateFilter,
605) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
606    let Some(trait_def_id) = trait_def_id.as_local() else {
607        // if `assoc_name` is None, then the query should've been redirected to an
608        // external provider
609        assert_matches!(filter, PredicateFilter::SelfTraitThatDefines(_));
610        return tcx.explicit_super_predicates_of(trait_def_id);
611    };
612
613    let Node::Item(item) = tcx.hir_node_by_def_id(trait_def_id) else {
614        bug!("trait_def_id {trait_def_id:?} is not an item");
615    };
616
617    let (generics, superbounds) = match item.kind {
618        hir::ItemKind::Trait(.., generics, supertraits, _) => (generics, supertraits),
619        hir::ItemKind::TraitAlias(_, generics, supertraits) => (generics, supertraits),
620        _ => span_bug!(item.span, "super_predicates invoked on non-trait"),
621    };
622
623    let icx = ItemCtxt::new(tcx, trait_def_id);
624
625    let self_param_ty = tcx.types.self_param;
626    let mut bounds = Vec::new();
627    icx.lowerer().lower_bounds(self_param_ty, superbounds, &mut bounds, ty::List::empty(), filter);
628
629    let where_bounds_that_match =
630        icx.probe_ty_param_bounds_in_generics(generics, item.owner_id.def_id, filter);
631
632    // Combine the two lists to form the complete set of superbounds:
633    let implied_bounds =
634        &*tcx.arena.alloc_from_iter(bounds.into_iter().chain(where_bounds_that_match));
635    debug!(?implied_bounds);
636
637    // Now require that immediate supertraits are lowered, which will, in
638    // turn, reach indirect supertraits, so we detect cycles now instead of
639    // overflowing during elaboration. Same for implied predicates, which
640    // make sure we walk into associated type bounds.
641    match filter {
642        PredicateFilter::SelfOnly => {
643            for &(pred, span) in implied_bounds {
644                debug!("superbound: {:?}", pred);
645                if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
646                    && bound.polarity == ty::PredicatePolarity::Positive
647                {
648                    tcx.at(span).explicit_super_predicates_of(bound.def_id());
649                }
650            }
651        }
652        PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
653            for &(pred, span) in implied_bounds {
654                debug!("superbound: {:?}", pred);
655                if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
656                    && bound.polarity == ty::PredicatePolarity::Positive
657                {
658                    tcx.at(span).explicit_implied_predicates_of(bound.def_id());
659                }
660            }
661        }
662        _ => {}
663    }
664
665    assert_only_contains_predicates_from(filter, implied_bounds, tcx.types.self_param);
666
667    ty::EarlyBinder::bind(implied_bounds)
668}
669
670// Make sure when elaborating supertraits, probing for associated types, etc.,
671// we really truly are elaborating clauses that have `ty` as their self type.
672// This is very important since downstream code relies on this being correct.
673pub(super) fn assert_only_contains_predicates_from<'tcx>(
674    filter: PredicateFilter,
675    bounds: &'tcx [(ty::Clause<'tcx>, Span)],
676    ty: Ty<'tcx>,
677) {
678    if !cfg!(debug_assertions) {
679        return;
680    }
681
682    match filter {
683        PredicateFilter::SelfOnly => {
684            for (clause, _) in bounds {
685                match clause.kind().skip_binder() {
686                    ty::ClauseKind::Trait(trait_predicate) => {
687                        assert_eq!(
688                            trait_predicate.self_ty(),
689                            ty,
690                            "expected `Self` predicate when computing \
691                            `{filter:?}` implied bounds: {clause:?}"
692                        );
693                    }
694                    ty::ClauseKind::Projection(projection_predicate) => {
695                        assert_eq!(
696                            projection_predicate.self_ty(),
697                            ty,
698                            "expected `Self` predicate when computing \
699                            `{filter:?}` implied bounds: {clause:?}"
700                        );
701                    }
702                    ty::ClauseKind::TypeOutlives(outlives_predicate) => {
703                        assert_eq!(
704                            outlives_predicate.0, ty,
705                            "expected `Self` predicate when computing \
706                            `{filter:?}` implied bounds: {clause:?}"
707                        );
708                    }
709                    ty::ClauseKind::HostEffect(host_effect_predicate) => {
710                        assert_eq!(
711                            host_effect_predicate.self_ty(),
712                            ty,
713                            "expected `Self` predicate when computing \
714                            `{filter:?}` implied bounds: {clause:?}"
715                        );
716                    }
717
718                    ty::ClauseKind::RegionOutlives(_)
719                    | ty::ClauseKind::ConstArgHasType(_, _)
720                    | ty::ClauseKind::WellFormed(_)
721                    | ty::ClauseKind::ConstEvaluatable(_) => {
722                        bug!(
723                            "unexpected non-`Self` predicate when computing \
724                            `{filter:?}` implied bounds: {clause:?}"
725                        );
726                    }
727                }
728            }
729        }
730        PredicateFilter::SelfTraitThatDefines(_) => {
731            for (clause, _) in bounds {
732                match clause.kind().skip_binder() {
733                    ty::ClauseKind::Trait(trait_predicate) => {
734                        assert_eq!(
735                            trait_predicate.self_ty(),
736                            ty,
737                            "expected `Self` predicate when computing \
738                            `{filter:?}` implied bounds: {clause:?}"
739                        );
740                    }
741
742                    ty::ClauseKind::Projection(_)
743                    | ty::ClauseKind::TypeOutlives(_)
744                    | ty::ClauseKind::RegionOutlives(_)
745                    | ty::ClauseKind::ConstArgHasType(_, _)
746                    | ty::ClauseKind::WellFormed(_)
747                    | ty::ClauseKind::ConstEvaluatable(_)
748                    | ty::ClauseKind::HostEffect(..) => {
749                        bug!(
750                            "unexpected non-`Self` predicate when computing \
751                            `{filter:?}` implied bounds: {clause:?}"
752                        );
753                    }
754                }
755            }
756        }
757        PredicateFilter::ConstIfConst => {
758            for (clause, _) in bounds {
759                match clause.kind().skip_binder() {
760                    ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
761                        trait_ref: _,
762                        constness: ty::BoundConstness::Maybe,
763                    }) => {}
764                    _ => {
765                        bug!(
766                            "unexpected non-`HostEffect` predicate when computing \
767                            `{filter:?}` implied bounds: {clause:?}"
768                        );
769                    }
770                }
771            }
772        }
773        PredicateFilter::SelfConstIfConst => {
774            for (clause, _) in bounds {
775                match clause.kind().skip_binder() {
776                    ty::ClauseKind::HostEffect(pred) => {
777                        assert_eq!(
778                            pred.constness,
779                            ty::BoundConstness::Maybe,
780                            "expected `~const` predicate when computing `{filter:?}` \
781                            implied bounds: {clause:?}",
782                        );
783                        assert_eq!(
784                            pred.trait_ref.self_ty(),
785                            ty,
786                            "expected `Self` predicate when computing `{filter:?}` \
787                            implied bounds: {clause:?}"
788                        );
789                    }
790                    _ => {
791                        bug!(
792                            "unexpected non-`HostEffect` predicate when computing \
793                            `{filter:?}` implied bounds: {clause:?}"
794                        );
795                    }
796                }
797            }
798        }
799        PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {}
800    }
801}
802
803/// Returns the predicates defined on `item_def_id` of the form
804/// `X: Foo` where `X` is the type parameter `def_id`.
805#[instrument(level = "trace", skip(tcx))]
806pub(super) fn type_param_predicates<'tcx>(
807    tcx: TyCtxt<'tcx>,
808    (item_def_id, def_id, assoc_name): (LocalDefId, LocalDefId, Ident),
809) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
810    match tcx.opt_rpitit_info(item_def_id.to_def_id()) {
811        Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
812            return tcx.type_param_predicates((opaque_def_id.expect_local(), def_id, assoc_name));
813        }
814        Some(ty::ImplTraitInTraitData::Impl { .. }) => {
815            unreachable!("should not be lowering bounds on RPITIT in impl")
816        }
817        None => {}
818    }
819
820    // In the HIR, bounds can derive from two places. Either
821    // written inline like `<T: Foo>` or in a where-clause like
822    // `where T: Foo`.
823
824    let param_id = tcx.local_def_id_to_hir_id(def_id);
825    let param_owner = tcx.hir_ty_param_owner(def_id);
826
827    // Don't look for bounds where the type parameter isn't in scope.
828    let parent = if item_def_id == param_owner {
829        // FIXME: Shouldn't this be unreachable?
830        None
831    } else {
832        tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local())
833    };
834
835    let result = if let Some(parent) = parent {
836        let icx = ItemCtxt::new(tcx, parent);
837        icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_name)
838    } else {
839        ty::EarlyBinder::bind(&[] as &[_])
840    };
841    let mut extend = None;
842
843    let item_hir_id = tcx.local_def_id_to_hir_id(item_def_id);
844
845    let hir_node = tcx.hir_node(item_hir_id);
846    let Some(hir_generics) = hir_node.generics() else {
847        return result;
848    };
849
850    if let Node::Item(item) = hir_node
851        && let hir::ItemKind::Trait(..) = item.kind
852        // Implied `Self: Trait` and supertrait bounds.
853        && param_id == item_hir_id
854    {
855        let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id.to_def_id());
856        extend = Some((identity_trait_ref.upcast(tcx), item.span));
857    }
858
859    let icx = ItemCtxt::new(tcx, item_def_id);
860    let extra_predicates = extend.into_iter().chain(icx.probe_ty_param_bounds_in_generics(
861        hir_generics,
862        def_id,
863        PredicateFilter::SelfTraitThatDefines(assoc_name),
864    ));
865
866    let bounds =
867        &*tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates));
868
869    // Double check that the bounds *only* contain `SelfTy: Trait` preds.
870    let self_ty = match tcx.def_kind(def_id) {
871        DefKind::TyParam => Ty::new_param(
872            tcx,
873            tcx.generics_of(item_def_id)
874                .param_def_id_to_index(tcx, def_id.to_def_id())
875                .expect("expected generic param to be owned by item"),
876            tcx.item_name(def_id.to_def_id()),
877        ),
878        DefKind::Trait | DefKind::TraitAlias => tcx.types.self_param,
879        _ => unreachable!(),
880    };
881    assert_only_contains_predicates_from(
882        PredicateFilter::SelfTraitThatDefines(assoc_name),
883        bounds,
884        self_ty,
885    );
886
887    ty::EarlyBinder::bind(bounds)
888}
889
890impl<'tcx> ItemCtxt<'tcx> {
891    /// Finds bounds from `hir::Generics`.
892    ///
893    /// This requires scanning through the HIR.
894    /// We do this to avoid having to lower *all* the bounds, which would create artificial cycles.
895    /// Instead, we can only lower the bounds for a type parameter `X` if `X::Foo` is used.
896    #[instrument(level = "trace", skip(self, hir_generics))]
897    fn probe_ty_param_bounds_in_generics(
898        &self,
899        hir_generics: &'tcx hir::Generics<'tcx>,
900        param_def_id: LocalDefId,
901        filter: PredicateFilter,
902    ) -> Vec<(ty::Clause<'tcx>, Span)> {
903        let mut bounds = Vec::new();
904
905        for predicate in hir_generics.predicates {
906            let hir_id = predicate.hir_id;
907            let hir::WherePredicateKind::BoundPredicate(predicate) = predicate.kind else {
908                continue;
909            };
910
911            match filter {
912                _ if predicate.is_param_bound(param_def_id.to_def_id()) => {
913                    // Ok
914                }
915                PredicateFilter::All => {
916                    // Ok
917                }
918                PredicateFilter::SelfOnly
919                | PredicateFilter::SelfTraitThatDefines(_)
920                | PredicateFilter::SelfConstIfConst
921                | PredicateFilter::SelfAndAssociatedTypeBounds => continue,
922                PredicateFilter::ConstIfConst => unreachable!(),
923            }
924
925            let bound_ty = self.lowerer().lower_ty_maybe_return_type_notation(predicate.bounded_ty);
926
927            let bound_vars = self.tcx.late_bound_vars(hir_id);
928            self.lowerer().lower_bounds(
929                bound_ty,
930                predicate.bounds,
931                &mut bounds,
932                bound_vars,
933                filter,
934            );
935        }
936
937        bounds
938    }
939}
940
941pub(super) fn const_conditions<'tcx>(
942    tcx: TyCtxt<'tcx>,
943    def_id: LocalDefId,
944) -> ty::ConstConditions<'tcx> {
945    if !tcx.is_conditionally_const(def_id) {
946        bug!("const_conditions invoked for item that is not conditionally const: {def_id:?}");
947    }
948
949    match tcx.opt_rpitit_info(def_id.to_def_id()) {
950        // RPITITs inherit const conditions of their parent fn
951        Some(
952            ty::ImplTraitInTraitData::Impl { fn_def_id }
953            | ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
954        ) => return tcx.const_conditions(fn_def_id),
955        None => {}
956    }
957
958    let (generics, trait_def_id_and_supertraits, has_parent) = match tcx.hir_node_by_def_id(def_id)
959    {
960        Node::Item(item) => match item.kind {
961            hir::ItemKind::Impl(impl_) => (impl_.generics, None, false),
962            hir::ItemKind::Fn { generics, .. } => (generics, None, false),
963            hir::ItemKind::Trait(_, _, _, generics, supertraits, _) => {
964                (generics, Some((item.owner_id.def_id, supertraits)), false)
965            }
966            _ => bug!("const_conditions called on wrong item: {def_id:?}"),
967        },
968        // While associated types are not really const, we do allow them to have `~const`
969        // bounds and where clauses. `const_conditions` is responsible for gathering
970        // these up so we can check them in `compare_type_predicate_entailment`, and
971        // in `HostEffect` goal computation.
972        Node::TraitItem(item) => match item.kind {
973            hir::TraitItemKind::Fn(_, _) | hir::TraitItemKind::Type(_, _) => {
974                (item.generics, None, true)
975            }
976            _ => bug!("const_conditions called on wrong item: {def_id:?}"),
977        },
978        Node::ImplItem(item) => match item.kind {
979            hir::ImplItemKind::Fn(_, _) | hir::ImplItemKind::Type(_) => {
980                (item.generics, None, tcx.is_conditionally_const(tcx.local_parent(def_id)))
981            }
982            _ => bug!("const_conditions called on wrong item: {def_id:?}"),
983        },
984        Node::ForeignItem(item) => match item.kind {
985            hir::ForeignItemKind::Fn(_, _, generics) => (generics, None, false),
986            _ => bug!("const_conditions called on wrong item: {def_id:?}"),
987        },
988        Node::OpaqueTy(opaque) => match opaque.origin {
989            hir::OpaqueTyOrigin::FnReturn { parent, .. } => return tcx.const_conditions(parent),
990            hir::OpaqueTyOrigin::AsyncFn { .. } | hir::OpaqueTyOrigin::TyAlias { .. } => {
991                unreachable!()
992            }
993        },
994        // N.B. Tuple ctors are unconditionally constant.
995        Node::Ctor(hir::VariantData::Tuple { .. }) => return Default::default(),
996        _ => bug!("const_conditions called on wrong item: {def_id:?}"),
997    };
998
999    let icx = ItemCtxt::new(tcx, def_id);
1000    let mut bounds = Vec::new();
1001
1002    for pred in generics.predicates {
1003        match pred.kind {
1004            hir::WherePredicateKind::BoundPredicate(bound_pred) => {
1005                let ty = icx.lowerer().lower_ty_maybe_return_type_notation(bound_pred.bounded_ty);
1006                let bound_vars = tcx.late_bound_vars(pred.hir_id);
1007                icx.lowerer().lower_bounds(
1008                    ty,
1009                    bound_pred.bounds.iter(),
1010                    &mut bounds,
1011                    bound_vars,
1012                    PredicateFilter::ConstIfConst,
1013                );
1014            }
1015            _ => {}
1016        }
1017    }
1018
1019    if let Some((def_id, supertraits)) = trait_def_id_and_supertraits {
1020        // We've checked above that the trait is conditionally const.
1021        bounds.push((
1022            ty::Binder::dummy(ty::TraitRef::identity(tcx, def_id.to_def_id()))
1023                .to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
1024            DUMMY_SP,
1025        ));
1026
1027        icx.lowerer().lower_bounds(
1028            tcx.types.self_param,
1029            supertraits,
1030            &mut bounds,
1031            ty::List::empty(),
1032            PredicateFilter::ConstIfConst,
1033        );
1034    }
1035
1036    ty::ConstConditions {
1037        parent: has_parent.then(|| tcx.local_parent(def_id).to_def_id()),
1038        predicates: tcx.arena.alloc_from_iter(bounds.into_iter().map(|(clause, span)| {
1039            (
1040                clause.kind().map_bound(|clause| match clause {
1041                    ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
1042                        trait_ref,
1043                        constness: ty::BoundConstness::Maybe,
1044                    }) => trait_ref,
1045                    _ => bug!("converted {clause:?}"),
1046                }),
1047                span,
1048            )
1049        })),
1050    }
1051}
1052
1053pub(super) fn explicit_implied_const_bounds<'tcx>(
1054    tcx: TyCtxt<'tcx>,
1055    def_id: LocalDefId,
1056) -> ty::EarlyBinder<'tcx, &'tcx [(ty::PolyTraitRef<'tcx>, Span)]> {
1057    if !tcx.is_conditionally_const(def_id) {
1058        bug!(
1059            "explicit_implied_const_bounds invoked for item that is not conditionally const: {def_id:?}"
1060        );
1061    }
1062
1063    let bounds = match tcx.opt_rpitit_info(def_id.to_def_id()) {
1064        // RPITIT's bounds are the same as opaque type bounds, but with
1065        // a projection self type.
1066        Some(ty::ImplTraitInTraitData::Trait { .. }) => {
1067            explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
1068        }
1069        Some(ty::ImplTraitInTraitData::Impl { .. }) => {
1070            span_bug!(tcx.def_span(def_id), "RPITIT in impl should not have item bounds")
1071        }
1072        None => match tcx.hir_node_by_def_id(def_id) {
1073            Node::Item(hir::Item { kind: hir::ItemKind::Trait(..), .. }) => {
1074                implied_predicates_with_filter(
1075                    tcx,
1076                    def_id.to_def_id(),
1077                    PredicateFilter::SelfConstIfConst,
1078                )
1079            }
1080            Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
1081            | Node::OpaqueTy(_) => {
1082                explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
1083            }
1084            _ => bug!("explicit_implied_const_bounds called on wrong item: {def_id:?}"),
1085        },
1086    };
1087
1088    bounds.map_bound(|bounds| {
1089        &*tcx.arena.alloc_from_iter(bounds.iter().copied().map(|(clause, span)| {
1090            (
1091                clause.kind().map_bound(|clause| match clause {
1092                    ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
1093                        trait_ref,
1094                        constness: ty::BoundConstness::Maybe,
1095                    }) => trait_ref,
1096                    _ => bug!("converted {clause:?}"),
1097                }),
1098                span,
1099            )
1100        }))
1101    })
1102}