Skip to main content

rustc_symbol_mangling/
legacy.rs

1use std::fmt::{self, Write};
2use std::mem::{self, discriminant};
3
4use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5use rustc_hashes::Hash64;
6use rustc_hir::def_id::{CrateNum, DefId};
7use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
8use rustc_middle::bug;
9use rustc_middle::ty::print::{PrettyPrinter, Print, PrintError, Printer};
10use rustc_middle::ty::{
11    self, GenericArg, GenericArgKind, Instance, ReifyReason, Ty, TyCtxt, TypeVisitableExt,
12    Unnormalized,
13};
14use tracing::debug;
15
16pub(super) fn mangle<'tcx>(
17    tcx: TyCtxt<'tcx>,
18    instance: Instance<'tcx>,
19    instantiating_crate: Option<CrateNum>,
20) -> String {
21    let def_id = instance.def_id();
22
23    // We want to compute the "type" of this item. Unfortunately, some
24    // kinds of items (e.g., synthetic static allocations from const eval)
25    // don't have a proper implementation for the `type_of` query. So walk
26    // back up the find the closest parent that DOES have a type.
27    let mut ty_def_id = def_id;
28    let instance_ty;
29    loop {
30        let key = tcx.def_key(ty_def_id);
31        match key.disambiguated_data.data {
32            DefPathData::TypeNs(_)
33            | DefPathData::ValueNs(_)
34            | DefPathData::Closure
35            | DefPathData::SyntheticCoroutineBody => {
36                instance_ty = tcx.type_of(ty_def_id).instantiate_identity().skip_norm_wip();
37                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_symbol_mangling/src/legacy.rs:37",
                        "rustc_symbol_mangling::legacy", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_symbol_mangling/src/legacy.rs"),
                        ::tracing_core::__macro_support::Option::Some(37u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_symbol_mangling::legacy"),
                        ::tracing_core::field::FieldSet::new(&["instance_ty"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&instance_ty)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?instance_ty);
38                break;
39            }
40            _ => {
41                // if we're making a symbol for something, there ought
42                // to be a value or type-def or something in there
43                // *somewhere*
44                ty_def_id.index = key.parent.unwrap_or_else(|| {
45                    ::rustc_middle::util::bug::bug_fmt(format_args!("finding type for {0:?}, encountered def-id {1:?} with no parent",
        def_id, ty_def_id));bug!(
46                        "finding type for {:?}, encountered def-id {:?} with no \
47                         parent",
48                        def_id,
49                        ty_def_id
50                    );
51                });
52            }
53        }
54    }
55
56    // Erase regions because they may not be deterministic when hashed
57    // and should not matter anyhow.
58    let instance_ty = tcx.erase_and_anonymize_regions(instance_ty);
59
60    let hash = get_symbol_hash(tcx, instance, instance_ty, instantiating_crate);
61
62    let mut p = LegacySymbolMangler { tcx, path: SymbolPath::new(), keep_within_component: false };
63    p.print_def_path(
64        def_id,
65        if let ty::InstanceKind::DropGlue(_, _)
66        | ty::InstanceKind::AsyncDropGlueCtorShim(_, _)
67        | ty::InstanceKind::FutureDropPollShim(_, _, _) = instance.def
68        {
69            // Add the name of the dropped type to the symbol name
70            &*instance.args
71        } else if let ty::InstanceKind::AsyncDropGlue(_, ty) = instance.def {
72            let ty::Coroutine(_, cor_args) = ty.kind() else {
73                ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"));bug!();
74            };
75            let drop_ty = cor_args.first().unwrap().expect_ty();
76            tcx.mk_args(&[GenericArg::from(drop_ty)])
77        } else {
78            &[]
79        },
80    )
81    .unwrap();
82
83    match instance.def {
84        ty::InstanceKind::ThreadLocalShim(..) => {
85            p.write_str("{{tls-shim}}").unwrap();
86        }
87        ty::InstanceKind::VTableShim(..) => {
88            p.write_str("{{vtable-shim}}").unwrap();
89        }
90        ty::InstanceKind::ReifyShim(_, reason) => {
91            p.write_str("{{reify-shim").unwrap();
92            match reason {
93                Some(ReifyReason::FnPtr) => p.write_str("-fnptr").unwrap(),
94                Some(ReifyReason::Vtable) => p.write_str("-vtable").unwrap(),
95                None => (),
96            }
97            p.write_str("}}").unwrap();
98        }
99        // FIXME(async_closures): This shouldn't be needed when we fix
100        // `Instance::ty`/`Instance::def_id`.
101        ty::InstanceKind::ConstructCoroutineInClosureShim { receiver_by_ref, .. } => {
102            p.write_str(if receiver_by_ref { "{{by-move-shim}}" } else { "{{by-ref-shim}}" })
103                .unwrap();
104        }
105        _ => {}
106    }
107
108    if let ty::InstanceKind::FutureDropPollShim(..) = instance.def {
109        let _ = p.write_str("{{drop-shim}}");
110    }
111
112    p.path.finish(hash)
113}
114
115fn get_symbol_hash<'tcx>(
116    tcx: TyCtxt<'tcx>,
117
118    // instance this name will be for
119    instance: Instance<'tcx>,
120
121    // type of the item, without any generic
122    // parameters instantiated; this is
123    // included in the hash as a kind of
124    // safeguard.
125    item_type: Ty<'tcx>,
126
127    instantiating_crate: Option<CrateNum>,
128) -> Hash64 {
129    let def_id = instance.def_id();
130    let args = instance.args;
131    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_symbol_mangling/src/legacy.rs:131",
                        "rustc_symbol_mangling::legacy", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_symbol_mangling/src/legacy.rs"),
                        ::tracing_core::__macro_support::Option::Some(131u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_symbol_mangling::legacy"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("get_symbol_hash(def_id={0:?}, parameters={1:?})",
                                                    def_id, args) as &dyn Value))])
            });
    } else { ; }
};debug!("get_symbol_hash(def_id={:?}, parameters={:?})", def_id, args);
132
133    tcx.with_stable_hashing_context(|mut hcx| {
134        let mut hasher = StableHasher::new();
135
136        // the main symbol name is not necessarily unique; hash in the
137        // compiler's internal def-path, guaranteeing each symbol has a
138        // truly unique path
139        tcx.def_path_hash(def_id).hash_stable(&mut hcx, &mut hasher);
140
141        // Include the main item-type. Note that, in this case, the
142        // assertions about `has_param` may not hold, but this item-type
143        // ought to be the same for every reference anyway.
144        if !!item_type.has_erasable_regions() {
    ::core::panicking::panic("assertion failed: !item_type.has_erasable_regions()")
};assert!(!item_type.has_erasable_regions());
145        hcx.while_hashing_spans(false, |hcx| {
146            item_type.hash_stable(hcx, &mut hasher);
147
148            // If this is a function, we hash the signature as well.
149            // This is not *strictly* needed, but it may help in some
150            // situations, see the `run-make/a-b-a-linker-guard` test.
151            if let ty::FnDef(..) = item_type.kind() {
152                item_type.fn_sig(tcx).hash_stable(hcx, &mut hasher);
153            }
154
155            // also include any type parameters (for generic items)
156            args.hash_stable(hcx, &mut hasher);
157
158            if let Some(instantiating_crate) = instantiating_crate {
159                tcx.stable_crate_id(instantiating_crate).hash_stable(hcx, &mut hasher);
160            }
161
162            // We want to avoid accidental collision between different types of instances.
163            // Especially, `VTableShim`s and `ReifyShim`s may overlap with their original
164            // instances without this.
165            discriminant(&instance.def).hash_stable(hcx, &mut hasher);
166        });
167
168        // 64 bits should be enough to avoid collisions.
169        hasher.finish::<Hash64>()
170    })
171}
172
173// Follow C++ namespace-mangling style, see
174// https://en.wikipedia.org/wiki/Name_mangling for more info.
175//
176// It turns out that on macOS you can actually have arbitrary symbols in
177// function names (at least when given to LLVM), but this is not possible
178// when using unix's linker. Perhaps one day when we just use a linker from LLVM
179// we won't need to do this name mangling. The problem with name mangling is
180// that it seriously limits the available characters. For example we can't
181// have things like &T in symbol names when one would theoretically
182// want them for things like impls of traits on that type.
183//
184// To be able to work on all platforms and get *some* reasonable output, we
185// use C++ name-mangling.
186#[derive(#[automatically_derived]
impl ::core::fmt::Debug for SymbolPath {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "SymbolPath",
            "result", &self.result, "temp_buf", &&self.temp_buf)
    }
}Debug)]
187struct SymbolPath {
188    result: String,
189    temp_buf: String,
190}
191
192impl SymbolPath {
193    fn new() -> Self {
194        let mut result =
195            SymbolPath { result: String::with_capacity(64), temp_buf: String::with_capacity(16) };
196        result.result.push_str("_ZN"); // _Z == Begin name-sequence, N == nested
197        result
198    }
199
200    fn finalize_pending_component(&mut self) {
201        if !self.temp_buf.is_empty() {
202            let _ = self.result.write_fmt(format_args!("{0}{1}", self.temp_buf.len(),
        self.temp_buf))write!(self.result, "{}{}", self.temp_buf.len(), self.temp_buf);
203            self.temp_buf.clear();
204        }
205    }
206
207    fn finish(mut self, hash: Hash64) -> String {
208        self.finalize_pending_component();
209        // E = end name-sequence
210        let _ = self.result.write_fmt(format_args!("17h{0:016x}E", hash))write!(self.result, "17h{hash:016x}E");
211        self.result
212    }
213}
214
215struct LegacySymbolMangler<'tcx> {
216    tcx: TyCtxt<'tcx>,
217    path: SymbolPath,
218
219    // When `true`, `finalize_pending_component` isn't used.
220    // This is needed when recursing into `print_path_with_qualified`,
221    // or `print_path_with_generic_args`, as any nested paths are
222    // logically within one component.
223    keep_within_component: bool,
224}
225
226// HACK(eddyb) this relies on using the `fmt` interface to get
227// `PrettyPrinter` aka pretty printing of e.g. types in paths,
228// symbol names should have their own printing machinery.
229
230impl<'tcx> Printer<'tcx> for LegacySymbolMangler<'tcx> {
231    fn tcx(&self) -> TyCtxt<'tcx> {
232        self.tcx
233    }
234
235    fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
236        // This might be reachable (via `pretty_print_dyn_existential`) even though
237        // `<Self As PrettyPrinter>::should_print_optional_region` returns false and
238        // `print_path_with_generic_args` filters out lifetimes. See #144994.
239        Ok(())
240    }
241
242    fn print_type(&mut self, ty: Ty<'tcx>) -> Result<(), PrintError> {
243        match *ty.kind() {
244            // Print all nominal types as paths (unlike `pretty_print_type`).
245            ty::FnDef(def_id, args)
246            | ty::Alias(ty::AliasTy {
247                kind: ty::Projection { def_id } | ty::Opaque { def_id },
248                args,
249                ..
250            })
251            | ty::Closure(def_id, args)
252            | ty::CoroutineClosure(def_id, args)
253            | ty::Coroutine(def_id, args) => self.print_def_path(def_id, args),
254
255            // The `pretty_print_type` formatting of array size depends on
256            // -Zverbose-internals flag, so we cannot reuse it here.
257            ty::Array(ty, size) => {
258                self.write_str("[")?;
259                self.print_type(ty)?;
260                self.write_str("; ")?;
261                if let Some(size) = size.try_to_target_usize(self.tcx()) {
262                    self.write_fmt(format_args!("{0}", size))write!(self, "{size}")?
263                } else if let ty::ConstKind::Param(param) = size.kind() {
264                    param.print(self)?
265                } else {
266                    self.write_str("_")?
267                }
268                self.write_str("]")?;
269                Ok(())
270            }
271
272            ty::Alias(ty::AliasTy { kind: ty::Inherent { .. }, .. }) => {
273                {
    ::core::panicking::panic_fmt(format_args!("unexpected inherent projection"));
}panic!("unexpected inherent projection")
274            }
275
276            _ => self.pretty_print_type(ty),
277        }
278    }
279
280    fn print_dyn_existential(
281        &mut self,
282        predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
283    ) -> Result<(), PrintError> {
284        let mut first = true;
285        for p in predicates {
286            if !first {
287                self.write_fmt(format_args!("+"))write!(self, "+")?;
288            }
289            first = false;
290            p.print(self)?;
291        }
292        Ok(())
293    }
294
295    fn print_const(&mut self, ct: ty::Const<'tcx>) -> Result<(), PrintError> {
296        // only print integers
297        match ct.kind() {
298            ty::ConstKind::Value(cv) if cv.ty.is_integral() => {
299                // The `pretty_print_const` formatting depends on -Zverbose-internals
300                // flag, so we cannot reuse it here.
301                let scalar = cv.to_leaf();
302                let signed = #[allow(non_exhaustive_omitted_patterns)] match cv.ty.kind() {
    ty::Int(_) => true,
    _ => false,
}matches!(cv.ty.kind(), ty::Int(_));
303                self.write_fmt(format_args!("{0:#?}",
        ty::ConstInt::new(scalar, signed, cv.ty.is_ptr_sized_integral())))write!(
304                    self,
305                    "{:#?}",
306                    ty::ConstInt::new(scalar, signed, cv.ty.is_ptr_sized_integral())
307                )?;
308            }
309            _ => self.write_str("_")?,
310        }
311        Ok(())
312    }
313
314    fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
315        self.write_str(self.tcx.crate_name(cnum).as_str())?;
316        Ok(())
317    }
318
319    fn print_path_with_qualified(
320        &mut self,
321        self_ty: Ty<'tcx>,
322        trait_ref: Option<ty::TraitRef<'tcx>>,
323    ) -> Result<(), PrintError> {
324        // Similar to `pretty_print_path_with_qualified`, but for the other
325        // types that are printed as paths (see `print_type` above).
326        match self_ty.kind() {
327            ty::FnDef(..)
328            | ty::Alias(..)
329            | ty::Closure(..)
330            | ty::CoroutineClosure(..)
331            | ty::Coroutine(..)
332                if trait_ref.is_none() =>
333            {
334                self.print_type(self_ty)
335            }
336
337            _ => self.pretty_print_path_with_qualified(self_ty, trait_ref),
338        }
339    }
340
341    fn print_path_with_impl(
342        &mut self,
343        print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
344        self_ty: Ty<'tcx>,
345        trait_ref: Option<ty::TraitRef<'tcx>>,
346    ) -> Result<(), PrintError> {
347        self.pretty_print_path_with_impl(
348            |cx| {
349                print_prefix(cx)?;
350
351                if cx.keep_within_component {
352                    // HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
353                    cx.write_str("::")?;
354                } else {
355                    cx.path.finalize_pending_component();
356                }
357
358                Ok(())
359            },
360            self_ty,
361            trait_ref,
362        )
363    }
364
365    fn print_path_with_simple(
366        &mut self,
367        print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
368        disambiguated_data: &DisambiguatedDefPathData,
369    ) -> Result<(), PrintError> {
370        print_prefix(self)?;
371
372        // Skip `::{{extern}}` blocks and `::{{constructor}}` on tuple/unit structs.
373        if let DefPathData::ForeignMod | DefPathData::Ctor = disambiguated_data.data {
374            return Ok(());
375        }
376
377        if self.keep_within_component {
378            // HACK(eddyb) print the path similarly to how `FmtPrinter` prints it.
379            self.write_str("::")?;
380        } else {
381            self.path.finalize_pending_component();
382        }
383
384        self.write_fmt(format_args!("{0}", disambiguated_data.data))write!(self, "{}", disambiguated_data.data)?;
385
386        Ok(())
387    }
388
389    fn print_path_with_generic_args(
390        &mut self,
391        print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
392        args: &[GenericArg<'tcx>],
393    ) -> Result<(), PrintError> {
394        print_prefix(self)?;
395
396        let args =
397            args.iter().cloned().filter(|arg| !#[allow(non_exhaustive_omitted_patterns)] match arg.kind() {
    GenericArgKind::Lifetime(_) => true,
    _ => false,
}matches!(arg.kind(), GenericArgKind::Lifetime(_)));
398        if args.clone().next().is_some() {
399            self.generic_delimiters(|cx| cx.comma_sep(args))
400        } else {
401            Ok(())
402        }
403    }
404
405    fn print_impl_path(
406        &mut self,
407        impl_def_id: DefId,
408        args: &'tcx [GenericArg<'tcx>],
409    ) -> Result<(), PrintError> {
410        let self_ty = self.tcx.type_of(impl_def_id);
411        let impl_trait_ref = self.tcx.impl_opt_trait_ref(impl_def_id);
412        let generics = self.tcx.generics_of(impl_def_id);
413        // We have two cases to worry about here:
414        // 1. We're printing a nested item inside of an impl item, like an inner
415        // function inside of a method. Due to the way that def path printing works,
416        // we'll render this something like `<Ty as Trait>::method::inner_fn`
417        // but we have no substs for this impl since it's not really inheriting
418        // generics from the outer item. We need to use the identity substs, and
419        // to normalize we need to use the correct param-env too.
420        // 2. We're mangling an item with identity substs. This seems to only happen
421        // when generating coverage, since we try to generate coverage for unused
422        // items too, and if something isn't monomorphized then we necessarily don't
423        // have anything to substitute the instance with.
424        // NOTE: We don't support mangling partially substituted but still polymorphic
425        // instances, like `impl<A> Tr<A> for ()` where `A` is substituted w/ `(T,)`.
426        let (typing_env, mut self_ty, mut impl_trait_ref) = if generics.count() > args.len()
427            || &args[..generics.count()]
428                == self
429                    .tcx
430                    .erase_and_anonymize_regions(ty::GenericArgs::identity_for_item(
431                        self.tcx,
432                        impl_def_id,
433                    ))
434                    .as_slice()
435        {
436            (
437                ty::TypingEnv::post_analysis(self.tcx, impl_def_id),
438                self_ty.instantiate_identity().skip_norm_wip(),
439                impl_trait_ref
440                    .map(|impl_trait_ref| impl_trait_ref.instantiate_identity().skip_norm_wip()),
441            )
442        } else {
443            if !!args.has_non_region_param() {
    {
        ::core::panicking::panic_fmt(format_args!("should not be mangling partially substituted polymorphic instance: {0:?} {1:?}",
                impl_def_id, args));
    }
};assert!(
444                !args.has_non_region_param(),
445                "should not be mangling partially substituted \
446                polymorphic instance: {impl_def_id:?} {args:?}"
447            );
448            (
449                ty::TypingEnv::fully_monomorphized(),
450                self_ty.instantiate(self.tcx, args).skip_norm_wip(),
451                impl_trait_ref.map(|impl_trait_ref| {
452                    impl_trait_ref.instantiate(self.tcx, args).skip_norm_wip()
453                }),
454            )
455        };
456
457        match &mut impl_trait_ref {
458            Some(impl_trait_ref) => {
459                match (&impl_trait_ref.self_ty(), &self_ty) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(impl_trait_ref.self_ty(), self_ty);
460                *impl_trait_ref = self
461                    .tcx
462                    .normalize_erasing_regions(typing_env, Unnormalized::new_wip(*impl_trait_ref));
463                self_ty = impl_trait_ref.self_ty();
464            }
465            None => {
466                self_ty =
467                    self.tcx.normalize_erasing_regions(typing_env, Unnormalized::new_wip(self_ty));
468            }
469        }
470
471        self.default_print_impl_path(impl_def_id, self_ty, impl_trait_ref)
472    }
473}
474
475impl<'tcx> PrettyPrinter<'tcx> for LegacySymbolMangler<'tcx> {
476    fn should_print_optional_region(&self, _region: ty::Region<'_>) -> bool {
477        false
478    }
479
480    // Identical to `PrettyPrinter::comma_sep` except there is no space after each comma.
481    fn comma_sep<T>(&mut self, mut elems: impl Iterator<Item = T>) -> Result<(), PrintError>
482    where
483        T: Print<'tcx, Self>,
484    {
485        if let Some(first) = elems.next() {
486            first.print(self)?;
487            for elem in elems {
488                self.write_str(",")?;
489                elem.print(self)?;
490            }
491        }
492        Ok(())
493    }
494
495    fn generic_delimiters(
496        &mut self,
497        f: impl FnOnce(&mut Self) -> Result<(), PrintError>,
498    ) -> Result<(), PrintError> {
499        self.write_fmt(format_args!("<"))write!(self, "<")?;
500
501        let kept_within_component = mem::replace(&mut self.keep_within_component, true);
502        f(self)?;
503        self.keep_within_component = kept_within_component;
504
505        self.write_fmt(format_args!(">"))write!(self, ">")?;
506
507        Ok(())
508    }
509}
510
511impl fmt::Write for LegacySymbolMangler<'_> {
512    fn write_str(&mut self, s: &str) -> fmt::Result {
513        // Name sanitation. LLVM will happily accept identifiers with weird names, but
514        // gas doesn't!
515        // gas accepts the following characters in symbols: a-z, A-Z, 0-9, ., _, $
516        // NVPTX assembly has more strict naming rules than gas, so additionally, dots
517        // are replaced with '$' there.
518
519        for c in s.chars() {
520            if self.path.temp_buf.is_empty() {
521                match c {
522                    'a'..='z' | 'A'..='Z' | '_' => {}
523                    _ => {
524                        // Underscore-qualify anything that didn't start as an ident.
525                        self.path.temp_buf.push('_');
526                    }
527                }
528            }
529            match c {
530                // Escape these with $ sequences
531                '@' => self.path.temp_buf.push_str("$SP$"),
532                '*' => self.path.temp_buf.push_str("$BP$"),
533                '&' => self.path.temp_buf.push_str("$RF$"),
534                '<' => self.path.temp_buf.push_str("$LT$"),
535                '>' => self.path.temp_buf.push_str("$GT$"),
536                '(' => self.path.temp_buf.push_str("$LP$"),
537                ')' => self.path.temp_buf.push_str("$RP$"),
538                ',' => self.path.temp_buf.push_str("$C$"),
539
540                '-' | ':' | '.' if self.tcx.has_strict_asm_symbol_naming() => {
541                    // NVPTX doesn't support these characters in symbol names.
542                    self.path.temp_buf.push('$')
543                }
544
545                // '.' doesn't occur in types and functions, so reuse it
546                // for ':' and '-'
547                '-' | ':' => self.path.temp_buf.push('.'),
548
549                // Avoid crashing LLVM in certain (LTO-related) situations, see #60925.
550                'm' if self.path.temp_buf.ends_with(".llv") => self.path.temp_buf.push_str("$u6d$"),
551
552                // These are legal symbols
553                'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '.' | '$' => self.path.temp_buf.push(c),
554
555                _ => {
556                    self.path.temp_buf.push('$');
557                    for c in c.escape_unicode().skip(1) {
558                        match c {
559                            '{' => {}
560                            '}' => self.path.temp_buf.push('$'),
561                            c => self.path.temp_buf.push(c),
562                        }
563                    }
564                }
565            }
566        }
567
568        Ok(())
569    }
570}