1//! Support code for rustdoc and external tools.
2//! You really don't want to be using this unless you need to.
34use std::collections::VecDeque;
5use std::iter;
67use 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;
1314use 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;
2021// 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_receiver_is_total_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}
2728#[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> {
30pub larger: FxIndexSet<RegionTarget<'tcx>>,
31pub smaller: FxIndexSet<RegionTarget<'tcx>>,
32}
3334pub enum AutoTraitResult<A> {
35 ExplicitImpl,
36 PositiveImpl(A),
37 NegativeImpl,
38}
3940pub struct AutoTraitInfo<'cx> {
41pub full_user_env: ty::ParamEnv<'cx>,
42pub region_data: RegionConstraintData<'cx>,
43pub vid_to_region: FxIndexMap<ty::RegionVid, ty::Region<'cx>>,
44}
4546pub struct AutoTraitFinder<'tcx> {
47 tcx: TyCtxt<'tcx>,
48}
4950impl<'tcx> AutoTraitFinder<'tcx> {
51pub fn new(tcx: TyCtxt<'tcx>) -> Self {
52AutoTraitFinder { tcx }
53 }
5455/// 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.
73pub fn find_auto_trait_generics<A>(
74&self,
75 ty: Ty<'tcx>,
76 typing_env: ty::TypingEnv<'tcx>,
77 trait_did: DefId,
78mut auto_trait_callback: impl FnMut(AutoTraitInfo<'tcx>) -> A,
79 ) -> AutoTraitResult<A> {
80let tcx = self.tcx;
8182let trait_ref = ty::TraitRef::new(tcx, trait_did, [ty]);
8384let (infcx, orig_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
85let mut selcx = SelectionContext::new(&infcx);
86for polarity in [ty::PredicatePolarity::Positive, ty::PredicatePolarity::Negative] {
87let result = selcx.select(&Obligation::new(
88 tcx,
89 ObligationCause::dummy(),
90 orig_env,
91 ty::TraitPredicate { trait_ref, polarity },
92 ));
93if 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
96return AutoTraitResult::ExplicitImpl;
97 }
98 }
99100let (infcx, orig_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
101let mut fresh_preds = FxIndexSet::default();
102103// 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.
135136let Some((new_env, user_env)) =
137self.evaluate_predicates(&infcx, trait_did, ty, orig_env, orig_env, &mut fresh_preds)
138else {
139return AutoTraitResult::NegativeImpl;
140 };
141142let (full_env, full_user_env) = self143 .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 });
147148{
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 );
153154// 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.
157let ocx = ObligationCtxt::new(&infcx);
158ocx.register_bound(ObligationCause::dummy(), full_env, ty, trait_did);
159let errors = ocx.evaluate_obligations_error_on_ambiguity();
160if !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 }
163164let outlives_env = OutlivesEnvironment::new(&infcx, CRATE_DEF_ID, full_env, []);
165let _ = infcx.process_registered_region_obligations(&outlives_env, |ty, _| Ok(ty));
166167let region_data = infcx.inner.borrow_mut().unwrap_region_constraints().data().clone();
168169let vid_to_region = self.map_vid_to_region(®ion_data);
170171let info = AutoTraitInfo { full_user_env, region_data, vid_to_region };
172173 AutoTraitResult::PositiveImpl(auto_trait_callback(info))
174 }
175176/// 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.
215fn 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>)> {
224let tcx = infcx.tcx;
225226// 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.
229for predicate in param_env.caller_bounds() {
230 fresh_preds.insert(self.clean_pred(infcx, predicate.as_predicate()));
231 }
232233let mut select = SelectionContext::new(infcx);
234235let mut already_visited = UnordSet::new();
236let mut predicates = VecDeque::new();
237predicates.push_back(ty::Binder::dummy(ty::TraitPredicate {
238 trait_ref: ty::TraitRef::new(infcx.tcx, trait_did, [ty]),
239240// Auto traits are positive
241polarity: ty::PredicatePolarity::Positive,
242 }));
243244let computed_preds = param_env.caller_bounds().iter().map(|c| c.as_predicate());
245let mut user_computed_preds: FxIndexSet<_> =
246user_env.caller_bounds().iter().map(|c| c.as_predicate()).collect();
247248let mut new_env = param_env;
249let dummy_cause = ObligationCause::dummy();
250251while let Some(pred) = predicates.pop_front() {
252if !already_visited.insert(pred) {
253continue;
254 }
255256// Call `infcx.resolve_vars_if_possible` to see if we can
257 // get rid of any inference variables.
258let obligation = infcx.resolve_vars_if_possible(Obligation::new(
259 tcx,
260 dummy_cause.clone(),
261 new_env,
262 pred,
263 ));
264let result = select.poly_select(&obligation);
265266match result {
267Ok(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.
270271if let ImplSource::UserDefined(ImplSourceUserDefinedData {
272 impl_def_id, ..
273 }) = impl_source
274 {
275// Blame 'tidy' for the weird bracket placement.
276if 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 );
282return None;
283 }
284 }
285286let obligations = impl_source.borrow_nested_obligations().iter().cloned();
287288if !self.evaluate_nested_obligations(
289 ty,
290 obligations,
291&mut user_computed_preds,
292 fresh_preds,
293&mut predicates,
294&mut select,
295 ) {
296return None;
297 }
298 }
299Ok(None) => {}
300Err(SelectionError::Unimplemented) => {
301if self.is_param_no_infer(pred.skip_binder().trait_ref.args) {
302 already_visited.remove(&pred);
303self.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 );
313return None;
314 }
315 }
316_ => {
::core::panicking::panic_fmt(format_args!("Unexpected error for \'{0:?}\': {1:?}",
ty, result));
}panic!("Unexpected error for '{ty:?}': {result:?}"),
317 };
318319let 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 }
325326let final_user_env = ty::ParamEnv::new(
327tcx.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 );
334335Some((new_env, final_user_env))
336 }
337338/// 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`).
361fn add_user_pred(
362&self,
363 user_computed_preds: &mut FxIndexSet<ty::Predicate<'tcx>>,
364 new_pred: ty::Predicate<'tcx>,
365 ) {
366let mut should_add_new = true;
367user_computed_preds.retain(|&old_pred| {
368if 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 {
373if new_trait.def_id() == old_trait.def_id() {
374let new_args = new_trait.trait_ref.args;
375let old_args = old_trait.trait_ref.args;
376377if !new_args.types().eq(old_args.types()) {
378// We can't compare lifetimes if the types are different,
379 // so skip checking `old_pred`.
380return true;
381 }
382383for (new_region, old_region) in
384iter::zip(new_args.regions(), old_args.regions())
385 {
386match (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(_, _)) => {}
390391 (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`.
415return 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`.
432should_add_new = false
433}
434_ => {}
435 }
436 }
437 }
438 }
439true
440});
441442if should_add_new {
443user_computed_preds.insert(new_pred);
444 }
445 }
446447/// 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.
449fn map_vid_to_region<'cx>(
450&self,
451 regions: &RegionConstraintData<'cx>,
452 ) -> FxIndexMap<ty::RegionVid, ty::Region<'cx>> {
453let mut vid_map = FxIndexMap::<RegionTarget<'cx>, RegionDeps<'cx>>::default();
454let mut finished_map = FxIndexMap::default();
455456for (c, _) in ®ions.constraints {
457match c.kind {
458 ConstraintKind::VarSubVar => {
459let sub_vid = c.sub.as_var();
460let sup_vid = c.sup.as_var();
461 {
462let deps1 = vid_map.entry(RegionTarget::RegionVid(sub_vid)).or_default();
463 deps1.larger.insert(RegionTarget::RegionVid(sup_vid));
464 }
465466let deps2 = vid_map.entry(RegionTarget::RegionVid(sup_vid)).or_default();
467 deps2.smaller.insert(RegionTarget::RegionVid(sub_vid));
468 }
469 ConstraintKind::RegSubVar => {
470let sup_vid = c.sup.as_var();
471 {
472let deps1 = vid_map.entry(RegionTarget::Region(c.sub)).or_default();
473 deps1.larger.insert(RegionTarget::RegionVid(sup_vid));
474 }
475476let deps2 = vid_map.entry(RegionTarget::RegionVid(sup_vid)).or_default();
477 deps2.smaller.insert(RegionTarget::Region(c.sub));
478 }
479 ConstraintKind::VarSubReg => {
480let sub_vid = c.sub.as_var();
481 finished_map.insert(sub_vid, c.sup);
482 }
483 ConstraintKind::RegSubReg => {
484 {
485let deps1 = vid_map.entry(RegionTarget::Region(c.sub)).or_default();
486 deps1.larger.insert(RegionTarget::Region(c.sup));
487 }
488489let deps2 = vid_map.entry(RegionTarget::Region(c.sup)).or_default();
490 deps2.smaller.insert(RegionTarget::Region(c.sub));
491 }
492 }
493 }
494495while !vid_map.is_empty() {
496let target = *vid_map.keys().next().unwrap();
497let deps = vid_map.swap_remove(&target).unwrap();
498499for smaller in deps.smaller.iter() {
500for larger in deps.larger.iter() {
501match (smaller, larger) {
502 (&RegionTarget::Region(_), &RegionTarget::Region(_)) => {
503if let IndexEntry::Occupied(v) = vid_map.entry(*smaller) {
504let smaller_deps = v.into_mut();
505 smaller_deps.larger.insert(*larger);
506 smaller_deps.larger.swap_remove(&target);
507 }
508509if let IndexEntry::Occupied(v) = vid_map.entry(*larger) {
510let larger_deps = v.into_mut();
511 larger_deps.smaller.insert(*smaller);
512 larger_deps.smaller.swap_remove(&target);
513 }
514 }
515 (&RegionTarget::RegionVid(v1), &RegionTarget::Region(r1)) => {
516 finished_map.insert(v1, r1);
517 }
518 (&RegionTarget::Region(_), &RegionTarget::RegionVid(_)) => {
519// Do nothing; we don't care about regions that are smaller than vids.
520}
521 (&RegionTarget::RegionVid(_), &RegionTarget::RegionVid(_)) => {
522if let IndexEntry::Occupied(v) = vid_map.entry(*smaller) {
523let smaller_deps = v.into_mut();
524 smaller_deps.larger.insert(*larger);
525 smaller_deps.larger.swap_remove(&target);
526 }
527528if let IndexEntry::Occupied(v) = vid_map.entry(*larger) {
529let larger_deps = v.into_mut();
530 larger_deps.smaller.insert(*smaller);
531 larger_deps.smaller.swap_remove(&target);
532 }
533 }
534 }
535 }
536 }
537 }
538539finished_map540 }
541542fn is_param_no_infer(&self, args: GenericArgsRef<'tcx>) -> bool {
543self.is_of_param(args.type_at(0)) && !args.types().any(|t| t.has_infer_types())
544 }
545546pub fn is_of_param(&self, ty: Ty<'tcx>) -> bool {
547match ty.kind() {
548 ty::Param(_) => true,
549 ty::Alias(ty::Projection, p) => self.is_of_param(p.self_ty()),
550_ => false,
551 }
552 }
553554fn is_self_referential_projection(&self, p: ty::PolyProjectionPredicate<'tcx>) -> bool {
555if let Some(ty) = p.term().skip_binder().as_type() {
556#[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
ty::Alias(ty::Projection, proj) if
proj == &p.skip_binder().projection_term.expect_ty(self.tcx) => true,
_ => false,
}matches!(ty.kind(), ty::Alias(ty::Projection, proj) if proj == &p.skip_binder().projection_term.expect_ty(self.tcx))557 } else {
558false
559}
560 }
561562fn evaluate_nested_obligations(
563&self,
564 ty: Ty<'_>,
565 nested: impl Iterator<Item = PredicateObligation<'tcx>>,
566 computed_preds: &mut FxIndexSet<ty::Predicate<'tcx>>,
567 fresh_preds: &mut FxIndexSet<ty::Predicate<'tcx>>,
568 predicates: &mut VecDeque<ty::PolyTraitPredicate<'tcx>>,
569 selcx: &mut SelectionContext<'_, 'tcx>,
570 ) -> bool {
571let dummy_cause = ObligationCause::dummy();
572573for obligation in nested {
574let is_new_pred =
575 fresh_preds.insert(self.clean_pred(selcx.infcx, obligation.predicate));
576577// Resolve any inference variables that we can, to help selection succeed
578let predicate = selcx.infcx.resolve_vars_if_possible(obligation.predicate);
579580// We only add a predicate as a user-displayable bound if
581 // it involves a generic parameter, and doesn't contain
582 // any inference variables.
583 //
584 // Displaying a bound involving a concrete type (instead of a generic
585 // parameter) would be pointless, since it's always true
586 // (e.g. u8: Copy)
587 // Displaying an inference variable is impossible, since they're
588 // an internal compiler detail without a defined visual representation
589 //
590 // We check this by calling is_of_param on the relevant types
591 // from the various possible predicates
592593let bound_predicate = predicate.kind();
594match bound_predicate.skip_binder() {
595 ty::PredicateKind::Clause(ty::ClauseKind::Trait(p)) => {
596// Add this to `predicates` so that we end up calling `select`
597 // with it. If this predicate ends up being unimplemented,
598 // then `evaluate_predicates` will handle adding it the `ParamEnv`
599 // if possible.
600predicates.push_back(bound_predicate.rebind(p));
601 }
602 ty::PredicateKind::Clause(ty::ClauseKind::Projection(p)) => {
603let p = bound_predicate.rebind(p);
604{
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:604",
"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(604u32),
::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!(
605"evaluate_nested_obligations: examining projection predicate {:?}",
606 predicate
607 );
608609// As described above, we only want to display
610 // bounds which include a generic parameter but don't include
611 // an inference variable.
612 // Additionally, we check if we've seen this predicate before,
613 // to avoid rendering duplicate bounds to the user.
614if self.is_param_no_infer(p.skip_binder().projection_term.args)
615 && !p.term().skip_binder().has_infer_types()
616 && is_new_pred
617 {
618{
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:618",
"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(618u32),
::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!(
619"evaluate_nested_obligations: adding projection predicate \
620 to computed_preds: {:?}",
621 predicate
622 );
623624// Under unusual circumstances, we can end up with a self-referential
625 // projection predicate. For example:
626 // <T as MyType>::Value == <T as MyType>::Value
627 // Not only is displaying this to the user pointless,
628 // having it in the ParamEnv will cause an issue if we try to call
629 // poly_project_and_unify_type on the predicate, since this kind of
630 // predicate will normally never end up in a ParamEnv.
631 //
632 // For these reasons, we ignore these weird predicates,
633 // ensuring that we're able to properly synthesize an auto trait impl
634if self.is_self_referential_projection(p) {
635{
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:635",
"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(635u32),
::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!(
636"evaluate_nested_obligations: encountered a projection
637 predicate equating a type with itself! Skipping"
638);
639 } else {
640self.add_user_pred(computed_preds, predicate);
641 }
642 }
643644// There are three possible cases when we project a predicate:
645 //
646 // 1. We encounter an error. This means that it's impossible for
647 // our current type to implement the auto trait - there's bound
648 // that we could add to our ParamEnv that would 'fix' this kind
649 // of error, as it's not caused by an unimplemented type.
650 //
651 // 2. We successfully project the predicate (Ok(Some(_))), generating
652 // some subobligations. We then process these subobligations
653 // like any other generated sub-obligations.
654 //
655 // 3. We receive an 'ambiguous' result (Ok(None))
656 // If we were actually trying to compile a crate,
657 // we would need to re-process this obligation later.
658 // However, all we care about is finding out what bounds
659 // are needed for our type to implement a particular auto trait.
660 // We've already added this obligation to our computed ParamEnv
661 // above (if it was necessary). Therefore, we don't need
662 // to do any further processing of the obligation.
663 //
664 // Note that we *must* try to project *all* projection predicates
665 // we encounter, even ones without inference variable.
666 // This ensures that we detect any projection errors,
667 // which indicate that our type can *never* implement the given
668 // auto trait. In that case, we will generate an explicit negative
669 // impl (e.g. 'impl !Send for MyType'). However, we don't
670 // try to process any of the generated subobligations -
671 // they contain no new information, since we already know
672 // that our type implements the projected-through trait,
673 // and can lead to weird region issues.
674 //
675 // Normally, we'll generate a negative impl as a result of encountering
676 // a type with an explicit negative impl of an auto trait
677 // (for example, raw pointers have !Send and !Sync impls)
678 // However, through some **interesting** manipulations of the type
679 // system, it's actually possible to write a type that never
680 // implements an auto trait due to a projection error, not a normal
681 // negative impl error. To properly handle this case, we need
682 // to ensure that we catch any potential projection errors,
683 // and turn them into an explicit negative impl for our type.
684{
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:684",
"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(684u32),
::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);
685686match project::poly_project_and_unify_term(selcx, &obligation.with(self.tcx, p))
687 {
688 ProjectAndUnifyResult::MismatchedProjectionTypes(e) => {
689{
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:689",
"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(689u32),
::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!(
690"evaluate_nested_obligations: Unable to unify predicate \
691 '{:?}' '{:?}', bailing out",
692 ty, e
693 );
694return false;
695 }
696 ProjectAndUnifyResult::Recursive => {
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: recursive projection predicate")
as &dyn Value))])
});
} else { ; }
};debug!("evaluate_nested_obligations: recursive projection predicate");
698return false;
699 }
700 ProjectAndUnifyResult::Holds(v) => {
701// We only care about sub-obligations
702 // when we started out trying to unify
703 // some inference variables. See the comment above
704 // for more information
705if p.term().skip_binder().has_infer_types() {
706if !self.evaluate_nested_obligations(
707 ty,
708 v.into_iter(),
709 computed_preds,
710 fresh_preds,
711 predicates,
712 selcx,
713 ) {
714return false;
715 }
716 }
717 }
718 ProjectAndUnifyResult::FailedNormalization => {
719// It's ok not to make progress when have no inference variables -
720 // in that case, we were only performing unification to check if an
721 // error occurred (which would indicate that it's impossible for our
722 // type to implement the auto trait).
723 // However, we should always make progress (either by generating
724 // subobligations or getting an error) when we started off with
725 // inference variables
726if p.term().skip_binder().has_infer_types() {
727{
::core::panicking::panic_fmt(format_args!("Unexpected result when selecting {0:?} {1:?}",
ty, obligation));
}panic!("Unexpected result when selecting {ty:?} {obligation:?}")728 }
729 }
730 }
731 }
732 ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(binder)) => {
733let binder = bound_predicate.rebind(binder);
734 selcx.infcx.enter_forall(binder, |pred| {
735 selcx.infcx.register_region_outlives_constraint(pred, &dummy_cause);
736 });
737 }
738 ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(binder)) => {
739let binder = bound_predicate.rebind(binder);
740match (
741 binder.no_bound_vars(),
742 binder.map_bound_ref(|pred| pred.0).no_bound_vars(),
743 ) {
744 (None, Some(t_a)) => {
745 selcx.infcx.register_type_outlives_constraint(
746 t_a,
747 selcx.infcx.tcx.lifetimes.re_static,
748&dummy_cause,
749 );
750 }
751 (Some(ty::OutlivesPredicate(t_a, r_b)), _) => {
752 selcx.infcx.register_type_outlives_constraint(
753 t_a,
754 r_b,
755&dummy_cause,
756 );
757 }
758_ => {}
759 };
760 }
761 ty::PredicateKind::ConstEquate(c1, c2) => {
762let evaluate = |c: ty::Const<'tcx>| {
763if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() {
764let ct = super::try_evaluate_const(
765 selcx.infcx,
766 c,
767 obligation.param_env,
768 );
769770if let Err(EvaluateConstErr::InvalidConstParamTy(_)) = ct {
771self.tcx.dcx().emit_err(UnableToConstructConstantValue {
772 span: self.tcx.def_span(unevaluated.def),
773 unevaluated,
774 });
775 }
776777 ct
778 } else {
779Ok(c)
780 }
781 };
782783match (evaluate(c1), evaluate(c2)) {
784 (Ok(c1), Ok(c2)) => {
785match selcx.infcx.at(&obligation.cause, obligation.param_env).eq(DefineOpaqueTypes::Yes,c1, c2)
786 {
787Ok(_) => (),
788Err(_) => return false,
789 }
790 }
791_ => return false,
792 }
793 }
794795// There's not really much we can do with these predicates -
796 // we start out with a `ParamEnv` with no inference variables,
797 // and these don't correspond to adding any new bounds to
798 // the `ParamEnv`.
799ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(..))
800 | ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
801 | ty::PredicateKind::NormalizesTo(..)
802 | ty::PredicateKind::AliasRelate(..)
803 | ty::PredicateKind::DynCompatible(..)
804 | ty::PredicateKind::Subtype(..)
805// FIXME(generic_const_exprs): you can absolutely add this as a where clauses
806| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
807 | ty::PredicateKind::Coerce(..)
808 | ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(_))
809 | ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(..)) => {}
810 ty::PredicateKind::Ambiguous => return false,
811 };
812 }
813true
814}
815816pub fn clean_pred(
817&self,
818 infcx: &InferCtxt<'tcx>,
819 p: ty::Predicate<'tcx>,
820 ) -> ty::Predicate<'tcx> {
821p.fold_with(&mut TypeFreshener::new(infcx))
822 }
823}