rustc_symbol_mangling/
test.rs1use rustc_hir::attrs::AttributeKind;
8use rustc_hir::def_id::LocalDefId;
9use rustc_hir::find_attr;
10use rustc_middle::ty::print::with_no_trimmed_paths;
11use rustc_middle::ty::{GenericArgs, Instance, TyCtxt};
12
13use crate::errors::{Kind, TestOutput};
14
15pub fn report_symbol_names(tcx: TyCtxt<'_>) {
16 if !tcx.features().rustc_attrs() {
20 return;
21 }
22
23 tcx.dep_graph.with_ignore(|| {
24 let mut symbol_names = SymbolNamesTest { tcx };
25 let crate_items = tcx.hir_crate_items(());
26
27 for id in crate_items.free_items() {
28 symbol_names.process_attrs(id.owner_id.def_id);
29 }
30
31 for id in crate_items.trait_items() {
32 symbol_names.process_attrs(id.owner_id.def_id);
33 }
34
35 for id in crate_items.impl_items() {
36 symbol_names.process_attrs(id.owner_id.def_id);
37 }
38
39 for id in crate_items.foreign_items() {
40 symbol_names.process_attrs(id.owner_id.def_id);
41 }
42 })
43}
44
45struct SymbolNamesTest<'tcx> {
46 tcx: TyCtxt<'tcx>,
47}
48
49impl SymbolNamesTest<'_> {
50 fn process_attrs(&mut self, def_id: LocalDefId) {
51 let tcx = self.tcx;
52 if let Some(attr_span) = {
'done:
{
for i in tcx.get_all_attrs(def_id) {
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(AttributeKind::RustcSymbolName(span))
=> {
break 'done Some(span);
}
_ => {}
}
}
None
}
}find_attr!(
57 tcx.get_all_attrs(def_id),
58 AttributeKind::RustcSymbolName(span) => span
59 ) {
60 let def_id = def_id.to_def_id();
61 let instance = Instance::new_raw(
62 def_id,
63 tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(tcx, def_id)),
64 );
65 let mangled = tcx.symbol_name(instance);
66 tcx.dcx().emit_err(TestOutput {
67 span: *attr_span,
68 kind: Kind::SymbolName,
69 content: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", mangled))
})format!("{mangled}"),
70 });
71 if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
72 tcx.dcx().emit_err(TestOutput {
73 span: *attr_span,
74 kind: Kind::Demangling,
75 content: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", demangling))
})format!("{demangling}"),
76 });
77 tcx.dcx().emit_err(TestOutput {
78 span: *attr_span,
79 kind: Kind::DemanglingAlt,
80 content: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:#}", demangling))
})format!("{demangling:#}"),
81 });
82 }
83 }
84
85 if let Some(attr_span) = {
'done:
{
for i in tcx.get_all_attrs(def_id) {
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(AttributeKind::RustcDefPath(span))
=> {
break 'done Some(span);
}
_ => {}
}
}
None
}
}find_attr!(
86 tcx.get_all_attrs(def_id),
87 AttributeKind::RustcDefPath(span) => span
88 ) {
89 tcx.dcx().emit_err(TestOutput {
90 span: *attr_span,
91 kind: Kind::DefPath,
92 content: { let _guard = NoTrimmedGuard::new(); tcx.def_path_str(def_id) }with_no_trimmed_paths!(tcx.def_path_str(def_id)),
93 });
94 }
95 }
96}