2023-06-20 12:19:40 +02:00
|
|
|
use rustc_middle::traits::query::NoSolution;
|
|
|
|
use rustc_middle::traits::solve::inspect::{self, CacheHit, CandidateKind};
|
|
|
|
use rustc_middle::traits::solve::{
|
|
|
|
CanonicalInput, Certainty, Goal, IsNormalizesToHack, QueryInput, QueryResult,
|
2023-06-08 19:10:07 +01:00
|
|
|
};
|
2023-06-20 12:19:40 +02:00
|
|
|
use rustc_middle::ty;
|
2023-06-08 19:10:07 +01:00
|
|
|
|
2023-06-09 01:11:58 +01:00
|
|
|
pub mod dump;
|
|
|
|
|
2023-06-08 23:24:01 +01:00
|
|
|
#[derive(Eq, PartialEq, Debug, Hash, HashStable)]
|
|
|
|
pub struct WipGoalEvaluation<'tcx> {
|
|
|
|
pub uncanonicalized_goal: Goal<'tcx, ty::Predicate<'tcx>>,
|
|
|
|
pub canonicalized_goal: Option<CanonicalInput<'tcx>>,
|
|
|
|
|
|
|
|
pub evaluation_steps: Vec<WipGoalEvaluationStep<'tcx>>,
|
|
|
|
|
|
|
|
pub cache_hit: Option<CacheHit>,
|
2023-06-08 23:58:34 +01:00
|
|
|
pub is_normalizes_to_hack: IsNormalizesToHack,
|
|
|
|
pub returned_goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
|
2023-06-08 23:24:01 +01:00
|
|
|
|
|
|
|
pub result: Option<QueryResult<'tcx>>,
|
|
|
|
}
|
|
|
|
impl<'tcx> WipGoalEvaluation<'tcx> {
|
|
|
|
pub fn finalize(self) -> inspect::GoalEvaluation<'tcx> {
|
|
|
|
inspect::GoalEvaluation {
|
|
|
|
uncanonicalized_goal: self.uncanonicalized_goal,
|
|
|
|
canonicalized_goal: self.canonicalized_goal.unwrap(),
|
|
|
|
kind: match self.cache_hit {
|
|
|
|
Some(hit) => inspect::GoalEvaluationKind::CacheHit(hit),
|
|
|
|
None => inspect::GoalEvaluationKind::Uncached {
|
|
|
|
revisions: self
|
|
|
|
.evaluation_steps
|
|
|
|
.into_iter()
|
|
|
|
.map(WipGoalEvaluationStep::finalize)
|
|
|
|
.collect(),
|
|
|
|
},
|
|
|
|
},
|
2023-06-08 23:58:34 +01:00
|
|
|
is_normalizes_to_hack: self.is_normalizes_to_hack,
|
|
|
|
returned_goals: self.returned_goals,
|
2023-06-08 23:24:01 +01:00
|
|
|
result: self.result.unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Debug, Hash, HashStable)]
|
|
|
|
pub struct WipAddedGoalsEvaluation<'tcx> {
|
|
|
|
pub evaluations: Vec<Vec<WipGoalEvaluation<'tcx>>>,
|
|
|
|
pub result: Option<Result<Certainty, NoSolution>>,
|
|
|
|
}
|
|
|
|
impl<'tcx> WipAddedGoalsEvaluation<'tcx> {
|
|
|
|
pub fn finalize(self) -> inspect::AddedGoalsEvaluation<'tcx> {
|
|
|
|
inspect::AddedGoalsEvaluation {
|
|
|
|
evaluations: self
|
|
|
|
.evaluations
|
|
|
|
.into_iter()
|
|
|
|
.map(|evaluations| {
|
|
|
|
evaluations.into_iter().map(WipGoalEvaluation::finalize).collect()
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
result: self.result.unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Debug, Hash, HashStable)]
|
|
|
|
pub struct WipGoalEvaluationStep<'tcx> {
|
|
|
|
pub instantiated_goal: QueryInput<'tcx, ty::Predicate<'tcx>>,
|
|
|
|
|
|
|
|
pub nested_goal_evaluations: Vec<WipAddedGoalsEvaluation<'tcx>>,
|
|
|
|
pub candidates: Vec<WipGoalCandidate<'tcx>>,
|
|
|
|
|
|
|
|
pub result: Option<QueryResult<'tcx>>,
|
|
|
|
}
|
|
|
|
impl<'tcx> WipGoalEvaluationStep<'tcx> {
|
|
|
|
pub fn finalize(self) -> inspect::GoalEvaluationStep<'tcx> {
|
|
|
|
inspect::GoalEvaluationStep {
|
|
|
|
instantiated_goal: self.instantiated_goal,
|
|
|
|
nested_goal_evaluations: self
|
|
|
|
.nested_goal_evaluations
|
|
|
|
.into_iter()
|
|
|
|
.map(WipAddedGoalsEvaluation::finalize)
|
|
|
|
.collect(),
|
|
|
|
candidates: self.candidates.into_iter().map(WipGoalCandidate::finalize).collect(),
|
|
|
|
result: self.result.unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Debug, Hash, HashStable)]
|
|
|
|
pub struct WipGoalCandidate<'tcx> {
|
|
|
|
pub nested_goal_evaluations: Vec<WipAddedGoalsEvaluation<'tcx>>,
|
|
|
|
pub candidates: Vec<WipGoalCandidate<'tcx>>,
|
|
|
|
pub kind: Option<CandidateKind<'tcx>>,
|
|
|
|
}
|
|
|
|
impl<'tcx> WipGoalCandidate<'tcx> {
|
|
|
|
pub fn finalize(self) -> inspect::GoalCandidate<'tcx> {
|
|
|
|
inspect::GoalCandidate {
|
|
|
|
nested_goal_evaluations: self
|
|
|
|
.nested_goal_evaluations
|
|
|
|
.into_iter()
|
|
|
|
.map(WipAddedGoalsEvaluation::finalize)
|
|
|
|
.collect(),
|
|
|
|
candidates: self.candidates.into_iter().map(WipGoalCandidate::finalize).collect(),
|
|
|
|
kind: self.kind.unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 19:10:07 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum DebugSolver<'tcx> {
|
|
|
|
Root,
|
2023-06-08 23:24:01 +01:00
|
|
|
GoalEvaluation(WipGoalEvaluation<'tcx>),
|
|
|
|
AddedGoalsEvaluation(WipAddedGoalsEvaluation<'tcx>),
|
|
|
|
GoalEvaluationStep(WipGoalEvaluationStep<'tcx>),
|
|
|
|
GoalCandidate(WipGoalCandidate<'tcx>),
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
|
2023-06-08 19:58:04 +01:00
|
|
|
pub struct ProofTreeBuilder<'tcx>(Option<Box<DebugSolver<'tcx>>>);
|
|
|
|
impl<'tcx> ProofTreeBuilder<'tcx> {
|
2023-06-08 23:24:01 +01:00
|
|
|
pub fn finalize(self) -> Option<inspect::GoalEvaluation<'tcx>> {
|
2023-06-08 23:58:34 +01:00
|
|
|
match *(self.0?) {
|
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-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 23:24:01 +01:00
|
|
|
pub fn is_noop(&self) -> bool {
|
|
|
|
self.0.is_none()
|
|
|
|
}
|
|
|
|
|
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 23:58:34 +01:00
|
|
|
is_normalizes_to_hack: IsNormalizesToHack,
|
2023-06-08 19:58:04 +01:00
|
|
|
) -> ProofTreeBuilder<'tcx> {
|
|
|
|
if self.0.is_none() {
|
|
|
|
return ProofTreeBuilder(None);
|
|
|
|
}
|
|
|
|
|
2023-06-08 23:24:01 +01:00
|
|
|
Self(Some(Box::new(DebugSolver::GoalEvaluation(WipGoalEvaluation {
|
2023-06-08 19:10:07 +01:00
|
|
|
uncanonicalized_goal: goal,
|
|
|
|
canonicalized_goal: None,
|
|
|
|
evaluation_steps: vec![],
|
2023-06-08 23:58:34 +01:00
|
|
|
is_normalizes_to_hack,
|
2023-06-08 19:21:55 +01:00
|
|
|
cache_hit: None,
|
2023-06-08 23:58:34 +01:00
|
|
|
returned_goals: vec![],
|
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 23:58:34 +01:00
|
|
|
pub fn returned_goals(&mut self, goals: &[Goal<'tcx, ty::Predicate<'tcx>>]) {
|
|
|
|
let this = match self.0.as_mut() {
|
|
|
|
None => return,
|
|
|
|
Some(this) => &mut **this,
|
|
|
|
};
|
|
|
|
|
|
|
|
match this {
|
|
|
|
DebugSolver::GoalEvaluation(evaluation) => {
|
|
|
|
assert!(evaluation.returned_goals.is_empty());
|
|
|
|
evaluation.returned_goals.extend(goals);
|
|
|
|
}
|
|
|
|
_ => 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
|
|
|
(
|
2023-06-08 23:24:01 +01:00
|
|
|
DebugSolver::AddedGoalsEvaluation(WipAddedGoalsEvaluation { evaluations, .. }),
|
2023-06-08 19:10:07 +01:00
|
|
|
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> {
|
2023-06-08 23:24:01 +01:00
|
|
|
if self.0.is_none() {
|
|
|
|
return Self(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
Self(Some(Box::new(DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
|
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> {
|
2023-06-08 23:24:01 +01:00
|
|
|
if self.0.is_none() {
|
|
|
|
return Self(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
Self(Some(Box::new(DebugSolver::GoalCandidate(WipGoalCandidate {
|
2023-06-08 19:10:07 +01:00
|
|
|
nested_goal_evaluations: vec![],
|
|
|
|
candidates: vec![],
|
2023-06-08 23:24:01 +01:00
|
|
|
kind: None,
|
2023-06-08 19:58:04 +01:00
|
|
|
}))))
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
2023-06-08 23:24:01 +01:00
|
|
|
pub fn candidate_kind(&mut self, kind: CandidateKind<'tcx>) {
|
2023-06-08 19:58:04 +01:00
|
|
|
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 23:24:01 +01:00
|
|
|
DebugSolver::GoalCandidate(WipGoalCandidate { kind: old_kind @ None, .. }) => {
|
|
|
|
*old_kind = Some(kind)
|
2023-06-08 19:10:07 +01:00
|
|
|
}
|
|
|
|
_ => 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
|
|
|
(
|
2023-06-08 23:24:01 +01:00
|
|
|
DebugSolver::GoalCandidate(WipGoalCandidate { candidates, .. })
|
|
|
|
| DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep { candidates, .. }),
|
2023-06-08 19:10:07 +01:00
|
|
|
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> {
|
2023-06-08 23:24:01 +01:00
|
|
|
if self.0.is_none() {
|
|
|
|
return Self(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
Self(Some(Box::new(DebugSolver::AddedGoalsEvaluation(WipAddedGoalsEvaluation {
|
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
|
|
|
(
|
2023-06-08 23:24:01 +01:00
|
|
|
DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
|
|
|
|
nested_goal_evaluations,
|
|
|
|
..
|
2023-06-08 19:10:07 +01:00
|
|
|
})
|
2023-06-08 23:24:01 +01:00
|
|
|
| DebugSolver::GoalCandidate(WipGoalCandidate { nested_goal_evaluations, .. }),
|
2023-06-08 19:10:07 +01:00
|
|
|
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);
|
|
|
|
}
|
2023-06-08 23:24:01 +01:00
|
|
|
DebugSolver::Root
|
|
|
|
| DebugSolver::AddedGoalsEvaluation(_)
|
|
|
|
| DebugSolver::GoalCandidate(_) => unreachable!(),
|
2023-06-08 19:10:07 +01:00
|
|
|
DebugSolver::GoalEvaluationStep(evaluation_step) => {
|
|
|
|
assert!(evaluation_step.result.is_none());
|
|
|
|
evaluation_step.result = Some(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|