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