1
Fork 0
rust/compiler/rustc_trait_selection/src/solve/inspect/mod.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

230 lines
7.4 KiB
Rust
Raw Normal View History

2023-06-08 19:10:07 +01:00
use rustc_middle::{
traits::{
query::NoSolution,
solve::{inspect::*, CanonicalInput, Certainty, Goal, QueryInput, QueryResult},
},
ty,
};
#[derive(Debug)]
pub enum DebugSolver<'tcx> {
Root,
GoalEvaluation(GoalEvaluation<'tcx>),
AddedGoalsEvaluation(AddedGoalsEvaluation<'tcx>),
GoalEvaluationStep(GoalEvaluationStep<'tcx>),
GoalCandidate(GoalCandidate<'tcx>),
}
2023-06-08 19:58:04 +01:00
pub struct ProofTreeBuilder<'tcx>(Option<Box<DebugSolver<'tcx>>>);
2023-06-08 19:10:07 +01:00
2023-06-08 19:58:04 +01:00
impl<'tcx> ProofTreeBuilder<'tcx> {
pub fn into_proof_tree(self) -> Option<DebugSolver<'tcx>> {
self.0.map(|tree| *tree)
2023-06-08 19:10:07 +01:00
}
2023-06-08 19:58:04 +01:00
pub fn new_root() -> ProofTreeBuilder<'tcx> {
Self(Some(Box::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> {
Self(None)
2023-06-08 19:10:07 +01:00
}
2023-06-08 19:58:04 +01:00
pub fn new_goal_evaluation(
2023-06-08 19:10:07 +01:00
&mut self,
goal: Goal<'tcx, ty::Predicate<'tcx>>,
2023-06-08 19:58:04 +01:00
) -> ProofTreeBuilder<'tcx> {
if self.0.is_none() {
return ProofTreeBuilder(None);
}
Self(Some(Box::new(DebugSolver::GoalEvaluation(GoalEvaluation {
2023-06-08 19:10:07 +01:00
uncanonicalized_goal: goal,
canonicalized_goal: None,
evaluation_steps: vec![],
2023-06-08 19:21:55 +01:00
cache_hit: None,
2023-06-08 19:10:07 +01:00
result: None,
2023-06-08 19:58:04 +01:00
}))))
2023-06-08 19:10:07 +01:00
}
2023-06-08 19:58:04 +01:00
pub fn canonicalized_goal(&mut self, canonical_goal: CanonicalInput<'tcx>) {
let this = match self.0.as_mut() {
None => return,
Some(this) => &mut **this,
};
match this {
2023-06-08 19:10:07 +01:00
DebugSolver::GoalEvaluation(goal_evaluation) => {
assert!(goal_evaluation.canonicalized_goal.is_none());
goal_evaluation.canonicalized_goal = Some(canonical_goal)
}
_ => unreachable!(),
}
}
2023-06-08 19:58:04 +01:00
pub fn cache_hit(&mut self, cache_hit: CacheHit) {
let this = match self.0.as_mut() {
None => return,
Some(this) => &mut **this,
};
match this {
2023-06-08 19:21:55 +01:00
DebugSolver::GoalEvaluation(goal_evaluation) => {
goal_evaluation.cache_hit = Some(cache_hit)
}
2023-06-08 19:10:07 +01:00
_ => unreachable!(),
};
}
2023-06-08 19:58:04 +01:00
pub fn goal_evaluation(&mut self, goal_evaluation: ProofTreeBuilder<'tcx>) {
let this = match self.0.as_mut() {
None => return,
Some(this) => &mut **this,
};
match (this, *goal_evaluation.0.unwrap()) {
2023-06-08 19:10:07 +01:00
(
DebugSolver::AddedGoalsEvaluation(AddedGoalsEvaluation { 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: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> {
Self(Some(Box::new(DebugSolver::GoalEvaluationStep(GoalEvaluationStep {
2023-06-08 19:10:07 +01:00
instantiated_goal,
nested_goal_evaluations: vec![],
candidates: vec![],
result: None,
2023-06-08 19:58:04 +01:00
}))))
2023-06-08 19:10:07 +01:00
}
2023-06-08 19:58:04 +01:00
pub fn goal_evaluation_step(&mut self, goal_eval_step: ProofTreeBuilder<'tcx>) {
let this = match self.0.as_mut() {
None => return,
Some(this) => &mut **this,
};
match (this, *goal_eval_step.0.unwrap()) {
2023-06-08 19:10:07 +01:00
(DebugSolver::GoalEvaluation(goal_eval), DebugSolver::GoalEvaluationStep(step)) => {
goal_eval.evaluation_steps.push(step);
}
_ => unreachable!(),
}
}
2023-06-08 19:58:04 +01:00
pub fn new_goal_candidate(&mut self) -> ProofTreeBuilder<'tcx> {
Self(Some(Box::new(DebugSolver::GoalCandidate(GoalCandidate {
2023-06-08 19:10:07 +01:00
nested_goal_evaluations: vec![],
candidates: vec![],
name: None,
result: None,
2023-06-08 19:58:04 +01:00
}))))
2023-06-08 19:10:07 +01:00
}
2023-06-08 19:58:04 +01:00
pub fn candidate_name(&mut self, f: &mut dyn FnMut() -> String) {
let this = match self.0.as_mut() {
None => return,
Some(this) => &mut **this,
};
2023-06-08 19:10:07 +01:00
2023-06-08 19:58:04 +01:00
match this {
2023-06-08 19:10:07 +01:00
DebugSolver::GoalCandidate(goal_candidate) => {
2023-06-08 19:58:04 +01:00
let name = f();
2023-06-08 19:10:07 +01:00
assert!(goal_candidate.name.is_none());
goal_candidate.name = Some(name);
}
_ => unreachable!(),
}
}
2023-06-08 19:58:04 +01:00
pub fn goal_candidate(&mut self, candidate: ProofTreeBuilder<'tcx>) {
let this = match self.0.as_mut() {
None => return,
Some(this) => &mut **this,
};
match (this, *candidate.0.unwrap()) {
2023-06-08 19:10:07 +01:00
(
DebugSolver::GoalCandidate(GoalCandidate { candidates, .. })
| DebugSolver::GoalEvaluationStep(GoalEvaluationStep { candidates, .. }),
DebugSolver::GoalCandidate(candidate),
) => candidates.push(candidate),
_ => unreachable!(),
}
}
2023-06-08 19:58:04 +01:00
pub fn new_evaluate_added_goals(&mut self) -> ProofTreeBuilder<'tcx> {
Self(Some(Box::new(DebugSolver::AddedGoalsEvaluation(AddedGoalsEvaluation {
2023-06-08 19:10:07 +01:00
evaluations: vec![],
result: None,
2023-06-08 19:58:04 +01:00
}))))
2023-06-08 19:10:07 +01:00
}
2023-06-08 19:58:04 +01:00
pub fn evaluate_added_goals_loop_start(&mut self) {
let this = match self.0.as_mut() {
None => return,
Some(this) => &mut **this,
};
match this {
2023-06-08 19:10:07 +01:00
DebugSolver::AddedGoalsEvaluation(this) => {
this.evaluations.push(vec![]);
}
_ => unreachable!(),
}
}
2023-06-08 19:58:04 +01:00
pub fn eval_added_goals_result(&mut self, result: Result<Certainty, NoSolution>) {
let this = match self.0.as_mut() {
None => return,
Some(this) => &mut **this,
};
match this {
2023-06-08 19:10:07 +01:00
DebugSolver::AddedGoalsEvaluation(this) => {
assert!(this.result.is_none());
this.result = Some(result);
}
_ => unreachable!(),
}
}
2023-06-08 19:58:04 +01:00
pub fn added_goals_evaluation(&mut self, goals_evaluation: ProofTreeBuilder<'tcx>) {
let this = match self.0.as_mut() {
None => return,
Some(this) => &mut **this,
};
match (this, *goals_evaluation.0.unwrap()) {
2023-06-08 19:10:07 +01:00
(
DebugSolver::GoalEvaluationStep(GoalEvaluationStep {
nested_goal_evaluations, ..
})
| DebugSolver::GoalCandidate(GoalCandidate { nested_goal_evaluations, .. }),
DebugSolver::AddedGoalsEvaluation(added_goals_evaluation),
) => nested_goal_evaluations.push(added_goals_evaluation),
_ => unreachable!(),
}
}
2023-06-08 19:58:04 +01:00
pub fn query_result(&mut self, result: QueryResult<'tcx>) {
let this = match self.0.as_mut() {
None => return,
Some(this) => &mut **this,
};
match this {
2023-06-08 19:10:07 +01:00
DebugSolver::GoalEvaluation(goal_evaluation) => {
assert!(goal_evaluation.result.is_none());
goal_evaluation.result = Some(result);
}
DebugSolver::Root | DebugSolver::AddedGoalsEvaluation(_) => unreachable!(),
DebugSolver::GoalEvaluationStep(evaluation_step) => {
assert!(evaluation_step.result.is_none());
evaluation_step.result = Some(result);
}
DebugSolver::GoalCandidate(candidate) => {
assert!(candidate.result.is_none());
candidate.result = Some(result);
}
}
}
}