Skip to content

Commit

Permalink
In --emit KIND=PATH options, only hash KIND
Browse files Browse the repository at this point in the history
The PATH has no material effect on the emitted artifact, and setting
the patch via `-o` or `--out-dir` does not affect the hash.

Closes #86044
  • Loading branch information
jsgf committed Jun 22, 2021
1 parent cef3ab7 commit a26d99f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_interface/src/tests.rs
Expand Up @@ -152,9 +152,9 @@ fn test_output_types_tracking_hash_different_paths() {
v2.output_types = OutputTypes::new(&[(OutputType::Exe, Some(PathBuf::from("/some/thing")))]);
v3.output_types = OutputTypes::new(&[(OutputType::Exe, None)]);

assert_different_hash(&v1, &v2);
assert_different_hash(&v1, &v3);
assert_different_hash(&v2, &v3);
assert_same_hash(&v1, &v2);
assert_same_hash(&v1, &v3);
assert_same_hash(&v2, &v3);
}

#[test]
Expand Down
14 changes: 12 additions & 2 deletions compiler/rustc_session/src/config.rs
Expand Up @@ -31,6 +31,7 @@ use std::collections::btree_map::{
};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::iter::{self, FromIterator};
use std::path::{Path, PathBuf};
use std::str::{self, FromStr};
Expand Down Expand Up @@ -325,10 +326,19 @@ impl Default for TrimmedDefPaths {

/// Use tree-based collections to cheaply get a deterministic `Hash` implementation.
/// *Do not* switch `BTreeMap` out for an unsorted container type! That would break
/// dependency tracking for command-line arguments.
#[derive(Clone, Hash, Debug)]
/// 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)]
pub struct OutputTypes(BTreeMap<OutputType, Option<PathBuf>>);

impl Hash for OutputTypes {
fn hash<H: Hasher>(&self, hasher: &mut H) {
for k in self.keys() {
k.hash(hasher);
}
}
}

impl_stable_hash_via_hash!(OutputTypes);

impl OutputTypes {
Expand Down
26 changes: 26 additions & 0 deletions src/test/run-make/emit-path-unhashed/Makefile
@@ -0,0 +1,26 @@
-include ../../run-make-fulldeps/tools.mk

OUT=$(TMPDIR)/emit

# --emit KIND=PATH should not affect crate hash vs --emit KIND
all: $(OUT)/a/libfoo.rlib $(OUT)/b/libfoo.rlib $(TMPDIR)/libfoo.rlib
$(RUSTC) -Zls $(TMPDIR)/libfoo.rlib > $(TMPDIR)/base.txt
$(RUSTC) -Zls $(OUT)/a/libfoo.rlib > $(TMPDIR)/a.txt
$(RUSTC) -Zls $(OUT)/b/libfoo.rlib > $(TMPDIR)/b.txt

diff $(TMPDIR)/base.txt $(TMPDIR)/a.txt
diff $(TMPDIR)/base.txt $(TMPDIR)/b.txt

# Default output name
$(TMPDIR)/libfoo.rlib: foo.rs
$(RUSTC) --emit link foo.rs

# Output named with -o
$(OUT)/a/libfoo.rlib: foo.rs
mkdir -p $(OUT)/a
$(RUSTC) --emit link -o $@ foo.rs

# Output named with KIND=PATH
$(OUT)/b/libfoo.rlib: foo.rs
mkdir -p $(OUT)/b
$(RUSTC) --emit link=$@ foo.rs
1 change: 1 addition & 0 deletions src/test/run-make/emit-path-unhashed/foo.rs
@@ -0,0 +1 @@
#![crate_type = "rlib"]

0 comments on commit a26d99f

Please sign in to comment.