1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::errors::{Kind, TestOutput};
use rustc_hir::def_id::LocalDefId;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_middle::ty::{subst::InternalSubsts, Instance, TyCtxt};
use rustc_span::symbol::{sym, Symbol};
const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
const DEF_PATH: Symbol = sym::rustc_def_path;
pub fn report_symbol_names(tcx: TyCtxt<'_>) {
if !tcx.features().rustc_attrs {
return;
}
tcx.dep_graph.with_ignore(|| {
let mut symbol_names = SymbolNamesTest { tcx };
let crate_items = tcx.hir_crate_items(());
for id in crate_items.items() {
symbol_names.process_attrs(id.def_id);
}
for id in crate_items.trait_items() {
symbol_names.process_attrs(id.def_id);
}
for id in crate_items.impl_items() {
symbol_names.process_attrs(id.def_id);
}
for id in crate_items.foreign_items() {
symbol_names.process_attrs(id.def_id);
}
})
}
struct SymbolNamesTest<'tcx> {
tcx: TyCtxt<'tcx>,
}
impl SymbolNamesTest<'_> {
fn process_attrs(&mut self, def_id: LocalDefId) {
let tcx = self.tcx;
for attr in tcx.get_attrs(def_id.to_def_id(), SYMBOL_NAME) {
let def_id = def_id.to_def_id();
let instance = Instance::new(
def_id,
tcx.erase_regions(InternalSubsts::identity_for_item(tcx, def_id)),
);
let mangled = tcx.symbol_name(instance);
tcx.sess.emit_err(TestOutput {
span: attr.span,
kind: Kind::SymbolName,
content: format!("{mangled}"),
});
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
tcx.sess.emit_err(TestOutput {
span: attr.span,
kind: Kind::Demangling,
content: format!("{demangling}"),
});
tcx.sess.emit_err(TestOutput {
span: attr.span,
kind: Kind::DemanglingAlt,
content: format!("{:#}", demangling),
});
}
}
for attr in tcx.get_attrs(def_id.to_def_id(), DEF_PATH) {
tcx.sess.emit_err(TestOutput {
span: attr.span,
kind: Kind::DefPath,
content: with_no_trimmed_paths!(tcx.def_path_str(def_id.to_def_id())),
});
}
}
}