Skip to main content

rustc_hir_analysis/collect/
predicates_of.rs

1use std::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_hir::find_attr;
9use rustc_middle::ty::{
10    self, GenericPredicates, ImplTraitInTraitData, Ty, TyCtxt, TypeVisitable, TypeVisitor, Upcast,
11};
12use rustc_middle::{bug, span_bug};
13use rustc_span::{DUMMY_SP, Ident, Span};
14use tracing::{debug, instrument, trace};
15
16use super::item_bounds::explicit_item_bounds_with_filter;
17use crate::collect::ItemCtxt;
18use crate::constrained_generic_params as cgp;
19use crate::delegation::inherit_predicates_for_delegation_item;
20use crate::hir_ty_lowering::{
21    HirTyLowerer, ImpliedBoundsContext, OverlappingAsssocItemConstraints, PredicateFilter,
22    RegionInferReason,
23};
24
25/// Returns a list of all type predicates (explicit and implicit) for the definition with
26/// ID `def_id`. This includes all predicates returned by `explicit_predicates_of`, plus
27/// inferred constraints concerning which regions outlive other regions.
28#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("predicates_of",
                                    "rustc_hir_analysis::collect::predicates_of",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
                                    ::tracing_core::__macro_support::Option::Some(28u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
                                    ::tracing_core::field::FieldSet::new(&["def_id"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_id)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: ty::GenericPredicates<'_> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let mut result = tcx.explicit_predicates_of(def_id);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/collect/predicates_of.rs:31",
                                    "rustc_hir_analysis::collect::predicates_of",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
                                    ::tracing_core::__macro_support::Option::Some(31u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&format_args!("predicates_of: explicit_predicates_of({0:?}) = {1:?}",
                                                                def_id, result) as &dyn Value))])
                        });
                } else { ; }
            };
            let inferred_outlives = tcx.inferred_outlives_of(def_id);
            if !inferred_outlives.is_empty() {
                {
                    use ::tracing::__macro_support::Callsite as _;
                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                        {
                            static META: ::tracing::Metadata<'static> =
                                {
                                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/collect/predicates_of.rs:35",
                                        "rustc_hir_analysis::collect::predicates_of",
                                        ::tracing::Level::DEBUG,
                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
                                        ::tracing_core::__macro_support::Option::Some(35u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
                                        ::tracing_core::field::FieldSet::new(&["message"],
                                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                        ::tracing::metadata::Kind::EVENT)
                                };
                            ::tracing::callsite::DefaultCallsite::new(&META)
                        };
                    let enabled =
                        ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            {
                                let interest = __CALLSITE.interest();
                                !interest.is_never() &&
                                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                        interest)
                            };
                    if enabled {
                        (|value_set: ::tracing::field::ValueSet|
                                    {
                                        let meta = __CALLSITE.metadata();
                                        ::tracing::Event::dispatch(meta, &value_set);
                                        ;
                                    })({
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = __CALLSITE.metadata().fields().iter();
                                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&format_args!("predicates_of: inferred_outlives_of({0:?}) = {1:?}",
                                                                    def_id, inferred_outlives) as &dyn Value))])
                            });
                    } else { ; }
                };
                let inferred_outlives_iter =
                    inferred_outlives.iter().map(|(clause, span)|
                            ((*clause).upcast(tcx), *span));
                if result.predicates.is_empty() {
                    result.predicates =
                        tcx.arena.alloc_from_iter(inferred_outlives_iter);
                } else {
                    result.predicates =
                        tcx.arena.alloc_from_iter(result.predicates.into_iter().copied().chain(inferred_outlives_iter));
                }
            }
            if tcx.is_trait(def_id) {
                let span = DUMMY_SP;
                result.predicates =
                    tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(std::iter::once((ty::TraitRef::identity(tcx,
                                            def_id).upcast(tcx), span))));
            }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/collect/predicates_of.rs:76",
                                    "rustc_hir_analysis::collect::predicates_of",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
                                    ::tracing_core::__macro_support::Option::Some(76u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&format_args!("predicates_of({0:?}) = {1:?}",
                                                                def_id, result) as &dyn Value))])
                        });
                } else { ; }
            };
            result
        }
    }
}#[instrument(level = "debug", skip(tcx))]
29pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
30    let mut result = tcx.explicit_predicates_of(def_id);
31    debug!("predicates_of: explicit_predicates_of({:?}) = {:?}", def_id, result);
32
33    let inferred_outlives = tcx.inferred_outlives_of(def_id);
34    if !inferred_outlives.is_empty() {
35        debug!("predicates_of: inferred_outlives_of({:?}) = {:?}", def_id, inferred_outlives,);
36        let inferred_outlives_iter =
37            inferred_outlives.iter().map(|(clause, span)| ((*clause).upcast(tcx), *span));
38        if result.predicates.is_empty() {
39            result.predicates = tcx.arena.alloc_from_iter(inferred_outlives_iter);
40        } else {
41            result.predicates = tcx.arena.alloc_from_iter(
42                result.predicates.into_iter().copied().chain(inferred_outlives_iter),
43            );
44        }
45    }
46
47    if tcx.is_trait(def_id) {
48        // For traits, add `Self: Trait` predicate. This is
49        // not part of the predicates that a user writes, but it
50        // is something that one must prove in order to invoke a
51        // method or project an associated type.
52        //
53        // In the chalk setup, this predicate is not part of the
54        // "predicates" for a trait item. But it is useful in
55        // rustc because if you directly (e.g.) invoke a trait
56        // method like `Trait::method(...)`, you must naturally
57        // prove that the trait applies to the types that were
58        // used, and adding the predicate into this list ensures
59        // that this is done.
60        //
61        // We use a DUMMY_SP here as a way to signal trait bounds that come
62        // from the trait itself that *shouldn't* be shown as the source of
63        // an obligation and instead be skipped. Otherwise we'd use
64        // `tcx.def_span(def_id);`
65        let span = DUMMY_SP;
66
67        result.predicates = tcx.arena.alloc_from_iter(
68            result
69                .predicates
70                .iter()
71                .copied()
72                .chain(std::iter::once((ty::TraitRef::identity(tcx, def_id).upcast(tcx), span))),
73        );
74    }
75
76    debug!("predicates_of({:?}) = {:?}", def_id, result);
77    result
78}
79
80/// Returns a list of user-specified type predicates for the definition with ID `def_id`.
81/// N.B., this does not include any implied/inferred constraints.
82x;#[instrument(level = "trace", skip(tcx), ret)]
83fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::GenericPredicates<'_> {
84    use rustc_hir::*;
85
86    match tcx.opt_rpitit_info(def_id.to_def_id()) {
87        Some(ImplTraitInTraitData::Trait { fn_def_id, .. }) => {
88            let mut predicates = Vec::new();
89
90            // RPITITs should inherit the predicates of their parent. This is
91            // both to ensure that the RPITITs are only instantiated when the
92            // parent predicates would hold, and also so that the param-env
93            // inherits these predicates as assumptions.
94            let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
95            predicates.extend(
96                tcx.explicit_predicates_of(fn_def_id)
97                    .instantiate_own(tcx, identity_args)
98                    .map(|(c, s)| (c.skip_norm_wip(), s)),
99            );
100
101            // We also install bidirectional outlives predicates for the RPITIT
102            // to keep the duplicates lifetimes from opaque lowering in sync.
103            // We only need to compute bidirectional outlives for the duplicated
104            // opaque lifetimes, which explains the slicing below.
105            compute_bidirectional_outlives_predicates(
106                tcx,
107                &tcx.generics_of(def_id.to_def_id()).own_params
108                    [tcx.generics_of(fn_def_id).own_params.len()..],
109                &mut predicates,
110            );
111
112            return ty::GenericPredicates {
113                parent: Some(tcx.local_parent(def_id).to_def_id()),
114                predicates: tcx.arena.alloc_from_iter(predicates),
115            };
116        }
117
118        Some(ImplTraitInTraitData::Impl { fn_def_id }) => {
119            let trait_item_def_id = tcx.trait_item_of(def_id).unwrap();
120            let trait_assoc_predicates = tcx.explicit_predicates_of(trait_item_def_id);
121
122            let impl_assoc_identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
123            let impl_def_id = tcx.parent(fn_def_id);
124            let impl_trait_ref_args =
125                tcx.impl_trait_ref(impl_def_id).instantiate_identity().skip_norm_wip().args;
126
127            let impl_assoc_args =
128                impl_assoc_identity_args.rebase_onto(tcx, impl_def_id, impl_trait_ref_args);
129
130            let impl_predicates = trait_assoc_predicates
131                .instantiate_own(tcx, impl_assoc_args)
132                .map(|(c, s)| (c.skip_norm_wip(), s));
133
134            return ty::GenericPredicates {
135                parent: Some(impl_def_id),
136                predicates: tcx.arena.alloc_from_iter(impl_predicates),
137            };
138        }
139
140        None => {}
141    }
142
143    let hir_id = tcx.local_def_id_to_hir_id(def_id);
144    let node = tcx.hir_node(hir_id);
145
146    if let Some(sig) = node.fn_sig()
147        && let Some(sig_id) = sig.decl.opt_delegation_sig_id()
148    {
149        return inherit_predicates_for_delegation_item(tcx, def_id, sig_id);
150    }
151
152    let mut is_trait = None;
153    let mut is_default_impl_trait = None;
154
155    let icx = ItemCtxt::new(tcx, def_id);
156
157    const NO_GENERICS: &hir::Generics<'_> = hir::Generics::empty();
158
159    // We use an `IndexSet` to preserve order of insertion.
160    // Preserving the order of insertion is important here so as not to break UI tests.
161    let mut predicates: FxIndexSet<(ty::Clause<'_>, Span)> = FxIndexSet::default();
162
163    let hir_generics = node.generics().unwrap_or(NO_GENERICS);
164    if let Node::Item(item) = node {
165        match item.kind {
166            ItemKind::Impl(impl_) => {
167                if let Some(of_trait) = impl_.of_trait
168                    && of_trait.defaultness.is_default()
169                {
170                    is_default_impl_trait = Some(ty::Binder::dummy(
171                        tcx.impl_trait_ref(def_id).instantiate_identity().skip_norm_wip(),
172                    ));
173                }
174            }
175            ItemKind::Trait { bounds: self_bounds, .. }
176            | ItemKind::TraitAlias(_, _, _, self_bounds) => {
177                is_trait = Some((self_bounds, item.span));
178            }
179            _ => {}
180        }
181    };
182
183    let generics = tcx.generics_of(def_id);
184
185    // Below we'll consider the bounds on the type parameters (including `Self`)
186    // and the explicit where-clauses, but to get the full set of predicates
187    // on a trait we must also consider the bounds that follow the trait's name,
188    // like `trait Foo: A + B + C`.
189    if let Some((self_bounds, span)) = is_trait {
190        let mut bounds = Vec::new();
191        icx.lowerer().lower_bounds(
192            tcx.types.self_param,
193            self_bounds,
194            &mut bounds,
195            ty::List::empty(),
196            PredicateFilter::All,
197            OverlappingAsssocItemConstraints::Allowed,
198        );
199        icx.lowerer().add_implicit_sizedness_bounds(
200            &mut bounds,
201            tcx.types.self_param,
202            self_bounds,
203            ImpliedBoundsContext::TraitDef(def_id),
204            span,
205        );
206        icx.lowerer().add_default_traits(
207            &mut bounds,
208            tcx.types.self_param,
209            self_bounds,
210            ImpliedBoundsContext::TraitDef(def_id),
211            span,
212        );
213        predicates.extend(bounds);
214    }
215
216    // In default impls, we can assume that the self type implements
217    // the trait. So in:
218    //
219    //     default impl Foo for Bar { .. }
220    //
221    // we add a default where clause `Bar: Foo`. We do a similar thing for traits
222    // (see below). Recall that a default impl is not itself an impl, but rather a
223    // set of defaults that can be incorporated into another impl.
224    if let Some(trait_ref) = is_default_impl_trait {
225        predicates.insert((trait_ref.upcast(tcx), tcx.def_span(def_id)));
226    }
227
228    // Add implicit predicates that should be treated as if the user has written them,
229    // including the implicit `T: Sized` for all generic parameters, and `ConstArgHasType`
230    // for const params.
231    for param in hir_generics.params {
232        match param.kind {
233            GenericParamKind::Lifetime { .. } => (),
234            GenericParamKind::Type { .. } => {
235                let param_ty = icx.lowerer().lower_ty_param(param.hir_id);
236                let mut bounds = Vec::new();
237                // Implicit bounds are added to type params unless a `?Trait` bound is found
238                icx.lowerer().add_implicit_sizedness_bounds(
239                    &mut bounds,
240                    param_ty,
241                    &[],
242                    ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
243                    param.span,
244                );
245                icx.lowerer().add_default_traits(
246                    &mut bounds,
247                    param_ty,
248                    &[],
249                    ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
250                    param.span,
251                );
252                trace!(?bounds);
253                predicates.extend(bounds);
254                trace!(?predicates);
255            }
256            hir::GenericParamKind::Const { .. } => {
257                let param_def_id = param.def_id.to_def_id();
258                let ct_ty = tcx.type_of(param_def_id).instantiate_identity().skip_norm_wip();
259                let ct = icx.lowerer().lower_const_param(param_def_id, param.hir_id);
260                predicates
261                    .insert((ty::ClauseKind::ConstArgHasType(ct, ct_ty).upcast(tcx), param.span));
262            }
263        }
264    }
265
266    trace!(?predicates);
267    // Add inline `<T: Foo>` bounds and bounds in the where clause.
268    for predicate in hir_generics.predicates {
269        match predicate.kind {
270            hir::WherePredicateKind::BoundPredicate(bound_pred) => {
271                let ty = icx.lowerer().lower_ty_maybe_return_type_notation(bound_pred.bounded_ty);
272                let bound_vars = tcx.late_bound_vars(predicate.hir_id);
273
274                // This is a `where Ty:` (sic!).
275                if bound_pred.bounds.is_empty() {
276                    if let ty::Param(_) = ty.kind() {
277                        // We can skip the predicate because type parameters are trivially WF.
278                    } else {
279                        // Keep the type around in a dummy predicate. That way, it's not a complete
280                        // noop (see #53696) and `Ty` is still checked for WF.
281
282                        let span = bound_pred.bounded_ty.span;
283                        let predicate = ty::Binder::bind_with_vars(
284                            ty::ClauseKind::WellFormed(ty.into()),
285                            bound_vars,
286                        );
287                        predicates.insert((predicate.upcast(tcx), span));
288                    }
289                }
290
291                let mut bounds = Vec::new();
292                icx.lowerer().lower_bounds(
293                    ty,
294                    bound_pred.bounds,
295                    &mut bounds,
296                    bound_vars,
297                    PredicateFilter::All,
298                    OverlappingAsssocItemConstraints::Allowed,
299                );
300                predicates.extend(bounds);
301            }
302
303            hir::WherePredicateKind::RegionPredicate(region_pred) => {
304                let r1 = icx
305                    .lowerer()
306                    .lower_lifetime(region_pred.lifetime, RegionInferReason::RegionPredicate);
307                predicates.extend(region_pred.bounds.iter().map(|bound| {
308                    let (r2, span) = match bound {
309                        hir::GenericBound::Outlives(lt) => (
310                            icx.lowerer().lower_lifetime(lt, RegionInferReason::RegionPredicate),
311                            lt.ident.span,
312                        ),
313                        bound => {
314                            span_bug!(
315                                bound.span(),
316                                "lifetime param bounds must be outlives, but found {bound:?}"
317                            )
318                        }
319                    };
320                    let pred =
321                        ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(r1, r2)).upcast(tcx);
322                    (pred, span)
323                }))
324            }
325        }
326    }
327
328    if tcx.features().generic_const_exprs() {
329        predicates.extend(const_evaluatable_predicates_of(tcx, def_id, &predicates));
330    }
331
332    let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(def_id));
333    // FIXME(staged_api): We might want to look at the normal stability attributes too but
334    // first we would need a way to let std/core use APIs with unstable feature bounds from
335    // within stable APIs.
336    let allow_unstable_feature_attr =
337        find_attr!(attrs, UnstableFeatureBound(i) => i).map(|i| i.as_slice()).unwrap_or_default();
338
339    for (feat_name, span) in allow_unstable_feature_attr {
340        predicates.insert((ty::ClauseKind::UnstableFeature(*feat_name).upcast(tcx), *span));
341    }
342
343    let mut predicates: Vec<_> = predicates.into_iter().collect();
344
345    // Subtle: before we store the predicates into the tcx, we
346    // sort them so that predicates like `T: Foo<Item=U>` come
347    // before uses of `U`. This avoids false ambiguity errors
348    // in trait checking. See `setup_constraining_predicates`
349    // for details.
350    if let Node::Item(&Item { kind: ItemKind::Impl(impl_), .. }) = node {
351        let self_ty = tcx.type_of(def_id).instantiate_identity().skip_norm_wip();
352        let trait_ref = impl_
353            .of_trait
354            .is_some()
355            .then(|| tcx.impl_trait_ref(def_id).instantiate_identity().skip_norm_wip());
356        cgp::setup_constraining_predicates(
357            tcx,
358            &mut predicates,
359            trait_ref,
360            &mut cgp::parameters_for_impl(tcx, self_ty, trait_ref),
361        );
362    }
363
364    // Opaque types duplicate some of their generic parameters.
365    // We create bi-directional Outlives predicates between the original
366    // and the duplicated parameter, to ensure that they do not get out of sync.
367    if let Node::OpaqueTy(..) = node {
368        compute_bidirectional_outlives_predicates(tcx, &generics.own_params, &mut predicates);
369        debug!(?predicates);
370    }
371
372    ty::GenericPredicates {
373        parent: generics.parent,
374        predicates: tcx.arena.alloc_from_iter(predicates),
375    }
376}
377
378/// Opaques have duplicated lifetimes and we need to compute bidirectional outlives predicates to
379/// enforce that these lifetimes stay in sync.
380fn compute_bidirectional_outlives_predicates<'tcx>(
381    tcx: TyCtxt<'tcx>,
382    opaque_own_params: &[ty::GenericParamDef],
383    predicates: &mut Vec<(ty::Clause<'tcx>, Span)>,
384) {
385    for param in opaque_own_params {
386        let orig_lifetime = tcx.map_opaque_lifetime_to_parent_lifetime(param.def_id.expect_local());
387        if let ty::ReEarlyParam(..) = orig_lifetime.kind() {
388            let dup_lifetime = ty::Region::new_early_param(
389                tcx,
390                ty::EarlyParamRegion { index: param.index, name: param.name },
391            );
392            let span = tcx.def_span(param.def_id);
393            predicates.push((
394                ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(orig_lifetime, dup_lifetime))
395                    .upcast(tcx),
396                span,
397            ));
398            predicates.push((
399                ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(dup_lifetime, orig_lifetime))
400                    .upcast(tcx),
401                span,
402            ));
403        }
404    }
405}
406
407x;#[instrument(level = "debug", skip(tcx, predicates), ret)]
408fn const_evaluatable_predicates_of<'tcx>(
409    tcx: TyCtxt<'tcx>,
410    def_id: LocalDefId,
411    predicates: &FxIndexSet<(ty::Clause<'tcx>, Span)>,
412) -> FxIndexSet<(ty::Clause<'tcx>, Span)> {
413    struct ConstCollector<'tcx> {
414        tcx: TyCtxt<'tcx>,
415        preds: FxIndexSet<(ty::Clause<'tcx>, Span)>,
416    }
417
418    fn is_const_param_default(tcx: TyCtxt<'_>, kind: ty::AliasConstKind<'_>) -> bool {
419        let ty::AliasConstKind::Anon { def_id } = kind else { return false };
420        let Some(local) = def_id.as_local() else { return false };
421
422        let hir_id = tcx.local_def_id_to_hir_id(local);
423        let (_, parent_node) = tcx
424            .hir_parent_iter(hir_id)
425            .skip_while(|(_, n)| matches!(n, Node::ConstArg(..)))
426            .next()
427            .unwrap();
428        matches!(
429            parent_node,
430            Node::GenericParam(hir::GenericParam { kind: hir::GenericParamKind::Const { .. }, .. })
431        )
432    }
433
434    impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ConstCollector<'tcx> {
435        fn visit_const(&mut self, c: ty::Const<'tcx>) {
436            if let ty::ConstKind::Alias(_, alias_const) = c.kind() {
437                if is_const_param_default(self.tcx, alias_const.kind) {
438                    // Do not look into const param defaults,
439                    // these get checked when they are actually instantiated.
440                    //
441                    // We do not want the following to error:
442                    //
443                    //     struct Foo<const N: usize, const M: usize = { N + 1 }>;
444                    //     struct Bar<const N: usize>(Foo<N, 3>);
445                    return;
446                }
447
448                // Skip type consts as mGCA doesn't support evaluatable clauses.
449                if alias_const.kind.is_type_const(self.tcx) {
450                    return;
451                }
452
453                let span = alias_const.kind.def_span(self.tcx);
454                self.preds.insert((ty::ClauseKind::ConstEvaluatable(c).upcast(self.tcx), span));
455            }
456        }
457    }
458
459    let hir_id = tcx.local_def_id_to_hir_id(def_id);
460    let node = tcx.hir_node(hir_id);
461
462    let mut collector = ConstCollector { tcx, preds: FxIndexSet::default() };
463
464    for (clause, _sp) in predicates {
465        clause.visit_with(&mut collector);
466    }
467
468    if let hir::Node::Item(item) = node
469        && let hir::ItemKind::Impl(impl_) = item.kind
470    {
471        if impl_.of_trait.is_some() {
472            debug!("visit impl trait_ref");
473            let trait_ref = tcx.impl_trait_ref(def_id);
474            trait_ref.instantiate_identity().skip_norm_wip().visit_with(&mut collector);
475        }
476
477        debug!("visit self_ty");
478        let self_ty = tcx.type_of(def_id);
479        self_ty.instantiate_identity().skip_norm_wip().visit_with(&mut collector);
480    }
481
482    if let Some(_) = tcx.hir_fn_sig_by_hir_id(hir_id) {
483        debug!("visit fn sig");
484        let fn_sig = tcx.fn_sig(def_id);
485        let fn_sig = fn_sig.instantiate_identity().skip_norm_wip();
486        debug!(?fn_sig);
487        fn_sig.visit_with(&mut collector);
488    }
489
490    collector.preds
491}
492
493pub(super) fn trait_explicit_predicates_and_bounds(
494    tcx: TyCtxt<'_>,
495    def_id: LocalDefId,
496) -> ty::GenericPredicates<'_> {
497    {
    match (&tcx.def_kind(def_id), &DefKind::Trait) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    }
};assert_eq!(tcx.def_kind(def_id), DefKind::Trait);
498    gather_explicit_predicates_of(tcx, def_id)
499}
500
501pub(super) fn explicit_predicates_of<'tcx>(
502    tcx: TyCtxt<'tcx>,
503    def_id: LocalDefId,
504) -> ty::GenericPredicates<'tcx> {
505    let def_kind = tcx.def_kind(def_id);
506    if let DefKind::Trait = def_kind {
507        // Remove bounds on associated types from the predicates, they will be
508        // returned by `explicit_item_bounds`.
509        let predicates_and_bounds = tcx.trait_explicit_predicates_and_bounds(def_id);
510        let trait_identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
511
512        let is_assoc_item_ty = |ty: Ty<'tcx>| {
513            // For a predicate from a where clause to become a bound on an
514            // associated type:
515            // * It must use the identity args of the item.
516            //   * We're in the scope of the trait, so we can't name any
517            //     parameters of the GAT. That means that all we need to
518            //     check are that the args of the projection are the
519            //     identity args of the trait.
520            // * It must be an associated type for this trait (*not* a
521            //   supertrait).
522            if let &ty::Alias(
523                _,
524                ty::AliasTy { kind: ty::Projection { def_id: projection_def_id }, args, .. },
525            ) = ty.kind()
526            {
527                args == trait_identity_args
528                    // FIXME(return_type_notation): This check should be more robust
529                    && !tcx.is_impl_trait_in_trait(projection_def_id)
530                    && tcx.parent(projection_def_id) == def_id.to_def_id()
531            } else {
532                false
533            }
534        };
535
536        let predicates: Vec<_> = predicates_and_bounds
537            .predicates
538            .iter()
539            .copied()
540            .filter(|(pred, _)| match pred.kind().skip_binder() {
541                ty::ClauseKind::Trait(tr) => !is_assoc_item_ty(tr.self_ty()),
542                ty::ClauseKind::Projection(proj) => {
543                    !is_assoc_item_ty(proj.projection_term.self_ty())
544                }
545                ty::ClauseKind::TypeOutlives(outlives) => !is_assoc_item_ty(outlives.0),
546                _ => true,
547            })
548            .collect();
549        if predicates.len() == predicates_and_bounds.predicates.len() {
550            predicates_and_bounds
551        } else {
552            ty::GenericPredicates {
553                parent: predicates_and_bounds.parent,
554                predicates: tcx.arena.alloc_slice(&predicates),
555            }
556        }
557    } else {
558        if def_kind == DefKind::AnonConst
559            && tcx.features().generic_const_exprs()
560            && let Some(defaulted_param_def_id) =
561                tcx.hir_opt_const_param_default_param_def_id(tcx.local_def_id_to_hir_id(def_id))
562        {
563            // In `generics_of` we set the generics' parent to be our parent's parent which means that
564            // we lose out on the predicates of our actual parent if we dont return those predicates here.
565            // (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
566            //
567            // struct Foo<T, const N: usize = { <T as Trait>::ASSOC }>(T) where T: Trait;
568            //        ^^^                     ^^^^^^^^^^^^^^^^^^^^^^^ the def id we are calling
569            //        ^^^                                             explicit_predicates_of on
570            //        parent item we dont have set as the
571            //        parent of generics returned by `generics_of`
572            //
573            // In the above code we want the anon const to have predicates in its param env for `T: Trait`
574            // and we would be calling `explicit_predicates_of(Foo)` here
575            let parent_def_id = tcx.local_parent(def_id);
576            let parent_preds = tcx.explicit_predicates_of(parent_def_id);
577
578            // If we dont filter out `ConstArgHasType` predicates then every single defaulted const parameter
579            // will ICE because of #106994. FIXME(generic_const_exprs): remove this when a more general solution
580            // to #106994 is implemented.
581            let filtered_predicates = parent_preds
582                .predicates
583                .into_iter()
584                .filter(|(pred, _)| {
585                    if let ty::ClauseKind::ConstArgHasType(ct, _) = pred.kind().skip_binder() {
586                        match ct.kind() {
587                            ty::ConstKind::Param(param_const) => {
588                                let defaulted_param_idx = tcx
589                                    .generics_of(parent_def_id)
590                                    .param_def_id_to_index[&defaulted_param_def_id.to_def_id()];
591                                param_const.index < defaulted_param_idx
592                            }
593                            _ => ::rustc_middle::util::bug::bug_fmt(format_args!("`ConstArgHasType` in `predicates_of`that isn\'t a `Param` const"))bug!(
594                                "`ConstArgHasType` in `predicates_of`\
595                                 that isn't a `Param` const"
596                            ),
597                        }
598                    } else {
599                        true
600                    }
601                })
602                .cloned();
603            return GenericPredicates {
604                parent: parent_preds.parent,
605                predicates: { tcx.arena.alloc_from_iter(filtered_predicates) },
606            };
607        }
608        gather_explicit_predicates_of(tcx, def_id)
609    }
610}
611
612/// Ensures that the super-predicates of the trait with a `DefId`
613/// of `trait_def_id` are lowered and stored. This also ensures that
614/// the transitive super-predicates are lowered.
615pub(super) fn explicit_super_predicates_of<'tcx>(
616    tcx: TyCtxt<'tcx>,
617    trait_def_id: LocalDefId,
618) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
619    implied_predicates_with_filter(tcx, trait_def_id.to_def_id(), PredicateFilter::SelfOnly)
620}
621
622pub(super) fn explicit_supertraits_containing_assoc_item<'tcx>(
623    tcx: TyCtxt<'tcx>,
624    (trait_def_id, assoc_ident): (DefId, Ident),
625) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
626    implied_predicates_with_filter(
627        tcx,
628        trait_def_id,
629        PredicateFilter::SelfTraitThatDefines(assoc_ident),
630    )
631}
632
633pub(super) fn explicit_implied_predicates_of<'tcx>(
634    tcx: TyCtxt<'tcx>,
635    trait_def_id: LocalDefId,
636) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
637    implied_predicates_with_filter(
638        tcx,
639        trait_def_id.to_def_id(),
640        if tcx.is_trait_alias(trait_def_id.to_def_id()) {
641            PredicateFilter::All
642        } else {
643            PredicateFilter::SelfAndAssociatedTypeBounds
644        },
645    )
646}
647
648/// Ensures that the super-predicates of the trait with a `DefId`
649/// of `trait_def_id` are lowered and stored. This also ensures that
650/// the transitive super-predicates are lowered.
651pub(super) fn implied_predicates_with_filter<'tcx>(
652    tcx: TyCtxt<'tcx>,
653    trait_def_id: DefId,
654    filter: PredicateFilter,
655) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
656    let Some(trait_def_id) = trait_def_id.as_local() else {
657        // if `assoc_ident` is None, then the query should've been redirected to an
658        // external provider
659        {
    match filter {
        PredicateFilter::SelfTraitThatDefines(_) => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "PredicateFilter::SelfTraitThatDefines(_)",
                ::core::option::Option::None);
        }
    }
};assert_matches!(filter, PredicateFilter::SelfTraitThatDefines(_));
660        return tcx.explicit_super_predicates_of(trait_def_id);
661    };
662
663    let Node::Item(item) = tcx.hir_node_by_def_id(trait_def_id) else {
664        ::rustc_middle::util::bug::bug_fmt(format_args!("trait_def_id {0:?} is not an item",
        trait_def_id));bug!("trait_def_id {trait_def_id:?} is not an item");
665    };
666
667    let (generics, superbounds) = match item.kind {
668        hir::ItemKind::Trait { generics, bounds: supertraits, .. } => (generics, supertraits),
669        hir::ItemKind::TraitAlias(_, _, generics, supertraits) => (generics, supertraits),
670        _ => ::rustc_middle::util::bug::span_bug_fmt(item.span,
    format_args!("super_predicates invoked on non-trait"))span_bug!(item.span, "super_predicates invoked on non-trait"),
671    };
672
673    let icx = ItemCtxt::new(tcx, trait_def_id);
674
675    let self_param_ty = tcx.types.self_param;
676    let mut bounds = Vec::new();
677    icx.lowerer().lower_bounds(
678        self_param_ty,
679        superbounds,
680        &mut bounds,
681        ty::List::empty(),
682        filter,
683        OverlappingAsssocItemConstraints::Allowed,
684    );
685    match filter {
686        PredicateFilter::All
687        | PredicateFilter::SelfOnly
688        | PredicateFilter::SelfTraitThatDefines(_)
689        | PredicateFilter::SelfAndAssociatedTypeBounds => {
690            icx.lowerer().add_implicit_sizedness_bounds(
691                &mut bounds,
692                self_param_ty,
693                superbounds,
694                ImpliedBoundsContext::TraitDef(trait_def_id),
695                item.span,
696            );
697            icx.lowerer().add_default_traits(
698                &mut bounds,
699                self_param_ty,
700                superbounds,
701                ImpliedBoundsContext::TraitDef(trait_def_id),
702                item.span,
703            );
704        }
705        //`ConstIfConst` is only interested in `[const]` bounds.
706        PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
707    }
708
709    let where_bounds_that_match =
710        icx.probe_ty_param_bounds_in_generics(generics, item.owner_id.def_id, filter);
711
712    // Combine the two lists to form the complete set of superbounds:
713    let implied_bounds =
714        &*tcx.arena.alloc_from_iter(bounds.into_iter().chain(where_bounds_that_match));
715    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/collect/predicates_of.rs:715",
                        "rustc_hir_analysis::collect::predicates_of",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
                        ::tracing_core::__macro_support::Option::Some(715u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
                        ::tracing_core::field::FieldSet::new(&["implied_bounds"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&implied_bounds)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?implied_bounds);
716
717    // Now require that immediate supertraits are lowered, which will, in
718    // turn, reach indirect supertraits, so we detect cycles now instead of
719    // overflowing during elaboration. Same for implied predicates, which
720    // make sure we walk into associated type bounds.
721    match filter {
722        PredicateFilter::SelfOnly => {
723            for &(pred, span) in implied_bounds {
724                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/collect/predicates_of.rs:724",
                        "rustc_hir_analysis::collect::predicates_of",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
                        ::tracing_core::__macro_support::Option::Some(724u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("superbound: {0:?}",
                                                    pred) as &dyn Value))])
            });
    } else { ; }
};debug!("superbound: {:?}", pred);
725                if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
726                    && bound.polarity == ty::PredicatePolarity::Positive
727                {
728                    tcx.at(span).explicit_super_predicates_of(bound.def_id());
729                }
730            }
731        }
732        PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
733            for &(pred, span) in implied_bounds {
734                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/collect/predicates_of.rs:734",
                        "rustc_hir_analysis::collect::predicates_of",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
                        ::tracing_core::__macro_support::Option::Some(734u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("superbound: {0:?}",
                                                    pred) as &dyn Value))])
            });
    } else { ; }
};debug!("superbound: {:?}", pred);
735                if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
736                    && bound.polarity == ty::PredicatePolarity::Positive
737                {
738                    tcx.at(span).explicit_implied_predicates_of(bound.def_id());
739                }
740            }
741        }
742        _ => {}
743    }
744
745    assert_only_contains_predicates_from(filter, implied_bounds, tcx.types.self_param);
746
747    ty::EarlyBinder::bind_iter(implied_bounds)
748}
749
750// Make sure when elaborating supertraits, probing for associated types, etc.,
751// we really truly are elaborating clauses that have `ty` as their self type.
752// This is very important since downstream code relies on this being correct.
753pub(super) fn assert_only_contains_predicates_from<'tcx>(
754    filter: PredicateFilter,
755    bounds: &'tcx [(ty::Clause<'tcx>, Span)],
756    ty: Ty<'tcx>,
757) {
758    if !truecfg!(debug_assertions) {
759        return;
760    }
761
762    match filter {
763        PredicateFilter::SelfOnly => {
764            for (clause, _) in bounds {
765                match clause.kind().skip_binder() {
766                    ty::ClauseKind::Trait(trait_predicate) => {
767                        {
    match (&trait_predicate.self_ty(), &ty) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("expected `Self` predicate when computing `{0:?}` implied bounds: {1:?}",
                            filter, clause)));
            }
        }
    }
};assert_eq!(
768                            trait_predicate.self_ty(),
769                            ty,
770                            "expected `Self` predicate when computing \
771                            `{filter:?}` implied bounds: {clause:?}"
772                        );
773                    }
774                    ty::ClauseKind::Projection(projection_predicate) => {
775                        {
    match (&projection_predicate.self_ty(), &ty) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("expected `Self` predicate when computing `{0:?}` implied bounds: {1:?}",
                            filter, clause)));
            }
        }
    }
};assert_eq!(
776                            projection_predicate.self_ty(),
777                            ty,
778                            "expected `Self` predicate when computing \
779                            `{filter:?}` implied bounds: {clause:?}"
780                        );
781                    }
782                    ty::ClauseKind::TypeOutlives(outlives_predicate) => {
783                        {
    match (&outlives_predicate.0, &ty) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("expected `Self` predicate when computing `{0:?}` implied bounds: {1:?}",
                            filter, clause)));
            }
        }
    }
};assert_eq!(
784                            outlives_predicate.0, ty,
785                            "expected `Self` predicate when computing \
786                            `{filter:?}` implied bounds: {clause:?}"
787                        );
788                    }
789                    ty::ClauseKind::HostEffect(host_effect_predicate) => {
790                        {
    match (&host_effect_predicate.self_ty(), &ty) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("expected `Self` predicate when computing `{0:?}` implied bounds: {1:?}",
                            filter, clause)));
            }
        }
    }
};assert_eq!(
791                            host_effect_predicate.self_ty(),
792                            ty,
793                            "expected `Self` predicate when computing \
794                            `{filter:?}` implied bounds: {clause:?}"
795                        );
796                    }
797
798                    ty::ClauseKind::RegionOutlives(_)
799                    | ty::ClauseKind::ConstArgHasType(_, _)
800                    | ty::ClauseKind::WellFormed(_)
801                    | ty::ClauseKind::UnstableFeature(_)
802                    | ty::ClauseKind::ConstEvaluatable(_) => {
803                        ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-`Self` predicate when computing `{0:?}` implied bounds: {1:?}",
        filter, clause));bug!(
804                            "unexpected non-`Self` predicate when computing \
805                            `{filter:?}` implied bounds: {clause:?}"
806                        );
807                    }
808                }
809            }
810        }
811        PredicateFilter::SelfTraitThatDefines(_) => {
812            for (clause, _) in bounds {
813                match clause.kind().skip_binder() {
814                    ty::ClauseKind::Trait(trait_predicate) => {
815                        {
    match (&trait_predicate.self_ty(), &ty) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("expected `Self` predicate when computing `{0:?}` implied bounds: {1:?}",
                            filter, clause)));
            }
        }
    }
};assert_eq!(
816                            trait_predicate.self_ty(),
817                            ty,
818                            "expected `Self` predicate when computing \
819                            `{filter:?}` implied bounds: {clause:?}"
820                        );
821                    }
822
823                    ty::ClauseKind::Projection(_)
824                    | ty::ClauseKind::TypeOutlives(_)
825                    | ty::ClauseKind::RegionOutlives(_)
826                    | ty::ClauseKind::ConstArgHasType(_, _)
827                    | ty::ClauseKind::WellFormed(_)
828                    | ty::ClauseKind::ConstEvaluatable(_)
829                    | ty::ClauseKind::UnstableFeature(_)
830                    | ty::ClauseKind::HostEffect(..) => {
831                        ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-`Self` predicate when computing `{0:?}` implied bounds: {1:?}",
        filter, clause));bug!(
832                            "unexpected non-`Self` predicate when computing \
833                            `{filter:?}` implied bounds: {clause:?}"
834                        );
835                    }
836                }
837            }
838        }
839        PredicateFilter::ConstIfConst => {
840            for (clause, _) in bounds {
841                match clause.kind().skip_binder() {
842                    ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
843                        trait_ref: _,
844                        constness: ty::BoundConstness::Maybe,
845                    }) => {}
846                    _ => {
847                        ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-`HostEffect` predicate when computing `{0:?}` implied bounds: {1:?}",
        filter, clause));bug!(
848                            "unexpected non-`HostEffect` predicate when computing \
849                            `{filter:?}` implied bounds: {clause:?}"
850                        );
851                    }
852                }
853            }
854        }
855        PredicateFilter::SelfConstIfConst => {
856            for (clause, _) in bounds {
857                match clause.kind().skip_binder() {
858                    ty::ClauseKind::HostEffect(pred) => {
859                        {
    match (&pred.constness, &ty::BoundConstness::Maybe) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("expected `[const]` predicate when computing `{0:?}` implied bounds: {1:?}",
                            filter, clause)));
            }
        }
    }
};assert_eq!(
860                            pred.constness,
861                            ty::BoundConstness::Maybe,
862                            "expected `[const]` predicate when computing `{filter:?}` \
863                            implied bounds: {clause:?}",
864                        );
865                        {
    match (&pred.trait_ref.self_ty(), &ty) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val,
                    ::core::option::Option::Some(format_args!("expected `Self` predicate when computing `{0:?}` implied bounds: {1:?}",
                            filter, clause)));
            }
        }
    }
};assert_eq!(
866                            pred.trait_ref.self_ty(),
867                            ty,
868                            "expected `Self` predicate when computing `{filter:?}` \
869                            implied bounds: {clause:?}"
870                        );
871                    }
872                    _ => {
873                        ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-`HostEffect` predicate when computing `{0:?}` implied bounds: {1:?}",
        filter, clause));bug!(
874                            "unexpected non-`HostEffect` predicate when computing \
875                            `{filter:?}` implied bounds: {clause:?}"
876                        );
877                    }
878                }
879            }
880        }
881        PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {}
882    }
883}
884
885/// Returns the predicates defined on `item_def_id` of the form
886/// `X: Foo` where `X` is the type parameter `def_id`.
887#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("type_param_predicates",
                                    "rustc_hir_analysis::collect::predicates_of",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
                                    ::tracing_core::__macro_support::Option::Some(887u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
                                    ::tracing_core::field::FieldSet::new(&["item_def_id",
                                                    "def_id", "assoc_ident"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&item_def_id)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&def_id)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&assoc_ident)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return:
                    ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            match tcx.opt_rpitit_info(item_def_id.to_def_id()) {
                Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) =>
                    {
                    return tcx.type_param_predicates((opaque_def_id.expect_local(),
                                def_id, assoc_ident));
                }
                Some(ty::ImplTraitInTraitData::Impl { .. }) => {
                    {
                        ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
                                format_args!("should not be lowering bounds on RPITIT in impl")));
                    }
                }
                None => {}
            }
            let param_id = tcx.local_def_id_to_hir_id(def_id);
            let param_owner = tcx.hir_ty_param_owner(def_id);
            let parent =
                if item_def_id == param_owner {
                    None
                } else {
                    tcx.generics_of(item_def_id).parent.map(|def_id|
                            def_id.expect_local())
                };
            let result =
                if let Some(parent) = parent {
                    let icx = ItemCtxt::new(tcx, parent);
                    icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_ident)
                } else { ty::EarlyBinder::bind_iter(&[] as &[_]) };
            let mut extend = None;
            let item_hir_id = tcx.local_def_id_to_hir_id(item_def_id);
            let hir_node = tcx.hir_node(item_hir_id);
            let Some(hir_generics) =
                hir_node.generics() else { return result; };
            if let Node::Item(item) = hir_node &&
                        let hir::ItemKind::Trait { .. } = item.kind &&
                    param_id == item_hir_id {
                let identity_trait_ref =
                    ty::TraitRef::identity(tcx, item_def_id.to_def_id());
                extend = Some((identity_trait_ref.upcast(tcx), item.span));
            }
            let icx = ItemCtxt::new(tcx, item_def_id);
            let extra_predicates =
                extend.into_iter().chain(icx.probe_ty_param_bounds_in_generics(hir_generics,
                        def_id,
                        PredicateFilter::SelfTraitThatDefines(assoc_ident)));
            let bounds =
                &*tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates));
            let self_ty =
                match tcx.def_kind(def_id) {
                    DefKind::TyParam =>
                        Ty::new_param(tcx,
                            tcx.generics_of(item_def_id).param_def_id_to_index(tcx,
                                    def_id.to_def_id()).expect("expected generic param to be owned by item"),
                            tcx.item_name(def_id.to_def_id())),
                    DefKind::Trait | DefKind::TraitAlias =>
                        tcx.types.self_param,
                    _ =>
                        ::core::panicking::panic("internal error: entered unreachable code"),
                };
            assert_only_contains_predicates_from(PredicateFilter::SelfTraitThatDefines(assoc_ident),
                bounds, self_ty);
            ty::EarlyBinder::bind_iter(bounds)
        }
    }
}#[instrument(level = "trace", skip(tcx))]
888pub(super) fn type_param_predicates<'tcx>(
889    tcx: TyCtxt<'tcx>,
890    (item_def_id, def_id, assoc_ident): (LocalDefId, LocalDefId, Ident),
891) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
892    match tcx.opt_rpitit_info(item_def_id.to_def_id()) {
893        Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
894            return tcx.type_param_predicates((opaque_def_id.expect_local(), def_id, assoc_ident));
895        }
896        Some(ty::ImplTraitInTraitData::Impl { .. }) => {
897            unreachable!("should not be lowering bounds on RPITIT in impl")
898        }
899        None => {}
900    }
901
902    // In the HIR, bounds can derive from two places. Either
903    // written inline like `<T: Foo>` or in a where-clause like
904    // `where T: Foo`.
905
906    let param_id = tcx.local_def_id_to_hir_id(def_id);
907    let param_owner = tcx.hir_ty_param_owner(def_id);
908
909    // Don't look for bounds where the type parameter isn't in scope.
910    let parent = if item_def_id == param_owner {
911        // FIXME: Shouldn't this be unreachable?
912        None
913    } else {
914        tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local())
915    };
916
917    let result = if let Some(parent) = parent {
918        let icx = ItemCtxt::new(tcx, parent);
919        icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_ident)
920    } else {
921        ty::EarlyBinder::bind_iter(&[] as &[_])
922    };
923    let mut extend = None;
924
925    let item_hir_id = tcx.local_def_id_to_hir_id(item_def_id);
926
927    let hir_node = tcx.hir_node(item_hir_id);
928    let Some(hir_generics) = hir_node.generics() else {
929        return result;
930    };
931
932    if let Node::Item(item) = hir_node
933        && let hir::ItemKind::Trait { .. } = item.kind
934        // Implied `Self: Trait` and supertrait bounds.
935        && param_id == item_hir_id
936    {
937        let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id.to_def_id());
938        extend = Some((identity_trait_ref.upcast(tcx), item.span));
939    }
940
941    let icx = ItemCtxt::new(tcx, item_def_id);
942    let extra_predicates = extend.into_iter().chain(icx.probe_ty_param_bounds_in_generics(
943        hir_generics,
944        def_id,
945        PredicateFilter::SelfTraitThatDefines(assoc_ident),
946    ));
947
948    let bounds =
949        &*tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates));
950
951    // Double check that the bounds *only* contain `SelfTy: Trait` preds.
952    let self_ty = match tcx.def_kind(def_id) {
953        DefKind::TyParam => Ty::new_param(
954            tcx,
955            tcx.generics_of(item_def_id)
956                .param_def_id_to_index(tcx, def_id.to_def_id())
957                .expect("expected generic param to be owned by item"),
958            tcx.item_name(def_id.to_def_id()),
959        ),
960        DefKind::Trait | DefKind::TraitAlias => tcx.types.self_param,
961        _ => unreachable!(),
962    };
963    assert_only_contains_predicates_from(
964        PredicateFilter::SelfTraitThatDefines(assoc_ident),
965        bounds,
966        self_ty,
967    );
968
969    ty::EarlyBinder::bind_iter(bounds)
970}
971
972impl<'tcx> ItemCtxt<'tcx> {
973    /// Finds bounds from `hir::Generics`.
974    ///
975    /// This requires scanning through the HIR.
976    /// We do this to avoid having to lower *all* the bounds, which would create artificial cycles.
977    /// Instead, we can only lower the bounds for a type parameter `X` if `X::Foo` is used.
978    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("probe_ty_param_bounds_in_generics",
                                    "rustc_hir_analysis::collect::predicates_of",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/collect/predicates_of.rs"),
                                    ::tracing_core::__macro_support::Option::Some(978u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::collect::predicates_of"),
                                    ::tracing_core::field::FieldSet::new(&["param_def_id",
                                                    "filter"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&param_def_id)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&filter)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Vec<(ty::Clause<'tcx>, Span)> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let mut bounds = Vec::new();
            if let PredicateFilter::All = filter {
                for param in hir_generics.params {
                    match param.kind {
                        hir::GenericParamKind::Type { .. } => {
                            let param_ty = self.lowerer().lower_ty_param(param.hir_id);
                            self.lowerer().add_implicit_sizedness_bounds(&mut bounds,
                                param_ty, &[],
                                ImpliedBoundsContext::TyParam(param.def_id,
                                    hir_generics.predicates), param.span);
                            self.lowerer().add_default_traits(&mut bounds, param_ty,
                                &[],
                                ImpliedBoundsContext::TyParam(param.def_id,
                                    hir_generics.predicates), param.span);
                        }
                        hir::GenericParamKind::Lifetime { .. } |
                            hir::GenericParamKind::Const { .. } => {}
                    }
                }
            }
            for predicate in hir_generics.predicates {
                let hir_id = predicate.hir_id;
                let hir::WherePredicateKind::BoundPredicate(predicate) =
                    predicate.kind else { continue; };
                match filter {
                    _ if predicate.is_param_bound(param_def_id.to_def_id()) =>
                        {}
                    PredicateFilter::All => {}
                    PredicateFilter::SelfOnly |
                        PredicateFilter::SelfTraitThatDefines(_) |
                        PredicateFilter::SelfConstIfConst |
                        PredicateFilter::SelfAndAssociatedTypeBounds => continue,
                    PredicateFilter::ConstIfConst =>
                        ::core::panicking::panic("internal error: entered unreachable code"),
                }
                let bound_ty =
                    self.lowerer().lower_ty_maybe_return_type_notation(predicate.bounded_ty);
                let bound_vars = self.tcx.late_bound_vars(hir_id);
                self.lowerer().lower_bounds(bound_ty, predicate.bounds,
                    &mut bounds, bound_vars, filter,
                    OverlappingAsssocItemConstraints::Allowed);
            }
            bounds
        }
    }
}#[instrument(level = "trace", skip(self, hir_generics))]
979    fn probe_ty_param_bounds_in_generics(
980        &self,
981        hir_generics: &'tcx hir::Generics<'tcx>,
982        param_def_id: LocalDefId,
983        filter: PredicateFilter,
984    ) -> Vec<(ty::Clause<'tcx>, Span)> {
985        let mut bounds = Vec::new();
986
987        if let PredicateFilter::All = filter {
988            for param in hir_generics.params {
989                match param.kind {
990                    hir::GenericParamKind::Type { .. } => {
991                        let param_ty = self.lowerer().lower_ty_param(param.hir_id);
992                        self.lowerer().add_implicit_sizedness_bounds(
993                            &mut bounds,
994                            param_ty,
995                            &[],
996                            ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
997                            param.span,
998                        );
999                        self.lowerer().add_default_traits(
1000                            &mut bounds,
1001                            param_ty,
1002                            &[],
1003                            ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
1004                            param.span,
1005                        );
1006                    }
1007                    hir::GenericParamKind::Lifetime { .. }
1008                    | hir::GenericParamKind::Const { .. } => {}
1009                }
1010            }
1011        }
1012
1013        for predicate in hir_generics.predicates {
1014            let hir_id = predicate.hir_id;
1015            let hir::WherePredicateKind::BoundPredicate(predicate) = predicate.kind else {
1016                continue;
1017            };
1018
1019            match filter {
1020                _ if predicate.is_param_bound(param_def_id.to_def_id()) => {
1021                    // Ok
1022                }
1023                PredicateFilter::All => {
1024                    // Ok
1025                }
1026                PredicateFilter::SelfOnly
1027                | PredicateFilter::SelfTraitThatDefines(_)
1028                | PredicateFilter::SelfConstIfConst
1029                | PredicateFilter::SelfAndAssociatedTypeBounds => continue,
1030                PredicateFilter::ConstIfConst => unreachable!(),
1031            }
1032
1033            let bound_ty = self.lowerer().lower_ty_maybe_return_type_notation(predicate.bounded_ty);
1034
1035            let bound_vars = self.tcx.late_bound_vars(hir_id);
1036            self.lowerer().lower_bounds(
1037                bound_ty,
1038                predicate.bounds,
1039                &mut bounds,
1040                bound_vars,
1041                filter,
1042                OverlappingAsssocItemConstraints::Allowed,
1043            );
1044        }
1045
1046        bounds
1047    }
1048}
1049
1050pub(super) fn const_conditions<'tcx>(
1051    tcx: TyCtxt<'tcx>,
1052    def_id: LocalDefId,
1053) -> ty::ConstConditions<'tcx> {
1054    if !tcx.is_conditionally_const(def_id) {
1055        ::rustc_middle::util::bug::bug_fmt(format_args!("const_conditions invoked for item that is not conditionally const: {0:?}",
        def_id));bug!("const_conditions invoked for item that is not conditionally const: {def_id:?}");
1056    }
1057
1058    match tcx.opt_rpitit_info(def_id.to_def_id()) {
1059        // RPITITs inherit const conditions of their parent fn
1060        Some(
1061            ty::ImplTraitInTraitData::Impl { fn_def_id }
1062            | ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
1063        ) => return tcx.const_conditions(fn_def_id),
1064        None => {}
1065    }
1066
1067    let (generics, trait_def_id_and_supertraits, has_parent) = match tcx.hir_node_by_def_id(def_id)
1068    {
1069        Node::Item(item) => match item.kind {
1070            hir::ItemKind::Impl(impl_) => (impl_.generics, None, false),
1071            hir::ItemKind::Fn { generics, .. } => (generics, None, false),
1072            hir::ItemKind::Trait { generics, bounds: supertraits, .. } => {
1073                (generics, Some((Some(item.owner_id.def_id), supertraits)), false)
1074            }
1075            hir::ItemKind::TraitAlias(_, _, generics, supertraits) => {
1076                (generics, Some((None, supertraits)), false)
1077            }
1078            _ => ::rustc_middle::util::bug::bug_fmt(format_args!("const_conditions called on wrong item: {0:?}",
        def_id))bug!("const_conditions called on wrong item: {def_id:?}"),
1079        },
1080        // While associated types are not really const, we do allow them to have `[const]`
1081        // bounds and where clauses. `const_conditions` is responsible for gathering
1082        // these up so we can check them in `compare_type_predicate_entailment`, and
1083        // in `HostEffect` goal computation.
1084        Node::TraitItem(item) => match item.kind {
1085            hir::TraitItemKind::Fn(_, _) | hir::TraitItemKind::Type(_, _) => {
1086                (item.generics, None, true)
1087            }
1088            _ => ::rustc_middle::util::bug::bug_fmt(format_args!("const_conditions called on wrong item: {0:?}",
        def_id))bug!("const_conditions called on wrong item: {def_id:?}"),
1089        },
1090        Node::ImplItem(item) => match item.kind {
1091            hir::ImplItemKind::Fn(_, _) | hir::ImplItemKind::Type(_) => {
1092                (item.generics, None, tcx.is_conditionally_const(tcx.local_parent(def_id)))
1093            }
1094            _ => ::rustc_middle::util::bug::bug_fmt(format_args!("const_conditions called on wrong item: {0:?}",
        def_id))bug!("const_conditions called on wrong item: {def_id:?}"),
1095        },
1096        Node::ForeignItem(item) => match item.kind {
1097            hir::ForeignItemKind::Fn(_, _, generics) => (generics, None, false),
1098            _ => ::rustc_middle::util::bug::bug_fmt(format_args!("const_conditions called on wrong item: {0:?}",
        def_id))bug!("const_conditions called on wrong item: {def_id:?}"),
1099        },
1100        Node::OpaqueTy(opaque) => match opaque.origin {
1101            hir::OpaqueTyOrigin::FnReturn { parent, .. } => return tcx.const_conditions(parent),
1102            hir::OpaqueTyOrigin::AsyncFn { .. } | hir::OpaqueTyOrigin::TyAlias { .. } => {
1103                ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
1104            }
1105        },
1106        // N.B. Tuple ctors are unconditionally constant.
1107        Node::Ctor(hir::VariantData::Tuple { .. }) => return Default::default(),
1108        Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(_), .. }) => {
1109            (hir::Generics::empty(), None, tcx.is_conditionally_const(tcx.local_parent(def_id)))
1110        }
1111        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("const_conditions called on wrong item: {0:?}",
        def_id))bug!("const_conditions called on wrong item: {def_id:?}"),
