From 8e139eefaf276108c56fda1ec0c7fb972ce8df0c Mon Sep 17 00:00:00 2001 From: lcnr Date: Thu, 21 Sep 2023 08:40:36 +0200 Subject: [PATCH] slight refactor, add comment --- .../src/solve/inspect/analyse.rs | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_trait_selection/src/solve/inspect/analyse.rs b/compiler/rustc_trait_selection/src/solve/inspect/analyse.rs index ece0e801fe8..e4fc8803e0f 100644 --- a/compiler/rustc_trait_selection/src/solve/inspect/analyse.rs +++ b/compiler/rustc_trait_selection/src/solve/inspect/analyse.rs @@ -123,6 +123,9 @@ fn candidates_recur( &inspect::ProbeStep::AddGoal(goal) => nested_goals.push(goal), inspect::ProbeStep::EvaluateGoals(_) => (), inspect::ProbeStep::NestedProbe(ref probe) => { + // Nested probes have to prove goals added in their parent + // but do not leak them, so we truncate the added goals + // afterwards. let num_goals = nested_goals.len(); self.candidates_recur(candidates, nested_goals, probe); nested_goals.truncate(num_goals); @@ -131,10 +134,25 @@ fn candidates_recur( } match probe.kind { - inspect::ProbeKind::Root { result: _ } - | inspect::ProbeKind::NormalizedSelfTyAssembly + inspect::ProbeKind::NormalizedSelfTyAssembly | inspect::ProbeKind::UnsizeAssembly | inspect::ProbeKind::UpcastProjectionCompatibility => (), + // We add a candidate for the root evaluation if there + // is only one way to prove a given goal, e.g. for `WellFormed`. + // + // FIXME: This is currently wrong if we don't even try any + // candidates, e.g. for a trait goal, as in this case `candidates` is + // actually supposed to be empty. + inspect::ProbeKind::Root { result: _ } => { + if candidates.is_empty() { + candidates.push(InspectCandidate { + goal: self, + kind: probe.kind, + nested_goals: nested_goals.clone(), + result, + }); + } + } inspect::ProbeKind::MiscCandidate { name: _, result } | inspect::ProbeKind::TraitCandidate { source: _, result } => { candidates.push(InspectCandidate { @@ -167,15 +185,6 @@ pub fn candidates(&'a self) -> Vec> { let mut nested_goals = vec![]; self.candidates_recur(&mut candidates, &mut nested_goals, &last_eval_step.evaluation); - if candidates.is_empty() { - candidates.push(InspectCandidate { - goal: self, - kind: last_eval_step.evaluation.kind, - nested_goals, - result: self.evaluation.evaluation.result, - }); - } - candidates }