Skip to content

Commit 11585b5

Browse files
committed
pprust: Use print_mac_common for delimited token groups
1 parent 23c5c1b commit 11585b5

15 files changed

+94
-78
lines changed

src/libsyntax/ast.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use crate::symbol::{Ident, Symbol as Name};
66
pub use crate::util::parser::ExprPrecedence;
77

88
use crate::ext::hygiene::{Mark, SyntaxContext};
9-
use crate::parse::token;
9+
use crate::parse::token::{self, DelimToken};
1010
use crate::print::pprust;
1111
use crate::ptr::P;
1212
use crate::source_map::{dummy_spanned, respan, Spanned};
@@ -1298,6 +1298,16 @@ impl Mac_ {
12981298
}
12991299
}
13001300

1301+
impl MacDelimiter {
1302+
crate fn to_token(self) -> DelimToken {
1303+
match self {
1304+
MacDelimiter::Parenthesis => DelimToken::Paren,
1305+
MacDelimiter::Bracket => DelimToken::Bracket,
1306+
MacDelimiter::Brace => DelimToken::Brace,
1307+
}
1308+
}
1309+
}
1310+
13011311
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
13021312
pub struct MacroDef {
13031313
pub tokens: TokenStream,

src/libsyntax/print/pprust.rs

+33-27
Original file line numberDiff line numberDiff line change
@@ -621,12 +621,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
621621
} else {
622622
match attr.tokens.trees().next() {
623623
Some(TokenTree::Delimited(_, delim, tts)) => {
624-
let delim = match delim {
625-
DelimToken::Brace => MacDelimiter::Brace,
626-
DelimToken::Bracket => MacDelimiter::Bracket,
627-
DelimToken::Paren | DelimToken::NoDelim => MacDelimiter::Parenthesis,
628-
};
629-
self.print_mac_common(&attr.path, false, None, tts, delim, attr.span);
624+
self.print_mac_common(
625+
Some(&attr.path), false, None, delim, tts, true, attr.span
626+
);
630627
}
631628
tree => {
632629
self.print_path(&attr.path, false, 0);
@@ -692,13 +689,11 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
692689
_ => {}
693690
}
694691
}
695-
TokenTree::Delimited(_, delim, tts) => {
696-
self.word(token_kind_to_string(&token::OpenDelim(delim)));
697-
self.space();
698-
self.print_tts(tts, convert_dollar_crate);
699-
self.space();
700-
self.word(token_kind_to_string(&token::CloseDelim(delim)))
701-
},
692+
TokenTree::Delimited(dspan, delim, tts) => {
693+
self.print_mac_common(
694+
None, false, None, delim, tts, convert_dollar_crate, dspan.entire()
695+
);
696+
}
702697
}
703698
}
704699

@@ -715,14 +710,17 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
715710

716711
fn print_mac_common(
717712
&mut self,
718-
path: &ast::Path,
713+
path: Option<&ast::Path>,
719714
has_bang: bool,
720715
ident: Option<ast::Ident>,
716+
delim: DelimToken,
721717
tts: TokenStream,
722-
delim: MacDelimiter,
718+
convert_dollar_crate: bool,
723719
span: Span,
724720
) {
725-
self.print_path(path, false, 0);
721+
if let Some(path) = path {
722+
self.print_path(path, false, 0);
723+
}
726724
if has_bang {
727725
self.word("!");
728726
}
@@ -732,18 +730,20 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
732730
self.space();
733731
}
734732
match delim {
735-
MacDelimiter::Parenthesis => self.popen(),
736-
MacDelimiter::Bracket => self.word("["),
737-
MacDelimiter::Brace => {
733+
DelimToken::Paren => self.popen(),
734+
DelimToken::Bracket => self.word("["),
735+
DelimToken::NoDelim => self.word(" "),
736+
DelimToken::Brace => {
738737
self.head("");
739738
self.bopen();
740739
}
741740
}
742-
self.print_tts(tts, true);
741+
self.print_tts(tts, convert_dollar_crate);
743742
match delim {
744-
MacDelimiter::Parenthesis => self.pclose(),
745-
MacDelimiter::Bracket => self.word("]"),
746-
MacDelimiter::Brace => self.bclose(span),
743+
DelimToken::Paren => self.pclose(),
744+
DelimToken::Bracket => self.word("]"),
745+
DelimToken::NoDelim => self.word(" "),
746+
DelimToken::Brace => self.bclose(span),
747747
}
748748
}
749749

