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