Skip to content

Commit ec667fb

Browse files
committed
Auto merge of #95031 - compiler-errors:param-env-cache, r=Aaron1011
Do not use `ParamEnv::and` when building a cache key from a param-env and trait eval candidate Do not use `ParamEnv::and` to cache a param-env with a selection/evaluation candidate. This is because if the param-env is `RevealAll` mode, and the candidate looks global (i.e. it has erased regions, which can show up when we normalize a projection type under a binder<sup>1</sup>), then when we use `ParamEnv::and` to pair the candidate and the param-env for use as a cache key, we will throw away the param-env's caller bounds, and we'll end up caching a candidate that we inferred from the param-env with a empty param-env, which may cause cache-hit later when we have an empty param-env, and possibly mess with normalization like we see in the referenced issue during codegen. Not sure how to trigger this with a more structured test, but changing `check-pass` to `build-pass` triggers the case that #94903 detected. <sup>1.</sup> That is, we will replace the late-bound region with a placeholder, which gets canonicalized and turned into an infererence variable, which gets erased during region freshening right before we cache the result. Sorry, it's quite a few steps. Fixes #94903 r? `@Aaron1011` (or reassign as you see fit)
2 parents ac4b345 + 8588f79 commit ec667fb

File tree

3 files changed

+20
-12
lines changed
  • compiler
    • rustc_middle/src/traits
    • rustc_trait_selection/src/traits/select
  • src/test/ui/higher-rank-trait-bounds/normalize-under-binder

3 files changed

+20
-12
lines changed

compiler/rustc_middle/src/traits/select.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ use rustc_hir::def_id::DefId;
1313
use rustc_query_system::cache::Cache;
1414

1515
pub type SelectionCache<'tcx> = Cache<
16-
ty::ParamEnvAnd<'tcx, ty::TraitPredicate<'tcx>>,
16+
// This cache does not use `ParamEnvAnd` in its keys because `ParamEnv::and` can replace
17+
// caller bounds with an empty list if the `TraitPredicate` looks global, which may happen
18+
// after erasing lifetimes from the predicate.
19+
(ty::ParamEnv<'tcx>, ty::TraitPredicate<'tcx>),
1720
SelectionResult<'tcx, SelectionCandidate<'tcx>>,
1821
>;
1922

20-
pub type EvaluationCache<'tcx> =
21-
Cache<ty::ParamEnvAnd<'tcx, ty::PolyTraitPredicate<'tcx>>, EvaluationResult>;
23+
pub type EvaluationCache<'tcx> = Cache<
24+
// See above: this cache does not use `ParamEnvAnd` in its keys due to sometimes incorrectly
25+
// caching with the wrong `ParamEnv`.
26+
(ty::ParamEnv<'tcx>, ty::PolyTraitPredicate<'tcx>),
27+
EvaluationResult,
28+
>;
2229

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

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -1025,11 +1025,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10251025

10261026
let tcx = self.tcx();
10271027
if self.can_use_global_caches(param_env) {
1028-
if let Some(res) = tcx.evaluation_cache.get(&param_env.and(trait_pred), tcx) {
1028+
if let Some(res) = tcx.evaluation_cache.get(&(param_env, trait_pred), tcx) {
10291029
return Some(res);
10301030
}
10311031
}
1032-
self.infcx.evaluation_cache.get(&param_env.and(trait_pred), tcx)
1032+
self.infcx.evaluation_cache.get(&(param_env, trait_pred), tcx)
10331033
}
10341034

10351035
fn insert_evaluation_cache(
@@ -1060,13 +1060,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10601060
// FIXME: Due to #50507 this overwrites the different values
10611061
// This should be changed to use HashMapExt::insert_same
10621062
// when that is fixed
1063-
self.tcx().evaluation_cache.insert(param_env.and(trait_pred), dep_node, result);
1063+
self.tcx().evaluation_cache.insert((param_env, trait_pred), dep_node, result);
10641064
return;
10651065
}
10661066
}
10671067

10681068
debug!(?trait_pred, ?result, "insert_evaluation_cache");
1069-
self.infcx.evaluation_cache.insert(param_env.and(trait_pred), dep_node, result);
1069+
self.infcx.evaluation_cache.insert((param_env, trait_pred), dep_node, result);
10701070
}
10711071

10721072
/// For various reasons, it's possible for a subobligation
@@ -1275,11 +1275,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12751275
pred.remap_constness(tcx, &mut param_env);
12761276

12771277
if self.can_use_global_caches(param_env) {
1278-
if let Some(res) = tcx.selection_cache.get(&param_env.and(pred), tcx) {
1278+
if let Some(res) = tcx.selection_cache.get(&(param_env, pred), tcx) {
12791279
return Some(res);
12801280
}
12811281
}
1282-
self.infcx.selection_cache.get(&param_env.and(pred), tcx)
1282+
self.infcx.selection_cache.get(&(param_env, pred), tcx)
12831283
}
12841284

12851285
/// Determines whether can we safely cache the result
@@ -1340,14 +1340,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13401340
if !candidate.needs_infer() {
13411341
debug!(?pred, ?candidate, "insert_candidate_cache global");
13421342
// This may overwrite the cache with the same value.
1343-
tcx.selection_cache.insert(param_env.and(pred), dep_node, candidate);
1343+
tcx.selection_cache.insert((param_env, pred), dep_node, candidate);
13441344
return;
13451345
}
13461346
}
13471347
}
13481348

13491349
debug!(?pred, ?candidate, "insert_candidate_cache local");
1350-
self.infcx.selection_cache.insert(param_env.and(pred), dep_node, candidate);
1350+
self.infcx.selection_cache.insert((param_env, pred), dep_node, candidate);
13511351
}
13521352

13531353
/// Matches a predicate against the bounds of its self type.

src/test/ui/higher-rank-trait-bounds/normalize-under-binder/issue-80706.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// check-pass
1+
// build-pass
22
// edition:2018
33

44
type BoxFuture<T> = std::pin::Pin<Box<dyn std::future::Future<Output=T>>>;
@@ -65,6 +65,7 @@ async fn run<S>(dep: &str)
6565
where
6666
S: Storage,
6767
for<'a> SaveUser<'a>: StorageRequest<S>,
68+
for<'a> SaveUser<'a>: StorageRequestReturnType,
6869
{
6970
User { dep }.save().await;
7071
}

0 commit comments

Comments
 (0)