Skip to content

Commit

Permalink
Prefer TraitPredicate over ConstnessAnd<TraitRef>
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk authored and fee1-dead committed Nov 29, 2021
1 parent 40f39e6 commit b16c811
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/traits/select.rs
Expand Up @@ -12,7 +12,7 @@ use rustc_hir::def_id::DefId;
use rustc_query_system::cache::Cache;

pub type SelectionCache<'tcx> = Cache<
(ty::ConstnessAnd<ty::ParamEnvAnd<'tcx, ty::TraitRef<'tcx>>>, ty::ImplPolarity),
ty::ParamEnvAnd<'tcx, ty::TraitPredicate<'tcx>>,
SelectionResult<'tcx, SelectionCandidate<'tcx>>,
>;

Expand Down
33 changes: 9 additions & 24 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Expand Up @@ -1231,19 +1231,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
return None;
}
let tcx = self.tcx();
let pred = &cache_fresh_trait_pred.skip_binder();
let trait_ref = pred.trait_ref;
let pred = cache_fresh_trait_pred.skip_binder();
if self.can_use_global_caches(param_env) {
if let Some(res) = tcx
.selection_cache
.get(&(param_env.and(trait_ref).with_constness(pred.constness), pred.polarity), tcx)
{
if let Some(res) = tcx.selection_cache.get(&param_env.and(pred), tcx) {
return Some(res);
}
}
self.infcx
.selection_cache
.get(&(param_env.and(trait_ref).with_constness(pred.constness), pred.polarity), tcx)
self.infcx.selection_cache.get(&param_env.and(pred), tcx)
}

/// Determines whether can we safely cache the result
Expand Down Expand Up @@ -1288,36 +1282,27 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
) {
let tcx = self.tcx();
let pred = cache_fresh_trait_pred.skip_binder();
let trait_ref = pred.trait_ref;

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

if self.can_use_global_caches(param_env) {
if let Err(Overflow) = candidate {
// Don't cache overflow globally; we only produce this in certain modes.
} else if !trait_ref.needs_infer() {
} else if !pred.needs_infer() {
if !candidate.needs_infer() {
debug!(?trait_ref, ?candidate, "insert_candidate_cache global");
debug!(?pred, ?candidate, "insert_candidate_cache global");
// This may overwrite the cache with the same value.
tcx.selection_cache.insert(
(param_env.and(trait_ref).with_constness(pred.constness), pred.polarity),
dep_node,
candidate,
);
tcx.selection_cache.insert(param_env.and(pred), dep_node, candidate);
return;
}
}
}

debug!(?trait_ref, ?candidate, "insert_candidate_cache local");
self.infcx.selection_cache.insert(
(param_env.and(trait_ref).with_constness(pred.constness), pred.polarity),
dep_node,
candidate,
);
debug!(?pred, ?candidate, "insert_candidate_cache local");
self.infcx.selection_cache.insert(param_env.and(pred), dep_node, candidate);
}

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

0 comments on commit b16c811

Please sign in to comment.