1use std::fmt::Write;
2use std::iter;
3use std::ops::Range;
4
5use rustc_abi::{ExternAbi, Integer};
6use rustc_data_structures::base_n::ToBaseN;
7use rustc_data_structures::fx::FxHashMap;
8use rustc_data_structures::intern::Interned;
9use rustc_hir as hir;
10use rustc_hir::def::CtorKind;
11use rustc_hir::def_id::{CrateNum, DefId};
12use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
13use rustc_middle::bug;
14use rustc_middle::ty::layout::IntegerExt;
15use rustc_middle::ty::print::{Print, PrintError, Printer};
16use rustc_middle::ty::{
17 self, FloatTy, GenericArg, GenericArgKind, Instance, IntTy, ReifyReason, Ty, TyCtxt,
18 TypeVisitable, TypeVisitableExt, UintTy,
19};
20use rustc_span::kw;
21
22pub(super) fn mangle<'tcx>(
23 tcx: TyCtxt<'tcx>,
24 instance: Instance<'tcx>,
25 instantiating_crate: Option<CrateNum>,
26) -> String {
27 let def_id = instance.def_id();
28 let args = tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), instance.args);
30
31 let prefix = "_R";
32 let mut cx: SymbolMangler<'_> = SymbolMangler {
33 tcx,
34 start_offset: prefix.len(),
35 paths: FxHashMap::default(),
36 types: FxHashMap::default(),
37 consts: FxHashMap::default(),
38 binders: vec![],
39 out: String::from(prefix),
40 };
41
42 let shim_kind = match instance.def {
44 ty::InstanceKind::ThreadLocalShim(_) => Some("tls"),
45 ty::InstanceKind::VTableShim(_) => Some("vtable"),
46 ty::InstanceKind::ReifyShim(_, None) => Some("reify"),
47 ty::InstanceKind::ReifyShim(_, Some(ReifyReason::FnPtr)) => Some("reify_fnptr"),
48 ty::InstanceKind::ReifyShim(_, Some(ReifyReason::Vtable)) => Some("reify_vtable"),
49
50 ty::InstanceKind::ConstructCoroutineInClosureShim { receiver_by_ref: true, .. } => {
53 Some("by_move")
54 }
55 ty::InstanceKind::ConstructCoroutineInClosureShim { receiver_by_ref: false, .. } => {
56 Some("by_ref")
57 }
58
59 _ => None,
60 };
61
62 if let Some(shim_kind) = shim_kind {
63 cx.path_append_ns(|cx| cx.print_def_path(def_id, args), 'S', 0, shim_kind).unwrap()
64 } else {
65 cx.print_def_path(def_id, args).unwrap()
66 };
67 if let Some(instantiating_crate) = instantiating_crate {
68 cx.print_def_path(instantiating_crate.as_def_id(), &[]).unwrap();
69 }
70 std::mem::take(&mut cx.out)
71}
72
73pub(super) fn mangle_typeid_for_trait_ref<'tcx>(
74 tcx: TyCtxt<'tcx>,
75 trait_ref: ty::ExistentialTraitRef<'tcx>,
76) -> String {
77 let mut cx = SymbolMangler {
79 tcx,
80 start_offset: 0,
81 paths: FxHashMap::default(),
82 types: FxHashMap::default(),
83 consts: FxHashMap::default(),
84 binders: vec![],
85 out: String::new(),
86 };
87 cx.print_def_path(trait_ref.def_id, &[]).unwrap();
88 std::mem::take(&mut cx.out)
89}
90
91struct BinderLevel {
92 lifetime_depths: Range<u32>,
103}
104
105struct SymbolMangler<'tcx> {
106 tcx: TyCtxt<'tcx>,
107 binders: Vec<BinderLevel>,
108 out: String,
109
110 start_offset: usize,
112 paths: FxHashMap<(DefId, &'tcx [GenericArg<'tcx>]), usize>,
114 types: FxHashMap<Ty<'tcx>, usize>,
115 consts: FxHashMap<ty::Const<'tcx>, usize>,
116}
117
118impl<'tcx> SymbolMangler<'tcx> {
119 fn push(&mut self, s: &str) {
120 self.out.push_str(s);
121 }
122
123 fn push_integer_62(&mut self, x: u64) {
129 push_integer_62(x, &mut self.out)
130 }
131
132 fn push_opt_integer_62(&mut self, tag: &str, x: u64) {
137 if let Some(x) = x.checked_sub(1) {
138 self.push(tag);
139 self.push_integer_62(x);
140 }
141 }
142
143 fn push_disambiguator(&mut self, dis: u64) {
144 self.push_opt_integer_62("s", dis);
145 }
146
147 fn push_ident(&mut self, ident: &str) {
148 push_ident(ident, &mut self.out)
149 }
150
151 fn path_append_ns(
152 &mut self,
153 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
154 ns: char,
155 disambiguator: u64,
156 name: &str,
157 ) -> Result<(), PrintError> {
158 self.push("N");
159 self.out.push(ns);
160 print_prefix(self)?;
161 self.push_disambiguator(disambiguator);
162 self.push_ident(name);
163 Ok(())
164 }
165
166 fn print_backref(&mut self, i: usize) -> Result<(), PrintError> {
167 self.push("B");
168 self.push_integer_62((i - self.start_offset) as u64);
169 Ok(())
170 }
171
172 fn in_binder<T>(
173 &mut self,
174 value: &ty::Binder<'tcx, T>,
175 print_value: impl FnOnce(&mut Self, &T) -> Result<(), PrintError>,
176 ) -> Result<(), PrintError>
177 where
178 T: TypeVisitable<TyCtxt<'tcx>>,
179 {
180 let mut lifetime_depths =
181 self.binders.last().map(|b| b.lifetime_depths.end).map_or(0..0, |i| i..i);
182
183 let lifetimes = value
185 .bound_vars()
186 .iter()
187 .filter(|var| matches!(var, ty::BoundVariableKind::Region(..)))
188 .count() as u32;
189
190 self.push_opt_integer_62("G", lifetimes as u64);
191 lifetime_depths.end += lifetimes;
192
193 self.binders.push(BinderLevel { lifetime_depths });
194 print_value(self, value.as_ref().skip_binder())?;
195 self.binders.pop();
196
197 Ok(())
198 }
199}
200
201impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
202 fn tcx(&self) -> TyCtxt<'tcx> {
203 self.tcx
204 }
205
206 fn print_def_path(
207 &mut self,
208 def_id: DefId,
209 args: &'tcx [GenericArg<'tcx>],
210 ) -> Result<(), PrintError> {
211 if let Some(&i) = self.paths.get(&(def_id, args)) {
212 return self.print_backref(i);
213 }
214 let start = self.out.len();
215
216 self.default_print_def_path(def_id, args)?;
217
218 if !args.iter().any(|k| k.has_escaping_bound_vars()) {
221 self.paths.insert((def_id, args), start);
222 }
223 Ok(())
224 }
225
226 fn print_impl_path(
227 &mut self,
228 impl_def_id: DefId,
229 args: &'tcx [GenericArg<'tcx>],
230 ) -> Result<(), PrintError> {
231 let key = self.tcx.def_key(impl_def_id);
232 let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id };
233
234 let self_ty = self.tcx.type_of(impl_def_id);
235 let impl_trait_ref = self.tcx.impl_trait_ref(impl_def_id);
236 let generics = self.tcx.generics_of(impl_def_id);
237 let (typing_env, mut self_ty, mut impl_trait_ref) = if generics.count() > args.len()
251 || &args[..generics.count()]
252 == self
253 .tcx
254 .erase_regions(ty::GenericArgs::identity_for_item(self.tcx, impl_def_id))
255 .as_slice()
256 {
257 (
258 ty::TypingEnv::post_analysis(self.tcx, impl_def_id),
259 self_ty.instantiate_identity(),
260 impl_trait_ref.map(|impl_trait_ref| impl_trait_ref.instantiate_identity()),
261 )
262 } else {
263 assert!(
264 !args.has_non_region_param(),
265 "should not be mangling partially substituted \
266 polymorphic instance: {impl_def_id:?} {args:?}"
267 );
268 (
269 ty::TypingEnv::fully_monomorphized(),
270 self_ty.instantiate(self.tcx, args),
271 impl_trait_ref.map(|impl_trait_ref| impl_trait_ref.instantiate(self.tcx, args)),
272 )
273 };
274
275 match &mut impl_trait_ref {
276 Some(impl_trait_ref) => {
277 assert_eq!(impl_trait_ref.self_ty(), self_ty);
278 *impl_trait_ref = self.tcx.normalize_erasing_regions(typing_env, *impl_trait_ref);
279 self_ty = impl_trait_ref.self_ty();
280 }
281 None => {
282 self_ty = self.tcx.normalize_erasing_regions(typing_env, self_ty);
283 }
284 }
285
286 self.push(match impl_trait_ref {
287 Some(_) => "X",
288 None => "M",
289 });
290
291 if impl_trait_ref.is_some() && args.iter().any(|a| a.has_non_region_param()) {
294 self.path_generic_args(
295 |this| {
296 this.path_append_ns(
297 |cx| cx.print_def_path(parent_def_id, &[]),
298 'I',
299 key.disambiguated_data.disambiguator as u64,
300 "",
301 )
302 },
303 args,
304 )?;
305 } else {
306 self.push_disambiguator(key.disambiguated_data.disambiguator as u64);
307 self.print_def_path(parent_def_id, &[])?;
308 }
309
310 self_ty.print(self)?;
311
312 if let Some(trait_ref) = impl_trait_ref {
313 self.print_def_path(trait_ref.def_id, trait_ref.args)?;
314 }
315
316 Ok(())
317 }
318
319 fn print_region(&mut self, region: ty::Region<'_>) -> Result<(), PrintError> {
320 let i = match *region {
321 ty::ReErased => 0,
324
325 ty::ReBound(debruijn, ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon }) => {
328 let binder = &self.binders[self.binders.len() - 1 - debruijn.index()];
329 let depth = binder.lifetime_depths.start + var.as_u32();
330
331 1 + (self.binders.last().unwrap().lifetime_depths.end - 1 - depth)
332 }
333
334 _ => bug!("symbol_names: non-erased region `{:?}`", region),
335 };
336 self.push("L");
337 self.push_integer_62(i as u64);
338 Ok(())
339 }
340
341 fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
342 let basic_type = match ty.kind() {
344 ty::Bool => "b",
345 ty::Char => "c",
346 ty::Str => "e",
347 ty::Tuple(_) if ty.is_unit() => "u",
348 ty::Int(IntTy::I8) => "a",
349 ty::Int(IntTy::I16) => "s",
350 ty::Int(IntTy::I32) => "l",
351 ty::Int(IntTy::I64) => "x",
352 ty::Int(IntTy::I128) => "n",
353 ty::Int(IntTy::Isize) => "i",
354 ty::Uint(UintTy::U8) => "h",
355 ty::Uint(UintTy::U16) => "t",
356 ty::Uint(UintTy::U32) => "m",
357 ty::Uint(UintTy::U64) => "y",
358 ty::Uint(UintTy::U128) => "o",
359 ty::Uint(UintTy::Usize) => "j",
360 ty::Float(FloatTy::F16) => "C3f16",
361 ty::Float(FloatTy::F32) => "f",
362 ty::Float(FloatTy::F64) => "d",
363 ty::Float(FloatTy::F128) => "C4f128",
364 ty::Never => "z",
365
366 ty::Param(_) => "p",
369
370 ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => bug!(),
371
372 _ => "",
373 };
374 if !basic_type.is_empty() {
375 self.push(basic_type);
376 return Ok(());
377 }
378
379 if let Some(&i) = self.types.get(&ty) {
380 return self.print_backref(i);
381 }
382 let start = self.out.len();
383
384 match *ty.kind() {
385 ty::Bool | ty::Char | ty::Str | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Never => {
387 unreachable!()
388 }
389 ty::Tuple(_) if ty.is_unit() => unreachable!(),
390
391 ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => {
393 unreachable!()
394 }
395
396 ty::Ref(r, ty, mutbl) => {
397 self.push(match mutbl {
398 hir::Mutability::Not => "R",
399 hir::Mutability::Mut => "Q",
400 });
401 if !r.is_erased() {
402 r.print(self)?;
403 }
404 ty.print(self)?;
405 }
406
407 ty::RawPtr(ty, mutbl) => {
408 self.push(match mutbl {
409 hir::Mutability::Not => "P",
410 hir::Mutability::Mut => "O",
411 });
412 ty.print(self)?;
413 }
414
415 ty::Pat(ty, pat) => match *pat {
416 ty::PatternKind::Range { start, end, include_end } => {
417 let consts = [
418 start.unwrap_or(self.tcx.consts.unit),
419 end.unwrap_or(self.tcx.consts.unit),
420 ty::Const::from_bool(self.tcx, include_end),
421 ];
422 self.push("T");
425 ty.print(self)?;
426 for ct in consts {
427 Ty::new_array_with_const_len(self.tcx, self.tcx.types.unit, ct)
428 .print(self)?;
429 }
430 self.push("E");
431 }
432 },
433
434 ty::Array(ty, len) => {
435 self.push("A");
436 ty.print(self)?;
437 self.print_const(len)?;
438 }
439 ty::Slice(ty) => {
440 self.push("S");
441 ty.print(self)?;
442 }
443
444 ty::Tuple(tys) => {
445 self.push("T");
446 for ty in tys.iter() {
447 ty.print(self)?;
448 }
449 self.push("E");
450 }
451
452 ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), args)
454 | ty::FnDef(def_id, args)
455 | ty::Closure(def_id, args)
456 | ty::CoroutineClosure(def_id, args)
457 | ty::Coroutine(def_id, args) => {
458 self.print_def_path(def_id, args)?;
459 }
460
461 ty::Alias(ty::Projection, ty::AliasTy { def_id, args, .. }) => {
464 self.print_def_path(def_id, args)?;
465 }
466
467 ty::Foreign(def_id) => {
468 self.print_def_path(def_id, &[])?;
469 }
470
471 ty::FnPtr(sig_tys, hdr) => {
472 let sig = sig_tys.with(hdr);
473 self.push("F");
474 self.in_binder(&sig, |cx, sig| {
475 if sig.safety.is_unsafe() {
476 cx.push("U");
477 }
478 match sig.abi {
479 ExternAbi::Rust => {}
480 ExternAbi::C { unwind: false } => cx.push("KC"),
481 abi => {
482 cx.push("K");
483 let name = abi.as_str();
484 if name.contains('-') {
485 cx.push_ident(&name.replace('-', "_"));
486 } else {
487 cx.push_ident(name);
488 }
489 }
490 }
491 for &ty in sig.inputs() {
492 ty.print(cx)?;
493 }
494 if sig.c_variadic {
495 cx.push("v");
496 }
497 cx.push("E");
498 sig.output().print(cx)
499 })?;
500 }
501
502 ty::UnsafeBinder(..) => todo!(),
504
505 ty::Dynamic(predicates, r, kind) => {
506 self.push(match kind {
507 ty::Dyn => "D",
508 ty::DynStar => "D*",
510 });
511 self.print_dyn_existential(predicates)?;
512 r.print(self)?;
513 }
514
515 ty::Alias(..) => bug!("symbol_names: unexpected alias"),
516 ty::CoroutineWitness(..) => bug!("symbol_names: unexpected `CoroutineWitness`"),
517 }
518
519 if !ty.has_escaping_bound_vars() {
522 self.types.insert(ty, start);
523 }
524 Ok(())
525 }
526
527 fn print_dyn_existential(
528 &mut self,
529 predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
530 ) -> Result<(), PrintError> {
531 self.in_binder(&predicates[0], |cx, _| {
558 for predicate in predicates.iter() {
559 match predicate.as_ref().skip_binder() {
564 ty::ExistentialPredicate::Trait(trait_ref) => {
565 let dummy_self = Ty::new_fresh(cx.tcx, 0);
567 let trait_ref = trait_ref.with_self_ty(cx.tcx, dummy_self);
568 cx.print_def_path(trait_ref.def_id, trait_ref.args)?;
569 }
570 ty::ExistentialPredicate::Projection(projection) => {
571 let name = cx.tcx.associated_item(projection.def_id).name;
572 cx.push("p");
573 cx.push_ident(name.as_str());
574 match projection.term.unpack() {
575 ty::TermKind::Ty(ty) => ty.print(cx),
576 ty::TermKind::Const(c) => c.print(cx),
577 }?;
578 }
579 ty::ExistentialPredicate::AutoTrait(def_id) => {
580 cx.print_def_path(*def_id, &[])?;
581 }
582 }
583 }
584 Ok(())
585 })?;
586
587 self.push("E");
588 Ok(())
589 }
590
591 fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
592 let cv = match ct.kind() {
594 ty::ConstKind::Value(cv) => cv,
595
596 ty::ConstKind::Param(_) => {
599 self.push("p");
601 return Ok(());
602 }
603
604 ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, args, .. }) => {
607 return self.print_def_path(def, args);
608 }
609
610 ty::ConstKind::Expr(_)
611 | ty::ConstKind::Infer(_)
612 | ty::ConstKind::Bound(..)
613 | ty::ConstKind::Placeholder(_)
614 | ty::ConstKind::Error(_) => bug!(),
615 };
616
617 if let Some(&i) = self.consts.get(&ct) {
618 self.print_backref(i)?;
619 return Ok(());
620 }
621
622 let ty::Value { ty: ct_ty, valtree } = cv;
623 let start = self.out.len();
624
625 match ct_ty.kind() {
626 ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => {
627 ct_ty.print(self)?;
628
629 let mut bits = cv
630 .try_to_bits(self.tcx, ty::TypingEnv::fully_monomorphized())
631 .expect("expected const to be monomorphic");
632
633 if let ty::Int(ity) = ct_ty.kind() {
635 let val =
636 Integer::from_int_ty(&self.tcx, *ity).size().sign_extend(bits) as i128;
637 if val < 0 {
638 self.push("n");
639 }
640 bits = val.unsigned_abs();
641 }
642
643 let _ = write!(self.out, "{bits:x}_");
644 }
645
646 ty::Str => {
648 let tcx = self.tcx();
649 let ref_ty = Ty::new_imm_ref(tcx, tcx.lifetimes.re_static, ct_ty);
652 let cv = ty::Value { ty: ref_ty, valtree };
653 let slice = cv.try_to_raw_bytes(tcx).unwrap_or_else(|| {
654 bug!("expected to get raw bytes from valtree {:?} for type {:}", valtree, ct_ty)
655 });
656 let s = std::str::from_utf8(slice).expect("non utf8 str from MIR interpreter");
657
658 self.push("e");
660
661 for byte in s.bytes() {
663 let _ = write!(self.out, "{byte:02x}");
664 }
665
666 self.push("_");
667 }
668
669 ty::Ref(_, _, mutbl) => {
672 self.push(match mutbl {
673 hir::Mutability::Not => "R",
674 hir::Mutability::Mut => "Q",
675 });
676
677 let pointee_ty =
678 ct_ty.builtin_deref(true).expect("tried to dereference on non-ptr type");
679 let dereferenced_const = ty::Const::new_value(self.tcx, valtree, pointee_ty);
680 dereferenced_const.print(self)?;
681 }
682
683 ty::Array(..) | ty::Tuple(..) | ty::Adt(..) | ty::Slice(_) => {
684 let contents = self.tcx.destructure_const(ct);
685 let fields = contents.fields.iter().copied();
686
687 let print_field_list = |this: &mut Self| {
688 for field in fields.clone() {
689 field.print(this)?;
690 }
691 this.push("E");
692 Ok(())
693 };
694
695 match *ct_ty.kind() {
696 ty::Array(..) | ty::Slice(_) => {
697 self.push("A");
698 print_field_list(self)?;
699 }
700 ty::Tuple(..) => {
701 self.push("T");
702 print_field_list(self)?;
703 }
704 ty::Adt(def, args) => {
705 let variant_idx =
706 contents.variant.expect("destructed const of adt without variant idx");
707 let variant_def = &def.variant(variant_idx);
708
709 self.push("V");
710 self.print_def_path(variant_def.def_id, args)?;
711
712 match variant_def.ctor_kind() {
713 Some(CtorKind::Const) => {
714 self.push("U");
715 }
716 Some(CtorKind::Fn) => {
717 self.push("T");
718 print_field_list(self)?;
719 }
720 None => {
721 self.push("S");
722 for (field_def, field) in iter::zip(&variant_def.fields, fields) {
723 let disambiguated_field =
727 self.tcx.def_key(field_def.did).disambiguated_data;
728 let field_name = disambiguated_field.data.get_opt_name();
729 self.push_disambiguator(
730 disambiguated_field.disambiguator as u64,
731 );
732 self.push_ident(field_name.unwrap_or(kw::Empty).as_str());
733
734 field.print(self)?;
735 }
736 self.push("E");
737 }
738 }
739 }
740 _ => unreachable!(),
741 }
742 }
743 _ => {
744 bug!("symbol_names: unsupported constant of type `{}` ({:?})", ct_ty, ct);
745 }
746 }
747
748 if !ct.has_escaping_bound_vars() {
751 self.consts.insert(ct, start);
752 }
753 Ok(())
754 }
755
756 fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
757 self.push("C");
758 let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
759 self.push_disambiguator(stable_crate_id.as_u64());
760 let name = self.tcx.crate_name(cnum);
761 self.push_ident(name.as_str());
762 Ok(())
763 }
764
765 fn path_qualified(
766 &mut self,
767 self_ty: Ty<'tcx>,
768 trait_ref: Option<ty::TraitRef<'tcx>>,
769 ) -> Result<(), PrintError> {
770 assert!(trait_ref.is_some());
771 let trait_ref = trait_ref.unwrap();
772
773 self.push("Y");
774 self_ty.print(self)?;
775 self.print_def_path(trait_ref.def_id, trait_ref.args)
776 }
777
778 fn path_append_impl(
779 &mut self,
780 _: impl FnOnce(&mut Self) -> Result<(), PrintError>,
781 _: &DisambiguatedDefPathData,
782 _: Ty<'tcx>,
783 _: Option<ty::TraitRef<'tcx>>,
784 ) -> Result<(), PrintError> {
785 unreachable!()
787 }
788
789 fn path_append(
790 &mut self,
791 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
792 disambiguated_data: &DisambiguatedDefPathData,
793 ) -> Result<(), PrintError> {
794 let ns = match disambiguated_data.data {
795 DefPathData::ForeignMod => return print_prefix(self),
798
799 DefPathData::TypeNs(_) => 't',
801 DefPathData::ValueNs(_) => 'v',
802 DefPathData::Closure => 'C',
803 DefPathData::Ctor => 'c',
804 DefPathData::AnonConst => 'k',
805 DefPathData::OpaqueTy => 'i',
806
807 DefPathData::CrateRoot
809 | DefPathData::Use
810 | DefPathData::GlobalAsm
811 | DefPathData::Impl
812 | DefPathData::MacroNs(_)
813 | DefPathData::LifetimeNs(_) => {
814 bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)
815 }
816 };
817
818 let name = disambiguated_data.data.get_opt_name();
819
820 self.path_append_ns(
821 print_prefix,
822 ns,
823 disambiguated_data.disambiguator as u64,
824 name.unwrap_or(kw::Empty).as_str(),
825 )
826 }
827
828 fn path_generic_args(
829 &mut self,
830 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
831 args: &[GenericArg<'tcx>],
832 ) -> Result<(), PrintError> {
833 let print_regions = args.iter().any(|arg| match arg.unpack() {
835 GenericArgKind::Lifetime(r) => !r.is_erased(),
836 _ => false,
837 });
838 let args = args.iter().cloned().filter(|arg| match arg.unpack() {
839 GenericArgKind::Lifetime(_) => print_regions,
840 _ => true,
841 });
842
843 if args.clone().next().is_none() {
844 return print_prefix(self);
845 }
846
847 self.push("I");
848 print_prefix(self)?;
849 for arg in args {
850 match arg.unpack() {
851 GenericArgKind::Lifetime(lt) => {
852 lt.print(self)?;
853 }
854 GenericArgKind::Type(ty) => {
855 ty.print(self)?;
856 }
857 GenericArgKind::Const(c) => {
858 self.push("K");
859 c.print(self)?;
860 }
861 }
862 }
863 self.push("E");
864
865 Ok(())
866 }
867}
868pub(crate) fn push_integer_62(x: u64, output: &mut String) {
874 if let Some(x) = x.checked_sub(1) {
875 output.push_str(&x.to_base(62));
876 }
877 output.push('_');
878}
879
880pub(crate) fn encode_integer_62(x: u64) -> String {
881 let mut output = String::new();
882 push_integer_62(x, &mut output);
883 output
884}
885
886pub(crate) fn push_ident(ident: &str, output: &mut String) {
887 let mut use_punycode = false;
888 for b in ident.bytes() {
889 match b {
890 b'_' | b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => {}
891 0x80..=0xff => use_punycode = true,
892 _ => bug!("symbol_names: bad byte {} in ident {:?}", b, ident),
893 }
894 }
895
896 let punycode_string;
897 let ident = if use_punycode {
898 output.push('u');
899
900 let mut punycode_bytes = match punycode::encode(ident) {
902 Ok(s) => s.into_bytes(),
903 Err(()) => bug!("symbol_names: punycode encoding failed for ident {:?}", ident),
904 };
905
906 if let Some(c) = punycode_bytes.iter_mut().rfind(|&&mut c| c == b'-') {
908 *c = b'_';
909 }
910
911 punycode_string = String::from_utf8(punycode_bytes).unwrap();
913 &punycode_string
914 } else {
915 ident
916 };
917
918 let _ = write!(output, "{}", ident.len());
919
920 if let Some('_' | '0'..='9') = ident.chars().next() {
922 output.push('_');
923 }
924
925 output.push_str(ident);
926}