//! Building proof trees incrementally during trait solving. //! //! This code is *a bit* of a mess and can hopefully be //! mostly ignored. For a general overview of how it works, //! see the comment on [ProofTreeBuilder]. use std::marker::PhantomData; use std::mem; use crate::solve::eval_ctxt::canonical; use crate::solve::{self, inspect, GenerateProofTree}; use rustc_middle::bug; use rustc_next_trait_solver::solve::{ CanonicalInput, Certainty, Goal, GoalSource, QueryInput, QueryResult, }; use rustc_type_ir::{self as ty, InferCtxtLike, Interner}; /// The core data structure when building proof trees. /// /// In case the current evaluation does not generate a proof /// tree, `state` is simply `None` and we avoid any work. /// /// The possible states of the solver are represented via /// variants of [DebugSolver]. For any nested computation we call /// `ProofTreeBuilder::new_nested_computation_kind` which /// creates a new `ProofTreeBuilder` to temporarily replace the /// current one. Once that nested computation is done, /// `ProofTreeBuilder::nested_computation_kind` is called /// to add the finished nested evaluation to the parent. /// /// We provide additional information to the current state /// by calling methods such as `ProofTreeBuilder::probe_kind`. /// /// The actual structure closely mirrors the finished proof /// trees. At the end of trait solving `ProofTreeBuilder::finalize` /// is called to recursively convert the whole structure to a /// finished proof tree. pub(in crate::solve) struct ProofTreeBuilder< Infcx: InferCtxtLike, I: Interner = ::Interner, > { _infcx: PhantomData, state: Option>>, } /// The current state of the proof tree builder, at most places /// in the code, only one or two variants are actually possible. /// /// We simply ICE in case that assumption is broken. #[derive(derivative::Derivative)] #[derivative(Debug(bound = ""))] enum DebugSolver { Root, GoalEvaluation(WipGoalEvaluation), CanonicalGoalEvaluation(WipCanonicalGoalEvaluation), CanonicalGoalEvaluationStep(WipCanonicalGoalEvaluationStep), } impl From> for DebugSolver { fn from(g: WipGoalEvaluation) -> DebugSolver { DebugSolver::GoalEvaluation(g) } } impl From> for DebugSolver { fn from(g: WipCanonicalGoalEvaluation) -> DebugSolver { DebugSolver::CanonicalGoalEvaluation(g) } } impl From> for DebugSolver { fn from(g: WipCanonicalGoalEvaluationStep) -> DebugSolver { DebugSolver::CanonicalGoalEvaluationStep(g) } } #[derive(derivative::Derivative)] #[derivative(PartialEq(bound = ""), Eq(bound = ""), Debug(bound = ""))] struct WipGoalEvaluation { pub uncanonicalized_goal: Goal, pub orig_values: Vec, pub evaluation: Option>, } impl WipGoalEvaluation { fn finalize(self) -> inspect::GoalEvaluation { inspect::GoalEvaluation { uncanonicalized_goal: self.uncanonicalized_goal, orig_values: self.orig_values, evaluation: self.evaluation.unwrap().finalize(), } } } #[derive(derivative::Derivative)] #[derivative(PartialEq(bound = ""), Eq(bound = ""))] pub(in crate::solve) enum WipCanonicalGoalEvaluationKind { Overflow, CycleInStack, ProvisionalCacheHit, Interned { final_revision: I::CanonicalGoalEvaluationStepRef }, } impl std::fmt::Debug for WipCanonicalGoalEvaluationKind { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Overflow => write!(f, "Overflow"), Self::CycleInStack => write!(f, "CycleInStack"), Self::ProvisionalCacheHit => write!(f, "ProvisionalCacheHit"), Self::Interned { final_revision: _ } => { f.debug_struct("Interned").finish_non_exhaustive() } } } } #[derive(derivative::Derivative)] #[derivative(PartialEq(bound = ""), Eq(bound = ""), Debug(bound = ""))] struct WipCanonicalGoalEvaluation { goal: CanonicalInput, kind: Option>, /// Only used for uncached goals. After we finished evaluating /// the goal, this is interned and moved into `kind`. final_revision: Option>, result: Option>, } impl WipCanonicalGoalEvaluation { fn finalize(self) -> inspect::CanonicalGoalEvaluation { // We've already interned the final revision in // `fn finalize_canonical_goal_evaluation`. assert!(self.final_revision.is_none()); let kind = match self.kind.unwrap() { WipCanonicalGoalEvaluationKind::Overflow => { inspect::CanonicalGoalEvaluationKind::Overflow } WipCanonicalGoalEvaluationKind::CycleInStack => { inspect::CanonicalGoalEvaluationKind::CycleInStack } WipCanonicalGoalEvaluationKind::ProvisionalCacheHit => { inspect::CanonicalGoalEvaluationKind::ProvisionalCacheHit } WipCanonicalGoalEvaluationKind::Interned { final_revision } => { inspect::CanonicalGoalEvaluationKind::Evaluation { final_revision } } }; inspect::CanonicalGoalEvaluation { goal: self.goal, kind, result: self.result.unwrap() } } } #[derive(derivative::Derivative)] #[derivative(PartialEq(bound = ""), Eq(bound = ""), Debug(bound = ""))] struct WipCanonicalGoalEvaluationStep { /// Unlike `EvalCtxt::var_values`, we append a new /// generic arg here whenever we create a new inference /// variable. /// /// This is necessary as we otherwise don't unify these /// vars when instantiating multiple `CanonicalState`. var_values: Vec, instantiated_goal: QueryInput, probe_depth: usize, evaluation: WipProbe, } impl WipCanonicalGoalEvaluationStep { fn current_evaluation_scope(&mut self) -> &mut WipProbe { let mut current = &mut self.evaluation; for _ in 0..self.probe_depth { match current.steps.last_mut() { Some(WipProbeStep::NestedProbe(p)) => current = p, _ => bug!(), } } current } fn finalize(self) -> inspect::CanonicalGoalEvaluationStep { let evaluation = self.evaluation.finalize(); match evaluation.kind { inspect::ProbeKind::Root { .. } => (), _ => unreachable!("unexpected root evaluation: {evaluation:?}"), } inspect::CanonicalGoalEvaluationStep { instantiated_goal: self.instantiated_goal, evaluation, } } } #[derive(derivative::Derivative)] #[derivative(PartialEq(bound = ""), Eq(bound = ""), Debug(bound = ""))] struct WipProbe { initial_num_var_values: usize, steps: Vec>, kind: Option>, final_state: Option>, } impl WipProbe { fn finalize(self) -> inspect::Probe { inspect::Probe { steps: self.steps.into_iter().map(WipProbeStep::finalize).collect(), kind: self.kind.unwrap(), final_state: self.final_state.unwrap(), } } } #[derive(derivative::Derivative)] #[derivative(PartialEq(bound = ""), Eq(bound = ""), Debug(bound = ""))] enum WipProbeStep { AddGoal(GoalSource, inspect::CanonicalState>), NestedProbe(WipProbe), MakeCanonicalResponse { shallow_certainty: Certainty }, RecordImplArgs { impl_args: inspect::CanonicalState }, } impl WipProbeStep { fn finalize(self) -> inspect::ProbeStep { match self { WipProbeStep::AddGoal(source, goal) => inspect::ProbeStep::AddGoal(source, goal), WipProbeStep::NestedProbe(probe) => inspect::ProbeStep::NestedProbe(probe.finalize()), WipProbeStep::RecordImplArgs { impl_args } => { inspect::ProbeStep::RecordImplArgs { impl_args } } WipProbeStep::MakeCanonicalResponse { shallow_certainty } => { inspect::ProbeStep::MakeCanonicalResponse { shallow_certainty } } } } } impl, I: Interner> ProofTreeBuilder { fn new(state: impl Into>) -> ProofTreeBuilder { ProofTreeBuilder { state: Some(Box::new(state.into())), _infcx: PhantomData } } fn opt_nested>>(&self, state: impl FnOnce() -> Option) -> Self { ProofTreeBuilder { state: self.state.as_ref().and_then(|_| Some(state()?.into())).map(Box::new), _infcx: PhantomData, } } fn nested>>(&self, state: impl FnOnce() -> T) -> Self { ProofTreeBuilder { state: self.state.as_ref().map(|_| Box::new(state().into())), _infcx: PhantomData, } } fn as_mut(&mut self) -> Option<&mut DebugSolver> { self.state.as_deref_mut() } pub fn take_and_enter_probe(&mut self) -> ProofTreeBuilder { let mut nested = ProofTreeBuilder { state: self.state.take(), _infcx: PhantomData }; nested.enter_probe(); nested } pub fn finalize(self) -> Option> { match *self.state? { DebugSolver::GoalEvaluation(wip_goal_evaluation) => { Some(wip_goal_evaluation.finalize()) } root => unreachable!("unexpected proof tree builder root node: {:?}", root), } } pub fn new_maybe_root(generate_proof_tree: GenerateProofTree) -> ProofTreeBuilder { match generate_proof_tree { GenerateProofTree::No => ProofTreeBuilder::new_noop(), GenerateProofTree::Yes => ProofTreeBuilder::new_root(), } } pub fn new_root() -> ProofTreeBuilder { ProofTreeBuilder::new(DebugSolver::Root) } pub fn new_noop() -> ProofTreeBuilder { ProofTreeBuilder { state: None, _infcx: PhantomData } } pub fn is_noop(&self) -> bool { self.state.is_none() } pub(in crate::solve) fn new_goal_evaluation( &mut self, goal: Goal, orig_values: &[I::GenericArg], kind: solve::GoalEvaluationKind, ) -> ProofTreeBuilder { self.opt_nested(|| match kind { solve::GoalEvaluationKind::Root => Some(WipGoalEvaluation { uncanonicalized_goal: goal, orig_values: orig_values.to_vec(), evaluation: None, }), solve::GoalEvaluationKind::Nested => None, }) } pub fn new_canonical_goal_evaluation( &mut self, goal: CanonicalInput, ) -> ProofTreeBuilder { self.nested(|| WipCanonicalGoalEvaluation { goal, kind: None, final_revision: None, result: None, }) } pub fn finalize_canonical_goal_evaluation( &mut self, tcx: I, ) -> Option { self.as_mut().map(|this| match this { DebugSolver::CanonicalGoalEvaluation(evaluation) => { let final_revision = mem::take(&mut evaluation.final_revision).unwrap(); let final_revision = tcx.intern_canonical_goal_evaluation_step(final_revision.finalize()); let kind = WipCanonicalGoalEvaluationKind::Interned { final_revision }; assert_eq!(evaluation.kind.replace(kind), None); final_revision } _ => unreachable!(), }) } pub fn canonical_goal_evaluation( &mut self, canonical_goal_evaluation: ProofTreeBuilder, ) { if let Some(this) = self.as_mut() { match (this, *canonical_goal_evaluation.state.unwrap()) { ( DebugSolver::GoalEvaluation(goal_evaluation), DebugSolver::CanonicalGoalEvaluation(canonical_goal_evaluation), ) => { let prev = goal_evaluation.evaluation.replace(canonical_goal_evaluation); assert_eq!(prev, None); } _ => unreachable!(), } } } pub fn canonical_goal_evaluation_kind(&mut self, kind: WipCanonicalGoalEvaluationKind) { if let Some(this) = self.as_mut() { match this { DebugSolver::CanonicalGoalEvaluation(canonical_goal_evaluation) => { assert_eq!(canonical_goal_evaluation.kind.replace(kind), None); } _ => unreachable!(), }; } } pub fn goal_evaluation(&mut self, goal_evaluation: ProofTreeBuilder) { if let Some(this) = self.as_mut() { match this { DebugSolver::Root => *this = *goal_evaluation.state.unwrap(), DebugSolver::CanonicalGoalEvaluationStep(_) => { assert!(goal_evaluation.state.is_none()) } _ => unreachable!(), } } } pub fn new_goal_evaluation_step( &mut self, var_values: ty::CanonicalVarValues, instantiated_goal: QueryInput, ) -> ProofTreeBuilder { self.nested(|| WipCanonicalGoalEvaluationStep { var_values: var_values.var_values.to_vec(), instantiated_goal, evaluation: WipProbe { initial_num_var_values: var_values.len(), steps: vec![], kind: None, final_state: None, }, probe_depth: 0, }) } pub fn goal_evaluation_step(&mut self, goal_evaluation_step: ProofTreeBuilder) { if let Some(this) = self.as_mut() { match (this, *goal_evaluation_step.state.unwrap()) { ( DebugSolver::CanonicalGoalEvaluation(canonical_goal_evaluations), DebugSolver::CanonicalGoalEvaluationStep(goal_evaluation_step), ) => { canonical_goal_evaluations.final_revision = Some(goal_evaluation_step); } _ => unreachable!(), } } } pub fn add_var_value>(&mut self, arg: T) { match self.as_mut() { None => {} Some(DebugSolver::CanonicalGoalEvaluationStep(state)) => { state.var_values.push(arg.into()); } Some(s) => bug!("tried to add var values to {s:?}"), } } pub fn enter_probe(&mut self) { match self.as_mut() { None => {} Some(DebugSolver::CanonicalGoalEvaluationStep(state)) => { let initial_num_var_values = state.var_values.len(); state.current_evaluation_scope().steps.push(WipProbeStep::NestedProbe(WipProbe { initial_num_var_values, steps: vec![], kind: None, final_state: None, })); state.probe_depth += 1; } Some(s) => bug!("tried to start probe to {s:?}"), } } pub fn probe_kind(&mut self, probe_kind: inspect::ProbeKind) { match self.as_mut() { None => {} Some(DebugSolver::CanonicalGoalEvaluationStep(state)) => { let prev = state.current_evaluation_scope().kind.replace(probe_kind); assert_eq!(prev, None); } _ => bug!(), } } pub fn probe_final_state(&mut self, infcx: &Infcx, max_input_universe: ty::UniverseIndex) { match self.as_mut() { None => {} Some(DebugSolver::CanonicalGoalEvaluationStep(state)) => { let final_state = canonical::make_canonical_state( infcx, &state.var_values, max_input_universe, (), ); let prev = state.current_evaluation_scope().final_state.replace(final_state); assert_eq!(prev, None); } _ => bug!(), } } pub fn add_normalizes_to_goal( &mut self, infcx: &Infcx, max_input_universe: ty::UniverseIndex, goal: Goal>, ) { self.add_goal( infcx, max_input_universe, GoalSource::Misc, goal.with(infcx.interner(), goal.predicate), ); } pub fn add_goal( &mut self, infcx: &Infcx, max_input_universe: ty::UniverseIndex, source: GoalSource, goal: Goal, ) { match self.as_mut() { None => {} Some(DebugSolver::CanonicalGoalEvaluationStep(state)) => { let goal = canonical::make_canonical_state( infcx, &state.var_values, max_input_universe, goal, ); state.current_evaluation_scope().steps.push(WipProbeStep::AddGoal(source, goal)) } _ => bug!(), } } pub(crate) fn record_impl_args( &mut self, infcx: &Infcx, max_input_universe: ty::UniverseIndex, impl_args: I::GenericArgs, ) { match self.as_mut() { Some(DebugSolver::CanonicalGoalEvaluationStep(state)) => { let impl_args = canonical::make_canonical_state( infcx, &state.var_values, max_input_universe, impl_args, ); state .current_evaluation_scope() .steps .push(WipProbeStep::RecordImplArgs { impl_args }); } None => {} _ => bug!(), } } pub fn make_canonical_response(&mut self, shallow_certainty: Certainty) { match self.as_mut() { Some(DebugSolver::CanonicalGoalEvaluationStep(state)) => { state .current_evaluation_scope() .steps .push(WipProbeStep::MakeCanonicalResponse { shallow_certainty }); } None => {} _ => bug!(), } } pub fn finish_probe(mut self) -> ProofTreeBuilder { match self.as_mut() { None => {} Some(DebugSolver::CanonicalGoalEvaluationStep(state)) => { assert_ne!(state.probe_depth, 0); let num_var_values = state.current_evaluation_scope().initial_num_var_values; state.var_values.truncate(num_var_values); state.probe_depth -= 1; } _ => bug!(), } self } pub fn query_result(&mut self, result: QueryResult) { if let Some(this) = self.as_mut() { match this { DebugSolver::CanonicalGoalEvaluation(canonical_goal_evaluation) => { assert_eq!(canonical_goal_evaluation.result.replace(result), None); } DebugSolver::CanonicalGoalEvaluationStep(evaluation_step) => { assert_eq!( evaluation_step .evaluation .kind .replace(inspect::ProbeKind::Root { result }), None ); } _ => unreachable!(), } } } }