rustc_symbol_mangling/
v0.rs

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    // FIXME(eddyb) this should ideally not be needed.
33    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    // Append `::{shim:...#0}` to shims that can coexist with a non-shim instance.
48    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        // FIXME(async_closures): This shouldn't be needed when we fix
56        // `Instance::ty`/`Instance::def_id`.
57        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        // rust_eh_personality must not be renamed as LLVM hard-codes the name
87        return "rust_eh_personality".to_owned();
88    }
89
90    let prefix = "_R";
91    let mut cx: SymbolMangler<'_> = SymbolMangler {
92        tcx,
93        start_offset: prefix.len(),
94        is_exportable: false,
95        paths: FxHashMap::default(),
96        types: FxHashMap::default(),
97        consts: FxHashMap::default(),
98        binders: vec![],
99        out: String::from(prefix),
100    };
101
102    cx.path_append_ns(
103        |cx| {
104            cx.push("C");
105            cx.push_disambiguator({
106                let mut hasher = StableHasher::new();
107                // Incorporate the rustc version to ensure #[rustc_std_internal_symbol] functions
108                // get a different symbol name depending on the rustc version.
109                //
110                // RUSTC_FORCE_RUSTC_VERSION is ignored here as otherwise different we would get an
111                // abi incompatibility with the standard library.
112                hasher.write(tcx.sess.cfg_version.as_bytes());
113
114                let hash: Hash64 = hasher.finish();
115                hash.as_u64()
116            });
117            cx.push_ident("__rustc");
118            Ok(())
119        },
120        'v',
121        0,
122        item_name,
123    )
124    .unwrap();
125
126    std::mem::take(&mut cx.out)
127}
128
129pub(super) fn mangle_typeid_for_trait_ref<'tcx>(
130    tcx: TyCtxt<'tcx>,
131    trait_ref: ty::ExistentialTraitRef<'tcx>,
132) -> String {
133    // FIXME(flip1995): See comment in `mangle_typeid_for_fnabi`.
134    let mut cx = SymbolMangler {
135        tcx,
136        start_offset: 0,
137        is_exportable: false,
138        paths: FxHashMap::default(),
139        types: FxHashMap::default(),
140        consts: FxHashMap::default(),
141        binders: vec![],
142        out: String::new(),
143    };
144    cx.print_def_path(trait_ref.def_id, &[]).unwrap();
145    std::mem::take(&mut cx.out)
146}
147
148struct BinderLevel {
149    /// The range of distances from the root of what's
150    /// being printed, to the lifetimes in a binder.
151    /// Specifically, a `BrAnon` lifetime has depth
152    /// `lifetime_depths.start + index`, going away from the
153    /// the root and towards its use site, as the var index increases.
154    /// This is used to flatten rustc's pairing of `BrAnon`
155    /// (intra-binder disambiguation) with a `DebruijnIndex`
156    /// (binder addressing), to "true" de Bruijn indices,
157    /// by subtracting the depth of a certain lifetime, from
158    /// the innermost depth at its use site.
159    lifetime_depths: Range<u32>,
160}
161
162struct SymbolMangler<'tcx> {
163    tcx: TyCtxt<'tcx>,
164    binders: Vec<BinderLevel>,
165    out: String,
166    is_exportable: bool,
167
168    /// The length of the prefix in `out` (e.g. 2 for `_R`).
169    start_offset: usize,
170    /// The values are start positions in `out`, in bytes.
171    paths: FxHashMap<(DefId, &'tcx [GenericArg<'tcx>]), usize>,
172    types: FxHashMap<Ty<'tcx>, usize>,
173    consts: FxHashMap<ty::Const<'tcx>, usize>,
174}
175
176impl<'tcx> SymbolMangler<'tcx> {
177    fn push(&mut self, s: &str) {
178        self.out.push_str(s);
179    }
180
181    /// Push a `_`-terminated base 62 integer, using the format
182    /// specified in the RFC as `<base-62-number>`, that is:
183    /// * `x = 0` is encoded as just the `"_"` terminator
184    /// * `x > 0` is encoded as `x - 1` in base 62, followed by `"_"`,
185    ///   e.g. `1` becomes `"0_"`, `62` becomes `"Z_"`, etc.
186    fn push_integer_62(&mut self, x: u64) {
187        push_integer_62(x, &mut self.out)
188    }
189
190    /// Push a `tag`-prefixed base 62 integer, when larger than `0`, that is:
191    /// * `x = 0` is encoded as `""` (nothing)
192    /// * `x > 0` is encoded as the `tag` followed by `push_integer_62(x - 1)`
193    ///   e.g. `1` becomes `tag + "_"`, `2` becomes `tag + "0_"`, etc.
194    fn push_opt_integer_62(&mut self, tag: &str, x: u64) {
195        if let Some(x) = x.checked_sub(1) {
196            self.push(tag);
197            self.push_integer_62(x);
198        }
199    }
200
201    fn push_disambiguator(&mut self, dis: u64) {
202        self.push_opt_integer_62("s", dis);
203    }
204
205    fn push_ident(&mut self, ident: &str) {
206        push_ident(ident, &mut self.out)
207    }
208
209    fn path_append_ns(
210        &mut self,
211        print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
212        ns: char,
213        disambiguator: u64,
214        name: &str,
215    ) -> Result<(), PrintError> {
216        self.push("N");
217        self.out.push(ns);
218        print_prefix(self)?;
219        self.push_disambiguator(disambiguator);
220        self.push_ident(name);
221        Ok(())
222    }
223
224    fn print_backref(&mut self, i: usize) -> Result<(), PrintError> {
225        self.push("B");
226        self.push_integer_62((i - self.start_offset) as u64);
227        Ok(())
228    }
229
230    fn wrap_binder<T>(
231        &mut self,
232        value: &ty::Binder<'tcx, T>,
233        print_value: impl FnOnce(&mut Self, &T) -> Result<(), PrintError>,
234    ) -> Result<(), PrintError>
235    where
236        T: TypeVisitable<TyCtxt<'tcx>>,
237    {
238        let mut lifetime_depths =
239            self.binders.last().map(|b| b.lifetime_depths.end).map_or(0..0, |i| i..i);
240
241        // FIXME(non-lifetime-binders): What to do here?
242        let lifetimes = value
243            .bound_vars()
244            .iter()
245            .filter(|var| matches!(var, ty::BoundVariableKind::Region(..)))
246            .count() as u32;
247
248        self.push_opt_integer_62("G", lifetimes as u64);
249        lifetime_depths.end += lifetimes;
250
251        self.binders.push(BinderLevel { lifetime_depths });
252        print_value(self, value.as_ref().skip_binder())?;
253        self.binders.pop();
254
255        Ok(())
256    }
257
258    fn print_pat(&mut self, pat: ty::Pattern<'tcx>) -> Result<(), std::fmt::Error> {
259        Ok(match *pat {
260            ty::PatternKind::Range { start, end } => {
261                let consts = [start, end];
262                for ct in consts {
263                    Ty::new_array_with_const_len(self.tcx, self.tcx.types.unit, ct).print(self)?;
264                }
265            }
266            ty::PatternKind::Or(patterns) => {
267                for pat in patterns {
268                    self.print_pat(pat)?;
269                }
270            }
271        })
272    }
273}
274
275impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
276    fn tcx(&self) -> TyCtxt<'tcx> {
277        self.tcx
278    }
279
280    fn print_def_path(
281        &mut self,
282        def_id: DefId,
283        args: &'tcx [GenericArg<'tcx>],
284    ) -> Result<(), PrintError> {
285        if let Some(&i) = self.paths.get(&(def_id, args)) {
286            return self.print_backref(i);
287        }
288        let start = self.out.len();
289
290        self.default_print_def_path(def_id, args)?;
291
292        // Only cache paths that do not refer to an enclosing
293        // binder (which would change depending on context).
294        if !args.iter().any(|k| k.has_escaping_bound_vars()) {
295            self.paths.insert((def_id, args), start);
296        }
297        Ok(())
298    }
299
300    fn print_impl_path(
301        &mut self,
302        impl_def_id: DefId,
303        args: &'tcx [GenericArg<'tcx>],
304    ) -> Result<(), PrintError> {
305        let key = self.tcx.def_key(impl_def_id);
306        let parent_def_id = DefId { index: key.parent.unwrap(), ..impl_def_id };
307
308        let self_ty = self.tcx.type_of(impl_def_id);
309        let impl_trait_ref = self.tcx.impl_trait_ref(impl_def_id);
310        let generics = self.tcx.generics_of(impl_def_id);
311        // We have two cases to worry about here:
312        // 1. We're printing a nested item inside of an impl item, like an inner
313        // function inside of a method. Due to the way that def path printing works,
314        // we'll render this something like `<Ty as Trait>::method::inner_fn`
315        // but we have no substs for this impl since it's not really inheriting
316        // generics from the outer item. We need to use the identity substs, and
317        // to normalize we need to use the correct param-env too.
318        // 2. We're mangling an item with identity substs. This seems to only happen
319        // when generating coverage, since we try to generate coverage for unused
320        // items too, and if something isn't monomorphized then we necessarily don't
321        // have anything to substitute the instance with.
322        // NOTE: We don't support mangling partially substituted but still polymorphic
323        // instances, like `impl<A> Tr<A> for ()` where `A` is substituted w/ `(T,)`.
324        let (typing_env, mut self_ty, mut impl_trait_ref) = if generics.count() > args.len()
325            || &args[..generics.count()]
326                == self
327                    .tcx
328                    .erase_regions(ty::GenericArgs::identity_for_item(self.tcx, impl_def_id))
329                    .as_slice()
330        {
331            (
332                ty::TypingEnv::post_analysis(self.tcx, impl_def_id),
333                self_ty.instantiate_identity(),
334                impl_trait_ref.map(|impl_trait_ref| impl_trait_ref.instantiate_identity()),
335            )
336        } else {
337            assert!(
338                !args.has_non_region_param(),
339                "should not be mangling partially substituted \
340                polymorphic instance: {impl_def_id:?} {args:?}"
341            );
342            (
343                ty::TypingEnv::fully_monomorphized(),
344                self_ty.instantiate(self.tcx, args),
345                impl_trait_ref.map(|impl_trait_ref| impl_trait_ref.instantiate(self.tcx, args)),
346            )
347        };
348
349        match &mut impl_trait_ref {
350            Some(impl_trait_ref) => {
351                assert_eq!(impl_trait_ref.self_ty(), self_ty);
352                *impl_trait_ref = self.tcx.normalize_erasing_regions(typing_env, *impl_trait_ref);
353                self_ty = impl_trait_ref.self_ty();
354            }
355            None => {
356                self_ty = self.tcx.normalize_erasing_regions(typing_env, self_ty);
357            }
358        }
359
360        self.push(match impl_trait_ref {
361            Some(_) => "X",
362            None => "M",
363        });
364
365        // Encode impl generic params if the generic parameters contain non-region parameters
366        // and this isn't an inherent impl.
367        if impl_trait_ref.is_some() && args.iter().any(|a| a.has_non_region_param()) {
368            self.path_generic_args(
369                |this| {
370                    this.path_append_ns(
371                        |cx| cx.print_def_path(parent_def_id, &[]),
372                        'I',
373                        key.disambiguated_data.disambiguator as u64,
374                        "",
375                    )
376                },
377                args,
378            )?;
379        } else {
380            let exported_impl_order = self.tcx.stable_order_of_exportable_impls(impl_def_id.krate);
381            let disambiguator = match self.is_exportable {
382                true => exported_impl_order[&impl_def_id] as u64,
383                false => {
384                    exported_impl_order.len() as u64 + key.disambiguated_data.disambiguator as u64
385                }
386            };
387            self.push_disambiguator(disambiguator);
388            self.print_def_path(parent_def_id, &[])?;
389        }
390
391        self_ty.print(self)?;
392
393        if let Some(trait_ref) = impl_trait_ref {
394            self.print_def_path(trait_ref.def_id, trait_ref.args)?;
395        }
396
397        Ok(())
398    }
399
400    fn print_region(&mut self, region: ty::Region<'_>) -> Result<(), PrintError> {
401        let i = match region.kind() {
402            // Erased lifetimes use the index 0, for a
403            // shorter mangling of `L_`.
404            ty::ReErased => 0,
405
406            // Bound lifetimes use indices starting at 1,
407            // see `BinderLevel` for more details.
408            ty::ReBound(debruijn, ty::BoundRegion { var, kind: ty::BoundRegionKind::Anon }) => {
409                let binder = &self.binders[self.binders.len() - 1 - debruijn.index()];
410                let depth = binder.lifetime_depths.start + var.as_u32();
411
412                1 + (self.binders.last().unwrap().lifetime_depths.end - 1 - depth)
413            }
414
415            _ => bug!("symbol_names: non-erased region `{:?}`", region),
416        };
417        self.push("L");
418        self.push_integer_62(i as u64);
419        Ok(())
420    }
421
422    fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
423        // Basic types, never cached (single-character).
424        let basic_type = match ty.kind() {
425            ty::Bool => "b",
426            ty::Char => "c",
427            ty::Str => "e",
428            ty::Tuple(_) if ty.is_unit() => "u",
429            ty::Int(IntTy::I8) => "a",
430            ty::Int(IntTy::I16) => "s",
431            ty::Int(IntTy::I32) => "l",
432            ty::Int(IntTy::I64) => "x",
433            ty::Int(IntTy::I128) => "n",
434            ty::Int(IntTy::Isize) => "i",
435            ty::Uint(UintTy::U8) => "h",
436            ty::Uint(UintTy::U16) => "t",
437            ty::Uint(UintTy::U32) => "m",
438            ty::Uint(UintTy::U64) => "y",
439            ty::Uint(UintTy::U128) => "o",
440            ty::Uint(UintTy::Usize) => "j",
441            ty::Float(FloatTy::F16) => "C3f16",
442            ty::Float(FloatTy::F32) => "f",
443            ty::Float(FloatTy::F64) => "d",
444            ty::Float(FloatTy::F128) => "C4f128",
445            ty::Never => "z",
446
447            // Should only be encountered within the identity-substituted
448            // impl header of an item nested within an impl item.
449            ty::Param(_) => "p",
450
451            ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => bug!(),
452
453            _ => "",
454        };
455        if !basic_type.is_empty() {
456            self.push(basic_type);
457            return Ok(());
458        }
459
460        if let Some(&i) = self.types.get(&ty) {
461            return self.print_backref(i);
462        }
463        let start = self.out.len();
464
465        match *ty.kind() {
466            // Basic types, handled above.
467            ty::Bool | ty::Char | ty::Str | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Never => {
468                unreachable!()
469            }
470            ty::Tuple(_) if ty.is_unit() => unreachable!(),
471
472            // Placeholders, also handled as part of basic types.
473            ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => {
474                unreachable!()
475            }
476
477            ty::Ref(r, ty, mutbl) => {
478                self.push(match mutbl {
479                    hir::Mutability::Not => "R",
480                    hir::Mutability::Mut => "Q",
481                });
482                if !r.is_erased() {
483                    r.print(self)?;
484                }
485                ty.print(self)?;
486            }
487
488            ty::RawPtr(ty, mutbl) => {
489                self.push(match mutbl {
490                    hir::Mutability::Not => "P",
491                    hir::Mutability::Mut => "O",
492                });
493                ty.print(self)?;
494            }
495
496            ty::Pat(ty, pat) => {
497                // HACK: Represent as tuple until we have something better.
498                // HACK: constants are used in arrays, even if the types don't match.
499                self.push("T");
500                ty.print(self)?;
501                self.print_pat(pat)?;
502                self.push("E");
503            }
504
505            ty::Array(ty, len) => {
506                self.push("A");
507                ty.print(self)?;
508                self.print_const(len)?;
509            }
510            ty::Slice(ty) => {
511                self.push("S");
512                ty.print(self)?;
513            }
514
515            ty::Tuple(tys) => {
516                self.push("T");
517                for ty in tys.iter() {
518                    ty.print(self)?;
519                }
520                self.push("E");
521            }
522
523            // Mangle all nominal types as paths.
524            ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), args)
525            | ty::FnDef(def_id, args)
526            | ty::Closure(def_id, args)
527            | ty::CoroutineClosure(def_id, args)
528            | ty::Coroutine(def_id, args) => {
529                self.print_def_path(def_id, args)?;
530            }
531
532            // We may still encounter projections here due to the printing
533            // logic sometimes passing identity-substituted impl headers.
534            ty::Alias(ty::Projection, ty::AliasTy { def_id, args, .. }) => {
535                self.print_def_path(def_id, args)?;
536            }
537
538            ty::Foreign(def_id) => {
539                self.print_def_path(def_id, &[])?;
540            }
541
542            ty::FnPtr(sig_tys, hdr) => {
543                let sig = sig_tys.with(hdr);
544                self.push("F");
545                self.wrap_binder(&sig, |cx, sig| {
546                    if sig.safety.is_unsafe() {
547                        cx.push("U");
548                    }
549                    match sig.abi {
550                        ExternAbi::Rust => {}
551                        ExternAbi::C { unwind: false } => cx.push("KC"),
552                        abi => {
553                            cx.push("K");
554                            let name = abi.as_str();
555                            if name.contains('-') {
556                                cx.push_ident(&name.replace('-', "_"));
557                            } else {
558                                cx.push_ident(name);
559                            }
560                        }
561                    }
562                    for &ty in sig.inputs() {
563                        ty.print(cx)?;
564                    }
565                    if sig.c_variadic {
566                        cx.push("v");
567                    }
568                    cx.push("E");
569                    sig.output().print(cx)
570                })?;
571            }
572
573            // FIXME(unsafe_binder):
574            ty::UnsafeBinder(..) => todo!(),
575
576            ty::Dynamic(predicates, r, kind) => {
577                self.push(match kind {
578                    ty::Dyn => "D",
579                    // FIXME(dyn-star): need to update v0 mangling docs
580                    ty::DynStar => "D*",
581                });
582                self.print_dyn_existential(predicates)?;
583                r.print(self)?;
584            }
585
586            ty::Alias(..) => bug!("symbol_names: unexpected alias"),
587            ty::CoroutineWitness(..) => bug!("symbol_names: unexpected `CoroutineWitness`"),
588        }
589
590        // Only cache types that do not refer to an enclosing
591        // binder (which would change depending on context).
592        if !ty.has_escaping_bound_vars() {
593            self.types.insert(ty, start);
594        }
595        Ok(())
596    }
597
598    fn print_dyn_existential(
599        &mut self,
600        predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
601    ) -> Result<(), PrintError> {
602        // Okay, so this is a bit tricky. Imagine we have a trait object like
603        // `dyn for<'a> Foo<'a, Bar = &'a ()>`. When we mangle this, the
604        // output looks really close to the syntax, where the `Bar = &'a ()` bit
605        // is under the same binders (`['a]`) as the `Foo<'a>` bit. However, we
606        // actually desugar these into two separate `ExistentialPredicate`s. We
607        // can't enter/exit the "binder scope" twice though, because then we
608        // would mangle the binders twice. (Also, side note, we merging these
609        // two is kind of difficult, because of potential HRTBs in the Projection
610        // predicate.)
611        //
612        // Also worth mentioning: imagine that we instead had
613        // `dyn for<'a> Foo<'a, Bar = &'a ()> + Send`. In this case, `Send` is
614        // under the same binders as `Foo`. Currently, this doesn't matter,
615        // because only *auto traits* are allowed other than the principal trait
616        // and all auto traits don't have any generics. Two things could
617        // make this not an "okay" mangling:
618        // 1) Instead of mangling only *used*
619        // bound vars, we want to mangle *all* bound vars (`for<'b> Send` is a
620        // valid trait predicate);
621        // 2) We allow multiple "principal" traits in the future, or at least
622        // allow in any form another trait predicate that can take generics.
623        //
624        // Here we assume that predicates have the following structure:
625        // [<Trait> [{<Projection>}]] [{<Auto>}]
626        // Since any predicates after the first one shouldn't change the binders,
627        // just put them all in the binders of the first.
628        self.wrap_binder(&predicates[0], |cx, _| {
629            for predicate in predicates.iter() {
630                // It would be nice to be able to validate bound vars here, but
631                // projections can actually include bound vars from super traits
632                // because of HRTBs (only in the `Self` type). Also, auto traits
633                // could have different bound vars *anyways*.
634                match predicate.as_ref().skip_binder() {
635                    ty::ExistentialPredicate::Trait(trait_ref) => {
636                        // Use a type that can't appear in defaults of type parameters.
637                        let dummy_self = Ty::new_fresh(cx.tcx, 0);
638                        let trait_ref = trait_ref.with_self_ty(cx.tcx, dummy_self);
639                        cx.print_def_path(trait_ref.def_id, trait_ref.args)?;
640                    }
641                    ty::ExistentialPredicate::Projection(projection) => {
642                        let name = cx.tcx.associated_item(projection.def_id).name();
643                        cx.push("p");
644                        cx.push_ident(name.as_str());
645                        match projection.term.kind() {
646                            ty::TermKind::Ty(ty) => ty.print(cx),
647                            ty::TermKind::Const(c) => c.print(cx),
648                        }?;
649                    }
650                    ty::ExistentialPredicate::AutoTrait(def_id) => {
651                        cx.print_def_path(*def_id, &[])?;
652                    }
653                }
654            }
655            Ok(())
656        })?;
657
658        self.push("E");
659        Ok(())
660    }
661
662    fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
663        // We only mangle a typed value if the const can be evaluated.
664        let cv = match ct.kind() {
665            ty::ConstKind::Value(cv) => cv,
666
667            // Should only be encountered within the identity-substituted
668            // impl header of an item nested within an impl item.
669            ty::ConstKind::Param(_) => {
670                // Never cached (single-character).
671                self.push("p");
672                return Ok(());
673            }
674
675            // We may still encounter unevaluated consts due to the printing
676            // logic sometimes passing identity-substituted impl headers.
677            ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, args, .. }) => {
678                return self.print_def_path(def, args);
679            }
680
681            ty::ConstKind::Expr(_)
682            | ty::ConstKind::Infer(_)
683            | ty::ConstKind::Bound(..)
684            | ty::ConstKind::Placeholder(_)
685            | ty::ConstKind::Error(_) => bug!(),
686        };
687
688        if let Some(&i) = self.consts.get(&ct) {
689            self.print_backref(i)?;
690            return Ok(());
691        }
692
693        let ty::Value { ty: ct_ty, valtree } = cv;
694        let start = self.out.len();
695
696        match ct_ty.kind() {
697            ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => {
698                ct_ty.print(self)?;
699
700                let mut bits = cv
701                    .try_to_bits(self.tcx, ty::TypingEnv::fully_monomorphized())
702                    .expect("expected const to be monomorphic");
703
704                // Negative integer values are mangled using `n` as a "sign prefix".
705                if let ty::Int(ity) = ct_ty.kind() {
706                    let val =
707                        Integer::from_int_ty(&self.tcx, *ity).size().sign_extend(bits) as i128;
708                    if val < 0 {
709                        self.push("n");
710                    }
711                    bits = val.unsigned_abs();
712                }
713
714                let _ = write!(self.out, "{bits:x}_");
715            }
716
717            // Handle `str` as partial support for unsized constants
718            ty::Str => {
719                let tcx = self.tcx();
720                // HACK(jaic1): hide the `str` type behind a reference
721                // for the following transformation from valtree to raw bytes
722                let ref_ty = Ty::new_imm_ref(tcx, tcx.lifetimes.re_static, ct_ty);
723                let cv = ty::Value { ty: ref_ty, valtree };
724                let slice = cv.try_to_raw_bytes(tcx).unwrap_or_else(|| {
725                    bug!("expected to get raw bytes from valtree {:?} for type {:}", valtree, ct_ty)
726                });
727                let s = std::str::from_utf8(slice).expect("non utf8 str from MIR interpreter");
728
729                // "e" for str as a basic type
730                self.push("e");
731
732                // FIXME(eddyb) use a specialized hex-encoding loop.
733                for byte in s.bytes() {
734                    let _ = write!(self.out, "{byte:02x}");
735                }
736
737                self.push("_");
738            }
739
740            // FIXME(valtrees): Remove the special case for `str`
741            // here and fully support unsized constants.
742            ty::Ref(_, _, mutbl) => {
743                self.push(match mutbl {
744                    hir::Mutability::Not => "R",
745                    hir::Mutability::Mut => "Q",
746                });
747
748                let pointee_ty =
749                    ct_ty.builtin_deref(true).expect("tried to dereference on non-ptr type");
750                let dereferenced_const = ty::Const::new_value(self.tcx, valtree, pointee_ty);
751                dereferenced_const.print(self)?;
752            }
753
754            ty::Array(..) | ty::Tuple(..) | ty::Adt(..) | ty::Slice(_) => {
755                let contents = self.tcx.destructure_const(ct);
756                let fields = contents.fields.iter().copied();
757
758                let print_field_list = |this: &mut Self| {
759                    for field in fields.clone() {
760                        field.print(this)?;
761                    }
762                    this.push("E");
763                    Ok(())
764                };
765
766                match *ct_ty.kind() {
767                    ty::Array(..) | ty::Slice(_) => {
768                        self.push("A");
769                        print_field_list(self)?;
770                    }
771                    ty::Tuple(..) => {
772                        self.push("T");
773                        print_field_list(self)?;
774                    }
775                    ty::Adt(def, args) => {
776                        let variant_idx =
777                            contents.variant.expect("destructed const of adt without variant idx");
778                        let variant_def = &def.variant(variant_idx);
779
780                        self.push("V");
781                        self.print_def_path(variant_def.def_id, args)?;
782
783                        match variant_def.ctor_kind() {
784                            Some(CtorKind::Const) => {
785                                self.push("U");
786                            }
787                            Some(CtorKind::Fn) => {
788                                self.push("T");
789                                print_field_list(self)?;
790                            }
791                            None => {
792                                self.push("S");
793                                for (field_def, field) in iter::zip(&variant_def.fields, fields) {
794                                    // HACK(eddyb) this mimics `path_append`,
795                                    // instead of simply using `field_def.ident`,
796                                    // just to be able to handle disambiguators.
797                                    let disambiguated_field =
798                                        self.tcx.def_key(field_def.did).disambiguated_data;
799                                    let field_name = disambiguated_field.data.get_opt_name();
800                                    self.push_disambiguator(
801                                        disambiguated_field.disambiguator as u64,
802                                    );
803                                    self.push_ident(field_name.unwrap().as_str());
804
805                                    field.print(self)?;
806                                }
807                                self.push("E");
808                            }
809                        }
810                    }
811                    _ => unreachable!(),
812                }
813            }
814            _ => {
815                bug!("symbol_names: unsupported constant of type `{}` ({:?})", ct_ty, ct);
816            }
817        }
818
819        // Only cache consts that do not refer to an enclosing
820        // binder (which would change depending on context).
821        if !ct.has_escaping_bound_vars() {
822            self.consts.insert(ct, start);
823        }
824        Ok(())
825    }
826
827    fn path_crate(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
828        self.push("C");
829        if !self.is_exportable {
830            let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
831            self.push_disambiguator(stable_crate_id.as_u64());
832        }
833        let name = self.tcx.crate_name(cnum);
834        self.push_ident(name.as_str());
835        Ok(())
836    }
837
838    fn path_qualified(
839        &mut self,
840        self_ty: Ty<'tcx>,
841        trait_ref: Option<ty::TraitRef<'tcx>>,
842    ) -> Result<(), PrintError> {
843        assert!(trait_ref.is_some());
844        let trait_ref = trait_ref.unwrap();
845
846        self.push("Y");
847        self_ty.print(self)?;
848        self.print_def_path(trait_ref.def_id, trait_ref.args)
849    }
850
851    fn path_append_impl(
852        &mut self,
853        _: impl FnOnce(&mut Self) -> Result<(), PrintError>,
854        _: &DisambiguatedDefPathData,
855        _: Ty<'tcx>,
856        _: Option<ty::TraitRef<'tcx>>,
857    ) -> Result<(), PrintError> {
858        // Inlined into `print_impl_path`
859        unreachable!()
860    }
861
862    fn path_append(
863        &mut self,
864        print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
865        disambiguated_data: &DisambiguatedDefPathData,
866    ) -> Result<(), PrintError> {
867        let ns = match disambiguated_data.data {
868            // Extern block segments can be skipped, names from extern blocks
869            // are effectively living in their parent modules.
870            DefPathData::ForeignMod => return print_prefix(self),
871
872            // Uppercase categories are more stable than lowercase ones.
873            DefPathData::TypeNs(_) => 't',
874            DefPathData::ValueNs(_) => 'v',
875            DefPathData::Closure => 'C',
876            DefPathData::Ctor => 'c',
877            DefPathData::AnonConst => 'k',
878            DefPathData::OpaqueTy => 'i',
879            DefPathData::SyntheticCoroutineBody => 's',
880            DefPathData::NestedStatic => 'n',
881
882            // These should never show up as `path_append` arguments.
883            DefPathData::CrateRoot
884            | DefPathData::Use
885            | DefPathData::GlobalAsm
886            | DefPathData::Impl
887            | DefPathData::MacroNs(_)
888            | DefPathData::LifetimeNs(_)
889            | DefPathData::OpaqueLifetime(_)
890            | DefPathData::AnonAssocTy(..) => {
891                bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)
892            }
893        };
894
895        let name = disambiguated_data.data.get_opt_name();
896
897        self.path_append_ns(
898            print_prefix,
899            ns,
900            disambiguated_data.disambiguator as u64,
901            name.unwrap_or(sym::empty).as_str(),
902        )
903    }
904
905    fn path_generic_args(
906        &mut self,
907        print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
908        args: &[GenericArg<'tcx>],
909    ) -> Result<(), PrintError> {
910        // Don't print any regions if they're all erased.
911        let print_regions = args.iter().any(|arg| match arg.kind() {
912            GenericArgKind::Lifetime(r) => !r.is_erased(),
913            _ => false,
914        });
915        let args = args.iter().cloned().filter(|arg| match arg.kind() {
916            GenericArgKind::Lifetime(_) => print_regions,
917            _ => true,
918        });
919
920        if args.clone().next().is_none() {
921            return print_prefix(self);
922        }
923
924        self.push("I");
925        print_prefix(self)?;
926        for arg in args {
927            match arg.kind() {
928                GenericArgKind::Lifetime(lt) => {
929                    lt.print(self)?;
930                }
931                GenericArgKind::Type(ty) => {
932                    ty.print(self)?;
933                }
934                GenericArgKind::Const(c) => {
935                    self.push("K");
936                    c.print(self)?;
937                }
938            }
939        }
940        self.push("E");
941
942        Ok(())
943    }
944}
945/// Push a `_`-terminated base 62 integer, using the format
946/// specified in the RFC as `<base-62-number>`, that is:
947/// * `x = 0` is encoded as just the `"_"` terminator
948/// * `x > 0` is encoded as `x - 1` in base 62, followed by `"_"`,
949///   e.g. `1` becomes `"0_"`, `62` becomes `"Z_"`, etc.
950pub(crate) fn push_integer_62(x: u64, output: &mut String) {
951    if let Some(x) = x.checked_sub(1) {
952        output.push_str(&x.to_base(62));
953    }
954    output.push('_');
955}
956
957pub(crate) fn encode_integer_62(x: u64) -> String {
958    let mut output = String::new();
959    push_integer_62(x, &mut output);
960    output
961}
962
963pub(crate) fn push_ident(ident: &str, output: &mut String) {
964    let mut use_punycode = false;
965    for b in ident.bytes() {
966        match b {
967            b'_' | b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => {}
968            0x80..=0xff => use_punycode = true,
969            _ => bug!("symbol_names: bad byte {} in ident {:?}", b, ident),
970        }
971    }
972
973    let punycode_string;
974    let ident = if use_punycode {
975        output.push('u');
976
977        // FIXME(eddyb) we should probably roll our own punycode implementation.
978        let mut punycode_bytes = match punycode::encode(ident) {
979            Ok(s) => s.into_bytes(),
980            Err(()) => bug!("symbol_names: punycode encoding failed for ident {:?}", ident),
981        };
982
983        // Replace `-` with `_`.
984        if let Some(c) = punycode_bytes.iter_mut().rfind(|&&mut c| c == b'-') {
985            *c = b'_';
986        }
987
988        // FIXME(eddyb) avoid rechecking UTF-8 validity.
989        punycode_string = String::from_utf8(punycode_bytes).unwrap();
990        &punycode_string
991    } else {
992        ident
993    };
994
995    let _ = write!(output, "{}", ident.len());
996
997    // Write a separating `_` if necessary (leading digit or `_`).
998    if let Some('_' | '0'..='9') = ident.chars().next() {
999        output.push('_');
1000    }
1001
1002    output.push_str(ident);
1003}