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::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#[derive(Debug)]
43pub enum ProjectionError<'tcx> {
44 TooManyCandidates,
46
47 TraitSelectionError(SelectionError<'tcx>),
49}
50
51#[derive(PartialEq, Eq, Debug)]
52enum ProjectionCandidate<'tcx> {
53 ParamEnv(ty::PolyProjectionPredicate<'tcx>),
55
56 TraitDef(ty::PolyProjectionPredicate<'tcx>),
59
60 Object(ty::PolyProjectionPredicate<'tcx>),
62
63 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 fn push_candidate(&mut self, candidate: ProjectionCandidate<'tcx>) -> bool {
87 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 if current == &candidate {
107 return false;
108 }
109
110 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 let () = convert_to_ambiguous;
137 *self = ProjectionCandidateSet::Ambiguous;
138 false
139 }
140}
141
142pub(super) enum ProjectAndUnifyResult<'tcx> {
151 Holds(PredicateObligations<'tcx>),
156 FailedNormalization,
159 Recursive,
162 MismatchedProjectionTypes(MismatchedProjectionTypes<'tcx>),
165}
166
167#[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#[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_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 selcx
279 .infcx
280 .projection_term_to_infer(param_env, alias_term, cause, depth + 1, obligations)
281 .into()
282 })
283}
284
285#[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 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 debug!("found cache entry: ambiguous");
325 return Ok(None);
326 }
327 Err(ProjectionCacheEntry::InProgress) => {
328 debug!("found cache entry: in-progress");
337
338 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 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 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 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 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
437fn 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#[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 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 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 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
561pub 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 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#[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 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 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 } 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 ProjectionCandidateSet::Error(e) => Err(ProjectionError::TraitSelectionError(e)),
701 ProjectionCandidateSet::Ambiguous => Err(ProjectionError::TooManyCandidates),
704 }
705}
706
707fn 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
725fn 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 return ControlFlow::Break(());
764 }
765 }
766 ProjectionMatchesProjection::Ambiguous => {
767 candidate_set.mark_ambiguous();
768 }
769 ProjectionMatchesProjection::No => {}
770 }
771
772 ControlFlow::Continue(())
773 },
774 || ambiguous = true,
777 );
778
779 if ambiguous {
780 candidate_set.mark_ambiguous();
781 }
782}
783
784fn 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 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 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 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 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 true
948 } else {
949 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 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 Err(ErrorGuaranteed { .. }) => true,
979 }
980 }
981 ImplSource::Builtin(BuiltinImplSource::Misc | BuiltinImplSource::Trivial, _) => {
982 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 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 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..)) => true,
1044
1045 ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
1046
1047 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 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 | ty::Foreign(_)
1097 | ty::Adt(..)
1100 | ty::Tuple(..)
1102 | ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(..))
1104 | ty::Error(..) => true,
1106
1107 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 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 false
1183 }
1184 ImplSource::Builtin(BuiltinImplSource::Object { .. }, _) => {
1185 false
1189 }
1190 ImplSource::Builtin(BuiltinImplSource::TraitUpcasting { .. }, _) => {
1191 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
1212fn 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 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
1246fn 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 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 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 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 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 let output_ty = if let Some(_) = kind_ty.to_opt_closure_kind()
1609 && !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 && !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 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 _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
1902fn 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
1970fn 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 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 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 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
2062fn 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 obligation.predicate.no_bound_vars().map(|predicate| {
2124 ProjectionCacheKey::new(
2125 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 super::evaluate_const(selcx.infcx, alias_ct, param_env)
2146}