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