Skip to content

Commit 8f3238f

Browse files
committed
Auto merge of #90128 - joshtriplett:stabilize-symbol-mangling-version, r=wesleywiser
Stabilize -Z symbol-mangling-version=v0 as -C symbol-mangling-version=v0 This allows selecting `v0` symbol-mangling without an unstable option. Selecting `legacy` still requires -Z unstable-options. This does not change the default symbol-mangling-version. See #89917 for a pull request changing the default. Rationale, from #89917: Rust's current mangling scheme depends on compiler internals; loses information about generic parameters (and other things) which makes for a worse experience when using external tools that need to interact with Rust symbol names; is inconsistent; and can contain . characters which aren't universally supported. Therefore, Rust has defined its own symbol mangling scheme which is defined in terms of the Rust language, not the compiler implementation; encodes information about generic parameters in a reversible way; has a consistent definition; and generates symbols that only use the characters A-Z, a-z, 0-9, and _. Support for the new Rust symbol mangling scheme has been added to upstream tools that will need to interact with Rust symbols (e.g. debuggers). This pull request allows enabling the new v0 symbol-mangling-version. See #89917 for references to the implementation of v0, and for references to the tool changes to decode Rust symbols.
2 parents 03360be + ff94b3b commit 8f3238f

File tree

28 files changed

+76
-46
lines changed

28 files changed

+76
-46
lines changed

compiler/rustc_codegen_gcc/config.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then
3838
fi
3939
fi
4040

41-
export RUSTFLAGS="$linker -Cpanic=abort -Zsymbol-mangling-version=v0 -Cdebuginfo=2 -Clto=off -Zpanic-abort-tests -Zcodegen-backend=$(pwd)/target/${CHANNEL:-debug}/librustc_codegen_gcc.$dylib_ext --sysroot $(pwd)/build_sysroot/sysroot"
41+
export RUSTFLAGS="$linker -Cpanic=abort -Csymbol-mangling-version=v0 -Cdebuginfo=2 -Clto=off -Zpanic-abort-tests -Zcodegen-backend=$(pwd)/target/${CHANNEL:-debug}/librustc_codegen_gcc.$dylib_ext --sysroot $(pwd)/build_sysroot/sysroot"
4242

4343
# FIXME(antoyo): remove once the atomic shim is gone
4444
if [[ `uname` == 'Darwin' ]]; then

compiler/rustc_codegen_gcc/test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ EOF
183183
git checkout src/test/ui/type-alias-impl-trait/auxiliary/cross_crate_ice2.rs
184184
rm src/test/ui/llvm-asm/llvm-asm-in-out-operand.rs || true # TODO(antoyo): Enable back this test if I ever implement the llvm_asm! macro.
185185

186-
RUSTC_ARGS="-Zpanic-abort-tests -Zsymbol-mangling-version=v0 -Zcodegen-backend="$(pwd)"/../target/"$CHANNEL"/librustc_codegen_gcc."$dylib_ext" --sysroot "$(pwd)"/../build_sysroot/sysroot -Cpanic=abort"
186+
RUSTC_ARGS="-Zpanic-abort-tests -Csymbol-mangling-version=v0 -Zcodegen-backend="$(pwd)"/../target/"$CHANNEL"/librustc_codegen_gcc."$dylib_ext" --sysroot "$(pwd)"/../build_sysroot/sysroot -Cpanic=abort"
187187

188188
echo "[TEST] rustc test suite"
189189
COMPILETEST_FORCE_STAGE0=1 ./x.py test --run always --stage 0 src/test/ui/ --rustc-args "$RUSTC_ARGS"

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ fn test_codegen_options_tracking_hash() {
594594
tracked!(relocation_model, Some(RelocModel::Pic));
595595
tracked!(soft_float, true);
596596
tracked!(split_debuginfo, Some(SplitDebuginfo::Packed));
597+
tracked!(symbol_mangling_version, Some(SymbolManglingVersion::V0));
597598
tracked!(target_cpu, Some(String::from("abc")));
598599
tracked!(target_feature, String::from("all the features, all of them"));
599600
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
721721
no_builtins: tcx.sess.contains_name(&attrs, sym::no_builtins),
722722
panic_runtime: tcx.sess.contains_name(&attrs, sym::panic_runtime),
723723
profiler_runtime: tcx.sess.contains_name(&attrs, sym::profiler_runtime),
724-
symbol_mangling_version: tcx.sess.opts.debugging_opts.get_symbol_mangling_version(),
724+
symbol_mangling_version: tcx.sess.opts.get_symbol_mangling_version(),
725725

726726
crate_deps,
727727
dylib_dependency_formats,

compiler/rustc_session/src/config.rs

+36-10
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,10 @@ impl Options {
781781
},
782782
}
783783
}
784+
785+
pub fn get_symbol_mangling_version(&self) -> SymbolManglingVersion {
786+
self.cg.symbol_mangling_version.unwrap_or(SymbolManglingVersion::Legacy)
787+
}
784788
}
785789

