Skip to content

Commit 99f652f

Browse files
Aaron1011jsgf
authored andcommitted
Only hash OutputTypes keys in non-crate-hash mode
This effectively turns OutputTypes into a hybrid where keys (OutputType) are TRACKED and the values (optional paths) are TRACKED_NO_CRATE_HASH.
1 parent f1f7f2f commit 99f652f

File tree

3 files changed

+77
-37
lines changed

3 files changed

+77
-37
lines changed

compiler/rustc_interface/src/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ fn test_output_types_tracking_hash_different_paths() {
160160
v2.output_types = OutputTypes::new(&[(OutputType::Exe, Some(PathBuf::from("/some/thing")))]);
161161
v3.output_types = OutputTypes::new(&[(OutputType::Exe, None)]);
162162

163-
assert_same_hash(&v1, &v2);
164-
assert_same_hash(&v1, &v3);
165-
assert_same_hash(&v2, &v3);
163+
assert_non_crate_hash_different(&v1, &v2);
164+
assert_non_crate_hash_different(&v1, &v3);
165+
assert_non_crate_hash_different(&v2, &v3);
166166
}
167167

168168
#[test]

compiler/rustc_session/src/config.rs

+62-29
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::collections::btree_map::{
3131
};
3232
use std::collections::{BTreeMap, BTreeSet};
3333
use std::fmt;
34-
use std::hash::{Hash, Hasher};
34+
use std::hash::Hash;
3535
use std::iter::{self, FromIterator};
3636
use std::path::{Path, PathBuf};
3737
use std::str::{self, FromStr};
@@ -328,19 +328,9 @@ impl Default for TrimmedDefPaths {
328328
/// *Do not* switch `BTreeMap` out for an unsorted container type! That would break
329329
/// dependency tracking for command-line arguments. Also only hash keys, since tracking
330330
/// should only depend on the output types, not the paths they're written to.
331-
#[derive(Clone, Debug)]
331+
#[derive(Clone, Debug, Hash)]
332332
pub struct OutputTypes(BTreeMap<OutputType, Option<PathBuf>>);
333333

334-
impl Hash for OutputTypes {
335-
fn hash<H: Hasher>(&self, hasher: &mut H) {
336-
for k in self.keys() {
337-
k.hash(hasher);
338-
}
339-
}
340-
}
341-
342-
impl_stable_hash_via_hash!(OutputTypes);
343-
344334
impl OutputTypes {
345335
pub fn new(entries: &[(OutputType, Option<PathBuf>)]) -> OutputTypes {
346336
OutputTypes(BTreeMap::from_iter(entries.iter().map(|&(k, ref v)| (k, v.clone()))))
@@ -2436,8 +2426,8 @@ crate mod dep_tracking {
24362426
use super::LdImpl;
24372427
use super::{
24382428
CFGuard, CrateType, DebugInfo, ErrorOutputType, InstrumentCoverage, LinkerPluginLto,
2439-
LtoCli, OptLevel, OutputTypes, Passes, SourceFileHashAlgorithm, SwitchWithOptPath,
2440-
SymbolManglingVersion, TrimmedDefPaths,
2429+
LtoCli, OptLevel, OutputType, OutputTypes, Passes, SourceFileHashAlgorithm,
2430+
SwitchWithOptPath, SymbolManglingVersion, TrimmedDefPaths,
24412431
};
24422432
use crate::lint;
24432433
use crate::options::WasiExecModel;
@@ -2453,25 +2443,35 @@ crate mod dep_tracking {
24532443
use std::path::PathBuf;
24542444

24552445
pub trait DepTrackingHash {
2456-
fn hash(&self, hasher: &mut DefaultHasher, error_format: ErrorOutputType);
2446+
fn hash(
2447+
&self,
2448+
hasher: &mut DefaultHasher,
2449+
error_format: ErrorOutputType,
2450+
for_crate_hash: bool,
2451+
);
24572452
}
24582453

24592454
macro_rules! impl_dep_tracking_hash_via_hash {
24602455
($($t:ty),+ $(,)?) => {$(
24612456
impl DepTrackingHash for $t {
2462-
fn hash(&self, hasher: &mut DefaultHasher, _: ErrorOutputType) {
2457+
fn hash(&self, hasher: &mut DefaultHasher, _: ErrorOutputType, _for_crate_hash: bool) {
24632458
Hash::hash(self, hasher);
24642459
}
24652460
}
24662461
)+};
24672462
}
24682463

24692464
impl<T: DepTrackingHash> DepTrackingHash for Option<T> {
2470-
fn hash(&self, hasher: &mut DefaultHasher, error_format: ErrorOutputType) {
2465+
fn hash(
2466+
&self,
2467+
hasher: &mut DefaultHasher,
2468+
error_format: ErrorOutputType,
2469+
for_crate_hash: bool,
2470+
) {
24712471
match self {
24722472
Some(x) => {
24732473
Hash::hash(&1, hasher);
2474-
DepTrackingHash::hash(x, hasher, error_format);
2474+
DepTrackingHash::hash(x, hasher, error_format, for_crate_hash);
24752475
}
24762476
None => Hash::hash(&0, hasher),
24772477
}
@@ -2501,7 +2501,6 @@ crate mod dep_tracking {
25012501
LtoCli,
25022502
DebugInfo,
25032503
UnstableFeatures,
2504-
OutputTypes,
25052504
NativeLib,
25062505
NativeLibKind,
25072506
SanitizerSet,
@@ -2515,18 +2514,24 @@ crate mod dep_tracking {
25152514
SourceFileHashAlgorithm,
25162515
TrimmedDefPaths,
25172516
Option<LdImpl>,
2517+
OutputType,
25182518
);
25192519

25202520
impl<T1, T2> DepTrackingHash for (T1, T2)
25212521
where
25222522
T1: DepTrackingHash,
25232523
T2: DepTrackingHash,
25242524
{
2525-
fn hash(&self, hasher: &mut DefaultHasher, error_format: ErrorOutputType) {
2525+
fn hash(
2526+
&self,
2527+
hasher: &mut DefaultHasher,
2528+
error_format: ErrorOutputType,
2529+
for_crate_hash: bool,
2530+
) {
25262531
Hash::hash(&0, hasher);
2527-
DepTrackingHash::hash(&self.0, hasher, error_format);
2532+
DepTrackingHash::hash(&self.0, hasher, error_format, for_crate_hash);
25282533
Hash::hash(&1, hasher);
2529-
DepTrackingHash::hash(&self.1, hasher, error_format);
2534+
DepTrackingHash::hash(&self.1, hasher, error_format, for_crate_hash);
25302535
}
25312536
}
25322537

@@ -2536,22 +2541,49 @@ crate mod dep_tracking {
25362541
T2: DepTrackingHash,
25372542
T3: DepTrackingHash,
25382543
{
2539-
fn hash(&self, hasher: &mut DefaultHasher, error_format: ErrorOutputType) {
2544+
fn hash(
2545+
&self,
2546+
hasher: &mut DefaultHasher,
2547+
error_format: ErrorOutputType,
2548+
for_crate_hash: bool,
2549+
) {
25402550
Hash::hash(&0, hasher);
2541-
DepTrackingHash::hash(&self.0, hasher, error_format);
2551+
DepTrackingHash::hash(&self.0, hasher, error_format, for_crate_hash);
25422552
Hash::hash(&1, hasher);
2543-
DepTrackingHash::hash(&self.1, hasher, error_format);
2553+
DepTrackingHash::hash(&self.1, hasher, error_format, for_crate_hash);
25442554
Hash::hash(&2, hasher);
2545-
DepTrackingHash::hash(&self.2, hasher, error_format);
2555+
DepTrackingHash::hash(&self.2, hasher, error_format, for_crate_hash);
25462556
}
25472557
}
25482558

25492559
impl<T: DepTrackingHash> DepTrackingHash for Vec<T> {
2550-
fn hash(&self, hasher: &mut DefaultHasher, error_format: ErrorOutputType) {
2560+
fn hash(
2561+
&self,
2562+
hasher: &mut DefaultHasher,
2563+
error_format: ErrorOutputType,
2564+
for_crate_hash: bool,
2565+
) {
25512566
Hash::hash(&self.len(), hasher);
25522567
for (index, elem) in self.iter().enumerate() {
25532568
Hash::hash(&index, hasher);
2554-
DepTrackingHash::hash(elem, hasher, error_format);
2569+
DepTrackingHash::hash(elem, hasher, error_format, for_crate_hash);
2570+
}
2571+
}
2572+
}
2573+
2574+
impl DepTrackingHash for OutputTypes {
2575+
fn hash(
2576+
&self,
2577+
hasher: &mut DefaultHasher,
2578+
error_format: ErrorOutputType,
2579+
for_crate_hash: bool,
2580+
) {
2581+
Hash::hash(&self.0.len(), hasher);
2582+
for (key, val) in &self.0 {
2583+
DepTrackingHash::hash(key, hasher, error_format, for_crate_hash);
2584+
if !for_crate_hash {
2585+
DepTrackingHash::hash(val, hasher, error_format, for_crate_hash);
2586+
}
25552587
}
25562588
}
25572589
}
@@ -2561,13 +2593,14 @@ crate mod dep_tracking {
25612593
sub_hashes: BTreeMap<&'static str, &dyn DepTrackingHash>,
25622594
hasher: &mut DefaultHasher,
25632595
error_format: ErrorOutputType,
2596+
for_crate_hash: bool,
25642597
) {
25652598
for (key, sub_hash) in sub_hashes {
25662599
// Using Hash::hash() instead of DepTrackingHash::hash() is fine for
25672600
// the keys, as they are just plain strings
25682601
Hash::hash(&key.len(), hasher);
25692602
Hash::hash(key, hasher);
2570-
sub_hash.hash(hasher, error_format);
2603+
sub_hash.hash(hasher, error_format, for_crate_hash);
25712604
}
25722605
}
25732606
}

compiler/rustc_session/src/options.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ macro_rules! hash_substruct {
4848
($opt_name:ident, $opt_expr:expr, $error_format:expr, $for_crate_hash:expr, $hasher:expr, [TRACKED_NO_CRATE_HASH]) => {{}};
4949
($opt_name:ident, $opt_expr:expr, $error_format:expr, $for_crate_hash:expr, $hasher:expr, [SUBSTRUCT]) => {
5050
use crate::config::dep_tracking::DepTrackingHash;
51-
$opt_expr.dep_tracking_hash($for_crate_hash, $error_format).hash($hasher, $error_format);
51+
$opt_expr.dep_tracking_hash($for_crate_hash, $error_format).hash(
52+
$hasher,
53+
$error_format,
54+
$for_crate_hash,
55+
);
5256
};
5357
}
5458

@@ -79,7 +83,8 @@ macro_rules! top_level_options {
7983
let mut hasher = DefaultHasher::new();
8084
dep_tracking::stable_hash(sub_hashes,
8185
&mut hasher,
82-
self.error_format);
86+
self.error_format,
87+
for_crate_hash);
8388
$({
8489
hash_substruct!($opt,
8590
&self.$opt,
@@ -236,19 +241,21 @@ macro_rules! options {
236241
build_options(matches, $stat, $prefix, $outputname, error_format)
237242
}
238243

239-
fn dep_tracking_hash(&self, _for_crate_hash: bool, error_format: ErrorOutputType) -> u64 {
244+
fn dep_tracking_hash(&self, for_crate_hash: bool, error_format: ErrorOutputType) -> u64 {
240245
let mut sub_hashes = BTreeMap::new();
241246
$({
242247
hash_opt!($opt,
243248
&self.$opt,
244249
&mut sub_hashes,
245-
_for_crate_hash,
250+
for_crate_hash,
246251
[$dep_tracking_marker]);
247252
})*
248253
let mut hasher = DefaultHasher::new();
249254
dep_tracking::stable_hash(sub_hashes,
250255
&mut hasher,
251-
error_format);
256+
error_format,
257+
for_crate_hash
258+
);
252259
hasher.finish()
253260
}
254261
}

0 commit comments

Comments
 (0)