From e6427699b51e7f52899ce794ccec82a4a1edb647 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 17 Oct 2026 20:18:21 +1100 Subject: [PATCH 1/3] clippy: Avoid hashing FileName This will make it possible to remove the compiler's Hash impl for FileName. --- src/tools/clippy/clippy_lints/src/inherent_impl.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tools/clippy/clippy_lints/src/inherent_impl.rs b/src/tools/clippy/clippy_lints/src/inherent_impl.rs index 5790c3cce2606..db0f046d72811 100644 --- a/src/tools/clippy/clippy_lints/src/inherent_impl.rs +++ b/src/tools/clippy/clippy_lints/src/inherent_impl.rs @@ -6,7 +6,7 @@ use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::{LocalDefId, LocalModId}; use rustc_hir::{Item, ItemKind, Node}; use rustc_lint::{LateContext, LateLintPass, impl_lint_pass}; -use rustc_span::{FileName, Span}; +use rustc_span::{Span, StableSourceFileId}; use std::collections::hash_map::Entry; declare_clippy_lint! { @@ -63,7 +63,7 @@ impl MultipleInherentImpl { #[derive(Hash, Eq, PartialEq, Clone)] enum Criterion { Module(LocalModId), - File(FileName), + File(StableSourceFileId), Crate, } @@ -95,7 +95,7 @@ impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl { InherentImplLintScope::Module => Criterion::Module(cx.tcx.parent_module(hir_id)), InherentImplLintScope::File => { let span = cx.tcx.hir_span(hir_id); - Criterion::File(cx.tcx.sess.source_map().lookup_source_file(span.lo()).name.clone()) + Criterion::File(cx.tcx.sess.source_map().lookup_source_file(span.lo()).stable_id) }, InherentImplLintScope::Crate => Criterion::Crate, }; From 9a82d1a75b8df54a9e174b6a73a5d45d48346862 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 17 Oct 2026 17:58:49 +1100 Subject: [PATCH 2/3] Sort `impl_dep_tracking_hash_via_hash!` --- compiler/rustc_session/src/config.rs | 98 ++++++++++++++-------------- 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 5a96c0524c093..6f27f51583183 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -3469,73 +3469,75 @@ pub(crate) mod dep_tracking { } impl_dep_tracking_hash_via_hash!( + // tidy-alphabetical-start (), + Align, AnnotateMoves, AutoDiff, - Offload, - bool, - usize, - NonZero, - 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, + Offload, OnBrokenPipe, + OptLevel, + OutFileName, + OutputType, PanicStrategy, + PatchableFunctionEntry, + PathBuf, + PointerAuthOption, + Polonius, + RealFileName, + 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 DepTrackingHash for (T1, T2) From d82fe8ab27bf2878a7d22a5759afbdcce7eb73e2 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 17 Oct 2026 18:14:45 +1100 Subject: [PATCH 3/3] Avoid stdlib's `impl Hash for Path` for stable hashing A subtle consequence of the stage0 redesign is that the compiler must take care to avoid relying on any stdlib-provided Hash impl when computing stable hashes, or at least any such impl that is actually liable to change. This was noticed when trying to strengthen the provided Hash impl for Path, which triggered baffling CI failures because the stable hashes produced by the stage1 compiler (built against stage0 stdlib) were inconsistent with the ones that would have been produced by the stage2 compiler (built against in-tree stdlib). This PR aims to unblock changes to `impl Hash for Path`, by making the compiler not rely on it for stable hashing. It does not attempt to solve the broader problem of the compiler relying on stdlib hash details. --- .../rustc_data_structures/src/stable_hash.rs | 125 +++++++++++++++++- .../src/stable_hash/tests.rs | 34 +++++ compiler/rustc_session/src/config.rs | 26 +++- compiler/rustc_span/src/lib.rs | 22 +-- 4 files changed, 191 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_data_structures/src/stable_hash.rs b/compiler/rustc_data_structures/src/stable_hash.rs index 8e7ca5a75d861..91578b9fd4308 100644 --- a/compiler/rustc_data_structures/src/stable_hash.rs +++ b/compiler/rustc_data_structures/src/stable_hash.rs @@ -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; @@ -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; + +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); @@ -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 for more context. pub trait StableHash { fn stable_hash(&self, hcx: &mut Hcx, hasher: &mut StableHasher); } @@ -575,8 +607,97 @@ impl StableHash for bit_set::BitMatrix { impl_stable_traits_for_trivial_type!(::std::ffi::OsStr); -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(&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, + } + + // 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(&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 = ::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. diff --git a/compiler/rustc_data_structures/src/stable_hash/tests.rs b/compiler/rustc_data_structures/src/stable_hash/tests.rs index 163e441d5a47d..fa73b2ce16331 100644 --- a/compiler/rustc_data_structures/src/stable_hash/tests.rs +++ b/compiler/rustc_data_structures/src/stable_hash/tests.rs @@ -1,3 +1,5 @@ +use std::path::Path; + use super::*; // The tests below compare the computed hashes to particular expected values @@ -29,6 +31,19 @@ fn hash(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() { @@ -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); +} diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 6f27f51583183..96d4e4ad82468 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -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; @@ -3510,10 +3510,8 @@ pub(crate) mod dep_tracking { OutputType, PanicStrategy, PatchableFunctionEntry, - PathBuf, PointerAuthOption, Polonius, - RealFileName, RelocModel, RelroLevel, RemapPathScopeComponents, @@ -3626,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>, diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index f0b53eac061ea..2207317a9227b 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -32,7 +32,7 @@ // this crate without this line making `rustc_span` available. extern crate self as rustc_span; -use rustc_data_structures::stable_hash::StableHashCtxt; +use rustc_data_structures::stable_hash::{DummyStableHashCtxt, StableHashCtxt}; use rustc_data_structures::{AtomicRef, outline}; use rustc_macros::{Decodable, Encodable, StableHash}; use rustc_serialize::opaque::mem_encoder::MemEncoder; @@ -317,7 +317,7 @@ pub struct RealFileName { /// The inner workings of `RealFileName`. /// /// It contains the `name`, `working_directory` and `embeddable_name` components. -#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Decodable, Encodable, Hash)] +#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, StableHash, Decodable, Encodable)] struct InnerRealFileName { /// The name. name: PathBuf, @@ -327,18 +327,18 @@ struct InnerRealFileName { embeddable_name: PathBuf, } -impl Hash for RealFileName { - #[inline] - fn hash(&self, state: &mut H) { +impl StableHash for RealFileName { + fn stable_hash(&self, hcx: &mut Hcx, hasher: &mut StableHasher) { + let RealFileName { local, maybe_remapped, scopes } = self; // To prevent #70924 from happening again we should only hash the // remapped path if that exists. This is because remapped paths to // sysroot crates (/rust/$hash or /rust/$version) remain stable even // if the corresponding local path changes. if !self.was_fully_remapped() { - self.local.hash(state); + local.stable_hash(hcx, hasher); } - self.maybe_remapped.hash(state); - self.scopes.bits().hash(state); + maybe_remapped.stable_hash(hcx, hasher); + scopes.bits().stable_hash(hcx, hasher); } } @@ -507,7 +507,7 @@ impl RealFileName { } /// Differentiates between real files and common virtual files. -#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash, Decodable, Encodable)] +#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, StableHash, Decodable, Encodable)] pub enum FileName { Real(RealFileName), /// Strings provided as `--cfg [cfgspec]`. @@ -2207,8 +2207,8 @@ impl StableSourceFileId { stable_crate_id: Option, ) -> Self { let mut hasher = StableHasher::new(); - filename.hash(&mut hasher); - stable_crate_id.hash(&mut hasher); + filename.stable_hash(&mut DummyStableHashCtxt, &mut hasher); + stable_crate_id.stable_hash(&mut DummyStableHashCtxt, &mut hasher); StableSourceFileId(hasher.finish()) } }