1
Fork 0

Make iteration order of region_scope_tree query stable

This commit is contained in:
Michael Woerister 2023-12-21 11:35:15 +01:00
parent d4a4c1dd28
commit 36dd3d4524
2 changed files with 21 additions and 26 deletions

View file

@ -7,12 +7,11 @@
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/borrow_check.html //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/borrow_check.html
use crate::ty::TyCtxt; use crate::ty::TyCtxt;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::unord::UnordMap;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::{HirIdMap, Node}; use rustc_hir::{HirIdMap, Node};
use rustc_macros::HashStable; use rustc_macros::HashStable;
use rustc_query_system::ich::StableHashingContext;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
use std::fmt; use std::fmt;
@ -205,7 +204,7 @@ impl Scope {
pub type ScopeDepth = u32; pub type ScopeDepth = u32;
/// The region scope tree encodes information about region relationships. /// The region scope tree encodes information about region relationships.
#[derive(Default, Debug)] #[derive(Default, Debug, HashStable)]
pub struct ScopeTree { pub struct ScopeTree {
/// If not empty, this body is the root of this region hierarchy. /// If not empty, this body is the root of this region hierarchy.
pub root_body: Option<hir::HirId>, pub root_body: Option<hir::HirId>,
@ -306,7 +305,7 @@ pub struct ScopeTree {
/// The reason is that semantically, until the `box` expression returns, /// The reason is that semantically, until the `box` expression returns,
/// the values are still owned by their containing expressions. So /// the values are still owned by their containing expressions. So
/// we'll see that `&x`. /// we'll see that `&x`.
pub yield_in_scope: FxHashMap<Scope, Vec<YieldData>>, pub yield_in_scope: UnordMap<Scope, Vec<YieldData>>,
} }
/// Identifies the reason that a given expression is an rvalue candidate /// Identifies the reason that a given expression is an rvalue candidate
@ -404,23 +403,3 @@ impl ScopeTree {
self.yield_in_scope.get(&scope).map(Deref::deref) self.yield_in_scope.get(&scope).map(Deref::deref)
} }
} }
impl<'a> HashStable<StableHashingContext<'a>> for ScopeTree {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
let ScopeTree {
root_body,
ref parent_map,
ref var_map,
ref destruction_scopes,
ref rvalue_candidates,
ref yield_in_scope,
} = *self;
root_body.hash_stable(hcx, hasher);
parent_map.hash_stable(hcx, hasher);
var_map.hash_stable(hcx, hasher);
destruction_scopes.hash_stable(hcx, hasher);
rvalue_candidates.hash_stable(hcx, hasher);
yield_in_scope.hash_stable(hcx, hasher);
}
}

View file

@ -1,6 +1,8 @@
use crate::{HashStableContext, Symbol}; use crate::{HashStableContext, Symbol};
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher, ToStableHashKey}; use rustc_data_structures::stable_hasher::{
Hash64, HashStable, StableHasher, StableOrd, ToStableHashKey,
};
use rustc_data_structures::unhash::Unhasher; use rustc_data_structures::unhash::Unhasher;
use rustc_data_structures::AtomicRef; use rustc_data_structures::AtomicRef;
use rustc_index::Idx; use rustc_index::Idx;
@ -132,6 +134,11 @@ impl Default for DefPathHash {
} }
} }
// Safety: `DefPathHash` sort order is not affected (de)serialization.
unsafe impl StableOrd for DefPathHash {
const CAN_USE_UNSTABLE_SORT: bool = true;
}
/// A [`StableCrateId`] is a 64-bit hash of a crate name, together with all /// A [`StableCrateId`] is a 64-bit hash of a crate name, together with all
/// `-Cmetadata` arguments, and some other data. It is to [`CrateNum`] what [`DefPathHash`] is to /// `-Cmetadata` arguments, and some other data. It is to [`CrateNum`] what [`DefPathHash`] is to
/// [`DefId`]. It is stable across compilation sessions. /// [`DefId`]. It is stable across compilation sessions.
@ -490,6 +497,15 @@ impl<CTX: HashStableContext> ToStableHashKey<CTX> for CrateNum {
} }
} }
impl<CTX: HashStableContext> ToStableHashKey<CTX> for DefPathHash {
type KeyType = DefPathHash;
#[inline]
fn to_stable_hash_key(&self, _: &CTX) -> DefPathHash {
*self
}
}
macro_rules! typed_def_id { macro_rules! typed_def_id {
($Name:ident, $LocalName:ident) => { ($Name:ident, $LocalName:ident) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]