Skip to content

Commit c0be619

Browse files
incr. comp.: Don't export impl_stable_hash_via_hash!() and warn about using it.
1 parent e2661ba commit c0be619

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
@@ -39,6 +39,7 @@ pub use rustc_lint_defs::{pluralize, Applicability};
3939
use rustc_serialize::json::Json;
4040
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
4141
use rustc_span::source_map::SourceMap;
42+
use rustc_span::HashStableContext;
4243
use rustc_span::{Loc, Span};
4344

4445
use std::borrow::Cow;
@@ -1531,6 +1532,7 @@ pub fn add_elided_lifetime_in_path_suggestion(
15311532
/// Useful type to use with `Result<>` indicate that an error has already
15321533
/// been reported to the user, so no need to continue checking.
15331534
#[derive(Clone, Copy, Debug, Encodable, Decodable, Hash, PartialEq, Eq, PartialOrd, Ord)]
1535+
#[derive(HashStable_Generic)]
15341536
pub struct ErrorGuaranteed(());
15351537

15361538
impl ErrorGuaranteed {
@@ -1540,5 +1542,3 @@ impl ErrorGuaranteed {
15401542
ErrorGuaranteed(())
15411543
}
15421544
}
1543-
1544-
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_serialize::json::Json;
1314
use rustc_span::edition::Edition;
@@ -147,7 +148,7 @@ impl<HCX: rustc_hir::HashStableContext> ToStableHashKey<HCX> for LintExpectation
147148
/// Setting for how to handle a lint.
148149
///
149150
/// See: <https://doc.rust-lang.org/rustc/lints/levels.html>
150-
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
151+
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash, HashStable_Generic)]
151152
pub enum Level {
152153
/// The `allow` level will not issue any message.
153154
Allow,
@@ -175,8 +176,6 @@ pub enum Level {
175176
Forbid,
176177
}
177178

178-
rustc_data_structures::impl_stable_hash_via_hash!(Level);
179-
180179
impl Level {
181180
/// Converts a level to a lower-case string.
182181
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};
@@ -80,7 +80,7 @@ pub enum CFProtection {
8080
Full,
8181
}
8282

83-
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
83+
#[derive(Clone, Copy, Debug, PartialEq, Hash, HashStable_Generic)]
8484
pub enum OptLevel {
8585
No, // -O0
8686
Less, // -O1
@@ -90,8 +90,6 @@ pub enum OptLevel {
9090
SizeMin, // -Oz
9191
}
9292

93-
impl_stable_hash_via_hash!(OptLevel);
94-
9593
/// This is what the `LtoCli` values get mapped to after resolving defaults and
9694
/// and taking other command line options into account.
9795
///
@@ -232,15 +230,13 @@ impl SwitchWithOptPath {
232230
}
233231
}
234232

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

242-
impl_stable_hash_via_hash!(SymbolManglingVersion);
243-
244240
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
245241
pub enum DebugInfo {
246242
None,
@@ -279,7 +275,7 @@ impl FromStr for SplitDwarfKind {
279275
}
280276
}
281277

282-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
278+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, HashStable_Generic)]
283279
#[derive(Encodable, Decodable)]
284280
pub enum OutputType {
285281
Bitcode,
@@ -292,7 +288,13 @@ pub enum OutputType {
292288
DepInfo,
293289
}
294290

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

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

404406
impl OutputTypes {
@@ -630,7 +632,7 @@ impl Input {
630632
}
631633
}
632634

633-
#[derive(Clone, Hash, Debug)]
635+
#[derive(Clone, Hash, Debug, HashStable_Generic)]
634636
pub struct OutputFilenames {
635637
pub out_directory: PathBuf,
636638
filestem: String,
@@ -639,8 +641,6 @@ pub struct OutputFilenames {
639641
pub outputs: OutputTypes,
640642
}
641643

642-
impl_stable_hash_via_hash!(OutputFilenames);
643-
644644
pub const RLINK_EXT: &str = "rlink";
645645
pub const RUST_CGU_EXT: &str = "rcgu";
646646
pub const DWARF_OBJECT_EXT: &str = "dwo";
@@ -854,15 +854,14 @@ impl DebuggingOptions {
854854
}
855855

856856
// The type of entry function, so users can have their own entry functions
857-
#[derive(Copy, Clone, PartialEq, Hash, Debug)]
857+
#[derive(Copy, Clone, PartialEq, Hash, Debug, HashStable_Generic)]
858858
pub enum EntryFnType {
859859
Main,
860860
Start,
861861
}
862862

863-
impl_stable_hash_via_hash!(EntryFnType);
864-
865863
#[derive(Copy, PartialEq, PartialOrd, Clone, Ord, Eq, Hash, Debug, Encodable, Decodable)]
864+
#[derive(HashStable_Generic)]
866865
pub enum CrateType {
867866
Executable,
868867
Dylib,
@@ -872,8 +871,6 @@ pub enum CrateType {
872871
ProcMacro,
873872
}
874873

875-
impl_stable_hash_via_hash!(CrateType);
876-
877874
impl CrateType {
878875
/// When generated, is this crate type an archive?
879876
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)