rust/compiler/rustc_symbol_mangling/src/test.rs

75 lines
2.7 KiB
Rust
Raw Normal View History

2016-03-16 14:00:20 -05:00
//! Walks the crate looking for items/impl-items/trait-items that have
//! 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
//! 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.
use rustc_hir::def_id::LocalDefId;
use rustc_middle::ty::print::with_no_trimmed_paths;
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
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;
}
tcx.dep_graph.with_ignore(|| {
let mut symbol_names = SymbolNamesTest { tcx };
let crate_items = tcx.hir_crate_items(());
for id in crate_items.items() {
symbol_names.process_attrs(id.def_id);
}
for id in crate_items.trait_items() {
symbol_names.process_attrs(id.def_id);
}
for id in crate_items.impl_items() {
symbol_names.process_attrs(id.def_id);
}
for id in crate_items.foreign_items() {
symbol_names.process_attrs(id.def_id);
}
})
2016-03-16 14:00:20 -05:00
}
struct SymbolNamesTest<'tcx> {
2019-06-13 16:48:52 -05:00
tcx: TyCtxt<'tcx>,
2016-03-16 14:00:20 -05:00
}
impl SymbolNamesTest<'_> {
fn process_attrs(&mut self, def_id: LocalDefId) {
let tcx = self.tcx;
2022-05-02 02:31:56 -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.
for attr in tcx.get_attrs(def_id.to_def_id(), SYMBOL_NAME) {
let def_id = def_id.to_def_id();
let instance = Instance::new(
def_id,
tcx.erase_regions(InternalSubsts::identity_for_item(tcx, def_id)),
);
let mangled = tcx.symbol_name(instance);
tcx.sess.span_err(attr.span, &format!("symbol-name({})", mangled));
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
tcx.sess.span_err(attr.span, &format!("demangling({})", demangling));
tcx.sess.span_err(attr.span, &format!("demangling-alt({:#})", demangling));
2016-03-16 14:00:20 -05:00
}
2022-05-02 02:31:56 -05:00
}
2016-03-16 14:00:20 -05:00
2022-05-02 02:31:56 -05:00
for attr in tcx.get_attrs(def_id.to_def_id(), DEF_PATH) {
let path = with_no_trimmed_paths!(tcx.def_path_str(def_id.to_def_id()));
tcx.sess.span_err(attr.span, &format!("def-path({})", path));
2016-03-16 14:00:20 -05:00
}
}
}