Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 123 additions & 2 deletions compiler/rustc_data_structures/src/stable_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::num::NonZero;

use rustc_index::bit_set::{self, DenseBitSet};
use rustc_index::{Idx, IndexSlice, IndexVec};
use rustc_macros::StableHash;
use smallvec::SmallVec;
use thin_vec::ThinVec;

Expand Down Expand Up @@ -37,6 +38,25 @@ pub trait StableHashCtxt {
fn assert_default_stable_hash_controls(&self, msg: &str);
}

/// Dummy impl of [`StableHashCtxt`], for code that can't provide a real context,
/// and only wants to compute stable hashes for types that don't rely on the context.
pub struct DummyStableHashCtxt;
Comment on lines +41 to +43

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"contextless stable hash context" 😁


impl StableHashCtxt for DummyStableHashCtxt {
fn stable_hash_span(&mut self, _span: RawSpan, _hasher: &mut StableHasher) {
panic!("method on dummy stable-hash context was unexpectedly called");
}
fn def_path_hash(&self, _def_id: RawDefId) -> Fingerprint {
panic!("method on dummy stable-hash context was unexpectedly called");
}
fn stable_hash_controls(&self) -> StableHashControls {
panic!("method on dummy stable-hash context was unexpectedly called");
}
fn assert_default_stable_hash_controls(&self, _msg: &str) {
panic!("method on dummy stable-hash context was unexpectedly called");
}
}

// A type used to work around `Span` not being visible in this crate. It is the same size as
// `Span` but has a different alignment.
pub struct RawSpan(pub u64);
Expand Down Expand Up @@ -71,6 +91,18 @@ pub struct RawDefId(pub u32, pub u32);
/// - `stable_hash()` must be independent of the host architecture. The
/// `StableHasher` takes care of endianness and `isize`/`usize` platform
/// differences.
///
/// # Consistency across compiler stages
///
/// Because the stage1 compiler is built against the bootstrap stdlib, but the stage2
/// compiler uses in-tree stdlib, implementations of `StableHash` must not rely on `Hash`
/// impls that were provided or derived by the standard library. Doing so leads to weird
/// bugs and CI failures that can block changes to those stdlib hash impls.
///
/// (Unfortunately, as of late 2026 we have many stable-hash impls that _do_
/// leak details of stdlib hash impls.)
///
/// See <https://github.com/rust-lang/rust/pull/164458> for more context.
pub trait StableHash {
fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher);
}
Expand Down Expand Up @@ -575,8 +607,97 @@ impl<R: Idx, C: Idx> StableHash for bit_set::BitMatrix<R, C> {

impl_stable_traits_for_trivial_type!(::std::ffi::OsStr);
Comment thread
jieyouxu marked this conversation as resolved.
Resolved

impl_stable_traits_for_trivial_type!(::std::path::Path);
impl_stable_traits_for_trivial_type!(::std::path::PathBuf);
impl StableHash for std::path::Path {
fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
use std::path::{Component, Prefix};

/// Tag value to be mixed in with each kind of component/prefix.
#[derive(StableHash)]
enum Tag {
StartOfPath,
PrefixVerbatim,
PrefixVerbatimUNC,
PrefixVerbatimDisk,
PrefixDeviceNS,
PrefixUNC,
PrefixDisk,
RootDir,
CurDir,
ParentDir,
Normal,
EndOfPath,
}
Comment thread
jieyouxu marked this conversation as resolved.
Resolved

// Explicitly tag the start of each path, so that empty paths always hash something.
Tag::StartOfPath.stable_hash(hcx, hasher);

for (i, component) in self.components().enumerate() {
// Mix in the index of each component, to avoid potentially trivial collisions
// between inputs like `("foo/bar", "baz")` and `("foo", "bar/baz")`.
i.stable_hash(hcx, hasher);
match component {
Component::Prefix(prefix) => match prefix.kind() {
Prefix::Verbatim(os_str) => {
Tag::PrefixVerbatim.stable_hash(hcx, hasher);
os_str.stable_hash(hcx, hasher);
}
Prefix::VerbatimUNC(host, share) => {
Tag::PrefixVerbatimUNC.stable_hash(hcx, hasher);
host.stable_hash(hcx, hasher);
share.stable_hash(hcx, hasher);
}
Prefix::VerbatimDisk(letter) => {
Tag::PrefixVerbatimDisk.stable_hash(hcx, hasher);
letter.stable_hash(hcx, hasher);
}
Prefix::DeviceNS(os_str) => {
Tag::PrefixDeviceNS.stable_hash(hcx, hasher);
os_str.stable_hash(hcx, hasher);
}
Prefix::UNC(host, share) => {
Tag::PrefixUNC.stable_hash(hcx, hasher);
host.stable_hash(hcx, hasher);
share.stable_hash(hcx, hasher);
}
Prefix::Disk(letter) => {
Tag::PrefixDisk.stable_hash(hcx, hasher);
letter.stable_hash(hcx, hasher);
}
},
Component::RootDir => Tag::RootDir.stable_hash(hcx, hasher),
Component::CurDir => Tag::CurDir.stable_hash(hcx, hasher),
Component::ParentDir => Tag::ParentDir.stable_hash(hcx, hasher),
Component::Normal(os_str) => {
Tag::Normal.stable_hash(hcx, hasher);
os_str.stable_hash(hcx, hasher);
}
}
}

// Explicitly tag the end of each path, so that empty paths always hash something.
// (Tagging the start and the end and the component-index is potentially overkill.)
Tag::EndOfPath.stable_hash(hcx, hasher);
}
}

