@@ -35,33 +35,39 @@ pub trait HashStableContext: rustc_ast::HashStableContext + rustc_abi::HashStabl
3535/// like [`Span`]s and empty tuples, are gracefully skipped so they don't clutter the
3636/// representation much.
3737pub 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+
3943 fn print_attribute ( & self , p : & mut Printer ) ;
4044}
4145
4246impl < 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 )
4549 }
4650
4751 fn print_attribute ( & self , p : & mut Printer ) {
4852 T :: print_attribute ( self , p)
4953 }
5054}
5155impl < 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 ( ) )
5458 }
59+
5560 fn print_attribute ( & self , p : & mut Printer ) {
5661 if let Some ( i) = self {
5762 T :: print_attribute ( i, p)
5863 }
5964 }
6065}
6166impl < 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 ( )
6469 }
70+
6571 fn print_attribute ( & self , p : & mut Printer ) {
6672 let mut last_printed = false ;
6773 p. word ( "[" ) ;
@@ -70,15 +76,15 @@ impl<T: PrintAttribute> PrintAttribute for ThinVec<T> {
7076 p. word_space ( "," ) ;
7177 }
7278 i. print_attribute ( p) ;
73- last_printed = i. print_something ( ) ;
79+ last_printed = i. should_render ( ) ;
7480 }
7581 p. word ( "]" ) ;
7682 }
7783}
7884macro_rules! print_skip {
7985 ( $( $t: ty) ,* $( , ) ?) => { $(
8086 impl PrintAttribute for $t {
81- fn print_something ( & self ) -> bool { false }
87+ fn should_render ( & self ) -> bool { false }
8288 fn print_attribute( & self , _: & mut Printer ) { }
8389 } ) *
8490 } ;
@@ -87,7 +93,7 @@ macro_rules! print_skip {
8793macro_rules! print_disp {
8894 ( $( $t: ty) ,* $( , ) ?) => { $(
8995 impl PrintAttribute for $t {
90- fn print_something ( & self ) -> bool { true }
96+ fn should_render ( & self ) -> bool { true }
9197 fn print_attribute( & self , p: & mut Printer ) {
9298 p. word( format!( "{}" , self ) ) ;
9399 }
@@ -97,7 +103,7 @@ macro_rules! print_disp {
97103macro_rules! print_debug {
98104 ( $( $t: ty) ,* $( , ) ?) => { $(
99105 impl PrintAttribute for $t {
100- fn print_something ( & self ) -> bool { true }
106+ fn should_render ( & self ) -> bool { true }
101107 fn print_attribute( & self , p: & mut Printer ) {
102108 p. word( format!( "{:?}" , self ) ) ;
103109 }
@@ -106,37 +112,39 @@ macro_rules! print_debug {
106112}
107113
108114macro_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 ) * } ;
110116 ( ) => { } ;
111117 ( $t: ident $( $ts: ident) * ) => {
112118 #[ allow( non_snake_case, unused) ]
113119 impl <$t: PrintAttribute , $( $ts: PrintAttribute ) ,* > PrintAttribute for ( $t, $( $ts) ,* ) {
114- fn print_something ( & self ) -> bool {
120+ fn should_render ( & self ) -> bool {
115121 let ( $t, $( $ts) ,* ) = self ;
116- print_tup!( num_print_something $t $( $ts) * ) != 0
122+ print_tup!( num_should_render $t $( $ts) * ) != 0
117123 }
118124
119125 fn print_attribute( & self , p: & mut Printer ) {
120126 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 ;
122128 if parens {
123- p. word ( "(" ) ;
129+ p. popen ( ) ;
124130 }
125131
126- let mut printed_anything = $t. print_something ( ) ;
132+ let mut printed_anything = $t. should_render ( ) ;
127133
128134 $t. print_attribute( p) ;
129135
130136 $(
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+ }
133141 printed_anything = true ;
134142 }
135143 $ts. print_attribute( p) ;
136144 ) *
137145
138146 if parens {
139- p. word ( ")" ) ;
147+ p. pclose ( ) ;
140148 }
141149 }
142150 }
@@ -147,8 +155,8 @@ macro_rules! print_tup {
147155
148156print_tup ! ( A B C D E F G H ) ;
149157print_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 ) ;
152160
153161/// Finds attributes in sequences of attributes by pattern matching.
154162///
0 commit comments