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