Skip to main content

rustc_hir_analysis/collect/
predicates_of.rs

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