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.
|
|
|
|
|
2021-01-31 17:21:04 +00:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2020-10-06 06:00:55 +00:00
|
|
|
use rustc_middle::ty::print::with_no_trimmed_paths;
|
2023-07-11 21:35:29 +00:00
|
|
|
use rustc_middle::ty::{GenericArgs, Instance, TyCtxt};
|
2020-01-01 18:30:57 +00:00
|
|
|
use rustc_span::symbol::{Symbol, sym};
|
2016-03-16 19:00:20 +00:00
|
|
|
|
2022-09-13 20:19:32 +00:00
|
|
|
use crate::errors::{Kind, TestOutput};
|
2024-07-28 22:13:50 +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.
|
2024-10-09 07:01:57 +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(|| {
|
2022-04-07 16:29:57 +00:00
|
|
|
let mut symbol_names = SymbolNamesTest { tcx };
|
2022-04-07 02:03:42 +00:00
|
|
|
let crate_items = tcx.hir_crate_items(());
|
|
|
|
|
2024-03-20 22:52:26 +00:00
|
|
|
for id in crate_items.free_items() {
|
2022-10-27 03:02:18 +00:00
|
|
|
symbol_names.process_attrs(id.owner_id.def_id);
|
2022-04-07 02:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for id in crate_items.trait_items() {
|
2022-10-27 03:02:18 +00:00
|
|
|
symbol_names.process_attrs(id.owner_id.def_id);
|
2022-04-07 02:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for id in crate_items.impl_items() {
|
2022-10-27 03:02:18 +00:00
|
|
|
symbol_names.process_attrs(id.owner_id.def_id);
|
2022-04-07 02:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for id in crate_items.foreign_items() {
|
2022-10-27 03:02:18 +00:00
|
|
|
symbol_names.process_attrs(id.owner_id.def_id);
|
2022-04-07 02:03:42 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-12-14 08:01:32 +00:00
|
|
|
impl SymbolNamesTest<'_> {
|
2021-01-31 17:21:04 +00:00
|
|
|
fn process_attrs(&mut self, def_id: LocalDefId) {
|
2017-04-14 19:30:06 +00:00
|
|
|
let tcx = self.tcx;
|
2022-05-02 07:31:56 +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.
|
2023-03-13 18:54:05 +00:00
|
|
|
for attr in tcx.get_attrs(def_id, SYMBOL_NAME) {
|
2022-05-02 07:31:56 +00:00
|
|
|
let def_id = def_id.to_def_id();
|
|
|
|
let instance = Instance::new(
|
|
|
|
def_id,
|
2023-07-11 21:35:29 +00:00
|
|
|
tcx.erase_regions(GenericArgs::identity_for_item(tcx, def_id)),
|
2022-05-02 07:31:56 +00:00
|
|
|
);
|
|
|
|
let mangled = tcx.symbol_name(instance);
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(TestOutput {
|
2022-08-27 04:24:13 +00:00
|
|
|
span: attr.span,
|
2022-09-13 20:19:32 +00:00
|
|
|
kind: Kind::SymbolName,
|
|
|
|
content: format!("{mangled}"),
|
2022-08-27 04:24:13 +00:00
|
|
|
});
|
2022-05-02 07:31:56 +00:00
|
|
|
if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(TestOutput {
|
2022-08-21 04:38:23 +00:00
|
|
|
span: attr.span,
|
2022-09-13 20:19:32 +00:00
|
|
|
kind: Kind::Demangling,
|
|
|
|
content: format!("{demangling}"),
|
2022-08-21 04:38:23 +00:00
|
|
|
});
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(TestOutput {
|
2022-08-21 04:38:23 +00:00
|
|
|
span: attr.span,
|
2022-09-13 20:19:32 +00:00
|
|
|
kind: Kind::DemanglingAlt,
|
2022-12-19 09:31:55 +00:00
|
|
|
content: format!("{demangling:#}"),
|
2022-08-21 04:38:23 +00:00
|
|
|
});
|
2016-03-16 19:00:20 +00:00
|
|
|
}
|
2022-05-02 07:31:56 +00:00
|
|
|
}
|
2016-03-16 19:00:20 +00:00
|
|
|
|
2023-03-13 18:54:05 +00:00
|
|
|
for attr in tcx.get_attrs(def_id, DEF_PATH) {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(TestOutput {
|
2022-08-21 04:51:33 +00:00
|
|
|
span: attr.span,
|
2022-09-13 20:19:32 +00:00
|
|
|
kind: Kind::DefPath,
|
2023-02-16 09:25:11 +00:00
|
|
|
content: with_no_trimmed_paths!(tcx.def_path_str(def_id)),
|
2022-08-21 04:51:33 +00:00
|
|
|
});
|
2016-03-16 19:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|