Skip to content

Commit

Permalink
GoalCandidate to Probe
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Sep 14, 2023
1 parent 2394959 commit be9d7e0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 40 deletions.
13 changes: 8 additions & 5 deletions compiler/rustc_middle/src/traits/solve/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,22 @@ pub struct GoalEvaluationStep<'tcx> {
pub instantiated_goal: QueryInput<'tcx, ty::Predicate<'tcx>>,

/// The actual evaluation of the goal, always `ProbeKind::Root`.
pub evaluation: GoalCandidate<'tcx>,
pub evaluation: Probe<'tcx>,
}

/// A self-contained computation during trait solving. This either
/// corresponds to a `EvalCtxt::probe(_X)` call or the root evaluation
/// of a goal.
#[derive(Eq, PartialEq)]
pub struct GoalCandidate<'tcx> {
pub struct Probe<'tcx> {
pub added_goals_evaluations: Vec<AddedGoalsEvaluation<'tcx>>,
pub candidates: Vec<GoalCandidate<'tcx>>,
pub nested_probes: Vec<Probe<'tcx>>,
pub kind: ProbeKind<'tcx>,
}

impl Debug for GoalCandidate<'_> {
impl Debug for Probe<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
ProofTreeFormatter::new(f).format_candidate(self)
ProofTreeFormatter::new(f).format_probe(self)
}
}

Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_middle/src/traits/solve/inspect/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
evaluation_step: &GoalEvaluationStep<'_>,
) -> std::fmt::Result {
writeln!(self.f, "INSTANTIATED: {:?}", evaluation_step.instantiated_goal)?;
self.format_candidate(&evaluation_step.evaluation)
self.format_probe(&evaluation_step.evaluation)
}

