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