Skip to content

Commit f2476ce

Browse files
committed
Convert ProofTreeVisitor to use VisitorResult
1 parent 047d954 commit f2476ce

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

compiler/rustc_trait_selection/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#![allow(rustc::diagnostic_outside_of_impl)]
1818
#![allow(rustc::untranslatable_diagnostic)]
1919
#![feature(associated_type_bounds)]
20+
#![feature(associated_type_defaults)]
2021
#![feature(box_patterns)]
2122
#![feature(control_flow_enum)]
2223
#![feature(extract_if)]

compiler/rustc_trait_selection/src/solve/inspect/analyse.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
/// and could be improved in the future. This is mostly good enough for
99
/// coherence right now and was annoying to implement, so I am leaving it
1010
/// as is until we start using it for something else.
11-
use std::ops::ControlFlow;
12-
1311
use rustc_infer::infer::InferCtxt;
1412
use rustc_middle::traits::query::NoSolution;
1513
use rustc_middle::traits::solve::{inspect, QueryResult};
1614
use rustc_middle::traits::solve::{Certainty, Goal};
1715
use rustc_middle::ty;
16+
use rustc_type_ir::try_visit;
17+
use rustc_type_ir::visit::VisitorResult;
1818

1919
use crate::solve::inspect::ProofTreeBuilder;
2020
use crate::solve::{GenerateProofTree, InferCtxtEvalExt};
@@ -53,10 +53,7 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
5353
/// to also use it to compute the most relevant goal
5454
/// for fulfillment errors. Will do that once we actually
5555
/// need it.
56-
pub fn visit_nested<V: ProofTreeVisitor<'tcx>>(
57-
&self,
58-
visitor: &mut V,
59-
) -> ControlFlow<V::BreakTy> {
56+
pub fn visit_nested<V: ProofTreeVisitor<'tcx>>(&self, visitor: &mut V) -> V::Result {
6057
// HACK: An arbitrary cutoff to avoid dealing with overflow and cycles.
6158
if self.goal.depth <= 10 {
6259
let infcx = self.goal.infcx;
@@ -75,7 +72,7 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
7572
"unexpected failure when instantiating {:?}: {:?}",
7673
goal, self.nested_goals
7774
);
78-
return ControlFlow::Continue(());
75+
return V::Result::output();
7976
}
8077
};
8178
instantiated_goals.push(goal);
@@ -84,17 +81,18 @@ impl<'a, 'tcx> InspectCandidate<'a, 'tcx> {
8481
for &goal in &instantiated_goals {
8582
let (_, proof_tree) = infcx.evaluate_root_goal(goal, GenerateProofTree::Yes);
8683
let proof_tree = proof_tree.unwrap();
87-
visitor.visit_goal(&InspectGoal::new(
84+
try_visit!(visitor.visit_goal(&InspectGoal::new(
8885
infcx,
8986
self.goal.depth + 1,
9087
&proof_tree,
91-
))?;
88+
)));
9289
}
9390

94-
ControlFlow::Continue(())
95-
})?;
91+
V::Result::output()
92+
})
93+
} else {
94+
V::Result::output()
9695
}
97-
ControlFlow::Continue(())
9896
}
9997
}
10098

@@ -211,9 +209,9 @@ impl<'a, 'tcx> InspectGoal<'a, 'tcx> {
211209

212210
/// The public API to interact with proof trees.
213211
pub trait ProofTreeVisitor<'tcx> {
214-
type BreakTy;
212+
type Result: VisitorResult = ();
215213

216-
fn visit_goal(&mut self, goal: &InspectGoal<'_, 'tcx>) -> ControlFlow<Self::BreakTy>;
214+
fn visit_goal(&mut self, goal: &InspectGoal<'_, 'tcx>) -> Self::Result;
217215
}
218216

219217
#[extension(pub trait ProofTreeInferCtxtExt<'tcx>)]
@@ -222,7 +220,7 @@ impl<'tcx> InferCtxt<'tcx> {
222220
&self,
223221
goal: Goal<'tcx, ty::Predicate<'tcx>>,
224222
visitor: &mut V,
225-
) -> ControlFlow<V::BreakTy> {
223+
) -> V::Result {
226224
self.probe(|_| {
227225
let (_, proof_tree) = self.evaluate_root_goal(goal, GenerateProofTree::Yes);
228226
let proof_tree = proof_tree.unwrap();

compiler/rustc_trait_selection/src/traits/coherence.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -954,18 +954,17 @@ struct AmbiguityCausesVisitor<'a, 'tcx> {
954954
}
955955

956956
impl<'a, 'tcx> ProofTreeVisitor<'tcx> for AmbiguityCausesVisitor<'a, 'tcx> {
957-
type BreakTy = !;
958-
fn visit_goal(&mut self, goal: &InspectGoal<'_, 'tcx>) -> ControlFlow<Self::BreakTy> {
957+
fn visit_goal(&mut self, goal: &InspectGoal<'_, 'tcx>) {
959958
let infcx = goal.infcx();
960959
for cand in goal.candidates() {
961-
cand.visit_nested(self)?;
960+
cand.visit_nested(self);
962961
}
963962
// When searching for intercrate ambiguity causes, we only need to look
964963
// at ambiguous goals, as for others the coherence unknowable candidate
965964
// was irrelevant.
966965
match goal.result() {
967966
Ok(Certainty::Maybe(_)) => {}
968-
Ok(Certainty::Yes) | Err(NoSolution) => return ControlFlow::Continue(()),
967+
Ok(Certainty::Yes) | Err(NoSolution) => return,
969968
}
970969

971970
let Goal { param_env, predicate } = goal.goal();
@@ -982,7 +981,7 @@ impl<'a, 'tcx> ProofTreeVisitor<'tcx> for AmbiguityCausesVisitor<'a, 'tcx> {
982981
{
983982
proj.projection_ty.trait_ref(infcx.tcx)
984983
}
985-
_ => return ControlFlow::Continue(()),
984+
_ => return,
986985
};
987986

988987
// Add ambiguity causes for reservation impls.
@@ -1082,8 +1081,6 @@ impl<'a, 'tcx> ProofTreeVisitor<'tcx> for AmbiguityCausesVisitor<'a, 'tcx> {
10821081
if let Some(ambiguity_cause) = ambiguity_cause {
10831082
self.causes.insert(ambiguity_cause);
10841083
}
1085-
1086-
ControlFlow::Continue(())
10871084
}
10881085
}
10891086

0 commit comments

Comments
 (0)