Skip to content

Commit 7840a0b

Browse files
committed
Auto merge of #57967 - eddyb:rmangle, r=michaelwoerister
Introduce Rust symbol mangling scheme. This is an implementation of a "feature-complete" Rust mangling scheme, in the vein of rust-lang/rfcs#2603 ~~- but with some differences, see rust-lang/rfcs#2603 (comment) for details~~ (@michaelwoerister integrated my proposed changes into the RFC itself). On nightly, you can now control the mangling scheme with `-Z symbol-mangling-version`, which can be: * `legacy`: the older mangling version, still the default currently * `v0`: the new RFC mangling version, as implemented by this PR To test the new mangling, set `RUSTFLAGS=-Zsymbol-mangling-version=v0` (or change [`rustflags` in `.cargo/config.toml`](https://doc.rust-lang.org/cargo/reference/config.html#configuration-keys)). Please note that only symbols from crates built with that flag will use the new mangling, and that tool support (e.g. debuggers) will be limited initially, and it may take a while for everything to be upstreamed. However, `RUST_BACKTRACE` should work out of the box with either mangling version. <hr/> The demangling implementation PR is rust-lang/rustc-demangle#23 ~~(this PR already uses it via a git dependency, to allow testing)~~. Discussion of the *design* of the mangling scheme should still happen on the RFC, but this PR's specific implementation details can be reviewed in parallel. *Notes for reviewers*: * ~~only the last 6 commits are specific to this branch, if necessary I can open a separate PR for everything else (it was meant to be its own small refactoring, but it got a bit out of hand)~~ ~~based on #58140~~ * the "harness" commit is only there because it does some extra validation (comparing the demangling from `rustc-demangle` to the compiler's pretty-printing, adjusted slightly to produce the same output), that I would like to try on crater * ~~there is the question of whether we should turn on the new mangling now, wait for tools to support it (I'm working on that), and/or have it under a `-Z` flag for now~~ (we're gating this on `-Z symbol-mangling-version=v0`, see above) r? @nikomatsakis / @michaelwoerister cc @rust-lang/compiler
2 parents 75f4644 + 3652ea4 commit 7840a0b

39 files changed

+1672
-582
lines changed

Cargo.lock

+13-5
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ dependencies = [
120120
"cfg-if 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
121121
"compiler_builtins 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
122122
"libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)",
123-
"rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
123+
"rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
124124
"rustc-std-workspace-core 1.0.0",
125125
]
126126

@@ -2023,6 +2023,11 @@ dependencies = [
20232023
"unicase 2.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
20242024
]
20252025

