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