impl StableOrd for std::path::Path {
/// FIXME(Zalathar): Unstably sorting paths is pre-existing behavior, but potentially wrong.
const CAN_USE_UNSTABLE_SORT: bool = true;
/// Stdlib path sorting should (hopefully) not change.
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}

impl StableHash for std::path::PathBuf {
fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
self.as_path().stable_hash(hcx, hasher);
}
}

impl StableOrd for std::path::PathBuf {
const CAN_USE_UNSTABLE_SORT: bool = <std::path::Path as StableOrd>::CAN_USE_UNSTABLE_SORT;
/// Stdlib path sorting should (hopefully) not change.
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}

// It is not safe to implement StableHash for HashSet, HashMap or any other collection type
// with unstable but observable iteration order.
Expand Down
34 changes: 34 additions & 0 deletions compiler/rustc_data_structures/src/stable_hash/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::Path;

use super::*;

// The tests below compare the computed hashes to particular expected values
Expand Down Expand Up @@ -29,6 +31,19 @@ fn hash<T: StableHash>(t: &T) -> Hash128 {
h.finish()
}

/// Computes the stable hash of two values in sequence.
///
/// This is similar to computing the stable hash of `(a, b)`, except that it
/// avoids relying on the actual impl of StableHash for tuples.
fn hash_two(a: &impl StableHash, b: &impl StableHash) -> Hash128 {
let hcx = &mut DummyStableHashCtxt;
let mut hasher = StableHasher::new();

a.stable_hash(hcx, &mut hasher);
b.stable_hash(hcx, &mut hasher);
hasher.finish()
}

