Skip to content

Do not cache ParamEnv candidates globally #94949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
}

fn can_cache_candidate_globally(
&self,
result: &SelectionResult<'tcx, SelectionCandidate<'tcx>>,
) -> bool {
// We cannot globally cache a result if it has infer variables
// (which means it is local to the infcx that holds the variable)
// or if the result is a ParamCandidate, which means it's local to
// the ParamEnv we are evaluating under.
!(result.needs_infer() || matches!(result, Ok(Some(SelectionCandidate::ParamCandidate(_)))))
}

fn insert_candidate_cache(
&mut self,
mut param_env: ty::ParamEnv<'tcx>,
Expand All @@ -1356,7 +1367,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
if let Err(Overflow) = candidate {
// Don't cache overflow globally; we only produce this in certain modes.
} else if !pred.needs_infer() {
if !candidate.needs_infer() {
if self.can_cache_candidate_globally(&candidate) {
debug!(?pred, ?candidate, "insert_candidate_cache global");
// This may overwrite the cache with the same value.
tcx.selection_cache.insert(param_env.and(pred), dep_node, candidate);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// check-pass
// build-pass
// edition:2018

type BoxFuture<T> = std::pin::Pin<Box<dyn std::future::Future<Output=T>>>;
Expand Down