rustc_trait_selection/traits/select/
candidate_assembly.rs

1//! Candidate assembly.
2//!
3//! The selection process begins by examining all in-scope impls,
4//! caller obligations, and so forth and assembling a list of
5//! candidates. See the [rustc dev guide] for more details.
6//!
7//! [rustc dev guide]:https://rustc-dev-guide.rust-lang.org/traits/resolution.html#candidate-assembly
8
9use 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            // Self is a type variable (e.g., `_: AsRef<str>`).
42            //
43            // This is somewhat problematic, as the current scheme can't really
44            // handle it turning to be a projection. This does end up as truly
45            // ambiguous in most cases anyway.
46            //
47            // Take the fast path out - this also improves
48            // performance by preventing assemble_candidates_from_impls from
49            // matching every impl for this trait.
50            return Ok(SelectionCandidateSet { vec: vec![], ambiguous: true });
51        }
52
53        let mut candidates = SelectionCandidateSet { vec: Vec::new(), ambiguous: false };
54
55        // Negative trait predicates have different rules than positive trait predicates.
56        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            // Other bounds. Consider both in-scope bounds from fn decl
64            // and applicable impls. There is a certain set of precedence rules here.
65            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                    // User-defined copy impls are permitted, but only for
74                    // structs and enums.
75                    self.assemble_candidates_from_impls(obligation, &mut candidates);
76
77                    // For other types, we'll use the builtin rules.
78                    self.assemble_builtin_copy_clone_candidate(
79                        obligation.predicate.self_ty().skip_binder(),
80                        &mut candidates,
81                    );
82                }
83                Some(LangItem::DiscriminantKind) => {
84                    // `DiscriminantKind` is automatically implemented for every type.
85                    candidates.vec.push(BuiltinCandidate);
86                }
87                Some(LangItem::PointeeTrait) => {
88                    // `Pointee` is automatically implemented for every type.
89                    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                    // User-defined transmutability impls are permitted.
116                    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                    // We re-match here for traits that can have both builtin impls and user written impls.
133                    // After the builtin impls we need to also add user written impls, which we do not want to
134                    // do in general because just checking if there are any is expensive.
135                    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        // Before we go into the whole placeholder thing, just
187        // quickly check if the self-type is a projection at all.
188        match obligation.predicate.skip_binder().trait_ref.self_ty().kind() {
189            // Excluding IATs and type aliases here as they don't have meaningful item bounds.
190            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            // The bounds returned by `item_bounds` may contain duplicates after
206            // normalization, so try to deduplicate when possible to avoid
207            // unnecessary ambiguity.
208            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                        // We checked the polarity already
227                        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                // On ambiguity.
251                || candidates.ambiguous = true,
252            );
253        });
254    }
255
256    /// Given an obligation like `<SomeTrait for T>`, searches the obligations that the caller
257    /// supplied to find out whether it is listed among them.
258    ///
259    /// Never affects the inference environment.
260    #[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            // Micro-optimization: filter out predicates with different polarities.
275            .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        // Keep only those bounds which may apply, and propagate overflow if it occurs.
280        for bound in bounds {
281            let bound =
282                util::lazily_elaborate_sizedness_candidate(self.infcx, stack.obligation, bound);
283
284            // Micro-optimization: filter out predicates relating to different traits.
285            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        // Okay to skip binder because the args on coroutine types never
308        // touch bound regions, they just capture the in-scope
309        // type/region parameters.
310        let self_ty = obligation.self_ty().skip_binder();
311        match self_ty.kind() {
312            // `async`/`gen` constructs get lowered to a special kind of coroutine that
313            // should *not* `impl Coroutine`.
314            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            // async constructs get lowered to a special kind of coroutine that
335            // should directly `impl Future`.
336            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        // gen constructs get lowered to a special kind of coroutine that
351        // should directly `impl Iterator`.
352        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            // gen constructs get lowered to a special kind of coroutine that
379            // should directly `impl AsyncIterator`.
380            if self.tcx().coroutine_is_async_gen(did) {
381                debug!(?self_ty, ?obligation, "assemble_iterator_candidates",);
382
383                // Can only confirm this candidate if we have constrained
384                // the `Yield` type to at least `Poll<Option<?0>>`..
385                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    /// Checks for the artificial impl that the compiler will create for an obligation like `X :
400    /// FnMut<..>` where `X` is a closure type.
401    ///
402    /// Note: the type parameters on a closure candidate are modeled as *output* type
403    /// parameters and hence do not affect whether this trait is a match or not. They will be
404    /// unified during the confirmation step.
405    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        // Okay to skip binder because the args on closure types never
413        // touch bound regions, they just capture the in-scope
414        // type/region parameters
415        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                    // Ambiguity if upvars haven't been constrained yet
441                    && !args.tupled_upvars_ty().is_ty_var()
442                {
443                    // A coroutine-closure implements `FnOnce` *always*, since it may
444                    // always be called once. It additionally implements `Fn`/`FnMut`
445                    // only if it has no upvars referencing the closure-env lifetime,
446                    // and if the closure kind permits it.
447                    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                    // This stays ambiguous until kind+upvars are determined.
456                    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                // Make sure this is actually an async closure.
487                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            // Closures and fn pointers implement `AsyncFn*` if their return types
502            // implement `Future`, which is checked later.
503            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            // Provide an impl, but only for suitable `fn` pointers.
512            ty::FnPtr(sig_tys, hdr) => {
513                if sig_tys.with(hdr).is_fn_trait_compatible() {
514                    candidates.vec.push(AsyncClosureCandidate);
515                }
516            }
517            // Provide an impl for suitable functions, rejecting `#[target_feature]` functions (RFC 2396).
518            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        // `to_opt_closure_kind` is kind of ICEy when it sees non-int types.
539        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        // Check that the self kind extends the goal kind. If it does,
547        // then there's nothing else to check.
548        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    /// Implements one of the `Fn()` family for a fn pointer.
557    fn assemble_fn_pointer_candidates(
558        &mut self,
559        obligation: &PolyTraitObligation<'tcx>,
560        candidates: &mut SelectionCandidateSet<'tcx>,
561    ) {
562        // Keep this function in sync with extract_tupled_inputs_and_output_from_callable
563        // until the old solver (and thus this function) is removed.
564
565        // Okay to skip binder because what we are inspecting doesn't involve bound regions.
566        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; // Could wind up being a fn() type.
571            }
572            // Provide an impl, but only for suitable `fn` pointers.
573            ty::FnPtr(sig_tys, hdr) => {
574                if sig_tys.with(hdr).is_fn_trait_compatible() {
575                    candidates.vec.push(FnPointerCandidate);
576                }
577            }
578            // Provide an impl for suitable functions, rejecting `#[target_feature]` functions (RFC 2396).
579            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    /// Searches for impls that might apply to `obligation`.
592    #[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                // Before we create the generic parameters and everything, first
605                // consider a "quick reject". This avoids creating more types
606                // and so forth that we need to.
607                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                // For every `default impl`, there's always a non-default `impl`
615                // that will *also* apply. There's no reason to register a candidate
616                // for this impl, since it is *not* proof that the trait goal holds.
617                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    /// The various `impl<T: FnPtr> Trait for T` in libcore are more like builtin impls for all function items
639    /// and function pointers and less like blanket impls. Rejecting them when they can't possibly apply (because
640    /// the obligation's self-type does not implement `FnPtr`) avoids reporting that the self type does not implement
641    /// `FnPtr`, when we wanted to report that it doesn't implement `Trait`.
642    #[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        // Let `impl<T: FnPtr> Trait for Vec<T>` go through the normal rejection path.
650        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            // Not the bound we're looking for
664            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                // Fast path to avoid evaluating an obligation that trivially holds.
671                // There may be more bounds, but these are checked by the regular path.
672                ty::FnPtr(..) => return false,
673
674                // These may potentially implement `FnPtr`
675                ty::Placeholder(..)
676                | ty::Dynamic(_, _)
677                | ty::Alias(_, _)
678                | ty::Infer(_)
679                | ty::Param(..)
680                | ty::Bound(_, _) => {}
681
682                // These can't possibly implement `FnPtr` as they are concrete types
683                // and not `FnPtr`
684                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                // FIXME: Function definitions could actually implement `FnPtr` by
706                // casting the ZST function def to a function pointer.
707                ty::FnDef(_, _) => return true,
708            }
709
710            // Generic params can implement `FnPtr` if the predicate
711            // holds within its own environment.
712            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        // Okay to skip binder here because the tests we do below do not involve bound regions.
737        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            // Only consider auto impls if there are no manual impls for the root of `self_ty`.
744            //
745            // For example, we only consider auto candidates for `&i32: Auto` if no explicit impl
746            // for `&SomeType: Auto` exists. Due to E0321 the only crate where impls
747            // for `&SomeType: Auto` can be defined is the crate where `Auto` has been defined.
748            //
749            // Generally, we have to guarantee that for all `SimplifiedType`s the only crate
750            // which may define impls for that type is either the crate defining the type
751            // or the trait. This should be guaranteed by the orphan check.
752            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                    // For object types, we don't know what the closed
763                    // over types are. This means we conservatively
764                    // say nothing; a candidate may be added by
765                    // `assemble_candidates_from_object_ty`.
766                }
767                ty::Foreign(..) => {
768                    // Since the contents of foreign types is unknown,
769                    // we don't add any `..` impl. Default traits could
770                    // still be provided by a manual implementation for
771                    // this trait and type.
772
773                    // Backward compatibility for default auto traits.
774                    // Test: ui/traits/default_auto_traits/extern-types.rs
775                    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                    // In these cases, we don't know what the actual
784                    // type is. Therefore, we cannot break it down
785                    // into its constituent types. So we don't
786                    // consider the `..` impl but instead just add no
787                    // candidates: this means that typeck will only
788                    // succeed if there is another reason to believe
789                    // that this obligation holds. That could be a
790                    // where-clause or, in the case of an object type,
791                    // it could be that the object type lists the
792                    // trait (e.g., `Foo+Send : Send`). See
793                    // `ui/typeck/typeck-default-trait-impl-send-param.rs`
794                    // for an example of a test case that exercises
795                    // this path.
796                }
797                ty::Infer(ty::TyVar(_) | ty::IntVar(_) | ty::FloatVar(_)) => {
798                    // The auto impl might apply; we don't know.
799                    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                                // Immovable coroutines are never `Unpin`, so
806                                // suppress the normal auto-impl candidate for it.
807                            }
808                            hir::Movability::Movable => {
809                                // Movable coroutines are always `Unpin`, so add an
810                                // unconditional builtin candidate with no sub-obligations.
811                                candidates.vec.push(BuiltinCandidate);
812                            }
813                        }
814                    } else {
815                        if self.should_stall_coroutine(coroutine_def_id) {
816                            candidates.ambiguous = true;
817                        } else {
818                            // Coroutines implement all other auto traits normally.
819                            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                        // We do not generate an auto impl candidate for `impl Trait`s which already
834                        // reference our auto trait.
835                        //
836                        // For example during candidate assembly for `impl Send: Send`, we don't have
837                        // to look at the constituent types for this opaque types to figure out that this
838                        // trivially holds.
839                        //
840                        // Note that this is only sound as projection candidates of opaque types
841                        // are always applicable for auto traits.
842                    } else if let TypingMode::Coherence = self.infcx.typing_mode() {
843                        // We do not emit auto trait candidates for opaque types in coherence.
844                        // Doing so can result in weird dependency cycles.
845                        candidates.ambiguous = true;
846                    } else if self.infcx.can_define_opaque_ty(alias.def_id) {
847                        // We do not emit auto trait candidates for opaque types in their defining scope, as
848                        // we need to know the hidden type first, which we can't reliably know within the defining
849                        // scope.
850                        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                    // Only consider auto impls of unsafe traits when there are
880                    // no unsafe fields.
881                    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    /// Searches for impls that might apply to `obligation`.
897    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                            // Only auto trait bounds exist.
930                            return;
931                        }
932                    }
933                    ty::Infer(ty::TyVar(_)) => {
934                        debug!("assemble_candidates_from_object_ty: ambiguous");
935                        candidates.ambiguous = true; // could wind up being an object type
936                        return;
937                    }
938                    _ => return,
939                };
940
941                debug!(?principal_trait_ref, "assemble_candidates_from_object_ty");
942
943                // Count only those upcast versions that match the trait-ref
944                // we are looking for. Specifically, do not only check for the
945                // correct trait, but also the correct type parameters.
946                // For example, we may be trying to upcast `Foo` to `Bar<i32>`,
947                // but `Foo` is declared as `trait Foo: Bar<u32>`.
948                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    /// Searches for unsizing that might apply to `obligation`.
968    fn assemble_candidates_for_unsizing(
969        &mut self,
970        obligation: &PolyTraitObligation<'tcx>,
971        candidates: &mut SelectionCandidateSet<'tcx>,
972    ) {
973        // We currently never consider higher-ranked obligations e.g.
974        // `for<'a> &'a T: Unsize<Trait+'a>` to be implemented. This is not
975        // because they are a priori invalid, and we could potentially add support
976        // for them later, it's just that there isn't really a strong need for it.
977        // A `T: Unsize<U>` obligation is always used as part of a `T: CoerceUnsize<U>`
978        // impl, and those are generally applied to concrete types.
979        //
980        // That said, one might try to write a fn with a where clause like
981        //     for<'a> Foo<'a, T>: Unsize<Foo<'a, Trait>>
982        // where the `'a` is kind of orthogonal to the relevant part of the `Unsize`.
983        // Still, you'd be more likely to write that where clause as
984        //     T: Trait
985        // so it seems ok if we (conservatively) fail to accept that `Unsize`
986        // obligation above. Should be possible to extend this in the future.
987        let Some(trait_pred) = obligation.predicate.no_bound_vars() else {
988            // Don't add any candidates if there are bound regions.
989            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            // Trait+Kx+'a -> Trait+Ky+'b (upcasts).
998            (&ty::Dynamic(a_data, a_region), &ty::Dynamic(b_data, b_region)) => {
999                // Upcast coercions permit several things:
1000                //
1001                // 1. Dropping auto traits, e.g., `Foo + Send` to `Foo`
1002                // 2. Tightening the region bound, e.g., `Foo + 'a` to `Foo + 'b` if `'a: 'b`
1003                // 3. Tightening trait to its super traits, eg. `Foo` to `Bar` if `Foo: Bar`
1004                //
1005                // Note that neither of the first two of these changes requires any
1006                // change at runtime. The third needs to change pointer metadata at runtime.
1007                //
1008                // We always perform upcasting coercions when we can because of reason
1009                // #2 (region bounds).
1010                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                    // We may upcast to auto traits that are either explicitly listed in
1014                    // the object type's bounds, or implied by the principal trait ref's
1015                    // supertraits.
1016                    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 of a's auto traits need to be in b's auto traits.
1026                        .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                    // not casual unsizing, now check whether this is trait upcasting coercion.
1032                    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            // `T` -> `Trait`
1061            (_, &ty::Dynamic(_, _)) => {
1062                candidates.vec.push(BuiltinUnsizeCandidate);
1063            }
1064
1065            // Ambiguous handling is below `T` -> `Trait`, because inference
1066            // variables can still implement `Unsize<Trait>` and nested
1067            // obligations will have the final say (likely deferred).
1068            (&ty::Infer(ty::TyVar(_)), _) | (_, &ty::Infer(ty::TyVar(_))) => {
1069                debug!("assemble_candidates_for_unsizing: ambiguous");
1070                candidates.ambiguous = true;
1071            }
1072
1073            // `[T; n]` -> `[T]`
1074            (&ty::Array(..), &ty::Slice(_)) => {
1075                candidates.vec.push(BuiltinUnsizeCandidate);
1076            }
1077
1078            // `Struct<T>` -> `Struct<U>`
1079            (&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        // Okay to skip binder here because the tests we do below do not involve bound regions.
1114        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    /// Assembles `Copy` and `Clone` candidates for built-in types with no libcore-defined
1125    /// `Copy` or `Clone` impls.
1126    #[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            // These impls are built-in because we cannot express sufficiently
1134            // generic impls in libcore.
1135            ty::FnDef(..) | ty::FnPtr(..) | ty::Error(_) | ty::Tuple(..) | ty::Pat(..) => {
1136                candidates.vec.push(BuiltinCandidate);
1137            }
1138
1139            // Implementations provided in libcore.
1140            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            // FIXME(unsafe_binder): Should we conditionally
1152            // (i.e. universally) implement copy/clone?
1153            ty::UnsafeBinder(_) => {}
1154
1155            // Not `Sized`, which is a supertrait of `Copy`/`Clone`.
1156            ty::Dynamic(..) | ty::Str | ty::Slice(..) | ty::Foreign(..) => {}
1157
1158            // Not `Copy` or `Clone` by design.
1159            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                                // Not yet resolved.
1175                                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                    // Not yet resolved.
1189                    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                    // Not yet resolved.
1200                    candidates.ambiguous = true;
1201                } else {
1202                    candidates.vec.push(BuiltinCandidate);
1203                }
1204            }
1205
1206            ty::CoroutineWitness(..) => {
1207                candidates.vec.push(SizedCandidate);
1208            }
1209
1210            // Fallback to whatever user-defined impls or param-env clauses exist in this case.
1211            ty::Adt(..) | ty::Alias(..) | ty::Param(..) | ty::Placeholder(..) => {}
1212
1213            ty::Infer(ty::TyVar(_)) => {
1214                candidates.ambiguous = true;
1215            }
1216
1217            // Only appears when assembling higher-ranked `for<T> T: Clone`.
1218            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    /// Assembles the `Sized` and `MetaSized` traits which are built-in to the language itself.
1227    #[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            // Always sized.
1236            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            // Conditionally `Sized`.
1267            ty::Tuple(..) | ty::Pat(..) | ty::Adt(..) | ty::UnsafeBinder(_) => {
1268                candidates.vec.push(SizedCandidate);
1269            }
1270
1271            // `MetaSized` but not `Sized`.
1272            ty::Str | ty::Slice(_) | ty::Dynamic(..) => match sizedness {
1273                SizedTraitKind::Sized => {}
1274                SizedTraitKind::MetaSized => {
1275                    candidates.vec.push(SizedCandidate);
1276                }
1277            },
1278
1279            // Not `MetaSized` or `Sized`.
1280            ty::Foreign(..) => {}
1281
1282            ty::Alias(..) | ty::Param(_) | ty::Placeholder(..) => {}
1283
1284            ty::Infer(ty::TyVar(_)) => {
1285                candidates.ambiguous = true;
1286            }
1287
1288            // Only appears when assembling higher-ranked `for<T> T: Sized`.
1289            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}