Skip to content

Commit 6a38322

Browse files
Rename print_something to should_render
1 parent 2b4694a commit 6a38322

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

compiler/rustc_attr_data_structures/src/lib.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,39 @@ pub trait HashStableContext: rustc_ast::HashStableContext + rustc_abi::HashStabl
3434
/// like [`Span`]s and empty tuples, are gracefully skipped so they don't clutter the
3535
/// representation much.
3636
pub trait PrintAttribute {
37-
fn print_something(&self) -> bool;
37+
/// Whether or not this will render as something meaningful, or if it's skipped
38+
/// (which will force the containing struct to also skip printing a comma
39+
/// and the field name).
40+
fn should_render(&self) -> bool;
41+
3842
fn print_attribute(&self, p: &mut Printer);
3943
}
4044

4145
impl<T: PrintAttribute> PrintAttribute for &T {
42-
fn print_something(&self) -> bool {
43-
T::print_something(self)
46+
fn should_render(&self) -> bool {
47+
T::should_render(self)
4448
}
4549

4650
fn print_attribute(&self, p: &mut Printer) {
4751
T::print_attribute(self, p)
4852
}
4953
}
5054
impl<T: PrintAttribute> PrintAttribute for Option<T> {
51-
fn print_something(&self) -> bool {
52-
self.as_ref().is_some_and(|x| x.print_something())
55+
fn should_render(&self) -> bool {
56+
self.as_ref().is_some_and(|x| x.should_render())
5357
}
58+
5459
fn print_attribute(&self, p: &mut Printer) {
5560
if let Some(i) = self {
5661
T::print_attribute(i, p)
5762
}
5863
}
5964
}
6065
impl<T: PrintAttribute> PrintAttribute for ThinVec<T> {
61-
fn print_something(&self) -> bool {
62-
self.is_empty() || self[0].print_something()
66+
fn should_render(&self) -> bool {
67+
self.is_empty() || self[0].should_render()
6368
}
69+
6470
fn print_attribute(&self, p: &mut Printer) {
6571
let mut last_printed = false;
6672
p.word("[");
@@ -69,15 +75,15 @@ impl<T: PrintAttribute> PrintAttribute for ThinVec<T> {
6975
p.word_space(",");
7076
}
7177
i.print_attribute(p);
72-
last_printed = i.print_something();
78+
last_printed = i.should_render();
7379
}
7480
p.word("]");
7581
}
7682
}
7783
macro_rules! print_skip {
7884
($($t: ty),* $(,)?) => {$(
7985
impl PrintAttribute for $t {
80-
fn print_something(&self) -> bool { false }
86+
fn should_render(&self) -> bool { false }
8187
fn print_attribute(&self, _: &mut Printer) { }
8288
})*
8389
};
@@ -86,7 +92,7 @@ macro_rules! print_skip {
8692
macro_rules! print_disp {
8793
($($t: ty),* $(,)?) => {$(
8894
impl PrintAttribute for $t {
89-
fn print_something(&self) -> bool { true }
95+
fn should_render(&self) -> bool { true }
9096
fn print_attribute(&self, p: &mut Printer) {
9197
p.word(format!("{}", self));
9298
}
@@ -96,7 +102,7 @@ macro_rules! print_disp {
96102
macro_rules! print_debug {
97103
($($t: ty),* $(,)?) => {$(
98104
impl PrintAttribute for $t {
99-
fn print_something(&self) -> bool { true }
105+
fn should_render(&self) -> bool { true }
100106
fn print_attribute(&self, p: &mut Printer) {
101107
p.word(format!("{:?}", self));
102108
}
@@ -105,29 +111,29 @@ macro_rules! print_debug {
105111
}
106112

107113
macro_rules! print_tup {
108-
(num_print_something $($ts: ident)*) => { 0 $(+ $ts.print_something() as usize)* };
114+
(num_should_render $($ts: ident)*) => { 0 $(+ $ts.should_render() as usize)* };
109115
() => {};
110116
($t: ident $($ts: ident)*) => {
111117
#[allow(non_snake_case, unused)]
112118
impl<$t: PrintAttribute, $($ts: PrintAttribute),*> PrintAttribute for ($t, $($ts),*) {
113-
fn print_something(&self) -> bool {
119+
fn should_render(&self) -> bool {
114120
let ($t, $($ts),*) = self;
115-
print_tup!(num_print_something $t $($ts)*) != 0
121+
print_tup!(num_should_render $t $($ts)*) != 0
116122
}
117123

118124
fn print_attribute(&self, p: &mut Printer) {
119125
let ($t, $($ts),*) = self;
120-
let parens = print_tup!(num_print_something $t $($ts)*) > 1;
126+
let parens = print_tup!(num_should_render $t $($ts)*) > 1;
121127
if parens {
122128
p.word("(");
123129
}
124130

125-
let mut printed_anything = $t.print_something();
131+
let mut printed_anything = $t.should_render();
126132

127133
$t.print_attribute(p);
128134

129135
$(
130-
if printed_anything && $ts.print_something() {
136+
if $ts.should_render() {
131137
p.word_space(",");
132138
printed_anything = true;
133139
}

compiler/rustc_macros/src/print_attribute.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, Tok
1616
let name = field.ident.as_ref().unwrap();
1717
let string_name = name.to_string();
1818
disps.push(quote! {
19-
if __printed_anything && #name.print_something() {
19+
if __printed_anything && #name.should_render() {
2020
__p.word_space(",");
2121
__printed_anything = true;
2222
}
@@ -31,7 +31,7 @@ fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, Tok
3131
quote! { {#(#field_names),*} },
3232
quote! {
3333
__p.word(#string_name);
34-
if true #(&& !#field_names.print_something())* {
34+
if true #(&& !#field_names.should_render())* {
3535
return;
3636
}
3737

@@ -48,7 +48,7 @@ fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, Tok
4848
for idx in 0..fields_unnamed.unnamed.len() {
4949
let name = format_ident!("f{idx}");
5050
disps.push(quote! {
51-
if __printed_anything && #name.print_something() {
51+
if __printed_anything && #name.should_render() {
5252
__p.word_space(",");
5353
__printed_anything = true;
5454
}
@@ -62,7 +62,7 @@ fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, Tok
6262
quote! {
6363
__p.word(#string_name);
6464

65-
if true #(&& !#field_names.print_something())* {
65+
if true #(&& !#field_names.should_render())* {
6666
return;
6767
}
6868

@@ -138,7 +138,7 @@ pub(crate) fn print_attribute(input: Structure<'_>) -> TokenStream {
138138
input.gen_impl(quote! {
139139
#[allow(unused)]
140140
gen impl PrintAttribute for @Self {
141-
fn print_something(&self) -> bool { #printed }
141+
fn should_render(&self) -> bool { #printed }
142142
fn print_attribute(&self, __p: &mut rustc_ast_pretty::pp::Printer) { #code }
143143
}
144144
})

0 commit comments

Comments
 (0)