rustc_trait_selection/traits/select/
confirmation.rs

1//! Confirmation.
2//!
3//! Confirmation unifies the output type parameters of the trait
4//! with the values found in the obligation, possibly yielding a
5//! type error. See the [rustc dev guide] for more details.
6//!
7//! [rustc dev guide]:
8//! https://rustc-dev-guide.rust-lang.org/traits/resolution.html#confirmation
9
10use std::ops::ControlFlow;
11
12use rustc_data_structures::stack::ensure_sufficient_stack;
13use rustc_hir::lang_items::LangItem;
14use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferOk};
15use rustc_infer::traits::ObligationCauseCode;
16use rustc_middle::traits::{BuiltinImplSource, SignatureMismatchData};
17use rustc_middle::ty::{self, GenericArgsRef, Region, SizedTraitKind, Ty, TyCtxt, Upcast};
18use rustc_middle::{bug, span_bug};
19use rustc_span::def_id::DefId;
20use thin_vec::thin_vec;
21use tracing::{debug, instrument};
22
23use super::SelectionCandidate::{self, *};
24use super::{PredicateObligations, SelectionContext};
25use crate::traits::normalize::{normalize_with_depth, normalize_with_depth_to};
26use crate::traits::util::{self, closure_trait_ref_and_return_type};
27use crate::traits::{
28    ImplSource, ImplSourceUserDefinedData, Normalized, Obligation, ObligationCause,
29    PolyTraitObligation, PredicateObligation, Selection, SelectionError, TraitObligation,
30};
31
32impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
33    #[instrument(level = "debug", skip(self))]
34    pub(super) fn confirm_candidate(
35        &mut self,
36        obligation: &PolyTraitObligation<'tcx>,
37        candidate: SelectionCandidate<'tcx>,
38    ) -> Result<Selection<'tcx>, SelectionError<'tcx>> {
39        Ok(match candidate {
40            SizedCandidate => {
41                let data = self.confirm_builtin_candidate(obligation);
42                ImplSource::Builtin(BuiltinImplSource::Misc, data)
43            }
44
45            BuiltinCandidate => {
46                let data = self.confirm_builtin_candidate(obligation);
47                ImplSource::Builtin(BuiltinImplSource::Misc, data)
48            }
49
50            TransmutabilityCandidate => {
51                let data = self.confirm_transmutability_candidate(obligation)?;
52                ImplSource::Builtin(BuiltinImplSource::Misc, data)
53            }
54
55            ParamCandidate(param) => {
56                let obligations =
57                    self.confirm_param_candidate(obligation, param.map_bound(|t| t.trait_ref));
58                ImplSource::Param(obligations)
59            }
60
61            ImplCandidate(impl_def_id) => {
62                ImplSource::UserDefined(self.confirm_impl_candidate(obligation, impl_def_id))
63            }
64
65            AutoImplCandidate => {
66                let data = self.confirm_auto_impl_candidate(obligation)?;
67                ImplSource::Builtin(BuiltinImplSource::Misc, data)
68            }
69
70            ProjectionCandidate { idx, .. } => {
71                let obligations = self.confirm_projection_candidate(obligation, idx)?;
72                ImplSource::Param(obligations)
73            }
74
75            ObjectCandidate(idx) => self.confirm_object_candidate(obligation, idx)?,
76
77            ClosureCandidate { .. } => {
78                let vtable_closure = self.confirm_closure_candidate(obligation)?;
79                ImplSource::Builtin(BuiltinImplSource::Misc, vtable_closure)
80            }
81
82            AsyncClosureCandidate => {
83                let vtable_closure = self.confirm_async_closure_candidate(obligation)?;
84                ImplSource::Builtin(BuiltinImplSource::Misc, vtable_closure)
85            }
86
87            // No nested obligations or confirmation process. The checks that we do in
88            // candidate assembly are sufficient.
89            AsyncFnKindHelperCandidate => {
90                ImplSource::Builtin(BuiltinImplSource::Misc, PredicateObligations::new())
91            }
92
93            CoroutineCandidate => {
94                let vtable_coroutine = self.confirm_coroutine_candidate(obligation)?;
95                ImplSource::Builtin(BuiltinImplSource::Misc, vtable_coroutine)
96            }
97
98            FutureCandidate => {
99                let vtable_future = self.confirm_future_candidate(obligation)?;
100                ImplSource::Builtin(BuiltinImplSource::Misc, vtable_future)
101            }
102
103            IteratorCandidate => {
104                let vtable_iterator = self.confirm_iterator_candidate(obligation)?;
105                ImplSource::Builtin(BuiltinImplSource::Misc, vtable_iterator)
106            }
107
108            AsyncIteratorCandidate => {
109                let vtable_iterator = self.confirm_async_iterator_candidate(obligation)?;
110                ImplSource::Builtin(BuiltinImplSource::Misc, vtable_iterator)
111            }
112
113            FnPointerCandidate => {
114                let data = self.confirm_fn_pointer_candidate(obligation)?;
115                ImplSource::Builtin(BuiltinImplSource::Misc, data)
116            }
117
118            PointerLikeCandidate => {
119                let data = self.confirm_pointer_like_candidate(obligation);
120                ImplSource::Builtin(BuiltinImplSource::Misc, data)
121            }
122
123            TraitAliasCandidate => {
124                let data = self.confirm_trait_alias_candidate(obligation);
125                ImplSource::Builtin(BuiltinImplSource::Misc, data)
126            }
127
128            BuiltinObjectCandidate => {
129                // This indicates something like `Trait + Send: Send`. In this case, we know that
130                // this holds because that's what the object type is telling us, and there's really
131                // no additional obligations to prove and no types in particular to unify, etc.
132                ImplSource::Builtin(BuiltinImplSource::Misc, PredicateObligations::new())
133            }
134
135            BuiltinUnsizeCandidate => self.confirm_builtin_unsize_candidate(obligation)?,
136
137            TraitUpcastingUnsizeCandidate(idx) => {
138                self.confirm_trait_upcasting_unsize_candidate(obligation, idx)?
139            }
140
141            BikeshedGuaranteedNoDropCandidate => {
142                self.confirm_bikeshed_guaranteed_no_drop_candidate(obligation)
143            }
144        })
145    }
146
147    fn confirm_projection_candidate(
148        &mut self,
149        obligation: &PolyTraitObligation<'tcx>,
150        idx: usize,
151    ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
152        let placeholder_trait_predicate =
153            self.infcx.enter_forall_and_leak_universe(obligation.predicate).trait_ref;
154        let placeholder_self_ty = self.infcx.shallow_resolve(placeholder_trait_predicate.self_ty());
155        let candidate_predicate = self
156            .for_each_item_bound(
157                placeholder_self_ty,
158                |_, clause, clause_idx, _| {
159                    if clause_idx == idx {
160                        ControlFlow::Break(clause)
161                    } else {
162                        ControlFlow::Continue(())
163                    }
164                },
165                || unreachable!(),
166            )
167            .break_value()
168            .expect("expected to index into clause that exists");
169        let candidate_predicate = candidate_predicate
170            .as_trait_clause()
171            .expect("projection candidate is not a trait predicate");
172        let candidate_predicate =
173            util::lazily_elaborate_sizedness_candidate(self.infcx, obligation, candidate_predicate);
174
175        let candidate = candidate_predicate.map_bound(|t| t.trait_ref);
176
177        let candidate = self.infcx.instantiate_binder_with_fresh_vars(
178            obligation.cause.span,
179            BoundRegionConversionTime::HigherRankedType,
180            candidate,
181        );
182        let mut obligations = PredicateObligations::new();
183        let candidate = normalize_with_depth_to(
184            self,
185            obligation.param_env,
186            obligation.cause.clone(),
187            obligation.recursion_depth + 1,
188            candidate,
189            &mut obligations,
190        );
191
192        obligations.extend(
193            self.infcx
194                .at(&obligation.cause, obligation.param_env)
195                .eq(DefineOpaqueTypes::No, placeholder_trait_predicate, candidate)
196                .map(|InferOk { obligations, .. }| obligations)
197                .map_err(|_| SelectionError::Unimplemented)?,
198        );
199
200        Ok(obligations)
201    }
202
203    fn confirm_param_candidate(
204        &mut self,
205        obligation: &PolyTraitObligation<'tcx>,
206        param: ty::PolyTraitRef<'tcx>,
207    ) -> PredicateObligations<'tcx> {
208        debug!(?obligation, ?param, "confirm_param_candidate");
209
210        let param = util::lazily_elaborate_sizedness_candidate(
211            self.infcx,
212            obligation,
213            param.upcast(self.infcx.tcx),
214        )
215        .map_bound(|p| p.trait_ref);
216
217        // During evaluation, we already checked that this
218        // where-clause trait-ref could be unified with the obligation
219        // trait-ref. Repeat that unification now without any
220        // transactional boundary; it should not fail.
221        match self.match_where_clause_trait_ref(obligation, param) {
222            Ok(obligations) => obligations,
223            Err(()) => {
224                bug!(
225                    "Where clause `{:?}` was applicable to `{:?}` but now is not",
226                    param,
227                    obligation
228                );
229            }
230        }
231    }
232
233    #[instrument(level = "debug", skip(self), ret)]
234    fn confirm_builtin_candidate(
235        &mut self,
236        obligation: &PolyTraitObligation<'tcx>,
237    ) -> PredicateObligations<'tcx> {
238        debug!(?obligation, "confirm_builtin_candidate");
239        let tcx = self.tcx();
240        let trait_def = obligation.predicate.def_id();
241        let self_ty = self.infcx.shallow_resolve(
242            self.infcx.enter_forall_and_leak_universe(obligation.predicate.self_ty()),
243        );
244        let types = match tcx.as_lang_item(trait_def) {
245            Some(LangItem::Sized) => self.sizedness_conditions(self_ty, SizedTraitKind::Sized),
246            Some(LangItem::MetaSized) => {
247                self.sizedness_conditions(self_ty, SizedTraitKind::MetaSized)
248            }
249            Some(LangItem::PointeeSized) => {
250                bug!("`PointeeSized` is removing during lowering");
251            }
252            Some(LangItem::Copy | LangItem::Clone | LangItem::TrivialClone) => {
253                self.copy_clone_conditions(self_ty)
254            }
255            Some(LangItem::FusedIterator) => {
256                if self.coroutine_is_gen(self_ty) {
257                    ty::Binder::dummy(vec![])
258                } else {
259                    unreachable!("tried to assemble `FusedIterator` for non-gen coroutine");
260                }
261            }
262            Some(
263                LangItem::Destruct
264                | LangItem::DiscriminantKind
265                | LangItem::FnPtrTrait
266                | LangItem::PointeeTrait
267                | LangItem::Tuple
268                | LangItem::Unpin,
269            ) => ty::Binder::dummy(vec![]),
270            other => bug!("unexpected builtin trait {trait_def:?} ({other:?})"),
271        };
272        let types = self.infcx.enter_forall_and_leak_universe(types);
273
274        let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
275        self.collect_predicates_for_types(
276            obligation.param_env,
277            cause,
278            obligation.recursion_depth + 1,
279            trait_def,
280            types,
281        )
282    }
283
284    #[instrument(level = "debug", skip(self))]
285    fn confirm_transmutability_candidate(
286        &mut self,
287        obligation: &PolyTraitObligation<'tcx>,
288    ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
289        use rustc_transmute::{Answer, Assume, Condition};
290
291        /// Flatten the `Condition` tree into a conjunction of obligations.
292        #[instrument(level = "debug", skip(tcx, obligation))]
293        fn flatten_answer_tree<'tcx>(
294            tcx: TyCtxt<'tcx>,
295            obligation: &PolyTraitObligation<'tcx>,
296            cond: Condition<Region<'tcx>, Ty<'tcx>>,
297            assume: Assume,
298        ) -> PredicateObligations<'tcx> {
299            match cond {
300                // FIXME(bryangarza): Add separate `IfAny` case, instead of treating as `IfAll`
301                // Not possible until the trait solver supports disjunctions of obligations
302                Condition::IfAll(conds) | Condition::IfAny(conds) => conds
303                    .into_iter()
304                    .flat_map(|cond| flatten_answer_tree(tcx, obligation, cond, assume))
305                    .collect(),
306                Condition::Immutable { ty } => {
307                    let trait_ref = ty::TraitRef::new(
308                        tcx,
309                        tcx.require_lang_item(LangItem::Freeze, obligation.cause.span),
310                        [ty::GenericArg::from(ty)],
311                    );
312                    thin_vec![Obligation::with_depth(
313                        tcx,
314                        obligation.cause.clone(),
315                        obligation.recursion_depth + 1,
316                        obligation.param_env,
317                        trait_ref,
318                    )]
319                }
320                Condition::Outlives { long, short } => {
321                    let outlives = ty::OutlivesPredicate(long, short);
322                    thin_vec![Obligation::with_depth(
323                        tcx,
324                        obligation.cause.clone(),
325                        obligation.recursion_depth + 1,
326                        obligation.param_env,
327                        outlives,
328                    )]
329                }
330                Condition::Transmutable { src, dst } => {
331                    let transmute_trait = obligation.predicate.def_id();
332                    let assume = obligation.predicate.skip_binder().trait_ref.args.const_at(2);
333                    let trait_ref = ty::TraitRef::new(
334                        tcx,
335                        transmute_trait,
336                        [
337                            ty::GenericArg::from(dst),
338                            ty::GenericArg::from(src),
339                            ty::GenericArg::from(assume),
340                        ],
341                    );
342                    thin_vec![Obligation::with_depth(
343                        tcx,
344                        obligation.cause.clone(),
345                        obligation.recursion_depth + 1,
346                        obligation.param_env,
347                        trait_ref,
348                    )]
349                }
350            }
351        }
352
353        let predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
354
355        let mut assume = predicate.trait_ref.args.const_at(2);
356        if self.tcx().features().generic_const_exprs() {
357            assume = crate::traits::evaluate_const(self.infcx, assume, obligation.param_env)
358        }
359        let Some(assume) = rustc_transmute::Assume::from_const(self.infcx.tcx, assume) else {
360            return Err(SelectionError::Unimplemented);
361        };
362
363        let dst = predicate.trait_ref.args.type_at(0);
364        let src = predicate.trait_ref.args.type_at(1);
365
366        debug!(?src, ?dst);
367        let mut transmute_env = rustc_transmute::TransmuteTypeEnv::new(self.infcx.tcx);
368        let maybe_transmutable =
369            transmute_env.is_transmutable(rustc_transmute::Types { dst, src }, assume);
370
371        let fully_flattened = match maybe_transmutable {
372            Answer::No(_) => Err(SelectionError::Unimplemented)?,
373            Answer::If(cond) => flatten_answer_tree(self.tcx(), obligation, cond, assume),
374            Answer::Yes => PredicateObligations::new(),
375        };
376
377        debug!(?fully_flattened);
378        Ok(fully_flattened)
379    }
380
381    /// This handles the case where an `auto trait Foo` impl is being used.
382    /// The idea is that the impl applies to `X : Foo` if the following conditions are met:
383    ///
384    /// 1. For each constituent type `Y` in `X`, `Y : Foo` holds
385    /// 2. For each where-clause `C` declared on `Foo`, `[Self => X] C` holds.
386    fn confirm_auto_impl_candidate(
387        &mut self,
388        obligation: &PolyTraitObligation<'tcx>,
389    ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
390        ensure_sufficient_stack(|| {
391            assert_eq!(obligation.predicate.polarity(), ty::PredicatePolarity::Positive);
392
393            let self_ty =
394                obligation.predicate.self_ty().map_bound(|ty| self.infcx.shallow_resolve(ty));
395            let self_ty = self.infcx.enter_forall_and_leak_universe(self_ty);
396
397            let constituents = self.constituent_types_for_auto_trait(self_ty)?;
398            let constituents = self.infcx.enter_forall_and_leak_universe(constituents);
399
400            let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
401            let mut obligations = self.collect_predicates_for_types(
402                obligation.param_env,
403                cause.clone(),
404                obligation.recursion_depth + 1,
405                obligation.predicate.def_id(),
406                constituents.types,
407            );
408
409            // Only normalize these goals if `-Zhigher-ranked-assumptions` is enabled, since
410            // we don't want to cause ourselves to do extra work if we're not even able to
411            // take advantage of these assumption clauses.
412            if self.tcx().sess.opts.unstable_opts.higher_ranked_assumptions {
413                // FIXME(coroutine_clone): We could uplift this into `collect_predicates_for_types`
414                // and do this for `Copy`/`Clone` too, but that's feature-gated so it doesn't really
415                // matter yet.
416                for assumption in constituents.assumptions {
417                    let assumption = normalize_with_depth_to(
418                        self,
419                        obligation.param_env,
420                        cause.clone(),
421                        obligation.recursion_depth + 1,
422                        assumption,
423                        &mut obligations,
424                    );
425                    self.infcx.register_region_assumption(assumption);
426                }
427            }
428
429            Ok(obligations)
430        })
431    }
432
433    fn confirm_impl_candidate(
434        &mut self,
435        obligation: &PolyTraitObligation<'tcx>,
436        impl_def_id: DefId,
437    ) -> ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>> {
438        debug!(?obligation, ?impl_def_id, "confirm_impl_candidate");
439
440        // First, create the generic parameters by matching the impl again,
441        // this time not in a probe.
442        let args = self.rematch_impl(impl_def_id, obligation);
443        debug!(?args, "impl args");
444        ensure_sufficient_stack(|| {
445            self.vtable_impl(
446                impl_def_id,
447                args,
448                &obligation.cause,
449                obligation.recursion_depth + 1,
450                obligation.param_env,
451                obligation.predicate,
452            )
453        })
454    }
455
456    fn vtable_impl(
457        &mut self,
458        impl_def_id: DefId,
459        args: Normalized<'tcx, GenericArgsRef<'tcx>>,
460        cause: &ObligationCause<'tcx>,
461        recursion_depth: usize,
462        param_env: ty::ParamEnv<'tcx>,
463        parent_trait_pred: ty::Binder<'tcx, ty::TraitPredicate<'tcx>>,
464    ) -> ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>> {
465        debug!(?impl_def_id, ?args, ?recursion_depth, "vtable_impl");
466
467        let mut impl_obligations = self.impl_or_trait_obligations(
468            cause,
469            recursion_depth,
470            param_env,
471            impl_def_id,
472            args.value,
473            parent_trait_pred,
474        );
475
476        debug!(?impl_obligations, "vtable_impl");
477
478        // Because of RFC447, the impl-trait-ref and obligations
479        // are sufficient to determine the impl args, without
480        // relying on projections in the impl-trait-ref.
481        //
482        // e.g., `impl<U: Tr, V: Iterator<Item=U>> Foo<<U as Tr>::T> for V`
483        impl_obligations.extend(args.obligations);
484
485        ImplSourceUserDefinedData { impl_def_id, args: args.value, nested: impl_obligations }
486    }
487
488    fn confirm_object_candidate(
489        &mut self,
490        obligation: &PolyTraitObligation<'tcx>,
491        index: usize,
492    ) -> Result<ImplSource<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
493        let tcx = self.tcx();
494        debug!(?obligation, ?index, "confirm_object_candidate");
495
496        let trait_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
497        let self_ty = self.infcx.shallow_resolve(trait_predicate.self_ty());
498        let ty::Dynamic(data, ..) = *self_ty.kind() else {
499            span_bug!(obligation.cause.span, "object candidate with non-object");
500        };
501
502        let object_trait_ref = data.principal().unwrap_or_else(|| {
503            span_bug!(obligation.cause.span, "object candidate with no principal")
504        });
505        let object_trait_ref = self.infcx.instantiate_binder_with_fresh_vars(
506            obligation.cause.span,
507            BoundRegionConversionTime::HigherRankedType,
508            object_trait_ref,
509        );
510        let object_trait_ref = object_trait_ref.with_self_ty(self.tcx(), self_ty);
511
512        let mut nested = PredicateObligations::new();
513
514        let mut supertraits = util::supertraits(tcx, ty::Binder::dummy(object_trait_ref));
515        let unnormalized_upcast_trait_ref =
516            supertraits.nth(index).expect("supertraits iterator no longer has as many elements");
517
518        let upcast_trait_ref = self.infcx.instantiate_binder_with_fresh_vars(
519            obligation.cause.span,
520            BoundRegionConversionTime::HigherRankedType,
521            unnormalized_upcast_trait_ref,
522        );
523        let upcast_trait_ref = normalize_with_depth_to(
524            self,
525            obligation.param_env,
526            obligation.cause.clone(),
527            obligation.recursion_depth + 1,
528            upcast_trait_ref,
529            &mut nested,
530        );
531
532        nested.extend(
533            self.infcx
534                .at(&obligation.cause, obligation.param_env)
535                .eq(DefineOpaqueTypes::No, trait_predicate.trait_ref, upcast_trait_ref)
536                .map(|InferOk { obligations, .. }| obligations)
537                .map_err(|_| SelectionError::Unimplemented)?,
538        );
539
540        // Check supertraits hold. This is so that their associated type bounds
541        // will be checked in the code below.
542        for (supertrait, _) in tcx
543            .explicit_super_predicates_of(trait_predicate.def_id())
544            .iter_instantiated_copied(tcx, trait_predicate.trait_ref.args)
545        {
546            let normalized_supertrait = normalize_with_depth_to(
547                self,
548                obligation.param_env,
549                obligation.cause.clone(),
550                obligation.recursion_depth + 1,
551                supertrait,
552                &mut nested,
553            );
554            nested.push(obligation.with(tcx, normalized_supertrait));
555        }
556
557        let assoc_types: Vec<_> = tcx
558            .associated_items(trait_predicate.def_id())
559            .in_definition_order()
560            // Associated types that require `Self: Sized` do not show up in the built-in
561            // implementation of `Trait for dyn Trait`, and can be dropped here.
562            .filter(|item| !tcx.generics_require_sized_self(item.def_id))
563            .filter_map(|item| if item.is_type() { Some(item.def_id) } else { None })
564            .collect();
565
566        for assoc_type in assoc_types {
567            let defs: &ty::Generics = tcx.generics_of(assoc_type);
568
569            if !defs.own_params.is_empty() {
570                tcx.dcx().span_delayed_bug(
571                    obligation.cause.span,
572                    "GATs in trait object shouldn't have been considered",
573                );
574                return Err(SelectionError::TraitDynIncompatible(trait_predicate.trait_ref.def_id));
575            }
576
577            // This maybe belongs in wf, but that can't (doesn't) handle
578            // higher-ranked things.
579            // Prevent, e.g., `dyn Iterator<Item = str>`.
580            for bound in self.tcx().item_bounds(assoc_type).transpose_iter() {
581                let normalized_bound = normalize_with_depth_to(
582                    self,
583                    obligation.param_env,
584                    obligation.cause.clone(),
585                    obligation.recursion_depth + 1,
586                    bound.instantiate(tcx, trait_predicate.trait_ref.args),
587                    &mut nested,
588                );
589                nested.push(obligation.with(tcx, normalized_bound));
590            }
591        }
592
593        debug!(?nested, "object nested obligations");
594
595        Ok(ImplSource::Builtin(BuiltinImplSource::Object(index), nested))
596    }
597
598    fn confirm_fn_pointer_candidate(
599        &mut self,
600        obligation: &PolyTraitObligation<'tcx>,
601    ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
602        debug!(?obligation, "confirm_fn_pointer_candidate");
603        let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
604        let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
605
606        let tcx = self.tcx();
607        let sig = self_ty.fn_sig(tcx);
608        let trait_ref = closure_trait_ref_and_return_type(
609            tcx,
610            obligation.predicate.def_id(),
611            self_ty,
612            sig,
613            util::TupleArgumentsFlag::Yes,
614        )
615        .map_bound(|(trait_ref, _)| trait_ref);
616
617        let mut nested =
618            self.equate_trait_refs(obligation.with(tcx, placeholder_predicate), trait_ref)?;
619        let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
620
621        // Confirm the `type Output: Sized;` bound that is present on `FnOnce`
622        let output_ty = self.infcx.enter_forall_and_leak_universe(sig.output());
623        let output_ty = normalize_with_depth_to(
624            self,
625            obligation.param_env,
626            cause.clone(),
627            obligation.recursion_depth,
628            output_ty,
629            &mut nested,
630        );
631        let tr = ty::TraitRef::new(
632            self.tcx(),
633            self.tcx().require_lang_item(LangItem::Sized, cause.span),
634            [output_ty],
635        );
636        nested.push(Obligation::new(self.infcx.tcx, cause, obligation.param_env, tr));
637
638        Ok(nested)
639    }
640
641    fn confirm_pointer_like_candidate(
642        &mut self,
643        obligation: &PolyTraitObligation<'tcx>,
644    ) -> PredicateObligations<'tcx> {
645        debug!(?obligation, "confirm_pointer_like_candidate");
646        let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
647        let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
648        let ty::Pat(base, _) = *self_ty.kind() else { bug!() };
649        let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
650
651        self.collect_predicates_for_types(
652            obligation.param_env,
653            cause,
654            obligation.recursion_depth + 1,
655            placeholder_predicate.def_id(),
656            vec![base],
657        )
658    }
659
660    fn confirm_trait_alias_candidate(
661        &mut self,
662        obligation: &PolyTraitObligation<'tcx>,
663    ) -> PredicateObligations<'tcx> {
664        debug!(?obligation, "confirm_trait_alias_candidate");
665
666        let predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
667        let trait_ref = predicate.trait_ref;
668        let trait_def_id = trait_ref.def_id;
669        let args = trait_ref.args;
670
671        let trait_obligations = self.impl_or_trait_obligations(
672            &obligation.cause,
673            obligation.recursion_depth,
674            obligation.param_env,
675            trait_def_id,
676            args,
677            obligation.predicate,
678        );
679
680        debug!(?trait_def_id, ?trait_obligations, "trait alias obligations");
681
682        trait_obligations
683    }
684
685    fn confirm_coroutine_candidate(
686        &mut self,
687        obligation: &PolyTraitObligation<'tcx>,
688    ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
689        let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
690        let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
691        let ty::Coroutine(coroutine_def_id, args) = *self_ty.kind() else {
692            bug!("closure candidate for non-closure {:?}", obligation);
693        };
694
695        debug!(?obligation, ?coroutine_def_id, ?args, "confirm_coroutine_candidate");
696
697        let coroutine_sig = args.as_coroutine().sig();
698
699        let (trait_ref, _, _) = super::util::coroutine_trait_ref_and_outputs(
700            self.tcx(),
701            obligation.predicate.def_id(),
702            self_ty,
703            coroutine_sig,
704        );
705
706        let nested = self.equate_trait_refs(
707            obligation.with(self.tcx(), placeholder_predicate),
708            ty::Binder::dummy(trait_ref),
709        )?;
710        debug!(?trait_ref, ?nested, "coroutine candidate obligations");
711
712        Ok(nested)
713    }
714
715    fn confirm_future_candidate(
716        &mut self,
717        obligation: &PolyTraitObligation<'tcx>,
718    ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
719        let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
720        let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
721        let ty::Coroutine(coroutine_def_id, args) = *self_ty.kind() else {
722            bug!("closure candidate for non-closure {:?}", obligation);
723        };
724
725        debug!(?obligation, ?coroutine_def_id, ?args, "confirm_future_candidate");
726
727        let coroutine_sig = args.as_coroutine().sig();
728
729        let (trait_ref, _) = super::util::future_trait_ref_and_outputs(
730            self.tcx(),
731            obligation.predicate.def_id(),
732            self_ty,
733            coroutine_sig,
734        );
735
736        let nested = self.equate_trait_refs(
737            obligation.with(self.tcx(), placeholder_predicate),
738            ty::Binder::dummy(trait_ref),
739        )?;
740        debug!(?trait_ref, ?nested, "future candidate obligations");
741
742        Ok(nested)
743    }
744
745    fn confirm_iterator_candidate(
746        &mut self,
747        obligation: &PolyTraitObligation<'tcx>,
748    ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
749        let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
750        let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
751        let ty::Coroutine(coroutine_def_id, args) = *self_ty.kind() else {
752            bug!("closure candidate for non-closure {:?}", obligation);
753        };
754
755        debug!(?obligation, ?coroutine_def_id, ?args, "confirm_iterator_candidate");
756
757        let gen_sig = args.as_coroutine().sig();
758
759        let (trait_ref, _) = super::util::iterator_trait_ref_and_outputs(
760            self.tcx(),
761            obligation.predicate.def_id(),
762            self_ty,
763            gen_sig,
764        );
765
766        let nested = self.equate_trait_refs(
767            obligation.with(self.tcx(), placeholder_predicate),
768            ty::Binder::dummy(trait_ref),
769        )?;
770        debug!(?trait_ref, ?nested, "iterator candidate obligations");
771
772        Ok(nested)
773    }
774
775    fn confirm_async_iterator_candidate(
776        &mut self,
777        obligation: &PolyTraitObligation<'tcx>,
778    ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
779        let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
780        let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
781        let ty::Coroutine(coroutine_def_id, args) = *self_ty.kind() else {
782            bug!("closure candidate for non-closure {:?}", obligation);
783        };
784
785        debug!(?obligation, ?coroutine_def_id, ?args, "confirm_async_iterator_candidate");
786
787        let gen_sig = args.as_coroutine().sig();
788
789        let (trait_ref, _) = super::util::async_iterator_trait_ref_and_outputs(
790            self.tcx(),
791            obligation.predicate.def_id(),
792            self_ty,
793            gen_sig,
794        );
795
796        let nested = self.equate_trait_refs(
797            obligation.with(self.tcx(), placeholder_predicate),
798            ty::Binder::dummy(trait_ref),
799        )?;
800        debug!(?trait_ref, ?nested, "iterator candidate obligations");
801
802        Ok(nested)
803    }
804
805    #[instrument(skip(self), level = "debug")]
806    fn confirm_closure_candidate(
807        &mut self,
808        obligation: &PolyTraitObligation<'tcx>,
809    ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
810        let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
811        let self_ty: Ty<'_> = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
812
813        let trait_ref = match *self_ty.kind() {
814            ty::Closure(..) => {
815                self.closure_trait_ref_unnormalized(self_ty, obligation.predicate.def_id())
816            }
817            ty::CoroutineClosure(_, args) => {
818                args.as_coroutine_closure().coroutine_closure_sig().map_bound(|sig| {
819                    ty::TraitRef::new(
820                        self.tcx(),
821                        obligation.predicate.def_id(),
822                        [self_ty, sig.tupled_inputs_ty],
823                    )
824                })
825            }
826            _ => {
827                bug!("closure candidate for non-closure {:?}", obligation);
828            }
829        };
830
831        self.equate_trait_refs(obligation.with(self.tcx(), placeholder_predicate), trait_ref)
832    }
833
834    #[instrument(skip(self), level = "debug")]
835    fn confirm_async_closure_candidate(
836        &mut self,
837        obligation: &PolyTraitObligation<'tcx>,
838    ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
839        let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
840        let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
841
842        let tcx = self.tcx();
843
844        let mut nested = PredicateObligations::new();
845        let (trait_ref, kind_ty) = match *self_ty.kind() {
846            ty::CoroutineClosure(_, args) => {
847                let args = args.as_coroutine_closure();
848                let trait_ref = args.coroutine_closure_sig().map_bound(|sig| {
849                    ty::TraitRef::new(
850                        self.tcx(),
851                        obligation.predicate.def_id(),
852                        [self_ty, sig.tupled_inputs_ty],
853                    )
854                });
855
856                // Note that unlike below, we don't need to check `Future + Sized` for
857                // the output coroutine because they are `Future + Sized` by construction.
858
859                (trait_ref, args.kind_ty())
860            }
861            ty::FnDef(..) | ty::FnPtr(..) => {
862                let sig = self_ty.fn_sig(tcx);
863                let trait_ref = sig.map_bound(|sig| {
864                    ty::TraitRef::new(
865                        self.tcx(),
866                        obligation.predicate.def_id(),
867                        [self_ty, Ty::new_tup(tcx, sig.inputs())],
868                    )
869                });
870
871                // We must additionally check that the return type impls `Future + Sized`.
872                let future_trait_def_id =
873                    tcx.require_lang_item(LangItem::Future, obligation.cause.span);
874                nested.push(obligation.with(
875                    tcx,
876                    sig.output().map_bound(|output_ty| {
877                        ty::TraitRef::new(tcx, future_trait_def_id, [output_ty])
878                    }),
879                ));
880                let sized_trait_def_id =
881                    tcx.require_lang_item(LangItem::Sized, obligation.cause.span);
882                nested.push(obligation.with(
883                    tcx,
884                    sig.output().map_bound(|output_ty| {
885                        ty::TraitRef::new(tcx, sized_trait_def_id, [output_ty])
886                    }),
887                ));
888
889                (trait_ref, Ty::from_closure_kind(tcx, ty::ClosureKind::Fn))
890            }
891            ty::Closure(_, args) => {
892                let args = args.as_closure();
893                let sig = args.sig();
894                let trait_ref = sig.map_bound(|sig| {
895                    ty::TraitRef::new(
896                        self.tcx(),
897                        obligation.predicate.def_id(),
898                        [self_ty, sig.inputs()[0]],
899                    )
900                });
901
902                // We must additionally check that the return type impls `Future + Sized`.
903                let future_trait_def_id =
904                    tcx.require_lang_item(LangItem::Future, obligation.cause.span);
905                let placeholder_output_ty = self.infcx.enter_forall_and_leak_universe(sig.output());
906                nested.push(obligation.with(
907                    tcx,
908                    ty::TraitRef::new(tcx, future_trait_def_id, [placeholder_output_ty]),
909                ));
910                let sized_trait_def_id =
911                    tcx.require_lang_item(LangItem::Sized, obligation.cause.span);
912                nested.push(obligation.with(
913                    tcx,
914                    sig.output().map_bound(|output_ty| {
915                        ty::TraitRef::new(tcx, sized_trait_def_id, [output_ty])
916                    }),
917                ));
918
919                (trait_ref, args.kind_ty())
920            }
921            _ => bug!("expected callable type for AsyncFn candidate"),
922        };
923
924        nested.extend(
925            self.equate_trait_refs(obligation.with(tcx, placeholder_predicate), trait_ref)?,
926        );
927
928        let goal_kind =
929            self.tcx().async_fn_trait_kind_from_def_id(obligation.predicate.def_id()).unwrap();
930
931        // If we have not yet determined the `ClosureKind` of the closure or coroutine-closure,
932        // then additionally register an `AsyncFnKindHelper` goal which will fail if the kind
933        // is constrained to an insufficient type later on.
934        if let Some(closure_kind) = self.infcx.shallow_resolve(kind_ty).to_opt_closure_kind() {
935            if !closure_kind.extends(goal_kind) {
936                return Err(SelectionError::Unimplemented);
937            }
938        } else {
939            nested.push(Obligation::new(
940                self.tcx(),
941                obligation.derived_cause(ObligationCauseCode::BuiltinDerived),
942                obligation.param_env,
943                ty::TraitRef::new(
944                    self.tcx(),
945                    self.tcx()
946                        .require_lang_item(LangItem::AsyncFnKindHelper, obligation.cause.span),
947                    [kind_ty, Ty::from_closure_kind(self.tcx(), goal_kind)],
948                ),
949            ));
950        }
951
952        Ok(nested)
953    }
954
955    /// In the case of closure types and fn pointers,
956    /// we currently treat the input type parameters on the trait as
957    /// outputs. This means that when we have a match we have only
958    /// considered the self type, so we have to go back and make sure
959    /// to relate the argument types too. This is kind of wrong, but
960    /// since we control the full set of impls, also not that wrong,
961    /// and it DOES yield better error messages (since we don't report
962    /// errors as if there is no applicable impl, but rather report
963    /// errors are about mismatched argument types.
964    ///
965    /// Here is an example. Imagine we have a closure expression
966    /// and we desugared it so that the type of the expression is
967    /// `Closure`, and `Closure` expects `i32` as argument. Then it
968    /// is "as if" the compiler generated this impl:
969    /// ```ignore (illustrative)
970    /// impl Fn(i32) for Closure { ... }
971    /// ```
972    /// Now imagine our obligation is `Closure: Fn(usize)`. So far
973    /// we have matched the self type `Closure`. At this point we'll
974    /// compare the `i32` to `usize` and generate an error.
975    ///
976    /// Note that this checking occurs *after* the impl has selected,
977    /// because these output type parameters should not affect the
978    /// selection of the impl. Therefore, if there is a mismatch, we
979    /// report an error to the user.
980    #[instrument(skip(self), level = "trace")]
981    fn equate_trait_refs(
982        &mut self,
983        obligation: TraitObligation<'tcx>,
984        found_trait_ref: ty::PolyTraitRef<'tcx>,
985    ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
986        let found_trait_ref = self.infcx.instantiate_binder_with_fresh_vars(
987            obligation.cause.span,
988            BoundRegionConversionTime::HigherRankedType,
989            found_trait_ref,
990        );
991        // Normalize the obligation and expected trait refs together, because why not
992        let Normalized { obligations: nested, value: (obligation_trait_ref, found_trait_ref) } =
993            ensure_sufficient_stack(|| {
994                normalize_with_depth(
995                    self,
996                    obligation.param_env,
997                    obligation.cause.clone(),
998                    obligation.recursion_depth + 1,
999                    (obligation.predicate.trait_ref, found_trait_ref),
1000                )
1001            });
1002
1003        // needed to define opaque types for tests/ui/type-alias-impl-trait/assoc-projection-ice.rs
1004        self.infcx
1005            .at(&obligation.cause, obligation.param_env)
1006            .eq(DefineOpaqueTypes::Yes, obligation_trait_ref, found_trait_ref)
1007            .map(|InferOk { mut obligations, .. }| {
1008                obligations.extend(nested);
1009                obligations
1010            })
1011            .map_err(|terr| {
1012                SelectionError::SignatureMismatch(Box::new(SignatureMismatchData {
1013                    expected_trait_ref: obligation_trait_ref,
1014                    found_trait_ref,
1015                    terr,
1016                }))
1017            })
1018    }
1019
1020    fn confirm_trait_upcasting_unsize_candidate(
1021        &mut self,
1022        obligation: &PolyTraitObligation<'tcx>,
1023        idx: usize,
1024    ) -> Result<ImplSource<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
1025        let tcx = self.tcx();
1026
1027        // `assemble_candidates_for_unsizing` should ensure there are no late-bound
1028        // regions here. See the comment there for more details.
1029        let predicate = obligation.predicate.no_bound_vars().unwrap();
1030        let a_ty = self.infcx.shallow_resolve(predicate.self_ty());
1031        let b_ty = self.infcx.shallow_resolve(predicate.trait_ref.args.type_at(1));
1032
1033        let ty::Dynamic(a_data, a_region) = *a_ty.kind() else {
1034            bug!("expected `dyn` type in `confirm_trait_upcasting_unsize_candidate`")
1035        };
1036        let ty::Dynamic(b_data, b_region) = *b_ty.kind() else {
1037            bug!("expected `dyn` type in `confirm_trait_upcasting_unsize_candidate`")
1038        };
1039
1040        let source_principal = a_data.principal().unwrap().with_self_ty(tcx, a_ty);
1041        let unnormalized_upcast_principal =
1042            util::supertraits(tcx, source_principal).nth(idx).unwrap();
1043
1044        let nested = self
1045            .match_upcast_principal(
1046                obligation,
1047                unnormalized_upcast_principal,
1048                a_data,
1049                b_data,
1050                a_region,
1051                b_region,
1052            )?
1053            .expect("did not expect ambiguity during confirmation");
1054
1055        Ok(ImplSource::Builtin(BuiltinImplSource::TraitUpcasting(idx), nested))
1056    }
1057
1058    fn confirm_builtin_unsize_candidate(
1059        &mut self,
1060        obligation: &PolyTraitObligation<'tcx>,
1061    ) -> Result<ImplSource<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
1062        let tcx = self.tcx();
1063
1064        // `assemble_candidates_for_unsizing` should ensure there are no late-bound
1065        // regions here. See the comment there for more details.
1066        let source = self.infcx.shallow_resolve(obligation.self_ty().no_bound_vars().unwrap());
1067        let target = obligation.predicate.skip_binder().trait_ref.args.type_at(1);
1068        let target = self.infcx.shallow_resolve(target);
1069        debug!(?source, ?target, "confirm_builtin_unsize_candidate");
1070
1071        Ok(match (source.kind(), target.kind()) {
1072            // `dyn Trait + Kx + 'a` -> `dyn Trait + Ky + 'b` (auto traits and lifetime subtyping).
1073            (&ty::Dynamic(data_a, r_a), &ty::Dynamic(data_b, r_b)) => {
1074                // See `assemble_candidates_for_unsizing` for more info.
1075                // We already checked the compatibility of auto traits within `assemble_candidates_for_unsizing`.
1076                let existential_predicates = if data_b.principal().is_some() {
1077                    tcx.mk_poly_existential_predicates_from_iter(
1078                        data_a
1079                            .principal()
1080                            .map(|b| b.map_bound(ty::ExistentialPredicate::Trait))
1081                            .into_iter()
1082                            .chain(
1083                                data_a
1084                                    .projection_bounds()
1085                                    .map(|b| b.map_bound(ty::ExistentialPredicate::Projection)),
1086                            )
1087                            .chain(
1088                                data_b
1089                                    .auto_traits()
1090                                    .map(ty::ExistentialPredicate::AutoTrait)
1091                                    .map(ty::Binder::dummy),
1092                            ),
1093                    )
1094                } else {
1095                    // If we're unsizing to a dyn type that has no principal, then drop
1096                    // the principal and projections from the type. We use the auto traits
1097                    // from the RHS type since as we noted that we've checked for auto
1098                    // trait compatibility during unsizing.
1099                    tcx.mk_poly_existential_predicates_from_iter(
1100                        data_b
1101                            .auto_traits()
1102                            .map(ty::ExistentialPredicate::AutoTrait)
1103                            .map(ty::Binder::dummy),
1104                    )
1105                };
1106                let source_trait = Ty::new_dynamic(tcx, existential_predicates, r_b);
1107
1108                // Require that the traits involved in this upcast are **equal**;
1109                // only the **lifetime bound** is changed.
1110                let InferOk { mut obligations, .. } = self
1111                    .infcx
1112                    .at(&obligation.cause, obligation.param_env)
1113                    .sup(DefineOpaqueTypes::Yes, target, source_trait)
1114                    .map_err(|_| SelectionError::Unimplemented)?;
1115
1116                // Register one obligation for 'a: 'b.
1117                let outlives = ty::OutlivesPredicate(r_a, r_b);
1118                obligations.push(Obligation::with_depth(
1119                    tcx,
1120                    obligation.cause.clone(),
1121                    obligation.recursion_depth + 1,
1122                    obligation.param_env,
1123                    obligation.predicate.rebind(outlives),
1124                ));
1125
1126                ImplSource::Builtin(BuiltinImplSource::Misc, obligations)
1127            }
1128
1129            // `T` -> `dyn Trait`
1130            (_, &ty::Dynamic(data, r)) => {
1131                let mut object_dids = data.auto_traits().chain(data.principal_def_id());
1132                if let Some(did) = object_dids.find(|did| !tcx.is_dyn_compatible(*did)) {
1133                    return Err(SelectionError::TraitDynIncompatible(did));
1134                }
1135
1136                let predicate_to_obligation = |predicate| {
1137                    Obligation::with_depth(
1138                        tcx,
1139                        obligation.cause.clone(),
1140                        obligation.recursion_depth + 1,
1141                        obligation.param_env,
1142                        predicate,
1143                    )
1144                };
1145
1146                // Create obligations:
1147                //  - Casting `T` to `Trait`
1148                //  - For all the various builtin bounds attached to the object cast. (In other
1149                //  words, if the object type is `Foo + Send`, this would create an obligation for
1150                //  the `Send` check.)
1151                //  - Projection predicates
1152                let mut nested: PredicateObligations<'_> = data
1153                    .iter()
1154                    .map(|predicate| predicate_to_obligation(predicate.with_self_ty(tcx, source)))
1155                    .collect();
1156
1157                // We can only make objects from sized types.
1158                let tr = ty::TraitRef::new(
1159                    tcx,
1160                    tcx.require_lang_item(LangItem::Sized, obligation.cause.span),
1161                    [source],
1162                );
1163                nested.push(predicate_to_obligation(tr.upcast(tcx)));
1164
1165                // If the type is `Foo + 'a`, ensure that the type
1166                // being cast to `Foo + 'a` outlives `'a`:
1167                let outlives = ty::OutlivesPredicate(source, r);
1168                nested.push(predicate_to_obligation(
1169                    ty::ClauseKind::TypeOutlives(outlives).upcast(tcx),
1170                ));
1171
1172                ImplSource::Builtin(BuiltinImplSource::Misc, nested)
1173            }
1174
1175            // `[T; n]` -> `[T]`
1176            (&ty::Array(a, _), &ty::Slice(b)) => {
1177                let InferOk { obligations, .. } = self
1178                    .infcx
1179                    .at(&obligation.cause, obligation.param_env)
1180                    .eq(DefineOpaqueTypes::Yes, b, a)
1181                    .map_err(|_| SelectionError::Unimplemented)?;
1182
1183                ImplSource::Builtin(BuiltinImplSource::Misc, obligations)
1184            }
1185
1186            // `Struct<T>` -> `Struct<U>`
1187            (&ty::Adt(def, args_a), &ty::Adt(_, args_b)) => {
1188                let unsizing_params = tcx.unsizing_params_for_adt(def.did());
1189                if unsizing_params.is_empty() {
1190                    return Err(SelectionError::Unimplemented);
1191                }
1192
1193                let tail_field = def.non_enum_variant().tail();
1194                let tail_field_ty = tcx.type_of(tail_field.did);
1195
1196                let mut nested = PredicateObligations::new();
1197
1198                // Extract `TailField<T>` and `TailField<U>` from `Struct<T>` and `Struct<U>`,
1199                // normalizing in the process, since `type_of` returns something directly from
1200                // HIR ty lowering (which means it's un-normalized).
1201                let source_tail = normalize_with_depth_to(
1202                    self,
1203                    obligation.param_env,
1204                    obligation.cause.clone(),
1205                    obligation.recursion_depth + 1,
1206                    tail_field_ty.instantiate(tcx, args_a),
1207                    &mut nested,
1208                );
1209                let target_tail = normalize_with_depth_to(
1210                    self,
1211                    obligation.param_env,
1212                    obligation.cause.clone(),
1213                    obligation.recursion_depth + 1,
1214                    tail_field_ty.instantiate(tcx, args_b),
1215                    &mut nested,
1216                );
1217
1218                // Check that the source struct with the target's
1219                // unsizing parameters is equal to the target.
1220                let args =
1221                    tcx.mk_args_from_iter(args_a.iter().enumerate().map(|(i, k)| {
1222                        if unsizing_params.contains(i as u32) { args_b[i] } else { k }
1223                    }));
1224                let new_struct = Ty::new_adt(tcx, def, args);
1225                let InferOk { obligations, .. } = self
1226                    .infcx
1227                    .at(&obligation.cause, obligation.param_env)
1228                    .eq(DefineOpaqueTypes::Yes, target, new_struct)
1229                    .map_err(|_| SelectionError::Unimplemented)?;
1230                nested.extend(obligations);
1231
1232                // Construct the nested `TailField<T>: Unsize<TailField<U>>` predicate.
1233                let tail_unsize_obligation = obligation.with(
1234                    tcx,
1235                    ty::TraitRef::new(
1236                        tcx,
1237                        obligation.predicate.def_id(),
1238                        [source_tail, target_tail],
1239                    ),
1240                );
1241                nested.push(tail_unsize_obligation);
1242
1243                ImplSource::Builtin(BuiltinImplSource::Misc, nested)
1244            }
1245
1246            _ => bug!("source: {source}, target: {target}"),
1247        })
1248    }
1249
1250    fn confirm_bikeshed_guaranteed_no_drop_candidate(
1251        &mut self,
1252        obligation: &PolyTraitObligation<'tcx>,
1253    ) -> ImplSource<'tcx, PredicateObligation<'tcx>> {
1254        let mut obligations = thin_vec![];
1255
1256        let tcx = self.tcx();
1257        let self_ty = obligation.predicate.self_ty();
1258        match *self_ty.skip_binder().kind() {
1259            // `&mut T` and `&T` always implement `BikeshedGuaranteedNoDrop`.
1260            ty::Ref(..) => {}
1261            // `ManuallyDrop<T>` always implements `BikeshedGuaranteedNoDrop`.
1262            ty::Adt(def, _) if def.is_manually_drop() => {}
1263            // Arrays and tuples implement `BikeshedGuaranteedNoDrop` only if
1264            // their constituent types implement `BikeshedGuaranteedNoDrop`.
1265            ty::Tuple(tys) => {
1266                obligations.extend(tys.iter().map(|elem_ty| {
1267                    obligation.with(
1268                        tcx,
1269                        self_ty.rebind(ty::TraitRef::new(
1270                            tcx,
1271                            obligation.predicate.def_id(),
1272                            [elem_ty],
1273                        )),
1274                    )
1275                }));
1276            }
1277            ty::Array(elem_ty, _) => {
1278                obligations.push(obligation.with(
1279                    tcx,
1280                    self_ty.rebind(ty::TraitRef::new(
1281                        tcx,
1282                        obligation.predicate.def_id(),
1283                        [elem_ty],
1284                    )),
1285                ));
1286            }
1287
1288            // All other types implement `BikeshedGuaranteedNoDrop` only if
1289            // they implement `Copy`. We could be smart here and short-circuit
1290            // some trivially `Copy`/`!Copy` types, but there's no benefit.
1291            ty::FnDef(..)
1292            | ty::FnPtr(..)
1293            | ty::Error(_)
1294            | ty::Uint(_)
1295            | ty::Int(_)
1296            | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
1297            | ty::Bool
1298            | ty::Float(_)
1299            | ty::Char
1300            | ty::RawPtr(..)
1301            | ty::Never
1302            | ty::Pat(..)
1303            | ty::Dynamic(..)
1304            | ty::Str
1305            | ty::Slice(_)
1306            | ty::Foreign(..)
1307            | ty::Adt(..)
1308            | ty::Alias(..)
1309            | ty::Param(_)
1310            | ty::Placeholder(..)
1311            | ty::Closure(..)
1312            | ty::CoroutineClosure(..)
1313            | ty::Coroutine(..)
1314            | ty::UnsafeBinder(_)
1315            | ty::CoroutineWitness(..)
1316            | ty::Bound(..) => {
1317                obligations.push(obligation.with(
1318                    tcx,
1319                    self_ty.map_bound(|ty| {
1320                        ty::TraitRef::new(
1321                            tcx,
1322                            tcx.require_lang_item(LangItem::Copy, obligation.cause.span),
1323                            [ty],
1324                        )
1325                    }),
1326                ));
1327            }
1328
1329            ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
1330                panic!("unexpected type `{self_ty:?}`")
1331            }
1332        }
1333
1334        ImplSource::Builtin(BuiltinImplSource::Misc, obligations)
1335    }
1336}