1use std::fmt::{self, Write};
2use std::mem::{self, discriminant};
3
4use rustc_data_structures::stable_hash::{StableHash, StableHasher};
5use rustc_hashes::Hash64;
6use rustc_hir::def_id::{CrateNum, DefId};
7use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
8use rustc_middle::bug;
9use rustc_middle::ty::print::{PrettyPrinter, Print, PrintError, Printer};
10use rustc_middle::ty::{
11 self, GenericArg, GenericArgKind, Instance, ReifyReason, Ty, TyCtxt, TypeVisitableExt,
12 Unnormalized,
13};
14use tracing::debug;
15
16pub(super) fn mangle<'tcx>(
17 tcx: TyCtxt<'tcx>,
18 instance: Instance<'tcx>,
19 instantiating_crate: Option<CrateNum>,
20) -> String {
21 let def_id = instance.def_id();
22
23 let mut ty_def_id = def_id;
28 let instance_ty;
29 loop {
30 let key = tcx.def_key(ty_def_id);
31 match key.disambiguated_data.data {
32 DefPathData::TypeNs(_)
33 | DefPathData::ValueNs(_)
34 | DefPathData::Closure
35 | DefPathData::SyntheticCoroutineBody => {
36 instance_ty = tcx.type_of(ty_def_id).instantiate_identity().skip_norm_wip();
37 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_symbol_mangling/src/legacy.rs:37",
"rustc_symbol_mangling::legacy", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_symbol_mangling/src/legacy.rs"),
::tracing_core::__macro_support::Option::Some(37u32),
::tracing_core::__macro_support::Option::Some("rustc_symbol_mangling::legacy"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("instance_ty")
}> =
::tracing::__macro_support::FieldName::new("instance_ty");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&instance_ty)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(?instance_ty);
38 break;
39 }
40 _ => {
41 ty_def_id.index = key.parent.unwrap_or_else(|| {
45 ::rustc_middle::util::bug::bug_fmt(format_args!("finding type for {0:?}, encountered def-id {1:?} with no parent",
def_id, ty_def_id));bug!(
46 "finding type for {:?}, encountered def-id {:?} with no \
47 parent",
48 def_id,
49 ty_def_id
50 );
51 });
52 }
53 }
54 }
55
56 let instance_ty = tcx.erase_and_anonymize_regions(instance_ty);
59
60 let hash = get_symbol_hash(tcx, instance, instance_ty, instantiating_crate);
61
62 let mut p = LegacySymbolMangler { tcx, path: SymbolPath::new(), keep_within_component: false };
63 p.print_def_path(
64 def_id,
65 if let ty::InstanceKind::Shim(ty::ShimKind::DropGlue(_, _))
66 | ty::InstanceKind::Shim(ty::ShimKind::AsyncDropGlueCtor(_, _))
67 | ty::InstanceKind::Shim(ty::ShimKind::FutureDropPoll(_, _, _)) = instance.def
68 {
69 &*instance.args
71 } else if let ty::InstanceKind::Shim(ty::ShimKind::AsyncDropGlue(_, ty)) = instance.def {
72 let ty::Coroutine(_, cor_args) = ty.kind() else {
73 ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
74 };
75 let drop_ty = cor_args.first().unwrap().expect_ty();
76 tcx.mk_args(&[GenericArg::from(drop_ty)])
77 } else {
78 &[]
79 },
80 )
81 .unwrap();
82
83 match instance.def {
84 ty::InstanceKind::Shim(ty::ShimKind::ThreadLocal(..)) => {
85 p.write_str("{{tls-shim}}").unwrap();
86 }
87 ty::InstanceKind::Shim(ty::ShimKind::VTable(..)) => {
88 p.write_str("{{vtable-shim}}").unwrap();
89 }
90 ty::InstanceKind::Shim(ty::ShimKind::Reify(_, reason)) => {
91 p.write_str("{{reify-shim").unwrap();
92 match reason {
93 Some(ReifyReason::FnPtr) => p.write_str("-fnptr").unwrap(),
94 Some(ReifyReason::Vtable) => p.write_str("-vtable").unwrap(),
95 None => (),
96 }
97 p.write_str("}}").unwrap();
98 }
99 ty::InstanceKind::Shim(ty::ShimKind::ConstructCoroutineInClosure {
102 receiver_by_ref,
103 ..
104 }) => {
105 p.write_str(if receiver_by_ref { "{{by-move-shim}}" } else { "{{by-ref-shim}}" })
106 .unwrap();
107 }
108 _ => {}
109 }
110
111 if let ty::InstanceKind::Shim(ty::ShimKind::FutureDropPoll(..)) = instance.def {
112 let _ = p.write_str("{{drop-shim}}");
113 }
114
115 p.path.finish(hash)
116}
117
118fn get_symbol_hash<'tcx>(
119 tcx: TyCtxt<'tcx>,
120
121 instance: Instance<'tcx>,
123
124 item_type: Ty<'tcx>,
129
130 instantiating_crate: Option<CrateNum>,
131) -> Hash64 {
132 let def_id = instance.def_id();
133 let args = instance.args;
134 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_symbol_mangling/src/legacy.rs:134",
"rustc_symbol_mangling::legacy", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_symbol_mangling/src/legacy.rs"),
::tracing_core::__macro_support::Option::Some(134u32),
::tracing_core::__macro_support::Option::Some("rustc_symbol_mangling::legacy"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("get_symbol_hash(def_id={0:?}, parameters={1:?})",
def_id, args) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("get_symbol_hash(def_id={:?}, parameters={:?})", def_id, args);
135
136 tcx.with_stable_hashing_context(|mut hcx| {
137 let mut hasher = StableHasher::new();
138
139 tcx.def_path_hash(def_id).stable_hash(&mut hcx, &mut hasher);
143
144 if !!item_type.has_erasable_regions() {
::core::panicking::panic("assertion failed: !item_type.has_erasable_regions()")
};assert!(!item_type.has_erasable_regions());
148 hcx.while_hashing_spans(false, |hcx| {
149 item_type.stable_hash(hcx, &mut hasher);
150
151 if let ty::FnDef(..) = item_type.kind() {
155 item_type.fn_sig(tcx).stable_hash(hcx, &mut hasher);
156 }
157
158 args.stable_hash(hcx, &mut hasher);
160
161 if let Some(instantiating_crate) = instantiating_crate {
162 tcx.stable_crate_id(instantiating_crate).stable_hash(hcx, &mut hasher);
163 }
164
165 discriminant(&instance.def).stable_hash(hcx, &mut hasher);
169 });
170
171 hasher.finish::<Hash64>()
173 })
174}
175
176#[derive(#[automatically_derived]
impl ::core::fmt::Debug for SymbolPath {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "SymbolPath",
"result", &self.result, "temp_buf", &&self.temp_buf)
}
}Debug)]
190struct SymbolPath {
191 result: String,
192 temp_buf: String,
193}
194
195impl SymbolPath {
196 fn new() -> Self {
197 let mut result =
198 SymbolPath { result: String::with_capacity(64), temp_buf: String::with_capacity(16) };
199 result.result.push_str("_ZN"); result
201 }
202
203 fn finalize_pending_component(&mut self) {
204 if !self.temp_buf.is_empty() {
205 let _ = self.result.write_fmt(format_args!("{0}{1}", self.temp_buf.len(),
self.temp_buf))write!(self.result, "{}{}", self.temp_buf.len(), self.temp_buf);
206 self.temp_buf.clear();
207 }
208 }
209
210 fn finish(mut self, hash: Hash64) -> String {
211 self.finalize_pending_component();
212 let _ = self.result.write_fmt(format_args!("17h{0:016x}E", hash))write!(self.result, "17h{hash:016x}E");
214 self.result
215 }
216}
217
218struct LegacySymbolMangler<'tcx> {
219 tcx: TyCtxt<'tcx>,
220 path: SymbolPath,
221
222 keep_within_component: bool,
227}
228
229impl<'tcx> Printer<'tcx> for LegacySymbolMangler<'tcx> {
234 fn tcx(&self) -> TyCtxt<'tcx> {
235 self.tcx
236 }
237
238 fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
239 Ok(())
243 }
244
245 fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
246 match *ty.kind() {
247 ty::Alias(
249 _,
250 ty::AliasTy {
251 kind: ty::Projection { def_id } | ty::Opaque { def_id }, args, ..
252 },
253 )
254 | ty::Closure(def_id, args)
255 | ty::CoroutineClosure(def_id, args)
256 | ty::Coroutine(def_id, args) => self.print_def_path(def_id, args),
257
258 ty::FnDef(def_id, args) => self.print_def_path(def_id, args.no_bound_vars().unwrap()),
259
260 ty::Array(ty, size) => {
263 self.write_str("[")?;
264 self.print_type(ty)?;
265 self.write_str("; ")?;
266 if let Some(size) = size.try_to_target_usize(self.tcx()) {
267 self.write_fmt(format_args!("{0}", size))write!(self, "{size}")?
268 } else if let ty::ConstKind::Param(param) = size.kind() {
269 param.print(self)?
270 } else {
271 self.write_str("_")?
272 }
273 self.write_str("]")?;
274 Ok(())
275 }
276
277 ty::Alias(_, ty::AliasTy { kind: ty::Inherent { .. }, .. }) => {
278 {
::core::panicking::panic_fmt(format_args!("unexpected inherent projection"));
}panic!("unexpected inherent projection")
279 }
280
281 _ => self.pretty_print_type(ty),
282 }
283 }
284
285 fn print_dyn_existential(
286 &mut self,
287 predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
288 ) -> Result<(), PrintError> {
289 let mut first = true;
290 for p in predicates {
291 if !first {
292 self.write_fmt(format_args!("+"))write!(self, "+")?;
293 }
294 first = false;
295 p.print(self)?;
296 }
297 Ok(())
298 }
299
300 fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
301 match ct.kind() {
303 ty::ConstKind::Value(cv) if cv.ty.is_integral() => {
304 let scalar = cv.to_leaf();
307 let signed = #[allow(non_exhaustive_omitted_patterns)] match cv.ty.kind() {
ty::Int(_) => true,
_ => false,
}matches!(cv.ty.kind(), ty::Int(_));
308 self.write_fmt(format_args!("{0:#?}",
ty::ConstInt::new(scalar, signed, cv.ty.is_ptr_sized_integral())))write!(
309 self,
310 "{:#?}",
311 ty::ConstInt::new(scalar, signed, cv.ty.is_ptr_sized_integral())
312 )?;
313 }
314 _ => self.write_str("_")?,
315 }
316 Ok(())
317 }
318
319 fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
320 self.write_str(self.tcx.crate_name(cnum).as_str())?;
321 Ok(())
322 }
323
324 fn print_path_with_qualified(
325 &mut self,
326 self_ty: Ty<'tcx>,
327 trait_ref: Option<ty::TraitRef<'tcx>>,
328 ) -> Result<(), PrintError> {
329 match self_ty.kind() {
332 ty::FnDef(..)
333 | ty::Alias(..)
334 | ty::Closure(..)
335 | ty::CoroutineClosure(..)
336 | ty::Coroutine(..)
337 if trait_ref.is_none() =>
338 {
339 self.print_type(self_ty)
340 }
341
342 _ => self.pretty_print_path_with_qualified(self_ty, trait_ref),
343 }
344 }
345
346 fn print_path_with_impl(
347 &mut self,
348 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
349 self_ty: Ty<'tcx>,
350 trait_ref: Option<ty::TraitRef<'tcx>>,
351 ) -> Result<(), PrintError> {
352 self.pretty_print_path_with_impl(
353 |cx| {
354 print_prefix(cx)?;
355
356 if cx.keep_within_component {
357 cx.write_str("::")?;
359 } else {
360 cx.path.finalize_pending_component();
361 }
362
363 Ok(())
364 },
365 self_ty,
366 trait_ref,
367 )
368 }
369
370 fn print_path_with_simple(
371 &mut self,
372 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
373 disambiguated_data: &DisambiguatedDefPathData,
374 ) -> Result<(), PrintError> {
375 print_prefix(self)?;
376
377 if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
379 return Ok(());
380 }
381
382 if self.keep_within_component {
383 self.write_str("::")?;
385 } else {
386 self.path.finalize_pending_component();
387 }
388
389 self.write_fmt(format_args!("{0}", disambiguated_data.data))write!(self, "{}", disambiguated_data.data)?;
390
391 Ok(())
392 }
393
394 fn print_path_with_generic_args(
395 &mut self,
396 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
397 args: &[GenericArg<'tcx>],
398 ) -> Result<(), PrintError> {
399 print_prefix(self)?;
400
401 let args =
402 args.iter().cloned().filter(|arg| !#[allow(non_exhaustive_omitted_patterns)] match arg.kind() {
GenericArgKind::Lifetime(_) => true,
_ => false,
}matches!(arg.kind(), GenericArgKind::Lifetime(_)));
403 if args.clone().next().is_some() {
404 self.generic_delimiters(|cx| cx.comma_sep(args))
405 } else {
406 Ok(())
407 }
408 }
409
410 fn print_impl_path(
411 &mut self,
412 impl_def_id: DefId,
413 args: &'tcx [GenericArg<'tcx>],
414 ) -> Result<(), PrintError> {
415 let self_ty = self.tcx.type_of(impl_def_id);
416 let impl_trait_ref = self.tcx.impl_opt_trait_ref(impl_def_id);
417 let generics = self.tcx.generics_of(impl_def_id);
418 let (typing_env, mut self_ty, mut impl_trait_ref) = if generics.count() > args.len()
432 || &args[..generics.count()]
433 == self
434 .tcx
435 .erase_and_anonymize_regions(ty::GenericArgs::identity_for_item(
436 self.tcx,
437 impl_def_id,
438 ))
439 .as_slice()
440 {
441 (
442 ty::TypingEnv::post_analysis(self.tcx, impl_def_id),
443 self_ty.instantiate_identity().skip_norm_wip(),
444 impl_trait_ref
445 .map(|impl_trait_ref| impl_trait_ref.instantiate_identity().skip_norm_wip()),
446 )
447 } else {
448 if !!args.has_non_region_param() {
{
::core::panicking::panic_fmt(format_args!("should not be mangling partially substituted polymorphic instance: {0:?} {1:?}",
impl_def_id, args));
}
};assert!(
449 !args.has_non_region_param(),
450 "should not be mangling partially substituted \
451 polymorphic instance: {impl_def_id:?} {args:?}"
452 );
453 (
454 ty::TypingEnv::fully_monomorphized(),
455 self_ty.instantiate(self.tcx, args).skip_norm_wip(),
456 impl_trait_ref.map(|impl_trait_ref| {
457 impl_trait_ref.instantiate(self.tcx, args).skip_norm_wip()
458 }),
459 )
460 };
461
462 match &mut impl_trait_ref {
463 Some(impl_trait_ref) => {
464 {
match (&impl_trait_ref.self_ty(), &self_ty) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};assert_eq!(impl_trait_ref.self_ty(), self_ty);
465 *impl_trait_ref = self
466 .tcx
467 .normalize_erasing_regions(typing_env, Unnormalized::new_wip(*impl_trait_ref));
468 self_ty = impl_trait_ref.self_ty();
469 }
470 None => {
471 self_ty =
472 self.tcx.normalize_erasing_regions(typing_env, Unnormalized::new_wip(self_ty));
473 }
474 }
475
476 self.default_print_impl_path(impl_def_id, self_ty, impl_trait_ref)
477 }
478}
479
480impl<'tcx> PrettyPrinter<'tcx> for LegacySymbolMangler<'tcx> {
481 fn should_print_optional_region(&self, _region: ty::Region<'_>) -> bool {
482 false
483 }
484
485 fn comma_sep<T>(&mut self, mut elems: impl Iterator<Item = T>) -> Result<(), PrintError>
487 where
488 T: Print<Self>,
489 {
490 if let Some(first) = elems.next() {
491 first.print(self)?;
492 for elem in elems {
493 self.write_str(",")?;
494 elem.print(self)?;
495 }
496 }
497 Ok(())
498 }
499
500 fn generic_delimiters(
501 &mut self,
502 f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
503 ) -> Result<(), PrintError> {
504 self.write_fmt(format_args!("<"))write!(self, "<")?;
505
506 let kept_within_component = mem::replace(&mut self.keep_within_component, true);
507 f(self)?;
508 self.keep_within_component = kept_within_component;
509
510 self.write_fmt(format_args!(">"))write!(self, ">")?;
511
512 Ok(())
513 }
514}
515
516impl fmt::Write for LegacySymbolMangler<'_> {
517 fn write_str(&mut self, s: &str) -> fmt::Result {
518 for c in s.chars() {
525 if self.path.temp_buf.is_empty() {
526 match c {
527 'a'..='z' | 'A'..='Z' | '_' => {}
528 _ => {
529 self.path.temp_buf.push('_');
531 }
532 }
533 }
534 match c {
535 '@' => self.path.temp_buf.push_str("$SP$"),
537 '*' => self.path.temp_buf.push_str("$BP$"),
538 '&' => self.path.temp_buf.push_str("$RF$"),
539 '<' => self.path.temp_buf.push_str("$LT$"),
540 '>' => self.path.temp_buf.push_str("$GT$"),
541 '(' => self.path.temp_buf.push_str("$LP$"),
542 ')' => self.path.temp_buf.push_str("$RP$"),
543 ',' => self.path.temp_buf.push_str("$C$"),
544
545 '-' | ':' | '.' if self.tcx.has_strict_asm_symbol_naming() => {
546 self.path.temp_buf.push('$')
548 }
549
550 '-' | ':' => self.path.temp_buf.push('.'),
553
554 'm' if self.path.temp_buf.ends_with(".llv") => self.path.temp_buf.push_str("$u6d$"),
556
557 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '.' | '$' => self.path.temp_buf.push(c),
559
560 _ => {
561 self.path.temp_buf.push('$');
562 for c in c.escape_unicode().skip(1) {
563 match c {
564 '{' => {}
565 '}' => self.path.temp_buf.push('$'),
566 c => self.path.temp_buf.push(c),
567 }
568 }
569 }
570 }
571 }
572
573 Ok(())
574 }
575}