Skip to content

Commit 444cbcd

Browse files
Address goal nits
1 parent d6a411c commit 444cbcd

File tree

4 files changed

+54
-38
lines changed

4 files changed

+54
-38
lines changed

compiler/rustc_trait_selection/src/solve/assembly.rs

+2-4
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, MaybeCause, QueryResult};
4+
use super::{CanonicalResponse, Certainty, EvalCtxt, Goal, QueryResult};
55
use rustc_hir::def_id::DefId;
66
use rustc_infer::traits::query::NoSolution;
77
use rustc_infer::traits::util::elaborate_predicates;
@@ -148,9 +148,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
148148
if goal.predicate.self_ty().is_ty_var() {
149149
return vec![Candidate {
150150
source: CandidateSource::BuiltinImpl,
151-
result: self
152-
.make_canonical_response(Certainty::Maybe(MaybeCause::Ambiguity))
153-
.unwrap(),
151+
result: self.make_canonical_response(Certainty::AMBIGUOUS).unwrap(),
154152
}];
155153
}
156154

compiler/rustc_trait_selection/src/solve/mod.rs

+46-27
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use std::mem;
2121

22+
use rustc_hir::def_id::DefId;
2223
use rustc_infer::infer::canonical::{Canonical, CanonicalVarKind, CanonicalVarValues};
2324
use rustc_infer::infer::canonical::{OriginalQueryValues, QueryRegionConstraints, QueryResponse};
2425
use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
@@ -27,7 +28,7 @@ use rustc_infer::traits::Obligation;
2728
use rustc_middle::infer::canonical::Certainty as OldCertainty;
2829
use rustc_middle::ty::{self, Ty, TyCtxt};
2930
use rustc_middle::ty::{
30-
RegionOutlivesPredicate, SubtypePredicate, ToPredicate, TypeOutlivesPredicate,
31+
CoercePredicate, RegionOutlivesPredicate, SubtypePredicate, ToPredicate, TypeOutlivesPredicate,
3132
};
3233
use rustc_span::DUMMY_SP;
3334

@@ -89,6 +90,8 @@ pub enum Certainty {
8990
}
9091

9192
impl Certainty {
93+
pub const AMBIGUOUS: Certainty = Certainty::Maybe(MaybeCause::Ambiguity);
94+
9295
/// When proving multiple goals using **AND**, e.g. nested obligations for an impl,
9396
/// use this function to unify the certainty of these goals
9497
pub fn unify_and(self, other: Certainty) -> Certainty {
@@ -248,21 +251,15 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
248251
ty::PredicateKind::Subtype(predicate) => {
249252
self.compute_subtype_goal(Goal { param_env, predicate })
250253
}
251-
ty::PredicateKind::Coerce(predicate) => self.compute_subtype_goal(Goal {
252-
param_env,
253-
predicate: SubtypePredicate {
254-
a_is_expected: true,
255-
a: predicate.a,
256-
b: predicate.b,
257-
},
258-
}),
259-
ty::PredicateKind::ClosureKind(_, substs, kind) => self.compute_closure_kind_goal(
260-
substs.as_closure().kind_ty().to_opt_closure_kind(),
261-
kind,
262-
),
263-
ty::PredicateKind::Ambiguous => {
264-
self.make_canonical_response(Certainty::Maybe(MaybeCause::Ambiguity))
254+
ty::PredicateKind::Coerce(predicate) => {
255+
self.compute_coerce_goal(Goal { param_env, predicate })
265256
}
257+
ty::PredicateKind::ClosureKind(def_id, substs, kind) => self
258+
.compute_closure_kind_goal(Goal {
259+
param_env,
260+
predicate: (def_id, substs, kind),
261+
}),
262+
ty::PredicateKind::Ambiguous => self.make_canonical_response(Certainty::AMBIGUOUS),
266263
// FIXME: implement these predicates :)
267264
ty::PredicateKind::WellFormed(_)
268265
| ty::PredicateKind::ObjectSafe(_)
@@ -296,28 +293,50 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
296293
self.make_canonical_response(Certainty::Yes)
297294
}
298295

296+
fn compute_coerce_goal(
297+
&mut self,
298+
goal: Goal<'tcx, CoercePredicate<'tcx>>,
299+
) -> QueryResult<'tcx> {
300+
self.compute_subtype_goal(Goal {
301+
param_env: goal.param_env,
302+
predicate: SubtypePredicate {
303+
a_is_expected: false,
304+
a: goal.predicate.a,
305+
b: goal.predicate.b,
306+
},
307+
})
308+
}
309+
299310
fn compute_subtype_goal(
300311
&mut self,
301312
goal: Goal<'tcx, SubtypePredicate<'tcx>>,
302313
) -> QueryResult<'tcx> {
303-
self.infcx.probe(|_| {
304-
let InferOk { value: (), obligations } = self
305-
.infcx
306-
.at(&ObligationCause::dummy(), goal.param_env)
307-
.sub(goal.predicate.a, goal.predicate.b)?;
308-
self.evaluate_all_and_make_canonical_response(
309-
obligations.into_iter().map(|pred| pred.into()).collect(),
310-
)
311-
})
314+
if goal.predicate.a.is_ty_var() && goal.predicate.b.is_ty_var() {
315+
// FIXME: Do we want to register a subtype relation between these vars?
316+
// That won't actually reflect in the query response, so it seems moot.
317+
self.make_canonical_response(Certainty::AMBIGUOUS)
318+
} else {
319+
self.infcx.probe(|_| {
320+
let InferOk { value: (), obligations } = self
321+
.infcx
322+
.at(&ObligationCause::dummy(), goal.param_env)
323+
.sub(goal.predicate.a, goal.predicate.b)?;
324+
self.evaluate_all_and_make_canonical_response(
325+
obligations.into_iter().map(|pred| pred.into()).collect(),
326+
)
327+
})
328+
}
312329
}
313330

