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