diff --git a/src/cargo/core/compiler/context/compilation_files.rs b/src/cargo/core/compiler/context/compilation_files.rs index aef7e69dc28..04d75fe0ac5 100644 --- a/src/cargo/core/compiler/context/compilation_files.rs +++ b/src/cargo/core/compiler/context/compilation_files.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::env; use std::fmt; -use std::hash::{Hash, Hasher, SipHasher}; +use std::hash::{Hash, Hasher}; use std::path::{Path, PathBuf}; use std::sync::Arc; @@ -11,7 +11,7 @@ use log::info; use super::{BuildContext, CompileKind, Context, FileFlavor, Layout}; use crate::core::compiler::{CompileMode, CompileTarget, CrateType, FileType, Unit}; use crate::core::{Target, TargetKind, Workspace}; -use crate::util::{self, CargoResult}; +use crate::util::{self, CargoResult, StableHasher}; /// The `Metadata` is a hash used to make unique file names for each unit in a /// build. It is also use for symbol mangling. @@ -481,7 +481,7 @@ fn compute_metadata( if !should_use_metadata(bcx, unit) { return None; } - let mut hasher = SipHasher::new(); + let mut hasher = StableHasher::new(); // This is a generic version number that can be changed to make // backwards-incompatible changes to any file structures in the output @@ -556,7 +556,7 @@ fn compute_metadata( Some(Metadata(hasher.finish())) } -fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut SipHasher) { +fn hash_rustc_version(bcx: &BuildContext<'_, '_>, hasher: &mut StableHasher) { let vers = &bcx.rustc().version; if vers.pre.is_empty() || bcx.config.cli_unstable().separate_nightlies { // For stable, keep the artifacts separate. This helps if someone is diff --git a/src/cargo/core/compiler/context/mod.rs b/src/cargo/core/compiler/context/mod.rs index 60f2b8af34d..fbba329c2e1 100644 --- a/src/cargo/core/compiler/context/mod.rs +++ b/src/cargo/core/compiler/context/mod.rs @@ -1,4 +1,3 @@ -#![allow(deprecated)] use std::collections::{BTreeSet, HashMap, HashSet}; use std::path::PathBuf; use std::sync::{Arc, Mutex}; diff --git a/src/cargo/util/hasher.rs b/src/cargo/util/hasher.rs new file mode 100644 index 00000000000..01e15ae2c04 --- /dev/null +++ b/src/cargo/util/hasher.rs @@ -0,0 +1,24 @@ +//! Implementation of a hasher that produces the same values across releases. +//! +//! The hasher should be fast and have a low chance of collisions (but is not +//! sufficient for cryptographic purposes). +#![allow(deprecated)] + +use std::hash::{Hasher, SipHasher}; + +pub struct StableHasher(SipHasher); + +impl StableHasher { + pub fn new() -> StableHasher { + StableHasher(SipHasher::new()) + } +} + +impl Hasher for StableHasher { + fn finish(&self) -> u64 { + self.0.finish() + } + fn write(&mut self, bytes: &[u8]) { + self.0.write(bytes) + } +} diff --git a/src/cargo/util/hex.rs b/src/cargo/util/hex.rs index baeb34781ec..0263e7a2d9a 100644 --- a/src/cargo/util/hex.rs +++ b/src/cargo/util/hex.rs @@ -1,7 +1,6 @@ -#![allow(deprecated)] - +use super::StableHasher; use std::fs::File; -use std::hash::{Hash, Hasher, SipHasher}; +use std::hash::{Hash, Hasher}; use std::io::Read; pub fn to_hex(num: u64) -> String { @@ -18,13 +17,13 @@ pub fn to_hex(num: u64) -> String { } pub fn hash_u64(hashable: H) -> u64 { - let mut hasher = SipHasher::new(); + let mut hasher = StableHasher::new(); hashable.hash(&mut hasher); hasher.finish() } pub fn hash_u64_file(mut file: &File) -> std::io::Result { - let mut hasher = SipHasher::new_with_keys(0, 0); + let mut hasher = StableHasher::new(); let mut buf = [0; 64 * 1024]; loop { let n = file.read(&mut buf)?; diff --git a/src/cargo/util/mod.rs b/src/cargo/util/mod.rs index 45e44ba61cf..7f2ba2697e4 100644 --- a/src/cargo/util/mod.rs +++ b/src/cargo/util/mod.rs @@ -9,6 +9,7 @@ pub use self::errors::{CargoResult, CargoResultExt, CliResult, Test}; pub use self::errors::{CargoTestError, CliError, ProcessError}; pub use self::flock::{FileLock, Filesystem}; pub use self::graph::Graph; +pub use self::hasher::StableHasher; pub use self::hex::{hash_u64, short_hash, to_hex}; pub use self::into_url::IntoUrl; pub use self::into_url_with_base::IntoUrlWithBase; @@ -39,6 +40,7 @@ pub mod diagnostic_server; pub mod errors; mod flock; pub mod graph; +mod hasher; pub mod hex; pub mod important_paths; pub mod into_url; diff --git a/src/cargo/util/rustc.rs b/src/cargo/util/rustc.rs index eb2176b7b9e..2ab08fec6cc 100644 --- a/src/cargo/util/rustc.rs +++ b/src/cargo/util/rustc.rs @@ -1,8 +1,6 @@ -#![allow(deprecated)] // for SipHasher - use std::collections::hash_map::{Entry, HashMap}; use std::env; -use std::hash::{Hash, Hasher, SipHasher}; +use std::hash::{Hash, Hasher}; use std::path::{Path, PathBuf}; use std::sync::Mutex; @@ -11,7 +9,7 @@ use serde::{Deserialize, Serialize}; use crate::core::InternedString; use crate::util::paths; -use crate::util::{self, profile, CargoResult, CargoResultExt, ProcessBuilder}; +use crate::util::{self, profile, CargoResult, CargoResultExt, ProcessBuilder, StableHasher}; /// Information on the `rustc` executable #[derive(Debug)] @@ -222,7 +220,7 @@ impl Drop for Cache { } fn rustc_fingerprint(path: &Path, rustup_rustc: &Path) -> CargoResult { - let mut hasher = SipHasher::new(); + let mut hasher = StableHasher::new(); let path = paths::resolve_executable(path)?; path.hash(&mut hasher); @@ -266,7 +264,7 @@ fn rustc_fingerprint(path: &Path, rustup_rustc: &Path) -> CargoResult { } fn process_fingerprint(cmd: &ProcessBuilder) -> u64 { - let mut hasher = SipHasher::new(); + let mut hasher = StableHasher::new(); cmd.get_args().hash(&mut hasher); let mut env = cmd.get_envs().iter().collect::>(); env.sort_unstable();