314331
fn compute_closure_kind_goal(
315332
&mut self,
316-
found_kind: Option<ty::ClosureKind>,
317-
expected_kind: ty::ClosureKind,
333+
goal: Goal<'tcx, (DefId, ty::SubstsRef<'tcx>, ty::ClosureKind)>,
318334
) -> QueryResult<'tcx> {
335+
let (_, substs, expected_kind) = goal.predicate;
336+
let found_kind = substs.as_closure().kind_ty().to_opt_closure_kind();
337+
319338
let Some(found_kind) = found_kind else {
320-
return self.make_canonical_response(Certainty::Maybe(MaybeCause::Ambiguity));
339+
return self.make_canonical_response(Certainty::AMBIGUOUS);
321340
};
322341
if found_kind.extends(expected_kind) {
323342
self.make_canonical_response(Certainty::Yes)

compiler/rustc_trait_selection/src/solve/project_goals.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::traits::{specialization_graph, translate_substs};
33
use super::assembly::{self, Candidate, CandidateSource};
44
use super::infcx_ext::InferCtxtExt;
55
use super::trait_goals::structural_traits;
6-
use super::{Certainty, EvalCtxt, Goal, MaybeCause, QueryResult};
6+
use super::{Certainty, EvalCtxt, Goal, QueryResult};
77
use rustc_errors::ErrorGuaranteed;
88
use rustc_hir::def::DefKind;
99
use rustc_hir::def_id::DefId;
@@ -229,8 +229,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
229229
goal.predicate.def_id(),
230230
impl_def_id
231231
)? else {
232-
let certainty = Certainty::Maybe(MaybeCause::Ambiguity);
233-
return ecx.make_canonical_response(trait_ref_certainty.unify_and(certainty));
232+
return ecx.make_canonical_response(trait_ref_certainty.unify_and(Certainty::AMBIGUOUS));
234233
};
235234

236235
if !assoc_def.item.defaultness(tcx).has_value() {
@@ -382,7 +381,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for ProjectionPredicate<'tcx> {
382381
.to_predicate(ecx.tcx());
383382
Self::consider_assumption(ecx, goal, pred)
384383
} else {
385-
ecx.make_canonical_response(Certainty::Maybe(MaybeCause::Ambiguity))
384+
ecx.make_canonical_response(Certainty::AMBIGUOUS)
386385
}
387386
}
388387

compiler/rustc_trait_selection/src/solve/trait_goals.rs

+3-3
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, MaybeCause, QueryResult};
7+
use super::{Certainty, EvalCtxt, Goal, QueryResult};
88
use rustc_hir::def_id::DefId;
99
use rustc_infer::infer::InferCtxt;
1010
use rustc_infer::traits::query::NoSolution;
@@ -133,7 +133,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
133133
goal: Goal<'tcx, Self>,
134134
) -> QueryResult<'tcx> {
135135
if goal.predicate.self_ty().has_non_region_infer() {
136-
return ecx.make_canonical_response(Certainty::Maybe(MaybeCause::Ambiguity));
136+
return ecx.make_canonical_response(Certainty::AMBIGUOUS);
137137
}
138138

139139
let tcx = ecx.tcx();
@@ -171,7 +171,7 @@ impl<'tcx> assembly::GoalKind<'tcx> for TraitPredicate<'tcx> {
171171
.to_predicate(ecx.tcx());
172172
Self::consider_assumption(ecx, goal, pred)
173173
} else {
174-
ecx.make_canonical_response(Certainty::Maybe(MaybeCause::Ambiguity))
174+
ecx.make_canonical_response(Certainty::AMBIGUOUS)
175175
}
176176
}
177177

0 commit comments

Comments
 (0)