rustc_trait_selection/traits/
project.rs

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