1use std::ops::ControlFlow;
11
12use rustc_ast::Mutability;
13use rustc_data_structures::stack::ensure_sufficient_stack;
14use rustc_hir::lang_items::LangItem;
15use rustc_infer::infer::{DefineOpaqueTypes, HigherRankedType, InferOk};
16use rustc_infer::traits::ObligationCauseCode;
17use rustc_middle::traits::{BuiltinImplSource, SignatureMismatchData};
18use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, Upcast};
19use rustc_middle::{bug, span_bug};
20use rustc_span::def_id::DefId;
21use rustc_type_ir::elaborate;
22use thin_vec::thin_vec;
23use tracing::{debug, instrument};
24
25use super::SelectionCandidate::{self, *};
26use super::{BuiltinImplConditions, PredicateObligations, SelectionContext};
27use crate::traits::normalize::{normalize_with_depth, normalize_with_depth_to};
28use crate::traits::util::{self, closure_trait_ref_and_return_type};
29use crate::traits::{
30 ImplSource, ImplSourceUserDefinedData, Normalized, Obligation, ObligationCause,
31 PolyTraitObligation, PredicateObligation, Selection, SelectionError, SignatureMismatch,
32 TraitDynIncompatible, TraitObligation, Unimplemented,
33};
34
35impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
36 #[instrument(level = "debug", skip(self))]
37 pub(super) fn confirm_candidate(
38 &mut self,
39 obligation: &PolyTraitObligation<'tcx>,
40 candidate: SelectionCandidate<'tcx>,
41 ) -> Result<Selection<'tcx>, SelectionError<'tcx>> {
42 let mut impl_src = match candidate {
43 BuiltinCandidate { has_nested } => {
44 let data = self.confirm_builtin_candidate(obligation, has_nested);
45 ImplSource::Builtin(BuiltinImplSource::Misc, data)
46 }
47
48 TransmutabilityCandidate => {
49 let data = self.confirm_transmutability_candidate(obligation)?;
50 ImplSource::Builtin(BuiltinImplSource::Misc, data)
51 }
52
53 ParamCandidate(param) => {
54 let obligations =
55 self.confirm_param_candidate(obligation, param.map_bound(|t| t.trait_ref));
56 ImplSource::Param(obligations)
57 }
58
59 ImplCandidate(impl_def_id) => {
60 ImplSource::UserDefined(self.confirm_impl_candidate(obligation, impl_def_id))
61 }
62
63 AutoImplCandidate => {
64 let data = self.confirm_auto_impl_candidate(obligation)?;
65 ImplSource::Builtin(BuiltinImplSource::Misc, data)
66 }
67
68 ProjectionCandidate(idx) => {
69 let obligations = self.confirm_projection_candidate(obligation, idx)?;
70 ImplSource::Param(obligations)
71 }
72
73 ObjectCandidate(idx) => self.confirm_object_candidate(obligation, idx)?,
74
75 ClosureCandidate { .. } => {
76 let vtable_closure = self.confirm_closure_candidate(obligation)?;
77 ImplSource::Builtin(BuiltinImplSource::Misc, vtable_closure)
78 }
79
80 AsyncClosureCandidate => {
81 let vtable_closure = self.confirm_async_closure_candidate(obligation)?;
82 ImplSource::Builtin(BuiltinImplSource::Misc, vtable_closure)
83 }
84
85 AsyncFnKindHelperCandidate => {
88 ImplSource::Builtin(BuiltinImplSource::Misc, PredicateObligations::new())
89 }
90
91 CoroutineCandidate => {
92 let vtable_coroutine = self.confirm_coroutine_candidate(obligation)?;
93 ImplSource::Builtin(BuiltinImplSource::Misc, vtable_coroutine)
94 }
95
96 FutureCandidate => {
97 let vtable_future = self.confirm_future_candidate(obligation)?;
98 ImplSource::Builtin(BuiltinImplSource::Misc, vtable_future)
99 }
100
101 IteratorCandidate => {
102 let vtable_iterator = self.confirm_iterator_candidate(obligation)?;
103 ImplSource::Builtin(BuiltinImplSource::Misc, vtable_iterator)
104 }
105
106 AsyncIteratorCandidate => {
107 let vtable_iterator = self.confirm_async_iterator_candidate(obligation)?;
108 ImplSource::Builtin(BuiltinImplSource::Misc, vtable_iterator)
109 }
110
111 FnPointerCandidate => {
112 let data = self.confirm_fn_pointer_candidate(obligation)?;
113 ImplSource::Builtin(BuiltinImplSource::Misc, data)
114 }
115
116 TraitAliasCandidate => {
117 let data = self.confirm_trait_alias_candidate(obligation);
118 ImplSource::Builtin(BuiltinImplSource::Misc, data)
119 }
120
121 BuiltinObjectCandidate => {
122 ImplSource::Builtin(BuiltinImplSource::Misc, PredicateObligations::new())
126 }
127
128 BuiltinUnsizeCandidate => self.confirm_builtin_unsize_candidate(obligation)?,
129
130 TraitUpcastingUnsizeCandidate(idx) => {
131 self.confirm_trait_upcasting_unsize_candidate(obligation, idx)?
132 }
133
134 BikeshedGuaranteedNoDropCandidate => {
135 self.confirm_bikeshed_guaranteed_no_drop_candidate(obligation)
136 }
137 };
138
139 for subobligation in impl_src.borrow_nested_obligations_mut() {
142 subobligation.set_depth_from_parent(obligation.recursion_depth);
143 }
144
145 Ok(impl_src)
146 }
147
148 fn confirm_projection_candidate(
149 &mut self,
150 obligation: &PolyTraitObligation<'tcx>,
151 idx: usize,
152 ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
153 let tcx = self.tcx();
154
155 let placeholder_trait_predicate =
156 self.infcx.enter_forall_and_leak_universe(obligation.predicate).trait_ref;
157 let placeholder_self_ty = self.infcx.shallow_resolve(placeholder_trait_predicate.self_ty());
158 let candidate_predicate = self
159 .for_each_item_bound(
160 placeholder_self_ty,
161 |_, clause, clause_idx| {
162 if clause_idx == idx {
163 ControlFlow::Break(clause)
164 } else {
165 ControlFlow::Continue(())
166 }
167 },
168 || unreachable!(),
169 )
170 .break_value()
171 .expect("expected to index into clause that exists");
172 let candidate = candidate_predicate
173 .as_trait_clause()
174 .expect("projection candidate is not a trait predicate")
175 .map_bound(|t| t.trait_ref);
176
177 let candidate = self.infcx.instantiate_binder_with_fresh_vars(
178 obligation.cause.span,
179 HigherRankedType,
180 candidate,
181 );
182 let mut obligations = PredicateObligations::new();
183 let candidate = normalize_with_depth_to(
184 self,
185 obligation.param_env,
186 obligation.cause.clone(),
187 obligation.recursion_depth + 1,
188 candidate,
189 &mut obligations,
190 );
191
192 obligations.extend(
193 self.infcx
194 .at(&obligation.cause, obligation.param_env)
195 .eq(DefineOpaqueTypes::No, placeholder_trait_predicate, candidate)
196 .map(|InferOk { obligations, .. }| obligations)
197 .map_err(|_| Unimplemented)?,
198 );
199
200 if let ty::Alias(ty::Projection, alias_ty) = placeholder_self_ty.kind() {
202 let predicates = tcx.predicates_of(alias_ty.def_id).instantiate_own(tcx, alias_ty.args);
203 for (predicate, _) in predicates {
204 let normalized = normalize_with_depth_to(
205 self,
206 obligation.param_env,
207 obligation.cause.clone(),
208 obligation.recursion_depth + 1,
209 predicate,
210 &mut obligations,
211 );
212 obligations.push(Obligation::with_depth(
213 self.tcx(),
214 obligation.cause.clone(),
215 obligation.recursion_depth + 1,
216 obligation.param_env,
217 normalized,
218 ));
219 }
220 }
221
222 Ok(obligations)
223 }
224
225 fn confirm_param_candidate(
226 &mut self,
227 obligation: &PolyTraitObligation<'tcx>,
228 param: ty::PolyTraitRef<'tcx>,
229 ) -> PredicateObligations<'tcx> {
230 debug!(?obligation, ?param, "confirm_param_candidate");
231
232 match self.match_where_clause_trait_ref(obligation, param) {
237 Ok(obligations) => obligations,
238 Err(()) => {
239 bug!(
240 "Where clause `{:?}` was applicable to `{:?}` but now is not",
241 param,
242 obligation
243 );
244 }
245 }
246 }
247
248 fn confirm_builtin_candidate(
249 &mut self,
250 obligation: &PolyTraitObligation<'tcx>,
251 has_nested: bool,
252 ) -> PredicateObligations<'tcx> {
253 debug!(?obligation, ?has_nested, "confirm_builtin_candidate");
254
255 let tcx = self.tcx();
256 let obligations = if has_nested {
257 let trait_def = obligation.predicate.def_id();
258 let conditions = if tcx.is_lang_item(trait_def, LangItem::Sized) {
259 self.sized_conditions(obligation)
260 } else if tcx.is_lang_item(trait_def, LangItem::Copy) {
261 self.copy_clone_conditions(obligation)
262 } else if tcx.is_lang_item(trait_def, LangItem::Clone) {
263 self.copy_clone_conditions(obligation)
264 } else if tcx.is_lang_item(trait_def, LangItem::FusedIterator) {
265 self.fused_iterator_conditions(obligation)
266 } else {
267 bug!("unexpected builtin trait {:?}", trait_def)
268 };
269 let BuiltinImplConditions::Where(nested) = conditions else {
270 bug!("obligation {:?} had matched a builtin impl but now doesn't", obligation);
271 };
272
273 let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
274 self.collect_predicates_for_types(
275 obligation.param_env,
276 cause,
277 obligation.recursion_depth + 1,
278 trait_def,
279 nested,
280 )
281 } else {
282 PredicateObligations::new()
283 };
284
285 debug!(?obligations);
286
287 obligations
288 }
289
290 #[instrument(level = "debug", skip(self))]
291 fn confirm_transmutability_candidate(
292 &mut self,
293 obligation: &PolyTraitObligation<'tcx>,
294 ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
295 use rustc_transmute::{Answer, Assume, Condition};
296
297 fn reference_obligations<'tcx>(
299 tcx: TyCtxt<'tcx>,
300 obligation: &PolyTraitObligation<'tcx>,
301 (src_lifetime, src_ty, src_mut): (ty::Region<'tcx>, Ty<'tcx>, Mutability),
302 (dst_lifetime, dst_ty, dst_mut): (ty::Region<'tcx>, Ty<'tcx>, Mutability),
303 assume: Assume,
304 ) -> PredicateObligations<'tcx> {
305 let make_transmute_obl = |src, dst| {
306 let transmute_trait = obligation.predicate.def_id();
307 let assume = obligation.predicate.skip_binder().trait_ref.args.const_at(2);
308 let trait_ref = ty::TraitRef::new(
309 tcx,
310 transmute_trait,
311 [
312 ty::GenericArg::from(dst),
313 ty::GenericArg::from(src),
314 ty::GenericArg::from(assume),
315 ],
316 );
317 Obligation::with_depth(
318 tcx,
319 obligation.cause.clone(),
320 obligation.recursion_depth + 1,
321 obligation.param_env,
322 obligation.predicate.rebind(trait_ref),
323 )
324 };
325
326 let make_freeze_obl = |ty| {
327 let trait_ref = ty::TraitRef::new(
328 tcx,
329 tcx.require_lang_item(LangItem::Freeze, None),
330 [ty::GenericArg::from(ty)],
331 );
332 Obligation::with_depth(
333 tcx,
334 obligation.cause.clone(),
335 obligation.recursion_depth + 1,
336 obligation.param_env,
337 trait_ref,
338 )
339 };
340
341 let make_outlives_obl = |target, region| {
342 let outlives = ty::OutlivesPredicate(target, region);
343 Obligation::with_depth(
344 tcx,
345 obligation.cause.clone(),
346 obligation.recursion_depth + 1,
347 obligation.param_env,
348 obligation.predicate.rebind(outlives),
349 )
350 };
351
352 let mut obls = PredicateObligations::with_capacity(1);
356 obls.push(make_transmute_obl(src_ty, dst_ty));
357 if !assume.lifetimes {
358 obls.push(make_outlives_obl(src_lifetime, dst_lifetime));
359 }
360
361 if src_mut == Mutability::Not {
365 obls.extend([make_freeze_obl(src_ty), make_freeze_obl(dst_ty)])
366 }
367
368 if dst_mut == Mutability::Mut {
375 obls.push(make_transmute_obl(dst_ty, src_ty));
376 if !assume.lifetimes {
377 obls.push(make_outlives_obl(dst_lifetime, src_lifetime));
378 }
379 }
380
381 obls
382 }
383
384 #[instrument(level = "debug", skip(tcx, obligation))]
386 fn flatten_answer_tree<'tcx>(
387 tcx: TyCtxt<'tcx>,
388 obligation: &PolyTraitObligation<'tcx>,
389 cond: Condition<rustc_transmute::layout::rustc::Ref<'tcx>>,
390 assume: Assume,
391 ) -> PredicateObligations<'tcx> {
392 match cond {
393 Condition::IfAll(conds) | Condition::IfAny(conds) => conds
396 .into_iter()
397 .flat_map(|cond| flatten_answer_tree(tcx, obligation, cond, assume))
398 .collect(),
399 Condition::IfTransmutable { src, dst } => reference_obligations(
400 tcx,
401 obligation,
402 (src.lifetime, src.ty, src.mutability),
403 (dst.lifetime, dst.ty, dst.mutability),
404 assume,
405 ),
406 }
407 }
408
409 let predicate = obligation.predicate.skip_binder();
410
411 let mut assume = predicate.trait_ref.args.const_at(2);
412 if self.tcx().features().generic_const_exprs() {
414 assume = crate::traits::evaluate_const(self.infcx, assume, obligation.param_env)
415 }
416 let Some(assume) = rustc_transmute::Assume::from_const(self.infcx.tcx, assume) else {
417 return Err(Unimplemented);
418 };
419
420 let dst = predicate.trait_ref.args.type_at(0);
421 let src = predicate.trait_ref.args.type_at(1);
422
423 debug!(?src, ?dst);
424 let mut transmute_env = rustc_transmute::TransmuteTypeEnv::new(self.infcx.tcx);
425 let maybe_transmutable =
426 transmute_env.is_transmutable(rustc_transmute::Types { dst, src }, assume);
427
428 let fully_flattened = match maybe_transmutable {
429 Answer::No(_) => Err(Unimplemented)?,
430 Answer::If(cond) => flatten_answer_tree(self.tcx(), obligation, cond, assume),
431 Answer::Yes => PredicateObligations::new(),
432 };
433
434 debug!(?fully_flattened);
435 Ok(fully_flattened)
436 }
437
438 fn confirm_auto_impl_candidate(
444 &mut self,
445 obligation: &PolyTraitObligation<'tcx>,
446 ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
447 debug!(?obligation, "confirm_auto_impl_candidate");
448
449 let self_ty = obligation.predicate.self_ty().map_bound(|ty| self.infcx.shallow_resolve(ty));
450 let types = self.constituent_types_for_ty(self_ty)?;
451 Ok(self.vtable_auto_impl(obligation, obligation.predicate.def_id(), types))
452 }
453
454 fn vtable_auto_impl(
456 &mut self,
457 obligation: &PolyTraitObligation<'tcx>,
458 trait_def_id: DefId,
459 nested: ty::Binder<'tcx, Vec<Ty<'tcx>>>,
460 ) -> PredicateObligations<'tcx> {
461 debug!(?nested, "vtable_auto_impl");
462 ensure_sufficient_stack(|| {
463 let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
464
465 assert_eq!(obligation.predicate.polarity(), ty::PredicatePolarity::Positive);
466
467 let obligations = self.collect_predicates_for_types(
468 obligation.param_env,
469 cause,
470 obligation.recursion_depth + 1,
471 trait_def_id,
472 nested,
473 );
474
475 debug!(?obligations, "vtable_auto_impl");
476
477 obligations
478 })
479 }
480
481 fn confirm_impl_candidate(
482 &mut self,
483 obligation: &PolyTraitObligation<'tcx>,
484 impl_def_id: DefId,
485 ) -> ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>> {
486 debug!(?obligation, ?impl_def_id, "confirm_impl_candidate");
487
488 let args = self.rematch_impl(impl_def_id, obligation);
491 debug!(?args, "impl args");
492 ensure_sufficient_stack(|| {
493 self.vtable_impl(
494 impl_def_id,
495 args,
496 &obligation.cause,
497 obligation.recursion_depth + 1,
498 obligation.param_env,
499 obligation.predicate,
500 )
501 })
502 }
503
504 fn vtable_impl(
505 &mut self,
506 impl_def_id: DefId,
507 args: Normalized<'tcx, GenericArgsRef<'tcx>>,
508 cause: &ObligationCause<'tcx>,
509 recursion_depth: usize,
510 param_env: ty::ParamEnv<'tcx>,
511 parent_trait_pred: ty::Binder<'tcx, ty::TraitPredicate<'tcx>>,
512 ) -> ImplSourceUserDefinedData<'tcx, PredicateObligation<'tcx>> {
513 debug!(?impl_def_id, ?args, ?recursion_depth, "vtable_impl");
514
515 let mut impl_obligations = self.impl_or_trait_obligations(
516 cause,
517 recursion_depth,
518 param_env,
519 impl_def_id,
520 args.value,
521 parent_trait_pred,
522 );
523
524 debug!(?impl_obligations, "vtable_impl");
525
526 impl_obligations.extend(args.obligations);
532
533 ImplSourceUserDefinedData { impl_def_id, args: args.value, nested: impl_obligations }
534 }
535
536 fn confirm_object_candidate(
537 &mut self,
538 obligation: &PolyTraitObligation<'tcx>,
539 index: usize,
540 ) -> Result<ImplSource<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
541 let tcx = self.tcx();
542 debug!(?obligation, ?index, "confirm_object_candidate");
543
544 let trait_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
545 let self_ty = self.infcx.shallow_resolve(trait_predicate.self_ty());
546 let ty::Dynamic(data, ..) = *self_ty.kind() else {
547 span_bug!(obligation.cause.span, "object candidate with non-object");
548 };
549
550 let object_trait_ref = data.principal().unwrap_or_else(|| {
551 span_bug!(obligation.cause.span, "object candidate with no principal")
552 });
553 let object_trait_ref = self.infcx.instantiate_binder_with_fresh_vars(
554 obligation.cause.span,
555 HigherRankedType,
556 object_trait_ref,
557 );
558 let object_trait_ref = object_trait_ref.with_self_ty(self.tcx(), self_ty);
559
560 let mut nested = PredicateObligations::new();
561
562 let mut supertraits = util::supertraits(tcx, ty::Binder::dummy(object_trait_ref));
563 let unnormalized_upcast_trait_ref =
564 supertraits.nth(index).expect("supertraits iterator no longer has as many elements");
565
566 let upcast_trait_ref = self.infcx.instantiate_binder_with_fresh_vars(
567 obligation.cause.span,
568 HigherRankedType,
569 unnormalized_upcast_trait_ref,
570 );
571 let upcast_trait_ref = normalize_with_depth_to(
572 self,
573 obligation.param_env,
574 obligation.cause.clone(),
575 obligation.recursion_depth + 1,
576 upcast_trait_ref,
577 &mut nested,
578 );
579
580 nested.extend(
581 self.infcx
582 .at(&obligation.cause, obligation.param_env)
583 .eq(DefineOpaqueTypes::No, trait_predicate.trait_ref, upcast_trait_ref)
584 .map(|InferOk { obligations, .. }| obligations)
585 .map_err(|_| Unimplemented)?,
586 );
587
588 for (supertrait, _) in tcx
591 .explicit_super_predicates_of(trait_predicate.def_id())
592 .iter_instantiated_copied(tcx, trait_predicate.trait_ref.args)
593 {
594 let normalized_supertrait = normalize_with_depth_to(
595 self,
596 obligation.param_env,
597 obligation.cause.clone(),
598 obligation.recursion_depth + 1,
599 supertrait,
600 &mut nested,
601 );
602 nested.push(obligation.with(tcx, normalized_supertrait));
603 }
604
605 let assoc_types: Vec<_> = tcx
606 .associated_items(trait_predicate.def_id())
607 .in_definition_order()
608 .filter(|item| !tcx.generics_require_sized_self(item.def_id))
611 .filter_map(
612 |item| if item.kind == ty::AssocKind::Type { Some(item.def_id) } else { None },
613 )
614 .collect();
615
616 for assoc_type in assoc_types {
617 let defs: &ty::Generics = tcx.generics_of(assoc_type);
618
619 if !defs.own_params.is_empty() {
620 tcx.dcx().span_delayed_bug(
621 obligation.cause.span,
622 "GATs in trait object shouldn't have been considered",
623 );
624 return Err(SelectionError::TraitDynIncompatible(trait_predicate.trait_ref.def_id));
625 }
626
627 for bound in self.tcx().item_bounds(assoc_type).transpose_iter() {
631 let normalized_bound = normalize_with_depth_to(
632 self,
633 obligation.param_env,
634 obligation.cause.clone(),
635 obligation.recursion_depth + 1,
636 bound.instantiate(tcx, trait_predicate.trait_ref.args),
637 &mut nested,
638 );
639 nested.push(obligation.with(tcx, normalized_bound));
640 }
641 }
642
643 debug!(?nested, "object nested obligations");
644
645 Ok(ImplSource::Builtin(BuiltinImplSource::Object(index), nested))
646 }
647
648 fn confirm_fn_pointer_candidate(
649 &mut self,
650 obligation: &PolyTraitObligation<'tcx>,
651 ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
652 debug!(?obligation, "confirm_fn_pointer_candidate");
653 let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
654 let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
655
656 let tcx = self.tcx();
657 let sig = self_ty.fn_sig(tcx);
658 let trait_ref = closure_trait_ref_and_return_type(
659 tcx,
660 obligation.predicate.def_id(),
661 self_ty,
662 sig,
663 util::TupleArgumentsFlag::Yes,
664 )
665 .map_bound(|(trait_ref, _)| trait_ref);
666
667 let mut nested =
668 self.equate_trait_refs(obligation.with(tcx, placeholder_predicate), trait_ref)?;
669 let cause = obligation.derived_cause(ObligationCauseCode::BuiltinDerived);
670
671 let output_ty = self.infcx.enter_forall_and_leak_universe(sig.output());
673 let output_ty = normalize_with_depth_to(
674 self,
675 obligation.param_env,
676 cause.clone(),
677 obligation.recursion_depth,
678 output_ty,
679 &mut nested,
680 );
681 let tr = ty::TraitRef::new(
682 self.tcx(),
683 self.tcx().require_lang_item(LangItem::Sized, Some(cause.span)),
684 [output_ty],
685 );
686 nested.push(Obligation::new(self.infcx.tcx, cause, obligation.param_env, tr));
687
688 Ok(nested)
689 }
690
691 fn confirm_trait_alias_candidate(
692 &mut self,
693 obligation: &PolyTraitObligation<'tcx>,
694 ) -> PredicateObligations<'tcx> {
695 debug!(?obligation, "confirm_trait_alias_candidate");
696
697 let predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
698 let trait_ref = predicate.trait_ref;
699 let trait_def_id = trait_ref.def_id;
700 let args = trait_ref.args;
701
702 let trait_obligations = self.impl_or_trait_obligations(
703 &obligation.cause,
704 obligation.recursion_depth,
705 obligation.param_env,
706 trait_def_id,
707 args,
708 obligation.predicate,
709 );
710
711 debug!(?trait_def_id, ?trait_obligations, "trait alias obligations");
712
713 trait_obligations
714 }
715
716 fn confirm_coroutine_candidate(
717 &mut self,
718 obligation: &PolyTraitObligation<'tcx>,
719 ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
720 let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
721 let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
722 let ty::Coroutine(coroutine_def_id, args) = *self_ty.kind() else {
723 bug!("closure candidate for non-closure {:?}", obligation);
724 };
725
726 debug!(?obligation, ?coroutine_def_id, ?args, "confirm_coroutine_candidate");
727
728 let coroutine_sig = args.as_coroutine().sig();
729
730 let (trait_ref, _, _) = super::util::coroutine_trait_ref_and_outputs(
731 self.tcx(),
732 obligation.predicate.def_id(),
733 self_ty,
734 coroutine_sig,
735 );
736
737 let nested = self.equate_trait_refs(
738 obligation.with(self.tcx(), placeholder_predicate),
739 ty::Binder::dummy(trait_ref),
740 )?;
741 debug!(?trait_ref, ?nested, "coroutine candidate obligations");
742
743 Ok(nested)
744 }
745
746 fn confirm_future_candidate(
747 &mut self,
748 obligation: &PolyTraitObligation<'tcx>,
749 ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
750 let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
751 let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
752 let ty::Coroutine(coroutine_def_id, args) = *self_ty.kind() else {
753 bug!("closure candidate for non-closure {:?}", obligation);
754 };
755
756 debug!(?obligation, ?coroutine_def_id, ?args, "confirm_future_candidate");
757
758 let coroutine_sig = args.as_coroutine().sig();
759
760 let (trait_ref, _) = super::util::future_trait_ref_and_outputs(
761 self.tcx(),
762 obligation.predicate.def_id(),
763 self_ty,
764 coroutine_sig,
765 );
766
767 let nested = self.equate_trait_refs(
768 obligation.with(self.tcx(), placeholder_predicate),
769 ty::Binder::dummy(trait_ref),
770 )?;
771 debug!(?trait_ref, ?nested, "future candidate obligations");
772
773 Ok(nested)
774 }
775
776 fn confirm_iterator_candidate(
777 &mut self,
778 obligation: &PolyTraitObligation<'tcx>,
779 ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
780 let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
781 let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
782 let ty::Coroutine(coroutine_def_id, args) = *self_ty.kind() else {
783 bug!("closure candidate for non-closure {:?}", obligation);
784 };
785
786 debug!(?obligation, ?coroutine_def_id, ?args, "confirm_iterator_candidate");
787
788 let gen_sig = args.as_coroutine().sig();
789
790 let (trait_ref, _) = super::util::iterator_trait_ref_and_outputs(
791 self.tcx(),
792 obligation.predicate.def_id(),
793 self_ty,
794 gen_sig,
795 );
796
797 let nested = self.equate_trait_refs(
798 obligation.with(self.tcx(), placeholder_predicate),
799 ty::Binder::dummy(trait_ref),
800 )?;
801 debug!(?trait_ref, ?nested, "iterator candidate obligations");
802
803 Ok(nested)
804 }
805
806 fn confirm_async_iterator_candidate(
807 &mut self,
808 obligation: &PolyTraitObligation<'tcx>,
809 ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
810 let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
811 let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
812 let ty::Coroutine(coroutine_def_id, args) = *self_ty.kind() else {
813 bug!("closure candidate for non-closure {:?}", obligation);
814 };
815
816 debug!(?obligation, ?coroutine_def_id, ?args, "confirm_async_iterator_candidate");
817
818 let gen_sig = args.as_coroutine().sig();
819
820 let (trait_ref, _) = super::util::async_iterator_trait_ref_and_outputs(
821 self.tcx(),
822 obligation.predicate.def_id(),
823 self_ty,
824 gen_sig,
825 );
826
827 let nested = self.equate_trait_refs(
828 obligation.with(self.tcx(), placeholder_predicate),
829 ty::Binder::dummy(trait_ref),
830 )?;
831 debug!(?trait_ref, ?nested, "iterator candidate obligations");
832
833 Ok(nested)
834 }
835
836 #[instrument(skip(self), level = "debug")]
837 fn confirm_closure_candidate(
838 &mut self,
839 obligation: &PolyTraitObligation<'tcx>,
840 ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
841 let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
842 let self_ty: Ty<'_> = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
843
844 let trait_ref = match *self_ty.kind() {
845 ty::Closure(..) => {
846 self.closure_trait_ref_unnormalized(self_ty, obligation.predicate.def_id())
847 }
848 ty::CoroutineClosure(_, args) => {
849 args.as_coroutine_closure().coroutine_closure_sig().map_bound(|sig| {
850 ty::TraitRef::new(
851 self.tcx(),
852 obligation.predicate.def_id(),
853 [self_ty, sig.tupled_inputs_ty],
854 )
855 })
856 }
857 _ => {
858 bug!("closure candidate for non-closure {:?}", obligation);
859 }
860 };
861
862 self.equate_trait_refs(obligation.with(self.tcx(), placeholder_predicate), trait_ref)
863 }
864
865 #[instrument(skip(self), level = "debug")]
866 fn confirm_async_closure_candidate(
867 &mut self,
868 obligation: &PolyTraitObligation<'tcx>,
869 ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
870 let placeholder_predicate = self.infcx.enter_forall_and_leak_universe(obligation.predicate);
871 let self_ty = self.infcx.shallow_resolve(placeholder_predicate.self_ty());
872
873 let tcx = self.tcx();
874
875 let mut nested = PredicateObligations::new();
876 let (trait_ref, kind_ty) = match *self_ty.kind() {
877 ty::CoroutineClosure(_, args) => {
878 let args = args.as_coroutine_closure();
879 let trait_ref = args.coroutine_closure_sig().map_bound(|sig| {
880 ty::TraitRef::new(
881 self.tcx(),
882 obligation.predicate.def_id(),
883 [self_ty, sig.tupled_inputs_ty],
884 )
885 });
886
887 (trait_ref, args.kind_ty())
891 }
892 ty::FnDef(..) | ty::FnPtr(..) => {
893 let sig = self_ty.fn_sig(tcx);
894 let trait_ref = sig.map_bound(|sig| {
895 ty::TraitRef::new(
896 self.tcx(),
897 obligation.predicate.def_id(),
898 [self_ty, Ty::new_tup(tcx, sig.inputs())],
899 )
900 });
901
902 let future_trait_def_id = tcx.require_lang_item(LangItem::Future, None);
904 nested.push(obligation.with(
905 tcx,
906 sig.output().map_bound(|output_ty| {
907 ty::TraitRef::new(tcx, future_trait_def_id, [output_ty])
908 }),
909 ));
910 let sized_trait_def_id = tcx.require_lang_item(LangItem::Sized, None);
911 nested.push(obligation.with(
912 tcx,
913 sig.output().map_bound(|output_ty| {
914 ty::TraitRef::new(tcx, sized_trait_def_id, [output_ty])
915 }),
916 ));
917
918 (trait_ref, Ty::from_closure_kind(tcx, ty::ClosureKind::Fn))
919 }
920 ty::Closure(_, args) => {
921 let args = args.as_closure();
922 let sig = args.sig();
923 let trait_ref = sig.map_bound(|sig| {
924 ty::TraitRef::new(
925 self.tcx(),
926 obligation.predicate.def_id(),
927 [self_ty, sig.inputs()[0]],
928 )
929 });
930
931 let future_trait_def_id = tcx.require_lang_item(LangItem::Future, None);
933 let placeholder_output_ty = self.infcx.enter_forall_and_leak_universe(sig.output());
934 nested.push(obligation.with(
935 tcx,
936 ty::TraitRef::new(tcx, future_trait_def_id, [placeholder_output_ty]),
937 ));
938 let sized_trait_def_id = tcx.require_lang_item(LangItem::Sized, None);
939 nested.push(obligation.with(
940 tcx,
941 sig.output().map_bound(|output_ty| {
942 ty::TraitRef::new(tcx, sized_trait_def_id, [output_ty])
943 }),
944 ));
945
946 (trait_ref, args.kind_ty())
947 }
948 _ => bug!("expected callable type for AsyncFn candidate"),
949 };
950
951 nested.extend(
952 self.equate_trait_refs(obligation.with(tcx, placeholder_predicate), trait_ref)?,
953 );
954
955 let goal_kind =
956 self.tcx().async_fn_trait_kind_from_def_id(obligation.predicate.def_id()).unwrap();
957
958 if let Some(closure_kind) = self.infcx.shallow_resolve(kind_ty).to_opt_closure_kind() {
962 if !closure_kind.extends(goal_kind) {
963 return Err(SelectionError::Unimplemented);
964 }
965 } else {
966 nested.push(Obligation::new(
967 self.tcx(),
968 obligation.derived_cause(ObligationCauseCode::BuiltinDerived),
969 obligation.param_env,
970 ty::TraitRef::new(
971 self.tcx(),
972 self.tcx().require_lang_item(
973 LangItem::AsyncFnKindHelper,
974 Some(obligation.cause.span),
975 ),
976 [kind_ty, Ty::from_closure_kind(self.tcx(), goal_kind)],
977 ),
978 ));
979 }
980
981 Ok(nested)
982 }
983
984 #[instrument(skip(self), level = "trace")]
1010 fn equate_trait_refs(
1011 &mut self,
1012 obligation: TraitObligation<'tcx>,
1013 found_trait_ref: ty::PolyTraitRef<'tcx>,
1014 ) -> Result<PredicateObligations<'tcx>, SelectionError<'tcx>> {
1015 let found_trait_ref = self.infcx.instantiate_binder_with_fresh_vars(
1016 obligation.cause.span,
1017 HigherRankedType,
1018 found_trait_ref,
1019 );
1020 let Normalized { obligations: nested, value: (obligation_trait_ref, found_trait_ref) } =
1022 ensure_sufficient_stack(|| {
1023 normalize_with_depth(
1024 self,
1025 obligation.param_env,
1026 obligation.cause.clone(),
1027 obligation.recursion_depth + 1,
1028 (obligation.predicate.trait_ref, found_trait_ref),
1029 )
1030 });
1031
1032 self.infcx
1034 .at(&obligation.cause, obligation.param_env)
1035 .eq(DefineOpaqueTypes::Yes, obligation_trait_ref, found_trait_ref)
1036 .map(|InferOk { mut obligations, .. }| {
1037 obligations.extend(nested);
1038 obligations
1039 })
1040 .map_err(|terr| {
1041 SignatureMismatch(Box::new(SignatureMismatchData {
1042 expected_trait_ref: obligation_trait_ref,
1043 found_trait_ref,
1044 terr,
1045 }))
1046 })
1047 }
1048
1049 fn confirm_trait_upcasting_unsize_candidate(
1050 &mut self,
1051 obligation: &PolyTraitObligation<'tcx>,
1052 idx: usize,
1053 ) -> Result<ImplSource<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
1054 let tcx = self.tcx();
1055
1056 let predicate = obligation.predicate.no_bound_vars().unwrap();
1059 let a_ty = self.infcx.shallow_resolve(predicate.self_ty());
1060 let b_ty = self.infcx.shallow_resolve(predicate.trait_ref.args.type_at(1));
1061
1062 let ty::Dynamic(a_data, a_region, ty::Dyn) = *a_ty.kind() else {
1063 bug!("expected `dyn` type in `confirm_trait_upcasting_unsize_candidate`")
1064 };
1065 let ty::Dynamic(b_data, b_region, ty::Dyn) = *b_ty.kind() else {
1066 bug!("expected `dyn` type in `confirm_trait_upcasting_unsize_candidate`")
1067 };
1068
1069 let source_principal = a_data.principal().unwrap().with_self_ty(tcx, a_ty);
1070 let unnormalized_upcast_principal =
1071 util::supertraits(tcx, source_principal).nth(idx).unwrap();
1072
1073 let nested = self
1074 .match_upcast_principal(
1075 obligation,
1076 unnormalized_upcast_principal,
1077 a_data,
1078 b_data,
1079 a_region,
1080 b_region,
1081 )?
1082 .expect("did not expect ambiguity during confirmation");
1083
1084 Ok(ImplSource::Builtin(BuiltinImplSource::TraitUpcasting(idx), nested))
1085 }
1086
1087 fn confirm_builtin_unsize_candidate(
1088 &mut self,
1089 obligation: &PolyTraitObligation<'tcx>,
1090 ) -> Result<ImplSource<'tcx, PredicateObligation<'tcx>>, SelectionError<'tcx>> {
1091 let tcx = self.tcx();
1092
1093 let source = self.infcx.shallow_resolve(obligation.self_ty().no_bound_vars().unwrap());
1096 let target = obligation.predicate.skip_binder().trait_ref.args.type_at(1);
1097 let target = self.infcx.shallow_resolve(target);
1098 debug!(?source, ?target, "confirm_builtin_unsize_candidate");
1099
1100 Ok(match (source.kind(), target.kind()) {
1101 (&ty::Dynamic(data_a, r_a, dyn_a), &ty::Dynamic(data_b, r_b, dyn_b))
1103 if dyn_a == dyn_b =>
1104 {
1105 let iter = data_a
1108 .principal()
1109 .filter(|_| {
1110 data_b.principal().is_some()
1112 })
1113 .map(|b| b.map_bound(ty::ExistentialPredicate::Trait))
1114 .into_iter()
1115 .chain(
1116 data_a
1117 .projection_bounds()
1118 .map(|b| b.map_bound(ty::ExistentialPredicate::Projection)),
1119 )
1120 .chain(
1121 data_b
1122 .auto_traits()
1123 .map(ty::ExistentialPredicate::AutoTrait)
1124 .map(ty::Binder::dummy),
1125 );
1126 let existential_predicates = tcx.mk_poly_existential_predicates_from_iter(iter);
1127 let source_trait = Ty::new_dynamic(tcx, existential_predicates, r_b, dyn_a);
1128
1129 let InferOk { mut obligations, .. } = self
1132 .infcx
1133 .at(&obligation.cause, obligation.param_env)
1134 .sup(DefineOpaqueTypes::Yes, target, source_trait)
1135 .map_err(|_| Unimplemented)?;
1136
1137 let outlives = ty::OutlivesPredicate(r_a, r_b);
1139 obligations.push(Obligation::with_depth(
1140 tcx,
1141 obligation.cause.clone(),
1142 obligation.recursion_depth + 1,
1143 obligation.param_env,
1144 obligation.predicate.rebind(outlives),
1145 ));
1146
1147 ImplSource::Builtin(BuiltinImplSource::Misc, obligations)
1148 }
1149
1150 (_, &ty::Dynamic(data, r, ty::Dyn)) => {
1152 let mut object_dids = data.auto_traits().chain(data.principal_def_id());
1153 if let Some(did) = object_dids.find(|did| !tcx.is_dyn_compatible(*did)) {
1154 return Err(TraitDynIncompatible(did));
1155 }
1156
1157 let predicate_to_obligation = |predicate| {
1158 Obligation::with_depth(
1159 tcx,
1160 obligation.cause.clone(),
1161 obligation.recursion_depth + 1,
1162 obligation.param_env,
1163 predicate,
1164 )
1165 };
1166
1167 let mut nested: PredicateObligations<'_> = data
1174 .iter()
1175 .map(|predicate| predicate_to_obligation(predicate.with_self_ty(tcx, source)))
1176 .collect();
1177
1178 let tr = ty::TraitRef::new(
1180 tcx,
1181 tcx.require_lang_item(LangItem::Sized, Some(obligation.cause.span)),
1182 [source],
1183 );
1184 nested.push(predicate_to_obligation(tr.upcast(tcx)));
1185
1186 let outlives = ty::OutlivesPredicate(source, r);
1189 nested.push(predicate_to_obligation(
1190 ty::ClauseKind::TypeOutlives(outlives).upcast(tcx),
1191 ));
1192
1193 if let Some(principal) = data.principal() {
1196 for supertrait in
1197 elaborate::supertraits(tcx, principal.with_self_ty(tcx, source))
1198 {
1199 if tcx.is_trait_alias(supertrait.def_id()) {
1200 continue;
1201 }
1202
1203 for &assoc_item in tcx.associated_item_def_ids(supertrait.def_id()) {
1204 if !tcx.is_impl_trait_in_trait(assoc_item) {
1205 continue;
1206 }
1207
1208 if tcx.generics_require_sized_self(assoc_item) {
1210 continue;
1211 }
1212
1213 let pointer_like_goal = pointer_like_goal_for_rpitit(
1214 tcx,
1215 supertrait,
1216 assoc_item,
1217 &obligation.cause,
1218 );
1219
1220 nested.push(predicate_to_obligation(pointer_like_goal.upcast(tcx)));
1221 }
1222 }
1223 }
1224
1225 ImplSource::Builtin(BuiltinImplSource::Misc, nested)
1226 }
1227
1228 (&ty::Array(a, _), &ty::Slice(b)) => {
1230 let InferOk { obligations, .. } = self
1231 .infcx
1232 .at(&obligation.cause, obligation.param_env)
1233 .eq(DefineOpaqueTypes::Yes, b, a)
1234 .map_err(|_| Unimplemented)?;
1235
1236 ImplSource::Builtin(BuiltinImplSource::Misc, obligations)
1237 }
1238
1239 (&ty::Adt(def, args_a), &ty::Adt(_, args_b)) => {
1241 let unsizing_params = tcx.unsizing_params_for_adt(def.did());
1242 if unsizing_params.is_empty() {
1243 return Err(Unimplemented);
1244 }
1245
1246 let tail_field = def.non_enum_variant().tail();
1247 let tail_field_ty = tcx.type_of(tail_field.did);
1248
1249 let mut nested = PredicateObligations::new();
1250
1251 let source_tail = normalize_with_depth_to(
1255 self,
1256 obligation.param_env,
1257 obligation.cause.clone(),
1258 obligation.recursion_depth + 1,
1259 tail_field_ty.instantiate(tcx, args_a),
1260 &mut nested,
1261 );
1262 let target_tail = normalize_with_depth_to(
1263 self,
1264 obligation.param_env,
1265 obligation.cause.clone(),
1266 obligation.recursion_depth + 1,
1267 tail_field_ty.instantiate(tcx, args_b),
1268 &mut nested,
1269 );
1270
1271 let args =
1274 tcx.mk_args_from_iter(args_a.iter().enumerate().map(|(i, k)| {
1275 if unsizing_params.contains(i as u32) { args_b[i] } else { k }
1276 }));
1277 let new_struct = Ty::new_adt(tcx, def, args);
1278 let InferOk { obligations, .. } = self
1279 .infcx
1280 .at(&obligation.cause, obligation.param_env)
1281 .eq(DefineOpaqueTypes::Yes, target, new_struct)
1282 .map_err(|_| Unimplemented)?;
1283 nested.extend(obligations);
1284
1285 let tail_unsize_obligation = obligation.with(
1287 tcx,
1288 ty::TraitRef::new(
1289 tcx,
1290 obligation.predicate.def_id(),
1291 [source_tail, target_tail],
1292 ),
1293 );
1294 nested.push(tail_unsize_obligation);
1295
1296 ImplSource::Builtin(BuiltinImplSource::Misc, nested)
1297 }
1298
1299 _ => bug!("source: {source}, target: {target}"),
1300 })
1301 }
1302
1303 fn confirm_bikeshed_guaranteed_no_drop_candidate(
1304 &mut self,
1305 obligation: &PolyTraitObligation<'tcx>,
1306 ) -> ImplSource<'tcx, PredicateObligation<'tcx>> {
1307 let mut obligations = thin_vec![];
1308
1309 let tcx = self.tcx();
1310 let self_ty = obligation.predicate.self_ty();
1311 match *self_ty.skip_binder().kind() {
1312 ty::Ref(..) => {}
1314 ty::Adt(def, _) if def.is_manually_drop() => {}
1316 ty::Tuple(tys) => {
1319 obligations.extend(tys.iter().map(|elem_ty| {
1320 obligation.with(
1321 tcx,
1322 self_ty.rebind(ty::TraitRef::new(
1323 tcx,
1324 obligation.predicate.def_id(),
1325 [elem_ty],
1326 )),
1327 )
1328 }));
1329 }
1330 ty::Array(elem_ty, _) => {
1331 obligations.push(obligation.with(
1332 tcx,
1333 self_ty.rebind(ty::TraitRef::new(
1334 tcx,
1335 obligation.predicate.def_id(),
1336 [elem_ty],
1337 )),
1338 ));
1339 }
1340
1341 ty::FnDef(..)
1345 | ty::FnPtr(..)
1346 | ty::Error(_)
1347 | ty::Uint(_)
1348 | ty::Int(_)
1349 | ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
1350 | ty::Bool
1351 | ty::Float(_)
1352 | ty::Char
1353 | ty::RawPtr(..)
1354 | ty::Never
1355 | ty::Pat(..)
1356 | ty::Dynamic(..)
1357 | ty::Str
1358 | ty::Slice(_)
1359 | ty::Foreign(..)
1360 | ty::Adt(..)
1361 | ty::Alias(..)
1362 | ty::Param(_)
1363 | ty::Placeholder(..)
1364 | ty::Closure(..)
1365 | ty::CoroutineClosure(..)
1366 | ty::Coroutine(..)
1367 | ty::UnsafeBinder(_)
1368 | ty::CoroutineWitness(..)
1369 | ty::Bound(..) => {
1370 obligations.push(obligation.with(
1371 tcx,
1372 self_ty.map_bound(|ty| {
1373 ty::TraitRef::new(
1374 tcx,
1375 tcx.require_lang_item(LangItem::Copy, Some(obligation.cause.span)),
1376 [ty],
1377 )
1378 }),
1379 ));
1380 }
1381
1382 ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
1383 panic!("unexpected type `{self_ty:?}`")
1384 }
1385 }
1386
1387 ImplSource::Builtin(BuiltinImplSource::Misc, obligations)
1388 }
1389}
1390
1391fn pointer_like_goal_for_rpitit<'tcx>(
1401 tcx: TyCtxt<'tcx>,
1402 supertrait: ty::PolyTraitRef<'tcx>,
1403 rpitit_item: DefId,
1404 cause: &ObligationCause<'tcx>,
1405) -> ty::PolyTraitRef<'tcx> {
1406 let mut bound_vars = supertrait.bound_vars().to_vec();
1407
1408 let args = supertrait.skip_binder().args.extend_to(tcx, rpitit_item, |arg, _| match arg.kind {
1409 ty::GenericParamDefKind::Lifetime => {
1410 let kind = ty::BoundRegionKind::Named(arg.def_id, tcx.item_name(arg.def_id));
1411 bound_vars.push(ty::BoundVariableKind::Region(kind));
1412 ty::Region::new_bound(
1413 tcx,
1414 ty::INNERMOST,
1415 ty::BoundRegion { var: ty::BoundVar::from_usize(bound_vars.len() - 1), kind },
1416 )
1417 .into()
1418 }
1419 ty::GenericParamDefKind::Type { .. } | ty::GenericParamDefKind::Const { .. } => {
1420 unreachable!()
1421 }
1422 });
1423
1424 ty::Binder::bind_with_vars(
1425 ty::TraitRef::new(
1426 tcx,
1427 tcx.require_lang_item(LangItem::PointerLike, Some(cause.span)),
1428 [Ty::new_projection_from_args(tcx, rpitit_item, args)],
1429 ),
1430 tcx.mk_bound_variable_kinds(&bound_vars),
1431 )
1432}