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.
67use 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};
1112use crate::errors::{Kind, TestOutput};
1314const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
15const DEF_PATH: Symbol = sym::rustc_def_path;
1617pub 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.
21if !tcx.features().rustc_attrs() {
22return;
23 }
2425tcx.dep_graph.with_ignore(|| {
26let mut symbol_names = SymbolNamesTest { tcx };
27let crate_items = tcx.hir_crate_items(());
2829for id in crate_items.free_items() {
30 symbol_names.process_attrs(id.owner_id.def_id);
31 }
3233for id in crate_items.trait_items() {
34 symbol_names.process_attrs(id.owner_id.def_id);
35 }
3637for id in crate_items.impl_items() {
38 symbol_names.process_attrs(id.owner_id.def_id);
39 }
4041for id in crate_items.foreign_items() {
42 symbol_names.process_attrs(id.owner_id.def_id);
43 }
44 })
45}
4647struct SymbolNamesTest<'tcx> {
48 tcx: TyCtxt<'tcx>,
49}
5051impl SymbolNamesTest<'_> {
52fn process_attrs(&mut self, def_id: LocalDefId) {
53let 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.
57for attr in tcx.get_attrs(def_id, SYMBOL_NAME) {
58let def_id = def_id.to_def_id();
59let instance = Instance::new(
60 def_id,
61 tcx.erase_regions(GenericArgs::identity_for_item(tcx, def_id)),
62 );
63let mangled = tcx.symbol_name(instance);
64 tcx.dcx().emit_err(TestOutput {
65 span: attr.span,
66 kind: Kind::SymbolName,
67 content: format!("{mangled}"),
68 });
69if 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 }
8283for 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}