Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 914ad9e

Browse files
committedMar 5, 2025·
Fix pretty printing of parsed attrs in hir_pretty
1 parent dd4d089 commit 914ad9e

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed
 

‎compiler/rustc_attr_data_structures/src/lib.rs

+4-4
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();
@@ -142,7 +142,7 @@ macro_rules! print_tup {
142142
)*
143143

144144
if parens {
145-
p.word(")");
145+
p.pclose();
146146
}
147147
}
148148
}
@@ -153,5 +153,5 @@ macro_rules! print_tup {
153153

154154
print_tup!(A B C D E F G H);
155155
print_skip!(Span, ());
156-
print_disp!(Symbol, u16, bool, NonZero<u32>);
157-
print_debug!(UintTy, IntTy, Align, AttrStyle, CommentKind, Transparency);
156+
print_disp!(u16, bool, NonZero<u32>);
157+
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-10
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@ extern crate std;
55
//@ compile-flags: -Zunpretty=hir
66
//@ check-pass
77

8-
#[attr="Deprecation{deprecation: Deprecation{since: Unspecifiednote:
9-
suggestion: }span: }")]
8+
#[attr = Deprecation {deprecation: Deprecation {since: Unspecified}}]
109
struct PlainDeprecated;
1110

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

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

20-
#[attr="Deprecation{deprecation: Deprecation{since: NonStandard(1.2.3)note:
21-
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"}}]
2221
struct SinceAndNote;
2322

24-
#[attr="Deprecation{deprecation: Deprecation{since: NonStandard(1.2.3)note:
25-
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"}}]
2625
struct FlippedOrder;

0 commit comments

Comments
 (0)
Please sign in to comment.