Skip to main content

rustc_attr_ir/
pretty_printing.rs

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