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