Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix repr pretty display #70075

Merged
merged 3 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/librustc_ast_pretty/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,19 @@ pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {

// This makes comma-separated lists look slightly nicer,
// and also addresses a specific regression described in issue #63896.
fn tt_prepend_space(tt: &TokenTree) -> bool {
fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool {
match tt {
TokenTree::Token(token) => match token.kind {
token::Comma => false,
_ => true,
},
TokenTree::Delimited(_, DelimToken::Paren, _) => match prev {
TokenTree::Token(token) => match token.kind {
token::Ident(_, _) => false,
_ => true,
},
_ => true,
},
_ => true,
}
}
Expand Down Expand Up @@ -650,11 +657,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
}

fn print_tts(&mut self, tts: tokenstream::TokenStream, convert_dollar_crate: bool) {
for (i, tt) in tts.into_trees().enumerate() {
if i != 0 && tt_prepend_space(&tt) {
let mut iter = tts.into_trees().peekable();
while let Some(tt) = iter.next() {
let show_space =
if let Some(next) = iter.peek() { tt_prepend_space(next, &tt) } else { false };
self.print_tt(tt, convert_dollar_crate);
if show_space {
self.space();
}
self.print_tt(tt, convert_dollar_crate);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3151,7 +3151,6 @@ fn render_attributes(w: &mut Buffer, it: &clean::Item, top: bool) {
continue;
}

// FIXME: this currently renders too many spaces as in: `#[repr(C, align (8))]`.
attrs.push_str(&pprust::attribute_to_string(&attr));
}
if !attrs.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/pretty/attr-literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![feature(rustc_attrs)]

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

Expand Down
7 changes: 3 additions & 4 deletions src/test/pretty/delimited-token-groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ macro_rules! mac { ($ ($ tt : tt) *) => () }
mac! {
struct S { field1 : u8, field2 : u16, } impl Clone for S
{
fn clone () -> S
fn clone() -> S
{
panic ! () ;

Expand All @@ -16,9 +16,8 @@ mac! {
}

mac! {
a
(aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
aaaaaaaa aaaaaaaa) a
a(aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
aaaaaaaa aaaaaaaa) a
[aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa
aaaaaaaa aaaaaaaa] a
{
Expand Down
2 changes: 1 addition & 1 deletion src/test/pretty/issue-68710-field-attr-proc-mac-lost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ const C: C =
#[cfg(debug_assertions)]
field: 0,

#[cfg(not (debug_assertions))]
#[cfg(not(debug_assertions))]
field: 1,};
2 changes: 1 addition & 1 deletion src/test/rustdoc/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ pub enum Foo {
Bar,
}

// @has foo/struct.Repr.html '//*[@class="docblock attributes top-attr"]' '#[repr(C, align (8))]'
// @has foo/struct.Repr.html '//*[@class="docblock attributes top-attr"]' '#[repr(C, align(8))]'
#[repr(C, align(8))]
pub struct Repr;
2 changes: 1 addition & 1 deletion src/test/ui/macros/trace-macro.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ LL | println!("Hello, World!");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: expanding `println! { "Hello, World!" }`
= note: to `{ $crate :: io :: _print ($crate :: format_args_nl ! ("Hello, World!")) ; }`
= note: to `{ $crate :: io :: _print($crate :: format_args_nl ! ("Hello, World!")) ; }`

2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/attribute-spans-preserved.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fn main () { let y : u32 = "z" ; { let x : u32 = "y" ; } }
fn main() { let y : u32 = "z" ; { let x : u32 = "y" ; } }
4 changes: 2 additions & 2 deletions src/test/ui/proc-macro/dollar-crate-issue-57089.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PRINT-BANG INPUT (DISPLAY): struct M ($crate :: S) ;
PRINT-BANG INPUT (DISPLAY): struct M($crate :: S) ;
PRINT-BANG INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -39,7 +39,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): struct A(crate::S);
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ($crate :: S) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A($crate :: S) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/proc-macro/dollar-crate-issue-62325.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PRINT-ATTR INPUT (DISPLAY): struct A(identity!(crate :: S));
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A (identity ! ($crate :: S)) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A(identity ! ($crate :: S)) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -55,7 +55,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): struct B(identity!(::dollar_crate_external :: S));
PRINT-ATTR RE-COLLECTED (DISPLAY): struct B (identity ! ($crate :: S)) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): struct B(identity ! ($crate :: S)) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/proc-macro/dollar-crate.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PRINT-BANG INPUT (DISPLAY): struct M ($crate :: S) ;
PRINT-BANG INPUT (DISPLAY): struct M($crate :: S) ;
PRINT-BANG INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -39,7 +39,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): struct A(crate::S);
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ($crate :: S) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A($crate :: S) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -80,7 +80,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-DERIVE INPUT (DISPLAY): struct D(crate::S);
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ($crate :: S) ;
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D($crate :: S) ;
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -120,7 +120,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
span: #3 bytes(LO..HI),
},
]
PRINT-BANG INPUT (DISPLAY): struct M ($crate :: S) ;
PRINT-BANG INPUT (DISPLAY): struct M($crate :: S) ;
PRINT-BANG INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -161,7 +161,7 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): struct A(::dollar_crate_external::S);
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ($crate :: S) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A($crate :: S) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -202,7 +202,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-DERIVE INPUT (DISPLAY): struct D(::dollar_crate_external::S);
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D ($crate :: S) ;
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D($crate :: S) ;
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down