2016-03-16 14:00:20 -05:00
|
|
|
//! Walks the crate looking for items/impl-items/trait-items that have
|
2018-12-19 04:31:35 -06:00
|
|
|
//! either a `rustc_symbol_name` or `rustc_def_path` attribute and
|
2016-03-16 14:00:20 -05:00
|
|
|
//! generates an error giving, respectively, the symbol name or
|
2018-12-19 04:31:35 -06:00
|
|
|
//! def-path. This is used for unit testing the code that generates
|
2016-03-16 14:00:20 -05:00
|
|
|
//! paths etc in all kinds of annoying scenarios.
|
|
|
|
|
2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir as hir;
|
2020-10-06 01:00:55 -05:00
|
|
|
use rustc_middle::ty::print::with_no_trimmed_paths;
|
2020-08-18 12:40:03 -05:00
|
|
|
use rustc_middle::ty::{subst::InternalSubsts, Instance, TyCtxt};
|
2020-01-01 12:30:57 -06:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2016-03-16 14:00:20 -05:00
|
|
|
|
2019-05-07 22:21:18 -05:00
|
|
|
const SYMBOL_NAME: Symbol = sym::rustc_symbol_name;
|
|
|
|
const DEF_PATH: Symbol = sym::rustc_def_path;
|
2016-03-16 14:00:20 -05:00
|
|
|
|
2019-06-21 13:27:44 -05:00
|
|
|
pub fn report_symbol_names(tcx: TyCtxt<'_>) {
|
2016-03-16 14:00:20 -05: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 09:11:02 -06:00
|
|
|
if !tcx.features().rustc_attrs {
|
2016-03-16 14:00:20 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-27 23:05:45 -06:00
|
|
|
tcx.dep_graph.with_ignore(|| {
|
2018-11-06 14:05:44 -06:00
|
|
|
let mut visitor = SymbolNamesTest { tcx };
|
2018-12-04 06:45:36 -06:00
|
|
|
tcx.hir().krate().visit_all_item_likes(&mut visitor);
|
2017-12-27 23:05:45 -06:00
|
|
|
})
|
2016-03-16 14:00:20 -05:00
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
struct SymbolNamesTest<'tcx> {
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2016-03-16 14:00:20 -05:00
|
|
|
}
|
|
|
|
|
2019-06-11 14:03:44 -05:00
|
|
|
impl SymbolNamesTest<'tcx> {
|
2019-12-22 16:42:04 -06:00
|
|
|
fn process_attrs(&mut self, hir_id: hir::HirId) {
|
2017-04-14 14:30:06 -05:00
|
|
|
let tcx = self.tcx;
|
2019-06-27 04:28:14 -05:00
|
|
|
let def_id = tcx.hir().local_def_id(hir_id);
|
2020-04-09 03:43:00 -05:00
|
|
|
for attr in tcx.get_attrs(def_id.to_def_id()).iter() {
|
2020-07-29 20:27:50 -05:00
|
|
|
if tcx.sess.check_name(attr, SYMBOL_NAME) {
|
2020-08-18 12:40:03 -05:00
|
|
|
let def_id = def_id.to_def_id();
|
|
|
|
let instance = Instance::new(
|
|
|
|
def_id,
|
2020-10-23 19:21:18 -05:00
|
|
|
tcx.erase_regions(InternalSubsts::identity_for_item(tcx, def_id)),
|
2020-08-18 12:40:03 -05:00
|
|
|
);
|
2020-07-29 20:27:50 -05:00
|
|
|
let mangled = tcx.symbol_name(instance);
|
2019-05-29 15:58:55 -05:00
|
|
|
tcx.sess.span_err(attr.span, &format!("symbol-name({})", mangled));
|
2020-07-10 00:45:05 -05:00
|
|
|
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
|
2019-05-29 15:58:55 -05:00
|
|
|
tcx.sess.span_err(attr.span, &format!("demangling({})", demangling));
|
|
|
|
tcx.sess.span_err(attr.span, &format!("demangling-alt({:#})", demangling));
|
|
|
|
}
|
2020-07-29 20:27:50 -05:00
|
|
|
} else if tcx.sess.check_name(attr, DEF_PATH) {
|
2020-10-06 01:00:55 -05:00
|
|
|
let path = with_no_trimmed_paths(|| tcx.def_path_str(def_id.to_def_id()));
|
2018-12-19 04:31:35 -06:00
|
|
|
tcx.sess.span_err(attr.span, &format!("def-path({})", path));
|
2016-03-16 14:00:20 -05: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 14:03:44 -05:00
|
|
|
impl hir::itemlikevisit::ItemLikeVisitor<'tcx> for SymbolNamesTest<'tcx> {
|
2019-11-28 12:28:50 -06:00
|
|
|
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
|
2021-01-30 10:47:51 -06:00
|
|
|
self.process_attrs(item.hir_id());
|
2016-03-16 14:00:20 -05:00
|
|
|
}
|
|
|
|
|
2019-11-28 14:47:10 -06:00
|
|
|
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
|
2021-01-30 13:46:50 -06:00
|
|
|
self.process_attrs(trait_item.hir_id());
|
2016-03-16 14:00:20 -05:00
|
|
|
}
|
|
|
|
|
2019-11-28 15:16:44 -06:00
|
|
|
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
|
2021-01-30 16:25:03 -06:00
|
|
|
self.process_attrs(impl_item.hir_id());
|
2016-03-16 14:00:20 -05:00
|
|
|
}
|
2020-11-11 14:57:54 -06:00
|
|
|
|
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &'tcx hir::ForeignItem<'tcx>) {
|
2021-01-31 17:33:38 -06:00
|
|
|
self.process_attrs(foreign_item.hir_id());
|
2020-11-11 14:57:54 -06:00
|
|
|
}
|
2016-03-16 14:00:20 -05:00
|
|
|
}
|