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