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.
9 use rustc_mir::monomorphize::Instance;
10 use syntax::symbol::{Symbol, sym};
12 const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
13 const DEF_PATH: Symbol = sym::rustc_def_path;
15 pub fn report_symbol_names<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
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 visitor = SymbolNamesTest { tcx };
25 tcx.hir().krate().visit_all_item_likes(&mut visitor);
29 struct SymbolNamesTest<'a, 'tcx:'a> {
30 tcx: TyCtxt<'a, 'tcx, 'tcx>,
33 impl<'a, 'tcx> SymbolNamesTest<'a, 'tcx> {
34 fn process_attrs(&mut self,
37 let def_id = tcx.hir().local_def_id_from_hir_id(hir_id);
38 for attr in tcx.get_attrs(def_id).iter() {
39 if attr.check_name(SYMBOL_NAME) {
40 // for now, can only use on monomorphic names
41 let instance = Instance::mono(tcx, def_id);
42 let mangled = self.tcx.symbol_name(instance);
43 tcx.sess.span_err(attr.span, &format!("symbol-name({})", mangled));
44 if let Ok(demangling) = rustc_demangle::try_demangle(&mangled.as_str()) {
45 tcx.sess.span_err(attr.span, &format!("demangling({})", demangling));
46 tcx.sess.span_err(attr.span, &format!("demangling-alt({:#})", demangling));
48 } else if attr.check_name(DEF_PATH) {
49 let path = tcx.def_path_str(def_id);
50 tcx.sess.span_err(attr.span, &format!("def-path({})", path));
53 // (*) The formatting of `tag({})` is chosen so that tests can elect
54 // to test the entirety of the string, if they choose, or else just
60 impl<'a, 'tcx> hir::itemlikevisit::ItemLikeVisitor<'tcx> for SymbolNamesTest<'a, 'tcx> {
61 fn visit_item(&mut self, item: &'tcx hir::Item) {
62 self.process_attrs(item.hir_id);
65 fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
66 self.process_attrs(trait_item.hir_id);
69 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
70 self.process_attrs(impl_item.hir_id);