786790
impl DebuggingOptions {
@@ -794,10 +798,6 @@ impl DebuggingOptions {
794798
deduplicate_diagnostics: self.deduplicate_diagnostics,
795799
}
796800
}
797-
798-
pub fn get_symbol_mangling_version(&self) -> SymbolManglingVersion {
799-
self.symbol_mangling_version.unwrap_or(SymbolManglingVersion::Legacy)
800-
}
801801
}
802802

803803
// The type of entry function, so users can have their own entry functions
@@ -2116,6 +2116,34 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
21162116
);
21172117
}
21182118

2119+
// Handle both `-Z symbol-mangling-version` and `-C symbol-mangling-version`; the latter takes
2120+
// precedence.
2121+
match (cg.symbol_mangling_version, debugging_opts.symbol_mangling_version) {
2122+
(Some(smv_c), Some(smv_z)) if smv_c != smv_z => {
2123+
early_error(
2124+
error_format,
2125+
"incompatible values passed for `-C symbol-mangling-version` \
2126+
and `-Z symbol-mangling-version`",
2127+
);
2128+
}
2129+
(Some(SymbolManglingVersion::V0), _) => {}
2130+
(Some(_), _) if !debugging_opts.unstable_options => {
2131+
early_error(
2132+
error_format,
2133+
"`-C symbol-mangling-version=legacy` requires `-Z unstable-options`",
2134+
);
2135+
}
2136+
(None, None) => {}
2137+
(None, smv) => {
2138+
early_warn(
2139+
error_format,
2140+
"`-Z symbol-mangling-version` is deprecated; use `-C symbol-mangling-version`",
2141+
);
2142+
cg.symbol_mangling_version = smv;
2143+
}
2144+
_ => {}
2145+
}
2146+
21192147
if debugging_opts.instrument_coverage.is_some()
21202148
&& debugging_opts.instrument_coverage != Some(InstrumentCoverage::Off)
21212149
{
@@ -2127,19 +2155,17 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
21272155
);
21282156
}
21292157

