Skip to content

Commit 2900c6d

Browse files
committed
split GoalEvaluation and CanonicalGoalEvaluation
the unnormalized goal is in the callers inference context, while anything inside of the `CanonicalGoalEvaluation` is inside of a new one.
1 parent 3071e0a commit 2900c6d

File tree

5 files changed

+141
-110
lines changed

5 files changed

+141
-110
lines changed

compiler/rustc_middle/src/traits/solve/inspect.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ pub enum CacheHit {
1616
#[derive(Eq, PartialEq, Hash, HashStable)]
1717
pub struct GoalEvaluation<'tcx> {
1818
pub uncanonicalized_goal: Goal<'tcx, ty::Predicate<'tcx>>,
19-
pub canonicalized_goal: CanonicalInput<'tcx>,
20-
21-
pub kind: GoalEvaluationKind<'tcx>,
2219
pub is_normalizes_to_hack: IsNormalizesToHack,
20+
pub evaluation: CanonicalGoalEvaluation<'tcx>,
2321
pub returned_goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
22+
}
2423

24+
#[derive(Eq, PartialEq, Hash, HashStable)]
25+
pub struct CanonicalGoalEvaluation<'tcx> {
26+
pub goal: CanonicalInput<'tcx>,
27+
pub kind: GoalEvaluationKind<'tcx>,
2528
pub result: QueryResult<'tcx>,
2629
}
2730

@@ -41,30 +44,20 @@ pub struct AddedGoalsEvaluation<'tcx> {
4144
pub evaluations: Vec<Vec<GoalEvaluation<'tcx>>>,
4245
pub result: Result<Certainty, NoSolution>,
4346
}
44-
impl Debug for AddedGoalsEvaluation<'_> {
45-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46-
ProofTreeFormatter::new(f).format_nested_goal_evaluation(self)
47-
}
48-
}
4947

5048
#[derive(Eq, PartialEq, Hash, HashStable)]
5149
pub struct GoalEvaluationStep<'tcx> {
5250
pub instantiated_goal: QueryInput<'tcx, ty::Predicate<'tcx>>,
5351

54-
pub nested_goal_evaluations: Vec<AddedGoalsEvaluation<'tcx>>,
52+
pub added_goals_evaluations: Vec<AddedGoalsEvaluation<'tcx>>,
5553
pub candidates: Vec<GoalCandidate<'tcx>>,
5654

5755
pub result: QueryResult<'tcx>,
5856
}
59-
impl Debug for GoalEvaluationStep<'_> {
60-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61-
ProofTreeFormatter::new(f).format_evaluation_step(self)
62-
}
63-
}
6457

6558
#[derive(Eq, PartialEq, Hash, HashStable)]
6659
pub struct GoalCandidate<'tcx> {
67-
pub nested_goal_evaluations: Vec<AddedGoalsEvaluation<'tcx>>,
60+
pub added_goals_evaluations: Vec<AddedGoalsEvaluation<'tcx>>,
6861
pub candidates: Vec<GoalCandidate<'tcx>>,
6962
pub kind: CandidateKind<'tcx>,
7063
}
@@ -83,8 +76,3 @@ pub enum CandidateKind<'tcx> {
8376
/// the source type upholds all of the target type's object bounds.
8477
UpcastProbe,
8578
}
86-
impl Debug for GoalCandidate<'_> {
87-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88-
ProofTreeFormatter::new(f).format_candidate(self)
89-
}
90-
}

compiler/rustc_middle/src/traits/solve/inspect/format.rs

+35-30
Original file line numberDiff line numberDiff line change
@@ -39,44 +39,49 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
3939
func(&mut ProofTreeFormatter { f: &mut Indentor { f: self.f, on_newline: true } })
4040
}
4141

