Skip to content

Commit 7c055af

Browse files
authored
Rollup merge of #107227 - lcnr:solver-new-external-api, r=compiler-errors
`new_outside_solver` -> `evaluate_root_goal` r? `@rust-lang/initiative-trait-system-refactor`
2 parents d94d928 + 033047a commit 7c055af

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

compiler/rustc_trait_selection/src/solve/fulfill.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::mem;
22

3+
use super::{Certainty, InferCtxtEvalExt};
34
use rustc_infer::{
45
infer::InferCtxt,
56
traits::{
@@ -8,8 +9,6 @@ use rustc_infer::{
89
},
910
};
1011

11-
use super::{search_graph, Certainty, EvalCtxt};
12-
1312
/// A trait engine using the new trait solver.
1413
///
1514
/// This is mostly identical to how `evaluate_all` works inside of the
@@ -66,9 +65,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> {
6665
let mut has_changed = false;
6766
for obligation in mem::take(&mut self.obligations) {
6867
let goal = obligation.clone().into();
69-
let search_graph = &mut search_graph::SearchGraph::new(infcx.tcx);
70-
let mut ecx = EvalCtxt::new_outside_solver(infcx, search_graph);
71-
let (changed, certainty) = match ecx.evaluate_goal(goal) {
68+
let (changed, certainty) = match infcx.evaluate_root_goal(goal) {
7269
Ok(result) => result,
7370
Err(NoSolution) => {
7471
errors.push(FulfillmentError {

compiler/rustc_trait_selection/src/solve/mod.rs

+30-12
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,36 @@ impl<'tcx> TyCtxtExt<'tcx> for TyCtxt<'tcx> {
152152
}
153153
}
154154

155+
pub trait InferCtxtEvalExt<'tcx> {
156+
/// Evaluates a goal from **outside** of the trait solver.
157+
///
158+
/// Using this while inside of the solver is wrong as it uses a new
159+
/// search graph which would break cycle detection.
160+
fn evaluate_root_goal(
161+
&self,
162+
goal: Goal<'tcx, ty::Predicate<'tcx>>,
163+
) -> Result<(bool, Certainty), NoSolution>;
164+
}
165+
166+
impl<'tcx> InferCtxtEvalExt<'tcx> for InferCtxt<'tcx> {
167+
fn evaluate_root_goal(
168+
&self,
169+
goal: Goal<'tcx, ty::Predicate<'tcx>>,
170+
) -> Result<(bool, Certainty), NoSolution> {
171+
let mut search_graph = search_graph::SearchGraph::new(self.tcx);
172+
173+
let result = EvalCtxt {
174+
search_graph: &mut search_graph,
175+
infcx: self,
176+
var_values: CanonicalVarValues::dummy(),
177+
}
178+
.evaluate_goal(goal);
179+
180+
assert!(search_graph.is_empty());
181+
result
182+
}
183+
}
184+
155185
struct EvalCtxt<'a, 'tcx> {
156186
infcx: &'a InferCtxt<'tcx>,
157187
var_values: CanonicalVarValues<'tcx>,
@@ -164,18 +194,6 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
164194
self.infcx.tcx
165195
}
166196

167-
/// Creates a new evaluation context outside of the trait solver.
168-
///
169-
/// With this solver making a canonical response doesn't make much sense.
170-
/// The `search_graph` for this solver has to be completely empty.
171-
fn new_outside_solver(
172-
infcx: &'a InferCtxt<'tcx>,
173-
search_graph: &'a mut search_graph::SearchGraph<'tcx>,
174-
) -> EvalCtxt<'a, 'tcx> {
175-
assert!(search_graph.is_empty());
176-
EvalCtxt { infcx, var_values: CanonicalVarValues::dummy(), search_graph }
177-
}
178-
179197
#[instrument(level = "debug", skip(tcx, search_graph), ret)]
180198
fn evaluate_canonical_goal(
181199
tcx: TyCtxt<'tcx>,

0 commit comments

Comments
 (0)