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::UnevaluatedConstKind<'_>) -> bool {
419        let ty::UnevaluatedConstKind::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::Unevaluated(uv) = c.kind() {
437                if is_const_param_default(self.tcx, uv.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 uv.kind.is_type_const(self.tcx) {
450                    return;
451                }
452
453                let span = uv.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(ty::AliasTy {
523                kind: ty::Projection { def_id: projection_def_id },
524                args,
525                ..
526            }) = ty.kind()
527            {
528                args == trait_identity_args
529                    // FIXME(return_type_notation): This check should be more robust
530                    && !tcx.is_impl_trait_in_trait(projection_def_id)
531                    && tcx.parent(projection_def_id) == def_id.to_def_id()
532            } else {
533                false
534            }
535        };
536
537        let predicates: Vec<_> = predicates_and_bounds
538            .predicates
539            .iter()
540            .copied()
541            .filter(|(pred, _)| match pred.kind().skip_binder() {
542                ty::ClauseKind::Trait(tr) => !is_assoc_item_ty(tr.self_ty()),
543                ty::ClauseKind::Projection(proj) => {
544                    !is_assoc_item_ty(proj.projection_term.self_ty())
545                }
546                ty::ClauseKind::TypeOutlives(outlives) => !is_assoc_item_ty(outlives.0),
547                _ => true,
548            })
549            .collect();
550        if predicates.len() == predicates_and_bounds.predicates.len() {
551            predicates_and_bounds
552        } else {
553            ty::GenericPredicates {
554                parent: predicates_and_bounds.parent,
555                predicates: tcx.arena.alloc_slice(&predicates),
556            }
557        }
558    } else {
559        if def_kind == DefKind::AnonConst
560            && tcx.features().generic_const_exprs()
561            && let Some(defaulted_param_def_id) =
562                tcx.hir_opt_const_param_default_param_def_id(tcx.local_def_id_to_hir_id(def_id))
563        {
564            // In `generics_of` we set the generics' parent to be our parent's parent which means that
565            // we lose out on the predicates of our actual parent if we dont return those predicates here.
566            // (See comment in `generics_of` for more information on why the parent shenanigans is necessary)
567            //
568            // struct Foo<T, const N: usize = { <T as Trait>::ASSOC }>(T) where T: Trait;
569            //        ^^^                     ^^^^^^^^^^^^^^^^^^^^^^^ the def id we are calling
570            //        ^^^                                             explicit_predicates_of on
571            //        parent item we dont have set as the
572            //        parent of generics returned by `generics_of`
573            //
574            // In the above code we want the anon const to have predicates in its param env for `T: Trait`
575            // and we would be calling `explicit_predicates_of(Foo)` here
576            let parent_def_id = tcx.local_parent(def_id);
577            let parent_preds = tcx.explicit_predicates_of(parent_def_id);
578
579            // If we dont filter out `ConstArgHasType` predicates then every single defaulted const parameter
580            // will ICE because of #106994. FIXME(generic_const_exprs): remove this when a more general solution
581            // to #106994 is implemented.
582            let filtered_predicates = parent_preds
583                .predicates
584                .into_iter()
585                .filter(|(pred, _)| {
586                    if let ty::ClauseKind::ConstArgHasType(ct, _) = pred.kind().skip_binder() {
587                        match ct.kind() {
588                            ty::ConstKind::Param(param_const) => {
589                                let defaulted_param_idx = tcx
590                                    .generics_of(parent_def_id)
591                                    .param_def_id_to_index[&defaulted_param_def_id.to_def_id()];
592                                param_const.index < defaulted_param_idx
593                            }
594                            _ => ::rustc_middle::util::bug::bug_fmt(format_args!("`ConstArgHasType` in `predicates_of`that isn\'t a `Param` const"))bug!(
595                                "`ConstArgHasType` in `predicates_of`\
596                                 that isn't a `Param` const"
597                            ),
598                        }
599                    } else {
600                        true
601                    }
602                })
603                .cloned();
604            return GenericPredicates {
605                parent: parent_preds.parent,
606                predicates: { tcx.arena.alloc_from_iter(filtered_predicates) },
607            };
608        }
609        gather_explicit_predicates_of(tcx, def_id)
610    }
611}
612
613/// Ensures that the super-predicates of the trait with a `DefId`
614/// of `trait_def_id` are lowered and stored. This also ensures that
615/// the transitive super-predicates are lowered.
616pub(super) fn explicit_super_predicates_of<'tcx>(
617    tcx: TyCtxt<'tcx>,
618    trait_def_id: LocalDefId,
619) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
620    implied_predicates_with_filter(tcx, trait_def_id.to_def_id(), PredicateFilter::SelfOnly)
621}
622
623pub(super) fn explicit_supertraits_containing_assoc_item<'tcx>(
624    tcx: TyCtxt<'tcx>,
625    (trait_def_id, assoc_ident): (DefId, Ident),
626) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
627    implied_predicates_with_filter(
628        tcx,
629        trait_def_id,
630        PredicateFilter::SelfTraitThatDefines(assoc_ident),
631    )
632}
633
634pub(super) fn explicit_implied_predicates_of<'tcx>(
635    tcx: TyCtxt<'tcx>,
636    trait_def_id: LocalDefId,
637) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
638    implied_predicates_with_filter(
639        tcx,
640        trait_def_id.to_def_id(),
641        if tcx.is_trait_alias(trait_def_id.to_def_id()) {
642            PredicateFilter::All
643        } else {
644            PredicateFilter::SelfAndAssociatedTypeBounds
645        },
646    )
647}
648
649/// Ensures that the super-predicates of the trait with a `DefId`
650/// of `trait_def_id` are lowered and stored. This also ensures that
651/// the transitive super-predicates are lowered.
652pub(super) fn implied_predicates_with_filter<'tcx>(
653    tcx: TyCtxt<'tcx>,
654    trait_def_id: DefId,
655    filter: PredicateFilter,
656) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
657    let Some(trait_def_id) = trait_def_id.as_local() else {
658        // if `assoc_ident` is None, then the query should've been redirected to an
659        // external provider
660        {
    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(_));
661        return tcx.explicit_super_predicates_of(trait_def_id);
662    };
663
664    let Node::Item(item) = tcx.hir_node_by_def_id(trait_def_id) else {
665        ::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");
666    };
667
668    let (generics, superbounds) = match item.kind {
669        hir::ItemKind::Trait { generics, bounds: supertraits, .. } => (generics, supertraits),
670        hir::ItemKind::TraitAlias(_, _, generics, supertraits) => (generics, supertraits),
671        _ => ::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"),
672    };
673
674    let icx = ItemCtxt::new(tcx, trait_def_id);
675
676    let self_param_ty = tcx.types.self_param;
677    let mut bounds = Vec::new();
678    icx.lowerer().lower_bounds(
679        self_param_ty,
680        superbounds,
681        &mut bounds,
682        ty::List::empty(),
683        filter,
684        OverlappingAsssocItemConstraints::Allowed,
685    );
686    match filter {
687        PredicateFilter::All
688        | PredicateFilter::SelfOnly
689        | PredicateFilter::SelfTraitThatDefines(_)
690        | PredicateFilter::SelfAndAssociatedTypeBounds => {
691            icx.lowerer().add_implicit_sizedness_bounds(
692                &mut bounds,
693                self_param_ty,
694                superbounds,
695                ImpliedBoundsContext::TraitDef(trait_def_id),
696                item.span,
697            );
698            icx.lowerer().add_default_traits(
699                &mut bounds,
700                self_param_ty,
701                superbounds,
702                ImpliedBoundsContext::TraitDef(trait_def_id),
703                item.span,
704            );
705        }
706        //`ConstIfConst` is only interested in `[const]` bounds.
707        PredicateFilter::ConstIfConst | PredicateFilter::SelfConstIfConst => {}
708    }
709
710    let where_bounds_that_match =
711        icx.probe_ty_param_bounds_in_generics(generics, item.owner_id.def_id, filter);
712
713    // Combine the two lists to form the complete set of superbounds:
714    let implied_bounds =
715        &*tcx.arena.alloc_from_iter(bounds.into_iter().chain(where_bounds_that_match));
716    {
    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:716",
                        "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(716u32),
                        ::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);
