Skip to content

Commit bae4682

Browse files
Rename print_something to should_render
1 parent 03eb454 commit bae4682

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
@@ -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.
3737
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+
3943
fn print_attribute(&self, p: &mut Printer);
4044
}
4145

4246
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)
4549
}
4650

4751
fn print_attribute(&self, p: &mut Printer) {
4852
T::print_attribute(self, p)
4953
}
5054
}
5155
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())
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
}
6166
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()
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
}
7884
macro_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 {
8793
macro_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 {
97103
macro_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,29 +112,29 @@ macro_rules! print_debug {
106112
}
107113

108114
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)* };
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 {
123129
p.word("(");
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() {
137+
if $ts.should_render() {
132138
p.word_space(",");
133139
printed_anything = true;
134140
}

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)