From 36dd3d4524ee6a0362f0866559e8289887406b83 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Thu, 21 Dec 2023 11:35:15 +0100 Subject: [PATCH] Make iteration order of region_scope_tree query stable --- compiler/rustc_middle/src/middle/region.rs | 29 +++------------------- compiler/rustc_span/src/def_id.rs | 18 +++++++++++++- 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_middle/src/middle/region.rs b/compiler/rustc_middle/src/middle/region.rs index b4dd8f6f4a7..5d6a7f75df8 100644 --- a/compiler/rustc_middle/src/middle/region.rs +++ b/compiler/rustc_middle/src/middle/region.rs @@ -7,12 +7,11 @@ //! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/borrow_check.html use crate::ty::TyCtxt; -use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; -use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; +use rustc_data_structures::fx::FxIndexMap; +use rustc_data_structures::unord::UnordMap; use rustc_hir as hir; use rustc_hir::{HirIdMap, Node}; use rustc_macros::HashStable; -use rustc_query_system::ich::StableHashingContext; use rustc_span::{Span, DUMMY_SP}; use std::fmt; @@ -205,7 +204,7 @@ impl Scope { pub type ScopeDepth = u32; /// The region scope tree encodes information about region relationships. -#[derive(Default, Debug)] +#[derive(Default, Debug, HashStable)] pub struct ScopeTree { /// If not empty, this body is the root of this region hierarchy. pub root_body: Option, @@ -306,7 +305,7 @@ pub struct ScopeTree { /// The reason is that semantically, until the `box` expression returns, /// the values are still owned by their containing expressions. So /// we'll see that `&x`. - pub yield_in_scope: FxHashMap>, + pub yield_in_scope: UnordMap>, } /// 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) } } - -impl<'a> HashStable> 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); - } -} diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index e397fab5459..9f1db227a7c 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -1,6 +1,8 @@ use crate::{HashStableContext, Symbol}; 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::AtomicRef; 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 /// `-Cmetadata` arguments, and some other data. It is to [`CrateNum`] what [`DefPathHash`] is to /// [`DefId`]. It is stable across compilation sessions. @@ -490,6 +497,15 @@ impl ToStableHashKey for CrateNum { } } +impl ToStableHashKey for DefPathHash { + type KeyType = DefPathHash; + + #[inline] + fn to_stable_hash_key(&self, _: &CTX) -> DefPathHash { + *self + } +} + macro_rules! typed_def_id { ($Name:ident, $LocalName:ident) => { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]