Skip to content

Commit 27af517

Browse files
committed
Auto merge of rust-lang#96082 - michaelwoerister:less_impl_stable_hash_via_hash, r=compiler-errors
incr. comp.: Don't export impl_stable_hash_via_hash!() and warn about using it. Fixes rust-lang#96013.
2 parents a2df8ba + c0be619 commit 27af517

File tree

8 files changed

+38
-37
lines changed

8 files changed

+38
-37
lines changed

compiler/rustc_data_structures/src/stable_hasher.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,14 @@ pub trait ToStableHashKey<HCX> {
207207
fn to_stable_hash_key(&self, hcx: &HCX) -> Self::KeyType;
208208
}
209209

210-
// Implement HashStable by just calling `Hash::hash()`. This works fine for
211-
// self-contained values that don't depend on the hashing context `CTX`.
212-
#[macro_export]
210+
/// Implement HashStable by just calling `Hash::hash()`.
211+
///
212+
/// **WARNING** This is only valid for types that *really* don't need any context for fingerprinting.
213+
/// But it is easy to misuse this macro (see [#96013](https://github.com/rust-lang/rust/issues/96013)
214+
/// for examples). Therefore this macro is not exported and should only be used in the limited cases
215+
/// here in this module.
216+
///
217+
/// Use `#[derive(HashStable_Generic)]` instead.
213218
macro_rules! impl_stable_hash_via_hash {
214219
($t:ty) => {
215220
impl<CTX> $crate::stable_hasher::HashStable<CTX> for $t {
@@ -246,12 +251,14 @@ impl<CTX> HashStable<CTX> for ! {
246251
}
247252

248253
impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
254+
#[inline]
249255
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
250256
self.get().hash_stable(ctx, hasher)
251257
}
252258
}
253259

254260
impl<CTX> HashStable<CTX> for ::std::num::NonZeroUsize {
261+
#[inline]
255262
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
256263
self.get().hash_stable(ctx, hasher)
257264
}
@@ -272,12 +279,14 @@ impl<CTX> HashStable<CTX> for f64 {
272279
}
273280

274281
impl<CTX> HashStable<CTX> for ::std::cmp::Ordering {
282+
#[inline]
275283
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
276284
(*self as i8).hash_stable(ctx, hasher);
277285
}
278286
}
279287

