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