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