280288
impl<T1: HashStable<CTX>, CTX> HashStable<CTX> for (T1,) {
289+
#[inline]
281290
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
282291
let (ref _0,) = *self;
283292
_0.hash_stable(ctx, hasher);

compiler/rustc_errors/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub use rustc_error_messages::{
3737
};
3838
pub use rustc_lint_defs::{pluralize, Applicability};
3939
use rustc_span::source_map::SourceMap;
40+
use rustc_span::HashStableContext;
4041
use rustc_span::{Loc, Span};
4142

4243
use std::borrow::Cow;
@@ -1494,6 +1495,7 @@ pub fn add_elided_lifetime_in_path_suggestion(
14941495
/// Useful type to use with `Result<>` indicate that an error has already
14951496
/// been reported to the user, so no need to continue checking.
14961497
#[derive(Clone, Copy, Debug, Encodable, Decodable, Hash, PartialEq, Eq, PartialOrd, Ord)]
1498+
#[derive(HashStable_Generic)]
14971499
pub struct ErrorGuaranteed(());
14981500

14991501
impl ErrorGuaranteed {
@@ -1503,5 +1505,3 @@ impl ErrorGuaranteed {
15031505
ErrorGuaranteed(())
15041506
}
15051507
}
1506-
1507-
rustc_data_structures::impl_stable_hash_via_hash!(ErrorGuaranteed);

compiler/rustc_hir/src/hir_id.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ rustc_index::newtype_index! {
7676
/// integers starting at zero, so a mapping that maps all or most nodes within
7777
/// an "item-like" to something else can be implemented by a `Vec` instead of a
7878
/// tree or hash map.
79+
#[derive(HashStable_Generic)]
7980
pub struct ItemLocalId { .. }
8081
}
81-
rustc_data_structures::impl_stable_hash_via_hash!(ItemLocalId);
82+
8283
impl ItemLocalId {
8384
/// Signal local id which should never be used.
8485
pub const INVALID: ItemLocalId = ItemLocalId::MAX;

compiler/rustc_lint_defs/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_ast::node_id::{NodeId, NodeMap};
88
use rustc_ast::{AttrId, Attribute};
99
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
1010
use rustc_error_messages::MultiSpan;
11+
use rustc_hir::HashStableContext;
1112
use rustc_hir::HirId;
1213
use rustc_span::edition::Edition;
1314
use rustc_span::{sym, symbol::Ident, Span, Symbol};
@@ -146,7 +147,7 @@ impl<HCX: rustc_hir::HashStableContext> ToStableHashKey<HCX> for LintExpectation
146147
/// Setting for how to handle a lint.
147148
///
148149
/// See: <https://doc.rust-lang.org/rustc/lints/levels.html>
149-
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
150+
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash, HashStable_Generic)]
150151
pub enum Level {
151152
/// The `allow` level will not issue any message.
152153
Allow,
@@ -174,8 +175,6 @@ pub enum Level {
174175
Forbid,
175176
}
176177

177-
rustc_data_structures::impl_stable_hash_via_hash!(Level);
178-
179178
impl Level {
180179
/// Converts a level to a lower-case string.
181180
pub fn as_str(self) -> &'static str {

compiler/rustc_session/src/config.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
44
pub use crate::options::*;
55

6-
use crate::lint;
76
use crate::search_paths::SearchPath;
87
use crate::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
98
use crate::{early_error, early_warn, Session};
9+
use crate::{lint, HashStableContext};
1010

1111
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
12-
use rustc_data_structures::impl_stable_hash_via_hash;
1312

13+
use rustc_data_structures::stable_hasher::ToStableHashKey;
1414
use rustc_target::abi::{Align, TargetDataLayout};
1515
use rustc_target::spec::{LinkerFlavor, SplitDebuginfo, Target, TargetTriple, TargetWarnings};
1616
use rustc_target::spec::{PanicStrategy, SanitizerSet, TARGETS};
@@ -78,7 +78,7 @@ pub enum CFProtection {
7878
Full,
7979
}
8080

81-
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
81+
#[derive(Clone, Copy, Debug, PartialEq, Hash, HashStable_Generic)]
8282
pub enum OptLevel {
8383
No, // -O0
8484
Less, // -O1
@@ -88,8 +88,6 @@ pub enum OptLevel {
8888
SizeMin, // -Oz
8989
}
9090

91-
impl_stable_hash_via_hash!(OptLevel);
92-
9391
/// This is what the `LtoCli` values get mapped to after resolving defaults and
9492
/// and taking other command line options into account.
9593
///
@@ -230,15 +228,13 @@ impl SwitchWithOptPath {
230228
}
231229
}
232230

233-
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
231+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable_Generic)]
234232
#[derive(Encodable, Decodable)]
235233
pub enum SymbolManglingVersion {
236234
Legacy,
237235
V0,
238236
}
239237

240-
impl_stable_hash_via_hash!(SymbolManglingVersion);
241-
242238
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
243239
pub enum DebugInfo {
244240
None,
@@ -277,7 +273,7 @@ impl FromStr for SplitDwarfKind {
277273
}
278274
}
279275

280-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
276+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, HashStable_Generic)]
281277
#[derive(Encodable, Decodable)]
282278
pub enum OutputType {
283279
Bitcode,
@@ -290,7 +286,13 @@ pub enum OutputType {
290286
DepInfo,
291287
}
292288

293-
impl_stable_hash_via_hash!(OutputType);
289+
impl<HCX: HashStableContext> ToStableHashKey<HCX> for OutputType {
290+
type KeyType = Self;
291+
292+
fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
293+
*self
294+
}
295+
}
294296