717
718    // Now require that immediate supertraits are lowered, which will, in
719    // turn, reach indirect supertraits, so we detect cycles now instead of
720    // overflowing during elaboration. Same for implied predicates, which
721    // make sure we walk into associated type bounds.
722    match filter {
723        PredicateFilter::SelfOnly => {
724            for &(pred, span) in implied_bounds {
725                {
    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:725",
                        "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(725u32),
                        ::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);
726                if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
727                    && bound.polarity == ty::PredicatePolarity::Positive
728                {
729                    tcx.at(span).explicit_super_predicates_of(bound.def_id());
730                }
731            }
732        }
733        PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
734            for &(pred, span) in implied_bounds {
735                {
    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:735",
                        "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(735u32),
                        ::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);
736                if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
737                    && bound.polarity == ty::PredicatePolarity::Positive
738                {
739                    tcx.at(span).explicit_implied_predicates_of(bound.def_id());
740                }
741            }
742        }
743        _ => {}
744    }
745
746    assert_only_contains_predicates_from(filter, implied_bounds, tcx.types.self_param);
747
748    ty::EarlyBinder::bind(implied_bounds)
749}
750
751// Make sure when elaborating supertraits, probing for associated types, etc.,
752// we really truly are elaborating clauses that have `ty` as their self type.
753// This is very important since downstream code relies on this being correct.
754pub(super) fn assert_only_contains_predicates_from<'tcx>(
755    filter: PredicateFilter,
756    bounds: &'tcx [(ty::Clause<'tcx>, Span)],
757    ty: Ty<'tcx>,
758) {
759    if !truecfg!(debug_assertions) {
760        return;
761    }
762
763    match filter {
764        PredicateFilter::SelfOnly => {
765            for (clause, _) in bounds {
766                match clause.kind().skip_binder() {
767                    ty::ClauseKind::Trait(trait_predicate) => {
768                        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!(
769                            trait_predicate.self_ty(),
770                            ty,
771                            "expected `Self` predicate when computing \
772                            `{filter:?}` implied bounds: {clause:?}"
773                        );
774                    }
775                    ty::ClauseKind::Projection(projection_predicate) => {
776                        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!(
777                            projection_predicate.self_ty(),
778                            ty,
779                            "expected `Self` predicate when computing \
780                            `{filter:?}` implied bounds: {clause:?}"
781                        );
782                    }
783                    ty::ClauseKind::TypeOutlives(outlives_predicate) => {
784                        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!(
785                            outlives_predicate.0, ty,
786                            "expected `Self` predicate when computing \
787                            `{filter:?}` implied bounds: {clause:?}"
788                        );
789                    }
790                    ty::ClauseKind::HostEffect(host_effect_predicate) => {
791                        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!(
792                            host_effect_predicate.self_ty(),
793                            ty,
794                            "expected `Self` predicate when computing \
795                            `{filter:?}` implied bounds: {clause:?}"
796                        );
797                    }
798
799                    ty::ClauseKind::RegionOutlives(_)
800                    | ty::ClauseKind::ConstArgHasType(_, _)
801                    | ty::ClauseKind::WellFormed(_)
802                    | ty::ClauseKind::UnstableFeature(_)
803                    | ty::ClauseKind::ConstEvaluatable(_) => {
804                        ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-`Self` predicate when computing `{0:?}` implied bounds: {1:?}",
        filter, clause));bug!(
805                            "unexpected non-`Self` predicate when computing \
806                            `{filter:?}` implied bounds: {clause:?}"
807                        );
808                    }
809                }
810            }
811        }
812        PredicateFilter::SelfTraitThatDefines(_) => {
813            for (clause, _) in bounds {
814                match clause.kind().skip_binder() {
815                    ty::ClauseKind::Trait(trait_predicate) => {
816                        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!(
817                            trait_predicate.self_ty(),
818                            ty,
819                            "expected `Self` predicate when computing \
820                            `{filter:?}` implied bounds: {clause:?}"
821                        );
822                    }
823
824                    ty::ClauseKind::Projection(_)
825                    | ty::ClauseKind::TypeOutlives(_)
826                    | ty::ClauseKind::RegionOutlives(_)
827                    | ty::ClauseKind::ConstArgHasType(_, _)
828                    | ty::ClauseKind::WellFormed(_)
829                    | ty::ClauseKind::ConstEvaluatable(_)
830                    | ty::ClauseKind::UnstableFeature(_)
831                    | ty::ClauseKind::HostEffect(..) => {
832                        ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-`Self` predicate when computing `{0:?}` implied bounds: {1:?}",
        filter, clause));bug!(
833                            "unexpected non-`Self` predicate when computing \
834                            `{filter:?}` implied bounds: {clause:?}"
835                        );
836                    }
837                }
838            }
839        }
840        PredicateFilter::ConstIfConst => {
841            for (clause, _) in bounds {
842                match clause.kind().skip_binder() {
843                    ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
844                        trait_ref: _,
845                        constness: ty::BoundConstness::Maybe,
846                    }) => {}
847                    _ => {
848                        ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-`HostEffect` predicate when computing `{0:?}` implied bounds: {1:?}",
        filter, clause));bug!(
849                            "unexpected non-`HostEffect` predicate when computing \
850                            `{filter:?}` implied bounds: {clause:?}"
851                        );
852                    }
853                }
854            }
855        }
856        PredicateFilter::SelfConstIfConst => {
857            for (clause, _) in bounds {
858                match clause.kind().skip_binder() {
859                    ty::ClauseKind::HostEffect(pred) => {
860                        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!(
861                            pred.constness,
862                            ty::BoundConstness::Maybe,
863                            "expected `[const]` predicate when computing `{filter:?}` \
864                            implied bounds: {clause:?}",
865                        );
866                        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!(
867                            pred.trait_ref.self_ty(),
868                            ty,
869                            "expected `Self` predicate when computing `{filter:?}` \
870                            implied bounds: {clause:?}"
871                        );
872                    }
873                    _ => {
874                        ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-`HostEffect` predicate when computing `{0:?}` implied bounds: {1:?}",
        filter, clause));bug!(
875                            "unexpected non-`HostEffect` predicate when computing \
876                            `{filter:?}` implied bounds: {clause:?}"
877                        );
878                    }
879                }
880            }
881        }
882        PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {}
883    }
884}
885
886/// Returns the predicates defined on `item_def_id` of the form
887/// `X: Foo` where `X` is the type parameter `def_id`.
888#[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(888u32),
                                    ::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(&[] 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(bounds)
        }
    }
}#[instrument(level = "trace", skip(tcx))]
889pub(super) fn type_param_predicates<'tcx>(
890    tcx: TyCtxt<'tcx>,
891    (item_def_id, def_id, assoc_ident): (LocalDefId, LocalDefId, Ident),
892) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
893    match tcx.opt_rpitit_info(item_def_id.to_def_id()) {
894        Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) => {
895            return tcx.type_param_predicates((opaque_def_id.expect_local(), def_id, assoc_ident));
896        }
897        Some(ty::ImplTraitInTraitData::Impl { .. }) => {
898            unreachable!("should not be lowering bounds on RPITIT in impl")
899        }
900        None => {}
901    }
902
903    // In the HIR, bounds can derive from two places. Either
904    // written inline like `<T: Foo>` or in a where-clause like
905    // `where T: Foo`.
906
907    let param_id = tcx.local_def_id_to_hir_id(def_id);
908    let param_owner = tcx.hir_ty_param_owner(def_id);
909
910    // Don't look for bounds where the type parameter isn't in scope.
911    let parent = if item_def_id == param_owner {
912        // FIXME: Shouldn't this be unreachable?
913        None
914    } else {
915        tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local())
916    };
917
918    let result = if let Some(parent) = parent {
919        let icx = ItemCtxt::new(tcx, parent);
920        icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_ident)
921    } else {
922        ty::EarlyBinder::bind(&[] as &[_])
923    };
924    let mut extend = None;
925
926    let item_hir_id = tcx.local_def_id_to_hir_id(item_def_id);
927
928    let hir_node = tcx.hir_node(item_hir_id);
929    let Some(hir_generics) = hir_node.generics() else {
930        return result;
931    };
932
933    if let Node::Item(item) = hir_node
934        && let hir::ItemKind::Trait { .. } = item.kind
935        // Implied `Self: Trait` and supertrait bounds.
936        && param_id == item_hir_id
937    {
938        let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id.to_def_id());
939        extend = Some((identity_trait_ref.upcast(tcx), item.span));
940    }
941
942    let icx = ItemCtxt::new(tcx, item_def_id);
943    let extra_predicates = extend.into_iter().chain(icx.probe_ty_param_bounds_in_generics(
944        hir_generics,
945        def_id,
946        PredicateFilter::SelfTraitThatDefines(assoc_ident),
947    ));
948
949    let bounds =
950        &*tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates));
951
952    // Double check that the bounds *only* contain `SelfTy: Trait` preds.
953    let self_ty = match tcx.def_kind(def_id) {
954        DefKind::TyParam => Ty::new_param(
955            tcx,
956            tcx.generics_of(item_def_id)
957                .param_def_id_to_index(tcx, def_id.to_def_id())
958                .expect("expected generic param to be owned by item"),
959            tcx.item_name(def_id.to_def_id()),
960        ),
961        DefKind::Trait | DefKind::TraitAlias => tcx.types.self_param,
962        _ => unreachable!(),
963    };
964    assert_only_contains_predicates_from(
965        PredicateFilter::SelfTraitThatDefines(assoc_ident),
966        bounds,
967        self_ty,
968    );
969
970    ty::EarlyBinder::bind(bounds)
971}
972
973impl<'tcx> ItemCtxt<'tcx> {
974    /// Finds bounds from `hir::Generics`.
975    ///
976    /// This requires scanning through the HIR.
977    /// We do this to avoid having to lower *all* the bounds, which would create artificial cycles.
978    /// Instead, we can only lower the bounds for a type parameter `X` if `X::Foo` is used.
979    #[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(979u32),
                                    ::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))]
