1use std::ops::ControlFlow;
4
5use rustc_data_structures::sso::SsoHashSet;
6use rustc_data_structures::stack::ensure_sufficient_stack;
7use rustc_errors::ErrorGuaranteed;
8use rustc_hir::def::DefKind;
9use rustc_hir::lang_items::LangItem;
10use rustc_infer::infer::DefineOpaqueTypes;
11use rustc_infer::infer::resolve::OpportunisticRegionResolver;
12use rustc_infer::traits::{ObligationCauseCode, PredicateObligations};
13use rustc_middle::traits::select::OverflowError;
14use rustc_middle::traits::{BuiltinImplSource, ImplSource, ImplSourceUserDefinedData};
15use rustc_middle::ty::fast_reject::DeepRejectCtxt;
16use rustc_middle::ty::{
17 self, Term, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, TypingMode, Upcast,
18};
19use rustc_middle::{bug, span_bug};
20use rustc_span::sym;
21use tracing::{debug, instrument};
22
23use super::{
24 MismatchedProjectionTypes, Normalized, NormalizedTerm, Obligation, ObligationCause,
25 PredicateObligation, ProjectionCacheEntry, ProjectionCacheKey, Selection, SelectionContext,
26 SelectionError, specialization_graph, translate_args, util,
27};
28use crate::errors::InherentProjectionNormalizationOverflow;
29use crate::infer::{BoundRegionConversionTime, InferOk};
30use crate::traits::normalize::{normalize_with_depth, normalize_with_depth_to};
31use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
32use crate::traits::select::ProjectionMatchesProjection;
33
34pub type PolyProjectionObligation<'tcx> = Obligation<'tcx, ty::PolyProjectionPredicate<'tcx>>;
35
36pub type ProjectionObligation<'tcx> = Obligation<'tcx, ty::ProjectionPredicate<'tcx>>;
37
38pub type ProjectionTermObligation<'tcx> = Obligation<'tcx, ty::AliasTerm<'tcx>>;
39
40pub(super) struct InProgress;
41
42#[derive(Debug)]
44pub enum ProjectionError<'tcx> {
45 TooManyCandidates,
47
48 TraitSelectionError(SelectionError<'tcx>),
50}
51
52#[derive(PartialEq, Eq, Debug)]
53enum ProjectionCandidate<'tcx> {
54 ParamEnv(ty::PolyProjectionPredicate<'tcx>),
56
57 TraitDef(ty::PolyProjectionPredicate<'tcx>),
60
61 Object(ty::PolyProjectionPredicate<'tcx>),
63
64 Select(Selection<'tcx>),
66}
67
68enum ProjectionCandidateSet<'tcx> {
69 None,
70 Single(ProjectionCandidate<'tcx>),
71 Ambiguous,
72 Error(SelectionError<'tcx>),
73}
74
75impl<'tcx> ProjectionCandidateSet<'tcx> {
76 fn mark_ambiguous(&mut self) {
77 *self = ProjectionCandidateSet::Ambiguous;
78 }
79
80 fn mark_error(&mut self, err: SelectionError<'tcx>) {
81 *self = ProjectionCandidateSet::Error(err);
82 }
83
84 fn push_candidate(&mut self, candidate: ProjectionCandidate<'tcx>) -> bool {
88 use self::ProjectionCandidate::*;
89 use self::ProjectionCandidateSet::*;
90
91 let convert_to_ambiguous;
100
101 match self {
102 None => {
103 *self = Single(candidate);
104 return true;
105 }
106
107 Single(current) => {
108 if current == &candidate {
111 return false;
112 }
113
114 match (current, candidate) {
122 (ParamEnv(..), ParamEnv(..)) => convert_to_ambiguous = (),
123 (ParamEnv(..), _) => return false,
124 (_, ParamEnv(..)) => bug!(
125 "should never prefer non-param-env candidates over param-env candidates"
126 ),
127 (_, _) => convert_to_ambiguous = (),
128 }
129 }
130
131 Ambiguous | Error(..) => {
132 return false;
133 }
134 }
135
136 let () = convert_to_ambiguous;
139 *self = Ambiguous;
140 false
141 }
142}
143
144pub(super) enum ProjectAndUnifyResult<'tcx> {
153 Holds(PredicateObligations<'tcx>),
158 FailedNormalization,
161 Recursive,
164 MismatchedProjectionTypes(MismatchedProjectionTypes<'tcx>),
167}
168
169#[instrument(level = "debug", skip(selcx))]
176pub(super) fn poly_project_and_unify_term<'cx, 'tcx>(
177 selcx: &mut SelectionContext<'cx, 'tcx>,
178 obligation: &PolyProjectionObligation<'tcx>,
179) -> ProjectAndUnifyResult<'tcx> {
180 let infcx = selcx.infcx;
181 let r = infcx.commit_if_ok(|_snapshot| {
182 let placeholder_predicate = infcx.enter_forall_and_leak_universe(obligation.predicate);
183
184 let placeholder_obligation = obligation.with(infcx.tcx, placeholder_predicate);
185 match project_and_unify_term(selcx, &placeholder_obligation) {
186 ProjectAndUnifyResult::MismatchedProjectionTypes(e) => Err(e),
187 other => Ok(other),
188 }
189 });
190
191 match r {
192 Ok(inner) => inner,
193 Err(err) => ProjectAndUnifyResult::MismatchedProjectionTypes(err),
194 }
195}
196
197#[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 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 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
255pub fn normalize_projection_ty<'a, 'b, 'tcx>(
262 selcx: &'a mut SelectionContext<'b, 'tcx>,
263 param_env: ty::ParamEnv<'tcx>,
264 projection_ty: ty::AliasTy<'tcx>,
265 cause: ObligationCause<'tcx>,
266 depth: usize,
267 obligations: &mut PredicateObligations<'tcx>,
268) -> Term<'tcx> {
269 opt_normalize_projection_term(
270 selcx,
271 param_env,
272 projection_ty.into(),
273 cause.clone(),
274 depth,
275 obligations,
276 )
277 .ok()
278 .flatten()
279 .unwrap_or_else(move || {
280 selcx
285 .infcx
286 .projection_ty_to_infer(param_env, projection_ty, cause, depth + 1, obligations)
287 .into()
288 })
289}
290
291#[instrument(level = "debug", skip(selcx, param_env, cause, obligations))]
302pub(super) fn opt_normalize_projection_term<'a, 'b, 'tcx>(
303 selcx: &'a mut SelectionContext<'b, 'tcx>,
304 param_env: ty::ParamEnv<'tcx>,
305 projection_term: ty::AliasTerm<'tcx>,
306 cause: ObligationCause<'tcx>,
307 depth: usize,
308 obligations: &mut PredicateObligations<'tcx>,
309) -> Result<Option<Term<'tcx>>, InProgress> {
310 let infcx = selcx.infcx;
311 debug_assert!(!selcx.infcx.next_trait_solver());
312 let projection_term = infcx.resolve_vars_if_possible(projection_term);
313 let cache_key = ProjectionCacheKey::new(projection_term, param_env);
314
315 let cache_entry = infcx.inner.borrow_mut().projection_cache().try_start(cache_key);
323 match cache_entry {
324 Ok(()) => debug!("no cache"),
325 Err(ProjectionCacheEntry::Ambiguous) => {
326 debug!("found cache entry: ambiguous");
330 return Ok(None);
331 }
332 Err(ProjectionCacheEntry::InProgress) => {
333 debug!("found cache entry: in-progress");
342
343 infcx.inner.borrow_mut().projection_cache().recur(cache_key);
347 return Err(InProgress);
348 }
349 Err(ProjectionCacheEntry::Recur) => {
350 debug!("recur cache");
351 return Err(InProgress);
352 }
353 Err(ProjectionCacheEntry::NormalizedTerm { ty, complete: _ }) => {
354 debug!(?ty, "found normalized ty");
366 obligations.extend(ty.obligations);
367 return Ok(Some(ty.value));
368 }
369 Err(ProjectionCacheEntry::Error) => {
370 debug!("opt_normalize_projection_type: found error");
371 let result = normalize_to_error(selcx, param_env, projection_term, cause, depth);
372 obligations.extend(result.obligations);
373 return Ok(Some(result.value));
374 }
375 }
376
377 let obligation =
378 Obligation::with_depth(selcx.tcx(), cause.clone(), depth, param_env, projection_term);
379
380 match project(selcx, &obligation) {
381 Ok(Projected::Progress(Progress {
382 term: projected_term,
383 obligations: mut projected_obligations,
384 })) => {
385 let projected_term = selcx.infcx.resolve_vars_if_possible(projected_term);
391
392 let mut result = if projected_term.has_aliases() {
393 let normalized_ty = normalize_with_depth_to(
394 selcx,
395 param_env,
396 cause,
397 depth + 1,
398 projected_term,
399 &mut projected_obligations,
400 );
401
402 Normalized { value: normalized_ty, obligations: projected_obligations }
403 } else {
404 Normalized { value: projected_term, obligations: projected_obligations }
405 };
406
407 let mut deduped = SsoHashSet::with_capacity(result.obligations.len());
408 result.obligations.retain(|obligation| deduped.insert(obligation.clone()));
409
410 infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
411 obligations.extend(result.obligations);
412 Ok(Some(result.value))
413 }
414 Ok(Projected::NoProgress(projected_ty)) => {
415 let result =
416 Normalized { value: projected_ty, obligations: PredicateObligations::new() };
417 infcx.inner.borrow_mut().projection_cache().insert_term(cache_key, result.clone());
418 Ok(Some(result.value))
420 }
421 Err(ProjectionError::TooManyCandidates) => {
422 debug!("opt_normalize_projection_type: too many candidates");
423 infcx.inner.borrow_mut().projection_cache().ambiguous(cache_key);
424 Ok(None)
425 }
426 Err(ProjectionError::TraitSelectionError(_)) => {
427 debug!("opt_normalize_projection_type: ERROR");
428 infcx.inner.borrow_mut().projection_cache().error(cache_key);
433 let result = normalize_to_error(selcx, param_env, projection_term, cause, depth);
434 obligations.extend(result.obligations);
435 Ok(Some(result.value))
436 }
437 }
438}
439
440fn normalize_to_error<'a, 'tcx>(
460 selcx: &SelectionContext<'a, 'tcx>,
461 param_env: ty::ParamEnv<'tcx>,
462 projection_term: ty::AliasTerm<'tcx>,
463 cause: ObligationCause<'tcx>,
464 depth: usize,
465) -> NormalizedTerm<'tcx> {
466 let trait_ref = ty::Binder::dummy(projection_term.trait_ref(selcx.tcx()));
467 let new_value = match projection_term.kind(selcx.tcx()) {
468 ty::AliasTermKind::ProjectionTy
469 | ty::AliasTermKind::InherentTy
470 | ty::AliasTermKind::OpaqueTy
471 | ty::AliasTermKind::WeakTy => selcx.infcx.next_ty_var(cause.span).into(),
472 ty::AliasTermKind::UnevaluatedConst | ty::AliasTermKind::ProjectionConst => {
473 selcx.infcx.next_const_var(cause.span).into()
474 }
475 };
476 let mut obligations = PredicateObligations::new();
477 obligations.push(Obligation {
478 cause,
479 recursion_depth: depth,
480 param_env,
481 predicate: trait_ref.upcast(selcx.tcx()),
482 });
483 Normalized { value: new_value, obligations }
484}
485
486#[instrument(level = "debug", skip(selcx, param_env, cause, obligations))]
488pub fn normalize_inherent_projection<'a, 'b, 'tcx>(
489 selcx: &'a mut SelectionContext<'b, 'tcx>,
490 param_env: ty::ParamEnv<'tcx>,
491 alias_ty: ty::AliasTy<'tcx>,
492 cause: ObligationCause<'tcx>,
493 depth: usize,
494 obligations: &mut PredicateObligations<'tcx>,
495) -> Ty<'tcx> {
496 let tcx = selcx.tcx();
497
498 if !tcx.recursion_limit().value_within_limit(depth) {
499 tcx.dcx().emit_fatal(InherentProjectionNormalizationOverflow {
501 span: cause.span,
502 ty: alias_ty.to_string(),
503 });
504 }
505
506 let args = compute_inherent_assoc_ty_args(
507 selcx,
508 param_env,
509 alias_ty,
510 cause.clone(),
511 depth,
512 obligations,
513 );
514
515 let predicates = tcx.predicates_of(alias_ty.def_id).instantiate(tcx, args);
517 for (predicate, span) in predicates {
518 let predicate = normalize_with_depth_to(
519 selcx,
520 param_env,
521 cause.clone(),
522 depth + 1,
523 predicate,
524 obligations,
525 );
526
527 let nested_cause = ObligationCause::new(
528 cause.span,
529 cause.body_id,
530 ObligationCauseCode::WhereClause(alias_ty.def_id, span),
535 );
536
537 obligations.push(Obligation::with_depth(
538 tcx,
539 nested_cause,
540 depth + 1,
541 param_env,
542 predicate,
543 ));
544 }
545
546 let ty = tcx.type_of(alias_ty.def_id).instantiate(tcx, args);
547
548 let mut ty = selcx.infcx.resolve_vars_if_possible(ty);
549 if ty.has_aliases() {
550 ty = normalize_with_depth_to(selcx, param_env, cause.clone(), depth + 1, ty, obligations);
551 }
552
553 ty
554}
555
556pub fn compute_inherent_assoc_ty_args<'a, 'b, 'tcx>(
557 selcx: &'a mut SelectionContext<'b, 'tcx>,
558 param_env: ty::ParamEnv<'tcx>,
559 alias_ty: ty::AliasTy<'tcx>,
560 cause: ObligationCause<'tcx>,
561 depth: usize,
562 obligations: &mut PredicateObligations<'tcx>,
563) -> ty::GenericArgsRef<'tcx> {
564 let tcx = selcx.tcx();
565
566 let impl_def_id = tcx.parent(alias_ty.def_id);
567 let impl_args = selcx.infcx.fresh_args_for_item(cause.span, impl_def_id);
568
569 let mut impl_ty = tcx.type_of(impl_def_id).instantiate(tcx, impl_args);
570 if !selcx.infcx.next_trait_solver() {
571 impl_ty = normalize_with_depth_to(
572 selcx,
573 param_env,
574 cause.clone(),
575 depth + 1,
576 impl_ty,
577 obligations,
578 );
579 }
580
581 let mut self_ty = alias_ty.self_ty();
584 if !selcx.infcx.next_trait_solver() {
585 self_ty = normalize_with_depth_to(
586 selcx,
587 param_env,
588 cause.clone(),
589 depth + 1,
590 self_ty,
591 obligations,
592 );
593 }
594
595 match selcx.infcx.at(&cause, param_env).eq(DefineOpaqueTypes::Yes, impl_ty, self_ty) {
596 Ok(mut ok) => obligations.append(&mut ok.obligations),
597 Err(_) => {
598 tcx.dcx().span_bug(
599 cause.span,
600 format!("{self_ty:?} was equal to {impl_ty:?} during selection but now it is not"),
601 );
602 }
603 }
604
605 alias_ty.rebase_inherent_args_onto_impl(impl_args, tcx)
606}
607
608enum Projected<'tcx> {
609 Progress(Progress<'tcx>),
610 NoProgress(ty::Term<'tcx>),
611}
612
613struct Progress<'tcx> {
614 term: ty::Term<'tcx>,
615 obligations: PredicateObligations<'tcx>,
616}
617
618impl<'tcx> Progress<'tcx> {
619 fn error(tcx: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Self {
620 Progress { term: Ty::new_error(tcx, guar).into(), obligations: PredicateObligations::new() }
621 }
622
623 fn with_addl_obligations(mut self, mut obligations: PredicateObligations<'tcx>) -> Self {
624 self.obligations.append(&mut obligations);
625 self
626 }
627}
628
629#[instrument(level = "info", skip(selcx))]
634fn project<'cx, 'tcx>(
635 selcx: &mut SelectionContext<'cx, 'tcx>,
636 obligation: &ProjectionTermObligation<'tcx>,
637) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
638 if !selcx.tcx().recursion_limit().value_within_limit(obligation.recursion_depth) {
639 return Err(ProjectionError::TraitSelectionError(SelectionError::Overflow(
642 OverflowError::Canonical,
643 )));
644 }
645
646 if let Err(guar) = obligation.predicate.error_reported() {
647 return Ok(Projected::Progress(Progress::error(selcx.tcx(), guar)));
648 }
649
650 let mut candidates = ProjectionCandidateSet::None;
651
652 assemble_candidates_from_param_env(selcx, obligation, &mut candidates);
656
657 assemble_candidates_from_trait_def(selcx, obligation, &mut candidates);
658
659 assemble_candidates_from_object_ty(selcx, obligation, &mut candidates);
660
661 if let ProjectionCandidateSet::Single(ProjectionCandidate::Object(_)) = candidates {
662 } else {
667 assemble_candidates_from_impls(selcx, obligation, &mut candidates);
668 };
669
670 match candidates {
671 ProjectionCandidateSet::Single(candidate) => {
672 confirm_candidate(selcx, obligation, candidate)
673 }
674 ProjectionCandidateSet::None => {
675 let tcx = selcx.tcx();
676 let term = obligation.predicate.to_term(tcx);
677 Ok(Projected::NoProgress(term))
678 }
679 ProjectionCandidateSet::Error(e) => Err(ProjectionError::TraitSelectionError(e)),
681 ProjectionCandidateSet::Ambiguous => Err(ProjectionError::TooManyCandidates),
684 }
685}
686
687fn assemble_candidates_from_param_env<'cx, 'tcx>(
691 selcx: &mut SelectionContext<'cx, 'tcx>,
692 obligation: &ProjectionTermObligation<'tcx>,
693 candidate_set: &mut ProjectionCandidateSet<'tcx>,
694) {
695 assemble_candidates_from_predicates(
696 selcx,
697 obligation,
698 candidate_set,
699 ProjectionCandidate::ParamEnv,
700 obligation.param_env.caller_bounds().iter(),
701 false,
702 );
703}
704
705fn assemble_candidates_from_trait_def<'cx, 'tcx>(
716 selcx: &mut SelectionContext<'cx, 'tcx>,
717 obligation: &ProjectionTermObligation<'tcx>,
718 candidate_set: &mut ProjectionCandidateSet<'tcx>,
719) {
720 debug!("assemble_candidates_from_trait_def(..)");
721 let mut ambiguous = false;
722 let _ = selcx.for_each_item_bound(
723 obligation.predicate.self_ty(),
724 |selcx, clause, _| {
725 let Some(clause) = clause.as_projection_clause() else {
726 return ControlFlow::Continue(());
727 };
728 if clause.item_def_id() != obligation.predicate.def_id {
729 return ControlFlow::Continue(());
730 }
731
732 let is_match =
733 selcx.infcx.probe(|_| selcx.match_projection_projections(obligation, clause, true));
734
735 match is_match {
736 ProjectionMatchesProjection::Yes => {
737 candidate_set.push_candidate(ProjectionCandidate::TraitDef(clause));
738
739 if !obligation.predicate.has_non_region_infer() {
740 return ControlFlow::Break(());
744 }
745 }
746 ProjectionMatchesProjection::Ambiguous => {
747 candidate_set.mark_ambiguous();
748 }
749 ProjectionMatchesProjection::No => {}
750 }
751
752 ControlFlow::Continue(())
753 },
754 || ambiguous = true,
757 );
758
759 if ambiguous {
760 candidate_set.mark_ambiguous();
761 }
762}
763
764fn assemble_candidates_from_object_ty<'cx, 'tcx>(
774 selcx: &mut SelectionContext<'cx, 'tcx>,
775 obligation: &ProjectionTermObligation<'tcx>,
776 candidate_set: &mut ProjectionCandidateSet<'tcx>,
777) {
778 debug!("assemble_candidates_from_object_ty(..)");
779
780 let tcx = selcx.tcx();
781
782 if !tcx.trait_def(obligation.predicate.trait_def_id(tcx)).implement_via_object {
783 return;
784 }
785
786 let self_ty = obligation.predicate.self_ty();
787 let object_ty = selcx.infcx.shallow_resolve(self_ty);
788 let data = match object_ty.kind() {
789 ty::Dynamic(data, ..) => data,
790 ty::Infer(ty::TyVar(_)) => {
791 candidate_set.mark_ambiguous();
794 return;
795 }
796 _ => return,
797 };
798 let env_predicates = data
799 .projection_bounds()
800 .filter(|bound| bound.item_def_id() == obligation.predicate.def_id)
801 .map(|p| p.with_self_ty(tcx, object_ty).upcast(tcx));
802
803 assemble_candidates_from_predicates(
804 selcx,
805 obligation,
806 candidate_set,
807 ProjectionCandidate::Object,
808 env_predicates,
809 false,
810 );
811}
812
813#[instrument(
814 level = "debug",
815 skip(selcx, candidate_set, ctor, env_predicates, potentially_unnormalized_candidates)
816)]
817fn assemble_candidates_from_predicates<'cx, 'tcx>(
818 selcx: &mut SelectionContext<'cx, 'tcx>,
819 obligation: &ProjectionTermObligation<'tcx>,
820 candidate_set: &mut ProjectionCandidateSet<'tcx>,
821 ctor: fn(ty::PolyProjectionPredicate<'tcx>) -> ProjectionCandidate<'tcx>,
822 env_predicates: impl Iterator<Item = ty::Clause<'tcx>>,
823 potentially_unnormalized_candidates: bool,
824) {
825 let infcx = selcx.infcx;
826 let drcx = DeepRejectCtxt::relate_rigid_rigid(selcx.tcx());
827 for predicate in env_predicates {
828 let bound_predicate = predicate.kind();
829 if let ty::ClauseKind::Projection(data) = predicate.kind().skip_binder() {
830 let data = bound_predicate.rebind(data);
831 if data.item_def_id() != obligation.predicate.def_id {
832 continue;
833 }
834
835 if !drcx
836 .args_may_unify(obligation.predicate.args, data.skip_binder().projection_term.args)
837 {
838 continue;
839 }
840
841 let is_match = infcx.probe(|_| {
842 selcx.match_projection_projections(
843 obligation,
844 data,
845 potentially_unnormalized_candidates,
846 )
847 });
848
849 match is_match {
850 ProjectionMatchesProjection::Yes => {
851 candidate_set.push_candidate(ctor(data));
852
853 if potentially_unnormalized_candidates
854 && !obligation.predicate.has_non_region_infer()
855 {
856 return;
860 }
861 }
862 ProjectionMatchesProjection::Ambiguous => {
863 candidate_set.mark_ambiguous();
864 }
865 ProjectionMatchesProjection::No => {}
866 }
867 }
868 }
869}
870
871#[instrument(level = "debug", skip(selcx, obligation, candidate_set))]
872fn assemble_candidates_from_impls<'cx, 'tcx>(
873 selcx: &mut SelectionContext<'cx, 'tcx>,
874 obligation: &ProjectionTermObligation<'tcx>,
875 candidate_set: &mut ProjectionCandidateSet<'tcx>,
876) {
877 let trait_ref = obligation.predicate.trait_ref(selcx.tcx());
880 let trait_obligation = obligation.with(selcx.tcx(), trait_ref);
881 let _ = selcx.infcx.commit_if_ok(|_| {
882 let impl_source = match selcx.select(&trait_obligation) {
883 Ok(Some(impl_source)) => impl_source,
884 Ok(None) => {
885 candidate_set.mark_ambiguous();
886 return Err(());
887 }
888 Err(e) => {
889 debug!(error = ?e, "selection error");
890 candidate_set.mark_error(e);
891 return Err(());
892 }
893 };
894
895 let eligible = match &impl_source {
896 ImplSource::UserDefined(impl_data) => {
897 match specialization_graph::assoc_def(
920 selcx.tcx(),
921 impl_data.impl_def_id,
922 obligation.predicate.def_id,
923 ) {
924 Ok(node_item) => {
925 if node_item.is_final() {
926 true
928 } else {
929 match selcx.infcx.typing_mode() {
934 TypingMode::Coherence
935 | TypingMode::Analysis { .. }
936 | TypingMode::Borrowck { .. }
937 | TypingMode::PostBorrowckAnalysis { .. } => {
938 debug!(
939 assoc_ty = ?selcx.tcx().def_path_str(node_item.item.def_id),
940 ?obligation.predicate,
941 "not eligible due to default",
942 );
943 false
944 }
945 TypingMode::PostAnalysis => {
946 let poly_trait_ref =
949 selcx.infcx.resolve_vars_if_possible(trait_ref);
950 !poly_trait_ref.still_further_specializable()
951 }
952 }
953 }
954 }
955 Err(ErrorGuaranteed { .. }) => true,
959 }
960 }
961 ImplSource::Builtin(BuiltinImplSource::Misc | BuiltinImplSource::Trivial, _) => {
962 let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
966
967 let tcx = selcx.tcx();
968 let lang_items = selcx.tcx().lang_items();
969 if [
970 lang_items.coroutine_trait(),
971 lang_items.future_trait(),
972 lang_items.iterator_trait(),
973 lang_items.async_iterator_trait(),
974 lang_items.fn_trait(),
975 lang_items.fn_mut_trait(),
976 lang_items.fn_once_trait(),
977 lang_items.async_fn_trait(),
978 lang_items.async_fn_mut_trait(),
979 lang_items.async_fn_once_trait(),
980 ]
981 .contains(&Some(trait_ref.def_id))
982 {
983 true
984 } else if tcx.is_lang_item(trait_ref.def_id, LangItem::AsyncFnKindHelper) {
985 if obligation.predicate.args.type_at(0).is_ty_var()
987 || obligation.predicate.args.type_at(4).is_ty_var()
988 || obligation.predicate.args.type_at(5).is_ty_var()
989 {
990 candidate_set.mark_ambiguous();
991 true
992 } else {
993 obligation.predicate.args.type_at(0).to_opt_closure_kind().is_some()
994 && obligation.predicate.args.type_at(1).to_opt_closure_kind().is_some()
995 }
996 } else if tcx.is_lang_item(trait_ref.def_id, LangItem::DiscriminantKind) {
997 match self_ty.kind() {
998 ty::Bool
999 | ty::Char
1000 | ty::Int(_)
1001 | ty::Uint(_)
1002 | ty::Float(_)
1003 | ty::Adt(..)
1004 | ty::Foreign(_)
1005 | ty::Str
1006 | ty::Array(..)
1007 | ty::Pat(..)
1008 | ty::Slice(_)
1009 | ty::RawPtr(..)
1010 | ty::Ref(..)
1011 | ty::FnDef(..)
1012 | ty::FnPtr(..)
1013 | ty::Dynamic(..)
1014 | ty::Closure(..)
1015 | ty::CoroutineClosure(..)
1016 | ty::Coroutine(..)
1017 | ty::CoroutineWitness(..)
1018 | ty::Never
1019 | ty::Tuple(..)
1020 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1022
1023 ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
1024
1025 ty::Param(_)
1029 | ty::Alias(..)
1030 | ty::Bound(..)
1031 | ty::Placeholder(..)
1032 | ty::Infer(..)
1033 | ty::Error(_) => false,
1034 }
1035 } else if tcx.is_lang_item(trait_ref.def_id, LangItem::AsyncDestruct) {
1036 match self_ty.kind() {
1037 ty::Bool
1038 | ty::Char
1039 | ty::Int(_)
1040 | ty::Uint(_)
1041 | ty::Float(_)
1042 | ty::Adt(..)
1043 | ty::Str
1044 | ty::Array(..)
1045 | ty::Slice(_)
1046 | ty::RawPtr(..)
1047 | ty::Ref(..)
1048 | ty::FnDef(..)
1049 | ty::FnPtr(..)
1050 | ty::UnsafeBinder(_)
1051 | ty::Dynamic(..)
1052 | ty::Closure(..)
1053 | ty::CoroutineClosure(..)
1054 | ty::Coroutine(..)
1055 | ty::CoroutineWitness(..)
1056 | ty::Pat(..)
1057 | ty::Never
1058 | ty::Tuple(..)
1059 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1060
1061 ty::Param(_)
1065 | ty::Foreign(_)
1066 | ty::Alias(..)
1067 | ty::Bound(..)
1068 | ty::Placeholder(..)
1069 | ty::Infer(_)
1070 | ty::Error(_) => false,
1071 }
1072 } else if tcx.is_lang_item(trait_ref.def_id, LangItem::PointeeTrait) {
1073 let tail = selcx.tcx().struct_tail_raw(
1074 self_ty,
1075 |ty| {
1076 normalize_with_depth(
1079 selcx,
1080 obligation.param_env,
1081 obligation.cause.clone(),
1082 obligation.recursion_depth + 1,
1083 ty,
1084 )
1085 .value
1086 },
1087 || {},
1088 );
1089
1090 match tail.kind() {
1091 ty::Bool
1092 | ty::Char
1093 | ty::Int(_)
1094 | ty::Uint(_)
1095 | ty::Float(_)
1096 | ty::Str
1097 | ty::Array(..)
1098 | ty::Pat(..)
1099 | ty::Slice(_)
1100 | ty::RawPtr(..)
1101 | ty::Ref(..)
1102 | ty::FnDef(..)
1103 | ty::FnPtr(..)
1104 | ty::Dynamic(..)
1105 | ty::Closure(..)
1106 | ty::CoroutineClosure(..)
1107 | ty::Coroutine(..)
1108 | ty::CoroutineWitness(..)
1109 | ty::Never
1110 | ty::Foreign(_)
1112 | ty::Adt(..)
1115 | ty::Tuple(..)
1117 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..))
1119 | ty::Error(..) => true,
1121
1122 ty::Param(_) | ty::Alias(..)
1126 if self_ty != tail
1127 || selcx.infcx.predicate_must_hold_modulo_regions(
1128 &obligation.with(
1129 selcx.tcx(),
1130 ty::TraitRef::new(
1131 selcx.tcx(),
1132 selcx.tcx().require_lang_item(
1133 LangItem::Sized,
1134 Some(obligation.cause.span),
1135 ),
1136 [self_ty],
1137 ),
1138 ),
1139 ) =>
1140 {
1141 true
1142 }
1143
1144 ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
1145
1146 ty::Param(_)
1148 | ty::Alias(..)
1149 | ty::Bound(..)
1150 | ty::Placeholder(..)
1151 | ty::Infer(..) => {
1152 if tail.has_infer_types() {
1153 candidate_set.mark_ambiguous();
1154 }
1155 false
1156 }
1157 }
1158 } else if tcx.trait_is_auto(trait_ref.def_id) {
1159 tcx.dcx().span_delayed_bug(
1160 tcx.def_span(obligation.predicate.def_id),
1161 "associated types not allowed on auto traits",
1162 );
1163 false
1164 } else {
1165 bug!("unexpected builtin trait with associated type: {trait_ref:?}")
1166 }
1167 }
1168 ImplSource::Param(..) => {
1169 false
1195 }
1196 ImplSource::Builtin(BuiltinImplSource::Object { .. }, _) => {
1197 false
1201 }
1202 ImplSource::Builtin(BuiltinImplSource::TraitUpcasting { .. }, _) => {
1203 selcx.tcx().dcx().span_delayed_bug(
1205 obligation.cause.span,
1206 format!("Cannot project an associated type from `{impl_source:?}`"),
1207 );
1208 return Err(());
1209 }
1210 };
1211
1212 if eligible {
1213 if candidate_set.push_candidate(ProjectionCandidate::Select(impl_source)) {
1214 Ok(())
1215 } else {
1216 Err(())
1217 }
1218 } else {
1219 Err(())
1220 }
1221 });
1222}
1223
1224fn confirm_candidate<'cx, 'tcx>(
1225 selcx: &mut SelectionContext<'cx, 'tcx>,
1226 obligation: &ProjectionTermObligation<'tcx>,
1227 candidate: ProjectionCandidate<'tcx>,
1228) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
1229 debug!(?obligation, ?candidate, "confirm_candidate");
1230 let mut result = match candidate {
1231 ProjectionCandidate::ParamEnv(poly_projection)
1232 | ProjectionCandidate::Object(poly_projection) => Ok(Projected::Progress(
1233 confirm_param_env_candidate(selcx, obligation, poly_projection, false),
1234 )),
1235 ProjectionCandidate::TraitDef(poly_projection) => Ok(Projected::Progress(
1236 confirm_param_env_candidate(selcx, obligation, poly_projection, true),
1237 )),
1238 ProjectionCandidate::Select(impl_source) => {
1239 confirm_select_candidate(selcx, obligation, impl_source)
1240 }
1241 };
1242
1243 if let Ok(Projected::Progress(progress)) = &mut result
1249 && progress.term.has_infer_regions()
1250 {
1251 progress.term = progress.term.fold_with(&mut OpportunisticRegionResolver::new(selcx.infcx));
1252 }
1253
1254 result
1255}
1256
1257fn confirm_select_candidate<'cx, 'tcx>(
1258 selcx: &mut SelectionContext<'cx, 'tcx>,
1259 obligation: &ProjectionTermObligation<'tcx>,
1260 impl_source: Selection<'tcx>,
1261) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
1262 match impl_source {
1263 ImplSource::UserDefined(data) => confirm_impl_candidate(selcx, obligation, data),
1264 ImplSource::Builtin(BuiltinImplSource::Misc | BuiltinImplSource::Trivial, data) => {
1265 let tcx = selcx.tcx();
1266 let trait_def_id = obligation.predicate.trait_def_id(tcx);
1267 let progress = if tcx.is_lang_item(trait_def_id, LangItem::Coroutine) {
1268 confirm_coroutine_candidate(selcx, obligation, data)
1269 } else if tcx.is_lang_item(trait_def_id, LangItem::Future) {
1270 confirm_future_candidate(selcx, obligation, data)
1271 } else if tcx.is_lang_item(trait_def_id, LangItem::Iterator) {
1272 confirm_iterator_candidate(selcx, obligation, data)
1273 } else if tcx.is_lang_item(trait_def_id, LangItem::AsyncIterator) {
1274 confirm_async_iterator_candidate(selcx, obligation, data)
1275 } else if selcx.tcx().fn_trait_kind_from_def_id(trait_def_id).is_some() {
1276 if obligation.predicate.self_ty().is_closure()
1277 || obligation.predicate.self_ty().is_coroutine_closure()
1278 {
1279 confirm_closure_candidate(selcx, obligation, data)
1280 } else {
1281 confirm_fn_pointer_candidate(selcx, obligation, data)
1282 }
1283 } else if selcx.tcx().async_fn_trait_kind_from_def_id(trait_def_id).is_some() {
1284 confirm_async_closure_candidate(selcx, obligation, data)
1285 } else if tcx.is_lang_item(trait_def_id, LangItem::AsyncFnKindHelper) {
1286 confirm_async_fn_kind_helper_candidate(selcx, obligation, data)
1287 } else {
1288 confirm_builtin_candidate(selcx, obligation, data)
1289 };
1290 Ok(Projected::Progress(progress))
1291 }
1292 ImplSource::Builtin(BuiltinImplSource::Object { .. }, _)
1293 | ImplSource::Param(..)
1294 | ImplSource::Builtin(BuiltinImplSource::TraitUpcasting { .. }, _) => {
1295 span_bug!(
1297 obligation.cause.span,
1298 "Cannot project an associated type from `{:?}`",
1299 impl_source
1300 )
1301 }
1302 }
1303}
1304
1305fn confirm_coroutine_candidate<'cx, 'tcx>(
1306 selcx: &mut SelectionContext<'cx, 'tcx>,
1307 obligation: &ProjectionTermObligation<'tcx>,
1308 nested: PredicateObligations<'tcx>,
1309) -> Progress<'tcx> {
1310 let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1311 let ty::Coroutine(_, args) = self_ty.kind() else {
1312 unreachable!(
1313 "expected coroutine self type for built-in coroutine candidate, found {self_ty}"
1314 )
1315 };
1316 let coroutine_sig = args.as_coroutine().sig();
1317 let Normalized { value: coroutine_sig, obligations } = normalize_with_depth(
1318 selcx,
1319 obligation.param_env,
1320 obligation.cause.clone(),
1321 obligation.recursion_depth + 1,
1322 coroutine_sig,
1323 );
1324
1325 debug!(?obligation, ?coroutine_sig, ?obligations, "confirm_coroutine_candidate");
1326
1327 let tcx = selcx.tcx();
1328
1329 let coroutine_def_id = tcx.require_lang_item(LangItem::Coroutine, None);
1330
1331 let (trait_ref, yield_ty, return_ty) = super::util::coroutine_trait_ref_and_outputs(
1332 tcx,
1333 coroutine_def_id,
1334 obligation.predicate.self_ty(),
1335 coroutine_sig,
1336 );
1337
1338 let ty = if tcx.is_lang_item(obligation.predicate.def_id, LangItem::CoroutineReturn) {
1339 return_ty
1340 } else if tcx.is_lang_item(obligation.predicate.def_id, LangItem::CoroutineYield) {
1341 yield_ty
1342 } else {
1343 span_bug!(
1344 tcx.def_span(obligation.predicate.def_id),
1345 "unexpected associated type: `Coroutine::{}`",
1346 tcx.item_name(obligation.predicate.def_id),
1347 );
1348 };
1349
1350 let predicate = ty::ProjectionPredicate {
1351 projection_term: ty::AliasTerm::new_from_args(
1352 tcx,
1353 obligation.predicate.def_id,
1354 trait_ref.args,
1355 ),
1356 term: ty.into(),
1357 };
1358
1359 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1360 .with_addl_obligations(nested)
1361 .with_addl_obligations(obligations)
1362}
1363
1364fn confirm_future_candidate<'cx, 'tcx>(
1365 selcx: &mut SelectionContext<'cx, 'tcx>,
1366 obligation: &ProjectionTermObligation<'tcx>,
1367 nested: PredicateObligations<'tcx>,
1368) -> Progress<'tcx> {
1369 let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1370 let ty::Coroutine(_, args) = self_ty.kind() else {
1371 unreachable!(
1372 "expected coroutine self type for built-in async future candidate, found {self_ty}"
1373 )
1374 };
1375 let coroutine_sig = args.as_coroutine().sig();
1376 let Normalized { value: coroutine_sig, obligations } = normalize_with_depth(
1377 selcx,
1378 obligation.param_env,
1379 obligation.cause.clone(),
1380 obligation.recursion_depth + 1,
1381 coroutine_sig,
1382 );
1383
1384 debug!(?obligation, ?coroutine_sig, ?obligations, "confirm_future_candidate");
1385
1386 let tcx = selcx.tcx();
1387 let fut_def_id = tcx.require_lang_item(LangItem::Future, None);
1388
1389 let (trait_ref, return_ty) = super::util::future_trait_ref_and_outputs(
1390 tcx,
1391 fut_def_id,
1392 obligation.predicate.self_ty(),
1393 coroutine_sig,
1394 );
1395
1396 debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name(), sym::Output);
1397
1398 let predicate = ty::ProjectionPredicate {
1399 projection_term: ty::AliasTerm::new_from_args(
1400 tcx,
1401 obligation.predicate.def_id,
1402 trait_ref.args,
1403 ),
1404 term: return_ty.into(),
1405 };
1406
1407 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1408 .with_addl_obligations(nested)
1409 .with_addl_obligations(obligations)
1410}
1411
1412fn confirm_iterator_candidate<'cx, 'tcx>(
1413 selcx: &mut SelectionContext<'cx, 'tcx>,
1414 obligation: &ProjectionTermObligation<'tcx>,
1415 nested: PredicateObligations<'tcx>,
1416) -> Progress<'tcx> {
1417 let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1418 let ty::Coroutine(_, args) = self_ty.kind() else {
1419 unreachable!("expected coroutine self type for built-in gen candidate, found {self_ty}")
1420 };
1421 let gen_sig = args.as_coroutine().sig();
1422 let Normalized { value: gen_sig, obligations } = normalize_with_depth(
1423 selcx,
1424 obligation.param_env,
1425 obligation.cause.clone(),
1426 obligation.recursion_depth + 1,
1427 gen_sig,
1428 );
1429
1430 debug!(?obligation, ?gen_sig, ?obligations, "confirm_iterator_candidate");
1431
1432 let tcx = selcx.tcx();
1433 let iter_def_id = tcx.require_lang_item(LangItem::Iterator, None);
1434
1435 let (trait_ref, yield_ty) = super::util::iterator_trait_ref_and_outputs(
1436 tcx,
1437 iter_def_id,
1438 obligation.predicate.self_ty(),
1439 gen_sig,
1440 );
1441
1442 debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name(), sym::Item);
1443
1444 let predicate = ty::ProjectionPredicate {
1445 projection_term: ty::AliasTerm::new_from_args(
1446 tcx,
1447 obligation.predicate.def_id,
1448 trait_ref.args,
1449 ),
1450 term: yield_ty.into(),
1451 };
1452
1453 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1454 .with_addl_obligations(nested)
1455 .with_addl_obligations(obligations)
1456}
1457
1458fn confirm_async_iterator_candidate<'cx, 'tcx>(
1459 selcx: &mut SelectionContext<'cx, 'tcx>,
1460 obligation: &ProjectionTermObligation<'tcx>,
1461 nested: PredicateObligations<'tcx>,
1462) -> Progress<'tcx> {
1463 let ty::Coroutine(_, args) = selcx.infcx.shallow_resolve(obligation.predicate.self_ty()).kind()
1464 else {
1465 unreachable!()
1466 };
1467 let gen_sig = args.as_coroutine().sig();
1468 let Normalized { value: gen_sig, obligations } = normalize_with_depth(
1469 selcx,
1470 obligation.param_env,
1471 obligation.cause.clone(),
1472 obligation.recursion_depth + 1,
1473 gen_sig,
1474 );
1475
1476 debug!(?obligation, ?gen_sig, ?obligations, "confirm_async_iterator_candidate");
1477
1478 let tcx = selcx.tcx();
1479 let iter_def_id = tcx.require_lang_item(LangItem::AsyncIterator, None);
1480
1481 let (trait_ref, yield_ty) = super::util::async_iterator_trait_ref_and_outputs(
1482 tcx,
1483 iter_def_id,
1484 obligation.predicate.self_ty(),
1485 gen_sig,
1486 );
1487
1488 debug_assert_eq!(tcx.associated_item(obligation.predicate.def_id).name(), sym::Item);
1489
1490 let ty::Adt(_poll_adt, args) = *yield_ty.kind() else {
1491 bug!();
1492 };
1493 let ty::Adt(_option_adt, args) = *args.type_at(0).kind() else {
1494 bug!();
1495 };
1496 let item_ty = args.type_at(0);
1497
1498 let predicate = ty::ProjectionPredicate {
1499 projection_term: ty::AliasTerm::new_from_args(
1500 tcx,
1501 obligation.predicate.def_id,
1502 trait_ref.args,
1503 ),
1504 term: item_ty.into(),
1505 };
1506
1507 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1508 .with_addl_obligations(nested)
1509 .with_addl_obligations(obligations)
1510}
1511
1512fn confirm_builtin_candidate<'cx, 'tcx>(
1513 selcx: &mut SelectionContext<'cx, 'tcx>,
1514 obligation: &ProjectionTermObligation<'tcx>,
1515 data: PredicateObligations<'tcx>,
1516) -> Progress<'tcx> {
1517 let tcx = selcx.tcx();
1518 let self_ty = obligation.predicate.self_ty();
1519 let item_def_id = obligation.predicate.def_id;
1520 let trait_def_id = tcx.trait_of_item(item_def_id).unwrap();
1521 let args = tcx.mk_args(&[self_ty.into()]);
1522 let (term, obligations) = if tcx.is_lang_item(trait_def_id, LangItem::DiscriminantKind) {
1523 let discriminant_def_id = tcx.require_lang_item(LangItem::Discriminant, None);
1524 assert_eq!(discriminant_def_id, item_def_id);
1525
1526 (self_ty.discriminant_ty(tcx).into(), PredicateObligations::new())
1527 } else if tcx.is_lang_item(trait_def_id, LangItem::AsyncDestruct) {
1528 let destructor_def_id = tcx.associated_item_def_ids(trait_def_id)[0];
1529 assert_eq!(destructor_def_id, item_def_id);
1530
1531 (self_ty.async_destructor_ty(tcx).into(), PredicateObligations::new())
1532 } else if tcx.is_lang_item(trait_def_id, LangItem::PointeeTrait) {
1533 let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, None);
1534 assert_eq!(metadata_def_id, item_def_id);
1535
1536 let mut obligations = PredicateObligations::new();
1537 let normalize = |ty| {
1538 normalize_with_depth_to(
1539 selcx,
1540 obligation.param_env,
1541 obligation.cause.clone(),
1542 obligation.recursion_depth + 1,
1543 ty,
1544 &mut obligations,
1545 )
1546 };
1547 let metadata_ty = self_ty.ptr_metadata_ty_or_tail(tcx, normalize).unwrap_or_else(|tail| {
1548 if tail == self_ty {
1549 let sized_predicate = ty::TraitRef::new(
1554 tcx,
1555 tcx.require_lang_item(LangItem::Sized, Some(obligation.cause.span)),
1556 [self_ty],
1557 );
1558 obligations.push(obligation.with(tcx, sized_predicate));
1559 tcx.types.unit
1560 } else {
1561 Ty::new_projection(tcx, metadata_def_id, [tail])
1564 }
1565 });
1566 (metadata_ty.into(), obligations)
1567 } else {
1568 bug!("unexpected builtin trait with associated type: {:?}", obligation.predicate);
1569 };
1570
1571 let predicate = ty::ProjectionPredicate {
1572 projection_term: ty::AliasTerm::new_from_args(tcx, item_def_id, args),
1573 term,
1574 };
1575
1576 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1577 .with_addl_obligations(obligations)
1578 .with_addl_obligations(data)
1579}
1580
1581fn confirm_fn_pointer_candidate<'cx, 'tcx>(
1582 selcx: &mut SelectionContext<'cx, 'tcx>,
1583 obligation: &ProjectionTermObligation<'tcx>,
1584 nested: PredicateObligations<'tcx>,
1585) -> Progress<'tcx> {
1586 let tcx = selcx.tcx();
1587 let fn_type = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1588 let sig = fn_type.fn_sig(tcx);
1589 let Normalized { value: sig, obligations } = normalize_with_depth(
1590 selcx,
1591 obligation.param_env,
1592 obligation.cause.clone(),
1593 obligation.recursion_depth + 1,
1594 sig,
1595 );
1596
1597 confirm_callable_candidate(selcx, obligation, sig, util::TupleArgumentsFlag::Yes)
1598 .with_addl_obligations(nested)
1599 .with_addl_obligations(obligations)
1600}
1601
1602fn confirm_closure_candidate<'cx, 'tcx>(
1603 selcx: &mut SelectionContext<'cx, 'tcx>,
1604 obligation: &ProjectionTermObligation<'tcx>,
1605 nested: PredicateObligations<'tcx>,
1606) -> Progress<'tcx> {
1607 let tcx = selcx.tcx();
1608 let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1609 let closure_sig = match *self_ty.kind() {
1610 ty::Closure(_, args) => args.as_closure().sig(),
1611
1612 ty::CoroutineClosure(def_id, args) => {
1616 let args = args.as_coroutine_closure();
1617 let kind_ty = args.kind_ty();
1618 args.coroutine_closure_sig().map_bound(|sig| {
1619 let output_ty = if let Some(_) = kind_ty.to_opt_closure_kind()
1623 && !args.tupled_upvars_ty().is_ty_var()
1625 {
1626 sig.to_coroutine_given_kind_and_upvars(
1627 tcx,
1628 args.parent_args(),
1629 tcx.coroutine_for_closure(def_id),
1630 ty::ClosureKind::FnOnce,
1631 tcx.lifetimes.re_static,
1632 args.tupled_upvars_ty(),
1633 args.coroutine_captures_by_ref_ty(),
1634 )
1635 } else {
1636 let upvars_projection_def_id =
1637 tcx.require_lang_item(LangItem::AsyncFnKindUpvars, None);
1638 let tupled_upvars_ty = Ty::new_projection(
1639 tcx,
1640 upvars_projection_def_id,
1641 [
1642 ty::GenericArg::from(kind_ty),
1643 Ty::from_closure_kind(tcx, ty::ClosureKind::FnOnce).into(),
1644 tcx.lifetimes.re_static.into(),
1645 sig.tupled_inputs_ty.into(),
1646 args.tupled_upvars_ty().into(),
1647 args.coroutine_captures_by_ref_ty().into(),
1648 ],
1649 );
1650 sig.to_coroutine(
1651 tcx,
1652 args.parent_args(),
1653 Ty::from_closure_kind(tcx, ty::ClosureKind::FnOnce),
1654 tcx.coroutine_for_closure(def_id),
1655 tupled_upvars_ty,
1656 )
1657 };
1658 tcx.mk_fn_sig(
1659 [sig.tupled_inputs_ty],
1660 output_ty,
1661 sig.c_variadic,
1662 sig.safety,
1663 sig.abi,
1664 )
1665 })
1666 }
1667
1668 _ => {
1669 unreachable!("expected closure self type for closure candidate, found {self_ty}");
1670 }
1671 };
1672
1673 let Normalized { value: closure_sig, obligations } = normalize_with_depth(
1674 selcx,
1675 obligation.param_env,
1676 obligation.cause.clone(),
1677 obligation.recursion_depth + 1,
1678 closure_sig,
1679 );
1680
1681 debug!(?obligation, ?closure_sig, ?obligations, "confirm_closure_candidate");
1682
1683 confirm_callable_candidate(selcx, obligation, closure_sig, util::TupleArgumentsFlag::No)
1684 .with_addl_obligations(nested)
1685 .with_addl_obligations(obligations)
1686}
1687
1688fn confirm_callable_candidate<'cx, 'tcx>(
1689 selcx: &mut SelectionContext<'cx, 'tcx>,
1690 obligation: &ProjectionTermObligation<'tcx>,
1691 fn_sig: ty::PolyFnSig<'tcx>,
1692 flag: util::TupleArgumentsFlag,
1693) -> Progress<'tcx> {
1694 let tcx = selcx.tcx();
1695
1696 debug!(?obligation, ?fn_sig, "confirm_callable_candidate");
1697
1698 let fn_once_def_id = tcx.require_lang_item(LangItem::FnOnce, None);
1699 let fn_once_output_def_id = tcx.require_lang_item(LangItem::FnOnceOutput, None);
1700
1701 let predicate = super::util::closure_trait_ref_and_return_type(
1702 tcx,
1703 fn_once_def_id,
1704 obligation.predicate.self_ty(),
1705 fn_sig,
1706 flag,
1707 )
1708 .map_bound(|(trait_ref, ret_type)| ty::ProjectionPredicate {
1709 projection_term: ty::AliasTerm::new_from_args(tcx, fn_once_output_def_id, trait_ref.args),
1710 term: ret_type.into(),
1711 });
1712
1713 confirm_param_env_candidate(selcx, obligation, predicate, true)
1714}
1715
1716fn confirm_async_closure_candidate<'cx, 'tcx>(
1717 selcx: &mut SelectionContext<'cx, 'tcx>,
1718 obligation: &ProjectionTermObligation<'tcx>,
1719 nested: PredicateObligations<'tcx>,
1720) -> Progress<'tcx> {
1721 let tcx = selcx.tcx();
1722 let self_ty = selcx.infcx.shallow_resolve(obligation.predicate.self_ty());
1723
1724 let goal_kind =
1725 tcx.async_fn_trait_kind_from_def_id(obligation.predicate.trait_def_id(tcx)).unwrap();
1726 let env_region = match goal_kind {
1727 ty::ClosureKind::Fn | ty::ClosureKind::FnMut => obligation.predicate.args.region_at(2),
1728 ty::ClosureKind::FnOnce => tcx.lifetimes.re_static,
1729 };
1730 let item_name = tcx.item_name(obligation.predicate.def_id);
1731
1732 let poly_cache_entry = match *self_ty.kind() {
1733 ty::CoroutineClosure(def_id, args) => {
1734 let args = args.as_coroutine_closure();
1735 let kind_ty = args.kind_ty();
1736 let sig = args.coroutine_closure_sig().skip_binder();
1737
1738 let term = match item_name {
1739 sym::CallOnceFuture | sym::CallRefFuture => {
1740 if let Some(closure_kind) = kind_ty.to_opt_closure_kind()
1741 && !args.tupled_upvars_ty().is_ty_var()
1743 {
1744 if !closure_kind.extends(goal_kind) {
1745 bug!("we should not be confirming if the closure kind is not met");
1746 }
1747 sig.to_coroutine_given_kind_and_upvars(
1748 tcx,
1749 args.parent_args(),
1750 tcx.coroutine_for_closure(def_id),
1751 goal_kind,
1752 env_region,
1753 args.tupled_upvars_ty(),
1754 args.coroutine_captures_by_ref_ty(),
1755 )
1756 } else {
1757 let upvars_projection_def_id =
1758 tcx.require_lang_item(LangItem::AsyncFnKindUpvars, None);
1759 let tupled_upvars_ty = Ty::new_projection(
1768 tcx,
1769 upvars_projection_def_id,
1770 [
1771 ty::GenericArg::from(kind_ty),
1772 Ty::from_closure_kind(tcx, goal_kind).into(),
1773 env_region.into(),
1774 sig.tupled_inputs_ty.into(),
1775 args.tupled_upvars_ty().into(),
1776 args.coroutine_captures_by_ref_ty().into(),
1777 ],
1778 );
1779 sig.to_coroutine(
1780 tcx,
1781 args.parent_args(),
1782 Ty::from_closure_kind(tcx, goal_kind),
1783 tcx.coroutine_for_closure(def_id),
1784 tupled_upvars_ty,
1785 )
1786 }
1787 }
1788 sym::Output => sig.return_ty,
1789 name => bug!("no such associated type: {name}"),
1790 };
1791 let projection_term = match item_name {
1792 sym::CallOnceFuture | sym::Output => ty::AliasTerm::new(
1793 tcx,
1794 obligation.predicate.def_id,
1795 [self_ty, sig.tupled_inputs_ty],
1796 ),
1797 sym::CallRefFuture => ty::AliasTerm::new(
1798 tcx,
1799 obligation.predicate.def_id,
1800 [ty::GenericArg::from(self_ty), sig.tupled_inputs_ty.into(), env_region.into()],
1801 ),
1802 name => bug!("no such associated type: {name}"),
1803 };
1804
1805 args.coroutine_closure_sig()
1806 .rebind(ty::ProjectionPredicate { projection_term, term: term.into() })
1807 }
1808 ty::FnDef(..) | ty::FnPtr(..) => {
1809 let bound_sig = self_ty.fn_sig(tcx);
1810 let sig = bound_sig.skip_binder();
1811
1812 let term = match item_name {
1813 sym::CallOnceFuture | sym::CallRefFuture => sig.output(),
1814 sym::Output => {
1815 let future_output_def_id = tcx.require_lang_item(LangItem::FutureOutput, None);
1816 Ty::new_projection(tcx, future_output_def_id, [sig.output()])
1817 }
1818 name => bug!("no such associated type: {name}"),
1819 };
1820 let projection_term = match item_name {
1821 sym::CallOnceFuture | sym::Output => ty::AliasTerm::new(
1822 tcx,
1823 obligation.predicate.def_id,
1824 [self_ty, Ty::new_tup(tcx, sig.inputs())],
1825 ),
1826 sym::CallRefFuture => ty::AliasTerm::new(
1827 tcx,
1828 obligation.predicate.def_id,
1829 [
1830 ty::GenericArg::from(self_ty),
1831 Ty::new_tup(tcx, sig.inputs()).into(),
1832 env_region.into(),
1833 ],
1834 ),
1835 name => bug!("no such associated type: {name}"),
1836 };
1837
1838 bound_sig.rebind(ty::ProjectionPredicate { projection_term, term: term.into() })
1839 }
1840 ty::Closure(_, args) => {
1841 let args = args.as_closure();
1842 let bound_sig = args.sig();
1843 let sig = bound_sig.skip_binder();
1844
1845 let term = match item_name {
1846 sym::CallOnceFuture | sym::CallRefFuture => sig.output(),
1847 sym::Output => {
1848 let future_output_def_id = tcx.require_lang_item(LangItem::FutureOutput, None);
1849 Ty::new_projection(tcx, future_output_def_id, [sig.output()])
1850 }
1851 name => bug!("no such associated type: {name}"),
1852 };
1853 let projection_term = match item_name {
1854 sym::CallOnceFuture | sym::Output => {
1855 ty::AliasTerm::new(tcx, obligation.predicate.def_id, [self_ty, sig.inputs()[0]])
1856 }
1857 sym::CallRefFuture => ty::AliasTerm::new(
1858 tcx,
1859 obligation.predicate.def_id,
1860 [ty::GenericArg::from(self_ty), sig.inputs()[0].into(), env_region.into()],
1861 ),
1862 name => bug!("no such associated type: {name}"),
1863 };
1864
1865 bound_sig.rebind(ty::ProjectionPredicate { projection_term, term: term.into() })
1866 }
1867 _ => bug!("expected callable type for AsyncFn candidate"),
1868 };
1869
1870 confirm_param_env_candidate(selcx, obligation, poly_cache_entry, true)
1871 .with_addl_obligations(nested)
1872}
1873
1874fn confirm_async_fn_kind_helper_candidate<'cx, 'tcx>(
1875 selcx: &mut SelectionContext<'cx, 'tcx>,
1876 obligation: &ProjectionTermObligation<'tcx>,
1877 nested: PredicateObligations<'tcx>,
1878) -> Progress<'tcx> {
1879 let [
1880 _closure_kind_ty,
1882 goal_kind_ty,
1883 borrow_region,
1884 tupled_inputs_ty,
1885 tupled_upvars_ty,
1886 coroutine_captures_by_ref_ty,
1887 ] = **obligation.predicate.args
1888 else {
1889 bug!();
1890 };
1891
1892 let predicate = ty::ProjectionPredicate {
1893 projection_term: ty::AliasTerm::new_from_args(
1894 selcx.tcx(),
1895 obligation.predicate.def_id,
1896 obligation.predicate.args,
1897 ),
1898 term: ty::CoroutineClosureSignature::tupled_upvars_by_closure_kind(
1899 selcx.tcx(),
1900 goal_kind_ty.expect_ty().to_opt_closure_kind().unwrap(),
1901 tupled_inputs_ty.expect_ty(),
1902 tupled_upvars_ty.expect_ty(),
1903 coroutine_captures_by_ref_ty.expect_ty(),
1904 borrow_region.expect_region(),
1905 )
1906 .into(),
1907 };
1908
1909 confirm_param_env_candidate(selcx, obligation, ty::Binder::dummy(predicate), false)
1910 .with_addl_obligations(nested)
1911}
1912
1913fn confirm_param_env_candidate<'cx, 'tcx>(
1914 selcx: &mut SelectionContext<'cx, 'tcx>,
1915 obligation: &ProjectionTermObligation<'tcx>,
1916 poly_cache_entry: ty::PolyProjectionPredicate<'tcx>,
1917 potentially_unnormalized_candidate: bool,
1918) -> Progress<'tcx> {
1919 let infcx = selcx.infcx;
1920 let cause = &obligation.cause;
1921 let param_env = obligation.param_env;
1922
1923 let cache_entry = infcx.instantiate_binder_with_fresh_vars(
1924 cause.span,
1925 BoundRegionConversionTime::HigherRankedType,
1926 poly_cache_entry,
1927 );
1928
1929 let cache_projection = cache_entry.projection_term;
1930 let mut nested_obligations = PredicateObligations::new();
1931 let obligation_projection = obligation.predicate;
1932 let obligation_projection = ensure_sufficient_stack(|| {
1933 normalize_with_depth_to(
1934 selcx,
1935 obligation.param_env,
1936 obligation.cause.clone(),
1937 obligation.recursion_depth + 1,
1938 obligation_projection,
1939 &mut nested_obligations,
1940 )
1941 });
1942 let cache_projection = if potentially_unnormalized_candidate {
1943 ensure_sufficient_stack(|| {
1944 normalize_with_depth_to(
1945 selcx,
1946 obligation.param_env,
1947 obligation.cause.clone(),
1948 obligation.recursion_depth + 1,
1949 cache_projection,
1950 &mut nested_obligations,
1951 )
1952 })
1953 } else {
1954 cache_projection
1955 };
1956
1957 debug!(?cache_projection, ?obligation_projection);
1958
1959 match infcx.at(cause, param_env).eq(
1960 DefineOpaqueTypes::Yes,
1961 cache_projection,
1962 obligation_projection,
1963 ) {
1964 Ok(InferOk { value: _, obligations }) => {
1965 nested_obligations.extend(obligations);
1966 assoc_ty_own_obligations(selcx, obligation, &mut nested_obligations);
1967 Progress { term: cache_entry.term, obligations: nested_obligations }
1970 }
1971 Err(e) => {
1972 let msg = format!(
1973 "Failed to unify obligation `{obligation:?}` with poly_projection `{poly_cache_entry:?}`: {e:?}",
1974 );
1975 debug!("confirm_param_env_candidate: {}", msg);
1976 let err = Ty::new_error_with_message(infcx.tcx, obligation.cause.span, msg);
1977 Progress { term: err.into(), obligations: PredicateObligations::new() }
1978 }
1979 }
1980}
1981
1982fn confirm_impl_candidate<'cx, 'tcx>(
1983 selcx: &mut SelectionContext<'cx, 'tcx>,
1984 obligation: &ProjectionTermObligation<'tcx>,
1985 impl_impl_source: ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>>,
1986) -> Result<Projected<'tcx>, ProjectionError<'tcx>> {
1987 let tcx = selcx.tcx();
1988
1989 let ImplSourceUserDefinedData { impl_def_id, args, mut nested } = impl_impl_source;
1990
1991 let assoc_item_id = obligation.predicate.def_id;
1992 let trait_def_id = tcx.trait_id_of_impl(impl_def_id).unwrap();
1993
1994 let param_env = obligation.param_env;
1995 let assoc_ty = match specialization_graph::assoc_def(tcx, impl_def_id, assoc_item_id) {
1996 Ok(assoc_ty) => assoc_ty,
1997 Err(guar) => return Ok(Projected::Progress(Progress::error(tcx, guar))),
1998 };
1999
2000 if !assoc_ty.item.defaultness(tcx).has_value() {
2006 debug!(
2007 "confirm_impl_candidate: no associated type {:?} for {:?}",
2008 assoc_ty.item.name(),
2009 obligation.predicate
2010 );
2011 if tcx.impl_self_is_guaranteed_unsized(impl_def_id) {
2012 return Ok(Projected::NoProgress(obligation.predicate.to_term(tcx)));
2017 } else {
2018 return Ok(Projected::Progress(Progress {
2019 term: Ty::new_misc_error(tcx).into(),
2020 obligations: nested,
2021 }));
2022 }
2023 }
2024
2025 let args = obligation.predicate.args.rebase_onto(tcx, trait_def_id, args);
2032 let args = translate_args(selcx.infcx, param_env, impl_def_id, args, assoc_ty.defining_node);
2033 let is_const = matches!(tcx.def_kind(assoc_ty.item.def_id), DefKind::AssocConst);
2034
2035 let term: ty::EarlyBinder<'tcx, ty::Term<'tcx>> = if is_const {
2036 let did = assoc_ty.item.def_id;
2037 let identity_args = crate::traits::GenericArgs::identity_for_item(tcx, did);
2038 let uv = ty::UnevaluatedConst::new(did, identity_args);
2039 ty::EarlyBinder::bind(ty::Const::new_unevaluated(tcx, uv).into())
2040 } else {
2041 tcx.type_of(assoc_ty.item.def_id).map_bound(|ty| ty.into())
2042 };
2043
2044 let progress = if !tcx.check_args_compatible(assoc_ty.item.def_id, args) {
2045 let err = Ty::new_error_with_message(
2046 tcx,
2047 obligation.cause.span,
2048 "impl item and trait item have different parameters",
2049 );
2050 Progress { term: err.into(), obligations: nested }
2051 } else {
2052 assoc_ty_own_obligations(selcx, obligation, &mut nested);
2053 Progress { term: term.instantiate(tcx, args), obligations: nested }
2054 };
2055 Ok(Projected::Progress(progress))
2056}
2057
2058fn assoc_ty_own_obligations<'cx, 'tcx>(
2061 selcx: &mut SelectionContext<'cx, 'tcx>,
2062 obligation: &ProjectionTermObligation<'tcx>,
2063 nested: &mut PredicateObligations<'tcx>,
2064) {
2065 let tcx = selcx.tcx();
2066 let predicates = tcx
2067 .predicates_of(obligation.predicate.def_id)
2068 .instantiate_own(tcx, obligation.predicate.args);
2069 for (predicate, span) in predicates {
2070 let normalized = normalize_with_depth_to(
2071 selcx,
2072 obligation.param_env,
2073 obligation.cause.clone(),
2074 obligation.recursion_depth + 1,
2075 predicate,
2076 nested,
2077 );
2078
2079 let nested_cause = if matches!(
2080 obligation.cause.code(),
2081 ObligationCauseCode::CompareImplItem { .. }
2082 | ObligationCauseCode::CheckAssociatedTypeBounds { .. }
2083 | ObligationCauseCode::AscribeUserTypeProvePredicate(..)
2084 ) {
2085 obligation.cause.clone()
2086 } else {
2087 ObligationCause::new(
2088 obligation.cause.span,
2089 obligation.cause.body_id,
2090 ObligationCauseCode::WhereClause(obligation.predicate.def_id, span),
2091 )
2092 };
2093 nested.push(Obligation::with_depth(
2094 tcx,
2095 nested_cause,
2096 obligation.recursion_depth + 1,
2097 obligation.param_env,
2098 normalized,
2099 ));
2100 }
2101}
2102
2103pub(crate) trait ProjectionCacheKeyExt<'cx, 'tcx>: Sized {
2104 fn from_poly_projection_obligation(
2105 selcx: &mut SelectionContext<'cx, 'tcx>,
2106 obligation: &PolyProjectionObligation<'tcx>,
2107 ) -> Option<Self>;
2108}
2109
2110impl<'cx, 'tcx> ProjectionCacheKeyExt<'cx, 'tcx> for ProjectionCacheKey<'tcx> {
2111 fn from_poly_projection_obligation(
2112 selcx: &mut SelectionContext<'cx, 'tcx>,
2113 obligation: &PolyProjectionObligation<'tcx>,
2114 ) -> Option<Self> {
2115 let infcx = selcx.infcx;
2116 obligation.predicate.no_bound_vars().map(|predicate| {
2119 ProjectionCacheKey::new(
2120 infcx.resolve_vars_if_possible(predicate.projection_term),
2125 obligation.param_env,
2126 )
2127 })
2128 }
2129}