42-
pub(super) fn format_goal_evaluation(&mut self, goal: &GoalEvaluation<'_>) -> std::fmt::Result {
43-
let goal_text = match goal.is_normalizes_to_hack {
42+
pub(super) fn format_goal_evaluation(&mut self, eval: &GoalEvaluation<'_>) -> std::fmt::Result {
43+
let goal_text = match eval.is_normalizes_to_hack {
4444
IsNormalizesToHack::Yes => "NORMALIZES-TO HACK GOAL",
4545
IsNormalizesToHack::No => "GOAL",
4646
};
47+
writeln!(self.f, "{}: {:?}", goal_text, eval.uncanonicalized_goal)?;
48+
self.nested(|this| this.format_canonical_goal_evaluation(&eval.evaluation))?;
49+
if eval.returned_goals.len() > 0 {
50+
writeln!(self.f, "NESTED GOALS ADDED TO CALLER: [")?;
51+
self.nested(|this| {
52+
for goal in eval.returned_goals.iter() {
53+
writeln!(this.f, "ADDED GOAL: {goal:?},")?;
54+
}
55+
Ok(())
56+
})?;
4757

48-
writeln!(self.f, "{}: {:?}", goal_text, goal.uncanonicalized_goal)?;
49-
writeln!(self.f, "CANONICALIZED: {:?}", goal.canonicalized_goal)?;
58+
writeln!(self.f, "]")
59+
} else {
60+
Ok(())
61+
}
62+
}
63+
64+
pub(super) fn format_canonical_goal_evaluation(
65+
&mut self,
66+
eval: &CanonicalGoalEvaluation<'_>,
67+
) -> std::fmt::Result {
68+
writeln!(self.f, "GOAL: {:?}", eval.goal)?;
5069

51-
match &goal.kind {
70+
match &eval.kind {
5271
GoalEvaluationKind::CacheHit(CacheHit::Global) => {
53-
writeln!(self.f, "GLOBAL CACHE HIT: {:?}", goal.result)
72+
writeln!(self.f, "GLOBAL CACHE HIT: {:?}", eval.result)
5473
}
5574
GoalEvaluationKind::CacheHit(CacheHit::Provisional) => {
56-
writeln!(self.f, "PROVISIONAL CACHE HIT: {:?}", goal.result)
75+
writeln!(self.f, "PROVISIONAL CACHE HIT: {:?}", eval.result)
5776
}
5877
GoalEvaluationKind::Uncached { revisions } => {
5978
for (n, step) in revisions.iter().enumerate() {
6079
writeln!(self.f, "REVISION {n}: {:?}", step.result)?;
6180
self.nested(|this| this.format_evaluation_step(step))?;
6281
}
63-
writeln!(self.f, "RESULT: {:?}", goal.result)
82+
writeln!(self.f, "RESULT: {:?}", eval.result)
6483
}
65-
}?;
66-
67-
if goal.returned_goals.len() > 0 {
68-
writeln!(self.f, "NESTED GOALS ADDED TO CALLER: [")?;
69-
self.nested(|this| {
70-
for goal in goal.returned_goals.iter() {
71-
writeln!(this.f, "ADDED GOAL: {goal:?},")?;
72-
}
73-
Ok(())
74-
})?;
75-
76-
writeln!(self.f, "]")?;
7784
}
78-
79-
Ok(())
8085
}
8186

8287
pub(super) fn format_evaluation_step(
@@ -88,8 +93,8 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
8893
for candidate in &evaluation_step.candidates {
8994
self.nested(|this| this.format_candidate(candidate))?;
9095
}
91-
for nested in &evaluation_step.nested_goal_evaluations {
92-
self.nested(|this| this.format_nested_goal_evaluation(nested))?;
96+
for nested in &evaluation_step.added_goals_evaluations {
97+
self.nested(|this| this.format_added_goals_evaluation(nested))?;
9398
}
9499

95100
Ok(())
@@ -115,20 +120,20 @@ impl<'a, 'b> ProofTreeFormatter<'a, 'b> {
115120
for candidate in &candidate.candidates {
116121
this.format_candidate(candidate)?;
117122
}
118-
for nested in &candidate.nested_goal_evaluations {
119-
this.format_nested_goal_evaluation(nested)?;
123+
for nested in &candidate.added_goals_evaluations {
124+
this.format_added_goals_evaluation(nested)?;
120125
}
121126
Ok(())
122127
})
123128
}
124129

125-
pub(super) fn format_nested_goal_evaluation(
130+
pub(super) fn format_added_goals_evaluation(
126131
&mut self,
127-
nested_goal_evaluation: &AddedGoalsEvaluation<'_>,
132+
added_goals_evaluation: &AddedGoalsEvaluation<'_>,
128133
) -> std::fmt::Result {
129-
writeln!(self.f, "TRY_EVALUATE_ADDED_GOALS: {:?}", nested_goal_evaluation.result)?;
134+
writeln!(self.f, "TRY_EVALUATE_ADDED_GOALS: {:?}", added_goals_evaluation.result)?;
130135

131-
for (n, revision) in nested_goal_evaluation.evaluations.iter().enumerate() {
136+
for (n, revision) in added_goals_evaluation.evaluations.iter().enumerate() {
132137
writeln!(self.f, "REVISION {n}")?;
133138
self.nested(|this| {
134139
for goal_evaluation in revision {

compiler/rustc_trait_selection/src/solve/eval_ctxt.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
237237
tcx: TyCtxt<'tcx>,
238238
search_graph: &'a mut search_graph::SearchGraph<'tcx>,
239239
canonical_input: CanonicalInput<'tcx>,
240-
goal_evaluation: &mut ProofTreeBuilder<'tcx>,
240+
canonical_goal_evaluation: &mut ProofTreeBuilder<'tcx>,
241241
f: impl FnOnce(&mut EvalCtxt<'_, 'tcx>, Goal<'tcx, ty::Predicate<'tcx>>) -> R,
242242
) -> R {
243243
let intercrate = match search_graph.solver_mode() {
@@ -260,7 +260,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
260260
search_graph,
261261
nested_goals: NestedGoals::new(),
262262
tainted: Ok(()),
263-
inspect: goal_evaluation.new_goal_evaluation_step(input),
263+
inspect: canonical_goal_evaluation.new_goal_evaluation_step(input),
264264
};
265265

266266
for &(key, ty) in &input.predefined_opaques_in_body.opaque_types {
@@ -274,7 +274,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
274274

275275
let result = f(&mut ecx, input.goal);
276276

277-
goal_evaluation.goal_evaluation_step(ecx.inspect);
277+
canonical_goal_evaluation.goal_evaluation_step(ecx.inspect);
278278

279279
// When creating a query response we clone the opaque type constraints
280280
// instead of taking them. This would cause an ICE here, since we have
@@ -302,24 +302,25 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
302302
tcx: TyCtxt<'tcx>,
303303
search_graph: &'a mut search_graph::SearchGraph<'tcx>,
304304
canonical_input: CanonicalInput<'tcx>,
305-
mut goal_evaluation: &mut ProofTreeBuilder<'tcx>,
305+
goal_evaluation: &mut ProofTreeBuilder<'tcx>,
306306
) -> QueryResult<'tcx> {
307-
goal_evaluation.canonicalized_goal(canonical_input);
307+
let mut canonical_goal_evaluation =
308+
goal_evaluation.new_canonical_goal_evaluation(canonical_input);
308309

309310
// Deal with overflow, caching, and coinduction.
310311
//
311312
// The actual solver logic happens in `ecx.compute_goal`.
312-
ensure_sufficient_stack(|| {
313+
let result = ensure_sufficient_stack(|| {
313314
search_graph.with_new_goal(
314315
tcx,
315316
canonical_input,
316-
goal_evaluation,
317-
|search_graph, goal_evaluation| {
317+
&mut canonical_goal_evaluation,
318+
|search_graph, canonical_goal_evaluation| {
318319
EvalCtxt::enter_canonical(
319320
tcx,
320321
search_graph,
321322
canonical_input,
322-
goal_evaluation,
323+
canonical_goal_evaluation,
323324
|ecx, goal| {
324325
let result = ecx.compute_goal(goal);
325326
ecx.inspect.query_result(result);
@@ -328,7 +329,11 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
328329
)
329330
},
330331
)
331-
})
332+
});
333+
334+
canonical_goal_evaluation.query_result(result);
335+
goal_evaluation.canonical_goal_evaluation(canonical_goal_evaluation);
336+
result
332337
}
333338

334339
/// Recursively evaluates `goal`, returning whether any inference vars have
@@ -347,7 +352,6 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
347352
canonical_goal,
348353
&mut goal_evaluation,
349354
);
350-
goal_evaluation.query_result(canonical_response);
351355
let canonical_response = match canonical_response {
352356
Err(e) => {
353357
self.inspect.goal_evaluation(goal_evaluation);

0 commit comments

Comments
 (0)