diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index 6a8ae525069c0..b7ab5f7cb889a 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -569,7 +569,7 @@ pub struct DerivedObligationCause<'tcx> { pub parent_code: InternedObligationCauseCode<'tcx>, } -#[derive(Clone, Debug, TypeFoldable, TypeVisitable, Lift)] +#[derive(PartialEq, Eq, Clone, Debug, TypeFoldable, TypeVisitable, Lift)] pub enum SelectionError<'tcx> { /// The trait is not implemented. Unimplemented, diff --git a/compiler/rustc_query_system/src/cache.rs b/compiler/rustc_query_system/src/cache.rs index 6e862db0b2547..61af88656c9d1 100644 --- a/compiler/rustc_query_system/src/cache.rs +++ b/compiler/rustc_query_system/src/cache.rs @@ -5,6 +5,7 @@ use crate::dep_graph::{DepContext, DepNodeIndex}; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::sync::Lock; +use std::fmt::Debug; use std::hash::Hash; pub struct Cache { @@ -30,13 +31,22 @@ impl Cache { } } -impl Cache { +impl Cache { pub fn get(&self, key: &Key, tcx: Tcx) -> Option { Some(self.hashmap.borrow().get(key)?.get(tcx)) } pub fn insert(&self, key: Key, dep_node: DepNodeIndex, value: Value) { - self.hashmap.borrow_mut().insert(key, WithDepNode::new(dep_node, value)); + // FIXME(#50507): For some reason we're getting different `DepNodeIndex`es + // for the same evaluation in the parallel compiler. Once that's fixed + // we can just use `HashMap::insert_same` here instead of doing all this. + self.hashmap + .borrow_mut() + .entry(key) + .and_modify(|old_value| { + assert_eq!(old_value.cached_value, value, "unstable cached value") + }) + .or_insert(WithDepNode::new(dep_node, value)); } } diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 6bb53418beabb..7283469705e85 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -1333,10 +1333,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { if self.can_use_global_caches(param_env) { if !trait_pred.needs_infer() { debug!(?trait_pred, ?result, "insert_evaluation_cache global"); - // This may overwrite the cache with the same value - // FIXME: Due to #50507 this overwrites the different values - // This should be changed to use HashMapExt::insert_same - // when that is fixed self.tcx().evaluation_cache.insert((param_env, trait_pred), dep_node, result); return; }