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