]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_utils/symbol_names_test.rs
Rollup merge of #63416 - RalfJung:apfloat, r=eddyb
[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, Instance};
9 use syntax::symbol::{Symbol, sym};
10
11 const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
12 const DEF_PATH: Symbol = sym::rustc_def_path;
13
14 pub fn report_symbol_names(tcx: TyCtxt<'_>) {
15     // if the `rustc_attrs` feature is not enabled, then the
16     // attributes we are interested in cannot be present anyway, so
17     // skip the walk.
18     if !tcx.features().rustc_attrs {
19         return;
20     }
21
22     tcx.dep_graph.with_ignore(|| {
23         let mut visitor = SymbolNamesTest { tcx };
24         tcx.hir().krate().visit_all_item_likes(&mut visitor);
25     })
26 }
27
28 struct SymbolNamesTest<'tcx> {
29     tcx: TyCtxt<'tcx>,
30 }
31
32 impl SymbolNamesTest<'tcx> {
33     fn process_attrs(&mut self,
34                      hir_id: hir::HirId) {
35         let tcx = self.tcx;
36         let def_id = tcx.hir().local_def_id(hir_id);
37         for attr in tcx.get_attrs(def_id).iter() {
38             if attr.check_name(SYMBOL_NAME) {
39                 // for now, can only use on monomorphic names
40                 let instance = Instance::mono(tcx, def_id);
41                 let mangled = self.tcx.symbol_name(instance);
42                 tcx.sess.span_err(attr.span, &format!("symbol-name({})", mangled));
43                 if let Ok(demangling) = rustc_demangle::try_demangle(&mangled.name.as_str()) {
44                     tcx.sess.span_err(attr.span, &format!("demangling({})", demangling));
45                     tcx.sess.span_err(attr.span, &format!("demangling-alt({:#})", demangling));
46                 }
47             } else if attr.check_name(DEF_PATH) {
48                 let path = tcx.def_path_str(def_id);
49                 tcx.sess.span_err(attr.span, &format!("def-path({})", path));
50             }
51
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
54             // some subset.
55         }
56     }
57 }
58
59 impl hir::itemlikevisit::ItemLikeVisitor<'tcx> for SymbolNamesTest<'tcx> {
60     fn visit_item(&mut self, item: &'tcx hir::Item) {
61         self.process_attrs(item.hir_id);
62     }
63
64     fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
65         self.process_attrs(trait_item.hir_id);
66     }
67
68     fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
69         self.process_attrs(impl_item.hir_id);
70     }
71 }