Skip to main content

rustc_symbol_mangling/
test.rs

1//! Walks the crate looking for items/impl-items/trait-items that have
2//! either a `rustc_symbol_name` or `rustc_def_path` attribute and
3//! generates an error giving, respectively, the symbol name or
4//! def-path. This is used for unit testing the code that generates
5//! paths etc in all kinds of annoying scenarios.
6
7use rustc_hir::def_id::LocalDefId;
8use rustc_hir::find_attr;
9use rustc_middle::ty::print::with_no_trimmed_paths;
10use rustc_middle::ty::{GenericArgs, Instance, TyCtxt};
11
12use crate::errors::{Kind, TestOutput};
13
14pub fn report_symbol_names(tcx: TyCtxt<'_>) {
15    // if the `rustc_attrs` feature is not enabled, then the
16    // attributes we are interested in cannot be present anyway, so
17    // skip the walk.
18    if !tcx.features().rustc_attrs() {
19        return;
20    }
21
22    tcx.dep_graph.with_ignore(|| {
23        let mut symbol_names = SymbolNamesTest { tcx };
24        let crate_items = tcx.hir_crate_items(());
25
26        for id in crate_items.free_items() {
27            symbol_names.process_attrs(id.owner_id.def_id);
28        }
29
30        for id in crate_items.trait_items() {
31            symbol_names.process_attrs(id.owner_id.def_id);
32        }
33
34        for id in crate_items.impl_items() {
35            symbol_names.process_attrs(id.owner_id.def_id);
36        }
37
38        for id in crate_items.foreign_items() {
39            symbol_names.process_attrs(id.owner_id.def_id);
40        }
41    })
42}
43
44struct SymbolNamesTest<'tcx> {
45    tcx: TyCtxt<'tcx>,
46}
47
48impl SymbolNamesTest<'_> {
49    fn process_attrs(&mut self, def_id: LocalDefId) {
50        let tcx = self.tcx;
51        // The formatting of `tag({})` is chosen so that tests can elect
52        // to test the entirety of the string, if they choose, or else just
53        // some subset.
54
55        if let Some(attr_span) = {

    #[allow(deprecated)]
    {
        {
            'done:
                {
                for i in tcx.get_all_attrs(def_id) {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(RustcSymbolName(span)) => {
                            break 'done Some(span);
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }
}find_attr!(tcx, def_id, RustcSymbolName(span) => span) {
56            let def_id = def_id.to_def_id();
57            let instance = Instance::new_raw(
58                def_id,
59                tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(tcx, def_id)),
60            );
61            let mangled = tcx.symbol_name(instance);
62            tcx.dcx().emit_err(TestOutput {
63                span: *attr_span,
64                kind: Kind::SymbolName,
65                content: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}", mangled))
    })format!("{mangled}"),
66            });
67            if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
68                tcx.dcx().emit_err(TestOutput {
69                    span: *attr_span,
70                    kind: Kind::Demangling,
71                    content: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}", demangling))
    })format!("{demangling}"),
72                });
73                tcx.dcx().emit_err(TestOutput {
74                    span: *attr_span,
75                    kind: Kind::DemanglingAlt,
76                    content: ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:#}", demangling))
    })format!("{demangling:#}"),
77                });
78            }
79        }
80
81        if let Some(attr_span) = {

    #[allow(deprecated)]
    {
        {
            'done:
                {
                for i in tcx.get_all_attrs(def_id) {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(RustcDefPath(span)) => {
                            break 'done Some(span);
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }
}find_attr!(
82            tcx, def_id,
83            RustcDefPath(span) => span
84        ) {
85            tcx.dcx().emit_err(TestOutput {
86                span: *attr_span,
87                kind: Kind::DefPath,
88                content: { let _guard = NoTrimmedGuard::new(); tcx.def_path_str(def_id) }with_no_trimmed_paths!(tcx.def_path_str(def_id)),
89            });
90        }
91    }
92}