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

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

449 lines
15 KiB
Rust
Raw Normal View History

2023-06-20 12:19:40 +02:00
use rustc_middle::traits::query::NoSolution;
2023-09-11 11:34:57 +02:00
use rustc_middle::traits::solve::inspect::{self, CacheHit, ProbeKind};
2023-06-20 12:19:40 +02:00
use rustc_middle::traits::solve::{
CanonicalInput, Certainty, Goal, IsNormalizesToHack, QueryInput, QueryResult,
2023-06-08 19:10:07 +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-07-04 14:56:05 +01:00
use super::eval_ctxt::UseGlobalCache;
use super::GenerateProofTree;
2023-09-11 11:34:57 +02:00
#[derive(Eq, PartialEq, Debug)]
pub struct WipGoalEvaluation<'tcx> {
pub uncanonicalized_goal: Goal<'tcx, ty::Predicate<'tcx>>,
pub evaluation: Option<WipCanonicalGoalEvaluation<'tcx>>,
pub is_normalizes_to_hack: IsNormalizesToHack,
pub returned_goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
}
2023-06-20 12:40:18 +02:00
impl<'tcx> WipGoalEvaluation<'tcx> {
pub fn finalize(self) -> inspect::GoalEvaluation<'tcx> {
inspect::GoalEvaluation {
uncanonicalized_goal: self.uncanonicalized_goal,
evaluation: self.evaluation.unwrap().finalize(),
is_normalizes_to_hack: self.is_normalizes_to_hack,
returned_goals: self.returned_goals,
}
}
}
#[derive(Eq, PartialEq, Debug)]
pub enum WipGoalEvaluationKind {
Overflow,
CacheHit(CacheHit),
}
2023-09-11 11:34:57 +02:00
#[derive(Eq, PartialEq, Debug)]
pub struct WipCanonicalGoalEvaluation<'tcx> {
pub goal: CanonicalInput<'tcx>,
pub kind: Option<WipGoalEvaluationKind>,
pub revisions: Vec<WipGoalEvaluationStep<'tcx>>,
pub result: Option<QueryResult<'tcx>>,
}
impl<'tcx> WipCanonicalGoalEvaluation<'tcx> {
pub fn finalize(self) -> inspect::CanonicalGoalEvaluation<'tcx> {
let kind = match self.kind {
Some(WipGoalEvaluationKind::Overflow) => inspect::GoalEvaluationKind::Overflow,
Some(WipGoalEvaluationKind::CacheHit(hit)) => {
inspect::GoalEvaluationKind::CacheHit(hit)
}
None => inspect::GoalEvaluationKind::Uncached {
revisions: self
.revisions
.into_iter()
.map(WipGoalEvaluationStep::finalize)
.collect(),
},
};
inspect::CanonicalGoalEvaluation { goal: self.goal, kind, result: self.result.unwrap() }
}
}
2023-09-11 11:34:57 +02:00
#[derive(Eq, PartialEq, Debug)]
pub struct WipAddedGoalsEvaluation<'tcx> {
pub evaluations: Vec<Vec<WipGoalEvaluation<'tcx>>>,
pub result: Option<Result<Certainty, NoSolution>>,
}
2023-06-20 12:40:18 +02:00
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(),
}
}
}
2023-09-11 11:34:57 +02:00
#[derive(Eq, PartialEq, Debug)]
pub struct WipGoalEvaluationStep<'tcx> {
pub instantiated_goal: QueryInput<'tcx, ty::Predicate<'tcx>>,
pub evaluation: WipGoalCandidate<'tcx>,
}
2023-06-20 12:40:18 +02:00
impl<'tcx> WipGoalEvaluationStep<'tcx> {
pub fn finalize(self) -> inspect::GoalEvaluationStep<'tcx> {
let evaluation = self.evaluation.finalize();
match evaluation.kind {
ProbeKind::Root { .. } => (),
_ => unreachable!("unexpected root evaluation: {evaluation:?}"),
}
inspect::GoalEvaluationStep { instantiated_goal: self.instantiated_goal, evaluation }
}
}
2023-09-11 11:34:57 +02:00
#[derive(Eq, PartialEq, Debug)]
pub struct WipGoalCandidate<'tcx> {
pub added_goals_evaluations: Vec<WipAddedGoalsEvaluation<'tcx>>,
pub candidates: Vec<WipGoalCandidate<'tcx>>,
2023-09-11 11:34:57 +02:00
pub kind: Option<ProbeKind<'tcx>>,
}
2023-06-20 12:40:18 +02:00
impl<'tcx> WipGoalCandidate<'tcx> {
pub fn finalize(self) -> inspect::GoalCandidate<'tcx> {
inspect::GoalCandidate {
added_goals_evaluations: self
.added_goals_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,
GoalEvaluation(WipGoalEvaluation<'tcx>),
CanonicalGoalEvaluation(WipCanonicalGoalEvaluation<'tcx>),
AddedGoalsEvaluation(WipAddedGoalsEvaluation<'tcx>),
GoalEvaluationStep(WipGoalEvaluationStep<'tcx>),
GoalCandidate(WipGoalCandidate<'tcx>),
2023-06-08 19:10:07 +01:00
}
2023-06-20 12:40:18 +02:00
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)
}
}
2023-06-20 12:40:18 +02:00
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<WipGoalCandidate<'tcx>> for DebugSolver<'tcx> {
fn from(g: WipGoalCandidate<'tcx>) -> DebugSolver<'tcx> {
DebugSolver::GoalCandidate(g)
}
}
pub struct ProofTreeBuilder<'tcx> {
2023-07-04 14:56:05 +01:00
state: Option<Box<BuilderData<'tcx>>>,
}
struct BuilderData<'tcx> {
tree: DebugSolver<'tcx>,
use_global_cache: UseGlobalCache,
2023-06-20 12:40:18 +02:00
}
2023-06-08 19:58:04 +01:00
impl<'tcx> ProofTreeBuilder<'tcx> {
fn new(
state: impl Into<DebugSolver<'tcx>>,
2023-07-04 14:56:05 +01:00
use_global_cache: UseGlobalCache,
) -> ProofTreeBuilder<'tcx> {
2023-07-04 14:56:05 +01:00
ProofTreeBuilder {
state: Some(Box::new(BuilderData { tree: state.into(), use_global_cache })),
}
}
2023-08-14 11:48:40 +02:00
fn nested<T: Into<DebugSolver<'tcx>>>(&self, state: impl FnOnce() -> T) -> Self {
2023-07-04 14:56:05 +01:00
match &self.state {
Some(prev_state) => Self {
state: Some(Box::new(BuilderData {
2023-08-14 11:48:40 +02:00
tree: state().into(),
2023-07-04 14:56:05 +01:00
use_global_cache: prev_state.use_global_cache,
})),
},
None => Self { state: None },
}
2023-06-20 12:40:18 +02:00
}
fn as_mut(&mut self) -> Option<&mut DebugSolver<'tcx>> {
2023-07-04 14:56:05 +01:00
self.state.as_mut().map(|boxed| &mut boxed.tree)
2023-06-20 12:40:18 +02:00
}
pub fn finalize(self) -> Option<inspect::GoalEvaluation<'tcx>> {
2023-07-04 14:56:05 +01:00
match self.state?.tree {
DebugSolver::GoalEvaluation(wip_goal_evaluation) => {
Some(wip_goal_evaluation.finalize())
}
root => unreachable!("unexpected proof tree builder root node: {:?}", root),
}
2023-06-08 19:10:07 +01:00
}
2023-07-04 14:56:05 +01:00
pub fn use_global_cache(&self) -> bool {
self.state
.as_ref()
.map(|state| matches!(state.use_global_cache, UseGlobalCache::Yes))
.unwrap_or(true)
}
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;
match opts.dump_solver_proof_tree {
DumpSolverProofTree::Always => {
let use_cache = opts.dump_solver_proof_tree_use_cache.unwrap_or(true);
ProofTreeBuilder::new_root(UseGlobalCache::from_bool(use_cache))
}
// `OnError` is handled by reevaluating goals in error
// reporting with `GenerateProofTree::Yes`.
DumpSolverProofTree::OnError | DumpSolverProofTree::Never => {
ProofTreeBuilder::new_noop()
}
}
}
2023-07-10 15:17:01 +02:00
GenerateProofTree::Yes(use_cache) => ProofTreeBuilder::new_root(use_cache),
}
}
2023-07-04 14:56:05 +01:00
pub fn new_root(use_global_cache: UseGlobalCache) -> ProofTreeBuilder<'tcx> {
ProofTreeBuilder::new(DebugSolver::Root, use_global_cache)
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
}
pub fn is_noop(&self) -> bool {
2023-06-20 12:40:18 +02:00
self.state.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>>,
is_normalizes_to_hack: IsNormalizesToHack,
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,
evaluation: None,
2023-07-04 14:56:05 +01:00
is_normalizes_to_hack,
returned_goals: vec![],
})
}
pub fn new_canonical_goal_evaluation(
&mut self,
goal: CanonicalInput<'tcx>,
) -> ProofTreeBuilder<'tcx> {
2023-08-14 11:48:40 +02:00
self.nested(|| WipCanonicalGoalEvaluation {
goal,
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
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() {
match (this, canonical_goal_evaluation.state.unwrap().tree) {
(
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
pub fn goal_evaluation_kind(&mut self, kind: WipGoalEvaluationKind) {
2023-06-20 12:40:18 +02:00
if let Some(this) = self.as_mut() {
match this {
DebugSolver::CanonicalGoalEvaluation(canonical_goal_evaluation) => {
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
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 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-07-04 14:56:05 +01:00
match (this, goal_evaluation.state.unwrap().tree) {
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,
evaluation: WipGoalCandidate {
added_goals_evaluations: vec![],
candidates: vec![],
kind: None,
},
2023-07-04 14:56:05 +01:00
})
2023-06-08 19:10:07 +01: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() {
match (this, goal_evaluation_step.state.unwrap().tree) {
(
DebugSolver::CanonicalGoalEvaluation(canonical_goal_evaluations),
DebugSolver::GoalEvaluationStep(goal_evaluation_step),
) => {
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-06-08 19:58:04 +01:00
pub fn new_goal_candidate(&mut self) -> ProofTreeBuilder<'tcx> {
2023-08-14 11:48:40 +02:00
self.nested(|| WipGoalCandidate {
added_goals_evaluations: vec![],
2023-07-04 14:56:05 +01:00
candidates: vec![],
kind: None,
})
2023-06-08 19:10:07 +01:00
}
2023-06-20 12:40:18 +02:00
2023-09-11 11:34:57 +02:00
pub fn probe_kind(&mut self, probe_kind: ProbeKind<'tcx>) {
2023-06-20 12:40:18 +02:00
if let Some(this) = self.as_mut() {
match this {
DebugSolver::GoalCandidate(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-06-08 19:58:04 +01:00
pub fn goal_candidate(&mut self, candidate: ProofTreeBuilder<'tcx>) {
2023-06-20 12:40:18 +02:00
if let Some(this) = self.as_mut() {
2023-07-04 14:56:05 +01:00
match (this, candidate.state.unwrap().tree) {
2023-06-20 12:40:18 +02:00
(
DebugSolver::GoalCandidate(WipGoalCandidate { candidates, .. })
| DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
evaluation: WipGoalCandidate { candidates, .. },
..
}),
2023-06-20 12:40:18 +02:00
DebugSolver::GoalCandidate(candidate),
) => candidates.push(candidate),
_ => unreachable!(),
}
2023-06-08 19:10:07 +01:00
}
}
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
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() {
match (this, added_goals_evaluation.state.unwrap().tree) {
2023-06-20 12:40:18 +02:00
(
DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
evaluation: WipGoalCandidate { added_goals_evaluations, .. },
2023-06-20 12:40:18 +02:00
..
})
| DebugSolver::GoalCandidate(WipGoalCandidate {
added_goals_evaluations, ..
2023-06-20 12:40:18 +02:00
}),
DebugSolver::AddedGoalsEvaluation(added_goals_evaluation),
) => added_goals_evaluations.push(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 {
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) => {
assert_eq!(
evaluation_step.evaluation.kind.replace(ProbeKind::Root { result }),
None
);
2023-06-20 12:40:18 +02:00
}
_ => unreachable!(),
2023-06-08 19:10:07 +01:00
}
}
}
}