2130-
// `-Z instrument-coverage` implies `-Z symbol-mangling-version=v0` - to ensure consistent
2158+
// `-Z instrument-coverage` implies `-C symbol-mangling-version=v0` - to ensure consistent
21312159
// and reversible name mangling. Note, LLVM coverage tools can analyze coverage over
21322160
// multiple runs, including some changes to source code; so mangled names must be consistent
21332161
// across compilations.
2134-
match debugging_opts.symbol_mangling_version {
2135-
None => {
2136-
debugging_opts.symbol_mangling_version = Some(SymbolManglingVersion::V0);
2137-
}
2162+
match cg.symbol_mangling_version {
2163+
None => cg.symbol_mangling_version = Some(SymbolManglingVersion::V0),
21382164
Some(SymbolManglingVersion::Legacy) => {
21392165
early_warn(
21402166
error_format,
21412167
"-Z instrument-coverage requires symbol mangling version `v0`, \
2142-
but `-Z symbol-mangling-version=legacy` was specified",
2168+
but `-C symbol-mangling-version=legacy` was specified",
21432169
);
21442170
}
21452171
Some(SymbolManglingVersion::V0) => {}

compiler/rustc_session/src/options.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,9 @@ options! {
10831083
"how to handle split-debuginfo, a platform-specific option"),
10841084
strip: Strip = (Strip::None, parse_strip, [UNTRACKED],
10851085
"tell the linker which information to strip (`none` (default), `debuginfo` or `symbols`)"),
1086+
symbol_mangling_version: Option<SymbolManglingVersion> = (None,
1087+
parse_symbol_mangling_version, [TRACKED],
1088+
"which mangling version to use for symbol names ('legacy' (default) or 'v0')"),
10861089
target_cpu: Option<String> = (None, parse_opt_string, [TRACKED],
10871090
"select target processor (`rustc --print target-cpus` for details)"),
10881091
target_feature: String = (String::new(), parse_target_feature, [TRACKED],
@@ -1227,7 +1230,7 @@ options! {
12271230
instrument_coverage: Option<InstrumentCoverage> = (None, parse_instrument_coverage, [TRACKED],
12281231
"instrument the generated code to support LLVM source-based code coverage \
12291232
reports (note, the compiler build config must include `profiler = true`); \
1230-
implies `-Z symbol-mangling-version=v0`. Optional values are:
1233+
implies `-C symbol-mangling-version=v0`. Optional values are:
12311234
`=all` (implicit value)
12321235
`=except-unused-generics`
12331236
`=except-unused-functions`

compiler/rustc_symbol_mangling/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,15 @@ fn compute_symbol_name<'tcx>(
237237

238238
// Pick the crate responsible for the symbol mangling version, which has to:
239239
// 1. be stable for each instance, whether it's being defined or imported
240-
// 2. obey each crate's own `-Z symbol-mangling-version`, as much as possible
240+
// 2. obey each crate's own `-C symbol-mangling-version`, as much as possible
241241
// We solve these as follows:
242242
// 1. because symbol names depend on both `def_id` and `instantiating_crate`,
243243
// both their `CrateNum`s are stable for any given instance, so we can pick
244244
// either and have a stable choice of symbol mangling version
245245
// 2. we favor `instantiating_crate` where possible (i.e. when `Some`)
246246
let mangling_version_crate = instantiating_crate.unwrap_or(def_id.krate);
247247
let mangling_version = if mangling_version_crate == LOCAL_CRATE {
248-
tcx.sess.opts.debugging_opts.get_symbol_mangling_version()
248+
tcx.sess.opts.get_symbol_mangling_version()
249249
} else {
250250
tcx.symbol_mangling_version(mangling_version_crate)
251251
};

src/doc/unstable-book/src/compiler-flags/instrument-coverage.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ When running a coverage-instrumented program, the counter values are written to
2727
[`llvm.instrprof.increment`]: https://llvm.org/docs/LangRef.html#llvm-instrprof-increment-intrinsic
2828
[llvm code coverage mapping format]: https://llvm.org/docs/CoverageMappingFormat.html
2929

30-
> **Note**: `-Z instrument-coverage` also automatically enables `-Z symbol-mangling-version=v0` (tracking issue [#60705]). The `v0` symbol mangler is strongly recommended, but be aware that this demangler is also experimental. The `v0` demangler can be overridden by explicitly adding `-Z symbol-mangling-version=legacy`.
30+
> **Note**: `-Z instrument-coverage` also automatically enables `-C symbol-mangling-version=v0` (tracking issue [#60705]). The `v0` symbol mangler is strongly recommended, but be aware that this demangler is also experimental. The `v0` demangler can be overridden by explicitly adding `-Z unstable-options -C symbol-mangling-version=legacy`.
3131
3232
[#60705]: https://github.com/rust-lang/rust/issues/60705
3333

src/test/codegen/inline-hint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Checks that closures, constructors, and shims except
22
// for a drop glue receive inline hint by default.
33
//
4-
// compile-flags: -Cno-prepopulate-passes -Zsymbol-mangling-version=v0
4+
// compile-flags: -Cno-prepopulate-passes -Csymbol-mangling-version=v0
55
#![crate_type = "lib"]
66

77
pub fn f() {

src/test/run-make-fulldeps/a-b-a-linker-guard/Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
# and will probably get removed once `legacy` is gone.
99

1010
all:
11-
$(RUSTC) a.rs --cfg x -C prefer-dynamic -Z symbol-mangling-version=legacy
12-
$(RUSTC) b.rs -C prefer-dynamic -Z symbol-mangling-version=legacy
11+
$(RUSTC) a.rs --cfg x -C prefer-dynamic -Z unstable-options -C symbol-mangling-version=legacy
12+
$(RUSTC) b.rs -C prefer-dynamic -Z unstable-options -C symbol-mangling-version=legacy
1313
$(call RUN,b)
14-
$(RUSTC) a.rs --cfg y -C prefer-dynamic -Z symbol-mangling-version=legacy
14+
$(RUSTC) a.rs --cfg y -C prefer-dynamic -Z unstable-options -C symbol-mangling-version=legacy
1515
$(call FAIL,b)

src/test/run-make-fulldeps/share-generics-dylib/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
-include ../../run-make-fulldeps/tools.mk
1313

14-
COMMON_ARGS=-Cprefer-dynamic -Zshare-generics=yes -Ccodegen-units=1 -Zsymbol-mangling-version=v0
14+
COMMON_ARGS=-Cprefer-dynamic -Zshare-generics=yes -Ccodegen-units=1 -Csymbol-mangling-version=v0
1515

1616
all:
1717
$(RUSTC) instance_provider_a.rs $(COMMON_ARGS) --crate-type=rlib

src/test/ui/lifetimes/issue-84604.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-pass
2-
// compile-flags: -Zsymbol-mangling-version=v0
2+
// compile-flags: -Csymbol-mangling-version=v0
33

44
pub fn f<T: ?Sized>() {}
55
pub trait Frob<T: ?Sized> {}

src/test/ui/panics/issue-47429-short-backtraces.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
// NOTE(eddyb) output differs between symbol mangling schemes
1616
// revisions: legacy v0
17-
// [legacy] compile-flags: -Zsymbol-mangling-version=legacy
18-
// [v0] compile-flags: -Zsymbol-mangling-version=v0
17+
// [legacy] compile-flags: -Zunstable-options -Csymbol-mangling-version=legacy
18+
// [v0] compile-flags: -Csymbol-mangling-version=v0
1919

2020
fn main() {
2121
panic!()

src/test/ui/polymorphization/closure_in_upvar/fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// build-pass
2-
// compile-flags:-Zpolymorphize=on -Zsymbol-mangling-version=v0
2+
// compile-flags:-Zpolymorphize=on -Csymbol-mangling-version=v0
33

44
fn foo(f: impl Fn()) {
55
let x = |_: ()| ();

src/test/ui/polymorphization/closure_in_upvar/fnmut.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// build-pass
2-
// compile-flags:-Zpolymorphize=on -Zsymbol-mangling-version=v0
2+
// compile-flags:-Zpolymorphize=on -Csymbol-mangling-version=v0
33

44
fn foo(f: impl Fn()) {
55
// Mutate an upvar from `x` so that it implements `FnMut`.

src/test/ui/polymorphization/closure_in_upvar/fnonce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// build-pass
2-
// compile-flags:-Zpolymorphize=on -Zsymbol-mangling-version=v0
2+
// compile-flags:-Zpolymorphize=on -Csymbol-mangling-version=v0
33

44
fn foo(f: impl Fn()) {
55
// Move a non-copy type into `x` so that it implements `FnOnce`.

src/test/ui/polymorphization/closure_in_upvar/other.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// build-pass
2-
// compile-flags:-Zpolymorphize=on -Zsymbol-mangling-version=v0
2+
// compile-flags:-Zpolymorphize=on -Csymbol-mangling-version=v0
33

44
fn y_uses_f(f: impl Fn()) {
55
let x = |_: ()| ();

src/test/ui/polymorphization/symbol-ambiguity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// build-pass
2-
// compile-flags: -Zpolymorphize=on -Zsymbol-mangling-version=v0
2+
// compile-flags: -Zpolymorphize=on -Csymbol-mangling-version=v0
33

44
pub(crate) struct Foo<'a, I, E>(I, &'a E);
55

src/test/ui/symbol-names/basic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// build-fail
22
// revisions: legacy v0
3-
//[legacy]compile-flags: -Z symbol-mangling-version=legacy
4-
//[v0]compile-flags: -Z symbol-mangling-version=v0
3+
//[legacy]compile-flags: -Z unstable-options -C symbol-mangling-version=legacy
4+
//[v0]compile-flags: -C symbol-mangling-version=v0
55

66
#![feature(rustc_attrs)]
77

src/test/ui/symbol-names/const-generics-demangling.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// build-fail
2-
// compile-flags: -Z symbol-mangling-version=v0 --crate-name=c
2+
// compile-flags: -C symbol-mangling-version=v0 --crate-name=c
33
// normalize-stderr-test: "c\[.*?\]" -> "c[HASH]"
44
#![feature(rustc_attrs)]
55

src/test/ui/symbol-names/const-generics-str-demangling.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// build-fail
2-
// compile-flags: -Z symbol-mangling-version=v0 --crate-name=c
2+
// compile-flags: -C symbol-mangling-version=v0 --crate-name=c
33
// normalize-stderr-test: "c\[.*?\]" -> "c[HASH]"
44
#![feature(adt_const_params, rustc_attrs)]
55
#![allow(incomplete_features)]

src/test/ui/symbol-names/const-generics-structural-demangling.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// build-fail
2-
// compile-flags: -Z symbol-mangling-version=v0 --crate-name=c
2+
// compile-flags: -C symbol-mangling-version=v0 --crate-name=c
33

44
// NOTE(eddyb) we need `core` for `core::option::Option`, normalize away its
55
// disambiguator hash, which can/should change (including between stage{1,2}).

src/test/ui/symbol-names/const-generics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// check-pass
22
// revisions: legacy v0
3-
//[legacy]compile-flags: -Z symbol-mangling-version=legacy --crate-type=lib
4-
//[v0]compile-flags: -Z symbol-mangling-version=v0 --crate-type=lib
3+
//[legacy]compile-flags: -Z unstable-options -C symbol-mangling-version=legacy --crate-type=lib
4+
//[v0]compile-flags: -C symbol-mangling-version=v0 --crate-type=lib
55

66
// `char`
77
pub struct Char<const F: char>;

src/test/ui/symbol-names/impl1.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// build-fail
22
// revisions: legacy v0
3-
//[legacy]compile-flags: -Z symbol-mangling-version=legacy
4-
//[v0]compile-flags: -Z symbol-mangling-version=v0
3+
//[legacy]compile-flags: -Z unstable-options -C symbol-mangling-version=legacy
4+
//[v0]compile-flags: -C symbol-mangling-version=v0
55
//[legacy]normalize-stderr-test: "h[\w]{16}E?\)" -> "<SYMBOL_HASH>)"
66

77
#![feature(auto_traits, rustc_attrs)]

src/test/ui/symbol-names/issue-60925.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// build-fail
22
// revisions: legacy v0
3-
//[legacy]compile-flags: -Z symbol-mangling-version=legacy
4-
//[v0]compile-flags: -Z symbol-mangling-version=v0
3+
//[legacy]compile-flags: -Z unstable-options -C symbol-mangling-version=legacy
4+
//[v0]compile-flags: -C symbol-mangling-version=v0
55

66
#![feature(rustc_attrs)]
77

src/test/ui/symbol-names/issue-75326.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// build-fail
22
// revisions: legacy v0
3-
//[legacy]compile-flags: -Z symbol-mangling-version=legacy
4-
//[v0]compile-flags: -Z symbol-mangling-version=v0
3+
//[legacy]compile-flags: -Z unstable-options -C symbol-mangling-version=legacy
4+
//[v0]compile-flags: -C symbol-mangling-version=v0
55
//[legacy]normalize-stderr-test: "h[\w{16}]+" -> "SYMBOL_HASH"
66

77
#![feature(rustc_attrs)]

src/test/ui/symbol-names/issue-76365.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// check-pass
22
// revisions: legacy v0
3-
//[legacy]compile-flags: -Z symbol-mangling-version=legacy --crate-type=lib
4-
//[v0]compile-flags: -Z symbol-mangling-version=v0 --crate-type=lib
3+
//[legacy]compile-flags: -Z unstable-options -C symbol-mangling-version=legacy --crate-type=lib
4+
//[v0]compile-flags: -C symbol-mangling-version=v0 --crate-type=lib
55

66

77
pub struct Bar<const F: bool>;

src/test/ui/symbol-names/trait-objects.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// build-fail
44
// revisions: v0
5-
//[v0]compile-flags: -Z symbol-mangling-version=v0
5+
//[v0]compile-flags: -C symbol-mangling-version=v0
66
//[v0]normalize-stderr-test: "core\[.*?\]" -> "core[HASH]"
77

88
#![feature(rustc_attrs)]

0 commit comments

Comments
 (0)