]> git.lizzy.rs Git - rust.git/commitdiff
pprust: Use `print_mac_common` for delimited token groups
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sat, 13 Jul 2019 20:08:29 +0000 (23:08 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Mon, 15 Jul 2019 09:42:07 +0000 (12:42 +0300)
15 files changed:
src/libsyntax/ast.rs
src/libsyntax/print/pprust.rs
src/test/pretty/cast-lt.pp
src/test/pretty/stmt_expr_attributes.rs
src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr
src/test/ui/editions/edition-keywords-2018-2015-parsing.stderr
src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr
src/test/ui/editions/edition-keywords-2018-2018-parsing.stderr
src/test/ui/macro_backtrace/main.stderr
src/test/ui/macros/trace-macro.stderr
src/test/ui/macros/trace_faulty_macros.stderr
src/test/ui/proc-macro/attribute-spans-preserved.stdout
src/test/ui/proc-macro/dollar-crate-issue-57089.stdout
src/test/ui/proc-macro/dollar-crate-issue-62325.stdout
src/test/ui/proc-macro/dollar-crate.stdout

index 8801e89a0cfc43cc6216d46cf72c4bda2c5b6403..6cfc1b77e03feeb3505f475f63d1b11449ddc819 100644 (file)
@@ -6,7 +6,7 @@
 pub use crate::util::parser::ExprPrecedence;
 
 use crate::ext::hygiene::{Mark, SyntaxContext};
-use crate::parse::token;
+use crate::parse::token::{self, DelimToken};
 use crate::print::pprust;
 use crate::ptr::P;
 use crate::source_map::{dummy_spanned, respan, Spanned};
@@ -1298,6 +1298,16 @@ pub fn stream(&self) -> TokenStream {
     }
 }
 