2026+
[[package]]
2027+
name = "punycode"
2028+
version = "0.4.0"
2029+
source = "registry+https://github.com/rust-lang/crates.io-index"
2030+
20262031
[[package]]
20272032
name = "quick-error"
20282033
version = "1.2.2"
@@ -2492,7 +2497,7 @@ dependencies = [
24922497

24932498
[[package]]
24942499
name = "rustc-demangle"
2495-
version = "0.1.10"
2500+
version = "0.1.15"
24962501
source = "registry+https://github.com/rust-lang/crates.io-index"
24972502
dependencies = [
24982503
"compiler_builtins 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2648,7 +2653,7 @@ dependencies = [
26482653
"cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
26492654
"memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
26502655
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
2651-
"rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
2656+
"rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
26522657
"rustc_llvm 0.0.0",
26532658
]
26542659

@@ -2665,7 +2670,7 @@ dependencies = [
26652670
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
26662671
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
26672672
"rustc 0.0.0",
2668-
"rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
2673+
"rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
26692674
"rustc_allocator 0.0.0",
26702675
"rustc_apfloat 0.0.0",
26712676
"rustc_codegen_utils 0.0.0",
@@ -2687,7 +2692,9 @@ version = "0.0.0"
26872692
dependencies = [
26882693
"flate2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
26892694
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
2695+
"punycode 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
26902696
"rustc 0.0.0",
2697+
"rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
26912698
"rustc_data_structures 0.0.0",
26922699
"rustc_metadata 0.0.0",
26932700
"rustc_mir 0.0.0",
@@ -4276,6 +4283,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
42764283
"checksum proptest 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "24f5844db2f839e97e3021980975f6ebf8691d9b9b2ca67ed3feb38dc3edb52c"
42774284
"checksum pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d6fdf85cda6cadfae5428a54661d431330b312bc767ddbc57adbedc24da66e32"
42784285
"checksum pulldown-cmark 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "051e60ace841b3bfecd402fe5051c06cb3bec4a6e6fdd060a37aa8eb829a1db3"
4286+
"checksum punycode 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6ddd112cca70a4d30883b2d21568a1d376ff8be4758649f64f973c6845128ad3"
42794287
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
42804288
"checksum quine-mc_cluskey 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "07589615d719a60c8dd8a4622e7946465dfef20d1a428f969e3443e7386d5f45"
42814289
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
@@ -4313,7 +4321,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
43134321
"checksum rustc-ap-serialize 407.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf09c60aaee892b0fd107544cfe607d8d463e7f33da34aa823566b8fd2b17f53"
43144322
"checksum rustc-ap-syntax 407.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "69f38cc120ff317678bbda8c4f58c1bbc1de64b615383ab01480482dde5e95a1"
43154323
"checksum rustc-ap-syntax_pos 407.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "20a0a201141c5c416b1924b079eeefc7b013e34ece0740ce4997f358b3684a7f"
4316-
"checksum rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "82ae957aa1b3055d8e086486723c0ccd3d7b8fa190ae8fa2e35543b6171c810e"
4324+
"checksum rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "a7f4dccf6f4891ebcc0c39f9b6eb1a83b9bf5d747cb439ec6fba4f3b977038af"
43174325
"checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8"
43184326
"checksum rustc-rayon 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8d98c51d9cbbe810c8b6693236d3412d8cd60513ff27a3e1b6af483dca0af544"
43194327
"checksum rustc-rayon 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0d2e07e19601f21c59aad953c2632172ba70cb27e685771514ea66e4062b3363"

src/librustc/query/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,10 @@ rustc_queries! {
624624
fatal_cycle
625625
desc { "test whether a crate has #![no_builtins]" }
626626
}
627+
query symbol_mangling_version(_: CrateNum) -> SymbolManglingVersion {
628+
fatal_cycle
629+
desc { "query a crate's symbol mangling version" }
630+
}
627631

628632
query extern_crate(_: DefId) -> Option<&'tcx ExternCrate> {
629633
eval_always

src/librustc/session/config.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ impl SwitchWithOptPath {
131131
}
132132
}
133133

134+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
135+
pub enum SymbolManglingVersion {
136+
Legacy,
137+
V0,
138+
}
139+
140+
impl_stable_hash_via_hash!(SymbolManglingVersion);
141+
134142
#[derive(Clone, Copy, PartialEq, Hash)]
135143
pub enum DebugInfo {
136144
None,
@@ -838,11 +846,14 @@ macro_rules! options {
838846
Some("an optional path to the profiling data output directory");
839847
pub const parse_merge_functions: Option<&str> =
840848
Some("one of: `disabled`, `trampolines`, or `aliases`");
849+
pub const parse_symbol_mangling_version: Option<&str> =
850+
Some("either `legacy` or `v0` (RFC 2603)");
841851
}
842852

843853
#[allow(dead_code)]
844854
mod $mod_set {
845-
use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath};
855+
use super::{$struct_name, Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath,
856+
SymbolManglingVersion};
846857
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
847858
use std::path::PathBuf;
848859
use std::str::FromStr;
@@ -1112,6 +1123,18 @@ macro_rules! options {
11121123
}
11131124
true
11141125
}
1126+
1127+
fn parse_symbol_mangling_version(
1128+
slot: &mut SymbolManglingVersion,
1129+
v: Option<&str>,
1130+
) -> bool {
1131+
*slot = match v {
1132+
Some("legacy") => SymbolManglingVersion::Legacy,
1133+
Some("v0") => SymbolManglingVersion::V0,
1134+
_ => return false,
1135+
};
1136+
true
1137+
}
11151138
}
11161139
) }
11171140

@@ -1457,6 +1480,9 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
14571480
"only allow the listed language features to be enabled in code (space separated)"),
14581481
emit_artifact_notifications: bool = (false, parse_bool, [UNTRACKED],
14591482
"emit notifications after each artifact has been output (only in the JSON format)"),
1483+
symbol_mangling_version: SymbolManglingVersion = (SymbolManglingVersion::Legacy,
1484+
parse_symbol_mangling_version, [TRACKED],
1485+
"which mangling version to use for symbol names"),
14601486
}
14611487