@@ -1356,9 +1356,14 @@ impl<'a> State<'a> {
13561356
}
13571357
}
13581358
ast::ItemKind::MacroDef(ref macro_def) => {
1359-
let path = &ast::Path::from_ident(ast::Ident::with_empty_ctxt(sym::macro_rules));
13601359
self.print_mac_common(
1361-
path, true, Some(item.ident), macro_def.stream(), MacDelimiter::Brace, item.span
1360+
Some(&ast::Path::from_ident(ast::Ident::with_empty_ctxt(sym::macro_rules))),
1361+
true,
1362+
Some(item.ident),
1363+
DelimToken::Brace,
1364+
macro_def.stream(),
1365+
true,
1366+
item.span,
13621367
);
13631368
}
13641369
}
@@ -1747,10 +1752,11 @@ impl<'a> State<'a> {
17471752
}
17481753

17491754
crate fn print_mac(&mut self, m: &ast::Mac) {
1750-
self.print_mac_common(&m.node.path, true, None, m.node.stream(), m.node.delim, m.span);
1755+
self.print_mac_common(
1756+
Some(&m.node.path), true, None, m.node.delim.to_token(), m.node.stream(), true, m.span
1757+
);
17511758
}
17521759

1753-
17541760
fn print_call_post(&mut self, args: &[P<ast::Expr>]) {
17551761
self.popen();
17561762
self.commasep_exprs(Inconsistent, args);

src/test/pretty/cast-lt.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
// pretty-mode:expanded
99
// pp-exact:cast-lt.pp
1010

11-
macro_rules! negative {( $ e : expr ) => { $ e < 0 } }
11+
macro_rules! negative {($ e : expr) => {$ e < 0 } }
1212

1313
fn main() { (1 as i32) < 0; }

src/test/pretty/stmt_expr_attributes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fn _8() {
113113
fn _9() {
114114
macro_rules!
115115
stmt_mac
116-
{( ) => { let _ = ( ) ; } }
116+
{() => {let _ = () ; } }
117117

118118
#[rustc_dummy]
119119
stmt_mac!();
@@ -130,7 +130,7 @@ fn _9() {
130130
let _ = ();
131131
}
132132

133-
macro_rules! expr_mac {( ) => { ( ) } }
133+
macro_rules! expr_mac {() => {() } }
134134

135135
fn _10() {
136136
let _ = #[rustc_dummy] expr_mac!();

src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ LL | produces_async! {}
77
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
88
help: you can escape reserved keywords to use them as identifiers
99
|
10-
LL | ( ) => ( pub fn r#async ( ) { } )
11-
| ^^^^^^^
10+
LL | () => (pub fn r#async () { })
11+
| ^^^^^^^
1212

1313
error: aborting due to previous error
1414

src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ LL | r#async = consumes_async_raw!(async);
3131
| ^^^^^ no rules expected this token in macro call
3232

3333
error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||`
34-
--> <::edition_kw_macro_2015::passes_ident macros>:1:25
34+
--> <::edition_kw_macro_2015::passes_ident macros>:1:22
3535
|
36-
LL | ( $ i : ident ) => ( $ i )
37-
| ^ expected one of `move`, `|`, or `||` here
36+
LL | ($ i : ident) => ($ i)
37+
| ^ expected one of `move`, `|`, or `||` here
3838
|
3939
::: $DIR/edition-keywords-2018-2015-parsing.rs:16:8
4040
|

src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ LL | produces_async! {}
77
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
88
help: you can escape reserved keywords to use them as identifiers
99
|
10-
LL | ( ) => ( pub fn r#async ( ) { } )
11-
| ^^^^^^^
10+
LL | () => (pub fn r#async () { })
11+
| ^^^^^^^
1212

1313
error: aborting due to previous error
1414

src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ LL | r#async = consumes_async_raw!(async);
3131
| ^^^^^ no rules expected this token in macro call
3232

3333
error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||`
34-
--> <::edition_kw_macro_2018::passes_ident macros>:1:25
34+
--> <::edition_kw_macro_2018::passes_ident macros>:1:22
3535
|
36-
LL | ( $ i : ident ) => ( $ i )
37-
| ^ expected one of `move`, `|`, or `||` here
36+
LL | ($ i : ident) => ($ i)
37+
| ^ expected one of `move`, `|`, or `||` here
3838
|
3939
::: $DIR/edition-keywords-2018-2018-parsing.rs:16:8
4040
|

src/test/ui/macro_backtrace/main.stderr

+20-20
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ LL | ping!();
2424
|
2525
::: <::ping::ping macros>:1:1
2626
|
27-
LL | ( ) => { pong ! ( ) ; }
28-
| -------------------------
29-
| | |
30-
| | in this macro invocation
27+
LL | () => {pong ! () ; }
28+
| --------------------
29+
| | |
30+
| | in this macro invocation
3131
| in this expansion of `ping!`
3232

3333
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error`
@@ -44,34 +44,34 @@ LL | deep!();
4444
|
4545
::: <::ping::deep macros>:1:1
4646
|
47-
LL | ( ) => { foo ! ( ) ; }
48-
| ------------------------
49-
| | |
50-
| | in this macro invocation (#2)
47+
LL | () => {foo ! () ; }
48+
| -------------------
49+
| | |
50+
| | in this macro invocation (#2)
5151
| in this expansion of `deep!` (#1)
5252
|
5353
::: <::ping::foo macros>:1:1
5454
|
55-
LL | ( ) => { bar ! ( ) ; }
56-
| ------------------------
57-
| | |
58-
| | in this macro invocation (#3)
55+
LL | () => {bar ! () ; }
56+
| -------------------
57+
| | |
58+
| | in this macro invocation (#3)
5959
| in this expansion of `foo!` (#2)
6060
|
6161
::: <::ping::bar macros>:1:1
6262
|
63-
LL | ( ) => { ping ! ( ) ; }
64-
| -------------------------
65-
| | |
66-
| | in this macro invocation (#4)
63+
LL | () => {ping ! () ; }
64+
| --------------------
65+
| | |
66+
| | in this macro invocation (#4)
6767
| in this expansion of `bar!` (#3)
6868
|
6969
::: <::ping::ping macros>:1:1
7070
|
71-
LL | ( ) => { pong ! ( ) ; }
72-
| -------------------------
73-
| | |
74-
| | in this macro invocation (#5)
71+
LL | () => {pong ! () ; }
72+
| --------------------
73+
| | |
74+
| | in this macro invocation (#5)
7575
| in this expansion of `ping!` (#4)
7676

7777
error: aborting due to 3 previous errors

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 ( format_args_nl ! ( "Hello, World!" ) ) ; }`
8+
= note: to `{$crate :: io :: _print (format_args_nl ! ("Hello, World!")) ; }`
99

src/test/ui/macros/trace_faulty_macros.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | my_faulty_macro!();
1717
| ^^^^^^^^^^^^^^^^^^^
1818
|
1919
= note: expanding `my_faulty_macro! { }`
20-
= note: to `my_faulty_macro ! ( bcd ) ;`
20+
= note: to `my_faulty_macro ! (bcd) ;`
2121
= note: expanding `my_faulty_macro! { bcd }`
2222

2323
error: recursion limit reached while expanding the macro `my_recursive_macro`
@@ -38,13 +38,13 @@ LL | my_recursive_macro!();
3838
| ^^^^^^^^^^^^^^^^^^^^^^
3939
|
4040
= note: expanding `my_recursive_macro! { }`
41-
= note: to `my_recursive_macro ! ( ) ;`
41+
= note: to `my_recursive_macro ! () ;`
4242
= note: expanding `my_recursive_macro! { }`
43-
= note: to `my_recursive_macro ! ( ) ;`
43+
= note: to `my_recursive_macro ! () ;`
4444
= note: expanding `my_recursive_macro! { }`
45-
= note: to `my_recursive_macro ! ( ) ;`
45+
= note: to `my_recursive_macro ! () ;`
4646
= note: expanding `my_recursive_macro! { }`
47-
= note: to `my_recursive_macro ! ( ) ;`
47+
= note: to `my_recursive_macro ! () ;`
4848

4949
error: aborting due to 2 previous errors
5050

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",

0 commit comments

Comments
 (0)