Skip to content

Commit 9ee7a15

Browse files
committed
traits/select: use global vs per-infcx caches more uniformly.
1 parent 7710ae0 commit 9ee7a15

File tree

1 file changed

+58
-68
lines changed

1 file changed

+58
-68
lines changed

src/librustc_infer/traits/select.rs

+58-68
Original file line numberDiff line numberDiff line change
@@ -835,18 +835,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
835835
trait_ref: ty::PolyTraitRef<'tcx>,
836836
) -> Option<EvaluationResult> {
837837
let tcx = self.tcx();
838-
if self.can_use_global_caches(param_env) {
839-
let cache = tcx.evaluation_cache.hashmap.borrow();
840-
if let Some(cached) = cache.get(&param_env.and(trait_ref)) {
841-
return Some(cached.get(tcx));
842-
}
843-
}
844-
self.infcx
845-
.evaluation_cache
846-
.hashmap
847-
.borrow()
848-
.get(&param_env.and(trait_ref))
849-
.map(|v| v.get(tcx))
838+
let cache = if self.can_use_global_caches(param_env) && !trait_ref.has_local_value() {
839+
&tcx.evaluation_cache
840+
} else {
841+
&self.infcx.evaluation_cache
842+
};
843+
844+
cache.hashmap.borrow().get(&param_env.and(trait_ref)).map(|v| v.get(tcx))
850845
}
851846

852847
fn insert_evaluation_cache(
@@ -862,28 +857,22 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
862857
return;
863858
}
864859

865-
if self.can_use_global_caches(param_env) {
866-
if !trait_ref.has_local_value() {
867-
debug!(
868-
"insert_evaluation_cache(trait_ref={:?}, candidate={:?}) global",
869-
trait_ref, result,
870-
);
871-
// This may overwrite the cache with the same value
872-
// FIXME: Due to #50507 this overwrites the different values
873-
// This should be changed to use HashMapExt::insert_same
874-
// when that is fixed
875-
self.tcx()
876-
.evaluation_cache
877-
.hashmap
878-
.borrow_mut()
879-
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, result));
880-
return;
881-
}
882-
}
860+
let cache = if self.can_use_global_caches(param_env) && !trait_ref.has_local_value() {
861+
debug!(
862+
"insert_evaluation_cache(trait_ref={:?}, candidate={:?}) global",
863+
trait_ref, result,
864+
);
865+
// This may overwrite the cache with the same value
866+
// FIXME: Due to #50507 this overwrites the different values
867+
// This should be changed to use HashMapExt::insert_same
868+
// when that is fixed
869+
&self.tcx().evaluation_cache
870+
} else {
871+
debug!("insert_evaluation_cache(trait_ref={:?}, candidate={:?})", trait_ref, result,);
872+
&self.infcx.evaluation_cache
873+
};
883874

884-
debug!("insert_evaluation_cache(trait_ref={:?}, candidate={:?})", trait_ref, result,);
885-
self.infcx
886-
.evaluation_cache
875+
cache
887876
.hashmap
888877
.borrow_mut()
889878
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, result));
@@ -982,6 +971,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
982971
cache_fresh_trait_pred,
983972
dep_node,
984973
candidate.clone(),
974+
stack.obligation.cause.span,
985975
);
986976
candidate
987977
}
@@ -1250,18 +1240,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12501240
) -> Option<SelectionResult<'tcx, SelectionCandidate<'tcx>>> {
12511241
let tcx = self.tcx();
12521242
let trait_ref = &cache_fresh_trait_pred.skip_binder().trait_ref;
1253-
if self.can_use_global_caches(param_env) {
1254-
let cache = tcx.selection_cache.hashmap.borrow();
1255-
if let Some(cached) = cache.get(&param_env.and(*trait_ref)) {
1256-
return Some(cached.get(tcx));
1257-
}
1258-
}
1259-
self.infcx
1260-
.selection_cache
1261-
.hashmap
1262-
.borrow()
1263-
.get(&param_env.and(*trait_ref))
1264-
.map(|v| v.get(tcx))
1243+
let cache = if self.can_use_global_caches(param_env) && !trait_ref.has_local_value() {
1244+
&tcx.selection_cache
1245+
} else {
1246+
&self.infcx.selection_cache
1247+
};
1248+
1249+
cache.hashmap.borrow().get(&param_env.and(*trait_ref)).map(|v| v.get(tcx))
12651250
}
12661251

12671252
/// Determines whether can we safely cache the result
@@ -1298,6 +1283,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12981283
cache_fresh_trait_pred: ty::PolyTraitPredicate<'tcx>,
12991284
dep_node: DepNodeIndex,
13001285
candidate: SelectionResult<'tcx, SelectionCandidate<'tcx>>,
1286+
span: rustc_span::Span,
13011287
) {
13021288
let tcx = self.tcx();
13031289
let trait_ref = cache_fresh_trait_pred.skip_binder().trait_ref;
@@ -1311,31 +1297,35 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13111297
return;
13121298
}
13131299

1314-
if self.can_use_global_caches(param_env) {
1315-
if let Err(Overflow) = candidate {
1316-
// Don't cache overflow globally; we only produce this in certain modes.
1317-
} else if !trait_ref.has_local_value() {
1318-
if !candidate.has_local_value() {
1319-
debug!(
1320-
"insert_candidate_cache(trait_ref={:?}, candidate={:?}) global",
1321-
trait_ref, candidate,
1322-
);
1323-
// This may overwrite the cache with the same value.
1324-
tcx.selection_cache
1325-
.hashmap
1326-
.borrow_mut()
1327-
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, candidate));
1328-
return;
1329-
}
1330-
}
1300+
// HACK(eddyb) never cache overflow (this check used to be global-only).
1301+
if let Err(Overflow) = candidate {
1302+
return;
13311303
}
13321304

1333-
debug!(
1334-
"insert_candidate_cache(trait_ref={:?}, candidate={:?}) local",
1335-
trait_ref, candidate,
1336-
);
1337-
self.infcx
1338-
.selection_cache
1305+
let cache = if self.can_use_global_caches(param_env) && !trait_ref.has_local_value() {
1306+
if candidate.has_local_value() {
1307+
span_bug!(
1308+
span,
1309+
"selecting inference-free `{}` resulted in `{:?}`?!",
1310+
trait_ref,
1311+
candidate,
1312+
);
1313+
}
1314+
debug!(
1315+
"insert_candidate_cache(trait_ref={:?}, candidate={:?}) global",
1316+
trait_ref, candidate,
1317+
);
1318+
// This may overwrite the cache with the same value.
1319+
&tcx.selection_cache
1320+
} else {
1321+
debug!(
1322+
"insert_candidate_cache(trait_ref={:?}, candidate={:?}) local",
1323+
trait_ref, candidate,
1324+
);
1325+
&self.infcx.selection_cache
1326+
};
1327+
1328+
cache
13391329
.hashmap
13401330
.borrow_mut()
13411331
.insert(param_env.and(trait_ref), WithDepNode::new(dep_node, candidate));

0 commit comments

Comments
 (0)