Auto merge of #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 https://github.com/rust-lang/rust/issues/96013.
This commit is contained in:
bors 2022-04-20 03:51:09 +00:00
commit 27af517549
8 changed files with 38 additions and 37 deletions

View file

@ -207,9 +207,14 @@ pub trait ToStableHashKey<HCX> {
fn to_stable_hash_key(&self, hcx: &HCX) -> Self::KeyType; fn to_stable_hash_key(&self, hcx: &HCX) -> Self::KeyType;
} }
// Implement HashStable by just calling `Hash::hash()`. This works fine for /// Implement HashStable by just calling `Hash::hash()`.
// self-contained values that don't depend on the hashing context `CTX`. ///
#[macro_export] /// **WARNING** This is only valid for types that *really* don't need any context for fingerprinting.
/// But it is easy to misuse this macro (see [#96013](https://github.com/rust-lang/rust/issues/96013)
/// for examples). Therefore this macro is not exported and should only be used in the limited cases
/// here in this module.
///
/// Use `#[derive(HashStable_Generic)]` instead.
macro_rules! impl_stable_hash_via_hash { macro_rules! impl_stable_hash_via_hash {
($t:ty) => { ($t:ty) => {
impl<CTX> $crate::stable_hasher::HashStable<CTX> for $t { impl<CTX> $crate::stable_hasher::HashStable<CTX> for $t {
@ -246,12 +251,14 @@ impl<CTX> HashStable<CTX> for ! {
} }
impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 { impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
#[inline]
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
self.get().hash_stable(ctx, hasher) self.get().hash_stable(ctx, hasher)
} }
} }
impl<CTX> HashStable<CTX> for ::std::num::NonZeroUsize { impl<CTX> HashStable<CTX> for ::std::num::NonZeroUsize {
#[inline]
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
self.get().hash_stable(ctx, hasher) self.get().hash_stable(ctx, hasher)
} }
@ -272,12 +279,14 @@ impl<CTX> HashStable<CTX> for f64 {
} }
impl<CTX> HashStable<CTX> for ::std::cmp::Ordering { impl<CTX> HashStable<CTX> for ::std::cmp::Ordering {
#[inline]
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
(*self as i8).hash_stable(ctx, hasher); (*self as i8).hash_stable(ctx, hasher);
} }
} }
impl<T1: HashStable<CTX>, CTX> HashStable<CTX> for (T1,) { impl<T1: HashStable<CTX>, CTX> HashStable<CTX> for (T1,) {
#[inline]
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
let (ref _0,) = *self; let (ref _0,) = *self;
_0.hash_stable(ctx, hasher); _0.hash_stable(ctx, hasher);

View file

@ -37,6 +37,7 @@ pub use rustc_error_messages::{
}; };
pub use rustc_lint_defs::{pluralize, Applicability}; pub use rustc_lint_defs::{pluralize, Applicability};
use rustc_span::source_map::SourceMap; use rustc_span::source_map::SourceMap;
use rustc_span::HashStableContext;
use rustc_span::{Loc, Span}; use rustc_span::{Loc, Span};
use std::borrow::Cow; use std::borrow::Cow;
@ -1494,6 +1495,7 @@ pub fn add_elided_lifetime_in_path_suggestion(
/// Useful type to use with `Result<>` indicate that an error has already /// Useful type to use with `Result<>` indicate that an error has already
/// been reported to the user, so no need to continue checking. /// been reported to the user, so no need to continue checking.
#[derive(Clone, Copy, Debug, Encodable, Decodable, Hash, PartialEq, Eq, PartialOrd, Ord)] #[derive(Clone, Copy, Debug, Encodable, Decodable, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(HashStable_Generic)]
pub struct ErrorGuaranteed(()); pub struct ErrorGuaranteed(());
impl ErrorGuaranteed { impl ErrorGuaranteed {
@ -1503,5 +1505,3 @@ impl ErrorGuaranteed {
ErrorGuaranteed(()) ErrorGuaranteed(())
} }
} }
rustc_data_structures::impl_stable_hash_via_hash!(ErrorGuaranteed);

View file

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

View file

@ -8,6 +8,7 @@ use rustc_ast::node_id::{NodeId, NodeMap};
use rustc_ast::{AttrId, Attribute}; use rustc_ast::{AttrId, Attribute};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use rustc_error_messages::MultiSpan; use rustc_error_messages::MultiSpan;
use rustc_hir::HashStableContext;
use rustc_hir::HirId; use rustc_hir::HirId;
use rustc_span::edition::Edition; use rustc_span::edition::Edition;
use rustc_span::{sym, symbol::Ident, Span, Symbol}; use rustc_span::{sym, symbol::Ident, Span, Symbol};
@ -146,7 +147,7 @@ impl<HCX: rustc_hir::HashStableContext> ToStableHashKey<HCX> for LintExpectation
/// Setting for how to handle a lint. /// Setting for how to handle a lint.
/// ///
/// See: <https://doc.rust-lang.org/rustc/lints/levels.html> /// See: <https://doc.rust-lang.org/rustc/lints/levels.html>
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)] #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash, HashStable_Generic)]
pub enum Level { pub enum Level {
/// The `allow` level will not issue any message. /// The `allow` level will not issue any message.
Allow, Allow,
@ -174,8 +175,6 @@ pub enum Level {
Forbid, Forbid,
} }
rustc_data_structures::impl_stable_hash_via_hash!(Level);
impl Level { impl Level {
/// Converts a level to a lower-case string. /// Converts a level to a lower-case string.
pub fn as_str(self) -> &'static str { pub fn as_str(self) -> &'static str {

View file

@ -3,14 +3,14 @@
pub use crate::options::*; pub use crate::options::*;
use crate::lint;
use crate::search_paths::SearchPath; use crate::search_paths::SearchPath;
use crate::utils::{CanonicalizedPath, NativeLib, NativeLibKind}; use crate::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
use crate::{early_error, early_warn, Session}; use crate::{early_error, early_warn, Session};
use crate::{lint, HashStableContext};
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::impl_stable_hash_via_hash;
use rustc_data_structures::stable_hasher::ToStableHashKey;
use rustc_target::abi::{Align, TargetDataLayout}; use rustc_target::abi::{Align, TargetDataLayout};
use rustc_target::spec::{LinkerFlavor, SplitDebuginfo, Target, TargetTriple, TargetWarnings}; use rustc_target::spec::{LinkerFlavor, SplitDebuginfo, Target, TargetTriple, TargetWarnings};
use rustc_target::spec::{PanicStrategy, SanitizerSet, TARGETS}; use rustc_target::spec::{PanicStrategy, SanitizerSet, TARGETS};
@ -78,7 +78,7 @@ pub enum CFProtection {
Full, Full,
} }
#[derive(Clone, Copy, Debug, PartialEq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Hash, HashStable_Generic)]
pub enum OptLevel { pub enum OptLevel {
No, // -O0 No, // -O0
Less, // -O1 Less, // -O1
@ -88,8 +88,6 @@ pub enum OptLevel {
SizeMin, // -Oz SizeMin, // -Oz
} }
impl_stable_hash_via_hash!(OptLevel);
/// This is what the `LtoCli` values get mapped to after resolving defaults and /// This is what the `LtoCli` values get mapped to after resolving defaults and
/// and taking other command line options into account. /// and taking other command line options into account.
/// ///
@ -230,15 +228,13 @@ impl SwitchWithOptPath {
} }
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable_Generic)]
#[derive(Encodable, Decodable)] #[derive(Encodable, Decodable)]
pub enum SymbolManglingVersion { pub enum SymbolManglingVersion {
Legacy, Legacy,
V0, V0,
} }
impl_stable_hash_via_hash!(SymbolManglingVersion);
#[derive(Clone, Copy, Debug, PartialEq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Hash)]
pub enum DebugInfo { pub enum DebugInfo {
None, None,
@ -277,7 +273,7 @@ impl FromStr for SplitDwarfKind {
} }
} }
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)] #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, HashStable_Generic)]
#[derive(Encodable, Decodable)] #[derive(Encodable, Decodable)]
pub enum OutputType { pub enum OutputType {
Bitcode, Bitcode,
@ -290,7 +286,13 @@ pub enum OutputType {
DepInfo, DepInfo,
} }
impl_stable_hash_via_hash!(OutputType); impl<HCX: HashStableContext> ToStableHashKey<HCX> for OutputType {
type KeyType = Self;
fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
*self
}
}
impl OutputType { impl OutputType {
fn is_compatible_with_codegen_units_and_single_output_file(&self) -> bool { fn is_compatible_with_codegen_units_and_single_output_file(&self) -> bool {
@ -396,7 +398,7 @@ pub enum TrimmedDefPaths {
/// *Do not* switch `BTreeMap` out for an unsorted container type! That would break /// *Do not* switch `BTreeMap` out for an unsorted container type! That would break
/// dependency tracking for command-line arguments. Also only hash keys, since tracking /// dependency tracking for command-line arguments. Also only hash keys, since tracking
/// should only depend on the output types, not the paths they're written to. /// should only depend on the output types, not the paths they're written to.
#[derive(Clone, Debug, Hash)] #[derive(Clone, Debug, Hash, HashStable_Generic)]
pub struct OutputTypes(BTreeMap<OutputType, Option<PathBuf>>); pub struct OutputTypes(BTreeMap<OutputType, Option<PathBuf>>);
impl OutputTypes { impl OutputTypes {
@ -585,7 +587,7 @@ impl Input {
} }
} }
#[derive(Clone, Hash, Debug)] #[derive(Clone, Hash, Debug, HashStable_Generic)]
pub struct OutputFilenames { pub struct OutputFilenames {
pub out_directory: PathBuf, pub out_directory: PathBuf,
filestem: String, filestem: String,
@ -594,8 +596,6 @@ pub struct OutputFilenames {
pub outputs: OutputTypes, pub outputs: OutputTypes,
} }
impl_stable_hash_via_hash!(OutputFilenames);
pub const RLINK_EXT: &str = "rlink"; pub const RLINK_EXT: &str = "rlink";
pub const RUST_CGU_EXT: &str = "rcgu"; pub const RUST_CGU_EXT: &str = "rcgu";
pub const DWARF_OBJECT_EXT: &str = "dwo"; pub const DWARF_OBJECT_EXT: &str = "dwo";
@ -808,15 +808,14 @@ impl DebuggingOptions {
} }
// The type of entry function, so users can have their own entry functions // The type of entry function, so users can have their own entry functions
#[derive(Copy, Clone, PartialEq, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Hash, Debug, HashStable_Generic)]
pub enum EntryFnType { pub enum EntryFnType {
Main, Main,
Start, Start,
} }
impl_stable_hash_via_hash!(EntryFnType);
#[derive(Copy, PartialEq, PartialOrd, Clone, Ord, Eq, Hash, Debug, Encodable, Decodable)] #[derive(Copy, PartialEq, PartialOrd, Clone, Ord, Eq, Hash, Debug, Encodable, Decodable)]
#[derive(HashStable_Generic)]
pub enum CrateType { pub enum CrateType {
Executable, Executable,
Dylib, Dylib,
@ -826,8 +825,6 @@ pub enum CrateType {
ProcMacro, ProcMacro,
} }
impl_stable_hash_via_hash!(CrateType);
impl CrateType { impl CrateType {
/// When generated, is this crate type an archive? /// When generated, is this crate type an archive?
pub fn is_archive(&self) -> bool { pub fn is_archive(&self) -> bool {

View file

@ -26,7 +26,7 @@ pub struct SearchPathFile {
pub file_name_str: String, pub file_name_str: String,
} }
#[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable)] #[derive(PartialEq, Clone, Copy, Debug, Hash, Eq, Encodable, Decodable, HashStable_Generic)]
pub enum PathKind { pub enum PathKind {
Native, Native,
Crate, Crate,
@ -36,8 +36,6 @@ pub enum PathKind {
All, All,
} }
rustc_data_structures::impl_stable_hash_via_hash!(PathKind);
impl PathKind { impl PathKind {
pub fn matches(&self, kind: PathKind) -> bool { pub fn matches(&self, kind: PathKind) -> bool {
match (self, kind) { match (self, kind) {

View file

@ -18,6 +18,7 @@ impl Session {
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
#[derive(HashStable_Generic)]
pub enum NativeLibKind { pub enum NativeLibKind {
/// Static library (e.g. `libfoo.a` on Linux or `foo.lib` on Windows/MSVC) /// Static library (e.g. `libfoo.a` on Linux or `foo.lib` on Windows/MSVC)
Static { Static {
@ -57,9 +58,8 @@ impl NativeLibKind {
} }
} }
rustc_data_structures::impl_stable_hash_via_hash!(NativeLibKind);
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
#[derive(HashStable_Generic)]
pub struct NativeLib { pub struct NativeLib {
pub name: String, pub name: String,
pub new_name: Option<String>, pub new_name: Option<String>,
@ -73,8 +73,6 @@ impl NativeLib {
} }
} }
rustc_data_structures::impl_stable_hash_via_hash!(NativeLib);
/// A path that has been canonicalized along with its original, non-canonicalized form /// A path that has been canonicalized along with its original, non-canonicalized form
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct CanonicalizedPath { pub struct CanonicalizedPath {

View file

@ -1130,6 +1130,7 @@ impl ExternalSource {
pub struct OffsetOverflowError; pub struct OffsetOverflowError;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
#[derive(HashStable_Generic)]
pub enum SourceFileHashAlgorithm { pub enum SourceFileHashAlgorithm {
Md5, Md5,
Sha1, Sha1,
@ -1149,8 +1150,6 @@ impl FromStr for SourceFileHashAlgorithm {
} }
} }
rustc_data_structures::impl_stable_hash_via_hash!(SourceFileHashAlgorithm);
/// The hash of the on-disk source file used for debug info. /// The hash of the on-disk source file used for debug info.
#[derive(Copy, Clone, PartialEq, Eq, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(HashStable_Generic, Encodable, Decodable)] #[derive(HashStable_Generic, Encodable, Decodable)]