diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index fe5f70f4fd9..690afe1d458 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -126,14 +126,17 @@ fn borrow(&self) -> &Fingerprint { } } -/// A [`StableCrateId`] is a 64 bit hash of the crate name combined with all -/// `-Cmetadata` arguments. It is to [`CrateNum`] what [`DefPathHash`] is to +/// A [`StableCrateId`] is a 64-bit hash of a crate name, together with all +/// `-Cmetadata` arguments, and some other data. It is to [`CrateNum`] what [`DefPathHash`] is to /// [`DefId`]. It is stable across compilation sessions. /// -/// Since the ID is a hash value there is a (very small) chance that two crates +/// Since the ID is a hash value, there is a small chance that two crates /// end up with the same [`StableCrateId`]. The compiler will check for such /// collisions when loading crates and abort compilation in order to avoid /// further trouble. +/// +/// See the discussion in [`DefId`] for more information +/// on the possibility of hash collisions in rustc, #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)] #[derive(HashStable_Generic, Encodable, Decodable)] pub struct StableCrateId(pub(crate) u64); @@ -174,8 +177,14 @@ pub fn new(crate_name: &str, is_exe: bool, mut metadata: Vec) -> StableC // Also incorporate the rustc version. Otherwise, with -Zsymbol-mangling-version=v0 // and no -Cmetadata, symbols from the same crate compiled with different versions of // rustc are named the same. - let rustc_version = option_env!("CFG_VERSION").unwrap_or("unknown version").as_bytes(); - hasher.write(rustc_version); + // + // RUSTC_FORCE_INCR_COMP_ARTIFACT_HEADER is used to inject rustc version information + // during testing. + if let Some(val) = std::env::var_os("RUSTC_FORCE_INCR_COMP_ARTIFACT_HEADER") { + hasher.write(val.to_string_lossy().into_owned().as_bytes()) + } else { + hasher.write(option_env!("CFG_VERSION").unwrap_or("unknown version").as_bytes()); + } StableCrateId(hasher.finish()) } diff --git a/src/test/run-make-fulldeps/crate-hash-rustc-version/Makefile b/src/test/run-make-fulldeps/crate-hash-rustc-version/Makefile new file mode 100644 index 00000000000..fd66702db7f --- /dev/null +++ b/src/test/run-make-fulldeps/crate-hash-rustc-version/Makefile @@ -0,0 +1,37 @@ +-include ../../run-make-fulldeps/tools.mk + +# Ensure that crates compiled with different rustc versions cannot +# be dynamically linked. + +FLAGS := -Cprefer-dynamic -Zsymbol-mangling-version=v0 +UNAME := $(shell uname) +ifeq ($(UNAME),Linux) + EXT=".so" + NM_CMD := nm -D +endif +ifeq ($(UNAME),Darwin) + EXT=".dylib" + NM_CMD := nm +endif + +ifndef NM_CMD +all: + exit 0 +else +all: + # a.rs is a dylib + $(RUSTC) a.rs --crate-type=dylib $(FLAGS) + # Write symbols to disk. + $(NM_CMD) $(call DYLIB,a) > $(TMPDIR)/symbolsbefore + # b.rs is a binary + $(RUSTC) b.rs --extern a=$(TMPDIR)/liba$(EXT) --crate-type=bin -Crpath $(FLAGS) + $(call RUN,b) + # Now re-compile a.rs with another rustc version + RUSTC_FORCE_INCR_COMP_ARTIFACT_HEADER=deadfeed $(RUSTC) a.rs --crate-type=dylib $(FLAGS) + # After compiling with a different rustc version, write symbols to disk again. + $(NM_CMD) $(call DYLIB,a) > $(TMPDIR)/symbolsafter + # As a sanity check, test if the symbols changed: + # If the symbols are identical, there's been an error. + if diff $(TMPDIR)/symbolsbefore $(TMPDIR)/symbolsafter; then exit 1; fi + $(call FAIL,b) +endif diff --git a/src/test/run-make-fulldeps/crate-hash-rustc-version/a.rs b/src/test/run-make-fulldeps/crate-hash-rustc-version/a.rs new file mode 100644 index 00000000000..d65b5ce8e88 --- /dev/null +++ b/src/test/run-make-fulldeps/crate-hash-rustc-version/a.rs @@ -0,0 +1,4 @@ +pub fn foo(mut x: String) -> String { + x.push_str(", world!"); + x +} diff --git a/src/test/run-make-fulldeps/crate-hash-rustc-version/b.rs b/src/test/run-make-fulldeps/crate-hash-rustc-version/b.rs new file mode 100644 index 00000000000..316ac26e73f --- /dev/null +++ b/src/test/run-make-fulldeps/crate-hash-rustc-version/b.rs @@ -0,0 +1,8 @@ +extern crate a; + +use a::foo; + +fn main() { + let x = String::from("Hello"); + println!("{}", foo(x)); +} diff --git a/src/test/ui/symbol-names/const-generics-demangling.stderr b/src/test/ui/symbol-names/const-generics-demangling.stderr index 1a8794e6d14..8aa08b8a22c 100644 --- a/src/test/ui/symbol-names/const-generics-demangling.stderr +++ b/src/test/ui/symbol-names/const-generics-demangling.stderr @@ -1,4 +1,4 @@ -error: symbol-name(_RMCsCRATE_HASH_1cINtB0_8UnsignedKhb_E) +error: symbol-name(_RMCsCRATE_HASH_1cINtB_8UnsignedKhb_E) --> $DIR/const-generics-demangling.rs:8:1 | LL | #[rustc_symbol_name] @@ -16,7 +16,7 @@ error: demangling-alt(>) LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs_CsCRATE_HASH_1cINtB2_6SignedKsn98_E) +error: symbol-name(_RMs_CsCRATE_HASH_1cINtB_6SignedKsn98_E) --> $DIR/const-generics-demangling.rs:16:1 | LL | #[rustc_symbol_name] @@ -34,7 +34,7 @@ error: demangling-alt(>) LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB3_4BoolKb1_E) +error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB_4BoolKb1_E) --> $DIR/const-generics-demangling.rs:24:1 | LL | #[rustc_symbol_name] @@ -52,7 +52,7 @@ error: demangling-alt(>) LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB3_4CharKc2202_E) +error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB_4CharKc2202_E) --> $DIR/const-generics-demangling.rs:32:1 | LL | #[rustc_symbol_name] diff --git a/src/test/ui/symbol-names/const-generics-str-demangling.stderr b/src/test/ui/symbol-names/const-generics-str-demangling.stderr index fa69c2a3d72..06d3cdda2f8 100644 --- a/src/test/ui/symbol-names/const-generics-str-demangling.stderr +++ b/src/test/ui/symbol-names/const-generics-str-demangling.stderr @@ -1,4 +1,4 @@ -error: symbol-name(_RMCsCRATE_HASH_1cINtB0_3StrKRe616263_E) +error: symbol-name(_RMCsCRATE_HASH_1cINtB_3StrKRe616263_E) --> $DIR/const-generics-str-demangling.rs:9:1 | LL | #[rustc_symbol_name] @@ -16,7 +16,7 @@ error: demangling-alt(>) LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs_CsCRATE_HASH_1cINtB2_3StrKRe27_E) +error: symbol-name(_RMs_CsCRATE_HASH_1cINtB_3StrKRe27_E) --> $DIR/const-generics-str-demangling.rs:15:1 | LL | #[rustc_symbol_name] @@ -34,7 +34,7 @@ error: demangling-alt(>) LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB3_3StrKRe090a_E) +error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB_3StrKRe090a_E) --> $DIR/const-generics-str-demangling.rs:21:1 | LL | #[rustc_symbol_name] @@ -52,7 +52,7 @@ error: demangling-alt(>) LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB3_3StrKRee28882c3bc_E) +error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB_3StrKRee28882c3bc_E) --> $DIR/const-generics-str-demangling.rs:27:1 | LL | #[rustc_symbol_name] @@ -70,7 +70,7 @@ error: demangling-alt(>) LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs2_CsCRATE_HASH_1cINtB3_3StrKRee183a1e18390e183ade1839be18394e1839ae18390e183935fe18392e18394e1839be183a0e18398e18394e1839ae183985fe183a1e18390e18393e18398e1839ae18398_E) +error: symbol-name(_RMs2_CsCRATE_HASH_1cINtB_3StrKRee183a1e18390e183ade1839be18394e1839ae18390e183935fe18392e18394e1839be183a0e18398e18394e1839ae183985fe183a1e18390e18393e18398e1839ae18398_E) --> $DIR/const-generics-str-demangling.rs:33:1 | LL | #[rustc_symbol_name] @@ -88,7 +88,7 @@ error: demangling-alt(_3StrKRef09f908af09fa688f09fa686f09f90ae20c2a720f09f90b6f09f9192e29895f09f94a520c2a720f09fa7a1f09f929bf09f929af09f9299f09f929c_E) --> $DIR/const-generics-str-demangling.rs:39:1 | LL | #[rustc_symbol_name] diff --git a/src/test/ui/symbol-names/const-generics-structural-demangling.stderr b/src/test/ui/symbol-names/const-generics-structural-demangling.stderr index f38342427b1..a4c997477ee 100644 --- a/src/test/ui/symbol-names/const-generics-structural-demangling.stderr +++ b/src/test/ui/symbol-names/const-generics-structural-demangling.stderr @@ -1,4 +1,4 @@ -error: symbol-name(_RMCsCRATE_HASH_1cINtB0_7RefByteKRh7b_E) +error: symbol-name(_RMCsCRATE_HASH_1cINtB_7RefByteKRh7b_E) --> $DIR/const-generics-structural-demangling.rs:14:1 | LL | #[rustc_symbol_name] @@ -16,7 +16,7 @@ error: demangling-alt(>) LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs_CsCRATE_HASH_1cINtB2_6RefZstKRAEE) +error: symbol-name(_RMs_CsCRATE_HASH_1cINtB_6RefZstKRAEE) --> $DIR/const-generics-structural-demangling.rs:24:1 | LL | #[rustc_symbol_name] @@ -34,7 +34,7 @@ error: demangling-alt(>) LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB3_11Array3BytesKAh1_h2_h3_EE) +error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB_11Array3BytesKAh1_h2_h3_EE) --> $DIR/const-generics-structural-demangling.rs:32:1 | LL | #[rustc_symbol_name] @@ -52,7 +52,7 @@ error: demangling-alt(>) LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB3_13TupleByteBoolKTh1_b0_EE) +error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB_13TupleByteBoolKTh1_b0_EE) --> $DIR/const-generics-structural-demangling.rs:40:1 | LL | #[rustc_symbol_name] @@ -70,7 +70,7 @@ error: demangling-alt(>) LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs2_CsCRATE_HASH_1cINtB3_11OptionUsizeKVNtINtNtCsCRATE_HASH_4core6option6OptionjE4NoneUE) +error: symbol-name(_RMs2_CsCRATE_HASH_1cINtB_11OptionUsizeKVNtINtNtCsCRATE_HASH_4core6option6OptionjE4NoneUE) --> $DIR/const-generics-structural-demangling.rs:50:1 | LL | #[rustc_symbol_name] @@ -88,7 +88,7 @@ error: demangling-alt(::None}>>) LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs3_CsCRATE_HASH_1cINtB3_11OptionUsizeKVNtINtNtCsCRATE_HASH_4core6option6OptionjE4SomeTj0_EE) +error: symbol-name(_RMs3_CsCRATE_HASH_1cINtB_11OptionUsizeKVNtINtNtCsCRATE_HASH_4core6option6OptionjE4SomeTj0_EE) --> $DIR/const-generics-structural-demangling.rs:58:1 | LL | #[rustc_symbol_name] @@ -106,7 +106,7 @@ error: demangling-alt(::Some(0)}>> LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RMs4_CsCRATE_HASH_1cINtB3_4Foo_KVNtB3_3FooS1sRe616263_2chc78_5sliceRAh1_h2_h3_EEE) +error: symbol-name(_RMs4_CsCRATE_HASH_1cINtB_4Foo_KVNtB_3FooS1sRe616263_2chc78_5sliceRAh1_h2_h3_EEE) --> $DIR/const-generics-structural-demangling.rs:72:1 | LL | #[rustc_symbol_name] @@ -124,7 +124,7 @@ error: demangling-alt(_4Bar_KVNtB_3BarS1xh7b_s_1xt1000_EE) --> $DIR/const-generics-structural-demangling.rs:88:5 | LL | #[rustc_symbol_name] diff --git a/src/test/ui/symbol-names/impl1.v0.stderr b/src/test/ui/symbol-names/impl1.v0.stderr index c9a448f8265..06778e57fb1 100644 --- a/src/test/ui/symbol-names/impl1.v0.stderr +++ b/src/test/ui/symbol-names/impl1.v0.stderr @@ -1,4 +1,4 @@ -error: symbol-name(_RNvMNtCsCRATE_HASH_5impl13fooNtB2_3Foo3bar) +error: symbol-name(_RNvMNtCsCRATE_HASH_5impl13fooNtB_3Foo3bar) --> $DIR/impl1.rs:14:9 | LL | #[rustc_symbol_name] @@ -22,7 +22,7 @@ error: def-path(foo::Foo::bar) LL | #[rustc_def_path] | ^^^^^^^^^^^^^^^^^ -error: symbol-name(_RNvMNtCsCRATE_HASH_5impl13barNtNtB4_3foo3Foo3baz) +error: symbol-name(_RNvMNtCsCRATE_HASH_5impl13barNtNtB_3foo3Foo3baz) --> $DIR/impl1.rs:32:9 | LL | #[rustc_symbol_name] @@ -46,7 +46,7 @@ error: def-path(bar::::baz) LL | #[rustc_def_path] | ^^^^^^^^^^^^^^^^^ -error: symbol-name(_RNvXNCNvCsCRATE_HASH_5impl14mains_0ARDNtB6_3Foop5AssocFG_KCRL0_hvEuNtB6_9AutoTraitEL_j3_NtB2_3Bar6method) +error: symbol-name(_RNvXNCNvCsCRATE_HASH_5impl14mains_0ARDNtB_3Foop5AssocFG_KCRL0_hvEuNtB_9AutoTraitEL_j3_NtB_3Bar6method) --> $DIR/impl1.rs:62:13 | LL | #[rustc_symbol_name] diff --git a/src/test/ui/symbol-names/issue-60925.v0.stderr b/src/test/ui/symbol-names/issue-60925.v0.stderr index dd95612d8c8..1cddba92085 100644 --- a/src/test/ui/symbol-names/issue-60925.v0.stderr +++ b/src/test/ui/symbol-names/issue-60925.v0.stderr @@ -1,4 +1,4 @@ -error: symbol-name(_RNvMNtCsCRATE_HASH_11issue_609253fooINtB2_3FooNtNtB4_4llvm3FooE3foo) +error: symbol-name(_RNvMNtCsCRATE_HASH_11issue_609253fooINtB_3FooNtNtB_4llvm3FooE3foo) --> $DIR/issue-60925.rs:21:9 | LL | #[rustc_symbol_name] diff --git a/src/test/ui/symbol-names/issue-75326.v0.stderr b/src/test/ui/symbol-names/issue-75326.v0.stderr index c691b11a28f..446fb8d6cf6 100644 --- a/src/test/ui/symbol-names/issue-75326.v0.stderr +++ b/src/test/ui/symbol-names/issue-75326.v0.stderr @@ -1,4 +1,4 @@ -error: symbol-name(_RNvXINICsCRATE_HASH_11issue_75326s_0pppEINtB5_3FooppENtB5_9Iterator24nextB5_) +error: symbol-name(_RNvXINICsCRATE_HASH_11issue_75326s_0pppEINtB_3FooppENtB_9Iterator24nextB_) --> $DIR/issue-75326.rs:41:5 | LL | #[rustc_symbol_name] diff --git a/src/test/ui/symbol-names/trait-objects.v0.stderr b/src/test/ui/symbol-names/trait-objects.v0.stderr index 8d55f42a419..6c5e55ed2ae 100644 --- a/src/test/ui/symbol-names/trait-objects.v0.stderr +++ b/src/test/ui/symbol-names/trait-objects.v0.stderr @@ -1,4 +1,4 @@ -error: symbol-name(_RNvXCsCRATE_HASH_13trait_objectsRDG_INtNtNtCsCRATE_HASH_4core3ops8function5FnMutTRL0_hEEp6OutputuEL_NtB2_3Bar6method) +error: symbol-name(_RNvXCsCRATE_HASH_13trait_objectsRDG_INtNtNtCsCRATE_HASH_4core3ops8function5FnMutTRL0_hEEp6OutputuEL_NtB_3Bar6method) --> $DIR/trait-objects.rs:15:5 | LL | #[rustc_symbol_name] @@ -16,7 +16,7 @@ error: demangling-alt(<&dyn for<'a> core::ops::function::FnMut<(&'a u8,), Output LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RNvXs_CsCRATE_HASH_13trait_objectsRDG_INtNtNtCsCRATE_HASH_4core3ops8function5FnMutTRL0_hEEp6OutputuNtNtBI_6marker4SendEL_NtB4_3Foo6method) +error: symbol-name(_RNvXs_CsCRATE_HASH_13trait_objectsRDG_INtNtNtCsCRATE_HASH_4core3ops8function5FnMutTRL0_hEEp6OutputuNtNtB_6marker4SendEL_NtB_3Foo6method) --> $DIR/trait-objects.rs:27:5 | LL | #[rustc_symbol_name] @@ -34,7 +34,7 @@ error: demangling-alt(<&dyn for<'a> core::ops::function::FnMut<(&'a u8,), Output LL | #[rustc_symbol_name] | ^^^^^^^^^^^^^^^^^^^^ -error: symbol-name(_RNvXs0_CsCRATE_HASH_13trait_objectsRDG_INtNtNtCsCRATE_HASH_4core3ops8function5FnMutTRL0_hEEp6OutputuNtNtBJ_6marker4SendEL_NtB5_3Baz6method) +error: symbol-name(_RNvXs0_CsCRATE_HASH_13trait_objectsRDG_INtNtNtCsCRATE_HASH_4core3ops8function5FnMutTRL0_hEEp6OutputuNtNtB_6marker4SendEL_NtB_3Baz6method) --> $DIR/trait-objects.rs:39:5 | LL | #[rustc_symbol_name] diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index ae97df583a3..f039ba59d23 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -3513,6 +3513,9 @@ fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> S const V0_CRATE_HASH_PREFIX_REGEX: &str = r"_R.*?Cs[0-9a-zA-Z]+_"; const V0_CRATE_HASH_REGEX: &str = r"Cs[0-9a-zA-Z]+_"; const V0_CRATE_HASH_PLACEHOLDER: &str = r"CsCRATE_HASH_"; + const V0_BACK_REF_PREFIX_REGEX: &str = r"\(_R.*?B[0-9a-zA-Z]_"; + const V0_BACK_REF_REGEX: &str = r"B[0-9a-zA-Z]_"; + const V0_BACK_REF_PLACEHOLDER: &str = r"B_"; const LEGACY_SYMBOL_HASH_REGEX: &str = r"h[\w]{16}E?\)"; const LEGACY_SYMBOL_HASH_PLACEHOLDER: &str = r"h)"; let test_name = self @@ -3547,6 +3550,16 @@ fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> S .replace_all(&normalized, V0_CRATE_HASH_PLACEHOLDER) .into_owned(); } + let back_ref_prefix_re = Regex::new(V0_BACK_REF_PREFIX_REGEX).unwrap(); + if back_ref_prefix_re.is_match(&normalized) { + // Normalize back references (see RFC 2603) + let back_ref_regex = format!("{}", V0_BACK_REF_REGEX); + let back_ref_placeholder = format!("{}", V0_BACK_REF_PLACEHOLDER); + normalized = Regex::new(&back_ref_regex) + .unwrap() + .replace_all(&normalized, back_ref_placeholder) + .into_owned(); + } // Normalize legacy mangled symbols normalized = Regex::new(LEGACY_SYMBOL_HASH_REGEX) .unwrap()