Skip to main content

rustc_trait_selection/traits/
auto_trait.rs

1//! Support code for rustdoc and external tools.
2//! You really don't want to be using this unless you need to.
3
4use std::collections::VecDeque;
5use std::iter;
6
7use rustc_data_structures::fx::{FxIndexMap, FxIndexSet, IndexEntry};
8use rustc_data_structures::unord::UnordSet;
9use rustc_hir::def_id::CRATE_DEF_ID;
10use rustc_infer::infer::DefineOpaqueTypes;
11use rustc_middle::ty::{Region, RegionVid};
12use rustc_span::DUMMY_SP;
13use tracing::debug;
14
15use super::*;
16use crate::errors::UnableToConstructConstantValue;
17use crate::infer::TypeFreshener;
18use crate::infer::region_constraints::{ConstraintKind, RegionConstraintData};
19use crate::regions::OutlivesEnvironmentBuildExt;
20use crate::traits::project::ProjectAndUnifyResult;
21
22// FIXME(twk): this is obviously not nice to duplicate like that
23#[derive(#[automatically_derived]
impl<'tcx> ::core::cmp::Eq for RegionTarget<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Region<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<RegionVid>;
    }
}Eq, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for RegionTarget<'tcx> {
    #[inline]
    fn eq(&self, other: &RegionTarget<'tcx>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (RegionTarget::Region(__self_0),
                    RegionTarget::Region(__arg1_0)) => __self_0 == __arg1_0,
                (RegionTarget::RegionVid(__self_0),
                    RegionTarget::RegionVid(__arg1_0)) => __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for RegionTarget<'tcx> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state);
        match self {
            RegionTarget::Region(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
            RegionTarget::RegionVid(__self_0) =>
                ::core::hash::Hash::hash(__self_0, state),
        }
    }
}Hash, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for RegionTarget<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for RegionTarget<'tcx> {
    #[inline]
    fn clone(&self) -> RegionTarget<'tcx> {
        let _: ::core::clone::AssertParamIsClone<Region<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<RegionVid>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for RegionTarget<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            RegionTarget::Region(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Region",
                    &__self_0),
            RegionTarget::RegionVid(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "RegionVid", &__self_0),
        }
    }
}Debug)]
24pub enum RegionTarget<'tcx> {
25    Region(Region<'tcx>),
26    RegionVid(RegionVid),
27}
28
29#[derive(#[automatically_derived]
impl<'tcx> ::core::default::Default for RegionDeps<'tcx> {
    #[inline]
    fn default() -> RegionDeps<'tcx> {
        RegionDeps {
            larger: ::core::default::Default::default(),
            smaller: ::core::default::Default::default(),
        }
    }
}Default, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for RegionDeps<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "RegionDeps",
            "larger", &self.larger, "smaller", &&self.smaller)
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for RegionDeps<'tcx> {
    #[inline]
    fn clone(&self) -> RegionDeps<'tcx> {
        RegionDeps {
            larger: ::core::clone::Clone::clone(&self.larger),
            smaller: ::core::clone::Clone::clone(&self.smaller),
        }
    }
}Clone)]
30pub struct RegionDeps<'tcx> {
31    pub larger: FxIndexSet<RegionTarget<'tcx>>,
32    pub smaller: FxIndexSet<RegionTarget<'tcx>>,
33}
34
35pub enum AutoTraitResult<A> {
36    ExplicitImpl,
37    PositiveImpl(A),
38    NegativeImpl,
39}
40
41pub struct AutoTraitInfo<'cx> {
42    pub full_user_env: ty::ParamEnv<'cx>,
43    pub region_data: RegionConstraintData<'cx>,
44    pub vid_to_region: FxIndexMap<ty::RegionVid, ty::Region<'cx>>,
45}
46
47pub struct AutoTraitFinder<'tcx> {
48    tcx: TyCtxt<'tcx>,
49}
50
51impl<'tcx> AutoTraitFinder<'tcx> {
52    pub fn new(tcx: TyCtxt<'tcx>) -> Self {
53        AutoTraitFinder { tcx }
54    }
55
56    /// Makes a best effort to determine whether and under which conditions an auto trait is
57    /// implemented for a type. For example, if you have
58    ///
59    /// ```
60    /// struct Foo<T> { data: Box<T> }
61    /// ```
62    ///
63    /// then this might return that `Foo<T>: Send` if `T: Send` (encoded in the AutoTraitResult
64    /// type). The analysis attempts to account for custom impls as well as other complex cases.
65    /// This result is intended for use by rustdoc and other such consumers.
66    ///
67    /// (Note that due to the coinductive nature of Send, the full and correct result is actually
68    /// quite simple to generate. That is, when a type has no custom impl, it is Send iff its field
69    /// types are all Send. So, in our example, we might have that `Foo<T>: Send` if `Box<T>: Send`.
70    /// But this is often not the best way to present to the user.)
71    ///
72    /// Warning: The API should be considered highly unstable, and it may be refactored or removed
73    /// in the future.
74    pub fn find_auto_trait_generics<A>(
75        &self,
76        ty: Ty<'tcx>,
77        typing_env: ty::TypingEnv<'tcx>,
78        trait_did: DefId,
79        mut auto_trait_callback: impl FnMut(AutoTraitInfo<'tcx>) -> A,
80    ) -> AutoTraitResult<A> {
81        let tcx = self.tcx;
82
83        let trait_ref = ty::TraitRef::new(tcx, trait_did, [ty]);
84
85        let (infcx, orig_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
86        let mut selcx = SelectionContext::new(&infcx);
87        for polarity in [ty::PredicatePolarity::Positive, ty::PredicatePolarity::Negative] {
88            let result = selcx.select(&Obligation::new(
89                tcx,
90                ObligationCause::dummy(),
91                orig_env,
92                ty::TraitPredicate { trait_ref, polarity },
93            ));
94            if let Ok(Some(ImplSource::UserDefined(_))) = result {
95                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/auto_trait.rs:95",
                        "rustc_trait_selection::traits::auto_trait",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/auto_trait.rs"),
                        ::tracing_core::__macro_support::Option::Some(95u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::auto_trait"),
                        ::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!("find_auto_trait_generics({0:?}): manual impl found, bailing out",
                                                    trait_ref) as &dyn Value))])
            });
    } else { ; }
};debug!("find_auto_trait_generics({trait_ref:?}): manual impl found, bailing out");
96                // If an explicit impl exists, it always takes priority over an auto impl
97                return AutoTraitResult::ExplicitImpl;
98            }
99        }
100
101        let (infcx, orig_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
102        let mut fresh_preds = FxIndexSet::default();
103
104        // Due to the way projections are handled by SelectionContext, we need to run
105        // evaluate_predicates twice: once on the original param env, and once on the result of
106        // the first evaluate_predicates call.
107        //
108        // The problem is this: most of rustc, including SelectionContext and traits::project,
109        // are designed to work with a concrete usage of a type (e.g., Vec<u8>
110        // fn<T>() { Vec<T> }. This information will generally never change - given
111        // the 'T' in fn<T>() { ... }, we'll never know anything else about 'T'.
112        // If we're unable to prove that 'T' implements a particular trait, we're done -
113        // there's nothing left to do but error out.
114        //
115        // However, synthesizing an auto trait impl works differently. Here, we start out with
116        // a set of initial conditions - the ParamEnv of the struct/enum/union we're dealing
117        // with - and progressively discover the conditions we need to fulfill for it to
118        // implement a certain auto trait. This ends up breaking two assumptions made by trait
119        // selection and projection:
120        //
121        // * We can always cache the result of a particular trait selection for the lifetime of
122        // an InfCtxt
123        // * Given a projection bound such as '<T as SomeTrait>::SomeItem = K', if 'T:
124        // SomeTrait' doesn't hold, then we don't need to care about the 'SomeItem = K'
125        //
126        // We fix the first assumption by manually clearing out all of the InferCtxt's caches
127        // in between calls to SelectionContext.select. This allows us to keep all of the
128        // intermediate types we create bound to the 'tcx lifetime, rather than needing to lift
129        // them between calls.
130        //
131        // We fix the second assumption by reprocessing the result of our first call to
132        // evaluate_predicates. Using the example of '<T as SomeTrait>::SomeItem = K', our first
133        // pass will pick up 'T: SomeTrait', but not 'SomeItem = K'. On our second pass,
134        // traits::project will see that 'T: SomeTrait' is in our ParamEnv, allowing
135        // SelectionContext to return it back to us.
136
137        let Some((new_env, user_env)) =
138            self.evaluate_predicates(&infcx, trait_did, ty, orig_env, orig_env, &mut fresh_preds)
139        else {
140            return AutoTraitResult::NegativeImpl;
141        };
142
143        let (full_env, full_user_env) = self
144            .evaluate_predicates(&infcx, trait_did, ty, new_env, user_env, &mut fresh_preds)
145            .unwrap_or_else(|| {
146                {
    ::core::panicking::panic_fmt(format_args!("Failed to fully process: {0:?} {1:?} {2:?}",
            ty, trait_did, orig_env));
}panic!("Failed to fully process: {ty:?} {trait_did:?} {orig_env:?}")
147            });
148
149        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/auto_trait.rs:149",
                        "rustc_trait_selection::traits::auto_trait",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/auto_trait.rs"),
                        ::tracing_core::__macro_support::Option::Some(149u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::auto_trait"),
                        ::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!("find_auto_trait_generics({0:?}): fulfilling with {1:?}",
                                                    trait_ref, full_env) as &dyn Value))])
            });
    } else { ; }
};debug!(
150            "find_auto_trait_generics({:?}): fulfilling \
151             with {:?}",
152            trait_ref, full_env
153        );
154
155        // At this point, we already have all of the bounds we need. FulfillmentContext is used
156        // to store all of the necessary region/lifetime bounds in the InferContext, as well as
157        // an additional sanity check.
158        let ocx = ObligationCtxt::new(&infcx);
159        ocx.register_bound(ObligationCause::dummy(), full_env, ty, trait_did);
160        let errors = ocx.evaluate_obligations_error_on_ambiguity();
161        if !errors.is_empty() {
162            {
    ::core::panicking::panic_fmt(format_args!("Unable to fulfill trait {0:?} for \'{1:?}\': {2:?}",
            trait_did, ty, errors));
};panic!("Unable to fulfill trait {trait_did:?} for '{ty:?}': {errors:?}");
163        }
164
165        let outlives_env = OutlivesEnvironment::new(&infcx, CRATE_DEF_ID, full_env, []);
166        let _ =
167            infcx.process_registered_region_obligations(&outlives_env, |ty, _| Ok(ty), DUMMY_SP);
168
169        let region_data = infcx.inner.borrow_mut().unwrap_region_constraints().data().clone();
170
171        let vid_to_region = self.map_vid_to_region(&region_data);
172
173        let info = AutoTraitInfo { full_user_env, region_data, vid_to_region };
174
175        AutoTraitResult::PositiveImpl(auto_trait_callback(info))
176    }
177
178    /// The core logic responsible for computing the bounds for our synthesized impl.
179    ///
180    /// To calculate the bounds, we call `SelectionContext.select` in a loop. Like
181    /// `FulfillmentContext`, we recursively select the nested obligations of predicates we
182    /// encounter. However, whenever we encounter an `UnimplementedError` involving a type
183    /// parameter, we add it to our `ParamEnv`. Since our goal is to determine when a particular
184    /// type implements an auto trait, Unimplemented errors tell us what conditions need to be met.
185    ///
186    /// This method ends up working somewhat similarly to `FulfillmentContext`, but with a few key
187    /// differences. `FulfillmentContext` works under the assumption that it's dealing with concrete
188    /// user code. According, it considers all possible ways that a `Predicate` could be met, which
189    /// isn't always what we want for a synthesized impl. For example, given the predicate `T:
190    /// Iterator`, `FulfillmentContext` can end up reporting an Unimplemented error for `T:
191    /// IntoIterator` -- since there's an implementation of `Iterator` where `T: IntoIterator`,
192    /// `FulfillmentContext` will drive `SelectionContext` to consider that impl before giving up.
193    /// If we were to rely on `FulfillmentContext`s decision, we might end up synthesizing an impl
194    /// like this:
195    /// ```ignore (illustrative)
196    /// impl<T> Send for Foo<T> where T: IntoIterator
197    /// ```
198    /// While it might be technically true that Foo implements Send where `T: IntoIterator`,
199    /// the bound is overly restrictive - it's really only necessary that `T: Iterator`.
200    ///
201    /// For this reason, `evaluate_predicates` handles predicates with type variables specially.
202    /// When we encounter an `Unimplemented` error for a bound such as `T: Iterator`, we immediately
203    /// add it to our `ParamEnv`, and add it to our stack for recursive evaluation. When we later
204    /// select it, we'll pick up any nested bounds, without ever inferring that `T: IntoIterator`
205    /// needs to hold.
206    ///
207    /// One additional consideration is supertrait bounds. Normally, a `ParamEnv` is only ever
208    /// constructed once for a given type. As part of the construction process, the `ParamEnv` will
209    /// have any supertrait bounds normalized -- e.g., if we have a type `struct Foo<T: Copy>`, the
210    /// `ParamEnv` will contain `T: Copy` and `T: Clone`, since `Copy: Clone`. When we construct our
211    /// own `ParamEnv`, we need to do this ourselves, through `traits::elaborate`, or
212    /// else `SelectionContext` will choke on the missing predicates. However, this should never
213    /// show up in the final synthesized generics: we don't want our generated docs page to contain
214    /// something like `T: Copy + Clone`, as that's redundant. Therefore, we keep track of a
215    /// separate `user_env`, which only holds the predicates that will actually be displayed to the
216    /// user.
217    fn evaluate_predicates(
218        &self,
219        infcx: &InferCtxt<'tcx>,
220        trait_did: DefId,
221        ty: Ty<'tcx>,
222        param_env: ty::ParamEnv<'tcx>,
223        user_env: ty::ParamEnv<'tcx>,
224        fresh_preds: &mut FxIndexSet<ty::Predicate<'tcx>>,
225    ) -> Option<(ty::ParamEnv<'tcx>, ty::ParamEnv<'tcx>)> {
226        let tcx = infcx.tcx;
227
228        // Don't try to process any nested obligations involving predicates
229        // that are already in the `ParamEnv` (modulo regions): we already
230        // know that they must hold.
231        for predicate in param_env.caller_bounds() {
232            fresh_preds.insert(self.clean_pred(infcx, predicate.as_predicate()));
233        }
234
235        let mut select = SelectionContext::new(infcx);
236
237        let mut already_visited = UnordSet::new();
238        let mut predicates = VecDeque::new();
239        predicates.push_back(ty::Binder::dummy(ty::TraitPredicate {
240            trait_ref: ty::TraitRef::new(infcx.tcx, trait_did, [ty]),
241
242            // Auto traits are positive
243            polarity: ty::PredicatePolarity::Positive,
244        }));
245
246        let computed_preds = param_env.caller_bounds().iter().map(|c| c.as_predicate());
247        let mut user_computed_preds: FxIndexSet<_> =
248            user_env.caller_bounds().iter().map(|c| c.as_predicate()).collect();
249
250        let mut new_env = param_env;
251        let dummy_cause = ObligationCause::dummy();
252
253        while let Some(pred) = predicates.pop_front() {
254            if !already_visited.insert(pred) {
255                continue;
256            }
257
258            // Call `infcx.resolve_vars_if_possible` to see if we can
259            // get rid of any inference variables.
260            let obligation = infcx.resolve_vars_if_possible(Obligation::new(
261                tcx,
262                dummy_cause.clone(),
263                new_env,
264                pred,
265            ));
266            let result = select.poly_select(&obligation);
267
268            match result {
269                Ok(Some(ref impl_source)) => {
270                    // If we see an explicit negative impl (e.g., `impl !Send for MyStruct`),
271                    // we immediately bail out, since it's impossible for us to continue.
272
273                    if let ImplSource::UserDefined(ImplSourceUserDefinedData {
274                        impl_def_id, ..
275                    }) = impl_source
276                    {
277                        // Blame 'tidy' for the weird bracket placement.
278                        if infcx.tcx.impl_polarity(*impl_def_id) != ty::ImplPolarity::Positive {
279                            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/auto_trait.rs:279",
                        "rustc_trait_selection::traits::auto_trait",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/auto_trait.rs"),
                        ::tracing_core::__macro_support::Option::Some(279u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::auto_trait"),
                        ::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!("evaluate_nested_obligations: found explicit negative impl{0:?}, bailing out",
                                                    impl_def_id) as &dyn Value))])
            });
    } else { ; }
};debug!(
280                                "evaluate_nested_obligations: found explicit negative impl\
281                                        {:?}, bailing out",
282                                impl_def_id
283                            );
284                            return None;
285                        }
286                    }
287
288                    let obligations = impl_source.borrow_nested_obligations().iter().cloned();
289
290                    if !self.evaluate_nested_obligations(
291                        ty,
292                        obligations,
293                        &mut user_computed_preds,
294                        fresh_preds,
295                        &mut predicates,
296                        &mut select,
297                    ) {
298                        return None;
299                    }
300                }
301                Ok(None) => {}
302                Err(SelectionError::Unimplemented) => {
303                    if self.is_param_no_infer(pred.skip_binder().trait_ref.args) {
304                        already_visited.remove(&pred);
305                        self.add_user_pred(&mut user_computed_preds, pred.upcast(self.tcx));
306                        predicates.push_back(pred);
307                    } else {
308                        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/auto_trait.rs:308",
                        "rustc_trait_selection::traits::auto_trait",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/auto_trait.rs"),
                        ::tracing_core::__macro_support::Option::Some(308u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::auto_trait"),
                        ::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!("evaluate_nested_obligations: `Unimplemented` found, bailing: {0:?} {1:?} {2:?}",
                                                    ty, pred, pred.skip_binder().trait_ref.args) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(
309                            "evaluate_nested_obligations: `Unimplemented` found, bailing: \
310                             {:?} {:?} {:?}",
311                            ty,
312                            pred,
313                            pred.skip_binder().trait_ref.args
314                        );
315                        return None;
316                    }
317                }
318                _ => {
    ::core::panicking::panic_fmt(format_args!("Unexpected error for \'{0:?}\': {1:?}",
            ty, result));
}panic!("Unexpected error for '{ty:?}': {result:?}"),
319            };
320
321            let normalized_preds =
322                elaborate(tcx, computed_preds.clone().chain(user_computed_preds.iter().cloned()));
323            new_env = ty::ParamEnv::new(
324                tcx.mk_clauses_from_iter(normalized_preds.filter_map(|p| p.as_clause())),
325            );
326        }
327
328        let final_user_env = ty::ParamEnv::new(
329            tcx.mk_clauses_from_iter(user_computed_preds.into_iter().filter_map(|p| p.as_clause())),
330        );
331        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/auto_trait.rs:331",
                        "rustc_trait_selection::traits::auto_trait",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/auto_trait.rs"),
                        ::tracing_core::__macro_support::Option::Some(331u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::auto_trait"),
                        ::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!("evaluate_nested_obligations(ty={0:?}, trait_did={1:?}): succeeded with \'{2:?}\' \'{3:?}\'",
                                                    ty, trait_did, new_env, final_user_env) as &dyn Value))])
            });
    } else { ; }
};debug!(
332            "evaluate_nested_obligations(ty={:?}, trait_did={:?}): succeeded with '{:?}' \
333             '{:?}'",
334            ty, trait_did, new_env, final_user_env
335        );
336
337        Some((new_env, final_user_env))
338    }
339
340    /// This method is designed to work around the following issue:
341    /// When we compute auto trait bounds, we repeatedly call `SelectionContext.select`,
342    /// progressively building a `ParamEnv` based on the results we get.
343    /// However, our usage of `SelectionContext` differs from its normal use within the compiler,
344    /// in that we capture and re-reprocess predicates from `Unimplemented` errors.
345    ///
346    /// This can lead to a corner case when dealing with region parameters.
347    /// During our selection loop in `evaluate_predicates`, we might end up with
348    /// two trait predicates that differ only in their region parameters:
349    /// one containing a HRTB lifetime parameter, and one containing a 'normal'
350    /// lifetime parameter. For example:
351    /// ```ignore (illustrative)
352    /// T as MyTrait<'a>
353    /// T as MyTrait<'static>
354    /// ```
355    /// If we put both of these predicates in our computed `ParamEnv`, we'll
356    /// confuse `SelectionContext`, since it will (correctly) view both as being applicable.
357    ///
358    /// To solve this, we pick the 'more strict' lifetime bound -- i.e., the HRTB
359    /// Our end goal is to generate a user-visible description of the conditions
360    /// under which a type implements an auto trait. A trait predicate involving
361    /// a HRTB means that the type needs to work with any choice of lifetime,
362    /// not just one specific lifetime (e.g., `'static`).
363    fn add_user_pred(
364        &self,
365        user_computed_preds: &mut FxIndexSet<ty::Predicate<'tcx>>,
366        new_pred: ty::Predicate<'tcx>,
367    ) {
368        let mut should_add_new = true;
369        user_computed_preds.retain(|&old_pred| {
370            if let (
371                ty::PredicateKind::Clause(ty::ClauseKind::Trait(new_trait)),
372                ty::PredicateKind::Clause(ty::ClauseKind::Trait(old_trait)),
373            ) = (new_pred.kind().skip_binder(), old_pred.kind().skip_binder())
374            {
375                if new_trait.def_id() == old_trait.def_id() {
376                    let new_args = new_trait.trait_ref.args;
377                    let old_args = old_trait.trait_ref.args;
378
379                    if !new_args.types().eq(old_args.types()) {
380                        // We can't compare lifetimes if the types are different,
381                        // so skip checking `old_pred`.
382                        return true;
383                    }
384
385                    for (new_region, old_region) in
386                        iter::zip(new_args.regions(), old_args.regions())
387                    {
388                        match (new_region.kind(), old_region.kind()) {
389                            // If both predicates have an `ReBound` (a HRTB) in the
390                            // same spot, we do nothing.
391                            (ty::ReBound(_, _), ty::ReBound(_, _)) => {}
392
393                            (ty::ReBound(_, _), _) | (_, ty::ReVar(_)) => {
394                                // One of these is true:
395                                // The new predicate has a HRTB in a spot where the old
396                                // predicate does not (if they both had a HRTB, the previous
397                                // match arm would have executed). A HRBT is a 'stricter'
398                                // bound than anything else, so we want to keep the newer
399                                // predicate (with the HRBT) in place of the old predicate.
400                                //
401                                // OR
402                                //
403                                // The old predicate has a region variable where the new
404                                // predicate has some other kind of region. An region
405                                // variable isn't something we can actually display to a user,
406                                // so we choose their new predicate (which doesn't have a region
407                                // variable).
408                                //
409                                // In both cases, we want to remove the old predicate,
410                                // from `user_computed_preds`, and replace it with the new
411                                // one. Having both the old and the new
412                                // predicate in a `ParamEnv` would confuse `SelectionContext`.
413                                //
414                                // We're currently in the predicate passed to 'retain',
415                                // so we return `false` to remove the old predicate from
416                                // `user_computed_preds`.
417                                return false;
418                            }
419                            (_, ty::ReBound(_, _)) | (ty::ReVar(_), _) => {
420                                // This is the opposite situation as the previous arm.
421                                // One of these is true:
422                                //
423                                // The old predicate has a HRTB lifetime in a place where the
424                                // new predicate does not.
425                                //
426                                // OR
427                                //
428                                // The new predicate has a region variable where the old
429                                // predicate has some other type of region.
430                                //
431                                // We want to leave the old
432                                // predicate in `user_computed_preds`, and skip adding
433                                // new_pred to `user_computed_params`.
434                                should_add_new = false
435                            }
436                            _ => {}
437                        }
438                    }
439                }
440            }
441            true
442        });
443
444        if should_add_new {
445            user_computed_preds.insert(new_pred);
446        }
447    }
448
449    /// This is very similar to `handle_lifetimes`. However, instead of matching `ty::Region`s
450    /// to each other, we match `ty::RegionVid`s to `ty::Region`s.
451    fn map_vid_to_region<'cx>(
452        &self,
453        regions: &RegionConstraintData<'cx>,
454    ) -> FxIndexMap<ty::RegionVid, ty::Region<'cx>> {
455        let mut vid_map = FxIndexMap::<RegionTarget<'cx>, RegionDeps<'cx>>::default();
456        let mut finished_map = FxIndexMap::default();
457
458        for c in regions.constraints.iter().flat_map(|(c, _)| c.iter_outlives()) {
459            match c.kind {
460                ConstraintKind::VarSubVar => {
461                    let sub_vid = c.sub.as_var();
462                    let sup_vid = c.sup.as_var();
463                    {
464                        let deps1 = vid_map.entry(RegionTarget::RegionVid(sub_vid)).or_default();
465                        deps1.larger.insert(RegionTarget::RegionVid(sup_vid));
466                    }
467
468                    let deps2 = vid_map.entry(RegionTarget::RegionVid(sup_vid)).or_default();
469                    deps2.smaller.insert(RegionTarget::RegionVid(sub_vid));
470                }
471                ConstraintKind::RegSubVar => {
472                    let sup_vid = c.sup.as_var();
473                    {
474                        let deps1 = vid_map.entry(RegionTarget::Region(c.sub)).or_default();
475                        deps1.larger.insert(RegionTarget::RegionVid(sup_vid));
476                    }
477
478                    let deps2 = vid_map.entry(RegionTarget::RegionVid(sup_vid)).or_default();
479                    deps2.smaller.insert(RegionTarget::Region(c.sub));
480                }
481                ConstraintKind::VarSubReg => {
482                    let sub_vid = c.sub.as_var();
483                    finished_map.insert(sub_vid, c.sup);
484                }
485                ConstraintKind::RegSubReg => {
486                    {
487                        let deps1 = vid_map.entry(RegionTarget::Region(c.sub)).or_default();
488                        deps1.larger.insert(RegionTarget::Region(c.sup));
489                    }
490
491                    let deps2 = vid_map.entry(RegionTarget::Region(c.sup)).or_default();
492                    deps2.smaller.insert(RegionTarget::Region(c.sub));
493                }
494
495                ConstraintKind::VarEqVar | ConstraintKind::VarEqReg | ConstraintKind::RegEqReg => {
496                    ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
497                }
498            }
499        }
500
501        while !vid_map.is_empty() {
502            let target = *vid_map.keys().next().unwrap();
503            let deps = vid_map.swap_remove(&target).unwrap();
504
505            for smaller in deps.smaller.iter() {
506                for larger in deps.larger.iter() {
507                    match (smaller, larger) {
508                        (&RegionTarget::Region(_), &RegionTarget::Region(_)) => {
509                            if let IndexEntry::Occupied(v) = vid_map.entry(*smaller) {
510                                let smaller_deps = v.into_mut();
511                                smaller_deps.larger.insert(*larger);
512                                smaller_deps.larger.swap_remove(&target);
513                            }
514
515                            if let IndexEntry::Occupied(v) = vid_map.entry(*larger) {
516                                let larger_deps = v.into_mut();
517                                larger_deps.smaller.insert(*smaller);
518                                larger_deps.smaller.swap_remove(&target);
519                            }
520                        }
521                        (&RegionTarget::RegionVid(v1), &RegionTarget::Region(r1)) => {
522                            finished_map.insert(v1, r1);
523                        }
524                        (&RegionTarget::Region(_), &RegionTarget::RegionVid(_)) => {
525                            // Do nothing; we don't care about regions that are smaller than vids.
526                        }
527                        (&RegionTarget::RegionVid(_), &RegionTarget::RegionVid(_)) => {
528                            if let IndexEntry::Occupied(v) = vid_map.entry(*smaller) {
529                                let smaller_deps = v.into_mut();
530                                smaller_deps.larger.insert(*larger);
531                                smaller_deps.larger.swap_remove(&target);
532                            }
533
534                            if let IndexEntry::Occupied(v) = vid_map.entry(*larger) {
535                                let larger_deps = v.into_mut();
536                                larger_deps.smaller.insert(*smaller);
537                                larger_deps.smaller.swap_remove(&target);
538                            }
539                        }
540                    }
541                }
542            }
543        }
544
545        finished_map
546    }
547
548    fn is_param_no_infer(&self, args: GenericArgsRef<'tcx>) -> bool {
549        self.is_of_param(args.type_at(0)) && !args.types().any(|t| t.has_infer_types())
550    }
551
552    pub fn is_of_param(&self, ty: Ty<'tcx>) -> bool {
553        match ty.kind() {
554            ty::Param(_) => true,
555            ty::Alias(p @ ty::AliasTy { kind: ty::Projection { .. }, .. }) => {
556                self.is_of_param(p.self_ty())
557            }
558            _ => false,
559        }
560    }
561
562    fn is_self_referential_projection(&self, p: ty::PolyProjectionPredicate<'tcx>) -> bool {
563        if let Some(ty) = p.term().skip_binder().as_type() {
564            #[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
    ty::Alias(proj @ ty::AliasTy { kind: ty::Projection { .. }, .. }) if
        proj == &p.skip_binder().projection_term.expect_ty(self.tcx) => true,
    _ => false,
}matches!(ty.kind(), ty::Alias(proj @ ty::AliasTy { kind: ty::Projection { .. }, .. }) if proj == &p.skip_binder().projection_term.expect_ty(self.tcx))
565        } else {
566            false
567        }
568    }
569
570    fn evaluate_nested_obligations(
571        &self,
572        ty: Ty<'_>,
573        nested: impl Iterator<Item = PredicateObligation<'tcx>>,
574        computed_preds: &mut FxIndexSet<ty::Predicate<'tcx>>,
575        fresh_preds: &mut FxIndexSet<ty::Predicate<'tcx>>,
576        predicates: &mut VecDeque<ty::PolyTraitPredicate<'tcx>>,
577        selcx: &mut SelectionContext<'_, 'tcx>,
578    ) -> bool {
579        let dummy_cause = ObligationCause::dummy();
580
581        for obligation in nested {
582            let is_new_pred =
583                fresh_preds.insert(self.clean_pred(selcx.infcx, obligation.predicate));
584
585            // Resolve any inference variables that we can, to help selection succeed
586            let predicate = selcx.infcx.resolve_vars_if_possible(obligation.predicate);
587
588            // We only add a predicate as a user-displayable bound if
589            // it involves a generic parameter, and doesn't contain
590            // any inference variables.
591            //
592            // Displaying a bound involving a concrete type (instead of a generic
593            // parameter) would be pointless, since it's always true
594            // (e.g. u8: Copy)
595            // Displaying an inference variable is impossible, since they're
596            // an internal compiler detail without a defined visual representation
597            //
598            // We check this by calling is_of_param on the relevant types
599            // from the various possible predicates
600
601            let bound_predicate = predicate.kind();
602            match bound_predicate.skip_binder() {
603                ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)) => {
604                    // Add this to `predicates` so that we end up calling `select`
605                    // with it. If this predicate ends up being unimplemented,
606                    // then `evaluate_predicates` will handle adding it the `ParamEnv`
607                    // if possible.
608                    predicates.push_back(bound_predicate.rebind(p));
609                }
610                ty::PredicateKind::Clause(ty::ClauseKind::Projection(p)) => {
611                    let p = bound_predicate.rebind(p);
612                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/auto_trait.rs:612",
                        "rustc_trait_selection::traits::auto_trait",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/auto_trait.rs"),
                        ::tracing_core::__macro_support::Option::Some(612u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::auto_trait"),
                        ::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!("evaluate_nested_obligations: examining projection predicate {0:?}",
                                                    predicate) as &dyn Value))])
            });
    } else { ; }
};debug!(
613                        "evaluate_nested_obligations: examining projection predicate {:?}",
614                        predicate
615                    );
616
617                    // As described above, we only want to display
618                    // bounds which include a generic parameter but don't include
619                    // an inference variable.
620                    // Additionally, we check if we've seen this predicate before,
621                    // to avoid rendering duplicate bounds to the user.
622                    if self.is_param_no_infer(p.skip_binder().projection_term.args)
623                        && !p.term().skip_binder().has_infer_types()
624                        && is_new_pred
625                    {
626                        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/auto_trait.rs:626",
                        "rustc_trait_selection::traits::auto_trait",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/auto_trait.rs"),
                        ::tracing_core::__macro_support::Option::Some(626u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::auto_trait"),
                        ::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!("evaluate_nested_obligations: adding projection predicate to computed_preds: {0:?}",
                                                    predicate) as &dyn Value))])
            });
    } else { ; }
};debug!(
627                            "evaluate_nested_obligations: adding projection predicate \
628                            to computed_preds: {:?}",
629                            predicate
630                        );
631
632                        // Under unusual circumstances, we can end up with a self-referential
633                        // projection predicate. For example:
634                        // <T as MyType>::Value == <T as MyType>::Value
635                        // Not only is displaying this to the user pointless,
636                        // having it in the ParamEnv will cause an issue if we try to call
637                        // poly_project_and_unify_type on the predicate, since this kind of
638                        // predicate will normally never end up in a ParamEnv.
639                        //
640                        // For these reasons, we ignore these weird predicates,
641                        // ensuring that we're able to properly synthesize an auto trait impl
642                        if self.is_self_referential_projection(p) {
643                            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/auto_trait.rs:643",
                        "rustc_trait_selection::traits::auto_trait",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/auto_trait.rs"),
                        ::tracing_core::__macro_support::Option::Some(643u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::auto_trait"),
                        ::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!("evaluate_nested_obligations: encountered a projection\n                                 predicate equating a type with itself! Skipping")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(
644                                "evaluate_nested_obligations: encountered a projection
645                                 predicate equating a type with itself! Skipping"
646                            );
647                        } else {
648                            self.add_user_pred(computed_preds, predicate);
649                        }
650                    }
651
652                    // There are three possible cases when we project a predicate:
653                    //
654                    // 1. We encounter an error. This means that it's impossible for
655                    // our current type to implement the auto trait - there's bound
656                    // that we could add to our ParamEnv that would 'fix' this kind
657                    // of error, as it's not caused by an unimplemented type.
658                    //
659                    // 2. We successfully project the predicate (Ok(Some(_))), generating
660                    //  some subobligations. We then process these subobligations
661                    //  like any other generated sub-obligations.
662                    //
663                    // 3. We receive an 'ambiguous' result (Ok(None))
664                    // If we were actually trying to compile a crate,
665                    // we would need to re-process this obligation later.
666                    // However, all we care about is finding out what bounds
667                    // are needed for our type to implement a particular auto trait.
668                    // We've already added this obligation to our computed ParamEnv
669                    // above (if it was necessary). Therefore, we don't need
670                    // to do any further processing of the obligation.
671                    //
672                    // Note that we *must* try to project *all* projection predicates
673                    // we encounter, even ones without inference variable.
674                    // This ensures that we detect any projection errors,
675                    // which indicate that our type can *never* implement the given
676                    // auto trait. In that case, we will generate an explicit negative
677                    // impl (e.g. 'impl !Send for MyType'). However, we don't
678                    // try to process any of the generated subobligations -
679                    // they contain no new information, since we already know
680                    // that our type implements the projected-through trait,
681                    // and can lead to weird region issues.
682                    //
683                    // Normally, we'll generate a negative impl as a result of encountering
684                    // a type with an explicit negative impl of an auto trait
685                    // (for example, raw pointers have !Send and !Sync impls)
686                    // However, through some **interesting** manipulations of the type
687                    // system, it's actually possible to write a type that never
688                    // implements an auto trait due to a projection error, not a normal
689                    // negative impl error. To properly handle this case, we need
690                    // to ensure that we catch any potential projection errors,
691                    // and turn them into an explicit negative impl for our type.
692                    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/auto_trait.rs:692",
                        "rustc_trait_selection::traits::auto_trait",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/auto_trait.rs"),
                        ::tracing_core::__macro_support::Option::Some(692u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::auto_trait"),
                        ::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!("Projecting and unifying projection predicate {0:?}",
                                                    predicate) as &dyn Value))])
            });
    } else { ; }
};debug!("Projecting and unifying projection predicate {:?}", predicate);
693
694                    match project::poly_project_and_unify_term(selcx, &obligation.with(self.tcx, p))
695                    {
696                        ProjectAndUnifyResult::MismatchedProjectionTypes(e) => {
697                            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/auto_trait.rs:697",
                        "rustc_trait_selection::traits::auto_trait",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/auto_trait.rs"),
                        ::tracing_core::__macro_support::Option::Some(697u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::auto_trait"),
                        ::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!("evaluate_nested_obligations: Unable to unify predicate \'{0:?}\' \'{1:?}\', bailing out",
                                                    ty, e) as &dyn Value))])
            });
    } else { ; }
};debug!(
698                                "evaluate_nested_obligations: Unable to unify predicate \
699                                 '{:?}' '{:?}', bailing out",
700                                ty, e
701                            );
702                            return false;
703                        }
704                        ProjectAndUnifyResult::Recursive => {
705                            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/traits/auto_trait.rs:705",
                        "rustc_trait_selection::traits::auto_trait",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/traits/auto_trait.rs"),
                        ::tracing_core::__macro_support::Option::Some(705u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::traits::auto_trait"),
                        ::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!("evaluate_nested_obligations: recursive projection predicate")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("evaluate_nested_obligations: recursive projection predicate");
706                            return false;
707                        }
708                        ProjectAndUnifyResult::Holds(v) => {
709                            // We only care about sub-obligations
710                            // when we started out trying to unify
711                            // some inference variables. See the comment above
712                            // for more information
713                            if p.term().skip_binder().has_infer_types() {
714                                if !self.evaluate_nested_obligations(
715                                    ty,
716                                    v.into_iter(),
717                                    computed_preds,
718                                    fresh_preds,
719                                    predicates,
720                                    selcx,
721                                ) {
722                                    return false;
723                                }
724                            }
725                        }
726                        ProjectAndUnifyResult::FailedNormalization => {
727                            // It's ok not to make progress when have no inference variables -
728                            // in that case, we were only performing unification to check if an
729                            // error occurred (which would indicate that it's impossible for our
730                            // type to implement the auto trait).
731                            // However, we should always make progress (either by generating
732                            // subobligations or getting an error) when we started off with
733                            // inference variables
734                            if p.term().skip_binder().has_infer_types() {
735                                {
    ::core::panicking::panic_fmt(format_args!("Unexpected result when selecting {0:?} {1:?}",
            ty, obligation));
}panic!("Unexpected result when selecting {ty:?} {obligation:?}")
736                            }
737                        }
738                    }
739                }
740                ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(binder)) => {
741                    let binder = bound_predicate.rebind(binder);
742                    selcx.infcx.enter_forall(binder, |pred| {
743                        selcx.infcx.register_region_outlives_constraint(
744                            pred,
745                            ty::VisibleForLeakCheck::Yes,
746                            &dummy_cause,
747                        );
748                    });
749                }
750                ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(binder)) => {
751                    let binder = bound_predicate.rebind(binder);
752                    match (
753                        binder.no_bound_vars(),
754                        binder.map_bound_ref(|pred| pred.0).no_bound_vars(),
755                    ) {
756                        (None, Some(t_a)) => {
757                            selcx.infcx.register_type_outlives_constraint(
758                                t_a,
759                                selcx.infcx.tcx.lifetimes.re_static,
760                                &dummy_cause,
761                            );
762                        }
763                        (Some(ty::OutlivesPredicate(t_a, r_b)), _) => {
764                            selcx.infcx.register_type_outlives_constraint(t_a, r_b, &dummy_cause);
765                        }
766                        _ => {}
767                    };
768                }
769                ty::PredicateKind::ConstEquate(c1, c2) => {
770                    let evaluate = |c: ty::Const<'tcx>| {
771                        if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() {
772                            let ct =
773                                super::try_evaluate_const(selcx.infcx, c, obligation.param_env);
774
775                            if let Err(EvaluateConstErr::InvalidConstParamTy(_)) = ct {
776                                self.tcx.dcx().emit_err(UnableToConstructConstantValue {
777                                    span: self.tcx.def_span(unevaluated.def),
778                                    unevaluated,
779                                });
780                            }
781
782                            ct
783                        } else {
784                            Ok(c)
785                        }
786                    };
787
788                    match (evaluate(c1), evaluate(c2)) {
789                        (Ok(c1), Ok(c2)) => {
790                            match selcx.infcx.at(&obligation.cause, obligation.param_env).eq(
791                                DefineOpaqueTypes::Yes,
792                                c1,
793                                c2,
794                            ) {
795                                Ok(_) => (),
796                                Err(_) => return false,
797                            }
798                        }
799                        _ => return false,
800                    }
801                }
802
803                // There's not really much we can do with these predicates -
804                // we start out with a `ParamEnv` with no inference variables,
805                // and these don't correspond to adding any new bounds to
806                // the `ParamEnv`.
807                ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..))
808                | ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
809                | ty::PredicateKind::NormalizesTo(..)
810                | ty::PredicateKind::AliasRelate(..)
811                | ty::PredicateKind::DynCompatible(..)
812                | ty::PredicateKind::Subtype(..)
813                | ty::PredicateKind::Coerce(..)
814                | ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(_))
815                | ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(..)) => {}
816                ty::PredicateKind::Ambiguous => return false,
817
818                // FIXME(generic_const_exprs): you can absolutely add this as a where clauses
819                ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..)) => return false,
820            };
821        }
822        true
823    }
824
825    pub fn clean_pred(
826        &self,
827        infcx: &InferCtxt<'tcx>,
828        p: ty::Predicate<'tcx>,
829    ) -> ty::Predicate<'tcx> {
830        p.fold_with(&mut TypeFreshener::new(infcx))
831    }
832}