Skip to content

Commit b84b1da

Browse files
Canonicalize trait solver response inside probe
1 parent 1f72129 commit b84b1da

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

compiler/rustc_trait_selection/src/solve/assembly.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Code shared by trait and projection goals for candidate assembly.
22
33
use super::infcx_ext::InferCtxtExt;
4-
use super::{CanonicalResponse, Certainty, EvalCtxt, Goal};
4+
use super::{CanonicalResponse, EvalCtxt, Goal, QueryResult};
55
use rustc_hir::def_id::DefId;
66
use rustc_infer::traits::query::NoSolution;
77
use rustc_middle::ty::TypeFoldable;
@@ -89,18 +89,18 @@ pub(super) trait GoalKind<'tcx>: TypeFoldable<'tcx> + Copy {
8989
ecx: &mut EvalCtxt<'_, 'tcx>,
9090
goal: Goal<'tcx, Self>,
9191
impl_def_id: DefId,
92-
) -> Result<Certainty, NoSolution>;
92+
) -> QueryResult<'tcx>;
9393

9494
fn consider_builtin_sized_candidate(
9595
ecx: &mut EvalCtxt<'_, 'tcx>,
9696
goal: Goal<'tcx, Self>,
97-
) -> Result<Certainty, NoSolution>;
97+
) -> QueryResult<'tcx>;
9898

9999
fn consider_assumption(
100100
ecx: &mut EvalCtxt<'_, 'tcx>,
101101
goal: Goal<'tcx, Self>,
102102
assumption: ty::Predicate<'tcx>,
103-
) -> Result<Certainty, NoSolution>;
103+
) -> QueryResult<'tcx>;
104104
}
105105
impl<'tcx> EvalCtxt<'_, 'tcx> {
106106
pub(super) fn assemble_and_evaluate_candidates<G: GoalKind<'tcx>>(
@@ -180,9 +180,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
180180
tcx.for_each_relevant_impl(
181181
goal.predicate.trait_def_id(tcx),
182182
goal.predicate.self_ty(),
183-
|impl_def_id| match G::consider_impl_candidate(self, goal, impl_def_id)
184-
.and_then(|certainty| self.make_canonical_response(certainty))
185-
{
183+
|impl_def_id| match G::consider_impl_candidate(self, goal, impl_def_id) {
186184
Ok(result) => candidates
187185
.push(Candidate { source: CandidateSource::Impl(impl_def_id), result }),
188186
Err(NoSolution) => (),
@@ -203,7 +201,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
203201
Err(NoSolution)
204202
};
205203

206-
match result.and_then(|certainty| self.make_canonical_response(certainty)) {
204+
match result {
207205
Ok(result) => {
208206
candidates.push(Candidate { source: CandidateSource::BuiltinImpl, result })
209207
}
@@ -217,9 +215,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
217215
candidates: &mut Vec<Candidate<'tcx>>,
218216
) {
219217
for (i, assumption) in goal.param_env.caller_bounds().iter().enumerate() {
220-
match G::consider_assumption(self, goal, assumption)
221-
.and_then(|certainty| self.make_canonical_response(certainty))
222-
{
218+
match G::consider_assumption(self, goal, assumption) {
223219
Ok(result) => {
224220
candidates.push(Candidate { source: CandidateSource::ParamEnv(i), result })
225221
}
@@ -268,9 +264,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
268264
.subst_iter_copied(self.tcx(), alias_ty.substs)
269265
.enumerate()
270266
{
271-
match G::consider_assumption(self, goal, assumption)
272-
.and_then(|certainty| self.make_canonical_response(certainty))
273-
{
267+
match G::consider_assumption(self, goal, assumption) {
274268
Ok(result) => {
275269
candidates.push(Candidate { source: CandidateSource::AliasBound(i), result })
276270
}

compiler/rustc_trait_selection/src/solve/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
313313
}
314314
})
315315
}
316+
317+
fn evaluate_all_and_make_canonical_response(
318+
&mut self,
319+
goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
320+
) -> QueryResult<'tcx> {
321+
self.evaluate_all(goals).and_then(|certainty| self.make_canonical_response(certainty))
322+
}
316323
}
317324

318325
#[instrument(level = "debug", skip(infcx), ret)]

compiler/rustc_trait_selection/src/solve/project_goals.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
191191
ecx: &mut EvalCtxt<'_, 'tcx>,
192192
goal: Goal<'tcx, ProjectionPredicate<'tcx>>,
193193
impl_def_id: DefId,
194-
) -> Result<Certainty, NoSolution> {
194+
) -> QueryResult<'tcx> {
195195
let tcx = ecx.tcx();
196196

197197
let goal_trait_ref = goal.predicate.projection_ty.trait_ref(tcx);
@@ -229,7 +229,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
229229
impl_def_id
230230
)? else {
231231
let certainty = Certainty::Maybe(MaybeCause::Ambiguity);
232-
return Ok(trait_ref_certainty.unify_and(certainty));
232+
return ecx.make_canonical_response(trait_ref_certainty.unify_and(certainty));
233233
};
234234

235235
if !assoc_def.item.defaultness(tcx).has_value() {
@@ -286,22 +286,22 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
286286
let rhs_certainty =
287287
ecx.evaluate_all(nested_goals).expect("failed to unify with unconstrained term");
288288

289-
Ok(trait_ref_certainty.unify_and(rhs_certainty))
289+
ecx.make_canonical_response(trait_ref_certainty.unify_and(rhs_certainty))
290290
})
291291
}
292292

