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_middle::ty::print::with_no_trimmed_paths;
9use rustc_middle::ty::{GenericArgs, Instance, TyCtxt};
10use rustc_span::{Symbol, sym};
11
12use crate::errors::{Kind, TestOutput};
13
14const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
15const DEF_PATH: Symbol = sym::rustc_def_path;
16
17pub fn report_symbol_names(tcx: TyCtxt<'_>) {
18    // if the `rustc_attrs` feature is not enabled, then the
19    // attributes we are interested in cannot be present anyway, so
20    // skip the walk.
21    if !tcx.features().rustc_attrs() {
22        return;
23    }
24
25    tcx.dep_graph.with_ignore(|| {
26        let mut symbol_names = SymbolNamesTest { tcx };
27        let crate_items = tcx.hir_crate_items(());
28
29        for id in crate_items.free_items() {
30            symbol_names.process_attrs(id.owner_id.def_id);
31        }
32
33        for id in crate_items.trait_items() {
34            symbol_names.process_attrs(id.owner_id.def_id);
35        }
36
37        for id in crate_items.impl_items() {
38            symbol_names.process_attrs(id.owner_id.def_id);
39        }
40
41        for id in crate_items.foreign_items() {
42            symbol_names.process_attrs(id.owner_id.def_id);
43        }
44    })
45}
46
47struct SymbolNamesTest<'tcx> {
48    tcx: TyCtxt<'tcx>,
49}
50
51impl SymbolNamesTest<'_> {
52    fn process_attrs(&mut self, def_id: LocalDefId) {
53        let tcx = self.tcx;
54        // The formatting of `tag({})` is chosen so that tests can elect
55        // to test the entirety of the string, if they choose, or else just
56        // some subset.
57        for attr in tcx.get_attrs(def_id, SYMBOL_NAME) {
58            let def_id = def_id.to_def_id();
59            let instance = Instance::new(
60                def_id,
61                tcx.erase_regions(GenericArgs::identity_for_item(tcx, def_id)),
62            );
63            let mangled = tcx.symbol_name(instance);
64            tcx.dcx().emit_err(TestOutput {
65                span: attr.span,
66                kind: Kind::SymbolName,
67                content: format!("{mangled}"),
68            });
69            if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
70                tcx.dcx().emit_err(TestOutput {
71                    span: attr.span,
72                    kind: Kind::Demangling,
73                    content: format!("{demangling}"),
74                });
75                tcx.dcx().emit_err(TestOutput {
76                    span: attr.span,
77                    kind: Kind::DemanglingAlt,
78                    content: format!("{demangling:#}"),
79                });
80            }
81        }
82
83        for attr in tcx.get_attrs(def_id, DEF_PATH) {
84            tcx.dcx().emit_err(TestOutput {
85                span: attr.span,
86                kind: Kind::DefPath,
87                content: with_no_trimmed_paths!(tcx.def_path_str(def_id)),
88            });
89        }
90    }
91}