|
7 | 7 | use rustc_hir::def_id::LocalDefId; |
8 | 8 | use rustc_middle::ty::print::with_no_trimmed_paths; |
9 | 9 | use rustc_middle::ty::{GenericArgs, Instance, TyCtxt}; |
10 | | -use rustc_span::{Symbol, sym}; |
| 10 | +use rustc_span::Span; |
11 | 11 |
|
12 | 12 | use crate::errors::{Kind, TestOutput}; |
13 | 13 |
|
14 | | -const SYMBOL_NAME: Symbol = sym::rustc_symbol_name; |
15 | | -const DEF_PATH: Symbol = sym::rustc_def_path; |
16 | | - |
17 | | -pub fn report_symbol_names(tcx: TyCtxt<'_>) { |
18 | | - // if the `rustc_attrs` feature is not enabled, then the |
19 | | - // attributes we are interested in cannot be present anyway, so |
20 | | - // skip the walk. |
21 | | - if !tcx.features().rustc_attrs() { |
22 | | - return; |
| 14 | +#[inline(always)] |
| 15 | +pub fn process_symbol_name_attr<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, attr_span: Span) { |
| 16 | + let def_id = def_id.to_def_id(); |
| 17 | + let instance = Instance::new_raw( |
| 18 | + def_id, |
| 19 | + tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(tcx, def_id)), |
| 20 | + ); |
| 21 | + let mangled = tcx.symbol_name(instance); |
| 22 | + tcx.dcx().emit_err(TestOutput { |
| 23 | + span: attr_span, |
| 24 | + kind: Kind::SymbolName, |
| 25 | + content: format!("{mangled}"), |
| 26 | + }); |
| 27 | + if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) { |
| 28 | + tcx.dcx().emit_err(TestOutput { |
| 29 | + span: attr_span, |
| 30 | + kind: Kind::Demangling, |
| 31 | + content: format!("{demangling}"), |
| 32 | + }); |
| 33 | + tcx.dcx().emit_err(TestOutput { |
| 34 | + span: attr_span, |
| 35 | + kind: Kind::DemanglingAlt, |
| 36 | + content: format!("{demangling:#}"), |
| 37 | + }); |
23 | 38 | } |
24 | | - |
25 | | - tcx.dep_graph.with_ignore(|| { |
26 | | - let mut symbol_names = SymbolNamesTest { tcx }; |
27 | | - let crate_items = tcx.hir_crate_items(()); |
28 | | - |
29 | | - for id in crate_items.free_items() { |
30 | | - symbol_names.process_attrs(id.owner_id.def_id); |
31 | | - } |
32 | | - |
33 | | - for id in crate_items.trait_items() { |
34 | | - symbol_names.process_attrs(id.owner_id.def_id); |
35 | | - } |
36 | | - |
37 | | - for id in crate_items.impl_items() { |
38 | | - symbol_names.process_attrs(id.owner_id.def_id); |
39 | | - } |
40 | | - |
41 | | - for id in crate_items.foreign_items() { |
42 | | - symbol_names.process_attrs(id.owner_id.def_id); |
43 | | - } |
44 | | - }) |
45 | | -} |
46 | | - |
47 | | -struct SymbolNamesTest<'tcx> { |
48 | | - tcx: TyCtxt<'tcx>, |
49 | 39 | } |
50 | 40 |
|
51 | | -impl SymbolNamesTest<'_> { |
52 | | - fn process_attrs(&mut self, def_id: LocalDefId) { |
53 | | - let tcx = self.tcx; |
54 | | - // The formatting of `tag({})` is chosen so that tests can elect |
55 | | - // to test the entirety of the string, if they choose, or else just |
56 | | - // some subset. |
57 | | - for attr in tcx.get_attrs(def_id, SYMBOL_NAME) { |
58 | | - let def_id = def_id.to_def_id(); |
59 | | - let instance = Instance::new_raw( |
60 | | - def_id, |
61 | | - tcx.erase_and_anonymize_regions(GenericArgs::identity_for_item(tcx, def_id)), |
62 | | - ); |
63 | | - let mangled = tcx.symbol_name(instance); |
64 | | - tcx.dcx().emit_err(TestOutput { |
65 | | - span: attr.span(), |
66 | | - kind: Kind::SymbolName, |
67 | | - content: format!("{mangled}"), |
68 | | - }); |
69 | | - if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) { |
70 | | - tcx.dcx().emit_err(TestOutput { |
71 | | - span: attr.span(), |
72 | | - kind: Kind::Demangling, |
73 | | - content: format!("{demangling}"), |
74 | | - }); |
75 | | - tcx.dcx().emit_err(TestOutput { |
76 | | - span: attr.span(), |
77 | | - kind: Kind::DemanglingAlt, |
78 | | - content: format!("{demangling:#}"), |
79 | | - }); |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - for attr in tcx.get_attrs(def_id, DEF_PATH) { |
84 | | - tcx.dcx().emit_err(TestOutput { |
85 | | - span: attr.span(), |
86 | | - kind: Kind::DefPath, |
87 | | - content: with_no_trimmed_paths!(tcx.def_path_str(def_id)), |
88 | | - }); |
89 | | - } |
90 | | - } |
| 41 | +#[inline(always)] |
| 42 | +pub fn process_def_path_attr<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, attr_span: Span) { |
| 43 | + tcx.dcx().emit_err(TestOutput { |
| 44 | + span: attr_span, |
| 45 | + kind: Kind::DefPath, |
| 46 | + content: with_no_trimmed_paths!(tcx.def_path_str(def_id)), |
| 47 | + }); |
91 | 48 | } |
0 commit comments