Skip to content

Commit 6b07cec

Browse files
committed
Cache with consistent env and bound
1 parent 87cd1ce commit 6b07cec

File tree

2 files changed

+36
-8
lines changed
  • compiler
    • rustc_middle/src/ty
    • rustc_trait_selection/src/traits/select

2 files changed

+36
-8
lines changed

compiler/rustc_middle/src/ty/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ pub enum BoundConstness {
230230
ConstIfConst,
231231
}
232232

233+
impl BoundConstness {
234+
/// Reduce `self` and `constness` to two possible combined states instead of four.
235+
pub fn and(&mut self, constness: hir::Constness) -> hir::Constness {
236+
match (constness, self) {
237+
(hir::Constness::Const, BoundConstness::ConstIfConst) => hir::Constness::Const,
238+
(_, this) => {
239+
*this = BoundConstness::NotConst;
240+
hir::Constness::NotConst
241+
}
242+
}
243+
}
244+
}
245+
233246
impl fmt::Display for BoundConstness {
234247
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
235248
match self {
@@ -1326,6 +1339,11 @@ impl<'tcx> ParamEnv<'tcx> {
13261339
self
13271340
}
13281341

1342+
pub fn with_constness(mut self, constness: hir::Constness) -> Self {
1343+
self.packed.set_tag(ParamTag { constness, ..self.packed.tag() });
1344+
self
1345+
}
1346+
13291347
pub fn with_const(mut self) -> Self {
13301348
self.packed.set_tag(ParamTag { constness: hir::Constness::Const, ..self.packed.tag() });
13311349
self

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

+18-8
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
675675
}
676676

677677
let stack = self.push_stack(previous_stack, &obligation);
678-
let fresh_trait_pred = stack.fresh_trait_pred;
678+
let mut fresh_trait_pred = stack.fresh_trait_pred;
679+
let mut param_env = obligation.param_env;
680+
681+
fresh_trait_pred = fresh_trait_pred.map_bound(|mut pred| {
682+
param_env = param_env.with_constness(pred.constness.and(param_env.constness()));
683+
pred
684+
});
679685

680686
debug!(?fresh_trait_pred);
681687

682-
if let Some(result) = self.check_evaluation_cache(obligation.param_env, fresh_trait_pred) {
688+
if let Some(result) = self.check_evaluation_cache(param_env, fresh_trait_pred) {
683689
debug!(?result, "CACHE HIT");
684690
return Ok(result);
685691
}
@@ -709,11 +715,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
709715
let reached_depth = stack.reached_depth.get();
710716
if reached_depth >= stack.depth {
711717
debug!(?result, "CACHE MISS");
712-
self.insert_evaluation_cache(obligation.param_env, fresh_trait_pred, dep_node, result);
718+
self.insert_evaluation_cache(param_env, fresh_trait_pred, dep_node, result);
713719

714720
stack.cache().on_completion(stack.dfn, |fresh_trait_pred, provisional_result| {
715721
self.insert_evaluation_cache(
716-
obligation.param_env,
722+
param_env,
717723
fresh_trait_pred,
718724
dep_node,
719725
provisional_result.max(result),
@@ -1200,7 +1206,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12001206

12011207
fn check_candidate_cache(
12021208
&mut self,
1203-
param_env: ty::ParamEnv<'tcx>,
1209+
mut param_env: ty::ParamEnv<'tcx>,
12041210
cache_fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
12051211
) -> Option<SelectionResult<'tcx, SelectionCandidate<'tcx>>> {
12061212
// Neither the global nor local cache is aware of intercrate
@@ -1211,7 +1217,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12111217
return None;
12121218
}
12131219
let tcx = self.tcx();
1214-
let pred = cache_fresh_trait_pred.skip_binder();
1220+
let mut pred = cache_fresh_trait_pred.skip_binder();
1221+
param_env = param_env.with_constness(pred.constness.and(param_env.constness()));
1222+
12151223
if self.can_use_global_caches(param_env) {
12161224
if let Some(res) = tcx.selection_cache.get(&param_env.and(pred), tcx) {
12171225
return Some(res);
@@ -1255,13 +1263,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12551263

12561264
fn insert_candidate_cache(
12571265
&mut self,
1258-
param_env: ty::ParamEnv<'tcx>,
1266+
mut param_env: ty::ParamEnv<'tcx>,
12591267
cache_fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
12601268
dep_node: DepNodeIndex,
12611269
candidate: SelectionResult<'tcx, SelectionCandidate<'tcx>>,
12621270
) {
12631271
let tcx = self.tcx();
1264-
let pred = cache_fresh_trait_pred.skip_binder();
1272+
let mut pred = cache_fresh_trait_pred.skip_binder();
1273+
1274+
param_env = param_env.with_constness(pred.constness.and(param_env.constness()));
12651275

12661276
if !self.can_cache_candidate(&candidate) {
12671277
debug!(?pred, ?candidate, "insert_candidate_cache - candidate is not cacheable");

0 commit comments

Comments
 (0)