Skip to content

Commit

Permalink
Skippable v0 symbol name sections (MCP 737) Proof-of-concept impl
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed May 24, 2024
1 parent bb6e11f commit 17c7fc4
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 25 deletions.
17 changes: 11 additions & 6 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ dependencies = [
"libc",
"miniz_oxide",
"object 0.32.2",
"rustc-demangle",
"rustc-demangle 0.1.24",
]

[[package]]
Expand Down Expand Up @@ -840,7 +840,7 @@ dependencies = [
"md-5",
"miniz_oxide",
"regex",
"rustc-demangle",
"rustc-demangle 0.1.25",
]

[[package]]
Expand Down Expand Up @@ -3246,7 +3246,7 @@ name = "rust-demangler"
version = "0.0.1"
dependencies = [
"regex",
"rustc-demangle",
"rustc-demangle 0.1.24",
]

[[package]]
Expand Down Expand Up @@ -3280,6 +3280,11 @@ dependencies = [
"rustc-std-workspace-core",
]

[[package]]
name = "rustc-demangle"
version = "0.1.25"
source = "git+https://github.com/michaelwoerister/rustc-demangle.git?branch=skip_unknown#7e674e483a7c61803edc8d59f2641a4ad52a1a30"

[[package]]
name = "rustc-hash"
version = "1.1.0"
Expand Down Expand Up @@ -3551,7 +3556,7 @@ dependencies = [
"libc",
"measureme",
"object 0.32.2",
"rustc-demangle",
"rustc-demangle 0.1.25",
"rustc_ast",
"rustc_attr",
"rustc_codegen_ssa",
Expand Down Expand Up @@ -4553,7 +4558,7 @@ name = "rustc_symbol_mangling"
version = "0.0.0"
dependencies = [
"punycode",
"rustc-demangle",
"rustc-demangle 0.1.25",
"rustc_data_structures",
"rustc_errors",
"rustc_hir",
Expand Down Expand Up @@ -5147,7 +5152,7 @@ dependencies = [
"r-efi-alloc",
"rand",
"rand_xorshift",
"rustc-demangle",
"rustc-demangle 0.1.24",
"std_detect",
"unwind",
"wasi",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ itertools = "0.12"
libc = "0.2"
measureme = "11"
object = { version = "0.32.0", default-features = false, features = ["std", "read"] }
rustc-demangle = "0.1.21"
rustc-demangle = { git = "https://github.com/michaelwoerister/rustc-demangle.git", branch = "skip_unknown" }
rustc_ast = { path = "../rustc_ast" }
rustc_attr = { path = "../rustc_attr" }
rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_symbol_mangling/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
# tidy-alphabetical-start
punycode = "0.4.0"
rustc-demangle = "0.1.21"
rustc-demangle = { git = "https://github.com/michaelwoerister/rustc-demangle.git", branch = "skip_unknown" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
rustc_hir = { path = "../rustc_hir" }
Expand Down
91 changes: 89 additions & 2 deletions compiler/rustc_symbol_mangling/src/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub(super) fn mangle<'tcx>(
consts: FxHashMap::default(),
binders: vec![],
out: String::from(prefix),
wrapper_level: GrammarFeatureLevel::BASE_LINE,
};

// Append `::{shim:...#0}` to shims that can coexist with a non-shim instance.
Expand Down Expand Up @@ -79,11 +80,13 @@ pub(super) fn mangle_typeid_for_trait_ref<'tcx>(
consts: FxHashMap::default(),
binders: vec![],
out: String::new(),
wrapper_level: GrammarFeatureLevel::BASE_LINE,
};
cx.print_def_path(trait_ref.def_id(), &[]).unwrap();
std::mem::take(&mut cx.out)
}

#[derive(Clone)]
struct BinderLevel {
/// The range of distances from the root of what's
/// being printed, to the lifetimes in a binder.
Expand All @@ -98,6 +101,15 @@ struct BinderLevel {
lifetime_depths: Range<u32>,
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct GrammarFeatureLevel(usize);

impl GrammarFeatureLevel {
const BASE_LINE: GrammarFeatureLevel = GrammarFeatureLevel(0);
const COMPLEX_CONST_GENERICS: GrammarFeatureLevel = GrammarFeatureLevel(1);
}

#[derive(Clone)]
struct SymbolMangler<'tcx> {
tcx: TyCtxt<'tcx>,
binders: Vec<BinderLevel>,
Expand All @@ -109,6 +121,8 @@ struct SymbolMangler<'tcx> {
paths: FxHashMap<(DefId, &'tcx [GenericArg<'tcx>]), usize>,
types: FxHashMap<Ty<'tcx>, usize>,
consts: FxHashMap<ty::Const<'tcx>, usize>,

wrapper_level: GrammarFeatureLevel,
}

impl<'tcx> SymbolMangler<'tcx> {
Expand Down Expand Up @@ -192,6 +206,75 @@ impl<'tcx> SymbolMangler<'tcx> {

Ok(())
}

fn required_feature_level_for_const(&self, ct: ty::Const<'tcx>) -> GrammarFeatureLevel {
let ct = ct.normalize(self.tcx, ty::ParamEnv::reveal_all());
match ct.kind() {
ty::ConstKind::Value(_) => {}

ty::ConstKind::Unevaluated(_)
| ty::ConstKind::Expr(_)
| ty::ConstKind::Param(_)
| ty::ConstKind::Infer(_)
| ty::ConstKind::Bound(..)
| ty::ConstKind::Placeholder(_)
| ty::ConstKind::Error(_) => {
return GrammarFeatureLevel::BASE_LINE;
}
}

let ty = ct.ty();

match ty.kind() {
ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => GrammarFeatureLevel::BASE_LINE,
_ => GrammarFeatureLevel::COMPLEX_CONST_GENERICS,
}
}

fn wrapped(
&mut self,
level: GrammarFeatureLevel,
f: &mut dyn FnMut(&mut Self) -> Result<(), PrintError>,
) -> Result<(), PrintError> {
if self.wrapper_level >= level {
return f(self);
}

self.out.push('C');
let backup = self.clone();

let mut digit_count = 2;

for _iteration in 0..10 {
self.wrapper_level = level;
let digit_range = self.out.len()..self.out.len() + digit_count;
for _ in 0..digit_count {
self.out.push('#');
}

self.out.push('_');

let start = self.out.len();

f(self)?;

let end = self.out.len();
let fragment_length = format!("{}", end - start);

// FIXME: Add check if any back references to interior were made. If
// not, we can just shift everything without remangling.
if fragment_length.len() == digit_count {
self.out.replace_range(digit_range, &fragment_length);
self.wrapper_level = backup.wrapper_level;
return Ok(());
}

*self = backup.clone();
digit_count = fragment_length.len();
}

Err(PrintError::default())
}
}

impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
Expand Down Expand Up @@ -813,8 +896,12 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
ty.print(self)?;
}
GenericArgKind::Const(c) => {
self.push("K");
c.print(self)?;
let required_feature_level = self.required_feature_level_for_const(c);

self.wrapped(required_feature_level, &mut |this| {
this.push("K");
c.print(this)
})?;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/coverage-dump/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ leb128 = "0.2.5"
md5 = { package = "md-5" , version = "0.10.5" }
miniz_oxide = "0.7.1"
regex = "1.8.4"
rustc-demangle = "0.1.23"
rustc-demangle = { git = "https://github.com/michaelwoerister/rustc-demangle.git", branch = "skip_unknown" }
12 changes: 6 additions & 6 deletions tests/ui/symbol-names/const-generics-str-demangling.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: symbol-name(_RMCsCRATE_HASH_1cINtB<REF>_3StrKRe616263_E)
error: symbol-name(_RMCsCRATE_HASH_1cINtB<REF>_3StrC10_KRe616263_E)
--> $DIR/const-generics-str-demangling.rs:9:1
|
LL | #[rustc_symbol_name]
Expand All @@ -16,7 +16,7 @@ error: demangling-alt(<c::Str<"abc">>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs_CsCRATE_HASH_1cINtB<REF>_3StrKRe27_E)
error: symbol-name(_RMs_CsCRATE_HASH_1cINtB<REF>_3StrC6_KRe27_E)
--> $DIR/const-generics-str-demangling.rs:15:1
|
LL | #[rustc_symbol_name]
Expand All @@ -34,7 +34,7 @@ error: demangling-alt(<c::Str<"'">>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB<REF>_3StrKRe090a_E)
error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB<REF>_3StrC8_KRe090a_E)
--> $DIR/const-generics-str-demangling.rs:21:1
|
LL | #[rustc_symbol_name]
Expand All @@ -52,7 +52,7 @@ error: demangling-alt(<c::Str<"\t\n">>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB<REF>_3StrKRee28882c3bc_E)
error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB<REF>_3StrC14_KRee28882c3bc_E)
--> $DIR/const-generics-str-demangling.rs:27:1
|
LL | #[rustc_symbol_name]
Expand All @@ -70,7 +70,7 @@ error: demangling-alt(<c::Str<"∂ü">>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs2_CsCRATE_HASH_1cINtB<REF>_3StrKRee183a1e18390e183ade1839be18394e1839ae18390e183935fe18392e18394e1839be183a0e18398e18394e1839ae183985fe183a1e18390e18393e18398e1839ae18398_E)
error: symbol-name(_RMs2_CsCRATE_HASH_1cINtB<REF>_3StrC140_KRee183a1e18390e183ade1839be18394e1839ae18390e183935fe18392e18394e1839be183a0e18398e18394e1839ae183985fe183a1e18390e18393e18398e1839ae18398_E)
--> $DIR/const-generics-str-demangling.rs:33:1
|
LL | #[rustc_symbol_name]
Expand All @@ -88,7 +88,7 @@ error: demangling-alt(<c::Str<"საჭმელად_გემრიელი
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs3_CsCRATE_HASH_1cINtB<REF>_3StrKRef09f908af09fa688f09fa686f09f90ae20c2a720f09f90b6f09f9192e29895f09f94a520c2a720f09fa7a1f09f929bf09f929af09f9299f09f929c_E)
error: symbol-name(_RMs3_CsCRATE_HASH_1cINtB<REF>_3StrC122_KRef09f908af09fa688f09fa686f09f90ae20c2a720f09f90b6f09f9192e29895f09f94a520c2a720f09fa7a1f09f929bf09f929af09f9299f09f929c_E)
--> $DIR/const-generics-str-demangling.rs:39:1
|
LL | #[rustc_symbol_name]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: symbol-name(_RMCsCRATE_HASH_1cINtB<REF>_7RefByteKRh7b_E)
error: symbol-name(_RMCsCRATE_HASH_1cINtB<REF>_7RefByteC6_KRh7b_E)
--> $DIR/const-generics-structural-demangling.rs:13:1
|
LL | #[rustc_symbol_name]
Expand All @@ -16,7 +16,7 @@ error: demangling-alt(<c::RefByte<{&123}>>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs_CsCRATE_HASH_1cINtB<REF>_6RefZstKRAEE)
error: symbol-name(_RMs_CsCRATE_HASH_1cINtB<REF>_6RefZstC4_KRAEE)
--> $DIR/const-generics-structural-demangling.rs:23:1
|
LL | #[rustc_symbol_name]
Expand All @@ -34,7 +34,7 @@ error: demangling-alt(<c::RefZst<{&[]}>>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB<REF>_11Array3BytesKAh1_h2_h3_EE)
error: symbol-name(_RMs0_CsCRATE_HASH_1cINtB<REF>_11Array3BytesC12_KAh1_h2_h3_EE)
--> $DIR/const-generics-structural-demangling.rs:31:1
|
LL | #[rustc_symbol_name]
Expand All @@ -52,7 +52,7 @@ error: demangling-alt(<c::Array3Bytes<{[1, 2, 3]}>>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB<REF>_13TupleByteBoolKTh1_b0_EE)
error: symbol-name(_RMs1_CsCRATE_HASH_1cINtB<REF>_13TupleByteBoolC9_KTh1_b0_EE)
--> $DIR/const-generics-structural-demangling.rs:39:1
|
LL | #[rustc_symbol_name]
Expand All @@ -70,7 +70,7 @@ error: demangling-alt(<c::TupleByteBool<{(1, false)}>>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs2_CsCRATE_HASH_1cINtB<REF>_11OptionUsizeKVNtINtB<REF>_8MyOptionjE4NoneUE)
error: symbol-name(_RMs2_CsCRATE_HASH_1cINtB<REF>_11OptionUsizeC27_KVNtINtB<REF>_8MyOptionjE4NoneUE)
--> $DIR/const-generics-structural-demangling.rs:55:1
|
LL | #[rustc_symbol_name]
Expand All @@ -88,7 +88,7 @@ error: demangling-alt(<c::OptionUsize<{c::MyOption::<usize>::None}>>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs3_CsCRATE_HASH_1cINtB<REF>_11OptionUsizeKVNtINtB<REF>_8MyOptionjE4SomeTj0_EE)
error: symbol-name(_RMs3_CsCRATE_HASH_1cINtB<REF>_11OptionUsizeC31_KVNtINtB<REF>_8MyOptionjE4SomeTj0_EE)
--> $DIR/const-generics-structural-demangling.rs:63:1
|
LL | #[rustc_symbol_name]
Expand All @@ -106,7 +106,7 @@ error: demangling-alt(<c::OptionUsize<{c::MyOption::<usize>::Some(0)}>>)
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMs4_CsCRATE_HASH_1cINtB<REF>_4Foo_KVNtB<REF>_3FooS1sRe616263_2chc78_5sliceRAh1_h2_h3_EEE)
error: symbol-name(_RMs4_CsCRATE_HASH_1cINtB<REF>_4Foo_C49_KVNtB<REF>_3FooS1sRe616263_2chc78_5sliceRAh1_h2_h3_EEE)
--> $DIR/const-generics-structural-demangling.rs:77:1
|
LL | #[rustc_symbol_name]
Expand All @@ -124,7 +124,7 @@ error: demangling-alt(<c::Foo_<{c::Foo { s: "abc", ch: 'x', slice: &[1, 2, 3] }}
LL | #[rustc_symbol_name]
| ^^^^^^^^^^^^^^^^^^^^

error: symbol-name(_RMsd_CsCRATE_HASH_1cINtB<REF>_4Bar_KVNtB<REF>_3BarS1xh7b_s_1xt1000_EE)
error: symbol-name(_RMsd_CsCRATE_HASH_1cINtB<REF>_4Bar_C29_KVNtB<REF>_3BarS1xh7b_s_1xt1000_EE)
--> $DIR/const-generics-structural-demangling.rs:93:5
|
LL | #[rustc_symbol_name]
Expand Down

0 comments on commit 17c7fc4

Please sign in to comment.