Skip to content

Commit 9ae9453

Browse files
Fix pretty printing of parsed attrs in hir_pretty
1 parent bae4682 commit 9ae9453

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

compiler/rustc_attr_data_structures/src/lib.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ macro_rules! print_tup {
126126
let ($t, $($ts),*) = self;
127127
let parens = print_tup!(num_should_render $t $($ts)*) > 1;
128128
if parens {
129-
p.word("(");
129+
p.popen();
130130
}
131131

132132
let mut printed_anything = $t.should_render();
@@ -135,14 +135,16 @@ macro_rules! print_tup {
135135

136136
$(
137137
if $ts.should_render() {
138-
p.word_space(",");
138+
if printed_anything {
139+
p.word_space(",");
140+
}
139141
printed_anything = true;
140142
}
141143
$ts.print_attribute(p);
142144
)*
143145

144146
if parens {
145-
p.word(")");
147+
p.pclose();
146148
}
147149
}
148150
}
@@ -153,5 +155,5 @@ macro_rules! print_tup {
153155

154156
print_tup!(A B C D E F G H);
155157
print_skip!(Span, ());
156-
print_disp!(Symbol, u16, bool, NonZero<u32>);
157-
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);

compiler/rustc_hir_pretty/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ impl<'a> State<'a> {
118118
self.hardbreak()
119119
}
120120
hir::Attribute::Parsed(pa) => {
121-
self.word("#[attr=\"");
121+
self.word("#[attr = ");
122122
pa.print_attribute(self);
123-
self.word("\")]");
123+
self.word("]");
124124
self.hardbreak()
125125
}
126126
}

compiler/rustc_macros/src/print_attribute.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ 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.should_render() {
20-
__p.word_space(",");
19+
if #name.should_render() {
20+
if __printed_anything {
21+
__p.word_space(",");
22+
}
23+
__p.word(#string_name);
24+
__p.word_space(":");
2125
__printed_anything = true;
2226
}
23-
__p.word(#string_name);
24-
__p.word_space(":");
2527
#name.print_attribute(__p);
2628
});
2729
field_names.push(name);
@@ -35,6 +37,7 @@ fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, Tok
3537
return;
3638
}
3739

40+
__p.nbsp();
3841
__p.word("{");
3942
#(#disps)*
4043
__p.word("}");
@@ -48,8 +51,10 @@ fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, Tok
4851
for idx in 0..fields_unnamed.unnamed.len() {
4952
let name = format_ident!("f{idx}");
5053
disps.push(quote! {
51-
if __printed_anything && #name.should_render() {
52-
__p.word_space(",");
54+
if #name.should_render() {
55+
if __printed_anything {
56+
__p.word_space(",");
57+
}
5358
__printed_anything = true;
5459
}
5560
#name.print_attribute(__p);
@@ -66,9 +71,9 @@ fn print_fields(name: &Ident, fields: &Fields) -> (TokenStream, TokenStream, Tok
6671
return;
6772
}
6873

69-
__p.word("(");
74+
__p.popen();
7075
#(#disps)*
71-
__p.word(")");
76+
__p.pclose();
7277
},
7378
quote! { true },
7479
)

tests/ui/unpretty/deprecated-attr.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//@ compile-flags: -Zunpretty=hir
22
//@ check-pass
33

4-
// FIXME(jdonszelmann): the pretty printing output for deprecated (and possibly more attrs) is
5-
// slightly broken.
64
#[deprecated]
75
pub struct PlainDeprecated;
86

tests/ui/unpretty/deprecated-attr.stdout

+9-12
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,21 @@ extern crate std;
55
//@ compile-flags: -Zunpretty=hir
66
//@ check-pass
77

8-
// FIXME(jdonszelmann): the pretty printing output for deprecated (and possibly more attrs) is
9-
// slightly broken.
10-
#[attr="Deprecation{deprecation: Deprecation{since: Unspecifiednote:
11-
suggestion: }span: }")]
8+
#[attr = Deprecation {deprecation: Deprecation {since: Unspecified}}]
129
struct PlainDeprecated;
1310

14-
#[attr="Deprecation{deprecation: Deprecation{since: Unspecifiednote:
15-
here's why this is deprecatedsuggestion: }span: }")]
11+
#[attr = Deprecation {deprecation: Deprecation {since: Unspecified, note:
12+
"here's why this is deprecated"}}]
1613
struct DirectNote;
1714

18-
#[attr="Deprecation{deprecation: Deprecation{since: Unspecifiednote:
19-
here's why this is deprecatedsuggestion: }span: }")]
15+
#[attr = Deprecation {deprecation: Deprecation {since: Unspecified, note:
16+
"here's why this is deprecated"}}]
2017
struct ExplicitNote;
2118

22-
#[attr="Deprecation{deprecation: Deprecation{since: NonStandard(1.2.3)note:
23-
here's why this is deprecatedsuggestion: }span: }")]
19+
#[attr = Deprecation {deprecation: Deprecation {since: NonStandard("1.2.3"),
20+
note: "here's why this is deprecated"}}]
2421
struct SinceAndNote;
2522

26-
#[attr="Deprecation{deprecation: Deprecation{since: NonStandard(1.2.3)note:
27-
here's why this is deprecatedsuggestion: }span: }")]
23+
#[attr = Deprecation {deprecation: Deprecation {since: NonStandard("1.2.3"),
24+
note: "here's why this is deprecated"}}]
2825
struct FlippedOrder;

0 commit comments

Comments
 (0)