Skip to content

Commit 60a2d06

Browse files
authored
Rollup merge of #70075 - GuillaumeGomez:fix-repr-display, r=petrochenkov
Fix repr pretty display Fixes #70027. r? @varkor
2 parents 292c538 + 9a017da commit 60a2d06

11 files changed

+32
-24
lines changed

src/librustc_ast_pretty/pprust.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,19 @@ pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
148148

149149
// This makes comma-separated lists look slightly nicer,
150150
// and also addresses a specific regression described in issue #63896.
151-
fn tt_prepend_space(tt: &TokenTree) -> bool {
151+
fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool {
152152
match tt {
153153
TokenTree::Token(token) => match token.kind {
154154
token::Comma => false,
155155
_ => true,
156156
},
157+
TokenTree::Delimited(_, DelimToken::Paren, _) => match prev {
158+
TokenTree::Token(token) => match token.kind {
159+
token::Ident(_, _) => false,
160+
_ => true,
161+
},
162+
_ => true,
163+
},
157164
_ => true,
158165
}
159166
}
@@ -650,11 +657,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
650657
}
651658

652659
fn print_tts(&mut self, tts: tokenstream::TokenStream, convert_dollar_crate: bool) {
653-
for (i, tt) in tts.into_trees().enumerate() {
654-
if i != 0 && tt_prepend_space(&tt) {
660+
let mut iter = tts.into_trees().peekable();
661+
while let Some(tt) = iter.next() {
662+
let show_space =
663+
if let Some(next) = iter.peek() { tt_prepend_space(next, &tt) } else { false };
664+
self.print_tt(tt, convert_dollar_crate);
665+
if show_space {
655666
self.space();
656667
}
657-
self.print_tt(tt, convert_dollar_crate);
658668
}
659669
}
660670

src/librustdoc/html/render.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3151,7 +3151,6 @@ fn render_attributes(w: &mut Buffer, it: &clean::Item, top: bool) {
31513151
continue;
31523152
}
31533153

3154-
// FIXME: this currently renders too many spaces as in: `#[repr(C, align (8))]`.
31553154
attrs.push_str(&pprust::attribute_to_string(&attr));
31563155
}
31573156
if !attrs.is_empty() {

src/test/pretty/attr-literals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![feature(rustc_attrs)]
66

77
fn main() {
8-
#![rustc_dummy("hi", 1, 2, 1.012, pi = 3.14, bye, name ("John"))]
8+
#![rustc_dummy("hi", 1, 2, 1.012, pi = 3.14, bye, name("John"))]
99
#[rustc_dummy = 8]
1010
fn f() { }
1111

src/test/pretty/delimited-token-groups.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ macro_rules! mac { ($ ($ tt : tt) *) => () }
77
mac! {
88
struct S { field1 : u8, field2 : u16, } impl Clone for S
99
{
10-
fn clone () -> S
10+
fn clone() -> S
1111
{
1212
panic ! () ;
1313

@@ -16,9 +16,8 @@ mac! {
1616
}
1717

1818
mac! {
19-
a
20-
(aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
21-
aaaaaaaa aaaaaaaa) a
19+
a(aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
20+
aaaaaaaa aaaaaaaa) a
2221
[aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
2322
aaaaaaaa aaaaaaaa] a
2423
{

src/test/pretty/issue-68710-field-attr-proc-mac-lost.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ const C: C =
1212
#[cfg(debug_assertions)]
1313
field: 0,
1414

15-
#[cfg(not (debug_assertions))]
15+
#[cfg(not(debug_assertions))]
1616
field: 1,};

src/test/rustdoc/attributes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ pub enum Foo {
1616
Bar,
1717
}
1818

19-
// @has foo/struct.Repr.html '//*[@class="docblock attributes top-attr"]' '#[repr(C, align (8))]'
19+
// @has foo/struct.Repr.html '//*[@class="docblock attributes top-attr"]' '#[repr(C, align(8))]'
2020
#[repr(C, align(8))]
2121
pub struct Repr;

src/test/ui/macros/trace-macro.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ LL | println!("Hello, World!");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: expanding `println! { "Hello, World!" }`
8-
= note: to `{ $crate :: io :: _print ($crate :: format_args_nl ! ("Hello, World!")) ; }`
8+
= note: to `{ $crate :: io :: _print($crate :: format_args_nl ! ("Hello, World!")) ; }`
99

Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fn main () { let y : u32 = "z" ; { let x : u32 = "y" ; } }
1+
fn main() { let y : u32 = "z" ; { let x : u32 = "y" ; } }

src/test/ui/proc-macro/dollar-crate-issue-57089.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-BANG INPUT (DISPLAY): struct M ($crate :: S) ;
1+
PRINT-BANG INPUT (DISPLAY): struct M($crate :: S) ;
22
PRINT-BANG INPUT (DEBUG): TokenStream [
33
Ident {
44
ident: "struct",
@@ -39,7 +39,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
3939
},
4040
]
4141
PRINT-ATTR INPUT (DISPLAY): struct A(crate::S);
42-
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ($crate :: S) ;
42+
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A($crate :: S) ;
4343
PRINT-ATTR INPUT (DEBUG): TokenStream [
4444
Ident {
4545
ident: "struct",

src/test/ui/proc-macro/dollar-crate-issue-62325.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PRINT-ATTR INPUT (DISPLAY): struct A(identity!(crate :: S));
2-
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A (identity ! ($crate :: S)) ;
2+
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A(identity ! ($crate :: S)) ;
33
PRINT-ATTR INPUT (DEBUG): TokenStream [
44
Ident {
55
ident: "struct",
@@ -55,7 +55,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
5555
},
5656
]
5757
PRINT-ATTR INPUT (DISPLAY): struct B(identity!(::dollar_crate_external :: S));
58-
PRINT-ATTR RE-COLLECTED (DISPLAY): struct B (identity ! ($crate :: S)) ;
58+
PRINT-ATTR RE-COLLECTED (DISPLAY): struct B(identity ! ($crate :: S)) ;
5959
PRINT-ATTR INPUT (DEBUG): TokenStream [
6060
Ident {
6161
ident: "struct",

src/test/ui/proc-macro/dollar-crate.stdout

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-BANG INPUT (DISPLAY): struct M ($crate :: S) ;
1+
PRINT-BANG INPUT (DISPLAY): struct M($crate :: S) ;
22
PRINT-BANG INPUT (DEBUG): TokenStream [
33
Ident {
44
ident: "struct",
@@ -39,7 +39,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
3939
},
4040
]
4141
PRINT-ATTR INPUT (DISPLAY): struct A(crate::S);
42-
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ($crate :: S) ;
42+
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A($crate :: S) ;
4343
PRINT-ATTR INPUT (DEBUG): TokenStream [
4444
Ident {
4545
ident: "struct",
@@ -80,7 +80,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
8080
},
8181
]
8282
PRINT-DERIVE INPUT (DISPLAY): struct D(crate::S);
83-
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ($crate :: S) ;
83+
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D($crate :: S) ;
8484
PRINT-DERIVE INPUT (DEBUG): TokenStream [
8585
Ident {
8686
ident: "struct",
@@ -120,7 +120,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
120120
span: #3 bytes(LO..HI),
121121
},
122122
]
123-
PRINT-BANG INPUT (DISPLAY): struct M ($crate :: S) ;
123+
PRINT-BANG INPUT (DISPLAY): struct M($crate :: S) ;
124124
PRINT-BANG INPUT (DEBUG): TokenStream [
125125
Ident {
126126
ident: "struct",
@@ -161,7 +161,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
161161
},
162162
]
163163
PRINT-ATTR INPUT (DISPLAY): struct A(::dollar_crate_external::S);
164-
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ($crate :: S) ;
164+
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A($crate :: S) ;
165165
PRINT-ATTR INPUT (DEBUG): TokenStream [
166166
Ident {
167167
ident: "struct",
@@ -202,7 +202,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
202202
},
203203
]
204204
PRINT-DERIVE INPUT (DISPLAY): struct D(::dollar_crate_external::S);
205-
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ($crate :: S) ;
205+
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D($crate :: S) ;
206206
PRINT-DERIVE INPUT (DEBUG): TokenStream [
207207
Ident {
208208
ident: "struct",

0 commit comments

Comments
 (0)