1use std::fmt::Write;
2use std::hash::Hasher;
3use std::iter;
4use std::ops::Range;
5
6use rustc_abi::{ExternAbi, Integer};
7use rustc_data_structures::base_n::ToBaseN;
8use rustc_data_structures::fx::FxHashMap;
9use rustc_data_structures::intern::Interned;
10use rustc_data_structures::stable_hasher::StableHasher;
11use rustc_hashes::Hash64;
12use rustc_hir as hir;
13use rustc_hir::def::CtorKind;
14use rustc_hir::def_id::{CrateNum, DefId};
15use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
16use rustc_middle::bug;
17use rustc_middle::ty::layout::IntegerExt;
18use rustc_middle::ty::print::{Print, PrintError, Printer};
19use rustc_middle::ty::{
20 self, FloatTy, GenericArg, GenericArgKind, Instance, IntTy, ReifyReason, Ty, TyCtxt,
21 TypeVisitable, TypeVisitableExt, UintTy, Unnormalized,
22};
23use rustc_span::sym;
24
25pub(super) fn mangle<'tcx>(
26 tcx: TyCtxt<'tcx>,
27 instance: Instance<'tcx>,
28 instantiating_crate: Option<CrateNum>,
29 is_exportable: bool,
30) -> String {
31 let def_id = instance.def_id();
32 let args = tcx.normalize_erasing_regions(
34 ty::TypingEnv::fully_monomorphized(),
35 Unnormalized::new_wip(instance.args),
36 );
37
38 let prefix = "_R";
39 let mut p: V0SymbolMangler<'_> = V0SymbolMangler {
40 tcx,
41 start_offset: prefix.len(),
42 is_exportable,
43 paths: FxHashMap::default(),
44 types: FxHashMap::default(),
45 consts: FxHashMap::default(),
46 binders: ::alloc::vec::Vec::new()vec![],
47 out: String::from(prefix),
48 };
49
50 let shim_kind = match instance.def {
52 ty::InstanceKind::ThreadLocalShim(_) => Some("tls"),
53 ty::InstanceKind::VTableShim(_) => Some("vtable"),
54 ty::InstanceKind::ReifyShim(_, None) => Some("reify"),
55 ty::InstanceKind::ReifyShim(_, Some(ReifyReason::FnPtr)) => Some("reify_fnptr"),
56 ty::InstanceKind::ReifyShim(_, Some(ReifyReason::Vtable)) => Some("reify_vtable"),
57
58 ty::InstanceKind::ConstructCoroutineInClosureShim { receiver_by_ref: true, .. } => {
61 Some("by_move")
62 }
63 ty::InstanceKind::ConstructCoroutineInClosureShim { receiver_by_ref: false, .. } => {
64 Some("by_ref")
65 }
66 ty::InstanceKind::FutureDropPollShim(_, _, _) => Some("drop"),
67 _ => None,
68 };
69
70 if let ty::InstanceKind::AsyncDropGlue(_, ty) = instance.def {
71 let ty::Coroutine(_, cor_args) = ty.kind() else {
72 ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
73 };
74 let drop_ty = cor_args.first().unwrap().expect_ty();
75 p.print_def_path(def_id, tcx.mk_args(&[GenericArg::from(drop_ty)])).unwrap()
76 } else if let Some(shim_kind) = shim_kind {
77 p.path_append_ns(|p| p.print_def_path(def_id, args), 'S', 0, shim_kind).unwrap()
78 } else {
79 p.print_def_path(def_id, args).unwrap()
80 };
81 if let Some(instantiating_crate) = instantiating_crate {
82 p.print_def_path(instantiating_crate.as_def_id(), &[]).unwrap();
83 }
84 std::mem::take(&mut p.out)
85}
86
87pub fn mangle_internal_symbol<'tcx>(tcx: TyCtxt<'tcx>, item_name: &str) -> String {
88 match item_name {
89 "rust_eh_personality" => return item_name.to_owned(),
91 "__isPlatformVersionAtLeast" | "__isOSVersionAtLeast" => return item_name.to_owned(),
94 _ => {}
95 }
96
97 let prefix = "_R";
98 let mut p: V0SymbolMangler<'_> = V0SymbolMangler {
99 tcx,
100 start_offset: prefix.len(),
101 is_exportable: false,
102 paths: FxHashMap::default(),
103 types: FxHashMap::default(),
104 consts: FxHashMap::default(),
105 binders: ::alloc::vec::Vec::new()vec![],
106 out: String::from(prefix),
107 };
108
109 p.path_append_ns(
110 |p| {
111 p.push("C");
112 p.push_disambiguator({
113 let mut hasher = StableHasher::new();
114 hasher.write(tcx.sess.cfg_version.as_bytes());
120
121 let hash: Hash64 = hasher.finish();
122 hash.as_u64()
123 });
124 p.push_ident("__rustc");
125 Ok(())
126 },
127 'v',
128 0,
129 item_name,
130 )
131 .unwrap();
132
133 std::mem::take(&mut p.out)
134}
135
136pub(super) fn mangle_typeid_for_trait_ref<'tcx>(
137 tcx: TyCtxt<'tcx>,
138 trait_ref: ty::ExistentialTraitRef<'tcx>,
139) -> String {
140 let mut p = V0SymbolMangler {
142 tcx,
143 start_offset: 0,
144 is_exportable: false,
145 paths: FxHashMap::default(),
146 types: FxHashMap::default(),
147 consts: FxHashMap::default(),
148 binders: ::alloc::vec::Vec::new()vec![],
149 out: String::new(),
150 };
151 p.print_def_path(trait_ref.def_id, &[]).unwrap();
152 std::mem::take(&mut p.out)
153}
154
155struct BinderLevel {
156 lifetime_depths: Range<u32>,
167}
168
169struct V0SymbolMangler<'tcx> {
170 tcx: TyCtxt<'tcx>,
171 binders: Vec<BinderLevel>,
172 out: String,
173 is_exportable: bool,
174
175 start_offset: usize,
177 paths: FxHashMap<(DefId, &'tcx [GenericArg<'tcx>]), usize>,
179 types: FxHashMap<Ty<'tcx>, usize>,
180 consts: FxHashMap<ty::Const<'tcx>, usize>,
181}
182
183impl<'tcx> V0SymbolMangler<'tcx> {
184 fn push(&mut self, s: &str) {
185 self.out.push_str(s);
186 }
187
188 fn push_integer_62(&mut self, x: u64) {
194 push_integer_62(x, &mut self.out)
195 }
196
197 fn push_opt_integer_62(&mut self, tag: &str, x: u64) {
202 if let Some(x) = x.checked_sub(1) {
203 self.push(tag);
204 self.push_integer_62(x);
205 }
206 }
207
208 fn push_disambiguator(&mut self, dis: u64) {
209 self.push_opt_integer_62("s", dis);
210 }
211
212 fn push_ident(&mut self, ident: &str) {
213 push_ident(ident, &mut self.out)
214 }
215
216 fn path_append_ns(
217 &mut self,
218 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
219 ns: char,
220 disambiguator: u64,
221 name: &str,
222 ) -> Result<(), PrintError> {
223 self.push("N");
224 self.out.push(ns);
225 print_prefix(self)?;
226 self.push_disambiguator(disambiguator);
227 self.push_ident(name);
228 Ok(())
229 }
230
231 fn print_backref(&mut self, i: usize) -> Result<(), PrintError> {
232 self.push("B");
233 self.push_integer_62((i - self.start_offset) as u64);
234 Ok(())
235 }
236
237 fn wrap_binder<T>(
238 &mut self,
239 value: &ty::Binder<'tcx, T>,
240 print_value: impl FnOnce(&mut Self, &T) -> Result<(), PrintError>,
241 ) -> Result<(), PrintError>
242 where
243 T: TypeVisitable<TyCtxt<'tcx>>,
244 {
245 let mut lifetime_depths =
246 self.binders.last().map(|b| b.lifetime_depths.end).map_or(0..0, |i| i..i);
247
248 let lifetimes = value
250 .bound_vars()
251 .iter()
252 .filter(|var| #[allow(non_exhaustive_omitted_patterns)] match var {
ty::BoundVariableKind::Region(..) => true,
_ => false,
}matches!(var, ty::BoundVariableKind::Region(..)))
253 .count() as u32;
254
255 self.push_opt_integer_62("G", lifetimes as u64);
256 lifetime_depths.end += lifetimes;
257
258 self.binders.push(BinderLevel { lifetime_depths });
259 print_value(self, value.as_ref().skip_binder())?;
260 self.binders.pop();
261
262 Ok(())
263 }
264
265 fn print_pat(&mut self, pat: ty::Pattern<'tcx>) -> Result<(), std::fmt::Error> {
266 Ok(match *pat {
267 ty::PatternKind::Range { start, end } => {
268 self.push("R");
269 self.print_const(start)?;
270 self.print_const(end)?;
271 }
272 ty::PatternKind::NotNull => {
273 self.tcx.types.unit.print(self)?;
274 }
275 ty::PatternKind::Or(patterns) => {
276 self.push("O");
277 for pat in patterns {
278 self.print_pat(pat)?;
279 }
280 self.push("E");
281 }
282 })
283 }
284}
285
286impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> {
287 fn tcx(&self) -> TyCtxt<'tcx> {
288 self.tcx
289 }
290
291 fn print_def_path(
292 &mut self,
293 def_id: DefId,
294 args: &'tcx [GenericArg<'tcx>],
295 ) -> Result<(), PrintError> {
296 if let Some(&i) = self.paths.get(&(def_id, args)) {
297 return self.print_backref(i);
298 }
299 let start = self.out.len();
300
301 self.default_print_def_path(def_id, args)?;
302
303 if !args.iter().any(|k| k.has_escaping_bound_vars()) {
306 self.paths.insert((def_id, args), start);
307 }
308 Ok(())
309 }
310
311 fn print_impl_path(
312 &mut self,
313 impl_def_id: DefId,
314 args: &'tcx [GenericArg<'tcx>],
315 ) -> Result<(), PrintError> {
316 let key = self.tcx.def_key(impl_def_id);
317 let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id };
318
319 let self_ty = self.tcx.type_of(impl_def_id);
320 let impl_trait_ref = self.tcx.impl_opt_trait_ref(impl_def_id);
321 let generics = self.tcx.generics_of(impl_def_id);
322 let (typing_env, mut self_ty, mut impl_trait_ref) = if generics.count() > args.len()
336 || &args[..generics.count()]
337 == self
338 .tcx
339 .erase_and_anonymize_regions(ty::GenericArgs::identity_for_item(
340 self.tcx,
341 impl_def_id,
342 ))
343 .as_slice()
344 {
345 (
346 ty::TypingEnv::post_analysis(self.tcx, impl_def_id),
347 self_ty.instantiate_identity().skip_norm_wip(),
348 impl_trait_ref
349 .map(|impl_trait_ref| impl_trait_ref.instantiate_identity().skip_norm_wip()),
350 )
351 } else {
352 if !(!args.has_non_region_param() && !args.has_free_regions()) {
{
::core::panicking::panic_fmt(format_args!("should not be mangling partially substituted polymorphic instance: {0:?} {1:?}",
impl_def_id, args));
}
};assert!(
353 !args.has_non_region_param() && !args.has_free_regions(),
354 "should not be mangling partially substituted \
355 polymorphic instance: {impl_def_id:?} {args:?}"
356 );
357 (
358 ty::TypingEnv::fully_monomorphized(),
359 self_ty.instantiate(self.tcx, args).skip_norm_wip(),
360 impl_trait_ref.map(|impl_trait_ref| {
361 impl_trait_ref.instantiate(self.tcx, args).skip_norm_wip()
362 }),
363 )
364 };
365
366 match &mut impl_trait_ref {
367 Some(impl_trait_ref) => {
368 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);
369 *impl_trait_ref = self
370 .tcx
371 .normalize_erasing_regions(typing_env, Unnormalized::new_wip(*impl_trait_ref));
372 self_ty = impl_trait_ref.self_ty();
373 }
374 None => {
375 self_ty =
376 self.tcx.normalize_erasing_regions(typing_env, Unnormalized::new_wip(self_ty));
377 }
378 }
379
380 self.push(match impl_trait_ref {
381 Some(_) => "X",
382 None => "M",
383 });
384
385 if impl_trait_ref.is_some() && args.iter().any(|a| a.has_non_region_param()) {
388 self.print_path_with_generic_args(
389 |this| {
390 this.path_append_ns(
391 |p| p.print_def_path(parent_def_id, &[]),
392 'I',
393 key.disambiguated_data.disambiguator as u64,
394 "",
395 )
396 },
397 args,
398 )?;
399 } else {
400 let exported_impl_order = self.tcx.stable_order_of_exportable_impls(impl_def_id.krate);
401 let disambiguator = match self.is_exportable {
402 true => exported_impl_order[&impl_def_id] as u64,
403 false => {
404 exported_impl_order.len() as u64 + key.disambiguated_data.disambiguator as u64
405 }
406 };
407 self.push_disambiguator(disambiguator);
408 self.print_def_path(parent_def_id, &[])?;
409 }
410
411 self_ty.print(self)?;
412
413 if let Some(trait_ref) = impl_trait_ref {
414 self.print_def_path(trait_ref.def_id, trait_ref.args)?;
415 }
416
417 Ok(())
418 }
419
420 fn print_region(&mut self, region: ty::Region<'_>) -> Result<(), PrintError> {
421 let i = match region.kind() {
422 ty::ReErased => 0,
425
426 ty::ReBound(
429 ty::BoundVarIndexKind::Bound(debruijn),
430 ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon },
431 ) => {
432 let binder = &self.binders[self.binders.len() - 1 - debruijn.index()];
433 let depth = binder.lifetime_depths.start + var.as_u32();
434
435 1 + (self.binders.last().unwrap().lifetime_depths.end - 1 - depth)
436 }
437
438 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("symbol_names: non-erased region `{0:?}`",
region))bug!("symbol_names: non-erased region `{:?}`", region),
439 };
440 self.push("L");
441 self.push_integer_62(i as u64);
442 Ok(())
443 }
444
445 fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
446 let basic_type = match ty.kind() {
448 ty::Bool => "b",
449 ty::Char => "c",
450 ty::Str => "e",
451 ty::Int(IntTy::I8) => "a",
452 ty::Int(IntTy::I16) => "s",
453 ty::Int(IntTy::I32) => "l",
454 ty::Int(IntTy::I64) => "x",
455 ty::Int(IntTy::I128) => "n",
456 ty::Int(IntTy::Isize) => "i",
457 ty::Uint(UintTy::U8) => "h",
458 ty::Uint(UintTy::U16) => "t",
459 ty::Uint(UintTy::U32) => "m",
460 ty::Uint(UintTy::U64) => "y",
461 ty::Uint(UintTy::U128) => "o",
462 ty::Uint(UintTy::Usize) => "j",
463 ty::Float(FloatTy::F16) => "C3f16",
464 ty::Float(FloatTy::F32) => "f",
465 ty::Float(FloatTy::F64) => "d",
466 ty::Float(FloatTy::F128) => "C4f128",
467 ty::Never => "z",
468
469 ty::Tuple(_) if ty.is_unit() => "u",
470
471 ty::Param(_) => "p",
474
475 _ => "",
476 };
477 if !basic_type.is_empty() {
478 self.push(basic_type);
479 return Ok(());
480 }
481
482 if let Some(&i) = self.types.get(&ty) {
483 return self.print_backref(i);
484 }
485 let start = self.out.len();
486
487 match *ty.kind() {
488 ty::Bool | ty::Char | ty::Str | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Never => {
490 ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
491 }
492 ty::Tuple(_) if ty.is_unit() => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
493 ty::Param(_) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
494
495 ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
496
497 ty::Ref(r, ty, mutbl) => {
498 self.push(match mutbl {
499 hir::Mutability::Not => "R",
500 hir::Mutability::Mut => "Q",
501 });
502 if !r.is_erased() {
503 r.print(self)?;
504 }
505 ty.print(self)?;
506 }
507
508 ty::RawPtr(ty, mutbl) => {
509 self.push(match mutbl {
510 hir::Mutability::Not => "P",
511 hir::Mutability::Mut => "O",
512 });
513 ty.print(self)?;
514 }
515
516 ty::Pat(ty, pat) => {
517 self.push("W");
518 ty.print(self)?;
519 self.print_pat(pat)?;
520 }
521
522 ty::Array(ty, len) => {
523 self.push("A");
524 ty.print(self)?;
525 self.print_const(len)?;
526 }
527 ty::Slice(ty) => {
528 self.push("S");
529 ty.print(self)?;
530 }
531
532 ty::Tuple(tys) => {
533 self.push("T");
534 for ty in tys.iter() {
535 ty.print(self)?;
536 }
537 self.push("E");
538 }
539
540 ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), args)
542 | ty::FnDef(def_id, args)
543 | ty::Closure(def_id, args)
544 | ty::CoroutineClosure(def_id, args)
545 | ty::Coroutine(def_id, args) => {
546 self.print_def_path(def_id, args)?;
547 }
548
549 ty::Alias(ty::AliasTy { kind: ty::Projection { def_id }, args, .. }) => {
552 self.print_def_path(def_id, args)?;
553 }
554
555 ty::Foreign(def_id) => {
556 self.print_def_path(def_id, &[])?;
557 }
558
559 ty::FnPtr(sig_tys, hdr) => {
560 let sig = sig_tys.with(hdr);
561 self.push("F");
562 self.wrap_binder(&sig, |p, sig| {
563 if sig.safety().is_unsafe() {
564 p.push("U");
565 }
566 match sig.abi() {
567 ExternAbi::Rust => {}
568 ExternAbi::C { unwind: false } => p.push("KC"),
569 abi => {
570 p.push("K");
571 let name = abi.as_str();
572 if name.contains('-') {
573 p.push_ident(&name.replace('-', "_"));
574 } else {
575 p.push_ident(name);
576 }
577 }
578 }
579 for &ty in sig.inputs() {
580 ty.print(p)?;
581 }
582 if sig.c_variadic() {
583 p.push("v");
584 }
585 p.push("E");
586 sig.output().print(p)
587 })?;
588 }
589
590 ty::UnsafeBinder(..) => ::core::panicking::panic("not yet implemented")todo!(),
592
593 ty::Dynamic(predicates, r) => {
594 self.push("D");
595 self.print_dyn_existential(predicates)?;
596 r.print(self)?;
597 }
598
599 ty::Alias(..) => ::rustc_middle::util::bug::bug_fmt(format_args!("symbol_names: unexpected alias"))bug!("symbol_names: unexpected alias"),
600 ty::CoroutineWitness(..) => ::rustc_middle::util::bug::bug_fmt(format_args!("symbol_names: unexpected `CoroutineWitness`"))bug!("symbol_names: unexpected `CoroutineWitness`"),
601 }
602
603 if !ty.has_escaping_bound_vars() {
606 self.types.insert(ty, start);
607 }
608 Ok(())
609 }
610
611 fn print_dyn_existential(
612 &mut self,
613 predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
614 ) -> Result<(), PrintError> {
615 self.wrap_binder(&predicates[0], |p, _| {
642 for predicate in predicates.iter() {
643 match predicate.as_ref().skip_binder() {
648 ty::ExistentialPredicate::Trait(trait_ref) => {
649 let dummy_self = Ty::new_fresh(p.tcx, 0);
651 let trait_ref = trait_ref.with_self_ty(p.tcx, dummy_self);
652 p.print_def_path(trait_ref.def_id, trait_ref.args)?;
653 }
654 ty::ExistentialPredicate::Projection(projection) => {
655 let name = p.tcx.associated_item(projection.def_id).name();
656 p.push("p");
657 p.push_ident(name.as_str());
658 match projection.term.kind() {
659 ty::TermKind::Ty(ty) => ty.print(p),
660 ty::TermKind::Const(c) => {
661 p.push("K");
662 c.print(p)
663 }
664 }?;
665 }
666 ty::ExistentialPredicate::AutoTrait(def_id) => {
667 p.print_def_path(*def_id, &[])?;
668 }
669 }
670 }
671 Ok(())
672 })?;
673
674 self.push("E");
675 Ok(())
676 }
677
678 fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
679 let cv = match ct.kind() {
681 ty::ConstKind::Value(cv) => cv,
682
683 ty::ConstKind::Param(_) => {
686 self.push("p");
688 return Ok(());
689 }
690
691 ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, args, .. }) => {
694 return self.print_def_path(def, args);
695 }
696
697 ty::ConstKind::Expr(_)
698 | ty::ConstKind::Infer(_)
699 | ty::ConstKind::Bound(..)
700 | ty::ConstKind::Placeholder(_)
701 | ty::ConstKind::Error(_) => ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
702 };
703
704 if let Some(&i) = self.consts.get(&ct) {
705 self.print_backref(i)?;
706 return Ok(());
707 }
708
709 let ty::Value { ty: ct_ty, valtree } = cv;
710 let start = self.out.len();
711
712 match ct_ty.kind() {
713 ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => {
714 ct_ty.print(self)?;
715
716 let mut bits = cv
717 .try_to_bits(self.tcx, ty::TypingEnv::fully_monomorphized())
718 .expect("expected const to be monomorphic");
719
720 if let ty::Int(ity) = ct_ty.kind() {
722 let val =
723 Integer::from_int_ty(&self.tcx, *ity).size().sign_extend(bits) as i128;
724 if val < 0 {
725 self.push("n");
726 }
727 bits = val.unsigned_abs();
728 }
729
730 let _ = self.out.write_fmt(format_args!("{0:x}_", bits))write!(self.out, "{bits:x}_");
731 }
732
733 ty::Str => {
735 let tcx = self.tcx();
736 let ref_ty = Ty::new_imm_ref(tcx, tcx.lifetimes.re_static, ct_ty);
739 let cv = ty::Value { ty: ref_ty, valtree };
740 let slice = cv.try_to_raw_bytes(tcx).unwrap_or_else(|| {
741 ::rustc_middle::util::bug::bug_fmt(format_args!("expected to get raw bytes from valtree {0:?} for type {1}",
valtree, ct_ty))bug!("expected to get raw bytes from valtree {:?} for type {:}", valtree, ct_ty)
742 });
743 let s = std::str::from_utf8(slice).expect("non utf8 str from MIR interpreter");
744
745 self.push("e");
747
748 for byte in s.bytes() {
750 let _ = self.out.write_fmt(format_args!("{0:02x}", byte))write!(self.out, "{byte:02x}");
751 }
752
753 self.push("_");
754 }
755
756 ty::Ref(_, _, mutbl) => {
759 self.push(match mutbl {
760 hir::Mutability::Not => "R",
761 hir::Mutability::Mut => "Q",
762 });
763
764 let pointee_ty =
765 ct_ty.builtin_deref(true).expect("tried to dereference on non-ptr type");
766 let dereferenced_const = ty::Const::new_value(self.tcx, valtree, pointee_ty);
767 dereferenced_const.print(self)?;
768 }
769
770 ty::Array(..) | ty::Tuple(..) | ty::Slice(_) => {
771 let fields = cv.to_branch().iter().copied();
772
773 let print_field_list = |this: &mut Self| {
774 for field in fields.clone() {
775 field.print(this)?;
776 }
777 this.push("E");
778 Ok(())
779 };
780
781 match *ct_ty.kind() {
782 ty::Array(..) | ty::Slice(_) => {
783 self.push("A");
784 print_field_list(self)?;
785 }
786 ty::Tuple(..) => {
787 self.push("T");
788 print_field_list(self)?;
789 }
790 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
791 }
792 }
793 ty::Adt(def, args) => {
794 let contents = cv.destructure_adt_const();
795 let fields = contents.fields.iter().copied();
796
797 let print_field_list = |this: &mut Self| {
798 for field in fields.clone() {
799 field.print(this)?;
800 }
801 this.push("E");
802 Ok(())
803 };
804
805 let variant_idx = contents.variant;
806 let variant_def = &def.variant(variant_idx);
807
808 self.push("V");
809 self.print_def_path(variant_def.def_id, args)?;
810
811 match variant_def.ctor_kind() {
812 Some(CtorKind::Const) => {
813 self.push("U");
814 }
815 Some(CtorKind::Fn) => {
816 self.push("T");
817 print_field_list(self)?;
818 }
819 None => {
820 self.push("S");
821 for (field_def, field) in iter::zip(&variant_def.fields, fields) {
822 let disambiguated_field =
826 self.tcx.def_key(field_def.did).disambiguated_data;
827 let field_name = disambiguated_field.data.get_opt_name();
828 self.push_disambiguator(disambiguated_field.disambiguator as u64);
829 self.push_ident(field_name.unwrap().as_str());
830
831 field.print(self)?;
832 }
833 self.push("E");
834 }
835 }
836 }
837 _ => {
838 ::rustc_middle::util::bug::bug_fmt(format_args!("symbol_names: unsupported constant of type `{0}` ({1:?})",
ct_ty, ct));bug!("symbol_names: unsupported constant of type `{}` ({:?})", ct_ty, ct);
839 }
840 }
841
842 if !ct.has_escaping_bound_vars() {
845 self.consts.insert(ct, start);
846 }
847 Ok(())
848 }
849
850 fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
851 self.push("C");
852 if !self.is_exportable {
853 let stable_crate_id = self.tcx.stable_crate_id(cnum);
854 self.push_disambiguator(stable_crate_id.as_u64());
855 }
856 let name = self.tcx.crate_name(cnum);
857 self.push_ident(name.as_str());
858 Ok(())
859 }
860
861 fn print_path_with_qualified(
862 &mut self,
863 self_ty: Ty<'tcx>,
864 trait_ref: Option<ty::TraitRef<'tcx>>,
865 ) -> Result<(), PrintError> {
866 if !trait_ref.is_some() {
::core::panicking::panic("assertion failed: trait_ref.is_some()")
};assert!(trait_ref.is_some());
867 let trait_ref = trait_ref.unwrap();
868
869 self.push("Y");
870 self_ty.print(self)?;
871 self.print_def_path(trait_ref.def_id, trait_ref.args)
872 }
873
874 fn print_path_with_impl(
875 &mut self,
876 _: impl FnOnce(&mut Self) -> Result<(), PrintError>,
877 _: Ty<'tcx>,
878 _: Option<ty::TraitRef<'tcx>>,
879 ) -> Result<(), PrintError> {
880 ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
882 }
883
884 fn print_path_with_simple(
885 &mut self,
886 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
887 disambiguated_data: &DisambiguatedDefPathData,
888 ) -> Result<(), PrintError> {
889 let ns = match disambiguated_data.data {
890 DefPathData::ForeignMod => return print_prefix(self),
893
894 DefPathData::TypeNs(_) => 't',
896 DefPathData::ValueNs(_) => 'v',
897 DefPathData::Closure => 'C',
898 DefPathData::Ctor => 'c',
899 DefPathData::AnonConst => 'K',
900 DefPathData::OpaqueTy => 'i',
901 DefPathData::SyntheticCoroutineBody => 's',
902 DefPathData::NestedStatic => 'n',
903 DefPathData::GlobalAsm => 'a',
904
905 DefPathData::CrateRoot
907 | DefPathData::Use
908 | DefPathData::Impl
909 | DefPathData::MacroNs(_)
910 | DefPathData::LifetimeNs(_)
911 | DefPathData::OpaqueLifetime(_)
912 | DefPathData::AnonAssocTy(..) => {
913 ::rustc_middle::util::bug::bug_fmt(format_args!("symbol_names: unexpected DefPathData: {0:?}",
disambiguated_data.data))bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)
914 }
915 };
916
917 let name = disambiguated_data.data.get_opt_name();
918
919 self.path_append_ns(
920 print_prefix,
921 ns,
922 disambiguated_data.disambiguator as u64,
923 name.unwrap_or(sym::empty).as_str(),
924 )
925 }
926
927 fn print_path_with_generic_args(
928 &mut self,
929 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
930 args: &[GenericArg<'tcx>],
931 ) -> Result<(), PrintError> {
932 let print_regions = args.iter().any(|arg| match arg.kind() {
934 GenericArgKind::Lifetime(r) => !r.is_erased(),
935 _ => false,
936 });
937 let args = args.iter().cloned().filter(|arg| match arg.kind() {
938 GenericArgKind::Lifetime(_) => print_regions,
939 _ => true,
940 });
941
942 if args.clone().next().is_none() {
943 return print_prefix(self);
944 }
945
946 self.push("I");
947 print_prefix(self)?;
948 for arg in args {
949 match arg.kind() {
950 GenericArgKind::Lifetime(lt) => {
951 lt.print(self)?;
952 }
953 GenericArgKind::Type(ty) => {
954 ty.print(self)?;
955 }
956 GenericArgKind::Const(c) => {
957 self.push("K");
958 c.print(self)?;
959 }
960 }
961 }
962 self.push("E");
963
964 Ok(())
965 }
966}
967pub(crate) fn push_integer_62(x: u64, output: &mut String) {
973 if let Some(x) = x.checked_sub(1) {
974 output.push_str(&x.to_base(62));
975 }
976 output.push('_');
977}
978
979pub(crate) fn encode_integer_62(x: u64) -> String {
980 let mut output = String::new();
981 push_integer_62(x, &mut output);
982 output
983}
984
985pub(crate) fn push_ident(ident: &str, output: &mut String) {
986 let mut use_punycode = false;
987 for b in ident.bytes() {
988 match b {
989 b'_' | b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => {}
990 0x80..=0xff => use_punycode = true,
991 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("symbol_names: bad byte {0} in ident {1:?}",
b, ident))bug!("symbol_names: bad byte {} in ident {:?}", b, ident),
992 }
993 }
994
995 let punycode_string;
996 let ident = if use_punycode {
997 output.push('u');
998
999 let mut punycode_bytes = match punycode::encode(ident) {
1001 Ok(s) => s.into_bytes(),
1002 Err(()) => ::rustc_middle::util::bug::bug_fmt(format_args!("symbol_names: punycode encoding failed for ident {0:?}",
ident))bug!("symbol_names: punycode encoding failed for ident {:?}", ident),
1003 };
1004
1005 if let Some(c) = punycode_bytes.iter_mut().rfind(|&&mut c| c == b'-') {
1007 *c = b'_';
1008 }
1009
1010 punycode_string = String::from_utf8(punycode_bytes).unwrap();
1012 &punycode_string
1013 } else {
1014 ident
1015 };
1016
1017 let _ = output.write_fmt(format_args!("{0}", ident.len()))write!(output, "{}", ident.len());
1018
1019 if let Some('_' | '0'..='9') = ident.chars().next() {
1021 output.push('_');
1022 }
1023
1024 output.push_str(ident);
1025}