+impl MacDelimiter {
+    crate fn to_token(self) -> DelimToken {
+        match self {
+            MacDelimiter::Parenthesis => DelimToken::Paren,
+            MacDelimiter::Bracket => DelimToken::Bracket,
+            MacDelimiter::Brace => DelimToken::Brace,
+        }
+    }
+}
+
 #[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
 pub struct MacroDef {
     pub tokens: TokenStream,
index af9f6bb442dd17ca40b8a757d86c0c0d015bee33..77f2dff0d8eba16b919478d5a345ecc8fcb7916f 100644 (file)
@@ -621,12 +621,9 @@ fn print_attribute_inline(&mut self, attr: &ast::Attribute,
             } else {
                 match attr.tokens.trees().next() {
                     Some(TokenTree::Delimited(_, delim, tts)) => {
-                        let delim = match delim {
-                            DelimToken::Brace => MacDelimiter::Brace,
-                            DelimToken::Bracket => MacDelimiter::Bracket,
-                            DelimToken::Paren | DelimToken::NoDelim => MacDelimiter::Parenthesis,
-                        };
-                        self.print_mac_common(&attr.path, false, None, tts, delim, attr.span);
+                        self.print_mac_common(
+                            Some(&attr.path), false, None, delim, tts, true, attr.span
+                        );
                     }
                     tree => {
                         self.print_path(&attr.path, false, 0);
@@ -692,13 +689,11 @@ fn print_tt(&mut self, tt: tokenstream::TokenTree, convert_dollar_crate: bool) {
                     _ => {}
                 }
             }
-            TokenTree::Delimited(_, delim, tts) => {
-                self.word(token_kind_to_string(&token::OpenDelim(delim)));
-                self.space();
-                self.print_tts(tts, convert_dollar_crate);
-                self.space();
-                self.word(token_kind_to_string(&token::CloseDelim(delim)))
-            },
+            TokenTree::Delimited(dspan, delim, tts) => {
+                self.print_mac_common(
+                    None, false, None, delim, tts, convert_dollar_crate, dspan.entire()
+                );
+            }
         }
     }
 
@@ -715,14 +710,17 @@ fn print_tts(&mut self, tts: tokenstream::TokenStream, convert_dollar_crate: boo
 
     fn print_mac_common(
         &mut self,
-        path: &ast::Path,
+        path: Option<&ast::Path>,
         has_bang: bool,
         ident: Option<ast::Ident>,
+        delim: DelimToken,
         tts: TokenStream,
-        delim: MacDelimiter,
+        convert_dollar_crate: bool,
         span: Span,
     ) {
-        self.print_path(path, false, 0);
+        if let Some(path) = path {
+            self.print_path(path, false, 0);
+        }
         if has_bang {
             self.word("!");
         }
@@ -732,18 +730,20 @@ fn print_mac_common(
             self.space();
         }
         match delim {
-            MacDelimiter::Parenthesis => self.popen(),
-            MacDelimiter::Bracket => self.word("["),
-            MacDelimiter::Brace => {
+            DelimToken::Paren => self.popen(),
+            DelimToken::Bracket => self.word("["),
+            DelimToken::NoDelim => self.word(" "),
+            DelimToken::Brace => {
                 self.head("");
                 self.bopen();
             }
         }
-        self.print_tts(tts, true);
+        self.print_tts(tts, convert_dollar_crate);
         match delim {
-            MacDelimiter::Parenthesis => self.pclose(),
-            MacDelimiter::Bracket => self.word("]"),
-            MacDelimiter::Brace => self.bclose(span),
+            DelimToken::Paren => self.pclose(),
+            DelimToken::Bracket => self.word("]"),
+            DelimToken::NoDelim => self.word(" "),
+            DelimToken::Brace => self.bclose(span),
         }
     }
 
@@ -1356,9 +1356,14 @@ fn print_associated_type(&mut self,
                 }
             }
             ast::ItemKind::MacroDef(ref macro_def) => {
-                let path = &ast::Path::from_ident(ast::Ident::with_empty_ctxt(sym::macro_rules));
                 self.print_mac_common(
-                    path, true, Some(item.ident), macro_def.stream(), MacDelimiter::Brace, item.span
+                    Some(&ast::Path::from_ident(ast::Ident::with_empty_ctxt(sym::macro_rules))),
+                    true,
+                    Some(item.ident),
+                    DelimToken::Brace,
+                    macro_def.stream(),
+                    true,
+                    item.span,
                 );
             }
         }
@@ -1747,10 +1752,11 @@ fn print_else(&mut self, els: Option<&ast::Expr>) {
     }
 
     crate fn print_mac(&mut self, m: &ast::Mac) {
-        self.print_mac_common(&m.node.path, true, None, m.node.stream(), m.node.delim, m.span);
+        self.print_mac_common(
+            Some(&m.node.path), true, None, m.node.delim.to_token(), m.node.stream(), true, m.span
+        );
     }
 
-
     fn print_call_post(&mut self, args: &[P<ast::Expr>]) {
         self.popen();
         self.commasep_exprs(Inconsistent, args);
index c9fd78163603312b0b915ef8295ed7fe7554d55b..1ae30983d202abba383bfa7c2db74fe6870707f5 100644 (file)
@@ -8,6 +8,6 @@ extern crate std;
 // pretty-mode:expanded
 // pp-exact:cast-lt.pp
 
-macro_rules! negative {( $ e : expr ) => { $ e < 0 } }
+macro_rules! negative {($ e : expr) => {$ e < 0 } }
 
 fn main() { (1 as i32) < 0; }
index 0d438d457bf305e60093d861350f09988fd83290..acc11f3f84abac69fdaa86e387e3c3a4f0df5b76 100644 (file)
@@ -113,7 +113,7 @@ fn _8() {
 fn _9() {
     macro_rules!
     stmt_mac
-    {(  ) => { let _ = (  ) ; } }
+    {() => {let _ = () ; } }
 
     #[rustc_dummy]
     stmt_mac!();
@@ -130,7 +130,7 @@ fn _9() {
     let _ = ();
 }
 
-macro_rules! expr_mac {(  ) => { (  ) } }
+macro_rules! expr_mac {() => {() } }
 
 fn _10() {
     let _ = #[rustc_dummy] expr_mac!();
index 9724f78db6618f32bd11be91969e28cd4d64022a..321545740cf486f430d6b51584bebc9f6caa6b06 100644 (file)
@@ -7,8 +7,8 @@ LL |     produces_async! {}
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
 help: you can escape reserved keywords to use them as identifiers
    |
-LL | (  ) => ( pub fn r#async (  ) {  } )
-   |                  ^^^^^^^
+LL | () => (pub fn r#async () { })
+   |               ^^^^^^^
 
 error: aborting due to previous error
 
index 0d8850c2397c6aae6be97eb87ca300c1f27204af..3c4a153353447f2b2778f1959941e5f32b680003 100644 (file)
@@ -31,10 +31,10 @@ LL |     r#async = consumes_async_raw!(async);
    |                                   ^^^^^ no rules expected this token in macro call
 
 error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||`
-  --> <::edition_kw_macro_2015::passes_ident macros>:1:25
+  --> <::edition_kw_macro_2015::passes_ident macros>:1:22
    |
-LL | ( $ i : ident ) => ( $ i )
-   |                         ^ expected one of `move`, `|`, or `||` here
+LL | ($ i : ident) => ($ i)
+   |                      ^ expected one of `move`, `|`, or `||` here
    | 
   ::: $DIR/edition-keywords-2018-2015-parsing.rs:16:8
    |
index ab601c8d8a707ca514f331b6a015bb066f8d5759..8942e3ce430a8e71edab78f36f89efa5bfbd5cdc 100644 (file)
@@ -7,8 +7,8 @@ LL |     produces_async! {}
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
 help: you can escape reserved keywords to use them as identifiers
    |
-LL | (  ) => ( pub fn r#async (  ) {  } )
-   |                  ^^^^^^^
+LL | () => (pub fn r#async () { })
+   |               ^^^^^^^
 
 error: aborting due to previous error
 
index 0604b600d23d0095ddfa79e5b644623528beaa39..46aa9ca34e17c05c626c4913c49af776e4b8e0c7 100644 (file)
@@ -31,10 +31,10 @@ LL |     r#async = consumes_async_raw!(async);
    |                                   ^^^^^ no rules expected this token in macro call
 
 error: macro expansion ends with an incomplete expression: expected one of `move`, `|`, or `||`
-  --> <::edition_kw_macro_2018::passes_ident macros>:1:25
+  --> <::edition_kw_macro_2018::passes_ident macros>:1:22
    |
-LL | ( $ i : ident ) => ( $ i )
-   |                         ^ expected one of `move`, `|`, or `||` here
+LL | ($ i : ident) => ($ i)
+   |                      ^ expected one of `move`, `|`, or `||` here
    | 
   ::: $DIR/edition-keywords-2018-2018-parsing.rs:16:8
    |
index 239b53f2338c41e289f3f3829d1cfd776edf5462..6f82d4040bce5ec5eccdda4a67b8bbf38ff47b34 100644 (file)
@@ -24,10 +24,10 @@ LL |       ping!();
    | 
   ::: <::ping::ping macros>:1:1
    |
-LL |   (  ) => { pong ! (  ) ; }
-   |   -------------------------
-   |   |         |
-   |   |         in this macro invocation
+LL |   () => {pong ! () ; }
+   |   --------------------
+   |   |      |
+   |   |      in this macro invocation
    |   in this expansion of `ping!`
 
 error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `error`
@@ -44,34 +44,34 @@ LL |       deep!();
    | 
   ::: <::ping::deep macros>:1:1
    |
-LL |   (  ) => { foo ! (  ) ; }
-   |   ------------------------
-   |   |         |
-   |   |         in this macro invocation (#2)
+LL |   () => {foo ! () ; }
+   |   -------------------
+   |   |      |
+   |   |      in this macro invocation (#2)
    |   in this expansion of `deep!` (#1)
    | 
   ::: <::ping::foo macros>:1:1
    |
-LL |   (  ) => { bar ! (  ) ; }
-   |   ------------------------
-   |   |         |
-   |   |         in this macro invocation (#3)
+LL |   () => {bar ! () ; }
+   |   -------------------
+   |   |      |
+   |   |      in this macro invocation (#3)
    |   in this expansion of `foo!` (#2)
    | 
   ::: <::ping::bar macros>:1:1
    |
-LL |   (  ) => { ping ! (  ) ; }
-   |   -------------------------
-   |   |         |
-   |   |         in this macro invocation (#4)
+LL |   () => {ping ! () ; }
+   |   --------------------
+   |   |      |
+   |   |      in this macro invocation (#4)
    |   in this expansion of `bar!` (#3)
    | 
   ::: <::ping::ping macros>:1:1
    |
-LL |   (  ) => { pong ! (  ) ; }
-   |   -------------------------
-   |   |         |
-   |   |         in this macro invocation (#5)
+LL |   () => {pong ! () ; }
+   |   --------------------
+   |   |      |
+   |   |      in this macro invocation (#5)
    |   in this expansion of `ping!` (#4)
 
 error: aborting due to 3 previous errors
index ebfed41bc280ee52fbf59e69048ea5763b7c4602..545ed33cd10be2e34c79efc321727752cdafbafb 100644 (file)
@@ -5,5 +5,5 @@ LL |     println!("Hello, World!");
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: expanding `println! { "Hello, World!" }`
-   = note: to `{ $crate :: io :: _print ( format_args_nl ! ( "Hello, World!" ) ) ; }`
+   = note: to `{$crate :: io :: _print (format_args_nl ! ("Hello, World!")) ; }`
 
index fc05012377b2a33121d58da57683f772b1d399d9..f06e6581ff7fb0b2cb9a160a473b5d3e770522a8 100644 (file)
@@ -17,7 +17,7 @@ LL |     my_faulty_macro!();
    |     ^^^^^^^^^^^^^^^^^^^
    |
    = note: expanding `my_faulty_macro! {  }`
-   = note: to `my_faulty_macro ! ( bcd ) ;`
+   = note: to `my_faulty_macro ! (bcd) ;`
    = note: expanding `my_faulty_macro! { bcd }`
 
 error: recursion limit reached while expanding the macro `my_recursive_macro`
@@ -38,13 +38,13 @@ LL |     my_recursive_macro!();
    |     ^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: expanding `my_recursive_macro! {  }`
-   = note: to `my_recursive_macro ! (  ) ;`
+   = note: to `my_recursive_macro ! () ;`
    = note: expanding `my_recursive_macro! {  }`
-   = note: to `my_recursive_macro ! (  ) ;`
+   = note: to `my_recursive_macro ! () ;`
    = note: expanding `my_recursive_macro! {  }`
-   = note: to `my_recursive_macro ! (  ) ;`
+   = note: to `my_recursive_macro ! () ;`
    = note: expanding `my_recursive_macro! {  }`
-   = note: to `my_recursive_macro ! (  ) ;`
+   = note: to `my_recursive_macro ! () ;`
 
 error: aborting due to 2 previous errors
 
index b1487fcd5edbdd7b8db1619f214b78db01b78de0..b2be35e8259936e73f10c550be06c9fcc569e317 100644 (file)
@@ -1 +1 @@
-fn main (  ) { let y : u32 = "z" ; { let x : u32 = "y" ; } }
+fn main () {let y : u32 = "z" ; {let x : u32 = "y" ; } }
index 0611fcb13f267dc17f471345ce1976df45be51b0..0fe02a9a34d18ba21e9d74841d54d2db183b3bf0 100644 (file)
@@ -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",
@@ -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",
index 6c483d7a91bbab5a35fdc9d2bfe0464c766a98c6..a499e1362ec0bba94c0d17779067d9a631be7b9c 100644 (file)
@@ -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",
@@ -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",
index 3c88ee99842a26e27f2e717fc08ce453e0372c4d..72b02ebcb76f450e80ce4937ce4c59b1c197e856 100644 (file)
@@ -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",
@@ -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",
@@ -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",
@@ -120,7 +120,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
         span: #2 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",
@@ -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",
@@ -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",