295297
impl OutputType {
296298
fn is_compatible_with_codegen_units_and_single_output_file(&self) -> bool {
@@ -396,7 +398,7 @@ pub enum TrimmedDefPaths {
396398
/// *Do not* switch `BTreeMap` out for an unsorted container type! That would break
397399
/// dependency tracking for command-line arguments. Also only hash keys, since tracking
398400
/// should only depend on the output types, not the paths they're written to.
399-
#[derive(Clone, Debug, Hash)]
401+
#[derive(Clone, Debug, Hash, HashStable_Generic)]
400402
pub struct OutputTypes(BTreeMap<OutputType, Option<PathBuf>>);
401403

402404
impl OutputTypes {
@@ -585,7 +587,7 @@ impl Input {
585587
}
586588
}
587589

588-
#[derive(Clone, Hash, Debug)]
590+
#[derive(Clone, Hash, Debug, HashStable_Generic)]
589591
pub struct OutputFilenames {
590592
pub out_directory: PathBuf,
591593
filestem: String,
@@ -594,8 +596,6 @@ pub struct OutputFilenames {
594596
pub outputs: OutputTypes,
595597
}
596598

597-
impl_stable_hash_via_hash!(OutputFilenames);
598-
599599
pub const RLINK_EXT: &str = "rlink";
600600
pub const RUST_CGU_EXT: &str = "rcgu";
601601
pub const DWARF_OBJECT_EXT: &str = "dwo";
@@ -808,15 +808,14 @@ impl DebuggingOptions {
808808
}
809809

810810
// The type of entry function, so users can have their own entry functions
811-
#[derive(Copy, Clone, PartialEq, Hash, Debug)]
811+
#[derive(Copy, Clone, PartialEq, Hash, Debug, HashStable_Generic)]
812812
pub enum EntryFnType {
813813
Main,
814814
Start,
815815
}
816816

817-
impl_stable_hash_via_hash!(EntryFnType);
818-
819817
#[derive(Copy, PartialEq, PartialOrd, Clone, Ord, Eq, Hash, Debug, Encodable, Decodable)]
818+
#[derive(HashStable_Generic)]
820819
pub enum CrateType {
821820
Executable,
822821
Dylib,
@@ -826,8 +825,6 @@ pub enum CrateType {
826825
ProcMacro,
827826
}
828827

829-
impl_stable_hash_via_hash!(CrateType);
830-
831828
impl CrateType {
832829
/// When generated, is this crate type an archive?
833830
pub fn is_archive(&self) -> bool {

compiler/rustc_session/src/search_paths.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct SearchPathFile {
2626
pub file_name_str: String,
2727
}
2828

29-
#[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable)]
29+
#[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable, HashStable_Generic)]
3030
pub enum PathKind {
3131
Native,
3232
Crate,
@@ -36,8 +36,6 @@ pub enum PathKind {
3636
All,
3737
}
3838

39-
rustc_data_structures::impl_stable_hash_via_hash!(PathKind);
40-
4139
impl PathKind {
4240
pub fn matches(&self, kind: PathKind) -> bool {
4341
match (self, kind) {

compiler/rustc_session/src/utils.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl Session {
1818
}
1919

2020
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
21+
#[derive(HashStable_Generic)]
2122
pub enum NativeLibKind {
2223
/// Static library (e.g. `libfoo.a` on Linux or `foo.lib` on Windows/MSVC)
2324
Static {
@@ -57,9 +58,8 @@ impl NativeLibKind {
5758
}
5859
}
5960

60-
rustc_data_structures::impl_stable_hash_via_hash!(NativeLibKind);
61-
6261
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
62+
#[derive(HashStable_Generic)]
6363
pub struct NativeLib {
6464
pub name: String,
6565
pub new_name: Option<String>,
@@ -73,8 +73,6 @@ impl NativeLib {
7373
}
7474
}
7575

76-
rustc_data_structures::impl_stable_hash_via_hash!(NativeLib);
77-
7876
/// A path that has been canonicalized along with its original, non-canonicalized form
7977
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
8078
pub struct CanonicalizedPath {

compiler/rustc_span/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@ impl ExternalSource {
11301130
pub struct OffsetOverflowError;
11311131

11321132
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
1133+
#[derive(HashStable_Generic)]
11331134
pub enum SourceFileHashAlgorithm {
11341135
Md5,
11351136
Sha1,
@@ -1149,8 +1150,6 @@ impl FromStr for SourceFileHashAlgorithm {
11491150
}
11501151
}
11511152

1152-
rustc_data_structures::impl_stable_hash_via_hash!(SourceFileHashAlgorithm);
1153-
11541153
/// The hash of the on-disk source file used for debug info.
11551154
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
11561155
#[derive(HashStable_Generic, Encodable, Decodable)]

0 commit comments

Comments
 (0)