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