]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_utils/symbol_names_test.rs
Auto merge of #61361 - estebank:infer-type, r=varkor
[rust.git] / src / librustc_codegen_utils / symbol_names_test.rs
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.
6
7 use rustc::hir;
8 use rustc::ty::TyCtxt;
9 use rustc_mir::monomorphize::Instance;
10 use syntax::symbol::{Symbol, sym};
11
12 const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
13 const DEF_PATH: Symbol = sym::rustc_def_path;
14
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
18     // skip the walk.
19     if !tcx.features().rustc_attrs {
20         return;
21     }
22
23     tcx.dep_graph.with_ignore(|| {
24         let mut visitor = SymbolNamesTest { tcx };
25         tcx.hir().krate().visit_all_item_likes(&mut visitor);
26     })
27 }
28
29 struct SymbolNamesTest<'a, 'tcx:'a> {
30     tcx: TyCtxt<'a, 'tcx, 'tcx>,
31 }
32
33 impl<'a, 'tcx> SymbolNamesTest<'a, 'tcx> {
34     fn process_attrs(&mut self,
35                      hir_id: hir::HirId) {
36         let tcx = self.tcx;
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));
47                 }
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));
51             }
52
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
55             // some subset.
56         }
57     }
58 }
59
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);
63     }
64
65     fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
66         self.process_attrs(trait_item.hir_id);
67     }
68
69     fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
70         self.process_attrs(impl_item.hir_id);
71     }
72 }