Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d161cc2

Browse files
oli-obkfee1-dead
authored andcommitted
Replace ConstnessAnd<TraitRef> with TraitPredicate which conveys the same information
1 parent b16c811 commit d161cc2

File tree

2 files changed

+46
-69
lines changed
  • compiler
    • rustc_middle/src/traits
    • rustc_trait_selection/src/traits/select

2 files changed

+46
-69
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: 44 additions & 65 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
@@ -2154,8 +2135,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
21542135

21552136
fn match_fresh_trait_refs(
21562137
&self,
2157-
previous: ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>,
2158-
current: ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>,
2138+
previous: ty::PolyTraitPredicate<'tcx>,
2139+
current: ty::PolyTraitPredicate<'tcx>,
21592140
param_env: ty::ParamEnv<'tcx>,
21602141
) -> bool {
21612142
let mut matcher = ty::_match::Match::new(self.tcx(), param_env);
@@ -2167,18 +2148,16 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
21672148
previous_stack: TraitObligationStackList<'o, 'tcx>,
21682149
obligation: &'o TraitObligation<'tcx>,
21692150
) -> TraitObligationStack<'o, 'tcx> {
2170-
let fresh_trait_ref = obligation
2151+
let fresh_trait_pred = obligation
21712152
.predicate
2172-
.to_poly_trait_ref()
21732153
.fold_with(&mut self.freshener)
2174-
.into_ok()
2175-
.with_constness(obligation.predicate.skip_binder().constness);
2154+
.into_ok();
21762155

21772156
let dfn = previous_stack.cache.next_dfn();
21782157
let depth = previous_stack.depth() + 1;
21792158
TraitObligationStack {
21802159
obligation,
2181-
fresh_trait_ref,
2160+
fresh_trait_pred,
21822161
reached_depth: Cell::new(depth),
21832162
previous: previous_stack,
21842163
dfn,
@@ -2372,7 +2351,7 @@ impl<'o, 'tcx> TraitObligationStack<'o, 'tcx> {
23722351
debug!(reached_depth, "update_reached_depth");
23732352
let mut p = self;
23742353
while reached_depth < p.depth {
2375-
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");
23762355
p.reached_depth.set(p.reached_depth.get().min(reached_depth));
23772356
p = p.previous.head.unwrap();
23782357
}
@@ -2451,7 +2430,7 @@ struct ProvisionalEvaluationCache<'tcx> {
24512430
/// - then we determine that `E` is in error -- we will then clear
24522431
/// all cache values whose DFN is >= 4 -- in this case, that
24532432
/// means the cached value for `F`.
2454-
map: RefCell<FxHashMap<ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>, ProvisionalEvaluation>>,
2433+
map: RefCell<FxHashMap<ty::PolyTraitPredicate<'tcx>, ProvisionalEvaluation>>,
24552434
}
24562435

24572436
/// A cache value for the provisional cache: contains the depth-first
@@ -2483,28 +2462,28 @@ impl<'tcx> ProvisionalEvaluationCache<'tcx> {
24832462
/// `reached_depth` (from the returned value).
24842463
fn get_provisional(
24852464
&self,
2486-
fresh_trait_ref: ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>,
2465+
fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
24872466
) -> Option<ProvisionalEvaluation> {
24882467
debug!(
2489-
?fresh_trait_ref,
2468+
?fresh_trait_pred,
24902469
"get_provisional = {:#?}",
2491-
self.map.borrow().get(&fresh_trait_ref),
2470+
self.map.borrow().get(&fresh_trait_pred),
24922471
);
2493-
Some(*self.map.borrow().get(&fresh_trait_ref)?)
2472+
Some(*self.map.borrow().get(&fresh_trait_pred)?)
24942473
}
24952474

24962475
/// Insert a provisional result into the cache. The result came
24972476
/// from the node with the given DFN. It accessed a minimum depth
2498-
/// of `reached_depth` to compute. It evaluated `fresh_trait_ref`
2477+
/// of `reached_depth` to compute. It evaluated `fresh_trait_pred`
24992478
/// and resulted in `result`.
25002479
fn insert_provisional(
25012480
&self,
25022481
from_dfn: usize,
25032482
reached_depth: usize,
2504-
fresh_trait_ref: ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>,
2483+
fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
25052484
result: EvaluationResult,
25062485
) {
2507-
debug!(?from_dfn, ?fresh_trait_ref, ?result, "insert_provisional");
2486+
debug!(?from_dfn, ?fresh_trait_pred, ?result, "insert_provisional");
25082487

25092488
let mut map = self.map.borrow_mut();
25102489

@@ -2528,7 +2507,7 @@ impl<'tcx> ProvisionalEvaluationCache<'tcx> {
25282507
}
25292508
}
25302509

2531-
map.insert(fresh_trait_ref, ProvisionalEvaluation { from_dfn, reached_depth, result });
2510+
map.insert(fresh_trait_pred, ProvisionalEvaluation { from_dfn, reached_depth, result });
25322511
}
25332512

25342513
/// Invoked when the node with dfn `dfn` does not get a successful
@@ -2579,16 +2558,16 @@ impl<'tcx> ProvisionalEvaluationCache<'tcx> {
25792558
fn on_completion(
25802559
&self,
25812560
dfn: usize,
2582-
mut op: impl FnMut(ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>, EvaluationResult),
2561+
mut op: impl FnMut(ty::PolyTraitPredicate<'tcx>, EvaluationResult),
25832562
) {
25842563
debug!(?dfn, "on_completion");
25852564

2586-
for (fresh_trait_ref, eval) in
2565+
for (fresh_trait_pred, eval) in
25872566
self.map.borrow_mut().drain_filter(|_k, eval| eval.from_dfn >= dfn)
25882567
{
2589-
debug!(?fresh_trait_ref, ?eval, "on_completion");
2568+
debug!(?fresh_trait_pred, ?eval, "on_completion");
25902569

2591-
op(fresh_trait_ref, eval.result);
2570+
op(fresh_trait_pred, eval.result);
25922571
}
25932572
}
25942573
}

0 commit comments

Comments
 (0)