]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/select/mod.rs
Fix incorrect auto trait displayed in rustdoc
[rust.git] / compiler / rustc_trait_selection / src / traits / select / mod.rs
1 //! Candidate selection. See the [rustc dev guide] for more information on how this works.
2 //!
3 //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html#selection
4
5 use self::EvaluationResult::*;
6 use self::SelectionCandidate::*;
7
8 use super::coherence::{self, Conflict};
9 use super::const_evaluatable;
10 use super::project;
11 use super::project::normalize_with_depth_to;
12 use super::project::ProjectionTyObligation;
13 use super::util;
14 use super::util::{closure_trait_ref_and_return_type, predicate_for_trait_def};
15 use super::wf;
16 use super::DerivedObligationCause;
17 use super::Normalized;
18 use super::Obligation;
19 use super::ObligationCauseCode;
20 use super::Selection;
21 use super::SelectionResult;
22 use super::TraitQueryMode;
23 use super::{ErrorReporting, Overflow, SelectionError};
24 use super::{ObligationCause, PredicateObligation, TraitObligation};
25
26 use crate::infer::{InferCtxt, InferOk, TypeFreshener};
27 use crate::traits::error_reporting::InferCtxtExt;
28 use crate::traits::project::ProjectionCacheKeyExt;
29 use crate::traits::ProjectionCacheKey;
30 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
31 use rustc_data_structures::stack::ensure_sufficient_stack;
32 use rustc_errors::Diagnostic;
33 use rustc_hir as hir;
34 use rustc_hir::def_id::DefId;
35 use rustc_infer::infer::LateBoundRegionConversionTime;
36 use rustc_middle::dep_graph::{DepKind, DepNodeIndex};
37 use rustc_middle::mir::interpret::ErrorHandled;
38 use rustc_middle::thir::abstract_const::NotConstEvaluatable;
39 use rustc_middle::ty::fast_reject::{self, TreatParams};
40 use rustc_middle::ty::print::with_no_trimmed_paths;
41 use rustc_middle::ty::relate::TypeRelation;
42 use rustc_middle::ty::subst::{GenericArgKind, Subst, SubstsRef};
43 use rustc_middle::ty::{self, PolyProjectionPredicate, ToPolyTraitRef, ToPredicate};
44 use rustc_middle::ty::{Ty, TyCtxt, TypeFoldable};
45 use rustc_span::symbol::sym;
46
47 use std::cell::{Cell, RefCell};
48 use std::cmp;
49 use std::fmt::{self, Display};
50 use std::iter;
51
52 pub use rustc_middle::traits::select::*;
53
54 mod candidate_assembly;
55 mod confirmation;
56
57 #[derive(Clone, Debug)]
58 pub enum IntercrateAmbiguityCause {
59     DownstreamCrate { trait_desc: String, self_desc: Option<String> },
60     UpstreamCrateUpdate { trait_desc: String, self_desc: Option<String> },
61     ReservationImpl { message: String },
62 }
63
64 impl IntercrateAmbiguityCause {
65     /// Emits notes when the overlap is caused by complex intercrate ambiguities.
66     /// See #23980 for details.
67     pub fn add_intercrate_ambiguity_hint(&self, err: &mut Diagnostic) {
68         err.note(&self.intercrate_ambiguity_hint());
69     }
70
71     pub fn intercrate_ambiguity_hint(&self) -> String {
72         match self {
73             IntercrateAmbiguityCause::DownstreamCrate { trait_desc, self_desc } => {
74                 let self_desc = if let Some(ty) = self_desc {
75                     format!(" for type `{}`", ty)
76                 } else {
77                     String::new()
78                 };
79                 format!("downstream crates may implement trait `{}`{}", trait_desc, self_desc)
80             }
81             IntercrateAmbiguityCause::UpstreamCrateUpdate { trait_desc, self_desc } => {
82                 let self_desc = if let Some(ty) = self_desc {
83                     format!(" for type `{}`", ty)
84                 } else {
85                     String::new()
86                 };
87                 format!(
88                     "upstream crates may add a new impl of trait `{}`{} \
89                      in future versions",
90                     trait_desc, self_desc
91                 )
92             }
93             IntercrateAmbiguityCause::ReservationImpl { message } => message.clone(),
94         }
95     }
96 }
97
98 pub struct SelectionContext<'cx, 'tcx> {
99     infcx: &'cx InferCtxt<'cx, 'tcx>,
100
101     /// Freshener used specifically for entries on the obligation
102     /// stack. This ensures that all entries on the stack at one time
103     /// will have the same set of placeholder entries, which is
104     /// important for checking for trait bounds that recursively
105     /// require themselves.
106     freshener: TypeFreshener<'cx, 'tcx>,
107
108     /// If `true`, indicates that the evaluation should be conservative
109     /// and consider the possibility of types outside this crate.
110     /// This comes up primarily when resolving ambiguity. Imagine
111     /// there is some trait reference `$0: Bar` where `$0` is an
112     /// inference variable. If `intercrate` is true, then we can never
113     /// say for sure that this reference is not implemented, even if
114     /// there are *no impls at all for `Bar`*, because `$0` could be
115     /// bound to some type that in a downstream crate that implements
116     /// `Bar`. This is the suitable mode for coherence. Elsewhere,
117     /// though, we set this to false, because we are only interested
118     /// in types that the user could actually have written --- in
119     /// other words, we consider `$0: Bar` to be unimplemented if
120     /// there is no type that the user could *actually name* that
121     /// would satisfy it. This avoids crippling inference, basically.
122     intercrate: bool,
123
124     intercrate_ambiguity_causes: Option<Vec<IntercrateAmbiguityCause>>,
125
126     /// Controls whether or not to filter out negative impls when selecting.
127     /// This is used in librustdoc to distinguish between the lack of an impl
128     /// and a negative impl
129     allow_negative_impls: bool,
130
131     /// The mode that trait queries run in, which informs our error handling
132     /// policy. In essence, canonicalized queries need their errors propagated
133     /// rather than immediately reported because we do not have accurate spans.
134     query_mode: TraitQueryMode,
135 }
136
137 // A stack that walks back up the stack frame.
138 struct TraitObligationStack<'prev, 'tcx> {
139     obligation: &'prev TraitObligation<'tcx>,
140
141     /// The trait predicate from `obligation` but "freshened" with the
142     /// selection-context's freshener. Used to check for recursion.
143     fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
144
145     /// Starts out equal to `depth` -- if, during evaluation, we
146     /// encounter a cycle, then we will set this flag to the minimum
147     /// depth of that cycle for all participants in the cycle. These
148     /// participants will then forego caching their results. This is
149     /// not the most efficient solution, but it addresses #60010. The
150     /// problem we are trying to prevent:
151     ///
152     /// - If you have `A: AutoTrait` requires `B: AutoTrait` and `C: NonAutoTrait`
153     /// - `B: AutoTrait` requires `A: AutoTrait` (coinductive cycle, ok)
154     /// - `C: NonAutoTrait` requires `A: AutoTrait` (non-coinductive cycle, not ok)
155     ///
156     /// you don't want to cache that `B: AutoTrait` or `A: AutoTrait`
157     /// is `EvaluatedToOk`; this is because they were only considered
158     /// ok on the premise that if `A: AutoTrait` held, but we indeed
159     /// encountered a problem (later on) with `A: AutoTrait. So we
160     /// currently set a flag on the stack node for `B: AutoTrait` (as
161     /// well as the second instance of `A: AutoTrait`) to suppress
162     /// caching.
163     ///
164     /// This is a simple, targeted fix. A more-performant fix requires
165     /// deeper changes, but would permit more caching: we could
166     /// basically defer caching until we have fully evaluated the
167     /// tree, and then cache the entire tree at once. In any case, the
168     /// performance impact here shouldn't be so horrible: every time
169     /// this is hit, we do cache at least one trait, so we only
170     /// evaluate each member of a cycle up to N times, where N is the
171     /// length of the cycle. This means the performance impact is
172     /// bounded and we shouldn't have any terrible worst-cases.
173     reached_depth: Cell<usize>,
174
175     previous: TraitObligationStackList<'prev, 'tcx>,
176
177     /// The number of parent frames plus one (thus, the topmost frame has depth 1).
178     depth: usize,
179
180     /// The depth-first number of this node in the search graph -- a
181     /// pre-order index. Basically, a freshly incremented counter.
182     dfn: usize,
183 }
184
185 struct SelectionCandidateSet<'tcx> {
186     // A list of candidates that definitely apply to the current
187     // obligation (meaning: types unify).
188     vec: Vec<SelectionCandidate<'tcx>>,
189
190     // If `true`, then there were candidates that might or might
191     // not have applied, but we couldn't tell. This occurs when some
192     // of the input types are type variables, in which case there are
193     // various "builtin" rules that might or might not trigger.
194     ambiguous: bool,
195 }
196
197 #[derive(PartialEq, Eq, Debug, Clone)]
198 struct EvaluatedCandidate<'tcx> {
199     candidate: SelectionCandidate<'tcx>,
200     evaluation: EvaluationResult,
201 }
202
203 /// When does the builtin impl for `T: Trait` apply?
204 #[derive(Debug)]
205 enum BuiltinImplConditions<'tcx> {
206     /// The impl is conditional on `T1, T2, ...: Trait`.
207     Where(ty::Binder<'tcx, Vec<Ty<'tcx>>>),
208     /// There is no built-in impl. There may be some other
209     /// candidate (a where-clause or user-defined impl).
210     None,
211     /// It is unknown whether there is an impl.
212     Ambiguous,
213 }
214
215 impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
216     pub fn new(infcx: &'cx InferCtxt<'cx, 'tcx>) -> SelectionContext<'cx, 'tcx> {
217         SelectionContext {
218             infcx,
219             freshener: infcx.freshener_keep_static(),
220             intercrate: false,
221             intercrate_ambiguity_causes: None,
222             allow_negative_impls: false,
223             query_mode: TraitQueryMode::Standard,
224         }
225     }
226
227     pub fn intercrate(infcx: &'cx InferCtxt<'cx, 'tcx>) -> SelectionContext<'cx, 'tcx> {
228         SelectionContext {
229             infcx,
230             freshener: infcx.freshener_keep_static(),
231             intercrate: true,
232             intercrate_ambiguity_causes: None,
233             allow_negative_impls: false,
234             query_mode: TraitQueryMode::Standard,
235         }
236     }
237
238     pub fn with_negative(
239         infcx: &'cx InferCtxt<'cx, 'tcx>,
240         allow_negative_impls: bool,
241     ) -> SelectionContext<'cx, 'tcx> {
242         debug!(?allow_negative_impls, "with_negative");
243         SelectionContext {
244             infcx,
245             freshener: infcx.freshener_keep_static(),
246             intercrate: false,
247             intercrate_ambiguity_causes: None,
248             allow_negative_impls,
249             query_mode: TraitQueryMode::Standard,
250         }
251     }
252
253     pub fn with_query_mode(
254         infcx: &'cx InferCtxt<'cx, 'tcx>,
255         query_mode: TraitQueryMode,
256     ) -> SelectionContext<'cx, 'tcx> {
257         debug!(?query_mode, "with_query_mode");
258         SelectionContext {
259             infcx,
260             freshener: infcx.freshener_keep_static(),
261             intercrate: false,
262             intercrate_ambiguity_causes: None,
263             allow_negative_impls: false,
264             query_mode,
265         }
266     }
267
268     /// Enables tracking of intercrate ambiguity causes. These are
269     /// used in coherence to give improved diagnostics. We don't do
270     /// this until we detect a coherence error because it can lead to
271     /// false overflow results (#47139) and because it costs
272     /// computation time.
273     pub fn enable_tracking_intercrate_ambiguity_causes(&mut self) {
274         assert!(self.intercrate);
275         assert!(self.intercrate_ambiguity_causes.is_none());
276         self.intercrate_ambiguity_causes = Some(vec![]);
277         debug!("selcx: enable_tracking_intercrate_ambiguity_causes");
278     }
279
280     /// Gets the intercrate ambiguity causes collected since tracking
281     /// was enabled and disables tracking at the same time. If
282     /// tracking is not enabled, just returns an empty vector.
283     pub fn take_intercrate_ambiguity_causes(&mut self) -> Vec<IntercrateAmbiguityCause> {
284         assert!(self.intercrate);
285         self.intercrate_ambiguity_causes.take().unwrap_or_default()
286     }
287
288     pub fn infcx(&self) -> &'cx InferCtxt<'cx, 'tcx> {
289         self.infcx
290     }
291
292     pub fn tcx(&self) -> TyCtxt<'tcx> {
293         self.infcx.tcx
294     }
295
296     pub fn is_intercrate(&self) -> bool {
297         self.intercrate
298     }
299
300     ///////////////////////////////////////////////////////////////////////////
301     // Selection
302     //
303     // The selection phase tries to identify *how* an obligation will
304     // be resolved. For example, it will identify which impl or
305     // parameter bound is to be used. The process can be inconclusive
306     // if the self type in the obligation is not fully inferred. Selection
307     // can result in an error in one of two ways:
308     //
309     // 1. If no applicable impl or parameter bound can be found.
310     // 2. If the output type parameters in the obligation do not match
311     //    those specified by the impl/bound. For example, if the obligation
312     //    is `Vec<Foo>: Iterable<Bar>`, but the impl specifies
313     //    `impl<T> Iterable<T> for Vec<T>`, than an error would result.
314
315     /// Attempts to satisfy the obligation. If successful, this will affect the surrounding
316     /// type environment by performing unification.
317     #[instrument(level = "debug", skip(self))]
318     pub fn select(
319         &mut self,
320         obligation: &TraitObligation<'tcx>,
321     ) -> SelectionResult<'tcx, Selection<'tcx>> {
322         let candidate = match self.select_from_obligation(obligation) {
323             Err(SelectionError::Overflow) => {
324                 // In standard mode, overflow must have been caught and reported
325                 // earlier.
326                 assert!(self.query_mode == TraitQueryMode::Canonical);
327                 return Err(SelectionError::Overflow);
328             }
329             Err(SelectionError::Ambiguous(_)) => {
330                 return Ok(None);
331             }
332             Err(e) => {
333                 return Err(e);
334             }
335             Ok(None) => {
336                 return Ok(None);
337             }
338             Ok(Some(candidate)) => candidate,
339         };
340
341         match self.confirm_candidate(obligation, candidate) {
342             Err(SelectionError::Overflow) => {
343                 assert!(self.query_mode == TraitQueryMode::Canonical);
344                 Err(SelectionError::Overflow)
345             }
346             Err(e) => Err(e),
347             Ok(candidate) => {
348                 debug!(?candidate, "confirmed");
349                 Ok(Some(candidate))
350             }
351         }
352     }
353
354     crate fn select_from_obligation(
355         &mut self,
356         obligation: &TraitObligation<'tcx>,
357     ) -> SelectionResult<'tcx, SelectionCandidate<'tcx>> {
358         debug_assert!(!obligation.predicate.has_escaping_bound_vars());
359
360         let pec = &ProvisionalEvaluationCache::default();
361         let stack = self.push_stack(TraitObligationStackList::empty(pec), obligation);
362
363         self.candidate_from_obligation(&stack)
364     }
365
366     ///////////////////////////////////////////////////////////////////////////
367     // EVALUATION
368     //
369     // Tests whether an obligation can be selected or whether an impl
370     // can be applied to particular types. It skips the "confirmation"
371     // step and hence completely ignores output type parameters.
372     //
373     // The result is "true" if the obligation *may* hold and "false" if
374     // we can be sure it does not.
375
376     /// Evaluates whether the obligation `obligation` can be satisfied (by any means).
377     pub fn predicate_may_hold_fatal(&mut self, obligation: &PredicateObligation<'tcx>) -> bool {
378         debug!(?obligation, "predicate_may_hold_fatal");
379
380         // This fatal query is a stopgap that should only be used in standard mode,
381         // where we do not expect overflow to be propagated.
382         assert!(self.query_mode == TraitQueryMode::Standard);
383
384         self.evaluate_root_obligation(obligation)
385             .expect("Overflow should be caught earlier in standard query mode")
386             .may_apply()
387     }
388
389     /// Evaluates whether the obligation `obligation` can be satisfied
390     /// and returns an `EvaluationResult`. This is meant for the
391     /// *initial* call.
392     pub fn evaluate_root_obligation(
393         &mut self,
394         obligation: &PredicateObligation<'tcx>,
395     ) -> Result<EvaluationResult, OverflowError> {
396         self.evaluation_probe(|this| {
397             this.evaluate_predicate_recursively(
398                 TraitObligationStackList::empty(&ProvisionalEvaluationCache::default()),
399                 obligation.clone(),
400             )
401         })
402     }
403
404     fn evaluation_probe(
405         &mut self,
406         op: impl FnOnce(&mut Self) -> Result<EvaluationResult, OverflowError>,
407     ) -> Result<EvaluationResult, OverflowError> {
408         self.infcx.probe(|snapshot| -> Result<EvaluationResult, OverflowError> {
409             let result = op(self)?;
410
411             match self.infcx.leak_check(true, snapshot) {
412                 Ok(()) => {}
413                 Err(_) => return Ok(EvaluatedToErr),
414             }
415
416             match self.infcx.region_constraints_added_in_snapshot(snapshot) {
417                 None => Ok(result),
418                 Some(_) => Ok(result.max(EvaluatedToOkModuloRegions)),
419             }
420         })
421     }
422
423     /// Evaluates the predicates in `predicates` recursively. Note that
424     /// this applies projections in the predicates, and therefore
425     /// is run within an inference probe.
426     #[instrument(skip(self, stack), level = "debug")]
427     fn evaluate_predicates_recursively<'o, I>(
428         &mut self,
429         stack: TraitObligationStackList<'o, 'tcx>,
430         predicates: I,
431     ) -> Result<EvaluationResult, OverflowError>
432     where
433         I: IntoIterator<Item = PredicateObligation<'tcx>> + std::fmt::Debug,
434     {
435         let mut result = EvaluatedToOk;
436         for obligation in predicates {
437             let eval = self.evaluate_predicate_recursively(stack, obligation.clone())?;
438             if let EvaluatedToErr = eval {
439                 // fast-path - EvaluatedToErr is the top of the lattice,
440                 // so we don't need to look on the other predicates.
441                 return Ok(EvaluatedToErr);
442             } else {
443                 result = cmp::max(result, eval);
444             }
445         }
446         Ok(result)
447     }
448
449     #[instrument(
450         level = "debug",
451         skip(self, previous_stack),
452         fields(previous_stack = ?previous_stack.head())
453     )]
454     fn evaluate_predicate_recursively<'o>(
455         &mut self,
456         previous_stack: TraitObligationStackList<'o, 'tcx>,
457         obligation: PredicateObligation<'tcx>,
458     ) -> Result<EvaluationResult, OverflowError> {
459         // `previous_stack` stores a `TraitObligation`, while `obligation` is
460         // a `PredicateObligation`. These are distinct types, so we can't
461         // use any `Option` combinator method that would force them to be
462         // the same.
463         match previous_stack.head() {
464             Some(h) => self.check_recursion_limit(&obligation, h.obligation)?,
465             None => self.check_recursion_limit(&obligation, &obligation)?,
466         }
467
468         let result = ensure_sufficient_stack(|| {
469             let bound_predicate = obligation.predicate.kind();
470             match bound_predicate.skip_binder() {
471                 ty::PredicateKind::Trait(t) => {
472                     let t = bound_predicate.rebind(t);
473                     debug_assert!(!t.has_escaping_bound_vars());
474                     let obligation = obligation.with(t);
475                     self.evaluate_trait_predicate_recursively(previous_stack, obligation)
476                 }
477
478                 ty::PredicateKind::Subtype(p) => {
479                     let p = bound_predicate.rebind(p);
480                     // Does this code ever run?
481                     match self.infcx.subtype_predicate(&obligation.cause, obligation.param_env, p) {
482                         Some(Ok(InferOk { mut obligations, .. })) => {
483                             self.add_depth(obligations.iter_mut(), obligation.recursion_depth);
484                             self.evaluate_predicates_recursively(
485                                 previous_stack,
486                                 obligations.into_iter(),
487                             )
488                         }
489                         Some(Err(_)) => Ok(EvaluatedToErr),
490                         None => Ok(EvaluatedToAmbig),
491                     }
492                 }
493
494                 ty::PredicateKind::Coerce(p) => {
495                     let p = bound_predicate.rebind(p);
496                     // Does this code ever run?
497                     match self.infcx.coerce_predicate(&obligation.cause, obligation.param_env, p) {
498                         Some(Ok(InferOk { mut obligations, .. })) => {
499                             self.add_depth(obligations.iter_mut(), obligation.recursion_depth);
500                             self.evaluate_predicates_recursively(
501                                 previous_stack,
502                                 obligations.into_iter(),
503                             )
504                         }
505                         Some(Err(_)) => Ok(EvaluatedToErr),
506                         None => Ok(EvaluatedToAmbig),
507                     }
508                 }
509
510                 ty::PredicateKind::WellFormed(arg) => match wf::obligations(
511                     self.infcx,
512                     obligation.param_env,
513                     obligation.cause.body_id,
514                     obligation.recursion_depth + 1,
515                     arg,
516                     obligation.cause.span,
517                 ) {
518                     Some(mut obligations) => {
519                         self.add_depth(obligations.iter_mut(), obligation.recursion_depth);
520                         self.evaluate_predicates_recursively(previous_stack, obligations)
521                     }
522                     None => Ok(EvaluatedToAmbig),
523                 },
524
525                 ty::PredicateKind::TypeOutlives(pred) => {
526                     // A global type with no late-bound regions can only
527                     // contain the "'static" lifetime (any other lifetime
528                     // would either be late-bound or local), so it is guaranteed
529                     // to outlive any other lifetime
530                     if pred.0.is_global() && !pred.0.has_late_bound_regions() {
531                         Ok(EvaluatedToOk)
532                     } else {
533                         Ok(EvaluatedToOkModuloRegions)
534                     }
535                 }
536
537                 ty::PredicateKind::RegionOutlives(..) => {
538                     // We do not consider region relationships when evaluating trait matches.
539                     Ok(EvaluatedToOkModuloRegions)
540                 }
541
542                 ty::PredicateKind::ObjectSafe(trait_def_id) => {
543                     if self.tcx().is_object_safe(trait_def_id) {
544                         Ok(EvaluatedToOk)
545                     } else {
546                         Ok(EvaluatedToErr)
547                     }
548                 }
549
550                 ty::PredicateKind::Projection(data) => {
551                     let data = bound_predicate.rebind(data);
552                     let project_obligation = obligation.with(data);
553                     match project::poly_project_and_unify_type(self, &project_obligation) {
554                         Ok(Ok(Some(mut subobligations))) => {
555                             'compute_res: {
556                                 // If we've previously marked this projection as 'complete', then
557                                 // use the final cached result (either `EvaluatedToOk` or
558                                 // `EvaluatedToOkModuloRegions`), and skip re-evaluating the
559                                 // sub-obligations.
560                                 if let Some(key) =
561                                     ProjectionCacheKey::from_poly_projection_predicate(self, data)
562                                 {
563                                     if let Some(cached_res) = self
564                                         .infcx
565                                         .inner
566                                         .borrow_mut()
567                                         .projection_cache()
568                                         .is_complete(key)
569                                     {
570                                         break 'compute_res Ok(cached_res);
571                                     }
572                                 }
573
574                                 self.add_depth(
575                                     subobligations.iter_mut(),
576                                     obligation.recursion_depth,
577                                 );
578                                 let res = self.evaluate_predicates_recursively(
579                                     previous_stack,
580                                     subobligations,
581                                 );
582                                 if let Ok(eval_rslt) = res
583                                     && (eval_rslt == EvaluatedToOk || eval_rslt == EvaluatedToOkModuloRegions)
584                                     && let Some(key) =
585                                         ProjectionCacheKey::from_poly_projection_predicate(
586                                             self, data,
587                                         )
588                                 {
589                                     // If the result is something that we can cache, then mark this
590                                     // entry as 'complete'. This will allow us to skip evaluating the
591                                     // suboligations at all the next time we evaluate the projection
592                                     // predicate.
593                                     self.infcx
594                                         .inner
595                                         .borrow_mut()
596                                         .projection_cache()
597                                         .complete(key, eval_rslt);
598                                 }
599                                 res
600                             }
601                         }
602                         Ok(Ok(None)) => Ok(EvaluatedToAmbig),
603                         Ok(Err(project::InProgress)) => Ok(EvaluatedToRecur),
604                         Err(_) => Ok(EvaluatedToErr),
605                     }
606                 }
607
608                 ty::PredicateKind::ClosureKind(_, closure_substs, kind) => {
609                     match self.infcx.closure_kind(closure_substs) {
610                         Some(closure_kind) => {
611                             if closure_kind.extends(kind) {
612                                 Ok(EvaluatedToOk)
613                             } else {
614                                 Ok(EvaluatedToErr)
615                             }
616                         }
617                         None => Ok(EvaluatedToAmbig),
618                     }
619                 }
620
621                 ty::PredicateKind::ConstEvaluatable(uv) => {
622                     match const_evaluatable::is_const_evaluatable(
623                         self.infcx,
624                         uv,
625                         obligation.param_env,
626                         obligation.cause.span,
627                     ) {
628                         Ok(()) => Ok(EvaluatedToOk),
629                         Err(NotConstEvaluatable::MentionsInfer) => Ok(EvaluatedToAmbig),
630                         Err(NotConstEvaluatable::MentionsParam) => Ok(EvaluatedToErr),
631                         Err(_) => Ok(EvaluatedToErr),
632                     }
633                 }
634
635                 ty::PredicateKind::ConstEquate(c1, c2) => {
636                     debug!(?c1, ?c2, "evaluate_predicate_recursively: equating consts");
637
638                     if self.tcx().features().generic_const_exprs {
639                         // FIXME: we probably should only try to unify abstract constants
640                         // if the constants depend on generic parameters.
641                         //
642                         // Let's just see where this breaks :shrug:
643                         if let (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) =
644                             (c1.val(), c2.val())
645                         {
646                             if self.infcx.try_unify_abstract_consts(a.shrink(), b.shrink()) {
647                                 return Ok(EvaluatedToOk);
648                             }
649                         }
650                     }
651
652                     let evaluate = |c: ty::Const<'tcx>| {
653                         if let ty::ConstKind::Unevaluated(unevaluated) = c.val() {
654                             self.infcx
655                                 .const_eval_resolve(
656                                     obligation.param_env,
657                                     unevaluated,
658                                     Some(obligation.cause.span),
659                                 )
660                                 .map(|val| ty::Const::from_value(self.tcx(), val, c.ty()))
661                         } else {
662                             Ok(c)
663                         }
664                     };
665
666                     match (evaluate(c1), evaluate(c2)) {
667                         (Ok(c1), Ok(c2)) => {
668                             match self
669                                 .infcx()
670                                 .at(&obligation.cause, obligation.param_env)
671                                 .eq(c1, c2)
672                             {
673                                 Ok(_) => Ok(EvaluatedToOk),
674                                 Err(_) => Ok(EvaluatedToErr),
675                             }
676                         }
677                         (Err(ErrorHandled::Reported(_)), _)
678                         | (_, Err(ErrorHandled::Reported(_))) => Ok(EvaluatedToErr),
679                         (Err(ErrorHandled::Linted), _) | (_, Err(ErrorHandled::Linted)) => {
680                             span_bug!(
681                                 obligation.cause.span(self.tcx()),
682                                 "ConstEquate: const_eval_resolve returned an unexpected error"
683                             )
684                         }
685                         (Err(ErrorHandled::TooGeneric), _) | (_, Err(ErrorHandled::TooGeneric)) => {
686                             if c1.has_infer_types_or_consts() || c2.has_infer_types_or_consts() {
687                                 Ok(EvaluatedToAmbig)
688                             } else {
689                                 // Two different constants using generic parameters ~> error.
690                                 Ok(EvaluatedToErr)
691                             }
692                         }
693                     }
694                 }
695                 ty::PredicateKind::TypeWellFormedFromEnv(..) => {
696                     bug!("TypeWellFormedFromEnv is only used for chalk")
697                 }
698             }
699         });
700
701         debug!("finished: {:?} from {:?}", result, obligation);
702
703         result
704     }
705
706     #[instrument(skip(self, previous_stack), level = "debug")]
707     fn evaluate_trait_predicate_recursively<'o>(
708         &mut self,
709         previous_stack: TraitObligationStackList<'o, 'tcx>,
710         mut obligation: TraitObligation<'tcx>,
711     ) -> Result<EvaluationResult, OverflowError> {
712         if !self.intercrate
713             && obligation.is_global()
714             && obligation.param_env.caller_bounds().iter().all(|bound| bound.needs_subst())
715         {
716             // If a param env has no global bounds, global obligations do not
717             // depend on its particular value in order to work, so we can clear
718             // out the param env and get better caching.
719             debug!("in global");
720             obligation.param_env = obligation.param_env.without_caller_bounds();
721         }
722
723         let stack = self.push_stack(previous_stack, &obligation);
724         let mut fresh_trait_pred = stack.fresh_trait_pred;
725         let mut param_env = obligation.param_env;
726
727         fresh_trait_pred = fresh_trait_pred.map_bound(|mut pred| {
728             pred.remap_constness(self.tcx(), &mut param_env);
729             pred
730         });
731
732         debug!(?fresh_trait_pred);
733
734         if let Some(result) = self.check_evaluation_cache(param_env, fresh_trait_pred) {
735             debug!(?result, "CACHE HIT");
736             return Ok(result);
737         }
738
739         if let Some(result) = stack.cache().get_provisional(fresh_trait_pred) {
740             debug!(?result, "PROVISIONAL CACHE HIT");
741             stack.update_reached_depth(result.reached_depth);
742             return Ok(result.result);
743         }
744
745         // Check if this is a match for something already on the
746         // stack. If so, we don't want to insert the result into the
747         // main cache (it is cycle dependent) nor the provisional
748         // cache (which is meant for things that have completed but
749         // for a "backedge" -- this result *is* the backedge).
750         if let Some(cycle_result) = self.check_evaluation_cycle(&stack) {
751             return Ok(cycle_result);
752         }
753
754         let (result, dep_node) = self.in_task(|this| this.evaluate_stack(&stack));
755         let result = result?;
756
757         if !result.must_apply_modulo_regions() {
758             stack.cache().on_failure(stack.dfn);
759         }
760
761         let reached_depth = stack.reached_depth.get();
762         if reached_depth >= stack.depth {
763             debug!(?result, "CACHE MISS");
764             self.insert_evaluation_cache(param_env, fresh_trait_pred, dep_node, result);
765
766             stack.cache().on_completion(
767                 stack.dfn,
768                 |fresh_trait_pred, provisional_result, provisional_dep_node| {
769                     // Create a new `DepNode` that has dependencies on:
770                     // * The `DepNode` for the original evaluation that resulted in a provisional cache
771                     // entry being crated
772                     // * The `DepNode` for the *current* evaluation, which resulted in us completing
773                     // provisional caches entries and inserting them into the evaluation cache
774                     //
775                     // This ensures that when a query reads this entry from the evaluation cache,
776                     // it will end up (transitively) dependening on all of the incr-comp dependencies
777                     // created during the evaluation of this trait. For example, evaluating a trait
778                     // will usually require us to invoke `type_of(field_def_id)` to determine the
779                     // constituent types, and we want any queries reading from this evaluation
780                     // cache entry to end up with a transitive `type_of(field_def_id`)` dependency.
781                     //
782                     // By using `in_task`, we're also creating an edge from the *current* query
783                     // to the newly-created `combined_dep_node`. This is probably redundant,
784                     // but it's better to add too many dep graph edges than to add too few
785                     // dep graph edges.
786                     let ((), combined_dep_node) = self.in_task(|this| {
787                         this.tcx().dep_graph.read_index(provisional_dep_node);
788                         this.tcx().dep_graph.read_index(dep_node);
789                     });
790                     self.insert_evaluation_cache(
791                         param_env,
792                         fresh_trait_pred,
793                         combined_dep_node,
794                         provisional_result.max(result),
795                     );
796                 },
797             );
798         } else {
799             debug!(?result, "PROVISIONAL");
800             debug!(
801                 "caching provisionally because {:?} \
802                  is a cycle participant (at depth {}, reached depth {})",
803                 fresh_trait_pred, stack.depth, reached_depth,
804             );
805
806             stack.cache().insert_provisional(
807                 stack.dfn,
808                 reached_depth,
809                 fresh_trait_pred,
810                 result,
811                 dep_node,
812             );
813         }
814
815         Ok(result)
816     }
817
818     /// If there is any previous entry on the stack that precisely
819     /// matches this obligation, then we can assume that the
820     /// obligation is satisfied for now (still all other conditions
821     /// must be met of course). One obvious case this comes up is
822     /// marker traits like `Send`. Think of a linked list:
823     ///
824     ///    struct List<T> { data: T, next: Option<Box<List<T>>> }
825     ///
826     /// `Box<List<T>>` will be `Send` if `T` is `Send` and
827     /// `Option<Box<List<T>>>` is `Send`, and in turn
828     /// `Option<Box<List<T>>>` is `Send` if `Box<List<T>>` is
829     /// `Send`.
830     ///
831     /// Note that we do this comparison using the `fresh_trait_ref`
832     /// fields. Because these have all been freshened using
833     /// `self.freshener`, we can be sure that (a) this will not
834     /// affect the inferencer state and (b) that if we see two
835     /// fresh regions with the same index, they refer to the same
836     /// unbound type variable.
837     fn check_evaluation_cycle(
838         &mut self,
839         stack: &TraitObligationStack<'_, 'tcx>,
840     ) -> Option<EvaluationResult> {
841         if let Some(cycle_depth) = stack
842             .iter()
843             .skip(1) // Skip top-most frame.
844             .find(|prev| {
845                 stack.obligation.param_env == prev.obligation.param_env
846                     && stack.fresh_trait_pred == prev.fresh_trait_pred
847             })
848             .map(|stack| stack.depth)
849         {
850             debug!("evaluate_stack --> recursive at depth {}", cycle_depth);
851
852             // If we have a stack like `A B C D E A`, where the top of
853             // the stack is the final `A`, then this will iterate over
854             // `A, E, D, C, B` -- i.e., all the participants apart
855             // from the cycle head. We mark them as participating in a
856             // cycle. This suppresses caching for those nodes. See
857             // `in_cycle` field for more details.
858             stack.update_reached_depth(cycle_depth);
859
860             // Subtle: when checking for a coinductive cycle, we do
861             // not compare using the "freshened trait refs" (which
862             // have erased regions) but rather the fully explicit
863             // trait refs. This is important because it's only a cycle
864             // if the regions match exactly.
865             let cycle = stack.iter().skip(1).take_while(|s| s.depth >= cycle_depth);
866             let tcx = self.tcx();
867             let cycle = cycle.map(|stack| stack.obligation.predicate.to_predicate(tcx));
868             if self.coinductive_match(cycle) {
869                 debug!("evaluate_stack --> recursive, coinductive");
870                 Some(EvaluatedToOk)
871             } else {
872                 debug!("evaluate_stack --> recursive, inductive");
873                 Some(EvaluatedToRecur)
874             }
875         } else {
876             None
877         }
878     }
879
880     fn evaluate_stack<'o>(
881         &mut self,
882         stack: &TraitObligationStack<'o, 'tcx>,
883     ) -> Result<EvaluationResult, OverflowError> {
884         // In intercrate mode, whenever any of the generics are unbound,
885         // there can always be an impl. Even if there are no impls in
886         // this crate, perhaps the type would be unified with
887         // something from another crate that does provide an impl.
888         //
889         // In intra mode, we must still be conservative. The reason is
890         // that we want to avoid cycles. Imagine an impl like:
891         //
892         //     impl<T:Eq> Eq for Vec<T>
893         //
894         // and a trait reference like `$0 : Eq` where `$0` is an
895         // unbound variable. When we evaluate this trait-reference, we
896         // will unify `$0` with `Vec<$1>` (for some fresh variable
897         // `$1`), on the condition that `$1 : Eq`. We will then wind
898         // up with many candidates (since that are other `Eq` impls
899         // that apply) and try to winnow things down. This results in
900         // a recursive evaluation that `$1 : Eq` -- as you can
901         // imagine, this is just where we started. To avoid that, we
902         // check for unbound variables and return an ambiguous (hence possible)
903         // match if we've seen this trait before.
904         //
905         // This suffices to allow chains like `FnMut` implemented in
906         // terms of `Fn` etc, but we could probably make this more
907         // precise still.
908         let unbound_input_types =
909             stack.fresh_trait_pred.skip_binder().trait_ref.substs.types().any(|ty| ty.is_fresh());
910
911         if stack.obligation.polarity() != ty::ImplPolarity::Negative {
912             // This check was an imperfect workaround for a bug in the old
913             // intercrate mode; it should be removed when that goes away.
914             if unbound_input_types && self.intercrate {
915                 debug!("evaluate_stack --> unbound argument, intercrate -->  ambiguous",);
916                 // Heuristics: show the diagnostics when there are no candidates in crate.
917                 if self.intercrate_ambiguity_causes.is_some() {
918                     debug!("evaluate_stack: intercrate_ambiguity_causes is some");
919                     if let Ok(candidate_set) = self.assemble_candidates(stack) {
920                         if !candidate_set.ambiguous && candidate_set.vec.is_empty() {
921                             let trait_ref = stack.obligation.predicate.skip_binder().trait_ref;
922                             let self_ty = trait_ref.self_ty();
923                             let cause = with_no_trimmed_paths!({
924                                 IntercrateAmbiguityCause::DownstreamCrate {
925                                     trait_desc: trait_ref.print_only_trait_path().to_string(),
926                                     self_desc: if self_ty.has_concrete_skeleton() {
927                                         Some(self_ty.to_string())
928                                     } else {
929                                         None
930                                     },
931                                 }
932                             });
933
934                             debug!(?cause, "evaluate_stack: pushing cause");
935                             self.intercrate_ambiguity_causes.as_mut().unwrap().push(cause);
936                         }
937                     }
938                 }
939                 return Ok(EvaluatedToAmbig);
940             }
941         }
942
943         if unbound_input_types
944             && stack.iter().skip(1).any(|prev| {
945                 stack.obligation.param_env == prev.obligation.param_env
946                     && self.match_fresh_trait_refs(
947                         stack.fresh_trait_pred,
948                         prev.fresh_trait_pred,
949                         prev.obligation.param_env,
950                     )
951             })
952         {
953             debug!("evaluate_stack --> unbound argument, recursive --> giving up",);
954             return Ok(EvaluatedToUnknown);
955         }
956
957         match self.candidate_from_obligation(stack) {
958             Ok(Some(c)) => self.evaluate_candidate(stack, &c),
959             Err(SelectionError::Ambiguous(_)) => Ok(EvaluatedToAmbig),
960             Ok(None) => Ok(EvaluatedToAmbig),
961             Err(Overflow) => Err(OverflowError::Canonical),
962             Err(ErrorReporting) => Err(OverflowError::ErrorReporting),
963             Err(..) => Ok(EvaluatedToErr),
964         }
965     }
966
967     /// For defaulted traits, we use a co-inductive strategy to solve, so
968     /// that recursion is ok. This routine returns `true` if the top of the
969     /// stack (`cycle[0]`):
970     ///
971     /// - is a defaulted trait,
972     /// - it also appears in the backtrace at some position `X`,
973     /// - all the predicates at positions `X..` between `X` and the top are
974     ///   also defaulted traits.
975     pub fn coinductive_match<I>(&mut self, mut cycle: I) -> bool
976     where
977         I: Iterator<Item = ty::Predicate<'tcx>>,
978     {
979         cycle.all(|predicate| self.coinductive_predicate(predicate))
980     }
981
982     fn coinductive_predicate(&self, predicate: ty::Predicate<'tcx>) -> bool {
983         let result = match predicate.kind().skip_binder() {
984             ty::PredicateKind::Trait(ref data) => self.tcx().trait_is_auto(data.def_id()),
985             _ => false,
986         };
987         debug!(?predicate, ?result, "coinductive_predicate");
988         result
989     }
990
991     /// Further evaluates `candidate` to decide whether all type parameters match and whether nested
992     /// obligations are met. Returns whether `candidate` remains viable after this further
993     /// scrutiny.
994     #[instrument(
995         level = "debug",
996         skip(self, stack),
997         fields(depth = stack.obligation.recursion_depth)
998     )]
999     fn evaluate_candidate<'o>(
1000         &mut self,
1001         stack: &TraitObligationStack<'o, 'tcx>,
1002         candidate: &SelectionCandidate<'tcx>,
1003     ) -> Result<EvaluationResult, OverflowError> {
1004         let mut result = self.evaluation_probe(|this| {
1005             let candidate = (*candidate).clone();
1006             match this.confirm_candidate(stack.obligation, candidate) {
1007                 Ok(selection) => {
1008                     debug!(?selection);
1009                     this.evaluate_predicates_recursively(
1010                         stack.list(),
1011                         selection.nested_obligations().into_iter(),
1012                     )
1013                 }
1014                 Err(..) => Ok(EvaluatedToErr),
1015             }
1016         })?;
1017
1018         // If we erased any lifetimes, then we want to use
1019         // `EvaluatedToOkModuloRegions` instead of `EvaluatedToOk`
1020         // as your final result. The result will be cached using
1021         // the freshened trait predicate as a key, so we need
1022         // our result to be correct by *any* choice of original lifetimes,
1023         // not just the lifetime choice for this particular (non-erased)
1024         // predicate.
1025         // See issue #80691
1026         if stack.fresh_trait_pred.has_erased_regions() {
1027             result = result.max(EvaluatedToOkModuloRegions);
1028         }
1029
1030         debug!(?result);
1031         Ok(result)
1032     }
1033
1034     fn check_evaluation_cache(
1035         &self,
1036         param_env: ty::ParamEnv<'tcx>,
1037         trait_pred: ty::PolyTraitPredicate<'tcx>,
1038     ) -> Option<EvaluationResult> {
1039         // Neither the global nor local cache is aware of intercrate
1040         // mode, so don't do any caching. In particular, we might
1041         // re-use the same `InferCtxt` with both an intercrate
1042         // and non-intercrate `SelectionContext`
1043         if self.intercrate {
1044             return None;
1045         }
1046
1047         let tcx = self.tcx();
1048         if self.can_use_global_caches(param_env) {
1049             if let Some(res) = tcx.evaluation_cache.get(&param_env.and(trait_pred), tcx) {
1050                 return Some(res);
1051             }
1052         }
1053         self.infcx.evaluation_cache.get(&param_env.and(trait_pred), tcx)
1054     }
1055
1056     fn insert_evaluation_cache(
1057         &mut self,
1058         param_env: ty::ParamEnv<'tcx>,
1059         trait_pred: ty::PolyTraitPredicate<'tcx>,
1060         dep_node: DepNodeIndex,
1061         result: EvaluationResult,
1062     ) {
1063         // Avoid caching results that depend on more than just the trait-ref
1064         // - the stack can create recursion.
1065         if result.is_stack_dependent() {
1066             return;
1067         }
1068
1069         // Neither the global nor local cache is aware of intercrate
1070         // mode, so don't do any caching. In particular, we might
1071         // re-use the same `InferCtxt` with both an intercrate
1072         // and non-intercrate `SelectionContext`
1073         if self.intercrate {
1074             return;
1075         }
1076
1077         if self.can_use_global_caches(param_env) {
1078             if !trait_pred.needs_infer() {
1079                 debug!(?trait_pred, ?result, "insert_evaluation_cache global");
1080                 // This may overwrite the cache with the same value
1081                 // FIXME: Due to #50507 this overwrites the different values
1082                 // This should be changed to use HashMapExt::insert_same
1083                 // when that is fixed
1084                 self.tcx().evaluation_cache.insert(param_env.and(trait_pred), dep_node, result);
1085                 return;
1086             }
1087         }
1088
1089         debug!(?trait_pred, ?result, "insert_evaluation_cache");
1090         self.infcx.evaluation_cache.insert(param_env.and(trait_pred), dep_node, result);
1091     }
1092
1093     /// For various reasons, it's possible for a subobligation
1094     /// to have a *lower* recursion_depth than the obligation used to create it.
1095     /// Projection sub-obligations may be returned from the projection cache,
1096     /// which results in obligations with an 'old' `recursion_depth`.
1097     /// Additionally, methods like `InferCtxt.subtype_predicate` produce
1098     /// subobligations without taking in a 'parent' depth, causing the
1099     /// generated subobligations to have a `recursion_depth` of `0`.
1100     ///
1101     /// To ensure that obligation_depth never decreases, we force all subobligations
1102     /// to have at least the depth of the original obligation.
1103     fn add_depth<T: 'cx, I: Iterator<Item = &'cx mut Obligation<'tcx, T>>>(
1104         &self,
1105         it: I,
1106         min_depth: usize,
1107     ) {
1108         it.for_each(|o| o.recursion_depth = cmp::max(min_depth, o.recursion_depth) + 1);
1109     }
1110
1111     fn check_recursion_depth<T: Display + TypeFoldable<'tcx>>(
1112         &self,
1113         depth: usize,
1114         error_obligation: &Obligation<'tcx, T>,
1115     ) -> Result<(), OverflowError> {
1116         if !self.infcx.tcx.recursion_limit().value_within_limit(depth) {
1117             match self.query_mode {
1118                 TraitQueryMode::Standard => {
1119                     if self.infcx.is_tainted_by_errors() {
1120                         return Err(OverflowError::ErrorReporting);
1121                     }
1122                     self.infcx.report_overflow_error(error_obligation, true);
1123                 }
1124                 TraitQueryMode::Canonical => {
1125                     return Err(OverflowError::Canonical);
1126                 }
1127             }
1128         }
1129         Ok(())
1130     }
1131
1132     /// Checks that the recursion limit has not been exceeded.
1133     ///
1134     /// The weird return type of this function allows it to be used with the `try` (`?`)
1135     /// operator within certain functions.
1136     #[inline(always)]
1137     fn check_recursion_limit<T: Display + TypeFoldable<'tcx>, V: Display + TypeFoldable<'tcx>>(
1138         &self,
1139         obligation: &Obligation<'tcx, T>,
1140         error_obligation: &Obligation<'tcx, V>,
1141     ) -> Result<(), OverflowError> {
1142         self.check_recursion_depth(obligation.recursion_depth, error_obligation)
1143     }
1144
1145     fn in_task<OP, R>(&mut self, op: OP) -> (R, DepNodeIndex)
1146     where
1147         OP: FnOnce(&mut Self) -> R,
1148     {
1149         let (result, dep_node) =
1150             self.tcx().dep_graph.with_anon_task(self.tcx(), DepKind::TraitSelect, || op(self));
1151         self.tcx().dep_graph.read_index(dep_node);
1152         (result, dep_node)
1153     }
1154
1155     /// filter_impls filters constant trait obligations and candidates that have a positive impl
1156     /// for a negative goal and a negative impl for a positive goal
1157     #[instrument(level = "debug", skip(self))]
1158     fn filter_impls(
1159         &mut self,
1160         candidates: Vec<SelectionCandidate<'tcx>>,
1161         obligation: &TraitObligation<'tcx>,
1162     ) -> Vec<SelectionCandidate<'tcx>> {
1163         let tcx = self.tcx();
1164         let mut result = Vec::with_capacity(candidates.len());
1165
1166         for candidate in candidates {
1167             // Respect const trait obligations
1168             if obligation.is_const() {
1169                 match candidate {
1170                     // const impl
1171                     ImplCandidate(def_id)
1172                         if tcx.impl_constness(def_id) == hir::Constness::Const => {}
1173                     // const param
1174                     ParamCandidate(trait_pred) if trait_pred.is_const_if_const() => {}
1175                     // auto trait impl
1176                     AutoImplCandidate(..) => {}
1177                     // generator, this will raise error in other places
1178                     // or ignore error with const_async_blocks feature
1179                     GeneratorCandidate => {}
1180                     // FnDef where the function is const
1181                     FnPointerCandidate { is_const: true } => {}
1182                     ConstDropCandidate(_) => {}
1183                     _ => {
1184                         // reject all other types of candidates
1185                         continue;
1186                     }
1187                 }
1188             }
1189
1190             if let ImplCandidate(def_id) = candidate {
1191                 if ty::ImplPolarity::Reservation == tcx.impl_polarity(def_id)
1192                     || obligation.polarity() == tcx.impl_polarity(def_id)
1193                     || self.allow_negative_impls
1194                 {
1195                     result.push(candidate);
1196                 }
1197             } else {
1198                 result.push(candidate);
1199             }
1200         }
1201
1202         result
1203     }
1204
1205     /// filter_reservation_impls filter reservation impl for any goal as ambiguous
1206     #[instrument(level = "debug", skip(self))]
1207     fn filter_reservation_impls(
1208         &mut self,
1209         candidate: SelectionCandidate<'tcx>,
1210         obligation: &TraitObligation<'tcx>,
1211     ) -> SelectionResult<'tcx, SelectionCandidate<'tcx>> {
1212         let tcx = self.tcx();
1213         // Treat reservation impls as ambiguity.
1214         if let ImplCandidate(def_id) = candidate {
1215             if let ty::ImplPolarity::Reservation = tcx.impl_polarity(def_id) {
1216                 if let Some(intercrate_ambiguity_clauses) = &mut self.intercrate_ambiguity_causes {
1217                     let attrs = tcx.get_attrs(def_id);
1218                     let attr = tcx.sess.find_by_name(&attrs, sym::rustc_reservation_impl);
1219                     let value = attr.and_then(|a| a.value_str());
1220                     if let Some(value) = value {
1221                         debug!(
1222                             "filter_reservation_impls: \
1223                                  reservation impl ambiguity on {:?}",
1224                             def_id
1225                         );
1226                         intercrate_ambiguity_clauses.push(
1227                             IntercrateAmbiguityCause::ReservationImpl {
1228                                 message: value.to_string(),
1229                             },
1230                         );
1231                     }
1232                 }
1233                 return Ok(None);
1234             }
1235         }
1236         Ok(Some(candidate))
1237     }
1238
1239     fn is_knowable<'o>(&mut self, stack: &TraitObligationStack<'o, 'tcx>) -> Option<Conflict> {
1240         debug!("is_knowable(intercrate={:?})", self.intercrate);
1241
1242         if !self.intercrate || stack.obligation.polarity() == ty::ImplPolarity::Negative {
1243             return None;
1244         }
1245
1246         let obligation = &stack.obligation;
1247         let predicate = self.infcx().resolve_vars_if_possible(obligation.predicate);
1248
1249         // Okay to skip binder because of the nature of the
1250         // trait-ref-is-knowable check, which does not care about
1251         // bound regions.
1252         let trait_ref = predicate.skip_binder().trait_ref;
1253
1254         coherence::trait_ref_is_knowable(self.tcx(), trait_ref)
1255     }
1256
1257     /// Returns `true` if the global caches can be used.
1258     fn can_use_global_caches(&self, param_env: ty::ParamEnv<'tcx>) -> bool {
1259         // If there are any inference variables in the `ParamEnv`, then we
1260         // always use a cache local to this particular scope. Otherwise, we
1261         // switch to a global cache.
1262         if param_env.needs_infer() {
1263             return false;
1264         }
1265
1266         // Avoid using the master cache during coherence and just rely
1267         // on the local cache. This effectively disables caching
1268         // during coherence. It is really just a simplification to
1269         // avoid us having to fear that coherence results "pollute"
1270         // the master cache. Since coherence executes pretty quickly,
1271         // it's not worth going to more trouble to increase the
1272         // hit-rate, I don't think.
1273         if self.intercrate || self.allow_negative_impls {
1274             return false;
1275         }
1276
1277         // Otherwise, we can use the global cache.
1278         true
1279     }
1280
1281     fn check_candidate_cache(
1282         &mut self,
1283         mut param_env: ty::ParamEnv<'tcx>,
1284         cache_fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
1285     ) -> Option<SelectionResult<'tcx, SelectionCandidate<'tcx>>> {
1286         // Neither the global nor local cache is aware of intercrate
1287         // mode, so don't do any caching. In particular, we might
1288         // re-use the same `InferCtxt` with both an intercrate
1289         // and non-intercrate `SelectionContext`
1290         if self.intercrate || self.allow_negative_impls {
1291             return None;
1292         }
1293         let tcx = self.tcx();
1294         let mut pred = cache_fresh_trait_pred.skip_binder();
1295         pred.remap_constness(tcx, &mut param_env);
1296
1297         if self.can_use_global_caches(param_env) {
1298             if let Some(res) = tcx.selection_cache.get(&param_env.and(pred), tcx) {
1299                 return Some(res);
1300             }
1301         }
1302         self.infcx.selection_cache.get(&param_env.and(pred), tcx)
1303     }
1304
1305     /// Determines whether can we safely cache the result
1306     /// of selecting an obligation. This is almost always `true`,
1307     /// except when dealing with certain `ParamCandidate`s.
1308     ///
1309     /// Ordinarily, a `ParamCandidate` will contain no inference variables,
1310     /// since it was usually produced directly from a `DefId`. However,
1311     /// certain cases (currently only librustdoc's blanket impl finder),
1312     /// a `ParamEnv` may be explicitly constructed with inference types.
1313     /// When this is the case, we do *not* want to cache the resulting selection
1314     /// candidate. This is due to the fact that it might not always be possible
1315     /// to equate the obligation's trait ref and the candidate's trait ref,
1316     /// if more constraints end up getting added to an inference variable.
1317     ///
1318     /// Because of this, we always want to re-run the full selection
1319     /// process for our obligation the next time we see it, since
1320     /// we might end up picking a different `SelectionCandidate` (or none at all).
1321     fn can_cache_candidate(
1322         &self,
1323         result: &SelectionResult<'tcx, SelectionCandidate<'tcx>>,
1324     ) -> bool {
1325         // Neither the global nor local cache is aware of intercrate
1326         // mode, so don't do any caching. In particular, we might
1327         // re-use the same `InferCtxt` with both an intercrate
1328         // and non-intercrate `SelectionContext`
1329         if self.intercrate {
1330             return false;
1331         }
1332         match result {
1333             Ok(Some(SelectionCandidate::ParamCandidate(trait_ref))) => !trait_ref.needs_infer(),
1334             _ => true,
1335         }
1336     }
1337
1338     fn insert_candidate_cache(
1339         &mut self,
1340         mut param_env: ty::ParamEnv<'tcx>,
1341         cache_fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
1342         dep_node: DepNodeIndex,
1343         candidate: SelectionResult<'tcx, SelectionCandidate<'tcx>>,
1344     ) {
1345         let tcx = self.tcx();
1346         let mut pred = cache_fresh_trait_pred.skip_binder();
1347
1348         pred.remap_constness(tcx, &mut param_env);
1349
1350         if !self.can_cache_candidate(&candidate) {
1351             debug!(?pred, ?candidate, "insert_candidate_cache - candidate is not cacheable");
1352             return;
1353         }
1354
1355         if self.can_use_global_caches(param_env) {
1356             if let Err(Overflow) = candidate {
1357                 // Don't cache overflow globally; we only produce this in certain modes.
1358             } else if !pred.needs_infer() {
1359                 if !candidate.needs_infer() {
1360                     debug!(?pred, ?candidate, "insert_candidate_cache global");
1361                     // This may overwrite the cache with the same value.
1362                     tcx.selection_cache.insert(param_env.and(pred), dep_node, candidate);
1363                     return;
1364                 }
1365             }
1366         }
1367
1368         debug!(?pred, ?candidate, "insert_candidate_cache local");
1369         self.infcx.selection_cache.insert(param_env.and(pred), dep_node, candidate);
1370     }
1371
1372     /// Matches a predicate against the bounds of its self type.
1373     ///
1374     /// Given an obligation like `<T as Foo>::Bar: Baz` where the self type is
1375     /// a projection, look at the bounds of `T::Bar`, see if we can find a
1376     /// `Baz` bound. We return indexes into the list returned by
1377     /// `tcx.item_bounds` for any applicable bounds.
1378     fn match_projection_obligation_against_definition_bounds(
1379         &mut self,
1380         obligation: &TraitObligation<'tcx>,
1381     ) -> smallvec::SmallVec<[usize; 2]> {
1382         let poly_trait_predicate = self.infcx().resolve_vars_if_possible(obligation.predicate);
1383         let placeholder_trait_predicate =
1384             self.infcx().replace_bound_vars_with_placeholders(poly_trait_predicate);
1385         debug!(
1386             ?placeholder_trait_predicate,
1387             "match_projection_obligation_against_definition_bounds"
1388         );
1389
1390         let tcx = self.infcx.tcx;
1391         let (def_id, substs) = match *placeholder_trait_predicate.trait_ref.self_ty().kind() {
1392             ty::Projection(ref data) => (data.item_def_id, data.substs),
1393             ty::Opaque(def_id, substs) => (def_id, substs),
1394             _ => {
1395                 span_bug!(
1396                     obligation.cause.span,
1397                     "match_projection_obligation_against_definition_bounds() called \
1398                      but self-ty is not a projection: {:?}",
1399                     placeholder_trait_predicate.trait_ref.self_ty()
1400                 );
1401             }
1402         };
1403         let bounds = tcx.item_bounds(def_id).subst(tcx, substs);
1404
1405         // The bounds returned by `item_bounds` may contain duplicates after
1406         // normalization, so try to deduplicate when possible to avoid
1407         // unnecessary ambiguity.
1408         let mut distinct_normalized_bounds = FxHashSet::default();
1409
1410         let matching_bounds = bounds
1411             .iter()
1412             .enumerate()
1413             .filter_map(|(idx, bound)| {
1414                 let bound_predicate = bound.kind();
1415                 if let ty::PredicateKind::Trait(pred) = bound_predicate.skip_binder() {
1416                     let bound = bound_predicate.rebind(pred.trait_ref);
1417                     if self.infcx.probe(|_| {
1418                         match self.match_normalize_trait_ref(
1419                             obligation,
1420                             bound,
1421                             placeholder_trait_predicate.trait_ref,
1422                         ) {
1423                             Ok(None) => true,
1424                             Ok(Some(normalized_trait))
1425                                 if distinct_normalized_bounds.insert(normalized_trait) =>
1426                             {
1427                                 true
1428                             }
1429                             _ => false,
1430                         }
1431                     }) {
1432                         return Some(idx);
1433                     }
1434                 }
1435                 None
1436             })
1437             .collect();
1438
1439         debug!(?matching_bounds, "match_projection_obligation_against_definition_bounds");
1440         matching_bounds
1441     }
1442
1443     /// Equates the trait in `obligation` with trait bound. If the two traits
1444     /// can be equated and the normalized trait bound doesn't contain inference
1445     /// variables or placeholders, the normalized bound is returned.
1446     fn match_normalize_trait_ref(
1447         &mut self,
1448         obligation: &TraitObligation<'tcx>,
1449         trait_bound: ty::PolyTraitRef<'tcx>,
1450         placeholder_trait_ref: ty::TraitRef<'tcx>,
1451     ) -> Result<Option<ty::PolyTraitRef<'tcx>>, ()> {
1452         debug_assert!(!placeholder_trait_ref.has_escaping_bound_vars());
1453         if placeholder_trait_ref.def_id != trait_bound.def_id() {
1454             // Avoid unnecessary normalization
1455             return Err(());
1456         }
1457
1458         let Normalized { value: trait_bound, obligations: _ } = ensure_sufficient_stack(|| {
1459             project::normalize_with_depth(
1460                 self,
1461                 obligation.param_env,
1462                 obligation.cause.clone(),
1463                 obligation.recursion_depth + 1,
1464                 trait_bound,
1465             )
1466         });
1467         self.infcx
1468             .at(&obligation.cause, obligation.param_env)
1469             .sup(ty::Binder::dummy(placeholder_trait_ref), trait_bound)
1470             .map(|InferOk { obligations: _, value: () }| {
1471                 // This method is called within a probe, so we can't have
1472                 // inference variables and placeholders escape.
1473                 if !trait_bound.needs_infer() && !trait_bound.has_placeholders() {
1474                     Some(trait_bound)
1475                 } else {
1476                     None
1477                 }
1478             })
1479             .map_err(|_| ())
1480     }
1481
1482     fn where_clause_may_apply<'o>(
1483         &mut self,
1484         stack: &TraitObligationStack<'o, 'tcx>,
1485         where_clause_trait_ref: ty::PolyTraitRef<'tcx>,
1486     ) -> Result<EvaluationResult, OverflowError> {
1487         self.evaluation_probe(|this| {
1488             match this.match_where_clause_trait_ref(stack.obligation, where_clause_trait_ref) {
1489                 Ok(obligations) => this.evaluate_predicates_recursively(stack.list(), obligations),
1490                 Err(()) => Ok(EvaluatedToErr),
1491             }
1492         })
1493     }
1494
1495     /// Return `Yes` if the obligation's predicate type applies to the env_predicate, and
1496     /// `No` if it does not. Return `Ambiguous` in the case that the projection type is a GAT,
1497     /// and applying this env_predicate constrains any of the obligation's GAT substitutions.
1498     ///
1499     /// This behavior is a somewhat of a hack to prevent overconstraining inference variables
1500     /// in cases like #91762.
1501     pub(super) fn match_projection_projections(
1502         &mut self,
1503         obligation: &ProjectionTyObligation<'tcx>,
1504         env_predicate: PolyProjectionPredicate<'tcx>,
1505         potentially_unnormalized_candidates: bool,
1506     ) -> ProjectionMatchesProjection {
1507         let mut nested_obligations = Vec::new();
1508         let (infer_predicate, _) = self.infcx.replace_bound_vars_with_fresh_vars(
1509             obligation.cause.span,
1510             LateBoundRegionConversionTime::HigherRankedType,
1511             env_predicate,
1512         );
1513         let infer_projection = if potentially_unnormalized_candidates {
1514             ensure_sufficient_stack(|| {
1515                 project::normalize_with_depth_to(
1516                     self,
1517                     obligation.param_env,
1518                     obligation.cause.clone(),
1519                     obligation.recursion_depth + 1,
1520                     infer_predicate.projection_ty,
1521                     &mut nested_obligations,
1522                 )
1523             })
1524         } else {
1525             infer_predicate.projection_ty
1526         };
1527
1528         let is_match = self
1529             .infcx
1530             .at(&obligation.cause, obligation.param_env)
1531             .sup(obligation.predicate, infer_projection)
1532             .map_or(false, |InferOk { obligations, value: () }| {
1533                 self.evaluate_predicates_recursively(
1534                     TraitObligationStackList::empty(&ProvisionalEvaluationCache::default()),
1535                     nested_obligations.into_iter().chain(obligations),
1536                 )
1537                 .map_or(false, |res| res.may_apply())
1538             });
1539
1540         if is_match {
1541             let generics = self.tcx().generics_of(obligation.predicate.item_def_id);
1542             // FIXME(generic-associated-types): Addresses aggressive inference in #92917.
1543             // If this type is a GAT, and of the GAT substs resolve to something new,
1544             // that means that we must have newly inferred something about the GAT.
1545             // We should give up in that case.
1546             if !generics.params.is_empty()
1547                 && obligation.predicate.substs[generics.parent_count..]
1548                     .iter()
1549                     .any(|&p| p.has_infer_types_or_consts() && self.infcx.shallow_resolve(p) != p)
1550             {
1551                 ProjectionMatchesProjection::Ambiguous
1552             } else {
1553                 ProjectionMatchesProjection::Yes
1554             }
1555         } else {
1556             ProjectionMatchesProjection::No
1557         }
1558     }
1559
1560     ///////////////////////////////////////////////////////////////////////////
1561     // WINNOW
1562     //
1563     // Winnowing is the process of attempting to resolve ambiguity by
1564     // probing further. During the winnowing process, we unify all
1565     // type variables and then we also attempt to evaluate recursive
1566     // bounds to see if they are satisfied.
1567
1568     /// Returns `true` if `victim` should be dropped in favor of
1569     /// `other`. Generally speaking we will drop duplicate
1570     /// candidates and prefer where-clause candidates.
1571     ///
1572     /// See the comment for "SelectionCandidate" for more details.
1573     fn candidate_should_be_dropped_in_favor_of(
1574         &mut self,
1575         sized_predicate: bool,
1576         victim: &EvaluatedCandidate<'tcx>,
1577         other: &EvaluatedCandidate<'tcx>,
1578         needs_infer: bool,
1579     ) -> bool {
1580         if victim.candidate == other.candidate {
1581             return true;
1582         }
1583
1584         // Check if a bound would previously have been removed when normalizing
1585         // the param_env so that it can be given the lowest priority. See
1586         // #50825 for the motivation for this.
1587         let is_global = |cand: &ty::PolyTraitPredicate<'tcx>| {
1588             cand.is_global() && !cand.has_late_bound_regions()
1589         };
1590
1591         // (*) Prefer `BuiltinCandidate { has_nested: false }`, `PointeeCandidate`,
1592         // `DiscriminantKindCandidate`, and `ConstDropCandidate` to anything else.
1593         //
1594         // This is a fix for #53123 and prevents winnowing from accidentally extending the
1595         // lifetime of a variable.
1596         match (&other.candidate, &victim.candidate) {
1597             (_, AutoImplCandidate(..)) | (AutoImplCandidate(..), _) => {
1598                 bug!(
1599                     "default implementations shouldn't be recorded \
1600                     when there are other valid candidates"
1601                 );
1602             }
1603
1604             // (*)
1605             (
1606                 BuiltinCandidate { has_nested: false }
1607                 | DiscriminantKindCandidate
1608                 | PointeeCandidate
1609                 | ConstDropCandidate(_),
1610                 _,
1611             ) => true,
1612             (
1613                 _,
1614                 BuiltinCandidate { has_nested: false }
1615                 | DiscriminantKindCandidate
1616                 | PointeeCandidate
1617                 | ConstDropCandidate(_),
1618             ) => false,
1619
1620             (ParamCandidate(other), ParamCandidate(victim)) => {
1621                 let same_except_bound_vars = other.skip_binder().trait_ref
1622                     == victim.skip_binder().trait_ref
1623                     && other.skip_binder().constness == victim.skip_binder().constness
1624                     && other.skip_binder().polarity == victim.skip_binder().polarity
1625                     && !other.skip_binder().trait_ref.has_escaping_bound_vars();
1626                 if same_except_bound_vars {
1627                     // See issue #84398. In short, we can generate multiple ParamCandidates which are
1628                     // the same except for unused bound vars. Just pick the one with the fewest bound vars
1629                     // or the current one if tied (they should both evaluate to the same answer). This is
1630                     // probably best characterized as a "hack", since we might prefer to just do our
1631                     // best to *not* create essentially duplicate candidates in the first place.
1632                     other.bound_vars().len() <= victim.bound_vars().len()
1633                 } else if other.skip_binder().trait_ref == victim.skip_binder().trait_ref
1634                     && victim.skip_binder().constness == ty::BoundConstness::NotConst
1635                     && other.skip_binder().polarity == victim.skip_binder().polarity
1636                 {
1637                     // Drop otherwise equivalent non-const candidates in favor of const candidates.
1638                     true
1639                 } else {
1640                     false
1641                 }
1642             }
1643
1644             // Drop otherwise equivalent non-const fn pointer candidates
1645             (FnPointerCandidate { .. }, FnPointerCandidate { is_const: false }) => true,
1646
1647             // If obligation is a sized predicate or the where-clause bound is
1648             // global, prefer the projection or object candidate. See issue
1649             // #50825 and #89352.
1650             (ObjectCandidate(_) | ProjectionCandidate(_), ParamCandidate(ref cand)) => {
1651                 sized_predicate || is_global(cand)
1652             }
1653             (ParamCandidate(ref cand), ObjectCandidate(_) | ProjectionCandidate(_)) => {
1654                 !(sized_predicate || is_global(cand))
1655             }
1656
1657             // Global bounds from the where clause should be ignored
1658             // here (see issue #50825). Otherwise, we have a where
1659             // clause so don't go around looking for impls.
1660             // Arbitrarily give param candidates priority
1661             // over projection and object candidates.
1662             (
1663                 ParamCandidate(ref cand),
1664                 ImplCandidate(..)
1665                 | ClosureCandidate
1666                 | GeneratorCandidate
1667                 | FnPointerCandidate { .. }
1668                 | BuiltinObjectCandidate
1669                 | BuiltinUnsizeCandidate
1670                 | TraitUpcastingUnsizeCandidate(_)
1671                 | BuiltinCandidate { .. }
1672                 | TraitAliasCandidate(..),
1673             ) => !is_global(cand),
1674             (
1675                 ImplCandidate(_)
1676                 | ClosureCandidate
1677                 | GeneratorCandidate
1678                 | FnPointerCandidate { .. }
1679                 | BuiltinObjectCandidate
1680                 | BuiltinUnsizeCandidate
1681                 | TraitUpcastingUnsizeCandidate(_)
1682                 | BuiltinCandidate { has_nested: true }
1683                 | TraitAliasCandidate(..),
1684                 ParamCandidate(ref cand),
1685             ) => {
1686                 // Prefer these to a global where-clause bound
1687                 // (see issue #50825).
1688                 is_global(cand) && other.evaluation.must_apply_modulo_regions()
1689             }
1690
1691             (ProjectionCandidate(i), ProjectionCandidate(j))
1692             | (ObjectCandidate(i), ObjectCandidate(j)) => {
1693                 // Arbitrarily pick the lower numbered candidate for backwards
1694                 // compatibility reasons. Don't let this affect inference.
1695                 i < j && !needs_infer
1696             }
1697             (ObjectCandidate(_), ProjectionCandidate(_))
1698             | (ProjectionCandidate(_), ObjectCandidate(_)) => {
1699                 bug!("Have both object and projection candidate")
1700             }
1701
1702             // Arbitrarily give projection and object candidates priority.
1703             (
1704                 ObjectCandidate(_) | ProjectionCandidate(_),
1705                 ImplCandidate(..)
1706                 | ClosureCandidate
1707                 | GeneratorCandidate
1708                 | FnPointerCandidate { .. }
1709                 | BuiltinObjectCandidate
1710                 | BuiltinUnsizeCandidate
1711                 | TraitUpcastingUnsizeCandidate(_)
1712                 | BuiltinCandidate { .. }
1713                 | TraitAliasCandidate(..),
1714             ) => true,
1715
1716             (
1717                 ImplCandidate(..)
1718                 | ClosureCandidate
1719                 | GeneratorCandidate
1720                 | FnPointerCandidate { .. }
1721                 | BuiltinObjectCandidate
1722                 | BuiltinUnsizeCandidate
1723                 | TraitUpcastingUnsizeCandidate(_)
1724                 | BuiltinCandidate { .. }
1725                 | TraitAliasCandidate(..),
1726                 ObjectCandidate(_) | ProjectionCandidate(_),
1727             ) => false,
1728
1729             (&ImplCandidate(other_def), &ImplCandidate(victim_def)) => {
1730                 // See if we can toss out `victim` based on specialization.
1731                 // This requires us to know *for sure* that the `other` impl applies
1732                 // i.e., `EvaluatedToOk`.
1733                 //
1734                 // FIXME(@lcnr): Using `modulo_regions` here seems kind of scary
1735                 // to me but is required for `std` to compile, so I didn't change it
1736                 // for now.
1737                 let tcx = self.tcx();
1738                 if other.evaluation.must_apply_modulo_regions() {
1739                     if tcx.specializes((other_def, victim_def)) {
1740                         return true;
1741                     }
1742                 }
1743
1744                 if other.evaluation.must_apply_considering_regions() {
1745                     match tcx.impls_are_allowed_to_overlap(other_def, victim_def) {
1746                         Some(ty::ImplOverlapKind::Permitted { marker: true }) => {
1747                             // Subtle: If the predicate we are evaluating has inference
1748                             // variables, do *not* allow discarding candidates due to
1749                             // marker trait impls.
1750                             //
1751                             // Without this restriction, we could end up accidentally
1752                             // constrainting inference variables based on an arbitrarily
1753                             // chosen trait impl.
1754                             //
1755                             // Imagine we have the following code:
1756                             //
1757                             // ```rust
1758                             // #[marker] trait MyTrait {}
1759                             // impl MyTrait for u8 {}
1760                             // impl MyTrait for bool {}
1761                             // ```
1762                             //
1763                             // And we are evaluating the predicate `<_#0t as MyTrait>`.
1764                             //
1765                             // During selection, we will end up with one candidate for each
1766                             // impl of `MyTrait`. If we were to discard one impl in favor
1767                             // of the other, we would be left with one candidate, causing
1768                             // us to "successfully" select the predicate, unifying
1769                             // _#0t with (for example) `u8`.
1770                             //
1771                             // However, we have no reason to believe that this unification
1772                             // is correct - we've essentially just picked an arbitrary
1773                             // *possibility* for _#0t, and required that this be the *only*
1774                             // possibility.
1775                             //
1776                             // Eventually, we will either:
1777                             // 1) Unify all inference variables in the predicate through
1778                             // some other means (e.g. type-checking of a function). We will
1779                             // then be in a position to drop marker trait candidates
1780                             // without constraining inference variables (since there are
1781                             // none left to constrin)
1782                             // 2) Be left with some unconstrained inference variables. We
1783                             // will then correctly report an inference error, since the
1784                             // existence of multiple marker trait impls tells us nothing
1785                             // about which one should actually apply.
1786                             !needs_infer
1787                         }
1788                         Some(_) => true,
1789                         None => false,
1790                     }
1791                 } else {
1792                     false
1793                 }
1794             }
1795
1796             // Everything else is ambiguous
1797             (
1798                 ImplCandidate(_)
1799                 | ClosureCandidate
1800                 | GeneratorCandidate
1801                 | FnPointerCandidate { .. }
1802                 | BuiltinObjectCandidate
1803                 | BuiltinUnsizeCandidate
1804                 | TraitUpcastingUnsizeCandidate(_)
1805                 | BuiltinCandidate { has_nested: true }
1806                 | TraitAliasCandidate(..),
1807                 ImplCandidate(_)
1808                 | ClosureCandidate
1809                 | GeneratorCandidate
1810                 | FnPointerCandidate { .. }
1811                 | BuiltinObjectCandidate
1812                 | BuiltinUnsizeCandidate
1813                 | TraitUpcastingUnsizeCandidate(_)
1814                 | BuiltinCandidate { has_nested: true }
1815                 | TraitAliasCandidate(..),
1816             ) => false,
1817         }
1818     }
1819
1820     fn sized_conditions(
1821         &mut self,
1822         obligation: &TraitObligation<'tcx>,
1823     ) -> BuiltinImplConditions<'tcx> {
1824         use self::BuiltinImplConditions::{Ambiguous, None, Where};
1825
1826         // NOTE: binder moved to (*)
1827         let self_ty = self.infcx.shallow_resolve(obligation.predicate.skip_binder().self_ty());
1828
1829         match self_ty.kind() {
1830             ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
1831             | ty::Uint(_)
1832             | ty::Int(_)
1833             | ty::Bool
1834             | ty::Float(_)
1835             | ty::FnDef(..)
1836             | ty::FnPtr(_)
1837             | ty::RawPtr(..)
1838             | ty::Char
1839             | ty::Ref(..)
1840             | ty::Generator(..)
1841             | ty::GeneratorWitness(..)
1842             | ty::Array(..)
1843             | ty::Closure(..)
1844             | ty::Never
1845             | ty::Error(_) => {
1846                 // safe for everything
1847                 Where(ty::Binder::dummy(Vec::new()))
1848             }
1849
1850             ty::Str | ty::Slice(_) | ty::Dynamic(..) | ty::Foreign(..) => None,
1851
1852             ty::Tuple(tys) => Where(
1853                 obligation.predicate.rebind(tys.last().map_or_else(Vec::new, |&last| vec![last])),
1854             ),
1855
1856             ty::Adt(def, substs) => {
1857                 let sized_crit = def.sized_constraint(self.tcx());
1858                 // (*) binder moved here
1859                 Where(
1860                     obligation.predicate.rebind({
1861                         sized_crit.iter().map(|ty| ty.subst(self.tcx(), substs)).collect()
1862                     }),
1863                 )
1864             }
1865
1866             ty::Projection(_) | ty::Param(_) | ty::Opaque(..) => None,
1867             ty::Infer(ty::TyVar(_)) => Ambiguous,
1868
1869             ty::Placeholder(..)
1870             | ty::Bound(..)
1871             | ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
1872                 bug!("asked to assemble builtin bounds of unexpected type: {:?}", self_ty);
1873             }
1874         }
1875     }
1876
1877     fn copy_clone_conditions(
1878         &mut self,
1879         obligation: &TraitObligation<'tcx>,
1880     ) -> BuiltinImplConditions<'tcx> {
1881         // NOTE: binder moved to (*)
1882         let self_ty = self.infcx.shallow_resolve(obligation.predicate.skip_binder().self_ty());
1883
1884         use self::BuiltinImplConditions::{Ambiguous, None, Where};
1885
1886         match *self_ty.kind() {
1887             ty::Infer(ty::IntVar(_))
1888             | ty::Infer(ty::FloatVar(_))
1889             | ty::FnDef(..)
1890             | ty::FnPtr(_)
1891             | ty::Error(_) => Where(ty::Binder::dummy(Vec::new())),
1892
1893             ty::Uint(_)
1894             | ty::Int(_)
1895             | ty::Bool
1896             | ty::Float(_)
1897             | ty::Char
1898             | ty::RawPtr(..)
1899             | ty::Never
1900             | ty::Ref(_, _, hir::Mutability::Not)
1901             | ty::Array(..) => {
1902                 // Implementations provided in libcore
1903                 None
1904             }
1905
1906             ty::Dynamic(..)
1907             | ty::Str
1908             | ty::Slice(..)
1909             | ty::Generator(..)
1910             | ty::GeneratorWitness(..)
1911             | ty::Foreign(..)
1912             | ty::Ref(_, _, hir::Mutability::Mut) => None,
1913
1914             ty::Tuple(tys) => {
1915                 // (*) binder moved here
1916                 Where(obligation.predicate.rebind(tys.iter().collect()))
1917             }
1918
1919             ty::Closure(_, substs) => {
1920                 // (*) binder moved here
1921                 let ty = self.infcx.shallow_resolve(substs.as_closure().tupled_upvars_ty());
1922                 if let ty::Infer(ty::TyVar(_)) = ty.kind() {
1923                     // Not yet resolved.
1924                     Ambiguous
1925                 } else {
1926                     Where(obligation.predicate.rebind(substs.as_closure().upvar_tys().collect()))
1927                 }
1928             }
1929
1930             ty::Adt(..) | ty::Projection(..) | ty::Param(..) | ty::Opaque(..) => {
1931                 // Fallback to whatever user-defined impls exist in this case.
1932                 None
1933             }
1934
1935             ty::Infer(ty::TyVar(_)) => {
1936                 // Unbound type variable. Might or might not have
1937                 // applicable impls and so forth, depending on what
1938                 // those type variables wind up being bound to.
1939                 Ambiguous
1940             }
1941
1942             ty::Placeholder(..)
1943             | ty::Bound(..)
1944             | ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
1945                 bug!("asked to assemble builtin bounds of unexpected type: {:?}", self_ty);
1946             }
1947         }
1948     }
1949
1950     /// For default impls, we need to break apart a type into its
1951     /// "constituent types" -- meaning, the types that it contains.
1952     ///
1953     /// Here are some (simple) examples:
1954     ///
1955     /// ```
1956     /// (i32, u32) -> [i32, u32]
1957     /// Foo where struct Foo { x: i32, y: u32 } -> [i32, u32]
1958     /// Bar<i32> where struct Bar<T> { x: T, y: u32 } -> [i32, u32]
1959     /// Zed<i32> where enum Zed { A(T), B(u32) } -> [i32, u32]
1960     /// ```
1961     fn constituent_types_for_ty(
1962         &self,
1963         t: ty::Binder<'tcx, Ty<'tcx>>,
1964     ) -> ty::Binder<'tcx, Vec<Ty<'tcx>>> {
1965         match *t.skip_binder().kind() {
1966             ty::Uint(_)
1967             | ty::Int(_)
1968             | ty::Bool
1969             | ty::Float(_)
1970             | ty::FnDef(..)
1971             | ty::FnPtr(_)
1972             | ty::Str
1973             | ty::Error(_)
1974             | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
1975             | ty::Never
1976             | ty::Char => ty::Binder::dummy(Vec::new()),
1977
1978             ty::Placeholder(..)
1979             | ty::Dynamic(..)
1980             | ty::Param(..)
1981             | ty::Foreign(..)
1982             | ty::Projection(..)
1983             | ty::Bound(..)
1984             | ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
1985                 bug!("asked to assemble constituent types of unexpected type: {:?}", t);
1986             }
1987
1988             ty::RawPtr(ty::TypeAndMut { ty: element_ty, .. }) | ty::Ref(_, element_ty, _) => {
1989                 t.rebind(vec![element_ty])
1990             }
1991
1992             ty::Array(element_ty, _) | ty::Slice(element_ty) => t.rebind(vec![element_ty]),
1993
1994             ty::Tuple(ref tys) => {
1995                 // (T1, ..., Tn) -- meets any bound that all of T1...Tn meet
1996                 t.rebind(tys.iter().collect())
1997             }
1998
1999             ty::Closure(_, ref substs) => {
2000                 let ty = self.infcx.shallow_resolve(substs.as_closure().tupled_upvars_ty());
2001                 t.rebind(vec![ty])
2002             }
2003
2004             ty::Generator(_, ref substs, _) => {
2005                 let ty = self.infcx.shallow_resolve(substs.as_generator().tupled_upvars_ty());
2006                 let witness = substs.as_generator().witness();
2007                 t.rebind([ty].into_iter().chain(iter::once(witness)).collect())
2008             }
2009
2010             ty::GeneratorWitness(types) => {
2011                 debug_assert!(!types.has_escaping_bound_vars());
2012                 types.map_bound(|types| types.to_vec())
2013             }
2014
2015             // For `PhantomData<T>`, we pass `T`.
2016             ty::Adt(def, substs) if def.is_phantom_data() => t.rebind(substs.types().collect()),
2017
2018             ty::Adt(def, substs) => {
2019                 t.rebind(def.all_fields().map(|f| f.ty(self.tcx(), substs)).collect())
2020             }
2021
2022             ty::Opaque(def_id, substs) => {
2023                 // We can resolve the `impl Trait` to its concrete type,
2024                 // which enforces a DAG between the functions requiring
2025                 // the auto trait bounds in question.
2026                 t.rebind(vec![self.tcx().type_of(def_id).subst(self.tcx(), substs)])
2027             }
2028         }
2029     }
2030
2031     fn collect_predicates_for_types(
2032         &mut self,
2033         param_env: ty::ParamEnv<'tcx>,
2034         cause: ObligationCause<'tcx>,
2035         recursion_depth: usize,
2036         trait_def_id: DefId,
2037         types: ty::Binder<'tcx, Vec<Ty<'tcx>>>,
2038     ) -> Vec<PredicateObligation<'tcx>> {
2039         // Because the types were potentially derived from
2040         // higher-ranked obligations they may reference late-bound
2041         // regions. For example, `for<'a> Foo<&'a i32> : Copy` would
2042         // yield a type like `for<'a> &'a i32`. In general, we
2043         // maintain the invariant that we never manipulate bound
2044         // regions, so we have to process these bound regions somehow.
2045         //
2046         // The strategy is to:
2047         //
2048         // 1. Instantiate those regions to placeholder regions (e.g.,
2049         //    `for<'a> &'a i32` becomes `&0 i32`.
2050         // 2. Produce something like `&'0 i32 : Copy`
2051         // 3. Re-bind the regions back to `for<'a> &'a i32 : Copy`
2052
2053         types
2054             .as_ref()
2055             .skip_binder() // binder moved -\
2056             .iter()
2057             .flat_map(|ty| {
2058                 let ty: ty::Binder<'tcx, Ty<'tcx>> = types.rebind(*ty); // <----/
2059
2060                 self.infcx.commit_unconditionally(|_| {
2061                     let placeholder_ty = self.infcx.replace_bound_vars_with_placeholders(ty);
2062                     let Normalized { value: normalized_ty, mut obligations } =
2063                         ensure_sufficient_stack(|| {
2064                             project::normalize_with_depth(
2065                                 self,
2066                                 param_env,
2067                                 cause.clone(),
2068                                 recursion_depth,
2069                                 placeholder_ty,
2070                             )
2071                         });
2072                     let placeholder_obligation = predicate_for_trait_def(
2073                         self.tcx(),
2074                         param_env,
2075                         cause.clone(),
2076                         trait_def_id,
2077                         recursion_depth,
2078                         normalized_ty,
2079                         &[],
2080                     );
2081                     obligations.push(placeholder_obligation);
2082                     obligations
2083                 })
2084             })
2085             .collect()
2086     }
2087
2088     ///////////////////////////////////////////////////////////////////////////
2089     // Matching
2090     //
2091     // Matching is a common path used for both evaluation and
2092     // confirmation.  It basically unifies types that appear in impls
2093     // and traits. This does affect the surrounding environment;
2094     // therefore, when used during evaluation, match routines must be
2095     // run inside of a `probe()` so that their side-effects are
2096     // contained.
2097
2098     fn rematch_impl(
2099         &mut self,
2100         impl_def_id: DefId,
2101         obligation: &TraitObligation<'tcx>,
2102     ) -> Normalized<'tcx, SubstsRef<'tcx>> {
2103         match self.match_impl(impl_def_id, obligation) {
2104             Ok(substs) => substs,
2105             Err(()) => {
2106                 bug!(
2107                     "Impl {:?} was matchable against {:?} but now is not",
2108                     impl_def_id,
2109                     obligation
2110                 );
2111             }
2112         }
2113     }
2114
2115     #[tracing::instrument(level = "debug", skip(self))]
2116     fn match_impl(
2117         &mut self,
2118         impl_def_id: DefId,
2119         obligation: &TraitObligation<'tcx>,
2120     ) -> Result<Normalized<'tcx, SubstsRef<'tcx>>, ()> {
2121         let impl_trait_ref = self.tcx().impl_trait_ref(impl_def_id).unwrap();
2122
2123         // Before we create the substitutions and everything, first
2124         // consider a "quick reject". This avoids creating more types
2125         // and so forth that we need to.
2126         if self.fast_reject_trait_refs(obligation, &impl_trait_ref) {
2127             return Err(());
2128         }
2129
2130         let placeholder_obligation =
2131             self.infcx().replace_bound_vars_with_placeholders(obligation.predicate);
2132         let placeholder_obligation_trait_ref = placeholder_obligation.trait_ref;
2133
2134         let impl_substs = self.infcx.fresh_substs_for_item(obligation.cause.span, impl_def_id);
2135
2136         let impl_trait_ref = impl_trait_ref.subst(self.tcx(), impl_substs);
2137
2138         debug!(?impl_trait_ref);
2139
2140         let Normalized { value: impl_trait_ref, obligations: mut nested_obligations } =
2141             ensure_sufficient_stack(|| {
2142                 project::normalize_with_depth(
2143                     self,
2144                     obligation.param_env,
2145                     obligation.cause.clone(),
2146                     obligation.recursion_depth + 1,
2147                     impl_trait_ref,
2148                 )
2149             });
2150
2151         debug!(?impl_trait_ref, ?placeholder_obligation_trait_ref);
2152
2153         let cause = ObligationCause::new(
2154             obligation.cause.span,
2155             obligation.cause.body_id,
2156             ObligationCauseCode::MatchImpl(obligation.cause.clone(), impl_def_id),
2157         );
2158
2159         let InferOk { obligations, .. } = self
2160             .infcx
2161             .at(&cause, obligation.param_env)
2162             .eq(placeholder_obligation_trait_ref, impl_trait_ref)
2163             .map_err(|e| debug!("match_impl: failed eq_trait_refs due to `{}`", e))?;
2164         nested_obligations.extend(obligations);
2165
2166         if !self.intercrate
2167             && self.tcx().impl_polarity(impl_def_id) == ty::ImplPolarity::Reservation
2168         {
2169             debug!("match_impl: reservation impls only apply in intercrate mode");
2170             return Err(());
2171         }
2172
2173         debug!(?impl_substs, ?nested_obligations, "match_impl: success");
2174         Ok(Normalized { value: impl_substs, obligations: nested_obligations })
2175     }
2176
2177     fn fast_reject_trait_refs(
2178         &mut self,
2179         obligation: &TraitObligation<'tcx>,
2180         impl_trait_ref: &ty::TraitRef<'tcx>,
2181     ) -> bool {
2182         // We can avoid creating type variables and doing the full
2183         // substitution if we find that any of the input types, when
2184         // simplified, do not match.
2185
2186         iter::zip(obligation.predicate.skip_binder().trait_ref.substs, impl_trait_ref.substs).any(
2187             |(obligation_arg, impl_arg)| {
2188                 match (obligation_arg.unpack(), impl_arg.unpack()) {
2189                     (GenericArgKind::Type(obligation_ty), GenericArgKind::Type(impl_ty)) => {
2190                         // Note, we simplify parameters for the obligation but not the
2191                         // impl so that we do not reject a blanket impl but do reject
2192                         // more concrete impls if we're searching for `T: Trait`.
2193                         let simplified_obligation_ty = fast_reject::simplify_type(
2194                             self.tcx(),
2195                             obligation_ty,
2196                             TreatParams::AsBoundTypes,
2197                         );
2198                         let simplified_impl_ty = fast_reject::simplify_type(
2199                             self.tcx(),
2200                             impl_ty,
2201                             TreatParams::AsPlaceholders,
2202                         );
2203
2204                         simplified_obligation_ty.is_some()
2205                             && simplified_impl_ty.is_some()
2206                             && simplified_obligation_ty != simplified_impl_ty
2207                     }
2208                     (GenericArgKind::Lifetime(_), GenericArgKind::Lifetime(_)) => {
2209                         // Lifetimes can never cause a rejection.
2210                         false
2211                     }
2212                     (GenericArgKind::Const(_), GenericArgKind::Const(_)) => {
2213                         // Conservatively ignore consts (i.e. assume they might
2214                         // unify later) until we have `fast_reject` support for
2215                         // them (if we'll ever need it, even).
2216                         false
2217                     }
2218                     _ => unreachable!(),
2219                 }
2220             },
2221         )
2222     }
2223
2224     /// Normalize `where_clause_trait_ref` and try to match it against
2225     /// `obligation`. If successful, return any predicates that
2226     /// result from the normalization.
2227     fn match_where_clause_trait_ref(
2228         &mut self,
2229         obligation: &TraitObligation<'tcx>,
2230         where_clause_trait_ref: ty::PolyTraitRef<'tcx>,
2231     ) -> Result<Vec<PredicateObligation<'tcx>>, ()> {
2232         self.match_poly_trait_ref(obligation, where_clause_trait_ref)
2233     }
2234
2235     /// Returns `Ok` if `poly_trait_ref` being true implies that the
2236     /// obligation is satisfied.
2237     #[instrument(skip(self), level = "debug")]
2238     fn match_poly_trait_ref(
2239         &mut self,
2240         obligation: &TraitObligation<'tcx>,
2241         poly_trait_ref: ty::PolyTraitRef<'tcx>,
2242     ) -> Result<Vec<PredicateObligation<'tcx>>, ()> {
2243         self.infcx
2244             .at(&obligation.cause, obligation.param_env)
2245             .sup(obligation.predicate.to_poly_trait_ref(), poly_trait_ref)
2246             .map(|InferOk { obligations, .. }| obligations)
2247             .map_err(|_| ())
2248     }
2249
2250     ///////////////////////////////////////////////////////////////////////////
2251     // Miscellany
2252
2253     fn match_fresh_trait_refs(
2254         &self,
2255         previous: ty::PolyTraitPredicate<'tcx>,
2256         current: ty::PolyTraitPredicate<'tcx>,
2257         param_env: ty::ParamEnv<'tcx>,
2258     ) -> bool {
2259         let mut matcher = ty::_match::Match::new(self.tcx(), param_env);
2260         matcher.relate(previous, current).is_ok()
2261     }
2262
2263     fn push_stack<'o>(
2264         &mut self,
2265         previous_stack: TraitObligationStackList<'o, 'tcx>,
2266         obligation: &'o TraitObligation<'tcx>,
2267     ) -> TraitObligationStack<'o, 'tcx> {
2268         let fresh_trait_pred = obligation.predicate.fold_with(&mut self.freshener);
2269
2270         let dfn = previous_stack.cache.next_dfn();
2271         let depth = previous_stack.depth() + 1;
2272         TraitObligationStack {
2273             obligation,
2274             fresh_trait_pred,
2275             reached_depth: Cell::new(depth),
2276             previous: previous_stack,
2277             dfn,
2278             depth,
2279         }
2280     }
2281
2282     #[instrument(skip(self), level = "debug")]
2283     fn closure_trait_ref_unnormalized(
2284         &mut self,
2285         obligation: &TraitObligation<'tcx>,
2286         substs: SubstsRef<'tcx>,
2287     ) -> ty::PolyTraitRef<'tcx> {
2288         let closure_sig = substs.as_closure().sig();
2289
2290         debug!(?closure_sig);
2291
2292         // (1) Feels icky to skip the binder here, but OTOH we know
2293         // that the self-type is an unboxed closure type and hence is
2294         // in fact unparameterized (or at least does not reference any
2295         // regions bound in the obligation). Still probably some
2296         // refactoring could make this nicer.
2297         closure_trait_ref_and_return_type(
2298             self.tcx(),
2299             obligation.predicate.def_id(),
2300             obligation.predicate.skip_binder().self_ty(), // (1)
2301             closure_sig,
2302             util::TupleArgumentsFlag::No,
2303         )
2304         .map_bound(|(trait_ref, _)| trait_ref)
2305     }
2306
2307     fn generator_trait_ref_unnormalized(
2308         &mut self,
2309         obligation: &TraitObligation<'tcx>,
2310         substs: SubstsRef<'tcx>,
2311     ) -> ty::PolyTraitRef<'tcx> {
2312         let gen_sig = substs.as_generator().poly_sig();
2313
2314         // (1) Feels icky to skip the binder here, but OTOH we know
2315         // that the self-type is an generator type and hence is
2316         // in fact unparameterized (or at least does not reference any
2317         // regions bound in the obligation). Still probably some
2318         // refactoring could make this nicer.
2319
2320         super::util::generator_trait_ref_and_outputs(
2321             self.tcx(),
2322             obligation.predicate.def_id(),
2323             obligation.predicate.skip_binder().self_ty(), // (1)
2324             gen_sig,
2325         )
2326         .map_bound(|(trait_ref, ..)| trait_ref)
2327     }
2328
2329     /// Returns the obligations that are implied by instantiating an
2330     /// impl or trait. The obligations are substituted and fully
2331     /// normalized. This is used when confirming an impl or default
2332     /// impl.
2333     #[tracing::instrument(level = "debug", skip(self, cause, param_env))]
2334     fn impl_or_trait_obligations(
2335         &mut self,
2336         cause: ObligationCause<'tcx>,
2337         recursion_depth: usize,
2338         param_env: ty::ParamEnv<'tcx>,
2339         def_id: DefId,           // of impl or trait
2340         substs: SubstsRef<'tcx>, // for impl or trait
2341     ) -> Vec<PredicateObligation<'tcx>> {
2342         let tcx = self.tcx();
2343
2344         // To allow for one-pass evaluation of the nested obligation,
2345         // each predicate must be preceded by the obligations required
2346         // to normalize it.
2347         // for example, if we have:
2348         //    impl<U: Iterator<Item: Copy>, V: Iterator<Item = U>> Foo for V
2349         // the impl will have the following predicates:
2350         //    <V as Iterator>::Item = U,
2351         //    U: Iterator, U: Sized,
2352         //    V: Iterator, V: Sized,
2353         //    <U as Iterator>::Item: Copy
2354         // When we substitute, say, `V => IntoIter<u32>, U => $0`, the last
2355         // obligation will normalize to `<$0 as Iterator>::Item = $1` and
2356         // `$1: Copy`, so we must ensure the obligations are emitted in
2357         // that order.
2358         let predicates = tcx.predicates_of(def_id);
2359         debug!(?predicates);
2360         assert_eq!(predicates.parent, None);
2361         let mut obligations = Vec::with_capacity(predicates.predicates.len());
2362         for (predicate, _) in predicates.predicates {
2363             debug!(?predicate);
2364             let predicate = normalize_with_depth_to(
2365                 self,
2366                 param_env,
2367                 cause.clone(),
2368                 recursion_depth,
2369                 predicate.subst(tcx, substs),
2370                 &mut obligations,
2371             );
2372             obligations.push(Obligation {
2373                 cause: cause.clone(),
2374                 recursion_depth,
2375                 param_env,
2376                 predicate,
2377             });
2378         }
2379
2380         obligations
2381     }
2382 }
2383
2384 trait TraitObligationExt<'tcx> {
2385     fn derived_cause(
2386         &self,
2387         variant: fn(DerivedObligationCause<'tcx>) -> ObligationCauseCode<'tcx>,
2388     ) -> ObligationCause<'tcx>;
2389 }
2390
2391 impl<'tcx> TraitObligationExt<'tcx> for TraitObligation<'tcx> {
2392     fn derived_cause(
2393         &self,
2394         variant: fn(DerivedObligationCause<'tcx>) -> ObligationCauseCode<'tcx>,
2395     ) -> ObligationCause<'tcx> {
2396         /*!
2397          * Creates a cause for obligations that are derived from
2398          * `obligation` by a recursive search (e.g., for a builtin
2399          * bound, or eventually a `auto trait Foo`). If `obligation`
2400          * is itself a derived obligation, this is just a clone, but
2401          * otherwise we create a "derived obligation" cause so as to
2402          * keep track of the original root obligation for error
2403          * reporting.
2404          */
2405
2406         let obligation = self;
2407
2408         // NOTE(flaper87): As of now, it keeps track of the whole error
2409         // chain. Ideally, we should have a way to configure this either
2410         // by using -Z verbose or just a CLI argument.
2411         let derived_cause = DerivedObligationCause {
2412             parent_trait_pred: obligation.predicate,
2413             parent_code: obligation.cause.clone_code(),
2414         };
2415         let derived_code = variant(derived_cause);
2416         ObligationCause::new(obligation.cause.span, obligation.cause.body_id, derived_code)
2417     }
2418 }
2419
2420 impl<'o, 'tcx> TraitObligationStack<'o, 'tcx> {
2421     fn list(&'o self) -> TraitObligationStackList<'o, 'tcx> {
2422         TraitObligationStackList::with(self)
2423     }
2424
2425     fn cache(&self) -> &'o ProvisionalEvaluationCache<'tcx> {
2426         self.previous.cache
2427     }
2428
2429     fn iter(&'o self) -> TraitObligationStackList<'o, 'tcx> {
2430         self.list()
2431     }
2432
2433     /// Indicates that attempting to evaluate this stack entry
2434     /// required accessing something from the stack at depth `reached_depth`.
2435     fn update_reached_depth(&self, reached_depth: usize) {
2436         assert!(
2437             self.depth >= reached_depth,
2438             "invoked `update_reached_depth` with something under this stack: \
2439              self.depth={} reached_depth={}",
2440             self.depth,
2441             reached_depth,
2442         );
2443         debug!(reached_depth, "update_reached_depth");
2444         let mut p = self;
2445         while reached_depth < p.depth {
2446             debug!(?p.fresh_trait_pred, "update_reached_depth: marking as cycle participant");
2447             p.reached_depth.set(p.reached_depth.get().min(reached_depth));
2448             p = p.previous.head.unwrap();
2449         }
2450     }
2451 }
2452
2453 /// The "provisional evaluation cache" is used to store intermediate cache results
2454 /// when solving auto traits. Auto traits are unusual in that they can support
2455 /// cycles. So, for example, a "proof tree" like this would be ok:
2456 ///
2457 /// - `Foo<T>: Send` :-
2458 ///   - `Bar<T>: Send` :-
2459 ///     - `Foo<T>: Send` -- cycle, but ok
2460 ///   - `Baz<T>: Send`
2461 ///
2462 /// Here, to prove `Foo<T>: Send`, we have to prove `Bar<T>: Send` and
2463 /// `Baz<T>: Send`. Proving `Bar<T>: Send` in turn required `Foo<T>: Send`.
2464 /// For non-auto traits, this cycle would be an error, but for auto traits (because
2465 /// they are coinductive) it is considered ok.
2466 ///
2467 /// However, there is a complication: at the point where we have
2468 /// "proven" `Bar<T>: Send`, we have in fact only proven it
2469 /// *provisionally*. In particular, we proved that `Bar<T>: Send`
2470 /// *under the assumption* that `Foo<T>: Send`. But what if we later
2471 /// find out this assumption is wrong?  Specifically, we could
2472 /// encounter some kind of error proving `Baz<T>: Send`. In that case,
2473 /// `Bar<T>: Send` didn't turn out to be true.
2474 ///
2475 /// In Issue #60010, we found a bug in rustc where it would cache
2476 /// these intermediate results. This was fixed in #60444 by disabling
2477 /// *all* caching for things involved in a cycle -- in our example,
2478 /// that would mean we don't cache that `Bar<T>: Send`.  But this led
2479 /// to large slowdowns.
2480 ///
2481 /// Specifically, imagine this scenario, where proving `Baz<T>: Send`
2482 /// first requires proving `Bar<T>: Send` (which is true:
2483 ///
2484 /// - `Foo<T>: Send` :-
2485 ///   - `Bar<T>: Send` :-
2486 ///     - `Foo<T>: Send` -- cycle, but ok
2487 ///   - `Baz<T>: Send`
2488 ///     - `Bar<T>: Send` -- would be nice for this to be a cache hit!
2489 ///     - `*const T: Send` -- but what if we later encounter an error?
2490 ///
2491 /// The *provisional evaluation cache* resolves this issue. It stores
2492 /// cache results that we've proven but which were involved in a cycle
2493 /// in some way. We track the minimal stack depth (i.e., the
2494 /// farthest from the top of the stack) that we are dependent on.
2495 /// The idea is that the cache results within are all valid -- so long as
2496 /// none of the nodes in between the current node and the node at that minimum
2497 /// depth result in an error (in which case the cached results are just thrown away).
2498 ///
2499 /// During evaluation, we consult this provisional cache and rely on
2500 /// it. Accessing a cached value is considered equivalent to accessing
2501 /// a result at `reached_depth`, so it marks the *current* solution as
2502 /// provisional as well. If an error is encountered, we toss out any
2503 /// provisional results added from the subtree that encountered the
2504 /// error.  When we pop the node at `reached_depth` from the stack, we
2505 /// can commit all the things that remain in the provisional cache.
2506 struct ProvisionalEvaluationCache<'tcx> {
2507     /// next "depth first number" to issue -- just a counter
2508     dfn: Cell<usize>,
2509
2510     /// Map from cache key to the provisionally evaluated thing.
2511     /// The cache entries contain the result but also the DFN in which they
2512     /// were added. The DFN is used to clear out values on failure.
2513     ///
2514     /// Imagine we have a stack like:
2515     ///
2516     /// - `A B C` and we add a cache for the result of C (DFN 2)
2517     /// - Then we have a stack `A B D` where `D` has DFN 3
2518     /// - We try to solve D by evaluating E: `A B D E` (DFN 4)
2519     /// - `E` generates various cache entries which have cyclic dependices on `B`
2520     ///   - `A B D E F` and so forth
2521     ///   - the DFN of `F` for example would be 5
2522     /// - then we determine that `E` is in error -- we will then clear
2523     ///   all cache values whose DFN is >= 4 -- in this case, that
2524     ///   means the cached value for `F`.
2525     map: RefCell<FxHashMap<ty::PolyTraitPredicate<'tcx>, ProvisionalEvaluation>>,
2526 }
2527
2528 /// A cache value for the provisional cache: contains the depth-first
2529 /// number (DFN) and result.
2530 #[derive(Copy, Clone, Debug)]
2531 struct ProvisionalEvaluation {
2532     from_dfn: usize,
2533     reached_depth: usize,
2534     result: EvaluationResult,
2535     /// The `DepNodeIndex` created for the `evaluate_stack` call for this provisional
2536     /// evaluation. When we create an entry in the evaluation cache using this provisional
2537     /// cache entry (see `on_completion`), we use this `dep_node` to ensure that future reads from
2538     /// the cache will have all of the necessary incr comp dependencies tracked.
2539     dep_node: DepNodeIndex,
2540 }
2541
2542 impl<'tcx> Default for ProvisionalEvaluationCache<'tcx> {
2543     fn default() -> Self {
2544         Self { dfn: Cell::new(0), map: Default::default() }
2545     }
2546 }
2547
2548 impl<'tcx> ProvisionalEvaluationCache<'tcx> {
2549     /// Get the next DFN in sequence (basically a counter).
2550     fn next_dfn(&self) -> usize {
2551         let result = self.dfn.get();
2552         self.dfn.set(result + 1);
2553         result
2554     }
2555
2556     /// Check the provisional cache for any result for
2557     /// `fresh_trait_ref`. If there is a hit, then you must consider
2558     /// it an access to the stack slots at depth
2559     /// `reached_depth` (from the returned value).
2560     fn get_provisional(
2561         &self,
2562         fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
2563     ) -> Option<ProvisionalEvaluation> {
2564         debug!(
2565             ?fresh_trait_pred,
2566             "get_provisional = {:#?}",
2567             self.map.borrow().get(&fresh_trait_pred),
2568         );
2569         Some(*self.map.borrow().get(&fresh_trait_pred)?)
2570     }
2571
2572     /// Insert a provisional result into the cache. The result came
2573     /// from the node with the given DFN. It accessed a minimum depth
2574     /// of `reached_depth` to compute. It evaluated `fresh_trait_pred`
2575     /// and resulted in `result`.
2576     fn insert_provisional(
2577         &self,
2578         from_dfn: usize,
2579         reached_depth: usize,
2580         fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
2581         result: EvaluationResult,
2582         dep_node: DepNodeIndex,
2583     ) {
2584         debug!(?from_dfn, ?fresh_trait_pred, ?result, "insert_provisional");
2585
2586         let mut map = self.map.borrow_mut();
2587
2588         // Subtle: when we complete working on the DFN `from_dfn`, anything
2589         // that remains in the provisional cache must be dependent on some older
2590         // stack entry than `from_dfn`. We have to update their depth with our transitive
2591         // depth in that case or else it would be referring to some popped note.
2592         //
2593         // Example:
2594         // A (reached depth 0)
2595         //   ...
2596         //      B // depth 1 -- reached depth = 0
2597         //          C // depth 2 -- reached depth = 1 (should be 0)
2598         //              B
2599         //          A // depth 0
2600         //   D (reached depth 1)
2601         //      C (cache -- reached depth = 2)
2602         for (_k, v) in &mut *map {
2603             if v.from_dfn >= from_dfn {
2604                 v.reached_depth = reached_depth.min(v.reached_depth);
2605             }
2606         }
2607
2608         map.insert(
2609             fresh_trait_pred,
2610             ProvisionalEvaluation { from_dfn, reached_depth, result, dep_node },
2611         );
2612     }
2613
2614     /// Invoked when the node with dfn `dfn` does not get a successful
2615     /// result.  This will clear out any provisional cache entries
2616     /// that were added since `dfn` was created. This is because the
2617     /// provisional entries are things which must assume that the
2618     /// things on the stack at the time of their creation succeeded --
2619     /// since the failing node is presently at the top of the stack,
2620     /// these provisional entries must either depend on it or some
2621     /// ancestor of it.
2622     fn on_failure(&self, dfn: usize) {
2623         debug!(?dfn, "on_failure");
2624         self.map.borrow_mut().retain(|key, eval| {
2625             if !eval.from_dfn >= dfn {
2626                 debug!("on_failure: removing {:?}", key);
2627                 false
2628             } else {
2629                 true
2630             }
2631         });
2632     }
2633
2634     /// Invoked when the node at depth `depth` completed without
2635     /// depending on anything higher in the stack (if that completion
2636     /// was a failure, then `on_failure` should have been invoked
2637     /// already). The callback `op` will be invoked for each
2638     /// provisional entry that we can now confirm.
2639     ///
2640     /// Note that we may still have provisional cache items remaining
2641     /// in the cache when this is done. For example, if there is a
2642     /// cycle:
2643     ///
2644     /// * A depends on...
2645     ///     * B depends on A
2646     ///     * C depends on...
2647     ///         * D depends on C
2648     ///     * ...
2649     ///
2650     /// Then as we complete the C node we will have a provisional cache
2651     /// with results for A, B, C, and D. This method would clear out
2652     /// the C and D results, but leave A and B provisional.
2653     ///
2654     /// This is determined based on the DFN: we remove any provisional
2655     /// results created since `dfn` started (e.g., in our example, dfn
2656     /// would be 2, representing the C node, and hence we would
2657     /// remove the result for D, which has DFN 3, but not the results for
2658     /// A and B, which have DFNs 0 and 1 respectively).
2659     fn on_completion(
2660         &self,
2661         dfn: usize,
2662         mut op: impl FnMut(ty::PolyTraitPredicate<'tcx>, EvaluationResult, DepNodeIndex),
2663     ) {
2664         debug!(?dfn, "on_completion");
2665
2666         for (fresh_trait_pred, eval) in
2667             self.map.borrow_mut().drain_filter(|_k, eval| eval.from_dfn >= dfn)
2668         {
2669             debug!(?fresh_trait_pred, ?eval, "on_completion");
2670
2671             op(fresh_trait_pred, eval.result, eval.dep_node);
2672         }
2673     }
2674 }
2675
2676 #[derive(Copy, Clone)]
2677 struct TraitObligationStackList<'o, 'tcx> {
2678     cache: &'o ProvisionalEvaluationCache<'tcx>,
2679     head: Option<&'o TraitObligationStack<'o, 'tcx>>,
2680 }
2681
2682 impl<'o, 'tcx> TraitObligationStackList<'o, 'tcx> {
2683     fn empty(cache: &'o ProvisionalEvaluationCache<'tcx>) -> TraitObligationStackList<'o, 'tcx> {
2684         TraitObligationStackList { cache, head: None }
2685     }
2686
2687     fn with(r: &'o TraitObligationStack<'o, 'tcx>) -> TraitObligationStackList<'o, 'tcx> {
2688         TraitObligationStackList { cache: r.cache(), head: Some(r) }
2689     }
2690
2691     fn head(&self) -> Option<&'o TraitObligationStack<'o, 'tcx>> {
2692         self.head
2693     }
2694
2695     fn depth(&self) -> usize {
2696         if let Some(head) = self.head { head.depth } else { 0 }
2697     }
2698 }
2699
2700 impl<'o, 'tcx> Iterator for TraitObligationStackList<'o, 'tcx> {
2701     type Item = &'o TraitObligationStack<'o, 'tcx>;
2702
2703     fn next(&mut self) -> Option<&'o TraitObligationStack<'o, 'tcx>> {
2704         let o = self.head?;
2705         *self = o.previous;
2706         Some(o)
2707     }
2708 }
2709
2710 impl<'o, 'tcx> fmt::Debug for TraitObligationStack<'o, 'tcx> {
2711     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2712         write!(f, "TraitObligationStack({:?})", self.obligation)
2713     }
2714 }
2715
2716 pub enum ProjectionMatchesProjection {
2717     Yes,
2718     Ambiguous,
2719     No,
2720 }