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::{AliasBoundKind, 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, OpaqueTypesJank, ParamEnvSource, QueryResult,
27 has_no_inference_or_external_constraints,
28};
29
30#[derive_where(Debug; I: Interner)]
35pub(super) struct Candidate<I: Interner> {
36 pub(super) source: CandidateSource<I>,
37 pub(super) result: CanonicalResponse<I>,
38 pub(super) head_usages: CandidateHeadUsages,
39}
40
41pub(super) trait GoalKind<D, I = <D as SolverDelegate>::Interner>:
43 TypeFoldable<I> + Copy + Eq + std::fmt::Display
44where
45 D: SolverDelegate<Interner = I>,
46 I: Interner,
47{
48 fn self_ty(self) -> I::Ty;
49
50 fn trait_ref(self, cx: I) -> ty::TraitRef<I>;
51
52 fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self;
53
54 fn trait_def_id(self, cx: I) -> I::TraitId;
55
56 fn probe_and_consider_implied_clause(
60 ecx: &mut EvalCtxt<'_, D>,
61 parent_source: CandidateSource<I>,
62 goal: Goal<I, Self>,
63 assumption: I::Clause,
64 requirements: impl IntoIterator<Item = (GoalSource, Goal<I, I::Predicate>)>,
65 ) -> Result<Candidate<I>, NoSolution> {
66 Self::probe_and_match_goal_against_assumption(ecx, parent_source, goal, assumption, |ecx| {
67 for (nested_source, goal) in requirements {
68 ecx.add_goal(nested_source, goal);
69 }
70 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
71 })
72 }
73
74 fn probe_and_consider_object_bound_candidate(
78 ecx: &mut EvalCtxt<'_, D>,
79 source: CandidateSource<I>,
80 goal: Goal<I, Self>,
81 assumption: I::Clause,
82 ) -> Result<Candidate<I>, NoSolution> {
83 Self::probe_and_match_goal_against_assumption(ecx, source, goal, assumption, |ecx| {
84 let cx = ecx.cx();
85 let ty::Dynamic(bounds, _) = goal.predicate.self_ty().kind() else {
86 panic!("expected object type in `probe_and_consider_object_bound_candidate`");
87 };
88 match structural_traits::predicates_for_object_candidate(
89 ecx,
90 goal.param_env,
91 goal.predicate.trait_ref(cx),
92 bounds,
93 ) {
94 Ok(requirements) => {
95 ecx.add_goals(GoalSource::ImplWhereBound, requirements);
96 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
97 }
98 Err(_) => {
99 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
100 }
101 }
102 })
103 }
104
105 fn consider_additional_alias_assumptions(
109 ecx: &mut EvalCtxt<'_, D>,
110 goal: Goal<I, Self>,
111 alias_ty: ty::AliasTy<I>,
112 ) -> Vec<Candidate<I>>;
113
114 fn probe_and_consider_param_env_candidate(
115 ecx: &mut EvalCtxt<'_, D>,
116 goal: Goal<I, Self>,
117 assumption: I::Clause,
118 ) -> Result<Candidate<I>, CandidateHeadUsages> {
119 match Self::fast_reject_assumption(ecx, goal, assumption) {
120 Ok(()) => {}
121 Err(NoSolution) => return Err(CandidateHeadUsages::default()),
122 }
123
124 let source = Cell::new(CandidateSource::ParamEnv(ParamEnvSource::Global));
131 let (result, head_usages) = ecx
132 .probe(|result: &QueryResult<I>| inspect::ProbeKind::TraitCandidate {
133 source: source.get(),
134 result: *result,
135 })
136 .enter_single_candidate(|ecx| {
137 Self::match_assumption(ecx, goal, assumption, |ecx| {
138 ecx.try_evaluate_added_goals()?;
139 source.set(ecx.characterize_param_env_assumption(goal.param_env, assumption)?);
140 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
141 })
142 });
143
144 match result {
145 Ok(result) => Ok(Candidate { source: source.get(), result, head_usages }),
146 Err(NoSolution) => Err(head_usages),
147 }
148 }
149
150 fn probe_and_match_goal_against_assumption(
155 ecx: &mut EvalCtxt<'_, D>,
156 source: CandidateSource<I>,
157 goal: Goal<I, Self>,
158 assumption: I::Clause,
159 then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
160 ) -> Result<Candidate<I>, NoSolution> {
161 Self::fast_reject_assumption(ecx, goal, assumption)?;
162
163 ecx.probe_trait_candidate(source)
164 .enter(|ecx| Self::match_assumption(ecx, goal, assumption, then))
165 }
166
167 fn fast_reject_assumption(
170 ecx: &mut EvalCtxt<'_, D>,
171 goal: Goal<I, Self>,
172 assumption: I::Clause,
173 ) -> Result<(), NoSolution>;
174
175 fn match_assumption(
177 ecx: &mut EvalCtxt<'_, D>,
178 goal: Goal<I, Self>,
179 assumption: I::Clause,
180 then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
181 ) -> QueryResult<I>;
182
183 fn consider_impl_candidate(
184 ecx: &mut EvalCtxt<'_, D>,
185 goal: Goal<I, Self>,
186 impl_def_id: I::ImplId,
187 then: impl FnOnce(&mut EvalCtxt<'_, D>, Certainty) -> QueryResult<I>,
188 ) -> Result<Candidate<I>, NoSolution>;
189
190 fn consider_error_guaranteed_candidate(
197 ecx: &mut EvalCtxt<'_, D>,
198 guar: I::ErrorGuaranteed,
199 ) -> Result<Candidate<I>, NoSolution>;
200
201 fn consider_auto_trait_candidate(
206 ecx: &mut EvalCtxt<'_, D>,
207 goal: Goal<I, Self>,
208 ) -> Result<Candidate<I>, NoSolution>;
209
210 fn consider_trait_alias_candidate(
212 ecx: &mut EvalCtxt<'_, D>,
213 goal: Goal<I, Self>,
214 ) -> Result<Candidate<I>, NoSolution>;
215
216 fn consider_builtin_sizedness_candidates(
222 ecx: &mut EvalCtxt<'_, D>,
223 goal: Goal<I, Self>,
224 sizedness: SizedTraitKind,
225 ) -> Result<Candidate<I>, NoSolution>;
226
227 fn consider_builtin_copy_clone_candidate(
232 ecx: &mut EvalCtxt<'_, D>,
233 goal: Goal<I, Self>,
234 ) -> Result<Candidate<I>, NoSolution>;
235
236 fn consider_builtin_fn_ptr_trait_candidate(
238 ecx: &mut EvalCtxt<'_, D>,
239 goal: Goal<I, Self>,
240 ) -> Result<Candidate<I>, NoSolution>;
241
242 fn consider_builtin_fn_trait_candidates(
245 ecx: &mut EvalCtxt<'_, D>,
246 goal: Goal<I, Self>,
247 kind: ty::ClosureKind,
248 ) -> Result<Candidate<I>, NoSolution>;
249
250 fn consider_builtin_async_fn_trait_candidates(
253 ecx: &mut EvalCtxt<'_, D>,
254 goal: Goal<I, Self>,
255 kind: ty::ClosureKind,
256 ) -> Result<Candidate<I>, NoSolution>;
257
258 fn consider_builtin_async_fn_kind_helper_candidate(
262 ecx: &mut EvalCtxt<'_, D>,
263 goal: Goal<I, Self>,
264 ) -> Result<Candidate<I>, NoSolution>;
265
266 fn consider_builtin_tuple_candidate(
268 ecx: &mut EvalCtxt<'_, D>,
269 goal: Goal<I, Self>,
270 ) -> Result<Candidate<I>, NoSolution>;
271
272 fn consider_builtin_pointee_candidate(
278 ecx: &mut EvalCtxt<'_, D>,
279 goal: Goal<I, Self>,
280 ) -> Result<Candidate<I>, NoSolution>;
281
282 fn consider_builtin_future_candidate(
286 ecx: &mut EvalCtxt<'_, D>,
287 goal: Goal<I, Self>,
288 ) -> Result<Candidate<I>, NoSolution>;
289
290 fn consider_builtin_iterator_candidate(
294 ecx: &mut EvalCtxt<'_, D>,
295 goal: Goal<I, Self>,
296 ) -> Result<Candidate<I>, NoSolution>;
297
298 fn consider_builtin_fused_iterator_candidate(
301 ecx: &mut EvalCtxt<'_, D>,
302 goal: Goal<I, Self>,
303 ) -> Result<Candidate<I>, NoSolution>;
304
305 fn consider_builtin_async_iterator_candidate(
306 ecx: &mut EvalCtxt<'_, D>,
307 goal: Goal<I, Self>,
308 ) -> Result<Candidate<I>, NoSolution>;
309
310 fn consider_builtin_coroutine_candidate(
314 ecx: &mut EvalCtxt<'_, D>,
315 goal: Goal<I, Self>,
316 ) -> Result<Candidate<I>, NoSolution>;
317
318 fn consider_builtin_discriminant_kind_candidate(
319 ecx: &mut EvalCtxt<'_, D>,
320 goal: Goal<I, Self>,
321 ) -> Result<Candidate<I>, NoSolution>;
322
323 fn consider_builtin_destruct_candidate(
324 ecx: &mut EvalCtxt<'_, D>,
325 goal: Goal<I, Self>,
326 ) -> Result<Candidate<I>, NoSolution>;
327
328 fn consider_builtin_transmute_candidate(
329 ecx: &mut EvalCtxt<'_, D>,
330 goal: Goal<I, Self>,
331 ) -> Result<Candidate<I>, NoSolution>;
332
333 fn consider_builtin_bikeshed_guaranteed_no_drop_candidate(
334 ecx: &mut EvalCtxt<'_, D>,
335 goal: Goal<I, Self>,
336 ) -> Result<Candidate<I>, NoSolution>;
337
338 fn consider_structural_builtin_unsize_candidates(
346 ecx: &mut EvalCtxt<'_, D>,
347 goal: Goal<I, Self>,
348 ) -> Vec<Candidate<I>>;
349}
350
351pub(super) enum AssembleCandidatesFrom {
359 All,
360 EnvAndBounds,
364}
365
366impl AssembleCandidatesFrom {
367 fn should_assemble_impl_candidates(&self) -> bool {
368 match self {
369 AssembleCandidatesFrom::All => true,
370 AssembleCandidatesFrom::EnvAndBounds => false,
371 }
372 }
373}
374
375#[derive(Debug)]
384pub(super) struct FailedCandidateInfo {
385 pub param_env_head_usages: CandidateHeadUsages,
386}
387
388impl<D, I> EvalCtxt<'_, D>
389where
390 D: SolverDelegate<Interner = I>,
391 I: Interner,
392{
393 pub(super) fn assemble_and_evaluate_candidates<G: GoalKind<D>>(
394 &mut self,
395 goal: Goal<I, G>,
396 assemble_from: AssembleCandidatesFrom,
397 ) -> (Vec<Candidate<I>>, FailedCandidateInfo) {
398 let mut candidates = vec![];
399 let mut failed_candidate_info =
400 FailedCandidateInfo { param_env_head_usages: CandidateHeadUsages::default() };
401 let Ok(normalized_self_ty) =
402 self.structurally_normalize_ty(goal.param_env, goal.predicate.self_ty())
403 else {
404 return (candidates, failed_candidate_info);
405 };
406
407 let goal: Goal<I, G> = goal
408 .with(self.cx(), goal.predicate.with_replaced_self_ty(self.cx(), normalized_self_ty));
409
410 if normalized_self_ty.is_ty_var() {
411 debug!("self type has been normalized to infer");
412 self.try_assemble_bounds_via_registered_opaques(goal, assemble_from, &mut candidates);
413 return (candidates, failed_candidate_info);
414 }
415
416 let goal = self.resolve_vars_if_possible(goal);
419
420 if let TypingMode::Coherence = self.typing_mode()
421 && let Ok(candidate) = self.consider_coherence_unknowable_candidate(goal)
422 {
423 candidates.push(candidate);
424 return (candidates, failed_candidate_info);
425 }
426
427 self.assemble_alias_bound_candidates(goal, &mut candidates);
428 self.assemble_param_env_candidates(goal, &mut candidates, &mut failed_candidate_info);
429
430 match assemble_from {
431 AssembleCandidatesFrom::All => {
432 self.assemble_builtin_impl_candidates(goal, &mut candidates);
433 if TypingMode::Coherence == self.typing_mode()
445 || !candidates.iter().any(|c| {
446 matches!(
447 c.source,
448 CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)
449 | CandidateSource::AliasBound(_)
450 ) && has_no_inference_or_external_constraints(c.result)
451 })
452 {
453 self.assemble_impl_candidates(goal, &mut candidates);
454 self.assemble_object_bound_candidates(goal, &mut candidates);
455 }
456 }
457 AssembleCandidatesFrom::EnvAndBounds => {
458 if matches!(normalized_self_ty.kind(), ty::Dynamic(..))
462 && !candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_)))
463 {
464 self.assemble_object_bound_candidates(goal, &mut candidates);
465 }
466 }
467 }
468
469 (candidates, failed_candidate_info)
470 }
471
472 pub(super) fn forced_ambiguity(
473 &mut self,
474 cause: MaybeCause,
475 ) -> Result<Candidate<I>, NoSolution> {
476 let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
485 let certainty = Certainty::Maybe { cause, opaque_types_jank: OpaqueTypesJank::AllGood };
486 self.probe_trait_candidate(source)
487 .enter(|this| this.evaluate_added_goals_and_make_canonical_response(certainty))
488 }
489
490 #[instrument(level = "trace", skip_all)]
491 fn assemble_impl_candidates<G: GoalKind<D>>(
492 &mut self,
493 goal: Goal<I, G>,
494 candidates: &mut Vec<Candidate<I>>,
495 ) {
496 let cx = self.cx();
497 cx.for_each_relevant_impl(
498 goal.predicate.trait_def_id(cx),
499 goal.predicate.self_ty(),
500 |impl_def_id| {
501 if cx.impl_is_default(impl_def_id) {
505 return;
506 }
507 match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
508 ecx.evaluate_added_goals_and_make_canonical_response(certainty)
509 }) {
510 Ok(candidate) => candidates.push(candidate),
511 Err(NoSolution) => (),
512 }
513 },
514 );
515 }
516
517 #[instrument(level = "trace", skip_all)]
518 fn assemble_builtin_impl_candidates<G: GoalKind<D>>(
519 &mut self,
520 goal: Goal<I, G>,
521 candidates: &mut Vec<Candidate<I>>,
522 ) {
523 let cx = self.cx();
524 let trait_def_id = goal.predicate.trait_def_id(cx);
525
526 let result = if let Err(guar) = goal.predicate.error_reported() {
534 G::consider_error_guaranteed_candidate(self, guar)
535 } else if cx.trait_is_auto(trait_def_id) {
536 G::consider_auto_trait_candidate(self, goal)
537 } else if cx.trait_is_alias(trait_def_id) {
538 G::consider_trait_alias_candidate(self, goal)
539 } else {
540 match cx.as_trait_lang_item(trait_def_id) {
541 Some(SolverTraitLangItem::Sized) => {
542 G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::Sized)
543 }
544 Some(SolverTraitLangItem::MetaSized) => {
545 G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::MetaSized)
546 }
547 Some(SolverTraitLangItem::PointeeSized) => {
548 unreachable!("`PointeeSized` is removed during lowering");
549 }
550 Some(
551 SolverTraitLangItem::Copy
552 | SolverTraitLangItem::Clone
553 | SolverTraitLangItem::TrivialClone,
554 ) => G::consider_builtin_copy_clone_candidate(self, goal),
555 Some(SolverTraitLangItem::Fn) => {
556 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
557 }
558 Some(SolverTraitLangItem::FnMut) => {
559 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnMut)
560 }
561 Some(SolverTraitLangItem::FnOnce) => {
562 G::consider_builtin_fn_trait_candidates(self, goal, ty::ClosureKind::FnOnce)
563 }
564 Some(SolverTraitLangItem::AsyncFn) => {
565 G::consider_builtin_async_fn_trait_candidates(self, goal, ty::ClosureKind::Fn)
566 }
567 Some(SolverTraitLangItem::AsyncFnMut) => {
568 G::consider_builtin_async_fn_trait_candidates(
569 self,
570 goal,
571 ty::ClosureKind::FnMut,
572 )
573 }
574 Some(SolverTraitLangItem::AsyncFnOnce) => {
575 G::consider_builtin_async_fn_trait_candidates(
576 self,
577 goal,
578 ty::ClosureKind::FnOnce,
579 )
580 }
581 Some(SolverTraitLangItem::FnPtrTrait) => {
582 G::consider_builtin_fn_ptr_trait_candidate(self, goal)
583 }
584 Some(SolverTraitLangItem::AsyncFnKindHelper) => {
585 G::consider_builtin_async_fn_kind_helper_candidate(self, goal)
586 }
587 Some(SolverTraitLangItem::Tuple) => G::consider_builtin_tuple_candidate(self, goal),
588 Some(SolverTraitLangItem::PointeeTrait) => {
589 G::consider_builtin_pointee_candidate(self, goal)
590 }
591 Some(SolverTraitLangItem::Future) => {
592 G::consider_builtin_future_candidate(self, goal)
593 }
594 Some(SolverTraitLangItem::Iterator) => {
595 G::consider_builtin_iterator_candidate(self, goal)
596 }
597 Some(SolverTraitLangItem::FusedIterator) => {
598 G::consider_builtin_fused_iterator_candidate(self, goal)
599 }
600 Some(SolverTraitLangItem::AsyncIterator) => {
601 G::consider_builtin_async_iterator_candidate(self, goal)
602 }
603 Some(SolverTraitLangItem::Coroutine) => {
604 G::consider_builtin_coroutine_candidate(self, goal)
605 }
606 Some(SolverTraitLangItem::DiscriminantKind) => {
607 G::consider_builtin_discriminant_kind_candidate(self, goal)
608 }
609 Some(SolverTraitLangItem::Destruct) => {
610 G::consider_builtin_destruct_candidate(self, goal)
611 }
612 Some(SolverTraitLangItem::TransmuteTrait) => {
613 G::consider_builtin_transmute_candidate(self, goal)
614 }
615 Some(SolverTraitLangItem::BikeshedGuaranteedNoDrop) => {
616 G::consider_builtin_bikeshed_guaranteed_no_drop_candidate(self, goal)
617 }
618 _ => Err(NoSolution),
619 }
620 };
621
622 candidates.extend(result);
623
624 if cx.is_trait_lang_item(trait_def_id, SolverTraitLangItem::Unsize) {
627 candidates.extend(G::consider_structural_builtin_unsize_candidates(self, goal));
628 }
629 }
630
631 #[instrument(level = "trace", skip_all)]
632 fn assemble_param_env_candidates<G: GoalKind<D>>(
633 &mut self,
634 goal: Goal<I, G>,
635 candidates: &mut Vec<Candidate<I>>,
636 failed_candidate_info: &mut FailedCandidateInfo,
637 ) {
638 for assumption in goal.param_env.caller_bounds().iter() {
639 match G::probe_and_consider_param_env_candidate(self, goal, assumption) {
640 Ok(candidate) => candidates.push(candidate),
641 Err(head_usages) => {
642 failed_candidate_info.param_env_head_usages.merge_usages(head_usages)
643 }
644 }
645 }
646 }
647
648 #[instrument(level = "trace", skip_all)]
649 fn assemble_alias_bound_candidates<G: GoalKind<D>>(
650 &mut self,
651 goal: Goal<I, G>,
652 candidates: &mut Vec<Candidate<I>>,
653 ) {
654 let () = self.probe(|_| ProbeKind::NormalizedSelfTyAssembly).enter(|ecx| {
655 ecx.assemble_alias_bound_candidates_recur(
656 goal.predicate.self_ty(),
657 goal,
658 candidates,
659 AliasBoundKind::SelfBounds,
660 );
661 });
662 }
663
664 fn assemble_alias_bound_candidates_recur<G: GoalKind<D>>(
674 &mut self,
675 self_ty: I::Ty,
676 goal: Goal<I, G>,
677 candidates: &mut Vec<Candidate<I>>,
678 consider_self_bounds: AliasBoundKind,
679 ) {
680 let (kind, alias_ty) = match self_ty.kind() {
681 ty::Bool
682 | ty::Char
683 | ty::Int(_)
684 | ty::Uint(_)
685 | ty::Float(_)
686 | ty::Adt(_, _)
687 | ty::Foreign(_)
688 | ty::Str
689 | ty::Array(_, _)
690 | ty::Pat(_, _)
691 | ty::Slice(_)
692 | ty::RawPtr(_, _)
693 | ty::Ref(_, _, _)
694 | ty::FnDef(_, _)
695 | ty::FnPtr(..)
696 | ty::UnsafeBinder(_)
697 | ty::Dynamic(..)
698 | ty::Closure(..)
699 | ty::CoroutineClosure(..)
700 | ty::Coroutine(..)
701 | ty::CoroutineWitness(..)
702 | ty::Never
703 | ty::Tuple(_)
704 | ty::Param(_)
705 | ty::Placeholder(..)
706 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
707 | ty::Error(_) => return,
708 ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) | ty::Bound(..) => {
709 panic!("unexpected self type for `{goal:?}`")
710 }
711
712 ty::Infer(ty::TyVar(_)) => {
713 if let Ok(result) =
717 self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
718 {
719 candidates.push(Candidate {
720 source: CandidateSource::AliasBound(consider_self_bounds),
721 result,
722 head_usages: CandidateHeadUsages::default(),
723 });
724 }
725 return;
726 }
727
728 ty::Alias(kind @ (ty::Projection | ty::Opaque), alias_ty) => (kind, alias_ty),
729 ty::Alias(ty::Inherent | ty::Free, _) => {
730 self.cx().delay_bug(format!("could not normalize {self_ty:?}, it is not WF"));
731 return;
732 }
733 };
734
735 match consider_self_bounds {
736 AliasBoundKind::SelfBounds => {
737 for assumption in self
738 .cx()
739 .item_self_bounds(alias_ty.def_id)
740 .iter_instantiated(self.cx(), alias_ty.args)
741 {
742 candidates.extend(G::probe_and_consider_implied_clause(
743 self,
744 CandidateSource::AliasBound(consider_self_bounds),
745 goal,
746 assumption,
747 [],
748 ));
749 }
750 }
751 AliasBoundKind::NonSelfBounds => {
752 for assumption in self
753 .cx()
754 .item_non_self_bounds(alias_ty.def_id)
755 .iter_instantiated(self.cx(), alias_ty.args)
756 {
757 candidates.extend(G::probe_and_consider_implied_clause(
758 self,
759 CandidateSource::AliasBound(consider_self_bounds),
760 goal,
761 assumption,
762 [],
763 ));
764 }
765 }
766 }
767
768 candidates.extend(G::consider_additional_alias_assumptions(self, goal, alias_ty));
769
770 if kind != ty::Projection {
771 return;
772 }
773
774 match self.structurally_normalize_ty(goal.param_env, alias_ty.self_ty()) {
776 Ok(next_self_ty) => self.assemble_alias_bound_candidates_recur(
777 next_self_ty,
778 goal,
779 candidates,
780 AliasBoundKind::NonSelfBounds,
781 ),
782 Err(NoSolution) => {}
783 }
784 }
785
786 #[instrument(level = "trace", skip_all)]
787 fn assemble_object_bound_candidates<G: GoalKind<D>>(
788 &mut self,
789 goal: Goal<I, G>,
790 candidates: &mut Vec<Candidate<I>>,
791 ) {
792 let cx = self.cx();
793 if !cx.trait_may_be_implemented_via_object(goal.predicate.trait_def_id(cx)) {
794 return;
795 }
796
797 let self_ty = goal.predicate.self_ty();
798 let bounds = match self_ty.kind() {
799 ty::Bool
800 | ty::Char
801 | ty::Int(_)
802 | ty::Uint(_)
803 | ty::Float(_)
804 | ty::Adt(_, _)
805 | ty::Foreign(_)
806 | ty::Str
807 | ty::Array(_, _)
808 | ty::Pat(_, _)
809 | ty::Slice(_)
810 | ty::RawPtr(_, _)
811 | ty::Ref(_, _, _)
812 | ty::FnDef(_, _)
813 | ty::FnPtr(..)
814 | ty::UnsafeBinder(_)
815 | ty::Alias(..)
816 | ty::Closure(..)
817 | ty::CoroutineClosure(..)
818 | ty::Coroutine(..)
819 | ty::CoroutineWitness(..)
820 | ty::Never
821 | ty::Tuple(_)
822 | ty::Param(_)
823 | ty::Placeholder(..)
824 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
825 | ty::Error(_) => return,
826 ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
827 | ty::Bound(..) => panic!("unexpected self type for `{goal:?}`"),
828 ty::Dynamic(bounds, ..) => bounds,
829 };
830
831 if bounds.principal_def_id().is_some_and(|def_id| !cx.trait_is_dyn_compatible(def_id)) {
833 return;
834 }
835
836 for bound in bounds.iter() {
840 match bound.skip_binder() {
841 ty::ExistentialPredicate::Trait(_) => {
842 }
844 ty::ExistentialPredicate::Projection(_)
845 | ty::ExistentialPredicate::AutoTrait(_) => {
846 candidates.extend(G::probe_and_consider_object_bound_candidate(
847 self,
848 CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
849 goal,
850 bound.with_self_ty(cx, self_ty),
851 ));
852 }
853 }
854 }
855
856 if let Some(principal) = bounds.principal() {
860 let principal_trait_ref = principal.with_self_ty(cx, self_ty);
861 for (idx, assumption) in elaborate::supertraits(cx, principal_trait_ref).enumerate() {
862 candidates.extend(G::probe_and_consider_object_bound_candidate(
863 self,
864 CandidateSource::BuiltinImpl(BuiltinImplSource::Object(idx)),
865 goal,
866 assumption.upcast(cx),
867 ));
868 }
869 }
870 }
871
872 #[instrument(level = "trace", skip_all)]
879 fn consider_coherence_unknowable_candidate<G: GoalKind<D>>(
880 &mut self,
881 goal: Goal<I, G>,
882 ) -> Result<Candidate<I>, NoSolution> {
883 self.probe_trait_candidate(CandidateSource::CoherenceUnknowable).enter(|ecx| {
884 let cx = ecx.cx();
885 let trait_ref = goal.predicate.trait_ref(cx);
886 if ecx.trait_ref_is_knowable(goal.param_env, trait_ref)? {
887 Err(NoSolution)
888 } else {
889 let predicate: I::Predicate = trait_ref.upcast(cx);
895 ecx.add_goals(
896 GoalSource::Misc,
897 elaborate::elaborate(cx, [predicate])
898 .skip(1)
899 .map(|predicate| goal.with(cx, predicate)),
900 );
901 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
902 }
903 })
904 }
905}
906
907pub(super) enum AllowInferenceConstraints {
908 Yes,
909 No,
910}
911
912impl<D, I> EvalCtxt<'_, D>
913where
914 D: SolverDelegate<Interner = I>,
915 I: Interner,
916{
917 pub(super) fn filter_specialized_impls(
921 &mut self,
922 allow_inference_constraints: AllowInferenceConstraints,
923 candidates: &mut Vec<Candidate<I>>,
924 ) {
925 match self.typing_mode() {
926 TypingMode::Coherence => return,
927 TypingMode::Analysis { .. }
928 | TypingMode::Borrowck { .. }
929 | TypingMode::PostBorrowckAnalysis { .. }
930 | TypingMode::PostAnalysis => {}
931 }
932
933 let mut i = 0;
934 'outer: while i < candidates.len() {
935 let CandidateSource::Impl(victim_def_id) = candidates[i].source else {
936 i += 1;
937 continue;
938 };
939
940 for (j, c) in candidates.iter().enumerate() {
941 if i == j {
942 continue;
943 }
944
945 let CandidateSource::Impl(other_def_id) = c.source else {
946 continue;
947 };
948
949 if matches!(allow_inference_constraints, AllowInferenceConstraints::Yes)
956 || has_only_region_constraints(c.result)
957 {
958 if self.cx().impl_specializes(other_def_id, victim_def_id) {
959 candidates.remove(i);
960 continue 'outer;
961 }
962 }
963 }
964
965 i += 1;
966 }
967 }
968
969 fn try_assemble_bounds_via_registered_opaques<G: GoalKind<D>>(
981 &mut self,
982 goal: Goal<I, G>,
983 assemble_from: AssembleCandidatesFrom,
984 candidates: &mut Vec<Candidate<I>>,
985 ) {
986 let self_ty = goal.predicate.self_ty();
987 let opaque_types = match self.typing_mode() {
989 TypingMode::Analysis { .. } => self.opaques_with_sub_unified_hidden_type(self_ty),
990 TypingMode::Coherence
991 | TypingMode::Borrowck { .. }
992 | TypingMode::PostBorrowckAnalysis { .. }
993 | TypingMode::PostAnalysis => vec![],
994 };
995
996 if opaque_types.is_empty() {
997 candidates.extend(self.forced_ambiguity(MaybeCause::Ambiguity));
998 return;
999 }
1000
1001 for &alias_ty in &opaque_types {
1002 debug!("self ty is sub unified with {alias_ty:?}");
1003
1004 struct ReplaceOpaque<I: Interner> {
1005 cx: I,
1006 alias_ty: ty::AliasTy<I>,
1007 self_ty: I::Ty,
1008 }
1009 impl<I: Interner> TypeFolder<I> for ReplaceOpaque<I> {
1010 fn cx(&self) -> I {
1011 self.cx
1012 }
1013 fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1014 if let ty::Alias(ty::Opaque, alias_ty) = ty.kind() {
1015 if alias_ty == self.alias_ty {
1016 return self.self_ty;
1017 }
1018 }
1019 ty.super_fold_with(self)
1020 }
1021 }
1022
1023 for item_bound in self
1031 .cx()
1032 .item_self_bounds(alias_ty.def_id)
1033 .iter_instantiated(self.cx(), alias_ty.args)
1034 {
1035 let assumption =
1036 item_bound.fold_with(&mut ReplaceOpaque { cx: self.cx(), alias_ty, self_ty });
1037 candidates.extend(G::probe_and_match_goal_against_assumption(
1038 self,
1039 CandidateSource::AliasBound(AliasBoundKind::SelfBounds),
1040 goal,
1041 assumption,
1042 |ecx| {
1043 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
1046 },
1047 ));
1048 }
1049 }
1050
1051 if assemble_from.should_assemble_impl_candidates() {
1056 let cx = self.cx();
1057 cx.for_each_blanket_impl(goal.predicate.trait_def_id(cx), |impl_def_id| {
1058 if cx.impl_is_default(impl_def_id) {
1062 return;
1063 }
1064
1065 match G::consider_impl_candidate(self, goal, impl_def_id, |ecx, certainty| {
1066 if ecx.shallow_resolve(self_ty).is_ty_var() {
1067 let certainty = certainty.and(Certainty::AMBIGUOUS);
1069 ecx.evaluate_added_goals_and_make_canonical_response(certainty)
1070 } else {
1071 Err(NoSolution)
1077 }
1078 }) {
1079 Ok(candidate) => candidates.push(candidate),
1080 Err(NoSolution) => (),
1081 }
1082 });
1083 }
1084
1085 if candidates.is_empty() {
1086 let source = CandidateSource::BuiltinImpl(BuiltinImplSource::Misc);
1087 let certainty = Certainty::Maybe {
1088 cause: MaybeCause::Ambiguity,
1089 opaque_types_jank: OpaqueTypesJank::ErrorIfRigidSelfTy,
1090 };
1091 candidates
1092 .extend(self.probe_trait_candidate(source).enter(|this| {
1093 this.evaluate_added_goals_and_make_canonical_response(certainty)
1094 }));
1095 }
1096 }
1097
1098 #[instrument(level = "debug", skip(self, inject_normalize_to_rigid_candidate), ret)]
1129 pub(super) fn assemble_and_merge_candidates<G: GoalKind<D>>(
1130 &mut self,
1131 proven_via: Option<TraitGoalProvenVia>,
1132 goal: Goal<I, G>,
1133 inject_normalize_to_rigid_candidate: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResult<I>,
1134 ) -> QueryResult<I> {
1135 let Some(proven_via) = proven_via else {
1136 return self.forced_ambiguity(MaybeCause::Ambiguity).map(|cand| cand.result);
1143 };
1144
1145 match proven_via {
1146 TraitGoalProvenVia::ParamEnv | TraitGoalProvenVia::AliasBound => {
1147 let (mut candidates, _) = self
1151 .assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::EnvAndBounds);
1152
1153 if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1156 candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1157 } else if candidates.is_empty() {
1158 return inject_normalize_to_rigid_candidate(self);
1161 }
1162
1163 if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1164 Ok(response)
1165 } else {
1166 self.flounder(&candidates)
1167 }
1168 }
1169 TraitGoalProvenVia::Misc => {
1170 let (mut candidates, _) =
1171 self.assemble_and_evaluate_candidates(goal, AssembleCandidatesFrom::All);
1172
1173 if candidates.iter().any(|c| matches!(c.source, CandidateSource::ParamEnv(_))) {
1176 candidates.retain(|c| matches!(c.source, CandidateSource::ParamEnv(_)));
1177 }
1178
1179 self.filter_specialized_impls(AllowInferenceConstraints::Yes, &mut candidates);
1185 if let Some((response, _)) = self.try_merge_candidates(&candidates) {
1186 Ok(response)
1187 } else {
1188 self.flounder(&candidates)
1189 }
1190 }
1191 }
1192 }
1193
1194 fn characterize_param_env_assumption(
1208 &mut self,
1209 param_env: I::ParamEnv,
1210 assumption: I::Clause,
1211 ) -> Result<CandidateSource<I>, NoSolution> {
1212 if assumption.has_bound_vars() {
1215 return Ok(CandidateSource::ParamEnv(ParamEnvSource::NonGlobal));
1216 }
1217
1218 match assumption.visit_with(&mut FindParamInClause {
1219 ecx: self,
1220 param_env,
1221 universes: vec![],
1222 }) {
1223 ControlFlow::Break(Err(NoSolution)) => Err(NoSolution),
1224 ControlFlow::Break(Ok(())) => Ok(CandidateSource::ParamEnv(ParamEnvSource::NonGlobal)),
1225 ControlFlow::Continue(()) => Ok(CandidateSource::ParamEnv(ParamEnvSource::Global)),
1226 }
1227 }
1228}
1229
1230struct FindParamInClause<'a, 'b, D: SolverDelegate<Interner = I>, I: Interner> {
1231 ecx: &'a mut EvalCtxt<'b, D>,
1232 param_env: I::ParamEnv,
1233 universes: Vec<Option<ty::UniverseIndex>>,
1234}
1235
1236impl<D, I> TypeVisitor<I> for FindParamInClause<'_, '_, D, I>
1237where
1238 D: SolverDelegate<Interner = I>,
1239 I: Interner,
1240{
1241 type Result = ControlFlow<Result<(), NoSolution>>;
1242
1243 fn visit_binder<T: TypeVisitable<I>>(&mut self, t: &ty::Binder<I, T>) -> Self::Result {
1244 self.universes.push(None);
1245 t.super_visit_with(self)?;
1246 self.universes.pop();
1247 ControlFlow::Continue(())
1248 }
1249
1250 fn visit_ty(&mut self, ty: I::Ty) -> Self::Result {
1251 let ty = self.ecx.replace_bound_vars(ty, &mut self.universes);
1252 let Ok(ty) = self.ecx.structurally_normalize_ty(self.param_env, ty) else {
1253 return ControlFlow::Break(Err(NoSolution));
1254 };
1255
1256 if let ty::Placeholder(p) = ty.kind() {
1257 if p.universe() == ty::UniverseIndex::ROOT {
1258 ControlFlow::Break(Ok(()))
1259 } else {
1260 ControlFlow::Continue(())
1261 }
1262 } else if ty.has_type_flags(TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_RE_INFER) {
1263 ty.super_visit_with(self)
1264 } else {
1265 ControlFlow::Continue(())
1266 }
1267 }
1268
1269 fn visit_const(&mut self, ct: I::Const) -> Self::Result {
1270 let ct = self.ecx.replace_bound_vars(ct, &mut self.universes);
1271 let Ok(ct) = self.ecx.structurally_normalize_const(self.param_env, ct) else {
1272 return ControlFlow::Break(Err(NoSolution));
1273 };
1274
1275 if let ty::ConstKind::Placeholder(p) = ct.kind() {
1276 if p.universe() == ty::UniverseIndex::ROOT {
1277 ControlFlow::Break(Ok(()))
1278 } else {
1279 ControlFlow::Continue(())
1280 }
1281 } else if ct.has_type_flags(TypeFlags::HAS_PLACEHOLDER | TypeFlags::HAS_RE_INFER) {
1282 ct.super_visit_with(self)
1283 } else {
1284 ControlFlow::Continue(())
1285 }
1286 }
1287
1288 fn visit_region(&mut self, r: I::Region) -> Self::Result {
1289 match self.ecx.eager_resolve_region(r).kind() {
1290 ty::ReStatic | ty::ReError(_) | ty::ReBound(..) => ControlFlow::Continue(()),
1291 ty::RePlaceholder(p) => {
1292 if p.universe() == ty::UniverseIndex::ROOT {
1293 ControlFlow::Break(Ok(()))
1294 } else {
1295 ControlFlow::Continue(())
1296 }
1297 }
1298 ty::ReVar(_) => ControlFlow::Break(Ok(())),
1299 ty::ReErased | ty::ReEarlyParam(_) | ty::ReLateParam(_) => {
1300 unreachable!("unexpected region in param-env clause")
1301 }
1302 }
1303 }
1304}