1pub(super) mod structural_traits;
4
5use std::cell::Cell;
6use std::ops::ControlFlow;
7
8use derive_where::derive_where;
9use rustc_type_ir::inherent::*;
10use rustc_type_ir::lang_items::SolverTraitLangItem;
11use rustc_type_ir::search_graph::CandidateHeadUsages;
12use rustc_type_ir::solve::SizedTraitKind;
13use rustc_type_ir::{
14 self as ty, Interner, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable,
15 TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, Upcast,
16 elaborate,
17};
18use tracing::{debug, instrument};
19
20use super::trait_goals::TraitGoalProvenVia;
21use super::{has_only_region_constraints, inspect};
22use crate::delegate::SolverDelegate;
23use crate::solve::inspect::ProbeKind;
24use crate::solve::{
25 BuiltinImplSource, CandidateSource, CanonicalResponse, Certainty, EvalCtxt, Goal, GoalSource,
26 MaybeCause, NoSolution, ParamEnvSource, QueryResult, has_no_inference_or_external_constraints,
27};
28
29enum AliasBoundKind {
30 SelfBounds,
31 NonSelfBounds,
32}
33
34#[derive_where(Debug; I: Interner)]
39pub(super) struct Candidate<I: Interner> {
40 pub(super) source: CandidateSource<I>,
41 pub(super) result: CanonicalResponse<I>,
42 pub(super) head_usages: CandidateHeadUsages,
43}
44
45pub(super) trait GoalKind<D, I = <D as SolverDelegate>::Interner>:
47 TypeFoldable<I> + Copy + Eq + std::fmt::Display
48where
49 D: SolverDelegate<Interner = I>,
50 I: Interner,
51{
52 fn self_ty(self) -> I::Ty;
53
54 fn trait_ref(self, cx: I) -> ty::TraitRef<I>;
55
56 fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self;
57
58 fn trait_def_id(self, cx: I) -> I::TraitId;
59
60 fn probe_and_consider_implied_clause(
64 ecx: &mut EvalCtxt<'_, D>,
65 parent_source: CandidateSource<I>,
66 goal: Goal<I, Self>,
67 assumption: I::Clause,
68 requirements: impl IntoIterator<Item = (GoalSource, Goal<I, I::Predicate>)>,
69 ) -> Result<Candidate<I>, NoSolution> {
70 Self::probe_and_match_goal_against_assumption(ecx, parent_source, goal, assumption, |ecx| {
71 for (nested_source, goal) in requirements {
72 ecx.add_goal(nested_source, goal);
73 }
74 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
75 })
76 }
77
78 fn probe_and_consider_object_bound_candidate(
82 ecx: &mut EvalCtxt<'_, D>,
83 source: CandidateSource<I>,
84 goal: Goal<I, Self>,
85 assumption: I::Clause,
86 ) -> Result<Candidate<I>, NoSolution> {
87 Self::probe_and_match_goal_against_assumption(ecx, source, goal, assumption, |ecx| {
88 let cx = ecx.cx();
89 let ty::Dynamic(bounds, _, _) = goal.predicate.self_ty().kind() else {
90 panic!("expected object type in `probe_and_consider_object_bound_candidate`");
91 };
92 match structural_traits::predicates_for_object_candidate(
93 ecx,
94 goal.param_env,
95 goal.predicate.trait_ref(cx),
96 bounds,
97 ) {
98 Ok(requirements) => {
99 ecx.add_goals(GoalSource::ImplWhereBound, requirements);
100 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
101 }
102 Err(_) => {
103 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
104 }
105 }
106 })
107 }
108
109 fn consider_additional_alias_assumptions(
113 ecx: &mut EvalCtxt<'_, D>,
114 goal: Goal<I, Self>,
115 alias_ty: ty::AliasTy<I>,
116 ) -> Vec<Candidate<I>>;
117
118 fn probe_and_consider_param_env_candidate(
119 ecx: &mut EvalCtxt<'_, D>,
120 goal: Goal<I, Self>,
121 assumption: I::Clause,
122 ) -> Result<Candidate<I>, CandidateHeadUsages> {
123 match Self::fast_reject_assumption(ecx, goal, assumption) {
124 Ok(()) => {}
125 Err(NoSolution) => return Err(CandidateHeadUsages::default()),
126 }
127
128 let source = Cell::new(CandidateSource::ParamEnv(ParamEnvSource::Global));
135 let (result, head_usages) = ecx
136 .probe(|result: &QueryResult<I>| inspect::ProbeKind::TraitCandidate {
137 source: source.get(),
138 result: *result,
139 })
140 .enter_single_candidate(|ecx| {
141 Self::match_assumption(ecx, goal, assumption, |ecx| {
142 ecx.try_evaluate_added_goals()?;
143 source.set(ecx.characterize_param_env_assumption(goal.param_env, assumption)?);
144 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
145 })
146 });
147
148 match result {
149 Ok(result) => Ok(Candidate { source: source.get(), result, head_usages }),
150 Err(NoSolution) => Err(head_usages),
151 }
152 }
153
154 fn probe_and_match_goal_against_assumption(
159 ecx: &mut EvalCtxt<'_, D>,
160 source: CandidateSource<I>,
161 goal: Goal<I, Self>,
162 assumption: I::Clause,
163 then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
164 ) -> Result<Candidate<I>, NoSolution> {
165 Self::fast_reject_assumption(ecx, goal, assumption)?;
166
167 ecx.probe_trait_candidate(source)
168 .enter(|ecx| Self::match_assumption(ecx, goal, assumption, then))
169 }
170
171 fn fast_reject_assumption(
174 ecx: &mut EvalCtxt<'_, D>,
175 goal: Goal<I, Self>,
176 assumption: I::Clause,
177 ) -> Result<(), NoSolution>;
178
179 fn match_assumption(
181 ecx: &mut EvalCtxt<'_, D>,
182 goal: Goal<I, Self>,
183 assumption: I::Clause,
184 then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
185 ) -> QueryResult<I>;
186
187 fn consider_impl_candidate(
188 ecx: &mut EvalCtxt<'_, D>,
189 goal: Goal<I, Self>,
190 impl_def_id: I::ImplId,
191 then: impl FnOnce(&mut EvalCtxt<'_, D>, Certainty) -> QueryResult<I>,
192 ) -> Result<Candidate<I>, NoSolution>;
193
194 fn consider_error_guaranteed_candidate(
201 ecx: &mut EvalCtxt<'_, D>,
202 guar: I::ErrorGuaranteed,
203 ) -> Result<Candidate<I>, NoSolution>;
204
205 fn consider_auto_trait_candidate(
210 ecx: &mut EvalCtxt<'_, D>,
211 goal: Goal<I, Self>,
212 ) -> Result<Candidate<I>, NoSolution>;
213
214 fn consider_trait_alias_candidate(
216 ecx: &mut EvalCtxt<'_, D>,
217 goal: Goal<I, Self>,
218 ) -> Result<Candidate<I>, NoSolution>;
219
220 fn consider_builtin_sizedness_candidates(
226 ecx: &mut EvalCtxt<'_, D>,
227 goal: Goal<I, Self>,
228 sizedness: SizedTraitKind,
229 ) -> Result<Candidate<I>, NoSolution>;
230
231 fn consider_builtin_copy_clone_candidate(
236 ecx: &mut EvalCtxt<'_, D>,
237 goal: Goal<I, Self>,
238 ) -> Result<Candidate<I>, NoSolution>;
239
240 fn consider_builtin_fn_ptr_trait_candidate(
242 ecx: &mut EvalCtxt<'_, D>,
243 goal: Goal<I, Self>,
244 ) -> Result<Candidate<I>, NoSolution>;
245
246 fn consider_builtin_fn_trait_candidates(
249 ecx: &mut EvalCtxt<'_, D>,
250 goal: Goal<I, Self>,
251 kind: ty::ClosureKind,
252 ) -> Result<Candidate<I>, NoSolution>;
253
254 fn consider_builtin_async_fn_trait_candidates(
257 ecx: &mut EvalCtxt<'_, D>,
258 goal: Goal<I, Self>,
259 kind: ty::ClosureKind,
260 ) -> Result<Candidate<I>, NoSolution>;
261
262 fn consider_builtin_async_fn_kind_helper_candidate(
266 ecx: &mut EvalCtxt<'_, D>,
267 goal: Goal<I, Self>,
268 ) -> Result<Candidate<I>, NoSolution>;
269
270 fn consider_builtin_tuple_candidate(
272 ecx: &mut EvalCtxt<'_, D>,
273 goal: Goal<I, Self>,
274 ) -> Result<Candidate<I>, NoSolution>;
275
276 fn consider_builtin_pointee_candidate(
282 ecx: &mut EvalCtxt<'_, D>,
283 goal: Goal<I, Self>,
284 ) -> Result<Candidate<I>, NoSolution>;
285
286 fn consider_builtin_future_candidate(
290 ecx: &mut EvalCtxt<'_, D>,
291 goal: Goal<I, Self>,
292 ) -> Result<Candidate<I>, NoSolution>;
293
294 fn consider_builtin_iterator_candidate(
298 ecx: &mut EvalCtxt<'_, D>,
299 goal: Goal<I, Self>,
300 ) -> Result<Candidate<I>, NoSolution>;
301
302 fn consider_builtin_fused_iterator_candidate(
305 ecx: &mut EvalCtxt<'_, D>,
306 goal: Goal<I, Self>,
307 ) -> Result<Candidate<I>, NoSolution>;
308
309 fn consider_builtin_async_iterator_candidate(
310 ecx: &mut EvalCtxt<'_, D>,
311 goal: Goal<I, Self>,
312 ) -> Result<Candidate<I>, NoSolution>;
313
314 fn consider_builtin_coroutine_candidate(
318 ecx: &mut EvalCtxt<'_, D>,
319 goal: Goal<I, Self>,
320 ) -> Result<Candidate<I>, NoSolution>;
321
322 fn consider_builtin_discriminant_kind_candidate(
323 ecx: &mut EvalCtxt<'_, D>,
324 goal: Goal<I, Self>,
325 ) -> Result<Candidate<I>, NoSolution>;
326
327 fn consider_builtin_destruct_candidate(
328 ecx: &mut EvalCtxt<'_, D>,
329 goal: Goal<I, Self>,
330 ) -> Result<Candidate<I>, NoSolution>;
331
332 fn consider_builtin_transmute_candidate(
333 ecx: &mut EvalCtxt<'_, D>,
334 goal: Goal<I, Self>,
335 ) -> Result<Candidate<I>, NoSolution>;
336
337 fn consider_builtin_bikeshed_guaranteed_no_drop_candidate(
338 ecx: &mut EvalCtxt<'_, D>,
339 goal: Goal<I, Self>,
340 ) -> Result<Candidate<I>, NoSolution>;
341
342 fn consider_structural_builtin_unsize_candidates(
350 ecx: &mut EvalCtxt<'_, D>,
351 goal: Goal<I, Self>,
352 ) -> Vec<Candidate<I>>;
353}
354
355pub(super) enum AssembleCandidatesFrom {
363 All,
364 EnvAndBounds,
368}
369
370impl AssembleCandidatesFrom {
371 fn should_assemble_impl_candidates(&self) -> bool {
372 match self {
373 AssembleCandidatesFrom::All => true,
374 AssembleCandidatesFrom::EnvAndBounds => false,
375 }
376 }
377}
378
379#[derive(Debug)]
388pub(super) struct FailedCandidateInfo {
389 pub param_env_head_usages: CandidateHeadUsages,
390}
391
392impl<D, I> EvalCtxt<'_, D>
393where
394 D: SolverDelegate<Interner = I>,
395 I: Interner,
396{
397 pub(super) fn assemble_and_evaluate_candidates<G: GoalKind<D>>(
398 &mut self,
399 goal: Goal<I, G>,
400 assemble_from: AssembleCandidatesFrom,
401 ) -> (Vec<Candidate<I>>, FailedCandidateInfo) {
402 let mut candidates = vec![];
403 let mut failed_candidate_info =
404 FailedCandidateInfo { param_env_head_usages: CandidateHeadUsages::default() };
405 let Ok(normalized_self_ty) =
406 self.structurally_normalize_ty(goal.param_env, goal.predicate.self_ty())
407 else {
408 return (candidates, failed_candidate_info);
409 };
410
411 let goal: Goal<I, G> = goal
412 .with(self.cx(), goal.predicate.with_replaced_self_ty(self.cx(), normalized_self_ty));
413
414 if normalized_self_ty.is_ty_var() {
415 debug!("self type has been normalized to infer");
416 self.try_assemble_bounds_via_registered_opaques(goal, assemble_from, &mut candidates);
417 return (candidates, failed_candidate_info);
418 }
419
420 let goal = self.resolve_vars_if_possible(goal);
423
424 if let TypingMode::Coherence = self.typing_mode()
425 && let Ok(candidate) = self.consider_coherence_unknowable_candidate(goal)
426 {
427 candidates.push(candidate);
428 return (candidates, failed_candidate_info);
429 }
430
431 self.assemble_alias_bound_candidates(goal, &mut candidates);
432 self.assemble_param_env_candidates(goal, &mut candidates, &mut failed_candidate_info);
433
434 match assemble_from {
435 AssembleCandidatesFrom::All => {
436 self.assemble_builtin_impl_candidates(goal, &mut candidates);
437 if TypingMode::Coherence == self.typing_mode()
449 || !candidates.iter().any(|c| {
450 matches!(
451 c.source,
452 CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)
453 | CandidateSource::AliasBound
454 ) && has_no_inference_or_external_constraints(c.result)
455 })
456 {
457 self.assemble_impl_candidates(goal, &mut candidates);
458 self.assemble_object_bound_candidates(goal, &mut candidates);
459 }
460 }
461 AssembleCandidatesFrom::EnvAndBounds => {}
462 }
463
464 (candidates, failed_candidate_info)
465 }
466
467 pub(super) fn forced_ambiguity(
468 &mut self,
469 cause: MaybeCause,
470 ) -> Result<Candidate<I>, NoSolution> {
471 let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
477 let certainty = Certainty::Maybe(cause);
478 self.probe_trait_candidate(source)
479 .enter(|this| this.evaluate_added_goals_and_make_canonical_response(certainty))
480 }
481
482 #[instrument(level = "trace", skip_all)]
483 fn assemble_impl_candidates<G: GoalKind<D>>(
484 &mut self,
485 goal: Goal<I, G>,
486 candidates: &mut Vec<Candidate<I>>,
487 ) {
488 let cx = self.cx();
489 cx.for_each_relevant_impl(
490 goal.predicate.trait_def_id(cx),
491 goal.predicate.self_ty(),
492 |impl_def_id| {
493 if cx.impl_is_default(impl_def_id) {
497 return;
498 }
499 match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
500 ecx.evaluate_added_goals_and_make_canonical_response(certainty)
501 }) {
502 Ok(candidate) => candidates.push(candidate),
503 Err(NoSolution) => (),
504 }
505 },
506 );
507 }
508
509 #[instrument(level = "trace", skip_all)]
510 fn assemble_builtin_impl_candidates<G: GoalKind<D>>(
511 &mut self,
512 goal: Goal<I, G>,
513 candidates: &mut Vec<Candidate<I>>,
514 ) {
515 let cx = self.cx();
516 let trait_def_id = goal.predicate.trait_def_id(cx);
517
518 let result = if let Err(guar) = goal.predicate.error_reported() {
526 G::consider_error_guaranteed_candidate(self, guar)
527 } else if cx.trait_is_auto(trait_def_id) {
528 G::consider_auto_trait_candidate(self, goal)
529 } else if cx.trait_is_alias(trait_def_id) {
530 G::consider_trait_alias_candidate(self, goal)
531 } else {
532 match cx.as_trait_lang_item(trait_def_id) {
533 Some(SolverTraitLangItem::Sized) => {
534 G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::Sized)
535 }
536 Some(SolverTraitLangItem::MetaSized) => {
537 G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::MetaSized)
538 }
539 Some(SolverTraitLangItem::PointeeSized) => {
540 unreachable!("`PointeeSized` is removed during lowering");
541 }
542 Some(SolverTraitLangItem::Copy | SolverTraitLangItem::Clone) => {
543 G::consider_builtin_copy_clone_candidate(self, goal)
544 }
545 Some(SolverTraitLangItem::Fn) => {
546 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
547 }
548 Some(SolverTraitLangItem::FnMut) => {
549 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnMut)
550 }
551 Some(SolverTraitLangItem::FnOnce) => {
552 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnOnce)
553 }
554 Some(SolverTraitLangItem::AsyncFn) => {
555 G::consider_builtin_async_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
556 }
557 Some(SolverTraitLangItem::AsyncFnMut) => {
558 G::consider_builtin_async_fn_trait_candidates(
559 self,
560 goal,
561 ty::ClosureKind::FnMut,
562 )
563 }
564 Some(SolverTraitLangItem::AsyncFnOnce) => {
565 G::consider_builtin_async_fn_trait_candidates(
566 self,
567 goal,
568 ty::ClosureKind::FnOnce,
569 )
570 }
571 Some(SolverTraitLangItem::FnPtrTrait) => {
572 G::consider_builtin_fn_ptr_trait_candidate(self, goal)
573 }
574 Some(SolverTraitLangItem::AsyncFnKindHelper) => {
575 G::consider_builtin_async_fn_kind_helper_candidate(self, goal)
576 }
577 Some(SolverTraitLangItem::Tuple) => G::consider_builtin_tuple_candidate(self, goal),
578 Some(SolverTraitLangItem::PointeeTrait) => {
579 G::consider_builtin_pointee_candidate(self, goal)
580 }
581 Some(SolverTraitLangItem::Future) => {
582 G::consider_builtin_future_candidate(self, goal)
583 }
584 Some(SolverTraitLangItem::Iterator) => {
585 G::consider_builtin_iterator_candidate(self, goal)
586 }
587 Some(SolverTraitLangItem::FusedIterator) => {
588 G::consider_builtin_fused_iterator_candidate(self, goal)
589 }
590 Some(SolverTraitLangItem::AsyncIterator) => {
591 G::consider_builtin_async_iterator_candidate(self, goal)
592 }
593 Some(SolverTraitLangItem::Coroutine) => {
594 G::consider_builtin_coroutine_candidate(self, goal)
595 }
596 Some(SolverTraitLangItem::DiscriminantKind) => {
597 G::consider_builtin_discriminant_kind_candidate(self, goal)
598 }
599 Some(SolverTraitLangItem::Destruct) => {
600 G::consider_builtin_destruct_candidate(self, goal)
601 }
602 Some(SolverTraitLangItem::TransmuteTrait) => {
603 G::consider_builtin_transmute_candidate(self, goal)
604 }
605 Some(SolverTraitLangItem::BikeshedGuaranteedNoDrop) => {
606 G::consider_builtin_bikeshed_guaranteed_no_drop_candidate(self, goal)
607 }
608 _ => Err(NoSolution),
609 }
610 };
611
612 candidates.extend(result);
613
614 if cx.is_trait_lang_item(trait_def_id, SolverTraitLangItem::Unsize) {
617 candidates.extend(G::consider_structural_builtin_unsize_candidates(self, goal));
618 }
619 }
620
621 #[instrument(level = "trace", skip_all)]
622 fn assemble_param_env_candidates<G: GoalKind<D>>(
623 &mut self,
624 goal: Goal<I, G>,
625 candidates: &mut Vec<Candidate<I>>,
626 failed_candidate_info: &mut FailedCandidateInfo,
627 ) {
628 for assumption in goal.param_env.caller_bounds().iter() {
629 match G::probe_and_consider_param_env_candidate(self, goal, assumption) {
630 Ok(candidate) => candidates.push(candidate),
631 Err(head_usages) => {
632 failed_candidate_info.param_env_head_usages.merge_usages(head_usages)
633 }
634 }
635 }
636 }
637
638 #[instrument(level = "trace", skip_all)]
639 fn assemble_alias_bound_candidates<G: GoalKind<D>>(
640 &mut self,
641 goal: Goal<I, G>,
642 candidates: &mut Vec<Candidate<I>>,
643 ) {
644 let () = self.probe(|_| ProbeKind::NormalizedSelfTyAssembly).enter(|ecx| {
645 ecx.assemble_alias_bound_candidates_recur(
646 goal.predicate.self_ty(),
647 goal,
648 candidates,
649 AliasBoundKind::SelfBounds,
650 );
651 });
652 }
653
654 fn assemble_alias_bound_candidates_recur<G: GoalKind<D>>(
664 &mut self,
665 self_ty: I::Ty,
666 goal: Goal<I, G>,
667 candidates: &mut Vec<Candidate<I>>,
668 consider_self_bounds: AliasBoundKind,
669 ) {
670 let (kind, alias_ty) = match self_ty.kind() {
671 ty::Bool
672 | ty::Char
673 | ty::Int(_)
674 | ty::Uint(_)
675 | ty::Float(_)
676 | ty::Adt(_, _)
677 | ty::Foreign(_)
678 | ty::Str
679 | ty::Array(_, _)
680 | ty::Pat(_, _)
681 | ty::Slice(_)
682 | ty::RawPtr(_, _)
683 | ty::Ref(_, _, _)
684 | ty::FnDef(_, _)
685 | ty::FnPtr(..)
686 | ty::UnsafeBinder(_)
687 | ty::Dynamic(..)
688 | ty::Closure(..)
689 | ty::CoroutineClosure(..)
690 | ty::Coroutine(..)
691 | ty::CoroutineWitness(..)
692 | ty::Never
693 | ty::Tuple(_)
694 | ty::Param(_)
695 | ty::Placeholder(..)
696 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
697 | ty::Error(_) => return,
698 ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) | ty::Bound(..) => {
699 panic!("unexpected self type for `{goal:?}`")
700 }
701
702 ty::Infer(ty::TyVar(_)) => {
703 if let Ok(result) =
707 self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
708 {
709 candidates.push(Candidate {
710 source: CandidateSource::AliasBound,
711 result,
712 head_usages: CandidateHeadUsages::default(),
713 });
714 }
715 return;
716 }
717
718 ty::Alias(kind @ (ty::Projection | ty::Opaque), alias_ty) => (kind, alias_ty),
719 ty::Alias(ty::Inherent | ty::Free, _) => {
720 self.cx().delay_bug(format!("could not normalize {self_ty:?}, it is not WF"));
721 return;
722 }
723 };
724
725 match consider_self_bounds {
726 AliasBoundKind::SelfBounds => {
727 for assumption in self
728 .cx()
729 .item_self_bounds(alias_ty.def_id)
730 .iter_instantiated(self.cx(), alias_ty.args)
731 {
732 candidates.extend(G::probe_and_consider_implied_clause(
733 self,
734 CandidateSource::AliasBound,
735 goal,
736 assumption,
737 [],
738 ));
739 }
740 }
741 AliasBoundKind::NonSelfBounds => {
742 for assumption in self
743 .cx()
744 .item_non_self_bounds(alias_ty.def_id)
745 .iter_instantiated(self.cx(), alias_ty.args)
746 {
747 candidates.extend(G::probe_and_consider_implied_clause(
748 self,
749 CandidateSource::AliasBound,
750 goal,
751 assumption,
752 [],
753 ));
754 }
755 }
756 }
757
758 candidates.extend(G::consider_additional_alias_assumptions(self, goal, alias_ty));
759
760 if kind != ty::Projection {
761 return;
762 }
763
764 match self.structurally_normalize_ty(goal.param_env, alias_ty.self_ty()) {
766 Ok(next_self_ty) => self.assemble_alias_bound_candidates_recur(
767 next_self_ty,
768 goal,
769 candidates,
770 AliasBoundKind::NonSelfBounds,
771 ),
772 Err(NoSolution) => {}
773 }
774 }
775
776 #[instrument(level = "trace", skip_all)]
777 fn assemble_object_bound_candidates<G: GoalKind<D>>(
778 &mut self,
779 goal: Goal<I, G>,
780 candidates: &mut Vec<Candidate<I>>,
781 ) {
782 let cx = self.cx();
783 if !cx.trait_may_be_implemented_via_object(goal.predicate.trait_def_id(cx)) {
784 return;
785 }
786
787 let self_ty = goal.predicate.self_ty();
788 let bounds = match self_ty.kind() {
789 ty::Bool
790 | ty::Char
791 | ty::Int(_)
792 | ty::Uint(_)
793 | ty::Float(_)
794 | ty::Adt(_, _)
795 | ty::Foreign(_)
796 | ty::Str
797 | ty::Array(_, _)
798 | ty::Pat(_, _)
799 | ty::Slice(_)
800 | ty::RawPtr(_, _)
801 | ty::Ref(_, _, _)
802 | ty::FnDef(_, _)
803 | ty::FnPtr(..)
804 | ty::UnsafeBinder(_)
805 | ty::Alias(..)
806 | ty::Closure(..)
807 | ty::CoroutineClosure(..)
808 | ty::Coroutine(..)
809 | ty::CoroutineWitness(..)
810 | ty::Never
811 | ty::Tuple(_)
812 | ty::Param(_)
813 | ty::Placeholder(..)
814 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
815 | ty::Error(_) => return,
816 ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
817 | ty::Bound(..) => panic!("unexpected self type for `{goal:?}`"),
818 ty::Dynamic(bounds, ..) => bounds,
819 };
820
821 if bounds.principal_def_id().is_some_and(|def_id| !cx.trait_is_dyn_compatible(def_id)) {
823 return;
824 }
825
826 for bound in bounds.iter() {
830 match bound.skip_binder() {
831 ty::ExistentialPredicate::Trait(_) => {
832 }
834 ty::ExistentialPredicate::Projection(_)
835 | ty::ExistentialPredicate::AutoTrait(_) => {
836 candidates.extend(G::probe_and_consider_object_bound_candidate(
837 self,
838 CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
839 goal,
840 bound.with_self_ty(cx, self_ty),
841 ));
842 }
843 }
844 }
845
846 if let Some(principal) = bounds.principal() {
850 let principal_trait_ref = principal.with_self_ty(cx, self_ty);
851 for (idx, assumption) in elaborate::supertraits(cx, principal_trait_ref).enumerate() {
852 candidates.extend(G::probe_and_consider_object_bound_candidate(
853 self,
854 CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
855 goal,
856 assumption.upcast(cx),
857 ));
858 }
859 }
860 }
861
862 #[instrument(level = "trace", skip_all)]
869 fn consider_coherence_unknowable_candidate<G: GoalKind<D>>(
870 &mut self,
871 goal: Goal<I, G>,
872 ) -> Result<Candidate<I>, NoSolution> {
873 self.probe_trait_candidate(CandidateSource::CoherenceUnknowable).enter(|ecx| {
874 let cx = ecx.cx();
875 let trait_ref = goal.predicate.trait_ref(cx);
876 if ecx.trait_ref_is_knowable(goal.param_env, trait_ref)? {
877 Err(NoSolution)
878 } else {
879 let predicate: I::Predicate = trait_ref.upcast(cx);
885 ecx.add_goals(
886 GoalSource::Misc,
887 elaborate::elaborate(cx, [predicate])
888 .skip(1)
889 .map(|predicate| goal.with(cx, predicate)),
890 );
891 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
892 }
893 })
894 }
895}
896
897pub(super) enum AllowInferenceConstraints {
898 Yes,
899 No,
900}
901
902impl<D, I> EvalCtxt<'_, D>
903where
904 D: SolverDelegate<Interner = I>,
905 I: Interner,
906{
907 pub(super) fn filter_specialized_impls(
911 &mut self,
912 allow_inference_constraints: AllowInferenceConstraints,
913 candidates: &mut Vec<Candidate<I>>,
914 ) {
915 match self.typing_mode() {
916 TypingMode::Coherence => return,
917 TypingMode::Analysis { .. }
918 | TypingMode::Borrowck { .. }
919 | TypingMode::PostBorrowckAnalysis { .. }
920 | TypingMode::PostAnalysis => {}
921 }
922
923 let mut i = 0;
924 'outer: while i < candidates.len() {
925 let CandidateSource::Impl(victim_def_id) = candidates[i].source else {
926 i += 1;
927 continue;
928 };
929
930 for (j, c) in candidates.iter().enumerate() {
931 if i == j {
932 continue;
933 }
934
935 let CandidateSource::Impl(other_def_id) = c.source else {
936 continue;
937 };
938
939 if matches!(allow_inference_constraints, AllowInferenceConstraints::Yes)
946 || has_only_region_constraints(c.result)
947 {
948 if self.cx().impl_specializes(other_def_id, victim_def_id) {
949 candidates.remove(i);
950 continue 'outer;
951 }
952 }
953 }
954
955 i += 1;
956 }
957 }
958
959 fn try_assemble_bounds_via_registered_opaques<G: GoalKind<D>>(
971 &mut self,
972 goal: Goal<I, G>,
973 assemble_from: AssembleCandidatesFrom,
974 candidates: &mut Vec<Candidate<I>>,
975 ) {
976 let self_ty = goal.predicate.self_ty();
977 let mut assemble_blanket_impls = false;
980 for alias_ty in self.opaques_with_sub_unified_hidden_type(self_ty) {
981 assemble_blanket_impls = true;
982 debug!("self ty is sub unified with {alias_ty:?}");
983
984 struct ReplaceOpaque<I: Interner> {
985 cx: I,
986 alias_ty: ty::AliasTy<I>,
987 self_ty: I::Ty,
988 }
989 impl<I: Interner> TypeFolder<I> for ReplaceOpaque<I> {
990 fn cx(&self) -> I {
991 self.cx
992 }
993 fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
994 if let ty::Alias(ty::Opaque, alias_ty) = ty.kind() {
995 if alias_ty == self.alias_ty {
996 return self.self_ty;
997 }
998 }
999 ty.super_fold_with(self)
1000 }
1001 }
1002
1003 for item_bound in self
1011 .cx()
1012 .item_self_bounds(alias_ty.def_id)
1013 .iter_instantiated(self.cx(), alias_ty.args)
1014 {
1015 let assumption =
1016 item_bound.fold_with(&mut ReplaceOpaque { cx: self.cx(), alias_ty, self_ty });
1017 candidates.extend(G::probe_and_match_goal_against_assumption(
1018 self,
1019 CandidateSource::AliasBound,
1020 goal,
1021 assumption,
1022 |ecx| {
1023 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
1026 },
1027 ));
1028 }
1029 }
1030
1031 if assemble_blanket_impls && assemble_from.should_assemble_impl_candidates() {
1035 let cx = self.cx();
1036 cx.for_each_blanket_impl(goal.predicate.trait_def_id(cx), |impl_def_id| {
1037 if cx.impl_is_default(impl_def_id) {
1041 return;
1042 }
1043
1044 match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
1045 if ecx.shallow_resolve(self_ty).is_ty_var() {
1046 let certainty = certainty.and(Certainty::AMBIGUOUS);
1048 ecx.evaluate_added_goals_and_make_canonical_response(certainty)
1049 } else {
1050 Err(NoSolution)
1056 }
1057 }) {
1058 Ok(candidate) => candidates.push(candidate),
1059 Err(NoSolution) => (),
1060 }
1061 });
1062 }
1063
1064 if candidates.is_empty() {
1065 candidates.extend(self.forced_ambiguity(MaybeCause::Ambiguity));
1066 }
1067 }
1068
1069 #[instrument(level = "debug", skip(self, inject_normalize_to_rigid_candidate), ret)]
1100 pub(super) fn assemble_and_merge_candidates<G: GoalKind<D>>(
1101 &mut self,
1102 proven_via: Option<TraitGoalProvenVia>,
1103 goal: Goal<I, G>,
1104 inject_normalize_to_rigid_candidate: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
1105 ) -> QueryResult<I> {
1106 let Some(proven_via) = proven_via else {
1107 return self.forced_ambiguity(MaybeCause::Ambiguity).map(|cand| cand.result);
1114 };
1115
1116 match proven_via {
1117 TraitGoalProvenVia::ParamEnv | TraitGoalProvenVia::AliasBound => {
1118 let (mut candidates, _) = self
1122 .assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::EnvAndBounds);
1123
1124 if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1127 candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1128 } else if candidates.is_empty() {
1129 return inject_normalize_to_rigid_candidate(self);
1132 }
1133
1134 if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1135 Ok(response)
1136 } else {
1137 self.flounder(&candidates)
1138 }
1139 }
1140 TraitGoalProvenVia::Misc => {
1141 let (mut candidates, _) =
1142 self.assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::All);
1143
1144 if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1147 candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1148 }
1149
1150 self.filter_specialized_impls(AllowInferenceConstraints::Yes, &mut candidates);
1156 if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1157 Ok(response)
1158 } else {
1159 self.flounder(&candidates)
1160 }
1161 }
1162 }
1163 }
1164
1165 fn characterize_param_env_assumption(
1179 &mut self,
1180 param_env: I::ParamEnv,
1181 assumption: I::Clause,
1182 ) -> Result<CandidateSource<I>, NoSolution> {
1183 if assumption.has_bound_vars() {
1186 return Ok(CandidateSource::ParamEnv(ParamEnvSource::NonGlobal));
1187 }
1188
1189 match assumption.visit_with(&mut FindParamInClause {
1190 ecx: self,
1191 param_env,
1192 universes: vec![],
1193 }) {
1194 ControlFlow::Break(Err(NoSolution)) => Err(NoSolution),
1195 ControlFlow::Break(Ok(())) => Ok(CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)),
1196 ControlFlow::Continue(()) => Ok(CandidateSource::ParamEnv(ParamEnvSource::Global)),
1197 }
1198 }
1199}
1200
1201struct FindParamInClause<'a, 'b, D: SolverDelegate<Interner = I>, I: Interner> {
1202 ecx: &'a mut EvalCtxt<'b, D>,
1203 param_env: I::ParamEnv,
1204 universes: Vec<Option<ty::UniverseIndex>>,
1205}
1206
1207impl<D, I> TypeVisitor<I> for FindParamInClause<'_, '_, D, I>
1208where
1209 D: SolverDelegate<Interner = I>,
1210 I: Interner,
1211{
1212 type Result = ControlFlow<Result<(), NoSolution>>;
1213
1214 fn visit_binder<T: TypeVisitable<I>>(&mut self, t: &ty::Binder<I, T>) -> Self::Result {
1215 self.universes.push(None);
1216 t.super_visit_with(self)?;
1217 self.universes.pop();
1218 ControlFlow::Continue(())
1219 }
1220
1221 fn visit_ty(&mut self, ty: I::Ty) -> Self::Result {
1222 let ty = self.ecx.replace_bound_vars(ty, &mut self.universes);
1223 let Ok(ty) = self.ecx.structurally_normalize_ty(self.param_env, ty) else {
1224 return ControlFlow::Break(Err(NoSolution));
1225 };
1226
1227 if let ty::Placeholder(p) = ty.kind() {
1228 if p.universe() == ty::UniverseIndex::ROOT {
1229 ControlFlow::Break(Ok(()))
1230 } else {
1231 ControlFlow::Continue(())
1232 }
1233 } else if ty.has_type_flags(TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_RE_INFER) {
1234 ty.super_visit_with(self)
1235 } else {
1236 ControlFlow::Continue(())
1237 }
1238 }
1239
1240 fn visit_const(&mut self, ct: I::Const) -> Self::Result {
1241 let ct = self.ecx.replace_bound_vars(ct, &mut self.universes);
1242 let Ok(ct) = self.ecx.structurally_normalize_const(self.param_env, ct) else {
1243 return ControlFlow::Break(Err(NoSolution));
1244 };
1245
1246 if let ty::ConstKind::Placeholder(p) = ct.kind() {
1247 if p.universe() == ty::UniverseIndex::ROOT {
1248 ControlFlow::Break(Ok(()))
1249 } else {
1250 ControlFlow::Continue(())
1251 }
1252 } else if ct.has_type_flags(TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_RE_INFER) {
1253 ct.super_visit_with(self)
1254 } else {
1255 ControlFlow::Continue(())
1256 }
1257 }
1258
1259 fn visit_region(&mut self, r: I::Region) -> Self::Result {
1260 match self.ecx.eager_resolve_region(r).kind() {
1261 ty::ReStatic | ty::ReError(_) | ty::ReBound(..) => ControlFlow::Continue(()),
1262 ty::RePlaceholder(p) => {
1263 if p.universe() == ty::UniverseIndex::ROOT {
1264 ControlFlow::Break(Ok(()))
1265 } else {
1266 ControlFlow::Continue(())
1267 }
1268 }
1269 ty::ReVar(_) => ControlFlow::Break(Ok(())),
1270 ty::ReErased | ty::ReEarlyParam(_) | ty::ReLateParam(_) => {
1271 unreachable!("unexpected region in param-env clause")
1272 }
1273 }
1274 }
1275}