Debian Codegraph @

third-party/rust-demangle/test-harness/tests/legacy.rs

raw
1 //! Tests copied from `https://github.com/rust-lang/rustc-demangle`'s
2 //! `src/legacy.rs` at `fd906f850f90f6d4845c7b8219d218293e0ab3ed`.
3 //!
4 //! These are the only changes made to the tests:
5 //! * `::` absolute paths -> `rust_demangle_c_test_harness::`
6 //! * `#[should_panic]` was added to tests that don't pass yet
7
8 macro_rules! t {
9 ($a:expr, $b:expr) => {
10 assert!(ok($a, $b))
11 };
12 }
13
14 macro_rules! t_err {
15 ($a:expr) => {
16 assert!(ok_err($a))
17 };
18 }
19
20 macro_rules! t_nohash {
21 ($a:expr, $b:expr) => {{
22 assert_eq!(
23 format!("{:#}", rust_demangle_c_test_harness::demangle($a)),
24 $b
25 );
26 }};
27 }
28
29 fn ok(sym: &str, expected: &str) -> bool {
30 match rust_demangle_c_test_harness::try_demangle(sym) {
31 Ok(s) => {
32 if s.to_string() == expected {
33 true
34 } else {
35 println!("\n{}\n!=\n{}\n", s, expected);
36 false
37 }
38 }
39 Err(_) => {
40 println!("error demangling");
41 false
42 }
43 }
44 }
45
46 fn ok_err(sym: &str) -> bool {
47 match rust_demangle_c_test_harness::try_demangle(sym) {
48 Ok(_) => {
49 println!("succeeded in demangling");
50 false
51 }
52 Err(_) => rust_demangle_c_test_harness::demangle(sym).to_string() == sym,
53 }
54 }
55
56 #[test]
57 fn demangle() {
58 t_err!("test");
59 t!("_ZN4testE", "test");
60 t_err!("_ZN4test");
61 t!("_ZN4test1a2bcE", "test::a::bc");
62 }
63
64 #[test]
65 fn demangle_dollars() {
66 t!("_ZN4$RP$E", ")");
67 t!("_ZN8$RF$testE", "&test");
68 t!("_ZN8$BP$test4foobE", "*test::foob");
69 t!("_ZN9$u20$test4foobE", " test::foob");
70 t!("_ZN35Bar$LT$$u5b$u32$u3b$$u20$4$u5d$$GT$E", "Bar<[u32; 4]>");
71 }
72
73 #[test]
74 fn demangle_many_dollars() {
75 t!("_ZN13test$u20$test4foobE", "test test::foob");
76 t!("_ZN12test$BP$test4foobE", "test*test::foob");
77 }
78
79 #[test]
80 fn demangle_osx() {
81 t!(
82 "__ZN5alloc9allocator6Layout9for_value17h02a996811f781011E",
83 "alloc::allocator::Layout::for_value::h02a996811f781011"
84 );
85 t!("__ZN38_$LT$core..option..Option$LT$T$GT$$GT$6unwrap18_MSG_FILE_LINE_COL17haf7cb8d5824ee659E", "<core::option::Option<T>>::unwrap::_MSG_FILE_LINE_COL::haf7cb8d5824ee659");
86 t!("__ZN4core5slice89_$LT$impl$u20$core..iter..traits..IntoIterator$u20$for$u20$$RF$$u27$a$u20$$u5b$T$u5d$$GT$9into_iter17h450e234d27262170E", "core::slice::<impl core::iter::traits::IntoIterator for &'a [T]>::into_iter::h450e234d27262170");
87 }
88
89 #[test]
90 fn demangle_windows() {
91 t!("ZN4testE", "test");
92 t!("ZN13test$u20$test4foobE", "test test::foob");
93 t!("ZN12test$RF$test4foobE", "test&test::foob");
94 }
95
96 #[test]
97 fn demangle_elements_beginning_with_underscore() {
98 t!("_ZN13_$LT$test$GT$E", "<test>");
99 t!("_ZN28_$u7b$$u7b$closure$u7d$$u7d$E", "{{closure}}");
100 t!("_ZN15__STATIC_FMTSTRE", "__STATIC_FMTSTR");
101 }
102
103 #[test]
104 fn demangle_trait_impls() {
105 t!(
106 "_ZN71_$LT$Test$u20$$u2b$$u20$$u27$static$u20$as$u20$foo..Bar$LT$Test$GT$$GT$3barE",
107 "<Test + 'static as foo::Bar<Test>>::bar"
108 );
109 }
110
111 #[test]
112 fn demangle_without_hash() {
113 let s = "_ZN3foo17h05af221e174051e9E";
114 t!(s, "foo::h05af221e174051e9");
115 t_nohash!(s, "foo");
116 }
117
118 #[test]
119 fn demangle_without_hash_edgecases() {
120 // One element, no hash.
121 t_nohash!("_ZN3fooE", "foo");
122 // Two elements, no hash.
123 t_nohash!("_ZN3foo3barE", "foo::bar");
124 // Longer-than-normal hash.
125 t_nohash!("_ZN3foo20h05af221e174051e9abcE", "foo");
126 // Shorter-than-normal hash.
127 t_nohash!("_ZN3foo5h05afE", "foo");
128 // Valid hash, but not at the end.
129 t_nohash!("_ZN17h05af221e174051e93fooE", "h05af221e174051e9::foo");
130 // Not a valid hash, missing the 'h'.
131 t_nohash!("_ZN3foo16ffaf221e174051e9E", "foo::ffaf221e174051e9");
132 // Not a valid hash, has a non-hex-digit.
133 t_nohash!("_ZN3foo17hg5af221e174051e9E", "foo::hg5af221e174051e9");
134 }
135
136 #[test]
137 fn demangle_thinlto() {
138 // One element, no hash.
139 t!("_ZN3fooE.llvm.9D1C9369", "foo");
140 t!("_ZN3fooE.llvm.9D1C9369@@16", "foo");
141 t_nohash!(
142 "_ZN9backtrace3foo17hbb467fcdaea5d79bE.llvm.A5310EB9",
143 "backtrace::foo"
144 );
145 }
146
147 #[test]
148 fn demangle_llvm_ir_branch_labels() {
149 t!("_ZN4core5slice77_$LT$impl$u20$core..ops..index..IndexMut$LT$I$GT$$u20$for$u20$$u5b$T$u5d$$GT$9index_mut17haf9727c2edfbc47bE.exit.i.i", "core::slice::<impl core::ops::index::IndexMut<I> for [T]>::index_mut::haf9727c2edfbc47b.exit.i.i");
150 t_nohash!("_ZN4core5slice77_$LT$impl$u20$core..ops..index..IndexMut$LT$I$GT$$u20$for$u20$$u5b$T$u5d$$GT$9index_mut17haf9727c2edfbc47bE.exit.i.i", "core::slice::<impl core::ops::index::IndexMut<I> for [T]>::index_mut.exit.i.i");
151 }
152
153 #[test]
154 fn demangle_ignores_suffix_that_doesnt_look_like_a_symbol() {
155 t_err!("_ZN3fooE.llvm moocow");
156 }
157
158 #[test]
159 fn dont_panic() {
160 rust_demangle_c_test_harness::demangle("_ZN2222222222222222222222EE").to_string();
161 rust_demangle_c_test_harness::demangle("_ZN5*70527e27.ll34csaғE").to_string();
162 rust_demangle_c_test_harness::demangle("_ZN5*70527a54.ll34_$b.1E").to_string();
163 rust_demangle_c_test_harness::demangle(
164 "\
165 _ZN5~saäb4e\n\
166 2734cOsbE\n\
167 5usage20h)3\0\0\0\0\0\0\07e2734cOsbE\
168 ",
169 )
170 .to_string();
171 }
172
173 #[test]
174 fn invalid_no_chop() {
175 t_err!("_ZNfooE");
176 }
177
178 #[test]
179 fn handle_assoc_types() {
180 t!("_ZN151_$LT$alloc..boxed..Box$LT$alloc..boxed..FnBox$LT$A$C$$u20$Output$u3d$R$GT$$u20$$u2b$$u20$$u27$a$GT$$u20$as$u20$core..ops..function..FnOnce$LT$A$GT$$GT$9call_once17h69e8f44b3723e1caE", "<alloc::boxed::Box<alloc::boxed::FnBox<A, Output=R> + 'a> as core::ops::function::FnOnce<A>>::call_once::h69e8f44b3723e1ca");
181 }
182
183 #[test]
184 fn handle_bang() {
185 t!(
186 "_ZN88_$LT$core..result..Result$LT$$u21$$C$$u20$E$GT$$u20$as$u20$std..process..Termination$GT$6report17hfc41d0da4a40b3e8E",
187 "<core::result::Result<!, E> as std::process::Termination>::report::hfc41d0da4a40b3e8"
188 );
189 }
190
191 #[test]
192 fn demangle_utf8_idents() {
193 t_nohash!(
194 "_ZN11utf8_idents157_$u10e1$$u10d0$$u10ed$$u10db$$u10d4$$u10da$$u10d0$$u10d3$_$u10d2$$u10d4$$u10db$$u10e0$$u10d8$$u10d4$$u10da$$u10d8$_$u10e1$$u10d0$$u10d3$$u10d8$$u10da$$u10d8$17h21634fd5714000aaE",
195 "utf8_idents::საჭმელად_გემრიელი_სადილი"
196 );
197 }
198
199 #[test]
200 fn demangle_issue_60925() {
201 t_nohash!(
202 "_ZN11issue_609253foo37Foo$LT$issue_60925..llv$u6d$..Foo$GT$3foo17h059a991a004536adE",
203 "issue_60925::foo::Foo<issue_60925::llvm::Foo>::foo"
204 );
205 }