]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/project.rs
Rollup merge of #93263 - sunfishcode:sunfishcode/detatched-console-handle, r=dtolnay
[rust.git] / compiler / rustc_trait_selection / src / traits / project.rs
1 //! Code for projecting associated types out of trait references.
2
3 use super::specialization_graph;
4 use super::translate_substs;
5 use super::util;
6 use super::MismatchedProjectionTypes;
7 use super::Obligation;
8 use super::ObligationCause;
9 use super::PredicateObligation;
10 use super::Selection;
11 use super::SelectionContext;
12 use super::SelectionError;
13 use super::{
14     ImplSourceClosureData, ImplSourceDiscriminantKindData, ImplSourceFnPointerData,
15     ImplSourceGeneratorData, ImplSourcePointeeData, ImplSourceUserDefinedData,
16 };
17 use super::{Normalized, NormalizedTy, ProjectionCacheEntry, ProjectionCacheKey};
18
19 use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
20 use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
21 use crate::traits::error_reporting::InferCtxtExt as _;
22 use crate::traits::select::ProjectionMatchesProjection;
23 use rustc_data_structures::sso::SsoHashSet;
24 use rustc_data_structures::stack::ensure_sufficient_stack;
25 use rustc_errors::ErrorGuaranteed;
26 use rustc_hir::def::DefKind;
27 use rustc_hir::def_id::DefId;
28 use rustc_hir::lang_items::LangItem;
29 use rustc_infer::infer::resolve::OpportunisticRegionResolver;
30 use rustc_middle::ty::fold::{TypeFoldable, TypeFolder};
31 use rustc_middle::ty::subst::Subst;
32 use rustc_middle::ty::{self, Term, ToPredicate, Ty, TyCtxt};
33 use rustc_span::symbol::sym;
34
35 use std::collections::BTreeMap;
36
37 pub use rustc_middle::traits::Reveal;
38
39 pub type PolyProjectionObligation<'tcx> = Obligation<'tcx, ty::PolyProjectionPredicate<'tcx>>;
40
41 pub type ProjectionObligation<'tcx> = Obligation<'tcx, ty::ProjectionPredicate<'tcx>>;
42
43 pub type ProjectionTyObligation<'tcx> = Obligation<'tcx, ty::ProjectionTy<'tcx>>;
44
45 pub(super) struct InProgress;
46
47 /// When attempting to resolve `<T as TraitRef>::Name` ...
48 #[derive(Debug)]
49 pub enum ProjectionError<'tcx> {
50     /// ...we found multiple sources of information and couldn't resolve the ambiguity.
51     TooManyCandidates,
52
53     /// ...an error occurred matching `T : TraitRef`
54     TraitSelectionError(SelectionError<'tcx>),
55 }
56
57 #[derive(PartialEq, Eq, Debug)]
58 enum ProjectionCandidate<'tcx> {
59     /// From a where-clause in the env or object type
60     ParamEnv(ty::PolyProjectionPredicate<'tcx>),
61
62     /// From the definition of `Trait` when you have something like <<A as Trait>::B as Trait2>::C
63     TraitDef(ty::PolyProjectionPredicate<'tcx>),
64
65     /// Bounds specified on an object type
66     Object(ty::PolyProjectionPredicate<'tcx>),
67
68     /// From an "impl" (or a "pseudo-impl" returned by select)
69     Select(Selection<'tcx>),
70 }
71
72 enum ProjectionCandidateSet<'tcx> {
73     None,
74     Single(ProjectionCandidate<'tcx>),
75     Ambiguous,
76     Error(SelectionError<'tcx>),
77 }
78
79 impl<'tcx> ProjectionCandidateSet<'tcx> {
80     fn mark_ambiguous(&mut self) {
81         *self = ProjectionCandidateSet::Ambiguous;
82     }
83
84     fn mark_error(&mut self, err: SelectionError<'tcx>) {
85         *self = ProjectionCandidateSet::Error(err);
86     }
87
88     // Returns true if the push was successful, or false if the candidate
89     // was discarded -- this could be because of ambiguity, or because
90     // a higher-priority candidate is already there.
91     fn push_candidate(&mut self, candidate: ProjectionCandidate<'tcx>) -> bool {
92         use self::ProjectionCandidate::*;
93         use self::ProjectionCandidateSet::*;
94
95         // This wacky variable is just used to try and
96         // make code readable and avoid confusing paths.
97         // It is assigned a "value" of `()` only on those
98         // paths in which we wish to convert `*self` to
99         // ambiguous (and return false, because the candidate
100         // was not used). On other paths, it is not assigned,
101         // and hence if those paths *could* reach the code that
102         // comes after the match, this fn would not compile.
103         let convert_to_ambiguous;
104
105         match self {
106             None => {
107                 *self = Single(candidate);
108                 return true;
109             }
110
111             Single(current) => {
112                 // Duplicates can happen inside ParamEnv. In the case, we
113                 // perform a lazy deduplication.
114                 if current == &candidate {
115                     return false;
116                 }
117
118                 // Prefer where-clauses. As in select, if there are multiple
119                 // candidates, we prefer where-clause candidates over impls.  This
120                 // may seem a bit surprising, since impls are the source of
121                 // "truth" in some sense, but in fact some of the impls that SEEM
122                 // applicable are not, because of nested obligations. Where
123                 // clauses are the safer choice. See the comment on
124                 // `select::SelectionCandidate` and #21974 for more details.
125                 match (current, candidate) {
126                     (ParamEnv(..), ParamEnv(..)) => convert_to_ambiguous = (),
127                     (ParamEnv(..), _) => return false,
128                     (_, ParamEnv(..)) => unreachable!(),
129                     (_, _) => convert_to_ambiguous = (),
130                 }
131             }
132
133             Ambiguous | Error(..) => {
134                 return false;
135             }
136         }
137
138         // We only ever get here when we moved from a single candidate
139         // to ambiguous.
140         let () = convert_to_ambiguous;
141         *self = Ambiguous;
142         false
143     }
144 }
145
146 /// Evaluates constraints of the form:
147 ///
148 ///     for<...> <T as Trait>::U == V
149 ///
150 /// If successful, this may result in additional obligations. Also returns
151 /// the projection cache key used to track these additional obligations.
152 ///
153 /// ## Returns
154 ///
155 /// - `Err(_)`: the projection can be normalized, but is not equal to the
156 ///   expected type.
157 /// - `Ok(Err(InProgress))`: this is called recursively while normalizing
158 ///   the same projection.
159 /// - `Ok(Ok(None))`: The projection cannot be normalized due to ambiguity
160 ///   (resolving some inference variables in the projection may fix this).
161 /// - `Ok(Ok(Some(obligations)))`: The projection bound holds subject to
162 ///    the given obligations. If the projection cannot be normalized because
163 ///    the required trait bound doesn't hold this returned with `obligations`
164 ///    being a predicate that cannot be proven.
165 #[instrument(level = "debug", skip(selcx))]
166 pub(super) fn poly_project_and_unify_type<'cx, 'tcx>(
167     selcx: &mut SelectionContext<'cx, 'tcx>,
168     obligation: &PolyProjectionObligation<'tcx>,
169 ) -> Result<
170     Result<Option<Vec<PredicateObligation<'tcx>>>, InProgress>,
171     MismatchedProjectionTypes<'tcx>,
172 > {
173     let infcx = selcx.infcx();
174     infcx.commit_if_ok(|_snapshot| {
175         let placeholder_predicate =
176             infcx.replace_bound_vars_with_placeholders(obligation.predicate);
177
178         let placeholder_obligation = obligation.with(placeholder_predicate);
179         let result = project_and_unify_type(selcx, &placeholder_obligation)?;
180         Ok(result)
181     })
182 }
183
184 /// Evaluates constraints of the form:
185 ///
186 ///     <T as Trait>::U == V
187 ///
188 /// If successful, this may result in additional obligations.
189 ///
190 /// See [poly_project_and_unify_type] for an explanation of the return value.
191 fn project_and_unify_type<'cx, 'tcx>(
192     selcx: &mut SelectionContext<'cx, 'tcx>,
193     obligation: &ProjectionObligation<'tcx>,
194 ) -> Result<
195     Result<Option<Vec<PredicateObligation<'tcx>>>, InProgress>,
196     MismatchedProjectionTypes<'tcx>,
197 > {
198     debug!(?obligation, "project_and_unify_type");
199
200     let mut obligations = vec![];
201
202     let infcx = selcx.infcx();
203     let normalized = match opt_normalize_projection_type(
204         selcx,
205         obligation.param_env,
206         obligation.predicate.projection_ty,
207         obligation.cause.clone(),
208         obligation.recursion_depth,
209         &mut obligations,
210     ) {
211         Ok(Some(n)) => n,
212         Ok(None) => return Ok(Ok(None)),
213         Err(InProgress) => return Ok(Err(InProgress)),
214     };
215     debug!(?normalized, ?obligations, "project_and_unify_type result");
216     match infcx
217         .at(&obligation.cause, obligation.param_env)
218         .eq(normalized, obligation.predicate.term)
219     {
220         Ok(InferOk { obligations: inferred_obligations, value: () }) => {
221             obligations.extend(inferred_obligations);
222             Ok(Ok(Some(obligations)))
223         }
224         Err(err) => {
225             debug!("project_and_unify_type: equating types encountered error {:?}", err);
226             Err(MismatchedProjectionTypes { err })
227         }
228     }
229 }
230
231 /// Normalizes any associated type projections in `value`, replacing
232 /// them with a fully resolved type where possible. The return value
233 /// combines the normalized result and any additional obligations that
234 /// were incurred as result.
235 pub fn normalize<'a, 'b, 'tcx, T>(
236     selcx: &'a mut SelectionContext<'b, 'tcx>,
237     param_env: ty::ParamEnv<'tcx>,
238     cause: ObligationCause<'tcx>,
239     value: T,
240 ) -> Normalized<'tcx, T>
241 where
242     T: TypeFoldable<'tcx>,
243 {
244     let mut obligations = Vec::new();
245     let value = normalize_to(selcx, param_env, cause, value, &mut obligations);
246     Normalized { value, obligations }
247 }
248
249 pub fn normalize_to<'a, 'b, 'tcx, T>(
250     selcx: &'a mut SelectionContext<'b, 'tcx>,
251     param_env: ty::ParamEnv<'tcx>,
252     cause: ObligationCause<'tcx>,
253     value: T,
254     obligations: &mut Vec<PredicateObligation<'tcx>>,
255 ) -> T
256 where
257     T: TypeFoldable<'tcx>,
258 {
259     normalize_with_depth_to(selcx, param_env, cause, 0, value, obligations)
260 }
261
262 /// As `normalize`, but with a custom depth.
263 pub fn normalize_with_depth<'a, 'b, 'tcx, T>(
264     selcx: &'a mut SelectionContext<'b, 'tcx>,
265     param_env: ty::ParamEnv<'tcx>,
266     cause: ObligationCause<'tcx>,
267     depth: usize,
268     value: T,
269 ) -> Normalized<'tcx, T>
270 where
271     T: TypeFoldable<'tcx>,
272 {
273     let mut obligations = Vec::new();
274     let value = normalize_with_depth_to(selcx, param_env, cause, depth, value, &mut obligations);
275     Normalized { value, obligations }
276 }
277
278 #[instrument(level = "info", skip(selcx, param_env, cause, obligations))]
279 pub fn normalize_with_depth_to<'a, 'b, 'tcx, T>(
280     selcx: &'a mut SelectionContext<'b, 'tcx>,
281     param_env: ty::ParamEnv<'tcx>,
282     cause: ObligationCause<'tcx>,
283     depth: usize,
284     value: T,
285     obligations: &mut Vec<PredicateObligation<'tcx>>,
286 ) -> T
287 where
288     T: TypeFoldable<'tcx>,
289 {
290     debug!(obligations.len = obligations.len());
291     let mut normalizer = AssocTypeNormalizer::new(selcx, param_env, cause, depth, obligations);
292     let result = ensure_sufficient_stack(|| normalizer.fold(value));
293     debug!(?result, obligations.len = normalizer.obligations.len());
294     debug!(?normalizer.obligations,);
295     result
296 }
297
298 #[instrument(level = "info", skip(selcx, param_env, cause, obligations))]
299 pub fn try_normalize_with_depth_to<'a, 'b, 'tcx, T>(
300     selcx: &'a mut SelectionContext<'b, 'tcx>,
301     param_env: ty::ParamEnv<'tcx>,
302     cause: ObligationCause<'tcx>,
303     depth: usize,
304     value: T,
305     obligations: &mut Vec<PredicateObligation<'tcx>>,
306 ) -> T
307 where
308     T: TypeFoldable<'tcx>,
309 {
310     debug!(obligations.len = obligations.len());
311     let mut normalizer = AssocTypeNormalizer::new_without_eager_inference_replacement(
312         selcx,
313         param_env,
314         cause,
315         depth,
316         obligations,
317     );
318     let result = ensure_sufficient_stack(|| normalizer.fold(value));
319     debug!(?result, obligations.len = normalizer.obligations.len());
320     debug!(?normalizer.obligations,);
321     result
322 }
323
324 pub(crate) fn needs_normalization<'tcx, T: TypeFoldable<'tcx>>(value: &T, reveal: Reveal) -> bool {
325     match reveal {
326         Reveal::UserFacing => value
327             .has_type_flags(ty::TypeFlags::HAS_TY_PROJECTION | ty::TypeFlags::HAS_CT_PROJECTION),
328         Reveal::All => value.has_type_flags(
329             ty::TypeFlags::HAS_TY_PROJECTION
330                 | ty::TypeFlags::HAS_TY_OPAQUE
331                 | ty::TypeFlags::HAS_CT_PROJECTION,
332         ),
333     }
334 }
335
336 struct AssocTypeNormalizer<'a, 'b, 'tcx> {
337     selcx: &'a mut SelectionContext<'b, 'tcx>,
338     param_env: ty::ParamEnv<'tcx>,
339     cause: ObligationCause<'tcx>,
340     obligations: &'a mut Vec<PredicateObligation<'tcx>>,
341     depth: usize,
342     universes: Vec<Option<ty::UniverseIndex>>,
343     /// If true, when a projection is unable to be completed, an inference
344     /// variable will be created and an obligation registered to project to that
345     /// inference variable. Also, constants will be eagerly evaluated.
346     eager_inference_replacement: bool,
347 }
348
349 impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
350     fn new(
351         selcx: &'a mut SelectionContext<'b, 'tcx>,
352         param_env: ty::ParamEnv<'tcx>,
353         cause: ObligationCause<'tcx>,
354         depth: usize,
355         obligations: &'a mut Vec<PredicateObligation<'tcx>>,
356     ) -> AssocTypeNormalizer<'a, 'b, 'tcx> {
357         AssocTypeNormalizer {
358             selcx,
359             param_env,
360             cause,
361             obligations,
362             depth,
363             universes: vec![],
364             eager_inference_replacement: true,
365         }
366     }
367
368     fn new_without_eager_inference_replacement(
369         selcx: &'a mut SelectionContext<'b, 'tcx>,
370         param_env: ty::ParamEnv<'tcx>,
371         cause: ObligationCause<'tcx>,
372         depth: usize,
373         obligations: &'a mut Vec<PredicateObligation<'tcx>>,
374     ) -> AssocTypeNormalizer<'a, 'b, 'tcx> {
375         AssocTypeNormalizer {
376             selcx,
377             param_env,
378             cause,
379             obligations,
380             depth,
381             universes: vec![],
382             eager_inference_replacement: false,
383         }
384     }
385
386     fn fold<T: TypeFoldable<'tcx>>(&mut self, value: T) -> T {
387         let value = self.selcx.infcx().resolve_vars_if_possible(value);
388         debug!(?value);
389
390         assert!(
391             !value.has_escaping_bound_vars(),
392             "Normalizing {:?} without wrapping in a `Binder`",
393             value
394         );
395
396         if !needs_normalization(&value, self.param_env.reveal()) {
397             value
398         } else {
399             value.fold_with(self)
400         }
401     }
402 }
403
404 impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
405     fn tcx<'c>(&'c self) -> TyCtxt<'tcx> {
406         self.selcx.tcx()
407     }
408
409     fn fold_binder<T: TypeFoldable<'tcx>>(
410         &mut self,
411         t: ty::Binder<'tcx, T>,
412     ) -> ty::Binder<'tcx, T> {
413         self.universes.push(None);
414         let t = t.super_fold_with(self);
415         self.universes.pop();
416         t
417     }
418
419     fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
420         if !needs_normalization(&ty, self.param_env.reveal()) {
421             return ty;
422         }
423
424         // We try to be a little clever here as a performance optimization in
425         // cases where there are nested projections under binders.
426         // For example:
427         // ```
428         // for<'a> fn(<T as Foo>::One<'a, Box<dyn Bar<'a, Item=<T as Foo>::Two<'a>>>>)
429         // ```
430         // We normalize the substs on the projection before the projecting, but
431         // if we're naive, we'll
432         //   replace bound vars on inner, project inner, replace placeholders on inner,
433         //   replace bound vars on outer, project outer, replace placeholders on outer
434         //
435         // However, if we're a bit more clever, we can replace the bound vars
436         // on the entire type before normalizing nested projections, meaning we
437         //   replace bound vars on outer, project inner,
438         //   project outer, replace placeholders on outer
439         //
440         // This is possible because the inner `'a` will already be a placeholder
441         // when we need to normalize the inner projection
442         //
443         // On the other hand, this does add a bit of complexity, since we only
444         // replace bound vars if the current type is a `Projection` and we need
445         // to make sure we don't forget to fold the substs regardless.
446
447         match *ty.kind() {
448             // This is really important. While we *can* handle this, this has
449             // severe performance implications for large opaque types with
450             // late-bound regions. See `issue-88862` benchmark.
451             ty::Opaque(def_id, substs) if !substs.has_escaping_bound_vars() => {
452                 // Only normalize `impl Trait` outside of type inference, usually in codegen.
453                 match self.param_env.reveal() {
454                     Reveal::UserFacing => ty.super_fold_with(self),
455
456                     Reveal::All => {
457                         let recursion_limit = self.tcx().recursion_limit();
458                         if !recursion_limit.value_within_limit(self.depth) {
459                             let obligation = Obligation::with_depth(
460                                 self.cause.clone(),
461                                 recursion_limit.0,
462                                 self.param_env,
463                                 ty,
464                             );
465                             self.selcx.infcx().report_overflow_error(&obligation, true);
466                         }
467
468                         let substs = substs.super_fold_with(self);
469                         let generic_ty = self.tcx().type_of(def_id);
470                         let concrete_ty = generic_ty.subst(self.tcx(), substs);
471                         self.depth += 1;
472                         let folded_ty = self.fold_ty(concrete_ty);
473                         self.depth -= 1;
474                         folded_ty
475                     }
476                 }
477             }
478
479             ty::Projection(data) if !data.has_escaping_bound_vars() => {
480                 // This branch is *mostly* just an optimization: when we don't
481                 // have escaping bound vars, we don't need to replace them with
482                 // placeholders (see branch below). *Also*, we know that we can
483                 // register an obligation to *later* project, since we know
484                 // there won't be bound vars there.
485
486                 let data = data.super_fold_with(self);
487                 let normalized_ty = if self.eager_inference_replacement {
488                     normalize_projection_type(
489                         self.selcx,
490                         self.param_env,
491                         data,
492                         self.cause.clone(),
493                         self.depth,
494                         &mut self.obligations,
495                     )
496                 } else {
497                     opt_normalize_projection_type(
498                         self.selcx,
499                         self.param_env,
500                         data,
501                         self.cause.clone(),
502                         self.depth,
503                         &mut self.obligations,
504                     )
505                     .ok()
506                     .flatten()
507                     .unwrap_or_else(|| ty::Term::Ty(ty.super_fold_with(self)))
508                 };
509                 debug!(
510                     ?self.depth,
511                     ?ty,
512                     ?normalized_ty,
513                     obligations.len = ?self.obligations.len(),
514                     "AssocTypeNormalizer: normalized type"
515                 );
516                 normalized_ty.ty().unwrap()
517             }
518
519             ty::Projection(data) => {
520                 // If there are escaping bound vars, we temporarily replace the
521                 // bound vars with placeholders. Note though, that in the case
522                 // that we still can't project for whatever reason (e.g. self
523                 // type isn't known enough), we *can't* register an obligation
524                 // and return an inference variable (since then that obligation
525                 // would have bound vars and that's a can of worms). Instead,
526                 // we just give up and fall back to pretending like we never tried!
527                 //
528                 // Note: this isn't necessarily the final approach here; we may
529                 // want to figure out how to register obligations with escaping vars
530                 // or handle this some other way.
531
532                 let infcx = self.selcx.infcx();
533                 let (data, mapped_regions, mapped_types, mapped_consts) =
534                     BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, data);
535                 let data = data.super_fold_with(self);
536                 let normalized_ty = opt_normalize_projection_type(
537                     self.selcx,
538                     self.param_env,
539                     data,
540                     self.cause.clone(),
541                     self.depth,
542                     &mut self.obligations,
543                 )
544                 .ok()
545                 .flatten()
546                 .map(|term| term.ty().unwrap())
547                 .map(|normalized_ty| {
548                     PlaceholderReplacer::replace_placeholders(
549                         infcx,
550                         mapped_regions,
551                         mapped_types,
552                         mapped_consts,
553                         &self.universes,
554                         normalized_ty,
555                     )
556                 })
557                 .unwrap_or_else(|| ty.super_fold_with(self));
558
559                 debug!(
560                     ?self.depth,
561                     ?ty,
562                     ?normalized_ty,
563                     obligations.len = ?self.obligations.len(),
564                     "AssocTypeNormalizer: normalized type"
565                 );
566                 normalized_ty
567             }
568
569             _ => ty.super_fold_with(self),
570         }
571     }
572
573     fn fold_const(&mut self, constant: ty::Const<'tcx>) -> ty::Const<'tcx> {
574         if self.selcx.tcx().lazy_normalization() || !self.eager_inference_replacement {
575             constant
576         } else {
577             let constant = constant.super_fold_with(self);
578             constant.eval(self.selcx.tcx(), self.param_env)
579         }
580     }
581 }
582
583 pub struct BoundVarReplacer<'me, 'tcx> {
584     infcx: &'me InferCtxt<'me, 'tcx>,
585     // These three maps track the bound variable that were replaced by placeholders. It might be
586     // nice to remove these since we already have the `kind` in the placeholder; we really just need
587     // the `var` (but we *could* bring that into scope if we were to track them as we pass them).
588     mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
589     mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
590     mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
591     // The current depth relative to *this* folding, *not* the entire normalization. In other words,
592     // the depth of binders we've passed here.
593     current_index: ty::DebruijnIndex,
594     // The `UniverseIndex` of the binding levels above us. These are optional, since we are lazy:
595     // we don't actually create a universe until we see a bound var we have to replace.
596     universe_indices: &'me mut Vec<Option<ty::UniverseIndex>>,
597 }
598
599 impl<'me, 'tcx> BoundVarReplacer<'me, 'tcx> {
600     /// Returns `Some` if we *were* able to replace bound vars. If there are any bound vars that
601     /// use a binding level above `universe_indices.len()`, we fail.
602     pub fn replace_bound_vars<T: TypeFoldable<'tcx>>(
603         infcx: &'me InferCtxt<'me, 'tcx>,
604         universe_indices: &'me mut Vec<Option<ty::UniverseIndex>>,
605         value: T,
606     ) -> (
607         T,
608         BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
609         BTreeMap<ty::PlaceholderType, ty::BoundTy>,
610         BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
611     ) {
612         let mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion> = BTreeMap::new();
613         let mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy> = BTreeMap::new();
614         let mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar> = BTreeMap::new();
615
616         let mut replacer = BoundVarReplacer {
617             infcx,
618             mapped_regions,
619             mapped_types,
620             mapped_consts,
621             current_index: ty::INNERMOST,
622             universe_indices,
623         };
624
625         let value = value.super_fold_with(&mut replacer);
626
627         (value, replacer.mapped_regions, replacer.mapped_types, replacer.mapped_consts)
628     }
629
630     fn universe_for(&mut self, debruijn: ty::DebruijnIndex) -> ty::UniverseIndex {
631         let infcx = self.infcx;
632         let index =
633             self.universe_indices.len() + self.current_index.as_usize() - debruijn.as_usize() - 1;
634         let universe = self.universe_indices[index].unwrap_or_else(|| {
635             for i in self.universe_indices.iter_mut().take(index + 1) {
636                 *i = i.or_else(|| Some(infcx.create_next_universe()))
637             }
638             self.universe_indices[index].unwrap()
639         });
640         universe
641     }
642 }
643
644 impl<'tcx> TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
645     fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
646         self.infcx.tcx
647     }
648
649     fn fold_binder<T: TypeFoldable<'tcx>>(
650         &mut self,
651         t: ty::Binder<'tcx, T>,
652     ) -> ty::Binder<'tcx, T> {
653         self.current_index.shift_in(1);
654         let t = t.super_fold_with(self);
655         self.current_index.shift_out(1);
656         t
657     }
658
659     fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
660         match *r {
661             ty::ReLateBound(debruijn, _)
662                 if debruijn.as_usize() + 1
663                     > self.current_index.as_usize() + self.universe_indices.len() =>
664             {
665                 bug!("Bound vars outside of `self.universe_indices`");
666             }
667             ty::ReLateBound(debruijn, br) if debruijn >= self.current_index => {
668                 let universe = self.universe_for(debruijn);
669                 let p = ty::PlaceholderRegion { universe, name: br.kind };
670                 self.mapped_regions.insert(p, br);
671                 self.infcx.tcx.mk_region(ty::RePlaceholder(p))
672             }
673             _ => r,
674         }
675     }
676
677     fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
678         match *t.kind() {
679             ty::Bound(debruijn, _)
680                 if debruijn.as_usize() + 1
681                     > self.current_index.as_usize() + self.universe_indices.len() =>
682             {
683                 bug!("Bound vars outside of `self.universe_indices`");
684             }
685             ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => {
686                 let universe = self.universe_for(debruijn);
687                 let p = ty::PlaceholderType { universe, name: bound_ty.var };
688                 self.mapped_types.insert(p, bound_ty);
689                 self.infcx.tcx.mk_ty(ty::Placeholder(p))
690             }
691             _ if t.has_vars_bound_at_or_above(self.current_index) => t.super_fold_with(self),
692             _ => t,
693         }
694     }
695
696     fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
697         match ct.val() {
698             ty::ConstKind::Bound(debruijn, _)
699                 if debruijn.as_usize() + 1
700                     > self.current_index.as_usize() + self.universe_indices.len() =>
701             {
702                 bug!("Bound vars outside of `self.universe_indices`");
703             }
704             ty::ConstKind::Bound(debruijn, bound_const) if debruijn >= self.current_index => {
705                 let universe = self.universe_for(debruijn);
706                 let p = ty::PlaceholderConst {
707                     universe,
708                     name: ty::BoundConst { var: bound_const, ty: ct.ty() },
709                 };
710                 self.mapped_consts.insert(p, bound_const);
711                 self.infcx
712                     .tcx
713                     .mk_const(ty::ConstS { val: ty::ConstKind::Placeholder(p), ty: ct.ty() })
714             }
715             _ if ct.has_vars_bound_at_or_above(self.current_index) => ct.super_fold_with(self),
716             _ => ct,
717         }
718     }
719 }
720
721 // The inverse of `BoundVarReplacer`: replaces placeholders with the bound vars from which they came.
722 pub struct PlaceholderReplacer<'me, 'tcx> {
723     infcx: &'me InferCtxt<'me, 'tcx>,
724     mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
725     mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
726     mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
727     universe_indices: &'me Vec<Option<ty::UniverseIndex>>,
728     current_index: ty::DebruijnIndex,
729 }
730
731 impl<'me, 'tcx> PlaceholderReplacer<'me, 'tcx> {
732     pub fn replace_placeholders<T: TypeFoldable<'tcx>>(
733         infcx: &'me InferCtxt<'me, 'tcx>,
734         mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
735         mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy>,
736         mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
737         universe_indices: &'me Vec<Option<ty::UniverseIndex>>,
738         value: T,
739     ) -> T {
740         let mut replacer = PlaceholderReplacer {
741             infcx,
742             mapped_regions,
743             mapped_types,
744             mapped_consts,
745             universe_indices,
746             current_index: ty::INNERMOST,
747         };
748         value.super_fold_with(&mut replacer)
749     }
750 }
751
752 impl<'tcx> TypeFolder<'tcx> for PlaceholderReplacer<'_, 'tcx> {
753     fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
754         self.infcx.tcx
755     }
756
757     fn fold_binder<T: TypeFoldable<'tcx>>(
758         &mut self,
759         t: ty::Binder<'tcx, T>,
760     ) -> ty::Binder<'tcx, T> {
761         if !t.has_placeholders() && !t.has_infer_regions() {
762             return t;
763         }
764         self.current_index.shift_in(1);
765         let t = t.super_fold_with(self);
766         self.current_index.shift_out(1);
767         t
768     }
769
770     fn fold_region(&mut self, r0: ty::Region<'tcx>) -> ty::Region<'tcx> {
771         let r1 = match *r0 {
772             ty::ReVar(_) => self
773                 .infcx
774                 .inner
775                 .borrow_mut()
776                 .unwrap_region_constraints()
777                 .opportunistic_resolve_region(self.infcx.tcx, r0),
778             _ => r0,
779         };
780
781         let r2 = match *r1 {
782             ty::RePlaceholder(p) => {
783                 let replace_var = self.mapped_regions.get(&p);
784                 match replace_var {
785                     Some(replace_var) => {
786                         let index = self
787                             .universe_indices
788                             .iter()
789                             .position(|u| matches!(u, Some(pu) if *pu == p.universe))
790                             .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
791                         let db = ty::DebruijnIndex::from_usize(
792                             self.universe_indices.len() - index + self.current_index.as_usize() - 1,
793                         );
794                         self.tcx().mk_region(ty::ReLateBound(db, *replace_var))
795                     }
796                     None => r1,
797                 }
798             }
799             _ => r1,
800         };
801
802         debug!(?r0, ?r1, ?r2, "fold_region");
803
804         r2
805     }
806
807     fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
808         match *ty.kind() {
809             ty::Placeholder(p) => {
810                 let replace_var = self.mapped_types.get(&p);
811                 match replace_var {
812                     Some(replace_var) => {
813                         let index = self
814                             .universe_indices
815                             .iter()
816                             .position(|u| matches!(u, Some(pu) if *pu == p.universe))
817                             .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
818                         let db = ty::DebruijnIndex::from_usize(
819                             self.universe_indices.len() - index + self.current_index.as_usize() - 1,
820                         );
821                         self.tcx().mk_ty(ty::Bound(db, *replace_var))
822                     }
823                     None => ty,
824                 }
825             }
826
827             _ if ty.has_placeholders() || ty.has_infer_regions() => ty.super_fold_with(self),
828             _ => ty,
829         }
830     }
831
832     fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
833         if let ty::ConstKind::Placeholder(p) = ct.val() {
834             let replace_var = self.mapped_consts.get(&p);
835             match replace_var {
836                 Some(replace_var) => {
837                     let index = self
838                         .universe_indices
839                         .iter()
840                         .position(|u| matches!(u, Some(pu) if *pu == p.universe))
841                         .unwrap_or_else(|| bug!("Unexpected placeholder universe."));
842                     let db = ty::DebruijnIndex::from_usize(
843                         self.universe_indices.len() - index + self.current_index.as_usize() - 1,
844                     );
845                     self.tcx().mk_const(ty::ConstS {
846                         val: ty::ConstKind::Bound(db, *replace_var),
847                         ty: ct.ty(),
848                     })
849                 }
850                 None => ct,
851             }
852         } else {
853             ct.super_fold_with(self)
854         }
855     }
856 }
857
858 /// The guts of `normalize`: normalize a specific projection like `<T
859 /// as Trait>::Item`. The result is always a type (and possibly
860 /// additional obligations). If ambiguity arises, which implies that
861 /// there are unresolved type variables in the projection, we will
862 /// substitute a fresh type variable `$X` and generate a new
863 /// obligation `<T as Trait>::Item == $X` for later.
864 pub fn normalize_projection_type<'a, 'b, 'tcx>(
865     selcx: &'a mut SelectionContext<'b, 'tcx>,
866     param_env: ty::ParamEnv<'tcx>,
867     projection_ty: ty::ProjectionTy<'tcx>,
868     cause: ObligationCause<'tcx>,
869     depth: usize,
870     obligations: &mut Vec<PredicateObligation<'tcx>>,
871 ) -> Term<'tcx> {
872     opt_normalize_projection_type(
873         selcx,
874         param_env,
875         projection_ty,
876         cause.clone(),
877         depth,
878         obligations,
879     )
880     .ok()
881     .flatten()
882     .unwrap_or_else(move || {
883         // if we bottom out in ambiguity, create a type variable
884         // and a deferred predicate to resolve this when more type
885         // information is available.
886
887         selcx
888             .infcx()
889             .infer_projection(param_env, projection_ty, cause, depth + 1, obligations)
890             .into()
891     })
892 }
893
894 /// The guts of `normalize`: normalize a specific projection like `<T
895 /// as Trait>::Item`. The result is always a type (and possibly
896 /// additional obligations). Returns `None` in the case of ambiguity,
897 /// which indicates that there are unbound type variables.
898 ///
899 /// This function used to return `Option<NormalizedTy<'tcx>>`, which contains a
900 /// `Ty<'tcx>` and an obligations vector. But that obligation vector was very
901 /// often immediately appended to another obligations vector. So now this
902 /// function takes an obligations vector and appends to it directly, which is
903 /// slightly uglier but avoids the need for an extra short-lived allocation.
904 #[instrument(level = "debug", skip(selcx, param_env, cause, obligations))]
905 fn opt_normalize_projection_type<'a, 'b, 'tcx>(
906     selcx: &'a mut SelectionContext<'b, 'tcx>,
907     param_env: ty::ParamEnv<'tcx>,
908     projection_ty: ty::ProjectionTy<'tcx>,
909     cause: ObligationCause<'tcx>,
910     depth: usize,
911     obligations: &mut Vec<PredicateObligation<'tcx>>,
912 ) -> Result<Option<Term<'tcx>>, InProgress> {
913     let infcx = selcx.infcx();
914     // Don't use the projection cache in intercrate mode -
915     // the `infcx` may be re-used between intercrate in non-intercrate
916     // mode, which could lead to using incorrect cache results.
917     let use_cache = !selcx.is_intercrate();
918
919     let projection_ty = infcx.resolve_vars_if_possible(projection_ty);
920     let cache_key = ProjectionCacheKey::new(projection_ty);
921
922     // FIXME(#20304) For now, I am caching here, which is good, but it
923     // means we don't capture the type variables that are created in
924     // the case of ambiguity. Which means we may create a large stream
925     // of such variables. OTOH, if we move the caching up a level, we
926     // would not benefit from caching when proving `T: Trait<U=Foo>`
927     // bounds. It might be the case that we want two distinct caches,
928     // or else another kind of cache entry.
929
930     let cache_result = if use_cache {
931         infcx.inner.borrow_mut().projection_cache().try_start(cache_key)
932     } else {
933         Ok(())
934     };
935     match cache_result {
936         Ok(()) => debug!("no cache"),
937         Err(ProjectionCacheEntry::Ambiguous) => {
938             // If we found ambiguity the last time, that means we will continue
939             // to do so until some type in the key changes (and we know it
940             // hasn't, because we just fully resolved it).
941             debug!("found cache entry: ambiguous");
942             return Ok(None);
943         }
944         Err(ProjectionCacheEntry::InProgress) => {
945             // Under lazy normalization, this can arise when
946             // bootstrapping.  That is, imagine an environment with a
947             // where-clause like `A::B == u32`. Now, if we are asked
948             // to normalize `A::B`, we will want to check the
949             // where-clauses in scope. So we will try to unify `A::B`
950             // with `A::B`, which can trigger a recursive
951             // normalization.
952
953             debug!("found cache entry: in-progress");
954
955             // Cache that normalizing this projection resulted in a cycle. This
956             // should ensure that, unless this happens within a snapshot that's
957             // rolled back, fulfillment or evaluation will notice the cycle.
958
959             if use_cache {
960                 infcx.inner.borrow_mut().projection_cache().recur(cache_key);
961             }
962             return Err(InProgress);
963         }
964         Err(ProjectionCacheEntry::Recur) => {
965             debug!("recur cache");
966             return Err(InProgress);
967         }
968         Err(ProjectionCacheEntry::NormalizedTy { ty, complete: _ }) => {
969             // This is the hottest path in this function.
970             //
971             // If we find the value in the cache, then return it along
972             // with the obligations that went along with it. Note
973             // that, when using a fulfillment context, these
974             // obligations could in principle be ignored: they have
975             // already been registered when the cache entry was
976             // created (and hence the new ones will quickly be
977             // discarded as duplicated). But when doing trait
978             // evaluation this is not the case, and dropping the trait
979             // evaluations can causes ICEs (e.g., #43132).
980             debug!(?ty, "found normalized ty");
981             obligations.extend(ty.obligations);
982             return Ok(Some(ty.value));
983         }
984         Err(ProjectionCacheEntry::Error) => {
985             debug!("opt_normalize_projection_type: found error");
986             let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth);
987             obligations.extend(result.obligations);
988             return Ok(Some(result.value.into()));
989         }
990     }
991
992     let obligation = Obligation::with_depth(cause.clone(), depth, param_env, projection_ty);
993
994     match project(selcx, &obligation) {
995         Ok(Projected::Progress(Progress {
996             term: projected_term,
997             obligations: mut projected_obligations,
998         })) => {
999             // if projection succeeded, then what we get out of this
1000             // is also non-normalized (consider: it was derived from
1001             // an impl, where-clause etc) and hence we must
1002             // re-normalize it
1003
1004             let projected_term = selcx.infcx().resolve_vars_if_possible(projected_term);
1005
1006             let mut result = if projected_term.has_projections() {
1007                 let mut normalizer = AssocTypeNormalizer::new(
1008                     selcx,
1009                     param_env,
1010                     cause,
1011                     depth + 1,
1012                     &mut projected_obligations,
1013                 );
1014                 let normalized_ty = normalizer.fold(projected_term);
1015
1016                 Normalized { value: normalized_ty, obligations: projected_obligations }
1017             } else {
1018                 Normalized { value: projected_term, obligations: projected_obligations }
1019             };
1020
1021             let mut deduped: SsoHashSet<_> = Default::default();
1022             result.obligations.drain_filter(|projected_obligation| {
1023                 if !deduped.insert(projected_obligation.clone()) {
1024                     return true;
1025                 }
1026                 false
1027             });
1028
1029             if use_cache {
1030                 infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
1031             }
1032             obligations.extend(result.obligations);
1033             Ok(Some(result.value))
1034         }
1035         Ok(Projected::NoProgress(projected_ty)) => {
1036             let result = Normalized { value: projected_ty, obligations: vec![] };
1037             if use_cache {
1038                 infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
1039             }
1040             // No need to extend `obligations`.
1041             Ok(Some(result.value))
1042         }
1043         Err(ProjectionError::TooManyCandidates) => {
1044             debug!("opt_normalize_projection_type: too many candidates");
1045             if use_cache {
1046                 infcx.inner.borrow_mut().projection_cache().ambiguous(cache_key);
1047             }
1048             Ok(None)
1049         }
1050         Err(ProjectionError::TraitSelectionError(_)) => {
1051             debug!("opt_normalize_projection_type: ERROR");
1052             // if we got an error processing the `T as Trait` part,
1053             // just return `ty::err` but add the obligation `T :
1054             // Trait`, which when processed will cause the error to be
1055             // reported later
1056
1057             if use_cache {
1058                 infcx.inner.borrow_mut().projection_cache().error(cache_key);
1059             }
1060             let result = normalize_to_error(selcx, param_env, projection_ty, cause, depth);
1061             obligations.extend(result.obligations);
1062             Ok(Some(result.value.into()))
1063         }
1064     }
1065 }
1066
1067 /// If we are projecting `<T as Trait>::Item`, but `T: Trait` does not
1068 /// hold. In various error cases, we cannot generate a valid
1069 /// normalized projection. Therefore, we create an inference variable
1070 /// return an associated obligation that, when fulfilled, will lead to
1071 /// an error.
1072 ///
1073 /// Note that we used to return `Error` here, but that was quite
1074 /// dubious -- the premise was that an error would *eventually* be
1075 /// reported, when the obligation was processed. But in general once
1076 /// you see an `Error` you are supposed to be able to assume that an
1077 /// error *has been* reported, so that you can take whatever heuristic
1078 /// paths you want to take. To make things worse, it was possible for
1079 /// cycles to arise, where you basically had a setup like `<MyType<$0>
1080 /// as Trait>::Foo == $0`. Here, normalizing `<MyType<$0> as
1081 /// Trait>::Foo> to `[type error]` would lead to an obligation of
1082 /// `<MyType<[type error]> as Trait>::Foo`. We are supposed to report
1083 /// an error for this obligation, but we legitimately should not,
1084 /// because it contains `[type error]`. Yuck! (See issue #29857 for
1085 /// one case where this arose.)
1086 fn normalize_to_error<'a, 'tcx>(
1087     selcx: &mut SelectionContext<'a, 'tcx>,
1088     param_env: ty::ParamEnv<'tcx>,
1089     projection_ty: ty::ProjectionTy<'tcx>,
1090     cause: ObligationCause<'tcx>,
1091     depth: usize,
1092 ) -> NormalizedTy<'tcx> {
1093     let trait_ref = ty::Binder::dummy(projection_ty.trait_ref(selcx.tcx()));
1094     let trait_obligation = Obligation {
1095         cause,
1096         recursion_depth: depth,
1097         param_env,
1098         predicate: trait_ref.without_const().to_predicate(selcx.tcx()),
1099     };
1100     let tcx = selcx.infcx().tcx;
1101     let def_id = projection_ty.item_def_id;
1102     let new_value = selcx.infcx().next_ty_var(TypeVariableOrigin {
1103         kind: TypeVariableOriginKind::NormalizeProjectionType,
1104         span: tcx.def_span(def_id),
1105     });
1106     Normalized { value: new_value, obligations: vec![trait_obligation] }
1107 }
1108
1109 enum Projected<'tcx> {
1110     Progress(Progress<'tcx>),
1111     NoProgress(ty::Term<'tcx>),
1112 }
1113
1114 struct Progress<'tcx> {
1115     term: ty::Term<'tcx>,
1116     obligations: Vec<PredicateObligation<'tcx>>,
1117 }
1118
1119 impl<'tcx> Progress<'tcx> {
1120     fn error(tcx: TyCtxt<'tcx>) -> Self {
1121         Progress { term: tcx.ty_error().into(), obligations: vec![] }
1122     }
1123
1124     fn with_addl_obligations(mut self, mut obligations: Vec<PredicateObligation<'tcx>>) -> Self {
1125         self.obligations.append(&mut obligations);
1126         self
1127     }
1128 }
1129
1130 /// Computes the result of a projection type (if we can).
1131 ///
1132 /// IMPORTANT:
1133 /// - `obligation` must be fully normalized
1134 #[tracing::instrument(level = "info", skip(selcx))]
1135 fn project<'cx, 'tcx>(
1136     selcx: &mut SelectionContext<'cx, 'tcx>,
1137     obligation: &ProjectionTyObligation<'tcx>,
1138 ) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
1139     if !selcx.tcx().recursion_limit().value_within_limit(obligation.recursion_depth) {
1140         // This should really be an immediate error, but some existing code
1141         // relies on being able to recover from this.
1142         return Err(ProjectionError::TraitSelectionError(SelectionError::Overflow));
1143     }
1144
1145     if obligation.predicate.references_error() {
1146         return Ok(Projected::Progress(Progress::error(selcx.tcx())));
1147     }
1148
1149     let mut candidates = ProjectionCandidateSet::None;
1150
1151     // Make sure that the following procedures are kept in order. ParamEnv
1152     // needs to be first because it has highest priority, and Select checks
1153     // the return value of push_candidate which assumes it's ran at last.
1154     assemble_candidates_from_param_env(selcx, obligation, &mut candidates);
1155
1156     assemble_candidates_from_trait_def(selcx, obligation, &mut candidates);
1157
1158     assemble_candidates_from_object_ty(selcx, obligation, &mut candidates);
1159
1160     if let ProjectionCandidateSet::Single(ProjectionCandidate::Object(_)) = candidates {
1161         // Avoid normalization cycle from selection (see
1162         // `assemble_candidates_from_object_ty`).
1163         // FIXME(lazy_normalization): Lazy normalization should save us from
1164         // having to special case this.
1165     } else {
1166         assemble_candidates_from_impls(selcx, obligation, &mut candidates);
1167     };
1168
1169     match candidates {
1170         ProjectionCandidateSet::Single(candidate) => {
1171             Ok(Projected::Progress(confirm_candidate(selcx, obligation, candidate)))
1172         }
1173         ProjectionCandidateSet::None => Ok(Projected::NoProgress(
1174             // FIXME(associated_const_generics): this may need to change in the future?
1175             // need to investigate whether or not this is fine.
1176             selcx
1177                 .tcx()
1178                 .mk_projection(obligation.predicate.item_def_id, obligation.predicate.substs)
1179                 .into(),
1180         )),
1181         // Error occurred while trying to processing impls.
1182         ProjectionCandidateSet::Error(e) => Err(ProjectionError::TraitSelectionError(e)),
1183         // Inherent ambiguity that prevents us from even enumerating the
1184         // candidates.
1185         ProjectionCandidateSet::Ambiguous => Err(ProjectionError::TooManyCandidates),
1186     }
1187 }
1188
1189 /// The first thing we have to do is scan through the parameter
1190 /// environment to see whether there are any projection predicates
1191 /// there that can answer this question.
1192 fn assemble_candidates_from_param_env<'cx, 'tcx>(
1193     selcx: &mut SelectionContext<'cx, 'tcx>,
1194     obligation: &ProjectionTyObligation<'tcx>,
1195     candidate_set: &mut ProjectionCandidateSet<'tcx>,
1196 ) {
1197     assemble_candidates_from_predicates(
1198         selcx,
1199         obligation,
1200         candidate_set,
1201         ProjectionCandidate::ParamEnv,
1202         obligation.param_env.caller_bounds().iter(),
1203         false,
1204     );
1205 }
1206
1207 /// In the case of a nested projection like <<A as Foo>::FooT as Bar>::BarT, we may find
1208 /// that the definition of `Foo` has some clues:
1209 ///
1210 /// ```
1211 /// trait Foo {
1212 ///     type FooT : Bar<BarT=i32>
1213 /// }
1214 /// ```
1215 ///
1216 /// Here, for example, we could conclude that the result is `i32`.
1217 fn assemble_candidates_from_trait_def<'cx, 'tcx>(
1218     selcx: &mut SelectionContext<'cx, 'tcx>,
1219     obligation: &ProjectionTyObligation<'tcx>,
1220     candidate_set: &mut ProjectionCandidateSet<'tcx>,
1221 ) {
1222     debug!("assemble_candidates_from_trait_def(..)");
1223
1224     let tcx = selcx.tcx();
1225     // Check whether the self-type is itself a projection.
1226     // If so, extract what we know from the trait and try to come up with a good answer.
1227     let bounds = match *obligation.predicate.self_ty().kind() {
1228         ty::Projection(ref data) => tcx.item_bounds(data.item_def_id).subst(tcx, data.substs),
1229         ty::Opaque(def_id, substs) => tcx.item_bounds(def_id).subst(tcx, substs),
1230         ty::Infer(ty::TyVar(_)) => {
1231             // If the self-type is an inference variable, then it MAY wind up
1232             // being a projected type, so induce an ambiguity.
1233             candidate_set.mark_ambiguous();
1234             return;
1235         }
1236         _ => return,
1237     };
1238
1239     assemble_candidates_from_predicates(
1240         selcx,
1241         obligation,
1242         candidate_set,
1243         ProjectionCandidate::TraitDef,
1244         bounds.iter(),
1245         true,
1246     );
1247 }
1248
1249 /// In the case of a trait object like
1250 /// `<dyn Iterator<Item = ()> as Iterator>::Item` we can use the existential
1251 /// predicate in the trait object.
1252 ///
1253 /// We don't go through the select candidate for these bounds to avoid cycles:
1254 /// In the above case, `dyn Iterator<Item = ()>: Iterator` would create a
1255 /// nested obligation of `<dyn Iterator<Item = ()> as Iterator>::Item: Sized`,
1256 /// this then has to be normalized without having to prove
1257 /// `dyn Iterator<Item = ()>: Iterator` again.
1258 fn assemble_candidates_from_object_ty<'cx, 'tcx>(
1259     selcx: &mut SelectionContext<'cx, 'tcx>,
1260     obligation: &ProjectionTyObligation<'tcx>,
1261     candidate_set: &mut ProjectionCandidateSet<'tcx>,
1262 ) {
1263     debug!("assemble_candidates_from_object_ty(..)");
1264
1265     let tcx = selcx.tcx();
1266
1267     let self_ty = obligation.predicate.self_ty();
1268     let object_ty = selcx.infcx().shallow_resolve(self_ty);
1269     let data = match object_ty.kind() {
1270         ty::Dynamic(data, ..) => data,
1271         ty::Infer(ty::TyVar(_)) => {
1272             // If the self-type is an inference variable, then it MAY wind up
1273             // being an object type, so induce an ambiguity.
1274             candidate_set.mark_ambiguous();
1275             return;
1276         }
1277         _ => return,
1278     };
1279     let env_predicates = data
1280         .projection_bounds()
1281         .filter(|bound| bound.item_def_id() == obligation.predicate.item_def_id)
1282         .map(|p| p.with_self_ty(tcx, object_ty).to_predicate(tcx));
1283
1284     assemble_candidates_from_predicates(
1285         selcx,
1286         obligation,
1287         candidate_set,
1288         ProjectionCandidate::Object,
1289         env_predicates,
1290         false,
1291     );
1292 }
1293
1294 #[tracing::instrument(
1295     level = "debug",
1296     skip(selcx, candidate_set, ctor, env_predicates, potentially_unnormalized_candidates)
1297 )]
1298 fn assemble_candidates_from_predicates<'cx, 'tcx>(
1299     selcx: &mut SelectionContext<'cx, 'tcx>,
1300     obligation: &ProjectionTyObligation<'tcx>,
1301     candidate_set: &mut ProjectionCandidateSet<'tcx>,
1302     ctor: fn(ty::PolyProjectionPredicate<'tcx>) -> ProjectionCandidate<'tcx>,
1303     env_predicates: impl Iterator<Item = ty::Predicate<'tcx>>,
1304     potentially_unnormalized_candidates: bool,
1305 ) {
1306     let infcx = selcx.infcx();
1307     for predicate in env_predicates {
1308         let bound_predicate = predicate.kind();
1309         if let ty::PredicateKind::Projection(data) = predicate.kind().skip_binder() {
1310             let data = bound_predicate.rebind(data);
1311             if data.projection_def_id() != obligation.predicate.item_def_id {
1312                 continue;
1313             }
1314
1315             let is_match = infcx.probe(|_| {
1316                 selcx.match_projection_projections(
1317                     obligation,
1318                     data,
1319                     potentially_unnormalized_candidates,
1320                 )
1321             });
1322
1323             match is_match {
1324                 ProjectionMatchesProjection::Yes => {
1325                     candidate_set.push_candidate(ctor(data));
1326
1327                     if potentially_unnormalized_candidates
1328                         && !obligation.predicate.has_infer_types_or_consts()
1329                     {
1330                         // HACK: Pick the first trait def candidate for a fully
1331                         // inferred predicate. This is to allow duplicates that
1332                         // differ only in normalization.
1333                         return;
1334                     }
1335                 }
1336                 ProjectionMatchesProjection::Ambiguous => {
1337                     candidate_set.mark_ambiguous();
1338                 }
1339                 ProjectionMatchesProjection::No => {}
1340             }
1341         }
1342     }
1343 }
1344
1345 #[tracing::instrument(level = "debug", skip(selcx, obligation, candidate_set))]
1346 fn assemble_candidates_from_impls<'cx, 'tcx>(
1347     selcx: &mut SelectionContext<'cx, 'tcx>,
1348     obligation: &ProjectionTyObligation<'tcx>,
1349     candidate_set: &mut ProjectionCandidateSet<'tcx>,
1350 ) {
1351     // If we are resolving `<T as TraitRef<...>>::Item == Type`,
1352     // start out by selecting the predicate `T as TraitRef<...>`:
1353     let poly_trait_ref = ty::Binder::dummy(obligation.predicate.trait_ref(selcx.tcx()));
1354     let trait_obligation = obligation.with(poly_trait_ref.to_poly_trait_predicate());
1355     let _ = selcx.infcx().commit_if_ok(|_| {
1356         let impl_source = match selcx.select(&trait_obligation) {
1357             Ok(Some(impl_source)) => impl_source,
1358             Ok(None) => {
1359                 candidate_set.mark_ambiguous();
1360                 return Err(());
1361             }
1362             Err(e) => {
1363                 debug!(error = ?e, "selection error");
1364                 candidate_set.mark_error(e);
1365                 return Err(());
1366             }
1367         };
1368
1369         let eligible = match &impl_source {
1370             super::ImplSource::Closure(_)
1371             | super::ImplSource::Generator(_)
1372             | super::ImplSource::FnPointer(_)
1373             | super::ImplSource::TraitAlias(_) => true,
1374             super::ImplSource::UserDefined(impl_data) => {
1375                 // We have to be careful when projecting out of an
1376                 // impl because of specialization. If we are not in
1377                 // codegen (i.e., projection mode is not "any"), and the
1378                 // impl's type is declared as default, then we disable
1379                 // projection (even if the trait ref is fully
1380                 // monomorphic). In the case where trait ref is not
1381                 // fully monomorphic (i.e., includes type parameters),
1382                 // this is because those type parameters may
1383                 // ultimately be bound to types from other crates that
1384                 // may have specialized impls we can't see. In the
1385                 // case where the trait ref IS fully monomorphic, this
1386                 // is a policy decision that we made in the RFC in
1387                 // order to preserve flexibility for the crate that
1388                 // defined the specializable impl to specialize later
1389                 // for existing types.
1390                 //
1391                 // In either case, we handle this by not adding a
1392                 // candidate for an impl if it contains a `default`
1393                 // type.
1394                 //
1395                 // NOTE: This should be kept in sync with the similar code in
1396                 // `rustc_ty_utils::instance::resolve_associated_item()`.
1397                 let node_item =
1398                     assoc_def(selcx, impl_data.impl_def_id, obligation.predicate.item_def_id)
1399                         .map_err(|ErrorGuaranteed { .. }| ())?;
1400
1401                 if node_item.is_final() {
1402                     // Non-specializable items are always projectable.
1403                     true
1404                 } else {
1405                     // Only reveal a specializable default if we're past type-checking
1406                     // and the obligation is monomorphic, otherwise passes such as
1407                     // transmute checking and polymorphic MIR optimizations could
1408                     // get a result which isn't correct for all monomorphizations.
1409                     if obligation.param_env.reveal() == Reveal::All {
1410                         // NOTE(eddyb) inference variables can resolve to parameters, so
1411                         // assume `poly_trait_ref` isn't monomorphic, if it contains any.
1412                         let poly_trait_ref = selcx.infcx().resolve_vars_if_possible(poly_trait_ref);
1413                         !poly_trait_ref.still_further_specializable()
1414                     } else {
1415                         debug!(
1416                             assoc_ty = ?selcx.tcx().def_path_str(node_item.item.def_id),
1417                             ?obligation.predicate,
1418                             "assemble_candidates_from_impls: not eligible due to default",
1419                         );
1420                         false
1421                     }
1422                 }
1423             }
1424             super::ImplSource::DiscriminantKind(..) => {
1425                 // While `DiscriminantKind` is automatically implemented for every type,
1426                 // the concrete discriminant may not be known yet.
1427                 //
1428                 // Any type with multiple potential discriminant types is therefore not eligible.
1429                 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1430
1431                 match self_ty.kind() {
1432                     ty::Bool
1433                     | ty::Char
1434                     | ty::Int(_)
1435                     | ty::Uint(_)
1436                     | ty::Float(_)
1437                     | ty::Adt(..)
1438                     | ty::Foreign(_)
1439                     | ty::Str
1440                     | ty::Array(..)
1441                     | ty::Slice(_)
1442                     | ty::RawPtr(..)
1443                     | ty::Ref(..)
1444                     | ty::FnDef(..)
1445                     | ty::FnPtr(..)
1446                     | ty::Dynamic(..)
1447                     | ty::Closure(..)
1448                     | ty::Generator(..)
1449                     | ty::GeneratorWitness(..)
1450                     | ty::Never
1451                     | ty::Tuple(..)
1452                     // Integers and floats always have `u8` as their discriminant.
1453                     | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1454
1455                     ty::Projection(..)
1456                     | ty::Opaque(..)
1457                     | ty::Param(..)
1458                     | ty::Bound(..)
1459                     | ty::Placeholder(..)
1460                     | ty::Infer(..)
1461                     | ty::Error(_) => false,
1462                 }
1463             }
1464             super::ImplSource::Pointee(..) => {
1465                 // While `Pointee` is automatically implemented for every type,
1466                 // the concrete metadata type may not be known yet.
1467                 //
1468                 // Any type with multiple potential metadata types is therefore not eligible.
1469                 let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1470
1471                 let tail = selcx.tcx().struct_tail_with_normalize(self_ty, |ty| {
1472                     // We throw away any obligations we get from this, since we normalize
1473                     // and confirm these obligations once again during confirmation
1474                     normalize_with_depth(
1475                         selcx,
1476                         obligation.param_env,
1477                         obligation.cause.clone(),
1478                         obligation.recursion_depth + 1,
1479                         ty,
1480                     )
1481                     .value
1482                 });
1483
1484                 match tail.kind() {
1485                     ty::Bool
1486                     | ty::Char
1487                     | ty::Int(_)
1488                     | ty::Uint(_)
1489                     | ty::Float(_)
1490                     | ty::Str
1491                     | ty::Array(..)
1492                     | ty::Slice(_)
1493                     | ty::RawPtr(..)
1494                     | ty::Ref(..)
1495                     | ty::FnDef(..)
1496                     | ty::FnPtr(..)
1497                     | ty::Dynamic(..)
1498                     | ty::Closure(..)
1499                     | ty::Generator(..)
1500                     | ty::GeneratorWitness(..)
1501                     | ty::Never
1502                     // Extern types have unit metadata, according to RFC 2850
1503                     | ty::Foreign(_)
1504                     // If returned by `struct_tail_without_normalization` this is a unit struct
1505                     // without any fields, or not a struct, and therefore is Sized.
1506                     | ty::Adt(..)
1507                     // If returned by `struct_tail_without_normalization` this is the empty tuple.
1508                     | ty::Tuple(..)
1509                     // Integers and floats are always Sized, and so have unit type metadata.
1510                     | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1511
1512                     // type parameters, opaques, and unnormalized projections have pointer
1513                     // metadata if they're known (e.g. by the param_env) to be sized
1514                     ty::Param(_) | ty::Projection(..) | ty::Opaque(..)
1515                         if tail.is_sized(selcx.tcx().at(obligation.cause.span), obligation.param_env) =>
1516                     {
1517                         true
1518                     }
1519
1520                     // FIXME(compiler-errors): are Bound and Placeholder types ever known sized?
1521                     ty::Param(_)
1522                     | ty::Projection(..)
1523                     | ty::Opaque(..)
1524                     | ty::Bound(..)
1525                     | ty::Placeholder(..)
1526                     | ty::Infer(..)
1527                     | ty::Error(_) => {
1528                         if tail.has_infer_types() {
1529                             candidate_set.mark_ambiguous();
1530                         }
1531                         false
1532                     }
1533                 }
1534             }
1535             super::ImplSource::Param(..) => {
1536                 // This case tell us nothing about the value of an
1537                 // associated type. Consider:
1538                 //
1539                 // ```
1540                 // trait SomeTrait { type Foo; }
1541                 // fn foo<T:SomeTrait>(...) { }
1542                 // ```
1543                 //
1544                 // If the user writes `<T as SomeTrait>::Foo`, then the `T
1545                 // : SomeTrait` binding does not help us decide what the
1546                 // type `Foo` is (at least, not more specifically than
1547                 // what we already knew).
1548                 //
1549                 // But wait, you say! What about an example like this:
1550                 //
1551                 // ```
1552                 // fn bar<T:SomeTrait<Foo=usize>>(...) { ... }
1553                 // ```
1554                 //
1555                 // Doesn't the `T : Sometrait<Foo=usize>` predicate help
1556                 // resolve `T::Foo`? And of course it does, but in fact
1557                 // that single predicate is desugared into two predicates
1558                 // in the compiler: a trait predicate (`T : SomeTrait`) and a
1559                 // projection. And the projection where clause is handled
1560                 // in `assemble_candidates_from_param_env`.
1561                 false
1562             }
1563             super::ImplSource::Object(_) => {
1564                 // Handled by the `Object` projection candidate. See
1565                 // `assemble_candidates_from_object_ty` for an explanation of
1566                 // why we special case object types.
1567                 false
1568             }
1569             super::ImplSource::AutoImpl(..)
1570             | super::ImplSource::Builtin(..)
1571             | super::ImplSource::TraitUpcasting(_)
1572             | super::ImplSource::ConstDrop(_) => {
1573                 // These traits have no associated types.
1574                 selcx.tcx().sess.delay_span_bug(
1575                     obligation.cause.span,
1576                     &format!("Cannot project an associated type from `{:?}`", impl_source),
1577                 );
1578                 return Err(());
1579             }
1580         };
1581
1582         if eligible {
1583             if candidate_set.push_candidate(ProjectionCandidate::Select(impl_source)) {
1584                 Ok(())
1585             } else {
1586                 Err(())
1587             }
1588         } else {
1589             Err(())
1590         }
1591     });
1592 }
1593
1594 fn confirm_candidate<'cx, 'tcx>(
1595     selcx: &mut SelectionContext<'cx, 'tcx>,
1596     obligation: &ProjectionTyObligation<'tcx>,
1597     candidate: ProjectionCandidate<'tcx>,
1598 ) -> Progress<'tcx> {
1599     debug!(?obligation, ?candidate, "confirm_candidate");
1600     let mut progress = match candidate {
1601         ProjectionCandidate::ParamEnv(poly_projection)
1602         | ProjectionCandidate::Object(poly_projection) => {
1603             confirm_param_env_candidate(selcx, obligation, poly_projection, false)
1604         }
1605
1606         ProjectionCandidate::TraitDef(poly_projection) => {
1607             confirm_param_env_candidate(selcx, obligation, poly_projection, true)
1608         }
1609
1610         ProjectionCandidate::Select(impl_source) => {
1611             confirm_select_candidate(selcx, obligation, impl_source)
1612         }
1613     };
1614
1615     // When checking for cycle during evaluation, we compare predicates with
1616     // "syntactic" equality. Since normalization generally introduces a type
1617     // with new region variables, we need to resolve them to existing variables
1618     // when possible for this to work. See `auto-trait-projection-recursion.rs`
1619     // for a case where this matters.
1620     if progress.term.has_infer_regions() {
1621         progress.term =
1622             progress.term.fold_with(&mut OpportunisticRegionResolver::new(selcx.infcx()));
1623     }
1624     progress
1625 }
1626
1627 fn confirm_select_candidate<'cx, 'tcx>(
1628     selcx: &mut SelectionContext<'cx, 'tcx>,
1629     obligation: &ProjectionTyObligation<'tcx>,
1630     impl_source: Selection<'tcx>,
1631 ) -> Progress<'tcx> {
1632     match impl_source {
1633         super::ImplSource::UserDefined(data) => confirm_impl_candidate(selcx, obligation, data),
1634         super::ImplSource::Generator(data) => confirm_generator_candidate(selcx, obligation, data),
1635         super::ImplSource::Closure(data) => confirm_closure_candidate(selcx, obligation, data),
1636         super::ImplSource::FnPointer(data) => confirm_fn_pointer_candidate(selcx, obligation, data),
1637         super::ImplSource::DiscriminantKind(data) => {
1638             confirm_discriminant_kind_candidate(selcx, obligation, data)
1639         }
1640         super::ImplSource::Pointee(data) => confirm_pointee_candidate(selcx, obligation, data),
1641         super::ImplSource::Object(_)
1642         | super::ImplSource::AutoImpl(..)
1643         | super::ImplSource::Param(..)
1644         | super::ImplSource::Builtin(..)
1645         | super::ImplSource::TraitUpcasting(_)
1646         | super::ImplSource::TraitAlias(..)
1647         | super::ImplSource::ConstDrop(_) => {
1648             // we don't create Select candidates with this kind of resolution
1649             span_bug!(
1650                 obligation.cause.span,
1651                 "Cannot project an associated type from `{:?}`",
1652                 impl_source
1653             )
1654         }
1655     }
1656 }
1657
1658 fn confirm_generator_candidate<'cx, 'tcx>(
1659     selcx: &mut SelectionContext<'cx, 'tcx>,
1660     obligation: &ProjectionTyObligation<'tcx>,
1661     impl_source: ImplSourceGeneratorData<'tcx, PredicateObligation<'tcx>>,
1662 ) -> Progress<'tcx> {
1663     let gen_sig = impl_source.substs.as_generator().poly_sig();
1664     let Normalized { value: gen_sig, obligations } = normalize_with_depth(
1665         selcx,
1666         obligation.param_env,
1667         obligation.cause.clone(),
1668         obligation.recursion_depth + 1,
1669         gen_sig,
1670     );
1671
1672     debug!(?obligation, ?gen_sig, ?obligations, "confirm_generator_candidate");
1673
1674     let tcx = selcx.tcx();
1675
1676     let gen_def_id = tcx.require_lang_item(LangItem::Generator, None);
1677
1678     let predicate = super::util::generator_trait_ref_and_outputs(
1679         tcx,
1680         gen_def_id,
1681         obligation.predicate.self_ty(),
1682         gen_sig,
1683     )
1684     .map_bound(|(trait_ref, yield_ty, return_ty)| {
1685         let name = tcx.associated_item(obligation.predicate.item_def_id).name;
1686         let ty = if name == sym::Return {
1687             return_ty
1688         } else if name == sym::Yield {
1689             yield_ty
1690         } else {
1691             bug!()
1692         };
1693
1694         ty::ProjectionPredicate {
1695             projection_ty: ty::ProjectionTy {
1696                 substs: trait_ref.substs,
1697                 item_def_id: obligation.predicate.item_def_id,
1698             },
1699             term: ty.into(),
1700         }
1701     });
1702
1703     confirm_param_env_candidate(selcx, obligation, predicate, false)
1704         .with_addl_obligations(impl_source.nested)
1705         .with_addl_obligations(obligations)
1706 }
1707
1708 fn confirm_discriminant_kind_candidate<'cx, 'tcx>(
1709     selcx: &mut SelectionContext<'cx, 'tcx>,
1710     obligation: &ProjectionTyObligation<'tcx>,
1711     _: ImplSourceDiscriminantKindData,
1712 ) -> Progress<'tcx> {
1713     let tcx = selcx.tcx();
1714
1715     let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1716     // We get here from `poly_project_and_unify_type` which replaces bound vars
1717     // with placeholders
1718     debug_assert!(!self_ty.has_escaping_bound_vars());
1719     let substs = tcx.mk_substs([self_ty.into()].iter());
1720
1721     let discriminant_def_id = tcx.require_lang_item(LangItem::Discriminant, None);
1722
1723     let predicate = ty::ProjectionPredicate {
1724         projection_ty: ty::ProjectionTy { substs, item_def_id: discriminant_def_id },
1725         term: self_ty.discriminant_ty(tcx).into(),
1726     };
1727
1728     // We get here from `poly_project_and_unify_type` which replaces bound vars
1729     // with placeholders, so dummy is okay here.
1730     confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1731 }
1732
1733 fn confirm_pointee_candidate<'cx, 'tcx>(
1734     selcx: &mut SelectionContext<'cx, 'tcx>,
1735     obligation: &ProjectionTyObligation<'tcx>,
1736     _: ImplSourcePointeeData,
1737 ) -> Progress<'tcx> {
1738     let tcx = selcx.tcx();
1739     let self_ty = selcx.infcx().shallow_resolve(obligation.predicate.self_ty());
1740
1741     let mut obligations = vec![];
1742     let (metadata_ty, check_is_sized) = self_ty.ptr_metadata_ty(tcx, |ty| {
1743         normalize_with_depth_to(
1744             selcx,
1745             obligation.param_env,
1746             obligation.cause.clone(),
1747             obligation.recursion_depth + 1,
1748             ty,
1749             &mut obligations,
1750         )
1751     });
1752     if check_is_sized {
1753         let sized_predicate = ty::Binder::dummy(ty::TraitRef::new(
1754             tcx.require_lang_item(LangItem::Sized, None),
1755             tcx.mk_substs_trait(self_ty, &[]),
1756         ))
1757         .without_const()
1758         .to_predicate(tcx);
1759         obligations.push(Obligation::new(
1760             obligation.cause.clone(),
1761             obligation.param_env,
1762             sized_predicate,
1763         ));
1764     }
1765
1766     let substs = tcx.mk_substs([self_ty.into()].iter());
1767     let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, None);
1768
1769     let predicate = ty::ProjectionPredicate {
1770         projection_ty: ty::ProjectionTy { substs, item_def_id: metadata_def_id },
1771         term: metadata_ty.into(),
1772     };
1773
1774     confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1775         .with_addl_obligations(obligations)
1776 }
1777
1778 fn confirm_fn_pointer_candidate<'cx, 'tcx>(
1779     selcx: &mut SelectionContext<'cx, 'tcx>,
1780     obligation: &ProjectionTyObligation<'tcx>,
1781     fn_pointer_impl_source: ImplSourceFnPointerData<'tcx, PredicateObligation<'tcx>>,
1782 ) -> Progress<'tcx> {
1783     let fn_type = selcx.infcx().shallow_resolve(fn_pointer_impl_source.fn_ty);
1784     let sig = fn_type.fn_sig(selcx.tcx());
1785     let Normalized { value: sig, obligations } = normalize_with_depth(
1786         selcx,
1787         obligation.param_env,
1788         obligation.cause.clone(),
1789         obligation.recursion_depth + 1,
1790         sig,
1791     );
1792
1793     confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
1794         .with_addl_obligations(fn_pointer_impl_source.nested)
1795         .with_addl_obligations(obligations)
1796 }
1797
1798 fn confirm_closure_candidate<'cx, 'tcx>(
1799     selcx: &mut SelectionContext<'cx, 'tcx>,
1800     obligation: &ProjectionTyObligation<'tcx>,
1801     impl_source: ImplSourceClosureData<'tcx, PredicateObligation<'tcx>>,
1802 ) -> Progress<'tcx> {
1803     let closure_sig = impl_source.substs.as_closure().sig();
1804     let Normalized { value: closure_sig, obligations } = normalize_with_depth(
1805         selcx,
1806         obligation.param_env,
1807         obligation.cause.clone(),
1808         obligation.recursion_depth + 1,
1809         closure_sig,
1810     );
1811
1812     debug!(?obligation, ?closure_sig, ?obligations, "confirm_closure_candidate");
1813
1814     confirm_callable_candidate(selcx, obligation, closure_sig, util::TupleArgumentsFlag::No)
1815         .with_addl_obligations(impl_source.nested)
1816         .with_addl_obligations(obligations)
1817 }
1818
1819 fn confirm_callable_candidate<'cx, 'tcx>(
1820     selcx: &mut SelectionContext<'cx, 'tcx>,
1821     obligation: &ProjectionTyObligation<'tcx>,
1822     fn_sig: ty::PolyFnSig<'tcx>,
1823     flag: util::TupleArgumentsFlag,
1824 ) -> Progress<'tcx> {
1825     let tcx = selcx.tcx();
1826
1827     debug!(?obligation, ?fn_sig, "confirm_callable_candidate");
1828
1829     let fn_once_def_id = tcx.require_lang_item(LangItem::FnOnce, None);
1830     let fn_once_output_def_id = tcx.require_lang_item(LangItem::FnOnceOutput, None);
1831
1832     let predicate = super::util::closure_trait_ref_and_return_type(
1833         tcx,
1834         fn_once_def_id,
1835         obligation.predicate.self_ty(),
1836         fn_sig,
1837         flag,
1838     )
1839     .map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
1840         projection_ty: ty::ProjectionTy {
1841             substs: trait_ref.substs,
1842             item_def_id: fn_once_output_def_id,
1843         },
1844         term: ret_type.into(),
1845     });
1846
1847     confirm_param_env_candidate(selcx, obligation, predicate, true)
1848 }
1849
1850 fn confirm_param_env_candidate<'cx, 'tcx>(
1851     selcx: &mut SelectionContext<'cx, 'tcx>,
1852     obligation: &ProjectionTyObligation<'tcx>,
1853     poly_cache_entry: ty::PolyProjectionPredicate<'tcx>,
1854     potentially_unnormalized_candidate: bool,
1855 ) -> Progress<'tcx> {
1856     let infcx = selcx.infcx();
1857     let cause = &obligation.cause;
1858     let param_env = obligation.param_env;
1859
1860     let (cache_entry, _) = infcx.replace_bound_vars_with_fresh_vars(
1861         cause.span,
1862         LateBoundRegionConversionTime::HigherRankedType,
1863         poly_cache_entry,
1864     );
1865
1866     let cache_projection = cache_entry.projection_ty;
1867     let mut nested_obligations = Vec::new();
1868     let obligation_projection = obligation.predicate;
1869     let obligation_projection = ensure_sufficient_stack(|| {
1870         normalize_with_depth_to(
1871             selcx,
1872             obligation.param_env,
1873             obligation.cause.clone(),
1874             obligation.recursion_depth + 1,
1875             obligation_projection,
1876             &mut nested_obligations,
1877         )
1878     });
1879     let cache_projection = if potentially_unnormalized_candidate {
1880         ensure_sufficient_stack(|| {
1881             normalize_with_depth_to(
1882                 selcx,
1883                 obligation.param_env,
1884                 obligation.cause.clone(),
1885                 obligation.recursion_depth + 1,
1886                 cache_projection,
1887                 &mut nested_obligations,
1888             )
1889         })
1890     } else {
1891         cache_projection
1892     };
1893
1894     debug!(?cache_projection, ?obligation_projection);
1895
1896     match infcx.at(cause, param_env).eq(cache_projection, obligation_projection) {
1897         Ok(InferOk { value: _, obligations }) => {
1898             nested_obligations.extend(obligations);
1899             assoc_ty_own_obligations(selcx, obligation, &mut nested_obligations);
1900             // FIXME(associated_const_equality): Handle consts here as well? Maybe this progress type should just take
1901             // a term instead.
1902             Progress { term: cache_entry.term, obligations: nested_obligations }
1903         }
1904         Err(e) => {
1905             let msg = format!(
1906                 "Failed to unify obligation `{:?}` with poly_projection `{:?}`: {:?}",
1907                 obligation, poly_cache_entry, e,
1908             );
1909             debug!("confirm_param_env_candidate: {}", msg);
1910             let err = infcx.tcx.ty_error_with_message(obligation.cause.span, &msg);
1911             Progress { term: err.into(), obligations: vec![] }
1912         }
1913     }
1914 }
1915
1916 fn confirm_impl_candidate<'cx, 'tcx>(
1917     selcx: &mut SelectionContext<'cx, 'tcx>,
1918     obligation: &ProjectionTyObligation<'tcx>,
1919     impl_impl_source: ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>>,
1920 ) -> Progress<'tcx> {
1921     let tcx = selcx.tcx();
1922
1923     let ImplSourceUserDefinedData { impl_def_id, substs, mut nested } = impl_impl_source;
1924     let assoc_item_id = obligation.predicate.item_def_id;
1925     let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
1926
1927     let param_env = obligation.param_env;
1928     let Ok(assoc_ty) = assoc_def(selcx, impl_def_id, assoc_item_id) else {
1929         return Progress { term: tcx.ty_error().into(), obligations: nested };
1930     };
1931
1932     if !assoc_ty.item.defaultness.has_value() {
1933         // This means that the impl is missing a definition for the
1934         // associated type. This error will be reported by the type
1935         // checker method `check_impl_items_against_trait`, so here we
1936         // just return Error.
1937         debug!(
1938             "confirm_impl_candidate: no associated type {:?} for {:?}",
1939             assoc_ty.item.name, obligation.predicate
1940         );
1941         return Progress { term: tcx.ty_error().into(), obligations: nested };
1942     }
1943     // If we're trying to normalize `<Vec<u32> as X>::A<S>` using
1944     //`impl<T> X for Vec<T> { type A<Y> = Box<Y>; }`, then:
1945     //
1946     // * `obligation.predicate.substs` is `[Vec<u32>, S]`
1947     // * `substs` is `[u32]`
1948     // * `substs` ends up as `[u32, S]`
1949     let substs = obligation.predicate.substs.rebase_onto(tcx, trait_def_id, substs);
1950     let substs =
1951         translate_substs(selcx.infcx(), param_env, impl_def_id, substs, assoc_ty.defining_node);
1952     let ty = tcx.type_of(assoc_ty.item.def_id);
1953     let is_const = matches!(tcx.def_kind(assoc_ty.item.def_id), DefKind::AssocConst);
1954     let term: ty::Term<'tcx> = if is_const {
1955         let identity_substs =
1956             crate::traits::InternalSubsts::identity_for_item(tcx, assoc_ty.item.def_id);
1957         let did = ty::WithOptConstParam::unknown(assoc_ty.item.def_id);
1958         let val = ty::ConstKind::Unevaluated(ty::Unevaluated::new(did, identity_substs));
1959         tcx.mk_const(ty::ConstS { ty, val }).into()
1960     } else {
1961         ty.into()
1962     };
1963     if substs.len() != tcx.generics_of(assoc_ty.item.def_id).count() {
1964         let err = tcx.ty_error_with_message(
1965             obligation.cause.span,
1966             "impl item and trait item have different parameter counts",
1967         );
1968         Progress { term: err.into(), obligations: nested }
1969     } else {
1970         assoc_ty_own_obligations(selcx, obligation, &mut nested);
1971         Progress { term: term.subst(tcx, substs), obligations: nested }
1972     }
1973 }
1974
1975 // Get obligations corresponding to the predicates from the where-clause of the
1976 // associated type itself.
1977 // Note: `feature(generic_associated_types)` is required to write such
1978 // predicates, even for non-generic associcated types.
1979 fn assoc_ty_own_obligations<'cx, 'tcx>(
1980     selcx: &mut SelectionContext<'cx, 'tcx>,
1981     obligation: &ProjectionTyObligation<'tcx>,
1982     nested: &mut Vec<PredicateObligation<'tcx>>,
1983 ) {
1984     let tcx = selcx.tcx();
1985     for predicate in tcx
1986         .predicates_of(obligation.predicate.item_def_id)
1987         .instantiate_own(tcx, obligation.predicate.substs)
1988         .predicates
1989     {
1990         let normalized = normalize_with_depth_to(
1991             selcx,
1992             obligation.param_env,
1993             obligation.cause.clone(),
1994             obligation.recursion_depth + 1,
1995             predicate,
1996             nested,
1997         );
1998         nested.push(Obligation::with_depth(
1999             obligation.cause.clone(),
2000             obligation.recursion_depth + 1,
2001             obligation.param_env,
2002             normalized,
2003         ));
2004     }
2005 }
2006
2007 /// Locate the definition of an associated type in the specialization hierarchy,
2008 /// starting from the given impl.
2009 ///
2010 /// Based on the "projection mode", this lookup may in fact only examine the
2011 /// topmost impl. See the comments for `Reveal` for more details.
2012 fn assoc_def(
2013     selcx: &SelectionContext<'_, '_>,
2014     impl_def_id: DefId,
2015     assoc_def_id: DefId,
2016 ) -> Result<specialization_graph::LeafDef, ErrorGuaranteed> {
2017     let tcx = selcx.tcx();
2018     let trait_def_id = tcx.impl_trait_ref(impl_def_id).unwrap().def_id;
2019     let trait_def = tcx.trait_def(trait_def_id);
2020
2021     // This function may be called while we are still building the
2022     // specialization graph that is queried below (via TraitDef::ancestors()),
2023     // so, in order to avoid unnecessary infinite recursion, we manually look
2024     // for the associated item at the given impl.
2025     // If there is no such item in that impl, this function will fail with a
2026     // cycle error if the specialization graph is currently being built.
2027     if let Some(&impl_item_id) = tcx.impl_item_implementor_ids(impl_def_id).get(&assoc_def_id) {
2028         let item = tcx.associated_item(impl_item_id);
2029         let impl_node = specialization_graph::Node::Impl(impl_def_id);
2030         return Ok(specialization_graph::LeafDef {
2031             item: *item,
2032             defining_node: impl_node,
2033             finalizing_node: if item.defaultness.is_default() { None } else { Some(impl_node) },
2034         });
2035     }
2036
2037     let ancestors = trait_def.ancestors(tcx, impl_def_id)?;
2038     if let Some(assoc_item) = ancestors.leaf_def(tcx, assoc_def_id) {
2039         Ok(assoc_item)
2040     } else {
2041         // This is saying that neither the trait nor
2042         // the impl contain a definition for this
2043         // associated type.  Normally this situation
2044         // could only arise through a compiler bug --
2045         // if the user wrote a bad item name, it
2046         // should have failed in astconv.
2047         bug!(
2048             "No associated type `{}` for {}",
2049             tcx.item_name(assoc_def_id),
2050             tcx.def_path_str(impl_def_id)
2051         )
2052     }
2053 }
2054
2055 crate trait ProjectionCacheKeyExt<'cx, 'tcx>: Sized {
2056     fn from_poly_projection_predicate(
2057         selcx: &mut SelectionContext<'cx, 'tcx>,
2058         predicate: ty::PolyProjectionPredicate<'tcx>,
2059     ) -> Option<Self>;
2060 }
2061
2062 impl<'cx, 'tcx> ProjectionCacheKeyExt<'cx, 'tcx> for ProjectionCacheKey<'tcx> {
2063     fn from_poly_projection_predicate(
2064         selcx: &mut SelectionContext<'cx, 'tcx>,
2065         predicate: ty::PolyProjectionPredicate<'tcx>,
2066     ) -> Option<Self> {
2067         let infcx = selcx.infcx();
2068         // We don't do cross-snapshot caching of obligations with escaping regions,
2069         // so there's no cache key to use
2070         predicate.no_bound_vars().map(|predicate| {
2071             ProjectionCacheKey::new(
2072                 // We don't attempt to match up with a specific type-variable state
2073                 // from a specific call to `opt_normalize_projection_type` - if
2074                 // there's no precise match, the original cache entry is "stranded"
2075                 // anyway.
2076                 infcx.resolve_vars_if_possible(predicate.projection_ty),
2077             )
2078         })
2079     }
2080 }