1use std::mem;
2use std::ops::ControlFlow;
3
4#[cfg(feature = "nightly")]
5use rustc_macros::HashStable_NoContext;
6use rustc_type_ir::data_structures::{HashMap, HashSet};
7use rustc_type_ir::inherent::*;
8use rustc_type_ir::relate::Relate;
9use rustc_type_ir::relate::solver_relating::RelateExt;
10use rustc_type_ir::search_graph::{CandidateHeadUsages, PathKind};
11use rustc_type_ir::{
12 self as ty, CanonicalVarValues, InferCtxtLike, Interner, TypeFoldable, TypeFolder,
13 TypeSuperFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
14 TypingMode,
15};
16use tracing::{debug, instrument, trace};
17
18use super::has_only_region_constraints;
19use crate::coherence;
20use crate::delegate::SolverDelegate;
21use crate::placeholder::BoundVarReplacer;
22use crate::resolve::eager_resolve_vars;
23use crate::solve::search_graph::SearchGraph;
24use crate::solve::ty::may_use_unstable_feature;
25use crate::solve::{
26 CanonicalInput, Certainty, FIXPOINT_STEP_LIMIT, Goal, GoalEvaluation, GoalSource,
27 GoalStalledOn, HasChanged, NestedNormalizationGoals, NoSolution, QueryInput, QueryResult,
28 inspect,
29};
30
31pub(super) mod canonical;
32mod probe;
33
34#[derive(Debug, Copy, Clone)]
39enum CurrentGoalKind {
40 Misc,
41 CoinductiveTrait,
46 NormalizesTo,
54}
55
56impl CurrentGoalKind {
57 fn from_query_input<I: Interner>(cx: I, input: QueryInput<I, I::Predicate>) -> CurrentGoalKind {
58 match input.goal.predicate.kind().skip_binder() {
59 ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
60 if cx.trait_is_coinductive(pred.trait_ref.def_id) {
61 CurrentGoalKind::CoinductiveTrait
62 } else {
63 CurrentGoalKind::Misc
64 }
65 }
66 ty::PredicateKind::NormalizesTo(_) => CurrentGoalKind::NormalizesTo,
67 _ => CurrentGoalKind::Misc,
68 }
69 }
70}
71
72pub struct EvalCtxt<'a, D, I = <D as SolverDelegate>::Interner>
73where
74 D: SolverDelegate<Interner = I>,
75 I: Interner,
76{
77 delegate: &'a D,
93
94 variables: I::CanonicalVarKinds,
97
98 current_goal_kind: CurrentGoalKind,
101 pub(super) var_values: CanonicalVarValues<I>,
102
103 pub(super) max_input_universe: ty::UniverseIndex,
113 pub(super) initial_opaque_types_storage_num_entries:
116 <D::Infcx as InferCtxtLike>::OpaqueTypeStorageEntries,
117
118 pub(super) search_graph: &'a mut SearchGraph<D>,
119
120 nested_goals: Vec<(GoalSource, Goal<I, I::Predicate>, Option<GoalStalledOn<I>>)>,
121
122 pub(super) origin_span: I::Span,
123
124 tainted: Result<(), NoSolution>,
131
132 pub(super) inspect: inspect::EvaluationStepBuilder<D>,
133}
134
135#[derive(PartialEq, Eq, Debug, Hash, Clone, Copy)]
136#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
137pub enum GenerateProofTree {
138 Yes,
139 No,
140}
141
142pub trait SolverDelegateEvalExt: SolverDelegate {
143 fn evaluate_root_goal(
148 &self,
149 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
150 span: <Self::Interner as Interner>::Span,
151 stalled_on: Option<GoalStalledOn<Self::Interner>>,
152 ) -> Result<GoalEvaluation<Self::Interner>, NoSolution>;
153
154 fn root_goal_may_hold_with_depth(
162 &self,
163 root_depth: usize,
164 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
165 ) -> bool;
166
167 fn evaluate_root_goal_for_proof_tree(
170 &self,
171 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
172 span: <Self::Interner as Interner>::Span,
173 ) -> (
174 Result<NestedNormalizationGoals<Self::Interner>, NoSolution>,
175 inspect::GoalEvaluation<Self::Interner>,
176 );
177}
178
179impl<D, I> SolverDelegateEvalExt for D
180where
181 D: SolverDelegate<Interner = I>,
182 I: Interner,
183{
184 #[instrument(level = "debug", skip(self))]
185 fn evaluate_root_goal(
186 &self,
187 goal: Goal<I, I::Predicate>,
188 span: I::Span,
189 stalled_on: Option<GoalStalledOn<I>>,
190 ) -> Result<GoalEvaluation<I>, NoSolution> {
191 EvalCtxt::enter_root(self, self.cx().recursion_limit(), span, |ecx| {
192 ecx.evaluate_goal(GoalSource::Misc, goal, stalled_on)
193 })
194 }
195
196 fn root_goal_may_hold_with_depth(
197 &self,
198 root_depth: usize,
199 goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
200 ) -> bool {
201 self.probe(|| {
202 EvalCtxt::enter_root(self, root_depth, I::Span::dummy(), |ecx| {
203 ecx.evaluate_goal(GoalSource::Misc, goal, None)
204 })
205 })
206 .is_ok()
207 }
208
209 #[instrument(level = "debug", skip(self))]
210 fn evaluate_root_goal_for_proof_tree(
211 &self,
212 goal: Goal<I, I::Predicate>,
213 span: I::Span,
214 ) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
215 evaluate_root_goal_for_proof_tree(self, goal, span)
216 }
217}
218
219impl<'a, D, I> EvalCtxt<'a, D>
220where
221 D: SolverDelegate<Interner = I>,
222 I: Interner,
223{
224 pub(super) fn typing_mode(&self) -> TypingMode<I> {
225 self.delegate.typing_mode()
226 }
227
228 pub(super) fn step_kind_for_source(&self, source: GoalSource) -> PathKind {
237 match source {
238 GoalSource::Misc => PathKind::Unknown,
246 GoalSource::NormalizeGoal(path_kind) => path_kind,
247 GoalSource::ImplWhereBound => match self.current_goal_kind {
248 CurrentGoalKind::CoinductiveTrait => PathKind::Coinductive,
251 CurrentGoalKind::NormalizesTo => PathKind::Inductive,
259 CurrentGoalKind::Misc => PathKind::Unknown,
263 },
264 GoalSource::TypeRelating => PathKind::Inductive,
268 GoalSource::InstantiateHigherRanked => PathKind::Inductive,
271 GoalSource::AliasBoundConstCondition | GoalSource::AliasWellFormed => PathKind::Unknown,
275 }
276 }
277
278 pub(super) fn enter_root<R>(
282 delegate: &D,
283 root_depth: usize,
284 origin_span: I::Span,
285 f: impl FnOnce(&mut EvalCtxt<'_, D>) -> R,
286 ) -> R {
287 let mut search_graph = SearchGraph::new(root_depth);
288
289 let mut ecx = EvalCtxt {
290 delegate,
291 search_graph: &mut search_graph,
292 nested_goals: Default::default(),
293 inspect: inspect::EvaluationStepBuilder::new_noop(),
294
295 max_input_universe: ty::UniverseIndex::ROOT,
298 initial_opaque_types_storage_num_entries: Default::default(),
299 variables: Default::default(),
300 var_values: CanonicalVarValues::dummy(),
301 current_goal_kind: CurrentGoalKind::Misc,
302 origin_span,
303 tainted: Ok(()),
304 };
305 let result = f(&mut ecx);
306 assert!(
307 ecx.nested_goals.is_empty(),
308 "root `EvalCtxt` should not have any goals added to it"
309 );
310 assert!(search_graph.is_empty());
311 result
312 }
313
314 pub(super) fn enter_canonical<R>(
322 cx: I,
323 search_graph: &'a mut SearchGraph<D>,
324 canonical_input: CanonicalInput<I>,
325 proof_tree_builder: &mut inspect::ProofTreeBuilder<D>,
326 f: impl FnOnce(&mut EvalCtxt<'_, D>, Goal<I, I::Predicate>) -> R,
327 ) -> R {
328 let (ref delegate, input, var_values) = D::build_with_canonical(cx, &canonical_input);
329 for &(key, ty) in &input.predefined_opaques_in_body.opaque_types {
330 let prev = delegate.register_hidden_type_in_storage(key, ty, I::Span::dummy());
331 if let Some(prev) = prev {
343 debug!(?key, ?ty, ?prev, "ignore duplicate in `opaque_types_storage`");
344 }
345 }
346
347 let initial_opaque_types_storage_num_entries = delegate.opaque_types_storage_num_entries();
348 let mut ecx = EvalCtxt {
349 delegate,
350 variables: canonical_input.canonical.variables,
351 var_values,
352 current_goal_kind: CurrentGoalKind::from_query_input(cx, input),
353 max_input_universe: canonical_input.canonical.max_universe,
354 initial_opaque_types_storage_num_entries,
355 search_graph,
356 nested_goals: Default::default(),
357 origin_span: I::Span::dummy(),
358 tainted: Ok(()),
359 inspect: proof_tree_builder.new_evaluation_step(var_values),
360 };
361
362 let result = f(&mut ecx, input.goal);
363 ecx.inspect.probe_final_state(ecx.delegate, ecx.max_input_universe);
364 proof_tree_builder.finish_evaluation_step(ecx.inspect);
365
366 delegate.reset_opaque_types();
372
373 result
374 }
375
376 pub(super) fn ignore_candidate_head_usages(&mut self, usages: CandidateHeadUsages) {
377 self.search_graph.ignore_candidate_head_usages(usages);
378 }
379
380 fn evaluate_goal(
383 &mut self,
384 source: GoalSource,
385 goal: Goal<I, I::Predicate>,
386 stalled_on: Option<GoalStalledOn<I>>,
387 ) -> Result<GoalEvaluation<I>, NoSolution> {
388 let (normalization_nested_goals, goal_evaluation) =
389 self.evaluate_goal_raw(source, goal, stalled_on)?;
390 assert!(normalization_nested_goals.is_empty());
391 Ok(goal_evaluation)
392 }
393
394 pub(super) fn evaluate_goal_raw(
402 &mut self,
403 source: GoalSource,
404 goal: Goal<I, I::Predicate>,
405 stalled_on: Option<GoalStalledOn<I>>,
406 ) -> Result<(NestedNormalizationGoals<I>, GoalEvaluation<I>), NoSolution> {
407 if let Some(GoalStalledOn { num_opaques, ref stalled_vars, ref sub_roots, stalled_cause }) =
411 stalled_on
412 && !stalled_vars.iter().any(|value| self.delegate.is_changed_arg(*value))
413 && !sub_roots
414 .iter()
415 .any(|&vid| self.delegate.sub_unification_table_root_var(vid) != vid)
416 && !self.delegate.opaque_types_storage_num_entries().needs_reevaluation(num_opaques)
417 {
418 return Ok((
419 NestedNormalizationGoals::empty(),
420 GoalEvaluation {
421 goal,
422 certainty: Certainty::Maybe(stalled_cause),
423 has_changed: HasChanged::No,
424 stalled_on,
425 },
426 ));
427 }
428
429 let opaque_types = self.delegate.clone_opaque_types_lookup_table();
433 let (goal, opaque_types) = eager_resolve_vars(self.delegate, (goal, opaque_types));
434
435 let (orig_values, canonical_goal) =
436 Self::canonicalize_goal(self.delegate, goal, opaque_types);
437 let canonical_result = self.search_graph.evaluate_goal(
438 self.cx(),
439 canonical_goal,
440 self.step_kind_for_source(source),
441 &mut inspect::ProofTreeBuilder::new_noop(),
442 );
443 let response = match canonical_result {
444 Err(e) => return Err(e),
445 Ok(response) => response,
446 };
447
448 let has_changed =
449 if !has_only_region_constraints(response) { HasChanged::Yes } else { HasChanged::No };
450
451 let (normalization_nested_goals, certainty) = Self::instantiate_and_apply_query_response(
452 self.delegate,
453 goal.param_env,
454 &orig_values,
455 response,
456 self.origin_span,
457 );
458
459 let stalled_on = match certainty {
470 Certainty::Yes => None,
471 Certainty::Maybe(stalled_cause) => match has_changed {
472 HasChanged::Yes => None,
477 HasChanged::No => {
478 let mut stalled_vars = orig_values;
479
480 if let Some(normalizes_to) = goal.predicate.as_normalizes_to() {
482 let normalizes_to = normalizes_to.skip_binder();
483 let rhs_arg: I::GenericArg = normalizes_to.term.into();
484 let idx = stalled_vars
485 .iter()
486 .rposition(|arg| *arg == rhs_arg)
487 .expect("expected unconstrained arg");
488 stalled_vars.swap_remove(idx);
489 }
490
491 let mut sub_roots = Vec::new();
493 stalled_vars.retain(|arg| match arg.kind() {
494 ty::GenericArgKind::Lifetime(_) => false,
496 ty::GenericArgKind::Type(ty) => match ty.kind() {
497 ty::Infer(ty::TyVar(vid)) => {
498 sub_roots.push(self.delegate.sub_unification_table_root_var(vid));
499 true
500 }
501 ty::Infer(_) => true,
502 ty::Param(_) | ty::Placeholder(_) => false,
503 _ => unreachable!("unexpected orig_value: {ty:?}"),
504 },
505 ty::GenericArgKind::Const(ct) => match ct.kind() {
506 ty::ConstKind::Infer(_) => true,
507 ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(_) => false,
508 _ => unreachable!("unexpected orig_value: {ct:?}"),
509 },
510 });
511
512 Some(GoalStalledOn {
513 num_opaques: canonical_goal
514 .canonical
515 .value
516 .predefined_opaques_in_body
517 .opaque_types
518 .len(),
519 stalled_vars,
520 sub_roots,
521 stalled_cause,
522 })
523 }
524 },
525 };
526
527 Ok((
528 normalization_nested_goals,
529 GoalEvaluation { goal, certainty, has_changed, stalled_on },
530 ))
531 }
532
533 pub(super) fn compute_goal(&mut self, goal: Goal<I, I::Predicate>) -> QueryResult<I> {
534 let Goal { param_env, predicate } = goal;
535 let kind = predicate.kind();
536 if let Some(kind) = kind.no_bound_vars() {
537 match kind {
538 ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
539 self.compute_trait_goal(Goal { param_env, predicate }).map(|(r, _via)| r)
540 }
541 ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
542 self.compute_host_effect_goal(Goal { param_env, predicate })
543 }
544 ty::PredicateKind::Clause(ty::ClauseKind::Projection(predicate)) => {
545 self.compute_projection_goal(Goal { param_env, predicate })
546 }
547 ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(predicate)) => {
548 self.compute_type_outlives_goal(Goal { param_env, predicate })
549 }
550 ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(predicate)) => {
551 self.compute_region_outlives_goal(Goal { param_env, predicate })
552 }
553 ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
554 self.compute_const_arg_has_type_goal(Goal { param_env, predicate: (ct, ty) })
555 }
556 ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature(symbol)) => {
557 self.compute_unstable_feature_goal(param_env, symbol)
558 }
559 ty::PredicateKind::Subtype(predicate) => {
560 self.compute_subtype_goal(Goal { param_env, predicate })
561 }
562 ty::PredicateKind::Coerce(predicate) => {
563 self.compute_coerce_goal(Goal { param_env, predicate })
564 }
565 ty::PredicateKind::DynCompatible(trait_def_id) => {
566 self.compute_dyn_compatible_goal(trait_def_id)
567 }
568 ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
569 self.compute_well_formed_goal(Goal { param_env, predicate: term })
570 }
571 ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
572 self.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
573 }
574 ty::PredicateKind::ConstEquate(_, _) => {
575 panic!("ConstEquate should not be emitted when `-Znext-solver` is active")
576 }
577 ty::PredicateKind::NormalizesTo(predicate) => {
578 self.compute_normalizes_to_goal(Goal { param_env, predicate })
579 }
580 ty::PredicateKind::AliasRelate(lhs, rhs, direction) => self
581 .compute_alias_relate_goal(Goal {
582 param_env,
583 predicate: (lhs, rhs, direction),
584 }),
585 ty::PredicateKind::Ambiguous => {
586 self.evaluate_added_goals_and_make_canonical_response(Certainty::AMBIGUOUS)
587 }
588 }
589 } else {
590 self.enter_forall(kind, |ecx, kind| {
591 let goal = goal.with(ecx.cx(), ty::Binder::dummy(kind));
592 ecx.add_goal(GoalSource::InstantiateHigherRanked, goal);
593 ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
594 })
595 }
596 }
597
598 #[instrument(level = "trace", skip(self))]
601 pub(super) fn try_evaluate_added_goals(&mut self) -> Result<Certainty, NoSolution> {
602 let mut response = Ok(Certainty::overflow(false));
603 for _ in 0..FIXPOINT_STEP_LIMIT {
604 match self.evaluate_added_goals_step() {
607 Ok(Some(cert)) => {
608 response = Ok(cert);
609 break;
610 }
611 Ok(None) => {}
612 Err(NoSolution) => {
613 response = Err(NoSolution);
614 break;
615 }
616 }
617 }
618
619 if response.is_err() {
620 self.tainted = Err(NoSolution);
621 }
622
623 response
624 }
625
626 fn evaluate_added_goals_step(&mut self) -> Result<Option<Certainty>, NoSolution> {
630 let cx = self.cx();
631 let mut unchanged_certainty = Some(Certainty::Yes);
633 for (source, goal, stalled_on) in mem::take(&mut self.nested_goals) {
634 if let Some(certainty) = self.delegate.compute_goal_fast_path(goal, self.origin_span) {
635 match certainty {
636 Certainty::Yes => {}
637 Certainty::Maybe(_) => {
638 self.nested_goals.push((source, goal, None));
639 unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
640 }
641 }
642 continue;
643 }
644
645 if let Some(pred) = goal.predicate.as_normalizes_to() {
656 let pred = pred.no_bound_vars().unwrap();
658 let unconstrained_rhs = self.next_term_infer_of_kind(pred.term);
661 let unconstrained_goal =
662 goal.with(cx, ty::NormalizesTo { alias: pred.alias, term: unconstrained_rhs });
663
664 let (
665 NestedNormalizationGoals(nested_goals),
666 GoalEvaluation { goal, certainty, stalled_on, has_changed: _ },
667 ) = self.evaluate_goal_raw(source, unconstrained_goal, stalled_on)?;
668 trace!(?nested_goals);
670 self.nested_goals.extend(nested_goals.into_iter().map(|(s, g)| (s, g, None)));
671
672 self.eq_structurally_relating_aliases(
687 goal.param_env,
688 pred.term,
689 unconstrained_rhs,
690 )?;
691
692 let with_resolved_vars = self.resolve_vars_if_possible(goal);
699 if pred.alias
700 != with_resolved_vars
701 .predicate
702 .as_normalizes_to()
703 .unwrap()
704 .no_bound_vars()
705 .unwrap()
706 .alias
707 {
708 unchanged_certainty = None;
709 }
710
711 match certainty {
712 Certainty::Yes => {}
713 Certainty::Maybe(_) => {
714 self.nested_goals.push((source, with_resolved_vars, stalled_on));
715 unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
716 }
717 }
718 } else {
719 let GoalEvaluation { goal, certainty, has_changed, stalled_on } =
720 self.evaluate_goal(source, goal, stalled_on)?;
721 if has_changed == HasChanged::Yes {
722 unchanged_certainty = None;
723 }
724
725 match certainty {
726 Certainty::Yes => {}
727 Certainty::Maybe(_) => {
728 self.nested_goals.push((source, goal, stalled_on));
729 unchanged_certainty = unchanged_certainty.map(|c| c.and(certainty));
730 }
731 }
732 }
733 }
734
735 Ok(unchanged_certainty)
736 }
737
738 pub(crate) fn record_impl_args(&mut self, impl_args: I::GenericArgs) {
740 self.inspect.record_impl_args(self.delegate, self.max_input_universe, impl_args)
741 }
742
743 pub(super) fn cx(&self) -> I {
744 self.delegate.cx()
745 }
746
747 #[instrument(level = "debug", skip(self))]
748 pub(super) fn add_goal(&mut self, source: GoalSource, mut goal: Goal<I, I::Predicate>) {
749 goal.predicate =
750 goal.predicate.fold_with(&mut ReplaceAliasWithInfer::new(self, source, goal.param_env));
751 self.inspect.add_goal(self.delegate, self.max_input_universe, source, goal);
752 self.nested_goals.push((source, goal, None));
753 }
754
755 #[instrument(level = "trace", skip(self, goals))]
756 pub(super) fn add_goals(
757 &mut self,
758 source: GoalSource,
759 goals: impl IntoIterator<Item = Goal<I, I::Predicate>>,
760 ) {
761 for goal in goals {
762 self.add_goal(source, goal);
763 }
764 }
765
766 pub(super) fn next_region_var(&mut self) -> I::Region {
767 let region = self.delegate.next_region_infer();
768 self.inspect.add_var_value(region);
769 region
770 }
771
772 pub(super) fn next_ty_infer(&mut self) -> I::Ty {
773 let ty = self.delegate.next_ty_infer();
774 self.inspect.add_var_value(ty);
775 ty
776 }
777
778 pub(super) fn next_const_infer(&mut self) -> I::Const {
779 let ct = self.delegate.next_const_infer();
780 self.inspect.add_var_value(ct);
781 ct
782 }
783
784 pub(super) fn next_term_infer_of_kind(&mut self, term: I::Term) -> I::Term {
787 match term.kind() {
788 ty::TermKind::Ty(_) => self.next_ty_infer().into(),
789 ty::TermKind::Const(_) => self.next_const_infer().into(),
790 }
791 }
792
793 #[instrument(level = "trace", skip(self), ret)]
798 pub(super) fn term_is_fully_unconstrained(&self, goal: Goal<I, ty::NormalizesTo<I>>) -> bool {
799 let universe_of_term = match goal.predicate.term.kind() {
800 ty::TermKind::Ty(ty) => {
801 if let ty::Infer(ty::TyVar(vid)) = ty.kind() {
802 self.delegate.universe_of_ty(vid).unwrap()
803 } else {
804 return false;
805 }
806 }
807 ty::TermKind::Const(ct) => {
808 if let ty::ConstKind::Infer(ty::InferConst::Var(vid)) = ct.kind() {
809 self.delegate.universe_of_ct(vid).unwrap()
810 } else {
811 return false;
812 }
813 }
814 };
815
816 struct ContainsTermOrNotNameable<'a, D: SolverDelegate<Interner = I>, I: Interner> {
817 term: I::Term,
818 universe_of_term: ty::UniverseIndex,
819 delegate: &'a D,
820 cache: HashSet<I::Ty>,
821 }
822
823 impl<D: SolverDelegate<Interner = I>, I: Interner> ContainsTermOrNotNameable<'_, D, I> {
824 fn check_nameable(&self, universe: ty::UniverseIndex) -> ControlFlow<()> {
825 if self.universe_of_term.can_name(universe) {
826 ControlFlow::Continue(())
827 } else {
828 ControlFlow::Break(())
829 }
830 }
831 }
832
833 impl<D: SolverDelegate<Interner = I>, I: Interner> TypeVisitor<I>
834 for ContainsTermOrNotNameable<'_, D, I>
835 {
836 type Result = ControlFlow<()>;
837 fn visit_ty(&mut self, t: I::Ty) -> Self::Result {
838 if self.cache.contains(&t) {
839 return ControlFlow::Continue(());
840 }
841
842 match t.kind() {
843 ty::Infer(ty::TyVar(vid)) => {
844 if let ty::TermKind::Ty(term) = self.term.kind()
845 && let ty::Infer(ty::TyVar(term_vid)) = term.kind()
846 && self.delegate.root_ty_var(vid) == self.delegate.root_ty_var(term_vid)
847 {
848 return ControlFlow::Break(());
849 }
850
851 self.check_nameable(self.delegate.universe_of_ty(vid).unwrap())?;
852 }
853 ty::Placeholder(p) => self.check_nameable(p.universe())?,
854 _ => {
855 if t.has_non_region_infer() || t.has_placeholders() {
856 t.super_visit_with(self)?
857 }
858 }
859 }
860
861 assert!(self.cache.insert(t));
862 ControlFlow::Continue(())
863 }
864
865 fn visit_const(&mut self, c: I::Const) -> Self::Result {
866 match c.kind() {
867 ty::ConstKind::Infer(ty::InferConst::Var(vid)) => {
868 if let ty::TermKind::Const(term) = self.term.kind()
869 && let ty::ConstKind::Infer(ty::InferConst::Var(term_vid)) = term.kind()
870 && self.delegate.root_const_var(vid)
871 == self.delegate.root_const_var(term_vid)
872 {
873 return ControlFlow::Break(());
874 }
875
876 self.check_nameable(self.delegate.universe_of_ct(vid).unwrap())
877 }
878 ty::ConstKind::Placeholder(p) => self.check_nameable(p.universe()),
879 _ => {
880 if c.has_non_region_infer() || c.has_placeholders() {
881 c.super_visit_with(self)
882 } else {
883 ControlFlow::Continue(())
884 }
885 }
886 }
887 }
888
889 fn visit_predicate(&mut self, p: I::Predicate) -> Self::Result {
890 if p.has_non_region_infer() || p.has_placeholders() {
891 p.super_visit_with(self)
892 } else {
893 ControlFlow::Continue(())
894 }
895 }
896
897 fn visit_clauses(&mut self, c: I::Clauses) -> Self::Result {
898 if c.has_non_region_infer() || c.has_placeholders() {
899 c.super_visit_with(self)
900 } else {
901 ControlFlow::Continue(())
902 }
903 }
904 }
905
906 let mut visitor = ContainsTermOrNotNameable {
907 delegate: self.delegate,
908 universe_of_term,
909 term: goal.predicate.term,
910 cache: Default::default(),
911 };
912 goal.predicate.alias.visit_with(&mut visitor).is_continue()
913 && goal.param_env.visit_with(&mut visitor).is_continue()
914 }
915
916 pub(super) fn sub_unify_ty_vids_raw(&self, a: ty::TyVid, b: ty::TyVid) {
917 self.delegate.sub_unify_ty_vids_raw(a, b)
918 }
919
920 #[instrument(level = "trace", skip(self, param_env), ret)]
921 pub(super) fn eq<T: Relate<I>>(
922 &mut self,
923 param_env: I::ParamEnv,
924 lhs: T,
925 rhs: T,
926 ) -> Result<(), NoSolution> {
927 self.relate(param_env, lhs, ty::Variance::Invariant, rhs)
928 }
929
930 #[instrument(level = "trace", skip(self, param_env), ret)]
936 pub(super) fn relate_rigid_alias_non_alias(
937 &mut self,
938 param_env: I::ParamEnv,
939 alias: ty::AliasTerm<I>,
940 variance: ty::Variance,
941 term: I::Term,
942 ) -> Result<(), NoSolution> {
943 if term.is_infer() {
946 let cx = self.cx();
947 let identity_args = self.fresh_args_for_item(alias.def_id);
956 let rigid_ctor = ty::AliasTerm::new_from_args(cx, alias.def_id, identity_args);
957 let ctor_term = rigid_ctor.to_term(cx);
958 let obligations = self.delegate.eq_structurally_relating_aliases(
959 param_env,
960 term,
961 ctor_term,
962 self.origin_span,
963 )?;
964 debug_assert!(obligations.is_empty());
965 self.relate(param_env, alias, variance, rigid_ctor)
966 } else {
967 Err(NoSolution)
968 }
969 }
970
971 #[instrument(level = "trace", skip(self, param_env), ret)]
975 pub(super) fn eq_structurally_relating_aliases<T: Relate<I>>(
976 &mut self,
977 param_env: I::ParamEnv,
978 lhs: T,
979 rhs: T,
980 ) -> Result<(), NoSolution> {
981 let result = self.delegate.eq_structurally_relating_aliases(
982 param_env,
983 lhs,
984 rhs,
985 self.origin_span,
986 )?;
987 assert_eq!(result, vec![]);
988 Ok(())
989 }
990
991 #[instrument(level = "trace", skip(self, param_env), ret)]
992 pub(super) fn sub<T: Relate<I>>(
993 &mut self,
994 param_env: I::ParamEnv,
995 sub: T,
996 sup: T,
997 ) -> Result<(), NoSolution> {
998 self.relate(param_env, sub, ty::Variance::Covariant, sup)
999 }
1000
1001 #[instrument(level = "trace", skip(self, param_env), ret)]
1002 pub(super) fn relate<T: Relate<I>>(
1003 &mut self,
1004 param_env: I::ParamEnv,
1005 lhs: T,
1006 variance: ty::Variance,
1007 rhs: T,
1008 ) -> Result<(), NoSolution> {
1009 let goals = self.delegate.relate(param_env, lhs, variance, rhs, self.origin_span)?;
1010 for &goal in goals.iter() {
1011 let source = match goal.predicate.kind().skip_binder() {
1012 ty::PredicateKind::Subtype { .. } | ty::PredicateKind::AliasRelate(..) => {
1013 GoalSource::TypeRelating
1014 }
1015 ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => GoalSource::Misc,
1017 p => unreachable!("unexpected nested goal in `relate`: {p:?}"),
1018 };
1019 self.add_goal(source, goal);
1020 }
1021 Ok(())
1022 }
1023
1024 #[instrument(level = "trace", skip(self, param_env), ret)]
1030 pub(super) fn eq_and_get_goals<T: Relate<I>>(
1031 &self,
1032 param_env: I::ParamEnv,
1033 lhs: T,
1034 rhs: T,
1035 ) -> Result<Vec<Goal<I, I::Predicate>>, NoSolution> {
1036 Ok(self.delegate.relate(param_env, lhs, ty::Variance::Invariant, rhs, self.origin_span)?)
1037 }
1038
1039 pub(super) fn instantiate_binder_with_infer<T: TypeFoldable<I> + Copy>(
1040 &self,
1041 value: ty::Binder<I, T>,
1042 ) -> T {
1043 self.delegate.instantiate_binder_with_infer(value)
1044 }
1045
1046 pub(super) fn enter_forall<T: TypeFoldable<I>, U>(
1049 &mut self,
1050 value: ty::Binder<I, T>,
1051 f: impl FnOnce(&mut Self, T) -> U,
1052 ) -> U {
1053 self.delegate.enter_forall(value, |value| f(self, value))
1054 }
1055
1056 pub(super) fn resolve_vars_if_possible<T>(&self, value: T) -> T
1057 where
1058 T: TypeFoldable<I>,
1059 {
1060 self.delegate.resolve_vars_if_possible(value)
1061 }
1062
1063 pub(super) fn shallow_resolve(&self, ty: I::Ty) -> I::Ty {
1064 self.delegate.shallow_resolve(ty)
1065 }
1066
1067 pub(super) fn eager_resolve_region(&self, r: I::Region) -> I::Region {
1068 if let ty::ReVar(vid) = r.kind() {
1069 self.delegate.opportunistic_resolve_lt_var(vid)
1070 } else {
1071 r
1072 }
1073 }
1074
1075 pub(super) fn fresh_args_for_item(&mut self, def_id: I::DefId) -> I::GenericArgs {
1076 let args = self.delegate.fresh_args_for_item(def_id);
1077 for arg in args.iter() {
1078 self.inspect.add_var_value(arg);
1079 }
1080 args
1081 }
1082
1083 pub(super) fn register_ty_outlives(&self, ty: I::Ty, lt: I::Region) {
1084 self.delegate.register_ty_outlives(ty, lt, self.origin_span);
1085 }
1086
1087 pub(super) fn register_region_outlives(&self, a: I::Region, b: I::Region) {
1088 self.delegate.sub_regions(b, a, self.origin_span);
1090 }
1091
1092 pub(super) fn well_formed_goals(
1094 &self,
1095 param_env: I::ParamEnv,
1096 term: I::Term,
1097 ) -> Option<Vec<Goal<I, I::Predicate>>> {
1098 self.delegate.well_formed_goals(param_env, term)
1099 }
1100
1101 pub(super) fn trait_ref_is_knowable(
1102 &mut self,
1103 param_env: I::ParamEnv,
1104 trait_ref: ty::TraitRef<I>,
1105 ) -> Result<bool, NoSolution> {
1106 let delegate = self.delegate;
1107 let lazily_normalize_ty = |ty| self.structurally_normalize_ty(param_env, ty);
1108 coherence::trait_ref_is_knowable(&**delegate, trait_ref, lazily_normalize_ty)
1109 .map(|is_knowable| is_knowable.is_ok())
1110 }
1111
1112 pub(super) fn fetch_eligible_assoc_item(
1113 &self,
1114 goal_trait_ref: ty::TraitRef<I>,
1115 trait_assoc_def_id: I::DefId,
1116 impl_def_id: I::ImplId,
1117 ) -> Result<Option<I::DefId>, I::ErrorGuaranteed> {
1118 self.delegate.fetch_eligible_assoc_item(goal_trait_ref, trait_assoc_def_id, impl_def_id)
1119 }
1120
1121 #[instrument(level = "debug", skip(self), ret)]
1122 pub(super) fn register_hidden_type_in_storage(
1123 &mut self,
1124 opaque_type_key: ty::OpaqueTypeKey<I>,
1125 hidden_ty: I::Ty,
1126 ) -> Option<I::Ty> {
1127 self.delegate.register_hidden_type_in_storage(opaque_type_key, hidden_ty, self.origin_span)
1128 }
1129
1130 pub(super) fn add_item_bounds_for_hidden_type(
1131 &mut self,
1132 opaque_def_id: I::DefId,
1133 opaque_args: I::GenericArgs,
1134 param_env: I::ParamEnv,
1135 hidden_ty: I::Ty,
1136 ) {
1137 let mut goals = Vec::new();
1138 self.delegate.add_item_bounds_for_hidden_type(
1139 opaque_def_id,
1140 opaque_args,
1141 param_env,
1142 hidden_ty,
1143 &mut goals,
1144 );
1145 self.add_goals(GoalSource::AliasWellFormed, goals);
1146 }
1147
1148 pub(super) fn evaluate_const(
1152 &self,
1153 param_env: I::ParamEnv,
1154 uv: ty::UnevaluatedConst<I>,
1155 ) -> Option<I::Const> {
1156 self.delegate.evaluate_const(param_env, uv)
1157 }
1158
1159 pub(super) fn is_transmutable(
1160 &mut self,
1161 dst: I::Ty,
1162 src: I::Ty,
1163 assume: I::Const,
1164 ) -> Result<Certainty, NoSolution> {
1165 self.delegate.is_transmutable(dst, src, assume)
1166 }
1167
1168 pub(super) fn replace_bound_vars<T: TypeFoldable<I>>(
1169 &self,
1170 t: T,
1171 universes: &mut Vec<Option<ty::UniverseIndex>>,
1172 ) -> T {
1173 BoundVarReplacer::replace_bound_vars(&**self.delegate, universes, t).0
1174 }
1175
1176 pub(super) fn may_use_unstable_feature(
1177 &self,
1178 param_env: I::ParamEnv,
1179 symbol: I::Symbol,
1180 ) -> bool {
1181 may_use_unstable_feature(&**self.delegate, param_env, symbol)
1182 }
1183
1184 pub(crate) fn opaques_with_sub_unified_hidden_type(
1185 &self,
1186 self_ty: I::Ty,
1187 ) -> impl Iterator<Item = ty::AliasTy<I>> + use<'a, D, I> {
1188 let delegate = self.delegate;
1189 delegate
1190 .clone_opaque_types_lookup_table()
1191 .into_iter()
1192 .chain(delegate.clone_duplicate_opaque_types())
1193 .filter_map(move |(key, hidden_ty)| {
1194 if let ty::Infer(ty::TyVar(self_vid)) = self_ty.kind() {
1195 if let ty::Infer(ty::TyVar(hidden_vid)) = hidden_ty.kind() {
1196 if delegate.sub_unification_table_root_var(self_vid)
1197 == delegate.sub_unification_table_root_var(hidden_vid)
1198 {
1199 return Some(ty::AliasTy::new_from_args(
1200 delegate.cx(),
1201 key.def_id.into(),
1202 key.args,
1203 ));
1204 }
1205 }
1206 }
1207 None
1208 })
1209 }
1210}
1211
1212struct ReplaceAliasWithInfer<'me, 'a, D, I>
1227where
1228 D: SolverDelegate<Interner = I>,
1229 I: Interner,
1230{
1231 ecx: &'me mut EvalCtxt<'a, D>,
1232 param_env: I::ParamEnv,
1233 normalization_goal_source: GoalSource,
1234 cache: HashMap<I::Ty, I::Ty>,
1235}
1236
1237impl<'me, 'a, D, I> ReplaceAliasWithInfer<'me, 'a, D, I>
1238where
1239 D: SolverDelegate<Interner = I>,
1240 I: Interner,
1241{
1242 fn new(
1243 ecx: &'me mut EvalCtxt<'a, D>,
1244 for_goal_source: GoalSource,
1245 param_env: I::ParamEnv,
1246 ) -> Self {
1247 let step_kind = ecx.step_kind_for_source(for_goal_source);
1248 ReplaceAliasWithInfer {
1249 ecx,
1250 param_env,
1251 normalization_goal_source: GoalSource::NormalizeGoal(step_kind),
1252 cache: Default::default(),
1253 }
1254 }
1255}
1256
1257impl<D, I> TypeFolder<I> for ReplaceAliasWithInfer<'_, '_, D, I>
1258where
1259 D: SolverDelegate<Interner = I>,
1260 I: Interner,
1261{
1262 fn cx(&self) -> I {
1263 self.ecx.cx()
1264 }
1265
1266 fn fold_ty(&mut self, ty: I::Ty) -> I::Ty {
1267 match ty.kind() {
1268 ty::Alias(..) if !ty.has_escaping_bound_vars() => {
1269 let infer_ty = self.ecx.next_ty_infer();
1270 let normalizes_to = ty::PredicateKind::AliasRelate(
1271 ty.into(),
1272 infer_ty.into(),
1273 ty::AliasRelationDirection::Equate,
1274 );
1275 self.ecx.add_goal(
1276 self.normalization_goal_source,
1277 Goal::new(self.cx(), self.param_env, normalizes_to),
1278 );
1279 infer_ty
1280 }
1281 _ => {
1282 if !ty.has_aliases() {
1283 ty
1284 } else if let Some(&entry) = self.cache.get(&ty) {
1285 return entry;
1286 } else {
1287 let res = ty.super_fold_with(self);
1288 assert!(self.cache.insert(ty, res).is_none());
1289 res
1290 }
1291 }
1292 }
1293 }
1294
1295 fn fold_const(&mut self, ct: I::Const) -> I::Const {
1296 match ct.kind() {
1297 ty::ConstKind::Unevaluated(..) if !ct.has_escaping_bound_vars() => {
1298 let infer_ct = self.ecx.next_const_infer();
1299 let normalizes_to = ty::PredicateKind::AliasRelate(
1300 ct.into(),
1301 infer_ct.into(),
1302 ty::AliasRelationDirection::Equate,
1303 );
1304 self.ecx.add_goal(
1305 self.normalization_goal_source,
1306 Goal::new(self.cx(), self.param_env, normalizes_to),
1307 );
1308 infer_ct
1309 }
1310 _ => ct.super_fold_with(self),
1311 }
1312 }
1313
1314 fn fold_predicate(&mut self, predicate: I::Predicate) -> I::Predicate {
1315 if predicate.allow_normalization() { predicate.super_fold_with(self) } else { predicate }
1316 }
1317}
1318
1319pub fn evaluate_root_goal_for_proof_tree_raw_provider<
1321 D: SolverDelegate<Interner = I>,
1322 I: Interner,
1323>(
1324 cx: I,
1325 canonical_goal: CanonicalInput<I>,
1326) -> (QueryResult<I>, I::Probe) {
1327 let mut inspect = inspect::ProofTreeBuilder::new();
1328 let canonical_result = SearchGraph::<D>::evaluate_root_goal_for_proof_tree(
1329 cx,
1330 cx.recursion_limit(),
1331 canonical_goal,
1332 &mut inspect,
1333 );
1334 let final_revision = inspect.unwrap();
1335 (canonical_result, cx.mk_probe(final_revision))
1336}
1337
1338pub(super) fn evaluate_root_goal_for_proof_tree<D: SolverDelegate<Interner = I>, I: Interner>(
1343 delegate: &D,
1344 goal: Goal<I, I::Predicate>,
1345 origin_span: I::Span,
1346) -> (Result<NestedNormalizationGoals<I>, NoSolution>, inspect::GoalEvaluation<I>) {
1347 let opaque_types = delegate.clone_opaque_types_lookup_table();
1348 let (goal, opaque_types) = eager_resolve_vars(delegate, (goal, opaque_types));
1349
1350 let (orig_values, canonical_goal) = EvalCtxt::canonicalize_goal(delegate, goal, opaque_types);
1351
1352 let (canonical_result, final_revision) =
1353 delegate.cx().evaluate_root_goal_for_proof_tree_raw(canonical_goal);
1354
1355 let proof_tree = inspect::GoalEvaluation {
1356 uncanonicalized_goal: goal,
1357 orig_values,
1358 final_revision,
1359 result: canonical_result,
1360 };
1361
1362 let response = match canonical_result {
1363 Err(e) => return (Err(e), proof_tree),
1364 Ok(response) => response,
1365 };
1366
1367 let (normalization_nested_goals, _certainty) = EvalCtxt::instantiate_and_apply_query_response(
1368 delegate,
1369 goal.param_env,
1370 &proof_tree.orig_values,
1371 response,
1372 origin_span,
1373 );
1374
1375 (Ok(normalization_nested_goals), proof_tree)
1376}