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(Eq, PartialEq, Hash, Copy, Clone, Debug)]
23pub enum RegionTarget<'tcx> {
24 Region(Region<'tcx>),
25 RegionVid(RegionVid),
26}
27
28#[derive(Default, Debug, 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 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 panic!("Failed to fully process: {ty:?} {trait_did:?} {orig_env:?}")
146 });
147
148 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 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(®ion_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 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 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 _ => 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 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 ®ions.constraints {
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 }
494
495 while !vid_map.is_empty() {
496 let target = *vid_map.keys().next().unwrap();
497 let deps = vid_map.swap_remove(&target).unwrap();
498
499 for smaller in deps.smaller.iter() {
500 for larger in deps.larger.iter() {
501 match (smaller, larger) {
502 (&RegionTarget::Region(_), &RegionTarget::Region(_)) => {
503 if let IndexEntry::Occupied(v) = vid_map.entry(*smaller) {
504 let smaller_deps = v.into_mut();
505 smaller_deps.larger.insert(*larger);
506 smaller_deps.larger.swap_remove(&target);
507 }
508
509 if let IndexEntry::Occupied(v) = vid_map.entry(*larger) {
510 let 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(_)) => {
522 if let IndexEntry::Occupied(v) = vid_map.entry(*smaller) {
523 let smaller_deps = v.into_mut();
524 smaller_deps.larger.insert(*larger);
525 smaller_deps.larger.swap_remove(&target);
526 }
527
528 if let IndexEntry::Occupied(v) = vid_map.entry(*larger) {
529 let larger_deps = v.into_mut();
530 larger_deps.smaller.insert(*smaller);
531 larger_deps.smaller.swap_remove(&target);
532 }
533 }
534 }
535 }
536 }
537 }
538
539 finished_map
540 }
541
542 fn is_param_no_infer(&self, args: GenericArgsRef<'tcx>) -> bool {
543 self.is_of_param(args.type_at(0)) && !args.types().any(|t| t.has_infer_types())
544 }
545
546 pub fn is_of_param(&self, ty: Ty<'tcx>) -> bool {
547 match ty.kind() {
548 ty::Param(_) => true,
549 ty::Alias(ty::Projection, p) => self.is_of_param(p.self_ty()),
550 _ => false,
551 }
552 }
553
554 fn is_self_referential_projection(&self, p: ty::PolyProjectionPredicate<'tcx>) -> bool {
555 if let Some(ty) = p.term().skip_binder().as_type() {
556 matches!(ty.kind(), ty::Alias(ty::Projection, proj) if proj == &p.skip_binder().projection_term.expect_ty(self.tcx))
557 } else {
558 false
559 }
560 }
561
562 fn 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 {
571 let dummy_cause = ObligationCause::dummy();
572
573 for obligation in nested {
574 let is_new_pred =
575 fresh_preds.insert(self.clean_pred(selcx.infcx, obligation.predicate));
576
577 // Resolve any inference variables that we can, to help selection succeed
578 let predicate = selcx.infcx.resolve_vars_if_possible(obligation.predicate);
579
580 // 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
592
593 let bound_predicate = predicate.kind();
594 match 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.
600 predicates.push_back(bound_predicate.rebind(p));
601 }
602 ty::PredicateKind::Clause(ty::ClauseKind::Projection(p)) => {
603 let p = bound_predicate.rebind(p);
604 debug!(
605 "evaluate_nested_obligations: examining projection predicate {:?}",
606 predicate
607 );
608
609 // 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.
614 if 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 debug!(
619 "evaluate_nested_obligations: adding projection predicate \
620 to computed_preds: {:?}",
621 predicate
622 );
623
624 // 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
634 if self.is_self_referential_projection(p) {
635 debug!(
636 "evaluate_nested_obligations: encountered a projection
637 predicate equating a type with itself! Skipping"
638 );
639 } else {
640 self.add_user_pred(computed_preds, predicate);
641 }
642 }
643
644 // 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 debug!("Projecting and unifying projection predicate {:?}", predicate);
685
686 match project::poly_project_and_unify_term(selcx, &obligation.with(self.tcx, p))
687 {
688 ProjectAndUnifyResult::MismatchedProjectionTypes(e) => {
689 debug!(
690 "evaluate_nested_obligations: Unable to unify predicate \
691 '{:?}' '{:?}', bailing out",
692 ty, e
693 );
694 return false;
695 }
696 ProjectAndUnifyResult::Recursive => {
697 debug!("evaluate_nested_obligations: recursive projection predicate");
698 return 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
705 if p.term().skip_binder().has_infer_types() {
706 if !self.evaluate_nested_obligations(
707 ty,
708 v.into_iter(),
709 computed_preds,
710 fresh_preds,
711 predicates,
712 selcx,
713 ) {
714 return 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
726 if p.term().skip_binder().has_infer_types() {
727 panic!("Unexpected result when selecting {ty:?} {obligation:?}")
728 }
729 }
730 }
731 }
732 ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(binder)) => {
733 let 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)) => {
739 let binder = bound_predicate.rebind(binder);
740 match (
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) => {
762 let evaluate = |c: ty::Const<'tcx>| {
763 if let ty::ConstKind::Unevaluated(unevaluated) = c.kind() {
764 let ct = super::try_evaluate_const(
765 selcx.infcx,
766 c,
767 obligation.param_env,
768 );
769
770 if let Err(EvaluateConstErr::InvalidConstParamTy(_)) = ct {
771 self.tcx.dcx().emit_err(UnableToConstructConstantValue {
772 span: self.tcx.def_span(unevaluated.def),
773 unevaluated,
774 });
775 }
776
777 ct
778 } else {
779 Ok(c)
780 }
781 };
782
783 match (evaluate(c1), evaluate(c2)) {
784 (Ok(c1), Ok(c2)) => {
785 match selcx.infcx.at(&obligation.cause, obligation.param_env).eq(DefineOpaqueTypes::Yes,c1, c2)
786 {
787 Ok(_) => (),
788 Err(_) => return false,
789 }
790 }
791 _ => return false,
792 }
793 }
794
795 // 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`.
799 ty::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 }
813 true
814 }
815
816 pub fn clean_pred(
817 &self,
818 infcx: &InferCtxt<'tcx>,
819 p: ty::Predicate<'tcx>,
820 ) -> ty::Predicate<'tcx> {
821 p.fold_with(&mut TypeFreshener::new(infcx))
822 }
823}