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_hash::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 trait_ref =
653 trait_ref.with_self_ty(p.tcx, p.tcx.types.trait_object_dummy_self);
654 p.print_def_path(trait_ref.def_id, trait_ref.args)?;
655 }
656 ty::ExistentialPredicate::Projection(projection) => {
657 let name = p.tcx.associated_item(projection.def_id).name();
658 p.push("p");
659 p.push_ident(name.as_str());
660 match projection.term.kind() {
661 ty::TermKind::Ty(ty) => ty.print(p),
662 ty::TermKind::Const(c) => {
663 p.push("K");
664 c.print(p)
665 }
666 }?;
667 }
668 ty::ExistentialPredicate::AutoTrait(def_id) => {
669 p.print_def_path(*def_id, &[])?;
670 }
671 }
672 }
673 Ok(())
674 })?;
675
676 self.push("E");
677 Ok(())
678 }
679
680 fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
681 let cv = match ct.kind() {
683 ty::ConstKind::Value(cv) => cv,
684
685 ty::ConstKind::Param(_) => {
688 self.push("p");
690 return Ok(());
691 }
692
693 ty::ConstKind::Unevaluated(ty::UnevaluatedConst { kind, args, .. }) => match kind {
696 ty::UnevaluatedConstKind::Projection { def_id }
697 | ty::UnevaluatedConstKind::Inherent { def_id }
698 | ty::UnevaluatedConstKind::Free { def_id }
699 | ty::UnevaluatedConstKind::Anon { def_id } => {
700 return self.print_def_path(def_id, args);
701 }
702 },
703
704 ty::ConstKind::Expr(_)
705 | ty::ConstKind::Infer(_)
706 | ty::ConstKind::Bound(..)
707 | ty::ConstKind::Placeholder(_)
708 | ty::ConstKind::Error(_) => ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
709 };
710
711 if let Some(&i) = self.consts.get(&ct) {
712 self.print_backref(i)?;
713 return Ok(());
714 }
715
716 let ty::Value { ty: ct_ty, valtree } = cv;
717 let start = self.out.len();
718
719 match ct_ty.kind() {
720 ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => {
721 ct_ty.print(self)?;
722
723 let mut bits = cv
724 .try_to_bits(self.tcx, ty::TypingEnv::fully_monomorphized())
725 .expect("expected const to be monomorphic");
726
727 if let ty::Int(ity) = ct_ty.kind() {
729 let val =
730 Integer::from_int_ty(&self.tcx, *ity).size().sign_extend(bits) as i128;
731 if val < 0 {
732 self.push("n");
733 }
734 bits = val.unsigned_abs();
735 }
736
737 let _ = self.out.write_fmt(format_args!("{0:x}_", bits))write!(self.out, "{bits:x}_");
738 }
739
740 ty::Str => {
742 let tcx = self.tcx();
743 let ref_ty = Ty::new_imm_ref(tcx, tcx.lifetimes.re_static, ct_ty);
746 let cv = ty::Value { ty: ref_ty, valtree };
747 let slice = cv.try_to_raw_bytes(tcx).unwrap_or_else(|| {
748 ::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)
749 });
750 let s = std::str::from_utf8(slice).expect("non utf8 str from MIR interpreter");
751
752 self.push("e");
754
755 for byte in s.bytes() {
757 let _ = self.out.write_fmt(format_args!("{0:02x}", byte))write!(self.out, "{byte:02x}");
758 }
759
760 self.push("_");
761 }
762
763 ty::Ref(_, _, mutbl) => {
766 self.push(match mutbl {
767 hir::Mutability::Not => "R",
768 hir::Mutability::Mut => "Q",
769 });
770
771 let pointee_ty =
772 ct_ty.builtin_deref(true).expect("tried to dereference on non-ptr type");
773 let dereferenced_const = ty::Const::new_value(self.tcx, valtree, pointee_ty);
774 dereferenced_const.print(self)?;
775 }
776
777 ty::Array(..) | ty::Tuple(..) | ty::Slice(_) => {
778 let fields = cv.to_branch().iter().copied();
779
780 let print_field_list = |this: &mut Self| {
781 for field in fields.clone() {
782 field.print(this)?;
783 }
784 this.push("E");
785 Ok(())
786 };
787
788 match *ct_ty.kind() {
789 ty::Array(..) | ty::Slice(_) => {
790 self.push("A");
791 print_field_list(self)?;
792 }
793 ty::Tuple(..) => {
794 self.push("T");
795 print_field_list(self)?;
796 }
797 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
798 }
799 }
800 ty::Adt(def, args) => {
801 let contents = cv.destructure_adt_const();
802 let fields = contents.fields.iter().copied();
803
804 let print_field_list = |this: &mut Self| {
805 for field in fields.clone() {
806 field.print(this)?;
807 }
808 this.push("E");
809 Ok(())
810 };
811
812 let variant_idx = contents.variant;
813 let variant_def = &def.variant(variant_idx);
814
815 self.push("V");
816 self.print_def_path(variant_def.def_id, args)?;
817
818 match variant_def.ctor_kind() {
819 Some(CtorKind::Const) => {
820 self.push("U");
821 }
822 Some(CtorKind::Fn) => {
823 self.push("T");
824 print_field_list(self)?;
825 }
826 None => {
827 self.push("S");
828 for (field_def, field) in iter::zip(&variant_def.fields, fields) {
829 let disambiguated_field =
833 self.tcx.def_key(field_def.did).disambiguated_data;
834 let field_name = disambiguated_field.data.get_opt_name();
835 self.push_disambiguator(disambiguated_field.disambiguator as u64);
836 self.push_ident(field_name.unwrap().as_str());
837
838 field.print(self)?;
839 }
840 self.push("E");
841 }
842 }
843 }
844 _ => {
845 ::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);
846 }
847 }
848
849 if !ct.has_escaping_bound_vars() {
852 self.consts.insert(ct, start);
853 }
854 Ok(())
855 }
856
857 fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
858 self.push("C");
859 if !self.is_exportable {
860 let stable_crate_id = self.tcx.stable_crate_id(cnum);
861 self.push_disambiguator(stable_crate_id.as_u64());
862 }
863 let name = self.tcx.crate_name(cnum);
864 self.push_ident(name.as_str());
865 Ok(())
866 }
867
868 fn print_path_with_qualified(
869 &mut self,
870 self_ty: Ty<'tcx>,
871 trait_ref: Option<ty::TraitRef<'tcx>>,
872 ) -> Result<(), PrintError> {
873 if !trait_ref.is_some() {
::core::panicking::panic("assertion failed: trait_ref.is_some()")
};assert!(trait_ref.is_some());
874 let trait_ref = trait_ref.unwrap();
875
876 self.push("Y");
877 self_ty.print(self)?;
878 self.print_def_path(trait_ref.def_id, trait_ref.args)
879 }
880
881 fn print_path_with_impl(
882 &mut self,
883 _: impl FnOnce(&mut Self) -> Result<(), PrintError>,
884 _: Ty<'tcx>,
885 _: Option<ty::TraitRef<'tcx>>,
886 ) -> Result<(), PrintError> {
887 ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
889 }
890
891 fn print_path_with_simple(
892 &mut self,
893 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
894 disambiguated_data: &DisambiguatedDefPathData,
895 ) -> Result<(), PrintError> {
896 let ns = match disambiguated_data.data {
897 DefPathData::ForeignMod => return print_prefix(self),
900
901 DefPathData::TypeNs(_) => 't',
903 DefPathData::ValueNs(_) => 'v',
904 DefPathData::Closure => 'C',
905 DefPathData::Ctor => 'c',
906 DefPathData::AnonConst => 'K',
907 DefPathData::OpaqueTy => 'i',
908 DefPathData::SyntheticCoroutineBody => 's',
909 DefPathData::NestedStatic => 'n',
910 DefPathData::GlobalAsm => 'a',
911
912 DefPathData::CrateRoot
914 | DefPathData::Use
915 | DefPathData::Impl
916 | DefPathData::MacroNs(_)
917 | DefPathData::LifetimeNs(_)
918 | DefPathData::OpaqueLifetime(_)
919 | DefPathData::AnonAssocTy(..) => {
920 ::rustc_middle::util::bug::bug_fmt(format_args!("symbol_names: unexpected DefPathData: {0:?}",
disambiguated_data.data))bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)
921 }
922 };
923
924 let name = disambiguated_data.data.get_opt_name();
925
926 self.path_append_ns(
927 print_prefix,
928 ns,
929 disambiguated_data.disambiguator as u64,
930 name.unwrap_or(sym::empty).as_str(),
931 )
932 }
933
934 fn print_path_with_generic_args(
935 &mut self,
936 print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
937 args: &[GenericArg<'tcx>],
938 ) -> Result<(), PrintError> {
939 let print_regions = args.iter().any(|arg| match arg.kind() {
941 GenericArgKind::Lifetime(r) => !r.is_erased(),
942 _ => false,
943 });
944 let args = args.iter().cloned().filter(|arg| match arg.kind() {
945 GenericArgKind::Lifetime(_) => print_regions,
946 _ => true,
947 });
948
949 if args.clone().next().is_none() {
950 return print_prefix(self);
951 }
952
953 self.push("I");
954 print_prefix(self)?;
955 for arg in args {
956 match arg.kind() {
957 GenericArgKind::Lifetime(lt) => {
958 lt.print(self)?;
959 }
960 GenericArgKind::Type(ty) => {
961 ty.print(self)?;
962 }
963 GenericArgKind::Const(c) => {
964 self.push("K");
965 c.print(self)?;
966 }
967 }
968 }
969 self.push("E");
970
971 Ok(())
972 }
973}
974pub(crate) fn push_integer_62(x: u64, output: &mut String) {
980 if let Some(x) = x.checked_sub(1) {
981 output.push_str(&x.to_base(62));
982 }
983 output.push('_');
984}
985
986pub(crate) fn encode_integer_62(x: u64) -> String {
987 let mut output = String::new();
988 push_integer_62(x, &mut output);
989 output
990}
991
992pub(crate) fn push_ident(ident: &str, output: &mut String) {
993 let mut use_punycode = false;
994 for b in ident.bytes() {
995 match b {
996 b'_' | b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => {}
997 0x80..=0xff => use_punycode = true,
998 _ => ::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),
999 }
1000 }
1001
1002 let punycode_string;
1003 let ident = if use_punycode {
1004 output.push('u');
1005
1006 let mut punycode_bytes = match punycode::encode(ident) {
1008 Ok(s) => s.into_bytes(),
1009 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),
1010 };
1011
1012 if let Some(c) = punycode_bytes.iter_mut().rfind(|&&mut c| c == b'-') {
1014 *c = b'_';
1015 }
1016
1017 punycode_string = String::from_utf8(punycode_bytes).unwrap();
1019 &punycode_string
1020 } else {
1021 ident
1022 };
1023
1024 let _ = output.write_fmt(format_args!("{0}", ident.len()))write!(output, "{}", ident.len());
1025
1026 if let Some('_' | '0'..='9') = ident.chars().next() {
1028 output.push('_');
1029 }
1030
1031 output.push_str(ident);
1032}