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