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