2023-09-20 21:41:07 +02:00
|
|
|
//! 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].
|
2023-10-30 14:23:35 +01:00
|
|
|
use std::mem;
|
|
|
|
|
2023-06-20 12:19:40 +02:00
|
|
|
use rustc_middle::traits::query::NoSolution;
|
|
|
|
use rustc_middle::traits::solve::{
|
|
|
|
CanonicalInput, Certainty, Goal, IsNormalizesToHack, QueryInput, QueryResult,
|
2023-06-08 19:10:07 +01:00
|
|
|
};
|
2023-07-04 10:01:54 +01:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2023-07-04 14:56:05 +01:00
|
|
|
use rustc_session::config::DumpSolverProofTree;
|
2023-06-08 19:10:07 +01:00
|
|
|
|
2023-09-20 21:41:07 +02:00
|
|
|
use crate::solve::{self, inspect, EvalCtxt, GenerateProofTree};
|
|
|
|
|
|
|
|
/// 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<'tcx> {
|
2023-10-30 14:23:35 +01:00
|
|
|
state: Option<Box<DebugSolver<'tcx>>>,
|
2023-09-20 21:41:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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(Debug)]
|
|
|
|
enum DebugSolver<'tcx> {
|
|
|
|
Root,
|
|
|
|
GoalEvaluation(WipGoalEvaluation<'tcx>),
|
|
|
|
CanonicalGoalEvaluation(WipCanonicalGoalEvaluation<'tcx>),
|
|
|
|
AddedGoalsEvaluation(WipAddedGoalsEvaluation<'tcx>),
|
|
|
|
GoalEvaluationStep(WipGoalEvaluationStep<'tcx>),
|
|
|
|
Probe(WipProbe<'tcx>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> From<WipGoalEvaluation<'tcx>> for DebugSolver<'tcx> {
|
|
|
|
fn from(g: WipGoalEvaluation<'tcx>) -> DebugSolver<'tcx> {
|
|
|
|
DebugSolver::GoalEvaluation(g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> From<WipCanonicalGoalEvaluation<'tcx>> for DebugSolver<'tcx> {
|
|
|
|
fn from(g: WipCanonicalGoalEvaluation<'tcx>) -> DebugSolver<'tcx> {
|
|
|
|
DebugSolver::CanonicalGoalEvaluation(g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> From<WipAddedGoalsEvaluation<'tcx>> for DebugSolver<'tcx> {
|
|
|
|
fn from(g: WipAddedGoalsEvaluation<'tcx>) -> DebugSolver<'tcx> {
|
|
|
|
DebugSolver::AddedGoalsEvaluation(g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> From<WipGoalEvaluationStep<'tcx>> for DebugSolver<'tcx> {
|
|
|
|
fn from(g: WipGoalEvaluationStep<'tcx>) -> DebugSolver<'tcx> {
|
|
|
|
DebugSolver::GoalEvaluationStep(g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> From<WipProbe<'tcx>> for DebugSolver<'tcx> {
|
|
|
|
fn from(p: WipProbe<'tcx>) -> DebugSolver<'tcx> {
|
|
|
|
DebugSolver::Probe(p)
|
|
|
|
}
|
|
|
|
}
|
2023-06-09 01:11:58 +01:00
|
|
|
|
2023-09-11 11:34:57 +02:00
|
|
|
#[derive(Eq, PartialEq, Debug)]
|
2023-09-20 21:41:07 +02:00
|
|
|
struct WipGoalEvaluation<'tcx> {
|
2023-06-08 23:24:01 +01:00
|
|
|
pub uncanonicalized_goal: Goal<'tcx, ty::Predicate<'tcx>>,
|
2023-09-20 21:41:07 +02:00
|
|
|
pub kind: WipGoalEvaluationKind<'tcx>,
|
2023-08-14 11:29:42 +02:00
|
|
|
pub evaluation: Option<WipCanonicalGoalEvaluation<'tcx>>,
|
2023-06-08 23:58:34 +01:00
|
|
|
pub returned_goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
|
2023-06-08 23:24:01 +01:00
|
|
|
}
|
2023-06-20 12:40:18 +02:00
|
|
|
|
2023-06-08 23:24:01 +01:00
|
|
|
impl<'tcx> WipGoalEvaluation<'tcx> {
|
2023-09-20 21:41:07 +02:00
|
|
|
fn finalize(self) -> inspect::GoalEvaluation<'tcx> {
|
2023-06-08 23:24:01 +01:00
|
|
|
inspect::GoalEvaluation {
|
|
|
|
uncanonicalized_goal: self.uncanonicalized_goal,
|
2023-09-14 15:10:45 +02:00
|
|
|
kind: match self.kind {
|
2023-09-20 21:41:07 +02:00
|
|
|
WipGoalEvaluationKind::Root { orig_values } => {
|
|
|
|
inspect::GoalEvaluationKind::Root { orig_values }
|
|
|
|
}
|
2023-09-14 15:10:45 +02:00
|
|
|
WipGoalEvaluationKind::Nested { is_normalizes_to_hack } => {
|
|
|
|
inspect::GoalEvaluationKind::Nested { is_normalizes_to_hack }
|
|
|
|
}
|
|
|
|
},
|
2023-08-14 11:29:42 +02:00
|
|
|
evaluation: self.evaluation.unwrap().finalize(),
|
|
|
|
returned_goals: self.returned_goals,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 15:48:04 +02:00
|
|
|
#[derive(Eq, PartialEq, Debug)]
|
2023-09-20 21:41:07 +02:00
|
|
|
pub(in crate::solve) enum WipGoalEvaluationKind<'tcx> {
|
|
|
|
Root { orig_values: Vec<ty::GenericArg<'tcx>> },
|
2023-09-14 15:10:45 +02:00
|
|
|
Nested { is_normalizes_to_hack: IsNormalizesToHack },
|
|
|
|
}
|
|
|
|
|
2023-10-30 14:23:35 +01:00
|
|
|
#[derive(Eq, PartialEq)]
|
|
|
|
pub(in crate::solve) enum WipCanonicalGoalEvaluationKind<'tcx> {
|
2023-09-11 15:48:04 +02:00
|
|
|
Overflow,
|
2023-10-30 14:23:35 +01:00
|
|
|
CycleInStack,
|
|
|
|
Interned { revisions: &'tcx [inspect::GoalEvaluationStep<'tcx>] },
|
|
|
|
}
|
|
|
|
|
|
|
|
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::Interned { revisions: _ } => f.debug_struct("Interned").finish_non_exhaustive(),
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 15:48:04 +02:00
|
|
|
}
|
|
|
|
|
2023-09-11 11:34:57 +02:00
|
|
|
#[derive(Eq, PartialEq, Debug)]
|
2023-09-20 21:41:07 +02:00
|
|
|
struct WipCanonicalGoalEvaluation<'tcx> {
|
|
|
|
goal: CanonicalInput<'tcx>,
|
2023-10-30 14:23:35 +01:00
|
|
|
kind: Option<WipCanonicalGoalEvaluationKind<'tcx>>,
|
|
|
|
/// Only used for uncached goals. After we finished evaluating
|
|
|
|
/// the goal, this is interned and moved into `kind`.
|
2023-09-20 21:41:07 +02:00
|
|
|
revisions: Vec<WipGoalEvaluationStep<'tcx>>,
|
|
|
|
result: Option<QueryResult<'tcx>>,
|
2023-08-14 11:29:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> WipCanonicalGoalEvaluation<'tcx> {
|
2023-09-20 21:41:07 +02:00
|
|
|
fn finalize(self) -> inspect::CanonicalGoalEvaluation<'tcx> {
|
2023-10-30 14:23:35 +01:00
|
|
|
assert!(self.revisions.is_empty());
|
|
|
|
let kind = match self.kind.unwrap() {
|
|
|
|
WipCanonicalGoalEvaluationKind::Overflow => {
|
2023-09-14 15:10:45 +02:00
|
|
|
inspect::CanonicalGoalEvaluationKind::Overflow
|
|
|
|
}
|
2023-10-30 14:23:35 +01:00
|
|
|
WipCanonicalGoalEvaluationKind::CycleInStack => {
|
|
|
|
inspect::CanonicalGoalEvaluationKind::CycleInStack
|
|
|
|
}
|
|
|
|
WipCanonicalGoalEvaluationKind::Interned { revisions } => {
|
|
|
|
inspect::CanonicalGoalEvaluationKind::Evaluation { revisions }
|
2023-08-14 11:29:42 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inspect::CanonicalGoalEvaluation { goal: self.goal, kind, result: self.result.unwrap() }
|
2023-06-08 23:24:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 11:34:57 +02:00
|
|
|
#[derive(Eq, PartialEq, Debug)]
|
2023-09-20 21:41:07 +02:00
|
|
|
struct WipAddedGoalsEvaluation<'tcx> {
|
|
|
|
evaluations: Vec<Vec<WipGoalEvaluation<'tcx>>>,
|
|
|
|
result: Option<Result<Certainty, NoSolution>>,
|
2023-06-08 23:24:01 +01:00
|
|
|
}
|
2023-06-20 12:40:18 +02:00
|
|
|
|
2023-06-08 23:24:01 +01:00
|
|
|
impl<'tcx> WipAddedGoalsEvaluation<'tcx> {
|
2023-09-20 21:41:07 +02:00
|
|
|
fn finalize(self) -> inspect::AddedGoalsEvaluation<'tcx> {
|
2023-06-08 23:24:01 +01:00
|
|
|
inspect::AddedGoalsEvaluation {
|
|
|
|
evaluations: self
|
|
|
|
.evaluations
|
|
|
|
.into_iter()
|
|
|
|
.map(|evaluations| {
|
|
|
|
evaluations.into_iter().map(WipGoalEvaluation::finalize).collect()
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
result: self.result.unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 11:34:57 +02:00
|
|
|
#[derive(Eq, PartialEq, Debug)]
|
2023-09-20 21:41:07 +02:00
|
|
|
struct WipGoalEvaluationStep<'tcx> {
|
|
|
|
instantiated_goal: QueryInput<'tcx, ty::Predicate<'tcx>>,
|
2023-06-08 23:24:01 +01:00
|
|
|
|
2023-09-20 21:41:07 +02:00
|
|
|
evaluation: WipProbe<'tcx>,
|
2023-06-08 23:24:01 +01:00
|
|
|
}
|
2023-06-20 12:40:18 +02:00
|
|
|
|
2023-06-08 23:24:01 +01:00
|
|
|
impl<'tcx> WipGoalEvaluationStep<'tcx> {
|
2023-09-20 21:41:07 +02:00
|
|
|
fn finalize(self) -> inspect::GoalEvaluationStep<'tcx> {
|
2023-09-11 15:48:04 +02:00
|
|
|
let evaluation = self.evaluation.finalize();
|
|
|
|
match evaluation.kind {
|
2023-09-20 21:41:07 +02:00
|
|
|
inspect::ProbeKind::Root { .. } => (),
|
2023-09-11 15:48:04 +02:00
|
|
|
_ => unreachable!("unexpected root evaluation: {evaluation:?}"),
|
2023-06-08 23:24:01 +01:00
|
|
|
}
|
2023-09-11 15:48:04 +02:00
|
|
|
inspect::GoalEvaluationStep { instantiated_goal: self.instantiated_goal, evaluation }
|
2023-06-08 23:24:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 11:34:57 +02:00
|
|
|
#[derive(Eq, PartialEq, Debug)]
|
2023-09-20 21:41:07 +02:00
|
|
|
struct WipProbe<'tcx> {
|
2023-09-14 10:20:23 +02:00
|
|
|
pub steps: Vec<WipProbeStep<'tcx>>,
|
2023-09-20 21:41:07 +02:00
|
|
|
pub kind: Option<inspect::ProbeKind<'tcx>>,
|
2023-06-08 23:24:01 +01:00
|
|
|
}
|
2023-06-20 12:40:18 +02:00
|
|
|
|
2023-09-14 09:58:29 +02:00
|
|
|
impl<'tcx> WipProbe<'tcx> {
|
2023-09-20 21:41:07 +02:00
|
|
|
fn finalize(self) -> inspect::Probe<'tcx> {
|
2023-09-14 09:58:29 +02:00
|
|
|
inspect::Probe {
|
2023-09-14 10:20:23 +02:00
|
|
|
steps: self.steps.into_iter().map(WipProbeStep::finalize).collect(),
|
2023-06-08 23:24:01 +01:00
|
|
|
kind: self.kind.unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 10:20:23 +02:00
|
|
|
#[derive(Eq, PartialEq, Debug)]
|
2023-09-20 21:41:07 +02:00
|
|
|
enum WipProbeStep<'tcx> {
|
|
|
|
AddGoal(inspect::CanonicalState<'tcx, Goal<'tcx, ty::Predicate<'tcx>>>),
|
2023-09-14 10:20:23 +02:00
|
|
|
EvaluateGoals(WipAddedGoalsEvaluation<'tcx>),
|
|
|
|
NestedProbe(WipProbe<'tcx>),
|
2023-10-26 13:22:08 +02:00
|
|
|
CommitIfOkStart,
|
|
|
|
CommitIfOkSuccess,
|
2023-09-14 10:20:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> WipProbeStep<'tcx> {
|
2023-09-20 21:41:07 +02:00
|
|
|
fn finalize(self) -> inspect::ProbeStep<'tcx> {
|
2023-09-14 10:20:23 +02:00
|
|
|
match self {
|
2023-09-14 10:41:36 +02:00
|
|
|
WipProbeStep::AddGoal(goal) => inspect::ProbeStep::AddGoal(goal),
|
2023-09-14 10:20:23 +02:00
|
|
|
WipProbeStep::EvaluateGoals(eval) => inspect::ProbeStep::EvaluateGoals(eval.finalize()),
|
|
|
|
WipProbeStep::NestedProbe(probe) => inspect::ProbeStep::NestedProbe(probe.finalize()),
|
2023-10-26 13:22:08 +02:00
|
|
|
WipProbeStep::CommitIfOkStart => inspect::ProbeStep::CommitIfOkStart,
|
|
|
|
WipProbeStep::CommitIfOkSuccess => inspect::ProbeStep::CommitIfOkSuccess,
|
2023-09-14 10:20:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 19:58:04 +01:00
|
|
|
impl<'tcx> ProofTreeBuilder<'tcx> {
|
2023-10-30 14:23:35 +01:00
|
|
|
fn new(state: impl Into<DebugSolver<'tcx>>) -> ProofTreeBuilder<'tcx> {
|
|
|
|
ProofTreeBuilder { state: Some(Box::new(state.into())) }
|
2023-07-04 14:56:05 +01:00
|
|
|
}
|
|
|
|
|
2023-08-14 11:48:40 +02:00
|
|
|
fn nested<T: Into<DebugSolver<'tcx>>>(&self, state: impl FnOnce() -> T) -> Self {
|
2023-10-30 14:23:35 +01:00
|
|
|
ProofTreeBuilder { state: self.state.as_ref().map(|_| Box::new(state().into())) }
|
2023-06-20 12:40:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn as_mut(&mut self) -> Option<&mut DebugSolver<'tcx>> {
|
2023-10-30 14:23:35 +01:00
|
|
|
self.state.as_deref_mut()
|
2023-06-20 12:40:18 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 23:24:01 +01:00
|
|
|
pub fn finalize(self) -> Option<inspect::GoalEvaluation<'tcx>> {
|
2023-10-30 14:23:35 +01:00
|
|
|
match *self.state? {
|
2023-06-08 23:24:01 +01:00
|
|
|
DebugSolver::GoalEvaluation(wip_goal_evaluation) => {
|
|
|
|
Some(wip_goal_evaluation.finalize())
|
|
|
|
}
|
2023-06-08 23:58:34 +01:00
|
|
|
root => unreachable!("unexpected proof tree builder root node: {:?}", root),
|
2023-06-08 23:24:01 +01:00
|
|
|
}
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:01:54 +01:00
|
|
|
pub fn new_maybe_root(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
generate_proof_tree: GenerateProofTree,
|
|
|
|
) -> ProofTreeBuilder<'tcx> {
|
|
|
|
match generate_proof_tree {
|
2023-07-10 15:17:01 +02:00
|
|
|
GenerateProofTree::Never => ProofTreeBuilder::new_noop(),
|
|
|
|
GenerateProofTree::IfEnabled => {
|
|
|
|
let opts = &tcx.sess.opts.unstable_opts;
|
2023-12-14 13:00:23 +01:00
|
|
|
match opts.next_solver.map(|c| c.dump_tree).unwrap_or_default() {
|
2023-10-30 14:23:35 +01:00
|
|
|
DumpSolverProofTree::Always => ProofTreeBuilder::new_root(),
|
2023-07-10 15:17:01 +02:00
|
|
|
// `OnError` is handled by reevaluating goals in error
|
|
|
|
// reporting with `GenerateProofTree::Yes`.
|
|
|
|
DumpSolverProofTree::OnError | DumpSolverProofTree::Never => {
|
|
|
|
ProofTreeBuilder::new_noop()
|
|
|
|
}
|
|
|
|
}
|
2023-07-04 10:01:54 +01:00
|
|
|
}
|
2023-10-30 14:23:35 +01:00
|
|
|
GenerateProofTree::Yes => ProofTreeBuilder::new_root(),
|
2023-07-04 10:01:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 14:23:35 +01:00
|
|
|
pub fn new_root() -> ProofTreeBuilder<'tcx> {
|
|
|
|
ProofTreeBuilder::new(DebugSolver::Root)
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
|
2023-06-08 19:58:04 +01:00
|
|
|
pub fn new_noop() -> ProofTreeBuilder<'tcx> {
|
2023-07-04 14:56:05 +01:00
|
|
|
ProofTreeBuilder { state: None }
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
|
2023-06-08 23:24:01 +01:00
|
|
|
pub fn is_noop(&self) -> bool {
|
2023-06-20 12:40:18 +02:00
|
|
|
self.state.is_none()
|
2023-06-08 23:24:01 +01:00
|
|
|
}
|
|
|
|
|
2023-09-20 21:41:07 +02:00
|
|
|
pub(in crate::solve) fn new_goal_evaluation(
|
2023-06-08 19:10:07 +01:00
|
|
|
&mut self,
|
|
|
|
goal: Goal<'tcx, ty::Predicate<'tcx>>,
|
2023-09-20 21:41:07 +02:00
|
|
|
orig_values: &[ty::GenericArg<'tcx>],
|
|
|
|
kind: solve::GoalEvaluationKind,
|
2023-06-08 19:58:04 +01:00
|
|
|
) -> ProofTreeBuilder<'tcx> {
|
2023-08-14 11:48:40 +02:00
|
|
|
self.nested(|| WipGoalEvaluation {
|
2023-07-04 14:56:05 +01:00
|
|
|
uncanonicalized_goal: goal,
|
2023-09-14 15:10:45 +02:00
|
|
|
kind: match kind {
|
2023-09-20 21:41:07 +02:00
|
|
|
solve::GoalEvaluationKind::Root => {
|
|
|
|
WipGoalEvaluationKind::Root { orig_values: orig_values.to_vec() }
|
|
|
|
}
|
|
|
|
solve::GoalEvaluationKind::Nested { is_normalizes_to_hack } => {
|
2023-09-14 15:10:45 +02:00
|
|
|
WipGoalEvaluationKind::Nested { is_normalizes_to_hack }
|
|
|
|
}
|
|
|
|
},
|
2023-08-14 11:29:42 +02:00
|
|
|
evaluation: None,
|
2023-07-04 14:56:05 +01:00
|
|
|
returned_goals: vec![],
|
2023-08-14 11:29:42 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_canonical_goal_evaluation(
|
|
|
|
&mut self,
|
|
|
|
goal: CanonicalInput<'tcx>,
|
|
|
|
) -> ProofTreeBuilder<'tcx> {
|
2023-08-14 11:48:40 +02:00
|
|
|
self.nested(|| WipCanonicalGoalEvaluation {
|
2023-08-14 11:29:42 +02:00
|
|
|
goal,
|
2023-09-11 15:48:04 +02:00
|
|
|
kind: None,
|
|
|
|
revisions: vec![],
|
2023-07-04 14:56:05 +01:00
|
|
|
result: None,
|
|
|
|
})
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
2023-06-20 12:40:18 +02:00
|
|
|
|
2023-10-30 14:23:35 +01:00
|
|
|
pub fn finalize_evaluation(
|
|
|
|
&mut self,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
) -> Option<&'tcx [inspect::GoalEvaluationStep<'tcx>]> {
|
|
|
|
self.as_mut().map(|this| match this {
|
|
|
|
DebugSolver::CanonicalGoalEvaluation(evaluation) => {
|
|
|
|
let revisions = mem::take(&mut evaluation.revisions)
|
|
|
|
.into_iter()
|
|
|
|
.map(WipGoalEvaluationStep::finalize);
|
|
|
|
let revisions = &*tcx.arena.alloc_from_iter(revisions);
|
|
|
|
let kind = WipCanonicalGoalEvaluationKind::Interned { revisions };
|
|
|
|
assert_eq!(evaluation.kind.replace(kind), None);
|
|
|
|
revisions
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-14 11:29:42 +02:00
|
|
|
pub fn canonical_goal_evaluation(&mut self, canonical_goal_evaluation: ProofTreeBuilder<'tcx>) {
|
2023-06-20 12:40:18 +02:00
|
|
|
if let Some(this) = self.as_mut() {
|
2023-10-30 14:23:35 +01:00
|
|
|
match (this, *canonical_goal_evaluation.state.unwrap()) {
|
2023-08-14 11:29:42 +02:00
|
|
|
(
|
|
|
|
DebugSolver::GoalEvaluation(goal_evaluation),
|
|
|
|
DebugSolver::CanonicalGoalEvaluation(canonical_goal_evaluation),
|
|
|
|
) => goal_evaluation.evaluation = Some(canonical_goal_evaluation),
|
2023-06-20 12:40:18 +02:00
|
|
|
_ => unreachable!(),
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 12:40:18 +02:00
|
|
|
|
2023-10-30 14:23:35 +01:00
|
|
|
pub fn goal_evaluation_kind(&mut self, kind: WipCanonicalGoalEvaluationKind<'tcx>) {
|
2023-06-20 12:40:18 +02:00
|
|
|
if let Some(this) = self.as_mut() {
|
|
|
|
match this {
|
2023-08-14 11:29:42 +02:00
|
|
|
DebugSolver::CanonicalGoalEvaluation(canonical_goal_evaluation) => {
|
2023-09-11 15:48:04 +02:00
|
|
|
assert_eq!(canonical_goal_evaluation.kind.replace(kind), None);
|
2023-06-20 12:40:18 +02:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
}
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
2023-06-20 12:40:18 +02:00
|
|
|
|
2023-06-08 23:58:34 +01:00
|
|
|
pub fn returned_goals(&mut self, goals: &[Goal<'tcx, ty::Predicate<'tcx>>]) {
|
2023-06-20 12:40:18 +02:00
|
|
|
if let Some(this) = self.as_mut() {
|
|
|
|
match this {
|
|
|
|
DebugSolver::GoalEvaluation(evaluation) => {
|
|
|
|
assert!(evaluation.returned_goals.is_empty());
|
|
|
|
evaluation.returned_goals.extend(goals);
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
2023-06-08 23:58:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-08 19:58:04 +01:00
|
|
|
pub fn goal_evaluation(&mut self, goal_evaluation: ProofTreeBuilder<'tcx>) {
|
2023-06-20 12:40:18 +02:00
|
|
|
if let Some(this) = self.as_mut() {
|
2023-10-30 14:23:35 +01:00
|
|
|
match (this, *goal_evaluation.state.unwrap()) {
|
2023-06-20 12:40:18 +02:00
|
|
|
(
|
|
|
|
DebugSolver::AddedGoalsEvaluation(WipAddedGoalsEvaluation {
|
|
|
|
evaluations, ..
|
|
|
|
}),
|
|
|
|
DebugSolver::GoalEvaluation(goal_evaluation),
|
|
|
|
) => evaluations.last_mut().unwrap().push(goal_evaluation),
|
|
|
|
(this @ DebugSolver::Root, goal_evaluation) => *this = goal_evaluation,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 19:58:04 +01:00
|
|
|
pub fn new_goal_evaluation_step(
|
2023-06-08 19:10:07 +01:00
|
|
|
&mut self,
|
|
|
|
instantiated_goal: QueryInput<'tcx, ty::Predicate<'tcx>>,
|
2023-06-08 19:58:04 +01:00
|
|
|
) -> ProofTreeBuilder<'tcx> {
|
2023-08-14 11:48:40 +02:00
|
|
|
self.nested(|| WipGoalEvaluationStep {
|
2023-07-04 14:56:05 +01:00
|
|
|
instantiated_goal,
|
2023-09-14 10:20:23 +02:00
|
|
|
evaluation: WipProbe { steps: vec![], kind: None },
|
2023-07-04 14:56:05 +01:00
|
|
|
})
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
2023-08-14 11:29:42 +02:00
|
|
|
pub fn goal_evaluation_step(&mut self, goal_evaluation_step: ProofTreeBuilder<'tcx>) {
|
2023-06-20 12:40:18 +02:00
|
|
|
if let Some(this) = self.as_mut() {
|
2023-10-30 14:23:35 +01:00
|
|
|
match (this, *goal_evaluation_step.state.unwrap()) {
|
2023-08-14 11:29:42 +02:00
|
|
|
(
|
|
|
|
DebugSolver::CanonicalGoalEvaluation(canonical_goal_evaluations),
|
|
|
|
DebugSolver::GoalEvaluationStep(goal_evaluation_step),
|
|
|
|
) => {
|
2023-09-11 15:48:04 +02:00
|
|
|
canonical_goal_evaluations.revisions.push(goal_evaluation_step);
|
2023-06-20 12:40:18 +02:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 09:58:29 +02:00
|
|
|
pub fn new_probe(&mut self) -> ProofTreeBuilder<'tcx> {
|
2023-09-14 10:20:23 +02:00
|
|
|
self.nested(|| WipProbe { steps: vec![], kind: None })
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
2023-06-20 12:40:18 +02:00
|
|
|
|
2023-09-20 21:41:07 +02:00
|
|
|
pub fn probe_kind(&mut self, probe_kind: inspect::ProbeKind<'tcx>) {
|
2023-06-20 12:40:18 +02:00
|
|
|
if let Some(this) = self.as_mut() {
|
|
|
|
match this {
|
2023-09-14 09:58:29 +02:00
|
|
|
DebugSolver::Probe(this) => {
|
2023-09-11 11:34:57 +02:00
|
|
|
assert_eq!(this.kind.replace(probe_kind), None)
|
2023-06-20 12:40:18 +02:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 12:40:18 +02:00
|
|
|
|
2023-09-20 21:41:07 +02:00
|
|
|
pub fn add_goal(ecx: &mut EvalCtxt<'_, 'tcx>, goal: Goal<'tcx, ty::Predicate<'tcx>>) {
|
|
|
|
// Can't use `if let Some(this) = ecx.inspect.as_mut()` here because
|
|
|
|
// we have to immutably use the `EvalCtxt` for `make_canonical_state`.
|
|
|
|
if ecx.inspect.is_noop() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let goal = Self::make_canonical_state(ecx, goal);
|
|
|
|
|
|
|
|
match ecx.inspect.as_mut().unwrap() {
|
|
|
|
DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
|
|
|
|
evaluation: WipProbe { steps, .. },
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| DebugSolver::Probe(WipProbe { steps, .. }) => steps.push(WipProbeStep::AddGoal(goal)),
|
|
|
|
s => unreachable!("tried to add {goal:?} to {s:?}"),
|
2023-09-14 10:41:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 09:58:29 +02:00
|
|
|
pub fn finish_probe(&mut self, probe: ProofTreeBuilder<'tcx>) {
|
2023-06-20 12:40:18 +02:00
|
|
|
if let Some(this) = self.as_mut() {
|
2023-10-30 14:23:35 +01:00
|
|
|
match (this, *probe.state.unwrap()) {
|
2023-06-20 12:40:18 +02:00
|
|
|
(
|
2023-09-14 10:20:23 +02:00
|
|
|
DebugSolver::Probe(WipProbe { steps, .. })
|
2023-09-11 15:48:04 +02:00
|
|
|
| DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
|
2023-09-14 10:20:23 +02:00
|
|
|
evaluation: WipProbe { steps, .. },
|
2023-09-11 15:48:04 +02:00
|
|
|
..
|
|
|
|
}),
|
2023-09-14 09:58:29 +02:00
|
|
|
DebugSolver::Probe(probe),
|
2023-09-14 10:20:23 +02:00
|
|
|
) => steps.push(WipProbeStep::NestedProbe(probe)),
|
2023-06-20 12:40:18 +02:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 13:22:08 +02:00
|
|
|
/// Used by `EvalCtxt::commit_if_ok` to flatten the work done inside
|
|
|
|
/// of the probe into the parent.
|
|
|
|
pub fn integrate_snapshot(&mut self, probe: ProofTreeBuilder<'tcx>) {
|
|
|
|
if let Some(this) = self.as_mut() {
|
|
|
|
match (this, *probe.state.unwrap()) {
|
|
|
|
(
|
|
|
|
DebugSolver::Probe(WipProbe { steps, .. })
|
|
|
|
| DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
|
|
|
|
evaluation: WipProbe { steps, .. },
|
|
|
|
..
|
|
|
|
}),
|
|
|
|
DebugSolver::Probe(probe),
|
|
|
|
) => {
|
|
|
|
steps.push(WipProbeStep::CommitIfOkStart);
|
|
|
|
assert_eq!(probe.kind, None);
|
|
|
|
steps.extend(probe.steps);
|
|
|
|
steps.push(WipProbeStep::CommitIfOkSuccess);
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 19:58:04 +01:00
|
|
|
pub fn new_evaluate_added_goals(&mut self) -> ProofTreeBuilder<'tcx> {
|
2023-08-14 11:48:40 +02:00
|
|
|
self.nested(|| WipAddedGoalsEvaluation { evaluations: vec![], result: None })
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
2023-06-20 12:40:18 +02:00
|
|
|
|
2023-06-08 19:58:04 +01:00
|
|
|
pub fn evaluate_added_goals_loop_start(&mut self) {
|
2023-06-20 12:40:18 +02:00
|
|
|
if let Some(this) = self.as_mut() {
|
|
|
|
match this {
|
|
|
|
DebugSolver::AddedGoalsEvaluation(this) => {
|
|
|
|
this.evaluations.push(vec![]);
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 12:40:18 +02:00
|
|
|
|
2023-06-08 19:58:04 +01:00
|
|
|
pub fn eval_added_goals_result(&mut self, result: Result<Certainty, NoSolution>) {
|
2023-06-20 12:40:18 +02:00
|
|
|
if let Some(this) = self.as_mut() {
|
|
|
|
match this {
|
|
|
|
DebugSolver::AddedGoalsEvaluation(this) => {
|
|
|
|
assert_eq!(this.result.replace(result), None);
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 12:40:18 +02:00
|
|
|
|
2023-08-14 11:29:42 +02:00
|
|
|
pub fn added_goals_evaluation(&mut self, added_goals_evaluation: ProofTreeBuilder<'tcx>) {
|
2023-06-20 12:40:18 +02:00
|
|
|
if let Some(this) = self.as_mut() {
|
2023-10-30 14:23:35 +01:00
|
|
|
match (this, *added_goals_evaluation.state.unwrap()) {
|
2023-06-20 12:40:18 +02:00
|
|
|
(
|
|
|
|
DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
|
2023-09-14 10:20:23 +02:00
|
|
|
evaluation: WipProbe { steps, .. },
|
2023-06-20 12:40:18 +02:00
|
|
|
..
|
|
|
|
})
|
2023-09-14 10:20:23 +02:00
|
|
|
| DebugSolver::Probe(WipProbe { steps, .. }),
|
2023-06-20 12:40:18 +02:00
|
|
|
DebugSolver::AddedGoalsEvaluation(added_goals_evaluation),
|
2023-09-14 10:20:23 +02:00
|
|
|
) => steps.push(WipProbeStep::EvaluateGoals(added_goals_evaluation)),
|
2023-06-20 12:40:18 +02:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 19:58:04 +01:00
|
|
|
pub fn query_result(&mut self, result: QueryResult<'tcx>) {
|
2023-06-20 12:40:18 +02:00
|
|
|
if let Some(this) = self.as_mut() {
|
|
|
|
match this {
|
2023-08-14 11:29:42 +02:00
|
|
|
DebugSolver::CanonicalGoalEvaluation(canonical_goal_evaluation) => {
|
|
|
|
assert_eq!(canonical_goal_evaluation.result.replace(result), None);
|
2023-06-20 12:40:18 +02:00
|
|
|
}
|
|
|
|
DebugSolver::GoalEvaluationStep(evaluation_step) => {
|
2023-09-11 15:48:04 +02:00
|
|
|
assert_eq!(
|
2023-09-20 21:41:07 +02:00
|
|
|
evaluation_step
|
|
|
|
.evaluation
|
|
|
|
.kind
|
|
|
|
.replace(inspect::ProbeKind::Root { result }),
|
2023-09-11 15:48:04 +02:00
|
|
|
None
|
|
|
|
);
|
2023-06-20 12:40:18 +02:00
|
|
|
}
|
2023-08-14 11:29:42 +02:00
|
|
|
_ => unreachable!(),
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|