Skip to content

Commit 63ce5c1

Browse files
committed
Replace ConstnessAnd<TraitRef> with TraitPredicate which conveys the same information
1 parent cde1f6a commit 63ce5c1

File tree

2 files changed

+45
-70
lines changed
  • compiler
    • rustc_middle/src/traits
    • rustc_trait_selection/src/traits/select

2 files changed

+45
-70
lines changed

compiler/rustc_middle/src/traits/select.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ pub type SelectionCache<'tcx> = Cache<
1616
SelectionResult<'tcx, SelectionCandidate<'tcx>>,
1717
>;
1818

19-
pub type EvaluationCache<'tcx> = Cache<
20-
(ty::ParamEnvAnd<'tcx, ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>>, ty::ImplPolarity),
21-
EvaluationResult,
22-
>;
19+
pub type EvaluationCache<'tcx> =
20+
Cache<ty::ParamEnvAnd<'tcx, ty::PolyTraitPredicate<'tcx>>, EvaluationResult>;
2321

2422
/// The selection process begins by considering all impls, where
2523
/// clauses, and so forth that might resolve an obligation. Sometimes

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 43 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use rustc_middle::ty::fast_reject;
3939
use rustc_middle::ty::print::with_no_trimmed_paths;
4040
use rustc_middle::ty::relate::TypeRelation;
4141
use rustc_middle::ty::subst::{GenericArgKind, Subst, SubstsRef};
42-
use rustc_middle::ty::WithConstness;
4342
use rustc_middle::ty::{self, PolyProjectionPredicate, ToPolyTraitRef, ToPredicate};
4443
use rustc_middle::ty::{Ty, TyCtxt, TypeFoldable};
4544
use rustc_span::symbol::sym;
@@ -138,9 +137,9 @@ pub struct SelectionContext<'cx, 'tcx> {
138137
struct TraitObligationStack<'prev, 'tcx> {
139138
obligation: &'prev TraitObligation<'tcx>,
140139

141-
/// The trait ref from `obligation` but "freshened" with the
140+
/// The trait predicate from `obligation` but "freshened" with the
142141
/// selection-context's freshener. Used to check for recursion.
143-
fresh_trait_ref: ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>,
142+
fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
144143

145144
/// Starts out equal to `depth` -- if, during evaluation, we
146145
/// encounter a cycle, then we will set this flag to the minimum
@@ -676,20 +675,16 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
676675
}
677676

678677
let stack = self.push_stack(previous_stack, &obligation);
679-
let fresh_trait_ref = stack.fresh_trait_ref;
678+
let fresh_trait_pred = stack.fresh_trait_pred;
680679

681-
debug!(?fresh_trait_ref);
680+
debug!(?fresh_trait_pred);
682681

683-
if let Some(result) = self.check_evaluation_cache(
684-
obligation.param_env,
685-
fresh_trait_ref,
686-
obligation.polarity(),
687-
) {
682+
if let Some(result) = self.check_evaluation_cache(obligation.param_env, fresh_trait_pred) {
688683
debug!(?result, "CACHE HIT");
689684
return Ok(result);
690685
}
691686

692-
if let Some(result) = stack.cache().get_provisional(fresh_trait_ref) {
687+
if let Some(result) = stack.cache().get_provisional(fresh_trait_pred) {
693688
debug!(?result, "PROVISIONAL CACHE HIT");
694689
stack.update_reached_depth(result.reached_depth);
695690
return Ok(result.result);
@@ -714,19 +709,12 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
714709
let reached_depth = stack.reached_depth.get();
715710
if reached_depth >= stack.depth {
716711
debug!(?result, "CACHE MISS");
717-
self.insert_evaluation_cache(
718-
obligation.param_env,
719-
fresh_trait_ref,
720-
obligation.polarity(),
721-
dep_node,
722-
result,
723-
);
712+
self.insert_evaluation_cache(obligation.param_env, fresh_trait_pred, dep_node, result);
724713

725-
stack.cache().on_completion(stack.dfn, |fresh_trait_ref, provisional_result| {
714+
stack.cache().on_completion(stack.dfn, |fresh_trait_pred, provisional_result| {
726715
self.insert_evaluation_cache(
727716
obligation.param_env,
728-
fresh_trait_ref,
729-
obligation.polarity(),
717+
fresh_trait_pred,
730718
dep_node,
731719
provisional_result.max(result),
732720
);
@@ -736,10 +724,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
736724
debug!(
737725
"caching provisionally because {:?} \
738726
is a cycle participant (at depth {}, reached depth {})",
739-
fresh_trait_ref, stack.depth, reached_depth,
727+
fresh_trait_pred, stack.depth, reached_depth,
740728
);
741729

742-
stack.cache().insert_provisional(stack.dfn, reached_depth, fresh_trait_ref, result);
730+
stack.cache().insert_provisional(stack.dfn, reached_depth, fresh_trait_pred, result);
743731
}
744732

745733
Ok(result)
@@ -773,7 +761,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
773761
.skip(1) // Skip top-most frame.
774762
.find(|prev| {
775763
stack.obligation.param_env == prev.obligation.param_env
776-
&& stack.fresh_trait_ref == prev.fresh_trait_ref
764+
&& stack.fresh_trait_pred == prev.fresh_trait_pred
777765
})
778766
.map(|stack| stack.depth)
779767
{
@@ -836,7 +824,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
836824
// terms of `Fn` etc, but we could probably make this more
837825
// precise still.
838826
let unbound_input_types =
839-
stack.fresh_trait_ref.value.skip_binder().substs.types().any(|ty| ty.is_fresh());
827+
stack.fresh_trait_pred.skip_binder().trait_ref.substs.types().any(|ty| ty.is_fresh());
840828

841829
if stack.obligation.polarity() != ty::ImplPolarity::Negative {
842830
// This check was an imperfect workaround for a bug in the old
@@ -874,8 +862,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
874862
&& stack.iter().skip(1).any(|prev| {
875863
stack.obligation.param_env == prev.obligation.param_env
876864
&& self.match_fresh_trait_refs(
877-
stack.fresh_trait_ref,
878-
prev.fresh_trait_ref,
865+
stack.fresh_trait_pred,
866+
prev.fresh_trait_pred,
879867
prev.obligation.param_env,
880868
)
881869
})
@@ -953,7 +941,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
953941
// not just the lifetime choice for this particular (non-erased)
954942
// predicate.
955943
// See issue #80691
956-
if stack.fresh_trait_ref.has_erased_regions() {
944+
if stack.fresh_trait_pred.has_erased_regions() {
957945
result = result.max(EvaluatedToOkModuloRegions);
958946
}
959947

@@ -964,8 +952,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
964952
fn check_evaluation_cache(
965953
&self,
966954
param_env: ty::ParamEnv<'tcx>,
967-
trait_ref: ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>,
968-
polarity: ty::ImplPolarity,
955+
trait_pred: ty::PolyTraitPredicate<'tcx>,
969956
) -> Option<EvaluationResult> {
970957
// Neither the global nor local cache is aware of intercrate
971958
// mode, so don't do any caching. In particular, we might
@@ -977,19 +964,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
977964

978965
let tcx = self.tcx();
979966
if self.can_use_global_caches(param_env) {
980-
if let Some(res) = tcx.evaluation_cache.get(&(param_env.and(trait_ref), polarity), tcx)
981-
{
967+
if let Some(res) = tcx.evaluation_cache.get(&param_env.and(trait_pred), tcx) {
982968
return Some(res);
983969
}
984970
}
985-
self.infcx.evaluation_cache.get(&(param_env.and(trait_ref), polarity), tcx)
971+
self.infcx.evaluation_cache.get(&param_env.and(trait_pred), tcx)
986972
}
987973

988974
fn insert_evaluation_cache(
989975
&mut self,
990976
param_env: ty::ParamEnv<'tcx>,
991-
trait_ref: ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>,
992-
polarity: ty::ImplPolarity,
977+
trait_pred: ty::PolyTraitPredicate<'tcx>,
993978
dep_node: DepNodeIndex,
994979
result: EvaluationResult,
995980
) {
@@ -1008,23 +993,19 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
1008993
}
1009994

1010995
if self.can_use_global_caches(param_env) {
1011-
if !trait_ref.needs_infer() {
1012-
debug!(?trait_ref, ?result, "insert_evaluation_cache global");
996+
if !trait_pred.needs_infer() {
997+
debug!(?trait_pred, ?result, "insert_evaluation_cache global");
1013998
// This may overwrite the cache with the same value
1014999
// FIXME: Due to #50507 this overwrites the different values
10151000
// This should be changed to use HashMapExt::insert_same
10161001
// when that is fixed
1017-
self.tcx().evaluation_cache.insert(
1018-
(param_env.and(trait_ref), polarity),
1019-
dep_node,
1020-
result,
1021-
);
1002+
self.tcx().evaluation_cache.insert(param_env.and(trait_pred), dep_node, result);
10221003
return;
10231004
}
10241005
}
10251006

1026-
debug!(?trait_ref, ?result, "insert_evaluation_cache");
1027-
self.infcx.evaluation_cache.insert((param_env.and(trait_ref), polarity), dep_node, result);
1007+
debug!(?trait_pred, ?result, "insert_evaluation_cache");
1008+
self.infcx.evaluation_cache.insert(param_env.and(trait_pred), dep_node, result);
10281009
}
10291010

10301011
/// For various reasons, it's possible for a subobligation
@@ -2157,8 +2138,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
21572138

21582139
fn match_fresh_trait_refs(
21592140
&self,
2160-
previous: ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>,
2161-
current: ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>,
2141+
previous: ty::PolyTraitPredicate<'tcx>,
2142+
current: ty::PolyTraitPredicate<'tcx>,
21622143
param_env: ty::ParamEnv<'tcx>,
21632144
) -> bool {
21642145
let mut matcher = ty::_match::Match::new(self.tcx(), param_env);
@@ -2170,17 +2151,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
21702151
previous_stack: TraitObligationStackList<'o, 'tcx>,
21712152
obligation: &'o TraitObligation<'tcx>,
21722153
) -> TraitObligationStack<'o, 'tcx> {
2173-
let fresh_trait_ref = obligation
2174-
.predicate
2175-
.to_poly_trait_ref()
2176-
.fold_with(&mut self.freshener)
2177-
.with_constness(obligation.predicate.skip_binder().constness);
2154+
let fresh_trait_pred = obligation.predicate.fold_with(&mut self.freshener);
21782155

21792156
let dfn = previous_stack.cache.next_dfn();
21802157
let depth = previous_stack.depth() + 1;
21812158
TraitObligationStack {
21822159
obligation,
2183-
fresh_trait_ref,
2160+
fresh_trait_pred,
21842161
reached_depth: Cell::new(depth),
21852162
previous: previous_stack,
21862163
dfn,
@@ -2374,7 +2351,7 @@ impl<'o, 'tcx> TraitObligationStack<'o, 'tcx> {
23742351
debug!(reached_depth, "update_reached_depth");
23752352
let mut p = self;
23762353
while reached_depth < p.depth {
2377-
debug!(?p.fresh_trait_ref, "update_reached_depth: marking as cycle participant");
2354+
debug!(?p.fresh_trait_pred, "update_reached_depth: marking as cycle participant");
23782355
p.reached_depth.set(p.reached_depth.get().min(reached_depth));
23792356
p = p.previous.head.unwrap();
23802357
}
@@ -2453,7 +2430,7 @@ struct ProvisionalEvaluationCache<'tcx> {
24532430
/// - then we determine that `E` is in error -- we will then clear
24542431
/// all cache values whose DFN is >= 4 -- in this case, that
24552432
/// means the cached value for `F`.
2456-
map: RefCell<FxHashMap<ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>, ProvisionalEvaluation>>,
2433+
map: RefCell<FxHashMap<ty::PolyTraitPredicate<'tcx>, ProvisionalEvaluation>>,
24572434
}
24582435

24592436
/// A cache value for the provisional cache: contains the depth-first
@@ -2485,28 +2462,28 @@ impl<'tcx> ProvisionalEvaluationCache<'tcx> {
24852462
/// `reached_depth` (from the returned value).
24862463
fn get_provisional(
24872464
&self,
2488-
fresh_trait_ref: ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>,
2465+
fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
24892466
) -> Option<ProvisionalEvaluation> {
24902467
debug!(
2491-
?fresh_trait_ref,
2468+
?fresh_trait_pred,
24922469
"get_provisional = {:#?}",
2493-
self.map.borrow().get(&fresh_trait_ref),
2470+
self.map.borrow().get(&fresh_trait_pred),
24942471
);
2495-
Some(*self.map.borrow().get(&fresh_trait_ref)?)
2472+
Some(*self.map.borrow().get(&fresh_trait_pred)?)
24962473
}
24972474

24982475
/// Insert a provisional result into the cache. The result came
24992476
/// from the node with the given DFN. It accessed a minimum depth
2500-
/// of `reached_depth` to compute. It evaluated `fresh_trait_ref`
2477+
/// of `reached_depth` to compute. It evaluated `fresh_trait_pred`
25012478
/// and resulted in `result`.
25022479
fn insert_provisional(
25032480
&self,
25042481
from_dfn: usize,
25052482
reached_depth: usize,
2506-
fresh_trait_ref: ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>,
2483+
fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
25072484
result: EvaluationResult,
25082485
) {
2509-
debug!(?from_dfn, ?fresh_trait_ref, ?result, "insert_provisional");
2486+
debug!(?from_dfn, ?fresh_trait_pred, ?result, "insert_provisional");
25102487

25112488
let mut map = self.map.borrow_mut();
25122489

@@ -2530,7 +2507,7 @@ impl<'tcx> ProvisionalEvaluationCache<'tcx> {
25302507
}
25312508
}
25322509

2533-
map.insert(fresh_trait_ref, ProvisionalEvaluation { from_dfn, reached_depth, result });
2510+
map.insert(fresh_trait_pred, ProvisionalEvaluation { from_dfn, reached_depth, result });
25342511
}
25352512

25362513
/// Invoked when the node with dfn `dfn` does not get a successful
@@ -2581,16 +2558,16 @@ impl<'tcx> ProvisionalEvaluationCache<'tcx> {
25812558
fn on_completion(
25822559
&self,
25832560
dfn: usize,
2584-
mut op: impl FnMut(ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>, EvaluationResult),
2561+
mut op: impl FnMut(ty::PolyTraitPredicate<'tcx>, EvaluationResult),
25852562
) {
25862563
debug!(?dfn, "on_completion");
25872564

2588-
for (fresh_trait_ref, eval) in
2565+
for (fresh_trait_pred, eval) in
25892566
self.map.borrow_mut().drain_filter(|_k, eval| eval.from_dfn >= dfn)
25902567
{
2591-
debug!(?fresh_trait_ref, ?eval, "on_completion");
2568+
debug!(?fresh_trait_pred, ?eval, "on_completion");
25922569

2593-
op(fresh_trait_ref, eval.result);
2570+
op(fresh_trait_pred, eval.result);
25942571
}
25952572
}
25962573
}

0 commit comments

Comments
 (0)