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 p: V0SymbolMangler<'_> = V0SymbolMangler {
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 p.print_def_path(def_id, tcx.mk_args(&[GenericArg::from(drop_ty)])).unwrap()
73 } else if let Some(shim_kind) = shim_kind {
74 p.path_append_ns(|p| p.print_def_path(def_id, args), 'S', 0, shim_kind).unwrap()
75 } else {
76 p.print_def_path(def_id, args).unwrap()
77 };
78 if let Some(instantiating_crate) = instantiating_crate {
79 p.print_def_path(instantiating_crate.as_def_id(), &[]).unwrap();
80 }
81 std::mem::take(&mut p.out)
82}
83
84pub fn mangle_internal_symbol<'tcx>(tcx: TyCtxt<'tcx>, item_name: &str) -> String {
85 match item_name {
86 "rust_eh_personality" => return item_name.to_owned(),
88 "__isPlatformVersionAtLeast" | "__isOSVersionAtLeast" => return item_name.to_owned(),
91 _ => {}
92 }
93
94 let prefix = "_R";
95 let mut p: V0SymbolMangler<'_> = V0SymbolMangler {
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 p.path_append_ns(
107 |p| {
108 p.push("C");
109 p.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 p.push_ident("__rustc");
122 Ok(())
123 },
124 'v',
125 0,
126 item_name,
127 )
128 .unwrap();
129
130 std::mem::take(&mut p.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 p = V0SymbolMangler {
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 p.print_def_path(trait_ref.def_id, &[]).unwrap();
149 std::mem::take(&mut p.out)
150}
151
152struct BinderLevel {
153 lifetime_depths: Range<u32>,
164}
165
166struct V0SymbolMangler<'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> V0SymbolMangler<'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 self.push("R");
266 self.print_const(start)?;
267 self.print_const(end)?;
268 }
269 ty::PatternKind::NotNull => {
270 self.tcx.types.unit.print(self)?;
271 }
272 ty::PatternKind::Or(patterns) => {
273 self.push("O");
274 for pat in patterns {
275 self.print_pat(pat)?;
276 }
277 self.push("E");
278 }
279 })
280 }
281}
282
283impl<'tcx> Printer<'tcx> for V0SymbolMangler<'tcx> {
284 fn tcx(&self) -> TyCtxt<'tcx> {
285 self.tcx
286 }
287
288 fn print_def_path(
289 &mut self,
290 def_id: DefId,
291 args: &'tcx [GenericArg<'tcx>],
292 ) -> Result<(), PrintError> {
293 if let Some(&i) = self.paths.get(&(def_id, args)) {
294 return self.print_backref(i);
295 }
296 let start = self.out.len();
297
298 self.default_print_def_path(def_id, args)?;
299
300 if !args.iter().any(|k| k.has_escaping_bound_vars()) {
303 self.paths.insert((def_id, args), start);
304 }
305 Ok(())
306 }
307
308 fn print_impl_path(
309 &mut self,
310 impl_def_id: DefId,
311 args: &'tcx [GenericArg<'tcx>],
312 ) -> Result<(), PrintError> {
313 let key = self.tcx.def_key(impl_def_id);
314 let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id };
315
316 let self_ty = self.tcx.type_of(impl_def_id);
317 let impl_trait_ref = self.tcx.impl_opt_trait_ref(impl_def_id);
318 let generics = self.tcx.generics_of(impl_def_id);
319 let (typing_env, mut self_ty, mut impl_trait_ref) = if generics.count() > args.len()
333 || &args[..generics.count()]
334 == self
335 .tcx
336 .erase_and_anonymize_regions(ty::GenericArgs::identity_for_item(
337 self.tcx,
338 impl_def_id,
339 ))
340 .as_slice()
341 {
342 (
343 ty::TypingEnv::post_analysis(self.tcx, impl_def_id),
344 self_ty.instantiate_identity(),
345 impl_trait_ref.map(|impl_trait_ref| impl_trait_ref.instantiate_identity()),
346 )
347 } else {
348 assert!(
349 !args.has_non_region_param() && !args.has_free_regions(),
350 "should not be mangling partially substituted \
351 polymorphic instance: {impl_def_id:?} {args:?}"
352 );
353 (
354 ty::TypingEnv::fully_monomorphized(),
355 self_ty.instantiate(self.tcx, args),
356 impl_trait_ref.map(|impl_trait_ref| impl_trait_ref.instantiate(self.tcx, args)),
357 )
358 };
359
360 match &mut impl_trait_ref {
361 Some(impl_trait_ref) => {
362 assert_eq!(impl_trait_ref.self_ty(), self_ty);
363 *impl_trait_ref = self.tcx.normalize_erasing_regions(typing_env, *impl_trait_ref);
364 self_ty = impl_trait_ref.self_ty();
365 }
366 None => {
367 self_ty = self.tcx.normalize_erasing_regions(typing_env, self_ty);
368 }
369 }
370
371 self.push(match impl_trait_ref {
372 Some(_) => "X",
373 None => "M",
374 });
375
376 if impl_trait_ref.is_some() && args.iter().any(|a| a.has_non_region_param()) {
379 self.print_path_with_generic_args(
380 |this| {
381 this.path_append_ns(
382 |p| p.print_def_path(parent_def_id, &[]),
383 'I',
384 key.disambiguated_data.disambiguator as u64,
385 "",
386 )
387 },
388 args,
389 )?;
390 } else {
391 let exported_impl_order = self.tcx.stable_order_of_exportable_impls(impl_def_id.krate);
392 let disambiguator = match self.is_exportable {
393 true => exported_impl_order[&impl_def_id] as u64,
394 false => {
395 exported_impl_order.len() as u64 + key.disambiguated_data.disambiguator as u64
396 }
397 };
398 self.push_disambiguator(disambiguator);
399 self.print_def_path(parent_def_id, &[])?;
400 }
401
402 self_ty.print(self)?;
403
404 if let Some(trait_ref) = impl_trait_ref {
405 self.print_def_path(trait_ref.def_id, trait_ref.args)?;
406 }
407
408 Ok(())
409 }
410
411 fn print_region(&mut self, region: ty::Region<'_>) -> Result<(), PrintError> {
412 let i = match region.kind() {
413 ty::ReErased => 0,
416
417 ty::ReBound(
420 ty::BoundVarIndexKind::Bound(debruijn),
421 ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon },
422 ) => {
423 let binder = &self.binders[self.binders.len() - 1 - debruijn.index()];
424 let depth = binder.lifetime_depths.start + var.as_u32();
425
426 1 + (self.binders.last().unwrap().lifetime_depths.end - 1 - depth)
427 }
428
429 _ => bug!("symbol_names: non-erased region `{:?}`", region),
430 };
431 self.push("L");
432 self.push_integer_62(i as u64);
433 Ok(())
434 }
435
436 fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
437 let basic_type = match ty.kind() {
439 ty::Bool => "b",
440 ty::Char => "c",
441 ty::Str => "e",
442 ty::Int(IntTy::I8) => "a",
443 ty::Int(IntTy::I16) => "s",
444 ty::Int(IntTy::I32) => "l",
445 ty::Int(IntTy::I64) => "x",
446 ty::Int(IntTy::I128) => "n",
447 ty::Int(IntTy::Isize) => "i",
448 ty::Uint(UintTy::U8) => "h",
449 ty::Uint(UintTy::U16) => "t",
450 ty::Uint(UintTy::U32) => "m",
451 ty::Uint(UintTy::U64) => "y",
452 ty::Uint(UintTy::U128) => "o",
453 ty::Uint(UintTy::Usize) => "j",
454 ty::Float(FloatTy::F16) => "C3f16",
455 ty::Float(FloatTy::F32) => "f",
456 ty::Float(FloatTy::F64) => "d",
457 ty::Float(FloatTy::F128) => "C4f128",
458 ty::Never => "z",
459
460 ty::Tuple(_) if ty.is_unit() => "u",
461
462 ty::Param(_) => "p",
465
466 _ => "",
467 };
468 if !basic_type.is_empty() {
469 self.push(basic_type);
470 return Ok(());
471 }
472
473 if let Some(&i) = self.types.get(&ty) {
474 return self.print_backref(i);
475 }
476 let start = self.out.len();
477
478 match *ty.kind() {
479 ty::Bool | ty::Char | ty::Str | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Never => {
481 unreachable!()
482 }
483 ty::Tuple(_) if ty.is_unit() => unreachable!(),
484 ty::Param(_) => unreachable!(),
485
486 ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => bug!(),
487
488 ty::Ref(r, ty, mutbl) => {
489 self.push(match mutbl {
490 hir::Mutability::Not => "R",
491 hir::Mutability::Mut => "Q",
492 });
493 if !r.is_erased() {
494 r.print(self)?;
495 }
496 ty.print(self)?;
497 }
498
499 ty::RawPtr(ty, mutbl) => {
500 self.push(match mutbl {
501 hir::Mutability::Not => "P",
502 hir::Mutability::Mut => "O",
503 });
504 ty.print(self)?;
505 }
506
507 ty::Pat(ty, pat) => {
508 self.push("W");
509 ty.print(self)?;
510 self.print_pat(pat)?;
511 }
512
513 ty::Array(ty, len) => {
514 self.push("A");
515 ty.print(self)?;
516 self.print_const(len)?;
517 }
518 ty::Slice(ty) => {
519 self.push("S");
520 ty.print(self)?;
521 }
522
523 ty::Tuple(tys) => {
524 self.push("T");
525 for ty in tys.iter() {
526 ty.print(self)?;
527 }
528 self.push("E");
529 }
530
531 ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), args)
533 | ty::FnDef(def_id, args)
534 | ty::Closure(def_id, args)
535 | ty::CoroutineClosure(def_id, args)
536 | ty::Coroutine(def_id, args) => {
537 self.print_def_path(def_id, args)?;
538 }
539
540 ty::Alias(ty::Projection, ty::AliasTy { def_id, args, .. }) => {
543 self.print_def_path(def_id, args)?;
544 }
545
546 ty::Foreign(def_id) => {
547 self.print_def_path(def_id, &[])?;
548 }
549
550 ty::FnPtr(sig_tys, hdr) => {
551 let sig = sig_tys.with(hdr);
552 self.push("F");
553 self.wrap_binder(&sig, |p, sig| {
554 if sig.safety.is_unsafe() {
555 p.push("U");
556 }
557 match sig.abi {
558 ExternAbi::Rust => {}
559 ExternAbi::C { unwind: false } => p.push("KC"),
560 abi => {
561 p.push("K");
562 let name = abi.as_str();
563 if name.contains('-') {
564 p.push_ident(&name.replace('-', "_"));
565 } else {
566 p.push_ident(name);
567 }
568 }
569 }
570 for &ty in sig.inputs() {
571 ty.print(p)?;
572 }
573 if sig.c_variadic {
574 p.push("v");
575 }
576 p.push("E");
577 sig.output().print(p)
578 })?;
579 }
580
581 ty::UnsafeBinder(..) => todo!(),
583
584 ty::Dynamic(predicates, r) => {
585 self.push("D");
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], |p, _| {
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(p.tcx, 0);
642 let trait_ref = trait_ref.with_self_ty(p.tcx, dummy_self);
643 p.print_def_path(trait_ref.def_id, trait_ref.args)?;
644 }
645 ty::ExistentialPredicate::Projection(projection) => {
646 let name = p.tcx.associated_item(projection.def_id).name();
647 p.push("p");
648 p.push_ident(name.as_str());
649 match projection.term.kind() {
650 ty::TermKind::Ty(ty) => ty.print(p),
651 ty::TermKind::Const(c) => c.print(p),
652 }?;
653 }
654 ty::ExistentialPredicate::AutoTrait(def_id) => {
655 p.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 print_crate_name(&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 print_path_with_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 print_path_with_impl(
856 &mut self,
857 _: impl FnOnce(&mut Self) -> Result<(), PrintError>,
858 _: Ty<'tcx>,
859 _: Option<ty::TraitRef<'tcx>>,
860 ) -> Result<(), PrintError> {
861 unreachable!()
863 }
864
865 fn print_path_with_simple(
866 &mut self,
867 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
868 disambiguated_data: &DisambiguatedDefPathData,
869 ) -> Result<(), PrintError> {
870 let ns = match disambiguated_data.data {
871 DefPathData::ForeignMod => return print_prefix(self),
874
875 DefPathData::TypeNs(_) => 't',
877 DefPathData::ValueNs(_) => 'v',
878 DefPathData::Closure => 'C',
879 DefPathData::Ctor => 'c',
880 DefPathData::AnonConst => 'K',
881 DefPathData::LateAnonConst => '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::DesugaredAnonymousLifetime
894 | DefPathData::OpaqueLifetime(_)
895 | DefPathData::AnonAssocTy(..) => {
896 bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)
897 }
898 };
899
900 let name = disambiguated_data.data.get_opt_name();
901
902 self.path_append_ns(
903 print_prefix,
904 ns,
905 disambiguated_data.disambiguator as u64,
906 name.unwrap_or(sym::empty).as_str(),
907 )
908 }
909
910 fn print_path_with_generic_args(
911 &mut self,
912 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
913 args: &[GenericArg<'tcx>],
914 ) -> Result<(), PrintError> {
915 let print_regions = args.iter().any(|arg| match arg.kind() {
917 GenericArgKind::Lifetime(r) => !r.is_erased(),
918 _ => false,
919 });
920 let args = args.iter().cloned().filter(|arg| match arg.kind() {
921 GenericArgKind::Lifetime(_) => print_regions,
922 _ => true,
923 });
924
925 if args.clone().next().is_none() {
926 return print_prefix(self);
927 }
928
929 self.push("I");
930 print_prefix(self)?;
931 for arg in args {
932 match arg.kind() {
933 GenericArgKind::Lifetime(lt) => {
934 lt.print(self)?;
935 }
936 GenericArgKind::Type(ty) => {
937 ty.print(self)?;
938 }
939 GenericArgKind::Const(c) => {
940 self.push("K");
941 c.print(self)?;
942 }
943 }
944 }
945 self.push("E");
946
947 Ok(())
948 }
949}
950pub(crate) fn push_integer_62(x: u64, output: &mut String) {
956 if let Some(x) = x.checked_sub(1) {
957 output.push_str(&x.to_base(62));
958 }
959 output.push('_');
960}
961
962pub(crate) fn encode_integer_62(x: u64) -> String {
963 let mut output = String::new();
964 push_integer_62(x, &mut output);
965 output
966}
967
968pub(crate) fn push_ident(ident: &str, output: &mut String) {
969 let mut use_punycode = false;
970 for b in ident.bytes() {
971 match b {
972 b'_' | b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => {}
973 0x80..=0xff => use_punycode = true,
974 _ => bug!("symbol_names: bad byte {} in ident {:?}", b, ident),
975 }
976 }
977
978 let punycode_string;
979 let ident = if use_punycode {
980 output.push('u');
981
982 let mut punycode_bytes = match punycode::encode(ident) {
984 Ok(s) => s.into_bytes(),
985 Err(()) => bug!("symbol_names: punycode encoding failed for ident {:?}", ident),
986 };
987
988 if let Some(c) = punycode_bytes.iter_mut().rfind(|&&mut c| c == b'-') {
990 *c = b'_';
991 }
992
993 punycode_string = String::from_utf8(punycode_bytes).unwrap();
995 &punycode_string
996 } else {
997 ident
998 };
999
1000 let _ = write!(output, "{}", ident.len());
1001
1002 if let Some('_' | '0'..='9') = ident.chars().next() {
1004 output.push('_');
1005 }
1006
1007 output.push_str(ident);
1008}