1 use rustc_data_structures::base_n;
2 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4 use rustc_hir::def_id::{CrateNum, DefId};
5 use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
6 use rustc_middle::ty::print::{Print, Printer};
7 use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst};
8 use rustc_middle::ty::{self, FloatTy, Instance, IntTy, Ty, TyCtxt, TypeFoldable, UintTy};
9 use rustc_target::spec::abi::Abi;
16 instance: Instance<'tcx>,
17 instantiating_crate: Option<CrateNum>,
19 let def_id = instance.def_id();
20 // FIXME(eddyb) this should ideally not be needed.
21 let substs = tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), instance.substs);
24 let mut cx = SymbolMangler {
26 compress: Some(Box::new(CompressionCaches {
27 start_offset: prefix.len(),
29 paths: FxHashMap::default(),
30 types: FxHashMap::default(),
31 consts: FxHashMap::default(),
34 out: String::from(prefix),
37 // Append `::{shim:...#0}` to shims that can coexist with a non-shim instance.
38 let shim_kind = match instance.def {
39 ty::InstanceDef::VtableShim(_) => Some("vtable"),
40 ty::InstanceDef::ReifyShim(_) => Some("reify"),
45 cx = if let Some(shim_kind) = shim_kind {
46 cx.path_append_ns(|cx| cx.print_def_path(def_id, substs), 'S', 0, shim_kind).unwrap()
48 cx.print_def_path(def_id, substs).unwrap()
50 if let Some(instantiating_crate) = instantiating_crate {
51 cx = cx.print_def_path(instantiating_crate.as_def_id(), &[]).unwrap();
56 struct CompressionCaches<'tcx> {
57 // The length of the prefix in `out` (e.g. 2 for `_R`).
60 // The values are start positions in `out`, in bytes.
61 paths: FxHashMap<(DefId, &'tcx [GenericArg<'tcx>]), usize>,
62 types: FxHashMap<Ty<'tcx>, usize>,
63 consts: FxHashMap<&'tcx ty::Const<'tcx>, usize>,
67 /// The range of distances from the root of what's
68 /// being printed, to the lifetimes in a binder.
69 /// Specifically, a `BrAnon(i)` lifetime has depth
70 /// `lifetime_depths.start + i`, going away from the
71 /// the root and towards its use site, as `i` increases.
72 /// This is used to flatten rustc's pairing of `BrAnon`
73 /// (intra-binder disambiguation) with a `DebruijnIndex`
74 /// (binder addressing), to "true" de Bruijn indices,
75 /// by subtracting the depth of a certain lifetime, from
76 /// the innermost depth at its use site.
77 lifetime_depths: Range<u32>,
80 struct SymbolMangler<'tcx> {
82 compress: Option<Box<CompressionCaches<'tcx>>>,
83 binders: Vec<BinderLevel>,
87 impl SymbolMangler<'tcx> {
88 fn push(&mut self, s: &str) {
92 /// Push a `_`-terminated base 62 integer, using the format
93 /// specified in the RFC as `<base-62-number>`, that is:
94 /// * `x = 0` is encoded as just the `"_"` terminator
95 /// * `x > 0` is encoded as `x - 1` in base 62, followed by `"_"`,
96 /// e.g. `1` becomes `"0_"`, `62` becomes `"Z_"`, etc.
97 fn push_integer_62(&mut self, x: u64) {
98 if let Some(x) = x.checked_sub(1) {
99 base_n::push_str(x as u128, 62, &mut self.out);
104 /// Push a `tag`-prefixed base 62 integer, when larger than `0`, that is:
105 /// * `x = 0` is encoded as `""` (nothing)
106 /// * `x > 0` is encoded as the `tag` followed by `push_integer_62(x - 1)`
107 /// e.g. `1` becomes `tag + "_"`, `2` becomes `tag + "0_"`, etc.
108 fn push_opt_integer_62(&mut self, tag: &str, x: u64) {
109 if let Some(x) = x.checked_sub(1) {
111 self.push_integer_62(x);
115 fn push_disambiguator(&mut self, dis: u64) {
116 self.push_opt_integer_62("s", dis);
119 fn push_ident(&mut self, ident: &str) {
120 let mut use_punycode = false;
121 for b in ident.bytes() {
123 b'_' | b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => {}
124 0x80..=0xff => use_punycode = true,
125 _ => bug!("symbol_names: bad byte {} in ident {:?}", b, ident),
130 let ident = if use_punycode {
133 // FIXME(eddyb) we should probably roll our own punycode implementation.
134 let mut punycode_bytes = match punycode::encode(ident) {
135 Ok(s) => s.into_bytes(),
136 Err(()) => bug!("symbol_names: punycode encoding failed for ident {:?}", ident),
139 // Replace `-` with `_`.
140 if let Some(c) = punycode_bytes.iter_mut().rfind(|&&mut c| c == b'-') {
144 // FIXME(eddyb) avoid rechecking UTF-8 validity.
145 punycode_string = String::from_utf8(punycode_bytes).unwrap();
151 let _ = write!(self.out, "{}", ident.len());
153 // Write a separating `_` if necessary (leading digit or `_`).
154 if let Some('_' | '0'..='9') = ident.chars().next() {
163 print_prefix: impl FnOnce(Self) -> Result<Self, !>,
167 ) -> Result<Self, !> {
170 self = print_prefix(self)?;
171 self.push_disambiguator(disambiguator as u64);
172 self.push_ident(name);
176 fn print_backref(mut self, i: usize) -> Result<Self, !> {
178 self.push_integer_62((i - self.compress.as_ref().unwrap().start_offset) as u64);
184 value: &ty::Binder<'tcx, T>,
185 print_value: impl FnOnce(Self, &T) -> Result<Self, !>,
188 T: TypeFoldable<'tcx>,
190 let regions = if value.has_late_bound_regions() {
191 self.tcx.collect_referenced_late_bound_regions(value)
196 let mut lifetime_depths =
197 self.binders.last().map(|b| b.lifetime_depths.end).map_or(0..0, |i| i..i);
199 let lifetimes = regions
203 _ => bug!("symbol_names: non-anonymized region `{:?}` in `{:?}`", br, value),
206 .map_or(0, |max| max + 1);
208 self.push_opt_integer_62("G", lifetimes as u64);
209 lifetime_depths.end += lifetimes;
211 self.binders.push(BinderLevel { lifetime_depths });
212 self = print_value(self, value.as_ref().skip_binder())?;
219 impl Printer<'tcx> for SymbolMangler<'tcx> {
225 type DynExistential = Self;
228 fn tcx(&self) -> TyCtxt<'tcx> {
235 substs: &'tcx [GenericArg<'tcx>],
236 ) -> Result<Self::Path, Self::Error> {
237 if let Some(&i) = self.compress.as_ref().and_then(|c| c.paths.get(&(def_id, substs))) {
238 return self.print_backref(i);
240 let start = self.out.len();
242 self = self.default_print_def_path(def_id, substs)?;
244 // Only cache paths that do not refer to an enclosing
245 // binder (which would change depending on context).
246 if !substs.iter().any(|k| k.has_escaping_bound_vars()) {
247 if let Some(c) = &mut self.compress {
248 c.paths.insert((def_id, substs), start);
257 substs: &'tcx [GenericArg<'tcx>],
258 mut self_ty: Ty<'tcx>,
259 mut impl_trait_ref: Option<ty::TraitRef<'tcx>>,
260 ) -> Result<Self::Path, Self::Error> {
261 let key = self.tcx.def_key(impl_def_id);
262 let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id };
264 let mut param_env = self.tcx.param_env_reveal_all_normalized(impl_def_id);
265 if !substs.is_empty() {
266 param_env = param_env.subst(self.tcx, substs);
269 match &mut impl_trait_ref {
270 Some(impl_trait_ref) => {
271 assert_eq!(impl_trait_ref.self_ty(), self_ty);
272 *impl_trait_ref = self.tcx.normalize_erasing_regions(param_env, *impl_trait_ref);
273 self_ty = impl_trait_ref.self_ty();
276 self_ty = self.tcx.normalize_erasing_regions(param_env, self_ty);
280 self.push(match impl_trait_ref {
285 // Encode impl generic params if the substitutions contain parameters (implying
286 // polymorphization is enabled) and this isn't an inherent impl.
287 if impl_trait_ref.is_some() && substs.iter().any(|a| a.has_param_types_or_consts()) {
288 self = self.path_generic_args(
291 |cx| cx.print_def_path(parent_def_id, &[]),
293 key.disambiguated_data.disambiguator as u64,
300 self.push_disambiguator(key.disambiguated_data.disambiguator as u64);
301 self = self.print_def_path(parent_def_id, &[])?;
304 self = self_ty.print(self)?;
306 if let Some(trait_ref) = impl_trait_ref {
307 self = self.print_def_path(trait_ref.def_id, trait_ref.substs)?;
313 fn print_region(mut self, region: ty::Region<'_>) -> Result<Self::Region, Self::Error> {
314 let i = match *region {
315 // Erased lifetimes use the index 0, for a
316 // shorter mangling of `L_`.
319 // Late-bound lifetimes use indices starting at 1,
320 // see `BinderLevel` for more details.
321 ty::ReLateBound(debruijn, ty::BoundRegion { kind: ty::BrAnon(i), .. }) => {
322 let binder = &self.binders[self.binders.len() - 1 - debruijn.index()];
323 let depth = binder.lifetime_depths.start + i;
325 1 + (self.binders.last().unwrap().lifetime_depths.end - 1 - depth)
328 _ => bug!("symbol_names: non-erased region `{:?}`", region),
331 self.push_integer_62(i as u64);
335 fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
336 // Basic types, never cached (single-character).
337 let basic_type = match ty.kind() {
341 ty::Tuple(_) if ty.is_unit() => "u",
342 ty::Int(IntTy::I8) => "a",
343 ty::Int(IntTy::I16) => "s",
344 ty::Int(IntTy::I32) => "l",
345 ty::Int(IntTy::I64) => "x",
346 ty::Int(IntTy::I128) => "n",
347 ty::Int(IntTy::Isize) => "i",
348 ty::Uint(UintTy::U8) => "h",
349 ty::Uint(UintTy::U16) => "t",
350 ty::Uint(UintTy::U32) => "m",
351 ty::Uint(UintTy::U64) => "y",
352 ty::Uint(UintTy::U128) => "o",
353 ty::Uint(UintTy::Usize) => "j",
354 ty::Float(FloatTy::F32) => "f",
355 ty::Float(FloatTy::F64) => "d",
358 // Placeholders (should be demangled as `_`).
359 ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => "p",
363 if !basic_type.is_empty() {
364 self.push(basic_type);
368 if let Some(&i) = self.compress.as_ref().and_then(|c| c.types.get(&ty)) {
369 return self.print_backref(i);
371 let start = self.out.len();
374 // Basic types, handled above.
375 ty::Bool | ty::Char | ty::Str | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Never => {
378 ty::Tuple(_) if ty.is_unit() => unreachable!(),
380 // Placeholders, also handled as part of basic types.
381 ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => {
385 ty::Ref(r, ty, mutbl) => {
386 self.push(match mutbl {
387 hir::Mutability::Not => "R",
388 hir::Mutability::Mut => "Q",
390 if *r != ty::ReErased {
391 self = r.print(self)?;
393 self = ty.print(self)?;
397 self.push(match mt.mutbl {
398 hir::Mutability::Not => "P",
399 hir::Mutability::Mut => "O",
401 self = mt.ty.print(self)?;
404 ty::Array(ty, len) => {
406 self = ty.print(self)?;
407 self = self.print_const(len)?;
411 self = ty.print(self)?;
416 for ty in tys.iter().map(|k| k.expect_ty()) {
417 self = ty.print(self)?;
422 // Mangle all nominal types as paths.
423 ty::Adt(&ty::AdtDef { did: def_id, .. }, substs)
424 | ty::FnDef(def_id, substs)
425 | ty::Opaque(def_id, substs)
426 | ty::Projection(ty::ProjectionTy { item_def_id: def_id, substs })
427 | ty::Closure(def_id, substs)
428 | ty::Generator(def_id, substs, _) => {
429 self = self.print_def_path(def_id, substs)?;
431 ty::Foreign(def_id) => {
432 self = self.print_def_path(def_id, &[])?;
437 self = self.in_binder(&sig, |mut cx, sig| {
438 if sig.unsafety == hir::Unsafety::Unsafe {
443 Abi::C { unwind: false } => cx.push("KC"),
446 let name = abi.name();
447 if name.contains('-') {
448 cx.push_ident(&name.replace('-', "_"));
454 for &ty in sig.inputs() {
461 sig.output().print(cx)
465 ty::Dynamic(predicates, r) => {
467 self = self.print_dyn_existential(predicates)?;
468 self = r.print(self)?;
471 ty::GeneratorWitness(_) => bug!("symbol_names: unexpected `GeneratorWitness`"),
474 // Only cache types that do not refer to an enclosing
475 // binder (which would change depending on context).
476 if !ty.has_escaping_bound_vars() {
477 if let Some(c) = &mut self.compress {
478 c.types.insert(ty, start);
484 fn print_dyn_existential(
486 predicates: &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
487 ) -> Result<Self::DynExistential, Self::Error> {
488 // Okay, so this is a bit tricky. Imagine we have a trait object like
489 // `dyn for<'a> Foo<'a, Bar = &'a ()>`. When we mangle this, the
490 // output looks really close to the syntax, where the `Bar = &'a ()` bit
491 // is under the same binders (`['a]`) as the `Foo<'a>` bit. However, we
492 // actually desugar these into two separate `ExistentialPredicate`s. We
493 // can't enter/exit the "binder scope" twice though, because then we
494 // would mangle the binders twice. (Also, side note, we merging these
495 // two is kind of difficult, because of potential HRTBs in the Projection
498 // Also worth mentioning: imagine that we instead had
499 // `dyn for<'a> Foo<'a, Bar = &'a ()> + Send`. In this case, `Send` is
500 // under the same binders as `Foo`. Currently, this doesn't matter,
501 // because only *auto traits* are allowed other than the principal trait
502 // and all auto traits don't have any generics. Two things could
503 // make this not an "okay" mangling:
504 // 1) Instead of mangling only *used*
505 // bound vars, we want to mangle *all* bound vars (`for<'b> Send` is a
506 // valid trait predicate);
507 // 2) We allow multiple "principal" traits in the future, or at least
508 // allow in any form another trait predicate that can take generics.
510 // Here we assume that predicates have the following structure:
511 // [<Trait> [{<Projection>}]] [{<Auto>}]
512 // Since any predicates after the first one shouldn't change the binders,
513 // just put them all in the binders of the first.
514 self = self.in_binder(&predicates[0], |mut cx, _| {
515 for predicate in predicates.iter() {
516 // It would be nice to be able to validate bound vars here, but
517 // projections can actually include bound vars from super traits
518 // because of HRTBs (only in the `Self` type). Also, auto traits
519 // could have different bound vars *anyways*.
520 match predicate.as_ref().skip_binder() {
521 ty::ExistentialPredicate::Trait(trait_ref) => {
522 // Use a type that can't appear in defaults of type parameters.
523 let dummy_self = cx.tcx.mk_ty_infer(ty::FreshTy(0));
524 let trait_ref = trait_ref.with_self_ty(cx.tcx, dummy_self);
525 cx = cx.print_def_path(trait_ref.def_id, trait_ref.substs)?;
527 ty::ExistentialPredicate::Projection(projection) => {
528 let name = cx.tcx.associated_item(projection.item_def_id).ident;
530 cx.push_ident(&name.as_str());
531 cx = projection.ty.print(cx)?;
533 ty::ExistentialPredicate::AutoTrait(def_id) => {
534 cx = cx.print_def_path(*def_id, &[])?;
545 fn print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
546 if let Some(&i) = self.compress.as_ref().and_then(|c| c.consts.get(&ct)) {
547 return self.print_backref(i);
549 let start = self.out.len();
552 let val = match ct.ty.kind() {
553 ty::Uint(_) | ty::Bool | ty::Char => {
554 ct.try_eval_bits(self.tcx, ty::ParamEnv::reveal_all(), ct.ty)
557 let param_env = ty::ParamEnv::reveal_all();
558 ct.try_eval_bits(self.tcx, param_env, ct.ty).and_then(|b| {
559 let sz = self.tcx.layout_of(param_env.and(ct.ty)).ok()?.size;
560 let val = sz.sign_extend(b) as i128;
564 Some(val.unsigned_abs())
568 bug!("symbol_names: unsupported constant of type `{}` ({:?})", ct.ty, ct);
572 if let Some(bits) = val {
573 // We only print the type if the const can be evaluated.
574 self = ct.ty.print(self)?;
575 let _ = write!(self.out, "{}{:x}_", if neg { "n" } else { "" }, bits);
577 // NOTE(eddyb) despite having the path, we need to
578 // encode a placeholder, as the path could refer
579 // back to e.g. an `impl` using the constant.
583 // Only cache consts that do not refer to an enclosing
584 // binder (which would change depending on context).
585 if !ct.has_escaping_bound_vars() {
586 if let Some(c) = &mut self.compress {
587 c.consts.insert(ct, start);
593 fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
595 let fingerprint = self.tcx.crate_disambiguator(cnum).to_fingerprint();
596 self.push_disambiguator(fingerprint.to_smaller_hash());
597 let name = self.tcx.crate_name(cnum).as_str();
598 self.push_ident(&name);
605 trait_ref: Option<ty::TraitRef<'tcx>>,
606 ) -> Result<Self::Path, Self::Error> {
607 assert!(trait_ref.is_some());
608 let trait_ref = trait_ref.unwrap();
611 self = self_ty.print(self)?;
612 self.print_def_path(trait_ref.def_id, trait_ref.substs)
617 _: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
618 _: &DisambiguatedDefPathData,
620 _: Option<ty::TraitRef<'tcx>>,
621 ) -> Result<Self::Path, Self::Error> {
622 // Inlined into `print_impl_path`
628 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
629 disambiguated_data: &DisambiguatedDefPathData,
630 ) -> Result<Self::Path, Self::Error> {
631 let ns = match disambiguated_data.data {
632 // Uppercase categories are more stable than lowercase ones.
633 DefPathData::TypeNs(_) => 't',
634 DefPathData::ValueNs(_) => 'v',
635 DefPathData::ClosureExpr => 'C',
636 DefPathData::Ctor => 'c',
637 DefPathData::AnonConst => 'k',
638 DefPathData::ImplTrait => 'i',
640 // These should never show up as `path_append` arguments.
641 DefPathData::CrateRoot
644 | DefPathData::MacroNs(_)
645 | DefPathData::LifetimeNs(_) => {
646 bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)
650 let name = disambiguated_data.data.get_opt_name().map(|s| s.as_str());
655 disambiguated_data.disambiguator as u64,
656 name.as_ref().map_or("", |s| &s[..]),
660 fn path_generic_args(
662 print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
663 args: &[GenericArg<'tcx>],
664 ) -> Result<Self::Path, Self::Error> {
665 // Don't print any regions if they're all erased.
666 let print_regions = args.iter().any(|arg| match arg.unpack() {
667 GenericArgKind::Lifetime(r) => *r != ty::ReErased,
670 let args = args.iter().cloned().filter(|arg| match arg.unpack() {
671 GenericArgKind::Lifetime(_) => print_regions,
675 if args.clone().next().is_none() {
676 return print_prefix(self);
680 self = print_prefix(self)?;
683 GenericArgKind::Lifetime(lt) => {
684 self = lt.print(self)?;
686 GenericArgKind::Type(ty) => {
687 self = ty.print(self)?;
689 GenericArgKind::Const(c) => {
691 self = c.print(self)?;