]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_trait_selection/src/traits/select/confirmation.rs
Rollup merge of #105174 - chenyukang:yukang/fix-105028-unused, r=eholk
[rust.git] / compiler / rustc_trait_selection / src / 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 use rustc_data_structures::stack::ensure_sufficient_stack;
10 use rustc_hir::lang_items::LangItem;
11 use rustc_index::bit_set::GrowableBitSet;
12 use rustc_infer::infer::InferOk;
13 use rustc_infer::infer::LateBoundRegionConversionTime::HigherRankedType;
14 use rustc_middle::ty::{
15     self, GenericArg, GenericArgKind, GenericParamDefKind, InternalSubsts, SubstsRef,
16     ToPolyTraitRef, ToPredicate, Ty, TyCtxt,
17 };
18 use rustc_span::def_id::DefId;
19
20 use crate::traits::project::{normalize_with_depth, normalize_with_depth_to};
21 use crate::traits::util::{self, closure_trait_ref_and_return_type, predicate_for_trait_def};
22 use crate::traits::{
23     BuiltinDerivedObligation, ImplDerivedObligation, ImplDerivedObligationCause, ImplSource,
24     ImplSourceAutoImplData, ImplSourceBuiltinData, ImplSourceClosureData,
25     ImplSourceConstDestructData, ImplSourceFnPointerData, ImplSourceFutureData,
26     ImplSourceGeneratorData, ImplSourceObjectData, ImplSourceTraitAliasData,
27     ImplSourceTraitUpcastingData, ImplSourceUserDefinedData, Normalized, ObjectCastObligation,
28     Obligation, ObligationCause, OutputTypeParameterMismatch, PredicateObligation, Selection,
29     SelectionError, TraitNotObjectSafe, TraitObligation, Unimplemented, VtblSegment,
30 };
31
32 use super::BuiltinImplConditions;
33 use super::SelectionCandidate::{self, *};
34 use super::SelectionContext;
35
36 use std::iter;
37 use std::ops::ControlFlow;
38
39 impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
40     #[instrument(level = "debug", skip(self))]
41     pub(super) fn confirm_candidate(
42         &mut self,
43         obligation: &TraitObligation<'tcx>,
44         candidate: SelectionCandidate<'tcx>,
45     ) -> Result<Selection<'tcx>, SelectionError<'tcx>> {
46         let mut impl_src = match candidate {
47             BuiltinCandidate { has_nested } => {
48                 let data = self.confirm_builtin_candidate(obligation, has_nested);
49                 ImplSource::Builtin(data)
50             }
51
52             TransmutabilityCandidate => {
53                 let data = self.confirm_transmutability_candidate(obligation)?;
54                 ImplSource::Builtin(data)
55             }
56
57             ParamCandidate(param) => {
58                 let obligations =
59                     self.confirm_param_candidate(obligation, param.map_bound(|t| t.trait_ref));
60                 ImplSource::Param(obligations, param.skip_binder().constness)
61             }
62
63             ImplCandidate(impl_def_id) => {
64                 ImplSource::UserDefined(self.confirm_impl_candidate(obligation, impl_def_id))
65             }
66
67             AutoImplCandidate => {
68                 let data = self.confirm_auto_impl_candidate(obligation);
69                 ImplSource::AutoImpl(data)
70             }
71
72             ProjectionCandidate(idx, constness) => {
73                 let obligations = self.confirm_projection_candidate(obligation, idx)?;
74                 ImplSource::Param(obligations, constness)
75             }
76
77             ObjectCandidate(idx) => {
78                 let data = self.confirm_object_candidate(obligation, idx)?;
79                 ImplSource::Object(data)
80             }
81
82             ClosureCandidate => {
83                 let vtable_closure = self.confirm_closure_candidate(obligation)?;
84                 ImplSource::Closure(vtable_closure)
85             }
86
87             GeneratorCandidate => {
88                 let vtable_generator = self.confirm_generator_candidate(obligation)?;
89                 ImplSource::Generator(vtable_generator)
90             }
91
92             FutureCandidate => {
93                 let vtable_future = self.confirm_future_candidate(obligation)?;
94                 ImplSource::Future(vtable_future)
95             }
96
97             FnPointerCandidate { .. } => {
98                 let data = self.confirm_fn_pointer_candidate(obligation)?;
99                 ImplSource::FnPointer(data)
100             }
101
102             TraitAliasCandidate => {
103                 let data = self.confirm_trait_alias_candidate(obligation);
104                 ImplSource::TraitAlias(data)
105             }
106
107             BuiltinObjectCandidate => {
108                 // This indicates something like `Trait + Send: Send`. In this case, we know that
109                 // this holds because that's what the object type is telling us, and there's really
110                 // no additional obligations to prove and no types in particular to unify, etc.
111                 ImplSource::Param(Vec::new(), ty::BoundConstness::NotConst)
112             }
113
114             BuiltinUnsizeCandidate => {
115                 let data = self.confirm_builtin_unsize_candidate(obligation)?;
116                 ImplSource::Builtin(data)
117             }
118
119             TraitUpcastingUnsizeCandidate(idx) => {
120                 let data = self.confirm_trait_upcasting_unsize_candidate(obligation, idx)?;
121                 ImplSource::TraitUpcasting(data)
122             }
123
124             ConstDestructCandidate(def_id) => {
125                 let data = self.confirm_const_destruct_candidate(obligation, def_id)?;
126                 ImplSource::ConstDestruct(data)
127             }
128         };
129
130         if !obligation.predicate.is_const_if_const() {
131             // normalize nested predicates according to parent predicate's constness.
132             impl_src = impl_src.map(|mut o| {
133                 o.predicate = o.predicate.without_const(self.tcx());
134                 o
135             });
136         }
137
138         Ok(impl_src)
139     }
140
141     fn confirm_projection_candidate(
142         &mut self,
143         obligation: &TraitObligation<'tcx>,
144         idx: usize,
145     ) -> Result<Vec<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
146         let tcx = self.tcx();
147
148         let trait_predicate = self.infcx.shallow_resolve(obligation.predicate);
149         let placeholder_trait_predicate =
150             self.infcx.replace_bound_vars_with_placeholders(trait_predicate).trait_ref;
151         let placeholder_self_ty = placeholder_trait_predicate.self_ty();
152         let placeholder_trait_predicate = ty::Binder::dummy(placeholder_trait_predicate);
153         let (def_id, substs) = match *placeholder_self_ty.kind() {
154             ty::Projection(proj) => (proj.item_def_id, proj.substs),
155             ty::Opaque(def_id, substs) => (def_id, substs),
156             _ => bug!("projection candidate for unexpected type: {:?}", placeholder_self_ty),
157         };
158
159         let candidate_predicate =
160             tcx.bound_item_bounds(def_id).map_bound(|i| i[idx]).subst(tcx, substs);
161         let candidate = candidate_predicate
162             .to_opt_poly_trait_pred()
163             .expect("projection candidate is not a trait predicate")
164             .map_bound(|t| t.trait_ref);
165         let mut obligations = Vec::new();
166         let candidate = normalize_with_depth_to(
167             self,
168             obligation.param_env,
169             obligation.cause.clone(),
170             obligation.recursion_depth + 1,
171             candidate,
172             &mut obligations,
173         );
174
175         obligations.extend(self.infcx.commit_if_ok(|_| {
176             self.infcx
177                 .at(&obligation.cause, obligation.param_env)
178                 .sup(placeholder_trait_predicate, candidate)
179                 .map(|InferOk { obligations, .. }| obligations)
180                 .map_err(|_| Unimplemented)
181         })?);
182
183         if let ty::Projection(..) = placeholder_self_ty.kind() {
184             let predicates = tcx.predicates_of(def_id).instantiate_own(tcx, substs).predicates;
185             debug!(?predicates, "projection predicates");
186             for predicate in predicates {
187                 let normalized = normalize_with_depth_to(
188                     self,
189                     obligation.param_env,
190                     obligation.cause.clone(),
191                     obligation.recursion_depth + 1,
192                     predicate,
193                     &mut obligations,
194                 );
195                 obligations.push(Obligation::with_depth(
196                     self.tcx(),
197                     obligation.cause.clone(),
198                     obligation.recursion_depth + 1,
199                     obligation.param_env,
200                     normalized,
201                 ));
202             }
203         }
204
205         Ok(obligations)
206     }
207
208     fn confirm_param_candidate(
209         &mut self,
210         obligation: &TraitObligation<'tcx>,
211         param: ty::PolyTraitRef<'tcx>,
212     ) -> Vec<PredicateObligation<'tcx>> {
213         debug!(?obligation, ?param, "confirm_param_candidate");
214
215         // During evaluation, we already checked that this
216         // where-clause trait-ref could be unified with the obligation
217         // trait-ref. Repeat that unification now without any
218         // transactional boundary; it should not fail.
219         match self.match_where_clause_trait_ref(obligation, param) {
220             Ok(obligations) => obligations,
221             Err(()) => {
222                 bug!(
223                     "Where clause `{:?}` was applicable to `{:?}` but now is not",
224                     param,
225                     obligation
226                 );
227             }
228         }
229     }
230
231     fn confirm_builtin_candidate(
232         &mut self,
233         obligation: &TraitObligation<'tcx>,
234         has_nested: bool,
235     ) -> ImplSourceBuiltinData<PredicateObligation<'tcx>> {
236         debug!(?obligation, ?has_nested, "confirm_builtin_candidate");
237
238         let lang_items = self.tcx().lang_items();
239         let obligations = if has_nested {
240             let trait_def = obligation.predicate.def_id();
241             let conditions = if Some(trait_def) == lang_items.sized_trait() {
242                 self.sized_conditions(obligation)
243             } else if Some(trait_def) == lang_items.copy_trait() {
244                 self.copy_clone_conditions(obligation)
245             } else if Some(trait_def) == lang_items.clone_trait() {
246                 self.copy_clone_conditions(obligation)
247             } else {
248                 bug!("unexpected builtin trait {:?}", trait_def)
249             };
250             let BuiltinImplConditions::Where(nested) = conditions else {
251                 bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation);
252             };
253
254             let cause = obligation.derived_cause(BuiltinDerivedObligation);
255             ensure_sufficient_stack(|| {
256                 self.collect_predicates_for_types(
257                     obligation.param_env,
258                     cause,
259                     obligation.recursion_depth + 1,
260                     trait_def,
261                     nested,
262                 )
263             })
264         } else {
265             vec![]
266         };
267
268         debug!(?obligations);
269
270         ImplSourceBuiltinData { nested: obligations }
271     }
272
273     fn confirm_transmutability_candidate(
274         &mut self,
275         obligation: &TraitObligation<'tcx>,
276     ) -> Result<ImplSourceBuiltinData<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
277         debug!(?obligation, "confirm_transmutability_candidate");
278
279         let predicate = obligation.predicate;
280
281         let type_at = |i| predicate.map_bound(|p| p.trait_ref.substs.type_at(i));
282         let const_at = |i| predicate.skip_binder().trait_ref.substs.const_at(i);
283
284         let src_and_dst = predicate.map_bound(|p| rustc_transmute::Types {
285             dst: p.trait_ref.substs.type_at(0),
286             src: p.trait_ref.substs.type_at(1),
287         });
288
289         let scope = type_at(2).skip_binder();
290
291         let Some(assume) =
292             rustc_transmute::Assume::from_const(self.infcx.tcx, obligation.param_env, const_at(3)) else {
293                 return Err(Unimplemented);
294             };
295
296         let cause = obligation.cause.clone();
297
298         let mut transmute_env = rustc_transmute::TransmuteTypeEnv::new(self.infcx);
299
300         let maybe_transmutable = transmute_env.is_transmutable(cause, src_and_dst, scope, assume);
301
302         use rustc_transmute::Answer;
303
304         match maybe_transmutable {
305             Answer::Yes => Ok(ImplSourceBuiltinData { nested: vec![] }),
306             _ => Err(Unimplemented),
307         }
308     }
309
310     /// This handles the case where an `auto trait Foo` impl is being used.
311     /// The idea is that the impl applies to `X : Foo` if the following conditions are met:
312     ///
313     /// 1. For each constituent type `Y` in `X`, `Y : Foo` holds
314     /// 2. For each where-clause `C` declared on `Foo`, `[Self => X] C` holds.
315     fn confirm_auto_impl_candidate(
316         &mut self,
317         obligation: &TraitObligation<'tcx>,
318     ) -> ImplSourceAutoImplData<PredicateObligation<'tcx>> {
319         debug!(?obligation, "confirm_auto_impl_candidate");
320
321         let self_ty = self.infcx.shallow_resolve(obligation.predicate.self_ty());
322         let types = self.constituent_types_for_ty(self_ty);
323         self.vtable_auto_impl(obligation, obligation.predicate.def_id(), types)
324     }
325
326     /// See `confirm_auto_impl_candidate`.
327     fn vtable_auto_impl(
328         &mut self,
329         obligation: &TraitObligation<'tcx>,
330         trait_def_id: DefId,
331         nested: ty::Binder<'tcx, Vec<Ty<'tcx>>>,
332     ) -> ImplSourceAutoImplData<PredicateObligation<'tcx>> {
333         debug!(?nested, "vtable_auto_impl");
334         ensure_sufficient_stack(|| {
335             let cause = obligation.derived_cause(BuiltinDerivedObligation);
336
337             let poly_trait_ref = obligation.predicate.to_poly_trait_ref();
338             let trait_ref = self.infcx.replace_bound_vars_with_placeholders(poly_trait_ref);
339             let trait_obligations: Vec<PredicateObligation<'_>> = self.impl_or_trait_obligations(
340                 &cause,
341                 obligation.recursion_depth + 1,
342                 obligation.param_env,
343                 trait_def_id,
344                 &trait_ref.substs,
345                 obligation.predicate,
346             );
347
348             let mut obligations = self.collect_predicates_for_types(
349                 obligation.param_env,
350                 cause,
351                 obligation.recursion_depth + 1,
352                 trait_def_id,
353                 nested,
354             );
355
356             // Adds the predicates from the trait.  Note that this contains a `Self: Trait`
357             // predicate as usual.  It won't have any effect since auto traits are coinductive.
358             obligations.extend(trait_obligations);
359
360             debug!(?obligations, "vtable_auto_impl");
361
362             ImplSourceAutoImplData { trait_def_id, nested: obligations }
363         })
364     }
365
366     fn confirm_impl_candidate(
367         &mut self,
368         obligation: &TraitObligation<'tcx>,
369         impl_def_id: DefId,
370     ) -> ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>> {
371         debug!(?obligation, ?impl_def_id, "confirm_impl_candidate");
372
373         // First, create the substitutions by matching the impl again,
374         // this time not in a probe.
375         let substs = self.rematch_impl(impl_def_id, obligation);
376         debug!(?substs, "impl substs");
377         ensure_sufficient_stack(|| {
378             self.vtable_impl(
379                 impl_def_id,
380                 substs,
381                 &obligation.cause,
382                 obligation.recursion_depth + 1,
383                 obligation.param_env,
384                 obligation.predicate,
385             )
386         })
387     }
388
389     fn vtable_impl(
390         &mut self,
391         impl_def_id: DefId,
392         substs: Normalized<'tcx, SubstsRef<'tcx>>,
393         cause: &ObligationCause<'tcx>,
394         recursion_depth: usize,
395         param_env: ty::ParamEnv<'tcx>,
396         parent_trait_pred: ty::Binder<'tcx, ty::TraitPredicate<'tcx>>,
397     ) -> ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>> {
398         debug!(?impl_def_id, ?substs, ?recursion_depth, "vtable_impl");
399
400         let mut impl_obligations = self.impl_or_trait_obligations(
401             cause,
402             recursion_depth,
403             param_env,
404             impl_def_id,
405             &substs.value,
406             parent_trait_pred,
407         );
408
409         debug!(?impl_obligations, "vtable_impl");
410
411         // Because of RFC447, the impl-trait-ref and obligations
412         // are sufficient to determine the impl substs, without
413         // relying on projections in the impl-trait-ref.
414         //
415         // e.g., `impl<U: Tr, V: Iterator<Item=U>> Foo<<U as Tr>::T> for V`
416         impl_obligations.extend(substs.obligations);
417
418         ImplSourceUserDefinedData { impl_def_id, substs: substs.value, nested: impl_obligations }
419     }
420
421     fn confirm_object_candidate(
422         &mut self,
423         obligation: &TraitObligation<'tcx>,
424         index: usize,
425     ) -> Result<ImplSourceObjectData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
426         let tcx = self.tcx();
427         debug!(?obligation, ?index, "confirm_object_candidate");
428
429         let trait_predicate = self.infcx.replace_bound_vars_with_placeholders(obligation.predicate);
430         let self_ty = self.infcx.shallow_resolve(trait_predicate.self_ty());
431         let obligation_trait_ref = ty::Binder::dummy(trait_predicate.trait_ref);
432         let ty::Dynamic(data, ..) = *self_ty.kind() else {
433             span_bug!(obligation.cause.span, "object candidate with non-object");
434         };
435
436         let object_trait_ref = data.principal().unwrap_or_else(|| {
437             span_bug!(obligation.cause.span, "object candidate with no principal")
438         });
439         let object_trait_ref = self.infcx.replace_bound_vars_with_fresh_vars(
440             obligation.cause.span,
441             HigherRankedType,
442             object_trait_ref,
443         );
444         let object_trait_ref = object_trait_ref.with_self_ty(self.tcx(), self_ty);
445
446         let mut nested = vec![];
447
448         let mut supertraits = util::supertraits(tcx, ty::Binder::dummy(object_trait_ref));
449         let unnormalized_upcast_trait_ref =
450             supertraits.nth(index).expect("supertraits iterator no longer has as many elements");
451
452         let upcast_trait_ref = normalize_with_depth_to(
453             self,
454             obligation.param_env,
455             obligation.cause.clone(),
456             obligation.recursion_depth + 1,
457             unnormalized_upcast_trait_ref,
458             &mut nested,
459         );
460
461         nested.extend(self.infcx.commit_if_ok(|_| {
462             self.infcx
463                 .at(&obligation.cause, obligation.param_env)
464                 .sup(obligation_trait_ref, upcast_trait_ref)
465                 .map(|InferOk { obligations, .. }| obligations)
466                 .map_err(|_| Unimplemented)
467         })?);
468
469         // Check supertraits hold. This is so that their associated type bounds
470         // will be checked in the code below.
471         for super_trait in tcx
472             .super_predicates_of(trait_predicate.def_id())
473             .instantiate(tcx, trait_predicate.trait_ref.substs)
474             .predicates
475             .into_iter()
476         {
477             let normalized_super_trait = normalize_with_depth_to(
478                 self,
479                 obligation.param_env,
480                 obligation.cause.clone(),
481                 obligation.recursion_depth + 1,
482                 super_trait,
483                 &mut nested,
484             );
485             nested.push(obligation.with(tcx, normalized_super_trait));
486         }
487
488         let assoc_types: Vec<_> = tcx
489             .associated_items(trait_predicate.def_id())
490             .in_definition_order()
491             .filter_map(
492                 |item| if item.kind == ty::AssocKind::Type { Some(item.def_id) } else { None },
493             )
494             .collect();
495
496         for assoc_type in assoc_types {
497             let defs: &ty::Generics = tcx.generics_of(assoc_type);
498
499             if !defs.params.is_empty() && !tcx.features().generic_associated_types_extended {
500                 tcx.sess.delay_span_bug(
501                     obligation.cause.span,
502                     "GATs in trait object shouldn't have been considered",
503                 );
504                 return Err(SelectionError::Unimplemented);
505             }
506
507             // This maybe belongs in wf, but that can't (doesn't) handle
508             // higher-ranked things.
509             // Prevent, e.g., `dyn Iterator<Item = str>`.
510             for bound in self.tcx().bound_item_bounds(assoc_type).transpose_iter() {
511                 let subst_bound =
512                     if defs.count() == 0 {
513                         bound.subst(tcx, trait_predicate.trait_ref.substs)
514                     } else {
515                         let mut substs = smallvec::SmallVec::with_capacity(defs.count());
516                         substs.extend(trait_predicate.trait_ref.substs.iter());
517                         let mut bound_vars: smallvec::SmallVec<[ty::BoundVariableKind; 8]> =
518                             smallvec::SmallVec::with_capacity(
519                                 bound.0.kind().bound_vars().len() + defs.count(),
520                             );
521                         bound_vars.extend(bound.0.kind().bound_vars().into_iter());
522                         InternalSubsts::fill_single(&mut substs, defs, &mut |param, _| match param
523                             .kind
524                         {
525                             GenericParamDefKind::Type { .. } => {
526                                 let kind = ty::BoundTyKind::Param(param.name);
527                                 let bound_var = ty::BoundVariableKind::Ty(kind);
528                                 bound_vars.push(bound_var);
529                                 tcx.mk_ty(ty::Bound(
530                                     ty::INNERMOST,
531                                     ty::BoundTy {
532                                         var: ty::BoundVar::from_usize(bound_vars.len() - 1),
533                                         kind,
534                                     },
535                                 ))
536                                 .into()
537                             }
538                             GenericParamDefKind::Lifetime => {
539                                 let kind = ty::BoundRegionKind::BrNamed(param.def_id, param.name);
540                                 let bound_var = ty::BoundVariableKind::Region(kind);
541                                 bound_vars.push(bound_var);
542                                 tcx.mk_region(ty::ReLateBound(
543                                     ty::INNERMOST,
544                                     ty::BoundRegion {
545                                         var: ty::BoundVar::from_usize(bound_vars.len() - 1),
546                                         kind,
547                                     },
548                                 ))
549                                 .into()
550                             }
551                             GenericParamDefKind::Const { .. } => {
552                                 let bound_var = ty::BoundVariableKind::Const;
553                                 bound_vars.push(bound_var);
554                                 tcx.mk_const(
555                                     ty::ConstKind::Bound(
556                                         ty::INNERMOST,
557                                         ty::BoundVar::from_usize(bound_vars.len() - 1),
558                                     ),
559                                     tcx.type_of(param.def_id),
560                                 )
561                                 .into()
562                             }
563                         });
564                         let bound_vars = tcx.mk_bound_variable_kinds(bound_vars.into_iter());
565                         let assoc_ty_substs = tcx.intern_substs(&substs);
566
567                         let bound_vars = tcx.mk_bound_variable_kinds(bound_vars.into_iter());
568                         let bound =
569                             bound.map_bound(|b| b.kind().skip_binder()).subst(tcx, assoc_ty_substs);
570                         tcx.mk_predicate(ty::Binder::bind_with_vars(bound, bound_vars))
571                     };
572                 let normalized_bound = normalize_with_depth_to(
573                     self,
574                     obligation.param_env,
575                     obligation.cause.clone(),
576                     obligation.recursion_depth + 1,
577                     subst_bound,
578                     &mut nested,
579                 );
580                 nested.push(obligation.with(tcx, normalized_bound));
581             }
582         }
583
584         debug!(?nested, "object nested obligations");
585
586         let vtable_base = super::super::vtable_trait_first_method_offset(
587             tcx,
588             (unnormalized_upcast_trait_ref, ty::Binder::dummy(object_trait_ref)),
589         );
590
591         Ok(ImplSourceObjectData { upcast_trait_ref, vtable_base, nested })
592     }
593
594     fn confirm_fn_pointer_candidate(
595         &mut self,
596         obligation: &TraitObligation<'tcx>,
597     ) -> Result<ImplSourceFnPointerData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>>
598     {
599         debug!(?obligation, "confirm_fn_pointer_candidate");
600
601         let self_ty = self
602             .infcx
603             .shallow_resolve(obligation.self_ty().no_bound_vars())
604             .expect("fn pointer should not capture bound vars from predicate");
605         let sig = self_ty.fn_sig(self.tcx());
606         let trait_ref = closure_trait_ref_and_return_type(
607             self.tcx(),
608             obligation.predicate.def_id(),
609             self_ty,
610             sig,
611             util::TupleArgumentsFlag::Yes,
612         )
613         .map_bound(|(trait_ref, _)| trait_ref);
614
615         let mut nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
616
617         // Confirm the `type Output: Sized;` bound that is present on `FnOnce`
618         let cause = obligation.derived_cause(BuiltinDerivedObligation);
619         let output_ty = self.infcx.replace_bound_vars_with_placeholders(sig.output());
620         let output_ty = normalize_with_depth_to(
621             self,
622             obligation.param_env,
623             cause.clone(),
624             obligation.recursion_depth,
625             output_ty,
626             &mut nested,
627         );
628         let tr =
629             ty::Binder::dummy(self.tcx().at(cause.span).mk_trait_ref(LangItem::Sized, [output_ty]));
630         nested.push(Obligation::new(self.infcx.tcx, cause, obligation.param_env, tr));
631
632         Ok(ImplSourceFnPointerData { fn_ty: self_ty, nested })
633     }
634
635     fn confirm_trait_alias_candidate(
636         &mut self,
637         obligation: &TraitObligation<'tcx>,
638     ) -> ImplSourceTraitAliasData<'tcx, PredicateObligation<'tcx>> {
639         debug!(?obligation, "confirm_trait_alias_candidate");
640
641         let alias_def_id = obligation.predicate.def_id();
642         let predicate = self.infcx.replace_bound_vars_with_placeholders(obligation.predicate);
643         let trait_ref = predicate.trait_ref;
644         let trait_def_id = trait_ref.def_id;
645         let substs = trait_ref.substs;
646
647         let trait_obligations = self.impl_or_trait_obligations(
648             &obligation.cause,
649             obligation.recursion_depth,
650             obligation.param_env,
651             trait_def_id,
652             &substs,
653             obligation.predicate,
654         );
655
656         debug!(?trait_def_id, ?trait_obligations, "trait alias obligations");
657
658         ImplSourceTraitAliasData { alias_def_id, substs, nested: trait_obligations }
659     }
660
661     fn confirm_generator_candidate(
662         &mut self,
663         obligation: &TraitObligation<'tcx>,
664     ) -> Result<ImplSourceGeneratorData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>>
665     {
666         // Okay to skip binder because the substs on generator types never
667         // touch bound regions, they just capture the in-scope
668         // type/region parameters.
669         let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
670         let ty::Generator(generator_def_id, substs, _) = *self_ty.kind() else {
671             bug!("closure candidate for non-closure {:?}", obligation);
672         };
673
674         debug!(?obligation, ?generator_def_id, ?substs, "confirm_generator_candidate");
675
676         let gen_sig = substs.as_generator().poly_sig();
677
678         // NOTE: The self-type is a generator type and hence is
679         // in fact unparameterized (or at least does not reference any
680         // regions bound in the obligation).
681         let self_ty = obligation
682             .predicate
683             .self_ty()
684             .no_bound_vars()
685             .expect("unboxed closure type should not capture bound vars from the predicate");
686
687         let trait_ref = super::util::generator_trait_ref_and_outputs(
688             self.tcx(),
689             obligation.predicate.def_id(),
690             self_ty,
691             gen_sig,
692         )
693         .map_bound(|(trait_ref, ..)| trait_ref);
694
695         let nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
696         debug!(?trait_ref, ?nested, "generator candidate obligations");
697
698         Ok(ImplSourceGeneratorData { generator_def_id, substs, nested })
699     }
700
701     fn confirm_future_candidate(
702         &mut self,
703         obligation: &TraitObligation<'tcx>,
704     ) -> Result<ImplSourceFutureData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
705         // Okay to skip binder because the substs on generator types never
706         // touch bound regions, they just capture the in-scope
707         // type/region parameters.
708         let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
709         let ty::Generator(generator_def_id, substs, _) = *self_ty.kind() else {
710             bug!("closure candidate for non-closure {:?}", obligation);
711         };
712
713         debug!(?obligation, ?generator_def_id, ?substs, "confirm_future_candidate");
714
715         let gen_sig = substs.as_generator().poly_sig();
716
717         let trait_ref = super::util::future_trait_ref_and_outputs(
718             self.tcx(),
719             obligation.predicate.def_id(),
720             obligation.predicate.no_bound_vars().expect("future has no bound vars").self_ty(),
721             gen_sig,
722         )
723         .map_bound(|(trait_ref, ..)| trait_ref);
724
725         let nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
726         debug!(?trait_ref, ?nested, "future candidate obligations");
727
728         Ok(ImplSourceFutureData { generator_def_id, substs, nested })
729     }
730
731     #[instrument(skip(self), level = "debug")]
732     fn confirm_closure_candidate(
733         &mut self,
734         obligation: &TraitObligation<'tcx>,
735     ) -> Result<ImplSourceClosureData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
736         let kind = self
737             .tcx()
738             .fn_trait_kind_from_def_id(obligation.predicate.def_id())
739             .unwrap_or_else(|| bug!("closure candidate for non-fn trait {:?}", obligation));
740
741         // Okay to skip binder because the substs on closure types never
742         // touch bound regions, they just capture the in-scope
743         // type/region parameters.
744         let self_ty = self.infcx.shallow_resolve(obligation.self_ty().skip_binder());
745         let ty::Closure(closure_def_id, substs) = *self_ty.kind() else {
746             bug!("closure candidate for non-closure {:?}", obligation);
747         };
748
749         let trait_ref = self.closure_trait_ref_unnormalized(obligation, substs);
750         let mut nested = self.confirm_poly_trait_refs(obligation, trait_ref)?;
751
752         debug!(?closure_def_id, ?trait_ref, ?nested, "confirm closure candidate obligations");
753
754         // FIXME: Chalk
755
756         if !self.tcx().sess.opts.unstable_opts.chalk {
757             nested.push(obligation.with(
758                 self.tcx(),
759                 ty::Binder::dummy(ty::PredicateKind::ClosureKind(closure_def_id, substs, kind)),
760             ));
761         }
762
763         Ok(ImplSourceClosureData { closure_def_id, substs, nested })
764     }
765
766     /// In the case of closure types and fn pointers,
767     /// we currently treat the input type parameters on the trait as
768     /// outputs. This means that when we have a match we have only
769     /// considered the self type, so we have to go back and make sure
770     /// to relate the argument types too. This is kind of wrong, but
771     /// since we control the full set of impls, also not that wrong,
772     /// and it DOES yield better error messages (since we don't report
773     /// errors as if there is no applicable impl, but rather report
774     /// errors are about mismatched argument types.
775     ///
776     /// Here is an example. Imagine we have a closure expression
777     /// and we desugared it so that the type of the expression is
778     /// `Closure`, and `Closure` expects `i32` as argument. Then it
779     /// is "as if" the compiler generated this impl:
780     /// ```ignore (illustrative)
781     /// impl Fn(i32) for Closure { ... }
782     /// ```
783     /// Now imagine our obligation is `Closure: Fn(usize)`. So far
784     /// we have matched the self type `Closure`. At this point we'll
785     /// compare the `i32` to `usize` and generate an error.
786     ///
787     /// Note that this checking occurs *after* the impl has selected,
788     /// because these output type parameters should not affect the
789     /// selection of the impl. Therefore, if there is a mismatch, we
790     /// report an error to the user.
791     #[instrument(skip(self), level = "trace")]
792     fn confirm_poly_trait_refs(
793         &mut self,
794         obligation: &TraitObligation<'tcx>,
795         expected_trait_ref: ty::PolyTraitRef<'tcx>,
796     ) -> Result<Vec<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
797         let obligation_trait_ref = obligation.predicate.to_poly_trait_ref();
798         // Normalize the obligation and expected trait refs together, because why not
799         let Normalized { obligations: nested, value: (obligation_trait_ref, expected_trait_ref) } =
800             ensure_sufficient_stack(|| {
801                 normalize_with_depth(
802                     self,
803                     obligation.param_env,
804                     obligation.cause.clone(),
805                     obligation.recursion_depth + 1,
806                     (obligation_trait_ref, expected_trait_ref),
807                 )
808             });
809
810         self.infcx
811             .at(&obligation.cause, obligation.param_env)
812             .sup(obligation_trait_ref, expected_trait_ref)
813             .map(|InferOk { mut obligations, .. }| {
814                 obligations.extend(nested);
815                 obligations
816             })
817             .map_err(|e| OutputTypeParameterMismatch(expected_trait_ref, obligation_trait_ref, e))
818     }
819
820     fn confirm_trait_upcasting_unsize_candidate(
821         &mut self,
822         obligation: &TraitObligation<'tcx>,
823         idx: usize,
824     ) -> Result<ImplSourceTraitUpcastingData<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>>
825     {
826         let tcx = self.tcx();
827
828         // `assemble_candidates_for_unsizing` should ensure there are no late-bound
829         // regions here. See the comment there for more details.
830         let source = self.infcx.shallow_resolve(obligation.self_ty().no_bound_vars().unwrap());
831         let target = obligation.predicate.skip_binder().trait_ref.substs.type_at(1);
832         let target = self.infcx.shallow_resolve(target);
833
834         debug!(?source, ?target, "confirm_trait_upcasting_unsize_candidate");
835
836         let mut nested = vec![];
837         let source_trait_ref;
838         let upcast_trait_ref;
839         match (source.kind(), target.kind()) {
840             // TraitA+Kx+'a -> TraitB+Ky+'b (trait upcasting coercion).
841             (
842                 &ty::Dynamic(ref data_a, r_a, repr_a @ ty::Dyn),
843                 &ty::Dynamic(ref data_b, r_b, ty::Dyn),
844             ) => {
845                 // See `assemble_candidates_for_unsizing` for more info.
846                 // We already checked the compatibility of auto traits within `assemble_candidates_for_unsizing`.
847                 let principal_a = data_a.principal().unwrap();
848                 source_trait_ref = principal_a.with_self_ty(tcx, source);
849                 upcast_trait_ref = util::supertraits(tcx, source_trait_ref).nth(idx).unwrap();
850                 assert_eq!(data_b.principal_def_id(), Some(upcast_trait_ref.def_id()));
851                 let existential_predicate = upcast_trait_ref.map_bound(|trait_ref| {
852                     ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef::erase_self_ty(
853                         tcx, trait_ref,
854                     ))
855                 });
856                 let iter = Some(existential_predicate)
857                     .into_iter()
858                     .chain(
859                         data_a
860                             .projection_bounds()
861                             .map(|b| b.map_bound(ty::ExistentialPredicate::Projection)),
862                     )
863                     .chain(
864                         data_b
865                             .auto_traits()
866                             .map(ty::ExistentialPredicate::AutoTrait)
867                             .map(ty::Binder::dummy),
868                     );
869                 let existential_predicates = tcx.mk_poly_existential_predicates(iter);
870                 let source_trait = tcx.mk_dynamic(existential_predicates, r_b, repr_a);
871
872                 // Require that the traits involved in this upcast are **equal**;
873                 // only the **lifetime bound** is changed.
874                 let InferOk { obligations, .. } = self
875                     .infcx
876                     .at(&obligation.cause, obligation.param_env)
877                     .sup(target, source_trait)
878                     .map_err(|_| Unimplemented)?;
879                 nested.extend(obligations);
880
881                 // Register one obligation for 'a: 'b.
882                 let cause = ObligationCause::new(
883                     obligation.cause.span,
884                     obligation.cause.body_id,
885                     ObjectCastObligation(source, target),
886                 );
887                 let outlives = ty::OutlivesPredicate(r_a, r_b);
888                 nested.push(Obligation::with_depth(
889                     tcx,
890                     cause,
891                     obligation.recursion_depth + 1,
892                     obligation.param_env,
893                     obligation.predicate.rebind(outlives),
894                 ));
895             }
896             _ => bug!(),
897         };
898
899         let vtable_segment_callback = {
900             let mut vptr_offset = 0;
901             move |segment| {
902                 match segment {
903                     VtblSegment::MetadataDSA => {
904                         vptr_offset += TyCtxt::COMMON_VTABLE_ENTRIES.len();
905                     }
906                     VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
907                         vptr_offset += util::count_own_vtable_entries(tcx, trait_ref);
908                         if trait_ref == upcast_trait_ref {
909                             if emit_vptr {
910                                 return ControlFlow::Break(Some(vptr_offset));
911                             } else {
912                                 return ControlFlow::Break(None);
913                             }
914                         }
915
916                         if emit_vptr {
917                             vptr_offset += 1;
918                         }
919                     }
920                 }
921                 ControlFlow::Continue(())
922             }
923         };
924
925         let vtable_vptr_slot =
926             super::super::prepare_vtable_segments(tcx, source_trait_ref, vtable_segment_callback)
927                 .unwrap();
928
929         Ok(ImplSourceTraitUpcastingData { upcast_trait_ref, vtable_vptr_slot, nested })
930     }
931
932     fn confirm_builtin_unsize_candidate(
933         &mut self,
934         obligation: &TraitObligation<'tcx>,
935     ) -> Result<ImplSourceBuiltinData<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
936         let tcx = self.tcx();
937
938         // `assemble_candidates_for_unsizing` should ensure there are no late-bound
939         // regions here. See the comment there for more details.
940         let source = self.infcx.shallow_resolve(obligation.self_ty().no_bound_vars().unwrap());
941         let target = obligation.predicate.skip_binder().trait_ref.substs.type_at(1);
942         let target = self.infcx.shallow_resolve(target);
943
944         debug!(?source, ?target, "confirm_builtin_unsize_candidate");
945
946         let mut nested = vec![];
947         match (source.kind(), target.kind()) {
948             // Trait+Kx+'a -> Trait+Ky+'b (auto traits and lifetime subtyping).
949             (&ty::Dynamic(ref data_a, r_a, dyn_a), &ty::Dynamic(ref data_b, r_b, dyn_b))
950                 if dyn_a == dyn_b =>
951             {
952                 // See `assemble_candidates_for_unsizing` for more info.
953                 // We already checked the compatibility of auto traits within `assemble_candidates_for_unsizing`.
954                 let iter = data_a
955                     .principal()
956                     .map(|b| b.map_bound(ty::ExistentialPredicate::Trait))
957                     .into_iter()
958                     .chain(
959                         data_a
960                             .projection_bounds()
961                             .map(|b| b.map_bound(ty::ExistentialPredicate::Projection)),
962                     )
963                     .chain(
964                         data_b
965                             .auto_traits()
966                             .map(ty::ExistentialPredicate::AutoTrait)
967                             .map(ty::Binder::dummy),
968                     );
969                 let existential_predicates = tcx.mk_poly_existential_predicates(iter);
970                 let source_trait = tcx.mk_dynamic(existential_predicates, r_b, dyn_a);
971
972                 // Require that the traits involved in this upcast are **equal**;
973                 // only the **lifetime bound** is changed.
974                 let InferOk { obligations, .. } = self
975                     .infcx
976                     .at(&obligation.cause, obligation.param_env)
977                     .sup(target, source_trait)
978                     .map_err(|_| Unimplemented)?;
979                 nested.extend(obligations);
980
981                 // Register one obligation for 'a: 'b.
982                 let cause = ObligationCause::new(
983                     obligation.cause.span,
984                     obligation.cause.body_id,
985                     ObjectCastObligation(source, target),
986                 );
987                 let outlives = ty::OutlivesPredicate(r_a, r_b);
988                 nested.push(Obligation::with_depth(
989                     tcx,
990                     cause,
991                     obligation.recursion_depth + 1,
992                     obligation.param_env,
993                     obligation.predicate.rebind(outlives),
994                 ));
995             }
996
997             // `T` -> `Trait`
998             (_, &ty::Dynamic(ref data, r, ty::Dyn)) => {
999                 let mut object_dids = data.auto_traits().chain(data.principal_def_id());
1000                 if let Some(did) = object_dids.find(|did| !tcx.is_object_safe(*did)) {
1001                     return Err(TraitNotObjectSafe(did));
1002                 }
1003
1004                 let cause = ObligationCause::new(
1005                     obligation.cause.span,
1006                     obligation.cause.body_id,
1007                     ObjectCastObligation(source, target),
1008                 );
1009
1010                 let predicate_to_obligation = |predicate| {
1011                     Obligation::with_depth(
1012                         tcx,
1013                         cause.clone(),
1014                         obligation.recursion_depth + 1,
1015                         obligation.param_env,
1016                         predicate,
1017                     )
1018                 };
1019
1020                 // Create obligations:
1021                 //  - Casting `T` to `Trait`
1022                 //  - For all the various builtin bounds attached to the object cast. (In other
1023                 //  words, if the object type is `Foo + Send`, this would create an obligation for
1024                 //  the `Send` check.)
1025                 //  - Projection predicates
1026                 nested.extend(
1027                     data.iter().map(|predicate| {
1028                         predicate_to_obligation(predicate.with_self_ty(tcx, source))
1029                     }),
1030                 );
1031
1032                 // We can only make objects from sized types.
1033                 let tr =
1034                     ty::Binder::dummy(tcx.at(cause.span).mk_trait_ref(LangItem::Sized, [source]));
1035                 nested.push(predicate_to_obligation(tr.without_const().to_predicate(tcx)));
1036
1037                 // If the type is `Foo + 'a`, ensure that the type
1038                 // being cast to `Foo + 'a` outlives `'a`:
1039                 let outlives = ty::OutlivesPredicate(source, r);
1040                 nested.push(predicate_to_obligation(ty::Binder::dummy(outlives).to_predicate(tcx)));
1041             }
1042
1043             // `[T; n]` -> `[T]`
1044             (&ty::Array(a, _), &ty::Slice(b)) => {
1045                 let InferOk { obligations, .. } = self
1046                     .infcx
1047                     .at(&obligation.cause, obligation.param_env)
1048                     .eq(b, a)
1049                     .map_err(|_| Unimplemented)?;
1050                 nested.extend(obligations);
1051             }
1052
1053             // `Struct<T>` -> `Struct<U>`
1054             (&ty::Adt(def, substs_a), &ty::Adt(_, substs_b)) => {
1055                 let maybe_unsizing_param_idx = |arg: GenericArg<'tcx>| match arg.unpack() {
1056                     GenericArgKind::Type(ty) => match ty.kind() {
1057                         ty::Param(p) => Some(p.index),
1058                         _ => None,
1059                     },
1060
1061                     // Lifetimes aren't allowed to change during unsizing.
1062                     GenericArgKind::Lifetime(_) => None,
1063
1064                     GenericArgKind::Const(ct) => match ct.kind() {
1065                         ty::ConstKind::Param(p) => Some(p.index),
1066                         _ => None,
1067                     },
1068                 };
1069
1070                 // FIXME(eddyb) cache this (including computing `unsizing_params`)
1071                 // by putting it in a query; it would only need the `DefId` as it
1072                 // looks at declared field types, not anything substituted.
1073
1074                 // The last field of the structure has to exist and contain type/const parameters.
1075                 let (tail_field, prefix_fields) =
1076                     def.non_enum_variant().fields.split_last().ok_or(Unimplemented)?;
1077                 let tail_field_ty = tcx.bound_type_of(tail_field.did);
1078
1079                 let mut unsizing_params = GrowableBitSet::new_empty();
1080                 for arg in tail_field_ty.0.walk() {
1081                     if let Some(i) = maybe_unsizing_param_idx(arg) {
1082                         unsizing_params.insert(i);
1083                     }
1084                 }
1085
1086                 // Ensure none of the other fields mention the parameters used
1087                 // in unsizing.
1088                 for field in prefix_fields {
1089                     for arg in tcx.type_of(field.did).walk() {
1090                         if let Some(i) = maybe_unsizing_param_idx(arg) {
1091                             unsizing_params.remove(i);
1092                         }
1093                     }
1094                 }
1095
1096                 if unsizing_params.is_empty() {
1097                     return Err(Unimplemented);
1098                 }
1099
1100                 // Extract `TailField<T>` and `TailField<U>` from `Struct<T>` and `Struct<U>`,
1101                 // normalizing in the process, since `type_of` returns something directly from
1102                 // astconv (which means it's un-normalized).
1103                 let source_tail = normalize_with_depth_to(
1104                     self,
1105                     obligation.param_env,
1106                     obligation.cause.clone(),
1107                     obligation.recursion_depth + 1,
1108                     tail_field_ty.subst(tcx, substs_a),
1109                     &mut nested,
1110                 );
1111                 let target_tail = normalize_with_depth_to(
1112                     self,
1113                     obligation.param_env,
1114                     obligation.cause.clone(),
1115                     obligation.recursion_depth + 1,
1116                     tail_field_ty.subst(tcx, substs_b),
1117                     &mut nested,
1118                 );
1119
1120                 // Check that the source struct with the target's
1121                 // unsizing parameters is equal to the target.
1122                 let substs = tcx.mk_substs(substs_a.iter().enumerate().map(|(i, k)| {
1123                     if unsizing_params.contains(i as u32) { substs_b[i] } else { k }
1124                 }));
1125                 let new_struct = tcx.mk_adt(def, substs);
1126                 let InferOk { obligations, .. } = self
1127                     .infcx
1128                     .at(&obligation.cause, obligation.param_env)
1129                     .eq(target, new_struct)
1130                     .map_err(|_| Unimplemented)?;
1131                 nested.extend(obligations);
1132
1133                 // Construct the nested `TailField<T>: Unsize<TailField<U>>` predicate.
1134                 nested.push(predicate_for_trait_def(
1135                     tcx,
1136                     obligation.param_env,
1137                     obligation.cause.clone(),
1138                     obligation.predicate.def_id(),
1139                     obligation.recursion_depth + 1,
1140                     [source_tail, target_tail],
1141                 ));
1142             }
1143
1144             // `(.., T)` -> `(.., U)`
1145             (&ty::Tuple(tys_a), &ty::Tuple(tys_b)) => {
1146                 assert_eq!(tys_a.len(), tys_b.len());
1147
1148                 // The last field of the tuple has to exist.
1149                 let (&a_last, a_mid) = tys_a.split_last().ok_or(Unimplemented)?;
1150                 let &b_last = tys_b.last().unwrap();
1151
1152                 // Check that the source tuple with the target's
1153                 // last element is equal to the target.
1154                 let new_tuple = tcx.mk_tup(a_mid.iter().copied().chain(iter::once(b_last)));
1155                 let InferOk { obligations, .. } = self
1156                     .infcx
1157                     .at(&obligation.cause, obligation.param_env)
1158                     .eq(target, new_tuple)
1159                     .map_err(|_| Unimplemented)?;
1160                 nested.extend(obligations);
1161
1162                 // Construct the nested `T: Unsize<U>` predicate.
1163                 nested.push(ensure_sufficient_stack(|| {
1164                     predicate_for_trait_def(
1165                         tcx,
1166                         obligation.param_env,
1167                         obligation.cause.clone(),
1168                         obligation.predicate.def_id(),
1169                         obligation.recursion_depth + 1,
1170                         [a_last, b_last],
1171                     )
1172                 }));
1173             }
1174
1175             _ => bug!("source: {source}, target: {target}"),
1176         };
1177
1178         Ok(ImplSourceBuiltinData { nested })
1179     }
1180
1181     fn confirm_const_destruct_candidate(
1182         &mut self,
1183         obligation: &TraitObligation<'tcx>,
1184         impl_def_id: Option<DefId>,
1185     ) -> Result<ImplSourceConstDestructData<PredicateObligation<'tcx>>, SelectionError<'tcx>> {
1186         // `~const Destruct` in a non-const environment is always trivially true, since our type is `Drop`
1187         if !obligation.is_const() {
1188             return Ok(ImplSourceConstDestructData { nested: vec![] });
1189         }
1190
1191         let drop_trait = self.tcx().require_lang_item(LangItem::Drop, None);
1192
1193         let tcx = self.tcx();
1194         let self_ty = self.infcx.shallow_resolve(obligation.self_ty());
1195
1196         let mut nested = vec![];
1197         let cause = obligation.derived_cause(BuiltinDerivedObligation);
1198
1199         // If we have a custom `impl const Drop`, then
1200         // first check it like a regular impl candidate.
1201         // This is copied from confirm_impl_candidate but remaps the predicate to `~const Drop` beforehand.
1202         if let Some(impl_def_id) = impl_def_id {
1203             let mut new_obligation = obligation.clone();
1204             new_obligation.predicate = new_obligation.predicate.map_bound(|mut trait_pred| {
1205                 trait_pred.trait_ref.def_id = drop_trait;
1206                 trait_pred
1207             });
1208             let substs = self.rematch_impl(impl_def_id, &new_obligation);
1209             debug!(?substs, "impl substs");
1210
1211             let cause = obligation.derived_cause(|derived| {
1212                 ImplDerivedObligation(Box::new(ImplDerivedObligationCause {
1213                     derived,
1214                     impl_def_id,
1215                     span: obligation.cause.span,
1216                 }))
1217             });
1218             let obligations = ensure_sufficient_stack(|| {
1219                 self.vtable_impl(
1220                     impl_def_id,
1221                     substs,
1222                     &cause,
1223                     new_obligation.recursion_depth + 1,
1224                     new_obligation.param_env,
1225                     obligation.predicate,
1226                 )
1227             });
1228             nested.extend(obligations.nested);
1229         }
1230
1231         // We want to confirm the ADT's fields if we have an ADT
1232         let mut stack = match *self_ty.skip_binder().kind() {
1233             ty::Adt(def, substs) => def.all_fields().map(|f| f.ty(tcx, substs)).collect(),
1234             _ => vec![self_ty.skip_binder()],
1235         };
1236
1237         while let Some(nested_ty) = stack.pop() {
1238             match *nested_ty.kind() {
1239                 // We know these types are trivially drop
1240                 ty::Bool
1241                 | ty::Char
1242                 | ty::Int(_)
1243                 | ty::Uint(_)
1244                 | ty::Float(_)
1245                 | ty::Infer(ty::IntVar(_))
1246                 | ty::Infer(ty::FloatVar(_))
1247                 | ty::Str
1248                 | ty::RawPtr(_)
1249                 | ty::Ref(..)
1250                 | ty::FnDef(..)
1251                 | ty::FnPtr(_)
1252                 | ty::Never
1253                 | ty::Foreign(_) => {}
1254
1255                 // `ManuallyDrop` is trivially drop
1256                 ty::Adt(def, _) if Some(def.did()) == tcx.lang_items().manually_drop() => {}
1257
1258                 // These types are built-in, so we can fast-track by registering
1259                 // nested predicates for their constituent type(s)
1260                 ty::Array(ty, _) | ty::Slice(ty) => {
1261                     stack.push(ty);
1262                 }
1263                 ty::Tuple(tys) => {
1264                     stack.extend(tys.iter());
1265                 }
1266                 ty::Closure(_, substs) => {
1267                     stack.push(substs.as_closure().tupled_upvars_ty());
1268                 }
1269                 ty::Generator(_, substs, _) => {
1270                     let generator = substs.as_generator();
1271                     stack.extend([generator.tupled_upvars_ty(), generator.witness()]);
1272                 }
1273                 ty::GeneratorWitness(tys) => {
1274                     stack.extend(tcx.erase_late_bound_regions(tys).to_vec());
1275                 }
1276
1277                 // If we have a projection type, make sure to normalize it so we replace it
1278                 // with a fresh infer variable
1279                 ty::Projection(..) => {
1280                     let predicate = normalize_with_depth_to(
1281                         self,
1282                         obligation.param_env,
1283                         cause.clone(),
1284                         obligation.recursion_depth + 1,
1285                         self_ty.rebind(ty::TraitPredicate {
1286                             trait_ref: self
1287                                 .tcx()
1288                                 .at(cause.span)
1289                                 .mk_trait_ref(LangItem::Destruct, [nested_ty]),
1290                             constness: ty::BoundConstness::ConstIfConst,
1291                             polarity: ty::ImplPolarity::Positive,
1292                         }),
1293                         &mut nested,
1294                     );
1295
1296                     nested.push(Obligation::with_depth(
1297                         tcx,
1298                         cause.clone(),
1299                         obligation.recursion_depth + 1,
1300                         obligation.param_env,
1301                         predicate,
1302                     ));
1303                 }
1304
1305                 // If we have any other type (e.g. an ADT), just register a nested obligation
1306                 // since it's either not `const Drop` (and we raise an error during selection),
1307                 // or it's an ADT (and we need to check for a custom impl during selection)
1308                 _ => {
1309                     let predicate = self_ty.rebind(ty::TraitPredicate {
1310                         trait_ref: self
1311                             .tcx()
1312                             .at(cause.span)
1313                             .mk_trait_ref(LangItem::Destruct, [nested_ty]),
1314                         constness: ty::BoundConstness::ConstIfConst,
1315                         polarity: ty::ImplPolarity::Positive,
1316                     });
1317
1318                     nested.push(Obligation::with_depth(
1319                         tcx,
1320                         cause.clone(),
1321                         obligation.recursion_depth + 1,
1322                         obligation.param_env,
1323                         predicate,
1324                     ));
1325                 }
1326             }
1327         }
1328
1329         Ok(ImplSourceConstDestructData { nested })
1330     }
1331 }