pub(super) fn format_candidate(&mut self, candidate: &GoalCandidate<'_>) -> std::fmt::Result {
match &candidate.kind {
pub(super) fn format_probe(&mut self, probe: &Probe<'_>) -> std::fmt::Result {
match &probe.kind {
ProbeKind::Root { result } => {
writeln!(self.f, "ROOT RESULT: {result:?}")
}
Expand All @@ -118,10 +118,10 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
}?;

self.nested(|this| {
for candidate in &candidate.candidates {
this.format_candidate(candidate)?;
for probe in &probe.nested_probes {
this.format_probe(probe)?;
}
for nested in &candidate.added_goals_evaluations {
for nested in &probe.added_goals_evaluations {
this.format_added_goals_evaluation(nested)?;
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_trait_selection/src/solve/eval_ctxt/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ where
search_graph: outer_ecx.search_graph,
nested_goals: outer_ecx.nested_goals.clone(),
tainted: outer_ecx.tainted,
inspect: outer_ecx.inspect.new_goal_candidate(),
inspect: outer_ecx.inspect.new_probe(),
};
let r = nested_ecx.infcx.probe(|_| f(&mut nested_ecx));
if !outer_ecx.inspect.is_noop() {
let probe_kind = probe_kind(&r);
nested_ecx.inspect.probe_kind(probe_kind);
outer_ecx.inspect.goal_candidate(nested_ecx.inspect);
outer_ecx.inspect.finish_probe(nested_ecx.inspect);
}
r
}
Expand Down
52 changes: 25 additions & 27 deletions compiler/rustc_trait_selection/src/solve/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'tcx> WipAddedGoalsEvaluation<'tcx> {
pub struct WipGoalEvaluationStep<'tcx> {
pub instantiated_goal: QueryInput<'tcx, ty::Predicate<'tcx>>,

pub evaluation: WipGoalCandidate<'tcx>,
pub evaluation: WipProbe<'tcx>,
}

impl<'tcx> WipGoalEvaluationStep<'tcx> {
Expand All @@ -102,21 +102,21 @@ impl<'tcx> WipGoalEvaluationStep<'tcx> {
}

#[derive(Eq, PartialEq, Debug)]
pub struct WipGoalCandidate<'tcx> {
pub struct WipProbe<'tcx> {
pub added_goals_evaluations: Vec<WipAddedGoalsEvaluation<'tcx>>,
pub candidates: Vec<WipGoalCandidate<'tcx>>,
pub nested_probes: Vec<WipProbe<'tcx>>,
pub kind: Option<ProbeKind<'tcx>>,
}

impl<'tcx> WipGoalCandidate<'tcx> {
pub fn finalize(self) -> inspect::GoalCandidate<'tcx> {
inspect::GoalCandidate {
impl<'tcx> WipProbe<'tcx> {
pub fn finalize(self) -> inspect::Probe<'tcx> {
inspect::Probe {
added_goals_evaluations: self
.added_goals_evaluations
.into_iter()
.map(WipAddedGoalsEvaluation::finalize)
.collect(),
candidates: self.candidates.into_iter().map(WipGoalCandidate::finalize).collect(),
nested_probes: self.nested_probes.into_iter().map(WipProbe::finalize).collect(),
kind: self.kind.unwrap(),
}
}
Expand All @@ -129,7 +129,7 @@ pub enum DebugSolver<'tcx> {
CanonicalGoalEvaluation(WipCanonicalGoalEvaluation<'tcx>),
AddedGoalsEvaluation(WipAddedGoalsEvaluation<'tcx>),
GoalEvaluationStep(WipGoalEvaluationStep<'tcx>),
GoalCandidate(WipGoalCandidate<'tcx>),
Probe(WipProbe<'tcx>),
}

impl<'tcx> From<WipGoalEvaluation<'tcx>> for DebugSolver<'tcx> {
Expand All @@ -156,9 +156,9 @@ impl<'tcx> From<WipGoalEvaluationStep<'tcx>> for DebugSolver<'tcx> {
}
}

impl<'tcx> From<WipGoalCandidate<'tcx>> for DebugSolver<'tcx> {
fn from(g: WipGoalCandidate<'tcx>) -> DebugSolver<'tcx> {
DebugSolver::GoalCandidate(g)
impl<'tcx> From<WipProbe<'tcx>> for DebugSolver<'tcx> {
fn from(p: WipProbe<'tcx>) -> DebugSolver<'tcx> {
DebugSolver::Probe(p)
}
}

Expand Down Expand Up @@ -329,9 +329,9 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
) -> ProofTreeBuilder<'tcx> {
self.nested(|| WipGoalEvaluationStep {
instantiated_goal,
evaluation: WipGoalCandidate {
evaluation: WipProbe {
added_goals_evaluations: vec![],
candidates: vec![],
nested_probes: vec![],
kind: None,
},
})
Expand All @@ -350,36 +350,36 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
}
}

pub fn new_goal_candidate(&mut self) -> ProofTreeBuilder<'tcx> {
self.nested(|| WipGoalCandidate {
pub fn new_probe(&mut self) -> ProofTreeBuilder<'tcx> {
self.nested(|| WipProbe {
added_goals_evaluations: vec![],
candidates: vec![],
nested_probes: vec![],
kind: None,
})
}

pub fn probe_kind(&mut self, probe_kind: ProbeKind<'tcx>) {
if let Some(this) = self.as_mut() {
match this {
DebugSolver::GoalCandidate(this) => {
DebugSolver::Probe(this) => {
assert_eq!(this.kind.replace(probe_kind), None)
}
_ => unreachable!(),
}
}
}

pub fn goal_candidate(&mut self, candidate: ProofTreeBuilder<'tcx>) {
pub fn finish_probe(&mut self, probe: ProofTreeBuilder<'tcx>) {
if let Some(this) = self.as_mut() {
match (this, candidate.state.unwrap().tree) {
match (this, probe.state.unwrap().tree) {
(
DebugSolver::GoalCandidate(WipGoalCandidate { candidates, .. })
DebugSolver::Probe(WipProbe { nested_probes, .. })
| DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
evaluation: WipGoalCandidate { candidates, .. },
evaluation: WipProbe { nested_probes, .. },
..
}),
DebugSolver::GoalCandidate(candidate),
) => candidates.push(candidate),
DebugSolver::Probe(probe),
) => nested_probes.push(probe),
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -416,12 +416,10 @@ impl<'tcx> ProofTreeBuilder<'tcx> {
match (this, added_goals_evaluation.state.unwrap().tree) {
(
DebugSolver::GoalEvaluationStep(WipGoalEvaluationStep {
evaluation: WipGoalCandidate { added_goals_evaluations, .. },
evaluation: WipProbe { added_goals_evaluations, .. },
..
})
| DebugSolver::GoalCandidate(WipGoalCandidate {
added_goals_evaluations, ..
}),
| DebugSolver::Probe(WipProbe { added_goals_evaluations, .. }),
DebugSolver::AddedGoalsEvaluation(added_goals_evaluation),
) => added_goals_evaluations.push(added_goals_evaluation),
_ => unreachable!(),
Expand Down

0 comments on commit be9d7e0

Please sign in to comment.