// Check that bit set hash includes the domain size.
#[test]
fn test_hash_bit_set() {
Expand Down Expand Up @@ -115,3 +130,22 @@ fn test_isize_compression() {
check_hash(0xFF, 0xFFFFFFFFFFFFFFFF);
check_hash(u64::MAX /* -1 */, 1);
}

#[test]
fn path_component_rebracketing() {
let hash_a = hash_two(&Path::new("alfa/bravo"), &Path::new("charlie"));
let hash_b = hash_two(&Path::new("alfa"), &Path::new("bravo/charlie"));

assert_ne!(hash_a, hash_b);
}

#[test]
fn empty_path_rebracketing() {
let nonempty_path = &Path::new("foo");
let empty_path = &Path::new("");

let hash_a = hash_two(nonempty_path, empty_path);
let hash_b = hash_two(empty_path, nonempty_path);

assert_ne!(hash_a, hash_b);
}
120 changes: 71 additions & 49 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3406,7 +3406,7 @@ pub(crate) mod dep_tracking {
use rustc_abi::Align;
use rustc_ast::attr::version::RustcVersion;
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::stable_hash::StableHasher;
use rustc_data_structures::stable_hash::{DummyStableHashCtxt, StableHash, StableHasher};
use rustc_errors::LanguageIdentifier;
use rustc_feature::UnstableFeatures;
use rustc_hashes::Hash64;
Expand Down Expand Up @@ -3469,73 +3469,73 @@ pub(crate) mod dep_tracking {
}

impl_dep_tracking_hash_via_hash!(
// tidy-alphabetical-start
(),
Align,
AnnotateMoves,
AutoDiff,
Offload,
bool,
usize,
NonZero<usize>,
u64,
Hash64,
String,
PathBuf,
lint::Level,
WasiExecModel,
u32,
FramePointer,
RelocModel,
BranchProtection,
CFGuard,
CFProtection,
CodeModel,
TlsModel,
InstrumentCoverage,
CodegenRetagOptions,
CollapseMacroDebuginfo,
CoverageOptions,
CrateType,
DebugInfo,
DebugInfoCompression,
Edition,
FmtDebug,
FramePointer,
FunctionReturn,
Hash64,
InliningThreshold,
InstrumentCoverage,
InstrumentMcount,
InstrumentMcountOpts,
InstrumentXRay,
CrateType,
LanguageIdentifier,
LinkerPluginLto,
LocationDetail,
LtoCli,
MergeFunctions,
MirStripDebugInfo,
NativeLib,
NextSolverConfig,
NonZero<usize>,
Offload,
OnBrokenPipe,
OptLevel,
OutFileName,
OutputType,
PanicStrategy,
PatchableFunctionEntry,
PointerAuthOption,
Polonius,
RelocModel,
RelroLevel,
OptLevel,
LtoCli,
DebugInfo,
DebugInfoCompression,
MirStripDebugInfo,
CollapseMacroDebuginfo,
UnstableFeatures,
NativeLib,
SanitizerSet,
CFGuard,
CFProtection,
TargetTuple,
Edition,
LinkerPluginLto,
RemapPathScopeComponents,
ResolveDocLinks,
RustcVersion,
SanitizerSet,
SourceFileHashAlgorithm,
SplitDebuginfo,
SplitDwarfKind,
StackProtector,
String,
SwitchWithOptPath,
SymbolManglingVersion,
SymbolVisibility,
RemapPathScopeComponents,
SourceFileHashAlgorithm,
OutFileName,
OutputType,
RealFileName,
LocationDetail,
FmtDebug,
BranchProtection,
LanguageIdentifier,
NextSolverConfig,
PatchableFunctionEntry,
Polonius,
InliningThreshold,
FunctionReturn,
Align,
CodegenRetagOptions,
RustcVersion,
PointerAuthOption,
TargetTuple,
TlsModel,
UnstableFeatures,
WasiExecModel,
bool,
lint::Level,
u32,
u64,
usize,
// tidy-alphabetical-end
);

impl<T1, T2> DepTrackingHash for (T1, T2)
Expand Down Expand Up @@ -3624,6 +3624,28 @@ pub(crate) mod dep_tracking {
}
}

impl DepTrackingHash for PathBuf {
fn hash(
&self,
hasher: &mut StableHasher,
_error_format: ErrorOutputType,
_for_crate_hash: bool,
) {
self.stable_hash(&mut DummyStableHashCtxt, hasher);
}
}

impl DepTrackingHash for RealFileName {
fn hash(
&self,
hasher: &mut StableHasher,
_error_format: ErrorOutputType,
_for_crate_hash: bool,
) {
self.stable_hash(&mut DummyStableHashCtxt, hasher);
}
}

// This is a stable hash because BTreeMap is a sorted container
pub(crate) fn stable_hash(
sub_hashes: BTreeMap<&'static str, &dyn DepTrackingHash>,
Expand Down
Loading
Loading