1use std::num::NonZero;
2use std::ops::Deref;
3use std::path::PathBuf;
4
5use rustc_abi::Align;
6use rustc_ast::ast::{Path, join_path_idents};
7use rustc_ast::attr::data_structures::CfgEntry;
8use rustc_ast::attr::version::RustcVersion;
9use rustc_ast::expand::autodiff_attrs::{DiffActivity, DiffMode};
10use rustc_ast::token::{CommentKind, DocFragmentKind};
11use rustc_ast::{AttrId, AttrStyle, IntTy, UintTy};
12use rustc_ast_pretty::pp::Printer;
13use rustc_data_structures::fx::FxIndexMap;
14use rustc_lint_defs::{LintName, Reflectiveness, ToolOptions};
15use rustc_span::def_id::DefId;
16use rustc_span::hygiene::Transparency;
17use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};
18use rustc_structures::{CollapseMacroDebuginfo, CrateType, Limit, NativeLibKind, SanitizerSet};
19use thin_vec::ThinVec;
20
21use crate::HashIgnoredAttrId;
22
23pub trait PrintAttribute {
30 fn should_render(&self) -> bool;
34
35 fn print_attribute(&self, p: &mut Printer);
36}
37
38impl<T: ?Sized + PrintAttribute> PrintAttribute for &T {
39 fn should_render(&self) -> bool {
40 T::should_render(self)
41 }
42
43 fn print_attribute(&self, p: &mut Printer) {
44 T::print_attribute(self, p)
45 }
46}
47impl<T: ?Sized + PrintAttribute> PrintAttribute for Box<T> {
48 fn should_render(&self) -> bool {
49 self.deref().should_render()
50 }
51
52 fn print_attribute(&self, p: &mut Printer) {
53 T::print_attribute(self.deref(), p)
54 }
55}
56impl<T: PrintAttribute> PrintAttribute for Option<T> {
57 fn should_render(&self) -> bool {
58 self.as_ref().is_some_and(|x| x.should_render())
59 }
60
61 fn print_attribute(&self, p: &mut Printer) {
62 if let Some(i) = self {
63 T::print_attribute(i, p)
64 }
65 }
66}
67
68impl<T: PrintAttribute> PrintAttribute for [T] {
69 fn should_render(&self) -> bool {
70 self.is_empty() || self[0].should_render()
71 }
72
73 fn print_attribute(&self, p: &mut Printer) {
74 let mut last_printed = false;
75 p.word("[");
76 for i in self {
77 if last_printed {
78 p.word_space(",");
79 }
80 i.print_attribute(p);
81 last_printed = i.should_render();
82 }
83 p.word("]");
84 }
85}
86
87impl<T: PrintAttribute> PrintAttribute for ThinVec<T> {
88 fn should_render(&self) -> bool {
89 self.as_slice().should_render()
90 }
91
92 fn print_attribute(&self, p: &mut Printer) {
93 self.as_slice().print_attribute(p)
94 }
95}
96impl<T: PrintAttribute, T2: PrintAttribute> PrintAttribute for FxIndexMap<T, T2> {
97 fn should_render(&self) -> bool {
98 self.is_empty() || self[0].should_render()
99 }
100
101 fn print_attribute(&self, p: &mut Printer) {
102 let mut last_printed = false;
103 p.word("[");
104 for (i, _) in self {
105 if last_printed {
106 p.word_space(",");
107 }
108 i.print_attribute(p);
109 last_printed = i.should_render();
110 }
111 p.word("]");
112 }
113}
114impl PrintAttribute for PathBuf {
115 fn should_render(&self) -> bool {
116 true
117 }
118
119 fn print_attribute(&self, p: &mut Printer) {
120 p.word(self.display().to_string());
121 }
122}
123impl PrintAttribute for Path {
124 fn should_render(&self) -> bool {
125 true
126 }
127
128 fn print_attribute(&self, p: &mut Printer) {
129 p.word(join_path_idents(self.segments.iter().map(|seg| seg.ident)));
130 }
131}
132
133macro_rules! print_skip {
134 ($($t: ty),* $(,)?) => {$(
135 impl PrintAttribute for $t {
136 fn should_render(&self) -> bool { false }
137 fn print_attribute(&self, _: &mut Printer) { }
138 })*
139 };
140}
141
142macro_rules! print_disp {
143 ($($t: ty),* $(,)?) => {$(
144 impl PrintAttribute for $t {
145 fn should_render(&self) -> bool { true }
146 fn print_attribute(&self, p: &mut Printer) {
147 p.word(format!("{}", self));
148 }
149 }
150 )*};
151}
152macro_rules! print_debug {
153 ($($t: ty),* $(,)?) => {$(
154 impl PrintAttribute for $t {
155 fn should_render(&self) -> bool { true }
156 fn print_attribute(&self, p: &mut Printer) {
157 p.word(format!("{:?}", self));
158 }
159 }
160 )*};
161}
162
163macro_rules! print_tup {
164 (num_should_render $($ts: ident)*) => { 0 $(+ $ts.should_render() as usize)* };
165 () => {};
166 ($t: ident $($ts: ident)*) => {
167 #[allow(non_snake_case, unused)]
168 impl<$t: PrintAttribute, $($ts: PrintAttribute),*> PrintAttribute for ($t, $($ts),*) {
169 fn should_render(&self) -> bool {
170 let ($t, $($ts),*) = self;
171 print_tup!(num_should_render $t $($ts)*) != 0
172 }
173
174 fn print_attribute(&self, p: &mut Printer) {
175 let ($t, $($ts),*) = self;
176 let parens = print_tup!(num_should_render $t $($ts)*) > 1;
177 if parens {
178 p.popen();
179 }
180
181 let mut printed_anything = $t.should_render();
182
183 $t.print_attribute(p);
184
185 $(
186 if $ts.should_render() {
187 if printed_anything {
188 p.word_space(",");
189 }
190 printed_anything = true;
191 }
192 $ts.print_attribute(p);
193 )*
194
195 if parens {
196 p.pclose();
197 }
198 }
199 }
200
201 print_tup!($($ts)*);
202 };
203}
204
205#[allow(non_snake_case, unused)]
impl<H: PrintAttribute> PrintAttribute for (H,) {
fn should_render(&self) -> bool {
let (H,) = self;
0 + H.should_render() as usize != 0
}
fn print_attribute(&self, p: &mut Printer) {
let (H,) = self;
let parens = 0 + H.should_render() as usize > 1;
if parens { p.popen(); }
let mut printed_anything = H.should_render();
H.print_attribute(p);
if parens { p.pclose(); }
}
}print_tup!(A B C D E F G H);
206impl PrintAttribute for Span {
fn should_render(&self) -> bool { false }
fn print_attribute(&self, _: &mut Printer) {}
}
impl PrintAttribute for () {
fn should_render(&self) -> bool { false }
fn print_attribute(&self, _: &mut Printer) {}
}
impl PrintAttribute for ErrorGuaranteed {
fn should_render(&self) -> bool { false }
fn print_attribute(&self, _: &mut Printer) {}
}
impl PrintAttribute for AttrId {
fn should_render(&self) -> bool { false }
fn print_attribute(&self, _: &mut Printer) {}
}
impl PrintAttribute for HashIgnoredAttrId {
fn should_render(&self) -> bool { false }
fn print_attribute(&self, _: &mut Printer) {}
}print_skip!(Span, (), ErrorGuaranteed, AttrId, HashIgnoredAttrId);
207impl PrintAttribute for u8 {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", self))
}));
}
}
impl PrintAttribute for u16 {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", self))
}));
}
}
impl PrintAttribute for u32 {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", self))
}));
}
}
impl PrintAttribute for u128 {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", self))
}));
}
}
impl PrintAttribute for usize {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", self))
}));
}
}
impl PrintAttribute for bool {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", self))
}));
}
}
impl PrintAttribute for NonZero<u32> {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", self))
}));
}
}
impl PrintAttribute for Limit {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", self))
}));
}
}print_disp!(u8, u16, u32, u128, usize, bool, NonZero<u32>, Limit);
208impl PrintAttribute for Symbol {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for Ident {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for UintTy {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for IntTy {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for Align {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for AttrStyle {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for CommentKind {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for DocFragmentKind {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for Transparency {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for SanitizerSet {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for DefId {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for RustcVersion {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for CfgEntry {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for DiffActivity {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for DiffMode {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for CrateType {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for NativeLibKind {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for CollapseMacroDebuginfo {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for LintName {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for ToolOptions {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}
impl PrintAttribute for Reflectiveness {
fn should_render(&self) -> bool { true }
fn print_attribute(&self, p: &mut Printer) {
p.word(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", self))
}));
}
}print_debug!(
209 Symbol,
210 Ident,
211 UintTy,
212 IntTy,
213 Align,
214 AttrStyle,
215 CommentKind,
216 DocFragmentKind,
217 Transparency,
218 SanitizerSet,
219 DefId,
220 RustcVersion,
221 CfgEntry,
222 DiffActivity,
223 DiffMode,
224 CrateType,
225 NativeLibKind,
226 CollapseMacroDebuginfo,
227 LintName,
228 ToolOptions,
229 Reflectiveness,
230);