Skip to content

Commit

Permalink
Auto merge of #96082 - michaelwoerister:less_impl_stable_hash_via_has…
Browse files Browse the repository at this point in the history
…h, r=compiler-errors

incr. comp.: Don't export impl_stable_hash_via_hash!() and warn about using it.

Fixes #96013.
  • Loading branch information
bors committed Apr 20, 2022
2 parents a2df8ba + c0be619 commit 27af517
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 37 deletions.
15 changes: 12 additions & 3 deletions compiler/rustc_data_structures/src/stable_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,14 @@ pub trait ToStableHashKey<HCX> {
fn to_stable_hash_key(&self, hcx: &HCX) -> Self::KeyType;
}

// Implement HashStable by just calling `Hash::hash()`. This works fine for
// self-contained values that don't depend on the hashing context `CTX`.
#[macro_export]
/// Implement HashStable by just calling `Hash::hash()`.
///
/// **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 {
($t:ty) => {
impl<CTX> $crate::stable_hasher::HashStable<CTX> for $t {
Expand Down Expand Up @@ -246,12 +251,14 @@ impl<CTX> HashStable<CTX> for ! {
}

impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
#[inline]
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
self.get().hash_stable(ctx, hasher)
}
}

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

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

impl<T1: HashStable<CTX>, CTX> HashStable<CTX> for (T1,) {
#[inline]
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
let (ref _0,) = *self;
_0.hash_stable(ctx, hasher);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub use rustc_error_messages::{
};
pub use rustc_lint_defs::{pluralize, Applicability};
use rustc_span::source_map::SourceMap;
use rustc_span::HashStableContext;
use rustc_span::{Loc, Span};

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

impl ErrorGuaranteed {
Expand All @@ -1503,5 +1505,3 @@ impl ErrorGuaranteed {
ErrorGuaranteed(())
}
}

rustc_data_structures::impl_stable_hash_via_hash!(ErrorGuaranteed);
3 changes: 2 additions & 1 deletion compiler/rustc_hir/src/hir_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ rustc_index::newtype_index! {
/// 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
/// tree or hash map.
#[derive(HashStable_Generic)]
pub struct ItemLocalId { .. }
}
rustc_data_structures::impl_stable_hash_via_hash!(ItemLocalId);

impl ItemLocalId {
/// Signal local id which should never be used.
pub const INVALID: ItemLocalId = ItemLocalId::MAX;
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rustc_ast::node_id::{NodeId, NodeMap};
use rustc_ast::{AttrId, Attribute};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use rustc_error_messages::MultiSpan;
use rustc_hir::HashStableContext;
use rustc_hir::HirId;
use rustc_span::edition::Edition;
use rustc_span::{sym, symbol::Ident, Span, Symbol};
Expand Down Expand Up @@ -146,7 +147,7 @@ impl<HCX: rustc_hir::HashStableContext> ToStableHashKey<HCX> for LintExpectation
/// Setting for how to handle a lint.
///
/// 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 {
/// The `allow` level will not issue any message.
Allow,
Expand Down Expand Up @@ -174,8 +175,6 @@ pub enum Level {
Forbid,
}

rustc_data_structures::impl_stable_hash_via_hash!(Level);

impl Level {
/// Converts a level to a lower-case string.
pub fn as_str(self) -> &'static str {
Expand Down
35 changes: 16 additions & 19 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

pub use crate::options::*;

use crate::lint;
use crate::search_paths::SearchPath;
use crate::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
use crate::{early_error, early_warn, Session};
use crate::{lint, HashStableContext};

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::spec::{LinkerFlavor, SplitDebuginfo, Target, TargetTriple, TargetWarnings};
use rustc_target::spec::{PanicStrategy, SanitizerSet, TARGETS};
Expand Down Expand Up @@ -78,7 +78,7 @@ pub enum CFProtection {
Full,
}

#[derive(Clone, Copy, Debug, PartialEq, Hash)]
#[derive(Clone, Copy, Debug, PartialEq, Hash, HashStable_Generic)]
pub enum OptLevel {
No, // -O0
Less, // -O1
Expand All @@ -88,8 +88,6 @@ pub enum OptLevel {
SizeMin, // -Oz
}

impl_stable_hash_via_hash!(OptLevel);

/// This is what the `LtoCli` values get mapped to after resolving defaults and
/// and taking other command line options into account.
///
Expand Down Expand Up @@ -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)]
pub enum SymbolManglingVersion {
Legacy,
V0,
}

impl_stable_hash_via_hash!(SymbolManglingVersion);

#[derive(Clone, Copy, Debug, PartialEq, Hash)]
pub enum DebugInfo {
None,
Expand Down Expand Up @@ -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)]
pub enum OutputType {
Bitcode,
Expand All @@ -290,7 +286,13 @@ pub enum OutputType {
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 {
fn is_compatible_with_codegen_units_and_single_output_file(&self) -> bool {
Expand Down Expand Up @@ -396,7 +398,7 @@ pub enum TrimmedDefPaths {
/// *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
/// 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>>);

impl OutputTypes {
Expand Down Expand Up @@ -585,7 +587,7 @@ impl Input {
}
}

#[derive(Clone, Hash, Debug)]
#[derive(Clone, Hash, Debug, HashStable_Generic)]
pub struct OutputFilenames {
pub out_directory: PathBuf,
filestem: String,
Expand All @@ -594,8 +596,6 @@ pub struct OutputFilenames {
pub outputs: OutputTypes,
}

impl_stable_hash_via_hash!(OutputFilenames);

pub const RLINK_EXT: &str = "rlink";
pub const RUST_CGU_EXT: &str = "rcgu";
pub const DWARF_OBJECT_EXT: &str = "dwo";
Expand Down Expand Up @@ -808,15 +808,14 @@ impl DebuggingOptions {
}

// 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 {
Main,
Start,
}

impl_stable_hash_via_hash!(EntryFnType);

#[derive(Copy, PartialEq, PartialOrd, Clone, Ord, Eq, Hash, Debug, Encodable, Decodable)]
#[derive(HashStable_Generic)]
pub enum CrateType {
Executable,
Dylib,
Expand All @@ -826,8 +825,6 @@ pub enum CrateType {
ProcMacro,
}

impl_stable_hash_via_hash!(CrateType);

impl CrateType {
/// When generated, is this crate type an archive?
pub fn is_archive(&self) -> bool {
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_session/src/search_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct SearchPathFile {
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 {
Native,
Crate,
Expand All @@ -36,8 +36,6 @@ pub enum PathKind {
All,
}

rustc_data_structures::impl_stable_hash_via_hash!(PathKind);

impl PathKind {
pub fn matches(&self, kind: PathKind) -> bool {
match (self, kind) {
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_session/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl Session {
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
#[derive(HashStable_Generic)]
pub enum NativeLibKind {
/// Static library (e.g. `libfoo.a` on Linux or `foo.lib` on Windows/MSVC)
Static {
Expand Down Expand Up @@ -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(HashStable_Generic)]
pub struct NativeLib {
pub name: String,
pub new_name: Option<String>,
Expand All @@ -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
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct CanonicalizedPath {
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,7 @@ impl ExternalSource {
pub struct OffsetOverflowError;

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
#[derive(HashStable_Generic)]
pub enum SourceFileHashAlgorithm {
Md5,
Sha1,
Expand All @@ -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.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(HashStable_Generic, Encodable, Decodable)]
Expand Down

0 comments on commit 27af517

Please sign in to comment.