293293
fn consider_builtin_sized_candidate(
294294
_ecx: &mut EvalCtxt<'_, 'tcx>,
295295
goal: Goal<'tcx, Self>,
296-
) -> Result<Certainty, NoSolution> {
296+
) -> QueryResult<'tcx> {
297297
bug!("`Sized` does not have an associated type: {:?}", goal);
298298
}
299299

300300
fn consider_assumption(
301301
_ecx: &mut EvalCtxt<'_, 'tcx>,
302302
_goal: Goal<'tcx, Self>,
303303
assumption: ty::Predicate<'tcx>,
304-
) -> Result<Certainty, NoSolution> {
304+
) -> QueryResult<'tcx> {
305305
if let Some(_poly_projection_pred) = assumption.to_opt_poly_projection_pred() {
306306
unimplemented!()
307307
} else {

compiler/rustc_trait_selection/src/solve/trait_goals.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::iter;
44

55
use super::assembly::{self, Candidate, CandidateSource};
66
use super::infcx_ext::InferCtxtExt;
7-
use super::{Certainty, EvalCtxt, Goal, QueryResult};
7+
use super::{EvalCtxt, Goal, QueryResult};
88
use rustc_hir::def_id::DefId;
99
use rustc_infer::traits::query::NoSolution;
1010
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
@@ -29,7 +29,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
2929
ecx: &mut EvalCtxt<'_, 'tcx>,
3030
goal: Goal<'tcx, TraitPredicate<'tcx>>,
3131
impl_def_id: DefId,
32-
) -> Result<Certainty, NoSolution> {
32+
) -> QueryResult<'tcx> {
3333
let tcx = ecx.tcx();
3434

3535
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
@@ -53,22 +53,22 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
5353
.into_iter()
5454
.map(|pred| goal.with(tcx, pred));
5555
nested_goals.extend(where_clause_bounds);
56-
ecx.evaluate_all(nested_goals)
56+
ecx.evaluate_all_and_make_canonical_response(nested_goals)
5757
})
5858
}
5959

6060
fn consider_builtin_sized_candidate(
6161
_ecx: &mut EvalCtxt<'_, 'tcx>,
6262
_goal: Goal<'tcx, Self>,
63-
) -> Result<Certainty, NoSolution> {
63+
) -> QueryResult<'tcx> {
6464
unimplemented!();
6565
}
6666

6767
fn consider_assumption(
6868
_ecx: &mut EvalCtxt<'_, 'tcx>,
6969
_goal: Goal<'tcx, Self>,
7070
assumption: ty::Predicate<'tcx>,
71-
) -> Result<Certainty, NoSolution> {
71+
) -> QueryResult<'tcx> {
7272
if let Some(_poly_trait_pred) = assumption.to_opt_poly_trait_pred() {
7373
unimplemented!()
7474
} else {

0 commit comments

Comments
 (0)