]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_symbol_mangling/src/test.rs
Rollup merge of #99019 - pierwill:doc-mir-statement, r=cjgillot
[rust.git] / compiler / rustc_symbol_mangling / src / 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::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};
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(tcx: TyCtxt<'_>) {
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 symbol_names = SymbolNamesTest { tcx };
25         let crate_items = tcx.hir_crate_items(());
26
27         for id in crate_items.items() {
28             symbol_names.process_attrs(id.def_id);
29         }
30
31         for id in crate_items.trait_items() {
32             symbol_names.process_attrs(id.def_id);
33         }
34
35         for id in crate_items.impl_items() {
36             symbol_names.process_attrs(id.def_id);
37         }
38
39         for id in crate_items.foreign_items() {
40             symbol_names.process_attrs(id.def_id);
41         }
42     })
43 }
44
45 struct SymbolNamesTest<'tcx> {
46     tcx: TyCtxt<'tcx>,
47 }
48
49 impl SymbolNamesTest<'_> {
50     fn process_attrs(&mut self, def_id: LocalDefId) {
51         let tcx = self.tcx;
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         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(
58                 def_id,
59                 tcx.erase_regions(InternalSubsts::identity_for_item(tcx, def_id)),
60             );
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));
66             }
67         }
68
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));
72         }
73     }
74 }