1112    };
1113
1114    let icx = ItemCtxt::new(tcx, def_id);
1115    let mut bounds = Vec::new();
1116
1117    for pred in generics.predicates {
1118        match pred.kind {
1119            hir::WherePredicateKind::BoundPredicate(bound_pred) => {
1120                let ty = icx.lowerer().lower_ty_maybe_return_type_notation(bound_pred.bounded_ty);
1121                let bound_vars = tcx.late_bound_vars(pred.hir_id);
1122                icx.lowerer().lower_bounds(
1123                    ty,
1124                    bound_pred.bounds.iter(),
1125                    &mut bounds,
1126                    bound_vars,
1127                    PredicateFilter::ConstIfConst,
1128                    OverlappingAsssocItemConstraints::Allowed,
1129                );
1130            }
1131            _ => {}
1132        }
1133    }
1134
1135    if let Some((def_id, supertraits)) = trait_def_id_and_supertraits {
1136        if let Some(def_id) = def_id {
1137            // We've checked above that the trait is conditionally const.
1138            bounds.push((
1139                ty::Binder::dummy(ty::TraitRef::identity(tcx, def_id.to_def_id()))
1140                    .to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
1141                DUMMY_SP,
1142            ));
1143        }
1144
1145        icx.lowerer().lower_bounds(
1146            tcx.types.self_param,
1147            supertraits,
1148            &mut bounds,
1149            ty::List::empty(),
1150            PredicateFilter::ConstIfConst,
1151            OverlappingAsssocItemConstraints::Allowed,
1152        );
1153    }
1154
1155    ty::ConstConditions {
1156        parent: has_parent.then(|| tcx.local_parent(def_id).to_def_id()),
1157        predicates: tcx.arena.alloc_from_iter(bounds.into_iter().map(|(clause, span)| {
1158            (
1159                clause.kind().map_bound(|clause| match clause {
1160                    ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
1161                        trait_ref,
1162                        constness: ty::BoundConstness::Maybe,
1163                    }) => trait_ref,
1164                    _ => ::rustc_middle::util::bug::bug_fmt(format_args!("converted {0:?}", clause))bug!("converted {clause:?}"),
1165                }),
1166                span,
1167            )
1168        })),
1169    }
1170}
1171
1172pub(super) fn explicit_implied_const_bounds<'tcx>(
1173    tcx: TyCtxt<'tcx>,
1174    def_id: LocalDefId,
1175) -> ty::EarlyBinder<'tcx, &'tcx [(ty::PolyTraitRef<'tcx>, Span)]> {
1176    if !tcx.is_conditionally_const(def_id) {
1177        ::rustc_middle::util::bug::bug_fmt(format_args!("explicit_implied_const_bounds invoked for item that is not conditionally const: {0:?}",
        def_id));bug!(
1178            "explicit_implied_const_bounds invoked for item that is not conditionally const: {def_id:?}"
1179        );
1180    }
1181
1182    let bounds = match tcx.opt_rpitit_info(def_id.to_def_id()) {
1183        // RPITIT's bounds are the same as opaque type bounds, but with
1184        // a projection self type.
1185        Some(ty::ImplTraitInTraitData::Trait { .. }) => {
1186            explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
1187        }
1188        Some(ty::ImplTraitInTraitData::Impl { .. }) => {
1189            ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(def_id),
    format_args!("RPITIT in impl should not have item bounds"))span_bug!(tcx.def_span(def_id), "RPITIT in impl should not have item bounds")
1190        }
1191        None => match tcx.hir_node_by_def_id(def_id) {
1192            Node::Item(hir::Item {
1193                kind: hir::ItemKind::Trait { .. } | hir::ItemKind::TraitAlias(..),
1194                ..
1195            }) => implied_predicates_with_filter(
1196                tcx,
1197                def_id.to_def_id(),
1198                PredicateFilter::SelfConstIfConst,
1199            ),
1200            Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
1201            | Node::OpaqueTy(_) => {
1202                explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
1203            }
1204            _ => ::rustc_middle::util::bug::bug_fmt(format_args!("explicit_implied_const_bounds called on wrong item: {0:?}",
        def_id))bug!("explicit_implied_const_bounds called on wrong item: {def_id:?}"),
1205        },
1206    };
1207
1208    bounds.map_bound(|bounds| {
1209        &*tcx.arena.alloc_from_iter(bounds.iter().copied().map(|(clause, span)| {
1210            (
1211                clause.kind().map_bound(|clause| match clause {
1212                    ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
1213                        trait_ref,
1214                        constness: ty::BoundConstness::Maybe,
1215                    }) => trait_ref,
1216                    _ => ::rustc_middle::util::bug::bug_fmt(format_args!("converted {0:?}", clause))bug!("converted {clause:?}"),
1217                }),
1218                span,
1219            )
1220        }))
1221    })
1222}