From 0cdd18d0a7b9d3c52017b57eecda6e198e9512b1 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 14 Jul 2019 16:09:39 +0300 Subject: [PATCH] pprust: Support `macro` macros --- src/libsyntax/print/pprust.rs | 31 ++++++++++++++----- src/test/pretty/macro.rs | 7 +++++ .../pretty-expanded-hygiene/input.pp.rs | 2 +- 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 src/test/pretty/macro.rs diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 47985351f7d..16e0bace925 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -20,6 +20,11 @@ use syntax_pos::{DUMMY_SP, FileName, Span}; use std::borrow::Cow; +pub enum MacHeader<'a> { + Path(&'a ast::Path), + Keyword(&'static str), +} + pub enum AnnNode<'a> { Ident(&'a ast::Ident), Name(&'a ast::Name), @@ -620,7 +625,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM match attr.tokens.trees().next() { Some(TokenTree::Delimited(_, delim, tts)) => { self.print_mac_common( - Some(&attr.path), false, None, delim, tts, true, attr.span + Some(MacHeader::Path(&attr.path)), false, None, delim, tts, true, attr.span ); } tree => { @@ -706,7 +711,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM fn print_mac_common( &mut self, - path: Option<&ast::Path>, + header: Option>, has_bang: bool, ident: Option, delim: DelimToken, @@ -717,8 +722,10 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM if delim == DelimToken::Brace { self.cbox(INDENT_UNIT); } - if let Some(path) = path { - self.print_path(path, false, 0); + match header { + Some(MacHeader::Path(path)) => self.print_path(path, false, 0), + Some(MacHeader::Keyword(kw)) => self.word(kw), + None => {} } if has_bang { self.word("!"); @@ -729,7 +736,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM } match delim { DelimToken::Brace => { - if path.is_some() || has_bang || ident.is_some() { + if header.is_some() || has_bang || ident.is_some() { self.nbsp(); } self.word("{"); @@ -1357,9 +1364,11 @@ impl<'a> State<'a> { } } ast::ItemKind::MacroDef(ref macro_def) => { + let (kw, has_bang) = + if macro_def.legacy { ("macro_rules", true) } else { ("macro", false) }; self.print_mac_common( - Some(&ast::Path::from_ident(ast::Ident::with_empty_ctxt(sym::macro_rules))), - true, + Some(MacHeader::Keyword(kw)), + has_bang, Some(item.ident), DelimToken::Brace, macro_def.stream(), @@ -1754,7 +1763,13 @@ impl<'a> State<'a> { crate fn print_mac(&mut self, m: &ast::Mac) { self.print_mac_common( - Some(&m.node.path), true, None, m.node.delim.to_token(), m.node.stream(), true, m.span + Some(MacHeader::Path(&m.node.path)), + true, + None, + m.node.delim.to_token(), + m.node.stream(), + true, + m.span, ); } diff --git a/src/test/pretty/macro.rs b/src/test/pretty/macro.rs new file mode 100644 index 00000000000..39677d1dc2d --- /dev/null +++ b/src/test/pretty/macro.rs @@ -0,0 +1,7 @@ +// pp-exact + +#![feature(decl_macro)] + +macro mac { ($ arg : expr) => { $ arg + $ arg } } + +fn main() { } diff --git a/src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs b/src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs index 9d081c970da..570dece023d 100644 --- a/src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs +++ b/src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs @@ -2,7 +2,7 @@ #![feature /* 0#0 */(no_core)] #![no_core /* 0#0 */] -macro_rules /* 0#0 */! foo /* 0#0 */ { ($ x : ident) => { y + $ x } } +macro_rules! foo /* 0#0 */ { ($ x : ident) => { y + $ x } } fn bar /* 0#0 */() { let x /* 0#0 */ = 1; y /* 0#1 */ + x /* 0#0 */ }