@@ -35,33 +35,39 @@ pub trait HashStableContext: rustc_ast::HashStableContext + rustc_abi::HashStabl
35
35
/// like [`Span`]s and empty tuples, are gracefully skipped so they don't clutter the
36
36
/// representation much.
37
37
pub trait PrintAttribute {
38
- fn print_something ( & self ) -> bool ;
38
+ /// Whether or not this will render as something meaningful, or if it's skipped
39
+ /// (which will force the containing struct to also skip printing a comma
40
+ /// and the field name).
41
+ fn should_render ( & self ) -> bool ;
42
+
39
43
fn print_attribute ( & self , p : & mut Printer ) ;
40
44
}
41
45
42
46
impl < T : PrintAttribute > PrintAttribute for & T {
43
- fn print_something ( & self ) -> bool {
44
- T :: print_something ( self )
47
+ fn should_render ( & self ) -> bool {
48
+ T :: should_render ( self )
45
49
}
46
50
47
51
fn print_attribute ( & self , p : & mut Printer ) {
48
52
T :: print_attribute ( self , p)
49
53
}
50
54
}
51
55
impl < T : PrintAttribute > PrintAttribute for Option < T > {
52
- fn print_something ( & self ) -> bool {
53
- self . as_ref ( ) . is_some_and ( |x| x. print_something ( ) )
56
+ fn should_render ( & self ) -> bool {
57
+ self . as_ref ( ) . is_some_and ( |x| x. should_render ( ) )
54
58
}
59
+
55
60
fn print_attribute ( & self , p : & mut Printer ) {
56
61
if let Some ( i) = self {
57
62
T :: print_attribute ( i, p)
58
63
}
59
64
}
60
65
}
61
66
impl < T : PrintAttribute > PrintAttribute for ThinVec < T > {
62
- fn print_something ( & self ) -> bool {
63
- self . is_empty ( ) || self [ 0 ] . print_something ( )
67
+ fn should_render ( & self ) -> bool {
68
+ self . is_empty ( ) || self [ 0 ] . should_render ( )
64
69
}
70
+
65
71
fn print_attribute ( & self , p : & mut Printer ) {
66
72
let mut last_printed = false ;
67
73
p. word ( "[" ) ;
@@ -70,15 +76,15 @@ impl<T: PrintAttribute> PrintAttribute for ThinVec<T> {
70
76
p. word_space ( "," ) ;
71
77
}
72
78
i. print_attribute ( p) ;
73
- last_printed = i. print_something ( ) ;
79
+ last_printed = i. should_render ( ) ;
74
80
}
75
81
p. word ( "]" ) ;
76
82
}
77
83
}
78
84
macro_rules! print_skip {
79
85
( $( $t: ty) ,* $( , ) ?) => { $(
80
86
impl PrintAttribute for $t {
81
- fn print_something ( & self ) -> bool { false }
87
+ fn should_render ( & self ) -> bool { false }
82
88
fn print_attribute( & self , _: & mut Printer ) { }
83
89
} ) *
84
90
} ;
@@ -87,7 +93,7 @@ macro_rules! print_skip {
87
93
macro_rules! print_disp {
88
94
( $( $t: ty) ,* $( , ) ?) => { $(
89
95
impl PrintAttribute for $t {
90
- fn print_something ( & self ) -> bool { true }
96
+ fn should_render ( & self ) -> bool { true }
91
97
fn print_attribute( & self , p: & mut Printer ) {
92
98
p. word( format!( "{}" , self ) ) ;
93
99
}
@@ -97,7 +103,7 @@ macro_rules! print_disp {
97
103
macro_rules! print_debug {
98
104
( $( $t: ty) ,* $( , ) ?) => { $(
99
105
impl PrintAttribute for $t {
100
- fn print_something ( & self ) -> bool { true }
106
+ fn should_render ( & self ) -> bool { true }
101
107
fn print_attribute( & self , p: & mut Printer ) {
102
108
p. word( format!( "{:?}" , self ) ) ;
103
109
}
@@ -106,37 +112,39 @@ macro_rules! print_debug {
106
112
}
107
113
108
114
macro_rules! print_tup {
109
- ( num_print_something $( $ts: ident) * ) => { 0 $( + $ts. print_something ( ) as usize ) * } ;
115
+ ( num_should_render $( $ts: ident) * ) => { 0 $( + $ts. should_render ( ) as usize ) * } ;
110
116
( ) => { } ;
111
117
( $t: ident $( $ts: ident) * ) => {
112
118
#[ allow( non_snake_case, unused) ]
113
119
impl <$t: PrintAttribute , $( $ts: PrintAttribute ) ,* > PrintAttribute for ( $t, $( $ts) ,* ) {
114
- fn print_something ( & self ) -> bool {
120
+ fn should_render ( & self ) -> bool {
115
121
let ( $t, $( $ts) ,* ) = self ;
116
- print_tup!( num_print_something $t $( $ts) * ) != 0
122
+ print_tup!( num_should_render $t $( $ts) * ) != 0
117
123
}
118
124
119
125
fn print_attribute( & self , p: & mut Printer ) {
120
126
let ( $t, $( $ts) ,* ) = self ;
121
- let parens = print_tup!( num_print_something $t $( $ts) * ) > 1 ;
127
+ let parens = print_tup!( num_should_render $t $( $ts) * ) > 1 ;
122
128
if parens {
123
- p. word ( "(" ) ;
129
+ p. popen ( ) ;
124
130
}
125
131
126
- let mut printed_anything = $t. print_something ( ) ;
132
+ let mut printed_anything = $t. should_render ( ) ;
127
133
128
134
$t. print_attribute( p) ;
129
135
130
136
$(
131
- if printed_anything && $ts. print_something( ) {
132
- p. word_space( "," ) ;
137
+ if $ts. should_render( ) {
138
+ if printed_anything {
139
+ p. word_space( "," ) ;
140
+ }
133
141
printed_anything = true ;
134
142
}
135
143
$ts. print_attribute( p) ;
136
144
) *
137
145
138
146
if parens {
139
- p. word ( ")" ) ;
147
+ p. pclose ( ) ;
140
148
}
141
149
}
142
150
}
@@ -147,8 +155,8 @@ macro_rules! print_tup {
147
155
148
156
print_tup ! ( A B C D E F G H ) ;
149
157
print_skip ! ( Span , ( ) ) ;
150
- print_disp ! ( Symbol , u16 , bool , NonZero <u32 >) ;
151
- print_debug ! ( UintTy , IntTy , Align , AttrStyle , CommentKind , Transparency ) ;
158
+ print_disp ! ( u16 , bool , NonZero <u32 >) ;
159
+ print_debug ! ( Symbol , UintTy , IntTy , Align , AttrStyle , CommentKind , Transparency ) ;
152
160
153
161
/// Finds attributes in sequences of attributes by pattern matching.
154
162
///
0 commit comments