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 p: V0SymbolMangler<'_> = V0SymbolMangler {
37        tcx,
38        start_offset: prefix.len(),
39        is_exportable,
40        paths: FxHashMap::default(),
41        types: FxHashMap::default(),
42        consts: FxHashMap::default(),
43        binders: vec![],
44        out: String::from(prefix),
45    };
46
47    // 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        p.print_def_path(def_id, tcx.mk_args(&[GenericArg::from(drop_ty)])).unwrap()
73    } else if let Some(shim_kind) = shim_kind {
74        p.path_append_ns(|p| p.print_def_path(def_id, args), 'S', 0, shim_kind).unwrap()
75    } else {
76        p.print_def_path(def_id, args).unwrap()
77    };
78    if let Some(instantiating_crate) = instantiating_crate {
79        p.print_def_path(instantiating_crate.as_def_id(), &[]).unwrap();
80    }
81    std::mem::take(&mut p.out)
82}
83
84pub fn mangle_internal_symbol<'tcx>(tcx: TyCtxt<'tcx>, item_name: &str) -> String {
85    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 p: V0SymbolMangler<'_> = V0SymbolMangler {
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    p.path_append_ns(
103        |p| {
104            p.push("C");
105            p.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            p.push_ident("__rustc");
118            Ok(())
119        },
120        'v',
121        0,
122        item_name,
123    )
124    .unwrap();
125
126    std::mem::take(&mut p.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 p = V0SymbolMangler {
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    p.print_def_path(trait_ref.def_id, &[]).unwrap();
145    std::mem::take(&mut p.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 V0SymbolMangler<'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> V0SymbolMangler<'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 V0SymbolMangler<'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.print_path_with_generic_args(
369                |this| {
370                    this.path_append_ns(
371                        |p| p.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::Int(IntTy::I8) => "a",
429            ty::Int(IntTy::I16) => "s",
430            ty::Int(IntTy::I32) => "l",
431            ty::Int(IntTy::I64) => "x",
432            ty::Int(IntTy::I128) => "n",
433            ty::Int(IntTy::Isize) => "i",
434            ty::Uint(UintTy::U8) => "h",
435            ty::Uint(UintTy::U16) => "t",
436            ty::Uint(UintTy::U32) => "m",
437            ty::Uint(UintTy::U64) => "y",
438            ty::Uint(UintTy::U128) => "o",
439            ty::Uint(UintTy::Usize) => "j",
440            ty::Float(FloatTy::F16) => "C3f16",
441            ty::Float(FloatTy::F32) => "f",
442            ty::Float(FloatTy::F64) => "d",
443            ty::Float(FloatTy::F128) => "C4f128",
444            ty::Never => "z",
445
446            ty::Tuple(_) if ty.is_unit() => "u",
447
448            // Should only be encountered within the identity-substituted
449            // impl header of an item nested within an impl item.
450            ty::Param(_) => "p",
451
452            _ => "",
453        };
454        if !basic_type.is_empty() {
455            self.push(basic_type);
456            return Ok(());
457        }
458
459        if let Some(&i) = self.types.get(&ty) {
460            return self.print_backref(i);
461        }
462        let start = self.out.len();
463
464        match *ty.kind() {
465            // Basic types, handled above.
466            ty::Bool | ty::Char | ty::Str | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Never => {
467                unreachable!()
468            }
469            ty::Tuple(_) if ty.is_unit() => unreachable!(),
470            ty::Param(_) => unreachable!(),
471
472            ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => bug!(),
473
474            ty::Ref(r, ty, mutbl) => {
475                self.push(match mutbl {
476                    hir::Mutability::Not => "R",
477                    hir::Mutability::Mut => "Q",
478                });
479                if !r.is_erased() {
480                    r.print(self)?;
481                }
482                ty.print(self)?;
483            }
484
485            ty::RawPtr(ty, mutbl) => {
486                self.push(match mutbl {
487                    hir::Mutability::Not => "P",
488                    hir::Mutability::Mut => "O",
489                });
490                ty.print(self)?;
491            }
492
493            ty::Pat(ty, pat) => {
494                // HACK: Represent as tuple until we have something better.
495                // HACK: constants are used in arrays, even if the types don't match.
496                self.push("T");
497                ty.print(self)?;
498                self.print_pat(pat)?;
499                self.push("E");
500            }
501
502            ty::Array(ty, len) => {
503                self.push("A");
504                ty.print(self)?;
505                self.print_const(len)?;
506            }
507            ty::Slice(ty) => {
508                self.push("S");
509                ty.print(self)?;
510            }
511
512            ty::Tuple(tys) => {
513                self.push("T");
514                for ty in tys.iter() {
515                    ty.print(self)?;
516                }
517                self.push("E");
518            }
519
520            // Mangle all nominal types as paths.
521            ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), args)
522            | ty::FnDef(def_id, args)
523            | ty::Closure(def_id, args)
524            | ty::CoroutineClosure(def_id, args)
525            | ty::Coroutine(def_id, args) => {
526                self.print_def_path(def_id, args)?;
527            }
528
529            // We may still encounter projections here due to the printing
530            // logic sometimes passing identity-substituted impl headers.
531            ty::Alias(ty::Projection, ty::AliasTy { def_id, args, .. }) => {
532                self.print_def_path(def_id, args)?;
533            }
534
535            ty::Foreign(def_id) => {
536                self.print_def_path(def_id, &[])?;
537            }
538
539            ty::FnPtr(sig_tys, hdr) => {
540                let sig = sig_tys.with(hdr);
541                self.push("F");
542                self.wrap_binder(&sig, |p, sig| {
543                    if sig.safety.is_unsafe() {
544                        p.push("U");
545                    }
546                    match sig.abi {
547                        ExternAbi::Rust => {}
548                        ExternAbi::C { unwind: false } => p.push("KC"),
549                        abi => {
550                            p.push("K");
551                            let name = abi.as_str();
552                            if name.contains('-') {
553                                p.push_ident(&name.replace('-', "_"));
554                            } else {
555                                p.push_ident(name);
556                            }
557                        }
558                    }
559                    for &ty in sig.inputs() {
560                        ty.print(p)?;
561                    }
562                    if sig.c_variadic {
563                        p.push("v");
564                    }
565                    p.push("E");
566                    sig.output().print(p)
567                })?;
568            }
569
570            // FIXME(unsafe_binder):
571            ty::UnsafeBinder(..) => todo!(),
572
573            ty::Dynamic(predicates, r, kind) => {
574                self.push(match kind {
575                    ty::Dyn => "D",
576                });
577                self.print_dyn_existential(predicates)?;
578                r.print(self)?;
579            }
580
581            ty::Alias(..) => bug!("symbol_names: unexpected alias"),
582            ty::CoroutineWitness(..) => bug!("symbol_names: unexpected `CoroutineWitness`"),
583        }
584
585        // Only cache types that do not refer to an enclosing
586        // binder (which would change depending on context).
587        if !ty.has_escaping_bound_vars() {
588            self.types.insert(ty, start);
589        }
590        Ok(())
591    }
592
593    fn print_dyn_existential(
594        &mut self,
595        predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
596    ) -> Result<(), PrintError> {
597        // Okay, so this is a bit tricky. Imagine we have a trait object like
598        // `dyn for<'a> Foo<'a, Bar = &'a ()>`. When we mangle this, the
599        // output looks really close to the syntax, where the `Bar = &'a ()` bit
600        // is under the same binders (`['a]`) as the `Foo<'a>` bit. However, we
601        // actually desugar these into two separate `ExistentialPredicate`s. We
602        // can't enter/exit the "binder scope" twice though, because then we
603        // would mangle the binders twice. (Also, side note, we merging these
604        // two is kind of difficult, because of potential HRTBs in the Projection
605        // predicate.)
606        //
607        // Also worth mentioning: imagine that we instead had
608        // `dyn for<'a> Foo<'a, Bar = &'a ()> + Send`. In this case, `Send` is
609        // under the same binders as `Foo`. Currently, this doesn't matter,
610        // because only *auto traits* are allowed other than the principal trait
611        // and all auto traits don't have any generics. Two things could
612        // make this not an "okay" mangling:
613        // 1) Instead of mangling only *used*
614        // bound vars, we want to mangle *all* bound vars (`for<'b> Send` is a
615        // valid trait predicate);
616        // 2) We allow multiple "principal" traits in the future, or at least
617        // allow in any form another trait predicate that can take generics.
618        //
619        // Here we assume that predicates have the following structure:
620        // [<Trait> [{<Projection>}]] [{<Auto>}]
621        // Since any predicates after the first one shouldn't change the binders,
622        // just put them all in the binders of the first.
623        self.wrap_binder(&predicates[0], |p, _| {
624            for predicate in predicates.iter() {
625                // It would be nice to be able to validate bound vars here, but
626                // projections can actually include bound vars from super traits
627                // because of HRTBs (only in the `Self` type). Also, auto traits
628                // could have different bound vars *anyways*.
629                match predicate.as_ref().skip_binder() {
630                    ty::ExistentialPredicate::Trait(trait_ref) => {
631                        // Use a type that can't appear in defaults of type parameters.
632                        let dummy_self = Ty::new_fresh(p.tcx, 0);
633                        let trait_ref = trait_ref.with_self_ty(p.tcx, dummy_self);
634                        p.print_def_path(trait_ref.def_id, trait_ref.args)?;
635                    }
636                    ty::ExistentialPredicate::Projection(projection) => {
637                        let name = p.tcx.associated_item(projection.def_id).name();
638                        p.push("p");
639                        p.push_ident(name.as_str());
640                        match projection.term.kind() {
641                            ty::TermKind::Ty(ty) => ty.print(p),
642                            ty::TermKind::Const(c) => c.print(p),
643                        }?;
644                    }
645                    ty::ExistentialPredicate::AutoTrait(def_id) => {
646                        p.print_def_path(*def_id, &[])?;
647                    }
648                }
649            }
650            Ok(())
651        })?;
652
653        self.push("E");
654        Ok(())
655    }
656
657    fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
658        // We only mangle a typed value if the const can be evaluated.
659        let cv = match ct.kind() {
660            ty::ConstKind::Value(cv) => cv,
661
662            // Should only be encountered within the identity-substituted
663            // impl header of an item nested within an impl item.
664            ty::ConstKind::Param(_) => {
665                // Never cached (single-character).
666                self.push("p");
667                return Ok(());
668            }
669
670            // We may still encounter unevaluated consts due to the printing
671            // logic sometimes passing identity-substituted impl headers.
672            ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, args, .. }) => {
673                return self.print_def_path(def, args);
674            }
675
676            ty::ConstKind::Expr(_)
677            | ty::ConstKind::Infer(_)
678            | ty::ConstKind::Bound(..)
679            | ty::ConstKind::Placeholder(_)
680            | ty::ConstKind::Error(_) => bug!(),
681        };
682
683        if let Some(&i) = self.consts.get(&ct) {
684            self.print_backref(i)?;
685            return Ok(());
686        }
687
688        let ty::Value { ty: ct_ty, valtree } = cv;
689        let start = self.out.len();
690
691        match ct_ty.kind() {
692            ty::Uint(_) | ty::Int(_) | ty::Bool | ty::Char => {
693                ct_ty.print(self)?;
694
695                let mut bits = cv
696                    .try_to_bits(self.tcx, ty::TypingEnv::fully_monomorphized())
697                    .expect("expected const to be monomorphic");
698
699                // Negative integer values are mangled using `n` as a "sign prefix".
700                if let ty::Int(ity) = ct_ty.kind() {
701                    let val =
702                        Integer::from_int_ty(&self.tcx, *ity).size().sign_extend(bits) as i128;
703                    if val < 0 {
704                        self.push("n");
705                    }
706                    bits = val.unsigned_abs();
707                }
708
709                let _ = write!(self.out, "{bits:x}_");
710            }
711
712            // Handle `str` as partial support for unsized constants
713            ty::Str => {
714                let tcx = self.tcx();
715                // HACK(jaic1): hide the `str` type behind a reference
716                // for the following transformation from valtree to raw bytes
717                let ref_ty = Ty::new_imm_ref(tcx, tcx.lifetimes.re_static, ct_ty);
718                let cv = ty::Value { ty: ref_ty, valtree };
719                let slice = cv.try_to_raw_bytes(tcx).unwrap_or_else(|| {
720                    bug!("expected to get raw bytes from valtree {:?} for type {:}", valtree, ct_ty)
721                });
722                let s = std::str::from_utf8(slice).expect("non utf8 str from MIR interpreter");
723
724                // "e" for str as a basic type
725                self.push("e");
726
727                // FIXME(eddyb) use a specialized hex-encoding loop.
728                for byte in s.bytes() {
729                    let _ = write!(self.out, "{byte:02x}");
730                }
731
732                self.push("_");
733            }
734
735            // FIXME(valtrees): Remove the special case for `str`
736            // here and fully support unsized constants.
737            ty::Ref(_, _, mutbl) => {
738                self.push(match mutbl {
739                    hir::Mutability::Not => "R",
740                    hir::Mutability::Mut => "Q",
741                });
742
743                let pointee_ty =
744                    ct_ty.builtin_deref(true).expect("tried to dereference on non-ptr type");
745                let dereferenced_const = ty::Const::new_value(self.tcx, valtree, pointee_ty);
746                dereferenced_const.print(self)?;
747            }
748
749            ty::Array(..) | ty::Tuple(..) | ty::Adt(..) | ty::Slice(_) => {
750                let contents = self.tcx.destructure_const(ct);
751                let fields = contents.fields.iter().copied();
752
753                let print_field_list = |this: &mut Self| {
754                    for field in fields.clone() {
755                        field.print(this)?;
756                    }
757                    this.push("E");
758                    Ok(())
759                };
760
761                match *ct_ty.kind() {
762                    ty::Array(..) | ty::Slice(_) => {
763                        self.push("A");
764                        print_field_list(self)?;
765                    }
766                    ty::Tuple(..) => {
767                        self.push("T");
768                        print_field_list(self)?;
769                    }
770                    ty::Adt(def, args) => {
771                        let variant_idx =
772                            contents.variant.expect("destructed const of adt without variant idx");
773                        let variant_def = &def.variant(variant_idx);
774
775                        self.push("V");
776                        self.print_def_path(variant_def.def_id, args)?;
777
778                        match variant_def.ctor_kind() {
779                            Some(CtorKind::Const) => {
780                                self.push("U");
781                            }
782                            Some(CtorKind::Fn) => {
783                                self.push("T");
784                                print_field_list(self)?;
785                            }
786                            None => {
787                                self.push("S");
788                                for (field_def, field) in iter::zip(&variant_def.fields, fields) {
789                                    // HACK(eddyb) this mimics `print_path_with_simple`,
790                                    // instead of simply using `field_def.ident`,
791                                    // just to be able to handle disambiguators.
792                                    let disambiguated_field =
793                                        self.tcx.def_key(field_def.did).disambiguated_data;
794                                    let field_name = disambiguated_field.data.get_opt_name();
795                                    self.push_disambiguator(
796                                        disambiguated_field.disambiguator as u64,
797                                    );
798                                    self.push_ident(field_name.unwrap().as_str());
799
800                                    field.print(self)?;
801                                }
802                                self.push("E");
803                            }
804                        }
805                    }
806                    _ => unreachable!(),
807                }
808            }
809            _ => {
810                bug!("symbol_names: unsupported constant of type `{}` ({:?})", ct_ty, ct);
811            }
812        }
813
814        // Only cache consts that do not refer to an enclosing
815        // binder (which would change depending on context).
816        if !ct.has_escaping_bound_vars() {
817            self.consts.insert(ct, start);
818        }
819        Ok(())
820    }
821
822    fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
823        self.push("C");
824        if !self.is_exportable {
825            let stable_crate_id = self.tcx.def_path_hash(cnum.as_def_id()).stable_crate_id();
826            self.push_disambiguator(stable_crate_id.as_u64());
827        }
828        let name = self.tcx.crate_name(cnum);
829        self.push_ident(name.as_str());
830        Ok(())
831    }
832
833    fn print_path_with_qualified(
834        &mut self,
835        self_ty: Ty<'tcx>,
836        trait_ref: Option<ty::TraitRef<'tcx>>,
837    ) -> Result<(), PrintError> {
838        assert!(trait_ref.is_some());
839        let trait_ref = trait_ref.unwrap();
840
841        self.push("Y");
842        self_ty.print(self)?;
843        self.print_def_path(trait_ref.def_id, trait_ref.args)
844    }
845
846    fn print_path_with_impl(
847        &mut self,
848        _: impl FnOnce(&mut Self) -> Result<(), PrintError>,
849        _: Ty<'tcx>,
850        _: Option<ty::TraitRef<'tcx>>,
851    ) -> Result<(), PrintError> {
852        // Inlined into `print_impl_path`
853        unreachable!()
854    }
855
856    fn print_path_with_simple(
857        &mut self,
858        print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
859        disambiguated_data: &DisambiguatedDefPathData,
860    ) -> Result<(), PrintError> {
861        let ns = match disambiguated_data.data {
862            // Extern block segments can be skipped, names from extern blocks
863            // are effectively living in their parent modules.
864            DefPathData::ForeignMod => return print_prefix(self),
865
866            // Uppercase categories are more stable than lowercase ones.
867            DefPathData::TypeNs(_) => 't',
868            DefPathData::ValueNs(_) => 'v',
869            DefPathData::Closure => 'C',
870            DefPathData::Ctor => 'c',
871            DefPathData::AnonConst => 'k',
872            DefPathData::OpaqueTy => 'i',
873            DefPathData::SyntheticCoroutineBody => 's',
874            DefPathData::NestedStatic => 'n',
875
876            // These should never show up as `print_path_with_simple` arguments.
877            DefPathData::CrateRoot
878            | DefPathData::Use
879            | DefPathData::GlobalAsm
880            | DefPathData::Impl
881            | DefPathData::MacroNs(_)
882            | DefPathData::LifetimeNs(_)
883            | DefPathData::OpaqueLifetime(_)
884            | DefPathData::AnonAssocTy(..) => {
885                bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)
886            }
887        };
888
889        let name = disambiguated_data.data.get_opt_name();
890
891        self.path_append_ns(
892            print_prefix,
893            ns,
894            disambiguated_data.disambiguator as u64,
895            name.unwrap_or(sym::empty).as_str(),
896        )
897    }
898
899    fn print_path_with_generic_args(
900        &mut self,
901        print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
902        args: &[GenericArg<'tcx>],
903    ) -> Result<(), PrintError> {
904        // Don't print any regions if they're all erased.
905        let print_regions = args.iter().any(|arg| match arg.kind() {
906            GenericArgKind::Lifetime(r) => !r.is_erased(),
907            _ => false,
908        });
909        let args = args.iter().cloned().filter(|arg| match arg.kind() {
910            GenericArgKind::Lifetime(_) => print_regions,
911            _ => true,
912        });
913
914        if args.clone().next().is_none() {
915            return print_prefix(self);
916        }
917
918        self.push("I");
919        print_prefix(self)?;
920        for arg in args {
921            match arg.kind() {
922                GenericArgKind::Lifetime(lt) => {
923                    lt.print(self)?;
924                }
925                GenericArgKind::Type(ty) => {
926                    ty.print(self)?;
927                }
928                GenericArgKind::Const(c) => {
929                    self.push("K");
930                    c.print(self)?;
931                }
932            }
933        }
934        self.push("E");
935
936        Ok(())
937    }
938}
939/// Push a `_`-terminated base 62 integer, using the format
940/// specified in the RFC as `<base-62-number>`, that is:
941/// * `x = 0` is encoded as just the `"_"` terminator
942/// * `x > 0` is encoded as `x - 1` in base 62, followed by `"_"`,
943///   e.g. `1` becomes `"0_"`, `62` becomes `"Z_"`, etc.
944pub(crate) fn push_integer_62(x: u64, output: &mut String) {
945    if let Some(x) = x.checked_sub(1) {
946        output.push_str(&x.to_base(62));
947    }
948    output.push('_');
949}
950
951pub(crate) fn encode_integer_62(x: u64) -> String {
952    let mut output = String::new();
953    push_integer_62(x, &mut output);
954    output
955}
956
957pub(crate) fn push_ident(ident: &str, output: &mut String) {
958    let mut use_punycode = false;
959    for b in ident.bytes() {
960        match b {
961            b'_' | b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => {}
962            0x80..=0xff => use_punycode = true,
963            _ => bug!("symbol_names: bad byte {} in ident {:?}", b, ident),
964        }
965    }
966
967    let punycode_string;
968    let ident = if use_punycode {
969        output.push('u');
970
971        // FIXME(eddyb) we should probably roll our own punycode implementation.
972        let mut punycode_bytes = match punycode::encode(ident) {
973            Ok(s) => s.into_bytes(),
974            Err(()) => bug!("symbol_names: punycode encoding failed for ident {:?}", ident),
975        };
976
977        // Replace `-` with `_`.
978        if let Some(c) = punycode_bytes.iter_mut().rfind(|&&mut c| c == b'-') {
979            *c = b'_';
980        }
981
982        // FIXME(eddyb) avoid rechecking UTF-8 validity.
983        punycode_string = String::from_utf8(punycode_bytes).unwrap();
984        &punycode_string
985    } else {
986        ident
987    };
988
989    let _ = write!(output, "{}", ident.len());
990
991    // Write a separating `_` if necessary (leading digit or `_`).
992    if let Some('_' | '0'..='9') = ident.chars().next() {
993        output.push('_');
994    }
995
996    output.push_str(ident);
997}