rustc_symbol_mangling/
v0.rs

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