2016-03-16 19:00:20 +00:00
|
|
|
//! Walks the crate looking for items/impl-items/trait-items that have
|
2018-12-19 10:31:35 +00:00
|
|
|
//! either a `rustc_symbol_name` or `rustc_def_path` attribute and
|
2016-03-16 19:00:20 +00:00
|
|
|
//! generates an error giving, respectively, the symbol name or
|
2018-12-19 10:31:35 +00:00
|
|
|
//! def-path. This is used for unit testing the code that generates
|
2016-03-16 19:00:20 +00:00
|
|
|
//! paths etc in all kinds of annoying scenarios.
|
|
|
|
|
2016-03-29 05:50:44 +00:00
|
|
|
use rustc::hir;
|
2019-05-23 17:15:48 +00:00
|
|
|
use rustc::ty::{TyCtxt, Instance};
|
2019-05-08 03:21:18 +00:00
|
|
|
use syntax::symbol::{Symbol, sym};
|
2016-03-16 19:00:20 +00:00
|
|
|
|
2019-05-08 03:21:18 +00:00
|
|
|
const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
|
|
|
|
const DEF_PATH: Symbol = sym::rustc_def_path;
|
2016-03-16 19:00:20 +00:00
|
|
|
|
2019-06-21 18:27:44 +00:00
|
|
|
pub fn report_symbol_names(tcx: TyCtxt<'_>) {
|
2016-03-16 19:00:20 +00:00
|
|
|
// if the `rustc_attrs` feature is not enabled, then the
|
|
|
|
// attributes we are interested in cannot be present anyway, so
|
|
|
|
// skip the walk.
|
2018-02-14 15:11:02 +00:00
|
|
|
if !tcx.features().rustc_attrs {
|
2016-03-16 19:00:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-28 05:05:45 +00:00
|
|
|
tcx.dep_graph.with_ignore(|| {
|
2018-11-06 20:05:44 +00:00
|
|
|
let mut visitor = SymbolNamesTest { tcx };
|
2018-12-04 12:45:36 +00:00
|
|
|
tcx.hir().krate().visit_all_item_likes(&mut visitor);
|
2017-12-28 05:05:45 +00:00
|
|
|
})
|
2016-03-16 19:00:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
struct SymbolNamesTest<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2016-03-16 19:00:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
impl SymbolNamesTest<'tcx> {
|
2016-03-16 19:00:20 +00:00
|
|
|
fn process_attrs(&mut self,
|
2019-02-26 10:04:58 +00:00
|
|
|
hir_id: hir::HirId) {
|
2017-04-14 19:30:06 +00:00
|
|
|
let tcx = self.tcx;
|
2019-06-27 09:28:14 +00:00
|
|
|
let def_id = tcx.hir().local_def_id(hir_id);
|
2016-03-22 17:23:36 +00:00
|
|
|
for attr in tcx.get_attrs(def_id).iter() {
|
2016-03-16 19:00:20 +00:00
|
|
|
if attr.check_name(SYMBOL_NAME) {
|
2016-03-21 17:11:42 +00:00
|
|
|
// for now, can only use on monomorphic names
|
2017-02-08 17:31:03 +00:00
|
|
|
let instance = Instance::mono(tcx, def_id);
|
2019-05-29 20:58:55 +00:00
|
|
|
let mangled = self.tcx.symbol_name(instance);
|
|
|
|
tcx.sess.span_err(attr.span, &format!("symbol-name({})", mangled));
|
2019-09-03 06:06:42 +00:00
|
|
|
if let Ok(demangling) = rustc_demangle::try_demangle(&mangled.name.as_str()) {
|
2019-05-29 20:58:55 +00:00
|
|
|
tcx.sess.span_err(attr.span, &format!("demangling({})", demangling));
|
|
|
|
tcx.sess.span_err(attr.span, &format!("demangling-alt({:#})", demangling));
|
|
|
|
}
|
2018-12-19 10:31:35 +00:00
|
|
|
} else if attr.check_name(DEF_PATH) {
|
|
|
|
let path = tcx.def_path_str(def_id);
|
|
|
|
tcx.sess.span_err(attr.span, &format!("def-path({})", path));
|
2016-03-16 19:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// (*) The formatting of `tag({})` is chosen so that tests can elect
|
|
|
|
// to test the entirety of the string, if they choose, or else just
|
|
|
|
// some subset.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
impl hir::itemlikevisit::ItemLikeVisitor<'tcx> for SymbolNamesTest<'tcx> {
|
2019-11-28 18:28:50 +00:00
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
|
2019-02-26 10:04:58 +00:00
|
|
|
self.process_attrs(item.hir_id);
|
2016-03-16 19:00:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 22:10:43 +00:00
|
|
|
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
|
2019-02-26 10:04:58 +00:00
|
|
|
self.process_attrs(trait_item.hir_id);
|
2016-03-16 19:00:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 22:10:43 +00:00
|
|
|
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
|
2019-02-26 10:04:58 +00:00
|
|
|
self.process_attrs(impl_item.hir_id);
|
2016-03-16 19:00:20 +00:00
|
|
|
}
|
|
|
|
}
|