Skip to content

Commit 6b9862d

Browse files
assert that global cache values are stable on insertion
1 parent 31656e7 commit 6b9862d

File tree

3 files changed

+13
-7
lines changed
  • compiler
    • rustc_middle/src/traits
    • rustc_query_system/src
    • rustc_trait_selection/src/traits/select

3 files changed

+13
-7
lines changed

compiler/rustc_middle/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ pub struct DerivedObligationCause<'tcx> {
569569
pub parent_code: InternedObligationCauseCode<'tcx>,
570570
}
571571

572-
#[derive(Clone, Debug, TypeFoldable, TypeVisitable, Lift)]
572+
#[derive(PartialEq, Eq, Clone, Debug, TypeFoldable, TypeVisitable, Lift)]
573573
pub enum SelectionError<'tcx> {
574574
/// The trait is not implemented.
575575
Unimplemented,

compiler/rustc_query_system/src/cache.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::dep_graph::{DepContext, DepNodeIndex};
55
use rustc_data_structures::fx::FxHashMap;
66
use rustc_data_structures::sync::Lock;
77

8+
use std::fmt::Debug;
89
use std::hash::Hash;
910

1011
pub struct Cache<Key, Value> {
@@ -30,13 +31,22 @@ impl<Key, Value> Cache<Key, Value> {
3031
}
3132
}
3233

33-
impl<Key: Eq + Hash, Value: Clone> Cache<Key, Value> {
34+
impl<Key: Eq + Hash, Value: Eq + Clone + Debug> Cache<Key, Value> {
3435
pub fn get<Tcx: DepContext>(&self, key: &Key, tcx: Tcx) -> Option<Value> {
3536
Some(self.hashmap.borrow().get(key)?.get(tcx))
3637
}
3738

3839
pub fn insert(&self, key: Key, dep_node: DepNodeIndex, value: Value) {
39-
self.hashmap.borrow_mut().insert(key, WithDepNode::new(dep_node, value));
40+
// FIXME(#50507): For some reason we're getting different `DepNodeIndex`es
41+
// for the same evaluation in the parallel compiler. Once that's fixed
42+
// we can just use `HashMap::insert_same` here instead of doing all this.
43+
self.hashmap
44+
.borrow_mut()
45+
.entry(key)
46+
.and_modify(|old_value| {
47+
assert_eq!(old_value.cached_value, value, "unstable cached value")
48+
})
49+
.or_insert(WithDepNode::new(dep_node, value));
4050
}
4151
}
4252

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

-4
Original file line numberDiff line numberDiff line change
@@ -1333,10 +1333,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13331333
if self.can_use_global_caches(param_env) {
13341334
if !trait_pred.needs_infer() {
13351335
debug!(?trait_pred, ?result, "insert_evaluation_cache global");
1336-
// This may overwrite the cache with the same value
1337-
// FIXME: Due to #50507 this overwrites the different values
1338-
// This should be changed to use HashMapExt::insert_same
1339-
// when that is fixed
13401336
self.tcx().evaluation_cache.insert((param_env, trait_pred), dep_node, result);
13411337
return;
13421338
}

0 commit comments

Comments
 (0)