1use std::debug_assert_matches;
2
3use rustc_type_ir::fast_reject::DeepRejectCtxt;
4use rustc_type_ir::inherent::*;
5use rustc_type_ir::lang_items::{SolverAdtLangItem, SolverProjectionLangItem, SolverTraitLangItem};
6use rustc_type_ir::solve::{
7 FetchEligibleAssocItemResponse, NoSolutionOrRerunNonErased, QueryResultOrRerunNonErased,
8 RerunNonErased, RerunReason, RerunResultExt,
9};
10use rustc_type_ir::{
11 self as ty, FieldInfo, Interner, NormalizesTo, PredicateKind, Unnormalized, Upcast as _,
12};
13use tracing::instrument;
14
15use crate::delegate::SolverDelegate;
16use crate::solve::assembly::structural_traits::{self, AsyncCallableRelevantTypes};
17use crate::solve::assembly::{self, Candidate};
18use crate::solve::inspect::ProbeKind;
19use crate::solve::{
20 BuiltinImplSource, CandidateSource, Certainty, EvalCtxt, Goal, GoalSource, MaybeInfo,
21 NoSolution, SizedTraitKind,
22};
23
24impl<D, I> EvalCtxt<'_, D>
25where
26 D: SolverDelegate<Interner = I>,
27 I: Interner,
28{
29 x;#[instrument(level = "trace", skip(self), ret)]
30 pub(super) fn compute_normalizes_to_goal(
31 &mut self,
32 goal: Goal<I, NormalizesTo<I>>,
33 ) -> QueryResultOrRerunNonErased<I> {
34 debug_assert!(self.term_is_fully_unconstrained(goal));
35 debug_assert_matches!(
36 goal.predicate.alias.kind,
37 ty::AliasTermKind::ProjectionTy { .. } | ty::AliasTermKind::ProjectionConst { .. }
38 );
39
40 let cx = self.cx();
41
42 let trait_ref = goal.predicate.alias.trait_ref(cx);
43 let (_, proven_via) = self.probe(|_| ProbeKind::ShadowedEnvProbing).enter(|ecx| {
44 let trait_goal: Goal<I, ty::TraitPredicate<I>> = goal.with(cx, trait_ref);
45 ecx.compute_trait_goal(trait_goal)
46 })?;
47 self.assemble_and_merge_candidates(
48 proven_via,
49 goal,
50 |ecx| {
51 for arg in goal.predicate.alias.own_args(cx).iter() {
61 let Some(term) = arg.as_term() else {
62 continue;
63 };
64 match ecx.structurally_normalize_term(goal.param_env, term) {
65 Ok(term) => {
66 if term.is_infer() {
67 return Some(ecx.evaluate_added_goals_and_make_canonical_response(
68 Certainty::AMBIGUOUS,
69 ));
70 }
71 }
72 Err(
73 e @ (NoSolutionOrRerunNonErased::NoSolution(NoSolution)
74 | NoSolutionOrRerunNonErased::RerunNonErased(_)),
75 ) => {
76 return Some(Err(e));
77 }
78 }
79 }
80
81 None
82 },
83 |ecx| {
84 ecx.probe(|&result| ProbeKind::RigidAlias { result })
85 .enter(|this| {
86 this.structurally_instantiate_normalizes_to_term(
87 goal,
88 goal.predicate.alias,
89 );
90 this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
91 })
92 .map_err(Into::into)
93 },
94 )
95 }
96
97 pub fn push_const_arg_has_type_goal(
100 &mut self,
101 param_env: I::ParamEnv,
102 alias: ty::AliasTerm<I>,
103 term: I::Term,
104 ) {
105 if let Some(ct) = term.as_const() {
106 let cx = self.cx();
107 let expected_ty = alias.expect_ct().type_of(cx).skip_norm_wip();
108 self.add_goal(
109 GoalSource::Misc,
110 Goal {
111 param_env,
112 predicate: ty::ClauseKind::ConstArgHasType(ct, expected_ty).upcast(cx),
113 },
114 );
115 }
116 }
117
118 fn instantiate_normalizes_to_term(&mut self, goal: Goal<I, NormalizesTo<I>>, term: I::Term) {
145 self.push_const_arg_has_type_goal(goal.param_env, goal.predicate.alias, term);
146 self.eq(goal.param_env, goal.predicate.term, term)
147 .expect("expected goal term to be fully unconstrained");
148 }
149
150 fn structurally_instantiate_normalizes_to_term(
153 &mut self,
154 goal: Goal<I, NormalizesTo<I>>,
155 term: ty::AliasTerm<I>,
156 ) {
157 self.relate_rigid_alias_non_alias(goal.param_env, term, ty::Invariant, goal.predicate.term)
158 .expect("expected goal term to be fully unconstrained");
159 }
160}
161
162impl<D, I> assembly::GoalKind<D> for NormalizesTo<I>
163where
164 D: SolverDelegate<Interner = I>,
165 I: Interner,
166{
167 fn self_ty(self) -> I::Ty {
168 self.self_ty()
169 }
170
171 fn trait_ref(self, cx: I) -> ty::TraitRef<I> {
172 self.alias.trait_ref(cx)
173 }
174
175 fn with_replaced_self_ty(self, cx: I, self_ty: I::Ty) -> Self {
176 self.with_replaced_self_ty(cx, self_ty)
177 }
178
179 fn trait_def_id(self, cx: I) -> I::TraitId {
180 self.trait_def_id(cx)
181 }
182
183 fn fast_reject_assumption(
184 ecx: &mut EvalCtxt<'_, D>,
185 goal: Goal<I, Self>,
186 assumption: I::Clause,
187 ) -> Result<(), NoSolution> {
188 let alias_def_id = match goal.predicate.alias.kind {
189 ty::AliasTermKind::ProjectionTy { def_id } => def_id.into(),
190 ty::AliasTermKind::ProjectionConst { def_id } => def_id.into(),
191 _ => return Err(NoSolution),
192 };
193 if let Some(projection_pred) = assumption.as_projection_clause()
194 && projection_pred.item_def_id() == alias_def_id
195 && DeepRejectCtxt::relate_rigid_rigid(ecx.cx()).args_may_unify(
196 goal.predicate.alias.args,
197 projection_pred.skip_binder().projection_term.args,
198 )
199 {
200 Ok(())
201 } else {
202 Err(NoSolution)
203 }
204 }
205
206 fn match_assumption(
207 ecx: &mut EvalCtxt<'_, D>,
208 goal: Goal<I, Self>,
209 assumption: I::Clause,
210 then: impl FnOnce(&mut EvalCtxt<'_, D>) -> QueryResultOrRerunNonErased<I>,
211 ) -> QueryResultOrRerunNonErased<I> {
212 let cx = ecx.cx();
213 let projection_pred = assumption.as_projection_clause().unwrap();
214 let assumption_projection_pred = ecx.instantiate_binder_with_infer(projection_pred);
215 ecx.eq(goal.param_env, goal.predicate.alias, assumption_projection_pred.projection_term)?;
216
217 ecx.instantiate_normalizes_to_term(goal, assumption_projection_pred.term);
218
219 ecx.add_goals(
222 GoalSource::AliasWellFormed,
223 cx.own_predicates_of(goal.predicate.alias.expect_projection_def_id().into())
224 .iter_instantiated(cx, goal.predicate.alias.args)
225 .map(Unnormalized::skip_norm_wip)
226 .map(|pred| goal.with(cx, pred)),
227 );
228
229 then(ecx)
230 }
231
232 fn probe_and_consider_object_bound_candidate(
236 ecx: &mut EvalCtxt<'_, D>,
237 source: CandidateSource<I>,
238 goal: Goal<I, Self>,
239 assumption: I::Clause,
240 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
241 Self::probe_and_match_goal_against_assumption(ecx, source, goal, assumption, |ecx| {
242 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
243 })
244 }
245
246 fn consider_additional_alias_assumptions(
247 _ecx: &mut EvalCtxt<'_, D>,
248 _goal: Goal<I, Self>,
249 _alias_ty: ty::AliasTy<I>,
250 ) -> Vec<Candidate<I>> {
251 ::alloc::vec::Vec::new()vec![]
252 }
253
254 fn consider_impl_candidate(
255 ecx: &mut EvalCtxt<'_, D>,
256 goal: Goal<I, NormalizesTo<I>>,
257 impl_def_id: I::ImplId,
258 then: impl FnOnce(&mut EvalCtxt<'_, D>, Certainty) -> QueryResultOrRerunNonErased<I>,
259 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
260 let cx = ecx.cx();
261
262 let alias_def_id = goal.predicate.alias.expect_projection_def_id();
263 let goal_trait_ref = goal.predicate.alias.trait_ref(cx);
264 let impl_trait_ref = cx.impl_trait_ref(impl_def_id);
265 if !DeepRejectCtxt::relate_rigid_infer(ecx.cx()).args_may_unify(
266 goal.predicate.alias.trait_ref(cx).args,
267 impl_trait_ref.skip_binder().args,
268 ) {
269 return Err(NoSolution.into());
270 }
271
272 let impl_polarity = cx.impl_polarity(impl_def_id);
274 match impl_polarity {
275 ty::ImplPolarity::Negative => return Err(NoSolution.into()),
276 ty::ImplPolarity::Reservation => {
277 {
::core::panicking::panic_fmt(format_args!("not implemented: {0}",
format_args!("reservation impl for trait with assoc item: {0:?}",
goal)));
}unimplemented!("reservation impl for trait with assoc item: {:?}", goal)
278 }
279 ty::ImplPolarity::Positive => {}
280 };
281
282 ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
283 let impl_args = ecx.fresh_args_for_item(impl_def_id.into());
284 let impl_trait_ref = impl_trait_ref.instantiate(cx, impl_args).skip_norm_wip();
285
286 ecx.eq(goal.param_env, goal_trait_ref, impl_trait_ref)?;
287
288 let where_clause_bounds = cx
289 .predicates_of(impl_def_id.into())
290 .iter_instantiated(cx, impl_args)
291 .map(Unnormalized::skip_norm_wip)
292 .map(|pred| goal.with(cx, pred));
293 ecx.add_goals(GoalSource::ImplWhereBound, where_clause_bounds);
294
295 ecx.try_evaluate_added_goals()?;
300
301 ecx.add_goals(
305 GoalSource::AliasWellFormed,
306 cx.own_predicates_of(alias_def_id.into())
307 .iter_instantiated(cx, goal.predicate.alias.args)
308 .map(Unnormalized::skip_norm_wip)
309 .map(|pred| goal.with(cx, pred)),
310 );
311
312 let error_response = |ecx: &mut EvalCtxt<'_, D>, guar| {
313 let error_term = match goal.predicate.alias.kind {
314 ty::AliasTermKind::ProjectionTy { .. } => Ty::new_error(cx, guar).into(),
315 ty::AliasTermKind::ProjectionConst { .. } => Const::new_error(cx, guar).into(),
316 kind => {
::core::panicking::panic_fmt(format_args!("expected projection, found {0:?}",
kind));
}panic!("expected projection, found {kind:?}"),
317 };
318 ecx.instantiate_normalizes_to_term(goal, error_term);
319 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
320 };
321
322 let target_item_def_id =
323 match ecx.fetch_eligible_assoc_item(goal_trait_ref, alias_def_id, impl_def_id) {
324 FetchEligibleAssocItemResponse::Found(target_item_def_id) => target_item_def_id,
325 FetchEligibleAssocItemResponse::NotFound(tm) => {
326 match tm {
327 ty::TypingMode::Coherence => {
339 ecx.add_goal(
340 GoalSource::Misc,
341 goal.with(cx, PredicateKind::Ambiguous),
342 );
343 return ecx
344 .evaluate_added_goals_and_make_canonical_response(
345 Certainty::Yes,
346 )
347 .map_err(Into::into);
348 }
349 ty::TypingMode::Typeck { .. }
351 | ty::TypingMode::PostTypeckUntilBorrowck { .. }
352 | ty::TypingMode::PostBorrowck { .. }
353 | ty::TypingMode::PostAnalysis
354 | ty::TypingMode::Codegen => {
355 ecx.structurally_instantiate_normalizes_to_term(
356 goal,
357 goal.predicate.alias,
358 );
359 return ecx
360 .evaluate_added_goals_and_make_canonical_response(
361 Certainty::Yes,
362 )
363 .map_err(Into::into);
364 }
365 };
366 }
367 FetchEligibleAssocItemResponse::Err(guar) => return error_response(ecx, guar),
368 FetchEligibleAssocItemResponse::NotFoundBecauseErased => {
369 ecx.opaque_accesses.rerun_always(RerunReason::FetchEligibleAssocItem)?;
370 return Err(NoSolution.into());
371 }
372 };
373
374 if !cx.has_item_definition(target_item_def_id) {
375 if cx.impl_self_is_guaranteed_unsized(impl_def_id) {
381 if ecx.typing_mode().is_coherence() {
382 ecx.add_goal(GoalSource::Misc, goal.with(cx, PredicateKind::Ambiguous));
393 return then(ecx, Certainty::Yes).map_err(Into::into);
394 } else {
395 ecx.structurally_instantiate_normalizes_to_term(goal, goal.predicate.alias);
396 return then(ecx, Certainty::Yes).map_err(Into::into);
397 }
398 } else {
399 return error_response(ecx, cx.delay_bug("missing item"));
400 }
401 }
402
403 let target_container_def_id = cx.impl_or_trait_assoc_term_parent(target_item_def_id);
404
405 let target_args = ecx.translate_args(
416 goal,
417 impl_def_id,
418 impl_args,
419 impl_trait_ref,
420 target_container_def_id,
421 )?;
422
423 if !cx.check_args_compatible(target_item_def_id.into(), target_args) {
424 return error_response(
425 ecx,
426 cx.delay_bug("associated item has mismatched arguments"),
427 );
428 }
429
430 let term = match goal.predicate.alias.kind {
432 ty::AliasTermKind::ProjectionTy { .. } => cx
433 .type_of(target_item_def_id.into())
434 .instantiate(cx, target_args)
435 .skip_norm_wip()
436 .into(),
437 ty::AliasTermKind::ProjectionConst { .. }
438 if cx.is_type_const(target_item_def_id.into()) =>
439 {
440 cx.const_of_item(target_item_def_id.into())
441 .instantiate(cx, target_args)
442 .skip_norm_wip()
443 .into()
444 }
445 ty::AliasTermKind::ProjectionConst { .. } => {
446 let uv = ty::UnevaluatedConst::new(
447 cx,
448 ty::UnevaluatedConstKind::Projection {
449 def_id: target_item_def_id.into().try_into().unwrap(),
450 },
451 target_args,
452 );
453 return ecx.evaluate_const_and_instantiate_projection_term(
454 goal.param_env,
455 goal.predicate.alias,
456 goal.predicate.term,
457 uv,
458 );
459 }
460 kind => {
::core::panicking::panic_fmt(format_args!("expected projection, found {0:?}",
kind));
}panic!("expected projection, found {kind:?}"),
461 };
462
463 ecx.instantiate_normalizes_to_term(goal, term);
464 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes).map_err(Into::into)
465 })
466 }
467
468 fn consider_error_guaranteed_candidate(
471 ecx: &mut EvalCtxt<'_, D>,
472 goal: Goal<I, Self>,
473 guar: I::ErrorGuaranteed,
474 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
475 let cx = ecx.cx();
476 let error_term = match goal.predicate.alias.kind {
477 ty::AliasTermKind::ProjectionTy { .. } => Ty::new_error(cx, guar).into(),
478 ty::AliasTermKind::ProjectionConst { .. } => Const::new_error(cx, guar).into(),
479 kind => {
::core::panicking::panic_fmt(format_args!("expected projection, found {0:?}",
kind));
}panic!("expected projection, found {kind:?}"),
480 };
481
482 ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
483 ecx.instantiate_normalizes_to_term(goal, error_term);
484 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
485 })
486 }
487
488 fn consider_auto_trait_candidate(
489 ecx: &mut EvalCtxt<'_, D>,
490 _goal: Goal<I, Self>,
491 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
492 ecx.cx().delay_bug("associated types not allowed on auto traits");
493 Err(NoSolution.into())
494 }
495
496 fn consider_trait_alias_candidate(
497 _ecx: &mut EvalCtxt<'_, D>,
498 goal: Goal<I, Self>,
499 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
500 {
::core::panicking::panic_fmt(format_args!("trait aliases do not have associated types: {0:?}",
goal));
};panic!("trait aliases do not have associated types: {:?}", goal);
501 }
502
503 fn consider_builtin_sizedness_candidates(
504 _ecx: &mut EvalCtxt<'_, D>,
505 goal: Goal<I, Self>,
506 _sizedness: SizedTraitKind,
507 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
508 {
::core::panicking::panic_fmt(format_args!("`Sized`/`MetaSized` does not have an associated type: {0:?}",
goal));
};panic!("`Sized`/`MetaSized` does not have an associated type: {:?}", goal);
509 }
510
511 fn consider_builtin_copy_clone_candidate(
512 _ecx: &mut EvalCtxt<'_, D>,
513 goal: Goal<I, Self>,
514 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
515 {
::core::panicking::panic_fmt(format_args!("`Copy`/`Clone` does not have an associated type: {0:?}",
goal));
};panic!("`Copy`/`Clone` does not have an associated type: {:?}", goal);
516 }
517
518 fn consider_builtin_fn_ptr_trait_candidate(
519 _ecx: &mut EvalCtxt<'_, D>,
520 goal: Goal<I, Self>,
521 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
522 {
::core::panicking::panic_fmt(format_args!("`FnPtr` does not have an associated type: {0:?}",
goal));
};panic!("`FnPtr` does not have an associated type: {:?}", goal);
523 }
524
525 fn consider_builtin_fn_trait_candidates(
526 ecx: &mut EvalCtxt<'_, D>,
527 goal: Goal<I, Self>,
528 goal_kind: ty::ClosureKind,
529 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
530 let cx = ecx.cx();
531 let Some(tupled_inputs_and_output) =
532 structural_traits::extract_tupled_inputs_and_output_from_callable(
533 cx,
534 goal.predicate.self_ty(),
535 goal_kind,
536 )?
537 else {
538 return ecx.forced_ambiguity(MaybeInfo::AMBIGUOUS);
539 };
540 let (inputs, output) = ecx.instantiate_binder_with_infer(tupled_inputs_and_output);
541
542 let output_is_sized_pred =
545 ty::TraitRef::new(cx, cx.require_trait_lang_item(SolverTraitLangItem::Sized), [output]);
546
547 let pred = ty::ProjectionPredicate {
548 projection_term: ty::AliasTerm::new(
549 cx,
550 goal.predicate.alias.kind,
551 [goal.predicate.self_ty(), inputs],
552 ),
553 term: output.into(),
554 }
555 .upcast(cx);
556
557 Self::probe_and_consider_implied_clause(
558 ecx,
559 CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
560 goal,
561 pred,
562 [(GoalSource::ImplWhereBound, goal.with(cx, output_is_sized_pred))],
563 )
564 .map_err(Into::into)
565 }
566
567 fn consider_builtin_async_fn_trait_candidates(
568 ecx: &mut EvalCtxt<'_, D>,
569 goal: Goal<I, Self>,
570 goal_kind: ty::ClosureKind,
571 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
572 let cx = ecx.cx();
573 let def_id = goal.predicate.alias.expect_projection_ty_def_id();
574
575 let env_region = match goal_kind {
576 ty::ClosureKind::Fn | ty::ClosureKind::FnMut => goal.predicate.alias.args.region_at(2),
577 ty::ClosureKind::FnOnce => Region::new_static(cx),
579 };
580 let (tupled_inputs_and_output_and_coroutine, nested_preds) =
581 structural_traits::extract_tupled_inputs_and_output_from_async_callable(
582 cx,
583 goal.predicate.self_ty(),
584 goal_kind,
585 env_region,
586 )?;
587 let AsyncCallableRelevantTypes {
588 tupled_inputs_ty,
589 output_coroutine_ty,
590 coroutine_return_ty,
591 } = ecx.instantiate_binder_with_infer(tupled_inputs_and_output_and_coroutine);
592
593 let output_is_sized_pred = ty::TraitRef::new(
596 cx,
597 cx.require_trait_lang_item(SolverTraitLangItem::Sized),
598 [output_coroutine_ty],
599 );
600
601 let (projection_term, term) = if cx
602 .is_projection_lang_item(def_id, SolverProjectionLangItem::CallOnceFuture)
603 {
604 (
605 ty::AliasTerm::new(
606 cx,
607 goal.predicate.alias.kind,
608 [goal.predicate.self_ty(), tupled_inputs_ty],
609 ),
610 output_coroutine_ty.into(),
611 )
612 } else if cx.is_projection_lang_item(def_id, SolverProjectionLangItem::CallRefFuture) {
613 (
614 ty::AliasTerm::new(
615 cx,
616 goal.predicate.alias.kind,
617 [
618 I::GenericArg::from(goal.predicate.self_ty()),
619 tupled_inputs_ty.into(),
620 env_region.into(),
621 ],
622 ),
623 output_coroutine_ty.into(),
624 )
625 } else if cx.is_projection_lang_item(def_id, SolverProjectionLangItem::AsyncFnOnceOutput) {
626 (
627 ty::AliasTerm::new(
628 cx,
629 goal.predicate.alias.kind,
630 [goal.predicate.self_ty(), tupled_inputs_ty],
631 ),
632 coroutine_return_ty.into(),
633 )
634 } else {
635 {
::core::panicking::panic_fmt(format_args!("no such associated type in `AsyncFn*`: {0:?}",
def_id));
}panic!("no such associated type in `AsyncFn*`: {:?}", def_id)
636 };
637 let pred = ty::ProjectionPredicate { projection_term, term }.upcast(cx);
638
639 Self::probe_and_consider_implied_clause(
640 ecx,
641 CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
642 goal,
643 pred,
644 [goal.with(cx, output_is_sized_pred)]
645 .into_iter()
646 .chain(nested_preds.into_iter().map(|pred| goal.with(cx, pred)))
647 .map(|goal| (GoalSource::ImplWhereBound, goal)),
648 )
649 }
650
651 fn consider_builtin_async_fn_kind_helper_candidate(
652 ecx: &mut EvalCtxt<'_, D>,
653 goal: Goal<I, Self>,
654 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
655 let [
656 closure_fn_kind_ty,
657 goal_kind_ty,
658 borrow_region,
659 tupled_inputs_ty,
660 tupled_upvars_ty,
661 coroutine_captures_by_ref_ty,
662 ] = *goal.predicate.alias.args.as_slice()
663 else {
664 ::core::panicking::panic("explicit panic");panic!();
665 };
666
667 if tupled_upvars_ty.expect_ty().is_ty_var() {
669 return ecx.forced_ambiguity(MaybeInfo::AMBIGUOUS);
670 }
671
672 let Some(closure_kind) = closure_fn_kind_ty.expect_ty().to_opt_closure_kind() else {
673 return Err(NoSolution.into());
675 };
676 let Some(goal_kind) = goal_kind_ty.expect_ty().to_opt_closure_kind() else {
677 return Err(NoSolution.into());
678 };
679 if !closure_kind.extends(goal_kind) {
680 return Err(NoSolution.into());
681 }
682
683 let upvars_ty = ty::CoroutineClosureSignature::tupled_upvars_by_closure_kind(
684 ecx.cx(),
685 goal_kind,
686 tupled_inputs_ty.expect_ty(),
687 tupled_upvars_ty.expect_ty(),
688 coroutine_captures_by_ref_ty.expect_ty(),
689 borrow_region.expect_region(),
690 );
691
692 ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
693 ecx.instantiate_normalizes_to_term(goal, upvars_ty.into());
694 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
695 })
696 }
697
698 fn consider_builtin_tuple_candidate(
699 _ecx: &mut EvalCtxt<'_, D>,
700 goal: Goal<I, Self>,
701 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
702 {
::core::panicking::panic_fmt(format_args!("`Tuple` does not have an associated type: {0:?}",
goal));
};panic!("`Tuple` does not have an associated type: {:?}", goal);
703 }
704
705 fn consider_builtin_pointee_candidate(
706 ecx: &mut EvalCtxt<'_, D>,
707 goal: Goal<I, Self>,
708 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
709 let cx = ecx.cx();
710 let metadata_def_id = cx.require_projection_lang_item(SolverProjectionLangItem::Metadata);
711 match (&ty::AliasTermKind::ProjectionTy { def_id: metadata_def_id },
&goal.predicate.alias.kind) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(
712 ty::AliasTermKind::ProjectionTy { def_id: metadata_def_id },
713 goal.predicate.alias.kind
714 );
715 let metadata_ty = match goal.predicate.self_ty().kind() {
716 ty::Bool
717 | ty::Char
718 | ty::Int(..)
719 | ty::Uint(..)
720 | ty::Float(..)
721 | ty::Array(..)
722 | ty::Pat(..)
723 | ty::RawPtr(..)
724 | ty::Ref(..)
725 | ty::FnDef(..)
726 | ty::FnPtr(..)
727 | ty::Closure(..)
728 | ty::CoroutineClosure(..)
729 | ty::Infer(ty::IntVar(..) | ty::FloatVar(..))
730 | ty::Coroutine(..)
731 | ty::CoroutineWitness(..)
732 | ty::Never
733 | ty::Foreign(..) => Ty::new_unit(cx),
734
735 ty::Error(e) => Ty::new_error(cx, e),
736
737 ty::Str | ty::Slice(_) => Ty::new_usize(cx),
738
739 ty::Dynamic(_, _) => {
740 let dyn_metadata = cx.require_adt_lang_item(SolverAdtLangItem::DynMetadata);
741 cx.type_of(dyn_metadata.into())
742 .instantiate(cx, &[I::GenericArg::from(goal.predicate.self_ty())])
743 .skip_norm_wip()
744 }
745
746 ty::Alias(_) | ty::Param(_) | ty::Placeholder(..) => {
747 let alias_bound_result =
752 ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
753 let sized_predicate = ty::TraitRef::new(
754 cx,
755 cx.require_trait_lang_item(SolverTraitLangItem::Sized),
756 [I::GenericArg::from(goal.predicate.self_ty())],
757 );
758 ecx.add_goal(GoalSource::Misc, goal.with(cx, sized_predicate));
759 ecx.instantiate_normalizes_to_term(goal, Ty::new_unit(cx).into());
760 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
761 });
762
763 let alias_bound_result = match alias_bound_result.map_err_to_rerun()? {
764 Ok(i) => Ok(i),
765 Err(NoSolution) => Err(NoSolution),
766 };
767
768 return alias_bound_result.or_else(|NoSolution| {
771 ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|this| {
772 this.structurally_instantiate_normalizes_to_term(
773 goal,
774 goal.predicate.alias,
775 );
776 this.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
777 })
778 });
779 }
780
781 ty::Adt(def, args) if def.is_struct() => match def.struct_tail_ty(cx) {
782 None => Ty::new_unit(cx),
783 Some(tail_ty) => Ty::new_projection(
784 cx,
785 metadata_def_id,
786 [tail_ty.instantiate(cx, args).skip_norm_wip()],
787 ),
788 },
789 ty::Adt(_, _) => Ty::new_unit(cx),
790
791 ty::Tuple(elements) => match elements.last() {
792 None => Ty::new_unit(cx),
793 Some(tail_ty) => Ty::new_projection(cx, metadata_def_id, [tail_ty]),
794 },
795
796 ty::UnsafeBinder(_) => {
797 ::core::panicking::panic("not yet implemented")todo!()
799 }
800
801 ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
802 | ty::Bound(..) => {
::core::panicking::panic_fmt(format_args!("unexpected self ty `{0:?}` when normalizing `<T as Pointee>::Metadata`",
goal.predicate.self_ty()));
}panic!(
803 "unexpected self ty `{:?}` when normalizing `<T as Pointee>::Metadata`",
804 goal.predicate.self_ty()
805 ),
806 };
807
808 ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
809 ecx.instantiate_normalizes_to_term(goal, metadata_ty.into());
810 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
811 })
812 }
813
814 fn consider_builtin_future_candidate(
815 ecx: &mut EvalCtxt<'_, D>,
816 goal: Goal<I, Self>,
817 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
818 let self_ty = goal.predicate.self_ty();
819 let ty::Coroutine(def_id, args) = self_ty.kind() else {
820 return Err(NoSolution.into());
821 };
822
823 let cx = ecx.cx();
825 if !cx.coroutine_is_async(def_id) {
826 return Err(NoSolution.into());
827 }
828
829 let term = args.as_coroutine().return_ty().into();
830
831 Self::probe_and_consider_implied_clause(
832 ecx,
833 CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
834 goal,
835 ty::ProjectionPredicate {
836 projection_term: ty::AliasTerm::new(ecx.cx(), goal.predicate.alias.kind, [self_ty]),
837 term,
838 }
839 .upcast(cx),
840 [],
843 )
844 }
845
846 fn consider_builtin_iterator_candidate(
847 ecx: &mut EvalCtxt<'_, D>,
848 goal: Goal<I, Self>,
849 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
850 let self_ty = goal.predicate.self_ty();
851 let ty::Coroutine(def_id, args) = self_ty.kind() else {
852 return Err(NoSolution.into());
853 };
854
855 let cx = ecx.cx();
857 if !cx.coroutine_is_gen(def_id) {
858 return Err(NoSolution.into());
859 }
860
861 let term = args.as_coroutine().yield_ty().into();
862
863 Self::probe_and_consider_implied_clause(
864 ecx,
865 CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
866 goal,
867 ty::ProjectionPredicate {
868 projection_term: ty::AliasTerm::new(ecx.cx(), goal.predicate.alias.kind, [self_ty]),
869 term,
870 }
871 .upcast(cx),
872 [],
875 )
876 .map_err(Into::into)
877 }
878
879 fn consider_builtin_fused_iterator_candidate(
880 _ecx: &mut EvalCtxt<'_, D>,
881 goal: Goal<I, Self>,
882 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
883 {
::core::panicking::panic_fmt(format_args!("`FusedIterator` does not have an associated type: {0:?}",
goal));
};panic!("`FusedIterator` does not have an associated type: {:?}", goal);
884 }
885
886 fn consider_builtin_async_iterator_candidate(
887 ecx: &mut EvalCtxt<'_, D>,
888 goal: Goal<I, Self>,
889 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
890 let self_ty = goal.predicate.self_ty();
891 let ty::Coroutine(def_id, args) = self_ty.kind() else {
892 return Err(NoSolution.into());
893 };
894
895 let cx = ecx.cx();
897 if !cx.coroutine_is_async_gen(def_id) {
898 return Err(NoSolution.into());
899 }
900
901 ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
902 let expected_ty = ecx.next_ty_infer();
903 let wrapped_expected_ty = Ty::new_adt(
906 cx,
907 cx.adt_def(cx.require_adt_lang_item(SolverAdtLangItem::Poll)),
908 cx.mk_args(&[Ty::new_adt(
909 cx,
910 cx.adt_def(cx.require_adt_lang_item(SolverAdtLangItem::Option)),
911 cx.mk_args(&[expected_ty.into()]),
912 )
913 .into()]),
914 );
915 let yield_ty = args.as_coroutine().yield_ty();
916 ecx.eq(goal.param_env, wrapped_expected_ty, yield_ty)?;
917 ecx.instantiate_normalizes_to_term(goal, expected_ty.into());
918 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
919 })
920 }
921
922 fn consider_builtin_coroutine_candidate(
923 ecx: &mut EvalCtxt<'_, D>,
924 goal: Goal<I, Self>,
925 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
926 let self_ty = goal.predicate.self_ty();
927 let ty::Coroutine(def_id, args) = self_ty.kind() else {
928 return Err(NoSolution.into());
929 };
930
931 let cx = ecx.cx();
933 if !cx.is_general_coroutine(def_id) {
934 return Err(NoSolution.into());
935 }
936
937 let coroutine = args.as_coroutine();
938 let def_id = goal.predicate.alias.expect_projection_ty_def_id();
939
940 let term = if cx.is_projection_lang_item(def_id, SolverProjectionLangItem::CoroutineReturn)
941 {
942 coroutine.return_ty().into()
943 } else if cx.is_projection_lang_item(def_id, SolverProjectionLangItem::CoroutineYield) {
944 coroutine.yield_ty().into()
945 } else {
946 {
::core::panicking::panic_fmt(format_args!("unexpected associated item `{0:?}` for `{1:?}`",
def_id, self_ty));
}panic!("unexpected associated item `{:?}` for `{self_ty:?}`", def_id)
947 };
948
949 Self::probe_and_consider_implied_clause(
950 ecx,
951 CandidateSource::BuiltinImpl(BuiltinImplSource::Misc),
952 goal,
953 ty::ProjectionPredicate {
954 projection_term: ty::AliasTerm::new(
955 ecx.cx(),
956 goal.predicate.alias.kind,
957 [self_ty, coroutine.resume_ty()],
958 ),
959 term,
960 }
961 .upcast(cx),
962 [],
965 )
966 }
967
968 fn consider_structural_builtin_unsize_candidates(
969 _ecx: &mut EvalCtxt<'_, D>,
970 goal: Goal<I, Self>,
971 ) -> Result<Vec<Candidate<I>>, RerunNonErased> {
972 {
::core::panicking::panic_fmt(format_args!("`Unsize` does not have an associated type: {0:?}",
goal));
};panic!("`Unsize` does not have an associated type: {:?}", goal);
973 }
974
975 fn consider_builtin_discriminant_kind_candidate(
976 ecx: &mut EvalCtxt<'_, D>,
977 goal: Goal<I, Self>,
978 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
979 let self_ty = goal.predicate.self_ty();
980 let discriminant_ty = match self_ty.kind() {
981 ty::Bool
982 | ty::Char
983 | ty::Int(..)
984 | ty::Uint(..)
985 | ty::Float(..)
986 | ty::Array(..)
987 | ty::Pat(..)
988 | ty::RawPtr(..)
989 | ty::Ref(..)
990 | ty::FnDef(..)
991 | ty::FnPtr(..)
992 | ty::Closure(..)
993 | ty::CoroutineClosure(..)
994 | ty::Infer(ty::IntVar(..) | ty::FloatVar(..))
995 | ty::Coroutine(..)
996 | ty::CoroutineWitness(..)
997 | ty::Never
998 | ty::Foreign(..)
999 | ty::Adt(_, _)
1000 | ty::Str
1001 | ty::Slice(_)
1002 | ty::Dynamic(_, _)
1003 | ty::Tuple(_)
1004 | ty::Error(_) => self_ty.discriminant_ty(ecx.cx()),
1005
1006 ty::UnsafeBinder(_) => {
1007 {
::core::panicking::panic_fmt(format_args!("not yet implemented: {0}",
format_args!("discr subgoal...")));
}todo!("discr subgoal...")
1009 }
1010
1011 ty::Alias(_) | ty::Param(_) | ty::Placeholder(..) => {
1015 return ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
1016 ecx.structurally_instantiate_normalizes_to_term(goal, goal.predicate.alias);
1017 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
1018 });
1019 }
1020
1021 ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
1022 | ty::Bound(..) => {
::core::panicking::panic_fmt(format_args!("unexpected self ty `{0:?}` when normalizing `<T as DiscriminantKind>::Discriminant`",
goal.predicate.self_ty()));
}panic!(
1023 "unexpected self ty `{:?}` when normalizing `<T as DiscriminantKind>::Discriminant`",
1024 goal.predicate.self_ty()
1025 ),
1026 };
1027
1028 ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
1029 ecx.instantiate_normalizes_to_term(goal, discriminant_ty.into());
1030 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
1031 })
1032 }
1033
1034 fn consider_builtin_destruct_candidate(
1035 _ecx: &mut EvalCtxt<'_, D>,
1036 goal: Goal<I, Self>,
1037 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
1038 {
::core::panicking::panic_fmt(format_args!("`Destruct` does not have an associated type: {0:?}",
goal));
};panic!("`Destruct` does not have an associated type: {:?}", goal);
1039 }
1040
1041 fn consider_builtin_transmute_candidate(
1042 _ecx: &mut EvalCtxt<'_, D>,
1043 goal: Goal<I, Self>,
1044 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
1045 {
::core::panicking::panic_fmt(format_args!("`TransmuteFrom` does not have an associated type: {0:?}",
goal));
}panic!("`TransmuteFrom` does not have an associated type: {:?}", goal)
1046 }
1047
1048 fn consider_builtin_bikeshed_guaranteed_no_drop_candidate(
1049 _ecx: &mut EvalCtxt<'_, D>,
1050 goal: Goal<I, Self>,
1051 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
1052 {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("`BikeshedGuaranteedNoDrop` does not have an associated type: {0:?}",
goal)));
}unreachable!("`BikeshedGuaranteedNoDrop` does not have an associated type: {:?}", goal)
1053 }
1054
1055 fn consider_builtin_field_candidate(
1056 ecx: &mut EvalCtxt<'_, D>,
1057 goal: Goal<I, Self>,
1058 ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
1059 let self_ty = goal.predicate.self_ty();
1060 let ty::Adt(def, args) = self_ty.kind() else {
1061 return Err(NoSolution.into());
1062 };
1063 let Some(FieldInfo { base, ty, .. }) = def.field_representing_type_info(ecx.cx(), args)
1064 else {
1065 return Err(NoSolution.into());
1066 };
1067 let def_id = goal.predicate.alias.expect_projection_ty_def_id();
1068 let ty = match ecx.cx().as_projection_lang_item(def_id) {
1069 Some(SolverProjectionLangItem::FieldBase) => base,
1070 Some(SolverProjectionLangItem::FieldType) => ty,
1071 _ => {
::core::panicking::panic_fmt(format_args!("unexpected associated type {0:?} in `Field`",
goal.predicate));
}panic!("unexpected associated type {:?} in `Field`", goal.predicate),
1072 };
1073 ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
1074 ecx.instantiate_normalizes_to_term(goal, ty.into());
1075 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
1076 })
1077 }
1078}
1079
1080impl<D, I> EvalCtxt<'_, D>
1081where
1082 D: SolverDelegate<Interner = I>,
1083 I: Interner,
1084{
1085 fn translate_args(
1086 &mut self,
1087 goal: Goal<I, ty::NormalizesTo<I>>,
1088 impl_def_id: I::ImplId,
1089 impl_args: I::GenericArgs,
1090 impl_trait_ref: rustc_type_ir::TraitRef<I>,
1091 target_container_def_id: I::DefId,
1092 ) -> Result<I::GenericArgs, NoSolution> {
1093 let cx = self.cx();
1094 Ok(if target_container_def_id == impl_trait_ref.def_id.into() {
1095 goal.predicate.alias.args
1097 } else if target_container_def_id == impl_def_id.into() {
1098 goal.predicate.alias.args.rebase_onto(cx, impl_trait_ref.def_id.into(), impl_args)
1101 } else {
1102 let target_args = self.fresh_args_for_item(target_container_def_id);
1103 let target_trait_ref = cx
1104 .impl_trait_ref(target_container_def_id.try_into().unwrap())
1105 .instantiate(cx, target_args)
1106 .skip_norm_wip();
1107 self.eq(goal.param_env, impl_trait_ref, target_trait_ref)?;
1109 self.add_goals(
1112 GoalSource::Misc,
1113 cx.predicates_of(target_container_def_id)
1114 .iter_instantiated(cx, target_args)
1115 .map(Unnormalized::skip_norm_wip)
1116 .map(|pred| goal.with(cx, pred)),
1117 );
1118 goal.predicate.alias.args.rebase_onto(cx, impl_trait_ref.def_id.into(), target_args)
1119 })
1120 }
1121}