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.
7 use rustc_hir::def_id::LocalDefId;
8 use rustc_middle::ty::print::with_no_trimmed_paths;
9 use rustc_middle::ty::{subst::InternalSubsts, Instance, TyCtxt};
10 use rustc_span::symbol::{sym, Symbol};
12 const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
13 const DEF_PATH: Symbol = sym::rustc_def_path;
15 pub fn report_symbol_names(tcx: TyCtxt<'_>) {
16 // if the `rustc_attrs` feature is not enabled, then the
17 // attributes we are interested in cannot be present anyway, so
19 if !tcx.features().rustc_attrs {
23 tcx.dep_graph.with_ignore(|| {
24 let mut symbol_names = SymbolNamesTest { tcx };
25 let crate_items = tcx.hir_crate_items(());
27 for id in crate_items.items() {
28 symbol_names.process_attrs(id.def_id);
31 for id in crate_items.trait_items() {
32 symbol_names.process_attrs(id.def_id);
35 for id in crate_items.impl_items() {
36 symbol_names.process_attrs(id.def_id);
39 for id in crate_items.foreign_items() {
40 symbol_names.process_attrs(id.def_id);
45 struct SymbolNamesTest<'tcx> {
49 impl SymbolNamesTest<'_> {
50 fn process_attrs(&mut self, def_id: LocalDefId) {
52 // The formatting of `tag({})` is chosen so that tests can elect
53 // to test the entirety of the string, if they choose, or else just
55 for attr in tcx.get_attrs(def_id.to_def_id(), SYMBOL_NAME) {
56 let def_id = def_id.to_def_id();
57 let instance = Instance::new(
59 tcx.erase_regions(InternalSubsts::identity_for_item(tcx, def_id)),
61 let mangled = tcx.symbol_name(instance);
62 tcx.sess.span_err(attr.span, &format!("symbol-name({})", mangled));
63 if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
64 tcx.sess.span_err(attr.span, &format!("demangling({})", demangling));
65 tcx.sess.span_err(attr.span, &format!("demangling-alt({:#})", demangling));
69 for attr in tcx.get_attrs(def_id.to_def_id(), DEF_PATH) {
70 let path = with_no_trimmed_paths!(tcx.def_path_str(def_id.to_def_id()));
71 tcx.sess.span_err(attr.span, &format!("def-path({})", path));