Skip to main content

rustc_infer/infer/outlives/
verify.rs

1use std::assert_matches;
2
3use rustc_middle::ty::outlives::{Component, compute_alias_components_recursive};
4use rustc_middle::ty::{self, OutlivesPredicate, Ty, TyCtxt};
5use smallvec::smallvec;
6use tracing::{debug, instrument};
7
8use crate::infer::outlives::env::RegionBoundPairs;
9use crate::infer::region_constraints::VerifyIfEq;
10use crate::infer::{GenericKind, VerifyBound};
11
12/// The `TypeOutlives` struct has the job of "lowering" a `T: 'a`
13/// obligation into a series of `'a: 'b` constraints and "verifys", as
14/// described on the module comment. The final constraints are emitted
15/// via a "delegate" of type `D` -- this is usually the `infcx`, which
16/// accrues them into the `region_obligations` code, but for NLL we
17/// use something else.
18pub(crate) struct VerifyBoundCx<'cx, 'tcx> {
19    tcx: TyCtxt<'tcx>,
20    region_bound_pairs: &'cx RegionBoundPairs<'tcx>,
21    /// During borrowck, if there are no outlives bounds on a generic
22    /// parameter `T`, we assume that `T: 'in_fn_body` holds.
23    ///
24    /// Outside of borrowck the only way to prove `T: '?0` is by
25    /// setting  `'?0` to `'empty`.
26    implicit_region_bound: Option<ty::Region<'tcx>>,
27    caller_bounds: &'cx [ty::PolyTypeOutlivesPredicate<'tcx>],
28}
29
30impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
31    pub(crate) fn new(
32        tcx: TyCtxt<'tcx>,
33        region_bound_pairs: &'cx RegionBoundPairs<'tcx>,
34        implicit_region_bound: Option<ty::Region<'tcx>>,
35        caller_bounds: &'cx [ty::PolyTypeOutlivesPredicate<'tcx>],
36    ) -> Self {
37        Self { tcx, region_bound_pairs, implicit_region_bound, caller_bounds }
38    }
39
40    #[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("param_or_placeholder_bound",
                                    "rustc_infer::infer::outlives::verify",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/outlives/verify.rs"),
                                    ::tracing_core::__macro_support::Option::Some(40u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::outlives::verify"),
                                    ::tracing_core::field::FieldSet::new(&["ty"],
                                        ::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(&ty)
                                                            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: VerifyBound<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let declared_bounds_from_env =
                self.declared_generic_bounds_from_env(ty);
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/outlives/verify.rs:46",
                                    "rustc_infer::infer::outlives::verify",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/outlives/verify.rs"),
                                    ::tracing_core::__macro_support::Option::Some(46u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::outlives::verify"),
                                    ::tracing_core::field::FieldSet::new(&["declared_bounds_from_env"],
                                        ::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(&declared_bounds_from_env)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let mut param_bounds = ::alloc::vec::Vec::new();
            for declared_bound in declared_bounds_from_env {
                let bound_region =
                    declared_bound.map_bound(|outlives| outlives.1);
                if let Some(region) = bound_region.no_bound_vars() {
                    param_bounds.push(VerifyBound::OutlivedBy(region));
                } else {
                    {
                        use ::tracing::__macro_support::Callsite as _;
                        static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                            {
                                static META: ::tracing::Metadata<'static> =
                                    {
                                        ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/outlives/verify.rs:55",
                                            "rustc_infer::infer::outlives::verify",
                                            ::tracing::Level::DEBUG,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/outlives/verify.rs"),
                                            ::tracing_core::__macro_support::Option::Some(55u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::outlives::verify"),
                                            ::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!("found that {0:?} outlives any lifetime, returning empty vector",
                                                                        ty) as &dyn Value))])
                                });
                        } else { ; }
                    };
                    return VerifyBound::AllBounds(::alloc::vec::Vec::new());
                }
            }
            if let Some(r) = self.implicit_region_bound {
                {
                    use ::tracing::__macro_support::Callsite as _;
                    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                        {
                            static META: ::tracing::Metadata<'static> =
                                {
                                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/outlives/verify.rs:63",
                                        "rustc_infer::infer::outlives::verify",
                                        ::tracing::Level::DEBUG,
                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/outlives/verify.rs"),
                                        ::tracing_core::__macro_support::Option::Some(63u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::outlives::verify"),
                                        ::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!("adding implicit region bound of {0:?}",
                                                                    r) as &dyn Value))])
                            });
                    } else { ; }
                };
                param_bounds.push(VerifyBound::OutlivedBy(r));
            }
            if param_bounds.is_empty() {
                VerifyBound::IsEmpty
            } else if param_bounds.len() == 1 {
                param_bounds.pop().unwrap()
            } else { VerifyBound::AnyBound(param_bounds) }
        }
    }
}#[instrument(level = "debug", skip(self))]
41    pub(crate) fn param_or_placeholder_bound(&self, ty: Ty<'tcx>) -> VerifyBound<'tcx> {
42        // Start with anything like `T: 'a` we can scrape from the
43        // environment. If the environment contains something like
44        // `for<'a> T: 'a`, then we know that `T` outlives everything.
45        let declared_bounds_from_env = self.declared_generic_bounds_from_env(ty);
46        debug!(?declared_bounds_from_env);
47        let mut param_bounds = vec![];
48        for declared_bound in declared_bounds_from_env {
49            let bound_region = declared_bound.map_bound(|outlives| outlives.1);
50            if let Some(region) = bound_region.no_bound_vars() {
51                // This is `T: 'a` for some free region `'a`.
52                param_bounds.push(VerifyBound::OutlivedBy(region));
53            } else {
54                // This is `for<'a> T: 'a`. This means that `T` outlives everything! All done here.
55                debug!("found that {ty:?} outlives any lifetime, returning empty vector");
56                return VerifyBound::AllBounds(vec![]);
57            }
58        }
59
60        // Add in the default bound of fn body that applies to all in
61        // scope type parameters:
62        if let Some(r) = self.implicit_region_bound {
63            debug!("adding implicit region bound of {r:?}");
64            param_bounds.push(VerifyBound::OutlivedBy(r));
65        }
66
67        if param_bounds.is_empty() {
68            // We know that all types `T` outlive `'empty`, so if we
69            // can find no other bound, then check that the region
70            // being tested is `'empty`.
71            VerifyBound::IsEmpty
72        } else if param_bounds.len() == 1 {
73            // Micro-opt: no need to store the vector if it's just len 1
74            param_bounds.pop().unwrap()
75        } else {
76            // If we can find any other bound `R` such that `T: R`, then
77            // we don't need to check for `'empty`, because `R: 'empty`.
78            VerifyBound::AnyBound(param_bounds)
79        }
80    }
81
82    /// Given a projection like `T::Item`, searches the environment
83    /// for where-clauses like `T::Item: 'a`. Returns the set of
84    /// regions `'a` that it finds.
85    ///
86    /// This is an "approximate" check -- it may not find all
87    /// applicable bounds, and not all the bounds it returns can be
88    /// relied upon. In particular, this check ignores region
89    /// identity. So, for example, if we have `<T as
90    /// Trait<'0>>::Item` where `'0` is a region variable, and the
91    /// user has `<T as Trait<'a>>::Item: 'b` in the environment, then
92    /// the clause from the environment only applies if `'0 = 'a`,
93    /// which we don't know yet. But we would still include `'b` in
94    /// this list.
95    pub(crate) fn approx_declared_bounds_from_env(
96        &self,
97        alias_ty: ty::AliasTy<'tcx>,
98    ) -> Vec<ty::PolyTypeOutlivesPredicate<'tcx>> {
99        // FIXME(#155345): Region handling should generally only
100        // deal with rigid aliases, making sure we do so correctly
101        // everywhere is effort, so we're just using `No` everywhere
102        // for now. This should change soon.
103        let erased_alias_ty = self.tcx.erase_and_anonymize_regions(
104            ty::set_aliases_to_non_rigid(self.tcx, alias_ty)
105                .skip_norm_wip()
106                .to_ty(self.tcx, ty::IsRigid::No),
107        );
108        self.declared_generic_bounds_from_env_for_erased_ty(erased_alias_ty)
109    }
110
111    #[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("alias_bound",
                                    "rustc_infer::infer::outlives::verify",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/outlives/verify.rs"),
                                    ::tracing_core::__macro_support::Option::Some(111u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::outlives::verify"),
                                    ::tracing_core::field::FieldSet::new(&["alias_ty"],
                                        ::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(&alias_ty)
                                                            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: VerifyBound<'tcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let env_bounds =
                self.approx_declared_bounds_from_env(alias_ty).into_iter().map(|binder|
                        {
                            if let Some(ty::OutlivesPredicate(ty, r)) =
                                            binder.no_bound_vars() &&
                                        let ty::Alias(_, alias_ty_from_bound) = *ty.kind() &&
                                    alias_ty_from_bound == alias_ty {
                                VerifyBound::OutlivedBy(r)
                            } else {
                                let verify_if_eq_b =
                                    binder.map_bound(|ty::OutlivesPredicate(ty, bound)|
                                            VerifyIfEq { ty, bound });
                                VerifyBound::IfEq(verify_if_eq_b)
                            }
                        });
            let definition_bounds =
                rustc_type_ir::outlives::declared_bounds_from_definition(self.tcx,
                        alias_ty).map(|r| VerifyBound::OutlivedBy(r));
            let recursive_bound =
                {
                    let mut components = ::smallvec::SmallVec::new();
                    compute_alias_components_recursive(self.tcx, alias_ty,
                        &mut components);
                    self.bound_from_components(&components)
                };
            VerifyBound::AnyBound(env_bounds.chain(definition_bounds).collect()).or(recursive_bound)
        }
    }
}#[instrument(level = "debug", skip(self))]
112    pub(crate) fn alias_bound(&self, alias_ty: ty::AliasTy<'tcx>) -> VerifyBound<'tcx> {
113        // Search the env for where clauses like `P: 'a`.
114        let env_bounds = self.approx_declared_bounds_from_env(alias_ty).into_iter().map(|binder| {
115            // FIXME(#155345): We probably want to assert the alias is rigid here.
116            if let Some(ty::OutlivesPredicate(ty, r)) = binder.no_bound_vars()
117                && let ty::Alias(_, alias_ty_from_bound) = *ty.kind()
118                && alias_ty_from_bound == alias_ty
119            {
120                // Micro-optimize if this is an exact match (this
121                // occurs often when there are no region variables
122                // involved).
123                VerifyBound::OutlivedBy(r)
124            } else {
125                let verify_if_eq_b =
126                    binder.map_bound(|ty::OutlivesPredicate(ty, bound)| VerifyIfEq { ty, bound });
127                VerifyBound::IfEq(verify_if_eq_b)
128            }
129        });
130
131        // Extend with bounds that we can find from the definition.
132        let definition_bounds =
133            rustc_type_ir::outlives::declared_bounds_from_definition(self.tcx, alias_ty)
134                .map(|r| VerifyBound::OutlivedBy(r));
135
136        // see the extensive comment in projection_must_outlive
137        let recursive_bound = {
138            let mut components = smallvec![];
139            compute_alias_components_recursive(self.tcx, alias_ty, &mut components);
140            self.bound_from_components(&components)
141        };
142
143        VerifyBound::AnyBound(env_bounds.chain(definition_bounds).collect()).or(recursive_bound)
144    }
145
146    fn bound_from_components(&self, components: &[Component<TyCtxt<'tcx>>]) -> VerifyBound<'tcx> {
147        let mut bounds = components
148            .iter()
149            .map(|component| self.bound_from_single_component(component))
150            // Remove bounds that must hold, since they are not interesting.
151            .filter(|bound| !bound.must_hold());
152
153        match (bounds.next(), bounds.next()) {
154            (Some(first), None) => first,
155            (first, second) => {
156                VerifyBound::AllBounds(first.into_iter().chain(second).chain(bounds).collect())
157            }
158        }
159    }
160
161    fn bound_from_single_component(
162        &self,
163        component: &Component<TyCtxt<'tcx>>,
164    ) -> VerifyBound<'tcx> {
165        match *component {
166            Component::Region(lt) => VerifyBound::OutlivedBy(lt),
167            Component::Param(param_ty) => self.param_or_placeholder_bound(param_ty.to_ty(self.tcx)),
168            Component::Placeholder(placeholder_ty) => {
169                self.param_or_placeholder_bound(Ty::new_placeholder(self.tcx, placeholder_ty))
170            }
171            Component::Alias(alias_ty) => self.alias_bound(alias_ty),
172            Component::EscapingAlias(ref components) => self.bound_from_components(components),
173            Component::UnresolvedInferenceVariable(v) => {
174                // Ignore this, we presume it will yield an error later, since
175                // if a type variable is not resolved by this point it never
176                // will be.
177                self.tcx
178                    .dcx()
179                    .delayed_bug(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("unresolved inference variable in outlives: {0:?}",
                v))
    })format!("unresolved inference variable in outlives: {v:?}"));
180                // Add a bound that never holds.
181                VerifyBound::AnyBound(::alloc::vec::Vec::new()vec![])
182            }
183        }
184    }
185
186    /// Searches the environment for where-clauses like `G: 'a` where
187    /// `G` is either some type parameter `T` or a projection like
188    /// `T::Item`. Returns a vector of the `'a` bounds it can find.
189    ///
190    /// This is a conservative check -- it may not find all applicable
191    /// bounds, but all the bounds it returns can be relied upon.
192    fn declared_generic_bounds_from_env(
193        &self,
194        generic_ty: Ty<'tcx>,
195    ) -> Vec<ty::PolyTypeOutlivesPredicate<'tcx>> {
196        {
    match generic_ty.kind() {
        ty::Param(_) | ty::Placeholder(_) => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "ty::Param(_) | ty::Placeholder(_)",
                ::core::option::Option::None);
        }
    }
};assert_matches!(generic_ty.kind(), ty::Param(_) | ty::Placeholder(_));
197        self.declared_generic_bounds_from_env_for_erased_ty(generic_ty)
198    }
199
200    /// Searches the environment to find all bounds that apply to `erased_ty`.
201    /// Obviously these must be approximate -- they are in fact both *over* and
202    /// and *under* approximated:
203    ///
204    /// * Over-approximated because we don't consider equality of regions.
205    /// * Under-approximated because we look for syntactic equality and so for complex types
206    ///   like `<T as Foo<fn(&u32, &u32)>>::Item` or whatever we may fail to figure out
207    ///   all the subtleties.
208    ///
209    /// In some cases, such as when `erased_ty` represents a `ty::Param`, however,
210    /// the result is precise.
211    #[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("declared_generic_bounds_from_env_for_erased_ty",
                                    "rustc_infer::infer::outlives::verify",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/outlives/verify.rs"),
                                    ::tracing_core::__macro_support::Option::Some(211u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::outlives::verify"),
                                    ::tracing_core::field::FieldSet::new(&["erased_ty"],
                                        ::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(&erased_ty)
                                                            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::PolyTypeOutlivesPredicate<'tcx>> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = self.tcx;
            let mut bounds = ::alloc::vec::Vec::new();
            bounds.extend(self.caller_bounds.iter().copied().filter(move
                        |outlives_predicate|
                        {
                            super::test_type_match::can_match_erased_ty(tcx,
                                *outlives_predicate, erased_ty)
                        }));
            bounds.extend(self.region_bound_pairs.iter().filter_map(|&OutlivesPredicate(p,
                            r)|
                        {
                            {
                                use ::tracing::__macro_support::Callsite as _;
                                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                    {
                                        static META: ::tracing::Metadata<'static> =
                                            {
                                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_infer/src/infer/outlives/verify.rs:238",
                                                    "rustc_infer::infer::outlives::verify",
                                                    ::tracing::Level::DEBUG,
                                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_infer/src/infer/outlives/verify.rs"),
                                                    ::tracing_core::__macro_support::Option::Some(238u32),
                                                    ::tracing_core::__macro_support::Option::Some("rustc_infer::infer::outlives::verify"),
                                                    ::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!("declared_generic_bounds_from_env_for_erased_ty: region_bound_pair = {0:?}",
                                                                                (r, p)) as &dyn Value))])
                                        });
                                } else { ; }
                            };
                            match (&p, erased_ty.kind()) {
                                (GenericKind::Param(p1), ty::Param(p2)) if p1 == p2 => {}
                                (GenericKind::Placeholder(p1), ty::Placeholder(p2)) if
                                    p1 == p2 => {}
                                (GenericKind::Alias(a1), ty::Alias(_, a2)) if
                                    a1.kind == a2.kind => {}
                                _ => return None,
                            }
                            let p_ty = p.to_ty(tcx);
                            let erased_p_ty =
                                self.tcx.erase_and_anonymize_regions(ty::set_aliases_to_non_rigid(self.tcx,
                                            p_ty).skip_norm_wip());
                            (erased_p_ty ==
                                        erased_ty).then_some(ty::Binder::dummy(ty::OutlivesPredicate(p_ty,
                                        r)))
                        }));
            bounds
        }
    }
}#[instrument(level = "debug", skip(self))]
212    fn declared_generic_bounds_from_env_for_erased_ty(
213        &self,
214        erased_ty: Ty<'tcx>,
215    ) -> Vec<ty::PolyTypeOutlivesPredicate<'tcx>> {
216        let tcx = self.tcx;
217        let mut bounds = vec![];
218
219        // To start, collect bounds from user environment. Note that
220        // parameter environments are already elaborated, so we don't
221        // have to worry about that.
222        bounds.extend(self.caller_bounds.iter().copied().filter(move |outlives_predicate| {
223            super::test_type_match::can_match_erased_ty(tcx, *outlives_predicate, erased_ty)
224        }));
225
226        // Next, collect regions we scraped from the well-formedness
227        // constraints in the fn signature. To do that, we walk the list
228        // of known relations from the fn ctxt.
229        //
230        // This is crucial because otherwise code like this fails:
231        //
232        //     fn foo<'a, A>(x: &'a A) { x.bar() }
233        //
234        // The problem is that the type of `x` is `&'a A`. To be
235        // well-formed, then, A must outlive `'a`, but we don't know that
236        // this holds from first principles.
237        bounds.extend(self.region_bound_pairs.iter().filter_map(|&OutlivesPredicate(p, r)| {
238            debug!(
239                "declared_generic_bounds_from_env_for_erased_ty: region_bound_pair = {:?}",
240                (r, p)
241            );
242            // Fast path for the common case.
243            match (&p, erased_ty.kind()) {
244                // In outlive routines, all types are expected to be fully normalized.
245                // And therefore we can safely use structural equality for alias types.
246                (GenericKind::Param(p1), ty::Param(p2)) if p1 == p2 => {}
247                (GenericKind::Placeholder(p1), ty::Placeholder(p2)) if p1 == p2 => {}
248                // FIXME(#155345): We probably want to assert that the rhs is rigid.
249                (GenericKind::Alias(a1), ty::Alias(_, a2)) if a1.kind == a2.kind => {}
250                _ => return None,
251            }
252
253            let p_ty = p.to_ty(tcx);
254            // FIXME(#155345): Region handling should generally only
255            // deal with rigid aliases, making sure we do so correctly
256            // everywhere is effort, so we're just using `No` everywhere
257            // for now. This should change soon.
258            let erased_p_ty = self.tcx.erase_and_anonymize_regions(
259                ty::set_aliases_to_non_rigid(self.tcx, p_ty).skip_norm_wip(),
260            );
261            (erased_p_ty == erased_ty).then_some(ty::Binder::dummy(ty::OutlivesPredicate(p_ty, r)))
262        }));
263
264        bounds
265    }
266}