980    fn probe_ty_param_bounds_in_generics(
981        &self,
982        hir_generics: &'tcx hir::Generics<'tcx>,
983        param_def_id: LocalDefId,
984        filter: PredicateFilter,
985    ) -> Vec<(ty::Clause<'tcx>, Span)> {
986        let mut bounds = Vec::new();
987
988        if let PredicateFilter::All = filter {
989            for param in hir_generics.params {
990                match param.kind {
991                    hir::GenericParamKind::Type { .. } => {
992                        let param_ty = self.lowerer().lower_ty_param(param.hir_id);
993                        self.lowerer().add_implicit_sizedness_bounds(
994                            &mut bounds,
995                            param_ty,
996                            &[],
997                            ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
998                            param.span,
999                        );
1000                        self.lowerer().add_default_traits(
1001                            &mut bounds,
1002                            param_ty,
1003                            &[],
1004                            ImpliedBoundsContext::TyParam(param.def_id, hir_generics.predicates),
1005                            param.span,
1006                        );
1007                    }
1008                    hir::GenericParamKind::Lifetime { .. }
1009                    | hir::GenericParamKind::Const { .. } => {}
1010                }
1011            }
1012        }
1013
1014        for predicate in hir_generics.predicates {
1015            let hir_id = predicate.hir_id;
1016            let hir::WherePredicateKind::BoundPredicate(predicate) = predicate.kind else {
1017                continue;
1018            };
1019
1020            match filter {
1021                _ if predicate.is_param_bound(param_def_id.to_def_id()) => {
1022                    // Ok
1023                }
1024                PredicateFilter::All => {
1025                    // Ok
1026                }
1027                PredicateFilter::SelfOnly
1028                | PredicateFilter::SelfTraitThatDefines(_)
1029                | PredicateFilter::SelfConstIfConst
1030                | PredicateFilter::SelfAndAssociatedTypeBounds => continue,
1031                PredicateFilter::ConstIfConst => unreachable!(),
1032            }
1033
1034            let bound_ty = self.lowerer().lower_ty_maybe_return_type_notation(predicate.bounded_ty);
1035
1036            let bound_vars = self.tcx.late_bound_vars(hir_id);
1037            self.lowerer().lower_bounds(
1038                bound_ty,
1039                predicate.bounds,
1040                &mut bounds,
1041                bound_vars,
1042                filter,
1043                OverlappingAsssocItemConstraints::Allowed,
1044            );
1045        }
1046
1047        bounds
1048    }
1049}
1050
1051pub(super) fn const_conditions<'tcx>(
1052    tcx: TyCtxt<'tcx>,
1053    def_id: LocalDefId,
1054) -> ty::ConstConditions<'tcx> {
1055    if !tcx.is_conditionally_const(def_id) {
1056        ::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:?}");
1057    }
1058
1059    match tcx.opt_rpitit_info(def_id.to_def_id()) {
1060        // RPITITs inherit const conditions of their parent fn
1061        Some(
1062            ty::ImplTraitInTraitData::Impl { fn_def_id }
1063            | ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
1064        ) => return tcx.const_conditions(fn_def_id),
1065        None => {}
1066    }
1067
1068    let (generics, trait_def_id_and_supertraits, has_parent) = match tcx.hir_node_by_def_id(def_id)
1069    {
1070        Node::Item(item) => match item.kind {
1071            hir::ItemKind::Impl(impl_) => (impl_.generics, None, false),
1072            hir::ItemKind::Fn { generics, .. } => (generics, None, false),
1073            hir::ItemKind::Trait { generics, bounds: supertraits, .. } => {
1074                (generics, Some((Some(item.owner_id.def_id), supertraits)), false)
1075            }
1076            hir::ItemKind::TraitAlias(_, _, generics, supertraits) => {
1077                (generics, Some((None, supertraits)), false)
1078            }
1079            _ => ::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:?}"),
1080        },
1081        // While associated types are not really const, we do allow them to have `[const]`
1082        // bounds and where clauses. `const_conditions` is responsible for gathering
1083        // these up so we can check them in `compare_type_predicate_entailment`, and
1084        // in `HostEffect` goal computation.
1085        Node::TraitItem(item) => match item.kind {
1086            hir::TraitItemKind::Fn(_, _) | hir::TraitItemKind::Type(_, _) => {
1087                (item.generics, None, true)
1088            }
1089            _ => ::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:?}"),
1090        },
1091        Node::ImplItem(item) => match item.kind {
1092            hir::ImplItemKind::Fn(_, _) | hir::ImplItemKind::Type(_) => {
1093                (item.generics, None, tcx.is_conditionally_const(tcx.local_parent(def_id)))
1094            }
1095            _ => ::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:?}"),
1096        },
1097        Node::ForeignItem(item) => match item.kind {
1098            hir::ForeignItemKind::Fn(_, _, generics) => (generics, None, false),
1099            _ => ::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:?}"),
1100        },
1101        Node::OpaqueTy(opaque) => match opaque.origin {
1102            hir::OpaqueTyOrigin::FnReturn { parent, .. } => return tcx.const_conditions(parent),
1103            hir::OpaqueTyOrigin::AsyncFn { .. } | hir::OpaqueTyOrigin::TyAlias { .. } => {
1104                ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
1105            }
1106        },
1107        // N.B. Tuple ctors are unconditionally constant.
1108        Node::Ctor(hir::VariantData::Tuple { .. }) => return Default::default(),
1109        Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(_), .. }) => {
1110            (hir::Generics::empty(), None, tcx.is_conditionally_const(tcx.local_parent(def_id)))
1111        }
1112        _ => ::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:?}"),
1113    };
1114
1115    let icx = ItemCtxt::new(tcx, def_id);
1116    let mut bounds = Vec::new();
1117
1118    for pred in generics.predicates {
1119        match pred.kind {
1120            hir::WherePredicateKind::BoundPredicate(bound_pred) => {
1121                let ty = icx.lowerer().lower_ty_maybe_return_type_notation(bound_pred.bounded_ty);
1122                let bound_vars = tcx.late_bound_vars(pred.hir_id);
1123                icx.lowerer().lower_bounds(
1124                    ty,
1125                    bound_pred.bounds.iter(),
1126                    &mut bounds,
1127                    bound_vars,
1128                    PredicateFilter::ConstIfConst,
1129                    OverlappingAsssocItemConstraints::Allowed,
1130                );
1131            }
1132            _ => {}
1133        }
1134    }
1135
1136    if let Some((def_id, supertraits)) = trait_def_id_and_supertraits {
1137        if let Some(def_id) = def_id {
1138            // We've checked above that the trait is conditionally const.
1139            bounds.push((
1140                ty::Binder::dummy(ty::TraitRef::identity(tcx, def_id.to_def_id()))
1141                    .to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
1142                DUMMY_SP,
1143            ));
1144        }
1145
1146        icx.lowerer().lower_bounds(
1147            tcx.types.self_param,
1148            supertraits,
1149            &mut bounds,
1150            ty::List::empty(),
1151            PredicateFilter::ConstIfConst,
1152            OverlappingAsssocItemConstraints::Allowed,
1153        );
1154    }
1155
1156    ty::ConstConditions {
1157        parent: has_parent.then(|| tcx.local_parent(def_id).to_def_id()),
1158        predicates: tcx.arena.alloc_from_iter(bounds.into_iter().map(|(clause, span)| {
1159            (
1160                clause.kind().map_bound(|clause| match clause {
1161                    ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
1162                        trait_ref,
1163                        constness: ty::BoundConstness::Maybe,
1164                    }) => trait_ref,
1165                    _ => ::rustc_middle::util::bug::bug_fmt(format_args!("converted {0:?}", clause))bug!("converted {clause:?}"),
1166                }),
1167                span,
1168            )
1169        })),
1170    }
1171}
1172
1173pub(super) fn explicit_implied_const_bounds<'tcx>(
1174    tcx: TyCtxt<'tcx>,
1175    def_id: LocalDefId,
1176) -> ty::EarlyBinder<'tcx, &'tcx [(ty::PolyTraitRef<'tcx>, Span)]> {
1177    if !tcx.is_conditionally_const(def_id) {
1178        ::rustc_middle::util::bug::bug_fmt(format_args!("explicit_implied_const_bounds invoked for item that is not conditionally const: {0:?}",
        def_id));bug!(
1179            "explicit_implied_const_bounds invoked for item that is not conditionally const: {def_id:?}"
1180        );
1181    }
1182
1183    let bounds = match tcx.opt_rpitit_info(def_id.to_def_id()) {
1184        // RPITIT's bounds are the same as opaque type bounds, but with
1185        // a projection self type.
1186        Some(ty::ImplTraitInTraitData::Trait { .. }) => {
1187            explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
1188        }
1189        Some(ty::ImplTraitInTraitData::Impl { .. }) => {
1190            ::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")
1191        }
1192        None => match tcx.hir_node_by_def_id(def_id) {
1193            Node::Item(hir::Item {
1194                kind: hir::ItemKind::Trait { .. } | hir::ItemKind::TraitAlias(..),
1195                ..
1196            }) => implied_predicates_with_filter(
1197                tcx,
1198                def_id.to_def_id(),
1199                PredicateFilter::SelfConstIfConst,
1200            ),
1201            Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
1202            | Node::OpaqueTy(_) => {
1203                explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
1204            }
1205            _ => ::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:?}"),
1206        },
1207    };
1208
1209    bounds.map_bound(|bounds| {
1210        &*tcx.arena.alloc_from_iter(bounds.iter().copied().map(|(clause, span)| {
1211            (
1212                clause.kind().map_bound(|clause| match clause {
1213                    ty::ClauseKind::HostEffect(ty::HostEffectPredicate {
1214                        trait_ref,
1215                        constness: ty::BoundConstness::Maybe,
1216                    }) => trait_ref,
1217                    _ => ::rustc_middle::util::bug::bug_fmt(format_args!("converted {0:?}", clause))bug!("converted {clause:?}"),
1218                }),
1219                span,
1220            )
1221        }))
1222    })
1223}