diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /compiler/rustc_symbol_mangling/src/test.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | compiler/rustc_symbol_mangling/src/test.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/compiler/rustc_symbol_mangling/src/test.rs b/compiler/rustc_symbol_mangling/src/test.rs new file mode 100644 index 000000000..7249ce04c --- /dev/null +++ b/compiler/rustc_symbol_mangling/src/test.rs @@ -0,0 +1,74 @@ +//! Walks the crate looking for items/impl-items/trait-items that have +//! either a `rustc_symbol_name` or `rustc_def_path` attribute and +//! generates an error giving, respectively, the symbol name or +//! def-path. This is used for unit testing the code that generates +//! 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}; +use rustc_span::symbol::{sym, Symbol}; + +const SYMBOL_NAME: Symbol = sym::rustc_symbol_name; +const DEF_PATH: Symbol = sym::rustc_def_path; + +pub fn report_symbol_names(tcx: TyCtxt<'_>) { + // if the `rustc_attrs` feature is not enabled, then the + // attributes we are interested in cannot be present anyway, so + // skip the walk. + if !tcx.features().rustc_attrs { + 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); + } + }) +} + +struct SymbolNamesTest<'tcx> { + tcx: TyCtxt<'tcx>, +} + +impl SymbolNamesTest<'_> { + fn process_attrs(&mut self, def_id: LocalDefId) { + let tcx = self.tcx; + // 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)); + } + } + + 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)); + } + } +} |