|
| 1 | +use super::{CanonicalInput, Certainty, Goal, NoSolution, QueryInput, QueryResult}; |
| 2 | +use crate::ty; |
| 3 | +use std::fmt::{Debug, Write}; |
| 4 | + |
| 5 | +#[derive(Eq, PartialEq, Hash, HashStable)] |
| 6 | +pub struct GoalEvaluation<'tcx> { |
| 7 | + pub uncanonicalized_goal: Goal<'tcx, ty::Predicate<'tcx>>, |
| 8 | + pub canonicalized_goal: Option<CanonicalInput<'tcx>>, |
| 9 | + |
| 10 | + /// To handle coinductive cycles we can end up re-evaluating a goal |
| 11 | + /// multiple times with different results for a nested goal. Each rerun |
| 12 | + /// is represented as an entry in this vec. |
| 13 | + pub evaluation_steps: Vec<GoalEvaluationStep<'tcx>>, |
| 14 | + |
| 15 | + pub cache_hit: bool, |
| 16 | + |
| 17 | + pub result: Option<QueryResult<'tcx>>, |
| 18 | +} |
| 19 | +impl Debug for GoalEvaluation<'_> { |
| 20 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 21 | + ProofTreeFormatter { f, on_newline: true }.format_goal_evaluation(self) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Eq, PartialEq, Hash, HashStable)] |
| 26 | +pub struct AddedGoalsEvaluation<'tcx> { |
| 27 | + pub evaluations: Vec<Vec<GoalEvaluation<'tcx>>>, |
| 28 | + pub result: Option<Result<Certainty, NoSolution>>, |
| 29 | +} |
| 30 | +impl Debug for AddedGoalsEvaluation<'_> { |
| 31 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 32 | + ProofTreeFormatter { f, on_newline: true }.format_nested_goal_evaluation(self) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[derive(Eq, PartialEq, Hash, HashStable)] |
| 37 | +pub struct GoalEvaluationStep<'tcx> { |
| 38 | + pub instantiated_goal: QueryInput<'tcx, ty::Predicate<'tcx>>, |
| 39 | + |
| 40 | + pub nested_goal_evaluations: Vec<AddedGoalsEvaluation<'tcx>>, |
| 41 | + pub candidates: Vec<GoalCandidate<'tcx>>, |
| 42 | + |
| 43 | + pub result: Option<QueryResult<'tcx>>, |
| 44 | +} |
| 45 | +impl Debug for GoalEvaluationStep<'_> { |
| 46 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 47 | + ProofTreeFormatter { f, on_newline: true }.format_evaluation_step(self) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +#[derive(Eq, PartialEq, Hash, HashStable)] |
| 52 | +pub struct GoalCandidate<'tcx> { |
| 53 | + pub nested_goal_evaluations: Vec<AddedGoalsEvaluation<'tcx>>, |
| 54 | + pub candidates: Vec<GoalCandidate<'tcx>>, |
| 55 | + |
| 56 | + pub name: Option<String>, |
| 57 | + pub result: Option<QueryResult<'tcx>>, |
| 58 | +} |
| 59 | +impl Debug for GoalCandidate<'_> { |
| 60 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 61 | + ProofTreeFormatter { f, on_newline: true }.format_candidate(self) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +struct ProofTreeFormatter<'a, 'b> { |
| 66 | + f: &'a mut (dyn Write + 'b), |
| 67 | + on_newline: bool, |
| 68 | +} |
| 69 | + |
| 70 | +impl Write for ProofTreeFormatter<'_, '_> { |
| 71 | + fn write_str(&mut self, s: &str) -> std::fmt::Result { |
| 72 | + for line in s.split_inclusive("\n") { |
| 73 | + if self.on_newline { |
| 74 | + self.f.write_str(" ")?; |
| 75 | + } |
| 76 | + self.on_newline = line.ends_with("\n"); |
| 77 | + self.f.write_str(line)?; |
| 78 | + } |
| 79 | + |
| 80 | + Ok(()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl ProofTreeFormatter<'_, '_> { |
| 85 | + fn nested(&mut self) -> ProofTreeFormatter<'_, '_> { |
| 86 | + ProofTreeFormatter { f: self, on_newline: true } |
| 87 | + } |
| 88 | + |
| 89 | + fn format_goal_evaluation(&mut self, goal: &GoalEvaluation<'_>) -> std::fmt::Result { |
| 90 | + let f = &mut *self.f; |
| 91 | + writeln!(f, "GOAL: {:?}", goal.uncanonicalized_goal)?; |
| 92 | + writeln!(f, "CANONICALIZED: {:?}", goal.canonicalized_goal)?; |
| 93 | + |
| 94 | + match goal.cache_hit { |
| 95 | + true => writeln!(f, "CACHE HIT: {:?}", goal.result), |
| 96 | + false => { |
| 97 | + for (n, step) in goal.evaluation_steps.iter().enumerate() { |
| 98 | + let f = &mut *self.f; |
| 99 | + writeln!(f, "REVISION {n}: {:?}", step.result.unwrap())?; |
| 100 | + let mut f = self.nested(); |
| 101 | + f.format_evaluation_step(step)?; |
| 102 | + } |
| 103 | + |
| 104 | + let f = &mut *self.f; |
| 105 | + writeln!(f, "RESULT: {:?}", goal.result.unwrap()) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + fn format_evaluation_step( |
| 111 | + &mut self, |
| 112 | + evaluation_step: &GoalEvaluationStep<'_>, |
| 113 | + ) -> std::fmt::Result { |
| 114 | + let f = &mut *self.f; |
| 115 | + writeln!(f, "INSTANTIATED: {:?}", evaluation_step.instantiated_goal)?; |
| 116 | + |
| 117 | + for candidate in &evaluation_step.candidates { |
| 118 | + let mut f = self.nested(); |
| 119 | + f.format_candidate(candidate)?; |
| 120 | + } |
| 121 | + for nested_goal_evaluation in &evaluation_step.nested_goal_evaluations { |
| 122 | + let mut f = self.nested(); |
| 123 | + f.format_nested_goal_evaluation(nested_goal_evaluation)?; |
| 124 | + } |
| 125 | + |
| 126 | + Ok(()) |
| 127 | + } |
| 128 | + |
| 129 | + fn format_candidate(&mut self, candidate: &GoalCandidate<'_>) -> std::fmt::Result { |
| 130 | + let f = &mut *self.f; |
| 131 | + |
| 132 | + match (candidate.name.as_ref(), candidate.result) { |
| 133 | + (Some(name), Some(result)) => writeln!(f, "CANDIDATE {}: {:?}", name, result,)?, |
| 134 | + (None, None) => writeln!(f, "MISC PROBE")?, |
| 135 | + (None, Some(_)) => unreachable!("unexpected probe with no name but a result"), |
| 136 | + (Some(_), None) => unreachable!("unexpected probe with a name but no candidate"), |
| 137 | + }; |
| 138 | + |
| 139 | + let mut f = self.nested(); |
| 140 | + for candidate in &candidate.candidates { |
| 141 | + f.format_candidate(candidate)?; |
| 142 | + } |
| 143 | + for nested_evaluations in &candidate.nested_goal_evaluations { |
| 144 | + f.format_nested_goal_evaluation(nested_evaluations)?; |
| 145 | + } |
| 146 | + |
| 147 | + Ok(()) |
| 148 | + } |
| 149 | + |
| 150 | + fn format_nested_goal_evaluation( |
| 151 | + &mut self, |
| 152 | + nested_goal_evaluation: &AddedGoalsEvaluation<'_>, |
| 153 | + ) -> std::fmt::Result { |
| 154 | + let f = &mut *self.f; |
| 155 | + writeln!(f, "TRY_EVALUATE_ADDED_GOALS: {:?}", nested_goal_evaluation.result.unwrap())?; |
| 156 | + |
| 157 | + for (n, revision) in nested_goal_evaluation.evaluations.iter().enumerate() { |
| 158 | + let f = &mut *self.f; |
| 159 | + writeln!(f, "REVISION {n}")?; |
| 160 | + let mut f = self.nested(); |
| 161 | + for goal_evaluation in revision { |
| 162 | + f.format_goal_evaluation(goal_evaluation)?; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + Ok(()) |
| 167 | + } |
| 168 | +} |
0 commit comments