14621488
pub fn default_lib_output() -> CrateType {
@@ -2551,7 +2577,8 @@ mod dep_tracking {
25512577
use std::path::PathBuf;
25522578
use std::collections::hash_map::DefaultHasher;
25532579
use super::{CrateType, DebugInfo, ErrorOutputType, OptLevel, OutputTypes,
2554-
Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath};
2580+
Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath,
2581+
SymbolManglingVersion};
25552582
use syntax::feature_gate::UnstableFeatures;
25562583
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel, TargetTriple};
25572584
use syntax::edition::Edition;
@@ -2620,6 +2647,7 @@ mod dep_tracking {
26202647
impl_dep_tracking_hash_via_hash!(Edition);
26212648
impl_dep_tracking_hash_via_hash!(LinkerPluginLto);
26222649
impl_dep_tracking_hash_via_hash!(SwitchWithOptPath);
2650+
impl_dep_tracking_hash_via_hash!(SymbolManglingVersion);
26232651

26242652
impl_dep_tracking_hash_for_sortable_vec_of!(String);
26252653
impl_dep_tracking_hash_for_sortable_vec_of!(PathBuf);
@@ -2693,7 +2721,7 @@ mod tests {
26932721
use std::collections::{BTreeMap, BTreeSet};
26942722
use std::iter::FromIterator;
26952723
use std::path::PathBuf;
2696-
use super::{Externs, OutputType, OutputTypes};
2724+
use super::{Externs, OutputType, OutputTypes, SymbolManglingVersion};
26972725
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel};
26982726
use syntax::symbol::sym;
26992727
use syntax::edition::{Edition, DEFAULT_EDITION};
@@ -3367,6 +3395,10 @@ mod tests {
33673395
opts = reference.clone();
33683396
opts.debugging_opts.allow_features = Some(vec![String::from("lang_items")]);
33693397
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
3398+
3399+
opts = reference.clone();
3400+
opts.debugging_opts.symbol_mangling_version = SymbolManglingVersion::V0;
3401+
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
33703402
}
33713403

33723404
#[test]

src/librustc/ty/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::mir::mono::CodegenUnit;
2020
use crate::mir;
2121
use crate::mir::interpret::GlobalId;
2222
use crate::session::CrateDisambiguator;
23-
use crate::session::config::{EntryFnType, OutputFilenames, OptLevel};
23+
use crate::session::config::{EntryFnType, OutputFilenames, OptLevel, SymbolManglingVersion};
2424
use crate::traits::{self, Vtable};
2525
use crate::traits::query::{
2626
CanonicalPredicateGoal, CanonicalProjectionGoal,

src/librustc_codegen_llvm/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test = false
1313
[dependencies]
1414
cc = "1.0.1" # Used to locate MSVC
1515
num_cpus = "1.0"
16-
rustc-demangle = "0.1.4"
16+
rustc-demangle = "0.1.15"
1717
rustc_llvm = { path = "../librustc_llvm" }
1818
memmap = "0.6"
1919

src/librustc_codegen_ssa/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test = false
1414
bitflags = "1.0.4"
1515
cc = "1.0.1"
1616
num_cpus = "1.0"
17-
rustc-demangle = "0.1.4"
17+
rustc-demangle = "0.1.15"
1818
memmap = "0.6"
1919
log = "0.4.5"
2020
libc = "0.2.44"

src/librustc_codegen_utils/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ test = false
1313
[dependencies]
1414
flate2 = "1.0"
1515
log = "0.4"
16+
punycode = "0.4.0"
17+
rustc-demangle = "0.1.15"
1618

1719
syntax = { path = "../libsyntax" }
1820
syntax_pos = { path = "../libsyntax_pos" }

src/librustc_codegen_utils/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#![feature(arbitrary_self_types)]
88
#![feature(box_patterns)]
99
#![feature(box_syntax)]
10+
#![feature(core_intrinsics)]
1011
#![feature(custom_attribute)]
12+
#![feature(never_type)]
1113
#![feature(nll)]
1214
#![allow(unused_attributes)]
1315
#![feature(rustc_diagnostic_macros)]

0 commit comments

Comments
 (0)