1use std::ops::ControlFlow;
10
11use hir::LangItem;
12use hir::def_id::DefId;
13use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
14use rustc_hir::{self as hir, CoroutineDesugaring, CoroutineKind};
15use rustc_infer::traits::{Obligation, PolyTraitObligation, SelectionError};
16use rustc_middle::ty::fast_reject::DeepRejectCtxt;
17use rustc_middle::ty::{self, SizedTraitKind, Ty, TypeVisitableExt, TypingMode, elaborate};
18use rustc_middle::{bug, span_bug};
19use tracing::{debug, instrument, trace};
20
21use super::SelectionCandidate::*;
22use super::{SelectionCandidateSet, SelectionContext, TraitObligationStack};
23use crate::traits::util;
24
25impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
26 #[instrument(skip(self, stack), level = "debug")]
27 pub(super) fn assemble_candidates<'o>(
28 &mut self,
29 stack: &TraitObligationStack<'o, 'tcx>,
30 ) -> Result<SelectionCandidateSet<'tcx>, SelectionError<'tcx>> {
31 let TraitObligationStack { obligation, .. } = *stack;
32 let obligation = &Obligation {
33 param_env: obligation.param_env,
34 cause: obligation.cause.clone(),
35 recursion_depth: obligation.recursion_depth,
36 predicate: self.infcx.resolve_vars_if_possible(obligation.predicate),
37 };
38
39 if obligation.predicate.skip_binder().self_ty().is_ty_var() {
40 debug!(ty = ?obligation.predicate.skip_binder().self_ty(), "ambiguous inference var or opaque type");
41 return Ok(SelectionCandidateSet { vec: vec![], ambiguous: true });
51 }
52
53 let mut candidates = SelectionCandidateSet { vec: Vec::new(), ambiguous: false };
54
55 if obligation.polarity() == ty::PredicatePolarity::Negative {
57 self.assemble_candidates_for_trait_alias(obligation, &mut candidates);
58 self.assemble_candidates_from_impls(obligation, &mut candidates);
59 self.assemble_candidates_from_caller_bounds(stack, &mut candidates)?;
60 } else {
61 self.assemble_candidates_for_trait_alias(obligation, &mut candidates);
62
63 let def_id = obligation.predicate.def_id();
66 let tcx = self.tcx();
67
68 let lang_item = tcx.as_lang_item(def_id);
69 match lang_item {
70 Some(LangItem::Copy | LangItem::Clone) => {
71 debug!(obligation_self_ty = ?obligation.predicate.skip_binder().self_ty());
72
73 self.assemble_candidates_from_impls(obligation, &mut candidates);
76
77 self.assemble_builtin_copy_clone_candidate(
79 obligation.predicate.self_ty().skip_binder(),
80 &mut candidates,
81 );
82 }
83 Some(LangItem::DiscriminantKind) => {
84 candidates.vec.push(BuiltinCandidate);
86 }
87 Some(LangItem::PointeeTrait) => {
88 candidates.vec.push(BuiltinCandidate);
90 }
91 Some(LangItem::Sized) => {
92 self.assemble_builtin_sized_candidate(
93 obligation.predicate.self_ty().skip_binder(),
94 &mut candidates,
95 SizedTraitKind::Sized,
96 );
97 }
98 Some(LangItem::MetaSized) => {
99 self.assemble_builtin_sized_candidate(
100 obligation.predicate.self_ty().skip_binder(),
101 &mut candidates,
102 SizedTraitKind::MetaSized,
103 );
104 }
105 Some(LangItem::PointeeSized) => {
106 bug!("`PointeeSized` is removed during lowering");
107 }
108 Some(LangItem::Unsize) => {
109 self.assemble_candidates_for_unsizing(obligation, &mut candidates);
110 }
111 Some(LangItem::Destruct) => {
112 self.assemble_const_destruct_candidates(obligation, &mut candidates);
113 }
114 Some(LangItem::TransmuteTrait) => {
115 self.assemble_candidates_from_impls(obligation, &mut candidates);
117 self.assemble_candidates_for_transmutability(obligation, &mut candidates);
118 }
119 Some(LangItem::Tuple) => {
120 self.assemble_candidate_for_tuple(obligation, &mut candidates);
121 }
122 Some(LangItem::FnPtrTrait) => {
123 self.assemble_candidates_for_fn_ptr_trait(obligation, &mut candidates);
124 }
125 Some(LangItem::BikeshedGuaranteedNoDrop) => {
126 self.assemble_candidates_for_bikeshed_guaranteed_no_drop_trait(
127 obligation,
128 &mut candidates,
129 );
130 }
131 _ => {
132 match lang_item {
136 Some(LangItem::Coroutine) => {
137 self.assemble_coroutine_candidates(obligation, &mut candidates);
138 }
139 Some(LangItem::Future) => {
140 self.assemble_future_candidates(obligation, &mut candidates);
141 }
142 Some(LangItem::Iterator) => {
143 self.assemble_iterator_candidates(obligation, &mut candidates);
144 }
145 Some(LangItem::FusedIterator) => {
146 self.assemble_fused_iterator_candidates(obligation, &mut candidates);
147 }
148 Some(LangItem::AsyncIterator) => {
149 self.assemble_async_iterator_candidates(obligation, &mut candidates);
150 }
151 Some(LangItem::AsyncFnKindHelper) => {
152 self.assemble_async_fn_kind_helper_candidates(
153 obligation,
154 &mut candidates,
155 );
156 }
157 Some(LangItem::AsyncFn | LangItem::AsyncFnMut | LangItem::AsyncFnOnce) => {
158 self.assemble_async_closure_candidates(obligation, &mut candidates);
159 }
160 Some(LangItem::Fn | LangItem::FnMut | LangItem::FnOnce) => {
161 self.assemble_closure_candidates(obligation, &mut candidates);
162 self.assemble_fn_pointer_candidates(obligation, &mut candidates);
163 }
164 _ => {}
165 }
166
167 self.assemble_candidates_from_impls(obligation, &mut candidates);
168 self.assemble_candidates_from_object_ty(obligation, &mut candidates);
169 }
170 }
171
172 self.assemble_candidates_from_projected_tys(obligation, &mut candidates);
173 self.assemble_candidates_from_caller_bounds(stack, &mut candidates)?;
174 self.assemble_candidates_from_auto_impls(obligation, &mut candidates);
175 }
176 debug!("candidate list size: {}", candidates.vec.len());
177 Ok(candidates)
178 }
179
180 #[instrument(level = "debug", skip(self, candidates))]
181 fn assemble_candidates_from_projected_tys(
182 &mut self,
183 obligation: &PolyTraitObligation<'tcx>,
184 candidates: &mut SelectionCandidateSet<'tcx>,
185 ) {
186 match obligation.predicate.skip_binder().trait_ref.self_ty().kind() {
189 ty::Alias(ty::Projection | ty::Opaque, _) => {}
191 ty::Infer(ty::TyVar(_)) => {
192 span_bug!(
193 obligation.cause.span,
194 "Self=_ should have been handled by assemble_candidates"
195 );
196 }
197 _ => return,
198 }
199
200 self.infcx.probe(|_| {
201 let poly_trait_predicate = self.infcx.resolve_vars_if_possible(obligation.predicate);
202 let placeholder_trait_predicate =
203 self.infcx.enter_forall_and_leak_universe(poly_trait_predicate);
204
205 let mut distinct_normalized_bounds = FxHashSet::default();
209 let _ = self.for_each_item_bound::<!>(
210 placeholder_trait_predicate.self_ty(),
211 |selcx, bound, idx, alias_bound_kind| {
212 let Some(bound) = bound.as_trait_clause() else {
213 return ControlFlow::Continue(());
214 };
215 if bound.polarity() != placeholder_trait_predicate.polarity {
216 return ControlFlow::Continue(());
217 }
218
219 selcx.infcx.probe(|_| {
220 let bound = util::lazily_elaborate_sizedness_candidate(
221 selcx.infcx,
222 obligation,
223 bound,
224 );
225
226 match selcx.match_normalize_trait_ref(
228 obligation,
229 placeholder_trait_predicate.trait_ref,
230 bound.map_bound(|pred| pred.trait_ref),
231 ) {
232 Ok(None) => {
233 candidates
234 .vec
235 .push(ProjectionCandidate { idx, kind: alias_bound_kind });
236 }
237 Ok(Some(normalized_trait))
238 if distinct_normalized_bounds.insert(normalized_trait) =>
239 {
240 candidates
241 .vec
242 .push(ProjectionCandidate { idx, kind: alias_bound_kind });
243 }
244 _ => {}
245 }
246 });
247
248 ControlFlow::Continue(())
249 },
250 || candidates.ambiguous = true,
252 );
253 });
254 }
255
256 #[instrument(level = "debug", skip(self, stack, candidates))]
261 fn assemble_candidates_from_caller_bounds<'o>(
262 &mut self,
263 stack: &TraitObligationStack<'o, 'tcx>,
264 candidates: &mut SelectionCandidateSet<'tcx>,
265 ) -> Result<(), SelectionError<'tcx>> {
266 debug!(?stack.obligation);
267
268 let bounds = stack
269 .obligation
270 .param_env
271 .caller_bounds()
272 .iter()
273 .filter_map(|p| p.as_trait_clause())
274 .filter(|p| p.polarity() == stack.obligation.predicate.polarity());
276
277 let drcx = DeepRejectCtxt::relate_rigid_rigid(self.tcx());
278 let obligation_args = stack.obligation.predicate.skip_binder().trait_ref.args;
279 for bound in bounds {
281 let bound =
282 util::lazily_elaborate_sizedness_candidate(self.infcx, stack.obligation, bound);
283
284 if bound.def_id() != stack.obligation.predicate.def_id() {
286 continue;
287 }
288
289 let bound_trait_ref = bound.map_bound(|t| t.trait_ref);
290 if !drcx.args_may_unify(obligation_args, bound_trait_ref.skip_binder().args) {
291 continue;
292 }
293 let wc = self.where_clause_may_apply(stack, bound_trait_ref)?;
294 if wc.may_apply() {
295 candidates.vec.push(ParamCandidate(bound));
296 }
297 }
298
299 Ok(())
300 }
301
302 fn assemble_coroutine_candidates(
303 &mut self,
304 obligation: &PolyTraitObligation<'tcx>,
305 candidates: &mut SelectionCandidateSet<'tcx>,
306 ) {
307 let self_ty = obligation.self_ty().skip_binder();
311 match self_ty.kind() {
312 ty::Coroutine(did, ..) if self.tcx().is_general_coroutine(*did) => {
315 debug!(?self_ty, ?obligation, "assemble_coroutine_candidates",);
316
317 candidates.vec.push(CoroutineCandidate);
318 }
319 ty::Infer(ty::TyVar(_)) => {
320 debug!("assemble_coroutine_candidates: ambiguous self-type");
321 candidates.ambiguous = true;
322 }
323 _ => {}
324 }
325 }
326
327 fn assemble_future_candidates(
328 &mut self,
329 obligation: &PolyTraitObligation<'tcx>,
330 candidates: &mut SelectionCandidateSet<'tcx>,
331 ) {
332 let self_ty = obligation.self_ty().skip_binder();
333 if let ty::Coroutine(did, ..) = self_ty.kind() {
334 if self.tcx().coroutine_is_async(*did) {
337 debug!(?self_ty, ?obligation, "assemble_future_candidates",);
338
339 candidates.vec.push(FutureCandidate);
340 }
341 }
342 }
343
344 fn assemble_iterator_candidates(
345 &mut self,
346 obligation: &PolyTraitObligation<'tcx>,
347 candidates: &mut SelectionCandidateSet<'tcx>,
348 ) {
349 let self_ty = obligation.self_ty().skip_binder();
350 if let ty::Coroutine(did, ..) = self_ty.kind()
353 && self.tcx().coroutine_is_gen(*did)
354 {
355 debug!(?self_ty, ?obligation, "assemble_iterator_candidates",);
356
357 candidates.vec.push(IteratorCandidate);
358 }
359 }
360
361 fn assemble_fused_iterator_candidates(
362 &mut self,
363 obligation: &PolyTraitObligation<'tcx>,
364 candidates: &mut SelectionCandidateSet<'tcx>,
365 ) {
366 if self.coroutine_is_gen(obligation.self_ty().skip_binder()) {
367 candidates.vec.push(BuiltinCandidate);
368 }
369 }
370
371 fn assemble_async_iterator_candidates(
372 &mut self,
373 obligation: &PolyTraitObligation<'tcx>,
374 candidates: &mut SelectionCandidateSet<'tcx>,
375 ) {
376 let self_ty = obligation.self_ty().skip_binder();
377 if let ty::Coroutine(did, args) = *self_ty.kind() {
378 if self.tcx().coroutine_is_async_gen(did) {
381 debug!(?self_ty, ?obligation, "assemble_iterator_candidates",);
382
383 let ty::Adt(_poll_def, args) = *args.as_coroutine().yield_ty().kind() else {
386 candidates.ambiguous = true;
387 return;
388 };
389 let ty::Adt(_option_def, _) = *args.type_at(0).kind() else {
390 candidates.ambiguous = true;
391 return;
392 };
393
394 candidates.vec.push(AsyncIteratorCandidate);
395 }
396 }
397 }
398
399 fn assemble_closure_candidates(
406 &mut self,
407 obligation: &PolyTraitObligation<'tcx>,
408 candidates: &mut SelectionCandidateSet<'tcx>,
409 ) {
410 let kind = self.tcx().fn_trait_kind_from_def_id(obligation.predicate.def_id()).unwrap();
411
412 let self_ty = obligation.self_ty().skip_binder();
416 match *self_ty.kind() {
417 ty::Closure(def_id, _) => {
418 let is_const = self.tcx().is_const_fn(def_id);
419 debug!(?kind, ?obligation, "assemble_unboxed_candidates");
420 match self.infcx.closure_kind(self_ty) {
421 Some(closure_kind) => {
422 debug!(?closure_kind, "assemble_unboxed_candidates");
423 if closure_kind.extends(kind) {
424 candidates.vec.push(ClosureCandidate { is_const });
425 }
426 }
427 None => {
428 if kind == ty::ClosureKind::FnOnce {
429 candidates.vec.push(ClosureCandidate { is_const });
430 } else {
431 candidates.ambiguous = true;
432 }
433 }
434 }
435 }
436 ty::CoroutineClosure(def_id, args) => {
437 let args = args.as_coroutine_closure();
438 let is_const = self.tcx().is_const_fn(def_id);
439 if let Some(closure_kind) = self.infcx.closure_kind(self_ty)
440 && !args.tupled_upvars_ty().is_ty_var()
442 {
443 if closure_kind.extends(kind) && !args.has_self_borrows() {
448 candidates.vec.push(ClosureCandidate { is_const });
449 } else if kind == ty::ClosureKind::FnOnce {
450 candidates.vec.push(ClosureCandidate { is_const });
451 }
452 } else if kind == ty::ClosureKind::FnOnce {
453 candidates.vec.push(ClosureCandidate { is_const });
454 } else {
455 candidates.ambiguous = true;
457 }
458 }
459 ty::Infer(ty::TyVar(_)) => {
460 debug!("assemble_unboxed_closure_candidates: ambiguous self-type");
461 candidates.ambiguous = true;
462 }
463 _ => {}
464 }
465 }
466
467 #[instrument(level = "debug", skip(self, candidates))]
468 fn assemble_async_closure_candidates(
469 &mut self,
470 obligation: &PolyTraitObligation<'tcx>,
471 candidates: &mut SelectionCandidateSet<'tcx>,
472 ) {
473 let goal_kind =
474 self.tcx().async_fn_trait_kind_from_def_id(obligation.predicate.def_id()).unwrap();
475
476 debug!("self_ty = {:?}", obligation.self_ty().skip_binder().kind());
477 match *obligation.self_ty().skip_binder().kind() {
478 ty::CoroutineClosure(def_id, args) => {
479 if let Some(closure_kind) =
480 args.as_coroutine_closure().kind_ty().to_opt_closure_kind()
481 && !closure_kind.extends(goal_kind)
482 {
483 return;
484 }
485
486 let Some(coroutine_kind) =
488 self.tcx().coroutine_kind(self.tcx().coroutine_for_closure(def_id))
489 else {
490 bug!("coroutine with no kind");
491 };
492
493 debug!(?coroutine_kind);
494 match coroutine_kind {
495 CoroutineKind::Desugared(CoroutineDesugaring::Async, _) => {
496 candidates.vec.push(AsyncClosureCandidate);
497 }
498 _ => (),
499 }
500 }
501 ty::Closure(_, args) => {
504 if let Some(closure_kind) = args.as_closure().kind_ty().to_opt_closure_kind()
505 && !closure_kind.extends(goal_kind)
506 {
507 return;
508 }
509 candidates.vec.push(AsyncClosureCandidate);
510 }
511 ty::FnPtr(sig_tys, hdr) => {
513 if sig_tys.with(hdr).is_fn_trait_compatible() {
514 candidates.vec.push(AsyncClosureCandidate);
515 }
516 }
517 ty::FnDef(def_id, _) => {
519 let tcx = self.tcx();
520 if tcx.fn_sig(def_id).skip_binder().is_fn_trait_compatible()
521 && tcx.codegen_fn_attrs(def_id).target_features.is_empty()
522 {
523 candidates.vec.push(AsyncClosureCandidate);
524 }
525 }
526 _ => {}
527 }
528 }
529
530 fn assemble_async_fn_kind_helper_candidates(
531 &mut self,
532 obligation: &PolyTraitObligation<'tcx>,
533 candidates: &mut SelectionCandidateSet<'tcx>,
534 ) {
535 let self_ty = obligation.self_ty().skip_binder();
536 let target_kind_ty = obligation.predicate.skip_binder().trait_ref.args.type_at(1);
537
538 if !(self_ty.is_integral() || self_ty.is_ty_var()) {
540 return;
541 }
542 if !(target_kind_ty.is_integral() || self_ty.is_ty_var()) {
543 return;
544 }
545
546 if let Some(closure_kind) = self_ty.to_opt_closure_kind()
549 && let Some(goal_kind) = target_kind_ty.to_opt_closure_kind()
550 && closure_kind.extends(goal_kind)
551 {
552 candidates.vec.push(AsyncFnKindHelperCandidate);
553 }
554 }
555
556 fn assemble_fn_pointer_candidates(
558 &mut self,
559 obligation: &PolyTraitObligation<'tcx>,
560 candidates: &mut SelectionCandidateSet<'tcx>,
561 ) {
562 let self_ty = obligation.self_ty().skip_binder();
567 match *self_ty.kind() {
568 ty::Infer(ty::TyVar(_)) => {
569 debug!("assemble_fn_pointer_candidates: ambiguous self-type");
570 candidates.ambiguous = true; }
572 ty::FnPtr(sig_tys, hdr) => {
574 if sig_tys.with(hdr).is_fn_trait_compatible() {
575 candidates.vec.push(FnPointerCandidate);
576 }
577 }
578 ty::FnDef(def_id, _) => {
580 let tcx = self.tcx();
581 if tcx.fn_sig(def_id).skip_binder().is_fn_trait_compatible()
582 && tcx.codegen_fn_attrs(def_id).target_features.is_empty()
583 {
584 candidates.vec.push(FnPointerCandidate);
585 }
586 }
587 _ => {}
588 }
589 }
590
591 #[instrument(level = "debug", skip(self, candidates))]
593 fn assemble_candidates_from_impls(
594 &mut self,
595 obligation: &PolyTraitObligation<'tcx>,
596 candidates: &mut SelectionCandidateSet<'tcx>,
597 ) {
598 let drcx = DeepRejectCtxt::relate_rigid_infer(self.tcx());
599 let obligation_args = obligation.predicate.skip_binder().trait_ref.args;
600 self.tcx().for_each_relevant_impl(
601 obligation.predicate.def_id(),
602 obligation.predicate.skip_binder().trait_ref.self_ty(),
603 |impl_def_id| {
604 let impl_trait_header = self.tcx().impl_trait_header(impl_def_id);
608 if !drcx
609 .args_may_unify(obligation_args, impl_trait_header.trait_ref.skip_binder().args)
610 {
611 return;
612 }
613
614 if self.tcx().defaultness(impl_def_id).is_default() {
618 return;
619 }
620
621 if self.reject_fn_ptr_impls(
622 impl_def_id,
623 obligation,
624 impl_trait_header.trait_ref.skip_binder().self_ty(),
625 ) {
626 return;
627 }
628
629 self.infcx.probe(|_| {
630 if let Ok(_args) = self.match_impl(impl_def_id, impl_trait_header, obligation) {
631 candidates.vec.push(ImplCandidate(impl_def_id));
632 }
633 });
634 },
635 );
636 }
637
638 #[instrument(level = "trace", skip(self), ret)]
643 fn reject_fn_ptr_impls(
644 &mut self,
645 impl_def_id: DefId,
646 obligation: &PolyTraitObligation<'tcx>,
647 impl_self_ty: Ty<'tcx>,
648 ) -> bool {
649 if !matches!(impl_self_ty.kind(), ty::Param(..)) {
651 return false;
652 }
653 let Some(fn_ptr_trait) = self.tcx().lang_items().fn_ptr_trait() else {
654 return false;
655 };
656
657 for &(predicate, _) in self.tcx().predicates_of(impl_def_id).predicates {
658 let ty::ClauseKind::Trait(pred) = predicate.kind().skip_binder() else { continue };
659 if fn_ptr_trait != pred.trait_ref.def_id {
660 continue;
661 }
662 trace!(?pred);
663 if pred.self_ty() != impl_self_ty {
665 continue;
666 }
667
668 let self_ty = obligation.self_ty().skip_binder();
669 match self_ty.kind() {
670 ty::FnPtr(..) => return false,
673
674 ty::Placeholder(..)
676 | ty::Dynamic(_, _)
677 | ty::Alias(_, _)
678 | ty::Infer(_)
679 | ty::Param(..)
680 | ty::Bound(_, _) => {}
681
682 ty::Bool
685 | ty::Char
686 | ty::Int(_)
687 | ty::Uint(_)
688 | ty::Float(_)
689 | ty::Adt(_, _)
690 | ty::Foreign(_)
691 | ty::Str
692 | ty::Array(_, _)
693 | ty::Pat(_, _)
694 | ty::Slice(_)
695 | ty::RawPtr(_, _)
696 | ty::Ref(_, _, _)
697 | ty::Closure(..)
698 | ty::CoroutineClosure(..)
699 | ty::Coroutine(_, _)
700 | ty::CoroutineWitness(..)
701 | ty::UnsafeBinder(_)
702 | ty::Never
703 | ty::Tuple(_)
704 | ty::Error(_) => return true,
705 ty::FnDef(_, _) => return true,
708 }
709
710 let obligation = Obligation::new(
713 self.tcx(),
714 obligation.cause.clone(),
715 obligation.param_env,
716 self.tcx().mk_predicate(obligation.predicate.map_bound(|mut pred| {
717 pred.trait_ref =
718 ty::TraitRef::new(self.tcx(), fn_ptr_trait, [pred.trait_ref.self_ty()]);
719 ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred))
720 })),
721 );
722 if let Ok(r) = self.evaluate_root_obligation(&obligation) {
723 if !r.may_apply() {
724 return true;
725 }
726 }
727 }
728 false
729 }
730
731 fn assemble_candidates_from_auto_impls(
732 &mut self,
733 obligation: &PolyTraitObligation<'tcx>,
734 candidates: &mut SelectionCandidateSet<'tcx>,
735 ) {
736 let self_ty = obligation.self_ty().skip_binder();
738 debug!(?self_ty, "assemble_candidates_from_auto_impls");
739
740 let def_id = obligation.predicate.def_id();
741
742 let mut check_impls = || {
743 let mut has_impl = false;
753 self.tcx().for_each_relevant_impl(def_id, self_ty, |_| has_impl = true);
754 if !has_impl {
755 candidates.vec.push(AutoImplCandidate)
756 }
757 };
758
759 if self.tcx().trait_is_auto(def_id) {
760 match *self_ty.kind() {
761 ty::Dynamic(..) => {
762 }
767 ty::Foreign(..) => {
768 if self.tcx().is_default_trait(def_id) {
776 check_impls()
777 }
778 }
779 ty::Param(..)
780 | ty::Alias(ty::Projection | ty::Inherent | ty::Free, ..)
781 | ty::Placeholder(..)
782 | ty::Bound(..) => {
783 }
797 ty::Infer(ty::TyVar(_) | ty::IntVar(_) | ty::FloatVar(_)) => {
798 candidates.ambiguous = true;
800 }
801 ty::Coroutine(coroutine_def_id, _) => {
802 if self.tcx().is_lang_item(def_id, LangItem::Unpin) {
803 match self.tcx().coroutine_movability(coroutine_def_id) {
804 hir::Movability::Static => {
805 }
808 hir::Movability::Movable => {
809 candidates.vec.push(BuiltinCandidate);
812 }
813 }
814 } else {
815 if self.should_stall_coroutine(coroutine_def_id) {
816 candidates.ambiguous = true;
817 } else {
818 candidates.vec.push(AutoImplCandidate);
820 }
821 }
822 }
823
824 ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
825 bug!(
826 "asked to assemble auto trait candidates of unexpected type: {:?}",
827 self_ty
828 );
829 }
830
831 ty::Alias(ty::Opaque, alias) => {
832 if candidates.vec.iter().any(|c| matches!(c, ProjectionCandidate { .. })) {
833 } else if let TypingMode::Coherence = self.infcx.typing_mode() {
843 candidates.ambiguous = true;
846 } else if self.infcx.can_define_opaque_ty(alias.def_id) {
847 candidates.ambiguous = true;
851 } else {
852 candidates.vec.push(AutoImplCandidate)
853 }
854 }
855
856 ty::CoroutineWitness(..) => {
857 candidates.vec.push(AutoImplCandidate);
858 }
859
860 ty::Bool
861 | ty::Char
862 | ty::Int(_)
863 | ty::Uint(_)
864 | ty::Float(_)
865 | ty::Str
866 | ty::Array(_, _)
867 | ty::Pat(_, _)
868 | ty::Slice(_)
869 | ty::Adt(..)
870 | ty::RawPtr(_, _)
871 | ty::Ref(..)
872 | ty::FnDef(..)
873 | ty::FnPtr(..)
874 | ty::Closure(..)
875 | ty::CoroutineClosure(..)
876 | ty::Never
877 | ty::Tuple(_)
878 | ty::UnsafeBinder(_) => {
879 if self.tcx().trait_def(def_id).safety.is_unsafe()
882 && self_ty.has_unsafe_fields()
883 {
884 return;
885 }
886
887 check_impls();
888 }
889 ty::Error(_) => {
890 candidates.vec.push(AutoImplCandidate);
891 }
892 }
893 }
894 }
895
896 fn assemble_candidates_from_object_ty(
898 &mut self,
899 obligation: &PolyTraitObligation<'tcx>,
900 candidates: &mut SelectionCandidateSet<'tcx>,
901 ) {
902 debug!(
903 self_ty = ?obligation.self_ty().skip_binder(),
904 "assemble_candidates_from_object_ty",
905 );
906
907 if !self.tcx().trait_def(obligation.predicate.def_id()).implement_via_object {
908 return;
909 }
910
911 self.infcx.probe(|_snapshot| {
912 let poly_trait_predicate = self.infcx.resolve_vars_if_possible(obligation.predicate);
913 self.infcx.enter_forall(poly_trait_predicate, |placeholder_trait_predicate| {
914 let self_ty = placeholder_trait_predicate.self_ty();
915 let principal_trait_ref = match self_ty.kind() {
916 ty::Dynamic(data, ..) => {
917 if data.auto_traits().any(|did| did == obligation.predicate.def_id()) {
918 debug!(
919 "assemble_candidates_from_object_ty: matched builtin bound, \
920 pushing candidate"
921 );
922 candidates.vec.push(BuiltinObjectCandidate);
923 return;
924 }
925
926 if let Some(principal) = data.principal() {
927 principal.with_self_ty(self.tcx(), self_ty)
928 } else {
929 return;
931 }
932 }
933 ty::Infer(ty::TyVar(_)) => {
934 debug!("assemble_candidates_from_object_ty: ambiguous");
935 candidates.ambiguous = true; return;
937 }
938 _ => return,
939 };
940
941 debug!(?principal_trait_ref, "assemble_candidates_from_object_ty");
942
943 let candidate_supertraits = util::supertraits(self.tcx(), principal_trait_ref)
949 .enumerate()
950 .filter(|&(_, upcast_trait_ref)| {
951 self.infcx.probe(|_| {
952 self.match_normalize_trait_ref(
953 obligation,
954 placeholder_trait_predicate.trait_ref,
955 upcast_trait_ref,
956 )
957 .is_ok()
958 })
959 })
960 .map(|(idx, _)| ObjectCandidate(idx));
961
962 candidates.vec.extend(candidate_supertraits);
963 })
964 })
965 }
966
967 fn assemble_candidates_for_unsizing(
969 &mut self,
970 obligation: &PolyTraitObligation<'tcx>,
971 candidates: &mut SelectionCandidateSet<'tcx>,
972 ) {
973 let Some(trait_pred) = obligation.predicate.no_bound_vars() else {
988 return;
990 };
991 let source = trait_pred.self_ty();
992 let target = trait_pred.trait_ref.args.type_at(1);
993
994 debug!(?source, ?target, "assemble_candidates_for_unsizing");
995
996 match (source.kind(), target.kind()) {
997 (&ty::Dynamic(a_data, a_region), &ty::Dynamic(b_data, b_region)) => {
999 let principal_def_id_a = a_data.principal_def_id();
1011 let principal_def_id_b = b_data.principal_def_id();
1012 if principal_def_id_a == principal_def_id_b || principal_def_id_b.is_none() {
1013 let a_auto_traits: FxIndexSet<DefId> = a_data
1017 .auto_traits()
1018 .chain(principal_def_id_a.into_iter().flat_map(|principal_def_id| {
1019 elaborate::supertrait_def_ids(self.tcx(), principal_def_id)
1020 .filter(|def_id| self.tcx().trait_is_auto(*def_id))
1021 }))
1022 .collect();
1023 let auto_traits_compatible = b_data
1024 .auto_traits()
1025 .all(|b| a_auto_traits.contains(&b));
1027 if auto_traits_compatible {
1028 candidates.vec.push(BuiltinUnsizeCandidate);
1029 }
1030 } else if principal_def_id_a.is_some() && principal_def_id_b.is_some() {
1031 let principal_a = a_data.principal().unwrap();
1033 let target_trait_did = principal_def_id_b.unwrap();
1034 let source_trait_ref = principal_a.with_self_ty(self.tcx(), source);
1035
1036 for (idx, upcast_trait_ref) in
1037 util::supertraits(self.tcx(), source_trait_ref).enumerate()
1038 {
1039 self.infcx.probe(|_| {
1040 if upcast_trait_ref.def_id() == target_trait_did
1041 && let Ok(nested) = self.match_upcast_principal(
1042 obligation,
1043 upcast_trait_ref,
1044 a_data,
1045 b_data,
1046 a_region,
1047 b_region,
1048 )
1049 {
1050 if nested.is_none() {
1051 candidates.ambiguous = true;
1052 }
1053 candidates.vec.push(TraitUpcastingUnsizeCandidate(idx));
1054 }
1055 })
1056 }
1057 }
1058 }
1059
1060 (_, &ty::Dynamic(_, _)) => {
1062 candidates.vec.push(BuiltinUnsizeCandidate);
1063 }
1064
1065 (&ty::Infer(ty::TyVar(_)), _) | (_, &ty::Infer(ty::TyVar(_))) => {
1069 debug!("assemble_candidates_for_unsizing: ambiguous");
1070 candidates.ambiguous = true;
1071 }
1072
1073 (&ty::Array(..), &ty::Slice(_)) => {
1075 candidates.vec.push(BuiltinUnsizeCandidate);
1076 }
1077
1078 (&ty::Adt(def_id_a, _), &ty::Adt(def_id_b, _)) if def_id_a.is_struct() => {
1080 if def_id_a == def_id_b {
1081 candidates.vec.push(BuiltinUnsizeCandidate);
1082 }
1083 }
1084
1085 _ => {}
1086 };
1087 }
1088
1089 #[instrument(level = "debug", skip(self, obligation, candidates))]
1090 fn assemble_candidates_for_transmutability(
1091 &mut self,
1092 obligation: &PolyTraitObligation<'tcx>,
1093 candidates: &mut SelectionCandidateSet<'tcx>,
1094 ) {
1095 if obligation.predicate.has_non_region_param() {
1096 return;
1097 }
1098
1099 if obligation.has_non_region_infer() {
1100 candidates.ambiguous = true;
1101 return;
1102 }
1103
1104 candidates.vec.push(TransmutabilityCandidate);
1105 }
1106
1107 #[instrument(level = "debug", skip(self, obligation, candidates))]
1108 fn assemble_candidates_for_trait_alias(
1109 &mut self,
1110 obligation: &PolyTraitObligation<'tcx>,
1111 candidates: &mut SelectionCandidateSet<'tcx>,
1112 ) {
1113 let self_ty = obligation.self_ty().skip_binder();
1115 debug!(?self_ty);
1116
1117 let def_id = obligation.predicate.def_id();
1118
1119 if self.tcx().is_trait_alias(def_id) {
1120 candidates.vec.push(TraitAliasCandidate);
1121 }
1122 }
1123
1124 #[instrument(level = "debug", skip(self, candidates))]
1127 fn assemble_builtin_copy_clone_candidate(
1128 &mut self,
1129 self_ty: Ty<'tcx>,
1130 candidates: &mut SelectionCandidateSet<'tcx>,
1131 ) {
1132 match *self_ty.kind() {
1133 ty::FnDef(..) | ty::FnPtr(..) | ty::Error(_) | ty::Tuple(..) | ty::Pat(..) => {
1136 candidates.vec.push(BuiltinCandidate);
1137 }
1138
1139 ty::Uint(_)
1141 | ty::Int(_)
1142 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
1143 | ty::Bool
1144 | ty::Float(_)
1145 | ty::Char
1146 | ty::RawPtr(..)
1147 | ty::Never
1148 | ty::Ref(_, _, hir::Mutability::Not)
1149 | ty::Array(..) => {}
1150
1151 ty::UnsafeBinder(_) => {}
1154
1155 ty::Dynamic(..) | ty::Str | ty::Slice(..) | ty::Foreign(..) => {}
1157
1158 ty::Ref(_, _, hir::Mutability::Mut) => {}
1160
1161 ty::Coroutine(coroutine_def_id, args) => {
1162 if self.should_stall_coroutine(coroutine_def_id) {
1163 candidates.ambiguous = true;
1164 return;
1165 }
1166
1167 match self.tcx().coroutine_movability(coroutine_def_id) {
1168 hir::Movability::Static => {}
1169 hir::Movability::Movable => {
1170 if self.tcx().features().coroutine_clone() {
1171 let resolved_upvars =
1172 self.infcx.shallow_resolve(args.as_coroutine().tupled_upvars_ty());
1173 if resolved_upvars.is_ty_var() {
1174 candidates.ambiguous = true;
1176 } else {
1177 candidates.vec.push(BuiltinCandidate);
1178 }
1179 }
1180 }
1181 }
1182 }
1183
1184 ty::Closure(_, args) => {
1185 let resolved_upvars =
1186 self.infcx.shallow_resolve(args.as_closure().tupled_upvars_ty());
1187 if resolved_upvars.is_ty_var() {
1188 candidates.ambiguous = true;
1190 } else {
1191 candidates.vec.push(BuiltinCandidate);
1192 }
1193 }
1194
1195 ty::CoroutineClosure(_, args) => {
1196 let resolved_upvars =
1197 self.infcx.shallow_resolve(args.as_coroutine_closure().tupled_upvars_ty());
1198 if resolved_upvars.is_ty_var() {
1199 candidates.ambiguous = true;
1201 } else {
1202 candidates.vec.push(BuiltinCandidate);
1203 }
1204 }
1205
1206 ty::CoroutineWitness(..) => {
1207 candidates.vec.push(SizedCandidate);
1208 }
1209
1210 ty::Adt(..) | ty::Alias(..) | ty::Param(..) | ty::Placeholder(..) => {}
1212
1213 ty::Infer(ty::TyVar(_)) => {
1214 candidates.ambiguous = true;
1215 }
1216
1217 ty::Bound(..) => {}
1219
1220 ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
1221 bug!("asked to assemble builtin bounds of unexpected type: {:?}", self_ty);
1222 }
1223 }
1224 }
1225
1226 #[instrument(level = "debug", skip(self, candidates))]
1228 fn assemble_builtin_sized_candidate(
1229 &mut self,
1230 self_ty: Ty<'tcx>,
1231 candidates: &mut SelectionCandidateSet<'tcx>,
1232 sizedness: SizedTraitKind,
1233 ) {
1234 match *self_ty.kind() {
1235 ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
1237 | ty::Uint(_)
1238 | ty::Int(_)
1239 | ty::Bool
1240 | ty::Float(_)
1241 | ty::FnDef(..)
1242 | ty::FnPtr(..)
1243 | ty::RawPtr(..)
1244 | ty::Char
1245 | ty::Ref(..)
1246 | ty::Array(..)
1247 | ty::Closure(..)
1248 | ty::CoroutineClosure(..)
1249 | ty::Never
1250 | ty::Error(_) => {
1251 candidates.vec.push(SizedCandidate);
1252 }
1253
1254 ty::Coroutine(coroutine_def_id, _) => {
1255 if self.should_stall_coroutine(coroutine_def_id) {
1256 candidates.ambiguous = true;
1257 } else {
1258 candidates.vec.push(SizedCandidate);
1259 }
1260 }
1261
1262 ty::CoroutineWitness(..) => {
1263 candidates.vec.push(SizedCandidate);
1264 }
1265
1266 ty::Tuple(..) | ty::Pat(..) | ty::Adt(..) | ty::UnsafeBinder(_) => {
1268 candidates.vec.push(SizedCandidate);
1269 }
1270
1271 ty::Str | ty::Slice(_) | ty::Dynamic(..) => match sizedness {
1273 SizedTraitKind::Sized => {}
1274 SizedTraitKind::MetaSized => {
1275 candidates.vec.push(SizedCandidate);
1276 }
1277 },
1278
1279 ty::Foreign(..) => {}
1281
1282 ty::Alias(..) | ty::Param(_) | ty::Placeholder(..) => {}
1283
1284 ty::Infer(ty::TyVar(_)) => {
1285 candidates.ambiguous = true;
1286 }
1287
1288 ty::Bound(..) => {}
1290
1291 ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
1292 bug!("asked to assemble builtin bounds of unexpected type: {:?}", self_ty);
1293 }
1294 }
1295 }
1296
1297 fn assemble_const_destruct_candidates(
1298 &mut self,
1299 _obligation: &PolyTraitObligation<'tcx>,
1300 candidates: &mut SelectionCandidateSet<'tcx>,
1301 ) {
1302 candidates.vec.push(BuiltinCandidate);
1303 }
1304
1305 fn assemble_candidate_for_tuple(
1306 &mut self,
1307 obligation: &PolyTraitObligation<'tcx>,
1308 candidates: &mut SelectionCandidateSet<'tcx>,
1309 ) {
1310 let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
1311 match self_ty.kind() {
1312 ty::Tuple(_) => {
1313 candidates.vec.push(BuiltinCandidate);
1314 }
1315 ty::Infer(ty::TyVar(_)) => {
1316 candidates.ambiguous = true;
1317 }
1318 ty::Bool
1319 | ty::Char
1320 | ty::Int(_)
1321 | ty::Uint(_)
1322 | ty::Float(_)
1323 | ty::Adt(_, _)
1324 | ty::Foreign(_)
1325 | ty::Str
1326 | ty::Array(_, _)
1327 | ty::Slice(_)
1328 | ty::RawPtr(_, _)
1329 | ty::Ref(_, _, _)
1330 | ty::FnDef(_, _)
1331 | ty::Pat(_, _)
1332 | ty::FnPtr(..)
1333 | ty::UnsafeBinder(_)
1334 | ty::Dynamic(_, _)
1335 | ty::Closure(..)
1336 | ty::CoroutineClosure(..)
1337 | ty::Coroutine(_, _)
1338 | ty::CoroutineWitness(..)
1339 | ty::Never
1340 | ty::Alias(..)
1341 | ty::Param(_)
1342 | ty::Bound(_, _)
1343 | ty::Error(_)
1344 | ty::Infer(_)
1345 | ty::Placeholder(_) => {}
1346 }
1347 }
1348
1349 fn assemble_candidates_for_fn_ptr_trait(
1350 &mut self,
1351 obligation: &PolyTraitObligation<'tcx>,
1352 candidates: &mut SelectionCandidateSet<'tcx>,
1353 ) {
1354 let self_ty = self.infcx.resolve_vars_if_possible(obligation.self_ty());
1355
1356 match self_ty.skip_binder().kind() {
1357 ty::FnPtr(..) => candidates.vec.push(BuiltinCandidate),
1358 ty::Bool
1359 | ty::Char
1360 | ty::Int(_)
1361 | ty::Uint(_)
1362 | ty::Float(_)
1363 | ty::Adt(..)
1364 | ty::Foreign(..)
1365 | ty::Str
1366 | ty::Array(..)
1367 | ty::Pat(..)
1368 | ty::Slice(_)
1369 | ty::RawPtr(_, _)
1370 | ty::Ref(..)
1371 | ty::FnDef(..)
1372 | ty::Placeholder(..)
1373 | ty::Dynamic(..)
1374 | ty::Closure(..)
1375 | ty::CoroutineClosure(..)
1376 | ty::Coroutine(..)
1377 | ty::CoroutineWitness(..)
1378 | ty::UnsafeBinder(_)
1379 | ty::Never
1380 | ty::Tuple(..)
1381 | ty::Alias(..)
1382 | ty::Param(..)
1383 | ty::Bound(..)
1384 | ty::Error(_)
1385 | ty::Infer(
1386 ty::InferTy::IntVar(_)
1387 | ty::InferTy::FloatVar(_)
1388 | ty::InferTy::FreshIntTy(_)
1389 | ty::InferTy::FreshFloatTy(_),
1390 ) => {}
1391 ty::Infer(ty::InferTy::TyVar(_) | ty::InferTy::FreshTy(_)) => {
1392 candidates.ambiguous = true;
1393 }
1394 }
1395 }
1396
1397 fn assemble_candidates_for_bikeshed_guaranteed_no_drop_trait(
1398 &mut self,
1399 obligation: &PolyTraitObligation<'tcx>,
1400 candidates: &mut SelectionCandidateSet<'tcx>,
1401 ) {
1402 match obligation.predicate.self_ty().skip_binder().kind() {
1403 ty::Ref(..)
1404 | ty::Adt(..)
1405 | ty::Tuple(_)
1406 | ty::Array(..)
1407 | ty::FnDef(..)
1408 | ty::FnPtr(..)
1409 | ty::Error(_)
1410 | ty::Uint(_)
1411 | ty::Int(_)
1412 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
1413 | ty::Bool
1414 | ty::Float(_)
1415 | ty::Char
1416 | ty::RawPtr(..)
1417 | ty::Never
1418 | ty::Pat(..)
1419 | ty::Dynamic(..)
1420 | ty::Str
1421 | ty::Slice(_)
1422 | ty::Foreign(..)
1423 | ty::Alias(..)
1424 | ty::Param(_)
1425 | ty::Placeholder(..)
1426 | ty::Closure(..)
1427 | ty::CoroutineClosure(..)
1428 | ty::Coroutine(..)
1429 | ty::UnsafeBinder(_)
1430 | ty::CoroutineWitness(..)
1431 | ty::Bound(..) => {
1432 candidates.vec.push(BikeshedGuaranteedNoDropCandidate);
1433 }
1434
1435 ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
1436 candidates.ambiguous = true;
1437 }
1438 }
1439 }
1440}