1 //! Walks the crate looking for items/impl-items/trait-items that have
2 //! either a `rustc_symbol_name` or `rustc_item_path` attribute and
3 //! generates an error giving, respectively, the symbol name or
4 //! item-path. This is used for unit testing the code that generates
5 //! paths etc in all kinds of annoying scenarios.
11 use rustc_mir::monomorphize::Instance;
13 const SYMBOL_NAME: &'static str = "rustc_symbol_name";
14 const ITEM_PATH: &'static str = "rustc_item_path";
16 pub fn report_symbol_names<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
17 // if the `rustc_attrs` feature is not enabled, then the
18 // attributes we are interested in cannot be present anyway, so
20 if !tcx.features().rustc_attrs {
24 tcx.dep_graph.with_ignore(|| {
25 let mut visitor = SymbolNamesTest { tcx };
26 tcx.hir().krate().visit_all_item_likes(&mut visitor);
30 struct SymbolNamesTest<'a, 'tcx:'a> {
31 tcx: TyCtxt<'a, 'tcx, 'tcx>,
34 impl<'a, 'tcx> SymbolNamesTest<'a, 'tcx> {
35 fn process_attrs(&mut self,
36 node_id: ast::NodeId) {
38 let def_id = tcx.hir().local_def_id(node_id);
39 for attr in tcx.get_attrs(def_id).iter() {
40 if attr.check_name(SYMBOL_NAME) {
41 // for now, can only use on monomorphic names
42 let instance = Instance::mono(tcx, def_id);
43 let name = self.tcx.symbol_name(instance);
44 tcx.sess.span_err(attr.span, &format!("symbol-name({})", name));
45 } else if attr.check_name(ITEM_PATH) {
46 let path = tcx.item_path_str(def_id);
47 tcx.sess.span_err(attr.span, &format!("item-path({})", path));
50 // (*) The formatting of `tag({})` is chosen so that tests can elect
51 // to test the entirety of the string, if they choose, or else just
57 impl<'a, 'tcx> hir::itemlikevisit::ItemLikeVisitor<'tcx> for SymbolNamesTest<'a, 'tcx> {
58 fn visit_item(&mut self, item: &'tcx hir::Item) {
59 self.process_attrs(item.id);
62 fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
63 self.process_attrs(trait_item.id);
66 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
67 self.process_attrs(impl_item.id);